using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Image;
using Ryujinx.Graphics.Gpu.State;
using Ryujinx.Graphics.Shader;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Gpu.Shader
{
using TextureDescriptor = Image.TextureDescriptor;
///
/// Memory cache of shader code.
///
class ShaderCache : IDisposable
{
private const int MaxProgramSize = 0x100000;
private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
private GpuContext _context;
private ShaderDumper _dumper;
private Dictionary> _cpPrograms;
private 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.
///
/// GPU virtual address of the binary shader code
/// Shared memory size of the compute shader
/// Local group size X of the computer shader
/// Local group size Y of the computer shader
/// Local group size Z of the computer shader
/// Compiled compute shader code
public ComputeShader GetComputeShader(ulong gpuVa, int sharedMemorySize, int localSizeX, int localSizeY, int localSizeZ)
{
bool isCached = _cpPrograms.TryGetValue(gpuVa, out List list);
if (isCached)
{
foreach (ComputeShader cachedCpShader in list)
{
if (!IsShaderDifferent(cachedCpShader, gpuVa))
{
return cachedCpShader;
}
}
}
CachedShader shader = TranslateComputeShader(gpuVa, sharedMemorySize, localSizeX, localSizeY, localSizeZ);
shader.HostShader = _context.Renderer.CompileShader(shader.Program);
IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader });
ComputeShader cpShader = new ComputeShader(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 GraphicsShader GetGraphicsShader(GpuState state, ShaderAddresses addresses)
{
bool isCached = _gpPrograms.TryGetValue(addresses, out List list);
if (isCached)
{
foreach (GraphicsShader cachedGpShaders in list)
{
if (!IsShaderDifferent(cachedGpShaders, addresses))
{
return cachedGpShaders;
}
}
}
GraphicsShader gpShaders = new GraphicsShader();
if (addresses.VertexA != 0)
{
gpShaders.Shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
}
else
{
gpShaders.Shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex);
}
gpShaders.Shaders[1] = TranslateGraphicsShader(state, ShaderStage.TessellationControl, addresses.TessControl);
gpShaders.Shaders[2] = TranslateGraphicsShader(state, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
gpShaders.Shaders[3] = TranslateGraphicsShader(state, ShaderStage.Geometry, addresses.Geometry);
gpShaders.Shaders[4] = TranslateGraphicsShader(state, ShaderStage.Fragment, addresses.Fragment);
BackpropQualifiers(gpShaders);
List hostShaders = new List();
for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
{
ShaderProgram program = gpShaders.Shaders[stage]?.Program;
if (program == null)
{
continue;
}
IShader hostShader = _context.Renderer.CompileShader(program);
gpShaders.Shaders[stage].HostShader = hostShader;
hostShaders.Add(hostShader);
}
gpShaders.HostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray());
if (!isCached)
{
list = new List();
_gpPrograms.Add(addresses, list);
}
list.Add(gpShaders);
return gpShaders;
}
///
/// Checks if compute shader code in memory is different from 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 IsShaderDifferent(ComputeShader cpShader, ulong gpuVa)
{
return IsShaderDifferent(cpShader.Shader, gpuVa);
}
///
/// Checks if graphics shader code from all stages in memory is different from the cached shaders.
///
/// Cached graphics shaders
/// GPU virtual addresses of all enabled shader stages
/// True if the code is different, false otherwise
private bool IsShaderDifferent(GraphicsShader gpShaders, ShaderAddresses addresses)
{
for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
{
CachedShader 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 (IsShaderDifferent(shader, gpuVa))
{
return true;
}
}
return false;
}
///
/// 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
/// True if the code is different, false otherwise
private bool IsShaderDifferent(CachedShader shader, ulong gpuVa)
{
if (shader == null)
{
return false;
}
for (int index = 0; index < shader.Code.Length; index++)
{
if (_context.MemoryAccessor.ReadInt32(gpuVa + (ulong)index * 4) != shader.Code[index])
{
return true;
}
}
return false;
}
///
/// Translates the binary Maxwell shader code to something that the host API accepts.
///
/// GPU virtual address of the binary shader code
/// Shared memory size of the compute shader
/// Local group size X of the computer shader
/// Local group size Y of the computer shader
/// Local group size Z of the computer shader
/// Compiled compute shader code
private CachedShader TranslateComputeShader(ulong gpuVa, int sharedMemorySize, int localSizeX, int localSizeY, int localSizeZ)
{
if (gpuVa == 0)
{
return null;
}
int QueryInfo(QueryInfoName info, int index)
{
return info switch
{
QueryInfoName.ComputeLocalSizeX => localSizeX,
QueryInfoName.ComputeLocalSizeY => localSizeY,
QueryInfoName.ComputeLocalSizeZ => localSizeZ,
QueryInfoName.ComputeSharedMemorySize => sharedMemorySize,
_ => QueryInfoCommon(info)
};
}
TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
ShaderProgram program;
Span code = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
program = Translator.Translate(code, callbacks, DefaultFlags | TranslationFlags.Compute);
int[] codeCached = MemoryMarshal.Cast(code.Slice(0, program.Size)).ToArray();
_dumper.Dump(code, compute: true, out string fullPath, out string codePath);
if (fullPath != null && codePath != null)
{
program.Prepend("// " + codePath);
program.Prepend("// " + fullPath);
}
return new CachedShader(program, codeCached);
}
///
/// 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 CachedShader TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0)
{
if (gpuVa == 0)
{
return null;
}
int QueryInfo(QueryInfoName info, int index)
{
return info switch
{
QueryInfoName.IsTextureBuffer => Convert.ToInt32(QueryIsTextureBuffer(state, (int)stage - 1, index)),
QueryInfoName.IsTextureRectangle => Convert.ToInt32(QueryIsTextureRectangle(state, (int)stage - 1, index)),
QueryInfoName.PrimitiveTopology => (int)GetPrimitiveTopology(),
_ => QueryInfoCommon(info)
};
}
TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
ShaderProgram program;
int[] codeCached = null;
if (gpuVaA != 0)
{
Span codeA = _context.MemoryAccessor.Read(gpuVaA, MaxProgramSize);
Span codeB = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
program = Translator.Translate(codeA, codeB, callbacks, DefaultFlags);
// TODO: We should also take "codeA" into account.
codeCached = MemoryMarshal.Cast(codeB.Slice(0, program.Size)).ToArray();
_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);
}
}
else
{
Span code = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
program = Translator.Translate(code, callbacks, DefaultFlags);
codeCached = MemoryMarshal.Cast(code.Slice(0, program.Size)).ToArray();
_dumper.Dump(code, compute: false, out string fullPath, out string codePath);
if (fullPath != null && codePath != null)
{
program.Prepend("// " + codePath);
program.Prepend("// " + fullPath);
}
}
ulong address = _context.MemoryManager.Translate(gpuVa);
return new CachedShader(program, codeCached);
}
///
/// Performs backwards propagation of interpolation qualifiers or later shader stages input,
/// to ealier shader stages output.
/// This is required by older versions of OpenGL (pre-4.3).
///
/// Graphics shader cached code
private void BackpropQualifiers(GraphicsShader program)
{
ShaderProgram fragmentShader = program.Shaders[4]?.Program;
bool isFirst = true;
for (int stage = 3; stage >= 0; stage--)
{
if (program.Shaders[stage] == null)
{
continue;
}
// We need to iterate backwards, since we do name replacement,
// and it would otherwise replace a subset of the longer names.
for (int attr = 31; attr >= 0; attr--)
{
string iq = fragmentShader?.Info.InterpolationQualifiers[attr].ToGlslQualifier() ?? string.Empty;
if (isFirst && !string.IsNullOrEmpty(iq))
{
program.Shaders[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr}", iq);
}
else
{
program.Shaders[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr} ", string.Empty);
}
}
isFirst = false;
}
}
///
/// Gets the primitive topology for the current draw.
/// This is required by geometry shaders.
///
/// Primitive topology
private InputTopology GetPrimitiveTopology()
{
switch (_context.Methods.PrimitiveType)
{
case PrimitiveType.Points:
return InputTopology.Points;
case PrimitiveType.Lines:
case PrimitiveType.LineLoop:
case PrimitiveType.LineStrip:
return InputTopology.Lines;
case PrimitiveType.LinesAdjacency:
case PrimitiveType.LineStripAdjacency:
return InputTopology.LinesAdjacency;
case PrimitiveType.Triangles:
case PrimitiveType.TriangleStrip:
case PrimitiveType.TriangleFan:
return InputTopology.Triangles;
case PrimitiveType.TrianglesAdjacency:
case PrimitiveType.TriangleStripAdjacency:
return InputTopology.TrianglesAdjacency;
}
return InputTopology.Points;
}
///
/// Check if the target of a given texture is texture buffer.
/// This is required as 1D textures and buffer textures shares the same sampler type on binary shader code,
/// but not on GLSL.
///
/// Current GPU state
/// Index of the shader stage
/// Index of the texture (this is the shader "fake" handle)
/// True if the texture is a buffer texture, false otherwise
private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int index)
{
return GetTextureDescriptor(state, stageIndex, index).UnpackTextureTarget() == TextureTarget.TextureBuffer;
}
///
/// Check if the target of a given texture is texture rectangle.
/// This is required as 2D textures and rectangle textures shares the same sampler type on binary shader code,
/// but not on GLSL.
///
/// Current GPU state
/// Index of the shader stage
/// Index of the texture (this is the shader "fake" handle)
/// True if the texture is a rectangle texture, false otherwise
private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int index)
{
var descriptor = GetTextureDescriptor(state, stageIndex, index);
TextureTarget target = descriptor.UnpackTextureTarget();
bool is2DTexture = target == TextureTarget.Texture2D ||
target == TextureTarget.Texture2DRect;
return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
}
///
/// Gets the texture descriptor for a given texture on the pool.
///
/// Current GPU state
/// Index of the shader stage
/// Index of the texture (this is the shader "fake" handle)
/// Texture descriptor
private TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int index)
{
return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, index);
}
///
/// Returns information required by both compute and graphics shader compilation.
///
/// Information queried
/// Requested information
private int QueryInfoCommon(QueryInfoName info)
{
return info switch
{
QueryInfoName.StorageBufferOffsetAlignment => _context.Capabilities.StorageBufferOffsetAlignment,
QueryInfoName.SupportsNonConstantTextureOffset => Convert.ToInt32(_context.Capabilities.SupportsNonConstantTextureOffset),
_ => 0
};
}
///
/// Prints a warning from the shader code translator.
///
/// Warning message
private static void PrintLog(string message)
{
Logger.PrintWarning(LogClass.Gpu, $"Shader translator: {message}");
}
///
/// 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 (ComputeShader shader in list)
{
shader.HostProgram.Dispose();
shader.Shader?.HostShader.Dispose();
}
}
foreach (List list in _gpPrograms.Values)
{
foreach (GraphicsShader shader in list)
{
shader.HostProgram.Dispose();
foreach (CachedShader cachedShader in shader.Shaders)
{
cachedShader?.HostShader.Dispose();
}
}
}
}
}
}