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

@ -3,14 +3,12 @@ using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Linq;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{
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)
{
context.AppendLine("#version 450 core");
@ -129,14 +127,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
context.AppendLine();
}
if (info.IAttributes.Count != 0 || context.Config.GpPassthrough)
if (context.Config.UsedInputAttributes != 0 || context.Config.GpPassthrough)
{
DeclareInputAttributes(context, info);
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);
@ -404,24 +402,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
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++)
{
DeclareInputAttribute(context, info, attr);
}
int index = BitOperations.TrailingZeroCount(usedAttribtes);
foreach (int attr in info.IAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes))
{
DeclareInputAttribute(context, info, attr);
}
}
else
{
foreach (int attr in info.IAttributes.OrderBy(x => x))
{
DeclareInputAttribute(context, info, attr);
}
DeclareInputAttribute(context, info, index);
usedAttribtes &= ~(1 << index);
}
}
@ -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}";
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)
{
if (context.Config.Stage == ShaderStage.Fragment || context.Config.GpPassthrough)
int usedAttribtes = context.Config.UsedOutputAttributes;
while (usedAttribtes != 0)
{
DeclareUsedOutputAttributes(context, info);
}
else
{
DeclareAllOutputAttributes(context, info);
}
}
int index = BitOperations.TrailingZeroCount(usedAttribtes);
private static void DeclareUsedOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
{
foreach (int attr in info.OAttributes.OrderBy(x => x))
{
DeclareOutputAttribute(context, attr);
}
}
DeclareOutputAttribute(context, index);
private static void DeclareAllOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
{
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);
usedAttribtes &= ~(1 << index);
}
}

View file

@ -49,46 +49,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
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);
context.LeaveScope();

View file

@ -1,4 +1,5 @@
using Ryujinx.Graphics.Shader.Instructions;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,10 +10,8 @@ namespace Ryujinx.Graphics.Shader.Decoders
{
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[]>();
Queue<ulong> funcQueue = new Queue<ulong>();
@ -90,8 +89,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
}
}
FillBlock(gpuAccessor, currBlock, limitAddress, startAddress, out bool blockHasBindless);
hasBindless |= blockHasBindless;
FillBlock(config, currBlock, limitAddress, startAddress);
if (currBlock.OpCodes.Count != 0)
{
@ -168,7 +166,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
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);
opBrIndir.PossibleTargets.Add(target);
target.Predecessors.Add(block);
@ -224,15 +222,11 @@ namespace Ryujinx.Graphics.Shader.Decoders
return false;
}
private static void FillBlock(
IGpuAccessor gpuAccessor,
Block block,
ulong limitAddress,
ulong startAddress,
out bool hasBindless)
private static void FillBlock(ShaderConfig config, Block block, ulong limitAddress, ulong startAddress)
{
IGpuAccessor gpuAccessor = config.GpuAccessor;
ulong address = block.Address;
hasBindless = false;
do
{
@ -274,13 +268,38 @@ namespace Ryujinx.Graphics.Shader.Decoders
OpCode op = makeOp(emitter, opAddress, opCode);
// 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 OpCodeTld4B) ||
(emitter == InstEmit.TexB) ||
(emitter == InstEmit.TldB) ||
(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);
}

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
{
class OpCodeAttribute : OpCodeAluReg
class OpCodeAttribute : OpCodeAluReg, IOpCodeAttribute
{
public int AttributeOffset { get; }
public int Count { get; }

View file

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

View file

@ -277,21 +277,11 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
public AstOperand GetOperandDef(Operand operand)
{
if (TryGetUserAttributeIndex(operand, out int attrIndex))
{
Info.OAttributes.Add(attrIndex);
}
return GetOperand(operand);
}
public AstOperand GetOperandUse(Operand operand)
{
if (TryGetUserAttributeIndex(operand, out int attrIndex))
{
Info.IAttributes.Add(attrIndex);
}
return GetOperand(operand);
}
@ -318,30 +308,5 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
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 HashSet<int> IAttributes { get; }
public HashSet<int> OAttributes { get; }
public HelperFunctionsMask HelperFunctionsMask { get; set; }
public StructuredProgramInfo()
{
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 int OperationsCount => _operations.Count;
private readonly IReadOnlyDictionary<ulong, int> _funcs;
private readonly List<Operation> _operations;
private readonly Dictionary<ulong, Operand> _labels;
@ -200,6 +202,7 @@ namespace Ryujinx.Graphics.Shader.Translation
if (target.Enabled)
{
Config.SetOutputUserAttribute(rtIndex);
regIndexBase += 4;
}
}

View file

@ -41,6 +41,10 @@ namespace Ryujinx.Graphics.Shader.Translation
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 _usedStorageBuffers;
private int _usedStorageBuffersWrite;
@ -170,6 +174,8 @@ namespace Ryujinx.Graphics.Shader.Translation
TextureHandlesForCache.UnionWith(other.TextureHandlesForCache);
UsedInputAttributes |= other.UsedInputAttributes;
UsedOutputAttributes |= other.UsedOutputAttributes;
_usedConstantBuffers |= other._usedConstantBuffers;
_usedStorageBuffers |= other._usedStorageBuffers;
_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)
{
ClipDistancesWritten |= (byte)(1 << index);

View file

@ -5,6 +5,7 @@ using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System;
using System.Collections.Generic;
using System.Numerics;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
@ -120,24 +121,17 @@ namespace Ryujinx.Graphics.Shader.Translation
Block[][] cfg;
ulong maxEndAddress = 0;
bool hasBindless;
if ((options.Flags & TranslationFlags.Compute) != 0)
{
config = new ShaderConfig(gpuAccessor, options, counts);
cfg = Decoder.Decode(gpuAccessor, address, out hasBindless);
cfg = Decoder.Decode(config, address);
}
else
{
config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, options, counts);
cfg = Decoder.Decode(gpuAccessor, address + HeaderSize, out hasBindless);
}
if (hasBindless)
{
config.SetUsedFeature(FeatureFlags.Bindless);
cfg = Decoder.Decode(config, address + HeaderSize);
}
for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++)
@ -151,7 +145,7 @@ namespace Ryujinx.Graphics.Shader.Translation
maxEndAddress = block.EndAddress;
}
if (!hasBindless)
if (!config.UsedFeatures.HasFlag(FeatureFlags.Bindless))
{
for (int index = 0; index < block.OpCodes.Count; index++)
{
@ -169,8 +163,10 @@ namespace Ryujinx.Graphics.Shader.Translation
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>();
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);
if (initializeOutputs && funcIndex == 0)
{
EmitOutputsInitialization(context, config);
initializationOperations = context.OperationsCount;
}
for (int blkIndex = 0; blkIndex < cfg[funcIndex].Length; blkIndex++)
{
Block block = cfg[funcIndex][blkIndex];
@ -201,6 +203,39 @@ namespace Ryujinx.Graphics.Shader.Translation
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)
{
for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)

View file

@ -38,7 +38,7 @@ namespace Ryujinx.Graphics.Shader.Translation
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.
// For shader A:
@ -57,7 +57,7 @@ namespace Ryujinx.Graphics.Shader.Translation
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];
@ -103,7 +103,17 @@ namespace Ryujinx.Graphics.Shader.Translation
if (temp != null)
{
operation.SetSource(srcIndex, temp);
// 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);
}
}
}
}
@ -126,13 +136,25 @@ namespace Ryujinx.Graphics.Shader.Translation
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)
{
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);
}