diff --git a/README.md b/README.md index 5803bbea3..1e08b86d5 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,10 @@ Releases will be published after the first full successful launch of at least on ## Status Execution reaches `_start`, stack unwinding and exception handling tables are built. All unimplemented functions throw std::runtime_error. `what()` is printed to stderr and the process terminates. -Audio output and video output initialization pass +Audio output and video output initialization pass. Reaches the `main` function with render loop (deep call chain, ~64KB stack frame function) - init and shader loading from disk fully complete. -Now: `ShaderRecompiler::Recompile: RdnaInstructionDecoder::Decode not implemented`. +Now: `ShaderRecompiler::Recompile: GraphBuilder::Build not implemented`. ## Disclaimer diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaInstructionDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaInstructionDecoder.cpp index 2bf82c11d..e6adfc705 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaInstructionDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaInstructionDecoder.cpp @@ -1,7 +1,15 @@ #include "RdnaDecoder/RdnaInstructionDecoder.hpp" +#include "RdnaDecoder/RdnaExportOpDecoder.hpp" +#include "RdnaDecoder/RdnaImageOpDecoder.hpp" +#include "RdnaDecoder/RdnaMemoryOpDecoder.hpp" +#include "RdnaDecoder/RdnaOpcode.hpp" +#include "RdnaDecoder/RdnaScalarOpDecoder.hpp" +#include "RdnaDecoder/RdnaVectorOpDecoder.hpp" #include +#include #include #include +#include namespace ShaderRecompiler { @@ -23,30 +31,151 @@ void applyLiteral(RdnaOperand& operand, std::uint32_t literal) { operand.signedVal = static_cast(literal); } +std::string toHexString(std::uint32_t value) { + char buffer[11]; + std::snprintf(buffer, sizeof(buffer), "0x%08x", value); + return std::string(buffer); +} + } RdnaProgram RdnaInstructionDecoder::Decode(std::span code) const { - throw std::runtime_error("RdnaInstructionDecoder::Decode not implemented"); + RdnaProgram program; + DecodeRdnaProgram(code, program); + return program; } RdnaInstruction RdnaInstructionDecoder::decodeAt(std::span code, std::uint32_t wordIndex) const { - throw std::runtime_error("RdnaInstructionDecoder::decodeAt not implemented"); + if (wordIndex >= code.size()) { + throw std::out_of_range("word index is out of the code span bounds"); + } + + const std::uint32_t programCounter = wordIndex * static_cast(sizeof(std::uint32_t)); + return DecodeRdnaInstruction(programCounter, code, wordIndex); } RdnaInstructionFamily GetRdnaInstructionFamily(std::uint32_t word) { - throw std::runtime_error("GetRdnaInstructionFamily not implemented"); + if ((word & 0x80000000u) == 0u) { + switch ((word >> 25u) & 0x3fu) { + case 0x3eu: return RdnaInstructionFamily::VOPC; + case 0x3fu: return RdnaInstructionFamily::VOP1; + default: return RdnaInstructionFamily::VOP2; + } + } + + if ((word & 0xc0000000u) == 0x80000000u) { + const std::uint32_t opcode = (word >> 23u) & 0x7fu; + switch (opcode) { + case 0x7du: return RdnaInstructionFamily::SOP1; + case 0x7eu: return RdnaInstructionFamily::SOPC; + case 0x7fu: return RdnaInstructionFamily::SOPP; + default: return opcode >= 0x60u ? RdnaInstructionFamily::SOPK : RdnaInstructionFamily::SOP2; + } + } + + switch (word >> 26u) { + case 0x32u: return RdnaInstructionFamily::VINTRP; + case 0x33u: return RdnaInstructionFamily::VOP3P; + case 0x35u: return RdnaInstructionFamily::VOP3; + case 0x36u: return RdnaInstructionFamily::DS; + case 0x37u: return RdnaInstructionFamily::FLAT; + case 0x38u: return RdnaInstructionFamily::MUBUF; + case 0x3au: return RdnaInstructionFamily::MTBUF; + case 0x3cu: return RdnaInstructionFamily::MIMG; + case 0x3du: return RdnaInstructionFamily::SMEM; + case 0x3eu: return RdnaInstructionFamily::EXP; + default: return RdnaInstructionFamily::Unknown; + } } RdnaInstruction DecodeRdnaInstruction(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex) { - throw std::runtime_error("DecodeRdnaInstruction not implemented"); + if (wordIndex >= code.size()) { + throw std::out_of_range("word index is out of the code span bounds"); + } + + switch (GetRdnaInstructionFamily(code[wordIndex])) { + case RdnaInstructionFamily::SOP1: return DecodeRdnaSop1(programCounter, code, wordIndex); + case RdnaInstructionFamily::SOP2: return DecodeRdnaSop2(programCounter, code, wordIndex); + case RdnaInstructionFamily::SOPK: return DecodeRdnaSopk(programCounter, code, wordIndex); + case RdnaInstructionFamily::SOPC: return DecodeRdnaSopc(programCounter, code, wordIndex); + case RdnaInstructionFamily::SOPP: return DecodeRdnaSopp(programCounter, code, wordIndex); + case RdnaInstructionFamily::VOP1: return DecodeRdnaVop1(programCounter, code, wordIndex); + case RdnaInstructionFamily::VOP2: return DecodeRdnaVop2(programCounter, code, wordIndex); + case RdnaInstructionFamily::VOP3: return DecodeRdnaVop3(programCounter, code, wordIndex); + case RdnaInstructionFamily::VOP3P: return DecodeRdnaVop3p(programCounter, code, wordIndex); + case RdnaInstructionFamily::VOPC: return DecodeRdnaVopc(programCounter, code, wordIndex); + case RdnaInstructionFamily::VINTRP: return DecodeRdnaVintrp(programCounter, code, wordIndex); + case RdnaInstructionFamily::SMEM: return DecodeRdnaSmem(programCounter, code, wordIndex); + case RdnaInstructionFamily::MUBUF: return DecodeRdnaMubuf(programCounter, code, wordIndex); + case RdnaInstructionFamily::MTBUF: return DecodeRdnaMtbuf(programCounter, code, wordIndex); + case RdnaInstructionFamily::FLAT: return DecodeRdnaFlat(programCounter, code, wordIndex); + case RdnaInstructionFamily::DS: return DecodeRdnaDs(programCounter, code, wordIndex); + case RdnaInstructionFamily::MIMG: return DecodeRdnaMimg(programCounter, code, wordIndex); + case RdnaInstructionFamily::EXP: return DecodeRdnaExportOp(programCounter, code, wordIndex); + default: break; + } + + throw std::invalid_argument("unknown RDNA instruction family at program counter " + toHexString(programCounter) + " raw word " + toHexString(code[wordIndex])); } RdnaProgram DecodeRdnaFrontProgram(std::span front) { - throw std::runtime_error("DecodeRdnaFrontProgram not implemented"); + RdnaProgram result; + std::uint32_t frontWords = 0; + + while (frontWords < front.size()) { + const std::uint32_t programCounter = frontWords * static_cast(sizeof(std::uint32_t)); + result.instructions.push_back(DecodeRdnaInstruction(programCounter, front, frontWords)); + + const RdnaInstruction& instruction = result.instructions.back(); + frontWords += instruction.wordCount; + + if (instruction.op == RdnaOpcode::SSetpcB64) { + if (instruction.source0.kind != RdnaOperandKind::ScalarRegister || instruction.source0.reg != 6u) { + throw std::invalid_argument("s_setpc_b64 in the front program does not target scalar register s6"); + } + + result.code = front.first(frontWords); + return result; + } + + if (instruction.op == RdnaOpcode::SEndpgm) { + throw std::invalid_argument("front program reached s_endpgm before s_setpc_b64"); + } + } + + throw std::out_of_range("front program decode reached the code boundary before s_setpc_b64"); } void DecodeRdnaProgram(std::span code, RdnaProgram& program) { - throw std::runtime_error("DecodeRdnaProgram not implemented"); + program.instructions.clear(); + program.instructions.reserve(code.size()); + program.code = code; + + std::vector branchTargets; + for (std::uint32_t wordIndex = 0; wordIndex < code.size();) { + const std::uint32_t programCounter = wordIndex * static_cast(sizeof(std::uint32_t)); + program.instructions.push_back(DecodeRdnaInstruction(programCounter, code, wordIndex)); + + const RdnaInstruction& instruction = program.instructions.back(); + wordIndex += instruction.wordCount; + + if (IsDirectBranchOpcode(instruction.op)) { + const std::uint32_t targetIndex = instruction.branchTarget / static_cast(sizeof(std::uint32_t)); + if (branchTargets.empty()) { + branchTargets.resize(code.size()); + } + if (targetIndex >= branchTargets.size()) { + throw std::out_of_range("branch target is out of the code span bounds"); + } + branchTargets[targetIndex] = true; + } + + if (instruction.op == RdnaOpcode::SEndpgm && (wordIndex >= code.size() || branchTargets.empty() || !branchTargets[wordIndex])) { + return; + } + } + + throw std::out_of_range("RDNA program decode reached the code boundary before s_endpgm"); } RdnaOperand DecodeRdnaScalarSource(std::uint32_t code, std::uint32_t programCounter) { @@ -166,23 +295,73 @@ void ReadRdnaLiteralOperands(std::span code, std::uint32_t } void SetRdnaUnsupported(RdnaInstruction& instruction, RdnaInstructionFamily family, std::uint32_t opcodeId, const char* reason) { - throw std::runtime_error("SetRdnaUnsupported not implemented"); + throw std::invalid_argument("unsupported RDNA instruction, family=" + std::to_string(static_cast(family)) + " opcode=" + toHexString(opcodeId) + " reason=" + reason + " pc=" + toHexString(instruction.programCounter)); } std::string RdnaOperandToString(const RdnaOperand& operand) { - throw std::runtime_error("RdnaOperandToString not implemented"); + switch (operand.kind) { + case RdnaOperandKind::None: return "none"; + case RdnaOperandKind::ScalarRegister: return "s" + std::to_string(operand.reg); + case RdnaOperandKind::VectorRegister: return "v" + std::to_string(operand.reg); + case RdnaOperandKind::VccLo: return "vcc_lo"; + case RdnaOperandKind::VccHi: return "vcc_hi"; + case RdnaOperandKind::ExecLo: return "exec_lo"; + case RdnaOperandKind::ExecHi: return "exec_hi"; + case RdnaOperandKind::Scc: return "scc"; + case RdnaOperandKind::Null: return "null"; + case RdnaOperandKind::LiteralConstant: return toHexString(operand.value); + case RdnaOperandKind::IntegerInlineConstant: return std::to_string(operand.signedVal); + case RdnaOperandKind::FloatInlineConstant: return toHexString(operand.value); + case RdnaOperandKind::Unknown: return "unknown"; + case RdnaOperandKind::VccZ: return "vccz"; + case RdnaOperandKind::ExecZ: return "execz"; + case RdnaOperandKind::M0: return "m0"; + case RdnaOperandKind::PopsExitingWaveId: return "pops_exiting_wave_id"; + } + + throw std::invalid_argument("unsupported operand kind for string conversion"); } const char* RdnaImageDimensionToString(RdnaImageDimension dimension) { - throw std::runtime_error("RdnaImageDimensionToString not implemented"); + switch (dimension) { + case RdnaImageDimension::Unknown: return "unknown"; + case RdnaImageDimension::Dim1D: return "1d"; + case RdnaImageDimension::Dim1DArray: return "1d_array"; + case RdnaImageDimension::Dim2D: return "2d"; + case RdnaImageDimension::Dim3D: return "3d"; + case RdnaImageDimension::Dim2DArray: return "2d_array"; + case RdnaImageDimension::Dim2DMsaa: return "2d_msaa"; + case RdnaImageDimension::Dim2DMsaaArray: return "2d_msaa_array"; + } + + throw std::invalid_argument("unsupported image dimension for string conversion"); } std::string RdnaInstructionToString(const RdnaInstruction& instruction) { - throw std::runtime_error("RdnaInstructionToString not implemented"); + std::string text = toHexString(instruction.programCounter) + ": family=" + std::to_string(static_cast(instruction.family)) + " opcode=" + toHexString(instruction.opcodeId); + + if (instruction.destination.kind != RdnaOperandKind::Unknown) { + text += " dst=" + RdnaOperandToString(instruction.destination); + } + if (instruction.destination2.kind != RdnaOperandKind::Unknown) { + text += " dst2=" + RdnaOperandToString(instruction.destination2); + } + + const RdnaOperand* sources[] = {&instruction.source0, &instruction.source1, &instruction.source2, &instruction.source3}; + for (std::uint32_t i = 0; i < instruction.sourceCount && i < 4u; ++i) { + text += " src" + std::to_string(i) + "=" + RdnaOperandToString(*sources[i]); + } + + return text; } std::string RdnaProgramToString(const RdnaProgram& program) { - throw std::runtime_error("RdnaProgramToString not implemented"); + std::string text; + for (const RdnaInstruction& instruction : program.instructions) { + text += RdnaInstructionToString(instruction); + text += "\n"; + } + return text; } } diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaProgram.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaProgram.cpp index fa8400999..c68337725 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaProgram.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaProgram.cpp @@ -1,10 +1,15 @@ #include "RdnaDecoder/RdnaProgram.hpp" -#include +#include namespace ShaderRecompiler { -const RdnaInstruction* FindInstructionAtProgramCounter(const RdnaProgram& program, std::uint32_t programCounter) { - throw std::runtime_error("FindInstructionAtProgramCounter not implemented"); -} + const RdnaInstruction* FindInstructionAtProgramCounter(const RdnaProgram& program, std::uint32_t programCounter) { + const auto it = std::lower_bound(program.instructions.begin(), program.instructions.end(), programCounter, + [](const RdnaInstruction& instruction, std::uint32_t pc) { return instruction.programCounter < pc; }); + if (it == program.instructions.end() || it->programCounter != programCounter) { + return nullptr; + } + return &(*it); + } }