diff options
Diffstat (limited to 'Ryujinx.Graphics.Shader/CodeGen')
7 files changed, 128 insertions, 281 deletions
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs index e20df384..f0f8ea35 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs @@ -1,7 +1,5 @@ -using Ryujinx.Graphics.Shader.IntermediateRepresentation; using Ryujinx.Graphics.Shader.StructuredIr; using Ryujinx.Graphics.Shader.Translation; -using System.Collections.Generic; using System.Text; namespace Ryujinx.Graphics.Shader.CodeGen.Glsl @@ -10,22 +8,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { public const string Tab = " "; - private readonly StructuredProgramInfo _info; - public StructuredFunction CurrentFunction { get; set; } public ShaderConfig Config { get; } - public bool CbIndexable => _info.UsesCbIndexing; - - public List<BufferDescriptor> CBufferDescriptors { get; } - public List<BufferDescriptor> SBufferDescriptors { get; } - public List<TextureDescriptor> TextureDescriptors { get; } - public List<TextureDescriptor> ImageDescriptors { get; } - public OperandManager OperandManager { get; } - private StringBuilder _sb; + private readonly StructuredProgramInfo _info; + + private readonly StringBuilder _sb; private int _level; @@ -36,11 +27,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl _info = info; Config = config; - CBufferDescriptors = new List<BufferDescriptor>(); - SBufferDescriptors = new List<BufferDescriptor>(); - TextureDescriptors = new List<TextureDescriptor>(); - ImageDescriptors = new List<TextureDescriptor>(); - OperandManager = new OperandManager(); _sb = new StringBuilder(); @@ -84,23 +70,32 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl AppendLine("}" + suffix); } - private int FindDescriptorIndex(List<TextureDescriptor> list, AstTextureOperation texOp) + private static int FindDescriptorIndex(TextureDescriptor[] array, AstTextureOperation texOp) { - return list.FindIndex(descriptor => - descriptor.Type == texOp.Type && - descriptor.CbufSlot == texOp.CbufSlot && - descriptor.HandleIndex == texOp.Handle && - descriptor.Format == texOp.Format); + for (int i = 0; i < array.Length; i++) + { + var descriptor = array[i]; + + if (descriptor.Type == texOp.Type && + descriptor.CbufSlot == texOp.CbufSlot && + descriptor.HandleIndex == texOp.Handle && + descriptor.Format == texOp.Format) + { + return i; + } + } + + return -1; } public int FindTextureDescriptorIndex(AstTextureOperation texOp) { - return FindDescriptorIndex(TextureDescriptors, texOp); + return FindDescriptorIndex(Config.GetTextureDescriptors(), texOp); } public int FindImageDescriptorIndex(AstTextureOperation texOp) { - return FindDescriptorIndex(ImageDescriptors, texOp); + return FindDescriptorIndex(Config.GetImageDescriptors(), texOp); } public StructuredFunction GetFunction(int id) diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs index 94487da0..6e67b682 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs @@ -70,30 +70,34 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl context.AppendLine(); } - if (info.CBuffers.Count != 0) + var cBufferDescriptors = context.Config.GetConstantBufferDescriptors(); + if (cBufferDescriptors.Length != 0) { - DeclareUniforms(context, info); + DeclareUniforms(context, cBufferDescriptors); context.AppendLine(); } - if (info.SBuffers.Count != 0) + var sBufferDescriptors = context.Config.GetStorageBufferDescriptors(); + if (sBufferDescriptors.Length != 0) { - DeclareStorages(context, info); + DeclareStorages(context, sBufferDescriptors); context.AppendLine(); } - if (info.Samplers.Count != 0) + var textureDescriptors = context.Config.GetTextureDescriptors(); + if (textureDescriptors.Length != 0) { - DeclareSamplers(context, info); + DeclareSamplers(context, textureDescriptors); context.AppendLine(); } - if (info.Images.Count != 0) + var imageDescriptors = context.Config.GetImageDescriptors(); + if (imageDescriptors.Length != 0) { - DeclareImages(context, info); + DeclareImages(context, imageDescriptors); context.AppendLine(); } @@ -246,58 +250,40 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl throw new ArgumentException($"Invalid variable type \"{type}\"."); } - private static void DeclareUniforms(CodeGenContext context, StructuredProgramInfo info) + private static void DeclareUniforms(CodeGenContext context, BufferDescriptor[] descriptors) { string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 16) + "]"; - if (info.UsesCbIndexing) + if (context.Config.UsedFeatures.HasFlag(FeatureFlags.CbIndexing)) { - int count = info.CBuffers.Max() + 1; - - int[] bindings = new int[count]; - - for (int i = 0; i < count; i++) - { - bindings[i] = context.Config.Counts.IncrementUniformBuffersCount(); - } - - foreach (int cbufSlot in info.CBuffers.OrderBy(x => x)) - { - context.CBufferDescriptors.Add(new BufferDescriptor(bindings[cbufSlot], cbufSlot)); - } - string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage); ubName += "_" + DefaultNames.UniformNamePrefix; string blockName = $"{ubName}_{DefaultNames.BlockSuffix}"; - context.AppendLine($"layout (binding = {bindings[0]}, std140) uniform {blockName}"); + context.AppendLine($"layout (binding = {descriptors[0].Binding}, std140) uniform {blockName}"); context.EnterScope(); context.AppendLine("vec4 " + DefaultNames.DataName + ubSize + ";"); - context.LeaveScope($" {ubName}[{NumberFormatter.FormatInt(count)}];"); + context.LeaveScope($" {ubName}[{NumberFormatter.FormatInt(descriptors.Length)}];"); } else { - foreach (int cbufSlot in info.CBuffers.OrderBy(x => x)) + foreach (var descriptor in descriptors) { - int binding = context.Config.Counts.IncrementUniformBuffersCount(); - - context.CBufferDescriptors.Add(new BufferDescriptor(binding, cbufSlot)); - string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage); - ubName += "_" + DefaultNames.UniformNamePrefix + cbufSlot; + ubName += "_" + DefaultNames.UniformNamePrefix + descriptor.Slot; - context.AppendLine($"layout (binding = {binding}, std140) uniform {ubName}"); + context.AppendLine($"layout (binding = {descriptor.Binding}, std140) uniform {ubName}"); context.EnterScope(); - context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, cbufSlot, false) + ubSize + ";"); + context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, descriptor.Slot, false) + ubSize + ";"); context.LeaveScope(";"); } } } - private static void DeclareStorages(CodeGenContext context, StructuredProgramInfo info) + private static void DeclareStorages(CodeGenContext context, BufferDescriptor[] descriptors) { string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage); @@ -305,130 +291,81 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl string blockName = $"{sbName}_{DefaultNames.BlockSuffix}"; - int count = info.SBuffers.Max() + 1; - - int[] bindings = new int[count]; - - for (int i = 0; i < count; i++) - { - bindings[i] = context.Config.Counts.IncrementStorageBuffersCount(); - } - - foreach (int sbufSlot in info.SBuffers) - { - context.SBufferDescriptors.Add(new BufferDescriptor(bindings[sbufSlot], sbufSlot)); - } - - context.AppendLine($"layout (binding = {bindings[0]}, std430) buffer {blockName}"); + context.AppendLine($"layout (binding = {descriptors[0].Binding}, std430) buffer {blockName}"); context.EnterScope(); context.AppendLine("uint " + DefaultNames.DataName + "[];"); - context.LeaveScope($" {sbName}[{NumberFormatter.FormatInt(count)}];"); + context.LeaveScope($" {sbName}[{NumberFormatter.FormatInt(descriptors.Length)}];"); } - private static void DeclareSamplers(CodeGenContext context, StructuredProgramInfo info) + private static void DeclareSamplers(CodeGenContext context, TextureDescriptor[] descriptors) { - HashSet<string> samplers = new HashSet<string>(); - - // Texture instructions other than TextureSample (like TextureSize) - // may have incomplete sampler type information. In those cases, - // we prefer instead the more accurate information from the - // TextureSample instruction, if both are available. - foreach (AstTextureOperation texOp in info.Samplers.OrderBy(x => x.Handle * 2 + (x.Inst == Instruction.TextureSample ? 0 : 1))) + int arraySize = 0; + foreach (var descriptor in descriptors) { - string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize); - - string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); - - if ((texOp.Flags & TextureFlags.Bindless) != 0 || !samplers.Add(samplerName)) - { - continue; - } - - int firstBinding = -1; - - if ((texOp.Type & SamplerType.Indexed) != 0) + if (descriptor.Type.HasFlag(SamplerType.Indexed)) { - for (int index = 0; index < texOp.ArraySize; index++) + if (arraySize == 0) { - int binding = context.Config.Counts.IncrementTexturesCount(); - - if (firstBinding < 0) - { - firstBinding = binding; - } - - var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2); - - context.TextureDescriptors.Add(desc); + arraySize = ShaderConfig.SamplerArraySize; + } + else if (--arraySize != 0) + { + continue; } } - else - { - firstBinding = context.Config.Counts.IncrementTexturesCount(); - var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle); + string indexExpr = NumberFormatter.FormatInt(arraySize); - context.TextureDescriptors.Add(desc); - } + string samplerName = OperandManager.GetSamplerName( + context.Config.Stage, + descriptor.CbufSlot, + descriptor.HandleIndex, + descriptor.Type.HasFlag(SamplerType.Indexed), + indexExpr); - string samplerTypeName = texOp.Type.ToGlslSamplerType(); + string samplerTypeName = descriptor.Type.ToGlslSamplerType(); - context.AppendLine($"layout (binding = {firstBinding}) uniform {samplerTypeName} {samplerName};"); + context.AppendLine($"layout (binding = {descriptor.Binding}) uniform {samplerTypeName} {samplerName};"); } } - private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info) + private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors) { - HashSet<string> images = new HashSet<string>(); - - foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle)) + int arraySize = 0; + foreach (var descriptor in descriptors) { - string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize); - - string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr); - - if ((texOp.Flags & TextureFlags.Bindless) != 0 || !images.Add(imageName)) + if (descriptor.Type.HasFlag(SamplerType.Indexed)) { - continue; - } - - int firstBinding = -1; - - if ((texOp.Type & SamplerType.Indexed) != 0) - { - for (int index = 0; index < texOp.ArraySize; index++) + if (arraySize == 0) { - int binding = context.Config.Counts.IncrementImagesCount(); - - if (firstBinding < 0) - { - firstBinding = binding; - } - - var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2); - - context.ImageDescriptors.Add(desc); + arraySize = ShaderConfig.SamplerArraySize; + } + else if (--arraySize != 0) + { + continue; } } - else - { - firstBinding = context.Config.Counts.IncrementImagesCount(); - var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle); + string indexExpr = NumberFormatter.FormatInt(arraySize); - context.ImageDescriptors.Add(desc); - } + string imageName = OperandManager.GetImageName( + context.Config.Stage, + descriptor.CbufSlot, + descriptor.HandleIndex, + descriptor.Format, + descriptor.Type.HasFlag(SamplerType.Indexed), + indexExpr); - string layout = texOp.Format.ToGlslFormat(); + string layout = descriptor.Format.ToGlslFormat(); if (!string.IsNullOrEmpty(layout)) { layout = ", " + layout; } - string imageTypeName = texOp.Type.ToGlslImageType(texOp.Format.GetComponentType()); + string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType()); - context.AppendLine($"layout (binding = {firstBinding}{layout}) uniform {imageTypeName} {imageName};"); + context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {imageTypeName} {imageName};"); } } @@ -528,7 +465,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage); - int scaleElements = context.TextureDescriptors.Count + context.ImageDescriptors.Count; + int scaleElements = context.Config.GetTextureDescriptors().Length + context.Config.GetImageDescriptors().Length; if (context.Config.Stage == ShaderStage.Fragment) { diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs index 37a1cd89..c430a21a 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs @@ -12,7 +12,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { private const string MainFunctionName = "main"; - public static GlslProgram Generate(StructuredProgramInfo info, ShaderConfig config) + public static string Generate(StructuredProgramInfo info, ShaderConfig config) { CodeGenContext context = new CodeGenContext(info, config); @@ -37,12 +37,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl PrintFunction(context, info, info.Functions[0], MainFunctionName); - return new GlslProgram( - context.CBufferDescriptors.ToArray(), - context.SBufferDescriptors.ToArray(), - context.TextureDescriptors.ToArray(), - context.ImageDescriptors.ToArray(), - context.GetCode()); + return context.GetCode(); } private static void PrintFunction(CodeGenContext context, StructuredProgramInfo info, StructuredFunction function, string funcName = null) diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslProgram.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslProgram.cs deleted file mode 100644 index 31b7f312..00000000 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslProgram.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Ryujinx.Graphics.Shader.CodeGen.Glsl -{ - class GlslProgram - { - public BufferDescriptor[] CBufferDescriptors { get; } - public BufferDescriptor[] SBufferDescriptors { get; } - public TextureDescriptor[] TextureDescriptors { get; } - public TextureDescriptor[] ImageDescriptors { get; } - - public string Code { get; } - - public GlslProgram( - BufferDescriptor[] cBufferDescriptors, - BufferDescriptor[] sBufferDescriptors, - TextureDescriptor[] textureDescriptors, - TextureDescriptor[] imageDescriptors, - string code) - { - CBufferDescriptors = cBufferDescriptors; - SBufferDescriptors = sBufferDescriptors; - TextureDescriptors = textureDescriptors; - ImageDescriptors = imageDescriptors; - Code = code; - } - } -}
\ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs index 0ea7f151..d5cd0f72 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs @@ -20,7 +20,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions } else if (node is AstOperand operand) { - return context.OperandManager.GetExpression(operand, context.Config, context.CbIndexable); + return context.OperandManager.GetExpression(operand, context.Config); } throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\"."); @@ -62,7 +62,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions switch (memRegion) { case Instruction.MrShared: args += LoadShared(context, operation); break; - case Instruction.MrStorage: args += LoadStorage(context, operation, forAtomic: true); break; + case Instruction.MrStorage: args += LoadStorage(context, operation); break; default: throw new InvalidOperationException($"Invalid memory region \"{memRegion}\"."); } diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs index 273aaef8..911c7b05 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs @@ -56,7 +56,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions string ApplyScaling(string vector) { int index = context.FindImageDescriptorIndex(texOp); - TextureUsageFlags flags = TextureUsageFlags.NeedsScaleValue; if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) && texOp.Inst == Instruction.ImageLoad && @@ -64,7 +63,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions !isIndexed) { // Image scales start after texture ones. - int scaleIndex = context.TextureDescriptors.Count + index; + int scaleIndex = context.Config.GetTextureDescriptors().Length + index; if (pCount == 3 && isArray) { @@ -75,19 +74,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions { vector = "Helper_TexelFetchScale(" + vector + ", " + scaleIndex + ")"; } - else - { - flags |= TextureUsageFlags.ResScaleUnsupported; - } - } - else - { - flags |= TextureUsageFlags.ResScaleUnsupported; - } - - if (!isBindless) - { - context.ImageDescriptors[index] = context.ImageDescriptors[index].SetFlag(flags); } return vector; @@ -112,7 +98,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions if (texOp.Inst == Instruction.ImageStore) { int texIndex = context.FindImageDescriptorIndex(texOp); - context.ImageDescriptors[texIndex] = context.ImageDescriptors[texIndex].SetFlag(TextureUsageFlags.ImageStore); VariableType type = texOp.Format.GetComponentType(); @@ -176,12 +161,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions if (src1 is AstOperand oper && oper.Type == OperandType.Constant) { - return OperandManager.GetConstantBufferName(oper.Value, offsetExpr, context.Config.Stage, context.CbIndexable); + bool cbIndexable = context.Config.UsedFeatures.HasFlag(Translation.FeatureFlags.CbIndexing); + return OperandManager.GetConstantBufferName(oper.Value, offsetExpr, context.Config.Stage, cbIndexable); } else { string slotExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0)); - return OperandManager.GetConstantBufferName(slotExpr, offsetExpr, context.Config.Stage); } } @@ -205,7 +190,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions return $"{arrayName}[{offsetExpr}]"; } - public static string LoadStorage(CodeGenContext context, AstOperation operation, bool forAtomic = false) + public static string LoadStorage(CodeGenContext context, AstOperation operation) { IAstNode src1 = operation.GetSource(0); IAstNode src2 = operation.GetSource(1); @@ -213,11 +198,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions string indexExpr = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0)); string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1)); - if (forAtomic) - { - SetStorageWriteFlag(context, src1, context.Config.Stage); - } - return GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage); } @@ -306,7 +286,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions string src = TypeConversion.ReinterpretCast(context, src3, srcType, VariableType.U32); - SetStorageWriteFlag(context, src1, context.Config.Stage); string sb = GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage); return $"{sb} = {src}"; @@ -471,7 +450,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions if (intCoords) { int index = context.FindTextureDescriptorIndex(texOp); - TextureUsageFlags flags = TextureUsageFlags.NeedsScaleValue; if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) && !isBindless && @@ -486,22 +464,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions { vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")"; } - else - { - flags |= TextureUsageFlags.ResScaleUnsupported; - } - } - else - { - // Resolution scaling cannot be applied to this texture right now. - // Flag so that we know to blacklist scaling on related textures when binding them. - - flags |= TextureUsageFlags.ResScaleUnsupported; - } - - if (!isBindless) - { - context.TextureDescriptors[index] = context.TextureDescriptors[index].SetFlag(flags); } } @@ -638,32 +600,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions } } - private static void SetStorageWriteFlag(CodeGenContext context, IAstNode indexExpr, ShaderStage stage) - { - // Attempt to find a BufferDescriptor with the given index. - // If it cannot be resolved or is not constant, assume that the slot expression could potentially index any of them, - // and set the flag on all storage buffers. - - int index = -1; - - if (indexExpr is AstOperand operand && operand.Type == OperandType.Constant) - { - index = context.SBufferDescriptors.FindIndex(buffer => buffer.Slot == operand.Value); - } - - if (index != -1) - { - context.SBufferDescriptors[index] = context.SBufferDescriptors[index].SetFlag(BufferUsageFlags.Write); - } - else - { - for (int i = 0; i < context.SBufferDescriptors.Count; i++) - { - context.SBufferDescriptors[i] = context.SBufferDescriptors[i].SetFlag(BufferUsageFlags.Write); - } - } - } - private static string GetStorageBufferAccessor(string slotExpr, string offsetExpr, ShaderStage stage) { string sbName = OperandManager.GetShaderStagePrefix(stage); diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs index 61f20082..9e79a080 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs @@ -94,30 +94,22 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl return name; } - public string GetExpression(AstOperand operand, ShaderConfig config, bool cbIndexable) + public string GetExpression(AstOperand operand, ShaderConfig config) { - switch (operand.Type) + return operand.Type switch { - case OperandType.Argument: - return GetArgumentName(operand.Value); - - case OperandType.Attribute: - return GetAttributeName(operand, config); - - case OperandType.Constant: - return NumberFormatter.FormatInt(operand.Value); - - case OperandType.ConstantBuffer: - return GetConstantBufferName(operand.CbufSlot, operand.CbufOffset, config.Stage, cbIndexable); - - case OperandType.LocalVariable: - return _locals[operand]; - - case OperandType.Undefined: - return DefaultNames.UndefinedName; - } - - throw new ArgumentException($"Invalid operand type \"{operand.Type}\"."); + OperandType.Argument => GetArgumentName(operand.Value), + OperandType.Attribute => GetAttributeName(operand, config), + OperandType.Constant => NumberFormatter.FormatInt(operand.Value), + OperandType.ConstantBuffer => GetConstantBufferName( + operand.CbufSlot, + operand.CbufOffset, + config.Stage, + config.UsedFeatures.HasFlag(FeatureFlags.CbIndexing)), + OperandType.LocalVariable => _locals[operand], + OperandType.Undefined => DefaultNames.UndefinedName, + _ => throw new ArgumentException($"Invalid operand type \"{operand.Type}\".") + }; } public static string GetConstantBufferName(int slot, int offset, ShaderStage stage, bool cbIndexable) @@ -242,9 +234,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl public static string GetSamplerName(ShaderStage stage, AstTextureOperation texOp, string indexExpr) { - string suffix = texOp.CbufSlot < 0 ? $"_tcb_{texOp.Handle:X}" : $"_cb{texOp.CbufSlot}_{texOp.Handle:X}"; + return GetSamplerName(stage, texOp.CbufSlot, texOp.Handle, texOp.Type.HasFlag(SamplerType.Indexed), indexExpr); + } + + public static string GetSamplerName(ShaderStage stage, int cbufSlot, int handle, bool indexed, string indexExpr) + { + string suffix = cbufSlot < 0 ? $"_tcb_{handle:X}" : $"_cb{cbufSlot}_{handle:X}"; - if ((texOp.Type & SamplerType.Indexed) != 0) + if (indexed) { suffix += $"a[{indexExpr}]"; } @@ -254,9 +251,22 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl public static string GetImageName(ShaderStage stage, AstTextureOperation texOp, string indexExpr) { - string suffix = texOp.CbufSlot < 0 ? $"_tcb_{texOp.Handle:X}_{texOp.Format.ToGlslFormat()}" : $"_cb{texOp.CbufSlot}_{texOp.Handle:X}_{texOp.Format.ToGlslFormat()}"; + return GetImageName(stage, texOp.CbufSlot, texOp.Handle, texOp.Format, texOp.Type.HasFlag(SamplerType.Indexed), indexExpr); + } + + public static string GetImageName( + ShaderStage stage, + int cbufSlot, + int handle, + TextureFormat format, + bool indexed, + string indexExpr) + { + string suffix = cbufSlot < 0 + ? $"_tcb_{handle:X}_{format.ToGlslFormat()}" + : $"_cb{cbufSlot}_{handle:X}_{format.ToGlslFormat()}"; - if ((texOp.Type & SamplerType.Indexed) != 0) + if (indexed) { suffix += $"a[{indexExpr}]"; } |
