Make sure attributes used on subsequent shader stages are initialized (#2538)

This commit is contained in:
gdkchan 2021-08-11 17:27:00 -03:00 committed by GitHub
parent 10d649e6d3
commit ed754af8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 347 additions and 262 deletions

View File

@ -38,7 +38,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <summary> /// <summary>
/// Version of the codegen (to be changed when codegen or guest format change). /// Version of the codegen (to be changed when codegen or guest format change).
/// </summary> /// </summary>
private const ulong ShaderCodeGenVersion = 2494; private const ulong ShaderCodeGenVersion = 2538;
// Progress reporting helpers // Progress reporting helpers
private volatile int _shaderCount; private volatile int _shaderCount;
@ -290,6 +290,43 @@ namespace Ryujinx.Graphics.Gpu.Shader
{ {
Task compileTask = Task.Run(() => Task compileTask = Task.Run(() =>
{ {
TranslatorContext[] shaderContexts = null;
if (!isHostProgramValid)
{
shaderContexts = new TranslatorContext[1 + entries.Length];
for (int i = 0; i < entries.Length; i++)
{
GuestShaderCacheEntry entry = entries[i];
if (entry == null)
{
continue;
}
var binaryCode = new Memory<byte>(entry.Code);
var gpuAccessor = new CachedGpuAccessor(
_context,
binaryCode,
binaryCode.Slice(binaryCode.Length - entry.Header.Cb1DataSize),
entry.Header.GpuAccessorHeader,
entry.TextureDescriptors);
var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags);
shaderContexts[i + 1] = Translator.CreateContext(0, gpuAccessor, options, counts);
if (entry.Header.SizeA != 0)
{
var options2 = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags | TranslationFlags.VertexA);
shaderContexts[0] = Translator.CreateContext((ulong)entry.Header.Size, gpuAccessor, options2, counts);
}
}
}
// Reconstruct code holder. // Reconstruct code holder.
for (int i = 0; i < entries.Length; i++) for (int i = 0; i < entries.Length; i++)
{ {
@ -301,9 +338,6 @@ namespace Ryujinx.Graphics.Gpu.Shader
} }
ShaderProgram program; ShaderProgram program;
if (entry.Header.SizeA != 0)
{
ShaderProgramInfo shaderProgramInfo; ShaderProgramInfo shaderProgramInfo;
if (isHostProgramValid) if (isHostProgramValid)
@ -313,58 +347,20 @@ namespace Ryujinx.Graphics.Gpu.Shader
} }
else else
{ {
var binaryCode = new Memory<byte>(entry.Code); int stageIndex = i + 1;
var gpuAccessor = new CachedGpuAccessor( TranslatorContext currentStage = shaderContexts[stageIndex];
_context, TranslatorContext nextStage = GetNextStageContext(shaderContexts, stageIndex);
binaryCode, TranslatorContext vertexA = stageIndex == 1 ? shaderContexts[0] : null;
binaryCode.Slice(binaryCode.Length - entry.Header.Cb1DataSize),
entry.Header.GpuAccessorHeader,
entry.TextureDescriptors);
var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags); program = currentStage.Translate(out shaderProgramInfo, nextStage, vertexA);
var options2 = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags | TranslationFlags.VertexA);
TranslatorContext translatorContext = Translator.CreateContext(0, gpuAccessor, options, counts);
TranslatorContext translatorContext2 = Translator.CreateContext((ulong)entry.Header.Size, gpuAccessor, options2, 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.
byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray(); byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray();
byte[] code2 = entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray(); byte[] code2 = entry.Header.SizeA != 0 ? entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray() : null;
shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2); shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2);
}
else
{
ShaderProgramInfo shaderProgramInfo;
if (isHostProgramValid)
{
program = new ShaderProgram(entry.Header.Stage, "");
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
}
else
{
var binaryCode = new Memory<byte>(entry.Code);
var gpuAccessor = new CachedGpuAccessor(
_context,
binaryCode,
binaryCode.Slice(binaryCode.Length - entry.Header.Cb1DataSize),
entry.Header.GpuAccessorHeader,
entry.TextureDescriptors);
var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags);
program = Translator.CreateContext(0, gpuAccessor, options, counts).Translate(out shaderProgramInfo);
}
byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray();
shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code);
}
shaderPrograms.Add(program); shaderPrograms.Add(program);
} }
@ -591,7 +587,7 @@ 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 shader = TranslateShader(channel.MemoryManager, shaderContexts[0]); ShaderCodeHolder shader = TranslateShader(_dumper, channel.MemoryManager, shaderContexts[0], null, null);
shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code); shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
@ -715,11 +711,10 @@ 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(channel.MemoryManager, shaderContexts[1], shaderContexts[0]); for (int stageIndex = 0; stageIndex < Constants.ShaderStages; stageIndex++)
shaders[1] = TranslateShader(channel.MemoryManager, shaderContexts[2]); {
shaders[2] = TranslateShader(channel.MemoryManager, shaderContexts[3]); shaders[stageIndex] = TranslateShader(_dumper, channel.MemoryManager, shaderContexts, stageIndex + 1);
shaders[3] = TranslateShader(channel.MemoryManager, shaderContexts[4]); }
shaders[4] = TranslateShader(channel.MemoryManager, shaderContexts[5]);
List<IShader> hostShaders = new List<IShader>(); List<IShader> hostShaders = new List<IShader>();
@ -942,53 +937,94 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <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="dumper">Optional shader code dumper</param>
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param> /// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
/// <param name="translatorContext">Current translator context to translate</param> /// <param name="stages">Translator context of all available shader stages</param>
/// <param name="translatorContext2">Optional translator context of the shader that should be combined</param> /// <param name="stageIndex">Index on the stages array to translate</param>
/// <returns>Compiled graphics shader code</returns> /// <returns>Compiled graphics shader code</returns>
private ShaderCodeHolder TranslateShader( private static ShaderCodeHolder TranslateShader(
ShaderDumper dumper,
MemoryManager memoryManager, MemoryManager memoryManager,
TranslatorContext translatorContext, TranslatorContext[] stages,
TranslatorContext translatorContext2 = null) int stageIndex)
{ {
if (translatorContext == null) TranslatorContext currentStage = stages[stageIndex];
TranslatorContext nextStage = GetNextStageContext(stages, stageIndex);
TranslatorContext vertexA = stageIndex == 1 ? stages[0] : null;
return TranslateShader(dumper, memoryManager, currentStage, nextStage, vertexA);
}
/// <summary>
/// Gets the next shader stage context, from an array of contexts and index of the current stage.
/// </summary>
/// <param name="stages">Translator context of all available shader stages</param>
/// <param name="stageIndex">Index on the stages array to translate</param>
/// <returns>The translator context of the next stage, or null if inexistent</returns>
private static TranslatorContext GetNextStageContext(TranslatorContext[] stages, int stageIndex)
{
for (int nextStageIndex = stageIndex + 1; nextStageIndex < stages.Length; nextStageIndex++)
{
if (stages[nextStageIndex] != null)
{
return stages[nextStageIndex];
}
}
return null;
}
/// <summary>
/// Translates a previously generated translator context to something that the host API accepts.
/// </summary>
/// <param name="dumper">Optional shader code dumper</param>
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
/// <param name="currentStage">Translator context of the stage to be translated</param>
/// <param name="nextStage">Translator context of the next active stage, if existent</param>
/// <param name="vertexA">Optional translator context of the shader that should be combined</param>
/// <returns>Compiled graphics shader code</returns>
private static ShaderCodeHolder TranslateShader(
ShaderDumper dumper,
MemoryManager memoryManager,
TranslatorContext currentStage,
TranslatorContext nextStage,
TranslatorContext vertexA)
{
if (currentStage == null)
{ {
return null; return null;
} }
if (translatorContext2 != null) if (vertexA != null)
{ {
byte[] codeA = memoryManager.GetSpan(translatorContext2.Address, translatorContext2.Size).ToArray(); byte[] codeA = memoryManager.GetSpan(vertexA.Address, vertexA.Size).ToArray();
byte[] codeB = memoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray(); byte[] codeB = memoryManager.GetSpan(currentStage.Address, currentStage.Size).ToArray();
_dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA); ShaderDumpPaths pathsA = default;
_dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB); ShaderDumpPaths pathsB = default;
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo, translatorContext2); if (dumper != null)
if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
{ {
program.Prepend("// " + codePathB); pathsA = dumper.Dump(codeA, compute: false);
program.Prepend("// " + fullPathB); pathsB = dumper.Dump(codeB, compute: false);
program.Prepend("// " + codePathA);
program.Prepend("// " + fullPathA);
} }
ShaderProgram program = currentStage.Translate(out ShaderProgramInfo shaderProgramInfo, nextStage, vertexA);
pathsB.Prepend(program);
pathsA.Prepend(program);
return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA); return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA);
} }
else else
{ {
byte[] code = memoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray(); byte[] code = memoryManager.GetSpan(currentStage.Address, currentStage.Size).ToArray();
_dumper.Dump(code, translatorContext.Stage == ShaderStage.Compute, out string fullPath, out string codePath); ShaderDumpPaths paths = dumper?.Dump(code, currentStage.Stage == ShaderStage.Compute) ?? default;
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo); ShaderProgram program = currentStage.Translate(out ShaderProgramInfo shaderProgramInfo, nextStage);
if (fullPath != null && codePath != null) paths.Prepend(program);
{
program.Prepend("// " + codePath);
program.Prepend("// " + fullPath);
}
return new ShaderCodeHolder(program, shaderProgramInfo, code); return new ShaderCodeHolder(program, shaderProgramInfo, code);
} }

