Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 3 additions & 4 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ag::AddressGuardTable>();
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;
Expand Down
23 changes: 23 additions & 0 deletions runtime/src/address_guard_buffer.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <span>

namespace leonida::address_guard {

inline VkBufferCreateInfo table_buffer_info(VkDeviceSize bytes, std::span<const std::uint32_t> 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
29 changes: 29 additions & 0 deletions runtime/test/address_guard_buffer_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <cstdio>
#include <vector>

#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<std::uint32_t> 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<std::uint32_t> 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;
}
Loading