From 3d014251a54df7d006da1ab746eb6eaa4b5342d4 Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Sun, 4 Oct 2026 10:27:07 +0200 Subject: [PATCH] fix(shader): lower only exact sample id and layer field extracts of the packed pixel ancillary Any other live use of the packed register keeps throwing, as on main, instead of reading SampleId << 8 | Layer << 16 with the other bits zero. The field range check no longer underflows for an extract that starts past a field's end. Adds the pixel_ancillary recompiler test. Co-Authored-By: Claude Opus 5.5 (1M context) --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 8 +++ core/libs/tests/PixelAncillary.cpp | 64 +++++++++++++++++++ .../Optimization/src/ConstantFolder.cpp | 57 +++++++---------- 3 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 core/libs/tests/PixelAncillary.cpp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index b373847f0..4388e8574 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -758,6 +758,14 @@ if(BUILD_TESTING) set_tests_properties(image_sample_gather PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(pixel_ancillary_tests ${LIBS_INCLUDE_DIR}/tests/PixelAncillary.cpp) + target_link_libraries(pixel_ancillary_tests PRIVATE shader_recompiler_agc_driver libc) + configure_windows_unwind(pixel_ancillary_tests) + add_test(NAME pixel_ancillary COMMAND pixel_ancillary_tests) + if(WIN32) + set_tests_properties(pixel_ancillary 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/PixelAncillary.cpp b/core/libs/tests/PixelAncillary.cpp new file mode 100644 index 000000000..2d6f10dad --- /dev/null +++ b/core/libs/tests/PixelAncillary.cpp @@ -0,0 +1,64 @@ +#include "IntermediateRepresentation/IrBuilder.hpp" +#include "Optimization/ConstantFolder.hpp" +#include "Optimization/DeadCodeEliminator.hpp" +#include "Optimization/ShaderInfoCollector.hpp" +#include +#include +#include + +using namespace ShaderRecompiler; +static void Require(bool value) { if (!value) throw std::runtime_error("packed pixel ancillary regression"); } +static IrValue& Build(IrProgram& program, IrOpcode opcode, std::uint32_t offset, std::uint32_t count) { + program.Resources().stage = IrShaderStage::Pixel; + program.Resources().resourceTrackingComplete = true; + auto& block = program.CreateBlock(); + program.SetEntryBlock(block); + program.BlockOrder().push_back(&block); + IrBuilder builder(program); + builder.SetInsertionPoint(block); + auto& ancillary = builder.Emit(IrOpcode::GetBuiltin, IrType::U32, {&builder.Constant(static_cast(StageInputKind::PackedAncillary)), &builder.Constant(0u)}); + auto& user = opcode == IrOpcode::BitwiseOr32 ? builder.Emit(opcode, IrType::U32, {&ancillary, &builder.Constant(offset)}) + : builder.Emit(opcode, IrType::U32, {&ancillary, &builder.Constant(offset), &builder.Constant(count)}); + static_cast(builder.Emit(IrOpcode::ReferenceU32, IrType::Void, {&user})); + static_cast(builder.Emit(IrOpcode::Return, IrType::Void, {})); + return user; +} +static void Lower(IrProgram& program) { + ConstantFolder().Fold(program); + DeadCodeEliminator().RemoveIdentities(program); + DeadCodeEliminator().Eliminate(program); + const ShaderPixelInputInfo pixel {}; + ShaderInfoCollector().Collect(program, ShaderStageInputInfo {nullptr, &pixel, nullptr}); +} +static void Extract(IrOpcode opcode, std::uint32_t offset, std::uint32_t count, StageInputKind kind, std::uint32_t fieldOffset) { + IrProgram program; + auto& user = Build(program, opcode, offset, count); + Lower(program); + const IrValue* field = user.Argument(0)->Resolve(); + Require(field->Opcode() == IrOpcode::GetBuiltin); + Require(static_cast(field->Argument(0)->Resolve()->ImmediateU32()) == kind); + Require(user.Argument(1)->Resolve()->ImmediateU32() == fieldOffset); + Require(user.Argument(2)->Resolve()->ImmediateU32() == count); +} +static void Refused(IrOpcode opcode, std::uint32_t offset, std::uint32_t count) { + IrProgram program; + static_cast(Build(program, opcode, offset, count)); + try { + Lower(program); + } catch (const std::runtime_error& error) { + Require(std::string(error.what()).find("unsupported live use") != std::string::npos); + return; + } + Require(false); +} +int main() { + Extract(IrOpcode::BitFieldUExtract, 8u, 4u, StageInputKind::SampleId, 0u); + Extract(IrOpcode::BitFieldUExtract, 9u, 2u, StageInputKind::SampleId, 1u); + Extract(IrOpcode::BitFieldUExtract, 16u, 13u, StageInputKind::Layer, 0u); + Extract(IrOpcode::BitFieldSExtract, 20u, 9u, StageInputKind::Layer, 4u); + Refused(IrOpcode::BitwiseOr32, 1u, 0u); + Refused(IrOpcode::BitFieldUExtract, 2u, 4u); + Refused(IrOpcode::BitFieldUExtract, 10u, 4u); + Refused(IrOpcode::BitFieldUExtract, 13u, 2u); + Refused(IrOpcode::BitFieldUExtract, 16u, 14u); +} diff --git a/core/shader/recompiler/Optimization/src/ConstantFolder.cpp b/core/shader/recompiler/Optimization/src/ConstantFolder.cpp index ac7bcfdcd..a05a6c7a8 100644 --- a/core/shader/recompiler/Optimization/src/ConstantFolder.cpp +++ b/core/shader/recompiler/Optimization/src/ConstantFolder.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -202,52 +201,42 @@ bool lowerPackedAncillary(IrProgram& program, IrBuilder& builder, IrValue& ancil } std::vector uses; collectUses(ancillary, uses); - if (uses.empty()) { - return false; - } - IrBlock& block = *ancillary.Parent(); - const auto emit = [&](IrOpcode opcode, std::initializer_list arguments) -> IrValue& { - IrValue& created = program.CreateValue(opcode, IrType::U32); - for (IrValue* argument : arguments) { - created.AddArgument(argument); - } - block.InsertInstructionBefore(&ancillary, &created); - return created; - }; std::array fields {}; const auto field = [&](std::size_t index) -> IrValue& { if (fields[index] == nullptr) { const auto builtin = index == 0u ? StageInputKind::SampleId : StageInputKind::Layer; - fields[index] = &emit(IrOpcode::GetBuiltin, {&builder.Constant(static_cast(builtin)), &builder.Constant(0u)}); + IrValue& created = program.CreateValue(IrOpcode::GetBuiltin, IrType::U32); + created.AddArgument(&builder.Constant(static_cast(builtin))); + created.AddArgument(&builder.Constant(0u)); + ancillary.Parent()->InsertInstructionBefore(&ancillary, &created); + fields[index] = &created; } return *fields[index]; }; constexpr std::array, 2> ranges {{{8u, 4u}, {16u, 13u}}}; - IrValue* packed = nullptr; + bool lowered = false; for (const IrUse& use : uses) { IrValue& user = *use.user; - if ((user.Opcode() == IrOpcode::BitFieldUExtract || user.Opcode() == IrOpcode::BitFieldSExtract) && use.operand == 0u) { - auto& offset = resolveArg(user, 1); - auto& count = resolveArg(user, 2); - if (isImmediate(offset, IrType::U32) && isImmediate(count, IrType::U32) && count.ImmediateU32() != 0u) { - const auto range = std::ranges::find_if(ranges, [&](const auto& candidate) { - return offset.ImmediateU32() >= candidate.first && count.ImmediateU32() <= candidate.first + candidate.second - offset.ImmediateU32(); - }); - if (range != ranges.end()) { - user.ReplaceArgument(0, &field(static_cast(range - ranges.begin()))); - user.ReplaceArgument(1, &builder.Constant(offset.ImmediateU32() - range->first)); - continue; - } - } + if ((user.Opcode() != IrOpcode::BitFieldUExtract && user.Opcode() != IrOpcode::BitFieldSExtract) || use.operand != 0u) { + continue; } - if (packed == nullptr) { - IrValue& sample = emit(IrOpcode::ShiftLeftLogical32, {&field(0u), &builder.Constant(ranges[0].first)}); - IrValue& layer = emit(IrOpcode::ShiftLeftLogical32, {&field(1u), &builder.Constant(ranges[1].first)}); - packed = &emit(IrOpcode::BitwiseOr32, {&sample, &layer}); + auto& offset = resolveArg(user, 1); + auto& count = resolveArg(user, 2); + if (!isImmediate(offset, IrType::U32) || !isImmediate(count, IrType::U32) || count.ImmediateU32() == 0u) { + continue; } - user.ReplaceArgument(use.operand, packed); + const auto range = std::ranges::find_if(ranges, [&](const auto& candidate) { + return offset.ImmediateU32() >= candidate.first && count.ImmediateU32() <= candidate.second && + offset.ImmediateU32() - candidate.first <= candidate.second - count.ImmediateU32(); + }); + if (range == ranges.end()) { + continue; + } + user.ReplaceArgument(0, &field(static_cast(range - ranges.begin()))); + user.ReplaceArgument(1, &builder.Constant(offset.ImmediateU32() - range->first)); + lowered = true; } - return true; + return lowered; } } // namespace