View File

@ -0,0 +1,49 @@
using Ryujinx.Graphics.Shader;
namespace Ryujinx.Graphics.Gpu.Shader
{
/// <summary>
/// Paths where shader code was dumped on disk.
/// </summary>
struct ShaderDumpPaths
{
/// <summary>
/// Path where the full shader code with header was dumped, or null if not dumped.
/// </summary>
public string FullPath { get; }
/// <summary>
/// Path where the shader code without header was dumped, or null if not dumped.
/// </summary>
public string CodePath { get; }
/// <summary>
/// True if the shader was dumped, false otherwise.
/// </summary>
public bool HasPath => FullPath != null && CodePath != null;
/// <summary>
/// Creates a new shader dumps path structure.
/// </summary>
/// <param name="fullPath">Path where the full shader code with header was dumped, or null if not dumped</param>
/// <param name="codePath">Path where the shader code without header was dumped, or null if not dumped</param>
public ShaderDumpPaths(string fullPath, string codePath)
{
FullPath = fullPath;
CodePath = codePath;
}
/// <summary>
/// Prepends the shader paths on the program source, as a comment.
/// </summary>
/// <param name="program">Program to prepend into</param>
public void Prepend(ShaderProgram program)
{
if (HasPath)
{
program.Prepend("// " + CodePath);
program.Prepend("// " + FullPath);
}
}
}
}

