| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using Ryujinx.Graphics.GAL;
- using Ryujinx.Graphics.Gpu.State;
- using Ryujinx.Graphics.Shader;
- using Ryujinx.Graphics.Shader.Translation;
- using System;
- using System.Collections.Generic;
- namespace Ryujinx.Graphics.Gpu.Shader
- {
- /// <summary>
- /// Memory cache of shader code.
- /// </summary>
- class ShaderCache : IDisposable
- {
- private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
- private readonly GpuContext _context;
- private readonly ShaderDumper _dumper;
- private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
- private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
- /// <summary>
- /// Creates a new instance of the shader cache.
- /// </summary>
- /// <param name="context">GPU context that the shader cache belongs to</param>
- public ShaderCache(GpuContext context)
- {
- _context = context;
- _dumper = new ShaderDumper();
- _cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
- _gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
- }
- /// <summary>
- /// Gets a compute shader from the cache.
- /// </summary>
- /// <remarks>
- /// This automatically translates, compiles and adds the code to the cache if not present.
- /// </remarks>
- /// <param name="state">Current GPU state</param>
- /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
- /// <param name="localSizeX">Local group size X of the computer shader</param>
- /// <param name="localSizeY">Local group size Y of the computer shader</param>
- /// <param name="localSizeZ">Local group size Z of the computer shader</param>
- /// <param name="localMemorySize">Local memory size of the compute shader</param>
- /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
- /// <returns>Compiled compute shader code</returns>
- public ShaderBundle GetComputeShader(
- GpuState state,
- ulong gpuVa,
- int localSizeX,
- int localSizeY,
- int localSizeZ,
- int localMemorySize,
- int sharedMemorySize)
- {
- bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ShaderBundle> list);
- if (isCached)
- {
- foreach (ShaderBundle cachedCpShader in list)
- {
- if (IsShaderEqual(cachedCpShader, gpuVa))
- {
- return cachedCpShader;
- }
- }
- }
- ShaderCodeHolder shader = TranslateComputeShader(
- state,
- gpuVa,
- localSizeX,
- localSizeY,
- localSizeZ,
- localMemorySize,
- sharedMemorySize);
- shader.HostShader = _context.Renderer.CompileShader(shader.Program);
- IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader });
- ShaderBundle cpShader = new ShaderBundle(hostProgram, shader);
- if (!isCached)
- {
- list = new List<ShaderBundle>();
- _cpPrograms.Add(gpuVa, list);
- }
- list.Add(cpShader);
- return cpShader;
- }
- /// <summary>
- /// Gets a graphics shader program from the shader cache.
- /// This includes all the specified shader stages.
- /// </summary>
- /// <remarks>
- /// This automatically translates, compiles and adds the code to the cache if not present.
- /// </remarks>
- /// <param name="state">Current GPU state</param>
- /// <param name="addresses">Addresses of the shaders for each stage</param>
- /// <returns>Compiled graphics shader code</returns>
- public ShaderBundle GetGraphicsShader(GpuState state, ShaderAddresses addresses)
- {
- bool isCached = _gpPrograms.TryGetValue(addresses, out List<ShaderBundle> list);
- if (isCached)
- {
- foreach (ShaderBundle cachedGpShaders in list)
- {
- if (IsShaderEqual(cachedGpShaders, addresses))
- {
- return cachedGpShaders;
- }
- }
- }
- ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
- if (addresses.VertexA != 0)
- {
- shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
- }
- else
- {
- shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex);
- }
- shaders[1] = TranslateGraphicsShader(state, ShaderStage.TessellationControl, addresses.TessControl);
- shaders[2] = TranslateGraphicsShader(state, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
- shaders[3] = TranslateGraphicsShader(state, ShaderStage.Geometry, addresses.Geometry);
- shaders[4] = TranslateGraphicsShader(state, ShaderStage.Fragment, addresses.Fragment);
- List<IShader> hostShaders = new List<IShader>();
- for (int stage = 0; stage < Constants.ShaderStages; stage++)
- {
- ShaderProgram program = shaders[stage]?.Program;
- if (program == null)
- {
- continue;
- }
- IShader hostShader = _context.Renderer.CompileShader(program);
- shaders[stage].HostShader = hostShader;
- hostShaders.Add(hostShader);
- }
- IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray());
- ShaderBundle gpShaders = new ShaderBundle(hostProgram, shaders);
- if (!isCached)
- {
- list = new List<ShaderBundle>();
- _gpPrograms.Add(addresses, list);
- }
- list.Add(gpShaders);
- return gpShaders;
- }
- /// <summary>
- /// Checks if compute shader code in memory is equal to the cached shader.
- /// </summary>
- /// <param name="cpShader">Cached compute shader</param>
- /// <param name="gpuVa">GPU virtual address of the shader code in memory</param>
- /// <returns>True if the code is different, false otherwise</returns>
- private bool IsShaderEqual(ShaderBundle cpShader, ulong gpuVa)
- {
- return IsShaderEqual(cpShader.Shaders[0], gpuVa);
- }
- /// <summary>
- /// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
- /// </summary>
- /// <param name="gpShaders">Cached graphics shaders</param>
- /// <param name="addresses">GPU virtual addresses of all enabled shader stages</param>
- /// <returns>True if the code is different, false otherwise</returns>
- private bool IsShaderEqual(ShaderBundle gpShaders, ShaderAddresses addresses)
- {
- for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
- {
- ShaderCodeHolder shader = gpShaders.Shaders[stage];
- ulong gpuVa = 0;
- switch (stage)
- {
- case 0: gpuVa = addresses.Vertex; break;
- case 1: gpuVa = addresses.TessControl; break;
- case 2: gpuVa = addresses.TessEvaluation; break;
- case 3: gpuVa = addresses.Geometry; break;
- case 4: gpuVa = addresses.Fragment; break;
- }
- if (!IsShaderEqual(shader, gpuVa, addresses.VertexA))
- {
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// Checks if the code of the specified cached shader is different from the code in memory.
- /// </summary>
- /// <param name="shader">Cached shader to compare with</param>
- /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
- /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" binary shader code</param>
- /// <returns>True if the code is different, false otherwise</returns>
- private bool IsShaderEqual(ShaderCodeHolder shader, ulong gpuVa, ulong gpuVaA = 0)
- {
- if (shader == null)
- {
- return true;
- }
- ReadOnlySpan<byte> memoryCode = _context.MemoryAccessor.GetSpan(gpuVa, shader.Code.Length);
- bool equals = memoryCode.SequenceEqual(shader.Code);
- if (equals && shader.Code2 != null)
- {
- memoryCode = _context.MemoryAccessor.GetSpan(gpuVaA, shader.Code2.Length);
- equals = memoryCode.SequenceEqual(shader.Code2);
- }
- return equals;
- }
- /// <summary>
- /// Translates the binary Maxwell shader code to something that the host API accepts.
- /// </summary>
- /// <param name="state">Current GPU state</param>
- /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
- /// <param name="localSizeX">Local group size X of the computer shader</param>
- /// <param name="localSizeY">Local group size Y of the computer shader</param>
- /// <param name="localSizeZ">Local group size Z of the computer shader</param>
- /// <param name="localMemorySize">Local memory size of the compute shader</param>
- /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
- /// <returns>Compiled compute shader code</returns>
- private ShaderCodeHolder TranslateComputeShader(
- GpuState state,
- ulong gpuVa,
- int localSizeX,
- int localSizeY,
- int localSizeZ,
- int localMemorySize,
- int sharedMemorySize)
- {
- if (gpuVa == 0)
- {
- return null;
- }
- GpuAccessor gpuAccessor = new GpuAccessor(_context, state, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
- ShaderProgram program;
- program = Translator.Translate(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
- byte[] code = _context.MemoryAccessor.ReadBytes(gpuVa, program.Size);
- _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
- if (fullPath != null && codePath != null)
- {
- program.Prepend("// " + codePath);
- program.Prepend("// " + fullPath);
- }
- return new ShaderCodeHolder(program, code);
- }
- /// <summary>
- /// Translates the binary Maxwell shader code to something that the host API accepts.
- /// </summary>
- /// <remarks>
- /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
- /// </remarks>
- /// <param name="state">Current GPU state</param>
- /// <param name="stage">Shader stage</param>
- /// <param name="gpuVa">GPU virtual address of the shader code</param>
- /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
- /// <returns>Compiled graphics shader code</returns>
- private ShaderCodeHolder TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0)
- {
- if (gpuVa == 0)
- {
- return null;
- }
- GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1);
- if (gpuVaA != 0)
- {
- ShaderProgram program = Translator.Translate(gpuVaA, gpuVa, gpuAccessor, DefaultFlags);
- byte[] codeA = _context.MemoryAccessor.ReadBytes(gpuVaA, program.SizeA);
- byte[] codeB = _context.MemoryAccessor.ReadBytes(gpuVa, program.Size);
- _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
- _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
- if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
- {
- program.Prepend("// " + codePathB);
- program.Prepend("// " + fullPathB);
- program.Prepend("// " + codePathA);
- program.Prepend("// " + fullPathA);
- }
- return new ShaderCodeHolder(program, codeB, codeA);
- }
- else
- {
- ShaderProgram program = Translator.Translate(gpuVa, gpuAccessor, DefaultFlags);
- byte[] code = _context.MemoryAccessor.ReadBytes(gpuVa, program.Size);
- _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
- if (fullPath != null && codePath != null)
- {
- program.Prepend("// " + codePath);
- program.Prepend("// " + fullPath);
- }
- return new ShaderCodeHolder(program, code);
- }
- }
- /// <summary>
- /// Disposes the shader cache, deleting all the cached shaders.
- /// It's an error to use the shader cache after disposal.
- /// </summary>
- public void Dispose()
- {
- foreach (List<ShaderBundle> list in _cpPrograms.Values)
- {
- foreach (ShaderBundle bundle in list)
- {
- bundle.Dispose();
- }
- }
- foreach (List<ShaderBundle> list in _gpPrograms.Values)
- {
- foreach (ShaderBundle bundle in list)
- {
- bundle.Dispose();
- }
- }
- }
- }
- }
|