Skip to content
Closed
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
4 changes: 4 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,10 @@ if(TARGET Vulkan::Headers)
add_executable(vulkan_device_markers_test test/vulkan_device_markers_test.cpp)
target_link_libraries(vulkan_device_markers_test PRIVATE Vulkan::Headers)
add_test(NAME vulkan_device_markers COMMAND vulkan_device_markers_test)
# The depth24 converter's scratch chunks are shared by the device's queue families, on the CPU (faked driver).
add_executable(depth24_scratch_sharing_test test/depth24_scratch_sharing_test.cpp)
target_link_libraries(depth24_scratch_sharing_test PRIVATE Vulkan::Headers)
add_test(NAME depth24_scratch_sharing COMMAND depth24_scratch_sharing_test)
# The submit lock's owner tracking, waiter count, submit handoff and compile gate (SubmitMutex), on the CPU.
if(WIN32)
add_executable(submit_mutex_test test/submit_mutex_test.cpp)
Expand Down
4 changes: 2 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14752,7 +14752,7 @@ class Device final
if (info.device_fault_diagnostics) {
state_.context->configure_device_fault(get_device,
{info.device_fault_info, info.device_checkpoints, info.device_diagnostics_config, info.device_markers},
&state_.memory);
&state_.memory, state_.families);
state_.context->add_diagnostic_queue(info.queue);
for (auto queue : info.command_queues) state_.context->add_diagnostic_queue(queue);
for (auto queue : info.compute_queues) state_.context->add_diagnostic_queue(queue);
Expand Down Expand Up @@ -15051,7 +15051,7 @@ class Device final
}
state_.context->enable_depth24(get_device, [this](const VkMemoryRequirements& requirements) {
return state_.allocate(requirements, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
}, max_storage_range);
}, max_storage_range, state_.families);
}
if (info.bindless && state_.context->loaded() && !create_bindless(get_device))
std::fprintf(stderr, "[rt] d3d12: the bindless set 0 could not be created\n");
Expand Down
16 changes: 10 additions & 6 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1325,11 +1325,14 @@ class VulkanContext {

bool loaded() const { return loaded_; }

// `memory`: the physical device's memory types, for the AMD marker buffer (enabled.markers).
// `memory`: the physical device's memory types, for the AMD marker buffer (enabled.markers); `families`: the
// device's distinct queue families, which all write that buffer.
void configure_device_fault(PFN_vkGetDeviceProcAddr get, DeviceFaultExtensions enabled,
const VkPhysicalDeviceMemoryProperties* memory = nullptr) {
const VkPhysicalDeviceMemoryProperties* memory = nullptr,
std::vector<std::uint32_t> families = {}) {
device_fault_ = std::make_unique<DeviceFaultDiagnostics>(get, device_, enabled, memory,
vk.vkCmdPipelineBarrier2 != nullptr);
vk.vkCmdPipelineBarrier2 != nullptr,
std::move(families));
device_fault_->set_submit_lock(&submit_mutex_);
if (fence_dispatcher_) fence_dispatcher_->set_device_fault(device_fault_.get());
if (loaded_) progress_watch_ = std::jthread([this](std::stop_token stop) { watch_queue_progress(stop); });
Expand Down Expand Up @@ -1911,12 +1914,13 @@ class VulkanContext {
}

// D24_UNORM_S8_UINT stored as D32: copies of its depth plane run a compute pass (vulkan_depth24.h). `allocate`
// gives the scratch buffers device-local memory. Without this such a copy is refused as unsupported.
// gives the scratch buffers device-local memory, shared by `families` (the device's distinct queue families).
// Without this such a copy is refused as unsupported.
void enable_depth24(PFN_vkGetDeviceProcAddr get, Depth24Converter::Allocate allocate,
std::uint64_t max_storage_range = 1u << 27) {
std::uint64_t max_storage_range = 1u << 27, std::vector<std::uint32_t> families = {}) {
std::unique_lock lock(resource_mutex_);
depth24_ = std::make_unique<Depth24Converter>(get, device_, std::move(allocate), max_storage_range,
device_fault_.get());
device_fault_.get(), std::move(families));
if (!depth24_->loaded()) {
depth24_.reset();
return;
Expand Down
11 changes: 9 additions & 2 deletions runtime/src/vulkan_depth24.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "depth24.h"
#include "depth24_convert_spv.h"
#include "queue_families.h"
#include "vulkan_aftermath.h"

namespace leonida {
Expand All @@ -32,9 +33,13 @@ class Depth24Converter {
using Allocate = std::function<VkDeviceMemory(const VkMemoryRequirements&)>;

// `max_range`: the device's maxStorageBufferRange; a copy is split into dispatches that stay inside it.
// `families`: the device's distinct queue families. A scratch chunk is reused by copies on any D3D12 queue
// (direct, compute or copy), so with more than one family it is made CONCURRENT over all of them.
Depth24Converter(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate,
std::uint64_t max_range = 1u << 27, DeviceFaultDiagnostics* diagnostics = nullptr)
: device_(device), diagnostics_(diagnostics), allocate_(std::move(allocate)), max_range_(max_range) {
std::uint64_t max_range = 1u << 27, DeviceFaultDiagnostics* diagnostics = nullptr,
std::vector<std::uint32_t> families = {})
: device_(device), diagnostics_(diagnostics), allocate_(std::move(allocate)), max_range_(max_range),
families_(std::move(families)) {
loaded_ = vk_.load(get, device);
}

Expand Down Expand Up @@ -335,6 +340,7 @@ class Depth24Converter {
VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
info.size = size;
info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
share_across_queue_families(info, families_);
if (vk_.vkCreateBuffer(device_, &info, nullptr, &out.buffer) != VK_SUCCESS) return false;
VkMemoryRequirements requirements{};
vk_.vkGetBufferMemoryRequirements(device_, out.buffer, &requirements);
Expand Down Expand Up @@ -364,6 +370,7 @@ class Depth24Converter {
DeviceFaultDiagnostics* diagnostics_;
Allocate allocate_;
std::uint64_t max_range_;
std::vector<std::uint32_t> families_;
Functions vk_;
bool loaded_ = false;
VkDescriptorSetLayout set_layout_ = VK_NULL_HANDLE;
Expand Down
15 changes: 8 additions & 7 deletions runtime/src/vulkan_device_fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -1155,10 +1155,11 @@ struct SubmitRecord {

class DeviceFaultDiagnostics {
public:
// `memory` and `synchronization2` are for the AMD markers (enabled.markers): where their buffer goes and whether
// vkCmdWriteBufferMarker2AMD may be used.
// `memory`, `synchronization2` and `families` are for the AMD markers (enabled.markers): where their buffer goes,
// whether vkCmdWriteBufferMarker2AMD may be used, and the device's distinct queue families, which all write it.
DeviceFaultDiagnostics(PFN_vkGetDeviceProcAddr get, VkDevice device, DeviceFaultExtensions enabled,
const VkPhysicalDeviceMemoryProperties* memory = nullptr, bool synchronization2 = false)
const VkPhysicalDeviceMemoryProperties* memory = nullptr, bool synchronization2 = false,
std::vector<std::uint32_t> families = {})
: device_(device) {
#define LEONIDA_FAULT_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name))
if (enabled.fault) LEONIDA_FAULT_LOAD(vkGetDeviceFaultInfoEXT);
Expand All @@ -1171,7 +1172,7 @@ class DeviceFaultDiagnostics {
std::fprintf(stderr, "[rt] device-fault: enabled (fault info %s, checkpoints %s, NVIDIA diagnostics config %s)\n",
vkGetDeviceFaultInfoEXT ? "on" : "unavailable", checkpoint_command() ? "on" : "unavailable",
enabled.diagnostics_config ? "on" : "unavailable");
if (enabled.markers) enable_markers(get, memory, synchronization2);
if (enabled.markers) enable_markers(get, memory, synchronization2, families);
else if (device_markers_requested())
std::fprintf(stderr, "[rt] device-markers: unavailable: the device has no VK_AMD_buffer_marker\n");
}
Expand Down Expand Up @@ -1452,10 +1453,10 @@ class DeviceFaultDiagnostics {

private:
void enable_markers(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties* memory,
bool synchronization2) {
bool synchronization2, std::span<const std::uint32_t> families) {
auto markers = std::make_unique<DeviceMarkers>();
const VkResult result =
memory ? markers->create(get, device_, *memory, synchronization2) : VK_ERROR_INITIALIZATION_FAILED;
const VkResult result = memory ? markers->create(get, device_, *memory, synchronization2, families)
: VK_ERROR_INITIALIZATION_FAILED;
if (result != VK_SUCCESS) {
std::fprintf(stderr, "[rt] device-markers: unavailable: the marker buffer could not be made (%d)\n",
int(result));
Expand Down
11 changes: 8 additions & 3 deletions runtime/src/vulkan_device_markers.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <span>
#include <vector>

#include "queue_families.h"

namespace leonida {

inline bool device_markers_requested() {
Expand Down Expand Up @@ -145,9 +147,9 @@ class DeviceMarkers {
if (memory_ && free_memory_) free_memory_(device_, memory_, nullptr);
}

// Returns the VkResult of the step that failed, or VK_SUCCESS.
// Returns the VkResult of the step that failed, or VK_SUCCESS. `families`: the device's distinct queue families.
VkResult create(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceMemoryProperties& memory,
bool synchronization2) {
bool synchronization2, std::span<const std::uint32_t> families = {}) {
device_ = device;
#define LEONIDA_MARKER_LOAD(type, name) const auto name = reinterpret_cast<type>(get(device, #name))
LEONIDA_MARKER_LOAD(PFN_vkCreateBuffer, vkCreateBuffer);
Expand All @@ -169,7 +171,10 @@ class DeviceMarkers {
VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
info.size = VkDeviceSize{8} * max_queues;
info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; // written by every queue, never read on the GPU
// Every queue writes it (it is never read on the GPU), whichever family the queue is on: CONCURRENT over the
// device's families when there is more than one, as two families may not use an EXCLUSIVE buffer without an
// ownership transfer.
share_across_queue_families(info, families);
VkResult result = vkCreateBuffer(device, &info, nullptr, &buffer_);
if (result != VK_SUCCESS) return result;
VkMemoryRequirements requirements{};
Expand Down
166 changes: 166 additions & 0 deletions runtime/test/depth24_scratch_sharing_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// The depth24 converter's scratch chunks (src/vulkan_depth24.h) are reused by copies on every D3D12 queue, direct,
// compute and copy, which may be three Vulkan queue families: with two or more families a chunk is CONCURRENT over
// all of them, as the device's other shared pools are (ScratchBuffers, IndirectArgumentClamp, BindlessSet); with one
// family, or none given, it stays EXCLUSIVE. On the CPU: the driver is faked and the chunk's VkBufferCreateInfo is
// logged. No GPU, no game data.
#include "../src/vulkan_depth24.h"

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <type_traits>
#include <vector>

#define EXPECT(condition) do { \
if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
} while (0)

using namespace leonida;

namespace {

template<typename Handle>
Handle fake_handle(std::uintptr_t value) {
if constexpr (std::is_pointer_v<Handle>) return reinterpret_cast<Handle>(value);
else return Handle(value);
}

struct Created {
VkDeviceSize size;
VkSharingMode sharing;
std::vector<std::uint32_t> families;
};
std::vector<Created> buffers;
std::uintptr_t next_handle = 0x1000;

VKAPI_ATTR VkResult VKAPI_CALL create_buffer(VkDevice, const VkBufferCreateInfo* info, const VkAllocationCallbacks*,
VkBuffer* buffer) {
Created c{info->size, info->sharingMode, {}};
if (info->sharingMode == VK_SHARING_MODE_CONCURRENT)
c.families.assign(info->pQueueFamilyIndices, info->pQueueFamilyIndices + info->queueFamilyIndexCount);
buffers.push_back(c);
*buffer = fake_handle<VkBuffer>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL destroy_buffer(VkDevice, VkBuffer, const VkAllocationCallbacks*) {}
VKAPI_ATTR void VKAPI_CALL requirements(VkDevice, VkBuffer, VkMemoryRequirements* out) { *out = {4u << 20, 256, 1}; }
VKAPI_ATTR VkResult VKAPI_CALL bind(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) { return VK_SUCCESS; }
VKAPI_ATTR void VKAPI_CALL free_memory(VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) {}
VKAPI_ATTR VkResult VKAPI_CALL create_module(VkDevice, const VkShaderModuleCreateInfo*, const VkAllocationCallbacks*,
VkShaderModule* out) {
*out = fake_handle<VkShaderModule>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL destroy_module(VkDevice, VkShaderModule, const VkAllocationCallbacks*) {}
VKAPI_ATTR VkResult VKAPI_CALL create_set_layout(VkDevice, const VkDescriptorSetLayoutCreateInfo*,
const VkAllocationCallbacks*, VkDescriptorSetLayout* out) {
*out = fake_handle<VkDescriptorSetLayout>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL destroy_set_layout(VkDevice, VkDescriptorSetLayout, const VkAllocationCallbacks*) {}
VKAPI_ATTR VkResult VKAPI_CALL create_pipeline_layout(VkDevice, const VkPipelineLayoutCreateInfo*,
const VkAllocationCallbacks*, VkPipelineLayout* out) {
*out = fake_handle<VkPipelineLayout>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL destroy_pipeline_layout(VkDevice, VkPipelineLayout, const VkAllocationCallbacks*) {}
VKAPI_ATTR VkResult VKAPI_CALL create_pipelines(VkDevice, VkPipelineCache, std::uint32_t count,
const VkComputePipelineCreateInfo*, const VkAllocationCallbacks*,
VkPipeline* out) {
for (std::uint32_t i = 0; i < count; ++i) out[i] = fake_handle<VkPipeline>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL destroy_pipeline(VkDevice, VkPipeline, const VkAllocationCallbacks*) {}
VKAPI_ATTR VkResult VKAPI_CALL create_pool(VkDevice, const VkDescriptorPoolCreateInfo*, const VkAllocationCallbacks*,
VkDescriptorPool* out) {
*out = fake_handle<VkDescriptorPool>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL destroy_pool(VkDevice, VkDescriptorPool, const VkAllocationCallbacks*) {}
VKAPI_ATTR VkResult VKAPI_CALL allocate_sets(VkDevice, const VkDescriptorSetAllocateInfo* info, VkDescriptorSet* out) {
for (std::uint32_t i = 0; i < info->descriptorSetCount; ++i) out[i] = fake_handle<VkDescriptorSet>(next_handle++);
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL update_sets(VkDevice, std::uint32_t, const VkWriteDescriptorSet*, std::uint32_t,
const VkCopyDescriptorSet*) {}
VKAPI_ATTR void VKAPI_CALL bind_pipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {}
VKAPI_ATTR void VKAPI_CALL bind_sets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, std::uint32_t,
std::uint32_t, const VkDescriptorSet*, std::uint32_t, const std::uint32_t*) {}
VKAPI_ATTR void VKAPI_CALL push_constants(VkCommandBuffer, VkPipelineLayout, VkShaderStageFlags, std::uint32_t,
std::uint32_t, const void*) {}
VKAPI_ATTR void VKAPI_CALL dispatch(VkCommandBuffer, std::uint32_t, std::uint32_t, std::uint32_t) {}
VKAPI_ATTR VkResult VKAPI_CALL counter_value(VkDevice, VkSemaphore, std::uint64_t* value) {
*value = ~std::uint64_t{0};
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL wait_idle(VkDevice) { return VK_SUCCESS; }

VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_proc(VkDevice, const char* name) {
#define LEONIDA_FAKE(entry, fn) if (!std::strcmp(name, entry)) return reinterpret_cast<PFN_vkVoidFunction>(fn)
LEONIDA_FAKE("vkCreateBuffer", create_buffer);
LEONIDA_FAKE("vkDestroyBuffer", destroy_buffer);
LEONIDA_FAKE("vkGetBufferMemoryRequirements", requirements);
LEONIDA_FAKE("vkBindBufferMemory", bind);
LEONIDA_FAKE("vkFreeMemory", free_memory);
LEONIDA_FAKE("vkCreateShaderModule", create_module);
LEONIDA_FAKE("vkDestroyShaderModule", destroy_module);
LEONIDA_FAKE("vkCreateDescriptorSetLayout", create_set_layout);
LEONIDA_FAKE("vkDestroyDescriptorSetLayout", destroy_set_layout);
LEONIDA_FAKE("vkCreatePipelineLayout", create_pipeline_layout);
LEONIDA_FAKE("vkDestroyPipelineLayout", destroy_pipeline_layout);
LEONIDA_FAKE("vkCreateComputePipelines", create_pipelines);
LEONIDA_FAKE("vkDestroyPipeline", destroy_pipeline);
LEONIDA_FAKE("vkCreateDescriptorPool", create_pool);
LEONIDA_FAKE("vkDestroyDescriptorPool", destroy_pool);
LEONIDA_FAKE("vkAllocateDescriptorSets", allocate_sets);
LEONIDA_FAKE("vkUpdateDescriptorSets", update_sets);
LEONIDA_FAKE("vkCmdBindPipeline", bind_pipeline);
LEONIDA_FAKE("vkCmdBindDescriptorSets", bind_sets);
LEONIDA_FAKE("vkCmdPushConstants", push_constants);
LEONIDA_FAKE("vkCmdDispatch", dispatch);
LEONIDA_FAKE("vkGetSemaphoreCounterValue", counter_value);
LEONIDA_FAKE("vkDeviceWaitIdle", wait_idle);
#undef LEONIDA_FAKE
return nullptr;
}

// A readback of a 64x64 D24 footprint: prepare() makes one scratch chunk for it.
Depth24Job job() {
Depth24Job j;
j.app = fake_handle<VkBuffer>(0x10);
j.app_size = 64 * 64 * 4;
j.width = j.height = 64;
j.slices = 1;
j.app_row_words = 64;
j.app_slice_words = 64 * 64;
return j;
}

// The chunk prepare() makes for one copy with the converter given `families`.
int chunk_sharing(std::vector<std::uint32_t> families, VkSharingMode mode, std::vector<std::uint32_t> expect) {
buffers.clear();
Depth24Converter converter(get_proc, fake_handle<VkDevice>(0x1),
[](const VkMemoryRequirements&) { return fake_handle<VkDeviceMemory>(0x20); },
1u << 27, nullptr, std::move(families));
EXPECT(converter.loaded());
Depth24Job whole = job();
std::vector<Depth24Job> parts;
EXPECT(converter.prepare(whole, parts) && whole.scratch && parts.size() == 1);
EXPECT(buffers.size() == 1);
EXPECT(buffers[0].sharing == mode && buffers[0].families == expect);
return 0;
}

} // namespace

int main() {
// Graphics, async compute and DMA families: the chunk is shared by all three.
if (int failed = chunk_sharing({0, 1, 2}, VK_SHARING_MODE_CONCURRENT, {0, 1, 2})) return failed;
// Copies on the async-compute family (no DMA family).
if (int failed = chunk_sharing({0, 2}, VK_SHARING_MODE_CONCURRENT, {0, 2})) return failed;
// One family (every D3D12 queue on the graphics family), or none given: EXCLUSIVE, as before.
if (int failed = chunk_sharing({0}, VK_SHARING_MODE_EXCLUSIVE, {})) return failed;
if (int failed = chunk_sharing({}, VK_SHARING_MODE_EXCLUSIVE, {})) return failed;
std::puts("depth24_scratch_sharing: ok");
return 0;
}
Loading
Loading