View File

@ -1,4 +1,4 @@
using System; using Ryujinx.Graphics.Shader;
using System.IO; using System.IO;
namespace Ryujinx.Graphics.Gpu.Shader namespace Ryujinx.Graphics.Gpu.Shader
@ -30,24 +30,20 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// </summary> /// </summary>
/// <param name="code">Code to be dumped</param> /// <param name="code">Code to be dumped</param>
/// <param name="compute">True for compute shader code, false for graphics shader code</param> /// <param name="compute">True for compute shader code, false for graphics shader code</param>
/// <param name="fullPath">Output path for the shader code with header included</param> /// <returns>Paths where the shader code was dumped</returns>
/// <param name="codePath">Output path for the shader code without header</param> public ShaderDumpPaths Dump(byte[] code, bool compute)
public void Dump(byte[] code, bool compute, out string fullPath, out string codePath)
{ {
_dumpPath = GraphicsConfig.ShadersDumpPath; _dumpPath = GraphicsConfig.ShadersDumpPath;
if (string.IsNullOrWhiteSpace(_dumpPath)) if (string.IsNullOrWhiteSpace(_dumpPath))
{ {
fullPath = null; return default;
codePath = null;
return;
} }
string fileName = "Shader" + CurrentDumpIndex.ToString("d4") + ".bin"; string fileName = "Shader" + CurrentDumpIndex.ToString("d4") + ".bin";
fullPath = Path.Combine(FullDir(), fileName); string fullPath = Path.Combine(FullDir(), fileName);
codePath = Path.Combine(CodeDir(), fileName); string codePath = Path.Combine(CodeDir(), fileName);
CurrentDumpIndex++; CurrentDumpIndex++;
@ -73,6 +69,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
{ {
codeWriter.Write(0); codeWriter.Write(0);
} }
return new ShaderDumpPaths(fullPath, codePath);
} }
/// <summary> /// <summary>

View File

