Make sure attributes used on subsequent shader stages are initialized (#2538)
This commit is contained in:
parent
10d649e6d3
commit
ed754af8d5
15 changed files with 347 additions and 262 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue