mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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) <noreply@anthropic.com>
This commit is contained in:
@@ -758,6 +758,14 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(image_sample_gather PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
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:$<TARGET_FILE_DIR:libc>")
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "IntermediateRepresentation/IrBuilder.hpp"
|
||||
#include "Optimization/ConstantFolder.hpp"
|
||||
#include "Optimization/DeadCodeEliminator.hpp"
|
||||
#include "Optimization/ShaderInfoCollector.hpp"
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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<std::uint32_t>(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<void>(builder.Emit(IrOpcode::ReferenceU32, IrType::Void, {&user}));
|
||||
static_cast<void>(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<StageInputKind>(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<void>(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);
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -202,52 +201,42 @@ bool lowerPackedAncillary(IrProgram& program, IrBuilder& builder, IrValue& ancil
|
||||
}
|
||||
std::vector<IrUse> uses;
|
||||
collectUses(ancillary, uses);
|
||||
if (uses.empty()) {
|
||||
return false;
|
||||
}
|
||||
IrBlock& block = *ancillary.Parent();
|
||||
const auto emit = [&](IrOpcode opcode, std::initializer_list<IrValue*> arguments) -> IrValue& {
|
||||
IrValue& created = program.CreateValue(opcode, IrType::U32);
|
||||
for (IrValue* argument : arguments) {
|
||||
created.AddArgument(argument);
|
||||
}
|
||||
block.InsertInstructionBefore(&ancillary, &created);
|
||||
return created;
|
||||
};
|
||||
std::array<IrValue*, 2> 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<std::uint32_t>(builtin)), &builder.Constant(0u)});
|
||||
IrValue& created = program.CreateValue(IrOpcode::GetBuiltin, IrType::U32);
|
||||
created.AddArgument(&builder.Constant(static_cast<std::uint32_t>(builtin)));
|
||||
created.AddArgument(&builder.Constant(0u));
|
||||
ancillary.Parent()->InsertInstructionBefore(&ancillary, &created);
|
||||
fields[index] = &created;
|
||||
}
|
||||
return *fields[index];
|
||||
};
|
||||
constexpr std::array<std::pair<std::uint32_t, std::uint32_t>, 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<std::size_t>(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<std::size_t>(range - ranges.begin())));
|
||||
user.ReplaceArgument(1, &builder.Constant(offset.ImmediateU32() - range->first));
|
||||
lowered = true;
|
||||
}
|
||||
return true;
|
||||
return lowered;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user