From 8232a4c0718a3e0a3f599bd785baa3289cbc5677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Tue, 6 Oct 2026 00:58:23 +0200 Subject: [PATCH] feat(agc): implement sceAgcDcbContextStateOpGetSize Co-Authored-By: Claude --- core/libs/prx/libSceAgc/CMakeLists.txt | 13 ++++ .../libSceAgc/DcbState/src/ContextState.cpp | 11 +++- .../prx/libSceAgc/tests/ContextStateSize.cpp | 63 +++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 core/libs/prx/libSceAgc/tests/ContextStateSize.cpp diff --git a/core/libs/prx/libSceAgc/CMakeLists.txt b/core/libs/prx/libSceAgc/CMakeLists.txt index f1822c8ce..ad9c4495d 100644 --- a/core/libs/prx/libSceAgc/CMakeLists.txt +++ b/core/libs/prx/libSceAgc/CMakeLists.txt @@ -135,6 +135,19 @@ if(BUILD_TESTING) endif() endif() +add_test_executable(agc_context_state_size_tests EXCLUDE_FROM_ALL tests/ContextStateSize.cpp) +target_include_directories(agc_context_state_size_tests PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(agc_context_state_size_tests PRIVATE libSceAgc libc) +configure_windows_unwind(agc_context_state_size_tests) +if(BUILD_TESTING) + set_target_properties(agc_context_state_size_tests PROPERTIES EXCLUDE_FROM_ALL FALSE) + add_test(NAME agc_context_state_size_tests COMMAND agc_context_state_size_tests) + if(WIN32) + set_tests_properties(agc_context_state_size_tests PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() + add_test_executable(agc_interpolant_mapping_tests EXCLUDE_FROM_ALL tests/InterpolantMapping.cpp) target_include_directories(agc_interpolant_mapping_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(agc_interpolant_mapping_tests PRIVATE libSceAgc libc) diff --git a/core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp b/core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp index cfd824f66..27f037bd2 100644 --- a/core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp +++ b/core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp @@ -16,9 +16,14 @@ uint32_t* APS5_VABI sceAgcDcbContextStateOp(CommandBuffer* buf, uint32_t operati } uint64_t APS5_VABI sceAgcDcbContextStateOpGetSize(uint32_t operation) { - (void)operation; - NotImplemented_nid_no_patch(__func__); - return 0; + Agc::Command::Require(operation <= 3, __func__, "invalid context state operation"); + if (operation == 0) { + return 5 * sizeof(std::uint32_t); + } + if (operation == 3) { + return 32 * sizeof(std::uint32_t); + } + return 27 * sizeof(std::uint32_t); } APS5_EXPORT("qj7QZpgr9Uw", sceAgcDcbContextStateAnotherOp); diff --git a/core/libs/prx/libSceAgc/tests/ContextStateSize.cpp b/core/libs/prx/libSceAgc/tests/ContextStateSize.cpp new file mode 100644 index 000000000..130b00848 --- /dev/null +++ b/core/libs/prx/libSceAgc/tests/ContextStateSize.cpp @@ -0,0 +1,63 @@ +#include "SceTypes.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libc/include/Shutdown.hpp" + +#include +#include +#include +#include + +extern "C" std::uint64_t APS5_VABI sceAgcDcbContextStateOpGetSize(std::uint32_t operation); +extern "C" std::uint32_t* APS5_VABI sceAgcDcbContextStateAnotherOp(CommandBuffer* buf, std::uint32_t operation); + +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& error) { + check(error.what()[0] != '\0', "empty exception message"); + return; + } + throw std::runtime_error("expected an exception"); +} + +void testSizes() { + const std::array sizes{20, 108, 108, 128}; + for (std::uint32_t operation = 0; operation < sizes.size(); ++operation) { + check(sceAgcDcbContextStateOpGetSize(operation) == sizes[operation], "context state size mismatch"); + std::array words{}; + CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), + nullptr, nullptr, 0}; + check(sceAgcDcbContextStateAnotherOp(&buffer, operation) == words.data(), "context state op did not start at the cursor"); + const auto written = static_cast(buffer.cursor_up - words.data()) * sizeof(std::uint32_t); + check(written == sizes[operation], "context state size differs from the written packets"); + } +} + +void testRejections() { + expectFailure([] { sceAgcDcbContextStateOpGetSize(4); }); + expectFailure([] { sceAgcDcbContextStateOpGetSize(0xffffffffu); }); +} + +} + +int main() { + try { + testSizes(); + testRejections(); + LibcRunShutdown_nid_postfix(); + std::puts("AGC context state size tests passed"); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + try { LibcRunShutdown_nid_postfix(); } + catch (const std::exception& shutdown) { std::fprintf(stderr, "shutdown: %s\n", shutdown.what()); } + return 1; + } +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 5fd46685b..996f74e62 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -40,6 +40,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceAgcGetIsTrinityMode](../../core/libs/prx/libSceAgc/Misc/src/Platform.cpp) (libSceAgc) - signature from the only PPSA26344 call (a pointer to a one-byte flag, result ignored); a null pointer throws because its error code is unknown - [zARR5aCmkoY](../../core/libs/prx/libSceAgc/DcbFlow/src/Control.cpp) (libSceAgc) - unknown name, signature - [qj7QZpgr9Uw](../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - unknown name +- [sceAgcDcbContextStateOpGetSize](../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - sizes from KytyPS5 only (5, 27, 27 and 32 dwords), equal to what `qj7QZpgr9Uw` writes; KytyPS5 returns 0 for an operation above 3, here it throws - [fd5Bp5tGTgo](../../core/libs/prx/libSceAgc/Misc/src/ShaderFusion.cpp) (libSceAgc) - unknown name - [dolOmWH+huQ](../../core/libs/prx/libSceAgc/Misc/src/ShaderFusion.cpp) (libSceAgc) - unknown name - [dbOlWdppb4o](../../core/libs/prx/libSceAgc/Shader/src/InterpolantMapping.cpp) (libSceAgc) - unknown name; same arguments as `sceAgcCreateInterpolantMapping`; the `SPI_PS_INPUT_CNTL` bits for `is_f16` 2 inputs follow KytyPS5, not confirmed on hardware