diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2020-11-09 19:35:04 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-09 19:35:04 -0300 |
| commit | 934a78005e75653529c320cf90e78fe6536447c2 (patch) | |
| tree | a0601ef1abfbace3c35c10fb048d2e33447054c2 /Ryujinx.Graphics.Shader | |
| parent | eda6b78894eef3d9dc1e8ea6984e2f5bd319d68e (diff) | |
Simplify logic for bindless texture handling (#1667)
* Simplify logic for bindless texture handling
* Nits
Diffstat (limited to 'Ryujinx.Graphics.Shader')
| -rw-r--r-- | Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs | 11 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs | 24 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs | 36 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs | 19 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs | 16 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs | 3 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs | 1 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/TextureDescriptor.cs | 33 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/Translation/Optimizations/BindlessElimination.cs | 29 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/Translation/Rewriter.cs (renamed from Ryujinx.Graphics.Shader/Translation/Lowering.cs) | 2 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Shader/Translation/Translator.cs | 2 |
11 files changed, 86 insertions, 90 deletions
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs index f86e82da..e20df384 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs @@ -86,18 +86,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl private int FindDescriptorIndex(List<TextureDescriptor> list, AstTextureOperation texOp) { - AstOperand operand = texOp.GetSource(0) as AstOperand; - bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0; - - int cBufSlot = bindless ? operand.CbufSlot : 0; - int cBufOffset = bindless ? operand.CbufOffset : 0; - return list.FindIndex(descriptor => descriptor.Type == texOp.Type && + descriptor.CbufSlot == texOp.CbufSlot && descriptor.HandleIndex == texOp.Handle && - descriptor.Format == texOp.Format && - descriptor.CbufSlot == cBufSlot && - descriptor.CbufOffset == cBufOffset); + descriptor.Format == texOp.Format); } public int FindTextureDescriptorIndex(AstTextureOperation texOp) diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs index 56a01264..825564b8 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs @@ -305,24 +305,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); - if (!samplers.Add(samplerName)) + if ((texOp.Flags & TextureFlags.Bindless) != 0 || !samplers.Add(samplerName)) { continue; } int firstBinding = -1; - if ((texOp.Flags & TextureFlags.Bindless) != 0) - { - AstOperand operand = texOp.GetSource(0) as AstOperand; - - firstBinding = context.Config.Counts.IncrementTexturesCount(); - - var desc = new TextureDescriptor(firstBinding, texOp.Type, operand.CbufSlot, operand.CbufOffset); - - context.TextureDescriptors.Add(desc); - } - else if ((texOp.Type & SamplerType.Indexed) != 0) + if ((texOp.Type & SamplerType.Indexed) != 0) { for (int index = 0; index < texOp.ArraySize; index++) { @@ -333,7 +323,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl firstBinding = binding; } - var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.Handle + index * 2); + var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2); context.TextureDescriptors.Add(desc); } @@ -342,7 +332,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { firstBinding = context.Config.Counts.IncrementTexturesCount(); - var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.Handle); + var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle); context.TextureDescriptors.Add(desc); } @@ -363,7 +353,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr); - if (!images.Add(imageName)) + if ((texOp.Flags & TextureFlags.Bindless) != 0 || !images.Add(imageName)) { continue; } @@ -381,7 +371,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl firstBinding = binding; } - var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.Handle + index * 2); + var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2); context.ImageDescriptors.Add(desc); } @@ -390,7 +380,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { firstBinding = context.Config.Counts.IncrementImagesCount(); - var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.Handle); + var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle); context.ImageDescriptors.Add(desc); } diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs index f10eb101..6244f68b 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs @@ -15,7 +15,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0; - bool isArray = (texOp.Type & SamplerType.Array) != 0; + // TODO: Bindless texture support. For now we just return 0/do nothing. + if (isBindless) + { + return texOp.Inst == Instruction.ImageLoad ? NumberFormatter.FormatFloat(0) : "// imageStore(bindless)"; + } + + bool isArray = (texOp.Type & SamplerType.Array) != 0; bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0; string texCall = texOp.Inst == Instruction.ImageLoad ? "imageLoad" : "imageStore"; @@ -79,7 +85,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions flags |= TextureUsageFlags.ResScaleUnsupported; } - context.ImageDescriptors[index] = context.ImageDescriptors[index].SetFlag(flags); + if (!isBindless) + { + context.ImageDescriptors[index] = context.ImageDescriptors[index].SetFlag(flags); + } return vector; } @@ -212,6 +221,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0; + // TODO: Bindless texture support. For now we just return 0. + if (isBindless) + { + return NumberFormatter.FormatFloat(0); + } + bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0; string indexExpr = null; @@ -306,6 +321,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0; bool isShadow = (texOp.Type & SamplerType.Shadow) != 0; + // TODO: Bindless texture support. For now we just return 0. + if (isBindless) + { + return NumberFormatter.FormatFloat(0); + } + // This combination is valid, but not available on GLSL. // For now, ignore the LOD level and do a normal sample. // TODO: How to implement it properly? @@ -469,7 +490,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions flags |= TextureUsageFlags.ResScaleUnsupported; } - context.TextureDescriptors[index] = context.TextureDescriptors[index].SetFlag(flags); + if (!isBindless) + { + context.TextureDescriptors[index] = context.TextureDescriptors[index].SetFlag(flags); + } } return vector; @@ -572,6 +596,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0; + // TODO: Bindless texture support. For now we just return 0. + if (isBindless) + { + return NumberFormatter.FormatInt(0); + } + bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0; string indexExpr = null; diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs index 8e878b0d..5c5d4403 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs @@ -241,22 +241,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl public static string GetSamplerName(ShaderStage stage, AstTextureOperation texOp, string indexExpr) { - string suffix; + string suffix = texOp.CbufSlot < 0 ? $"_tcb_{texOp.Handle:X}" : $"_cb{texOp.CbufSlot}_{texOp.Handle:X}"; - if ((texOp.Flags & TextureFlags.Bindless) != 0) - { - AstOperand operand = texOp.GetSource(0) as AstOperand; - - suffix = $"_{texOp.Type.ToGlslSamplerType()}_cb{operand.CbufSlot}_{operand.CbufOffset}"; - } - else + if ((texOp.Type & SamplerType.Indexed) != 0) { - suffix = texOp.Handle.ToString("X"); - - if ((texOp.Type & SamplerType.Indexed) != 0) - { - suffix += $"a[{indexExpr}]"; - } + suffix += $"a[{indexExpr}]"; } return GetShaderStagePrefix(stage) + "_" + DefaultNames.SamplerNamePrefix + suffix; @@ -264,7 +253,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl public static string GetImageName(ShaderStage stage, AstTextureOperation texOp, string indexExpr) { - string suffix = texOp.Handle.ToString("X") + "_" + texOp.Format.ToGlslFormat(); + string suffix = texOp.CbufSlot < 0 ? $"_tcb_{texOp.Handle:X}_{texOp.Format.ToGlslFormat()}" : $"_cb{texOp.CbufSlot}_{texOp.Handle:X}_{texOp.Format.ToGlslFormat()}"; if ((texOp.Type & SamplerType.Indexed) != 0) { diff --git a/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs b/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs index 9c5cd25c..52f02bfb 100644 --- a/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs +++ b/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs @@ -2,9 +2,13 @@ namespace Ryujinx.Graphics.Shader.IntermediateRepresentation { class TextureOperation : Operation { + private const int DefaultCbufSlot = -1; + public SamplerType Type { get; private set; } public TextureFlags Flags { get; private set; } + public int CbufSlot { get; private set; } + public int Handle { get; private set; } public TextureFormat Format { get; set; } @@ -18,9 +22,10 @@ namespace Ryujinx.Graphics.Shader.IntermediateRepresentation Operand dest, params Operand[] sources) : base(inst, compIndex, dest, sources) { - Type = type; - Flags = flags; - Handle = handle; + Type = type; + Flags = flags; + CbufSlot = DefaultCbufSlot; + Handle = handle; } public void TurnIntoIndexed(int handle) @@ -30,7 +35,7 @@ namespace Ryujinx.Graphics.Shader.IntermediateRepresentation Handle = handle; } - public void SetHandle(int handle) + public void SetHandle(int handle, int cbufSlot = DefaultCbufSlot) { if ((Flags & TextureFlags.Bindless) != 0) { @@ -39,7 +44,8 @@ namespace Ryujinx.Graphics.Shader.IntermediateRepresentation RemoveSource(0); } - Handle = handle; + CbufSlot = cbufSlot; + Handle = handle; } } }
\ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs b/Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs index 188bf919..cdb97488 100644 --- a/Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs +++ b/Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs @@ -8,6 +8,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr public TextureFormat Format { get; } public TextureFlags Flags { get; } + public int CbufSlot { get; } public int Handle { get; } public int ArraySize { get; } @@ -16,6 +17,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr SamplerType type, TextureFormat format, TextureFlags flags, + int cbufSlot, int handle, int arraySize, int index, @@ -24,6 +26,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr Type = type; Format = format; Flags = flags; + CbufSlot = cbufSlot; Handle = handle; ArraySize = arraySize; } diff --git a/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs b/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs index 66570dc9..733805cd 100644 --- a/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs +++ b/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs @@ -117,6 +117,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr texOp.Type, texOp.Format, texOp.Flags, + texOp.CbufSlot, texOp.Handle, 4, // TODO: Non-hardcoded array size. texOp.Index, diff --git a/Ryujinx.Graphics.Shader/TextureDescriptor.cs b/Ryujinx.Graphics.Shader/TextureDescriptor.cs index b39d6c3d..95f5e01d 100644 --- a/Ryujinx.Graphics.Shader/TextureDescriptor.cs +++ b/Ryujinx.Graphics.Shader/TextureDescriptor.cs @@ -5,46 +5,21 @@ namespace Ryujinx.Graphics.Shader public int Binding { get; } public SamplerType Type { get; } - public TextureFormat Format { get; } + public int CbufSlot { get; } public int HandleIndex { get; } - public bool IsBindless { get; } - - public int CbufSlot { get; } - public int CbufOffset { get; } - public TextureUsageFlags Flags { get; set; } - public TextureDescriptor(int binding, SamplerType type, TextureFormat format, int handleIndex) + public TextureDescriptor(int binding, SamplerType type, TextureFormat format, int cbufSlot, int handleIndex) { Binding = binding; Type = type; Format = format; + CbufSlot = cbufSlot; HandleIndex = handleIndex; - - IsBindless = false; - - CbufSlot = 0; - CbufOffset = 0; - - Flags = TextureUsageFlags.None; - } - - public TextureDescriptor(int binding, SamplerType type, int cbufSlot, int cbufOffset) - { - Binding = binding; - Type = type; - Format = TextureFormat.Unknown; - HandleIndex = 0; - - IsBindless = true; - - CbufSlot = cbufSlot; - CbufOffset = cbufOffset; - - Flags = TextureUsageFlags.None; + Flags = TextureUsageFlags.None; } public TextureDescriptor SetFlag(TextureUsageFlags flag) diff --git a/Ryujinx.Graphics.Shader/Translation/Optimizations/BindlessElimination.cs b/Ryujinx.Graphics.Shader/Translation/Optimizations/BindlessElimination.cs index a26c81c9..b326aaa4 100644 --- a/Ryujinx.Graphics.Shader/Translation/Optimizations/BindlessElimination.cs +++ b/Ryujinx.Graphics.Shader/Translation/Optimizations/BindlessElimination.cs @@ -5,15 +5,14 @@ namespace Ryujinx.Graphics.Shader.Translation.Optimizations { class BindlessElimination { - private const int NvnTextureBufferSlot = 2; - public static void RunPass(BasicBlock block, ShaderConfig config) { // We can turn a bindless into regular access by recognizing the pattern // produced by the compiler for separate texture and sampler. // We check for the following conditions: + // - The handle is a constant buffer value. // - The handle is the result of a bitwise OR logical operation. - // - Both sources of the OR operation comes from CB2 (used by NVN to hold texture handles). + // - Both sources of the OR operation comes from a constant buffer. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next) { if (!(node.Value is TextureOperation texOp)) @@ -26,9 +25,19 @@ namespace Ryujinx.Graphics.Shader.Translation.Optimizations continue; } - if (texOp.Inst == Instruction.TextureSample) + if (texOp.Inst == Instruction.Lod || + texOp.Inst == Instruction.TextureSample || + texOp.Inst == Instruction.TextureSize) { - if (!(texOp.GetSource(0).AsgOp is Operation handleCombineOp)) + Operand bindlessHandle = texOp.GetSource(0); + + if (bindlessHandle.Type == OperandType.ConstantBuffer) + { + texOp.SetHandle(bindlessHandle.GetCbufOffset(), bindlessHandle.GetCbufSlot()); + continue; + } + + if (!(bindlessHandle.AsgOp is Operation handleCombineOp)) { continue; } @@ -41,21 +50,21 @@ namespace Ryujinx.Graphics.Shader.Translation.Optimizations Operand src0 = handleCombineOp.GetSource(0); Operand src1 = handleCombineOp.GetSource(1); - if (src0.Type != OperandType.ConstantBuffer || src0.GetCbufSlot() != NvnTextureBufferSlot || - src1.Type != OperandType.ConstantBuffer || src1.GetCbufSlot() != NvnTextureBufferSlot) + if (src0.Type != OperandType.ConstantBuffer || + src1.Type != OperandType.ConstantBuffer || src0.GetCbufSlot() != src1.GetCbufSlot()) { continue; } - texOp.SetHandle(src0.GetCbufOffset() | (src1.GetCbufOffset() << 16)); + texOp.SetHandle(src0.GetCbufOffset() | (src1.GetCbufOffset() << 16), src0.GetCbufSlot()); } else if (texOp.Inst == Instruction.ImageLoad || texOp.Inst == Instruction.ImageStore) { Operand src0 = texOp.GetSource(0); - if (src0.Type == OperandType.ConstantBuffer && src0.GetCbufSlot() == NvnTextureBufferSlot) + if (src0.Type == OperandType.ConstantBuffer) { - texOp.SetHandle(src0.GetCbufOffset()); + texOp.SetHandle(src0.GetCbufOffset(), src0.GetCbufSlot()); texOp.Format = config.GetTextureFormat(texOp.Handle); } } diff --git a/Ryujinx.Graphics.Shader/Translation/Lowering.cs b/Ryujinx.Graphics.Shader/Translation/Rewriter.cs index 6da25983..a928f935 100644 --- a/Ryujinx.Graphics.Shader/Translation/Lowering.cs +++ b/Ryujinx.Graphics.Shader/Translation/Rewriter.cs @@ -8,7 +8,7 @@ using static Ryujinx.Graphics.Shader.Translation.GlobalMemory; namespace Ryujinx.Graphics.Shader.Translation { - static class Lowering + static class Rewriter { public static void RunPass(BasicBlock[] blocks, ShaderConfig config) { diff --git a/Ryujinx.Graphics.Shader/Translation/Translator.cs b/Ryujinx.Graphics.Shader/Translation/Translator.cs index f86b6964..3485b5ed 100644 --- a/Ryujinx.Graphics.Shader/Translation/Translator.cs +++ b/Ryujinx.Graphics.Shader/Translation/Translator.cs @@ -96,7 +96,7 @@ namespace Ryujinx.Graphics.Shader.Translation Optimizer.RunPass(cfg.Blocks, config); - Lowering.RunPass(cfg.Blocks, config); + Rewriter.RunPass(cfg.Blocks, config); } funcs[i] = new Function(cfg.Blocks, $"fun{i}", false, inArgumentsCount, outArgumentsCount); |
