summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/frontend/maxwell/translate/impl/texture_load_swizzled.cpp
blob: 0863bdfcd4224655467f54ea6f7b51dba2e121a6 (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
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <utility>

#include "common/bit_field.h"
#include "common/common_types.h"
#include "shader_recompiler/frontend/ir/modifiers.h"
#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"

namespace Shader::Maxwell {
namespace {
enum class Precision : u64 {
    F16,
    F32,
};

constexpr unsigned R = 1;
constexpr unsigned G = 2;
constexpr unsigned B = 4;
constexpr unsigned A = 8;

constexpr std::array RG_LUT{
    R,     //
    G,     //
    B,     //
    A,     //
    R | G, //
    R | A, //
    G | A, //
    B | A, //
};

constexpr std::array RGBA_LUT{
    R | G | B,     //
    R | G | A,     //
    R | B | A,     //
    G | B | A,     //
    R | G | B | A, //
};

union Encoding {
    u64 raw;
    BitField<59, 1, Precision> precision;
    BitField<54, 1, u64> aoffi;
    BitField<53, 1, u64> lod;
    BitField<55, 1, u64> ms;
    BitField<49, 1, u64> nodep;
    BitField<28, 8, IR::Reg> dest_reg_b;
    BitField<0, 8, IR::Reg> dest_reg_a;
    BitField<8, 8, IR::Reg> src_reg_a;
    BitField<20, 8, IR::Reg> src_reg_b;
    BitField<36, 13, u64> cbuf_offset;
    BitField<50, 3, u64> swizzle;
    BitField<53, 4, u64> encoding;
};

void CheckAlignment(IR::Reg reg, size_t alignment) {
    if (!IR::IsAligned(reg, alignment)) {
        throw NotImplementedException("Unaligned source register {}", reg);
    }
}

IR::Value MakeOffset(TranslatorVisitor& v, IR::Reg reg) {
    const IR::U32 value{v.X(reg)};
    return v.ir.CompositeConstruct(v.ir.BitFieldExtract(value, v.ir.Imm32(0), v.ir.Imm32(4), true),
                                   v.ir.BitFieldExtract(value, v.ir.Imm32(4), v.ir.Imm32(4), true));
}

IR::Value Sample(TranslatorVisitor& v, u64 insn) {
    const Encoding tlds{insn};
    const IR::U32 handle{v.ir.Imm32(static_cast<u32>(tlds.cbuf_offset * 4))};
    const IR::Reg reg_a{tlds.src_reg_a};
    const IR::Reg reg_b{tlds.src_reg_b};
    IR::Value coords;
    IR::U32 lod{v.ir.Imm32(0U)};
    IR::Value offsets;
    IR::U32 multisample;
    Shader::TextureType texture_type{};
    switch (tlds.encoding) {
    case 0:
        texture_type = Shader::TextureType::Color1D;
        coords = v.X(reg_a);
        break;
    case 1:
        texture_type = Shader::TextureType::Color1D;
        coords = v.X(reg_a);
        lod = v.X(reg_b);
        break;
    case 2:
        texture_type = Shader::TextureType::Color2D;
        coords = v.ir.CompositeConstruct(v.X(reg_a), v.X(reg_b));
        break;
    case 4:
        CheckAlignment(reg_a, 2);
        texture_type = Shader::TextureType::Color2D;
        coords = v.ir.CompositeConstruct(v.X(reg_a), v.X(reg_a + 1));
        offsets = MakeOffset(v, reg_b);
        break;
    case 5:
        CheckAlignment(reg_a, 2);
        texture_type = Shader::TextureType::Color2D;
        coords = v.ir.CompositeConstruct(v.X(reg_a), v.X(reg_a + 1));
        lod = v.X(reg_b);
        break;
    case 6:
        CheckAlignment(reg_a, 2);
        texture_type = Shader::TextureType::Color2D;
        coords = v.ir.CompositeConstruct(v.X(reg_a), v.X(reg_a + 1));
        multisample = v.X(reg_b);
        break;
    case 7:
        CheckAlignment(reg_a, 2);
        texture_type = Shader::TextureType::Color3D;
        coords = v.ir.CompositeConstruct(v.X(reg_a), v.X(reg_a + 1), v.X(reg_b));
        break;
    case 8: {
        CheckAlignment(reg_b, 2);
        const IR::U32 array{v.ir.BitFieldExtract(v.X(reg_a), v.ir.Imm32(0), v.ir.Imm32(16))};
        texture_type = Shader::TextureType::ColorArray2D;
        coords = v.ir.CompositeConstruct(v.X(reg_b), v.X(reg_b + 1), array);
        break;
    }
    case 12:
        CheckAlignment(reg_a, 2);
        CheckAlignment(reg_b, 2);
        texture_type = Shader::TextureType::Color2D;
        coords = v.ir.CompositeConstruct(v.X(reg_a), v.X(reg_a + 1));
        lod = v.X(reg_b);
        offsets = MakeOffset(v, reg_b + 1);
        break;
    default:
        throw NotImplementedException("Illegal encoding {}", tlds.encoding.Value());
    }
    IR::TextureInstInfo info{};
    if (tlds.precision == Precision::F16) {
        info.relaxed_precision.Assign(1);
    }
    info.type.Assign(texture_type);
    return v.ir.ImageFetch(handle, coords, offsets, lod, multisample, info);
}

unsigned Swizzle(u64 insn) {
    const Encoding tlds{insn};
    const size_t encoding{tlds.swizzle};
    if (tlds.dest_reg_b == IR::Reg::RZ) {
        if (encoding >= RG_LUT.size()) {
            throw NotImplementedException("Illegal RG encoding {}", encoding);
        }
        return RG_LUT[encoding];
    } else {
        if (encoding >= RGBA_LUT.size()) {
            throw NotImplementedException("Illegal RGBA encoding {}", encoding);
        }
        return RGBA_LUT[encoding];
    }
}

IR::F32 Extract(TranslatorVisitor& v, const IR::Value& sample, unsigned component) {
    return IR::F32{v.ir.CompositeExtract(sample, component)};
}

IR::Reg RegStoreComponent32(u64 insn, unsigned index) {
    const Encoding tlds{insn};
    switch (index) {
    case 0:
        return tlds.dest_reg_a;
    case 1:
        CheckAlignment(tlds.dest_reg_a, 2);
        return tlds.dest_reg_a + 1;
    case 2:
        return tlds.dest_reg_b;
    case 3:
        CheckAlignment(tlds.dest_reg_b, 2);
        return tlds.dest_reg_b + 1;
    }
    throw LogicError("Invalid store index {}", index);
}

void Store32(TranslatorVisitor& v, u64 insn, const IR::Value& sample) {
    const unsigned swizzle{Swizzle(insn)};
    unsigned store_index{0};
    for (unsigned component = 0; component < 4; ++component) {
        if (((swizzle >> component) & 1) == 0) {
            continue;
        }
        const IR::Reg dest{RegStoreComponent32(insn, store_index)};
        v.F(dest, Extract(v, sample, component));
        ++store_index;
    }
}

IR::U32 Pack(TranslatorVisitor& v, const IR::F32& lhs, const IR::F32& rhs) {
    return v.ir.PackHalf2x16(v.ir.CompositeConstruct(lhs, rhs));
}

void Store16(TranslatorVisitor& v, u64 insn, const IR::Value& sample) {
    const unsigned swizzle{Swizzle(insn)};
    unsigned store_index{0};
    std::array<IR::F32, 4> swizzled;
    for (unsigned component = 0; component < 4; ++component) {
        if (((swizzle >> component) & 1) == 0) {
            continue;
        }
        swizzled[store_index] = Extract(v, sample, component);
        ++store_index;
    }
    const IR::F32 zero{v.ir.Imm32(0.0f)};
    const Encoding tlds{insn};
    switch (store_index) {
    case 1:
        v.X(tlds.dest_reg_a, Pack(v, swizzled[0], zero));
        break;
    case 2:
    case 3:
    case 4:
        v.X(tlds.dest_reg_a, Pack(v, swizzled[0], swizzled[1]));
        switch (store_index) {
        case 2:
            break;
        case 3:
            v.X(tlds.dest_reg_b, Pack(v, swizzled[2], zero));
            break;
        case 4:
            v.X(tlds.dest_reg_b, Pack(v, swizzled[2], swizzled[3]));
            break;
        }
        break;
    }
}
} // Anonymous namespace

void TranslatorVisitor::TLDS(u64 insn) {
    const IR::Value sample{Sample(*this, insn)};
    if (Encoding{insn}.precision == Precision::F32) {
        Store32(*this, insn, sample);
    } else {
        Store16(*this, insn, sample);
    }
}
} // namespace Shader::Maxwell