From 3e646ffd6a76517be7f09696dbfcb674fee57b68 Mon Sep 17 00:00:00 2001 From: okuznetsov Date: Thu, 5 Nov 2026 21:40:56 +0000 Subject: [PATCH] layersvt: Add GetDownstream and use it in dispatch helpers Add GetDownstream in dispatch_downstream.h so dispatch helpers share one typed table lookup and LayerBase needs only one dispatch friend. Adopt GetDownstream in LayerBase::EnumerateDeviceExtensionProperties and LayerBase::GetPhysicalDeviceToolProperties, and add unit tests for GetDownstream. Test: ctest --test-dir build --output-on-failure Change-Id: I02117cd8e16472c533062cb89c2ab8066a6a6964 --- layersvt/common/README.md | 3 +- layersvt/common/dispatch_downstream.h | 58 +++++++++------ layersvt/common/layer_base.cpp | 24 ++----- layersvt/common/layer_base.h | 7 +- .../test/common/test_dispatch_downstream.cpp | 72 +++++++++++++++++++ layersvt/test/common/test_layer_manifest.cpp | 43 +++++++++++ 6 files changed, 161 insertions(+), 46 deletions(-) diff --git a/layersvt/common/README.md b/layersvt/common/README.md index 4ad5ff0d04..ce32403dc8 100644 --- a/layersvt/common/README.md +++ b/layersvt/common/README.md @@ -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. @@ -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. diff --git a/layersvt/common/dispatch_downstream.h b/layersvt/common/dispatch_downstream.h index 4e95bbe75a..8d1e278099 100644 --- a/layersvt/common/dispatch_downstream.h +++ b/layersvt/common/dispatch_downstream.h @@ -38,26 +38,48 @@ struct MemberTraits { }; /** - * 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 -inline auto DispatchDownstream(Handle handle, Args&&... args) { - using TableType = typename MemberTraits::ClassType; - static_assert(std::is_same_v || - std::is_same_v, +template +[[nodiscard]] inline auto GetDownstream(Handle handle) { + using Traits = MemberTraits; + using TableType = typename Traits::ClassType; + static_assert(std::is_same_v || std::is_same_v, "MemberPointer must be a member of VkuInstanceDispatchTable or VkuDeviceDispatchTable"); TableType* table = nullptr; if constexpr (std::is_same_v) { + static_assert(std::is_same_v || std::is_same_v, + "Instance dispatch table commands require a VkInstance or VkPhysicalDevice handle"); table = LayerBase::GetInstanceDispatchTable(handle); } else { + static_assert(std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v, + "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)...); +/** + * 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 +inline auto DispatchDownstream(Handle handle, Args&&... args) { + auto downstream_function = GetDownstream(handle); + assert(downstream_function != nullptr && "Dispatch table and function pointer must exist for valid handle"); + return downstream_function(handle, std::forward(args)...); } /** @@ -68,19 +90,9 @@ inline auto DispatchDownstream(Handle handle, Args&&... args) { template inline auto DispatchDownstreamOr(Fallback&& fallback, Handle handle, Args&&... args) { using TableType = typename MemberTraits::ClassType; - static_assert(std::is_same_v || - std::is_same_v, - "MemberPointer must be a member of VkuInstanceDispatchTable or VkuDeviceDispatchTable"); using ReturnType = decltype((std::declval()->*MemberPointer)(handle, std::forward(args)...)); - TableType* table = nullptr; - if constexpr (std::is_same_v) { - table = LayerBase::GetInstanceDispatchTable(handle); - } else { - table = LayerBase::GetDeviceDispatchTable(handle); - } - - if (table && table->*MemberPointer) { - return static_cast((table->*MemberPointer)(handle, std::forward(args)...)); + if (auto downstream_function = GetDownstream(handle)) { + return static_cast(downstream_function(handle, std::forward(args)...)); } if constexpr (std::is_invocable_v) { diff --git a/layersvt/common/layer_base.cpp b/layersvt/common/layer_base.cpp index 0d98144a6d..da7433d6d4 100644 --- a/layersvt/common/layer_base.cpp +++ b/layersvt/common/layer_base.cpp @@ -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( @@ -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); } return GetPhysicalDeviceToolPropertiesWithDownstream(physical_device, tool_count, tool_properties, downstream); } diff --git a/layersvt/common/layer_base.h b/layersvt/common/layer_base.h index 8ab4204f4b..84072f95b8 100644 --- a/layersvt/common/layer_base.h +++ b/layersvt/common/layer_base.h @@ -229,11 +229,8 @@ class LayerBase { friend class LayerBaseTestPeer; - template - friend auto DispatchDownstream(Handle handle, Args&&... args); - - template - friend auto DispatchDownstreamOr(Fallback&& fallback, Handle handle, Args&&... args); + template + friend auto GetDownstream(Handle handle); // Vulkan intercept commands (static C-compatible functions) diff --git a/layersvt/test/common/test_dispatch_downstream.cpp b/layersvt/test/common/test_dispatch_downstream.cpp index 0cd553c66f..9b79650420 100644 --- a/layersvt/test/common/test_dispatch_downstream.cpp +++ b/layersvt/test/common/test_dispatch_downstream.cpp @@ -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(static_cast(0xA111)); + auto mock_instance = reinterpret_cast(&mock_instance_vtable); + void* mock_physical_device_object = mock_instance_vtable; + auto mock_physical_device = reinterpret_cast(&mock_physical_device_object); + + void* mock_device_vtable = reinterpret_cast(static_cast(0xB222)); + auto mock_device = reinterpret_cast(&mock_device_vtable); + void* mock_queue_object = mock_device_vtable; + auto mock_queue = reinterpret_cast(&mock_queue_object); + void* mock_command_buffer_object = mock_device_vtable; + auto mock_command_buffer = reinterpret_cast(&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); +} diff --git a/layersvt/test/common/test_layer_manifest.cpp b/layersvt/test/common/test_layer_manifest.cpp index ea1bdb7cba..516e60c848 100644 --- a/layersvt/test/common/test_layer_manifest.cpp +++ b/layersvt/test/common/test_layer_manifest.cpp @@ -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(static_cast(0xC111)); + auto mock_instance = reinterpret_cast(&mock_instance_vtable); + void* mock_physical_device_object = mock_instance_vtable; + auto mock_physical_device = reinterpret_cast(&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) {