From 1df78e7ad65459d990fa2dcf391da1296dfd886c Mon Sep 17 00:00:00 2001 From: gdk Date: Sun, 24 Nov 2019 14:20:48 -0300 Subject: [PATCH] Simplify shader uniform buffer access codegen --- .../CodeGen/Glsl/Declarations.cs | 6 ++-- .../Glsl/Instructions/InstGenMemory.cs | 9 ++++-- .../CodeGen/Glsl/OperandManager.cs | 28 ++++++------------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs index e8b449612..bedfbaeec 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs @@ -206,13 +206,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl context.CBufferDescriptors.Add(new BufferDescriptor(ubName, cbufSlot)); - context.AppendLine("layout (std140) uniform " + ubName); + context.AppendLine("uniform " + ubName); context.EnterScope(); - string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 16) + "]"; + string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 4) + "]"; - context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, cbufSlot) + ubSize + ";"); + context.AppendLine("precise float " + OperandManager.GetUbName(context.Config.Stage, cbufSlot) + ubSize + ";"); context.LeaveScope(";"); } diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs index c535d8fcf..2d070e08f 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs @@ -116,7 +116,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions offsetExpr = Enclose(offsetExpr, src2, Instruction.ShiftRightS32, isLhs: true); - return OperandManager.GetConstantBufferName(src1, offsetExpr, context.Config.Stage); + // TODO: For now this is assumed to be constant + // (we only use constant slots right now), but we should also + // support non-constant values, necessary for full LDC implementation. + int slot = ((AstOperand)src1).Value; + + return OperandManager.GetUniformBufferAccessor(slot, offsetExpr, context.Config.Stage); } public static string LoadGlobal(CodeGenContext context, AstOperation operation) @@ -503,7 +508,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions // restrictions. int ubOffset = GlobalToStorage.GetStorageCbOffset(stage, slot); - string ubName = OperandManager.GetConstantBufferName(0, ubOffset, stage); + string ubName = OperandManager.GetUniformBufferAccessor(0, ubOffset, stage); offsetExpr = $"{offsetExpr} - int((floatBitsToUint({ubName}) & {mask}) >> 2)"; diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs index 4c9d5b550..802b32e1c 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs @@ -93,7 +93,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl return NumberFormatter.FormatInt(operand.Value); case OperandType.ConstantBuffer: - return GetConstantBufferName(operand.CbufSlot, operand.CbufOffset, stage); + return GetUniformBufferAccessor(operand.CbufSlot, operand.CbufOffset, stage); case OperandType.LocalVariable: return _locals[operand]; @@ -105,28 +105,16 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl throw new ArgumentException($"Invalid operand type \"{operand.Type}\"."); } - public static string GetConstantBufferName(int slot, int offset, ShaderStage stage) + public static string GetUniformBufferAccessor(int slot, int offset, ShaderStage stage) + { + return GetUniformBufferAccessor(slot, NumberFormatter.FormatInt(offset), stage); + } + + public static string GetUniformBufferAccessor(int slot, string offsetExpr, ShaderStage stage) { string ubName = GetUbName(stage, slot); - ubName += "[" + (offset >> 2) + "]"; - - return ubName + "." + GetSwizzleMask(offset & 3); - } - - public static string GetConstantBufferName(IAstNode slot, string offsetExpr, ShaderStage stage) - { - // Non-constant slots are not supported. - // It is expected that upstream stages are never going to generate non-constant - // slot access. - AstOperand operand = (AstOperand)slot; - - string ubName = GetUbName(stage, operand.Value); - - string index0 = "[" + offsetExpr + " >> 2]"; - string index1 = "[" + offsetExpr + " & 3]"; - - return ubName + index0 + index1; + return $"{ubName}[{offsetExpr}]"; } public static string GetOutAttributeName(AstOperand attr, ShaderStage stage)