2020-01-01 16:39:09 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-11-14 19:26:40 +01:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2019-12-16 05:59:46 +01:00
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
2019-11-14 19:26:40 +01:00
|
|
|
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
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
using TextureDescriptor = Image.TextureDescriptor;
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Memory cache of shader code.
|
|
|
|
/// </summary>
|
2019-12-31 23:09:49 +01:00
|
|
|
class ShaderCache : IDisposable
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
private const int MaxProgramSize = 0x100000;
|
|
|
|
|
2019-12-16 05:59:46 +01:00
|
|
|
private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
|
|
|
|
|
2019-11-14 19:26:40 +01:00
|
|
|
private GpuContext _context;
|
|
|
|
|
|
|
|
private ShaderDumper _dumper;
|
|
|
|
|
|
|
|
private Dictionary<ulong, List<ComputeShader>> _cpPrograms;
|
|
|
|
|
|
|
|
private Dictionary<ShaderAddresses, List<GraphicsShader>> _gpPrograms;
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the shader cache.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU context that the shader cache belongs to</param>
|
2019-11-14 19:26:40 +01:00
|
|
|
public ShaderCache(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
_dumper = new ShaderDumper();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
_cpPrograms = new Dictionary<ulong, List<ComputeShader>>();
|
|
|
|
|
|
|
|
_gpPrograms = new Dictionary<ShaderAddresses, List<GraphicsShader>>();
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a compute shader from the cache.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// This automatically translates, compiles and adds the code to the cache if not present.
|
|
|
|
/// </remarks>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <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>
|
2020-02-02 04:25:52 +01:00
|
|
|
/// <param name="localMemorySize">Local memory size of the compute shader</param>
|
|
|
|
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <returns>Compiled compute shader code</returns>
|
2020-02-02 04:25:52 +01:00
|
|
|
public ComputeShader GetComputeShader(
|
|
|
|
ulong gpuVa,
|
|
|
|
int localSizeX,
|
|
|
|
int localSizeY,
|
|
|
|
int localSizeZ,
|
|
|
|
int localMemorySize,
|
|
|
|
int sharedMemorySize)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ComputeShader> list);
|
|
|
|
|
|
|
|
if (isCached)
|
|
|
|
{
|
|
|
|
foreach (ComputeShader cachedCpShader in list)
|
|
|
|
{
|
|
|
|
if (!IsShaderDifferent(cachedCpShader, gpuVa))
|
|
|
|
{
|
|
|
|
return cachedCpShader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
CachedShader shader = TranslateComputeShader(
|
|
|
|
gpuVa,
|
|
|
|
localSizeX,
|
|
|
|
localSizeY,
|
|
|
|
localSizeZ,
|
|
|
|
localMemorySize,
|
|
|
|
sharedMemorySize);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-05 17:40:21 +01:00
|
|
|
shader.HostShader = _context.Renderer.CompileShader(shader.Program);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-05 17:40:21 +01:00
|
|
|
IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader });
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
ComputeShader cpShader = new ComputeShader(hostProgram, shader);
|
|
|
|
|
|
|
|
if (!isCached)
|
|
|
|
{
|
|
|
|
list = new List<ComputeShader>();
|
|
|
|
|
|
|
|
_cpPrograms.Add(gpuVa, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(cpShader);
|
|
|
|
|
|
|
|
return cpShader;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a graphics shader program from the shader cache.
|
|
|
|
/// This includes all the specified shader stages.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// This automatically translates, compiles and adds the code to the cache if not present.
|
|
|
|
/// </remarks>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="addresses">Addresses of the shaders for each stage</param>
|
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
public GraphicsShader GetGraphicsShader(GpuState state, ShaderAddresses addresses)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
bool isCached = _gpPrograms.TryGetValue(addresses, out List<GraphicsShader> list);
|
|
|
|
|
|
|
|
if (isCached)
|
|
|
|
{
|
|
|
|
foreach (GraphicsShader cachedGpShaders in list)
|
|
|
|
{
|
|
|
|
if (!IsShaderDifferent(cachedGpShaders, addresses))
|
|
|
|
{
|
|
|
|
return cachedGpShaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsShader gpShaders = new GraphicsShader();
|
|
|
|
|
|
|
|
if (addresses.VertexA != 0)
|
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
gpShaders.Shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
gpShaders.Shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 23:09:49 +01:00
|
|
|
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);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
BackpropQualifiers(gpShaders);
|
|
|
|
|
|
|
|
List<IShader> hostShaders = new List<IShader>();
|
|
|
|
|
2019-12-31 23:09:49 +01:00
|
|
|
for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
ShaderProgram program = gpShaders.Shaders[stage]?.Program;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
if (program == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IShader hostShader = _context.Renderer.CompileShader(program);
|
|
|
|
|
2019-12-31 23:09:49 +01:00
|
|
|
gpShaders.Shaders[stage].HostShader = hostShader;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
hostShaders.Add(hostShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpShaders.HostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray());
|
|
|
|
|
|
|
|
if (!isCached)
|
|
|
|
{
|
|
|
|
list = new List<GraphicsShader>();
|
|
|
|
|
|
|
|
_gpPrograms.Add(addresses, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(gpShaders);
|
|
|
|
|
|
|
|
return gpShaders;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if compute shader code in memory is different from 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>
|
2019-11-14 19:26:40 +01:00
|
|
|
private bool IsShaderDifferent(ComputeShader cpShader, ulong gpuVa)
|
|
|
|
{
|
|
|
|
return IsShaderDifferent(cpShader.Shader, gpuVa);
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if graphics shader code from all stages in memory is different from 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>
|
2019-11-14 19:26:40 +01:00
|
|
|
private bool IsShaderDifferent(GraphicsShader gpShaders, ShaderAddresses addresses)
|
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
CachedShader shader = gpShaders.Shaders[stage];
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <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>
|
|
|
|
/// <returns>True if the code is different, false otherwise</returns>
|
2019-11-14 19:26:40 +01:00
|
|
|
private bool IsShaderDifferent(CachedShader shader, ulong gpuVa)
|
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
if (shader == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:49:48 +01:00
|
|
|
for (int index = 0; index < shader.Code.Length; index++)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2019-11-25 19:49:48 +01:00
|
|
|
if (_context.MemoryAccessor.ReadInt32(gpuVa + (ulong)index * 4) != shader.Code[index])
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Translates the binary Maxwell shader code to something that the host API accepts.
|
|
|
|
/// </summary>
|
|
|
|
/// <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>
|
2020-02-02 04:25:52 +01:00
|
|
|
/// <param name="localMemorySize">Local memory size of the compute shader</param>
|
|
|
|
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <returns>Compiled compute shader code</returns>
|
2020-02-02 04:25:52 +01:00
|
|
|
private CachedShader TranslateComputeShader(
|
|
|
|
ulong gpuVa,
|
|
|
|
int localSizeX,
|
|
|
|
int localSizeY,
|
|
|
|
int localSizeZ,
|
|
|
|
int localMemorySize,
|
|
|
|
int sharedMemorySize)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
if (gpuVa == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
int QueryInfo(QueryInfoName info, int index)
|
2019-12-16 05:59:46 +01:00
|
|
|
{
|
2020-01-01 16:39:09 +01:00
|
|
|
return info switch
|
2019-12-16 05:59:46 +01:00
|
|
|
{
|
2020-01-01 16:39:09 +01:00
|
|
|
QueryInfoName.ComputeLocalSizeX => localSizeX,
|
|
|
|
QueryInfoName.ComputeLocalSizeY => localSizeY,
|
|
|
|
QueryInfoName.ComputeLocalSizeZ => localSizeZ,
|
2020-02-02 04:25:52 +01:00
|
|
|
QueryInfoName.ComputeLocalMemorySize => localMemorySize,
|
2020-01-01 16:39:09 +01:00
|
|
|
QueryInfoName.ComputeSharedMemorySize => sharedMemorySize,
|
|
|
|
_ => QueryInfoCommon(info)
|
|
|
|
};
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
|
2019-12-16 05:59:46 +01:00
|
|
|
|
|
|
|
ShaderProgram program;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-13 00:27:50 +01:00
|
|
|
ReadOnlySpan<byte> code = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
program = Translator.Translate(code, callbacks, DefaultFlags | TranslationFlags.Compute);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
int[] codeCached = MemoryMarshal.Cast<byte, int>(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);
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Translates the binary Maxwell shader code to something that the host API accepts.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
|
|
|
|
/// </remarks>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <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>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
private CachedShader TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
if (gpuVa == 0)
|
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
return null;
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
int QueryInfo(QueryInfoName info, int index)
|
2019-12-16 05:59:46 +01:00
|
|
|
{
|
2020-01-01 16:39:09 +01:00
|
|
|
return info switch
|
2019-12-16 05:59:46 +01:00
|
|
|
{
|
2020-01-01 16:39:09 +01:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|
2019-12-16 05:59:46 +01:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
|
2019-12-16 05:59:46 +01:00
|
|
|
|
2019-11-14 19:26:40 +01:00
|
|
|
ShaderProgram program;
|
|
|
|
|
|
|
|
int[] codeCached = null;
|
|
|
|
|
|
|
|
if (gpuVaA != 0)
|
|
|
|
{
|
2020-01-13 00:27:50 +01:00
|
|
|
ReadOnlySpan<byte> codeA = _context.MemoryAccessor.GetSpan(gpuVaA, MaxProgramSize);
|
|
|
|
ReadOnlySpan<byte> codeB = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
program = Translator.Translate(codeA, codeB, callbacks, DefaultFlags);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2019-12-16 05:59:46 +01:00
|
|
|
// TODO: We should also take "codeA" into account.
|
2019-11-14 19:26:40 +01:00
|
|
|
codeCached = MemoryMarshal.Cast<byte, int>(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
|
|
|
|
{
|
2020-01-13 00:27:50 +01:00
|
|
|
ReadOnlySpan<byte> code = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
program = Translator.Translate(code, callbacks, DefaultFlags);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
codeCached = MemoryMarshal.Cast<byte, int>(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);
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// 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).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="program">Graphics shader cached code</param>
|
2019-11-14 19:26:40 +01:00
|
|
|
private void BackpropQualifiers(GraphicsShader program)
|
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
ShaderProgram fragmentShader = program.Shaders[4]?.Program;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
bool isFirst = true;
|
|
|
|
|
|
|
|
for (int stage = 3; stage >= 0; stage--)
|
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
if (program.Shaders[stage] == null)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-01-05 17:40:21 +01:00
|
|
|
if (isFirst && !string.IsNullOrEmpty(iq))
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
program.Shaders[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr}", iq);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
program.Shaders[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr} ", string.Empty);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 03:53:09 +01:00
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the primitive topology for the current draw.
|
|
|
|
/// This is required by geometry shaders.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Primitive topology</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// 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.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="stageIndex">Index of the shader stage</param>
|
|
|
|
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
|
|
|
|
/// <returns>True if the texture is a buffer texture, false otherwise</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int index)
|
|
|
|
{
|
|
|
|
return GetTextureDescriptor(state, stageIndex, index).UnpackTextureTarget() == TextureTarget.TextureBuffer;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// 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.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="stageIndex">Index of the shader stage</param>
|
|
|
|
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
|
|
|
|
/// <returns>True if the texture is a rectangle texture, false otherwise</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int index)
|
2019-12-01 03:53:09 +01:00
|
|
|
{
|
2019-12-16 05:59:46 +01:00
|
|
|
var descriptor = GetTextureDescriptor(state, stageIndex, index);
|
|
|
|
|
|
|
|
TextureTarget target = descriptor.UnpackTextureTarget();
|
|
|
|
|
|
|
|
bool is2DTexture = target == TextureTarget.Texture2D ||
|
|
|
|
target == TextureTarget.Texture2DRect;
|
|
|
|
|
|
|
|
return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the texture descriptor for a given texture on the pool.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="stageIndex">Index of the shader stage</param>
|
|
|
|
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
|
|
|
|
/// <returns>Texture descriptor</returns>
|
2019-12-29 18:41:50 +01:00
|
|
|
private TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int index)
|
2019-12-16 05:59:46 +01:00
|
|
|
{
|
|
|
|
return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, index);
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns information required by both compute and graphics shader compilation.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="info">Information queried</param>
|
|
|
|
/// <returns>Requested information</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
private int QueryInfoCommon(QueryInfoName info)
|
|
|
|
{
|
2020-01-01 16:39:09 +01:00
|
|
|
return info switch
|
2019-12-16 05:59:46 +01:00
|
|
|
{
|
2020-01-01 16:39:09 +01:00
|
|
|
QueryInfoName.StorageBufferOffsetAlignment => _context.Capabilities.StorageBufferOffsetAlignment,
|
|
|
|
QueryInfoName.SupportsNonConstantTextureOffset => Convert.ToInt32(_context.Capabilities.SupportsNonConstantTextureOffset),
|
|
|
|
_ => 0
|
|
|
|
};
|
|
|
|
}
|
2019-12-16 05:59:46 +01:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Prints a warning from the shader code translator.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">Warning message</param>
|
|
|
|
private static void PrintLog(string message)
|
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.Gpu, $"Shader translator: {message}");
|
2019-12-01 03:53:09 +01:00
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
|
|
|
|
/// <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<ComputeShader> list in _cpPrograms.Values)
|
|
|
|
{
|
|
|
|
foreach (ComputeShader shader in list)
|
|
|
|
{
|
|
|
|
shader.HostProgram.Dispose();
|
2020-01-05 17:40:21 +01:00
|
|
|
shader.Shader?.HostShader.Dispose();
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (List<GraphicsShader> list in _gpPrograms.Values)
|
|
|
|
{
|
|
|
|
foreach (GraphicsShader shader in list)
|
|
|
|
{
|
|
|
|
shader.HostProgram.Dispose();
|
|
|
|
|
|
|
|
foreach (CachedShader cachedShader in shader.Shaders)
|
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
cachedShader?.HostShader.Dispose();
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
}
|