@ -3,14 +3,12 @@ using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation; using Ryujinx.Graphics.Shader.Translation;
using System; using System;
using System.Linq; using System.Linq;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.CodeGen.Glsl namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{ {
static class Declarations static class Declarations
{ {
// At least 16 attributes are guaranteed by the spec.
public const int MaxAttributes = 16;
public static void Declare(CodeGenContext context, StructuredProgramInfo info) public static void Declare(CodeGenContext context, StructuredProgramInfo info)
{ {
context.AppendLine("#version 450 core"); context.AppendLine("#version 450 core");
@ -129,14 +127,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
context.AppendLine(); context.AppendLine();
} }
if (info.IAttributes.Count != 0 || context.Config.GpPassthrough) if (context.Config.UsedInputAttributes != 0 || context.Config.GpPassthrough)
{ {
DeclareInputAttributes(context, info); DeclareInputAttributes(context, info);
context.AppendLine(); context.AppendLine();
} }
if (info.OAttributes.Count != 0 || context.Config.Stage != ShaderStage.Fragment) if (context.Config.UsedOutputAttributes != 0 || context.Config.Stage != ShaderStage.Fragment)
{ {
DeclareOutputAttributes(context, info); DeclareOutputAttributes(context, info);
@ -404,24 +402,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info) private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
{ {
if (context.Config.GpPassthrough) int usedAttribtes = context.Config.UsedInputAttributes;
while (usedAttribtes != 0)
{ {
for (int attr = 0; attr < MaxAttributes; attr++) int index = BitOperations.TrailingZeroCount(usedAttribtes);
{
DeclareInputAttribute(context, info, attr);
}
foreach (int attr in info.IAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes)) DeclareInputAttribute(context, info, index);
{
DeclareInputAttribute(context, info, attr); usedAttribtes &= ~(1 << index);
}
}
else
{
foreach (int attr in info.IAttributes.OrderBy(x => x))
{
DeclareInputAttribute(context, info, attr);
}
} }
} }
@ -440,8 +428,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
}; };
} }
string pass = context.Config.GpPassthrough && !info.OAttributes.Contains(attr) ? "passthrough, " : string.Empty; string pass = (context.Config.PassthroughAttributes & (1 << attr)) != 0 ? "passthrough, " : string.Empty;
string name = $"{DefaultNames.IAttributePrefix}{attr}"; string name = $"{DefaultNames.IAttributePrefix}{attr}";
if ((context.Config.Options.Flags & TranslationFlags.Feedback) != 0) if ((context.Config.Options.Flags & TranslationFlags.Feedback) != 0)
@ -461,34 +448,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info) private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
{ {
if (context.Config.Stage == ShaderStage.Fragment || context.Config.GpPassthrough) int usedAttribtes = context.Config.UsedOutputAttributes;
while (usedAttribtes != 0)
{ {
DeclareUsedOutputAttributes(context, info); int index = BitOperations.TrailingZeroCount(usedAttribtes);
}
else
{
DeclareAllOutputAttributes(context, info);
}
}
private static void DeclareUsedOutputAttributes(CodeGenContext context, StructuredProgramInfo info) DeclareOutputAttribute(context, index);
{
foreach (int attr in info.OAttributes.OrderBy(x => x))
{
DeclareOutputAttribute(context, attr);
}
}
private static void DeclareAllOutputAttributes(CodeGenContext context, StructuredProgramInfo info) usedAttribtes &= ~(1 << index);
{
for (int attr = 0; attr < MaxAttributes; attr++)
{
DeclareOutputAttribute(context, attr);
}
foreach (int attr in info.OAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes))
{
DeclareOutputAttribute(context, attr);
} }
} }

View File

@ -49,46 +49,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
Declarations.DeclareLocals(context, function); Declarations.DeclareLocals(context, function);
if (funcName == MainFunctionName)
{
// Some games will leave some elements of gl_Position uninitialized,
// in those cases, the elements will contain undefined values according
// to the spec, but on NVIDIA they seems to be always initialized to (0, 0, 0, 1),
// so we do explicit initialization to avoid UB on non-NVIDIA gpus.
if (context.Config.Stage == ShaderStage.Vertex)
{
context.AppendLine("gl_Position = vec4(0.0, 0.0, 0.0, 1.0);");
}
// Ensure that unused attributes are set, otherwise the downstream
// compiler may eliminate them.
// (Not needed for fragment shader as it is the last stage).
if (context.Config.Stage != ShaderStage.Compute &&
context.Config.Stage != ShaderStage.Fragment &&
!context.Config.GpPassthrough)
{
for (int attr = 0; attr < Declarations.MaxAttributes; attr++)
{
if (info.OAttributes.Contains(attr))
{
continue;
}
if ((context.Config.Options.Flags & TranslationFlags.Feedback) != 0)
{
context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_x = 0.0;");
context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_y = 0.0;");
context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_z = 0.0;");
context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_w = 1.0;");
}
else
{
context.AppendLine($"{DefaultNames.OAttributePrefix}{attr} = vec4(0.0, 0.0, 0.0, 1.0);");
}
}
}
}
PrintBlock(context, function.MainBlock); PrintBlock(context, function.MainBlock);
context.LeaveScope(); context.LeaveScope();

