diff options
Diffstat (limited to 'src/shader_recompiler/frontend/maxwell/translate/impl/impl.cpp')
| -rw-r--r-- | src/shader_recompiler/frontend/maxwell/translate/impl/impl.cpp | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/impl.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/impl.cpp new file mode 100644 index 000000000..b446aae0e --- /dev/null +++ b/src/shader_recompiler/frontend/maxwell/translate/impl/impl.cpp | |||
| @@ -0,0 +1,272 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/bit_field.h" | ||
| 6 | #include "shader_recompiler/frontend/ir/ir_emitter.h" | ||
| 7 | #include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" | ||
| 8 | |||
| 9 | namespace Shader::Maxwell { | ||
| 10 | namespace { | ||
| 11 | [[nodiscard]] IR::U32 CbufLowerBits(IR::IREmitter& ir, bool unaligned, const IR::U32& binding, | ||
| 12 | u32 offset) { | ||
| 13 | if (unaligned) { | ||
| 14 | return ir.Imm32(0); | ||
| 15 | } | ||
| 16 | return ir.GetCbuf(binding, IR::U32{IR::Value{offset}}); | ||
| 17 | } | ||
| 18 | } // Anonymous namespace | ||
| 19 | |||
| 20 | IR::U32 TranslatorVisitor::X(IR::Reg reg) { | ||
| 21 | return ir.GetReg(reg); | ||
| 22 | } | ||
| 23 | |||
| 24 | IR::U64 TranslatorVisitor::L(IR::Reg reg) { | ||
| 25 | if (!IR::IsAligned(reg, 2)) { | ||
| 26 | throw NotImplementedException("Unaligned source register {}", reg); | ||
| 27 | } | ||
| 28 | return IR::U64{ir.PackUint2x32(ir.CompositeConstruct(X(reg), X(reg + 1)))}; | ||
| 29 | } | ||
| 30 | |||
| 31 | IR::F32 TranslatorVisitor::F(IR::Reg reg) { | ||
| 32 | return ir.BitCast<IR::F32>(X(reg)); | ||
| 33 | } | ||
| 34 | |||
| 35 | IR::F64 TranslatorVisitor::D(IR::Reg reg) { | ||
| 36 | if (!IR::IsAligned(reg, 2)) { | ||
| 37 | throw NotImplementedException("Unaligned source register {}", reg); | ||
| 38 | } | ||
| 39 | return IR::F64{ir.PackDouble2x32(ir.CompositeConstruct(X(reg), X(reg + 1)))}; | ||
| 40 | } | ||
| 41 | |||
| 42 | void TranslatorVisitor::X(IR::Reg dest_reg, const IR::U32& value) { | ||
| 43 | ir.SetReg(dest_reg, value); | ||
| 44 | } | ||
| 45 | |||
| 46 | void TranslatorVisitor::L(IR::Reg dest_reg, const IR::U64& value) { | ||
| 47 | if (!IR::IsAligned(dest_reg, 2)) { | ||
| 48 | throw NotImplementedException("Unaligned destination register {}", dest_reg); | ||
| 49 | } | ||
| 50 | const IR::Value result{ir.UnpackUint2x32(value)}; | ||
| 51 | for (int i = 0; i < 2; i++) { | ||
| 52 | X(dest_reg + i, IR::U32{ir.CompositeExtract(result, static_cast<size_t>(i))}); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | void TranslatorVisitor::F(IR::Reg dest_reg, const IR::F32& value) { | ||
| 57 | X(dest_reg, ir.BitCast<IR::U32>(value)); | ||
| 58 | } | ||
| 59 | |||
| 60 | void TranslatorVisitor::D(IR::Reg dest_reg, const IR::F64& value) { | ||
| 61 | if (!IR::IsAligned(dest_reg, 2)) { | ||
| 62 | throw NotImplementedException("Unaligned destination register {}", dest_reg); | ||
| 63 | } | ||
| 64 | const IR::Value result{ir.UnpackDouble2x32(value)}; | ||
| 65 | for (int i = 0; i < 2; i++) { | ||
| 66 | X(dest_reg + i, IR::U32{ir.CompositeExtract(result, static_cast<size_t>(i))}); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | IR::U32 TranslatorVisitor::GetReg8(u64 insn) { | ||
| 71 | union { | ||
| 72 | u64 raw; | ||
| 73 | BitField<8, 8, IR::Reg> index; | ||
| 74 | } const reg{insn}; | ||
| 75 | return X(reg.index); | ||
| 76 | } | ||
| 77 | |||
| 78 | IR::U32 TranslatorVisitor::GetReg20(u64 insn) { | ||
| 79 | union { | ||
| 80 | u64 raw; | ||
| 81 | BitField<20, 8, IR::Reg> index; | ||
| 82 | } const reg{insn}; | ||
| 83 | return X(reg.index); | ||
| 84 | } | ||
| 85 | |||
| 86 | IR::U32 TranslatorVisitor::GetReg39(u64 insn) { | ||
| 87 | union { | ||
| 88 | u64 raw; | ||
| 89 | BitField<39, 8, IR::Reg> index; | ||
| 90 | } const reg{insn}; | ||
| 91 | return X(reg.index); | ||
| 92 | } | ||
| 93 | |||
| 94 | IR::F32 TranslatorVisitor::GetFloatReg8(u64 insn) { | ||
| 95 | return ir.BitCast<IR::F32>(GetReg8(insn)); | ||
| 96 | } | ||
| 97 | |||
| 98 | IR::F32 TranslatorVisitor::GetFloatReg20(u64 insn) { | ||
| 99 | return ir.BitCast<IR::F32>(GetReg20(insn)); | ||
| 100 | } | ||
| 101 | |||
| 102 | IR::F32 TranslatorVisitor::GetFloatReg39(u64 insn) { | ||
| 103 | return ir.BitCast<IR::F32>(GetReg39(insn)); | ||
| 104 | } | ||
| 105 | |||
| 106 | IR::F64 TranslatorVisitor::GetDoubleReg20(u64 insn) { | ||
| 107 | union { | ||
| 108 | u64 raw; | ||
| 109 | BitField<20, 8, IR::Reg> index; | ||
| 110 | } const reg{insn}; | ||
| 111 | return D(reg.index); | ||
| 112 | } | ||
| 113 | |||
| 114 | IR::F64 TranslatorVisitor::GetDoubleReg39(u64 insn) { | ||
| 115 | union { | ||
| 116 | u64 raw; | ||
| 117 | BitField<39, 8, IR::Reg> index; | ||
| 118 | } const reg{insn}; | ||
| 119 | return D(reg.index); | ||
| 120 | } | ||
| 121 | |||
| 122 | static std::pair<IR::U32, IR::U32> CbufAddr(u64 insn) { | ||
| 123 | union { | ||
| 124 | u64 raw; | ||
| 125 | BitField<20, 14, u64> offset; | ||
| 126 | BitField<34, 5, u64> binding; | ||
| 127 | } const cbuf{insn}; | ||
| 128 | |||
| 129 | if (cbuf.binding >= 18) { | ||
| 130 | throw NotImplementedException("Out of bounds constant buffer binding {}", cbuf.binding); | ||
| 131 | } | ||
| 132 | if (cbuf.offset >= 0x10'000) { | ||
| 133 | throw NotImplementedException("Out of bounds constant buffer offset {}", cbuf.offset); | ||
| 134 | } | ||
| 135 | const IR::Value binding{static_cast<u32>(cbuf.binding)}; | ||
| 136 | const IR::Value byte_offset{static_cast<u32>(cbuf.offset) * 4}; | ||
| 137 | return {IR::U32{binding}, IR::U32{byte_offset}}; | ||
| 138 | } | ||
| 139 | |||
| 140 | IR::U32 TranslatorVisitor::GetCbuf(u64 insn) { | ||
| 141 | const auto [binding, byte_offset]{CbufAddr(insn)}; | ||
| 142 | return ir.GetCbuf(binding, byte_offset); | ||
| 143 | } | ||
| 144 | |||
| 145 | IR::F32 TranslatorVisitor::GetFloatCbuf(u64 insn) { | ||
| 146 | const auto [binding, byte_offset]{CbufAddr(insn)}; | ||
| 147 | return ir.GetFloatCbuf(binding, byte_offset); | ||
| 148 | } | ||
| 149 | |||
| 150 | IR::F64 TranslatorVisitor::GetDoubleCbuf(u64 insn) { | ||
| 151 | union { | ||
| 152 | u64 raw; | ||
| 153 | BitField<20, 1, u64> unaligned; | ||
| 154 | } const cbuf{insn}; | ||
| 155 | |||
| 156 | const auto [binding, offset_value]{CbufAddr(insn)}; | ||
| 157 | const bool unaligned{cbuf.unaligned != 0}; | ||
| 158 | const u32 offset{offset_value.U32()}; | ||
| 159 | const IR::Value addr{unaligned ? offset | 4u : (offset & ~7u) | 4u}; | ||
| 160 | |||
| 161 | const IR::U32 value{ir.GetCbuf(binding, IR::U32{addr})}; | ||
| 162 | const IR::U32 lower_bits{CbufLowerBits(ir, unaligned, binding, offset)}; | ||
| 163 | return ir.PackDouble2x32(ir.CompositeConstruct(lower_bits, value)); | ||
| 164 | } | ||
| 165 | |||
| 166 | IR::U64 TranslatorVisitor::GetPackedCbuf(u64 insn) { | ||
| 167 | union { | ||
| 168 | u64 raw; | ||
| 169 | BitField<20, 1, u64> unaligned; | ||
| 170 | } const cbuf{insn}; | ||
| 171 | |||
| 172 | if (cbuf.unaligned != 0) { | ||
| 173 | throw NotImplementedException("Unaligned packed constant buffer read"); | ||
| 174 | } | ||
| 175 | const auto [binding, lower_offset]{CbufAddr(insn)}; | ||
| 176 | const IR::U32 upper_offset{ir.Imm32(lower_offset.U32() + 4)}; | ||
| 177 | const IR::U32 lower_value{ir.GetCbuf(binding, lower_offset)}; | ||
| 178 | const IR::U32 upper_value{ir.GetCbuf(binding, upper_offset)}; | ||
| 179 | return ir.PackUint2x32(ir.CompositeConstruct(lower_value, upper_value)); | ||
| 180 | } | ||
| 181 | |||
| 182 | IR::U32 TranslatorVisitor::GetImm20(u64 insn) { | ||
| 183 | union { | ||
| 184 | u64 raw; | ||
| 185 | BitField<20, 19, u64> value; | ||
| 186 | BitField<56, 1, u64> is_negative; | ||
| 187 | } const imm{insn}; | ||
| 188 | |||
| 189 | if (imm.is_negative != 0) { | ||
| 190 | const s64 raw{static_cast<s64>(imm.value)}; | ||
| 191 | return ir.Imm32(static_cast<s32>(-(1LL << 19) + raw)); | ||
| 192 | } else { | ||
| 193 | return ir.Imm32(static_cast<u32>(imm.value)); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | IR::F32 TranslatorVisitor::GetFloatImm20(u64 insn) { | ||
| 198 | union { | ||
| 199 | u64 raw; | ||
| 200 | BitField<20, 19, u64> value; | ||
| 201 | BitField<56, 1, u64> is_negative; | ||
| 202 | } const imm{insn}; | ||
| 203 | const u32 sign_bit{static_cast<u32>(imm.is_negative != 0 ? (1ULL << 31) : 0)}; | ||
| 204 | const u32 value{static_cast<u32>(imm.value) << 12}; | ||
| 205 | return ir.Imm32(Common::BitCast<f32>(value | sign_bit)); | ||
| 206 | } | ||
| 207 | |||
| 208 | IR::F64 TranslatorVisitor::GetDoubleImm20(u64 insn) { | ||
| 209 | union { | ||
| 210 | u64 raw; | ||
| 211 | BitField<20, 19, u64> value; | ||
| 212 | BitField<56, 1, u64> is_negative; | ||
| 213 | } const imm{insn}; | ||
| 214 | const u64 sign_bit{imm.is_negative != 0 ? (1ULL << 63) : 0}; | ||
| 215 | const u64 value{imm.value << 44}; | ||
| 216 | return ir.Imm64(Common::BitCast<f64>(value | sign_bit)); | ||
| 217 | } | ||
| 218 | |||
| 219 | IR::U64 TranslatorVisitor::GetPackedImm20(u64 insn) { | ||
| 220 | const s64 value{GetImm20(insn).U32()}; | ||
| 221 | return ir.Imm64(static_cast<u64>(static_cast<s64>(value) << 32)); | ||
| 222 | } | ||
| 223 | |||
| 224 | IR::U32 TranslatorVisitor::GetImm32(u64 insn) { | ||
| 225 | union { | ||
| 226 | u64 raw; | ||
| 227 | BitField<20, 32, u64> value; | ||
| 228 | } const imm{insn}; | ||
| 229 | return ir.Imm32(static_cast<u32>(imm.value)); | ||
| 230 | } | ||
| 231 | |||
| 232 | IR::F32 TranslatorVisitor::GetFloatImm32(u64 insn) { | ||
| 233 | union { | ||
| 234 | u64 raw; | ||
| 235 | BitField<20, 32, u64> value; | ||
| 236 | } const imm{insn}; | ||
| 237 | return ir.Imm32(Common::BitCast<f32>(static_cast<u32>(imm.value))); | ||
| 238 | } | ||
| 239 | |||
| 240 | void TranslatorVisitor::SetZFlag(const IR::U1& value) { | ||
| 241 | ir.SetZFlag(value); | ||
| 242 | } | ||
| 243 | |||
| 244 | void TranslatorVisitor::SetSFlag(const IR::U1& value) { | ||
| 245 | ir.SetSFlag(value); | ||
| 246 | } | ||
| 247 | |||
| 248 | void TranslatorVisitor::SetCFlag(const IR::U1& value) { | ||
| 249 | ir.SetCFlag(value); | ||
| 250 | } | ||
| 251 | |||
| 252 | void TranslatorVisitor::SetOFlag(const IR::U1& value) { | ||
| 253 | ir.SetOFlag(value); | ||
| 254 | } | ||
| 255 | |||
| 256 | void TranslatorVisitor::ResetZero() { | ||
| 257 | SetZFlag(ir.Imm1(false)); | ||
| 258 | } | ||
| 259 | |||
| 260 | void TranslatorVisitor::ResetSFlag() { | ||
| 261 | SetSFlag(ir.Imm1(false)); | ||
| 262 | } | ||
| 263 | |||
| 264 | void TranslatorVisitor::ResetCFlag() { | ||
| 265 | SetCFlag(ir.Imm1(false)); | ||
| 266 | } | ||
| 267 | |||
| 268 | void TranslatorVisitor::ResetOFlag() { | ||
| 269 | SetOFlag(ir.Imm1(false)); | ||
| 270 | } | ||
| 271 | |||
| 272 | } // namespace Shader::Maxwell | ||