aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/Instructions/InstEmitMove.cs
blob: 51b706011ca66eb9d2a84a396f28efa53405e53d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation;

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

namespace Ryujinx.Graphics.Shader.Instructions
{
    static partial class InstEmit
    {
        public static void MovR(EmitterContext context)
        {
            InstMovR op = context.GetOp<InstMovR>();

            context.Copy(GetDest(op.Dest), GetSrcReg(context, op.SrcA));
        }

        public static void MovI(EmitterContext context)
        {
            InstMovI op = context.GetOp<InstMovI>();

            context.Copy(GetDest(op.Dest), GetSrcImm(context, op.Imm20));
        }

        public static void MovC(EmitterContext context)
        {
            InstMovC op = context.GetOp<InstMovC>();

            context.Copy(GetDest(op.Dest), GetSrcCbuf(context, op.CbufSlot, op.CbufOffset));
        }

        public static void Mov32i(EmitterContext context)
        {
            InstMov32i op = context.GetOp<InstMov32i>();

            context.Copy(GetDest(op.Dest), GetSrcImm(context, op.Imm32));
        }

        public static void R2pR(EmitterContext context)
        {
            InstR2pR op = context.GetOp<InstR2pR>();

            Operand value = GetSrcReg(context, op.SrcA);
            Operand mask = GetSrcReg(context, op.SrcB);

            EmitR2p(context, value, mask, op.ByteSel, op.Ccpr);
        }

        public static void R2pI(EmitterContext context)
        {
            InstR2pI op = context.GetOp<InstR2pI>();

            Operand value = GetSrcReg(context, op.SrcA);
            Operand mask = GetSrcImm(context, Imm20ToSInt(op.Imm20));

            EmitR2p(context, value, mask, op.ByteSel, op.Ccpr);
        }

        public static void R2pC(EmitterContext context)
        {
            InstR2pC op = context.GetOp<InstR2pC>();

            Operand value = GetSrcReg(context, op.SrcA);
            Operand mask = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitR2p(context, value, mask, op.ByteSel, op.Ccpr);
        }

        public static void S2r(EmitterContext context)
        {
            InstS2r op = context.GetOp<InstS2r>();

            Operand src;

            switch (op.SReg)
            {
                case SReg.LaneId:
                    src = Attribute(AttributeConsts.LaneId);
                    break;

                case SReg.InvocationId:
                    src = Attribute(AttributeConsts.InvocationId);
                    break;

                case SReg.YDirection:
                    src = ConstF(1); // TODO: Use value from Y direction GPU register.
                    break;

                case SReg.ThreadKill:
                    src = context.Config.Stage == ShaderStage.Fragment ? Attribute(AttributeConsts.ThreadKill) : Const(0);
                    break;

                case SReg.InvocationInfo:
                    if (context.Config.Stage != ShaderStage.Compute && context.Config.Stage != ShaderStage.Fragment)
                    {
                        Operand primitiveId = Attribute(AttributeConsts.PrimitiveId);
                        Operand patchVerticesIn;

                        if (context.Config.Stage == ShaderStage.TessellationEvaluation)
                        {
                            patchVerticesIn = context.ShiftLeft(Attribute(AttributeConsts.PatchVerticesIn), Const(16));
                        }
                        else
                        {
                            InputTopology inputTopology = context.Config.GpuAccessor.QueryPrimitiveTopology();

                            int inputVertices = inputTopology switch
                            {
                                InputTopology.Points => 1,
                                InputTopology.Lines or
                                InputTopology.LinesAdjacency => 2,
                                InputTopology.Triangles or
                                InputTopology.TrianglesAdjacency => 3,
                                _ => 1
                            };

                            patchVerticesIn = Const(inputVertices << 16);
                        }

                        src = context.BitwiseOr(primitiveId, patchVerticesIn);
                    }
                    else
                    {
                        src = Const(0);
                    }
                    break;

                case SReg.TId:
                    Operand tidX = Attribute(AttributeConsts.ThreadIdX);
                    Operand tidY = Attribute(AttributeConsts.ThreadIdY);
                    Operand tidZ = Attribute(AttributeConsts.ThreadIdZ);

                    tidY = context.ShiftLeft(tidY, Const(16));
                    tidZ = context.ShiftLeft(tidZ, Const(26));

                    src = context.BitwiseOr(tidX, context.BitwiseOr(tidY, tidZ));
                    break;

                case SReg.TIdX:
                    src = Attribute(AttributeConsts.ThreadIdX);
                    break;
                case SReg.TIdY:
                    src = Attribute(AttributeConsts.ThreadIdY);
                    break;
                case SReg.TIdZ:
                    src = Attribute(AttributeConsts.ThreadIdZ);
                    break;

                case SReg.CtaIdX:
                    src = Attribute(AttributeConsts.CtaIdX);
                    break;
                case SReg.CtaIdY:
                    src = Attribute(AttributeConsts.CtaIdY);
                    break;
                case SReg.CtaIdZ:
                    src = Attribute(AttributeConsts.CtaIdZ);
                    break;

                case SReg.EqMask:
                    src = Attribute(AttributeConsts.EqMask);
                    break;
                case SReg.LtMask:
                    src = Attribute(AttributeConsts.LtMask);
                    break;
                case SReg.LeMask:
                    src = Attribute(AttributeConsts.LeMask);
                    break;
                case SReg.GtMask:
                    src = Attribute(AttributeConsts.GtMask);
                    break;
                case SReg.GeMask:
                    src = Attribute(AttributeConsts.GeMask);
                    break;

                default:
                    src = Const(0);
                    break;
            }

            context.Copy(GetDest(op.Dest), src);
        }

        public static void SelR(EmitterContext context)
        {
            InstSelR op = context.GetOp<InstSelR>();

            Operand srcA = GetSrcReg(context, op.SrcA);
            Operand srcB = GetSrcReg(context, op.SrcB);
            Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);

            EmitSel(context, srcA, srcB, srcPred, op.Dest);
        }

        public static void SelI(EmitterContext context)
        {
            InstSelI op = context.GetOp<InstSelI>();

            Operand srcA = GetSrcReg(context, op.SrcA);
            Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
            Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);

            EmitSel(context, srcA, srcB, srcPred, op.Dest);
        }

        public static void SelC(EmitterContext context)
        {
            InstSelC op = context.GetOp<InstSelC>();

            Operand srcA = GetSrcReg(context, op.SrcA);
            Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
            Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);

            EmitSel(context, srcA, srcB, srcPred, op.Dest);
        }

        private static void EmitR2p(EmitterContext context, Operand value, Operand mask, ByteSel byteSel, bool ccpr)
        {
            Operand Test(Operand value, int bit)
            {
                return context.ICompareNotEqual(context.BitwiseAnd(value, Const(1 << bit)), Const(0));
            }

            if (ccpr)
            {
                // TODO: Support Register to condition code flags copy.
                context.Config.GpuAccessor.Log("R2P.CC not implemented.");
            }
            else
            {
                int shift = (int)byteSel * 8;

                for (int bit = 0; bit < RegisterConsts.PredsCount; bit++)
                {
                    Operand pred = Register(bit, RegisterType.Predicate);
                    Operand res = context.ConditionalSelect(Test(mask, bit), Test(value, bit + shift), pred);
                    context.Copy(pred, res);
                }
            }
        }

        private static void EmitSel(EmitterContext context, Operand srcA, Operand srcB, Operand srcPred, int rd)
        {
            Operand res = context.ConditionalSelect(srcPred, srcA, srcB);

            context.Copy(GetDest(rd), res);
        }
    }
}