From 1566e1b397b50451fa7896bab6cd0e45676e8f58 Mon Sep 17 00:00:00 2001 From: Chad Date: Sun, 4 Oct 2026 18:35:47 -0500 Subject: [PATCH 1/2] feat(agc): implement packet and range predication and the predication sizes sceAgcSetPacketPredication sets or clears the predicate bit of a type-3 packet header, and sceAgcSetRangePredication does so for every type-3 packet between two addresses, skipping type-2 fillers. The z-pass, bool and disable predication size queries return the size of the SET_PREDICATION packet sceAgcDcbSetPredication writes. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgc/CMakeLists.txt | 20 ++++ .../libSceAgc/DcbState/src/Predication.cpp | 9 +- .../libSceAgc/Predication/src/Predication.cpp | 32 +++++-- core/libs/prx/libSceAgc/tests/Predication.cpp | 96 +++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + 5 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 core/libs/prx/libSceAgc/tests/Predication.cpp diff --git a/core/libs/prx/libSceAgc/CMakeLists.txt b/core/libs/prx/libSceAgc/CMakeLists.txt index 1ac5e1b6b..02cf9bf23 100644 --- a/core/libs/prx/libSceAgc/CMakeLists.txt +++ b/core/libs/prx/libSceAgc/CMakeLists.txt @@ -105,6 +105,26 @@ if(BUILD_TESTING) endif() endif() +add_test_executable(agc_predication_tests EXCLUDE_FROM_ALL + tests/Predication.cpp + Command/src/Packet.cpp + Command/src/Control.cpp + DcbState/src/Predication.cpp + DcbDraw/src/IndexBuffer.cpp + Predication/src/Predication.cpp +) +target_include_directories(agc_predication_tests PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(agc_predication_tests PRIVATE libc) +configure_windows_unwind(agc_predication_tests) +if(BUILD_TESTING) + set_target_properties(agc_predication_tests PROPERTIES EXCLUDE_FROM_ALL FALSE) + add_test(NAME agc_predication_tests COMMAND agc_predication_tests) + if(WIN32) + set_tests_properties(agc_predication_tests PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() + add_test_executable(agc_shader_fusion_tests EXCLUDE_FROM_ALL tests/ShaderFusion.cpp) target_include_directories(agc_shader_fusion_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(agc_shader_fusion_tests PRIVATE libSceAgc libc) diff --git a/core/libs/prx/libSceAgc/DcbState/src/Predication.cpp b/core/libs/prx/libSceAgc/DcbState/src/Predication.cpp index 13aaa4386..f524eb3b1 100644 --- a/core/libs/prx/libSceAgc/DcbState/src/Predication.cpp +++ b/core/libs/prx/libSceAgc/DcbState/src/Predication.cpp @@ -15,18 +15,15 @@ uint32_t* APS5_VABI sceAgcDcbSetPredication(CommandBuffer* buf, uint8_t conditio } std::uint32_t APS5_VABI sceAgcDcbSetZPassPredicationEnableGetSize() { - NotImplemented_nid_no_patch(__func__); - return 0; + return 16; } std::uint32_t APS5_VABI sceAgcDcbSetPredicationDisableGetSize() { - NotImplemented_nid_no_patch(__func__); - return 0; + return 16; } std::uint32_t APS5_VABI sceAgcDcbSetBoolPredicationEnableGetSize() { - NotImplemented_nid_no_patch(__func__); - return 0; + return 16; } } diff --git a/core/libs/prx/libSceAgc/Predication/src/Predication.cpp b/core/libs/prx/libSceAgc/Predication/src/Predication.cpp index 2ae555e4d..586b40e41 100644 --- a/core/libs/prx/libSceAgc/Predication/src/Predication.cpp +++ b/core/libs/prx/libSceAgc/Predication/src/Predication.cpp @@ -1,5 +1,7 @@ #include "prx/libSceAgc/Predication/include/Predication.hpp" +#include "prx/libSceAgc/Command/include/Packet.hpp" + #include #include #include "SceTypes.hpp" @@ -8,18 +10,30 @@ extern "C" { int APS5_VABI sceAgcSetPacketPredication(uint32_t* packet, uint32_t predication) { - (void)packet; - (void)predication; - NotImplemented_nid_no_patch(__func__); - return 0; + Agc::Command::Require(packet != nullptr, __func__, "null packet"); + Agc::Command::CheckBits(predication, 1, __func__); + Agc::Command::Require((packet[0] >> 30u) == 3u, __func__, "packet is not a type-3 packet"); + packet[0] = (packet[0] & ~1u) | predication; + return 0; } int APS5_VABI sceAgcSetRangePredication(uint32_t* start, const volatile uint32_t* end, uint32_t predication) { - (void)start; - (void)end; - (void)predication; - NotImplemented_nid_no_patch(__func__); - return 0; + Agc::Command::Require(start != nullptr && end != nullptr && start <= end, __func__, "invalid packet range"); + Agc::Command::CheckBits(predication, 1, __func__); + auto* packet = start; + while (packet < end) { + const auto type = packet[0] >> 30u; + if (type == 2u) { + ++packet; + continue; + } + Agc::Command::Require(type == 3u, __func__, "range holds a packet that is neither type 2 nor type 3"); + const auto dwords = ((packet[0] >> 16u) & 0x3fffu) + 2u; + Agc::Command::Require(static_cast(end - packet) >= dwords, __func__, "range ends inside a packet"); + packet[0] = (packet[0] & ~1u) | predication; + packet += dwords; + } + return 0; } } diff --git a/core/libs/prx/libSceAgc/tests/Predication.cpp b/core/libs/prx/libSceAgc/tests/Predication.cpp new file mode 100644 index 000000000..b01d77ad2 --- /dev/null +++ b/core/libs/prx/libSceAgc/tests/Predication.cpp @@ -0,0 +1,96 @@ +#include "prx/libSceAgc/Command/include/Packet.hpp" + +#include +#include +#include +#include + +extern "C" { +std::uint32_t* APS5_VABI sceAgcDcbSetPredication(CommandBuffer*, std::uint8_t, std::uint8_t, std::uint8_t, const volatile void*, std::uint32_t); +std::uint32_t APS5_VABI sceAgcDcbSetZPassPredicationEnableGetSize(); +std::uint32_t APS5_VABI sceAgcDcbSetPredicationDisableGetSize(); +std::uint32_t APS5_VABI sceAgcDcbSetBoolPredicationEnableGetSize(); +std::uint32_t* APS5_VABI sceAgcDcbSetIndexCount(CommandBuffer*, std::uint32_t); +std::uint32_t* APS5_VABI sceAgcDcbSetIndexBuffer(CommandBuffer*, std::uint64_t); +int APS5_VABI sceAgcSetPacketPredication(std::uint32_t*, std::uint32_t); +int APS5_VABI sceAgcSetRangePredication(std::uint32_t*, const volatile std::uint32_t*, std::uint32_t); +} + +namespace { + +void check(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +template +void expectFailure(TAction action) { + try { + action(); + } catch (const std::runtime_error&) { + return; + } + throw std::runtime_error("expected invalid input to fail"); +} + +struct Storage { + std::array words{}; + CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0}; +}; + +void testSizes() { + Storage storage; + alignas(16) static std::uint64_t query = 0; + auto* packet = sceAgcDcbSetPredication(&storage.buffer, 1, 1, 0, &query, 0); + const auto bytes = static_cast((storage.buffer.cursor_up - packet) * sizeof(std::uint32_t)); + check(bytes == 16, "SET_PREDICATION is not four dwords"); + check(sceAgcDcbSetZPassPredicationEnableGetSize() == bytes, "z-pass predication size"); + check(sceAgcDcbSetPredicationDisableGetSize() == bytes, "predication disable size"); + check(sceAgcDcbSetBoolPredicationEnableGetSize() == bytes, "bool predication size"); +} + +void testPacket() { + Storage storage; + auto* packet = sceAgcDcbSetIndexCount(&storage.buffer, 7); + const auto header = packet[0]; + check(sceAgcSetPacketPredication(packet, 1) == 0 && packet[0] == (header | 1u) && packet[1] == 7, "predication bit not set"); + check(sceAgcSetPacketPredication(packet, 0) == 0 && packet[0] == header, "predication bit not cleared"); + expectFailure([&] { sceAgcSetPacketPredication(packet, 2); }); + expectFailure([&] { sceAgcSetPacketPredication(nullptr, 1); }); + std::uint32_t filler = 0x80000000u; + expectFailure([&] { sceAgcSetPacketPredication(&filler, 1); }); + check(packet[0] == header && filler == 0x80000000u, "rejected calls modified a packet"); +} + +void testRange() { + Storage storage; + auto* first = sceAgcDcbSetIndexCount(&storage.buffer, 3); + *storage.buffer.cursor_up++ = 0x80000000u; + auto* second = sceAgcDcbSetIndexBuffer(&storage.buffer, 0x1000); + auto* third = sceAgcDcbSetIndexCount(&storage.buffer, 5); + const std::array headers{first[0], second[0], third[0]}; + check(sceAgcSetRangePredication(first, storage.buffer.cursor_up, 1) == 0, "range predication failed"); + check(first[0] == (headers[0] | 1u) && second[0] == (headers[1] | 1u) && third[0] == (headers[2] | 1u), "range did not predicate every packet"); + check(first[2] == 0x80000000u && second[1] == 0x1000u && third[1] == 5u, "range predication changed a payload or filler"); + check(sceAgcSetRangePredication(second, third, 0) == 0 && second[0] == headers[1] && third[0] == (headers[2] | 1u), "range end is not exclusive"); + check(sceAgcSetRangePredication(first, first, 0) == 0 && first[0] == (headers[0] | 1u), "empty range changed a packet"); + expectFailure([&] { sceAgcSetRangePredication(first, first + 1, 1); }); + expectFailure([&] { sceAgcSetRangePredication(second, first, 1); }); + expectFailure([&] { sceAgcSetRangePredication(first, third, 2); }); + std::array invalid{0x40000000u, 0}; + expectFailure([&] { sceAgcSetRangePredication(invalid.data(), invalid.data() + invalid.size(), 1); }); +} + +} + +int main() { + try { + testSizes(); + testPacket(); + testRange(); + std::puts("agc predication tests passed"); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 9fe00e510..62f649a0e 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -52,6 +52,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceAvPlayerInit / sceAvPlayerInitEx](../../core/libs/prx/libSceAvPlayer/src/Player.cpp) (libSceAvPlayer) - behaviour without a memory replacement unknown; frame and sample buffers then come from the guest heap - [SceAvPlayerVideoEx](../../core/libs/SceTypes.hpp) (libSceAvPlayer) - frame rate field and encoding unknown; it is left zero in frame and stream info - [AJM Opus decoder](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native, codec 24) - the initialize parameter layout (`u32 channels, u32 sample rate, u32`, seen as `2, 48000, 0`) and the little-endian `u16` byte count before each packet come from one title (The Smurfs Dreams); only 48000 Hz and a zero third word are accepted +- [sceAgcDcbSetZPassPredicationEnableGetSize, sceAgcDcbSetPredicationDisableGetSize, sceAgcDcbSetBoolPredicationEnableGetSize](../../core/libs/prx/libSceAgc/DcbState/src/Predication.cpp) (libSceAgc) - the builders are inline in the SDK and not exported; the size is assumed to be that of the SET_PREDICATION packet `sceAgcDcbSetPredication` writes (16 bytes) ### Functional From d4f7ddaa1af420333acdebbcab4b1c4515453402 Mon Sep 17 00:00:00 2001 From: Chad Date: Mon, 5 Oct 2026 14:49:17 -0500 Subject: [PATCH 2/2] fix(agc): count the one-dword NOP pad as one dword in range predication 0xffff1000 is a type-3 NOP whose count field is 0x3fff, which the command processor reads as a single dword. sceAgcSetRangePredication read it as a 16385-dword packet and skipped the packets after it, or threw in a short range. It is now stepped over like a type-2 filler. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgc/Predication/src/Predication.cpp | 2 +- core/libs/prx/libSceAgc/tests/Predication.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceAgc/Predication/src/Predication.cpp b/core/libs/prx/libSceAgc/Predication/src/Predication.cpp index 586b40e41..dcaad59b9 100644 --- a/core/libs/prx/libSceAgc/Predication/src/Predication.cpp +++ b/core/libs/prx/libSceAgc/Predication/src/Predication.cpp @@ -23,7 +23,7 @@ int APS5_VABI sceAgcSetRangePredication(uint32_t* start, const volatile uint32_t auto* packet = start; while (packet < end) { const auto type = packet[0] >> 30u; - if (type == 2u) { + if (type == 2u || (packet[0] & 0x3fffff00u) == 0x3fff1000u) { ++packet; continue; } diff --git a/core/libs/prx/libSceAgc/tests/Predication.cpp b/core/libs/prx/libSceAgc/tests/Predication.cpp index b01d77ad2..32a6619ed 100644 --- a/core/libs/prx/libSceAgc/tests/Predication.cpp +++ b/core/libs/prx/libSceAgc/tests/Predication.cpp @@ -65,12 +65,13 @@ void testRange() { Storage storage; auto* first = sceAgcDcbSetIndexCount(&storage.buffer, 3); *storage.buffer.cursor_up++ = 0x80000000u; + *storage.buffer.cursor_up++ = 0xffff1000u; auto* second = sceAgcDcbSetIndexBuffer(&storage.buffer, 0x1000); auto* third = sceAgcDcbSetIndexCount(&storage.buffer, 5); const std::array headers{first[0], second[0], third[0]}; check(sceAgcSetRangePredication(first, storage.buffer.cursor_up, 1) == 0, "range predication failed"); check(first[0] == (headers[0] | 1u) && second[0] == (headers[1] | 1u) && third[0] == (headers[2] | 1u), "range did not predicate every packet"); - check(first[2] == 0x80000000u && second[1] == 0x1000u && third[1] == 5u, "range predication changed a payload or filler"); + check(first[2] == 0x80000000u && first[3] == 0xffff1000u && second[1] == 0x1000u && third[1] == 5u, "range predication changed a payload, filler or pad"); check(sceAgcSetRangePredication(second, third, 0) == 0 && second[0] == headers[1] && third[0] == (headers[2] | 1u), "range end is not exclusive"); check(sceAgcSetRangePredication(first, first, 0) == 0 && first[0] == (headers[0] | 1u), "empty range changed a packet"); expectFailure([&] { sceAgcSetRangePredication(first, first + 1, 1); });