aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/Translation/Optimizations/BindlessToIndexed.cs
blob: ca46a1f53e2c628104db4b04eb1bd61c8d733a68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;

using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;

namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
    static class BindlessToIndexed
    {
        public static void RunPass(BasicBlock block, ShaderConfig config)
        {
            // We can turn a bindless texture access into a indexed access,
            // as long the following conditions are true:
            // - The handle is loaded using a LDC instruction.
            // - The handle is loaded from the constant buffer with the handles (CB2 for NVN).
            // - The load has a constant offset.
            // The base offset of the array of handles on the constant buffer is the constant offset.
            for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
            {
                if (!(node.Value is TextureOperation texOp))
                {
                    continue;
                }

                if ((texOp.Flags & TextureFlags.Bindless) == 0)
                {
                    continue;
                }

                if (!(texOp.GetSource(0).AsgOp is Operation handleAsgOp))
                {
                    continue;
                }

                if (handleAsgOp.Inst != Instruction.LoadConstant)
                {
                    continue;
                }

                Operand ldcSrc0 = handleAsgOp.GetSource(0);
                Operand ldcSrc1 = handleAsgOp.GetSource(1);

                if (ldcSrc0.Type != OperandType.Constant || ldcSrc0.Value != 2)
                {
                    continue;
                }

                if (!(ldcSrc1.AsgOp is Operation shrOp) || shrOp.Inst != Instruction.ShiftRightU32)
                {
                    continue;
                }

                if (!(shrOp.GetSource(0).AsgOp is Operation addOp) || addOp.Inst != Instruction.Add)
                {
                    continue;
                }

                Operand addSrc1 = addOp.GetSource(1);

                if (addSrc1.Type != OperandType.Constant)
                {
                    continue;
                }

                TurnIntoIndexed(config, texOp, addSrc1.Value / 4);

                Operand index = Local();

                Operand source = addOp.GetSource(0);

                Operation shrBy3 = new Operation(Instruction.ShiftRightU32, index, source, Const(3));

                block.Operations.AddBefore(node, shrBy3);

                texOp.SetSource(0, index);
            }
        }

        private static void TurnIntoIndexed(ShaderConfig config, TextureOperation texOp, int handle)
        {
            texOp.TurnIntoIndexed(handle);
            config.SetUsedTexture(texOp.Inst, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, handle);
        }
    }
}