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

#include <array>
#include <bit>

#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 Type : u64 {
    _1D,
    BUFFER_1D,
    ARRAY_1D,
    _2D,
    ARRAY_2D,
    _3D,
};

enum class Size : u64 {
    U32,
    S32,
    U64,
    S64,
    F32FTZRN,
    F16x2FTZRN,
    SD32,
    SD64,
};

enum class AtomicOp : u64 {
    ADD,
    MIN,
    MAX,
    INC,
    DEC,
    AND,
    OR,
    XOR,
    EXCH,
};

enum class Clamp : u64 {
    IGN,
    Default,
    TRAP,
};

TextureType GetType(Type type) {
    switch (type) {
    case Type::_1D:
        return TextureType::Color1D;
    case Type::BUFFER_1D:
        return TextureType::Buffer;
    case Type::ARRAY_1D:
        return TextureType::ColorArray1D;
    case Type::_2D:
        return TextureType::Color2D;
    case Type::ARRAY_2D:
        return TextureType::ColorArray2D;
    case Type::_3D:
        return TextureType::Color3D;
    }
    throw NotImplementedException("Invalid type {}", type);
}

IR::Value MakeCoords(TranslatorVisitor& v, IR::Reg reg, Type type) {
    switch (type) {
    case Type::_1D:
    case Type::BUFFER_1D:
        return v.X(reg);
    case Type::_2D:
        return v.ir.CompositeConstruct(v.X(reg), v.X(reg + 1));
    case Type::_3D:
        return v.ir.CompositeConstruct(v.X(reg), v.X(reg + 1), v.X(reg + 2));
    default:
        break;
    }
    throw NotImplementedException("Invalid type {}", type);
}

IR::Value ApplyAtomicOp(IR::IREmitter& ir, const IR::U32& handle, const IR::Value& coords,
                        const IR::Value& op_b, IR::TextureInstInfo info, AtomicOp op,
                        bool is_signed) {
    switch (op) {
    case AtomicOp::ADD:
        return ir.ImageAtomicIAdd(handle, coords, op_b, info);
    case AtomicOp::MIN:
        return ir.ImageAtomicIMin(handle, coords, op_b, is_signed, info);
    case AtomicOp::MAX:
        return ir.ImageAtomicIMax(handle, coords, op_b, is_signed, info);
    case AtomicOp::INC:
        return ir.ImageAtomicInc(handle, coords, op_b, info);
    case AtomicOp::DEC:
        return ir.ImageAtomicDec(handle, coords, op_b, info);
    case AtomicOp::AND:
        return ir.ImageAtomicAnd(handle, coords, op_b, info);
    case AtomicOp::OR:
        return ir.ImageAtomicOr(handle, coords, op_b, info);
    case AtomicOp::XOR:
        return ir.ImageAtomicXor(handle, coords, op_b, info);
    case AtomicOp::EXCH:
        return ir.ImageAtomicExchange(handle, coords, op_b, info);
    default:
        throw NotImplementedException("Atomic Operation {}", op);
    }
}

ImageFormat Format(Size size) {
    switch (size) {
    case Size::U32:
    case Size::S32:
    case Size::SD32:
        return ImageFormat::R32_UINT;
    default:
        break;
    }
    throw NotImplementedException("Invalid size {}", size);
}

bool IsSizeInt32(Size size) {
    switch (size) {
    case Size::U32:
    case Size::S32:
    case Size::SD32:
        return true;
    default:
        return false;
    }
}

void ImageAtomOp(TranslatorVisitor& v, IR::Reg dest_reg, IR::Reg operand_reg, IR::Reg coord_reg,
                 IR::Reg bindless_reg, AtomicOp op, Clamp clamp, Size size, Type type,
                 u64 bound_offset, bool is_bindless, bool write_result) {
    if (clamp != Clamp::IGN) {
        throw NotImplementedException("Clamp {}", clamp);
    }
    if (!IsSizeInt32(size)) {
        throw NotImplementedException("Size {}", size);
    }
    const bool is_signed{size == Size::S32};
    const ImageFormat format{Format(size)};
    const TextureType tex_type{GetType(type)};
    const IR::Value coords{MakeCoords(v, coord_reg, type)};

    const IR::U32 handle{is_bindless != 0 ? v.X(bindless_reg)
                                          : v.ir.Imm32(static_cast<u32>(bound_offset * 4))};
    IR::TextureInstInfo info{};
    info.type.Assign(tex_type);
    info.image_format.Assign(format);

    // TODO: float/64-bit operand
    const IR::Value op_b{v.X(operand_reg)};
    const IR::Value color{ApplyAtomicOp(v.ir, handle, coords, op_b, info, op, is_signed)};

    if (write_result) {
        v.X(dest_reg, IR::U32{color});
    }
}
} // Anonymous namespace

void TranslatorVisitor::SUATOM(u64 insn) {
    union {
        u64 raw;
        BitField<54, 1, u64> is_bindless;
        BitField<29, 4, AtomicOp> op;
        BitField<33, 3, Type> type;
        BitField<51, 3, Size> size;
        BitField<49, 2, Clamp> clamp;
        BitField<0, 8, IR::Reg> dest_reg;
        BitField<8, 8, IR::Reg> coord_reg;
        BitField<20, 8, IR::Reg> operand_reg;
        BitField<36, 13, u64> bound_offset;    // !is_bindless
        BitField<39, 8, IR::Reg> bindless_reg; // is_bindless
    } const suatom{insn};

    ImageAtomOp(*this, suatom.dest_reg, suatom.operand_reg, suatom.coord_reg, suatom.bindless_reg,
                suatom.op, suatom.clamp, suatom.size, suatom.type, suatom.bound_offset,
                suatom.is_bindless != 0, true);
}

void TranslatorVisitor::SURED(u64 insn) {
    // TODO: confirm offsets
    union {
        u64 raw;
        BitField<51, 1, u64> is_bound;
        BitField<21, 3, AtomicOp> op;
        BitField<33, 3, Type> type;
        BitField<20, 3, Size> size;
        BitField<49, 2, Clamp> clamp;
        BitField<0, 8, IR::Reg> operand_reg;
        BitField<8, 8, IR::Reg> coord_reg;
        BitField<36, 13, u64> bound_offset;    // is_bound
        BitField<39, 8, IR::Reg> bindless_reg; // !is_bound
    } const sured{insn};
    ImageAtomOp(*this, IR::Reg::RZ, sured.operand_reg, sured.coord_reg, sured.bindless_reg,
                sured.op, sured.clamp, sured.size, sured.type, sured.bound_offset,
                sured.is_bound == 0, false);
}

} // namespace Shader::Maxwell