aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Gal/Shader/ShaderDecoder.cs
blob: 4958dfcf44c2d71a23ebb61bea66bceaa9860b4e (plain)
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
namespace Ryujinx.Graphics.Gal.Shader
{
    static class ShaderDecoder
    {
        private const bool AddDbgComments = true;

        public static ShaderIrBlock DecodeBasicBlock(int[] Code, int Offset)
        {
            ShaderIrBlock Block = new ShaderIrBlock();

            while (Offset + 2 <= Code.Length)
            {
                int InstPos = Offset * 4;

                Block.Position = InstPos;

                Block.MarkLabel(InstPos);

                //Ignore scheduling instructions, which are written every 32 bytes.
                if ((Offset & 7) == 0)
                {
                    Offset += 2;

                    continue;
                }

                uint Word0 = (uint)Code[Offset++];
                uint Word1 = (uint)Code[Offset++];

                long OpCode = Word0 | (long)Word1 << 32;

                ShaderDecodeFunc Decode = ShaderOpCodeTable.GetDecoder(OpCode);

                if (AddDbgComments)
                {
                    string DbgOpCode = $"0x{InstPos:x8}: 0x{OpCode:x16} ";

                    Block.AddNode(new ShaderIrCmnt(DbgOpCode + (Decode?.Method.Name ?? "???")));
                }

                if (Decode == null)
                {
                    continue;
                }

                Decode(Block, OpCode);

                if (Block.GetLastNode() is ShaderIrOp Op && Op.Inst == ShaderIrInst.Exit)
                {
                    break;
                }
            }

            return Block;
        }

        private static bool IsFlowChange(ShaderIrInst Inst)
        {
            return Inst == ShaderIrInst.Exit;
        }
    }
}