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)
{
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="header">The guest shader program header</param>
/// <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)
{
@ -376,9 +376,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// <returns>Guest shader cahe entries from the runtime contexts</returns>
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];
@ -387,15 +389,17 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
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];
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);
@ -421,7 +425,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
}
}
entries[i] = entry;
entries[i - startIndex] = entry;
}
return entries;

View File

@ -103,7 +103,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
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);

View File

@ -128,7 +128,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
// Reconstruct code holder.
if (isHostProgramValid)
{
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo();
}
else
@ -176,7 +176,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length];
List<ShaderProgram> shaderPrograms = new List<ShaderProgram>();
TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformationFeedbackInformations(ref guestProgramReadOnlySpan, fileHeader);
TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformFeedbackInformation(ref guestProgramReadOnlySpan, fileHeader);
TranslationFlags flags = DefaultFlags;
@ -217,14 +217,17 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (isHostProgramValid)
{
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
}
else
{
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.
@ -239,7 +242,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (isHostProgramValid)
{
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
}
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);
@ -461,17 +464,14 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (addresses.VertexA != 0)
{
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
}
else
{
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags | TranslationFlags.VertexA, ShaderStage.Vertex, addresses.VertexA);
}
shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
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 isShaderCacheReadOnly = false;
@ -501,11 +501,11 @@ namespace Ryujinx.Graphics.Gpu.Shader
// The shader isn't currently cached, translate it and compile it.
ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
shaders[0] = TranslateShader(shaderContexts[0]);
shaders[1] = TranslateShader(shaderContexts[1]);
shaders[2] = TranslateShader(shaderContexts[2]);
shaders[3] = TranslateShader(shaderContexts[3]);
shaders[4] = TranslateShader(shaderContexts[4]);
shaders[0] = TranslateShader(shaderContexts[1], shaderContexts[0]);
shaders[1] = TranslateShader(shaderContexts[2]);
shaders[2] = TranslateShader(shaderContexts[3]);
shaders[3] = TranslateShader(shaderContexts[4]);
shaders[4] = TranslateShader(shaderContexts[5]);
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="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>The generated translator context</returns>
private TranslatorContext DecodeGraphicsShader(
GpuState state,
TranslationCounts counts,
TranslationFlags flags,
ShaderStage stage,
ulong gpuVa,
ulong gpuVaA = 0)
ulong gpuVa)
{
if (gpuVa == 0)
{
@ -713,37 +711,31 @@ namespace Ryujinx.Graphics.Gpu.Shader
GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1);
if (gpuVaA != 0)
{
return Translator.CreateContext(gpuVaA, gpuVa, gpuAccessor, flags, counts);
}
else
{
return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
}
return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
}
/// <summary>
/// Translates a previously generated translator context to something that the host API accepts.
/// </summary>
/// <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>
private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext)
private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext, TranslatorContext translatorContext2 = null)
{
if (translatorContext == 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();
_dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
_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)
{

View File

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

View File

@ -36,22 +36,7 @@ namespace Ryujinx.Graphics.Shader.Translation
return new TranslatorContext(address, cfg, config);
}
public static TranslatorContext CreateContext(
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)
internal static ShaderProgram Translate(FunctionCode[] functions, ShaderConfig config, out ShaderProgramInfo shaderProgramInfo)
{
var cfgs = new ControlFlowGraph[functions.Length];
var frus = new RegisterUsage.FunctionRegisterUsage[functions.Length];
@ -113,7 +98,7 @@ namespace Ryujinx.Graphics.Shader.Translation
string glslCode = program.Code;
return new ShaderProgram(config.Stage, glslCode, config.Size, sizeA);
return new ShaderProgram(config.Stage, glslCode);
}
private static Block[][] DecodeShader(

View File

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