From 51a27032f01826e0cec56c53da4359fd2c38c8f3 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Fri, 11 Nov 2022 13:22:49 -0300 Subject: [PATCH] Fix VertexId and InstanceId on Vulkan (#3833) * Fix VertexId and InstanceId on Vulkan * Shader cache version bump --- .../Shader/DiskCache/DiskCacheHostStorage.cs | 2 +- .../CodeGen/Glsl/OperandManager.cs | 4 ++++ .../CodeGen/Spirv/Declarations.cs | 8 ++++++-- .../Instructions/InstEmitAttribute.cs | 19 ++++++++++++++++++- .../Translation/AttributeConsts.cs | 5 +++++ .../Translation/AttributeInfo.cs | 4 ++++ 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs b/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs index 02ae06f65..3f3a3c50e 100644 --- a/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs +++ b/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs @@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache private const ushort FileFormatVersionMajor = 1; private const ushort FileFormatVersionMinor = 2; private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor; - private const uint CodeGenVersion = 3807; + private const uint CodeGenVersion = 3833; private const string SharedTocFileName = "shared.toc"; private const string SharedDataFileName = "shared.data"; diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs index fd284316f..67442e5a1 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs @@ -48,6 +48,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { AttributeConsts.TessCoordY, new BuiltInAttribute("gl_TessCoord.y", VariableType.F32) }, { AttributeConsts.InstanceId, new BuiltInAttribute("gl_InstanceID", VariableType.S32) }, { AttributeConsts.VertexId, new BuiltInAttribute("gl_VertexID", VariableType.S32) }, + { AttributeConsts.BaseInstance, new BuiltInAttribute("gl_BaseInstance", VariableType.S32) }, + { AttributeConsts.BaseVertex, new BuiltInAttribute("gl_BaseVertex", VariableType.S32) }, + { AttributeConsts.InstanceIndex, new BuiltInAttribute("gl_InstanceIndex", VariableType.S32) }, + { AttributeConsts.VertexIndex, new BuiltInAttribute("gl_VertexIndex", VariableType.S32) }, { AttributeConsts.FrontFacing, new BuiltInAttribute("gl_FrontFacing", VariableType.Bool) }, // Special. diff --git a/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs index 9f8dd7dfa..c007b9a20 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs @@ -704,8 +704,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv AttributeConsts.ClipDistance0 => BuiltIn.ClipDistance, AttributeConsts.PointCoordX => BuiltIn.PointCoord, AttributeConsts.TessCoordX => BuiltIn.TessCoord, - AttributeConsts.InstanceId => BuiltIn.InstanceId, // FIXME: Invalid - AttributeConsts.VertexId => BuiltIn.VertexId, // FIXME: Invalid + AttributeConsts.InstanceId => BuiltIn.InstanceId, + AttributeConsts.VertexId => BuiltIn.VertexId, + AttributeConsts.BaseInstance => BuiltIn.BaseInstance, + AttributeConsts.BaseVertex => BuiltIn.BaseVertex, + AttributeConsts.InstanceIndex => BuiltIn.InstanceIndex, + AttributeConsts.VertexIndex => BuiltIn.VertexIndex, AttributeConsts.FrontFacing => BuiltIn.FrontFacing, AttributeConsts.FragmentOutputDepth => BuiltIn.FragDepth, AttributeConsts.ThreadKill => BuiltIn.HelperInvocation, diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs index 7edf5deb8..2f75d248b 100644 --- a/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs +++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs @@ -51,7 +51,7 @@ namespace Ryujinx.Graphics.Shader.Instructions offset |= AttributeConsts.LoadOutputMask; } - Operand src = op.P ? AttributePerPatch(offset) : Attribute(offset); + Operand src = op.P ? AttributePerPatch(offset) : CreateInputAttribute(context, offset); context.Copy(Register(rd), src); } @@ -312,5 +312,22 @@ namespace Ryujinx.Graphics.Shader.Instructions return attr; } + + private static Operand CreateInputAttribute(EmitterContext context, int attr) + { + if (context.Config.Options.TargetApi == TargetApi.Vulkan) + { + if (attr == AttributeConsts.InstanceId) + { + return context.ISubtract(Attribute(AttributeConsts.InstanceIndex), Attribute(AttributeConsts.BaseInstance)); + } + else if (attr == AttributeConsts.VertexId) + { + return Attribute(AttributeConsts.VertexIndex); + } + } + + return Attribute(attr); + } } } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/Translation/AttributeConsts.cs b/Ryujinx.Graphics.Shader/Translation/AttributeConsts.cs index f4e39d0da..47367f89c 100644 --- a/Ryujinx.Graphics.Shader/Translation/AttributeConsts.cs +++ b/Ryujinx.Graphics.Shader/Translation/AttributeConsts.cs @@ -95,5 +95,10 @@ namespace Ryujinx.Graphics.Shader.Translation public const int LtMask = 0x2000040; public const int ThreadKill = 0x2000044; + + public const int BaseInstance = 0x2000050; + public const int BaseVertex = 0x2000054; + public const int InstanceIndex = 0x2000058; + public const int VertexIndex = 0x200005c; } } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/Translation/AttributeInfo.cs b/Ryujinx.Graphics.Shader/Translation/AttributeInfo.cs index 35dd56e82..6e95722ff 100644 --- a/Ryujinx.Graphics.Shader/Translation/AttributeInfo.cs +++ b/Ryujinx.Graphics.Shader/Translation/AttributeInfo.cs @@ -27,6 +27,10 @@ namespace Ryujinx.Graphics.Shader.Translation { AttributeConsts.TessCoordY, new AttributeInfo(AttributeConsts.TessCoordX, 1, 3, AggregateType.Vector | AggregateType.FP32) }, { AttributeConsts.InstanceId, new AttributeInfo(AttributeConsts.InstanceId, 0, 1, AggregateType.S32) }, { AttributeConsts.VertexId, new AttributeInfo(AttributeConsts.VertexId, 0, 1, AggregateType.S32) }, + { AttributeConsts.BaseInstance, new AttributeInfo(AttributeConsts.BaseInstance, 0, 1, AggregateType.S32) }, + { AttributeConsts.BaseVertex, new AttributeInfo(AttributeConsts.BaseVertex, 0, 1, AggregateType.S32) }, + { AttributeConsts.InstanceIndex, new AttributeInfo(AttributeConsts.InstanceIndex, 0, 1, AggregateType.S32) }, + { AttributeConsts.VertexIndex, new AttributeInfo(AttributeConsts.VertexIndex, 0, 1, AggregateType.S32) }, { AttributeConsts.FrontFacing, new AttributeInfo(AttributeConsts.FrontFacing, 0, 1, AggregateType.Bool) }, // Special.