summaryrefslogtreecommitdiffstats
path: root/src/video_core/engines/shader_bytecode.h
blob: eff0c35a15db44f9d65e5939036f54f09048fc9b (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <map>
#include <string>
#include "common/bit_field.h"

namespace Tegra {
namespace Shader {

struct Register {
    Register() = default;

    constexpr Register(u64 value) : value(value) {}

    constexpr u64 GetIndex() const {
        return value;
    }

    constexpr operator u64() const {
        return value;
    }

    template <typename T>
    constexpr u64 operator-(const T& oth) const {
        return value - oth;
    }

    template <typename T>
    constexpr u64 operator&(const T& oth) const {
        return value & oth;
    }

    constexpr u64 operator&(const Register& oth) const {
        return value & oth.value;
    }

    constexpr u64 operator~() const {
        return ~value;
    }

private:
    u64 value;
};

union Attribute {
    Attribute() = default;

    constexpr Attribute(u64 value) : value(value) {}

    enum class Index : u64 {
        Position = 7,
        Attribute_0 = 8,
    };

    union {
        BitField<22, 2, u64> element;
        BitField<24, 6, Index> index;
        BitField<47, 3, u64> size;
    } fmt20;

    union {
        BitField<30, 2, u64> element;
        BitField<32, 6, Index> index;
    } fmt28;

    BitField<39, 8, u64> reg;
    u64 value;
};

union Uniform {
    BitField<20, 14, u64> offset;
    BitField<34, 5, u64> index;
};

union OpCode {
    enum class Id : u64 {
        TEXS = 0x6C,
        IPA = 0xE0,
        FFMA_IMM = 0x65,
        FFMA_CR = 0x93,
        FFMA_RC = 0xA3,
        FFMA_RR = 0xB3,

        FADD_C = 0x98B,
        FMUL_C = 0x98D,
        MUFU = 0xA10,
        FADD_R = 0xB8B,
        FMUL_R = 0xB8D,
        LD_A = 0x1DFB,
        ST_A = 0x1DFE,

        FSETP_R = 0x5BB,
        FSETP_C = 0x4BB,
        EXIT = 0xE30,
        KIL = 0xE33,

        FMUL_IMM = 0x70D,
        FMUL_IMM_x = 0x72D,
        FADD_IMM = 0x70B,
        FADD_IMM_x = 0x72B,
    };

    enum class Type {
        Trivial,
        Arithmetic,
        Ffma,
        Flow,
        Memory,
        Unknown,
    };

    struct Info {
        Type type;
        std::string name;
    };

    OpCode() = default;

    constexpr OpCode(Id value) : value(static_cast<u64>(value)) {}

    constexpr OpCode(u64 value) : value{value} {}

    constexpr Id EffectiveOpCode() const {
        switch (op1) {
        case Id::TEXS:
            return op1;
        }

        switch (op2) {
        case Id::IPA:
            return op2;
        }

        switch (op3) {
        case Id::FFMA_IMM:
        case Id::FFMA_CR:
        case Id::FFMA_RC:
        case Id::FFMA_RR:
            return op3;
        }

        switch (op4) {
        case Id::EXIT:
        case Id::FSETP_R:
        case Id::FSETP_C:
        case Id::KIL:
            return op4;
        }

        switch (op5) {
        case Id::MUFU:
        case Id::LD_A:
        case Id::ST_A:
        case Id::FADD_R:
        case Id::FADD_C:
        case Id::FMUL_R:
        case Id::FMUL_C:
            return op5;

        case Id::FMUL_IMM:
        case Id::FMUL_IMM_x:
            return Id::FMUL_IMM;

        case Id::FADD_IMM:
        case Id::FADD_IMM_x:
            return Id::FADD_IMM;
        }

        return static_cast<Id>(value);
    }

    static const Info& GetInfo(const OpCode& opcode) {
        static const std::map<Id, Info> info_table{BuildInfoTable()};
        const auto& search{info_table.find(opcode.EffectiveOpCode())};
        if (search != info_table.end()) {
            return search->second;
        }

        static const Info unknown{Type::Unknown, "UNK"};
        return unknown;
    }

    constexpr operator Id() const {
        return static_cast<Id>(value);
    }

    constexpr OpCode operator<<(size_t bits) const {
        return value << bits;
    }

    constexpr OpCode operator>>(size_t bits) const {
        return value >> bits;
    }

    template <typename T>
    constexpr u64 operator-(const T& oth) const {
        return value - oth;
    }

    constexpr u64 operator&(const OpCode& oth) const {
        return value & oth.value;
    }

    constexpr u64 operator~() const {
        return ~value;
    }

    static std::map<Id, Info> BuildInfoTable() {
        std::map<Id, Info> info_table;
        info_table[Id::TEXS] = {Type::Memory, "texs"};
        info_table[Id::LD_A] = {Type::Memory, "ld_a"};
        info_table[Id::ST_A] = {Type::Memory, "st_a"};
        info_table[Id::MUFU] = {Type::Arithmetic, "mufu"};
        info_table[Id::FFMA_IMM] = {Type::Ffma, "ffma_imm"};
        info_table[Id::FFMA_CR] = {Type::Ffma, "ffma_cr"};
        info_table[Id::FFMA_RC] = {Type::Ffma, "ffma_rc"};
        info_table[Id::FFMA_RR] = {Type::Ffma, "ffma_rr"};
        info_table[Id::FADD_R] = {Type::Arithmetic, "fadd_r"};
        info_table[Id::FADD_C] = {Type::Arithmetic, "fadd_c"};
        info_table[Id::FADD_IMM] = {Type::Arithmetic, "fadd_imm"};
        info_table[Id::FMUL_R] = {Type::Arithmetic, "fmul_r"};
        info_table[Id::FMUL_C] = {Type::Arithmetic, "fmul_c"};
        info_table[Id::FMUL_IMM] = {Type::Arithmetic, "fmul_imm"};
        info_table[Id::FSETP_C] = {Type::Arithmetic, "fsetp_c"};
        info_table[Id::FSETP_R] = {Type::Arithmetic, "fsetp_r"};
        info_table[Id::EXIT] = {Type::Trivial, "exit"};
        info_table[Id::IPA] = {Type::Trivial, "ipa"};
        info_table[Id::KIL] = {Type::Flow, "kil"};
        return info_table;
    }

    BitField<57, 7, Id> op1;
    BitField<56, 8, Id> op2;
    BitField<55, 9, Id> op3;
    BitField<52, 12, Id> op4;
    BitField<51, 13, Id> op5;
    u64 value;
};
static_assert(sizeof(OpCode) == 0x8, "Incorrect structure size");

} // namespace Shader
} // namespace Tegra

namespace std {

// TODO(bunne): The below is forbidden by the C++ standard, but works fine. See #330.
template <>
struct make_unsigned<Tegra::Shader::Attribute> {
    using type = Tegra::Shader::Attribute;
};

template <>
struct make_unsigned<Tegra::Shader::Register> {
    using type = Tegra::Shader::Register;
};

template <>
struct make_unsigned<Tegra::Shader::OpCode> {
    using type = Tegra::Shader::OpCode;
};

} // namespace std

namespace Tegra {
namespace Shader {

enum class Pred : u64 {
    UnusedIndex = 0x7,
    NeverExecute = 0xf,
};

enum class SubOp : u64 {
    Cos = 0x0,
    Sin = 0x1,
    Ex2 = 0x2,
    Lg2 = 0x3,
    Rcp = 0x4,
    Rsq = 0x5,
};

union Instruction {
    Instruction& operator=(const Instruction& instr) {
        hex = instr.hex;
        return *this;
    }

    OpCode opcode;
    BitField<0, 8, Register> gpr0;
    BitField<8, 8, Register> gpr8;
    BitField<16, 4, Pred> pred;
    BitField<20, 8, Register> gpr20;
    BitField<20, 7, SubOp> sub_op;
    BitField<28, 8, Register> gpr28;
    BitField<36, 13, u64> imm36;
    BitField<39, 8, Register> gpr39;

    union {
        BitField<45, 1, u64> negate_b;
        BitField<46, 1, u64> abs_a;
        BitField<48, 1, u64> negate_a;
        BitField<49, 1, u64> abs_b;
        BitField<50, 1, u64> abs_d;
    } alu;

    union {
        BitField<48, 1, u64> negate_b;
        BitField<49, 1, u64> negate_c;
    } ffma;

    BitField<60, 1, u64> is_b_gpr;
    BitField<59, 1, u64> is_c_gpr;

    Attribute attribute;
    Uniform uniform;

    u64 hex;
};
static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
static_assert(std::is_standard_layout<Instruction>::value,
              "Structure does not have standard layout");

} // namespace Shader
} // namespace Tegra