From e2634022962479da8a539c6afdf98e84a698203e Mon Sep 17 00:00:00 2001 From: Chad Date: Mon, 5 Oct 2026 19:56:34 -0500 Subject: [PATCH 1/2] feat(shader): implement image_msaa_load Decode MIMG opcode 0x80 on 2D MSAA and 2D MSAA array images with a single-bit dmask and return four dwords: the selected component of the fragment named in the address and the three that follow it. Each fragment is read with its own image fetch, adding the fragment step to the packed address dword so a16 layouts keep working. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 8 +++ core/libs/tests/ImageMsaaLoad.cpp | 69 +++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 1 + .../RdnaDecoder/src/RdnaImageOpDecoder.cpp | 9 ++- .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 1 + .../Translation/TranslationContext.hpp | 3 +- .../MemoryInstructions/ImageInstructions.cpp | 14 ++++ .../MemoryInstructionDispatch.cpp | 2 + .../MemoryResourceHelpers.cpp | 7 +- 9 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 core/libs/tests/ImageMsaaLoad.cpp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 90d971f7a..d35258026 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -780,6 +780,14 @@ if(BUILD_TESTING) set_tests_properties(image_sample_gather PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(image_msaa_load_tests ${LIBS_INCLUDE_DIR}/tests/ImageMsaaLoad.cpp) + target_link_libraries(image_msaa_load_tests PRIVATE shader_recompiler_agc_driver libc) + configure_windows_unwind(image_msaa_load_tests) + add_test(NAME image_msaa_load COMMAND image_msaa_load_tests) + if(WIN32) + set_tests_properties(image_msaa_load PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_indexed_draw_tests ${LIBS_INCLUDE_DIR}/tests/GuestIndexedDraw.cpp) target_include_directories(guest_indexed_draw_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_indexed_draw_tests PRIVATE libSceAgc libSceAgcDriver libc) diff --git a/core/libs/tests/ImageMsaaLoad.cpp b/core/libs/tests/ImageMsaaLoad.cpp new file mode 100644 index 000000000..4e3a57353 --- /dev/null +++ b/core/libs/tests/ImageMsaaLoad.cpp @@ -0,0 +1,69 @@ +#include "Translation/TranslationContext.hpp" +#include "RdnaDecoder/RdnaImageOpDecoder.hpp" +#include +#include +#include + +using namespace ShaderRecompiler; +static void Require(bool value) { if (!value) throw std::runtime_error("image msaa load regression"); } +static std::uint32_t Word0(std::uint32_t dmask, std::uint32_t dimension) { + return (0x3cu << 26u) | (dmask << 8u) | (dimension << 3u) | 1u; +} +static RdnaInstruction Decode(std::uint32_t word0, std::uint32_t word1) { + const std::array code{word0, word1}; + return DecodeRdnaMimg(0u, code, 0u); +} +static bool Rejects(std::uint32_t word0, std::uint32_t word1) { + try { + (void)Decode(word0, word1); + } catch (const std::runtime_error&) { + return true; + } + return false; +} +static void Check(std::uint32_t dimension, bool a16, std::uint32_t components, std::uint32_t fragmentDword, std::uint32_t fragmentShift) { + const RdnaInstruction instruction = Decode(Word0(0x4u, dimension), (a16 ? 0x40000000u : 0u) | (8u << 8u)); + Require(instruction.op == RdnaOpcode::ImageMsaaLoad); + Require(instruction.family == RdnaInstructionFamily::MIMG); + Require(IsImageOpcode(instruction.op)); + Require(instruction.imageOpcodeId == 0x80u); + Require(instruction.imageAddressComponents == components); + Require(instruction.dataComponents == 4u); + Require(instruction.dataDwordCount == 4u); + IrProgram program; + auto& block = program.CreateBlock(); + program.SetEntryBlock(block); + TranslationContext context(program, block, 256); + context.TranslateInstruction(instruction); + std::vector reads; + for (auto* value : block.Instructions()) { + if (value->Opcode() == IrOpcode::ImageRead) { + reads.push_back(value); + } + } + Require(reads.size() == 4u); + for (std::uint32_t fragment = 0u; fragment < 4u; ++fragment) { + IrValue* address = reads[fragment]->Argument(1)->Resolve(); + Require(address->Opcode() == IrOpcode::MakeImageAddress); + IrValue* word = address->Argument(fragmentDword)->Resolve(); + if (fragment == 0u) { + Require(word->Opcode() != IrOpcode::IAdd32); + continue; + } + Require(word->Opcode() == IrOpcode::IAdd32); + IrValue* offset = word->Argument(1)->Resolve(); + Require(offset->HasImmediate() && offset->ImmediateU32() == fragment << fragmentShift); + const auto& memory = program.Resources().memoryInfo[reads[fragment]->Flags().index]; + Require(memory.dmask == 0x4u && memory.dataDwords == 1u); + } +} +int main() { + Check(6u, false, 3u, 2u, 0u); + Check(7u, false, 4u, 3u, 0u); + Check(6u, true, 3u, 1u, 0u); + Check(7u, true, 4u, 1u, 16u); + Require(Rejects(Word0(0x1u, 1u), 0u)); + Require(Rejects(Word0(0x3u, 6u), 0u)); + Require(Rejects(Word0(0x1u, 6u), 0x80000000u)); + Require(Rejects(Word0(0x1u, 6u) | 0x00010000u, 0u)); +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 5986b1d72..ef4daa6c7 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -955,6 +955,7 @@ enum class RdnaOpcode : std::uint16_t { ImageStoreMip, ImageStorePck, ImageStoreMipPck, + ImageMsaaLoad, ImageAtomicSwap, ImageAtomicAdd, ImageAtomicUmin, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp index 53e957a5e..b4d31ad94 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp @@ -132,6 +132,7 @@ constexpr ImageOpcodeInfo imageOpcodes[] = { {0x0au, RdnaOpcode::ImageStorePck, nullptr, 0, false, false, false}, {0x0bu, RdnaOpcode::ImageStoreMipPck, nullptr, 0, false, false, false}, {0x0eu, RdnaOpcode::ImageGetResinfo, nullptr, 0, false, false, false}, + {0x80u, RdnaOpcode::ImageMsaaLoad, nullptr, 0, false, false, false}, {0x60u, RdnaOpcode::ImageGetLod, nullptr, 0, false, false, false}, {0xe6u, RdnaOpcode::ImageBvhIntersectRay, "image_bvh_intersect_ray", 0, false, false, false}, {0xe7u, RdnaOpcode::ImageBvh64IntersectRay, "image_bvh64_intersect_ray", 0, false, false, false}, @@ -336,6 +337,10 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span= 1u && opcode <= 5u) || opcode == 9u || opcode == 0x0bu)) { throw std::runtime_error("unsupported multisampled MIMG operation"); } + const bool msaaLoad = info.opcode == RdnaOpcode::ImageMsaaLoad; + if (msaaLoad && !multisampled) { + throw std::runtime_error("image_msaa_load requires a multisampled dimension"); + } auto flags = info.flags | (a16 ? RdnaImageSampleFlagA16 : 0u); if ((info.flags & RdnaImageSampleFlagDerivative) != 0u) { flags |= gradientCount(dimension) << RdnaImageSampleGradientCountShift; @@ -343,7 +348,7 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span> 8u) & 15u; const bool compareSwap = info.opcode == RdnaOpcode::ImageAtomicCmpswap || info.opcode == RdnaOpcode::ImageAtomicFcmpswap; - if (dmask == 0u || (compareSwap ? dmask != 3u : (info.gather || info.atomic) && !std::has_single_bit(dmask))) { + if (dmask == 0u || (compareSwap ? dmask != 3u : (info.gather || info.atomic || msaaLoad) && !std::has_single_bit(dmask))) { throw std::runtime_error("invalid MIMG data mask"); } if (d16 && !(info.sample || info.gather || opcode == 0u || opcode == 1u || opcode == 8u || opcode == 9u)) { @@ -371,7 +376,7 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span 256u - vaddr) { throw std::runtime_error("MIMG address register range overflow"); } - const auto dataComponents = info.gather ? 4u : static_cast(std::popcount(dmask)); + const auto dataComponents = info.gather || msaaLoad ? 4u : static_cast(std::popcount(dmask)); const auto dataDwords = d16 ? (dataComponents + 1u) / 2u : dataComponents; const auto statusDwords = (word0 & 0x00010000u) != 0u ? 1u : 0u; if (dataDwords + statusDwords > 256u - vdata) { diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index e68c49ad1..0171c61c9 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -822,6 +822,7 @@ bool IsImageOpcode(RdnaOpcode opcode) { case RdnaOpcode::ImageStoreMip: case RdnaOpcode::ImageStorePck: case RdnaOpcode::ImageStoreMipPck: + case RdnaOpcode::ImageMsaaLoad: case RdnaOpcode::ImageAtomicSwap: case RdnaOpcode::ImageAtomicAdd: case RdnaOpcode::ImageAtomicUmin: diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 50e26ccf8..4f4db7b11 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -79,7 +79,7 @@ private: IrValue* getScalarAddressResource(std::uint32_t base); IrValue* getImageResource(const MemoryInfo& memory); IrValue* getSamplerResource(const MemoryInfo& memory); - IrValue* makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base); + IrValue* makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base, std::uint32_t fragmentOffset = 0u); IrValue* constructU32x4(const RdnaOperand& base, std::uint32_t count); void writeImageComponents(const RdnaOperand& dst, IrValue* value, const MemoryInfo& memory, std::uint32_t componentLimit); BufferAddress readBufferAddress(const RdnaInstruction& inst); @@ -100,6 +100,7 @@ private: bool imageGetResinfo(const RdnaInstruction& inst); bool imageGetLod(const RdnaInstruction& inst); bool imageLoad(const RdnaInstruction& inst); + bool imageMsaaLoad(const RdnaInstruction& inst); bool imageStore(const RdnaInstruction& inst); bool imageSample(const RdnaInstruction& inst); bool imageGather(const RdnaInstruction& inst); diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp index 2f0bf639b..0207f3e53 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp @@ -120,6 +120,20 @@ bool TranslationContext::imageLoad(const RdnaInstruction& inst) { return true; } +bool TranslationContext::imageMsaaLoad(const RdnaInstruction& inst) { + MemoryInfo memory = imageMemoryInfoFromInstruction(inst); + memory.dataDwords = 1u; + memory.componentCount = 1u; + IrValue* resource = getImageResource(memory); + IrValue& exec = ir.GetExec(); + for (std::uint32_t fragment = 0u; fragment < 4u; ++fragment) { + IrValue* address = makeImageAddress(inst, inst.source0, fragment); + IrValue& result = ir.Emit(IrOpcode::ImageRead, IrOpcodeType(IrOpcode::ImageRead), {resource, address, &exec}, addMemoryInfo(memory, inst.programCounter)); + writeImageComponents(offsetOperand(inst.destination, fragment), &result, memory, 4u); + } + return true; +} + bool TranslationContext::imageStore(const RdnaInstruction& inst) { const MemoryInfo memory = imageMemoryInfoFromInstruction(inst); IrValue* resource = getImageResource(memory); diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index 24993bd96..5e0a602da 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -600,6 +600,8 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { case RdnaOpcode::ImageLoadMipPck: case RdnaOpcode::ImageLoadMipPckSgn: return imageLoad(inst); + case RdnaOpcode::ImageMsaaLoad: + return imageMsaaLoad(inst); case RdnaOpcode::ImageStore: case RdnaOpcode::ImageStoreMip: case RdnaOpcode::ImageStorePck: diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryResourceHelpers.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryResourceHelpers.cpp index 369b78f6c..bff55b085 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryResourceHelpers.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryResourceHelpers.cpp @@ -93,7 +93,7 @@ IrValue* TranslationContext::getSamplerResource(const MemoryInfo& memory) { return &ir.Emit(IrOpcode::GetSamplerResource, IrOpcodeType(IrOpcode::GetSamplerResource), {&dword0.Value(), &dword1.Value(), &dword2.Value(), &dword3.Value()}); } -IrValue* TranslationContext::makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base) { +IrValue* TranslationContext::makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base, std::uint32_t fragmentOffset) { std::array components{}; IrValue& zero = ir.Constant(0u); components.fill(&zero); @@ -110,6 +110,11 @@ IrValue* TranslationContext::makeImageAddress(const RdnaInstruction& inst, const components[index] = &readRawU32(offsetOperand(plainBase, index)).Value(); } } + if (fragmentOffset != 0u) { + const RdnaImageAddressComponent fragment = GetRdnaImageAddressComponentLayout(inst.imageSampleFlags, inst.imageAddressComponents - 1u); + IrValue*& word = components[fragment.bitOffset / 32u]; + word = &ir.IAdd(*word, ir.Constant(fragmentOffset << (fragment.bitOffset % 32u))); + } if ((inst.imageSampleFlags & RdnaImageSampleFlagCd) != 0u) { const auto mask = ballotMask(IrU1(ir.GetExec())); IrValue& lane = ir.Emit(IrOpcode::LaneId, IrType::U32, {}); From 8cfe87bd164719cb4eaf1036690644ead44ff385 Mon Sep 17 00:00:00 2001 From: Chad Date: Mon, 5 Oct 2026 20:21:50 -0500 Subject: [PATCH 2/2] build(libSceAgcDriver): register the image_msaa_load test apart from other pending tests Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index d35258026..89d4f60b8 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -780,14 +780,6 @@ if(BUILD_TESTING) set_tests_properties(image_sample_gather PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() - add_test_executable(image_msaa_load_tests ${LIBS_INCLUDE_DIR}/tests/ImageMsaaLoad.cpp) - target_link_libraries(image_msaa_load_tests PRIVATE shader_recompiler_agc_driver libc) - configure_windows_unwind(image_msaa_load_tests) - add_test(NAME image_msaa_load COMMAND image_msaa_load_tests) - if(WIN32) - set_tests_properties(image_msaa_load PROPERTIES ENVIRONMENT_MODIFICATION - "PATH=path_list_prepend:$") - endif() add_test_executable(guest_indexed_draw_tests ${LIBS_INCLUDE_DIR}/tests/GuestIndexedDraw.cpp) target_include_directories(guest_indexed_draw_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_indexed_draw_tests PRIVATE libSceAgc libSceAgcDriver libc) @@ -825,3 +817,14 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() endif() + +if(BUILD_TESTING) + add_test_executable(image_msaa_load_tests ${LIBS_INCLUDE_DIR}/tests/ImageMsaaLoad.cpp) + target_link_libraries(image_msaa_load_tests PRIVATE shader_recompiler_agc_driver libc) + configure_windows_unwind(image_msaa_load_tests) + add_test(NAME image_msaa_load COMMAND image_msaa_load_tests) + if(WIN32) + set_tests_properties(image_msaa_load PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif()