diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2023-05-25 17:46:58 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-25 17:46:58 -0300 |
| commit | 8f0c89ffd65eb2b979b24d457708218050fec6ae (patch) | |
| tree | 5657b8a7f6246659d97f0f574f3f1829d65a235e /src/Ryujinx.Graphics.Shader/CodeGen/Spirv | |
| parent | 2c9715acf6cb9ae8bceabec5e81602c7d64c7192 (diff) | |
Generate scaling helper functions on IR (#4714)
* Generate scaling helper functions on IR
* Delete unused code
* Split RewriteTextureSample and move gather bias add to an earlier pass
* Remove using
* Shader cache version bump
Diffstat (limited to 'src/Ryujinx.Graphics.Shader/CodeGen/Spirv')
5 files changed, 20 insertions, 269 deletions
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs index 0ef89b39..7af6d316 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs @@ -36,6 +36,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv public Dictionary<IoDefinition, Instruction> OutputsPerPatch { get; } = new Dictionary<IoDefinition, Instruction>(); public Instruction CoordTemp { get; set; } + public StructuredFunction CurrentFunction { get; set; } private readonly Dictionary<AstOperand, Instruction> _locals = new Dictionary<AstOperand, Instruction>(); private readonly Dictionary<int, Instruction[]> _localForArgs = new Dictionary<int, Instruction[]>(); private readonly Dictionary<int, Instruction> _funcArgs = new Dictionary<int, Instruction>(); diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs index fda0dc47..eb64f824 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs @@ -4,7 +4,6 @@ using Ryujinx.Graphics.Shader.Translation; using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Numerics; using static Spv.Specification; @@ -114,6 +113,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv Add(Instruction.MemoryBarrier, GenerateMemoryBarrier); Add(Instruction.Minimum, GenerateMinimum); Add(Instruction.MinimumU32, GenerateMinimumU32); + Add(Instruction.Modulo, GenerateModulo); Add(Instruction.Multiply, GenerateMultiply); Add(Instruction.MultiplyHighS32, GenerateMultiplyHighS32); Add(Instruction.MultiplyHighU32, GenerateMultiplyHighU32); @@ -744,8 +744,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv pCoords = Src(AggregateType.S32); } - pCoords = ScalingHelpers.ApplyScaling(context, texOp, pCoords, intCoords: true, isBindless, isIndexed, isArray, pCount); - (var imageType, var imageVariable) = context.Images[new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format)]; var image = context.Load(imageType, imageVariable); @@ -1040,6 +1038,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv return GenerateBinaryU32(context, operation, context.Delegates.GlslUMin); } + private static OperationResult GenerateModulo(CodeGenContext context, AstOperation operation) + { + return GenerateBinary(context, operation, context.Delegates.FMod, null); + } + private static OperationResult GenerateMultiply(CodeGenContext context, AstOperation operation) { return GenerateBinary(context, operation, context.Delegates.FMul, context.Delegates.IMul); @@ -1101,7 +1104,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv private static OperationResult GenerateReturn(CodeGenContext context, AstOperation operation) { - context.Return(); + if (operation.SourcesCount != 0) + { + context.ReturnValue(context.Get(context.CurrentFunction.ReturnType, operation.GetSource(0))); + } + else + { + context.Return(); + } + return OperationResult.Invalid; } @@ -1439,35 +1450,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv } } - SpvInstruction ApplyBias(SpvInstruction vector, SpvInstruction image) - { - int gatherBiasPrecision = context.Config.GpuAccessor.QueryHostGatherBiasPrecision(); - if (isGather && gatherBiasPrecision != 0) - { - // GPU requires texture gather to be slightly offset to match NVIDIA behaviour when point is exactly between two texels. - // Offset by the gather precision divided by 2 to correct for rounding. - var sizeType = pCount == 1 ? context.TypeS32() : context.TypeVector(context.TypeS32(), pCount); - var pVectorType = pCount == 1 ? context.TypeFP32() : context.TypeVector(context.TypeFP32(), pCount); - - var bias = context.Constant(context.TypeFP32(), (float)(1 << (gatherBiasPrecision + 1))); - var biasVector = context.CompositeConstruct(pVectorType, Enumerable.Repeat(bias, pCount).ToArray()); - - var one = context.Constant(context.TypeFP32(), 1f); - var oneVector = context.CompositeConstruct(pVectorType, Enumerable.Repeat(one, pCount).ToArray()); - - var divisor = context.FMul( - pVectorType, - context.ConvertSToF(pVectorType, context.ImageQuerySize(sizeType, image)), - biasVector); - - vector = context.FAdd(pVectorType, vector, context.FDiv(pVectorType, oneVector, divisor)); - } - - return vector; - } - SpvInstruction pCoords = AssemblePVector(pCount); - pCoords = ScalingHelpers.ApplyScaling(context, texOp, pCoords, intCoords, isBindless, isIndexed, isArray, pCount); SpvInstruction AssembleDerivativesVector(int count) { @@ -1638,8 +1621,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv image = context.Image(imageType, image); } - pCoords = ApplyBias(pCoords, image); - var operands = operandsList.ToArray(); SpvInstruction result; @@ -1755,11 +1736,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv result = context.CompositeExtract(context.TypeS32(), result, (SpvLiteralInteger)texOp.Index); } - if (texOp.Index < 2 || (type & SamplerType.Mask) == SamplerType.Texture3D) - { - result = ScalingHelpers.ApplyUnscaling(context, texOp.WithType(type), result, isBindless, isIndexed); - } - return new OperationResult(AggregateType.S32, result); } } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ScalingHelpers.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ScalingHelpers.cs deleted file mode 100644 index c8b21e88..00000000 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ScalingHelpers.cs +++ /dev/null @@ -1,227 +0,0 @@ -using Ryujinx.Graphics.Shader.IntermediateRepresentation; -using Ryujinx.Graphics.Shader.StructuredIr; -using Ryujinx.Graphics.Shader.Translation; -using static Spv.Specification; - -namespace Ryujinx.Graphics.Shader.CodeGen.Spirv -{ - using SpvInstruction = Spv.Generator.Instruction; - - static class ScalingHelpers - { - public static SpvInstruction ApplyScaling( - CodeGenContext context, - AstTextureOperation texOp, - SpvInstruction vector, - bool intCoords, - bool isBindless, - bool isIndexed, - bool isArray, - int pCount) - { - if (intCoords) - { - if (context.Config.Stage.SupportsRenderScale() && - !isBindless && - !isIndexed) - { - int index = texOp.Inst == Instruction.ImageLoad - ? context.Config.GetTextureDescriptors().Length + context.Config.FindImageDescriptorIndex(texOp) - : context.Config.FindTextureDescriptorIndex(texOp); - - if (pCount == 3 && isArray) - { - return ApplyScaling2DArray(context, vector, index); - } - else if (pCount == 2 && !isArray) - { - return ApplyScaling2D(context, vector, index); - } - } - } - - return vector; - } - - private static SpvInstruction ApplyScaling2DArray(CodeGenContext context, SpvInstruction vector, int index) - { - // The array index is not scaled, just x and y. - var vectorXY = context.VectorShuffle(context.TypeVector(context.TypeS32(), 2), vector, vector, 0, 1); - var vectorZ = context.CompositeExtract(context.TypeS32(), vector, 2); - var vectorXYScaled = ApplyScaling2D(context, vectorXY, index); - var vectorScaled = context.CompositeConstruct(context.TypeVector(context.TypeS32(), 3), vectorXYScaled, vectorZ); - - return vectorScaled; - } - - private static SpvInstruction ApplyScaling2D(CodeGenContext context, SpvInstruction vector, int index) - { - var pointerType = context.TypePointer(StorageClass.Uniform, context.TypeFP32()); - var fieldIndex = context.Constant(context.TypeU32(), 4); - var scaleIndex = context.Constant(context.TypeU32(), index); - - if (context.Config.Stage == ShaderStage.Vertex) - { - var scaleCountPointerType = context.TypePointer(StorageClass.Uniform, context.TypeS32()); - var scaleCountElemPointer = context.AccessChain(scaleCountPointerType, context.ConstantBuffers[0], context.Constant(context.TypeU32(), 3)); - var scaleCount = context.Load(context.TypeS32(), scaleCountElemPointer); - - scaleIndex = context.IAdd(context.TypeU32(), scaleIndex, scaleCount); - } - - scaleIndex = context.IAdd(context.TypeU32(), scaleIndex, context.Constant(context.TypeU32(), 1)); - - var scaleElemPointer = context.AccessChain(pointerType, context.ConstantBuffers[0], fieldIndex, scaleIndex); - var scale = context.Load(context.TypeFP32(), scaleElemPointer); - - var ivector2Type = context.TypeVector(context.TypeS32(), 2); - var localVector = context.CoordTemp; - - var passthrough = context.FOrdEqual(context.TypeBool(), scale, context.Constant(context.TypeFP32(), 1f)); - - var mergeLabel = context.Label(); - - if (context.Config.Stage == ShaderStage.Fragment) - { - var scaledInterpolatedLabel = context.Label(); - var scaledNoInterpolationLabel = context.Label(); - - var needsInterpolation = context.FOrdLessThan(context.TypeBool(), scale, context.Constant(context.TypeFP32(), 0f)); - - context.SelectionMerge(mergeLabel, SelectionControlMask.MaskNone); - context.BranchConditional(needsInterpolation, scaledInterpolatedLabel, scaledNoInterpolationLabel); - - // scale < 0.0 - context.AddLabel(scaledInterpolatedLabel); - - ApplyScalingInterpolated(context, localVector, vector, scale); - context.Branch(mergeLabel); - - // scale >= 0.0 - context.AddLabel(scaledNoInterpolationLabel); - - ApplyScalingNoInterpolation(context, localVector, vector, scale); - context.Branch(mergeLabel); - - context.AddLabel(mergeLabel); - - var passthroughLabel = context.Label(); - var finalMergeLabel = context.Label(); - - context.SelectionMerge(finalMergeLabel, SelectionControlMask.MaskNone); - context.BranchConditional(passthrough, passthroughLabel, finalMergeLabel); - - context.AddLabel(passthroughLabel); - - context.Store(localVector, vector); - context.Branch(finalMergeLabel); - - context.AddLabel(finalMergeLabel); - - return context.Load(ivector2Type, localVector); - } - else - { - var passthroughLabel = context.Label(); - var scaledLabel = context.Label(); - - context.SelectionMerge(mergeLabel, SelectionControlMask.MaskNone); - context.BranchConditional(passthrough, passthroughLabel, scaledLabel); - - // scale == 1.0 - context.AddLabel(passthroughLabel); - - context.Store(localVector, vector); - context.Branch(mergeLabel); - - // scale != 1.0 - context.AddLabel(scaledLabel); - - ApplyScalingNoInterpolation(context, localVector, vector, scale); - context.Branch(mergeLabel); - - context.AddLabel(mergeLabel); - - return context.Load(ivector2Type, localVector); - } - } - - private static void ApplyScalingInterpolated(CodeGenContext context, SpvInstruction output, SpvInstruction vector, SpvInstruction scale) - { - var vector2Type = context.TypeVector(context.TypeFP32(), 2); - - var scaleNegated = context.FNegate(context.TypeFP32(), scale); - var scaleVector = context.CompositeConstruct(vector2Type, scaleNegated, scaleNegated); - - var vectorFloat = context.ConvertSToF(vector2Type, vector); - var vectorScaled = context.VectorTimesScalar(vector2Type, vectorFloat, scaleNegated); - - var fragCoordPointer = context.Inputs[new IoDefinition(StorageKind.Input, IoVariable.FragmentCoord)]; - var fragCoord = context.Load(context.TypeVector(context.TypeFP32(), 4), fragCoordPointer); - var fragCoordXY = context.VectorShuffle(vector2Type, fragCoord, fragCoord, 0, 1); - - var scaleMod = context.FMod(vector2Type, fragCoordXY, scaleVector); - var vectorInterpolated = context.FAdd(vector2Type, vectorScaled, scaleMod); - - context.Store(output, context.ConvertFToS(context.TypeVector(context.TypeS32(), 2), vectorInterpolated)); - } - - private static void ApplyScalingNoInterpolation(CodeGenContext context, SpvInstruction output, SpvInstruction vector, SpvInstruction scale) - { - if (context.Config.Stage == ShaderStage.Vertex) - { - scale = context.GlslFAbs(context.TypeFP32(), scale); - } - - var vector2Type = context.TypeVector(context.TypeFP32(), 2); - - var vectorFloat = context.ConvertSToF(vector2Type, vector); - var vectorScaled = context.VectorTimesScalar(vector2Type, vectorFloat, scale); - - context.Store(output, context.ConvertFToS(context.TypeVector(context.TypeS32(), 2), vectorScaled)); - } - - public static SpvInstruction ApplyUnscaling( - CodeGenContext context, - AstTextureOperation texOp, - SpvInstruction size, - bool isBindless, - bool isIndexed) - { - if (context.Config.Stage.SupportsRenderScale() && - !isBindless && - !isIndexed) - { - int index = context.Config.FindTextureDescriptorIndex(texOp); - - var pointerType = context.TypePointer(StorageClass.Uniform, context.TypeFP32()); - var fieldIndex = context.Constant(context.TypeU32(), 4); - var scaleIndex = context.Constant(context.TypeU32(), index); - - if (context.Config.Stage == ShaderStage.Vertex) - { - var scaleCountPointerType = context.TypePointer(StorageClass.Uniform, context.TypeS32()); - var scaleCountElemPointer = context.AccessChain(scaleCountPointerType, context.ConstantBuffers[0], context.Constant(context.TypeU32(), 3)); - var scaleCount = context.Load(context.TypeS32(), scaleCountElemPointer); - - scaleIndex = context.IAdd(context.TypeU32(), scaleIndex, scaleCount); - } - - scaleIndex = context.IAdd(context.TypeU32(), scaleIndex, context.Constant(context.TypeU32(), 1)); - - var scaleElemPointer = context.AccessChain(pointerType, context.ConstantBuffers[0], fieldIndex, scaleIndex); - var scale = context.GlslFAbs(context.TypeFP32(), context.Load(context.TypeFP32(), scaleElemPointer)); - - var passthrough = context.FOrdEqual(context.TypeBool(), scale, context.Constant(context.TypeFP32(), 1f)); - - var sizeFloat = context.ConvertSToF(context.TypeFP32(), size); - var sizeUnscaled = context.FDiv(context.TypeFP32(), sizeFloat, scale); - var sizeUnscaledInt = context.ConvertFToS(context.TypeS32(), sizeUnscaled); - - return context.Select(context.TypeS32(), passthrough, size, sizeUnscaledInt); - } - - return size; - } - } -}
\ No newline at end of file diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvDelegates.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvDelegates.cs index 3ccfd7f5..0fa954e1 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvDelegates.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvDelegates.cs @@ -67,6 +67,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv public readonly FuncBinaryInstruction GlslSMax; public readonly FuncBinaryInstruction GlslFMin; public readonly FuncBinaryInstruction GlslSMin; + public readonly FuncBinaryInstruction FMod; public readonly FuncBinaryInstruction FMul; public readonly FuncBinaryInstruction IMul; public readonly FuncBinaryInstruction FSub; @@ -174,6 +175,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv GlslSMax = context.GlslSMax; GlslFMin = context.GlslFMin; GlslSMin = context.GlslSMin; + FMod = context.FMod; FMul = context.FMul; IMul = context.IMul; FSub = context.FSub; diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs index 3e11a974..a55e09fd 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs @@ -144,10 +144,9 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv private static void Generate(CodeGenContext context, StructuredProgramInfo info, int funcIndex) { - var function = info.Functions[funcIndex]; - - (_, var spvFunc) = context.GetFunction(funcIndex); + (var function, var spvFunc) = context.GetFunction(funcIndex); + context.CurrentFunction = function; context.AddFunction(spvFunc); context.StartFunction(); |
