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
3 changes: 2 additions & 1 deletion layersvt/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The `layersvt_common` library provides a modern, thread-safe C++ foundation for
* **`DispatchTableManager`** ([`dispatch_table_manager.h`](dispatch_table_manager.h), [`dispatch_table_manager.cpp`](dispatch_table_manager.cpp)):
Thread-safe registry for `VkuInstanceDispatchTable` and `VkuDeviceDispatchTable` keyed by dispatchable handle. Resolves a `VkPhysicalDevice` to its parent `VkInstance` and instance dispatch table through the shared dispatch key (loader ABI), so no per-physical-device tracking is needed. Tracks and forwards `VK_LOADER_DATA_CALLBACK` to initialize dispatchable handles created internally by layers.
* **`dispatch_downstream.h`** ([`dispatch_downstream.h`](dispatch_downstream.h)):
Header-only template metaprogramming helpers (`DispatchDownstream`, `DispatchDownstreamIfAvailable`, `DispatchDownstreamOr`, `DispatchDownstreamOrSuccess`) that deduce table types at compile time and forward commands downstream.
Header-only template metaprogramming helpers (`GetDownstream`, `DispatchDownstream`, `DispatchDownstreamIfAvailable`, `DispatchDownstreamOr`, `DispatchDownstreamOrSuccess`) that deduce table types at compile time to inspect or forward commands downstream.
* **`layersvt_entrypoints`** ([`layer_entrypoints.cpp`](layer_entrypoints.cpp)):
CMake `OBJECT` library that exports standard C symbols (`vkGetInstanceProcAddr`, `vkGetDeviceProcAddr`, `vkNegotiateLoaderLayerInterfaceVersion`, and the four Android loader enumeration entry points) without macro duplication.

Expand Down Expand Up @@ -315,6 +315,7 @@ target_link_libraries(test_MyCustomLayer_layer layersvt_common)
Use `DispatchDownstream<&VkuDeviceDispatchTable::CmdDraw>(...)` to invoke the next layer or driver. It automatically deduces instance vs. device dispatch tables from the member pointer at compile time and asserts that tables and function pointers are non-null.

**Choosing a dispatch helper**:
* `GetDownstream` — retrieves the typed downstream function pointer without invoking it (`nullptr` when the handle is null, unregistered, or its slot is null).
* `DispatchDownstream` — commands guaranteed to exist downstream (asserts on a missing table or entry point).
* `DispatchDownstreamIfAvailable` — optional commands returning `void` (e.g. debug marker / debug utils label commands of layer-implemented extensions); does nothing if the entry point is unavailable.
* `DispatchDownstreamOrSuccess` — optional commands returning `VkResult`; returns `VK_SUCCESS` if the entry point is unavailable.
Expand Down
58 changes: 35 additions & 23 deletions layersvt/common/dispatch_downstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,48 @@ struct MemberTraits<Member Class::*> {
};

/**
* Forwards a required Vulkan command downstream using the dispatch table.
* Deduces instance vs. device table and asserts the entry point is non-null.
* Returns the result of calling the downstream Vulkan command.
* Retrieves a Vulkan command pointer from the downstream dispatch table for a handle without
* invoking it. The dispatch table type (VkuInstanceDispatchTable or VkuDeviceDispatchTable) is
* deduced from MemberPointer; Handle must be VkInstance or VkPhysicalDevice for instance commands,
* and VkDevice, VkQueue, VkCommandBuffer, or VkExternalComputeQueueNV for device commands.
* Returns the downstream command pointer, or nullptr if the handle is null, unregistered, or its
* dispatch table slot is null.
*
* The lookup uses the dispatch key of the handle. A dispatchable object that the layer creates
* itself, such as a VkCommandBuffer, gets the key of its device from the callback returned by
* LayerBase::GetDeviceLoaderDataCallback(). Call that callback, when the loader provides one,
* before dispatching through such an object.
*/
template <auto MemberPointer, typename Handle, typename... Args>
inline auto DispatchDownstream(Handle handle, Args&&... args) {
using TableType = typename MemberTraits<decltype(MemberPointer)>::ClassType;
static_assert(std::is_same_v<TableType, VkuInstanceDispatchTable> ||
std::is_same_v<TableType, VkuDeviceDispatchTable>,
template <auto MemberPointer, typename Handle>
[[nodiscard]] inline auto GetDownstream(Handle handle) {
using Traits = MemberTraits<decltype(MemberPointer)>;
using TableType = typename Traits::ClassType;
static_assert(std::is_same_v<TableType, VkuInstanceDispatchTable> || std::is_same_v<TableType, VkuDeviceDispatchTable>,
"MemberPointer must be a member of VkuInstanceDispatchTable or VkuDeviceDispatchTable");
TableType* table = nullptr;
if constexpr (std::is_same_v<TableType, VkuInstanceDispatchTable>) {
static_assert(std::is_same_v<Handle, VkInstance> || std::is_same_v<Handle, VkPhysicalDevice>,
"Instance dispatch table commands require a VkInstance or VkPhysicalDevice handle");
table = LayerBase::GetInstanceDispatchTable(handle);
} else {
static_assert(std::is_same_v<Handle, VkDevice> || std::is_same_v<Handle, VkQueue> ||
std::is_same_v<Handle, VkCommandBuffer> || std::is_same_v<Handle, VkExternalComputeQueueNV>,
"Device dispatch table commands require a dispatchable device-level handle");
table = LayerBase::GetDeviceDispatchTable(handle);
}
return table != nullptr ? table->*MemberPointer : typename Traits::MemberType{nullptr};
}

assert(table != nullptr && "Dispatch table must exist for valid handle");
assert(table->*MemberPointer != nullptr && "Function pointer must exist in dispatch table");
return (table->*MemberPointer)(handle, std::forward<Args>(args)...);
/**
* Forwards a required Vulkan command downstream using the dispatch table.
* Deduces instance vs. device table and asserts the entry point is non-null.
* Returns the result of calling the downstream Vulkan command.
*/
template <auto MemberPointer, typename Handle, typename... Args>
inline auto DispatchDownstream(Handle handle, Args&&... args) {
auto downstream_function = GetDownstream<MemberPointer>(handle);
assert(downstream_function != nullptr && "Dispatch table and function pointer must exist for valid handle");
return downstream_function(handle, std::forward<Args>(args)...);
}

/**
Expand All @@ -68,19 +90,9 @@ inline auto DispatchDownstream(Handle handle, Args&&... args) {
template <auto MemberPointer, typename Fallback, typename Handle, typename... Args>
inline auto DispatchDownstreamOr(Fallback&& fallback, Handle handle, Args&&... args) {
using TableType = typename MemberTraits<decltype(MemberPointer)>::ClassType;
static_assert(std::is_same_v<TableType, VkuInstanceDispatchTable> ||
std::is_same_v<TableType, VkuDeviceDispatchTable>,
"MemberPointer must be a member of VkuInstanceDispatchTable or VkuDeviceDispatchTable");
using ReturnType = decltype((std::declval<TableType*>()->*MemberPointer)(handle, std::forward<Args>(args)...));
TableType* table = nullptr;
if constexpr (std::is_same_v<TableType, VkuInstanceDispatchTable>) {
table = LayerBase::GetInstanceDispatchTable(handle);
} else {
table = LayerBase::GetDeviceDispatchTable(handle);
}

if (table && table->*MemberPointer) {
return static_cast<ReturnType>((table->*MemberPointer)(handle, std::forward<Args>(args)...));
if (auto downstream_function = GetDownstream<MemberPointer>(handle)) {
return static_cast<ReturnType>(downstream_function(handle, std::forward<Args>(args)...));
}

if constexpr (std::is_invocable_v<Fallback>) {
Expand Down
24 changes: 7 additions & 17 deletions layersvt/common/layer_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,9 @@ VkResult LayerBase::EnumerateDeviceLayerProperties(VkPhysicalDevice physical_dev

VkResult LayerBase::EnumerateDeviceExtensionProperties(VkPhysicalDevice physical_device, const char* layer_name,
uint32_t* property_count, VkExtensionProperties* properties) {
PFN_vkEnumerateDeviceExtensionProperties downstream = nullptr;
if (physical_device != VK_NULL_HANDLE) {
auto* table = GetInstanceDispatchTable(physical_device);
if (table != nullptr) {
downstream = table->EnumerateDeviceExtensionProperties;
}
}
return EnumerateDeviceExtensionPropertiesWithDownstream(physical_device, layer_name, property_count, properties, downstream);
return EnumerateDeviceExtensionPropertiesWithDownstream(
physical_device, layer_name, property_count, properties,
GetDownstream<&VkuInstanceDispatchTable::EnumerateDeviceExtensionProperties>(physical_device));
}

VkResult LayerBase::EnumerateDeviceExtensionPropertiesWithDownstream(
Expand Down Expand Up @@ -347,15 +342,10 @@ VkResult LayerBase::EnumerateDeviceExtensionPropertiesWithDownstream(

VkResult LayerBase::GetPhysicalDeviceToolProperties(VkPhysicalDevice physical_device, uint32_t* tool_count,
VkPhysicalDeviceToolPropertiesEXT* tool_properties) {
PFN_vkGetPhysicalDeviceToolPropertiesEXT downstream = nullptr;
if (physical_device != VK_NULL_HANDLE) {
auto* table = GetInstanceDispatchTable(physical_device);
if (table != nullptr) {
downstream = table->GetPhysicalDeviceToolPropertiesEXT;
if (!downstream) {
downstream = table->GetPhysicalDeviceToolProperties;
}
}
PFN_vkGetPhysicalDeviceToolPropertiesEXT downstream =
GetDownstream<&VkuInstanceDispatchTable::GetPhysicalDeviceToolPropertiesEXT>(physical_device);
if (!downstream) {
downstream = GetDownstream<&VkuInstanceDispatchTable::GetPhysicalDeviceToolProperties>(physical_device);
}
Comment thread
olehkuznetsov marked this conversation as resolved.
Resolved
return GetPhysicalDeviceToolPropertiesWithDownstream(physical_device, tool_count, tool_properties, downstream);
}
Expand Down
7 changes: 2 additions & 5 deletions layersvt/common/layer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,8 @@ class LayerBase {

friend class LayerBaseTestPeer;

template <auto MemberPointer, typename Handle, typename... Args>
friend auto DispatchDownstream(Handle handle, Args&&... args);

template <auto MemberPointer, typename Fallback, typename Handle, typename... Args>
friend auto DispatchDownstreamOr(Fallback&& fallback, Handle handle, Args&&... args);
template <auto MemberPointer, typename Handle>
friend auto GetDownstream(Handle handle);

// Vulkan intercept commands (static C-compatible functions)

Expand Down
72 changes: 72 additions & 0 deletions layersvt/test/common/test_dispatch_downstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,75 @@ TEST(DispatchDownstreamTest, DispatchDownstreamIfAvailable) {
EXPECT_EQ(called_device, mock_device);
EXPECT_EQ(called_allocator, &allocator);
}

TEST(DispatchDownstreamTest, GetDownstream) {
LayerBase layer;
auto& dispatch_table_manager = LayerBaseTestPeer::GetDispatchTableManager(layer);

void* mock_instance_vtable = reinterpret_cast<void*>(static_cast<uintptr_t>(0xA111));
auto mock_instance = reinterpret_cast<VkInstance>(&mock_instance_vtable);
void* mock_physical_device_object = mock_instance_vtable;
auto mock_physical_device = reinterpret_cast<VkPhysicalDevice>(&mock_physical_device_object);

void* mock_device_vtable = reinterpret_cast<void*>(static_cast<uintptr_t>(0xB222));
auto mock_device = reinterpret_cast<VkDevice>(&mock_device_vtable);
void* mock_queue_object = mock_device_vtable;
auto mock_queue = reinterpret_cast<VkQueue>(&mock_queue_object);
void* mock_command_buffer_object = mock_device_vtable;
auto mock_command_buffer = reinterpret_cast<VkCommandBuffer>(&mock_command_buffer_object);

// Typed null handles return nullptr for both instance and device commands.
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumeratePhysicalDevices>(VkInstance{VK_NULL_HANDLE}), nullptr);
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumerateDeviceExtensionProperties>(VkPhysicalDevice{VK_NULL_HANDLE}),
nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::DeviceWaitIdle>(VkDevice{VK_NULL_HANDLE}), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::QueueWaitIdle>(VkQueue{VK_NULL_HANDLE}), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::EndCommandBuffer>(VkCommandBuffer{VK_NULL_HANDLE}), nullptr);

// Unregistered handles return nullptr.
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumeratePhysicalDevices>(mock_instance), nullptr);
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumerateDeviceExtensionProperties>(mock_physical_device), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::DeviceWaitIdle>(mock_device), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::QueueWaitIdle>(mock_queue), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::EndCommandBuffer>(mock_command_buffer), nullptr);

// Registered handles with null entry points return nullptr.
dispatch_table_manager.InitInstanceTable(mock_instance, [](VkInstance, const char*) -> PFN_vkVoidFunction { return nullptr; });
dispatch_table_manager.InitDeviceTable(mock_device, [](VkDevice, const char*) -> PFN_vkVoidFunction { return nullptr; });

EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumeratePhysicalDevices>(mock_instance), nullptr);
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumerateDeviceExtensionProperties>(mock_physical_device), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::DeviceWaitIdle>(mock_device), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::QueueWaitIdle>(mock_queue), nullptr);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::EndCommandBuffer>(mock_command_buffer), nullptr);

// Registered handles with populated entry points return the expected function pointers.
const PFN_vkEnumeratePhysicalDevices enumerate_physical_devices = [](VkInstance, uint32_t*, VkPhysicalDevice*) -> VkResult {
return VK_SUCCESS;
};
const PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
[](VkPhysicalDevice, const char*, uint32_t*, VkExtensionProperties*) -> VkResult { return VK_SUCCESS; };
const PFN_vkDeviceWaitIdle device_wait_idle = [](VkDevice) -> VkResult { return VK_SUCCESS; };
const PFN_vkQueueWaitIdle queue_wait_idle = [](VkQueue) -> VkResult { return VK_SUCCESS; };
const PFN_vkEndCommandBuffer end_command_buffer = [](VkCommandBuffer) -> VkResult { return VK_SUCCESS; };

auto* instance_table = dispatch_table_manager.GetInstanceDispatchTable(mock_instance);
auto* device_table = dispatch_table_manager.GetDeviceDispatchTable(mock_device);
ASSERT_NE(instance_table, nullptr);
ASSERT_NE(device_table, nullptr);
instance_table->EnumeratePhysicalDevices = enumerate_physical_devices;
instance_table->EnumerateDeviceExtensionProperties = enumerate_device_extensions;
device_table->DeviceWaitIdle = device_wait_idle;
device_table->QueueWaitIdle = queue_wait_idle;
device_table->EndCommandBuffer = end_command_buffer;

EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumeratePhysicalDevices>(mock_instance), enumerate_physical_devices);
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumerateDeviceExtensionProperties>(mock_instance),
enumerate_device_extensions);
EXPECT_EQ(GetDownstream<&VkuInstanceDispatchTable::EnumerateDeviceExtensionProperties>(mock_physical_device),
enumerate_device_extensions);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::DeviceWaitIdle>(mock_device), device_wait_idle);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::QueueWaitIdle>(mock_device), queue_wait_idle);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::QueueWaitIdle>(mock_queue), queue_wait_idle);
EXPECT_EQ(GetDownstream<&VkuDeviceDispatchTable::EndCommandBuffer>(mock_command_buffer), end_command_buffer);
}
43 changes: 43 additions & 0 deletions layersvt/test/common/test_layer_manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,49 @@ TEST(LayerBaseTest, GetPhysicalDeviceToolPropertiesDispatch) {
result = LayerBaseTestPeer::GetPhysicalDeviceToolProperties(VK_NULL_HANDLE, &count, tools.data());
EXPECT_EQ(result, VK_SUCCESS);
EXPECT_STREQ(tools[0].name, "VK_LAYER_TEST_Common");

// Test EXT-to-core fallback with a registered instance table where EXT is null and core 1.3 is set
void* mock_instance_vtable = reinterpret_cast<void*>(static_cast<uintptr_t>(0xC111));
auto mock_instance = reinterpret_cast<VkInstance>(&mock_instance_vtable);
void* mock_physical_device_object = mock_instance_vtable;
auto mock_physical_device = reinterpret_cast<VkPhysicalDevice>(&mock_physical_device_object);

auto& dispatch_table_manager = LayerBaseTestPeer::GetDispatchTableManager(layer);
dispatch_table_manager.InitInstanceTable(mock_instance,
[](VkInstance, const char*) -> PFN_vkVoidFunction { return nullptr; });
auto* instance_table = dispatch_table_manager.GetInstanceDispatchTable(mock_instance);
ASSERT_NE(instance_table, nullptr);
instance_table->GetPhysicalDeviceToolPropertiesEXT = nullptr;

static int core_calls = 0;
core_calls = 0;
instance_table->GetPhysicalDeviceToolProperties =
[](VkPhysicalDevice, uint32_t* tool_count, VkPhysicalDeviceToolProperties* properties) -> VkResult {
++core_calls;
if (!properties) {
*tool_count = 1;
return VK_SUCCESS;
}
EXPECT_EQ(properties[0].sType, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TOOL_PROPERTIES);
EXPECT_EQ(properties[0].pNext, nullptr);
std::strncpy(properties[0].name, "CoreDriverTool", VK_MAX_EXTENSION_NAME_SIZE);
*tool_count = 1;
return VK_SUCCESS;
};

count = 0;
result = LayerBaseTestPeer::GetPhysicalDeviceToolProperties(mock_physical_device, &count, nullptr);
EXPECT_EQ(result, VK_SUCCESS);
ASSERT_EQ(count, 2u);
EXPECT_EQ(core_calls, 2);

tools.assign(count, VkPhysicalDeviceToolPropertiesEXT{});
result = LayerBaseTestPeer::GetPhysicalDeviceToolProperties(mock_physical_device, &count, tools.data());
EXPECT_EQ(result, VK_SUCCESS);
ASSERT_EQ(count, 2u);
EXPECT_EQ(core_calls, 4);
EXPECT_STREQ(tools[0].name, "CoreDriverTool");
EXPECT_STREQ(tools[1].name, "VK_LAYER_TEST_Common");
}

TEST(LayerBaseEnumerationTest, EmptyExtensionsWithBuffer) {
Expand Down
Loading