From f06577169c4052e6363256896a9b46eb98ffa10f Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:42:00 -0600 Subject: [PATCH] Address guard: share the page-table buffer across queue families create_address_guard() made the page table that shaders translated with --address-guard read through its device address with VK_SHARING_MODE_EXCLUSIVE, though those shaders run on the direct and the async-compute queue. The other buffers every queue reads are shared by the device's queue families (DeviceState::share). The create info now comes from address_guard::table_buffer_info(), which shares it the same way (concurrent on two or more families, exclusive on one), with a CPU test. Found by kevincommits (#1608). Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 10 ++++++++ runtime/shims/d3d12_vulkan_device.cpp | 7 +++--- runtime/src/address_guard_buffer.h | 23 +++++++++++++++++ runtime/test/address_guard_buffer_test.cpp | 29 ++++++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 runtime/src/address_guard_buffer.h create mode 100644 runtime/test/address_guard_buffer_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3c0112060b..452616051c 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2404,6 +2404,16 @@ if(TARGET Vulkan::Headers) if(BUILD_TESTING) add_test(NAME vulkan_scratch COMMAND vulkan_scratch_test) endif() + # CPU-only: the address guard's page-table buffer is shared by the device's queue families + # (runtime/src/address_guard_buffer.h). + add_executable(address_guard_buffer_test test/address_guard_buffer_test.cpp) + target_link_libraries(address_guard_buffer_test PRIVATE Vulkan::Headers) + if(MSVC) + target_compile_options(address_guard_buffer_test PRIVATE /W4 /permissive- /utf-8) + endif() + if(BUILD_TESTING) + add_test(NAME address_guard_buffer COMMAND address_guard_buffer_test) + endif() if(WIN32) # CPU-only: a completed GPU label is stored into a page the D3D12 layer watches (read-only until written) and # dropped for a page that is read-only by the title's own doing (runtime/src/vulkan_gpu_labels.h). diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 748d6144a5..ea2073aa13 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -34,6 +34,7 @@ #include "../src/command_list_epochs.h" #include "../src/value_write_sink.h" #include "../src/address_guard.h" +#include "../src/address_guard_buffer.h" #include "../src/arena_blocks.h" #include "../src/gfx10_descriptors.h" #include "../src/depth24_fragment.h" @@ -6563,10 +6564,8 @@ struct DeviceState { if (!context || !arena.mapped || arena.size < ag::kGuardEnd) return false; auto& vk = context->vk; guard.table = std::make_unique(); - VkBufferCreateInfo create{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; - create.size = guard.table->bytes(); - create.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; - create.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + // Read by guarded shaders on every queue: shared by the device's queue families. + const VkBufferCreateInfo create = ag::table_buffer_info(guard.table->bytes(), families); if (vk.vkCreateBuffer(info.device, &create, nullptr, &guard.buffer) != VK_SUCCESS) { guard.buffer = VK_NULL_HANDLE; return false; diff --git a/runtime/src/address_guard_buffer.h b/runtime/src/address_guard_buffer.h new file mode 100644 index 0000000000..444d0250b0 --- /dev/null +++ b/runtime/src/address_guard_buffer.h @@ -0,0 +1,23 @@ +#pragma once + +// The address guard's page-table buffer (address_guard.h; docs/d3d12_vulkan.md, "Address guard"). Shaders translated +// with --address-guard read it through its device address on every D3D12 queue, so it is shared by all of the +// device's queue families, like the other buffers the device's queues all read. + +#include "queue_families.h" + +#include +#include + +namespace leonida::address_guard { + +inline VkBufferCreateInfo table_buffer_info(VkDeviceSize bytes, std::span families) { + VkBufferCreateInfo create{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + create.size = bytes; + create.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; + create.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + share_across_queue_families(create, families); + return create; +} + +} // namespace leonida::address_guard diff --git a/runtime/test/address_guard_buffer_test.cpp b/runtime/test/address_guard_buffer_test.cpp new file mode 100644 index 0000000000..74d77841a2 --- /dev/null +++ b/runtime/test/address_guard_buffer_test.cpp @@ -0,0 +1,29 @@ +// leonida::address_guard::table_buffer_info (src/address_guard_buffer.h): the address guard's page table is read by +// guarded shaders on the direct and the async-compute queue, so its buffer is shared by every distinct queue family +// of the device, and stays exclusive on a device with one family. No GPU, no device. +#include "../src/address_guard_buffer.h" + +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ +} while (0) + +int main() { + namespace ag = leonida::address_guard; + const std::vector two{0, 2}; + const VkBufferCreateInfo shared = ag::table_buffer_info(0x2000, two); + EXPECT(shared.sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO && shared.size == 0x2000); + EXPECT(shared.usage == (VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)); + EXPECT(shared.sharingMode == VK_SHARING_MODE_CONCURRENT); + EXPECT(shared.queueFamilyIndexCount == 2 && shared.pQueueFamilyIndices == two.data()); + + const std::vector one{0}; + const VkBufferCreateInfo single = ag::table_buffer_info(0x2000, one); + EXPECT(single.sharingMode == VK_SHARING_MODE_EXCLUSIVE && single.queueFamilyIndexCount == 0 && + single.pQueueFamilyIndices == nullptr); + std::puts("address_guard_buffer_test: ok"); + return 0; +}