diff --git a/core/libs/prx/libSceAgc/CMakeLists.txt b/core/libs/prx/libSceAgc/CMakeLists.txt index c092d74ec..544eb0e9f 100644 --- a/core/libs/prx/libSceAgc/CMakeLists.txt +++ b/core/libs/prx/libSceAgc/CMakeLists.txt @@ -147,6 +147,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..dcaad59b9 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[0] & 0x3fffff00u) == 0x3fff1000u) { + ++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..32a6619ed --- /dev/null +++ b/core/libs/prx/libSceAgc/tests/Predication.cpp @@ -0,0 +1,97 @@ +#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; + *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 && 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); }); + 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 64ef0dc4f..7fd09e707 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -120,6 +120,7 @@ Throughout the project, every function at every stage either **does exactly what - [libSceUlt queues](../../core/libs/prx/libSceUlt/Export.cpp) - the added `sceUltQueuePop`, `sceUltQueueTryPush`, `sceUltQueueDestroy` and `sceUltQueueDataResourcePoolDestroy` signatures are inferred from the existing push/pop and object APIs. Queue destruction is assumed to reject blocked callers with `ULT_ERROR_BUSY` and otherwise discard queued data; pool exhaustion returns `ULT_ERROR_AGAIN`. These rules, alignment checks and shared slot limits have not been confirmed on PS5 hardware or a title. Non-null queue and data-pool option parameters throw because their layouts are unknown. - [_sceUlobjmgrRegisterObject](../../core/libs/prx/ulobjmgr/Export.cpp) (ulobjmgr) - parameters taken from shadPS4 (PS4): a nonzero 64-bit object, a nonzero 32-bit kind and a 32-bit id output; the meaning of the first two and the PS5 signature are unverified. The id bound of `_sceUlobjmgrUnregisterObject` (below 0x4000) and the raw `EINVAL` (22) return also come from shadPS4 - [sceShareGetCurrentStatus](../../core/libs/prx/libSceShare/Export.cpp) (libSceShare) - validation (feature flag non-zero, status non-null) and the all-zero 16-byte status taken from the [KytyPS5](https://github.com/KytyPS5/KytyPS5) reimplementation, not confirmed on a PS5 title; the meaning of the recording status values is unknown +- [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