From ad7404a7895f84440292ee6eca6b02be83b390db Mon Sep 17 00:00:00 2001 From: mjnhbg3 <2287071+mjnhbg3@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:39:30 -0500 Subject: [PATCH 1/2] test(kyty): validate indirect dispatch dimensions across writes --- .../host_gpu/renderer/cache/bufferCache.h | 1 + .../KytyShim/tests/CMakeLists.txt | 8 + .../tests/IndirectDispatchDimensions.cpp | 221 ++++++++++++++++++ .../tests/IndirectDispatchDimensionsMain.cpp | 3 + 4 files changed, 233 insertions(+) create mode 100644 core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensions.cpp create mode 100644 core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensionsMain.cpp diff --git a/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h b/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h index a2c2df0497..94ba4de757 100644 --- a/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h +++ b/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h @@ -292,6 +292,7 @@ class BufferCache { private: friend struct BufferCacheTestAccess; friend struct BufferCacheGcTestAccess; + friend struct IndirectDispatchTestAccess; friend class TextureCache; [[nodiscard]] std::pair ObtainSynchronizedBuffer( diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt index 7948d3399b..0ec38e7d01 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt @@ -267,6 +267,14 @@ if(NOT ANYPS5_KYTY_DLL AND (WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux")) set_tests_properties(kyty_shim_buffer_cache_pages PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77 ENVIRONMENT "KYTY_GPU_PIPELINE=0" PASS_REGULAR_EXPRESSION "kyty_shim_buffer_cache_pages: ok" FAIL_REGULAR_EXPRESSION "FAILED") + target_sources(libSceAgcDriver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensions.cpp) + add_test_executable(kyty_shim_indirect_dispatch_dimensions_tests ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensionsMain.cpp) + configure_host_test_executable(kyty_shim_indirect_dispatch_dimensions_tests) + target_link_libraries(kyty_shim_indirect_dispatch_dimensions_tests PRIVATE libSceAgcDriver libkernel libc) + add_test(NAME kyty_shim_indirect_dispatch_dimensions COMMAND kyty_shim_indirect_dispatch_dimensions_tests) + set_tests_properties(kyty_shim_indirect_dispatch_dimensions PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77 + ENVIRONMENT "KYTY_GPU_PIPELINE=0" + PASS_REGULAR_EXPRESSION "kyty_shim_indirect_dispatch_dimensions: ok" FAIL_REGULAR_EXPRESSION "FAILED") endif() if(WIN32) diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensions.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensions.cpp new file mode 100644 index 0000000000..302085d465 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensions.cpp @@ -0,0 +1,221 @@ +#include "TestVulkanDevice.h" +#include "common/emulatorConfig.h" +#include "common/threads.h" +#include "graphics/guest_gpu/command_processor/commandProcessor.h" +#include "graphics/guest_gpu/graphicsRun.h" +#include "graphics/guest_gpu/pm4.h" +#include "graphics/host_gpu/graphicContext.h" +#include "graphics/host_gpu/renderer/cache/bufferCache.h" +#include "graphics/shader/shader.h" +#include "kernel/memory.h" +#include "prx/libkernel/DirectMemory/DirectMemory.hpp" +#include "prx/libkernel/include/Api.hpp" + +#include +#include +#include +#include +#include +#include +#include + +using namespace Libs::Graphics; + +namespace Libs::Graphics { + +struct IndirectDispatchTestAccess { + static uint64_t ReadbackCount(const BufferCache& cache) { return cache.m_readback_count; } + static void DiscardGpuWrites(BufferCache& cache, uint64_t address, uint64_t size) { + cache.DiscardGpuWrites(address, size); + } +}; + +} + +namespace { + +constexpr uint64_t Page = 0x4000; +constexpr uint32_t Format32UInt = 20; +constexpr uint32_t DispatchMode = 0x41; +constexpr uint32_t Sentinel = 0x5aa55aa5; +constexpr std::array SentinelShader { + 0x7e0002ffu, Sentinel, 0xe0700000u, 0x80000000u, 0xbf810000u, +}; +constexpr std::array DimensionsShader { + 0x7e0002ffu, 3u, 0xe0700000u, 0x80000000u, + 0x7e0002ffu, 2u, 0xe0700004u, 0x80000000u, + 0x7e0002ffu, 2u, 0xe0700008u, 0x80000000u, + 0xbf810000u, +}; +constexpr std::array CounterShader { + 0x7e000281u, 0xe0c80000u, 0x80000000u, 0xbf810000u, +}; + +struct GpuWritesCleanup { + BufferCache& cache; + uint64_t args; + uint64_t output; + ~GpuWritesCleanup() { + IndirectDispatchTestAccess::DiscardGpuWrites(cache, args, Page); + IndirectDispatchTestAccess::DiscardGpuWrites(cache, output, Page); + } +}; + +void Require(bool passed, const char* description) { + if (!passed) throw std::runtime_error(description); +} + +void WaitForTick(CommandScheduler& scheduler, uint64_t tick) { + auto& master = scheduler.GetMasterSemaphore(); + const auto semaphore = master.Handle(); + const vk::SemaphoreWaitInfo info { + .semaphoreCount = 1, .pSemaphores = &semaphore, .pValues = &tick}; + Require(scheduler.Graphics().device.waitSemaphores(info, 5'000'000'000) == vk::Result::eSuccess, + "GPU dispatch did not finish"); + master.Refresh(); + scheduler.WaitPriorityOperations(tick); +} + +void Dispatch(CommandProcessor& processor, uint64_t code, uint64_t output, uint32_t size, + uint64_t args = 0) { + constexpr uint32_t Control = (0x01016facu & ~(0x7fu << 12u)) | + (Format32UInt << 12u) | (3u << 28u); + std::vector commands { + KYTY_PM4(4, Pm4::IT_SET_SH_REG, 0), 0x20c, static_cast(code >> 8u), + static_cast(code >> 40u), + KYTY_PM4(3, Pm4::IT_SET_SH_REG, 0), 0x212, 0, + KYTY_PM4(3, Pm4::IT_SET_SH_REG, 0), 0x213, 4u << 1u, + KYTY_PM4(5, Pm4::IT_SET_SH_REG, 0), 0x207, 1, 1, 1, + KYTY_PM4(6, Pm4::IT_SET_SH_REG, 0), 0x240, static_cast(output), + static_cast((output >> 32u) & 0xffffu), size, Control, + }; + if (args != 0) { + commands.insert(commands.end(), { + KYTY_PM4(4, Pm4::IT_DISPATCH_INDIRECT, 0), static_cast(args), + static_cast(args >> 32u), DispatchMode, + }); + } else { + commands.insert(commands.end(), { + KYTY_PM4(5, Pm4::IT_DISPATCH_DIRECT, 0), 1, 1, 1, DispatchMode, + }); + } + Pm4Execution execution; + Require(processor.Process(execution, commands) == Pm4ProcessResult::Complete, + "the compute dispatch did not finish recording"); +} + +void Complete(CommandScheduler& scheduler) { + const auto tick = scheduler.CurrentTick(); + scheduler.Flush(); + WaitForTick(scheduler, tick); +} + +uint32_t ReadCounter(BufferCache& cache, uint64_t address) { + uint32_t value = 0; + cache.ReadMemory(address, sizeof(value)); + Require(Libs::LibKernel::Memory::TryReadBacking(address, &value, sizeof(value)), + "could not read the output counter"); + return value; +} + +void Probe(GraphicContext& graphics) { + void* guest = nullptr; + Require(sceKernelMapFlexibleMemory(&guest, 3 * Page, 0x03, 0) == 0 && guest != nullptr, + "could not map synthetic guest memory"); + const auto code = reinterpret_cast(guest); + const auto sentinel_code = code; + const auto dimensions_code = code + 0x100; + const auto counter_code = code + 0x200; + const auto args = code + Page; + const auto output = code + 2 * Page; + std::memcpy(reinterpret_cast(sentinel_code), SentinelShader.data(), + SentinelShader.size() * sizeof(uint32_t)); + std::memcpy(reinterpret_cast(dimensions_code), DimensionsShader.data(), + DimensionsShader.size() * sizeof(uint32_t)); + std::memcpy(reinterpret_cast(counter_code), CounterShader.data(), + CounterShader.size() * sizeof(uint32_t)); + ShaderMapUserData(sentinel_code, {.type = Prospero::ShaderBinaryType::kCs, + .code_size_bytes = static_cast(SentinelShader.size() * sizeof(uint32_t))}); + ShaderMapUserData(dimensions_code, {.type = Prospero::ShaderBinaryType::kCs, + .code_size_bytes = static_cast(DimensionsShader.size() * sizeof(uint32_t))}); + ShaderMapUserData(counter_code, {.type = Prospero::ShaderBinaryType::kCs, + .code_size_bytes = static_cast(CounterShader.size() * sizeof(uint32_t))}); + const uint32_t zero = 0; + Libs::LibKernel::Memory::WriteBacking(output, &zero, sizeof(zero)); + { + RenderContext renderer(graphics); + renderer.InitializeGpu(nullptr); + CommandProcessor processor(renderer, 0); + processor.Reset(); + auto& scheduler = renderer.GetCommandScheduler(); + scheduler.Begin(processor.GetCtx(), processor.GetUcfg(), processor.GetShCtx()); + auto& cache = renderer.GetBufferCache(); + GpuWritesCleanup cleanup {cache, args, output}; + Dispatch(processor, counter_code, output, sizeof(uint32_t)); + Complete(scheduler); + Require(ReadCounter(cache, output) == 1, "the direct counter control produced the wrong count"); + Dispatch(processor, sentinel_code, args + 12, sizeof(uint32_t)); + Complete(scheduler); + Require(cache.HasGpuDirtyBytes(args + 12, sizeof(uint32_t)), + "the GPU-written neighbor was not tracked"); + const auto readbacks_before = IndirectDispatchTestAccess::ReadbackCount(cache); + const std::array cpu_dimensions {2, 3, 1}; + Require(renderer.WriteAroundGpuWrites(reinterpret_cast(args), cpu_dimensions.data(), + sizeof(cpu_dimensions)), + "could not write CPU dispatch dimensions"); + Require(!cache.HasGpuDirtyBytes(args, sizeof(cpu_dimensions)), + "the CPU-written dimensions are GPU-dirty"); + Dispatch(processor, counter_code, output, sizeof(uint32_t), args); + Complete(scheduler); + const auto readbacks_after = IndirectDispatchTestAccess::ReadbackCount(cache); + Require(readbacks_after == readbacks_before, + "indirect dispatch read back the neighboring GPU-written value"); + Require(cache.HasGpuDirtyBytes(args + 12, sizeof(uint32_t)), + "indirect dispatch consumed the neighboring GPU-written value"); + const auto cpu_count = ReadCounter(cache, output); + if (cpu_count != 7) std::fprintf(stderr, "CPU indirect counter: %u, expected 7\n", cpu_count); + Require(cpu_count == 7, "CPU-written dimensions produced the wrong count"); + Require(IndirectDispatchTestAccess::ReadbackCount(cache) > readbacks_before, + "the output read did not exercise the readback counter"); + const std::array zero_dimensions {0, 2, 3}; + Require(renderer.WriteAroundGpuWrites(reinterpret_cast(args), zero_dimensions.data(), + sizeof(zero_dimensions)), + "could not write zero dispatch dimensions"); + Dispatch(processor, counter_code, output, sizeof(uint32_t), args); + Complete(scheduler); + Require(ReadCounter(cache, output) == 7, "zero dimensions executed compute threads"); + Dispatch(processor, dimensions_code, args, sizeof(cpu_dimensions)); + Dispatch(processor, counter_code, output, sizeof(uint32_t), args); + Complete(scheduler); + const auto gpu_count = ReadCounter(cache, output); + Require(gpu_count == 19, "GPU-written dimensions produced the wrong count"); + std::printf("CPU dispatch readbacks %llu -> %llu; counter direct 1, CPU %u, zero 7, GPU %u\n", + static_cast(readbacks_before), + static_cast(readbacks_after), cpu_count, gpu_count); + } + Require(sceKernelMunmap(code, 3 * Page) == 0, "could not unmap synthetic guest memory"); +} + +} + +extern "C" int RunKytyIndirectDispatchDimensionsTest() { + try { + Common::InitializeThreads(); + Config::Initialize(); + Config::Load(Config::ConfigOptions {}); + KytyShimTests::TestVulkanDevice gpu; + if (const auto* unavailable = gpu.Initialize()) { + const bool required = std::getenv("ANYPS5_REQUIRE_VULKAN") != nullptr; + std::fprintf(stderr, "kyty_shim_indirect_dispatch_dimensions: %s, no usable Vulkan device (%s)\n", + required ? "FAILED" : "skipped", unavailable); + return required ? 1 : 77; + } + Require(gpu.graphics.CreateAllocator(), "could not create the Vulkan allocator"); + Probe(gpu.graphics); + std::printf("kyty_shim_indirect_dispatch_dimensions: ok\n"); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "kyty_shim_indirect_dispatch_dimensions: FAILED: %s\n", error.what()); + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensionsMain.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensionsMain.cpp new file mode 100644 index 0000000000..58ac6278f3 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/IndirectDispatchDimensionsMain.cpp @@ -0,0 +1,3 @@ +extern "C" int RunKytyIndirectDispatchDimensionsTest(); + +int main() { return RunKytyIndirectDispatchDimensionsTest(); } From a40ac8161d40fbd00e8951713066a26a371dcde3 Mon Sep 17 00:00:00 2001 From: mjnhbg3 <2287071+mjnhbg3@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:57:27 -0500 Subject: [PATCH 2/2] test(kyty): omit Vulkan probe from ASan CPU suite --- .../KytyShim/tests/CMakeLists.txt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt index 0ec38e7d01..4d5bdc20d1 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt @@ -267,14 +267,16 @@ if(NOT ANYPS5_KYTY_DLL AND (WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux")) set_tests_properties(kyty_shim_buffer_cache_pages PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77 ENVIRONMENT "KYTY_GPU_PIPELINE=0" PASS_REGULAR_EXPRESSION "kyty_shim_buffer_cache_pages: ok" FAIL_REGULAR_EXPRESSION "FAILED") - target_sources(libSceAgcDriver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensions.cpp) - add_test_executable(kyty_shim_indirect_dispatch_dimensions_tests ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensionsMain.cpp) - configure_host_test_executable(kyty_shim_indirect_dispatch_dimensions_tests) - target_link_libraries(kyty_shim_indirect_dispatch_dimensions_tests PRIVATE libSceAgcDriver libkernel libc) - add_test(NAME kyty_shim_indirect_dispatch_dimensions COMMAND kyty_shim_indirect_dispatch_dimensions_tests) - set_tests_properties(kyty_shim_indirect_dispatch_dimensions PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77 - ENVIRONMENT "KYTY_GPU_PIPELINE=0" - PASS_REGULAR_EXPRESSION "kyty_shim_indirect_dispatch_dimensions: ok" FAIL_REGULAR_EXPRESSION "FAILED") + if(NOT ANYPS5_ENABLE_ASAN) + target_sources(libSceAgcDriver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensions.cpp) + add_test_executable(kyty_shim_indirect_dispatch_dimensions_tests ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensionsMain.cpp) + configure_host_test_executable(kyty_shim_indirect_dispatch_dimensions_tests) + target_link_libraries(kyty_shim_indirect_dispatch_dimensions_tests PRIVATE libSceAgcDriver libkernel libc) + add_test(NAME kyty_shim_indirect_dispatch_dimensions COMMAND kyty_shim_indirect_dispatch_dimensions_tests) + set_tests_properties(kyty_shim_indirect_dispatch_dimensions PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77 + ENVIRONMENT "KYTY_GPU_PIPELINE=0" + PASS_REGULAR_EXPRESSION "kyty_shim_indirect_dispatch_dimensions: ok" FAIL_REGULAR_EXPRESSION "FAILED") + endif() endif() if(WIN32)