From 729c1f9fef2a76e9248058b9700d20e1a259d33a Mon Sep 17 00:00:00 2001 From: Ryongen Date: Thu, 26 Nov 2026 08:58:26 +0100 Subject: [PATCH 1/2] fix(kyty): back the device address page table only where buffers are registered The page table holds an entry for every 16 KiB page of the guest address space and took 768 MiB of VRAM. It is now a sparse-resident buffer whose 64 KiB blocks are zeroed and then bound on first write, on devices with strict non-resident reads; elsewhere it stays a plain buffer. --- core/kyty/graphics/host_gpu/graphicContext.h | 1 + .../host_gpu/renderer/cache/bufferCache.cpp | 5 +- .../host_gpu/renderer/cache/streamBuffer.cpp | 161 ++++++++++++++++++ .../host_gpu/renderer/cache/streamBuffer.h | 9 + .../presentation/window/vulkanWindow.cpp | 11 ++ 5 files changed, 185 insertions(+), 2 deletions(-) diff --git a/core/kyty/graphics/host_gpu/graphicContext.h b/core/kyty/graphics/host_gpu/graphicContext.h index a59c2ac1ff4..d578f70bfd8 100644 --- a/core/kyty/graphics/host_gpu/graphicContext.h +++ b/core/kyty/graphics/host_gpu/graphicContext.h @@ -29,6 +29,7 @@ struct GraphicContext { vk::Device device = nullptr; VmaAllocator allocator = nullptr; bool memory_budget_ext_enabled = false; + bool partial_buffers_enabled = false; bool host_import_enabled = false; uint64_t host_import_alignment = 0; bool compute_subgroup_size_control_enabled = false; diff --git a/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp b/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp index 2e6e38998ce..07542bb5dca 100644 --- a/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp +++ b/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp @@ -211,6 +211,7 @@ void BufferCache::ChangeRegister(BufferId id) { for (uint64_t i = 0; i < size_pages; ++i) { addresses.push_back(buffer.BufferDeviceAddress() + (i << CACHING_PAGEBITS)); } + m_bda_pagetable_buffer.Commit(table_offset, addresses.size() * sizeof(vk::DeviceAddress)); WriteDataBuffer(m_bda_pagetable_buffer, table_offset, addresses.data(), addresses.size() * sizeof(vk::DeviceAddress)); } else { @@ -360,8 +361,7 @@ BufferCache::BufferCache(GraphicContext& graphics, CommandScheduler& scheduler, : m_graphics(graphics), m_scheduler(scheduler), m_resources(resources), m_fault_manager(graphics, scheduler, *this), m_gds_buffer(graphics, scheduler, MemoryUsage::Stream, 0, AllFlags, GdsBufferSize), - m_bda_pagetable_buffer(graphics, scheduler, MemoryUsage::DeviceLocal, 0, AllFlags, - BDA_PAGETABLE_SIZE), + m_bda_pagetable_buffer(graphics, scheduler, Buffer::SparseResident {}, AllFlags, BDA_PAGETABLE_SIZE), m_memory_tracker(page_manager), m_staging_buffer(graphics, scheduler, MemoryUsage::Upload, 512 * MiB), m_stream_buffer(graphics, scheduler, MemoryUsage::Stream, 64 * MiB), @@ -379,6 +379,7 @@ BufferCache::BufferCache(GraphicContext& graphics, CommandScheduler& scheduler, for (auto* buffer: {&m_bda_pagetable_buffer, m_fault_manager.GetFaultBuffer()}) { buffer->Fill(0, buffer->Size(), 0); } + m_bda_pagetable_buffer.Commit(NATIVE_RT_CONTEXT_PAGE * sizeof(vk::DeviceAddress), sizeof(vk::DeviceAddress)); const auto null_id = m_slot_buffers.insert(m_graphics, m_scheduler, MemoryUsage::DeviceLocal, 0, AllFlags, 16); EXIT_IF(null_id != NULL_BUFFER_ID); diff --git a/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.cpp b/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.cpp index 1ee46949b92..e166d174a01 100644 --- a/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.cpp +++ b/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.cpp @@ -7,6 +7,8 @@ #include "graphics/host_gpu/graphicContext.h" #include "graphics/host_gpu/renderer/commandScheduler.h" +#include +#include #include #include #include @@ -63,7 +65,157 @@ Buffer::Buffer(GraphicContext& graphics, CommandScheduler& scheduler, MemoryUsag uint64_t cpu_address, vk::BufferUsageFlags flags, uint64_t size) : m_graphics(&graphics), m_scheduler(&scheduler), m_usage(usage), m_cpu_address(cpu_address), m_size(size) { + Create(flags); +} + +Buffer::Buffer(GraphicContext& graphics, CommandScheduler& scheduler, SparseResident, + vk::BufferUsageFlags flags, uint64_t size) + : m_graphics(&graphics), m_scheduler(&scheduler), m_usage(MemoryUsage::DeviceLocal), m_size(size) { + if (!graphics.partial_buffers_enabled || (flags & vk::BufferUsageFlagBits::eShaderDeviceAddress)) { + Create(flags); + return; + } + vk::BufferCreateInfo buffer_info {}; + buffer_info.flags = vk::BufferCreateFlagBits::eSparseBinding | vk::BufferCreateFlagBits::eSparseResidency; + buffer_info.size = size; + buffer_info.usage = flags; + const std::array families {graphics.queue_family, graphics.readback_family}; + if (graphics.readback_family != static_cast(-1)) { + buffer_info.sharingMode = vk::SharingMode::eConcurrent; + buffer_info.queueFamilyIndexCount = 2; + buffer_info.pQueueFamilyIndices = families.data(); + } + if (graphics.device.createBuffer(&buffer_info, nullptr, &m_buffer) != vk::Result::eSuccess) { + m_buffer = nullptr; + Create(flags); + return; + } + const auto requirements = graphics.device.getBufferMemoryRequirements(m_buffer); + m_sparse_block = requirements.alignment; + m_sparse_types = requirements.memoryTypeBits; + m_sparse_size = requirements.size; + m_sparse_blocks.assign((m_sparse_size + m_sparse_block - 1) / m_sparse_block, nullptr); +#if defined(ANYPS5_KYTY) + m_device_local = true; +#endif +} + +void Buffer::Commit(uint64_t offset, uint64_t size) { + if (m_sparse_block == 0 || size == 0) { + return; + } + EXIT_IF(offset > m_size || size > m_size - offset); + auto& graphics = *m_graphics; + const auto first = offset / m_sparse_block; + const auto last = (offset + size - 1) / m_sparse_block; + std::vector blocks; + for (auto block = first; block <= last; block++) { + if (m_sparse_blocks[block] == nullptr) { + blocks.push_back(block); + } + } + if (blocks.empty()) { + return; + } + vk::BufferCreateInfo zero_info {}; + zero_info.size = m_sparse_block; + zero_info.usage = vk::BufferUsageFlagBits::eTransferDst; + std::vector zero_buffers(blocks.size(), nullptr); + std::vector binds; + binds.reserve(blocks.size()); + for (size_t i = 0; i < blocks.size(); i++) { + const auto block = blocks[i]; + EXIT_IF(graphics.device.createBuffer(&zero_info, nullptr, &zero_buffers[i]) != vk::Result::eSuccess); + const auto zero_requirements = graphics.device.getBufferMemoryRequirements(zero_buffers[i]); + VkMemoryRequirements memory {}; + memory.size = std::max(m_sparse_block, zero_requirements.size); + memory.alignment = std::max(m_sparse_block, zero_requirements.alignment); + memory.memoryTypeBits = m_sparse_types & zero_requirements.memoryTypeBits; + VmaAllocationCreateInfo alloc_info {}; + alloc_info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + VmaAllocationInfo allocated {}; + const auto result = + vmaAllocateMemory(graphics.allocator, &memory, &alloc_info, &m_sparse_blocks[block], &allocated); + if (result != VK_SUCCESS) { + std::array heaps {}; + const auto heap_count = graphics.GetHeapUsage(heaps); + for (uint32_t heap = 0; heap < heap_count; heap++) { + LOGF("Buffer: heap %u%s: %" PRIu64 " MiB used of a %" PRIu64 " MiB budget\n", heap, + heaps[heap].device_local ? " (device local)" : "", heaps[heap].usage >> 20u, + heaps[heap].budget >> 20u); + } + EXIT("Buffer: could not allocate sparse block %" PRIu64 " of %" PRIu64 " bytes: %s\n", block, + m_sparse_block, vk::to_string(static_cast(result)).c_str()); + } + EXIT_IF(graphics.device.bindBufferMemory(zero_buffers[i], allocated.deviceMemory, allocated.offset) != + vk::Result::eSuccess); + vk::SparseMemoryBind bind {}; + bind.resourceOffset = block * m_sparse_block; + bind.size = std::min(m_sparse_block, m_sparse_size - block * m_sparse_block); + bind.memory = allocated.deviceMemory; + bind.memoryOffset = allocated.offset; + binds.push_back(bind); + } + vk::CommandPoolCreateInfo pool_info {}; + pool_info.flags = vk::CommandPoolCreateFlagBits::eTransient; + pool_info.queueFamilyIndex = graphics.queue_family; + vk::CommandPool pool = nullptr; + EXIT_IF(graphics.device.createCommandPool(&pool_info, nullptr, &pool) != vk::Result::eSuccess); + vk::CommandBufferAllocateInfo allocate {}; + allocate.commandPool = pool; + allocate.level = vk::CommandBufferLevel::ePrimary; + allocate.commandBufferCount = 1; + vk::CommandBuffer command = nullptr; + EXIT_IF(graphics.device.allocateCommandBuffers(&allocate, &command) != vk::Result::eSuccess); + vk::CommandBufferBeginInfo begin_info {}; + begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit; + EXIT_IF(command.begin(&begin_info) != vk::Result::eSuccess); + for (const auto zero_buffer: zero_buffers) { + command.fillBuffer(zero_buffer, 0, VK_WHOLE_SIZE, 0); + } + EXIT_IF(command.end() != vk::Result::eSuccess); + vk::SemaphoreCreateInfo semaphore_info {}; + vk::Semaphore zeroed = nullptr; + EXIT_IF(graphics.device.createSemaphore(&semaphore_info, nullptr, &zeroed) != vk::Result::eSuccess); + vk::FenceCreateInfo fence_info {}; + vk::Fence fence = nullptr; + EXIT_IF(graphics.device.createFence(&fence_info, nullptr, &fence) != vk::Result::eSuccess); + vk::SubmitInfo submit {}; + submit.commandBufferCount = 1; + submit.pCommandBuffers = &command; + submit.signalSemaphoreCount = 1; + submit.pSignalSemaphores = &zeroed; + vk::SparseBufferMemoryBindInfo buffer_bind {m_buffer, static_cast(binds.size()), binds.data()}; + vk::BindSparseInfo bind_info {}; + bind_info.waitSemaphoreCount = 1; + bind_info.pWaitSemaphores = &zeroed; + bind_info.bufferBindCount = 1; + bind_info.pBufferBinds = &buffer_bind; + { + Common::LockGuard lock {graphics.queue_mutex}; + EXIT_IF(graphics.queue.submit(1, &submit, nullptr) != vk::Result::eSuccess); + const auto result = graphics.queue.bindSparse(1, &bind_info, fence); + if (result != vk::Result::eSuccess) { + EXIT("Buffer: sparse bind of %zu blocks at 0x%" PRIx64 " failed: %s (block %" PRIu64 + ", memory size %" PRIu64 ", buffer size %" PRIu64 ")\n", + binds.size(), binds.front().resourceOffset, vk::to_string(result).c_str(), m_sparse_block, + m_sparse_size, m_size); + } + } + EXIT_IF(graphics.device.waitForFences(1, &fence, VK_TRUE, UINT64_MAX) != vk::Result::eSuccess); + graphics.device.destroyFence(fence, nullptr); + graphics.device.destroySemaphore(zeroed, nullptr); + graphics.device.destroyCommandPool(pool, nullptr); + for (const auto zero_buffer: zero_buffers) { + graphics.device.destroyBuffer(zero_buffer, nullptr); + } +} + +void Buffer::Create(vk::BufferUsageFlags flags) { KYTY_PROFILER_FUNCTION(); + auto& graphics = *m_graphics; + const auto usage = m_usage; + const auto size = m_size; EXIT_IF(graphics.allocator == nullptr || size == 0); vk::BufferCreateInfo buffer_info {}; @@ -138,6 +290,15 @@ Buffer::~Buffer() { if (m_buffer == nullptr) { return; } + if (m_sparse_block != 0) { + m_graphics->device.destroyBuffer(m_buffer); + for (auto* block: m_sparse_blocks) { + if (block != nullptr) { + vmaFreeMemory(m_graphics->allocator, block); + } + } + return; + } if (m_in_place) { m_graphics->device.destroyBuffer(m_buffer); } else { diff --git a/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h b/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h index 0975ae54ebe..304fb1d4499 100644 --- a/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h +++ b/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h @@ -43,6 +43,9 @@ class Buffer { uint64_t cpu_address, vk::BufferUsageFlags flags, uint64_t size); Buffer(GraphicContext& graphics, CommandScheduler& scheduler, uint64_t cpu_address, uint64_t size, vk::Buffer imported, vk::DeviceAddress device_address); + struct SparseResident {}; + Buffer(GraphicContext& graphics, CommandScheduler& scheduler, SparseResident sparse, vk::BufferUsageFlags flags, + uint64_t size); ~Buffer(); KYTY_CLASS_NO_COPY(Buffer); @@ -74,6 +77,7 @@ class Buffer { void CopyFrom(CommandBuffer& command, const Buffer& source, uint64_t source_offset, uint64_t destination_offset, uint64_t size); void Fill(uint64_t offset, uint64_t size, uint32_t value); + void Commit(uint64_t offset, uint64_t size); bool is_deleted = false; int stream_score = 0; @@ -86,6 +90,7 @@ class Buffer { [[nodiscard]] CommandScheduler& Scheduler() const noexcept { return *m_scheduler; } private: + void Create(vk::BufferUsageFlags flags); [[nodiscard]] vk::BufferMemoryBarrier Barrier(uint64_t offset, uint64_t size, vk::AccessFlags source, vk::AccessFlags destination) const; @@ -104,6 +109,10 @@ class Buffer { bool m_device_local = false; #endif std::span m_mapped; + uint64_t m_sparse_block = 0; + uint32_t m_sparse_types = 0; + uint64_t m_sparse_size = 0; + std::vector m_sparse_blocks; }; class StreamBuffer final: public Buffer { diff --git a/core/kyty/graphics/presentation/window/vulkanWindow.cpp b/core/kyty/graphics/presentation/window/vulkanWindow.cpp index 45d6f1522fa..d22c5b50c0d 100644 --- a/core/kyty/graphics/presentation/window/vulkanWindow.cpp +++ b/core/kyty/graphics/presentation/window/vulkanWindow.cpp @@ -707,6 +707,17 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics, graphics.indirect_draws_enabled = device_features.multiDrawIndirect == VK_TRUE && device_features.drawIndirectFirstInstance == VK_TRUE && features12.drawIndirectCount == VK_TRUE; + { + const auto families = physical_device.getQueueFamilyProperties(); + const auto sparse = physical_device.getProperties().sparseProperties; + graphics.partial_buffers_enabled = + supported2.features.sparseBinding == VK_TRUE && supported2.features.sparseResidencyBuffer == VK_TRUE && + sparse.residencyNonResidentStrict == VK_TRUE && queue_family < families.size() && + static_cast(families[queue_family].queueFlags & vk::QueueFlagBits::eSparseBinding); + device_features.sparseBinding = graphics.partial_buffers_enabled ? VK_TRUE : VK_FALSE; + device_features.sparseResidencyBuffer = graphics.partial_buffers_enabled ? VK_TRUE : VK_FALSE; + LOGF("Vulkan partially resident buffers: %s\n", graphics.partial_buffers_enabled ? "true" : "false"); + } device_features.shaderInt64 = VK_TRUE; device_features.shaderFloat64 = supported2.features.shaderFloat64 && float_controls.shaderSignedZeroInfNanPreserveFloat64; From 6b2320a40c04048bf10fc2e0e953fcbdce0ca0b3 Mon Sep 17 00:00:00 2001 From: Ryongen Date: Thu, 26 Nov 2026 17:46:16 +0100 Subject: [PATCH 2/2] test(kyty): cover the device address page table with and without sparse residency Checks that unbound and untouched entries read 0, registered pages hold their device addresses and removed ones read 0 again, on a sparse-resident table and on the dense fallback. --- .../host_gpu/renderer/cache/bufferCache.h | 1 + .../host_gpu/renderer/cache/streamBuffer.h | 1 + .../KytyShim/tests/BdaPageTable.cpp | 161 ++++++++++++++++++ .../KytyShim/tests/BdaPageTableMain.cpp | 5 + .../KytyShim/tests/CMakeLists.txt | 10 ++ .../KytyShim/tests/TestVulkanDevice.h | 15 +- 6 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTable.cpp create mode 100644 core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTableMain.cpp diff --git a/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h b/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h index 94ba4de757d..0d7f5c0f934 100644 --- a/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h +++ b/core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h @@ -293,6 +293,7 @@ class BufferCache { friend struct BufferCacheTestAccess; friend struct BufferCacheGcTestAccess; friend struct IndirectDispatchTestAccess; + friend struct BufferCacheBdaTestAccess; friend class TextureCache; [[nodiscard]] std::pair ObtainSynchronizedBuffer( diff --git a/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h b/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h index 304fb1d4499..3caecb82d3c 100644 --- a/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h +++ b/core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h @@ -58,6 +58,7 @@ class Buffer { #endif [[nodiscard]] MemoryUsage Usage() const noexcept { return m_usage; } [[nodiscard]] bool InPlace() const noexcept { return m_in_place; } + [[nodiscard]] bool IsSparseResident() const noexcept { return m_sparse_block != 0; } [[nodiscard]] uint64_t CpuAddress() const noexcept { return m_cpu_address; } [[nodiscard]] vk::DeviceAddress BufferDeviceAddress() const noexcept { EXIT_IF(m_device_address == 0); diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTable.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTable.cpp new file mode 100644 index 00000000000..4dc1e69914f --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTable.cpp @@ -0,0 +1,161 @@ +#include "TestVulkanDevice.h" +#include "common/emulatorConfig.h" +#include "common/threads.h" +#include "graphics/guest_gpu/command_processor/commandProcessor.h" +#include "graphics/host_gpu/renderer/cache/bufferCache.h" +#include "graphics/host_gpu/renderer/renderContext.h" +#include "prx/libkernel/DirectMemory/DirectMemory.hpp" +#include "prx/libkernel/include/Api.hpp" + +#include +#include +#include +#include +#include +#include + +namespace Libs::Graphics { + +struct BufferCacheBdaTestAccess { + static uint64_t PageIndex(uint64_t address) { return BufferCache::PageIndex(address); } +}; + +} + +namespace { + +using namespace Libs::Graphics; + +constexpr uint64_t Page = BufferCache::CACHING_PAGESIZE; +constexpr uint64_t TableBlock = 0x10000; +constexpr uint64_t Entry = sizeof(vk::DeviceAddress); +constexpr uint64_t GuestBytes = 3 * Page; +constexpr uint64_t FarBlocks = 16; +constexpr int CpuReadWrite = 0x03; + +const char* g_name = "kyty_shim_bda_page_table"; + +std::vector ReadTable(GraphicContext& graphics, CommandScheduler& scheduler, Buffer& table, uint64_t offset, + uint64_t size) { + Buffer download(graphics, scheduler, MemoryUsage::Download, 0, vk::BufferUsageFlagBits::eTransferDst, size); + download.CopyFrom(scheduler.Current(), table, offset, 0, size); + scheduler.Finish(); + download.Invalidate(0, size); + std::vector entries(size / Entry); + std::memcpy(entries.data(), download.Mapped().data(), size); + return entries; +} + +uint64_t BlockOffset(uint64_t page_index) { + return page_index * Entry / TableBlock * TableBlock; +} + +const char* CheckZero(const std::vector& entries, const char* failure) { + for (const auto entry: entries) { + if (entry != 0) { + return failure; + } + } + return nullptr; +} + +const char* CheckPageTable(GraphicContext& graphics, uint64_t address, bool sparse) { + RenderContext renderer(graphics); + CommandProcessor producer(renderer, 0); + producer.Reset(); + auto& scheduler = renderer.GetCommandScheduler(); + auto& buffers = renderer.GetBufferCache(); + scheduler.Begin(producer.GetCtx(), producer.GetUcfg(), producer.GetShCtx()); + auto& table = *buffers.GetBdaPageTableBuffer(); + + const char* failure = nullptr; + if (table.IsSparseResident() != sparse) { + failure = sparse ? "the page table is not sparse-resident on a device with sparse residency buffers" + : "the page table is sparse-resident without sparse residency buffers"; + } + const auto first_page = BufferCacheBdaTestAccess::PageIndex(address); + const auto block = BlockOffset(first_page); + const auto far_block = block + FarBlocks * TableBlock; + if (failure == nullptr && far_block + TableBlock <= table.Size()) { + failure = CheckZero(ReadTable(graphics, scheduler, table, far_block, TableBlock), + "an entry of a page table block no buffer was registered in did not read 0"); + } + if (failure == nullptr) { + failure = CheckZero(ReadTable(graphics, scheduler, table, block, TableBlock), + "an entry read before any buffer was registered in its block was not 0"); + } + if (failure == nullptr) { + (void)buffers.ObtainBuffer(address, GuestBytes, false, false); + const auto id = buffers.RegisteredBuffer(address, Page); + if (!id) { + failure = "the buffer was not registered"; + } else { + const auto& buffer = buffers.GetBuffer(id); + const auto base = BufferCacheBdaTestAccess::PageIndex(buffer.CpuAddress()); + const auto pages = (buffer.Size() + Page - 1) / Page; + const auto entries = ReadTable(graphics, scheduler, table, block, TableBlock); + const auto first = block / Entry; + for (uint64_t index = 0; index < entries.size() && failure == nullptr; index++) { + const auto page = first + index; + const bool inside = page >= base && page < base + pages; + const auto expected = inside ? buffer.BufferDeviceAddress() + (page - base) * Page : 0; + if (entries[index] != expected) { + failure = inside ? "a registered page did not hold its buffer's device address" + : "an entry next to a registered buffer in a newly backed block was not 0"; + } + } + } + } + (void)buffers.DropFreedRange(address, GuestBytes, nullptr, nullptr); + if (failure == nullptr) { + failure = CheckZero(ReadTable(graphics, scheduler, table, block, TableBlock), + "an entry of a removed buffer did not read 0 again"); + } + const auto last = scheduler.CurrentTick(); + scheduler.Wait(last); + scheduler.WaitPriorityOperations(last); + return failure; +} + +} + +extern "C" int RunKytyBdaPageTableTest(int argc, char** argv) { + const bool sparse = argc > 1 && std::string_view(argv[1]) == "sparse"; + g_name = sparse ? "kyty_shim_bda_page_table_sparse" : "kyty_shim_bda_page_table_dense"; + Common::InitializeThreads(); + Config::Initialize(); + Config::Load(Config::ConfigOptions {}); + KytyShimTests::TestVulkanDevice gpu; + if (const auto* unavailable = gpu.Initialize(false, sparse)) { + const bool required = std::getenv("ANYPS5_REQUIRE_VULKAN") != nullptr; + std::fprintf(stderr, "%s: %s, no usable Vulkan device (%s)\n", g_name, required ? "FAILED" : "skipped", + unavailable); + return required ? 1 : 77; + } + if (sparse && !gpu.graphics.partial_buffers_enabled) { + std::fprintf(stderr, "%s: skipped, the device has no strict sparse residency buffers\n", g_name); + return 77; + } + if (!gpu.graphics.CreateAllocator()) { + std::fprintf(stderr, "%s: FAILED: could not create the allocator\n", g_name); + return 1; + } + void* guest = nullptr; + constexpr uint64_t Mapped = GuestBytes + Page; + if (sceKernelMapFlexibleMemory(&guest, Mapped, CpuReadWrite, 0) != 0 || guest == nullptr) { + std::fprintf(stderr, "%s: FAILED: could not map guest backing\n", g_name); + return 1; + } + const auto address = (reinterpret_cast(guest) + Page - 1) / Page * Page; + const auto* failure = CheckPageTable(gpu.graphics, address, sparse); + if (sceKernelMunmap(reinterpret_cast(guest), Mapped) != 0) { + std::fprintf(stderr, "%s: FAILED: could not unmap guest backing\n", g_name); + return 1; + } + if (failure != nullptr) { + std::fprintf(stderr, "%s: FAILED: %s\n", g_name, failure); + return 1; + } + std::printf("%s: ok\n", g_name); + return 0; +} diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTableMain.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTableMain.cpp new file mode 100644 index 00000000000..c22d338532f --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/BdaPageTableMain.cpp @@ -0,0 +1,5 @@ +extern "C" int RunKytyBdaPageTableTest(int argc, char** argv); + +int main(int argc, char** argv) { + return RunKytyBdaPageTableTest(argc, argv); +} diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt index 4d5bdc20d14..55c95170fcf 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt @@ -277,6 +277,16 @@ if(NOT ANYPS5_KYTY_DLL AND (WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux")) ENVIRONMENT "KYTY_GPU_PIPELINE=0" PASS_REGULAR_EXPRESSION "kyty_shim_indirect_dispatch_dimensions: ok" FAIL_REGULAR_EXPRESSION "FAILED") endif() + target_sources(libSceAgcDriver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/BdaPageTable.cpp) + add_test_executable(kyty_shim_bda_page_table_tests ${CMAKE_CURRENT_SOURCE_DIR}/BdaPageTableMain.cpp) + configure_host_test_executable(kyty_shim_bda_page_table_tests) + target_link_libraries(kyty_shim_bda_page_table_tests PRIVATE libSceAgcDriver libkernel libc) + foreach(mode sparse dense) + add_test(NAME kyty_shim_bda_page_table_${mode} COMMAND kyty_shim_bda_page_table_tests ${mode}) + set_tests_properties(kyty_shim_bda_page_table_${mode} PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77 + ENVIRONMENT "KYTY_GPU_PIPELINE=0" + PASS_REGULAR_EXPRESSION "kyty_shim_bda_page_table_${mode}: ok" FAIL_REGULAR_EXPRESSION "FAILED") + endforeach() endif() if(WIN32) diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/TestVulkanDevice.h b/core/libs/prx/libSceAgcDriver/KytyShim/tests/TestVulkanDevice.h index d7882e154e7..8d0516b941d 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/tests/TestVulkanDevice.h +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/TestVulkanDevice.h @@ -15,7 +15,7 @@ struct TestVulkanDevice { vk::detail::DynamicLoader loader; Libs::Graphics::GraphicContext graphics; - const char* Initialize(bool require_tiling = false) { + const char* Initialize(bool require_tiling = false, bool sparse_buffers = false) { if (!loader.success()) { return "Vulkan loader unavailable"; } @@ -36,7 +36,7 @@ struct TestVulkanDevice { return "Vulkan device enumeration failed"; } for (const auto device : devices.value) { - if (InitializeDevice(device, require_tiling)) { + if (InitializeDevice(device, require_tiling, sparse_buffers)) { return nullptr; } } @@ -56,7 +56,7 @@ struct TestVulkanDevice { } private: - bool InitializeDevice(vk::PhysicalDevice physical, bool require_tiling) { + bool InitializeDevice(vk::PhysicalDevice physical, bool require_tiling, bool sparse_buffers) { vk::PhysicalDeviceVulkan12Properties limits {}; vk::PhysicalDeviceVulkan11Properties properties11 {.pNext = &limits}; vk::PhysicalDeviceProperties2 properties {.pNext = &properties11}; @@ -117,10 +117,16 @@ struct TestVulkanDevice { .pNext = &features12, .storageBuffer16BitAccess = require_tiling}; const vk::PhysicalDeviceVulkan13Features features13 { .pNext = &features11, .synchronization2 = VK_TRUE, .dynamicRendering = VK_TRUE}; + const bool sparse = sparse_buffers && supported.features.sparseBinding && + supported.features.sparseResidencyBuffer && + properties.properties.sparseProperties.residencyNonResidentStrict && + static_cast(family->queueFlags & vk::QueueFlagBits::eSparseBinding); const vk::PhysicalDeviceFeatures core_features { .sampleRateShading = VK_TRUE, .shaderStorageImageMultisample = supported.features.shaderStorageImageMultisample, - .shaderInt64 = VK_TRUE}; + .shaderInt64 = VK_TRUE, + .sparseBinding = sparse ? VK_TRUE : VK_FALSE, + .sparseResidencyBuffer = sparse ? VK_TRUE : VK_FALSE}; const float priority = 1.0f; const vk::DeviceQueueCreateInfo queue_info { .queueFamilyIndex = static_cast(family - families.begin()), @@ -145,6 +151,7 @@ struct TestVulkanDevice { graphics.queue_family = queue_info.queueFamilyIndex; graphics.device.getQueue(graphics.queue_family, 0, &graphics.queue); graphics.occlusion_queries_enabled = true; + graphics.partial_buffers_enabled = sparse; return true; } };