diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index afcf3cb1c..d7d1421a1 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -104,6 +104,15 @@ endforeach() add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS}) if(BUILD_TESTING) + add_executable(guest_rendering_wait_tests tests/GuestRenderingWait.cpp) + target_include_directories(guest_rendering_wait_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_rendering_wait_tests PRIVATE libSceAgcDriver libc) + configure_windows_unwind(guest_rendering_wait_tests) + add_test(NAME guest_rendering_wait COMMAND guest_rendering_wait_tests) + if(WIN32) + set_tests_properties(guest_rendering_wait PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_executable(guest_buffer_reuse_tests tests/GuestBufferReuse.cpp) target_include_directories(guest_buffer_reuse_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(guest_buffer_reuse_tests PRIVATE libc) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp index f5b6ab74a..79f96d8c1 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace AgcDriver { @@ -11,6 +12,15 @@ class FrameTiming; inline constexpr std::uint32_t FlipPacketHeader = 0xc004105cu; inline constexpr std::uint32_t FlipPacketWords = 6; +// AnyPS5's internal encoding for the driver-generated rendering wait. +inline constexpr std::uint32_t RenderingWaitPacketHeader = 0xc0021018u; +inline constexpr std::uint32_t RenderingWaitPacketWords = 4; + +class IRenderingWait { +public: + virtual ~IRenderingWait() = default; + virtual void Wait() = 0; +}; struct FlipInfo { std::uint32_t handle; @@ -30,6 +40,9 @@ class IVideoOutput { public: virtual ~IVideoOutput() = default; virtual std::shared_ptr Reserve(const FlipInfo& info) = 0; + virtual std::shared_ptr CaptureRenderingWait(std::uint32_t index) { + throw std::runtime_error("VideoOut: rendering waits are unsupported by this output"); + } virtual void Fail(std::exception_ptr error) noexcept = 0; }; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp index a221b2015..58fca2e72 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp @@ -49,6 +49,7 @@ struct Submission { std::vector commands; std::map> shaders; std::map> flips; + std::map> renderingWaits; bool suspend = false; FrameTiming::Clock::time_point received; FrameTiming::Clock::time_point copied; @@ -118,6 +119,13 @@ public: require(accepted != std::numeric_limits::max(), "submission serial overflow"); for (std::size_t cursor = 0; cursor < submission.commands.size();) { const auto* words = submission.commands.data() + cursor; + if (words[0] == RenderingWaitPacketHeader) { + const auto output = outputs.find(words[1]); + require(output != outputs.end(), "rendering wait references an unregistered video output"); + auto wait = output->second->CaptureRenderingWait(words[2]); + require(wait != nullptr, "video output returned a null rendering wait"); + submission.renderingWaits.emplace(cursor, std::move(wait)); + } if (words[0] == FlipPacketHeader) { const auto output = outputs.find(words[1]); require(output != outputs.end(), "flip references an unregistered video output"); @@ -567,7 +575,10 @@ private: if (device != nullptr) device->WaitIdle(); timing.Mark("device_idle_wait"); } - if (header == FlipPacketHeader) { + if (header == RenderingWaitPacketHeader) { + submission.renderingWaits.at(cursor)->Wait(); + timing.Mark("rendering_wait"); + } else if (header == FlipPacketHeader) { CheckFailure(); timing.Mark("flip_prepare"); } else if (opcode == 0x15) { diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp index c22d4a440..0c6acf712 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp @@ -91,8 +91,7 @@ std::string_view UnsupportedReason(std::uint32_t header) { const auto opcode = (header >> 8u) & 0xffu; if (opcode == 0x10) { switch ((header >> 2u) & 0x3fu) { - case 0: case 0x09: case 0x0b: case 0x0c: case 0x17: case 0x1a: return {}; - case 0x06: return "VideoOut buffer-completion wait interface is not implemented"; + case 0: case 0x06: case 0x09: case 0x0b: case 0x0c: case 0x17: case 0x1a: return {}; case 0x14: case 0x18: return "guest cache actions and GPU release events are not implemented"; default: return "custom packet has no implemented contract in the reference dispatch table"; } @@ -137,6 +136,7 @@ void Validate(std::span packet, std::uint32_t queue) { require((packet[1] & 0xffff0000u) != 0x68750000u, "typed user-data and legacy flip markers are not implemented"); break; case 0x09: size(2); break; + case 0x06: graphics(); size(4); require(packet[3] == 0, "unsupported rendering wait mode"); break; case 0x0b: { const auto data = std::as_bytes(packet.subspan(1)); require(std::find(data.begin(), data.end(), std::byte{}) != data.end(), "unterminated marker text"); diff --git a/core/libs/prx/libSceAgcDriver/Submit/src/Dcb.cpp b/core/libs/prx/libSceAgcDriver/Submit/src/Dcb.cpp index 6c217c0b4..48da441e6 100644 --- a/core/libs/prx/libSceAgcDriver/Submit/src/Dcb.cpp +++ b/core/libs/prx/libSceAgcDriver/Submit/src/Dcb.cpp @@ -1,5 +1,9 @@ #include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include +#include #include #include @@ -8,6 +12,22 @@ extern "C" { +std::uint32_t APS5_VABI sceAgcDriverGetWaitRenderingPacketSizeInDwords() { + return AgcDriver::RenderingWaitPacketWords; +} + +std::uint32_t APS5_VABI sceAgcDriverWaitUntilSafeForRendering(std::uint32_t** command, + std::uint32_t capacity, std::uint32_t mode, std::uint32_t handle, int index) { + if (!command || capacity < AgcDriver::RenderingWaitPacketWords || mode != 0 || index < 0) + throw std::invalid_argument("AGC driver: invalid rendering wait arguments"); + AgcDriver::GuestMemory::CheckRange(command, sizeof(*command), alignof(std::uint32_t*), true); + AgcDriver::GuestMemory::CheckRange(*command, AgcDriver::RenderingWaitPacketWords * sizeof(std::uint32_t), alignof(std::uint32_t), true); + const std::uint32_t words[] = {AgcDriver::RenderingWaitPacketHeader, handle, static_cast(index), mode}; + std::copy(std::begin(words), std::end(words), *command); + *command += AgcDriver::RenderingWaitPacketWords; + return 0; +} + int APS5_VABI sceAgcDriverSubmitDcb(const Packet* packet) { AgcDriver::Submit(packet, 0); return 0; diff --git a/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp b/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp index d3440fdf8..9f1b13b34 100644 --- a/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp +++ b/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp @@ -26,10 +26,34 @@ void checkConfig(const VideoOutConfig& cfg) { cfg.Check(); } +class RenderingWait final : public AgcDriver::IRenderingWait { + std::shared_ptr _config; + std::uint32_t _index; + std::uint64_t _ticket; +public: + RenderingWait(std::shared_ptr config, std::uint32_t index, std::uint64_t ticket) + : _config(std::move(config)), _index(index), _ticket(ticket) {} + void Wait() override { + std::unique_lock lock(_config->mutex); + _config->vblankCond.wait(lock, [&] { + return _config->failure || _config->closing || !_config->opened || + _config->bufferReuse[_index].IsComplete(_ticket); + }); + checkConfig(*_config); + } +}; + class VideoOutput final : public AgcDriver::IVideoOutput { public: VideoOutput(std::shared_ptr config, std::shared_ptr requests) : cfg(std::move(config)), queue(std::move(requests)) {} + std::shared_ptr CaptureRenderingWait(std::uint32_t index) override { + std::lock_guard lock(cfg->mutex); + checkConfig(*cfg); + require(index < VIDEO_OUT_BUFFER_NUM_MAX && cfg->buffers[index].Occupied(), "wait buffer is not registered"); + return std::make_shared(cfg, index, cfg->bufferReuse[index].Capture()); + } + std::shared_ptr Reserve(const AgcDriver::FlipInfo& info) override { require(info.mode == VIDEO_OUT_FLIP_MODE_VSYNC, "unsupported flip mode"); require(info.index >= VIDEO_OUT_BUFFER_INDEX_BLACK && info.index < VIDEO_OUT_BUFFER_NUM_MAX, "invalid flip index"); diff --git a/core/libs/tests/GuestRenderingWait.cpp b/core/libs/tests/GuestRenderingWait.cpp new file mode 100644 index 000000000..71d3cbe98 --- /dev/null +++ b/core/libs/tests/GuestRenderingWait.cpp @@ -0,0 +1,80 @@ +#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" +#include "prx/libSceVideoOut/include/BufferReuseTracker.hpp" +#include "prx/libc/include/Shutdown.hpp" +#include +#include +#include +#include +#include +extern "C" std::uint32_t APS5_VABI sceAgcDriverGetWaitRenderingPacketSizeInDwords(); +extern "C" std::uint32_t APS5_VABI sceAgcDriverWaitUntilSafeForRendering(std::uint32_t**, std::uint32_t, std::uint32_t, std::uint32_t, int); +static void Require(bool value) { if (!value) std::abort(); } +class Output final : public AgcDriver::IVideoOutput, public AgcDriver::IRenderingWait, + public AgcDriver::IFlipRequest, public std::enable_shared_from_this { +public: + std::mutex mutex; + std::condition_variable changed; + BufferReuseTracker reuse; + const std::uint64_t earlier = reuse.Reserve(); + std::uint64_t captured = 0, later = 0; + bool entered = false, flipped = false; + std::shared_ptr CaptureRenderingWait(std::uint32_t index) override { + Require(index == 0); + std::lock_guard lock(mutex); + captured = reuse.Capture(); + return shared_from_this(); + } + std::shared_ptr Reserve(const AgcDriver::FlipInfo& info) override { + Require(info.index == 0); + std::lock_guard lock(mutex); + later = reuse.Reserve(); + return shared_from_this(); + } + void Wait() override { + std::unique_lock lock(mutex); + entered = true; + changed.notify_all(); + Require(changed.wait_for(lock, std::chrono::seconds(5), [&] { return reuse.IsComplete(captured); })); + } + void GpuReady(const std::shared_ptr&) override { + std::lock_guard lock(mutex); + Require(reuse.IsComplete(captured)); + reuse.Complete(later); + flipped = true; + } + void Fail(std::exception_ptr) noexcept override { std::abort(); } +}; +int main() { + auto output = std::make_shared(); + AgcDriverRegisterVideoOutput_nid_postfix(7, output); + std::array words{}; + words.back() = 0x12345678; + auto* cursor = words.data(); + const auto size = sceAgcDriverGetWaitRenderingPacketSizeInDwords(); + Require(size == AgcDriver::RenderingWaitPacketWords); + bool rejected = false; + try { sceAgcDriverWaitUntilSafeForRendering(&cursor, size - 1, 0, 7, 0); } + catch (const std::invalid_argument&) { rejected = true; } + Require(rejected && cursor == words.data() && words[0] == 0); + Require(sceAgcDriverWaitUntilSafeForRendering(&cursor, size, 0, 7, 0) == 0); + Require(cursor == words.data() + size); + Require(words[0] == AgcDriver::RenderingWaitPacketHeader && words[1] == 7 && words[2] == 0 && words[3] == 0); + const std::uint32_t flip[] = {AgcDriver::FlipPacketHeader, 7, 0, 1, 0, 0}; + for (auto word : flip) *cursor++ = word; + Require(words.back() == 0x12345678); + Packet packet{words.data(), 10, 0, {}}; + sceAgcDriverSubmitDcb(&packet); + { + std::unique_lock lock(output->mutex); + Require(output->changed.wait_for(lock, std::chrono::seconds(5), [&] { return output->entered; })); + Require(!output->flipped && output->later > output->captured); + output->reuse.Complete(output->earlier); + output->changed.notify_all(); + } + AgcDriverWaitIdle_nid_postfix(); + Require(output->flipped); + AgcDriverUnregisterVideoOutput_nid_postfix(7, output); + LibcRunShutdown_nid_postfix(); +} diff --git a/docs/YAMAGI_QUAKE2.md b/docs/YAMAGI_QUAKE2.md index 0503635a0..0563951e6 100644 --- a/docs/YAMAGI_QUAKE2.md +++ b/docs/YAMAGI_QUAKE2.md @@ -486,3 +486,27 @@ Inspection of the native ps5-opengl caller establishes the wait argument order as command pointer, packet capacity, mode, video handle, buffer index. The parameter names in the link-only stub do not reflect that calling convention. No game runtime source was copied into the compatibility library. + +## Follow-up: rendering-wait command execution + +Implemented sceAgcDriverGetWaitRenderingPacketSizeInDwords and +sceAgcDriverWaitUntilSafeForRendering for mode zero. The writer validates +capacity and writable memory, emits a four-dword internal wait packet, and +advances the guest command pointer. This encoding is owned by AnyPS5's paired +writer/decoder; it is not claimed to reproduce console-generated packet bytes. + +Submission captures a VideoOut buffer ticket before reserving subsequent flips. +The worker executes the wait outside the GPU mutex. VideoOut waits for earlier +reservations to retire and wakes on close or failure. The captured configuration +object prevents a reopened handle from satisfying a wait for an old port. + +A test submits a generated wait followed by a flip through the real AGC driver +with a controlled output. It proves the wait blocks on earlier ownership and +continues after that ownership is released, despite the later flip reservation. +It also checks packet capacity, pointer advancement, and write boundaries. +All twenty-seven CTest cases pass, along with the existing VideoOut flip lifetime +and reopen regression executables. Actual console packet compatibility, other +wait modes, and rendering a game frame remain unverified. + +The unchanged game now resolves both rendering-wait imports and stops at +`Z4QosVuAsA0` (`pthread_once`). Guest entry-point execution has not yet occurred.