From 835af46048c1ca7514f59a7d4a03b2a366403ab1 Mon Sep 17 00:00:00 2001 From: Peter Harris Date: Mon, 9 Dec 2024 14:07:28 +0000 Subject: [PATCH] Avoid copying static code in the generator --- generator/generate_vulkan_common.py | 14 -- .../vk_codegen/device_dispatch_table.txt | 1 + .../vk_codegen/function_vkCreateDevice.txt | 6 +- .../vk_codegen/function_vkCreateInstance.txt | 2 +- generator/vk_codegen/instance_defs.txt | 31 +-- .../vk_codegen/instance_dispatch_table.txt | 1 + generator/vk_common/CMakeLists.txt | 42 ---- generator/vk_common/utils.hpp | 101 -------- source_common/framework/CMakeLists.txt | 5 +- .../framework/device_dispatch_table.hpp | 1 + .../framework/entry_utils.cpp | 132 +++++----- source_common/framework/entry_utils.hpp | 236 +++--------------- .../framework/instance_dispatch_table.hpp | 1 + .../framework/instance_functions.cpp | 39 +-- source_common/framework/utils.hpp | 4 +- 15 files changed, 122 insertions(+), 494 deletions(-) delete mode 100644 generator/vk_common/CMakeLists.txt delete mode 100644 generator/vk_common/utils.hpp rename generator/vk_common/entry_utils.hpp => source_common/framework/entry_utils.cpp (75%) diff --git a/generator/generate_vulkan_common.py b/generator/generate_vulkan_common.py index 34292c9..5d9d2d2 100644 --- a/generator/generate_vulkan_common.py +++ b/generator/generate_vulkan_common.py @@ -685,15 +685,6 @@ def main(): base_dir = os.path.dirname(__file__) outdir = os.path.join(base_dir, '..', 'source_common', 'framework') - # Clean the output directory if needed - if os.path.exists(outdir): - if not os.path.isdir(outdir): - print(f'ERROR: Output location "{outdir}" is not a directory') - return 1 - - shutil.rmtree(outdir, ignore_errors=True) - os.makedirs(outdir) - # Parse the XML headers tree = ET.parse('./khronos/vulkan/registry/vk.xml') root = tree.getroot() @@ -732,11 +723,6 @@ def main(): # Load hand written function bodies manual_commands = load_handwritten_commands() - # Generate static resources - base_dir = os.path.dirname(__file__) - source_dir = os.path.join(base_dir, 'vk_common') - copy_resource(source_dir, outdir) - # Generate dynamic resources outfile = os.path.join(outdir, 'instance_dispatch_table.hpp') with open(outfile, 'w', encoding='utf-8', newline='\n') as handle: diff --git a/generator/vk_codegen/device_dispatch_table.txt b/generator/vk_codegen/device_dispatch_table.txt index e2302db..4ff4bd5 100644 --- a/generator/vk_codegen/device_dispatch_table.txt +++ b/generator/vk_codegen/device_dispatch_table.txt @@ -3,6 +3,7 @@ #include #include "framework/device_functions.hpp" +#include "framework/utils.hpp" #include "utils/misc.hpp" #if __has_include ("layer_device_functions.hpp") diff --git a/generator/vk_codegen/function_vkCreateDevice.txt b/generator/vk_codegen/function_vkCreateDevice.txt index afc8ff1..218d8a3 100644 --- a/generator/vk_codegen/function_vkCreateDevice.txt +++ b/generator/vk_codegen/function_vkCreateDevice.txt @@ -16,17 +16,15 @@ // Advance the link info for the next element on the chain chainInfo->u.pLayerInfo = chainInfo->u.pLayerInfo->pNext; - auto res = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice); if (res != VK_SUCCESS) { return res; } - auto device = std::make_unique(layer, physicalDevice, *pDevice, fpGetDeviceProcAddr); - - // Hold the lock to access layer-wide global store + // Retake the lock to access layer-wide global store lock.lock(); + auto device = std::make_unique(layer, physicalDevice, *pDevice, fpGetDeviceProcAddr); Device::store(*pDevice, std::move(device)); return VK_SUCCESS; \ No newline at end of file diff --git a/generator/vk_codegen/function_vkCreateInstance.txt b/generator/vk_codegen/function_vkCreateInstance.txt index 4dfb209..be127a2 100644 --- a/generator/vk_codegen/function_vkCreateInstance.txt +++ b/generator/vk_codegen/function_vkCreateInstance.txt @@ -14,7 +14,7 @@ return res; } - // Hold the lock to access layer-wide global store + // Retake the lock to access layer-wide global store auto instance = std::make_unique(*pInstance, fpGetInstanceProcAddr); { std::lock_guard lock { g_vulkanLock }; diff --git a/generator/vk_codegen/instance_defs.txt b/generator/vk_codegen/instance_defs.txt index b7f2602..6b43588 100644 --- a/generator/vk_codegen/instance_defs.txt +++ b/generator/vk_codegen/instance_defs.txt @@ -2,37 +2,12 @@ #include #include -#include "utils.hpp" -#include "instance.hpp" #include "device.hpp" +#include "entry_utils.hpp" +#include "instance.hpp" #include "instance_functions.hpp" +#include "utils.hpp" extern std::mutex g_vulkanLock; -static VkLayerInstanceCreateInfo* get_chain_info( - const VkInstanceCreateInfo* pCreateInfo, - VkLayerFunction func -) { - auto* info = static_cast(pCreateInfo->pNext); - while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && info->function == func)) - { - info = static_cast(info->pNext); - } - - return const_cast(info); -} - -static VkLayerDeviceCreateInfo* get_chain_info( - const VkDeviceCreateInfo* pCreateInfo, - VkLayerFunction func -) { - auto* info = static_cast(pCreateInfo->pNext); - while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && info->function == func)) - { - info = static_cast(info->pNext); - } - - return const_cast(info); -} - {FUNCTION_DEFS} diff --git a/generator/vk_codegen/instance_dispatch_table.txt b/generator/vk_codegen/instance_dispatch_table.txt index 70f7552..5b95bd6 100644 --- a/generator/vk_codegen/instance_dispatch_table.txt +++ b/generator/vk_codegen/instance_dispatch_table.txt @@ -3,6 +3,7 @@ #include #include "framework/instance_functions.hpp" +#include "framework/utils.hpp" #include "utils/misc.hpp" #if __has_include ("layer_instance_functions.hpp") diff --git a/generator/vk_common/CMakeLists.txt b/generator/vk_common/CMakeLists.txt deleted file mode 100644 index 247b795..0000000 --- a/generator/vk_common/CMakeLists.txt +++ /dev/null @@ -1,42 +0,0 @@ -# SPDX-License-Identifier: MIT -# ----------------------------------------------------------------------------- -# Copyright (c) 2024 Arm Limited -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# ----------------------------------------------------------------------------- - -set(LIB_BINARY lib_layer_framework) - -add_library( - ${LIB_BINARY} STATIC - device_functions.cpp - instance_functions.cpp) - -target_include_directories( - ${LIB_BINARY} PRIVATE - # Note, this includes from the layer-specific tree - ${PROJECT_SOURCE_DIR}/source - ../) - -target_include_directories( - ${LIB_BINARY} SYSTEM PRIVATE - ../../khronos/vulkan/include - ../) - -lgl_set_build_options(${LIB_BINARY}) diff --git a/generator/vk_common/utils.hpp b/generator/vk_common/utils.hpp deleted file mode 100644 index 9ddbe92..0000000 --- a/generator/vk_common/utils.hpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * ---------------------------------------------------------------------------- - * Copyright (c) 2024 Arm Limited - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - * ---------------------------------------------------------------------------- - */ - -/** - * @file - * This module implements miscellaneous utility functions. - */ - -#pragma once - -#include -#include -#include - -#include -#include - -#ifdef __ANDROID__ - #include -#endif - -/** - * Tag type used for function dispatch; - */ -struct user_tag {}; - -/** - * @brief Convert a dispatchable API handle to the underlying dispatch key. - * - * @param ptr The dispatchable handle from the API. - * - * \return The dispatch key. - */ -static inline void* getDispatchKey( - void* ptr -) { - return *static_cast(ptr); -} - -/** - * @brief Enable to enable API entrypoint tracing to the log/logcat. - */ -#define CONFIG_TRACE 0 - -/** - * @brief Enable to enable layer logging to the log/logcat. - */ -#define CONFIG_LOG 1 - -#if CONFIG_TRACE - #ifdef __ANDROID__ - #if !defined(LGL_LOG_TAG) - #error "LGL_LOG_TAG not defined" - #endif - - #define LAYER_TRACE(x) __android_log_print(ANDROID_LOG_INFO, LGL_LOG_TAG, "API Trace: %s", x) - #else - #define LAYER_TRACE(x) printf("API Trace: %s\n", x) - #endif -#else - #define LAYER_TRACE(x) -#endif - -#if CONFIG_LOG - #ifdef __ANDROID__ - #if !defined(LGL_LOG_TAG) - #error "LGL_LOG_TAG not defined" - #endif - - #define LAYER_LOG(...) __android_log_print(ANDROID_LOG_DEBUG, LGL_LOG_TAG, __VA_ARGS__) - #define LAYER_ERR(...) __android_log_print(ANDROID_LOG_ERROR, LGL_LOG_TAG, __VA_ARGS__) - #else - #define LAYER_LOG(...) printf(__VA_ARGS__); printf("\n"); - #define LAYER_ERR(...) printf(__VA_ARGS__); printf("\n"); - #endif -#else - #define LAYER_LOG(...) - #define LAYER_ERR(...) -#endif diff --git a/source_common/framework/CMakeLists.txt b/source_common/framework/CMakeLists.txt index 247b795..86889ca 100644 --- a/source_common/framework/CMakeLists.txt +++ b/source_common/framework/CMakeLists.txt @@ -26,12 +26,15 @@ set(LIB_BINARY lib_layer_framework) add_library( ${LIB_BINARY} STATIC device_functions.cpp + entry_utils.cpp instance_functions.cpp) target_include_directories( ${LIB_BINARY} PRIVATE - # Note, this includes from the layer-specific tree + # Include from the layer-specific tree ${PROJECT_SOURCE_DIR}/source + # Needed for CMake generated version.hpp + ${PROJECT_BINARY_DIR}/source ../) target_include_directories( diff --git a/source_common/framework/device_dispatch_table.hpp b/source_common/framework/device_dispatch_table.hpp index cc16f1c..4506b90 100644 --- a/source_common/framework/device_dispatch_table.hpp +++ b/source_common/framework/device_dispatch_table.hpp @@ -28,6 +28,7 @@ #include #include "framework/device_functions.hpp" +#include "framework/utils.hpp" #include "utils/misc.hpp" #if __has_include ("layer_device_functions.hpp") diff --git a/generator/vk_common/entry_utils.hpp b/source_common/framework/entry_utils.cpp similarity index 75% rename from generator/vk_common/entry_utils.hpp rename to source_common/framework/entry_utils.cpp index bb370d0..b59bf78 100644 --- a/generator/vk_common/entry_utils.hpp +++ b/source_common/framework/entry_utils.cpp @@ -29,66 +29,34 @@ * implemented as library code which can be swapped for alternative * implementations on a per-layer basis if needed. */ -#include -#include -#include -#include -#include "utils.hpp" -#include "version.hpp" +#include "framework/entry_utils.hpp" -#include "instance.hpp" -#include "instance_functions.hpp" -#include "device.hpp" -#include "device_dispatch_table.hpp" -#include "device_functions.hpp" - -extern std::mutex g_vulkanLock; - -#define VK_LAYER_EXPORT __attribute__((visibility("default"))) - -#if defined(VK_USE_PLATFORM_ANDROID_KHR) - #define VK_LAYER_EXPORT_ANDROID VK_LAYER_EXPORT -#else - #define VK_LAYER_EXPORT_ANDROID -#endif - -/** - * @brief The layer configuration. - */ -#define LGL_VERSION VK_MAKE_VERSION(LGL_VER_MAJOR, LGL_VER_MINOR, LGL_VER_PATCH) - -static const std::array layerProps = { - {{ LGL_LAYER_NAME, LGL_VERSION, 1, LGL_LAYER_DESC }}, -}; +VkLayerInstanceCreateInfo* get_chain_info( + const VkInstanceCreateInfo* pCreateInfo, + VkLayerFunction func +) { + auto* info = static_cast(pCreateInfo->pNext); + while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && info->function == func)) + { + info = static_cast(info->pNext); + } -/** - * @brief Dispatch table lookup entry. - */ -struct DispatchTableEntry -{ - /** - * @brief The function entrypoint name. - */ - const char* name; - - /** - * @brief The function pointer. - */ - PFN_vkVoidFunction function; -}; + return const_cast(info); +} -/** - * @brief Utility macro to define a lookup for a core function. - */ -#define VK_TABLE_ENTRY(func) \ - { STR(func), reinterpret_cast(func) } +VkLayerDeviceCreateInfo* get_chain_info( + const VkDeviceCreateInfo* pCreateInfo, + VkLayerFunction func +) { + auto* info = static_cast(pCreateInfo->pNext); + while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && info->function == func)) + { + info = static_cast(info->pNext); + } -/** - * @brief Utility macro to define a lookup for a layer-dispatch-only function. - */ -#define VK_TABLE_ENTRYL(func) \ - { STR(func), reinterpret_cast(layer_##func) } + return const_cast(info); +} /** * @brief Fetch the layer function for a given instance entrypoint name. @@ -98,7 +66,7 @@ struct DispatchTableEntry * \return The layer function pointer, or \c nullptr if the layer doesn't * intercept the function. */ -static PFN_vkVoidFunction get_fixed_instance_layer_function( +PFN_vkVoidFunction get_fixed_instance_layer_function( const char* name ) { static const DispatchTableEntry layerFunctions[] = { @@ -128,7 +96,7 @@ static PFN_vkVoidFunction get_fixed_instance_layer_function( * \return The layer function pointer, or \c nullptr if the layer doesn't * intercept the function. */ -static PFN_vkVoidFunction get_instance_layer_function( +PFN_vkVoidFunction get_instance_layer_function( const char* name ) { for (auto &function : instanceIntercepts) @@ -149,7 +117,7 @@ static PFN_vkVoidFunction get_instance_layer_function( * * \return The layer function pointer, or \c nullptr if the layer doesn't intercept the function. */ -static PFN_vkVoidFunction get_device_layer_function( +PFN_vkVoidFunction get_device_layer_function( const char* name ) { static const DispatchTableEntry layerFunctions[] = { @@ -178,7 +146,7 @@ static PFN_vkVoidFunction get_device_layer_function( } /** See Vulkan API for documentation. */ -static PFN_vkVoidFunction vkGetDeviceProcAddr_default( +PFN_vkVoidFunction vkGetDeviceProcAddr_default( VkDevice device, const char* pName ) { @@ -203,7 +171,7 @@ static PFN_vkVoidFunction vkGetDeviceProcAddr_default( } /** See Vulkan API for documentation. */ -static PFN_vkVoidFunction vkGetInstanceProcAddr_default( +PFN_vkVoidFunction vkGetInstanceProcAddr_default( VkInstance instance, const char* pName ) { @@ -238,8 +206,44 @@ static PFN_vkVoidFunction vkGetInstanceProcAddr_default( return layerFunction; } +/* TODO. */ +std::vector getInstanceExtensionList( + const VkInstanceCreateInfo* pCreateInfo +) { + std::vector foundExtensions; + + // Get layer chain info, and save next pointer so we can restore later + auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); + + // Fetch the functions needed to query extensions availability + auto fpGetProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr; + auto fpGetExtensionsRaw = fpGetProcAddr(nullptr, "vkEnumerateDeviceExtensionProperties"); + auto fpGetExtensions = reinterpret_cast(fpGetExtensionsRaw); + if (!fpGetExtensions) + { + return foundExtensions; + } + + // Query number of extensions + uint32_t count; + fpGetExtensions(nullptr, &count, nullptr); + + // Reserve memory for, and then query, the extensions + std::vector extensions; + extensions.resize(count); + fpGetExtensions(nullptr, &count, extensions.data()); + + // Build the function return list + for (uint32_t i = 0; i < count; i++) + { + foundExtensions.emplace_back(extensions[i].extensionName); + } + + return foundExtensions; +} + /** See Vulkan API for documentation. */ -static VkResult vkEnumerateInstanceExtensionProperties_default( +VkResult vkEnumerateInstanceExtensionProperties_default( const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties @@ -258,7 +262,7 @@ static VkResult vkEnumerateInstanceExtensionProperties_default( } /** See Vulkan API for documentation. */ -static VkResult vkEnumerateDeviceExtensionProperties_default( +VkResult vkEnumerateDeviceExtensionProperties_default( VkPhysicalDevice gpu, const char* pLayerName, uint32_t* pPropertyCount, @@ -293,7 +297,7 @@ static VkResult vkEnumerateDeviceExtensionProperties_default( } /** See Vulkan API for documentation. */ -static VkResult vkEnumerateInstanceLayerProperties_default( +VkResult vkEnumerateInstanceLayerProperties_default( uint32_t* pPropertyCount, VkLayerProperties* pProperties ) { @@ -317,7 +321,7 @@ static VkResult vkEnumerateInstanceLayerProperties_default( } /** See Vulkan API for documentation. */ -static VkResult vkEnumerateDeviceLayerProperties_default( +VkResult vkEnumerateDeviceLayerProperties_default( VkPhysicalDevice gpu, uint32_t* pPropertyCount, VkLayerProperties* pProperties diff --git a/source_common/framework/entry_utils.hpp b/source_common/framework/entry_utils.hpp index bb370d0..09f7601 100644 --- a/source_common/framework/entry_utils.hpp +++ b/source_common/framework/entry_utils.hpp @@ -32,7 +32,9 @@ #include #include #include +#include #include +#include #include "utils.hpp" #include "version.hpp" @@ -90,6 +92,15 @@ struct DispatchTableEntry #define VK_TABLE_ENTRYL(func) \ { STR(func), reinterpret_cast(layer_##func) } + +VkLayerInstanceCreateInfo* get_chain_info( + const VkInstanceCreateInfo* pCreateInfo, + VkLayerFunction func); + +VkLayerDeviceCreateInfo* get_chain_info( + const VkDeviceCreateInfo* pCreateInfo, + VkLayerFunction func); + /** * @brief Fetch the layer function for a given instance entrypoint name. * @@ -98,27 +109,8 @@ struct DispatchTableEntry * \return The layer function pointer, or \c nullptr if the layer doesn't * intercept the function. */ -static PFN_vkVoidFunction get_fixed_instance_layer_function( - const char* name -) { - static const DispatchTableEntry layerFunctions[] = { - VK_TABLE_ENTRY(vkGetInstanceProcAddr), - VK_TABLE_ENTRY(vkEnumerateDeviceLayerProperties), - VK_TABLE_ENTRY(vkEnumerateDeviceExtensionProperties), - VK_TABLE_ENTRY(vkEnumerateInstanceLayerProperties), - VK_TABLE_ENTRY(vkEnumerateInstanceExtensionProperties), - }; - - for (auto &function : layerFunctions) - { - if (!strcmp(function.name, name)) - { - return function.function; - } - } - - return nullptr; -} +PFN_vkVoidFunction get_fixed_instance_layer_function( + const char* name); /** * @brief Fetch the layer function for a given instance entrypoint name. @@ -128,19 +120,8 @@ static PFN_vkVoidFunction get_fixed_instance_layer_function( * \return The layer function pointer, or \c nullptr if the layer doesn't * intercept the function. */ -static PFN_vkVoidFunction get_instance_layer_function( - const char* name -) { - for (auto &function : instanceIntercepts) - { - if (!strcmp(function.name, name)) - { - return function.function; - } - } - - return nullptr; -} +PFN_vkVoidFunction get_instance_layer_function( + const char* name); /** * @brief Fetch the layer function for a given device entrypoint name. @@ -149,196 +130,43 @@ static PFN_vkVoidFunction get_instance_layer_function( * * \return The layer function pointer, or \c nullptr if the layer doesn't intercept the function. */ -static PFN_vkVoidFunction get_device_layer_function( - const char* name -) { - static const DispatchTableEntry layerFunctions[] = { - VK_TABLE_ENTRY(vkGetDeviceProcAddr), - VK_TABLE_ENTRY(vkEnumerateDeviceExtensionProperties), - VK_TABLE_ENTRY(vkEnumerateDeviceLayerProperties), - }; - - for (auto &function : layerFunctions) - { - if (!strcmp(function.name, name)) - { - return function.function; - } - } - - for (auto &function : deviceIntercepts) - { - if (!strcmp(function.name, name)) - { - return function.function; - } - } - - return nullptr; -} +PFN_vkVoidFunction get_device_layer_function( + const char* name); /** See Vulkan API for documentation. */ -static PFN_vkVoidFunction vkGetDeviceProcAddr_default( +PFN_vkVoidFunction vkGetDeviceProcAddr_default( VkDevice device, - const char* pName -) { - // Hold the lock to access layer-wide global store - std::unique_lock lock { g_vulkanLock }; - auto* layer = Device::retrieve(device); - lock.unlock(); - - // Only expose functions that the driver exposes to avoid changing - // queryable interface behavior seen by the application - auto driver_function = layer->driver.vkGetDeviceProcAddr(device, pName); - auto layer_function = get_device_layer_function(pName); - - // If driver exposes it and the layer has one, use the layer function - if (driver_function && layer_function) - { - return layer_function; - } - - // Otherwise just use the driver function, which may be nullptr - return driver_function; -} + const char* pName); /** See Vulkan API for documentation. */ -static PFN_vkVoidFunction vkGetInstanceProcAddr_default( +PFN_vkVoidFunction vkGetInstanceProcAddr_default( VkInstance instance, - const char* pName -) { - // Always expose these functions ... - PFN_vkVoidFunction layerFunction = get_fixed_instance_layer_function(pName); - if (layerFunction) - { - return layerFunction; - } - - // Otherwise, only expose functions that the driver exposes to avoid - // changing queryable interface behavior seen by the application - layerFunction = get_instance_layer_function(pName); - if (instance) { - std::unique_lock lock { g_vulkanLock }; - auto* layer = Instance::retrieve(instance); - - // Don't hold the lock while calling the driver - lock.unlock(); - PFN_vkVoidFunction driverFunction = layer->nlayerGetProcAddress(layer->instance, pName); - - // If driver exposes it and the layer has one, use the layer function - if (driverFunction && layerFunction) - { - return layerFunction; - } - - // Otherwise just use the driver function, which may be nullptr - return driverFunction; - } + const char* pName); - return layerFunction; -} +/* TODO. */ +std::vector getInstanceExtensionList( + const VkInstanceCreateInfo* pCreateInfo); /** See Vulkan API for documentation. */ -static VkResult vkEnumerateInstanceExtensionProperties_default( +VkResult vkEnumerateInstanceExtensionProperties_default( const char* pLayerName, uint32_t* pPropertyCount, - VkExtensionProperties* pProperties -) { - LAYER_TRACE(__func__); - - UNUSED(pProperties); - - if (!pLayerName || strcmp(pLayerName, layerProps[0].layerName)) - { - return VK_ERROR_LAYER_NOT_PRESENT; - } - - *pPropertyCount = 0; - return VK_SUCCESS; -} + VkExtensionProperties* pProperties); /** See Vulkan API for documentation. */ -static VkResult vkEnumerateDeviceExtensionProperties_default( +VkResult vkEnumerateDeviceExtensionProperties_default( VkPhysicalDevice gpu, const char* pLayerName, uint32_t* pPropertyCount, - VkExtensionProperties* pProperties -) { - LAYER_TRACE(__func__); - - UNUSED(pProperties); - - // Android layer enumeration will always pass a nullptr for the device - if (!gpu) - { - if (!pLayerName || strcmp(pLayerName, layerProps[0].layerName)) - { - return VK_ERROR_LAYER_NOT_PRESENT; - } - - *pPropertyCount = 0; - return VK_SUCCESS; - } - - // For other cases forward to the driver to handle it - assert(!pLayerName); - - // Hold the lock to access layer-wide global store - std::unique_lock lock { g_vulkanLock }; - auto* layer = Instance::retrieve(gpu); - - // Release the lock to call into the driver - lock.unlock(); - return layer->driver.vkEnumerateDeviceExtensionProperties(gpu, pLayerName, pPropertyCount, pProperties); -} + VkExtensionProperties* pProperties); /** See Vulkan API for documentation. */ -static VkResult vkEnumerateInstanceLayerProperties_default( +VkResult vkEnumerateInstanceLayerProperties_default( uint32_t* pPropertyCount, - VkLayerProperties* pProperties -) { - LAYER_TRACE(__func__); - - if (pProperties) - { - size_t count = std::min(layerProps.size(), static_cast(*pPropertyCount)); - if (count < layerProps.size()) - { - return VK_INCOMPLETE; - } - - memcpy(pProperties, layerProps.data(), count * sizeof(VkLayerProperties)); - *pPropertyCount = count; - return VK_SUCCESS; - } - - *pPropertyCount = layerProps.size(); - return VK_SUCCESS; -} + VkLayerProperties* pProperties); /** See Vulkan API for documentation. */ -static VkResult vkEnumerateDeviceLayerProperties_default( +VkResult vkEnumerateDeviceLayerProperties_default( VkPhysicalDevice gpu, uint32_t* pPropertyCount, - VkLayerProperties* pProperties -) { - LAYER_TRACE(__func__); - - UNUSED(gpu); - - if (pProperties) - { - size_t count = std::min(layerProps.size(), static_cast(*pPropertyCount)); - if (count < layerProps.size()) - { - return VK_INCOMPLETE; - } - - memcpy(pProperties, layerProps.data(), count * sizeof(VkLayerProperties)); - *pPropertyCount = count; - return VK_SUCCESS; - } - - *pPropertyCount = layerProps.size(); - return VK_SUCCESS; -} + VkLayerProperties* pProperties); diff --git a/source_common/framework/instance_dispatch_table.hpp b/source_common/framework/instance_dispatch_table.hpp index 3b01941..8ef5751 100644 --- a/source_common/framework/instance_dispatch_table.hpp +++ b/source_common/framework/instance_dispatch_table.hpp @@ -28,6 +28,7 @@ #include #include "framework/instance_functions.hpp" +#include "framework/utils.hpp" #include "utils/misc.hpp" #if __has_include ("layer_instance_functions.hpp") diff --git a/source_common/framework/instance_functions.cpp b/source_common/framework/instance_functions.cpp index 0d3b4fd..e422bfe 100644 --- a/source_common/framework/instance_functions.cpp +++ b/source_common/framework/instance_functions.cpp @@ -27,39 +27,14 @@ #include #include -#include "utils.hpp" -#include "instance.hpp" #include "device.hpp" +#include "entry_utils.hpp" +#include "instance.hpp" #include "instance_functions.hpp" +#include "utils.hpp" extern std::mutex g_vulkanLock; -static VkLayerInstanceCreateInfo* get_chain_info( - const VkInstanceCreateInfo* pCreateInfo, - VkLayerFunction func -) { - auto* info = static_cast(pCreateInfo->pNext); - while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && info->function == func)) - { - info = static_cast(info->pNext); - } - - return const_cast(info); -} - -static VkLayerDeviceCreateInfo* get_chain_info( - const VkDeviceCreateInfo* pCreateInfo, - VkLayerFunction func -) { - auto* info = static_cast(pCreateInfo->pNext); - while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && info->function == func)) - { - info = static_cast(info->pNext); - } - - return const_cast(info); -} - #if defined(VK_USE_PLATFORM_ANDROID_KHR) /* See Vulkan API for documentation. */ @@ -145,17 +120,15 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_vkCreateDevice_default( // Advance the link info for the next element on the chain chainInfo->u.pLayerInfo = chainInfo->u.pLayerInfo->pNext; - auto res = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice); if (res != VK_SUCCESS) { return res; } - auto device = std::make_unique(layer, physicalDevice, *pDevice, fpGetDeviceProcAddr); - - // Hold the lock to access layer-wide global store + // Retake the lock to access layer-wide global store lock.lock(); + auto device = std::make_unique(layer, physicalDevice, *pDevice, fpGetDeviceProcAddr); Device::store(*pDevice, std::move(device)); return VK_SUCCESS; @@ -222,7 +195,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_vkCreateInstance_default( return res; } - // Hold the lock to access layer-wide global store + // Retake the lock to access layer-wide global store auto instance = std::make_unique(*pInstance, fpGetInstanceProcAddr); { std::lock_guard lock { g_vulkanLock }; diff --git a/source_common/framework/utils.hpp b/source_common/framework/utils.hpp index 9ddbe92..b00feb7 100644 --- a/source_common/framework/utils.hpp +++ b/source_common/framework/utils.hpp @@ -89,8 +89,8 @@ static inline void* getDispatchKey( #error "LGL_LOG_TAG not defined" #endif - #define LAYER_LOG(...) __android_log_print(ANDROID_LOG_DEBUG, LGL_LOG_TAG, __VA_ARGS__) - #define LAYER_ERR(...) __android_log_print(ANDROID_LOG_ERROR, LGL_LOG_TAG, __VA_ARGS__) + #define LAYER_LOG(...) __android_log_print(ANDROID_LOG_INFO, LGL_LOG_TAG, __VA_ARGS__) + #define LAYER_ERR(...) __android_log_print(ANDROID_LOG_INFO, LGL_LOG_TAG, __VA_ARGS__) #else #define LAYER_LOG(...) printf(__VA_ARGS__); printf("\n"); #define LAYER_ERR(...) printf(__VA_ARGS__); printf("\n");