View File

@ -1,4 +1,5 @@
using Ryujinx.Graphics.Shader.Instructions; using Ryujinx.Graphics.Shader.Instructions;
using Ryujinx.Graphics.Shader.Translation;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -9,10 +10,8 @@ namespace Ryujinx.Graphics.Shader.Decoders
{ {
static class Decoder static class Decoder
{ {
public static Block[][] Decode(IGpuAccessor gpuAccessor, ulong startAddress, out bool hasBindless) public static Block[][] Decode(ShaderConfig config, ulong startAddress)
{ {
hasBindless = false;
List<Block[]> funcs = new List<Block[]>(); List<Block[]> funcs = new List<Block[]>();
Queue<ulong> funcQueue = new Queue<ulong>(); Queue<ulong> funcQueue = new Queue<ulong>();
@ -90,8 +89,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
} }
} }
FillBlock(gpuAccessor, currBlock, limitAddress, startAddress, out bool blockHasBindless); FillBlock(config, currBlock, limitAddress, startAddress);
hasBindless |= blockHasBindless;
if (currBlock.OpCodes.Count != 0) if (currBlock.OpCodes.Count != 0)
{ {
@ -168,7 +166,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
for (int i = 0; i < cbOffsetsCount; i++) for (int i = 0; i < cbOffsetsCount; i++)
{ {
uint targetOffset = gpuAccessor.ConstantBuffer1Read(cbBaseOffset + i * 4); uint targetOffset = config.GpuAccessor.ConstantBuffer1Read(cbBaseOffset + i * 4);
Block target = GetBlock(baseOffset + targetOffset); Block target = GetBlock(baseOffset + targetOffset);
opBrIndir.PossibleTargets.Add(target); opBrIndir.PossibleTargets.Add(target);
target.Predecessors.Add(block); target.Predecessors.Add(block);
@ -224,15 +222,11 @@ namespace Ryujinx.Graphics.Shader.Decoders
return false; return false;
} }
private static void FillBlock( private static void FillBlock(ShaderConfig config, Block block, ulong limitAddress, ulong startAddress)
IGpuAccessor gpuAccessor,
Block block,
ulong limitAddress,
ulong startAddress,
out bool hasBindless)
{ {
IGpuAccessor gpuAccessor = config.GpuAccessor;
ulong address = block.Address; ulong address = block.Address;
hasBindless = false;
do do
{ {
@ -274,13 +268,38 @@ namespace Ryujinx.Graphics.Shader.Decoders
OpCode op = makeOp(emitter, opAddress, opCode); OpCode op = makeOp(emitter, opAddress, opCode);
// We check these patterns to figure out the presence of bindless access // We check these patterns to figure out the presence of bindless access
hasBindless |= (op is OpCodeImage image && image.IsBindless) || if ((op is OpCodeImage image && image.IsBindless) ||
(op is OpCodeTxd txd && txd.IsBindless) || (op is OpCodeTxd txd && txd.IsBindless) ||
(op is OpCodeTld4B) || (op is OpCodeTld4B) ||
(emitter == InstEmit.TexB) || (emitter == InstEmit.TexB) ||
(emitter == InstEmit.TldB) || (emitter == InstEmit.TldB) ||
(emitter == InstEmit.TmmlB) || (emitter == InstEmit.TmmlB) ||
(emitter == InstEmit.TxqB); (emitter == InstEmit.TxqB))
{
config.SetUsedFeature(FeatureFlags.Bindless);
}
// Populate used attributes.
if (op is IOpCodeAttribute opAttr)
{
for (int elemIndex = 0; elemIndex < opAttr.Count; elemIndex++)
{
int attr = opAttr.AttributeOffset + elemIndex * 4;
if (attr >= AttributeConsts.UserAttributeBase && attr < AttributeConsts.UserAttributeEnd)
{
int index = (attr - AttributeConsts.UserAttributeBase) / 16;
if (op.Emitter == InstEmit.Ast)
{
config.SetOutputUserAttribute(index);
}
else
{
config.SetInputUserAttribute(index);
}
}
}
}
block.OpCodes.Add(op); block.OpCodes.Add(op);
} }

View File

@ -0,0 +1,8 @@
namespace Ryujinx.Graphics.Shader.Decoders
{
interface IOpCodeAttribute
{
int AttributeOffset { get; }
int Count { get; }
}
}

View File

@ -2,7 +2,7 @@ using Ryujinx.Graphics.Shader.Instructions;
namespace Ryujinx.Graphics.Shader.Decoders namespace Ryujinx.Graphics.Shader.Decoders
{ {
class OpCodeAttribute : OpCodeAluReg class OpCodeAttribute : OpCodeAluReg, IOpCodeAttribute
{ {
public int AttributeOffset { get; } public int AttributeOffset { get; }
public int Count { get; } public int Count { get; }

View File

@ -2,9 +2,10 @@ using Ryujinx.Graphics.Shader.Instructions;
namespace Ryujinx.Graphics.Shader.Decoders namespace Ryujinx.Graphics.Shader.Decoders
{ {
class OpCodeIpa : OpCodeAluReg class OpCodeIpa : OpCodeAluReg, IOpCodeAttribute
{ {
public int AttributeOffset { get; } public int AttributeOffset { get; }
public int Count => 1;
public InterpolationMode Mode { get; } public InterpolationMode Mode { get; }

View File

@ -277,21 +277,11 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
public AstOperand GetOperandDef(Operand operand) public AstOperand GetOperandDef(Operand operand)
{ {
if (TryGetUserAttributeIndex(operand, out int attrIndex))
{
Info.OAttributes.Add(attrIndex);
}
return GetOperand(operand); return GetOperand(operand);
} }
public AstOperand GetOperandUse(Operand operand) public AstOperand GetOperandUse(Operand operand)
{ {
if (TryGetUserAttributeIndex(operand, out int attrIndex))
{
Info.IAttributes.Add(attrIndex);
}
return GetOperand(operand); return GetOperand(operand);
} }
@ -318,30 +308,5 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
return astOperand; return astOperand;
} }
private static bool TryGetUserAttributeIndex(Operand operand, out int attrIndex)
{
if (operand.Type == OperandType.Attribute)
{
if (operand.Value >= AttributeConsts.UserAttributeBase &&
operand.Value < AttributeConsts.UserAttributeEnd)
{
attrIndex = (operand.Value - AttributeConsts.UserAttributeBase) >> 4;
return true;
}
else if (operand.Value >= AttributeConsts.FragmentOutputColorBase &&
operand.Value < AttributeConsts.FragmentOutputColorEnd)
{
attrIndex = (operand.Value - AttributeConsts.FragmentOutputColorBase) >> 4;
return true;
}
}
attrIndex = 0;
return false;
}
} }
} }

View File

@ -6,17 +6,11 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
{ {
public List<StructuredFunction> Functions { get; } public List<StructuredFunction> Functions { get; }
public HashSet<int> IAttributes { get; }
public HashSet<int> OAttributes { get; }
public HelperFunctionsMask HelperFunctionsMask { get; set; } public HelperFunctionsMask HelperFunctionsMask { get; set; }
public StructuredProgramInfo() public StructuredProgramInfo()
{ {
Functions = new List<StructuredFunction>(); Functions = new List<StructuredFunction>();
IAttributes = new HashSet<int>();
OAttributes = new HashSet<int>();
} }
} }
} }

View File

@ -15,6 +15,8 @@ namespace Ryujinx.Graphics.Shader.Translation
public bool IsNonMain { get; } public bool IsNonMain { get; }
public int OperationsCount => _operations.Count;
private readonly IReadOnlyDictionary<ulong, int> _funcs; private readonly IReadOnlyDictionary<ulong, int> _funcs;
private readonly List<Operation> _operations; private readonly List<Operation> _operations;
private readonly Dictionary<ulong, Operand> _labels; private readonly Dictionary<ulong, Operand> _labels;
@ -200,6 +202,7 @@ namespace Ryujinx.Graphics.Shader.Translation
if (target.Enabled) if (target.Enabled)
{ {
Config.SetOutputUserAttribute(rtIndex);
regIndexBase += 4; regIndexBase += 4;
} }
} }

View File

@ -41,6 +41,10 @@ namespace Ryujinx.Graphics.Shader.Translation
private readonly TranslationCounts _counts; private readonly TranslationCounts _counts;
public int UsedInputAttributes { get; private set; }
public int UsedOutputAttributes { get; private set; }
public int PassthroughAttributes { get; private set; }
private int _usedConstantBuffers; private int _usedConstantBuffers;
private int _usedStorageBuffers; private int _usedStorageBuffers;
private int _usedStorageBuffersWrite; private int _usedStorageBuffersWrite;
@ -170,6 +174,8 @@ namespace Ryujinx.Graphics.Shader.Translation
TextureHandlesForCache.UnionWith(other.TextureHandlesForCache); TextureHandlesForCache.UnionWith(other.TextureHandlesForCache);
UsedInputAttributes |= other.UsedInputAttributes;
UsedOutputAttributes |= other.UsedOutputAttributes;
_usedConstantBuffers |= other._usedConstantBuffers; _usedConstantBuffers |= other._usedConstantBuffers;
_usedStorageBuffers |= other._usedStorageBuffers; _usedStorageBuffers |= other._usedStorageBuffers;
_usedStorageBuffersWrite |= other._usedStorageBuffersWrite; _usedStorageBuffersWrite |= other._usedStorageBuffersWrite;
@ -191,6 +197,28 @@ namespace Ryujinx.Graphics.Shader.Translation
} }
} }
public void SetInputUserAttribute(int index)
{
UsedInputAttributes |= 1 << index;
}
public void SetOutputUserAttribute(int index)
{
UsedOutputAttributes |= 1 << index;
}
public void MergeOutputUserAttributes(int mask)
{
if (GpPassthrough)
{
PassthroughAttributes = mask & ~UsedOutputAttributes;
}
else
{
UsedOutputAttributes |= mask;
}
}
public void SetClipDistanceWritten(int index) public void SetClipDistanceWritten(int index)
{ {
ClipDistancesWritten |= (byte)(1 << index); ClipDistancesWritten |= (byte)(1 << index);

View File

@ -5,6 +5,7 @@ using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation.Optimizations; using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper; using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
@ -120,24 +121,17 @@ namespace Ryujinx.Graphics.Shader.Translation
Block[][] cfg; Block[][] cfg;
ulong maxEndAddress = 0; ulong maxEndAddress = 0;
bool hasBindless;
if ((options.Flags & TranslationFlags.Compute) != 0) if ((options.Flags & TranslationFlags.Compute) != 0)
{ {
config = new ShaderConfig(gpuAccessor, options, counts); config = new ShaderConfig(gpuAccessor, options, counts);
cfg = Decoder.Decode(gpuAccessor, address, out hasBindless); cfg = Decoder.Decode(config, address);
} }
else else
{ {
config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, options, counts); config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, options, counts);
cfg = Decoder.Decode(gpuAccessor, address + HeaderSize, out hasBindless); cfg = Decoder.Decode(config, address + HeaderSize);
}
if (hasBindless)
{
config.SetUsedFeature(FeatureFlags.Bindless);
} }
for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++) for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++)
@ -151,7 +145,7 @@ namespace Ryujinx.Graphics.Shader.Translation
maxEndAddress = block.EndAddress; maxEndAddress = block.EndAddress;
} }
if (!hasBindless) if (!config.UsedFeatures.HasFlag(FeatureFlags.Bindless))
{ {
for (int index = 0; index < block.OpCodes.Count; index++) for (int index = 0; index < block.OpCodes.Count; index++)
{ {
@ -169,8 +163,10 @@ namespace Ryujinx.Graphics.Shader.Translation
return cfg; return cfg;
} }
internal static FunctionCode[] EmitShader(Block[][] cfg, ShaderConfig config) internal static FunctionCode[] EmitShader(Block[][] cfg, ShaderConfig config, bool initializeOutputs, out int initializationOperations)
{ {
initializationOperations = 0;
Dictionary<ulong, int> funcIds = new Dictionary<ulong, int>(); Dictionary<ulong, int> funcIds = new Dictionary<ulong, int>();
for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++) for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++)
@ -184,6 +180,12 @@ namespace Ryujinx.Graphics.Shader.Translation
{ {
EmitterContext context = new EmitterContext(config, funcIndex != 0, funcIds); EmitterContext context = new EmitterContext(config, funcIndex != 0, funcIds);
if (initializeOutputs && funcIndex == 0)
{
EmitOutputsInitialization(context, config);
initializationOperations = context.OperationsCount;
}
for (int blkIndex = 0; blkIndex < cfg[funcIndex].Length; blkIndex++) for (int blkIndex = 0; blkIndex < cfg[funcIndex].Length; blkIndex++)
{ {
Block block = cfg[funcIndex][blkIndex]; Block block = cfg[funcIndex][blkIndex];
@ -201,6 +203,39 @@ namespace Ryujinx.Graphics.Shader.Translation
return funcs.ToArray(); return funcs.ToArray();
} }
private static void EmitOutputsInitialization(EmitterContext context, ShaderConfig config)
{
// Compute has no output attributes, and fragment is the last stage, so we
// don't need to initialize outputs on those stages.
if (config.Stage == ShaderStage.Compute || config.Stage == ShaderStage.Fragment)
{
return;
}
void InitializeOutput(int baseAttr)
{
for (int c = 0; c < 4; c++)
{
context.Copy(Attribute(baseAttr + c * 4), ConstF(c == 3 ? 1f : 0f));
}
}
if (config.Stage == ShaderStage.Vertex)
{
InitializeOutput(AttributeConsts.PositionX);
}
int usedAttribtes = context.Config.UsedOutputAttributes;
while (usedAttribtes != 0)
{
int index = BitOperations.TrailingZeroCount(usedAttribtes);
InitializeOutput(AttributeConsts.UserAttributeBase + index * 16);
usedAttribtes &= ~(1 << index);
}
}
private static void EmitOps(EmitterContext context, Block block) private static void EmitOps(EmitterContext context, Block block)
{ {
for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++) for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)

View File

@ -38,7 +38,7 @@ namespace Ryujinx.Graphics.Shader.Translation
operand.Value < AttributeConsts.UserAttributeEnd; operand.Value < AttributeConsts.UserAttributeEnd;
} }
private static FunctionCode[] Combine(FunctionCode[] a, FunctionCode[] b) private static FunctionCode[] Combine(FunctionCode[] a, FunctionCode[] b, int aStart)
{ {
// Here we combine two shaders. // Here we combine two shaders.
// For shader A: // For shader A:
@ -57,7 +57,7 @@ namespace Ryujinx.Graphics.Shader.Translation
Operand lblB = Label(); Operand lblB = Label();
for (int index = 0; index < a[0].Code.Length; index++) for (int index = aStart; index < a[0].Code.Length; index++)
{ {
Operation operation = a[0].Code[index]; Operation operation = a[0].Code[index];
@ -102,11 +102,21 @@ namespace Ryujinx.Graphics.Shader.Translation
Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4]; Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4];
if (temp != null) if (temp != null)
{
// TODO: LoadAttribute should accept any integer value as first argument,
// then we don't need special case here. Right now it expects the first
// operand to be of type "attribute".
if ((operation.Inst & Instruction.Mask) == Instruction.LoadAttribute)
{
operation.TurnIntoCopy(temp);
}
else
{ {
operation.SetSource(srcIndex, temp); operation.SetSource(srcIndex, temp);
} }
} }
} }
}
ops.Add(operation); ops.Add(operation);
} }
@ -126,13 +136,25 @@ namespace Ryujinx.Graphics.Shader.Translation
return output; return output;
} }
public ShaderProgram Translate(out ShaderProgramInfo shaderProgramInfo, TranslatorContext other = null) public ShaderProgram Translate(
out ShaderProgramInfo shaderProgramInfo,
TranslatorContext nextStage = null,
TranslatorContext other = null)
{ {
FunctionCode[] code = EmitShader(_cfg, _config); if (nextStage != null)
{
_config.MergeOutputUserAttributes(nextStage._config.UsedInputAttributes);
}
FunctionCode[] code = EmitShader(_cfg, _config, initializeOutputs: other == null, out _);
if (other != null) if (other != null)
{ {
code = Combine(EmitShader(other._cfg, other._config), code); other._config.MergeOutputUserAttributes(_config.UsedOutputAttributes);
FunctionCode[] otherCode = EmitShader(other._cfg, other._config, initializeOutputs: true, out int aStart);
code = Combine(otherCode, code, aStart);
_config.InheritFrom(other._config); _config.InheritFrom(other._config);
} }