Simplify handling of shader vertex A (#1999)

* Simplify handling of shader vertex A

* Theres no transformation feedback, its transform

* Merge TextureHandlesForCache
This commit is contained in:
gdkchan 2021-02-07 20:42:17 -03:00 committed by GitHub
parent 1319eda8b7
commit 4047477866
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 94 deletions

View file

@ -280,7 +280,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
} }
} }
// Transformation feedback // Transform feedback
if (tfd != null) if (tfd != null)
{ {
foreach (TransformFeedbackDescriptor transform in tfd) foreach (TransformFeedbackDescriptor transform in tfd)
@ -311,7 +311,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// <param name="data">The raw guest transform feedback descriptors</param> /// <param name="data">The raw guest transform feedback descriptors</param>
/// <param name="header">The guest shader program header</param> /// <param name="header">The guest shader program header</param>
/// <returns>The transform feedback descriptors read from guest</returns> /// <returns>The transform feedback descriptors read from guest</returns>
public static TransformFeedbackDescriptor[] ReadTransformationFeedbackInformations(ref ReadOnlySpan<byte> data, GuestShaderCacheHeader header) public static TransformFeedbackDescriptor[] ReadTransformFeedbackInformation(ref ReadOnlySpan<byte> data, GuestShaderCacheHeader header)
{ {
if (header.TransformFeedbackCount != 0) if (header.TransformFeedbackCount != 0)
{ {
@ -376,9 +376,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// <returns>Guest shader cahe entries from the runtime contexts</returns> /// <returns>Guest shader cahe entries from the runtime contexts</returns>
public static GuestShaderCacheEntry[] CreateShaderCacheEntries(MemoryManager memoryManager, ReadOnlySpan<TranslatorContext> shaderContexts) public static GuestShaderCacheEntry[] CreateShaderCacheEntries(MemoryManager memoryManager, ReadOnlySpan<TranslatorContext> shaderContexts)
{ {
GuestShaderCacheEntry[] entries = new GuestShaderCacheEntry[shaderContexts.Length]; int startIndex = shaderContexts.Length > 1 ? 1 : 0;
for (int i = 0; i < shaderContexts.Length; i++) GuestShaderCacheEntry[] entries = new GuestShaderCacheEntry[shaderContexts.Length - startIndex];
for (int i = startIndex; i < shaderContexts.Length; i++)
{ {
TranslatorContext context = shaderContexts[i]; TranslatorContext context = shaderContexts[i];
@ -387,15 +389,17 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
continue; continue;
} }
int sizeA = context.AddressA == 0 ? 0 : context.SizeA; TranslatorContext translatorContext2 = i == 1 ? shaderContexts[0] : null;
int sizeA = translatorContext2 != null ? translatorContext2.Size : 0;
byte[] code = new byte[context.Size + sizeA]; byte[] code = new byte[context.Size + sizeA];
memoryManager.GetSpan(context.Address, context.Size).CopyTo(code); memoryManager.GetSpan(context.Address, context.Size).CopyTo(code);
if (context.AddressA != 0) if (translatorContext2 != null)
{ {
memoryManager.GetSpan(context.AddressA, context.SizeA).CopyTo(code.AsSpan().Slice(context.Size, context.SizeA)); memoryManager.GetSpan(translatorContext2.Address, sizeA).CopyTo(code.AsSpan().Slice(context.Size, sizeA));
} }
GuestGpuAccessorHeader gpuAccessorHeader = CreateGuestGpuAccessorCache(context.GpuAccessor); GuestGpuAccessorHeader gpuAccessorHeader = CreateGuestGpuAccessorCache(context.GpuAccessor);
@ -421,7 +425,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
} }
} }
entries[i] = entry; entries[i - startIndex] = entry;
} }
return entries; return entries;

View file

@ -103,7 +103,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
ReadOnlySpan<GuestShaderCacheEntry> cachedShaderEntries = GuestShaderCacheEntry.Parse(ref guestProgramReadOnlySpan, out GuestShaderCacheHeader fileHeader); ReadOnlySpan<GuestShaderCacheEntry> cachedShaderEntries = GuestShaderCacheEntry.Parse(ref guestProgramReadOnlySpan, out GuestShaderCacheHeader fileHeader);
TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformationFeedbackInformations(ref guestProgramReadOnlySpan, fileHeader); TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformFeedbackInformation(ref guestProgramReadOnlySpan, fileHeader);
Hash128 newHash = CacheHelper.ComputeGuestHashFromCache(cachedShaderEntries, tfd); Hash128 newHash = CacheHelper.ComputeGuestHashFromCache(cachedShaderEntries, tfd);

View file

@ -128,7 +128,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
// Reconstruct code holder. // Reconstruct code holder.
if (isHostProgramValid) if (isHostProgramValid)
{ {
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA); program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo(); shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo();
} }
else else
@ -176,7 +176,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length]; ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length];
List<ShaderProgram> shaderPrograms = new List<ShaderProgram>(); List<ShaderProgram> shaderPrograms = new List<ShaderProgram>();
TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformationFeedbackInformations(ref guestProgramReadOnlySpan, fileHeader); TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformFeedbackInformation(ref guestProgramReadOnlySpan, fileHeader);
TranslationFlags flags = DefaultFlags; TranslationFlags flags = DefaultFlags;
@ -217,14 +217,17 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (isHostProgramValid) if (isHostProgramValid)
{ {
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA); program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo(); shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
} }
else else
{ {
IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors); IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
program = Translator.CreateContext((ulong)entry.Header.Size, 0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo); TranslatorContext translatorContext = Translator.CreateContext(0, gpuAccessor, flags, counts);
TranslatorContext translatorContext2 = Translator.CreateContext((ulong)entry.Header.Size, gpuAccessor, flags | TranslationFlags.VertexA, counts);
program = translatorContext.Translate(out shaderProgramInfo, translatorContext2);
} }
// NOTE: Vertex B comes first in the shader cache. // NOTE: Vertex B comes first in the shader cache.
@ -239,7 +242,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (isHostProgramValid) if (isHostProgramValid)
{ {
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA); program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo(); shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
} }
else else
@ -446,7 +449,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
} }
} }
TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages]; TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages + 1];
TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(state); TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(state);
@ -461,17 +464,14 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (addresses.VertexA != 0) if (addresses.VertexA != 0)
{ {
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA); shaderContexts[0] = DecodeGraphicsShader(state, counts, flags | TranslationFlags.VertexA, ShaderStage.Vertex, addresses.VertexA);
}
else
{
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
} }
shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl); shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation); shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry); shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment); shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
shaderContexts[5] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
bool isShaderCacheEnabled = _cacheManager != null; bool isShaderCacheEnabled = _cacheManager != null;
bool isShaderCacheReadOnly = false; bool isShaderCacheReadOnly = false;
@ -501,11 +501,11 @@ namespace Ryujinx.Graphics.Gpu.Shader
// The shader isn't currently cached, translate it and compile it. // The shader isn't currently cached, translate it and compile it.
ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages]; ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
shaders[0] = TranslateShader(shaderContexts[0]); shaders[0] = TranslateShader(shaderContexts[1], shaderContexts[0]);
shaders[1] = TranslateShader(shaderContexts[1]); shaders[1] = TranslateShader(shaderContexts[2]);
shaders[2] = TranslateShader(shaderContexts[2]); shaders[2] = TranslateShader(shaderContexts[3]);
shaders[3] = TranslateShader(shaderContexts[3]); shaders[3] = TranslateShader(shaderContexts[4]);
shaders[4] = TranslateShader(shaderContexts[4]); shaders[4] = TranslateShader(shaderContexts[5]);
List<IShader> hostShaders = new List<IShader>(); List<IShader> hostShaders = new List<IShader>();
@ -696,15 +696,13 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <param name="flags">Flags that controls shader translation</param> /// <param name="flags">Flags that controls shader translation</param>
/// <param name="stage">Shader stage</param> /// <param name="stage">Shader stage</param>
/// <param name="gpuVa">GPU virtual address of the shader code</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>The generated translator context</returns> /// <returns>The generated translator context</returns>
private TranslatorContext DecodeGraphicsShader( private TranslatorContext DecodeGraphicsShader(
GpuState state, GpuState state,
TranslationCounts counts, TranslationCounts counts,
TranslationFlags flags, TranslationFlags flags,
ShaderStage stage, ShaderStage stage,
ulong gpuVa, ulong gpuVa)
ulong gpuVaA = 0)
{ {
if (gpuVa == 0) if (gpuVa == 0)
{ {
@ -713,37 +711,31 @@ namespace Ryujinx.Graphics.Gpu.Shader
GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1); GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1);
if (gpuVaA != 0) return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
{
return Translator.CreateContext(gpuVaA, gpuVa, gpuAccessor, flags, counts);
}
else
{
return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
}
} }
/// <summary> /// <summary>
/// Translates a previously generated translator context to something that the host API accepts. /// Translates a previously generated translator context to something that the host API accepts.
/// </summary> /// </summary>
/// <param name="translatorContext">Current translator context to translate</param> /// <param name="translatorContext">Current translator context to translate</param>
/// <param name="translatorContext2">Optional translator context of the shader that should be combined</param>
/// <returns>Compiled graphics shader code</returns> /// <returns>Compiled graphics shader code</returns>
private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext) private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext, TranslatorContext translatorContext2 = null)
{ {
if (translatorContext == null) if (translatorContext == null)
{ {
return null; return null;
} }
if (translatorContext.AddressA != 0) if (translatorContext2 != null)
{ {
byte[] codeA = _context.MemoryManager.GetSpan(translatorContext.AddressA, translatorContext.SizeA).ToArray(); byte[] codeA = _context.MemoryManager.GetSpan(translatorContext2.Address, translatorContext2.Size).ToArray();
byte[] codeB = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray(); byte[] codeB = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
_dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA); _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
_dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB); _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo); ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo, translatorContext2);
if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null) if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
{ {

View file

@ -8,15 +8,10 @@ namespace Ryujinx.Graphics.Shader
public string Code { get; private set; } public string Code { get; private set; }
public int SizeA { get; } public ShaderProgram(ShaderStage stage, string code)
public int Size { get; }
public ShaderProgram(ShaderStage stage, string code, int size, int sizeA)
{ {
Stage = stage; Stage = stage;
Code = code; Code = code;
SizeA = sizeA;
Size = size;
} }
public void Prepend(string line) public void Prepend(string line)

View file

@ -36,22 +36,7 @@ namespace Ryujinx.Graphics.Shader.Translation
return new TranslatorContext(address, cfg, config); return new TranslatorContext(address, cfg, config);
} }
public static TranslatorContext CreateContext( internal static ShaderProgram Translate(FunctionCode[] functions, ShaderConfig config, out ShaderProgramInfo shaderProgramInfo)
ulong addressA,
ulong addressB,
IGpuAccessor gpuAccessor,
TranslationFlags flags,
TranslationCounts counts = null)
{
counts ??= new TranslationCounts();
Block[][] cfgA = DecodeShader(addressA, gpuAccessor, flags | TranslationFlags.VertexA, counts, out ShaderConfig configA);
Block[][] cfgB = DecodeShader(addressB, gpuAccessor, flags, counts, out ShaderConfig configB);
return new TranslatorContext(addressA, addressB, cfgA, cfgB, configA, configB);
}
internal static ShaderProgram Translate(FunctionCode[] functions, ShaderConfig config, out ShaderProgramInfo shaderProgramInfo, int sizeA = 0)
{ {
var cfgs = new ControlFlowGraph[functions.Length]; var cfgs = new ControlFlowGraph[functions.Length];
var frus = new RegisterUsage.FunctionRegisterUsage[functions.Length]; var frus = new RegisterUsage.FunctionRegisterUsage[functions.Length];
@ -113,7 +98,7 @@ namespace Ryujinx.Graphics.Shader.Translation
string glslCode = program.Code; string glslCode = program.Code;
return new ShaderProgram(config.Stage, glslCode, config.Size, sizeA); return new ShaderProgram(config.Stage, glslCode);
} }
private static Block[][] DecodeShader( private static Block[][] DecodeShader(

View file

@ -10,16 +10,12 @@ namespace Ryujinx.Graphics.Shader.Translation
public class TranslatorContext public class TranslatorContext
{ {
private readonly Block[][] _cfg; private readonly Block[][] _cfg;
private readonly Block[][] _cfgA;
private ShaderConfig _config; private ShaderConfig _config;
private ShaderConfig _configA;
public ulong Address { get; } public ulong Address { get; }
public ulong AddressA { get; }
public ShaderStage Stage => _config.Stage; public ShaderStage Stage => _config.Stage;
public int Size => _config.Size; public int Size => _config.Size;
public int SizeA => _configA != null ? _configA.Size : 0;
public HashSet<int> TextureHandlesForCache => _config.TextureHandlesForCache; public HashSet<int> TextureHandlesForCache => _config.TextureHandlesForCache;
@ -27,22 +23,9 @@ namespace Ryujinx.Graphics.Shader.Translation
internal TranslatorContext(ulong address, Block[][] cfg, ShaderConfig config) internal TranslatorContext(ulong address, Block[][] cfg, ShaderConfig config)
{ {
Address = address; Address = address;
AddressA = 0; _config = config;
_config = config; _cfg = cfg;
_configA = null;
_cfg = cfg;
_cfgA = null;
}
internal TranslatorContext(ulong addressA, ulong addressB, Block[][] cfgA, Block[][] cfgB, ShaderConfig configA, ShaderConfig configB)
{
Address = addressB;
AddressA = addressA;
_config = configB;
_configA = configA;
_cfg = cfgB;
_cfgA = cfgA;
} }
private static bool IsUserAttribute(Operand operand) private static bool IsUserAttribute(Operand operand)
@ -141,20 +124,19 @@ namespace Ryujinx.Graphics.Shader.Translation
return output; return output;
} }
public ShaderProgram Translate(out ShaderProgramInfo shaderProgramInfo) public ShaderProgram Translate(out ShaderProgramInfo shaderProgramInfo, TranslatorContext other = null)
{ {
FunctionCode[] code = EmitShader(_cfg, _config); FunctionCode[] code = EmitShader(_cfg, _config);
if (_configA != null) if (other != null)
{ {
FunctionCode[] codeA = EmitShader(_cfgA, _configA); _config.SetUsedFeature(other._config.UsedFeatures);
TextureHandlesForCache.UnionWith(other.TextureHandlesForCache);
_config.SetUsedFeature(_configA.UsedFeatures); code = Combine(EmitShader(other._cfg, other._config), code);
code = Combine(codeA, code);
} }
return Translator.Translate(code, _config, out shaderProgramInfo, SizeA); return Translator.Translate(code, _config, out shaderProgramInfo);
} }
} }
} }