aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/Instructions/InstEmitHelper.cs
blob: 8638fb8f2b7e88ac20dc27b477346804085d1c7b (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
250
251
252
253
254
255
256
257
258
259
260
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Runtime.CompilerServices;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;

namespace Ryujinx.Graphics.Shader.Instructions
{
    static class InstEmitHelper
    {
        public static Operand GetZF()
        {
            return Register(0, RegisterType.Flag);
        }

        public static Operand GetNF()
        {
            return Register(1, RegisterType.Flag);
        }

        public static Operand GetCF()
        {
            return Register(2, RegisterType.Flag);
        }

        public static Operand GetVF()
        {
            return Register(3, RegisterType.Flag);
        }

        public static Operand GetDest(int rd)
        {
            return Register(rd, RegisterType.Gpr);
        }

        public static Operand GetDest2(int rd)
        {
            return Register(rd | 1, RegisterType.Gpr);
        }

        public static Operand GetSrcCbuf(EmitterContext context, int cbufSlot, int cbufOffset, bool isFP64 = false)
        {
            if (isFP64)
            {
                return context.PackDouble2x32(
                    Cbuf(cbufSlot, cbufOffset),
                    Cbuf(cbufSlot, cbufOffset + 1));
            }
            else
            {
                return Cbuf(cbufSlot, cbufOffset);
            }
        }

        public static Operand GetSrcImm(EmitterContext context, int imm, bool isFP64 = false)
        {
            if (isFP64)
            {
                return context.PackDouble2x32(Const(0), Const(imm));
            }
            else
            {
                return Const(imm);
            }
        }

        public static Operand GetSrcReg(EmitterContext context, int reg, bool isFP64 = false)
        {
            if (isFP64)
            {
                return context.PackDouble2x32(Register(reg, RegisterType.Gpr), Register(reg | 1, RegisterType.Gpr));
            }
            else
            {
                return Register(reg, RegisterType.Gpr);
            }
        }

        public static Operand[] GetHalfSrc(
            EmitterContext context,
            HalfSwizzle swizzle,
            int ra,
            bool negate,
            bool absolute)
        {
            Operand[] operands = GetHalfUnpacked(context, GetSrcReg(context, ra), swizzle);

            return FPAbsNeg(context, operands, absolute, negate);
        }

        public static Operand[] GetHalfSrc(
            EmitterContext context,
            HalfSwizzle swizzle,
            int cbufSlot,
            int cbufOffset,
            bool negate,
            bool absolute)
        {
            Operand[] operands = GetHalfUnpacked(context, GetSrcCbuf(context, cbufSlot, cbufOffset), swizzle);

            return FPAbsNeg(context, operands, absolute, negate);
        }

        public static Operand[] GetHalfSrc(EmitterContext context, int immH0, int immH1)
        {
            ushort low = (ushort)(immH0 << 6);
            ushort high = (ushort)(immH1 << 6);

            return new Operand[]
            {
                ConstF((float)Unsafe.As<ushort, Half>(ref low)),
                ConstF((float)Unsafe.As<ushort, Half>(ref high)),
            };
        }

        public static Operand[] GetHalfSrc(EmitterContext context, int imm32)
        {
            ushort low = (ushort)imm32;
            ushort high = (ushort)(imm32 >> 16);

            return new Operand[]
            {
                ConstF((float)Unsafe.As<ushort, Half>(ref low)),
                ConstF((float)Unsafe.As<ushort, Half>(ref high)),
            };
        }

        public static Operand[] FPAbsNeg(EmitterContext context, Operand[] operands, bool abs, bool neg)
        {
            for (int index = 0; index < operands.Length; index++)
            {
                operands[index] = context.FPAbsNeg(operands[index], abs, neg);
            }

            return operands;
        }

        public static Operand[] GetHalfUnpacked(EmitterContext context, Operand src, HalfSwizzle swizzle)
        {
            return swizzle switch
            {
                HalfSwizzle.F16 => new Operand[]
                                    {
                        context.UnpackHalf2x16Low (src),
                        context.UnpackHalf2x16High(src),
                                    },
                HalfSwizzle.F32 => new Operand[] { src, src },
                HalfSwizzle.H0H0 => new Operand[]
                    {
                        context.UnpackHalf2x16Low(src),
                        context.UnpackHalf2x16Low(src),
                    },
                HalfSwizzle.H1H1 => new Operand[]
                    {
                        context.UnpackHalf2x16High(src),
                        context.UnpackHalf2x16High(src),
                    },
                _ => throw new ArgumentException($"Invalid swizzle \"{swizzle}\"."),
            };
        }

        public static Operand GetHalfPacked(EmitterContext context, OFmt swizzle, Operand[] results, int rd)
        {
            switch (swizzle)
            {
                case OFmt.F16:
                    return context.PackHalf2x16(results[0], results[1]);

                case OFmt.F32:
                    return results[0];

                case OFmt.MrgH0:
                    {
                        Operand h1 = GetHalfDest(context, rd, isHigh: true);

                        return context.PackHalf2x16(results[0], h1);
                    }

                case OFmt.MrgH1:
                    {
                        Operand h0 = GetHalfDest(context, rd, isHigh: false);

                        return context.PackHalf2x16(h0, results[1]);
                    }
            }

            throw new ArgumentException($"Invalid swizzle \"{swizzle}\".");
        }

        public static Operand GetHalfDest(EmitterContext context, int rd, bool isHigh)
        {
            if (isHigh)
            {
                return context.UnpackHalf2x16High(GetDest(rd));
            }
            else
            {
                return context.UnpackHalf2x16Low(GetDest(rd));
            }
        }

        public static Operand GetPredicate(EmitterContext context, int pred, bool not)
        {
            Operand local = Register(pred, RegisterType.Predicate);

            if (not)
            {
                local = context.BitwiseNot(local);
            }

            return local;
        }

        public static void SetDest(EmitterContext context, Operand value, int rd, bool isFP64)
        {
            if (isFP64)
            {
                context.Copy(GetDest(rd), context.UnpackDouble2x32Low(value));
                context.Copy(GetDest2(rd), context.UnpackDouble2x32High(value));
            }
            else
            {
                context.Copy(GetDest(rd), value);
            }
        }

        public static int Imm16ToSInt(int imm16)
        {
            return (short)imm16;
        }

        public static int Imm20ToFloat(int imm20)
        {
            return imm20 << 12;
        }

        public static int Imm20ToSInt(int imm20)
        {
            return (imm20 << 12) >> 12;
        }

        public static int Imm24ToSInt(int imm24)
        {
            return (imm24 << 8) >> 8;
        }

        public static Operand SignExtendTo32(EmitterContext context, Operand src, int srcBits)
        {
            return context.BitfieldExtractS32(src, Const(0), Const(srcBits));
        }

        public static Operand ZeroExtendTo32(EmitterContext context, Operand src, int srcBits)
        {
            int mask = (int)(uint.MaxValue >> (32 - srcBits));

            return context.BitwiseAnd(src, Const(mask));
        }
    }
}