diff options
Diffstat (limited to 'src/Ryujinx.Graphics.Shader/CodeGen/Glsl')
4 files changed, 59 insertions, 117 deletions
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs index 94b850e7..2370b49f 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs @@ -75,22 +75,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl DeclareStorageBuffers(context, context.Config.Properties.StorageBuffers.Values); DeclareMemories(context, context.Config.Properties.LocalMemories.Values, isShared: false); DeclareMemories(context, context.Config.Properties.SharedMemories.Values, isShared: true); - - var textureDescriptors = context.Config.GetTextureDescriptors(); - if (textureDescriptors.Length != 0) - { - DeclareSamplers(context, textureDescriptors); - - context.AppendLine(); - } - - var imageDescriptors = context.Config.GetImageDescriptors(); - if (imageDescriptors.Length != 0) - { - DeclareImages(context, imageDescriptors); - - context.AppendLine(); - } + DeclareSamplers(context, context.Config.Properties.Textures.Values); + DeclareImages(context, context.Config.Properties.Images.Values); if (context.Config.Stage != ShaderStage.Compute) { @@ -369,80 +355,71 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } } - private static void DeclareSamplers(CodeGenContext context, TextureDescriptor[] descriptors) + private static void DeclareSamplers(CodeGenContext context, IEnumerable<TextureDefinition> definitions) { int arraySize = 0; - foreach (var descriptor in descriptors) + + foreach (var definition in definitions) { - if (descriptor.Type.HasFlag(SamplerType.Indexed)) + string indexExpr = string.Empty; + + if (definition.Type.HasFlag(SamplerType.Indexed)) { if (arraySize == 0) { - arraySize = ShaderConfig.SamplerArraySize; + arraySize = ResourceManager.SamplerArraySize; } else if (--arraySize != 0) { continue; } - } - string indexExpr = NumberFormatter.FormatInt(arraySize); - - string samplerName = OperandManager.GetSamplerName( - context.Config.Stage, - descriptor.CbufSlot, - descriptor.HandleIndex, - descriptor.Type.HasFlag(SamplerType.Indexed), - indexExpr); + indexExpr = $"[{NumberFormatter.FormatInt(arraySize)}]"; + } - string samplerTypeName = descriptor.Type.ToGlslSamplerType(); + string samplerTypeName = definition.Type.ToGlslSamplerType(); string layout = string.Empty; if (context.Config.Options.TargetApi == TargetApi.Vulkan) { - layout = ", set = 2"; + layout = $", set = {definition.Set}"; } - context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {samplerTypeName} {samplerName};"); + context.AppendLine($"layout (binding = {definition.Binding}{layout}) uniform {samplerTypeName} {definition.Name}{indexExpr};"); } } - private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors) + private static void DeclareImages(CodeGenContext context, IEnumerable<TextureDefinition> definitions) { int arraySize = 0; - foreach (var descriptor in descriptors) + + foreach (var definition in definitions) { - if (descriptor.Type.HasFlag(SamplerType.Indexed)) + string indexExpr = string.Empty; + + if (definition.Type.HasFlag(SamplerType.Indexed)) { if (arraySize == 0) { - arraySize = ShaderConfig.SamplerArraySize; + arraySize = ResourceManager.SamplerArraySize; } else if (--arraySize != 0) { continue; } - } - string indexExpr = NumberFormatter.FormatInt(arraySize); - - string imageName = OperandManager.GetImageName( - context.Config.Stage, - descriptor.CbufSlot, - descriptor.HandleIndex, - descriptor.Format, - descriptor.Type.HasFlag(SamplerType.Indexed), - indexExpr); + indexExpr = $"[{NumberFormatter.FormatInt(arraySize)}]"; + } - string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType()); + string imageTypeName = definition.Type.ToGlslImageType(definition.Format.GetComponentType()); - if (descriptor.Flags.HasFlag(TextureUsageFlags.ImageCoherent)) + if (definition.Flags.HasFlag(TextureUsageFlags.ImageCoherent)) { imageTypeName = "coherent " + imageTypeName; } - string layout = descriptor.Format.ToGlslFormat(); + string layout = definition.Format.ToGlslFormat(); if (!string.IsNullOrEmpty(layout)) { @@ -451,10 +428,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl if (context.Config.Options.TargetApi == TargetApi.Vulkan) { - layout = $", set = 3{layout}"; + layout = $", set = {definition.Set}{layout}"; } - context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {imageTypeName} {imageName};"); + context.AppendLine($"layout (binding = {definition.Binding}{layout}) uniform {imageTypeName} {definition.Name}{indexExpr};"); } } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/DefaultNames.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/DefaultNames.cs index 842228ed..54bf9aeb 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/DefaultNames.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/DefaultNames.cs @@ -4,9 +4,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { public const string LocalNamePrefix = "temp"; - public const string SamplerNamePrefix = "tex"; - public const string ImageNamePrefix = "img"; - public const string PerPatchAttributePrefix = "patch_attr_"; public const string IAttributePrefix = "in_attr"; public const string OAttributePrefix = "out_attr"; diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs index e0faed29..7e6d8bb5 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs @@ -84,7 +84,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = Src(AggregateType.S32); } - string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr); + string imageName = GetImageName(context.Config, texOp, indexExpr); texCallBuilder.Append('('); texCallBuilder.Append(imageName); @@ -216,7 +216,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32); } - string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); + string samplerName = GetSamplerName(context.Config, texOp, indexExpr); int coordsIndex = isBindless || isIndexed ? 1 : 0; @@ -342,7 +342,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = Src(AggregateType.S32); } - string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); + string samplerName = GetSamplerName(context.Config, texOp, indexExpr); texCall += "(" + samplerName; @@ -538,7 +538,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32); } - string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); + string samplerName = GetSamplerName(context.Config, texOp, indexExpr); if (texOp.Index == 3) { @@ -546,8 +546,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions } else { - TextureDescriptor descriptor = context.Config.FindTextureDescriptor(texOp); - bool hasLod = !descriptor.Type.HasFlag(SamplerType.Multisample) && descriptor.Type != SamplerType.TextureBuffer; + context.Config.Properties.Textures.TryGetValue(texOp.Binding, out TextureDefinition definition); + bool hasLod = !definition.Type.HasFlag(SamplerType.Multisample) && (definition.Type & SamplerType.Mask) != SamplerType.TextureBuffer; string texCall; if (hasLod) @@ -715,6 +715,30 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions return varName; } + private static string GetSamplerName(ShaderConfig config, AstTextureOperation texOp, string indexExpr) + { + string name = config.Properties.Textures[texOp.Binding].Name; + + if (texOp.Type.HasFlag(SamplerType.Indexed)) + { + name = $"{name}[{indexExpr}]"; + } + + return name; + } + + private static string GetImageName(ShaderConfig config, AstTextureOperation texOp, string indexExpr) + { + string name = config.Properties.Images[texOp.Binding].Name; + + if (texOp.Type.HasFlag(SamplerType.Indexed)) + { + name = $"{name}[{indexExpr}]"; + } + + return name; + } + private static string GetMask(int index) { return $".{"rgba".AsSpan(index, 1)}"; diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs index 0ca3b55f..17ffad9a 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs @@ -11,9 +11,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { class OperandManager { - private static readonly string[] _stagePrefixes = new string[] { "cp", "vp", "tcp", "tep", "gp", "fp" }; - - private readonly Dictionary<AstOperand, string> _locals; + private Dictionary<AstOperand, string> _locals; public OperandManager() { @@ -41,60 +39,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl }; } - public static string GetSamplerName(ShaderStage stage, AstTextureOperation texOp, string indexExpr) - { - 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 (indexed) - { - suffix += $"a[{indexExpr}]"; - } - - return GetShaderStagePrefix(stage) + "_" + DefaultNames.SamplerNamePrefix + suffix; - } - - public static string GetImageName(ShaderStage stage, AstTextureOperation texOp, string indexExpr) - { - 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 (indexed) - { - suffix += $"a[{indexExpr}]"; - } - - return GetShaderStagePrefix(stage) + "_" + DefaultNames.ImageNamePrefix + suffix; - } - - public static string GetShaderStagePrefix(ShaderStage stage) - { - int index = (int)stage; - - if ((uint)index >= _stagePrefixes.Length) - { - return "invalid"; - } - - return _stagePrefixes[index]; - } - public static string GetArgumentName(int argIndex) { return $"{DefaultNames.ArgumentNamePrefix}{argIndex}"; |
