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

@ -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++)