From abeb5954a3eb8d1f13c6fa98474a6e017f00f3cd Mon Sep 17 00:00:00 2001 From: videogamer10293 <347855792+videogamer10293@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:06:29 +0000 Subject: [PATCH 1/6] feat(agc): set an NV checkpoint before every recorded command under APS5_GPU_CHECKPOINTS With NV diagnostic checkpoints enabled, the device's vkGetDeviceProcAddr is wrapped so every recorded draw, dispatch, copy, blit, clear, fill, update, barrier, render pass begin and end, query command and secondary command buffer execution gets a checkpoint before it, and every command buffer one after it begins and one before it ends. The marker carries the command kind from bit 40 and the last guest dispatch or draw token of its command buffer, so a device-lost report names the kind of the last command reached and the guest program before it, without claiming fault token ring entries. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 1 + .../Execution/include/DeviceFault.hpp | 26 +- .../Execution/include/SubmissionSnapshots.hpp | 33 ++ .../Execution/src/VulkanDevice.cpp | 7 +- .../Graphics/include/CommandCheckpoints.hpp | 14 + .../Graphics/src/CommandCheckpoints.cpp | 378 ++++++++++++++++++ .../libSceAgcDriver/Graphics/src/Recorder.cpp | 2 + .../tests/CommandCheckpoints.cpp | 304 ++++++++++++++ .../prx/libSceAgcDriver/tests/DeviceFault.cpp | 74 ++++ .../cmake/agc_command_checkpoints_tests.cmake | 15 + docs/dev/BUILD.md | 2 +- docs/dev/ENVIRONMENT.md | 2 +- .../changes/agc-checkpoint-command-kinds.md | 9 + 13 files changed, 856 insertions(+), 11 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp create mode 100644 core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp create mode 100644 core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp create mode 100644 core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake create mode 100644 docs/dev/debt/changes/agc-checkpoint-command-kinds.md diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 26d8d9642c2f..28fb6059205b 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -513,6 +513,7 @@ set(agcDriverSources ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/PipelineJournal.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/RuntimeSwitches.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/Resources.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/CommandCheckpoints.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/DeferredDestruction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/MemoryAccounting.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/src/PlaceholderImages.cpp diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp index 0c3183d52798..fc38cb59d130 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp @@ -342,12 +342,17 @@ inline std::vector DeviceFaultReport(VkDevice device, PFN_vkGetDevi for (const auto& point : points) { const FaultToken token = static_cast(reinterpret_cast(point.pCheckpointMarker)); const auto found = ring.Find(token); - if (!found.has_value()) { - std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: unknown token %u queue 0x%llx", static_cast(token), static_cast(reinterpret_cast(queue))); + const auto kind = CheckpointKindOf(point.pCheckpointMarker); + const std::string lead = kind == CheckpointKind::none ? std::string() : std::string(CheckpointKindName(kind)) + " after "; + if (kind != CheckpointKind::none && token == 0) { + std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %s, no guest token before it on its command buffer, queue 0x%llx", CheckpointKindName(kind), static_cast(reinterpret_cast(queue))); + } else if (!found.has_value()) { + if (kind != CheckpointKind::none) std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %s after token %u, no longer in the ring, queue 0x%llx", CheckpointKindName(kind), static_cast(token), static_cast(reinterpret_cast(queue))); + else std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: unknown token %u queue 0x%llx", static_cast(token), static_cast(reinterpret_cast(queue))); } else { const std::size_t descLength = strnlen(found->desc, sizeof(found->desc)); - if (descLength == 0) std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: token %u batch %llu queue 0x%llx program 0x%llx variant %llu", static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId)); - else std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: token %u batch %llu queue 0x%llx program 0x%llx variant %llu %.*s", static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId), static_cast(descLength), found->desc); + if (descLength == 0) std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %stoken %u batch %llu queue 0x%llx program 0x%llx variant %llu", lead.c_str(), static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId)); + else std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %stoken %u batch %llu queue 0x%llx program 0x%llx variant %llu %.*s", lead.c_str(), static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId), static_cast(descLength), found->desc); } DeviceFaultAppendStage(checkpoint, sizeof(checkpoint), point.stage); lines.emplace_back(checkpoint); @@ -563,13 +568,18 @@ inline std::vector DeviceFaultReport(VkDevice device, PFN_vkGetDevi for (const auto& point : points) { const FaultToken token = static_cast(reinterpret_cast(point.pCheckpointMarker)); const auto found = ring.Find(token); - if (!found.has_value()) { - std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: unknown token %u queue 0x%llx", static_cast(token), static_cast(reinterpret_cast(queue))); + const auto kind = CheckpointKindOf(point.pCheckpointMarker); + const std::string lead = kind == CheckpointKind::none ? std::string() : std::string(CheckpointKindName(kind)) + " after "; + if (kind != CheckpointKind::none && token == 0) { + std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %s, no guest token before it on its command buffer, queue 0x%llx", CheckpointKindName(kind), static_cast(reinterpret_cast(queue))); + } else if (!found.has_value()) { + if (kind != CheckpointKind::none) std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %s after token %u, no longer in the ring, queue 0x%llx", CheckpointKindName(kind), static_cast(token), static_cast(reinterpret_cast(queue))); + else std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: unknown token %u queue 0x%llx", static_cast(token), static_cast(reinterpret_cast(queue))); } else { const char* stage = DeviceFaultStageName(found->stage); const std::size_t descLength = strnlen(found->desc, sizeof(found->desc)); - if (descLength == 0) std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: token %u batch %llu queue 0x%llx program 0x%llx variant %llu stage %s", static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId), stage); - else std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: token %u batch %llu queue 0x%llx program 0x%llx variant %llu stage %s %.*s", static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId), stage, static_cast(descLength), found->desc); + if (descLength == 0) std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %stoken %u batch %llu queue 0x%llx program 0x%llx variant %llu stage %s", lead.c_str(), static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId), stage); + else std::snprintf(checkpoint, sizeof(checkpoint), "[device-fault] checkpoint: %stoken %u batch %llu queue 0x%llx program 0x%llx variant %llu stage %s %.*s", lead.c_str(), static_cast(token), static_cast(found->submitSerial), static_cast(reinterpret_cast(queue)), static_cast(found->programAddress), static_cast(found->variantId), stage, static_cast(descLength), found->desc); if (found->argBuffer != 0) { DeviceFaultAppendStage(checkpoint, sizeof(checkpoint), point.stage); lines.emplace_back(checkpoint); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp index 25c2f2379cb7..3643792cb7fb 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp @@ -17,6 +17,39 @@ using FaultToken = std::uint32_t; enum class FaultStage : std::uint32_t { unknown = 0, graphics = 1, compute = 2, mesh = 3, task = 4, copy = 5 }; +enum class CheckpointKind : std::uint8_t { none = 0, draw, dispatch, copy, blit, clear, fill, update, barrier, renderPassBegin, renderPassEnd, query, execute, commandBufferBegin, commandBufferEnd, count }; + +inline constexpr unsigned CheckpointKindShift = 40; + +inline std::uintptr_t CheckpointMarker(FaultToken token, CheckpointKind kind) noexcept { + return static_cast(token) | (static_cast(kind) << CheckpointKindShift); +} + +inline CheckpointKind CheckpointKindOf(const void* marker) noexcept { + const auto kind = (reinterpret_cast(marker) >> CheckpointKindShift) & 0xffu; + return kind < static_cast(CheckpointKind::count) ? static_cast(kind) : CheckpointKind::none; +} + +inline const char* CheckpointKindName(CheckpointKind kind) noexcept { + switch (kind) { + case CheckpointKind::draw: return "draw"; + case CheckpointKind::dispatch: return "dispatch"; + case CheckpointKind::copy: return "copy"; + case CheckpointKind::blit: return "blit"; + case CheckpointKind::clear: return "clear"; + case CheckpointKind::fill: return "fill"; + case CheckpointKind::update: return "update"; + case CheckpointKind::barrier: return "barrier"; + case CheckpointKind::renderPassBegin: return "render pass begin"; + case CheckpointKind::renderPassEnd: return "render pass end"; + case CheckpointKind::query: return "query"; + case CheckpointKind::execute: return "execute commands"; + case CheckpointKind::commandBufferBegin: return "command buffer begin"; + case CheckpointKind::commandBufferEnd: return "command buffer end"; + default: return "none"; + } +} + struct FaultTokenEntry { alignas(std::atomic_ref::required_alignment) mutable FaultToken token = 0; std::uint64_t submitSerial = 0; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index d7acf3208d71..5603ed4433ea 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -13,6 +13,7 @@ #include "SpirvBackend/SpirvOptimizer.hpp" #endif #include "prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp" +#include "prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp" #include "prx/libSceAgcDriver/Graphics/include/PipelineLibrary.hpp" #include "prx/libSceAgcDriver/Graphics/include/DeferredDestruction.hpp" #include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" @@ -2652,6 +2653,7 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) : check(state->InstanceFunction("vkCreateDevice")(selected, &deviceInfo, nullptr, &state->device), "vkCreateDevice"); state->shaderProfile = std::move(shaderProfile); if (compileOnly) return; + if (checkpoints) state->deviceProc = Graphics::CheckpointCommandKinds(state->device, state->deviceProc); if (state->deferDestruction) state->contextDeviceProc = Graphics::DeferDestructionToIdle(state->device, state->deviceProc); if (calibratedTimestamps != nullptr) { const bool khr = std::strcmp(calibratedTimestamps, VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME) == 0; @@ -6484,7 +6486,10 @@ void VulkanDevice::recordDispatch(RecordedDispatch& record) { } } } - if (state->checkpointMarker != nullptr) state->checkpointMarker(commands, reinterpret_cast(static_cast(faultToken))); + if (state->checkpointMarker != nullptr) { + state->checkpointMarker(commands, reinterpret_cast(static_cast(faultToken))); + if (faultToken != 0) Graphics::NoteRecordedCheckpoint(commands, faultToken); + } if (faultToken != 0 && state->bufferMarker != nullptr) state->bufferMarker(commands, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, state->markerBuffer, 0, faultToken); if (split) { if (splitSlice->listed >= 0) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp new file mode 100644 index 000000000000..7a640528fdaa --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp @@ -0,0 +1,14 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_GRAPHICS_INCLUDE_COMMANDCHECKPOINTS_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_GRAPHICS_INCLUDE_COMMANDCHECKPOINTS_HPP + +#include "prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Context.hpp" + +namespace AgcDriver::Graphics { + +PFN_vkGetDeviceProcAddr CheckpointCommandKinds(VkDevice device, PFN_vkGetDeviceProcAddr resolver); +void NoteRecordedCheckpoint(VkCommandBuffer commands, AgcDriver::FaultToken token) noexcept; + +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp new file mode 100644 index 000000000000..9173230feb59 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp @@ -0,0 +1,378 @@ +#include "prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp" +#include +#include +#include +#include +#include + +namespace AgcDriver::Graphics { + +namespace { + +struct RecordedCheckpoint { + VkCommandBuffer commands = VK_NULL_HANDLE; + AgcDriver::FaultToken token = 0; +}; + +RecordedCheckpoint& lastRecorded() noexcept { + thread_local RecordedCheckpoint recorded; + return recorded; +} + +std::atomic& resolverSlot() noexcept { + static std::atomic slot{nullptr}; + return slot; +} + +std::atomic& setCheckpointSlot() noexcept { + static std::atomic slot{nullptr}; + return slot; +} + +void mark(VkCommandBuffer commands, AgcDriver::CheckpointKind kind) noexcept { + const auto setCheckpoint = setCheckpointSlot().load(std::memory_order_acquire); + if (setCheckpoint == nullptr || commands == VK_NULL_HANDLE) return; + const auto& recorded = lastRecorded(); + const AgcDriver::FaultToken token = recorded.commands == commands ? recorded.token : 0; + setCheckpoint(commands, reinterpret_cast(AgcDriver::CheckpointMarker(token, kind))); +} + +enum class Slot : std::size_t { + BeginCommandBuffer, + EndCommandBuffer, + CmdDraw, + CmdDrawIndexed, + CmdDrawIndirect, + CmdDrawIndexedIndirect, + CmdDrawIndirectCount, + CmdDrawIndexedIndirectCount, + CmdDrawMeshTasks, + CmdDrawMeshTasksIndirect, + CmdDispatch, + CmdDispatchBase, + CmdDispatchIndirect, + CmdCopyBuffer, + CmdCopyImage, + CmdCopyBufferToImage, + CmdCopyImageToBuffer, + CmdBlitImage, + CmdResolveImage, + CmdClearColorImage, + CmdClearDepthStencilImage, + CmdClearAttachments, + CmdFillBuffer, + CmdUpdateBuffer, + CmdPipelineBarrier, + CmdPipelineBarrier2, + CmdBeginRenderPass, + CmdEndRenderPass, + CmdBeginRendering, + CmdEndRendering, + CmdResetQueryPool, + CmdBeginQuery, + CmdEndQuery, + CmdCopyQueryPoolResults, + CmdWriteTimestamp, + CmdExecuteCommands, +}; + +template +struct Real { + static std::atomic& Holder() noexcept { + static std::atomic slot{nullptr}; + return slot; + } + static TFunction Get() noexcept { + return Holder().load(std::memory_order_acquire); + } + static void Store(PFN_vkVoidFunction real, const char* name) { + const auto function = reinterpret_cast(real); + TFunction expected = nullptr; + if (Holder().compare_exchange_strong(expected, function, std::memory_order_acq_rel) || expected == function) return; + throw std::runtime_error(std::string("AGC graphics: command checkpoints got two different functions for ") + name); + } +}; + +VKAPI_ATTR VkResult VKAPI_CALL checkpointedBeginCommandBuffer(VkCommandBuffer commands, const VkCommandBufferBeginInfo* info) { + const auto result = Real::Get()(commands, info); + auto& recorded = lastRecorded(); + if (recorded.commands == commands) recorded.token = 0; + if (result == VK_SUCCESS) mark(commands, AgcDriver::CheckpointKind::commandBufferBegin); + return result; +} + +VKAPI_ATTR VkResult VKAPI_CALL checkpointedEndCommandBuffer(VkCommandBuffer commands) { + mark(commands, AgcDriver::CheckpointKind::commandBufferEnd); + return Real::Get()(commands); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDraw(VkCommandBuffer commands, std::uint32_t vertexCount, std::uint32_t instanceCount, std::uint32_t firstVertex, std::uint32_t firstInstance) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, vertexCount, instanceCount, firstVertex, firstInstance); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexed(VkCommandBuffer commands, std::uint32_t indexCount, std::uint32_t instanceCount, std::uint32_t firstIndex, std::int32_t vertexOffset, std::uint32_t firstInstance) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, std::uint32_t drawCount, std::uint32_t stride) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, buffer, offset, drawCount, stride); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexedIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, std::uint32_t drawCount, std::uint32_t stride) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, buffer, offset, drawCount, stride); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndirectCount(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer, VkDeviceSize countOffset, std::uint32_t maxDrawCount, std::uint32_t stride) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexedIndirectCount(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer, VkDeviceSize countOffset, std::uint32_t maxDrawCount, std::uint32_t stride) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawMeshTasks(VkCommandBuffer commands, std::uint32_t x, std::uint32_t y, std::uint32_t z) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, x, y, z); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDrawMeshTasksIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, std::uint32_t drawCount, std::uint32_t stride) { + mark(commands, AgcDriver::CheckpointKind::draw); + Real::Get()(commands, buffer, offset, drawCount, stride); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDispatch(VkCommandBuffer commands, std::uint32_t x, std::uint32_t y, std::uint32_t z) { + mark(commands, AgcDriver::CheckpointKind::dispatch); + Real::Get()(commands, x, y, z); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDispatchBase(VkCommandBuffer commands, std::uint32_t baseX, std::uint32_t baseY, std::uint32_t baseZ, std::uint32_t x, std::uint32_t y, std::uint32_t z) { + mark(commands, AgcDriver::CheckpointKind::dispatch); + Real::Get()(commands, baseX, baseY, baseZ, x, y, z); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedDispatchIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset) { + mark(commands, AgcDriver::CheckpointKind::dispatch); + Real::Get()(commands, buffer, offset); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedCopyBuffer(VkCommandBuffer commands, VkBuffer source, VkBuffer destination, std::uint32_t regionCount, const VkBufferCopy* regions) { + mark(commands, AgcDriver::CheckpointKind::copy); + Real::Get()(commands, source, destination, regionCount, regions); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedCopyImage(VkCommandBuffer commands, VkImage source, VkImageLayout sourceLayout, VkImage destination, VkImageLayout destinationLayout, std::uint32_t regionCount, const VkImageCopy* regions) { + mark(commands, AgcDriver::CheckpointKind::copy); + Real::Get()(commands, source, sourceLayout, destination, destinationLayout, regionCount, regions); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedCopyBufferToImage(VkCommandBuffer commands, VkBuffer source, VkImage destination, VkImageLayout layout, std::uint32_t regionCount, const VkBufferImageCopy* regions) { + mark(commands, AgcDriver::CheckpointKind::copy); + Real::Get()(commands, source, destination, layout, regionCount, regions); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedCopyImageToBuffer(VkCommandBuffer commands, VkImage source, VkImageLayout layout, VkBuffer destination, std::uint32_t regionCount, const VkBufferImageCopy* regions) { + mark(commands, AgcDriver::CheckpointKind::copy); + Real::Get()(commands, source, layout, destination, regionCount, regions); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedBlitImage(VkCommandBuffer commands, VkImage source, VkImageLayout sourceLayout, VkImage destination, VkImageLayout destinationLayout, std::uint32_t regionCount, const VkImageBlit* regions, VkFilter filter) { + mark(commands, AgcDriver::CheckpointKind::blit); + Real::Get()(commands, source, sourceLayout, destination, destinationLayout, regionCount, regions, filter); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedResolveImage(VkCommandBuffer commands, VkImage source, VkImageLayout sourceLayout, VkImage destination, VkImageLayout destinationLayout, std::uint32_t regionCount, const VkImageResolve* regions) { + mark(commands, AgcDriver::CheckpointKind::blit); + Real::Get()(commands, source, sourceLayout, destination, destinationLayout, regionCount, regions); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedClearColorImage(VkCommandBuffer commands, VkImage image, VkImageLayout layout, const VkClearColorValue* color, std::uint32_t rangeCount, const VkImageSubresourceRange* ranges) { + mark(commands, AgcDriver::CheckpointKind::clear); + Real::Get()(commands, image, layout, color, rangeCount, ranges); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedClearDepthStencilImage(VkCommandBuffer commands, VkImage image, VkImageLayout layout, const VkClearDepthStencilValue* value, std::uint32_t rangeCount, const VkImageSubresourceRange* ranges) { + mark(commands, AgcDriver::CheckpointKind::clear); + Real::Get()(commands, image, layout, value, rangeCount, ranges); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedClearAttachments(VkCommandBuffer commands, std::uint32_t attachmentCount, const VkClearAttachment* attachments, std::uint32_t rectCount, const VkClearRect* rects) { + mark(commands, AgcDriver::CheckpointKind::clear); + Real::Get()(commands, attachmentCount, attachments, rectCount, rects); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedFillBuffer(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, std::uint32_t data) { + mark(commands, AgcDriver::CheckpointKind::fill); + Real::Get()(commands, buffer, offset, size, data); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedUpdateBuffer(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data) { + mark(commands, AgcDriver::CheckpointKind::update); + Real::Get()(commands, buffer, offset, size, data); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedPipelineBarrier(VkCommandBuffer commands, VkPipelineStageFlags source, VkPipelineStageFlags destination, VkDependencyFlags dependency, std::uint32_t memoryCount, const VkMemoryBarrier* memory, std::uint32_t bufferCount, const VkBufferMemoryBarrier* buffers, std::uint32_t imageCount, const VkImageMemoryBarrier* images) { + mark(commands, AgcDriver::CheckpointKind::barrier); + Real::Get()(commands, source, destination, dependency, memoryCount, memory, bufferCount, buffers, imageCount, images); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedPipelineBarrier2(VkCommandBuffer commands, const VkDependencyInfo* dependency) { + mark(commands, AgcDriver::CheckpointKind::barrier); + Real::Get()(commands, dependency); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedBeginRenderPass(VkCommandBuffer commands, const VkRenderPassBeginInfo* begin, VkSubpassContents contents) { + mark(commands, AgcDriver::CheckpointKind::renderPassBegin); + Real::Get()(commands, begin, contents); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedEndRenderPass(VkCommandBuffer commands) { + mark(commands, AgcDriver::CheckpointKind::renderPassEnd); + Real::Get()(commands); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedBeginRendering(VkCommandBuffer commands, const VkRenderingInfo* info) { + mark(commands, AgcDriver::CheckpointKind::renderPassBegin); + Real::Get()(commands, info); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedEndRendering(VkCommandBuffer commands) { + mark(commands, AgcDriver::CheckpointKind::renderPassEnd); + Real::Get()(commands); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedResetQueryPool(VkCommandBuffer commands, VkQueryPool pool, std::uint32_t first, std::uint32_t count) { + mark(commands, AgcDriver::CheckpointKind::query); + Real::Get()(commands, pool, first, count); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedBeginQuery(VkCommandBuffer commands, VkQueryPool pool, std::uint32_t query, VkQueryControlFlags flags) { + mark(commands, AgcDriver::CheckpointKind::query); + Real::Get()(commands, pool, query, flags); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedEndQuery(VkCommandBuffer commands, VkQueryPool pool, std::uint32_t query) { + mark(commands, AgcDriver::CheckpointKind::query); + Real::Get()(commands, pool, query); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedCopyQueryPoolResults(VkCommandBuffer commands, VkQueryPool pool, std::uint32_t first, std::uint32_t count, VkBuffer destination, VkDeviceSize offset, VkDeviceSize stride, VkQueryResultFlags flags) { + mark(commands, AgcDriver::CheckpointKind::query); + Real::Get()(commands, pool, first, count, destination, offset, stride, flags); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedWriteTimestamp(VkCommandBuffer commands, VkPipelineStageFlagBits stage, VkQueryPool pool, std::uint32_t query) { + mark(commands, AgcDriver::CheckpointKind::query); + Real::Get()(commands, stage, pool, query); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedExecuteCommands(VkCommandBuffer commands, std::uint32_t count, const VkCommandBuffer* buffers) { + mark(commands, AgcDriver::CheckpointKind::execute); + Real::Get()(commands, count, buffers); +} + +struct Hook { + const char* name; + void (*store)(PFN_vkVoidFunction real, const char* name); + PFN_vkVoidFunction wrapper; +}; + +template +Hook hook(const char* name, TFunction wrapper) noexcept { + return {name, &Real::Store, reinterpret_cast(wrapper)}; +} + +const Hook* findHook(const char* name) noexcept { + static const Hook hooks[] = { + hook("vkBeginCommandBuffer", &checkpointedBeginCommandBuffer), + hook("vkEndCommandBuffer", &checkpointedEndCommandBuffer), + hook("vkCmdDraw", &checkpointedDraw), + hook("vkCmdDrawIndexed", &checkpointedDrawIndexed), + hook("vkCmdDrawIndirect", &checkpointedDrawIndirect), + hook("vkCmdDrawIndexedIndirect", &checkpointedDrawIndexedIndirect), + hook("vkCmdDrawIndirectCountKHR", &checkpointedDrawIndirectCount), + hook("vkCmdDrawIndexedIndirectCountKHR", &checkpointedDrawIndexedIndirectCount), + hook("vkCmdDrawMeshTasksEXT", &checkpointedDrawMeshTasks), + hook("vkCmdDrawMeshTasksIndirectEXT", &checkpointedDrawMeshTasksIndirect), + hook("vkCmdDispatch", &checkpointedDispatch), + hook("vkCmdDispatchBase", &checkpointedDispatchBase), + hook("vkCmdDispatchIndirect", &checkpointedDispatchIndirect), + hook("vkCmdCopyBuffer", &checkpointedCopyBuffer), + hook("vkCmdCopyImage", &checkpointedCopyImage), + hook("vkCmdCopyBufferToImage", &checkpointedCopyBufferToImage), + hook("vkCmdCopyImageToBuffer", &checkpointedCopyImageToBuffer), + hook("vkCmdBlitImage", &checkpointedBlitImage), + hook("vkCmdResolveImage", &checkpointedResolveImage), + hook("vkCmdClearColorImage", &checkpointedClearColorImage), + hook("vkCmdClearDepthStencilImage", &checkpointedClearDepthStencilImage), + hook("vkCmdClearAttachments", &checkpointedClearAttachments), + hook("vkCmdFillBuffer", &checkpointedFillBuffer), + hook("vkCmdUpdateBuffer", &checkpointedUpdateBuffer), + hook("vkCmdPipelineBarrier", &checkpointedPipelineBarrier), + hook("vkCmdPipelineBarrier2", &checkpointedPipelineBarrier2), + hook("vkCmdPipelineBarrier2KHR", &checkpointedPipelineBarrier2), + hook("vkCmdBeginRenderPass", &checkpointedBeginRenderPass), + hook("vkCmdEndRenderPass", &checkpointedEndRenderPass), + hook("vkCmdBeginRendering", &checkpointedBeginRendering), + hook("vkCmdBeginRenderingKHR", &checkpointedBeginRendering), + hook("vkCmdEndRendering", &checkpointedEndRendering), + hook("vkCmdEndRenderingKHR", &checkpointedEndRendering), + hook("vkCmdResetQueryPool", &checkpointedResetQueryPool), + hook("vkCmdBeginQuery", &checkpointedBeginQuery), + hook("vkCmdEndQuery", &checkpointedEndQuery), + hook("vkCmdCopyQueryPoolResults", &checkpointedCopyQueryPoolResults), + hook("vkCmdWriteTimestamp", &checkpointedWriteTimestamp), + hook("vkCmdExecuteCommands", &checkpointedExecuteCommands), + }; + for (const auto& entry : hooks) { + if (std::strcmp(name, entry.name) == 0) return &entry; + } + return nullptr; +} + +bool recordsState(const char* name) noexcept { + static const char* const prefixes[] = {"vkCmdBind", "vkCmdSet", "vkCmdPush"}; + for (const auto* prefix : prefixes) { + if (std::strncmp(name, prefix, std::strlen(prefix)) == 0) return true; + } + return std::strcmp(name, "vkCmdWriteBufferMarkerAMD") == 0 || std::strcmp(name, "vkCmdWriteBufferMarker2AMD") == 0; +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL checkpointingDeviceProc(VkDevice device, const char* name) { + const auto resolver = resolverSlot().load(std::memory_order_acquire); + if (resolver == nullptr || name == nullptr) return nullptr; + const auto real = resolver(device, name); + if (real == nullptr) return nullptr; + if (const auto* found = findHook(name)) { + found->store(real, name); + return found->wrapper; + } + if (std::strncmp(name, "vkCmd", 5) == 0 && !recordsState(name)) throw std::runtime_error(std::string("AGC graphics: command checkpoints do not know whether ") + name + " records work; add it to CommandCheckpoints.cpp"); + return real; +} + +} + +PFN_vkGetDeviceProcAddr CheckpointCommandKinds(VkDevice device, PFN_vkGetDeviceProcAddr resolver) { + if (device == VK_NULL_HANDLE || resolver == nullptr) throw std::invalid_argument("AGC graphics: command checkpoints need a device and its resolver"); + const auto setCheckpoint = reinterpret_cast(resolver(device, "vkCmdSetCheckpointNV")); + if (setCheckpoint == nullptr) throw std::runtime_error("AGC graphics: missing Vulkan function: vkCmdSetCheckpointNV"); + PFN_vkGetDeviceProcAddr expectedResolver = nullptr; + if (!resolverSlot().compare_exchange_strong(expectedResolver, resolver, std::memory_order_acq_rel) && expectedResolver != resolver) throw std::runtime_error("AGC graphics: command checkpoints found two different device resolvers"); + PFN_vkCmdSetCheckpointNV expectedCheckpoint = nullptr; + if (!setCheckpointSlot().compare_exchange_strong(expectedCheckpoint, setCheckpoint, std::memory_order_acq_rel) && expectedCheckpoint != setCheckpoint) throw std::runtime_error("AGC graphics: command checkpoints found two different vkCmdSetCheckpointNV functions"); + return &checkpointingDeviceProc; +} + +void NoteRecordedCheckpoint(VkCommandBuffer commands, AgcDriver::FaultToken token) noexcept { + auto& recorded = lastRecorded(); + recorded.commands = commands; + recorded.token = token; +} + +} diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp index 2fa30af0d3f8..ee840b90cb87 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp @@ -9,6 +9,7 @@ #include "prx/libc/include/HostThreadSlot.hpp" #include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp" #include "prx/libSceAgcDriver/Graphics/include/Recorder.hpp" +#include "prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp" #include "prx/libSceAgcDriver/Graphics/include/DrawStageTimingKeys.hpp" #include "prx/libSceAgcDriver/Graphics/include/BdaResources.hpp" #include "prx/libSceAgcDriver/Graphics/include/ReferenceStorage.hpp" @@ -1284,6 +1285,7 @@ void WriteNvCheckpointToken(const Context& context, VkCommandBuffer commands, st auto setCheckpoint = reinterpret_cast(context.deviceProc(context.device, "vkCmdSetCheckpointNV")); if (setCheckpoint == nullptr) return; EmitNvCheckpointToken(commands, static_cast(token), setCheckpoint); + NoteRecordedCheckpoint(commands, static_cast(token)); } catch (...) { } } diff --git a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp new file mode 100644 index 000000000000..911a36daecfb --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp @@ -0,0 +1,304 @@ +#include "prx/libSceAgcDriver/Graphics/include/CommandCheckpoints.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace AgcDriver::Graphics { +void WriteNvCheckpointToken(const Context& context, VkCommandBuffer commands, std::uint64_t token, std::uint32_t queue); +} + +namespace { + +using AgcDriver::CheckpointKind; + +int failures = 0; + +void Expect(bool condition, const std::string& what) { + if (condition) return; + std::fprintf(stderr, "FAIL: %s\n", what.c_str()); + ++failures; +} + +struct Event { + std::string what; + VkCommandBuffer commands = VK_NULL_HANDLE; + std::uintptr_t marker = 0; +}; + +std::vector events; + +void Log(const char* what, VkCommandBuffer commands) { + events.push_back({what, commands, 0}); +} + +VkDevice Device() { + return reinterpret_cast(static_cast(0xD0)); +} + +VkCommandBuffer Commands(std::uintptr_t value) { + return reinterpret_cast(value); +} + +VKAPI_ATTR void VKAPI_CALL mockSetCheckpoint(VkCommandBuffer commands, const void* marker) { + events.push_back({"checkpoint", commands, reinterpret_cast(marker)}); +} + +std::map& Functions() { + static std::map functions = [] { + std::map made; + const auto add = [&](const char* name, auto function) { made[name] = reinterpret_cast(function); }; + add("vkCmdSetCheckpointNV", &mockSetCheckpoint); + add("vkBeginCommandBuffer", static_cast([](VkCommandBuffer c, const VkCommandBufferBeginInfo*) { Log("vkBeginCommandBuffer", c); return VK_SUCCESS; })); + add("vkEndCommandBuffer", static_cast([](VkCommandBuffer c) { Log("vkEndCommandBuffer", c); return VK_SUCCESS; })); + add("vkCmdDraw", static_cast([](VkCommandBuffer c, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t) { Log("vkCmdDraw", c); })); + add("vkCmdDrawIndexed", static_cast([](VkCommandBuffer c, std::uint32_t, std::uint32_t, std::uint32_t, std::int32_t, std::uint32_t) { Log("vkCmdDrawIndexed", c); })); + add("vkCmdDrawIndirect", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t) { Log("vkCmdDrawIndirect", c); })); + add("vkCmdDrawIndexedIndirect", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t) { Log("vkCmdDrawIndexedIndirect", c); })); + add("vkCmdDrawIndirectCountKHR", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t) { Log("vkCmdDrawIndirectCountKHR", c); })); + add("vkCmdDrawIndexedIndirectCountKHR", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t) { Log("vkCmdDrawIndexedIndirectCountKHR", c); })); + add("vkCmdDrawMeshTasksEXT", static_cast([](VkCommandBuffer c, std::uint32_t, std::uint32_t, std::uint32_t) { Log("vkCmdDrawMeshTasksEXT", c); })); + add("vkCmdDrawMeshTasksIndirectEXT", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t) { Log("vkCmdDrawMeshTasksIndirectEXT", c); })); + add("vkCmdDispatch", static_cast([](VkCommandBuffer c, std::uint32_t, std::uint32_t, std::uint32_t) { Log("vkCmdDispatch", c); })); + add("vkCmdDispatchBase", static_cast([](VkCommandBuffer c, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t) { Log("vkCmdDispatchBase", c); })); + add("vkCmdDispatchIndirect", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize) { Log("vkCmdDispatchIndirect", c); })); + add("vkCmdCopyBuffer", static_cast([](VkCommandBuffer c, VkBuffer, VkBuffer, std::uint32_t, const VkBufferCopy*) { Log("vkCmdCopyBuffer", c); })); + add("vkCmdCopyImage", static_cast([](VkCommandBuffer c, VkImage, VkImageLayout, VkImage, VkImageLayout, std::uint32_t, const VkImageCopy*) { Log("vkCmdCopyImage", c); })); + add("vkCmdCopyBufferToImage", static_cast([](VkCommandBuffer c, VkBuffer, VkImage, VkImageLayout, std::uint32_t, const VkBufferImageCopy*) { Log("vkCmdCopyBufferToImage", c); })); + add("vkCmdCopyImageToBuffer", static_cast([](VkCommandBuffer c, VkImage, VkImageLayout, VkBuffer, std::uint32_t, const VkBufferImageCopy*) { Log("vkCmdCopyImageToBuffer", c); })); + add("vkCmdBlitImage", static_cast([](VkCommandBuffer c, VkImage, VkImageLayout, VkImage, VkImageLayout, std::uint32_t, const VkImageBlit*, VkFilter) { Log("vkCmdBlitImage", c); })); + add("vkCmdResolveImage", static_cast([](VkCommandBuffer c, VkImage, VkImageLayout, VkImage, VkImageLayout, std::uint32_t, const VkImageResolve*) { Log("vkCmdResolveImage", c); })); + add("vkCmdClearColorImage", static_cast([](VkCommandBuffer c, VkImage, VkImageLayout, const VkClearColorValue*, std::uint32_t, const VkImageSubresourceRange*) { Log("vkCmdClearColorImage", c); })); + add("vkCmdClearDepthStencilImage", static_cast([](VkCommandBuffer c, VkImage, VkImageLayout, const VkClearDepthStencilValue*, std::uint32_t, const VkImageSubresourceRange*) { Log("vkCmdClearDepthStencilImage", c); })); + add("vkCmdClearAttachments", static_cast([](VkCommandBuffer c, std::uint32_t, const VkClearAttachment*, std::uint32_t, const VkClearRect*) { Log("vkCmdClearAttachments", c); })); + add("vkCmdFillBuffer", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, VkDeviceSize, std::uint32_t) { Log("vkCmdFillBuffer", c); })); + add("vkCmdUpdateBuffer", static_cast([](VkCommandBuffer c, VkBuffer, VkDeviceSize, VkDeviceSize, const void*) { Log("vkCmdUpdateBuffer", c); })); + add("vkCmdPipelineBarrier", static_cast([](VkCommandBuffer c, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags, std::uint32_t, const VkMemoryBarrier*, std::uint32_t, const VkBufferMemoryBarrier*, std::uint32_t, const VkImageMemoryBarrier*) { Log("vkCmdPipelineBarrier", c); })); + add("vkCmdPipelineBarrier2", static_cast([](VkCommandBuffer c, const VkDependencyInfo*) { Log("vkCmdPipelineBarrier2", c); })); + add("vkCmdBeginRenderPass", static_cast([](VkCommandBuffer c, const VkRenderPassBeginInfo*, VkSubpassContents) { Log("vkCmdBeginRenderPass", c); })); + add("vkCmdEndRenderPass", static_cast([](VkCommandBuffer c) { Log("vkCmdEndRenderPass", c); })); + add("vkCmdBeginRenderingKHR", static_cast([](VkCommandBuffer c, const VkRenderingInfo*) { Log("vkCmdBeginRendering", c); })); + add("vkCmdEndRenderingKHR", static_cast([](VkCommandBuffer c) { Log("vkCmdEndRendering", c); })); + add("vkCmdResetQueryPool", static_cast([](VkCommandBuffer c, VkQueryPool, std::uint32_t, std::uint32_t) { Log("vkCmdResetQueryPool", c); })); + add("vkCmdBeginQuery", static_cast([](VkCommandBuffer c, VkQueryPool, std::uint32_t, VkQueryControlFlags) { Log("vkCmdBeginQuery", c); })); + add("vkCmdEndQuery", static_cast([](VkCommandBuffer c, VkQueryPool, std::uint32_t) { Log("vkCmdEndQuery", c); })); + add("vkCmdCopyQueryPoolResults", static_cast([](VkCommandBuffer c, VkQueryPool, std::uint32_t, std::uint32_t, VkBuffer, VkDeviceSize, VkDeviceSize, VkQueryResultFlags) { Log("vkCmdCopyQueryPoolResults", c); })); + add("vkCmdWriteTimestamp", static_cast([](VkCommandBuffer c, VkPipelineStageFlagBits, VkQueryPool, std::uint32_t) { Log("vkCmdWriteTimestamp", c); })); + add("vkCmdExecuteCommands", static_cast([](VkCommandBuffer c, std::uint32_t, const VkCommandBuffer*) { Log("vkCmdExecuteCommands", c); })); + add("vkCmdBindPipeline", static_cast([](VkCommandBuffer c, VkPipelineBindPoint, VkPipeline) { Log("vkCmdBindPipeline", c); })); + add("vkCmdSetViewport", static_cast([](VkCommandBuffer c, std::uint32_t, std::uint32_t, const VkViewport*) { Log("vkCmdSetViewport", c); })); + add("vkCmdPushConstants", static_cast([](VkCommandBuffer c, VkPipelineLayout, VkShaderStageFlags, std::uint32_t, std::uint32_t, const void*) { Log("vkCmdPushConstants", c); })); + add("vkCmdWriteBufferMarkerAMD", static_cast([](VkCommandBuffer c, VkPipelineStageFlagBits, VkBuffer, VkDeviceSize, std::uint32_t) { Log("vkCmdWriteBufferMarkerAMD", c); })); + add("vkCmdNextSubpass", static_cast([](VkCommandBuffer c, VkSubpassContents) { Log("vkCmdNextSubpass", c); })); + add("vkDestroyBuffer", static_cast([](VkDevice, VkBuffer, const VkAllocationCallbacks*) {})); + return made; + }(); + return functions; +} + +bool splitBarrier = false; + +VKAPI_ATTR void VKAPI_CALL otherBarrier2(VkCommandBuffer commands, const VkDependencyInfo*) { + Log("other barrier2", commands); +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mockProc(VkDevice, const char* name) { + if (splitBarrier && std::strcmp(name, "vkCmdPipelineBarrier2KHR") == 0) return reinterpret_cast(&otherBarrier2); + if (std::strcmp(name, "vkCmdBeginRendering") == 0) name = "vkCmdBeginRenderingKHR"; + if (std::strcmp(name, "vkCmdEndRendering") == 0) name = "vkCmdEndRenderingKHR"; + if (std::strcmp(name, "vkCmdPipelineBarrier2KHR") == 0) name = "vkCmdPipelineBarrier2"; + const auto found = Functions().find(name); + return found == Functions().end() ? nullptr : found->second; +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL otherProc(VkDevice device, const char* name) { + return mockProc(device, name); +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL noCheckpointProc(VkDevice device, const char* name) { + if (std::strcmp(name, "vkCmdSetCheckpointNV") == 0) return nullptr; + return mockProc(device, name); +} + +template +TFunction Resolve(PFN_vkGetDeviceProcAddr proc, const char* name) { + return reinterpret_cast(proc(Device(), name)); +} + +AgcDriver::FaultToken TokenOf(const Event& event) { + return static_cast(event.marker); +} + +CheckpointKind KindOf(const Event& event) { + return AgcDriver::CheckpointKindOf(reinterpret_cast(event.marker)); +} + +void CheckCommand(PFN_vkGetDeviceProcAddr proc, const char* name, CheckpointKind kind, void (*call)(PFN_vkVoidFunction function, VkCommandBuffer commands), const char* logged = nullptr) { + const auto wrapper = proc(Device(), name); + Expect(wrapper != nullptr && wrapper != Functions()[name == std::string("vkCmdBeginRendering") ? "vkCmdBeginRenderingKHR" : name == std::string("vkCmdEndRendering") ? "vkCmdEndRenderingKHR" : name == std::string("vkCmdPipelineBarrier2KHR") ? "vkCmdPipelineBarrier2" : name], std::string(name) + " was not wrapped"); + if (wrapper == nullptr) return; + events.clear(); + call(wrapper, Commands(0x10)); + const std::string real = logged != nullptr ? logged : name; + Expect(events.size() == 2 && events[0].what == "checkpoint" && events[1].what == real, std::string(name) + " did not record a checkpoint and then itself"); + if (events.size() == 2 && events[0].what == "checkpoint") { + Expect(KindOf(events[0]) == kind, std::string(name) + " recorded the kind " + AgcDriver::CheckpointKindName(KindOf(events[0]))); + Expect(events[0].commands == Commands(0x10), std::string(name) + " recorded its checkpoint into another command buffer"); + } +} + +template +TFunction As(PFN_vkVoidFunction function) { + return reinterpret_cast(function); +} + +void EveryCommandRecordsItsKind(PFN_vkGetDeviceProcAddr proc) { + CheckCommand(proc, "vkCmdDraw", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 3, 1, 0, 0); }); + CheckCommand(proc, "vkCmdDrawIndexed", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 3, 1, 0, 0, 0); }); + CheckCommand(proc, "vkCmdDrawIndirect", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1, 16); }); + CheckCommand(proc, "vkCmdDrawIndexedIndirect", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1, 20); }); + CheckCommand(proc, "vkCmdDrawIndirectCountKHR", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, VK_NULL_HANDLE, 0, 1, 16); }); + CheckCommand(proc, "vkCmdDrawIndexedIndirectCountKHR", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, VK_NULL_HANDLE, 0, 1, 20); }); + CheckCommand(proc, "vkCmdDrawMeshTasksEXT", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 1, 1, 1); }); + CheckCommand(proc, "vkCmdDrawMeshTasksIndirectEXT", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1, 12); }); + CheckCommand(proc, "vkCmdDispatch", CheckpointKind::dispatch, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 1, 1, 1); }); + CheckCommand(proc, "vkCmdDispatchBase", CheckpointKind::dispatch, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 4, 0, 0, 1, 1, 1); }); + CheckCommand(proc, "vkCmdDispatchIndirect", CheckpointKind::dispatch, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0); }); + CheckCommand(proc, "vkCmdCopyBuffer", CheckpointKind::copy, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_NULL_HANDLE, 0, nullptr); }); + CheckCommand(proc, "vkCmdCopyImage", CheckpointKind::copy, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, 0, nullptr); }); + CheckCommand(proc, "vkCmdCopyBufferToImage", CheckpointKind::copy, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, 0, nullptr); }); + CheckCommand(proc, "vkCmdCopyImageToBuffer", CheckpointKind::copy, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, VK_NULL_HANDLE, 0, nullptr); }); + CheckCommand(proc, "vkCmdBlitImage", CheckpointKind::blit, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, 0, nullptr, VK_FILTER_NEAREST); }); + CheckCommand(proc, "vkCmdResolveImage", CheckpointKind::blit, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, 0, nullptr); }); + CheckCommand(proc, "vkCmdClearColorImage", CheckpointKind::clear, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, nullptr, 0, nullptr); }); + CheckCommand(proc, "vkCmdClearDepthStencilImage", CheckpointKind::clear, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, nullptr, 0, nullptr); }); + CheckCommand(proc, "vkCmdClearAttachments", CheckpointKind::clear, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, nullptr, 0, nullptr); }); + CheckCommand(proc, "vkCmdFillBuffer", CheckpointKind::fill, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 4, 0); }); + CheckCommand(proc, "vkCmdUpdateBuffer", CheckpointKind::update, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 4, nullptr); }); + CheckCommand(proc, "vkCmdPipelineBarrier", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); }); + CheckCommand(proc, "vkCmdPipelineBarrier2", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }); + CheckCommand(proc, "vkCmdPipelineBarrier2KHR", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }, "vkCmdPipelineBarrier2"); + CheckCommand(proc, "vkCmdBeginRenderPass", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr, VK_SUBPASS_CONTENTS_INLINE); }); + CheckCommand(proc, "vkCmdEndRenderPass", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }); + CheckCommand(proc, "vkCmdBeginRendering", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }); + CheckCommand(proc, "vkCmdBeginRenderingKHR", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }, "vkCmdBeginRendering"); + CheckCommand(proc, "vkCmdEndRendering", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }); + CheckCommand(proc, "vkCmdEndRenderingKHR", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }, "vkCmdEndRendering"); + CheckCommand(proc, "vkCmdResetQueryPool", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1); }); + CheckCommand(proc, "vkCmdBeginQuery", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 0); }); + CheckCommand(proc, "vkCmdEndQuery", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0); }); + CheckCommand(proc, "vkCmdCopyQueryPoolResults", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1, VK_NULL_HANDLE, 0, 8, 0); }); + CheckCommand(proc, "vkCmdWriteTimestamp", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_NULL_HANDLE, 0); }); + CheckCommand(proc, "vkCmdExecuteCommands", CheckpointKind::execute, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, nullptr); }); + CheckCommand(proc, "vkEndCommandBuffer", CheckpointKind::commandBufferEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { static_cast(As(f)(c)); }); + const auto begin = Resolve(proc, "vkBeginCommandBuffer"); + events.clear(); + const VkCommandBufferBeginInfo info{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + Expect(begin(Commands(0x10), &info) == VK_SUCCESS, "vkBeginCommandBuffer lost its result"); + Expect(events.size() == 2 && events[0].what == "vkBeginCommandBuffer" && events[1].what == "checkpoint" && KindOf(events[1]) == CheckpointKind::commandBufferBegin, "vkBeginCommandBuffer did not record a checkpoint after beginning"); +} + +void CheckpointsCarryTheGuestToken(PFN_vkGetDeviceProcAddr proc) { + const auto barrier = Resolve(proc, "vkCmdPipelineBarrier"); + const auto dispatch = Resolve(proc, "vkCmdDispatch"); + const auto begin = Resolve(proc, "vkBeginCommandBuffer"); + events.clear(); + AgcDriver::Graphics::NoteRecordedCheckpoint(Commands(0x20), 77); + dispatch(Commands(0x20), 32, 1, 1); + barrier(Commands(0x20), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); + barrier(Commands(0x30), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); + Expect(events.size() == 6, "the token checks did not record three checkpoints and three commands"); + if (events.size() == 6) { + Expect(KindOf(events[0]) == CheckpointKind::dispatch && TokenOf(events[0]) == 77, "a dispatch after a guest token does not carry it"); + Expect(KindOf(events[2]) == CheckpointKind::barrier && TokenOf(events[2]) == 77, "a barrier after a guest token does not carry it"); + Expect(TokenOf(events[4]) == 0, "a checkpoint on another command buffer carries its guest token"); + } + const VkCommandBufferBeginInfo info{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + static_cast(begin(Commands(0x20), &info)); + events.clear(); + barrier(Commands(0x20), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); + Expect(!events.empty() && TokenOf(events[0]) == 0, "a guest token outlived vkBeginCommandBuffer of its command buffer"); +} + +void RecorderNotesItsToken(PFN_vkGetDeviceProcAddr proc) { + AgcDriver::Graphics::Context context{}; + context.device = Device(); + context.queue = reinterpret_cast(static_cast(0x900)); + context.deviceProc = proc; + events.clear(); + AgcDriver::Graphics::WriteNvCheckpointToken(context, Commands(0x50), 4242, 0); + Expect(events.size() == 1 && events[0].what == "checkpoint" && TokenOf(events[0]) == 4242 && KindOf(events[0]) == CheckpointKind::none, "the recorder did not write its guest token"); + Resolve(proc, "vkCmdFillBuffer")(Commands(0x50), VK_NULL_HANDLE, 0, 4, 0); + Expect(events.size() == 3 && TokenOf(events[1]) == 4242 && KindOf(events[1]) == CheckpointKind::fill, "a command after the recorder's guest token does not carry it"); +} + +void PassesStateThroughAndRejectsUnknownCommands(PFN_vkGetDeviceProcAddr proc) { + for (const char* name : {"vkCmdBindPipeline", "vkCmdSetViewport", "vkCmdPushConstants", "vkCmdWriteBufferMarkerAMD", "vkCmdSetCheckpointNV", "vkDestroyBuffer"}) { + Expect(proc(Device(), name) == Functions()[name], std::string(name) + " did not pass through"); + } + Expect(proc(Device(), "vkCmdTraceRaysKHR") == nullptr, "a command the device lacks did not resolve to null"); + bool threw = false; + try { + static_cast(proc(Device(), "vkCmdNextSubpass")); + } catch (const std::runtime_error&) { + threw = true; + } + Expect(threw, "an unknown work command passed through without a checkpoint"); +} + +void RejectsMismatchedSetups() { + bool threw = false; + try { + static_cast(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &noCheckpointProc)); + } catch (const std::runtime_error&) { + threw = true; + } + Expect(threw, "command checkpoints were set up without vkCmdSetCheckpointNV"); + threw = false; + try { + static_cast(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &otherProc)); + } catch (const std::runtime_error&) { + threw = true; + } + Expect(threw, "a second, different device resolver was accepted"); + Expect(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &mockProc) != nullptr, "the same resolver was refused on a second device"); +} + +void RejectsTwoFunctionsForOneCommand(PFN_vkGetDeviceProcAddr proc) { + static_cast(proc(Device(), "vkCmdPipelineBarrier2")); + Expect(proc(Device(), "vkCmdPipelineBarrier2") != nullptr, "resolving one command twice was refused"); + splitBarrier = true; + bool threw = false; + try { + static_cast(proc(Device(), "vkCmdPipelineBarrier2KHR")); + } catch (const std::runtime_error&) { + threw = true; + } + splitBarrier = false; + Expect(threw, "an alias resolving to another function shared the command's slot"); +} + +} + +int main() { + try { + const auto proc = AgcDriver::Graphics::CheckpointCommandKinds(Device(), &mockProc); + Expect(proc != nullptr && proc != &mockProc, "command checkpoints did not return their own resolver"); + EveryCommandRecordsItsKind(proc); + CheckpointsCarryTheGuestToken(proc); + RecorderNotesItsToken(proc); + PassesStateThroughAndRejectsUnknownCommands(proc); + RejectsMismatchedSetups(); + RejectsTwoFunctionsForOneCommand(proc); + } catch (const std::exception& error) { + std::fprintf(stderr, "agc_command_checkpoints: %s\n", error.what()); + return 1; + } + if (failures != 0) return 1; + std::puts("agc_command_checkpoints: passed"); + return 0; +} diff --git a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp index 8ef1d6f9bba1..c4374cb541c4 100644 --- a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp @@ -406,6 +406,77 @@ void BatchBeginCheckpoint() { checkpointToken = 0; } +const void* kindMarker = nullptr; + +VKAPI_ATTR void VKAPI_CALL MockKindCheckpointData(VkQueue, std::uint32_t* count, VkCheckpointDataNV* data) { + if (data == nullptr) { + *count = 1; + return; + } + Require(*count == 1, "the kind checkpoint array has the wrong size"); + data[0].stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; + data[0].pCheckpointMarker = const_cast(kindMarker); +} + +void CheckpointKindMarkers() { + for (std::uint32_t kind = 0; kind < static_cast(AgcDriver::CheckpointKind::count); ++kind) { + const auto marker = AgcDriver::CheckpointMarker(0xFFFFFFFFu, static_cast(kind)); + Require(static_cast(marker) == 0xFFFFFFFFu, "a command kind changed the marker's token"); + Require(AgcDriver::CheckpointKindOf(reinterpret_cast(marker)) == static_cast(kind), "a command kind did not survive its marker"); + } + Require(AgcDriver::CheckpointKindOf(reinterpret_cast(static_cast(0x1234u))) == AgcDriver::CheckpointKind::none, "a plain token marker decoded a command kind"); + const auto outOfRange = static_cast(0xFEu) << AgcDriver::CheckpointKindShift; + Require(AgcDriver::CheckpointKindOf(reinterpret_cast(outOfRange)) == AgcDriver::CheckpointKind::none, "an unknown command kind was not decoded as none"); + const auto highNibble = static_cast(0x13u) << AgcDriver::CheckpointKindShift; + Require(AgcDriver::CheckpointKindOf(reinterpret_cast(highNibble)) == AgcDriver::CheckpointKind::none, "a kind byte above the known kinds was decoded from its low bits"); + Require(AgcDriver::CheckpointMarker(0x1234u, AgcDriver::CheckpointKind::copy) == (static_cast(0x1234u) | (static_cast(3u) << 40u)), "the copy kind is not stored from bit 40 of the marker"); + const char* const names[] = {"none", "draw", "dispatch", "copy", "blit", "clear", "fill", "update", "barrier", "render pass begin", "render pass end", "query", "execute commands", "command buffer begin", "command buffer end"}; + static_assert(sizeof(names) / sizeof(names[0]) == static_cast(AgcDriver::CheckpointKind::count)); + for (std::uint32_t kind = 0; kind < static_cast(AgcDriver::CheckpointKind::count); ++kind) { + Require(std::strcmp(AgcDriver::CheckpointKindName(static_cast(kind)), names[kind]) == 0, "a command kind lost its name"); + } +} + +void KindCheckpointOutsideRing() { + AgcDriver::FaultTokenRing& ring = AgcDriver::FaultTokenRing::Instance(); + constexpr AgcDriver::FaultToken stale = 0x0FFFFFFFu; + kindMarker = reinterpret_cast(AgcDriver::CheckpointMarker(stale, AgcDriver::CheckpointKind::barrier)); + const auto lines = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring); + Require(lines.size() == 1, "a kind checkpoint after a token outside the ring does not report one line"); + char expected[256]; + std::snprintf(expected, sizeof(expected), "[device-fault] checkpoint: barrier after token %u, no longer in the ring, queue 0x2000 at stage 0x1", static_cast(stale)); + Require(lines[0] == expected, "a kind checkpoint after a token outside the ring did not name its command kind"); + const auto staged = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring, static_cast(nullptr)); + Require(staged.size() == 1 && staged[0] == expected, "a kind checkpoint after a token outside the ring did not name its command kind in the residency report"); + kindMarker = reinterpret_cast(AgcDriver::CheckpointMarker(0, AgcDriver::CheckpointKind::copy)); + const auto first = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring); + const std::string none = "[device-fault] checkpoint: copy, no guest token before it on its command buffer, queue 0x2000 at stage 0x1"; + Require(first.size() == 1 && first[0] == none, "a kind checkpoint without a guest token before it changed its line"); + const auto firstStaged = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring, static_cast(nullptr)); + Require(firstStaged.size() == 1 && firstStaged[0] == none, "a kind checkpoint without a guest token before it changed its line in the residency report"); + kindMarker = reinterpret_cast(static_cast(stale)); + const auto unknown = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring); + std::snprintf(expected, sizeof(expected), "[device-fault] checkpoint: unknown token %u queue 0x2000 at stage 0x1", static_cast(stale)); + Require(unknown.size() == 1 && unknown[0] == expected, "a plain token outside the ring changed its line"); + kindMarker = nullptr; +} + +void KindCheckpointInRing() { + AgcDriver::FaultTokenRing& ring = AgcDriver::FaultTokenRing::Instance(); + const AgcDriver::FaultToken token = ring.Claim(); + Require(token != 0, "the kind checkpoint token is invalid"); + ClaimBracketToken(ring, token, 11, 0x5003e9700ull, 9, "dispatch 32x1x1"); + kindMarker = reinterpret_cast(AgcDriver::CheckpointMarker(token, AgcDriver::CheckpointKind::barrier)); + const auto lines = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring); + Require(lines.size() == 1, "a kind checkpoint after a token in the ring does not report one line"); + char expected[256]; + std::snprintf(expected, sizeof(expected), "[device-fault] checkpoint: barrier after token %u batch 11 queue 0x2000 program 0x5003e9700 variant 9 dispatch 32x1x1 at stage 0x1", static_cast(token)); + Require(lines[0] == expected, "a kind checkpoint after a token in the ring lost the guest program"); + const auto staged = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring, static_cast(nullptr)); + Require(staged.size() == 1 && staged[0].rfind("[device-fault] checkpoint: barrier after token ", 0) == 0 && staged[0].find("program 0x5003e9700 variant 9 stage ") != std::string::npos, "a kind checkpoint after a token in the ring lost the guest program in the residency report"); + kindMarker = nullptr; +} + void ReportBracketOnly() { AgcDriver::FaultTokenRing& ring = AgcDriver::FaultTokenRing::Instance(); const AgcDriver::SubmitBracket bracket{7, 5, 0, 0}; @@ -764,6 +835,9 @@ int main() { EffectiveSerialZeroHasNoMarker(); TokenCheckpoint(); BatchBeginCheckpoint(); + CheckpointKindMarkers(); + KindCheckpointOutsideRing(); + KindCheckpointInRing(); ReportBracketOnly(); ExtraQueueEmptyBracket(); TokenReuse(); diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake new file mode 100644 index 000000000000..60d7e405f272 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake @@ -0,0 +1,15 @@ +add_test_executable(agc_command_checkpoints_tests EXCLUDE_FROM_ALL tests/CommandCheckpoints.cpp) +target_include_directories(agc_command_checkpoints_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/core/shader/recompiler ${ANYPS5_VULKAN_HEADERS_INCLUDE_DIR}) +target_link_libraries(agc_command_checkpoints_tests PRIVATE libSceAgcDriver libc) +set_target_properties(agc_command_checkpoints_tests PROPERTIES CXX_EXTENSIONS OFF) +configure_windows_unwind(agc_command_checkpoints_tests) + +if(BUILD_TESTING) + set_target_properties(agc_command_checkpoints_tests PROPERTIES EXCLUDE_FROM_ALL OFF) + add_test(NAME agc_command_checkpoints COMMAND agc_command_checkpoints_tests) + set_tests_properties(agc_command_checkpoints PROPERTIES ENVIRONMENT "APS5_GPU_CHECKPOINTS=1") + if(WIN32) + set_tests_properties(agc_command_checkpoints PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/docs/dev/BUILD.md b/docs/dev/BUILD.md index dc7ebaaad006..6bd3b8e54f92 100644 --- a/docs/dev/BUILD.md +++ b/docs/dev/BUILD.md @@ -402,7 +402,7 @@ Set `APS5_TRACE_MEMORY_BUDGET=1` to track Vulkan allocations by device, heap and ## Device loss reports -On a device that supports `VK_EXT_device_fault`, the first `VK_ERROR_DEVICE_LOST` prints the driver's fault description, the faulting addresses and the vendor fault codes as `[device-fault]` lines. Set `APS5_GPU_CHECKPOINTS=1` to also record a `VK_NV_device_diagnostic_checkpoints` marker with the program address before each compute dispatch; the report then lists the last markers the GPU reached. On a device without that extension but with `VK_AMD_buffer_marker` (RADV and AMD's own drivers), each compute dispatch instead writes a numbered marker into a host-visible buffer when the GPU reaches it and another when it finishes; the report then names the programs of the last dispatch reached, of the last one finished and of up to eight dispatches between them. Checkpoints are disabled by default and ignored on devices without the extension. +On a device that supports `VK_EXT_device_fault`, the first `VK_ERROR_DEVICE_LOST` prints the driver's fault description, the faulting addresses and the vendor fault codes as `[device-fault]` lines. Set `APS5_GPU_CHECKPOINTS=1` to also record a `VK_NV_device_diagnostic_checkpoints` marker with the program address before each compute dispatch and guest draw, and, before every other command the driver records and after each command buffer begins, a marker naming the command kind (draw, dispatch, copy, blit, clear, fill, update, barrier, render pass begin or end, query, execute commands, command buffer begin or end) and the last guest dispatch or draw of its command buffer; the report then lists the last markers the GPU reached, with the kind and the guest program of each. On a device without that extension but with `VK_AMD_buffer_marker` (RADV and AMD's own drivers), each compute dispatch instead writes a numbered marker into a host-visible buffer when the GPU reaches it and another when it finishes; the report then names the programs of the last dispatch reached, of the last one finished and of up to eight dispatches between them. Checkpoints are disabled by default and ignored on devices without the extension. ## Worker sampler diff --git a/docs/dev/ENVIRONMENT.md b/docs/dev/ENVIRONMENT.md index 8b4c5aa192cf..6f4f58ee851d 100644 --- a/docs/dev/ENVIRONMENT.md +++ b/docs/dev/ENVIRONMENT.md @@ -248,7 +248,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_FRAME_CAPTURE_TRIGGER` | string | `core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp` | | file whose appearance starts the frame capture at the next frame boundary at which the driver is idle; a number in it replaces the frame count. While it waits, `[frame-capture] waiting to start the capture` is logged at the first and every 50th boundary, and the capture gives up after 600 | | `APS5_FRAME_REPLAY_ATTEMPT` | integer | `core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp` | 0 | number of times `agc_frame_replay` has started itself again on Windows because a host region sat in a captured GPU-visible range; set by the replay for the process it starts, which stops relaunching after 8 | | `APS5_FULL_BARRIERS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | records every barrier instead of merging redundant ones | -| `APS5_GPU_CHECKPOINTS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | attaches device diagnostic checkpoints through VK_NV_device_diagnostic_checkpoints, or buffer markers through VK_AMD_buffer_marker on a device without it | +| `APS5_GPU_CHECKPOINTS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | attaches device diagnostic checkpoints through VK_NV_device_diagnostic_checkpoints, before each dispatch and draw, and before every other recorded command with its kind and the last guest dispatch or draw, or buffer markers through VK_AMD_buffer_marker on a device without it | | `APS5_GPU_CHECKPOINT_BATCHES` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | with `APS5_GPU_CHECKPOINTS`, also records a checkpoint token at the start of every queue-0 command batch, so a device loss before the first draw or dispatch names the batch; without `APS5_GPU_CHECKPOINTS` it does nothing | | `APS5_GPU_COPY_MAX_KIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` | 1024 | size cap in KiB on a descriptor sub-range copied through the GPU path, larger ones staying on the CPU unless a pending GPU write, pending storage results or a unit shadow overlaps them (the GPU then copies them whatever their size, so the CPU does not wait) | | `APS5_GPU_NO_WRITEBACK` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | | skips writing GPU results back into guest memory | diff --git a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md new file mode 100644 index 000000000000..1117b906d8a1 --- /dev/null +++ b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md @@ -0,0 +1,9 @@ +# libSceAgcDriver + +## change [Device-lost fault attribution] + +- [Device-lost fault attribution](../../../core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp) (libSceAgcDriver): a device loss prints the faulting VA with its guest snapshot (owner or neighbours), a bracket window (last completed, in flight, first not started, last 8 tokens) and draw/dispatch/mesh tokens (serial, program, variant, stage, desc). Indirect draws/dispatches record buffer VA, offset, stride and up to 4 record-time words (unsynced read, never submits); mesh sets the stage. Verified-copy Find; clear-first Claim (reuse-tested). Residency today: never bound (faulting submit snapshot found and uncovered) else unknown; evicted/import/refused lines have no producer yet. Serial at marker offset 0, tokens at 4. Needs APS5_GPU_CHECKPOINTS. Cannot name T# or pending write-back; no task token path yet. Vendor dump via APS5_DUMP_DEVICE_FAULT where supported. Opened for PPSA01342 (#10634): second-world load faults a write. + +## add Functional + +- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, or a replay on another thread, loses that token. NV checkpoint data for the main queue was empty after most resets seen on an RTX 5070 Ti (621.24), so a hang may name no main-queue command. From e0925a7e5c9f94f99bf5940784e4d548971e2bc6 Mon Sep 17 00:00:00 2001 From: videogamer10293 <347855792+videogamer10293@users.noreply.github.com> Date: Sat, 21 Nov 2026 03:39:41 +0000 Subject: [PATCH 2/6] test(agc): guard command checkpoint setup checks Cover a wrapped command the device lacks, a missing vkCmdSetCheckpointNV with the same resolver, and a second, different vkCmdSetCheckpointNV. Co-Authored-By: Claude Opus 5.5 --- .../tests/CommandCheckpoints.cpp | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp index 911a36daecfb..96887631e150 100644 --- a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp @@ -101,12 +101,19 @@ std::map& Functions() { } bool splitBarrier = false; +bool otherCheckpoint = false; +const char* missing = nullptr; VKAPI_ATTR void VKAPI_CALL otherBarrier2(VkCommandBuffer commands, const VkDependencyInfo*) { Log("other barrier2", commands); } +VKAPI_ATTR void VKAPI_CALL otherSetCheckpoint(VkCommandBuffer, const void*) { +} + VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mockProc(VkDevice, const char* name) { + if (missing != nullptr && std::strcmp(name, missing) == 0) return nullptr; + if (otherCheckpoint && std::strcmp(name, "vkCmdSetCheckpointNV") == 0) return reinterpret_cast(&otherSetCheckpoint); if (splitBarrier && std::strcmp(name, "vkCmdPipelineBarrier2KHR") == 0) return reinterpret_cast(&otherBarrier2); if (std::strcmp(name, "vkCmdBeginRendering") == 0) name = "vkCmdBeginRenderingKHR"; if (std::strcmp(name, "vkCmdEndRendering") == 0) name = "vkCmdEndRenderingKHR"; @@ -119,11 +126,6 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL otherProc(VkDevice device, const char* return mockProc(device, name); } -VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL noCheckpointProc(VkDevice device, const char* name) { - if (std::strcmp(name, "vkCmdSetCheckpointNV") == 0) return nullptr; - return mockProc(device, name); -} - template TFunction Resolve(PFN_vkGetDeviceProcAddr proc, const char* name) { return reinterpret_cast(proc(Device(), name)); @@ -241,6 +243,9 @@ void PassesStateThroughAndRejectsUnknownCommands(PFN_vkGetDeviceProcAddr proc) { Expect(proc(Device(), name) == Functions()[name], std::string(name) + " did not pass through"); } Expect(proc(Device(), "vkCmdTraceRaysKHR") == nullptr, "a command the device lacks did not resolve to null"); + missing = "vkCmdDraw"; + Expect(proc(Device(), "vkCmdDraw") == nullptr, "a wrapped command the device lacks did not resolve to null"); + missing = nullptr; bool threw = false; try { static_cast(proc(Device(), "vkCmdNextSubpass")); @@ -250,15 +255,23 @@ void PassesStateThroughAndRejectsUnknownCommands(PFN_vkGetDeviceProcAddr proc) { Expect(threw, "an unknown work command passed through without a checkpoint"); } -void RejectsMismatchedSetups() { - bool threw = false; +bool ThrowsMentioning(PFN_vkGetDeviceProcAddr resolver, const char* text) { try { - static_cast(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &noCheckpointProc)); - } catch (const std::runtime_error&) { - threw = true; + static_cast(AgcDriver::Graphics::CheckpointCommandKinds(Device(), resolver)); + } catch (const std::runtime_error& error) { + return std::string(error.what()).find(text) != std::string::npos; } - Expect(threw, "command checkpoints were set up without vkCmdSetCheckpointNV"); - threw = false; + return false; +} + +void RejectsMismatchedSetups() { + missing = "vkCmdSetCheckpointNV"; + Expect(ThrowsMentioning(&mockProc, "missing Vulkan function: vkCmdSetCheckpointNV"), "command checkpoints were set up without vkCmdSetCheckpointNV"); + missing = nullptr; + otherCheckpoint = true; + Expect(ThrowsMentioning(&mockProc, "two different vkCmdSetCheckpointNV"), "a second, different vkCmdSetCheckpointNV was accepted"); + otherCheckpoint = false; + bool threw = false; try { static_cast(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &otherProc)); } catch (const std::runtime_error&) { From 53c3844051564316ba256879ada1ae35aa78a4e4 Mon Sep 17 00:00:00 2001 From: videogamer10293 <347855792+videogamer10293@users.noreply.github.com> Date: Sat, 21 Nov 2026 06:08:47 +0000 Subject: [PATCH 3/6] feat(agc): checkpoint event commands and tag command kind markers Wrap vkCmdSetEvent, vkCmdResetEvent and vkCmdWaitEvents (and their 2 and KHR forms), which the vkCmdSet state prefix let through, and the core names of the indirect count draws. Give each KHR or core alias its own slot, so a layer that returns separate entry points for them does not throw. Tag kind markers in their top 16 bits so markers the driver inserts never decode as a command kind. Scan the driver sources for vkCmd literals, PFN_vkCmd types and X(Cmd...) entries in the test, so CI fails on a command the checkpoints do not classify. Co-Authored-By: Claude Opus 5.5 --- .../Execution/include/SubmissionSnapshots.hpp | 11 +- .../Graphics/src/CommandCheckpoints.cpp | 94 ++++++++++-- .../tests/CommandCheckpoints.cpp | 139 +++++++++++++++--- .../prx/libSceAgcDriver/tests/DeviceFault.cpp | 10 +- .../cmake/agc_command_checkpoints_tests.cmake | 2 +- docs/dev/BUILD.md | 2 +- .../changes/agc-checkpoint-command-kinds.md | 6 +- 7 files changed, 215 insertions(+), 49 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp index 3643792cb7fb..ea1e2dbfd29e 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/SubmissionSnapshots.hpp @@ -17,15 +17,20 @@ using FaultToken = std::uint32_t; enum class FaultStage : std::uint32_t { unknown = 0, graphics = 1, compute = 2, mesh = 3, task = 4, copy = 5 }; -enum class CheckpointKind : std::uint8_t { none = 0, draw, dispatch, copy, blit, clear, fill, update, barrier, renderPassBegin, renderPassEnd, query, execute, commandBufferBegin, commandBufferEnd, count }; +enum class CheckpointKind : std::uint8_t { none = 0, draw, dispatch, copy, blit, clear, fill, update, barrier, renderPassBegin, renderPassEnd, query, execute, commandBufferBegin, commandBufferEnd, event, wait, count }; + +static_assert(sizeof(std::uintptr_t) == 8, "command checkpoint markers need 64-bit pointers"); inline constexpr unsigned CheckpointKindShift = 40; +inline constexpr unsigned CheckpointTagShift = 48; +inline constexpr std::uintptr_t CheckpointTag = 0xA65C; inline std::uintptr_t CheckpointMarker(FaultToken token, CheckpointKind kind) noexcept { - return static_cast(token) | (static_cast(kind) << CheckpointKindShift); + return static_cast(token) | (static_cast(kind) << CheckpointKindShift) | (CheckpointTag << CheckpointTagShift); } inline CheckpointKind CheckpointKindOf(const void* marker) noexcept { + if ((reinterpret_cast(marker) >> CheckpointTagShift) != CheckpointTag) return CheckpointKind::none; const auto kind = (reinterpret_cast(marker) >> CheckpointKindShift) & 0xffu; return kind < static_cast(CheckpointKind::count) ? static_cast(kind) : CheckpointKind::none; } @@ -46,6 +51,8 @@ inline const char* CheckpointKindName(CheckpointKind kind) noexcept { case CheckpointKind::execute: return "execute commands"; case CheckpointKind::commandBufferBegin: return "command buffer begin"; case CheckpointKind::commandBufferEnd: return "command buffer end"; + case CheckpointKind::event: return "event"; + case CheckpointKind::wait: return "wait events"; default: return "none"; } } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp index 9173230feb59..ea6d45f59d6c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp @@ -74,6 +74,20 @@ enum class Slot : std::size_t { CmdCopyQueryPoolResults, CmdWriteTimestamp, CmdExecuteCommands, + CmdSetEvent, + CmdResetEvent, + CmdWaitEvents, + CmdSetEvent2, + CmdResetEvent2, + CmdWaitEvents2, + CmdDrawIndirectCountCore, + CmdDrawIndexedIndirectCountCore, + CmdPipelineBarrier2KHR, + CmdBeginRenderingKHR, + CmdEndRenderingKHR, + CmdSetEvent2KHR, + CmdResetEvent2KHR, + CmdWaitEvents2KHR, }; template @@ -87,8 +101,9 @@ struct Real { } static void Store(PFN_vkVoidFunction real, const char* name) { const auto function = reinterpret_cast(real); - TFunction expected = nullptr; - if (Holder().compare_exchange_strong(expected, function, std::memory_order_acq_rel) || expected == function) return; + TFunction expected = Holder().load(std::memory_order_acquire); + if (expected == nullptr && Holder().compare_exchange_strong(expected, function, std::memory_order_acq_rel)) return; + if (expected == function) return; throw std::runtime_error(std::string("AGC graphics: command checkpoints got two different functions for ") + name); } }; @@ -126,14 +141,16 @@ VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexedIndirect(VkCommandBuffer comma Real::Get()(commands, buffer, offset, drawCount, stride); } +template VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndirectCount(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer, VkDeviceSize countOffset, std::uint32_t maxDrawCount, std::uint32_t stride) { mark(commands, AgcDriver::CheckpointKind::draw); - Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); + Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); } +template VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexedIndirectCount(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer, VkDeviceSize countOffset, std::uint32_t maxDrawCount, std::uint32_t stride) { mark(commands, AgcDriver::CheckpointKind::draw); - Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); + Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); } VKAPI_ATTR void VKAPI_CALL checkpointedDrawMeshTasks(VkCommandBuffer commands, std::uint32_t x, std::uint32_t y, std::uint32_t z) { @@ -221,9 +238,10 @@ VKAPI_ATTR void VKAPI_CALL checkpointedPipelineBarrier(VkCommandBuffer commands, Real::Get()(commands, source, destination, dependency, memoryCount, memory, bufferCount, buffers, imageCount, images); } +template VKAPI_ATTR void VKAPI_CALL checkpointedPipelineBarrier2(VkCommandBuffer commands, const VkDependencyInfo* dependency) { mark(commands, AgcDriver::CheckpointKind::barrier); - Real::Get()(commands, dependency); + Real::Get()(commands, dependency); } VKAPI_ATTR void VKAPI_CALL checkpointedBeginRenderPass(VkCommandBuffer commands, const VkRenderPassBeginInfo* begin, VkSubpassContents contents) { @@ -236,14 +254,16 @@ VKAPI_ATTR void VKAPI_CALL checkpointedEndRenderPass(VkCommandBuffer commands) { Real::Get()(commands); } +template VKAPI_ATTR void VKAPI_CALL checkpointedBeginRendering(VkCommandBuffer commands, const VkRenderingInfo* info) { mark(commands, AgcDriver::CheckpointKind::renderPassBegin); - Real::Get()(commands, info); + Real::Get()(commands, info); } +template VKAPI_ATTR void VKAPI_CALL checkpointedEndRendering(VkCommandBuffer commands) { mark(commands, AgcDriver::CheckpointKind::renderPassEnd); - Real::Get()(commands); + Real::Get()(commands); } VKAPI_ATTR void VKAPI_CALL checkpointedResetQueryPool(VkCommandBuffer commands, VkQueryPool pool, std::uint32_t first, std::uint32_t count) { @@ -276,6 +296,39 @@ VKAPI_ATTR void VKAPI_CALL checkpointedExecuteCommands(VkCommandBuffer commands, Real::Get()(commands, count, buffers); } +VKAPI_ATTR void VKAPI_CALL checkpointedSetEvent(VkCommandBuffer commands, VkEvent event, VkPipelineStageFlags stage) { + mark(commands, AgcDriver::CheckpointKind::event); + Real::Get()(commands, event, stage); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedResetEvent(VkCommandBuffer commands, VkEvent event, VkPipelineStageFlags stage) { + mark(commands, AgcDriver::CheckpointKind::event); + Real::Get()(commands, event, stage); +} + +VKAPI_ATTR void VKAPI_CALL checkpointedWaitEvents(VkCommandBuffer commands, std::uint32_t eventCount, const VkEvent* events, VkPipelineStageFlags source, VkPipelineStageFlags destination, std::uint32_t memoryCount, const VkMemoryBarrier* memory, std::uint32_t bufferCount, const VkBufferMemoryBarrier* buffers, std::uint32_t imageCount, const VkImageMemoryBarrier* images) { + mark(commands, AgcDriver::CheckpointKind::wait); + Real::Get()(commands, eventCount, events, source, destination, memoryCount, memory, bufferCount, buffers, imageCount, images); +} + +template +VKAPI_ATTR void VKAPI_CALL checkpointedSetEvent2(VkCommandBuffer commands, VkEvent event, const VkDependencyInfo* dependency) { + mark(commands, AgcDriver::CheckpointKind::event); + Real::Get()(commands, event, dependency); +} + +template +VKAPI_ATTR void VKAPI_CALL checkpointedResetEvent2(VkCommandBuffer commands, VkEvent event, VkPipelineStageFlags2 stage) { + mark(commands, AgcDriver::CheckpointKind::event); + Real::Get()(commands, event, stage); +} + +template +VKAPI_ATTR void VKAPI_CALL checkpointedWaitEvents2(VkCommandBuffer commands, std::uint32_t eventCount, const VkEvent* events, const VkDependencyInfo* dependencies) { + mark(commands, AgcDriver::CheckpointKind::wait); + Real::Get()(commands, eventCount, events, dependencies); +} + struct Hook { const char* name; void (*store)(PFN_vkVoidFunction real, const char* name); @@ -295,8 +348,10 @@ const Hook* findHook(const char* name) noexcept { hook("vkCmdDrawIndexed", &checkpointedDrawIndexed), hook("vkCmdDrawIndirect", &checkpointedDrawIndirect), hook("vkCmdDrawIndexedIndirect", &checkpointedDrawIndexedIndirect), - hook("vkCmdDrawIndirectCountKHR", &checkpointedDrawIndirectCount), - hook("vkCmdDrawIndexedIndirectCountKHR", &checkpointedDrawIndexedIndirectCount), + hook("vkCmdDrawIndirectCountKHR", &checkpointedDrawIndirectCount), + hook("vkCmdDrawIndirectCount", &checkpointedDrawIndirectCount), + hook("vkCmdDrawIndexedIndirectCountKHR", &checkpointedDrawIndexedIndirectCount), + hook("vkCmdDrawIndexedIndirectCount", &checkpointedDrawIndexedIndirectCount), hook("vkCmdDrawMeshTasksEXT", &checkpointedDrawMeshTasks), hook("vkCmdDrawMeshTasksIndirectEXT", &checkpointedDrawMeshTasksIndirect), hook("vkCmdDispatch", &checkpointedDispatch), @@ -314,20 +369,29 @@ const Hook* findHook(const char* name) noexcept { hook("vkCmdFillBuffer", &checkpointedFillBuffer), hook("vkCmdUpdateBuffer", &checkpointedUpdateBuffer), hook("vkCmdPipelineBarrier", &checkpointedPipelineBarrier), - hook("vkCmdPipelineBarrier2", &checkpointedPipelineBarrier2), - hook("vkCmdPipelineBarrier2KHR", &checkpointedPipelineBarrier2), + hook("vkCmdPipelineBarrier2", &checkpointedPipelineBarrier2), + hook("vkCmdPipelineBarrier2KHR", &checkpointedPipelineBarrier2), hook("vkCmdBeginRenderPass", &checkpointedBeginRenderPass), hook("vkCmdEndRenderPass", &checkpointedEndRenderPass), - hook("vkCmdBeginRendering", &checkpointedBeginRendering), - hook("vkCmdBeginRenderingKHR", &checkpointedBeginRendering), - hook("vkCmdEndRendering", &checkpointedEndRendering), - hook("vkCmdEndRenderingKHR", &checkpointedEndRendering), + hook("vkCmdBeginRendering", &checkpointedBeginRendering), + hook("vkCmdBeginRenderingKHR", &checkpointedBeginRendering), + hook("vkCmdEndRendering", &checkpointedEndRendering), + hook("vkCmdEndRenderingKHR", &checkpointedEndRendering), hook("vkCmdResetQueryPool", &checkpointedResetQueryPool), hook("vkCmdBeginQuery", &checkpointedBeginQuery), hook("vkCmdEndQuery", &checkpointedEndQuery), hook("vkCmdCopyQueryPoolResults", &checkpointedCopyQueryPoolResults), hook("vkCmdWriteTimestamp", &checkpointedWriteTimestamp), hook("vkCmdExecuteCommands", &checkpointedExecuteCommands), + hook("vkCmdSetEvent", &checkpointedSetEvent), + hook("vkCmdResetEvent", &checkpointedResetEvent), + hook("vkCmdWaitEvents", &checkpointedWaitEvents), + hook("vkCmdSetEvent2", &checkpointedSetEvent2), + hook("vkCmdSetEvent2KHR", &checkpointedSetEvent2), + hook("vkCmdResetEvent2", &checkpointedResetEvent2), + hook("vkCmdResetEvent2KHR", &checkpointedResetEvent2), + hook("vkCmdWaitEvents2", &checkpointedWaitEvents2), + hook("vkCmdWaitEvents2KHR", &checkpointedWaitEvents2), }; for (const auto& entry : hooks) { if (std::strcmp(name, entry.name) == 0) return &entry; diff --git a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp index 96887631e150..9be539ab3f00 100644 --- a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp @@ -3,7 +3,12 @@ #include #include #include +#include +#include #include +#include +#include +#include #include #include #include @@ -94,32 +99,66 @@ std::map& Functions() { add("vkCmdPushConstants", static_cast([](VkCommandBuffer c, VkPipelineLayout, VkShaderStageFlags, std::uint32_t, std::uint32_t, const void*) { Log("vkCmdPushConstants", c); })); add("vkCmdWriteBufferMarkerAMD", static_cast([](VkCommandBuffer c, VkPipelineStageFlagBits, VkBuffer, VkDeviceSize, std::uint32_t) { Log("vkCmdWriteBufferMarkerAMD", c); })); add("vkCmdNextSubpass", static_cast([](VkCommandBuffer c, VkSubpassContents) { Log("vkCmdNextSubpass", c); })); + add("vkCmdSetEvent", static_cast([](VkCommandBuffer c, VkEvent, VkPipelineStageFlags) { Log("vkCmdSetEvent", c); })); + add("vkCmdResetEvent", static_cast([](VkCommandBuffer c, VkEvent, VkPipelineStageFlags) { Log("vkCmdResetEvent", c); })); + add("vkCmdWaitEvents", static_cast([](VkCommandBuffer c, std::uint32_t, const VkEvent*, VkPipelineStageFlags, VkPipelineStageFlags, std::uint32_t, const VkMemoryBarrier*, std::uint32_t, const VkBufferMemoryBarrier*, std::uint32_t, const VkImageMemoryBarrier*) { Log("vkCmdWaitEvents", c); })); + add("vkCmdSetEvent2", static_cast([](VkCommandBuffer c, VkEvent, const VkDependencyInfo*) { Log("vkCmdSetEvent2", c); })); + add("vkCmdResetEvent2", static_cast([](VkCommandBuffer c, VkEvent, VkPipelineStageFlags2) { Log("vkCmdResetEvent2", c); })); + add("vkCmdWaitEvents2", static_cast([](VkCommandBuffer c, std::uint32_t, const VkEvent*, const VkDependencyInfo*) { Log("vkCmdWaitEvents2", c); })); add("vkDestroyBuffer", static_cast([](VkDevice, VkBuffer, const VkAllocationCallbacks*) {})); return made; }(); return functions; } -bool splitBarrier = false; +std::map splits; +bool anyCommand = false; bool otherCheckpoint = false; const char* missing = nullptr; -VKAPI_ATTR void VKAPI_CALL otherBarrier2(VkCommandBuffer commands, const VkDependencyInfo*) { - Log("other barrier2", commands); +template +struct Other; + +template +struct Other { + static VKAPI_ATTR void VKAPI_CALL Call(VkCommandBuffer commands, TArguments...) { + Log("other", commands); + } +}; + +template +PFN_vkVoidFunction OtherFunction() { + return reinterpret_cast(&Other::Call); } VKAPI_ATTR void VKAPI_CALL otherSetCheckpoint(VkCommandBuffer, const void*) { } +const char* Canonical(const char* name) { + static const std::map aliases = { + {"vkCmdDrawIndirectCount", "vkCmdDrawIndirectCountKHR"}, + {"vkCmdDrawIndexedIndirectCount", "vkCmdDrawIndexedIndirectCountKHR"}, + {"vkCmdBeginRendering", "vkCmdBeginRenderingKHR"}, + {"vkCmdEndRendering", "vkCmdEndRenderingKHR"}, + {"vkCmdPipelineBarrier2KHR", "vkCmdPipelineBarrier2"}, + {"vkCmdSetEvent2KHR", "vkCmdSetEvent2"}, + {"vkCmdResetEvent2KHR", "vkCmdResetEvent2"}, + {"vkCmdWaitEvents2KHR", "vkCmdWaitEvents2"}, + }; + const auto found = aliases.find(name); + return found == aliases.end() ? name : found->second; +} + +VKAPI_ATTR void VKAPI_CALL anySourceCommand() { +} + VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mockProc(VkDevice, const char* name) { if (missing != nullptr && std::strcmp(name, missing) == 0) return nullptr; if (otherCheckpoint && std::strcmp(name, "vkCmdSetCheckpointNV") == 0) return reinterpret_cast(&otherSetCheckpoint); - if (splitBarrier && std::strcmp(name, "vkCmdPipelineBarrier2KHR") == 0) return reinterpret_cast(&otherBarrier2); - if (std::strcmp(name, "vkCmdBeginRendering") == 0) name = "vkCmdBeginRenderingKHR"; - if (std::strcmp(name, "vkCmdEndRendering") == 0) name = "vkCmdEndRenderingKHR"; - if (std::strcmp(name, "vkCmdPipelineBarrier2KHR") == 0) name = "vkCmdPipelineBarrier2"; - const auto found = Functions().find(name); - return found == Functions().end() ? nullptr : found->second; + if (const auto split = splits.find(name); split != splits.end()) return split->second; + const auto found = Functions().find(Canonical(name)); + if (found != Functions().end()) return found->second; + return anyCommand && std::strncmp(name, "vkCmd", 5) == 0 ? reinterpret_cast(&anySourceCommand) : nullptr; } VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL otherProc(VkDevice device, const char* name) { @@ -141,7 +180,7 @@ CheckpointKind KindOf(const Event& event) { void CheckCommand(PFN_vkGetDeviceProcAddr proc, const char* name, CheckpointKind kind, void (*call)(PFN_vkVoidFunction function, VkCommandBuffer commands), const char* logged = nullptr) { const auto wrapper = proc(Device(), name); - Expect(wrapper != nullptr && wrapper != Functions()[name == std::string("vkCmdBeginRendering") ? "vkCmdBeginRenderingKHR" : name == std::string("vkCmdEndRendering") ? "vkCmdEndRenderingKHR" : name == std::string("vkCmdPipelineBarrier2KHR") ? "vkCmdPipelineBarrier2" : name], std::string(name) + " was not wrapped"); + Expect(wrapper != nullptr && wrapper != Functions()[Canonical(name)], std::string(name) + " was not wrapped"); if (wrapper == nullptr) return; events.clear(); call(wrapper, Commands(0x10)); @@ -183,19 +222,22 @@ void EveryCommandRecordsItsKind(PFN_vkGetDeviceProcAddr proc) { CheckCommand(proc, "vkCmdUpdateBuffer", CheckpointKind::update, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 4, nullptr); }); CheckCommand(proc, "vkCmdPipelineBarrier", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); }); CheckCommand(proc, "vkCmdPipelineBarrier2", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }); - CheckCommand(proc, "vkCmdPipelineBarrier2KHR", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }, "vkCmdPipelineBarrier2"); CheckCommand(proc, "vkCmdBeginRenderPass", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr, VK_SUBPASS_CONTENTS_INLINE); }); CheckCommand(proc, "vkCmdEndRenderPass", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }); CheckCommand(proc, "vkCmdBeginRendering", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }); - CheckCommand(proc, "vkCmdBeginRenderingKHR", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }, "vkCmdBeginRendering"); CheckCommand(proc, "vkCmdEndRendering", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }); - CheckCommand(proc, "vkCmdEndRenderingKHR", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }, "vkCmdEndRendering"); CheckCommand(proc, "vkCmdResetQueryPool", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1); }); CheckCommand(proc, "vkCmdBeginQuery", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 0); }); CheckCommand(proc, "vkCmdEndQuery", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0); }); CheckCommand(proc, "vkCmdCopyQueryPoolResults", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, 1, VK_NULL_HANDLE, 0, 8, 0); }); CheckCommand(proc, "vkCmdWriteTimestamp", CheckpointKind::query, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_NULL_HANDLE, 0); }); CheckCommand(proc, "vkCmdExecuteCommands", CheckpointKind::execute, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, nullptr); }); + CheckCommand(proc, "vkCmdSetEvent", CheckpointKind::event, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_PIPELINE_STAGE_TRANSFER_BIT); }); + CheckCommand(proc, "vkCmdResetEvent", CheckpointKind::event, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, VK_PIPELINE_STAGE_TRANSFER_BIT); }); + CheckCommand(proc, "vkCmdWaitEvents", CheckpointKind::wait, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, nullptr, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); }); + CheckCommand(proc, "vkCmdSetEvent2", CheckpointKind::event, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, nullptr); }); + CheckCommand(proc, "vkCmdResetEvent2", CheckpointKind::event, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0); }); + CheckCommand(proc, "vkCmdWaitEvents2", CheckpointKind::wait, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, nullptr, nullptr); }); CheckCommand(proc, "vkEndCommandBuffer", CheckpointKind::commandBufferEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { static_cast(As(f)(c)); }); const auto begin = Resolve(proc, "vkBeginCommandBuffer"); events.clear(); @@ -278,26 +320,78 @@ void RejectsMismatchedSetups() { threw = true; } Expect(threw, "a second, different device resolver was accepted"); - Expect(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &mockProc) != nullptr, "the same resolver was refused on a second device"); + Expect(AgcDriver::Graphics::CheckpointCommandKinds(Device(), &mockProc) != nullptr, "the same resolver was refused when set up again"); } void RejectsTwoFunctionsForOneCommand(PFN_vkGetDeviceProcAddr proc) { - static_cast(proc(Device(), "vkCmdPipelineBarrier2")); - Expect(proc(Device(), "vkCmdPipelineBarrier2") != nullptr, "resolving one command twice was refused"); - splitBarrier = true; + static_cast(proc(Device(), "vkCmdDispatch")); + Expect(proc(Device(), "vkCmdDispatch") != nullptr, "resolving one command twice was refused"); + splits["vkCmdDispatch"] = OtherFunction(); bool threw = false; try { - static_cast(proc(Device(), "vkCmdPipelineBarrier2KHR")); + static_cast(proc(Device(), "vkCmdDispatch")); } catch (const std::runtime_error&) { threw = true; } - splitBarrier = false; - Expect(threw, "an alias resolving to another function shared the command's slot"); + splits.erase("vkCmdDispatch"); + Expect(threw, "a command resolving to a second, different function was accepted"); +} + +void KeepsAliasesApart(PFN_vkGetDeviceProcAddr proc) { + splits["vkCmdDrawIndirectCount"] = OtherFunction(); + splits["vkCmdDrawIndexedIndirectCount"] = OtherFunction(); + splits["vkCmdPipelineBarrier2KHR"] = OtherFunction(); + splits["vkCmdBeginRenderingKHR"] = OtherFunction(); + splits["vkCmdEndRenderingKHR"] = OtherFunction(); + splits["vkCmdSetEvent2KHR"] = OtherFunction(); + splits["vkCmdResetEvent2KHR"] = OtherFunction(); + splits["vkCmdWaitEvents2KHR"] = OtherFunction(); + CheckCommand(proc, "vkCmdDrawIndirectCount", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, VK_NULL_HANDLE, 0, 1, 16); }, "other"); + CheckCommand(proc, "vkCmdDrawIndexedIndirectCount", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, VK_NULL_HANDLE, 0, 1, 20); }, "other"); + CheckCommand(proc, "vkCmdPipelineBarrier2KHR", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }, "other"); + CheckCommand(proc, "vkCmdBeginRenderingKHR", CheckpointKind::renderPassBegin, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }, "other"); + CheckCommand(proc, "vkCmdEndRenderingKHR", CheckpointKind::renderPassEnd, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c); }, "other"); + CheckCommand(proc, "vkCmdSetEvent2KHR", CheckpointKind::event, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, nullptr); }, "other"); + CheckCommand(proc, "vkCmdResetEvent2KHR", CheckpointKind::event, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0); }, "other"); + CheckCommand(proc, "vkCmdWaitEvents2KHR", CheckpointKind::wait, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, 0, nullptr, nullptr); }, "other"); + CheckCommand(proc, "vkCmdPipelineBarrier2", CheckpointKind::barrier, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, nullptr); }); + CheckCommand(proc, "vkCmdDrawIndirectCountKHR", CheckpointKind::draw, [](PFN_vkVoidFunction f, VkCommandBuffer c) { As(f)(c, VK_NULL_HANDLE, 0, VK_NULL_HANDLE, 0, 1, 16); }); +} + +void ClassifiesEverySourceCommand(PFN_vkGetDeviceProcAddr proc, const std::filesystem::path& root) { + const std::regex literal("\"(vkCmd[A-Za-z0-9]+)\""); + const std::regex type("PFN_(vkCmd[A-Za-z0-9]+)"); + const std::regex macro("X\\((Cmd[A-Za-z0-9]+)\\)"); + std::set names; + for (const auto& entry : std::filesystem::recursive_directory_iterator(root)) { + if (!entry.is_regular_file()) continue; + const auto relative = std::filesystem::relative(entry.path(), root).generic_string(); + if (relative.rfind("tests/", 0) == 0) continue; + const auto extension = entry.path().extension().string(); + if (extension != ".cpp" && extension != ".hpp" && extension != ".h" && extension != ".inl") continue; + std::ifstream file(entry.path(), std::ios::binary); + std::stringstream text; + text << file.rdbuf(); + const auto content = text.str(); + for (std::sregex_iterator match(content.begin(), content.end(), literal), end; match != end; ++match) names.insert((*match)[1].str()); + for (std::sregex_iterator match(content.begin(), content.end(), type), end; match != end; ++match) names.insert((*match)[1].str()); + for (std::sregex_iterator match(content.begin(), content.end(), macro), end; match != end; ++match) names.insert("vk" + (*match)[1].str()); + } + Expect(names.size() > 20, "the source scan found too few vkCmd names under " + root.generic_string()); + anyCommand = true; + for (const auto& name : names) { + try { + static_cast(proc(Device(), name.c_str())); + } catch (const std::runtime_error& error) { + Expect(false, name + " in the driver sources is not classified: " + error.what()); + } + } + anyCommand = false; } } -int main() { +int main(int argc, char** argv) { try { const auto proc = AgcDriver::Graphics::CheckpointCommandKinds(Device(), &mockProc); Expect(proc != nullptr && proc != &mockProc, "command checkpoints did not return their own resolver"); @@ -307,6 +401,9 @@ int main() { PassesStateThroughAndRejectsUnknownCommands(proc); RejectsMismatchedSetups(); RejectsTwoFunctionsForOneCommand(proc); + KeepsAliasesApart(proc); + Expect(argc == 2, "the test needs the libSceAgcDriver source directory"); + if (argc == 2) ClassifiesEverySourceCommand(proc, argv[1]); } catch (const std::exception& error) { std::fprintf(stderr, "agc_command_checkpoints: %s\n", error.what()); return 1; diff --git a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp index c4374cb541c4..8de59954dd12 100644 --- a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp @@ -425,12 +425,14 @@ void CheckpointKindMarkers() { Require(AgcDriver::CheckpointKindOf(reinterpret_cast(marker)) == static_cast(kind), "a command kind did not survive its marker"); } Require(AgcDriver::CheckpointKindOf(reinterpret_cast(static_cast(0x1234u))) == AgcDriver::CheckpointKind::none, "a plain token marker decoded a command kind"); - const auto outOfRange = static_cast(0xFEu) << AgcDriver::CheckpointKindShift; + const auto tag = AgcDriver::CheckpointTag << AgcDriver::CheckpointTagShift; + const auto outOfRange = tag | (static_cast(0xFEu) << AgcDriver::CheckpointKindShift); Require(AgcDriver::CheckpointKindOf(reinterpret_cast(outOfRange)) == AgcDriver::CheckpointKind::none, "an unknown command kind was not decoded as none"); - const auto highNibble = static_cast(0x13u) << AgcDriver::CheckpointKindShift; + const auto highNibble = tag | (static_cast(0x13u) << AgcDriver::CheckpointKindShift); Require(AgcDriver::CheckpointKindOf(reinterpret_cast(highNibble)) == AgcDriver::CheckpointKind::none, "a kind byte above the known kinds was decoded from its low bits"); - Require(AgcDriver::CheckpointMarker(0x1234u, AgcDriver::CheckpointKind::copy) == (static_cast(0x1234u) | (static_cast(3u) << 40u)), "the copy kind is not stored from bit 40 of the marker"); - const char* const names[] = {"none", "draw", "dispatch", "copy", "blit", "clear", "fill", "update", "barrier", "render pass begin", "render pass end", "query", "execute commands", "command buffer begin", "command buffer end"}; + Require(AgcDriver::CheckpointKindOf(reinterpret_cast(static_cast(0x000001A2B3C4D5E6ull))) == AgcDriver::CheckpointKind::none, "a pointer-like marker from the driver decoded a command kind"); + Require(AgcDriver::CheckpointKindOf(reinterpret_cast(static_cast(AgcDriver::CheckpointMarker(7u, AgcDriver::CheckpointKind::barrier)) ^ (static_cast(1u) << 63u))) == AgcDriver::CheckpointKind::none, "a marker with another tag decoded a command kind"); + const char* const names[] = {"none", "draw", "dispatch", "copy", "blit", "clear", "fill", "update", "barrier", "render pass begin", "render pass end", "query", "execute commands", "command buffer begin", "command buffer end", "event", "wait events"}; static_assert(sizeof(names) / sizeof(names[0]) == static_cast(AgcDriver::CheckpointKind::count)); for (std::uint32_t kind = 0; kind < static_cast(AgcDriver::CheckpointKind::count); ++kind) { Require(std::strcmp(AgcDriver::CheckpointKindName(static_cast(kind)), names[kind]) == 0, "a command kind lost its name"); diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake index 60d7e405f272..9e21b77b662e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_command_checkpoints_tests.cmake @@ -6,7 +6,7 @@ configure_windows_unwind(agc_command_checkpoints_tests) if(BUILD_TESTING) set_target_properties(agc_command_checkpoints_tests PROPERTIES EXCLUDE_FROM_ALL OFF) - add_test(NAME agc_command_checkpoints COMMAND agc_command_checkpoints_tests) + add_test(NAME agc_command_checkpoints COMMAND agc_command_checkpoints_tests ${CMAKE_CURRENT_SOURCE_DIR}) set_tests_properties(agc_command_checkpoints PROPERTIES ENVIRONMENT "APS5_GPU_CHECKPOINTS=1") if(WIN32) set_tests_properties(agc_command_checkpoints PROPERTIES ENVIRONMENT_MODIFICATION diff --git a/docs/dev/BUILD.md b/docs/dev/BUILD.md index 6bd3b8e54f92..b91ffb3820fc 100644 --- a/docs/dev/BUILD.md +++ b/docs/dev/BUILD.md @@ -402,7 +402,7 @@ Set `APS5_TRACE_MEMORY_BUDGET=1` to track Vulkan allocations by device, heap and ## Device loss reports -On a device that supports `VK_EXT_device_fault`, the first `VK_ERROR_DEVICE_LOST` prints the driver's fault description, the faulting addresses and the vendor fault codes as `[device-fault]` lines. Set `APS5_GPU_CHECKPOINTS=1` to also record a `VK_NV_device_diagnostic_checkpoints` marker with the program address before each compute dispatch and guest draw, and, before every other command the driver records and after each command buffer begins, a marker naming the command kind (draw, dispatch, copy, blit, clear, fill, update, barrier, render pass begin or end, query, execute commands, command buffer begin or end) and the last guest dispatch or draw of its command buffer; the report then lists the last markers the GPU reached, with the kind and the guest program of each. On a device without that extension but with `VK_AMD_buffer_marker` (RADV and AMD's own drivers), each compute dispatch instead writes a numbered marker into a host-visible buffer when the GPU reaches it and another when it finishes; the report then names the programs of the last dispatch reached, of the last one finished and of up to eight dispatches between them. Checkpoints are disabled by default and ignored on devices without the extension. +On a device that supports `VK_EXT_device_fault`, the first `VK_ERROR_DEVICE_LOST` prints the driver's fault description, the faulting addresses and the vendor fault codes as `[device-fault]` lines. Set `APS5_GPU_CHECKPOINTS=1` to also record a `VK_NV_device_diagnostic_checkpoints` marker with the program address before each compute dispatch and guest draw, and, before every other command the driver records and after each command buffer begins, a marker naming the command kind (draw, dispatch, copy, blit, clear, fill, update, barrier, render pass begin or end, query, execute commands, event, wait events, command buffer begin or end) and the last guest dispatch or draw of its command buffer; the report then lists the last markers the GPU reached, with the kind and the guest program of each. On a device without that extension but with `VK_AMD_buffer_marker` (RADV and AMD's own drivers), each compute dispatch instead writes a numbered marker into a host-visible buffer when the GPU reaches it and another when it finishes; the report then names the programs of the last dispatch reached, of the last one finished and of up to eight dispatches between them. Checkpoints are disabled by default and ignored on devices without the extension. ## Worker sampler diff --git a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md index 1117b906d8a1..f5a6bd16bc95 100644 --- a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md +++ b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md @@ -1,9 +1,5 @@ # libSceAgcDriver -## change [Device-lost fault attribution] - -- [Device-lost fault attribution](../../../core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp) (libSceAgcDriver): a device loss prints the faulting VA with its guest snapshot (owner or neighbours), a bracket window (last completed, in flight, first not started, last 8 tokens) and draw/dispatch/mesh tokens (serial, program, variant, stage, desc). Indirect draws/dispatches record buffer VA, offset, stride and up to 4 record-time words (unsynced read, never submits); mesh sets the stage. Verified-copy Find; clear-first Claim (reuse-tested). Residency today: never bound (faulting submit snapshot found and uncovered) else unknown; evicted/import/refused lines have no producer yet. Serial at marker offset 0, tokens at 4. Needs APS5_GPU_CHECKPOINTS. Cannot name T# or pending write-back; no task token path yet. Vendor dump via APS5_DUMP_DEVICE_FAULT where supported. Opened for PPSA01342 (#10634): second-world load faults a write. - ## add Functional -- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, or a replay on another thread, loses that token. NV checkpoint data for the main queue was empty after most resets seen on an RTX 5070 Ti (621.24), so a hang may name no main-queue command. +- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, a replay on another thread, or a buffer begun again on another thread loses or keeps a stale token. vkCmdBind*, vkCmdPush* and vkCmdSet* other than events pass as state, and the source scan in the test sees only literal, PFN_ and X(Cmd...) names; submit semaphore waits get none (a stuck wait shows the last command buffer end); kind markers carry no submit serial. One driver per process: real functions are process-wide and a second vkCmdSetCheckpointNV throws. NV checkpoint data for a queue can be empty after a reset. From 8e313e544afdc8de8d1c5b76a7d5db7146ca3358 Mon Sep 17 00:00:00 2001 From: videogamer10293 <347855792+videogamer10293@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:39:26 +0000 Subject: [PATCH 4/6] test(agc): keep the hook table out of the checkpoint source scan Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp | 1 + core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp index 9be539ab3f00..1d88997dfd78 100644 --- a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp @@ -367,6 +367,7 @@ void ClassifiesEverySourceCommand(PFN_vkGetDeviceProcAddr proc, const std::files if (!entry.is_regular_file()) continue; const auto relative = std::filesystem::relative(entry.path(), root).generic_string(); if (relative.rfind("tests/", 0) == 0) continue; + if (relative == "Graphics/src/CommandCheckpoints.cpp") continue; const auto extension = entry.path().extension().string(); if (extension != ".cpp" && extension != ".hpp" && extension != ".h" && extension != ".inl") continue; std::ifstream file(entry.path(), std::ios::binary); diff --git a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp index 8de59954dd12..c23176db48ee 100644 --- a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp @@ -467,15 +467,15 @@ void KindCheckpointInRing() { AgcDriver::FaultTokenRing& ring = AgcDriver::FaultTokenRing::Instance(); const AgcDriver::FaultToken token = ring.Claim(); Require(token != 0, "the kind checkpoint token is invalid"); - ClaimBracketToken(ring, token, 11, 0x5003e9700ull, 9, "dispatch 32x1x1"); + ClaimBracketToken(ring, token, 11, 0x1240000ull, 9, "dispatch 32x1x1"); kindMarker = reinterpret_cast(AgcDriver::CheckpointMarker(token, AgcDriver::CheckpointKind::barrier)); const auto lines = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring); Require(lines.size() == 1, "a kind checkpoint after a token in the ring does not report one line"); char expected[256]; - std::snprintf(expected, sizeof(expected), "[device-fault] checkpoint: barrier after token %u batch 11 queue 0x2000 program 0x5003e9700 variant 9 dispatch 32x1x1 at stage 0x1", static_cast(token)); + std::snprintf(expected, sizeof(expected), "[device-fault] checkpoint: barrier after token %u batch 11 queue 0x2000 program 0x1240000 variant 9 dispatch 32x1x1 at stage 0x1", static_cast(token)); Require(lines[0] == expected, "a kind checkpoint after a token in the ring lost the guest program"); const auto staged = AgcDriver::DeviceFaultReport(VK_NULL_HANDLE, nullptr, FakeQueue(), MockKindCheckpointData, 0, nullptr, AgcDriver::SubmitBracket{}, ring, static_cast(nullptr)); - Require(staged.size() == 1 && staged[0].rfind("[device-fault] checkpoint: barrier after token ", 0) == 0 && staged[0].find("program 0x5003e9700 variant 9 stage ") != std::string::npos, "a kind checkpoint after a token in the ring lost the guest program in the residency report"); + Require(staged.size() == 1 && staged[0].rfind("[device-fault] checkpoint: barrier after token ", 0) == 0 && staged[0].find("program 0x1240000 variant 9 stage ") != std::string::npos, "a kind checkpoint after a token in the ring lost the guest program in the residency report"); kindMarker = nullptr; } From 4bb143fe6403a0b2c66c720b4afdbd83a4543799 Mon Sep 17 00:00:00 2001 From: videogamer10293 <347855792+videogamer10293@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:30:21 +0000 Subject: [PATCH 5/6] feat(agc): leave out the kind checkpoint right after a guest token Co-Authored-By: Claude Opus 5.5 --- .../Graphics/src/CommandCheckpoints.cpp | 36 ++++++++++++------ .../tests/CommandCheckpoints.cpp | 37 ++++++++++++++++--- .../changes/agc-checkpoint-command-kinds.md | 2 +- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp index ea6d45f59d6c..b021e09c16ea 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp @@ -12,6 +12,7 @@ namespace { struct RecordedCheckpoint { VkCommandBuffer commands = VK_NULL_HANDLE; AgcDriver::FaultToken token = 0; + bool tokenIsLast = false; }; RecordedCheckpoint& lastRecorded() noexcept { @@ -32,11 +33,21 @@ std::atomic& setCheckpointSlot() noexcept { void mark(VkCommandBuffer commands, AgcDriver::CheckpointKind kind) noexcept { const auto setCheckpoint = setCheckpointSlot().load(std::memory_order_acquire); if (setCheckpoint == nullptr || commands == VK_NULL_HANDLE) return; - const auto& recorded = lastRecorded(); + auto& recorded = lastRecorded(); const AgcDriver::FaultToken token = recorded.commands == commands ? recorded.token : 0; + if (recorded.commands == commands) recorded.tokenIsLast = false; setCheckpoint(commands, reinterpret_cast(AgcDriver::CheckpointMarker(token, kind))); } +void markWork(VkCommandBuffer commands, AgcDriver::CheckpointKind kind) noexcept { + auto& recorded = lastRecorded(); + if (recorded.commands == commands && recorded.tokenIsLast) { + recorded.tokenIsLast = false; + return; + } + mark(commands, kind); +} + enum class Slot : std::size_t { BeginCommandBuffer, EndCommandBuffer, @@ -122,59 +133,59 @@ VKAPI_ATTR VkResult VKAPI_CALL checkpointedEndCommandBuffer(VkCommandBuffer comm } VKAPI_ATTR void VKAPI_CALL checkpointedDraw(VkCommandBuffer commands, std::uint32_t vertexCount, std::uint32_t instanceCount, std::uint32_t firstVertex, std::uint32_t firstInstance) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, vertexCount, instanceCount, firstVertex, firstInstance); } VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexed(VkCommandBuffer commands, std::uint32_t indexCount, std::uint32_t instanceCount, std::uint32_t firstIndex, std::int32_t vertexOffset, std::uint32_t firstInstance) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); } VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, std::uint32_t drawCount, std::uint32_t stride) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, buffer, offset, drawCount, stride); } VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexedIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, std::uint32_t drawCount, std::uint32_t stride) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, buffer, offset, drawCount, stride); } template VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndirectCount(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer, VkDeviceSize countOffset, std::uint32_t maxDrawCount, std::uint32_t stride) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); } template VKAPI_ATTR void VKAPI_CALL checkpointedDrawIndexedIndirectCount(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer, VkDeviceSize countOffset, std::uint32_t maxDrawCount, std::uint32_t stride) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, buffer, offset, countBuffer, countOffset, maxDrawCount, stride); } VKAPI_ATTR void VKAPI_CALL checkpointedDrawMeshTasks(VkCommandBuffer commands, std::uint32_t x, std::uint32_t y, std::uint32_t z) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, x, y, z); } VKAPI_ATTR void VKAPI_CALL checkpointedDrawMeshTasksIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, std::uint32_t drawCount, std::uint32_t stride) { - mark(commands, AgcDriver::CheckpointKind::draw); + markWork(commands, AgcDriver::CheckpointKind::draw); Real::Get()(commands, buffer, offset, drawCount, stride); } VKAPI_ATTR void VKAPI_CALL checkpointedDispatch(VkCommandBuffer commands, std::uint32_t x, std::uint32_t y, std::uint32_t z) { - mark(commands, AgcDriver::CheckpointKind::dispatch); + markWork(commands, AgcDriver::CheckpointKind::dispatch); Real::Get()(commands, x, y, z); } VKAPI_ATTR void VKAPI_CALL checkpointedDispatchBase(VkCommandBuffer commands, std::uint32_t baseX, std::uint32_t baseY, std::uint32_t baseZ, std::uint32_t x, std::uint32_t y, std::uint32_t z) { - mark(commands, AgcDriver::CheckpointKind::dispatch); + markWork(commands, AgcDriver::CheckpointKind::dispatch); Real::Get()(commands, baseX, baseY, baseZ, x, y, z); } VKAPI_ATTR void VKAPI_CALL checkpointedDispatchIndirect(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset) { - mark(commands, AgcDriver::CheckpointKind::dispatch); + markWork(commands, AgcDriver::CheckpointKind::dispatch); Real::Get()(commands, buffer, offset); } @@ -437,6 +448,7 @@ void NoteRecordedCheckpoint(VkCommandBuffer commands, AgcDriver::FaultToken toke auto& recorded = lastRecorded(); recorded.commands = commands; recorded.token = token; + recorded.tokenIsLast = true; } } diff --git a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp index 1d88997dfd78..25fa344ceba7 100644 --- a/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/CommandCheckpoints.cpp @@ -190,6 +190,14 @@ void CheckCommand(PFN_vkGetDeviceProcAddr proc, const char* name, CheckpointKind Expect(KindOf(events[0]) == kind, std::string(name) + " recorded the kind " + AgcDriver::CheckpointKindName(KindOf(events[0]))); Expect(events[0].commands == Commands(0x10), std::string(name) + " recorded its checkpoint into another command buffer"); } + events.clear(); + AgcDriver::Graphics::NoteRecordedCheckpoint(Commands(0x10), 9); + call(wrapper, Commands(0x10)); + if (kind == CheckpointKind::draw || kind == CheckpointKind::dispatch) { + Expect(events.size() == 1 && events[0].what == real, std::string(name) + " got a second checkpoint right after a guest token"); + } else { + Expect(events.size() == 2 && KindOf(events[0]) == kind && TokenOf(events[0]) == 9, std::string(name) + " lost its checkpoint after a guest token"); + } } template @@ -250,22 +258,37 @@ void CheckpointsCarryTheGuestToken(PFN_vkGetDeviceProcAddr proc) { const auto barrier = Resolve(proc, "vkCmdPipelineBarrier"); const auto dispatch = Resolve(proc, "vkCmdDispatch"); const auto begin = Resolve(proc, "vkBeginCommandBuffer"); + const auto draw = Resolve(proc, "vkCmdDraw"); events.clear(); AgcDriver::Graphics::NoteRecordedCheckpoint(Commands(0x20), 77); + dispatch(Commands(0x30), 8, 1, 1); dispatch(Commands(0x20), 32, 1, 1); + dispatch(Commands(0x20), 16, 1, 1); barrier(Commands(0x20), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); barrier(Commands(0x30), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); - Expect(events.size() == 6, "the token checks did not record three checkpoints and three commands"); - if (events.size() == 6) { - Expect(KindOf(events[0]) == CheckpointKind::dispatch && TokenOf(events[0]) == 77, "a dispatch after a guest token does not carry it"); - Expect(KindOf(events[2]) == CheckpointKind::barrier && TokenOf(events[2]) == 77, "a barrier after a guest token does not carry it"); - Expect(TokenOf(events[4]) == 0, "a checkpoint on another command buffer carries its guest token"); + Expect(events.size() == 9, "the token checks did not record four checkpoints and five commands"); + if (events.size() == 9) { + Expect(events[0].what == "checkpoint" && KindOf(events[0]) == CheckpointKind::dispatch && TokenOf(events[0]) == 0, "a dispatch on another command buffer lost its checkpoint after a guest token"); + Expect(events[2].what == "vkCmdDispatch", "a guest dispatch got a second checkpoint right after its guest token"); + Expect(KindOf(events[3]) == CheckpointKind::dispatch && TokenOf(events[3]) == 77, "a second dispatch after a guest token does not carry it"); + Expect(KindOf(events[5]) == CheckpointKind::barrier && TokenOf(events[5]) == 77, "a barrier after a guest token does not carry it"); + Expect(TokenOf(events[7]) == 0, "a checkpoint on another command buffer carries its guest token"); } + events.clear(); + AgcDriver::Graphics::NoteRecordedCheckpoint(Commands(0x20), 78); + barrier(Commands(0x20), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); + draw(Commands(0x20), 3, 1, 0, 0); + Expect(events.size() == 4 && KindOf(events[2]) == CheckpointKind::draw && TokenOf(events[2]) == 78, "a draw after a guest token and a barrier lost its checkpoint"); const VkCommandBufferBeginInfo info{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; static_cast(begin(Commands(0x20), &info)); events.clear(); barrier(Commands(0x20), 0, 0, 0, 0, nullptr, 0, nullptr, 0, nullptr); Expect(!events.empty() && TokenOf(events[0]) == 0, "a guest token outlived vkBeginCommandBuffer of its command buffer"); + AgcDriver::Graphics::NoteRecordedCheckpoint(Commands(0x20), 80); + static_cast(begin(Commands(0x20), &info)); + events.clear(); + dispatch(Commands(0x20), 4, 1, 1); + Expect(events.size() == 2 && KindOf(events[0]) == CheckpointKind::dispatch, "a dispatch after vkBeginCommandBuffer lost its checkpoint to an earlier guest token"); } void RecorderNotesItsToken(PFN_vkGetDeviceProcAddr proc) { @@ -278,6 +301,10 @@ void RecorderNotesItsToken(PFN_vkGetDeviceProcAddr proc) { Expect(events.size() == 1 && events[0].what == "checkpoint" && TokenOf(events[0]) == 4242 && KindOf(events[0]) == CheckpointKind::none, "the recorder did not write its guest token"); Resolve(proc, "vkCmdFillBuffer")(Commands(0x50), VK_NULL_HANDLE, 0, 4, 0); Expect(events.size() == 3 && TokenOf(events[1]) == 4242 && KindOf(events[1]) == CheckpointKind::fill, "a command after the recorder's guest token does not carry it"); + events.clear(); + AgcDriver::Graphics::WriteNvCheckpointToken(context, Commands(0x50), 4243, 0); + Resolve(proc, "vkCmdDraw")(Commands(0x50), 3, 1, 0, 0); + Expect(events.size() == 2 && TokenOf(events[0]) == 4243 && events[1].what == "vkCmdDraw", "a guest draw got a second checkpoint right after the recorder's guest token"); } void PassesStateThroughAndRejectsUnknownCommands(PFN_vkGetDeviceProcAddr proc) { diff --git a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md index f5a6bd16bc95..ac056d21eba1 100644 --- a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md +++ b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md @@ -2,4 +2,4 @@ ## add Functional -- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, a replay on another thread, or a buffer begun again on another thread loses or keeps a stale token. vkCmdBind*, vkCmdPush* and vkCmdSet* other than events pass as state, and the source scan in the test sees only literal, PFN_ and X(Cmd...) names; submit semaphore waits get none (a stuck wait shows the last command buffer end); kind markers carry no submit serial. One driver per process: real functions are process-wide and a second vkCmdSetCheckpointNV throws. NV checkpoint data for a queue can be empty after a reset. +- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, a replay on another thread, or a buffer begun again on another thread loses or keeps a stale token. vkCmdBind*, vkCmdPush* and vkCmdSet* other than events pass as state, and the source scan in the test sees only literal, PFN_ and X(Cmd...) names; submit semaphore waits get none; kind markers carry no submit serial. Real functions are process-wide; a second vkCmdSetCheckpointNV throws. A draw or dispatch right after a guest token gets no checkpoint of its own, even if it is not that token's work. From d93cf5b77690022591c836ab4355c971a19acd18 Mon Sep 17 00:00:00 2001 From: videogamer10293 <347855792+videogamer10293@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:21:59 +0000 Subject: [PATCH 6/6] docs(agc): note that a batch-begin token also skips the next kind checkpoint Co-Authored-By: Claude Opus 5.5 --- docs/dev/BUILD.md | 2 +- docs/dev/ENVIRONMENT.md | 2 +- docs/dev/debt/changes/agc-checkpoint-command-kinds.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/dev/BUILD.md b/docs/dev/BUILD.md index b91ffb3820fc..37aaecb0aeb4 100644 --- a/docs/dev/BUILD.md +++ b/docs/dev/BUILD.md @@ -402,7 +402,7 @@ Set `APS5_TRACE_MEMORY_BUDGET=1` to track Vulkan allocations by device, heap and ## Device loss reports -On a device that supports `VK_EXT_device_fault`, the first `VK_ERROR_DEVICE_LOST` prints the driver's fault description, the faulting addresses and the vendor fault codes as `[device-fault]` lines. Set `APS5_GPU_CHECKPOINTS=1` to also record a `VK_NV_device_diagnostic_checkpoints` marker with the program address before each compute dispatch and guest draw, and, before every other command the driver records and after each command buffer begins, a marker naming the command kind (draw, dispatch, copy, blit, clear, fill, update, barrier, render pass begin or end, query, execute commands, event, wait events, command buffer begin or end) and the last guest dispatch or draw of its command buffer; the report then lists the last markers the GPU reached, with the kind and the guest program of each. On a device without that extension but with `VK_AMD_buffer_marker` (RADV and AMD's own drivers), each compute dispatch instead writes a numbered marker into a host-visible buffer when the GPU reaches it and another when it finishes; the report then names the programs of the last dispatch reached, of the last one finished and of up to eight dispatches between them. Checkpoints are disabled by default and ignored on devices without the extension. +On a device that supports `VK_EXT_device_fault`, the first `VK_ERROR_DEVICE_LOST` prints the driver's fault description, the faulting addresses and the vendor fault codes as `[device-fault]` lines. Set `APS5_GPU_CHECKPOINTS=1` to also record a `VK_NV_device_diagnostic_checkpoints` marker with the program address before each compute dispatch and guest draw, and, before every other command the driver records and after each command buffer begins, a marker naming the command kind (draw, dispatch, copy, blit, clear, fill, update, barrier, render pass begin or end, query, execute commands, event, wait events, command buffer begin or end) and the last guest dispatch or draw of its command buffer (or its batch-begin token with `APS5_GPU_CHECKPOINT_BATCHES`); the report then lists the last markers the GPU reached, with the kind and the guest program of each. On a device without that extension but with `VK_AMD_buffer_marker` (RADV and AMD's own drivers), each compute dispatch instead writes a numbered marker into a host-visible buffer when the GPU reaches it and another when it finishes; the report then names the programs of the last dispatch reached, of the last one finished and of up to eight dispatches between them. Checkpoints are disabled by default and ignored on devices without the extension. ## Worker sampler diff --git a/docs/dev/ENVIRONMENT.md b/docs/dev/ENVIRONMENT.md index 6f4f58ee851d..36a2db7789b2 100644 --- a/docs/dev/ENVIRONMENT.md +++ b/docs/dev/ENVIRONMENT.md @@ -248,7 +248,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_FRAME_CAPTURE_TRIGGER` | string | `core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp` | | file whose appearance starts the frame capture at the next frame boundary at which the driver is idle; a number in it replaces the frame count. While it waits, `[frame-capture] waiting to start the capture` is logged at the first and every 50th boundary, and the capture gives up after 600 | | `APS5_FRAME_REPLAY_ATTEMPT` | integer | `core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp` | 0 | number of times `agc_frame_replay` has started itself again on Windows because a host region sat in a captured GPU-visible range; set by the replay for the process it starts, which stops relaunching after 8 | | `APS5_FULL_BARRIERS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | records every barrier instead of merging redundant ones | -| `APS5_GPU_CHECKPOINTS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | attaches device diagnostic checkpoints through VK_NV_device_diagnostic_checkpoints, before each dispatch and draw, and before every other recorded command with its kind and the last guest dispatch or draw, or buffer markers through VK_AMD_buffer_marker on a device without it | +| `APS5_GPU_CHECKPOINTS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | attaches device diagnostic checkpoints through VK_NV_device_diagnostic_checkpoints, before each dispatch and draw, and before every other recorded command with its kind and the last guest dispatch or draw (or batch-begin token with `APS5_GPU_CHECKPOINT_BATCHES`), or buffer markers through VK_AMD_buffer_marker on a device without it | | `APS5_GPU_CHECKPOINT_BATCHES` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | with `APS5_GPU_CHECKPOINTS`, also records a checkpoint token at the start of every queue-0 command batch, so a device loss before the first draw or dispatch names the batch; without `APS5_GPU_CHECKPOINTS` it does nothing | | `APS5_GPU_COPY_MAX_KIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` | 1024 | size cap in KiB on a descriptor sub-range copied through the GPU path, larger ones staying on the CPU unless a pending GPU write, pending storage results or a unit shadow overlaps them (the GPU then copies them whatever their size, so the CPU does not wait) | | `APS5_GPU_NO_WRITEBACK` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | | skips writing GPU results back into guest memory | diff --git a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md index ac056d21eba1..8c62a9fab608 100644 --- a/docs/dev/debt/changes/agc-checkpoint-command-kinds.md +++ b/docs/dev/debt/changes/agc-checkpoint-command-kinds.md @@ -2,4 +2,4 @@ ## add Functional -- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, a replay on another thread, or a buffer begun again on another thread loses or keeps a stale token. vkCmdBind*, vkCmdPush* and vkCmdSet* other than events pass as state, and the source scan in the test sees only literal, PFN_ and X(Cmd...) names; submit semaphore waits get none; kind markers carry no submit serial. Real functions are process-wide; a second vkCmdSetCheckpointNV throws. A draw or dispatch right after a guest token gets no checkpoint of its own, even if it is not that token's work. +- [Command kind checkpoints](../../../core/libs/prx/libSceAgcDriver/Graphics/src/CommandCheckpoints.cpp) (libSceAgcDriver): with APS5_GPU_CHECKPOINTS and VK_NV_device_diagnostic_checkpoints, a checkpoint before each recorded command and after vkBeginCommandBuffer names only the command kind and the last guest token of its command buffer on the recording thread; a thread recording two command buffers at once, a replay on another thread, or a buffer begun again on another thread loses or keeps a stale token. vkCmdBind*, vkCmdPush* and vkCmdSet* other than events pass as state, and the source scan in the test sees only literal, PFN_ and X(Cmd...) names; submit semaphore waits get none; kind markers carry no submit serial. Real functions are process-wide; a second vkCmdSetCheckpointNV throws. A draw or dispatch right after a guest token, or after the batch-begin token of APS5_GPU_CHECKPOINT_BATCHES, gets no checkpoint of its own, even if it is not that token's work.