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
{
///
/// Memory cache of shader code.
///
class ShaderCache : IDisposable
{
private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
private readonly GpuContext _context;
private readonly ShaderDumper _dumper;
private readonly Dictionary> _cpPrograms;
private readonly Dictionary> _gpPrograms;
///
/// Creates a new instance of the shader cache.
///
/// GPU context that the shader cache belongs to
public ShaderCache(GpuContext context)
{
_context = context;
_dumper = new ShaderDumper();
_cpPrograms = new Dictionary>();
_gpPrograms = new Dictionary>();
}
///
/// Gets a compute shader from the cache.
///
///
/// This automatically translates, compiles and adds the code to the cache if not present.
///
/// Current GPU state
/// GPU virtual address of the binary shader code
/// Local group size X of the computer shader
/// Local group size Y of the computer shader
/// Local group size Z of the computer shader
/// Local memory size of the compute shader
/// Shared memory size of the compute shader
/// Compiled compute shader code
public ShaderBundle GetComputeShader(
GpuState state,
ulong gpuVa,
int localSizeX,
int localSizeY,
int localSizeZ,
int localMemorySize,
int sharedMemorySize)
{
bool isCached = _cpPrograms.TryGetValue(gpuVa, out List 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 }, null);
ShaderBundle cpShader = new ShaderBundle(hostProgram, shader);
if (!isCached)
{
list = new List();
_cpPrograms.Add(gpuVa, list);
}
list.Add(cpShader);
return cpShader;
}
///
/// Gets a graphics shader program from the shader cache.
/// This includes all the specified shader stages.
///
///
/// This automatically translates, compiles and adds the code to the cache if not present.
///
/// Current GPU state
/// Addresses of the shaders for each stage
/// Compiled graphics shader code
public ShaderBundle GetGraphicsShader(GpuState state, ShaderAddresses addresses)
{
bool isCached = _gpPrograms.TryGetValue(addresses, out List 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 hostShaders = new List();
for (int stage = 0; stage < Constants.ShaderStages; stage++)
{
ShaderProgram program = shaders[stage]?.Program;
if (program == null)
{
continue;
}
var tfd = GetTransformFeedbackDescriptors(state);
IShader hostShader = _context.Renderer.CompileShader(program);
shaders[stage].HostShader = hostShader;
hostShaders.Add(hostShader);
}
IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), GetTransformFeedbackDescriptors(state));
ShaderBundle gpShaders = new ShaderBundle(hostProgram, shaders);
if (!isCached)
{
list = new List();
_gpPrograms.Add(addresses, list);
}
list.Add(gpShaders);
return gpShaders;
}
///
/// Gets transform feedback state from the current GPU state.
///
/// Current GPU state
/// Four transform feedback descriptors for the enabled TFBs, or null if TFB is disabled
private TransformFeedbackDescriptor[] GetTransformFeedbackDescriptors(GpuState state)
{
bool tfEnable = state.Get(MethodOffset.TfEnable);
if (!tfEnable)
{
return null;
}
TransformFeedbackDescriptor[] descs = new TransformFeedbackDescriptor[Constants.TotalTransformFeedbackBuffers];
for (int i = 0; i < Constants.TotalTransformFeedbackBuffers; i++)
{
var tf = state.Get(MethodOffset.TfState, i);
int length = (int)Math.Min((uint)tf.VaryingsCount, 0x80);
var varyingLocations = state.GetSpan(MethodOffset.TfVaryingLocations + i * 0x80, length).ToArray();
descs[i] = new TransformFeedbackDescriptor(tf.BufferIndex, tf.Stride, varyingLocations);
}
return descs;
}
///
/// Checks if compute shader code in memory is equal to the cached shader.
///
/// Cached compute shader
/// GPU virtual address of the shader code in memory
/// True if the code is different, false otherwise
private bool IsShaderEqual(ShaderBundle cpShader, ulong gpuVa)
{
return IsShaderEqual(cpShader.Shaders[0], gpuVa);
}
///
/// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
///
/// Cached graphics shaders
/// GPU virtual addresses of all enabled shader stages
/// True if the code is different, false otherwise
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;
}
///
/// Checks if the code of the specified cached shader is different from the code in memory.
///
/// Cached shader to compare with
/// GPU virtual address of the binary shader code
/// Optional GPU virtual address of the "Vertex A" binary shader code
/// True if the code is different, false otherwise
private bool IsShaderEqual(ShaderCodeHolder shader, ulong gpuVa, ulong gpuVaA = 0)
{
if (shader == null)
{
return true;
}
ReadOnlySpan 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;
}
///
/// Translates the binary Maxwell shader code to something that the host API accepts.
///
/// Current GPU state
/// GPU virtual address of the binary shader code
/// Local group size X of the computer shader
/// Local group size Y of the computer shader
/// Local group size Z of the computer shader
/// Local memory size of the compute shader
/// Shared memory size of the compute shader
/// Compiled compute shader code
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);
}
///
/// Translates the binary Maxwell shader code to something that the host API accepts.
///
///
/// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
///
/// Current GPU state
/// Shader stage
/// GPU virtual address of the shader code
/// Optional GPU virtual address of the "Vertex A" shader code
/// Compiled graphics shader code
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);
}
}
///
/// Disposes the shader cache, deleting all the cached shaders.
/// It's an error to use the shader cache after disposal.
///
public void Dispose()
{
foreach (List list in _cpPrograms.Values)
{
foreach (ShaderBundle bundle in list)
{
bundle.Dispose();
}
}
foreach (List list in _gpPrograms.Values)
{
foreach (ShaderBundle bundle in list)
{
bundle.Dispose();
}
}
}
}
}