Commit 950cdd40 authored by Joseph Jenner-Bailey's avatar Joseph Jenner-Bailey Committed by Matteo Franchin
Browse files

layer: improve how layer handles Vulkan extensions



In vkCreateInstance:

- ppEnabledExtensionNames is checked to determine which window-system
  platforms the layer should enable support for.
- support in the layer is always enabled if possible, even if this may
  be provided by the ICDs. Platforms not supported by the layer should
  still be correctly supported by the ICDs (untested.)
- pApplicationInfo is changed to bump the Vulkan API version
  in order to enable instance extensions that are necessary for some
  of the platforms implemented in the layer.

In vkCreateDevice:

- ppEnabledExtensionNames is extended with device extensions required
  by the layer.
- if the extensions are not supported by the physical device, the
  layer fails to initialize the device.

Change-Id: Ibdd69fca38e7909d5b8f0ac7698805ea5f425ac6
Signed-off-by: Joseph Jenner-Bailey's avatarJoe Jenner-Bailey <joe.jenner-bailey@arm.com>
Signed-off-by: Matteo Franchin's avatarMatteo Franchin <matteo.franchin@arm.com>
parent f0dcf4fb
Pipeline #328792 passed with stage
in 1 minute and 38 seconds
......@@ -14,17 +14,17 @@
"instance_extensions": [
{
"name" : "VK_EXT_headless_surface",
"spec_version" : 1
"spec_version" : "1"
},
{
"name" : "VK_KHR_surface",
"spec_version" : 1
"spec_version" : "1"
}
],
"device_extensions": [
{
"name" : "VK_KHR_swapchain",
"spec_version" : 1
"spec_version" : "1"
}
],
"enable_environment": {
......
/*
* Copyright (c) 2016-2020 Arm Limited.
* Copyright (c) 2016-2021 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
......@@ -25,11 +25,15 @@
#include <cassert>
#include <cstdio>
#include <cstring>
#include <vulkan/vk_layer.h>
#include "private_data.hpp"
#include "surface_api.hpp"
#include "swapchain_api.hpp"
#include "util/extension_list.hpp"
#include "util/custom_allocator.hpp"
#include "wsi/wsi_factory.hpp"
#define VK_LAYER_API_VERSION VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION)
......@@ -37,10 +41,7 @@ namespace layer
{
static const VkLayerProperties global_layer = {
"VK_LAYER_window_system_integration",
VK_LAYER_API_VERSION,
1,
"Window system integration layer",
"VK_LAYER_window_system_integration", VK_LAYER_API_VERSION, 1, "Window system integration layer",
};
static const VkExtensionProperties device_extension[] = { { VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_KHR_SWAPCHAIN_SPEC_VERSION } };
......@@ -115,10 +116,7 @@ VKAPI_ATTR VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCr
return chain_info;
}
/*
* This is where we get our initialisation and construct our dispatch table. All layers must implement the function.
* If you wish to intercept any device functions at all you need to implement vkCreateDevice.
*/
/* This is where the layer is initialised and the instance dispatch table is constructed. */
VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
VkInstance *pInstance)
{
......@@ -131,8 +129,8 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con
return VK_ERROR_INITIALIZATION_FAILED;
}
/* Retrieve the vkGetInstanceProcAddr and the vkCreateInstance function pointers for the next layer in the chain. */
PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(nullptr, "vkCreateInstance");
if (nullptr == fpCreateInstance)
{
......@@ -142,11 +140,55 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con
/* Advance the link info for the next element on the chain. */
layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext;
/* Now call create instance on the chain further down the list. */
VkResult ret = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
/* The layer needs some Vulkan 1.1 functionality in order to operate correctly.
* We thus change the application info to require this API version, if necessary.
* This may have consequences for ICDs whose behaviour depends on apiVersion.
*/
const uint32_t minimum_required_vulkan_version = VK_API_VERSION_1_1;
VkApplicationInfo modified_app_info{};
if (nullptr != pCreateInfo->pApplicationInfo)
{
modified_app_info = *pCreateInfo->pApplicationInfo;
if (modified_app_info.apiVersion < minimum_required_vulkan_version)
{
modified_app_info.apiVersion = minimum_required_vulkan_version;
}
}
else
{
modified_app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
modified_app_info.apiVersion = minimum_required_vulkan_version;
}
instance_private_data::create(*pInstance, fpGetInstanceProcAddr, loader_callback);
return ret;
VkInstanceCreateInfo modified_info = *pCreateInfo;
modified_info.pApplicationInfo = &modified_app_info;
/* Now call create instance on the chain further down the list.
* Note that we do not remove the extensions that the layer supports from modified_info.ppEnabledExtensionNames.
* Layers have to abide the rule that vkCreateInstance must not generate an error for unrecognized extension names.
* Also, the loader filters the extension list to ensure that ICDs do not see extensions that they do not support.
*/
VkResult result;
result = fpCreateInstance(&modified_info, pAllocator, pInstance);
if (result != VK_SUCCESS)
{
return result;
}
instance_dispatch_table table;
result = table.populate(*pInstance, fpGetInstanceProcAddr);
if (result != VK_SUCCESS)
{
return result;
}
/* Find all the platforms that the layer can handle based on pCreateInfo->ppEnabledExtensionNames. */
auto layer_platforms_to_enable = wsi::find_enabled_layer_platforms(pCreateInfo);
std::unique_ptr<instance_private_data> inst_data{
new instance_private_data{table, loader_callback, layer_platforms_to_enable}};
instance_private_data::set(*pInstance, std::move(inst_data));
return VK_SUCCESS;
}
VKAPI_ATTR VkResult create_device(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
......@@ -173,134 +215,170 @@ VKAPI_ATTR VkResult create_device(VkPhysicalDevice physicalDevice, const VkDevic
/* Advance the link info for the next element on the chain. */
layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext;
/* Now call create device on the chain further down the list. */
VkResult ret = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
device_private_data::create(*pDevice, fpGetDeviceProcAddr, physicalDevice, loader_callback);
return ret;
}
} /* namespace layer */
extern "C"
{
VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL
wsi_layer_vkGetDeviceProcAddr(VkDevice device, const char *funcName);
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
wsi_layer_vkGetInstanceProcAddr(VkInstance instance, const char *funcName);
/* Clean up the dispatch table for this instance. */
VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
wsi_layer_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator)
/* Copy the extension to a util::extension_list. */
util::allocator allocator{pAllocator, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND};
util::extension_list enabled_extensions{allocator};
VkResult result;
result = enabled_extensions.add(pCreateInfo->ppEnabledExtensionNames, pCreateInfo->enabledExtensionCount);
if (result != VK_SUCCESS)
{
assert(instance);
layer::instance_private_data::get(layer::get_key(instance))
.disp.DestroyInstance(instance, pAllocator);
layer::instance_private_data::destroy(instance);
return result;
}
VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
wsi_layer_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator)
/* Add the extensions required by the platforms that are being enabled in the layer. */
auto &inst_data = instance_private_data::get(physicalDevice);
const util::wsi_platform_set& enabled_platforms = inst_data.get_enabled_platforms();
result = wsi::add_extensions_required_by_layer(physicalDevice, enabled_platforms, enabled_extensions);
if (result != VK_SUCCESS)
{
layer::device_private_data::destroy(device);
return result;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
wsi_layer_vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkInstance *pInstance)
util::vector<const char *> modified_enabled_extensions{allocator};
if (!enabled_extensions.get_extension_strings(modified_enabled_extensions))
{
return layer::create_instance(pCreateInfo, pAllocator, pInstance);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
wsi_layer_vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkDevice *pDevice)
/* Now call create device on the chain further down the list. */
VkDeviceCreateInfo modified_info = *pCreateInfo;
modified_info.ppEnabledExtensionNames = modified_enabled_extensions.data();
modified_info.enabledExtensionCount = modified_enabled_extensions.size();
result = fpCreateDevice(physicalDevice, &modified_info, pAllocator, pDevice);
if (result != VK_SUCCESS)
{
return layer::create_device(physicalDevice, pCreateInfo, pAllocator, pDevice);
return result;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)
device_dispatch_table table;
result = table.populate(*pDevice, fpGetDeviceProcAddr);
if (result != VK_SUCCESS)
{
assert(pVersionStruct);
assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
return result;
}
/* 2 is the minimum interface version which would utilize this function. */
assert(pVersionStruct->loaderLayerInterfaceVersion >= 2);
std::unique_ptr<device_private_data> device{new device_private_data{inst_data, physicalDevice, *pDevice,
table, loader_callback}};
device_private_data::set(*pDevice, std::move(device));
return VK_SUCCESS;
}
/* Set our requested interface version. Set to 2 for now to separate us from newer versions. */
pVersionStruct->loaderLayerInterfaceVersion = 2;
} /* namespace layer */
/* Fill in struct values. */
pVersionStruct->pfnGetInstanceProcAddr = &wsi_layer_vkGetInstanceProcAddr;
pVersionStruct->pfnGetDeviceProcAddr = &wsi_layer_vkGetDeviceProcAddr;
pVersionStruct->pfnGetPhysicalDeviceProcAddr = nullptr;
extern "C" {
VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL wsi_layer_vkGetDeviceProcAddr(VkDevice device, const char *funcName);
return VK_SUCCESS;
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL wsi_layer_vkGetInstanceProcAddr(VkInstance instance,
const char *funcName);
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
wsi_layer_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
uint32_t *pCount, VkExtensionProperties *pProperties)
{
if (pLayerName && !strcmp(pLayerName, layer::global_layer.layerName))
return layer::extension_properties(1, layer::device_extension, pCount, pProperties);
/* Clean up the dispatch table for this instance. */
VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL wsi_layer_vkDestroyInstance(VkInstance instance,
const VkAllocationCallbacks *pAllocator)
{
assert(instance);
layer::instance_private_data::get(instance).disp.DestroyInstance(instance, pAllocator);
layer::instance_private_data::destroy(instance);
}
assert(physicalDevice);
return layer::instance_private_data::get(layer::get_key(physicalDevice))
.disp.EnumerateDeviceExtensionProperties(physicalDevice, pLayerName, pCount, pProperties);
}
VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL wsi_layer_vkDestroyDevice(VkDevice device,
const VkAllocationCallbacks *pAllocator)
{
layer::device_private_data::destroy(device);
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
wsi_layer_vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties)
{
if (pLayerName && !strcmp(pLayerName, layer::global_layer.layerName))
return layer::extension_properties(1, layer::instance_extension, pCount, pProperties);
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL wsi_layer_vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkInstance *pInstance)
{
return layer::create_instance(pCreateInfo, pAllocator, pInstance);
}
return VK_ERROR_LAYER_NOT_PRESENT;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL wsi_layer_vkCreateDevice(VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkDevice *pDevice)
{
return layer::create_device(physicalDevice, pCreateInfo, pAllocator, pDevice);
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
wsi_layer_vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties)
{
return layer::layer_properties(1, &layer::global_layer, pCount, pProperties);
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)
{
assert(pVersionStruct);
assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
/* 2 is the minimum interface version which would utilize this function. */
assert(pVersionStruct->loaderLayerInterfaceVersion >= 2);
/* Set our requested interface version. Set to 2 for now to separate us from newer versions. */
pVersionStruct->loaderLayerInterfaceVersion = 2;
/* Fill in struct values. */
pVersionStruct->pfnGetInstanceProcAddr = &wsi_layer_vkGetInstanceProcAddr;
pVersionStruct->pfnGetDeviceProcAddr = &wsi_layer_vkGetDeviceProcAddr;
pVersionStruct->pfnGetPhysicalDeviceProcAddr = nullptr;
#define GET_PROC_ADDR(func) \
return VK_SUCCESS;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL wsi_layer_vkEnumerateDeviceExtensionProperties(
VkPhysicalDevice physicalDevice, const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties)
{
if (pLayerName && !strcmp(pLayerName, layer::global_layer.layerName))
return layer::extension_properties(1, layer::device_extension, pCount, pProperties);
assert(physicalDevice);
return layer::instance_private_data::get(physicalDevice)
.disp.EnumerateDeviceExtensionProperties(physicalDevice, pLayerName, pCount, pProperties);
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL wsi_layer_vkEnumerateInstanceExtensionProperties(
const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties)
{
if (pLayerName && !strcmp(pLayerName, layer::global_layer.layerName))
return layer::extension_properties(1, layer::instance_extension, pCount, pProperties);
return VK_ERROR_LAYER_NOT_PRESENT;
}
VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
wsi_layer_vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties)
{
return layer::layer_properties(1, &layer::global_layer, pCount, pProperties);
}
#define GET_PROC_ADDR(func) \
if (!strcmp(funcName, #func)) \
return (PFN_vkVoidFunction)&wsi_layer_##func;
VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL
wsi_layer_vkGetDeviceProcAddr(VkDevice device, const char *funcName)
{
GET_PROC_ADDR(vkCreateSwapchainKHR);
GET_PROC_ADDR(vkDestroySwapchainKHR);
GET_PROC_ADDR(vkGetSwapchainImagesKHR);
GET_PROC_ADDR(vkAcquireNextImageKHR);
GET_PROC_ADDR(vkQueuePresentKHR);
VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL wsi_layer_vkGetDeviceProcAddr(VkDevice device, const char *funcName)
{
GET_PROC_ADDR(vkCreateSwapchainKHR);
GET_PROC_ADDR(vkDestroySwapchainKHR);
GET_PROC_ADDR(vkGetSwapchainImagesKHR);
GET_PROC_ADDR(vkAcquireNextImageKHR);
GET_PROC_ADDR(vkQueuePresentKHR);
return layer::device_private_data::get(layer::get_key(device)).disp.GetDeviceProcAddr(device, funcName);
}
return layer::device_private_data::get(device).disp.GetDeviceProcAddr(device, funcName);
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
wsi_layer_vkGetInstanceProcAddr(VkInstance instance, const char *funcName)
{
GET_PROC_ADDR(vkGetDeviceProcAddr);
GET_PROC_ADDR(vkGetInstanceProcAddr);
GET_PROC_ADDR(vkCreateInstance);
GET_PROC_ADDR(vkDestroyInstance);
GET_PROC_ADDR(vkCreateDevice);
GET_PROC_ADDR(vkDestroyDevice);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfaceSupportKHR);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfaceFormatsKHR);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfacePresentModesKHR);
GET_PROC_ADDR(vkEnumerateDeviceExtensionProperties);
GET_PROC_ADDR(vkEnumerateInstanceExtensionProperties);
GET_PROC_ADDR(vkEnumerateInstanceLayerProperties);
return layer::instance_private_data::get(layer::get_key(instance)).disp.GetInstanceProcAddr(instance, funcName);
}
VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL wsi_layer_vkGetInstanceProcAddr(VkInstance instance,
const char *funcName)
{
GET_PROC_ADDR(vkGetDeviceProcAddr);
GET_PROC_ADDR(vkGetInstanceProcAddr);
GET_PROC_ADDR(vkCreateInstance);
GET_PROC_ADDR(vkDestroyInstance);
GET_PROC_ADDR(vkCreateDevice);
GET_PROC_ADDR(vkDestroyDevice);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfaceSupportKHR);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfaceFormatsKHR);
GET_PROC_ADDR(vkGetPhysicalDeviceSurfacePresentModesKHR);
GET_PROC_ADDR(vkEnumerateDeviceExtensionProperties);
GET_PROC_ADDR(vkEnumerateInstanceExtensionProperties);
GET_PROC_ADDR(vkEnumerateInstanceLayerProperties);
return layer::instance_private_data::get(instance).disp.GetInstanceProcAddr(instance, funcName);
}
} /* extern "C" */
/*
* Copyright (c) 2018-2019 Arm Limited.
* Copyright (c) 2018-2021 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
......@@ -22,92 +22,196 @@
* SOFTWARE.
*/
#include <cassert>
#include <map>
#include <mutex>
#include "private_data.hpp"
using scoped_mutex = std::lock_guard<std::mutex>;
#include "wsi/wsi_factory.hpp"
#include <unordered_map>
namespace layer
{
static std::mutex g_data_lock;
static std::map<void *, instance_private_data *> g_instance_data;
static std::map<void *, device_private_data *> g_device_data;
static std::unordered_map<void *, std::unique_ptr<instance_private_data>> g_instance_data;
static std::unordered_map<void *, std::unique_ptr<device_private_data>> g_device_data;
template <typename object_type, typename get_proc_type>
static PFN_vkVoidFunction get_proc_helper(object_type obj, get_proc_type get_proc,
const char* proc_name, bool required, bool &ok)
{
PFN_vkVoidFunction ret = get_proc(obj, proc_name);
if (nullptr == ret && required)
{
ok = false;
}
return ret;
}
instance_private_data::instance_private_data(VkInstance inst, PFN_vkGetInstanceProcAddr get_proc,
PFN_vkSetInstanceLoaderData set_loader_data)
: disp(inst, get_proc)
VkResult instance_dispatch_table::populate(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
{
bool ok = true;
#define REQUIRED(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(instance, get_proc, "vk" #x, true, ok));
#define OPTIONAL(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(instance, get_proc, "vk" #x, false, ok));
INSTANCE_ENTRYPOINTS_LIST(REQUIRED, OPTIONAL);
#undef REQUIRED
#undef OPTIONAL
return ok ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
}
VkResult device_dispatch_table::populate(VkDevice device, PFN_vkGetDeviceProcAddr get_proc)
{
bool ok = true;
#define REQUIRED(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(device, get_proc, "vk" #x, true, ok));
#define OPTIONAL(x) x = reinterpret_cast<PFN_vk##x>(get_proc_helper(device, get_proc, "vk" #x, false, ok));
DEVICE_ENTRYPOINTS_LIST(REQUIRED, OPTIONAL);
#undef REQUIRED
#undef OPTIONAL
return ok ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
}
instance_private_data::instance_private_data(const instance_dispatch_table& table,
PFN_vkSetInstanceLoaderData set_loader_data,
util::wsi_platform_set enabled_layer_platforms)
: disp(table)
, SetInstanceLoaderData(set_loader_data)
, enabled_layer_platforms(enabled_layer_platforms)
{
}
instance_private_data &instance_private_data::create(VkInstance inst, PFN_vkGetInstanceProcAddr get_proc,
PFN_vkSetInstanceLoaderData set_loader_data)
template <typename dispatchable_type>
static inline void *get_key(dispatchable_type dispatchable_object)
{
return *reinterpret_cast<void **>(dispatchable_object);
}
void instance_private_data::set(VkInstance inst, std::unique_ptr<instance_private_data> inst_data)
{
instance_private_data *inst_data = new instance_private_data(inst, get_proc, set_loader_data);
scoped_mutex lock(g_data_lock);
g_instance_data[get_key(inst)] = inst_data;
return *inst_data;
g_instance_data[get_key(inst)] = std::move(inst_data);
}
instance_private_data &instance_private_data::get(void *key)
template <typename dispatchable_type>
static instance_private_data &get_instance_private_data(dispatchable_type dispatchable_object)
{
scoped_mutex lock(g_data_lock);
instance_private_data *data = g_instance_data[key];
assert(data);
return *data;
return *g_instance_data[get_key(dispatchable_object)];
}
instance_private_data &instance_private_data::get(VkInstance instance)
{
return get_instance_private_data(instance);
}
instance_private_data &instance_private_data::get(VkPhysicalDevice phys_dev)
{
return get_instance_private_data(phys_dev);
}
static VkIcdWsiPlatform get_platform_of_surface(VkSurfaceKHR surface)
{
VkIcdSurfaceBase *surface_base = reinterpret_cast<VkIcdSurfaceBase *>(surface);
return surface_base->platform;
}
bool instance_private_data::does_layer_support_surface(VkSurfaceKHR surface)
{
return enabled_layer_platforms.contains(get_platform_of_surface(surface));
}
bool instance_private_data::do_icds_support_surface(VkPhysicalDevice, VkSurfaceKHR)
{
/* For now assume ICDs do not support VK_KHR_surface. This means that the layer will handle all the surfaces it can
* handle (even if the ICDs can handle the surface) and only call down for surfaces it cannot handle. In the future
* we may allow system integrators to configure which ICDs have precedence handling which platforms.
*/
return false;
}
bool instance_private_data::should_layer_handle_surface(VkPhysicalDevice phys_dev, VkSurfaceKHR surface)
{
/* If the layer cannot handle the surface, then necessarily the ICDs or layers below us must be able to do it:
* the fact that the surface exists means that the Vulkan loader created it. In turn, this means that someone
* among the ICDs and layers advertised support for it. If it's not us, then it must be one of the layers/ICDs
* below us. It is therefore safe to always return false (and therefore call-down) when layer_can_handle_surface
* is false.
*/
bool icd_can_handle_surface = do_icds_support_surface(phys_dev, surface);
bool layer_can_handle_surface = does_layer_support_surface(surface);
bool ret = layer_can_handle_surface && !icd_can_handle_surface;
return ret;
}
void instance_private_data::destroy(VkInstance inst)
{
instance_private_data *data;
{
scoped_mutex lock(g_data_lock);
data = g_instance_data[get_key(inst)];
assert(data);
g_instance_data.erase(get_key(inst));
}
delete data;
scoped_mutex lock(g_data_lock);
g_instance_data.erase(get_key(inst));
}
device_private_data::device_private_data(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
instance_private_data &inst_data, PFN_vkSetDeviceLoaderData set_loader_data)
: disp(dev, get_proc)
, instance_data(inst_data)
, SetDeviceLoaderData(set_loader_data)
device_private_data::device_private_data(instance_private_data &inst_data, VkPhysicalDevice phys_dev, VkDevice dev,
const device_dispatch_table &table, PFN_vkSetDeviceLoaderData set_loader_data)
: disp{table}
, instance_data{inst_data}
, SetDeviceLoaderData{set_loader_data}
, physical_device{phys_dev}
, device{dev}
{
}
device_private_data &device_private_data::create(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
VkPhysicalDevice phys_dev, PFN_vkSetDeviceLoaderData set_loader_data)
void device_private_data::set(VkDevice dev, std::unique_ptr<device_private_data> dev_data)
{
device_private_data *dev_data =
new device_private_data(dev, get_proc, instance_private_data::get(get_key(phys_dev)), set_loader_data);
scoped_mutex lock(g_data_lock);
g_device_data[get_key(dev)] = dev_data;
return *dev_data;
g_device_data[get_key(dev)] = std::move(dev_data);
}
device_private_data &device_private_data::get(void *key)
template <typename dispatchable_type>
static device_private_data &get_device_private_data(dispatchable_type dispatchable_object)
{
scoped_mutex lock(g_data_lock);
device_private_data *data = g_device_data[key];
assert(data);
return *data;
return *g_device_data[get_key(dispatchable_object)];
}