2019-05-24 15:57:50 +01:00
|
|
|
/*
|
2024-12-04 13:02:34 +00:00
|
|
|
* Copyright (c) 2018-2025 Arm Limited.
|
2019-05-24 15:57:50 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-03-18 12:05:46 +00:00
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
|
|
2019-05-24 15:57:50 +01:00
|
|
|
#include "private_data.hpp"
|
2020-06-24 09:36:21 +01:00
|
|
|
#include "wsi/wsi_factory.hpp"
|
2021-07-21 20:19:52 +01:00
|
|
|
#include "wsi/surface.hpp"
|
2021-05-27 13:53:07 +01:00
|
|
|
#include "util/unordered_map.hpp"
|
|
|
|
|
#include "util/log.hpp"
|
2022-03-18 12:05:46 +00:00
|
|
|
#include "util/helpers.hpp"
|
2025-01-15 16:05:05 +00:00
|
|
|
#include "util/macros.hpp"
|
2019-05-24 15:57:50 +01:00
|
|
|
|
|
|
|
|
namespace layer
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
static std::mutex g_data_lock;
|
2021-05-27 13:53:07 +01:00
|
|
|
|
|
|
|
|
/* The dictionaries below use plain pointers to store the instance/device private data objects.
|
|
|
|
|
* This means that these objects are leaked if the application terminates without calling vkDestroyInstance
|
|
|
|
|
* or vkDestroyDevice. This is fine as it is the application's responsibility to call these.
|
|
|
|
|
*/
|
|
|
|
|
static util::unordered_map<void *, instance_private_data *> g_instance_data{ util::allocator::get_generic() };
|
|
|
|
|
static util::unordered_map<void *, device_private_data *> g_device_data{ util::allocator::get_generic() };
|
2020-06-24 09:36:21 +01:00
|
|
|
|
2024-01-24 09:21:00 +01:00
|
|
|
VkResult instance_dispatch_table::populate(VkInstance instance, PFN_vkGetInstanceProcAddr get_proc)
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
2024-01-24 09:21:00 +01:00
|
|
|
static constexpr entrypoint entrypoints_init[] = {
|
|
|
|
|
#define DISPATCH_TABLE_ENTRY(name, ext_name, api_version, required) \
|
|
|
|
|
{ "vk" #name, ext_name, nullptr, api_version, false, required },
|
|
|
|
|
INSTANCE_ENTRYPOINTS_LIST(DISPATCH_TABLE_ENTRY)
|
|
|
|
|
#undef DISPATCH_TABLE_ENTRY
|
2024-11-15 12:49:31 +00:00
|
|
|
#if VULKAN_WSI_LAYER_EXPERIMENTAL
|
|
|
|
|
{ "GetSwapchainTimeDomainPropertiesEXT", VK_EXT_PRESENT_TIMING_EXTENSION_NAME, nullptr, API_VERSION_MAX, false,
|
|
|
|
|
false }
|
|
|
|
|
#endif /* VULKAN_WSI_LAYER_EXPERIMENTAL */
|
2024-01-24 09:21:00 +01:00
|
|
|
};
|
2024-11-15 12:49:31 +00:00
|
|
|
|
2024-01-24 09:21:00 +01:00
|
|
|
static constexpr auto num_entrypoints = std::distance(std::begin(entrypoints_init), std::end(entrypoints_init));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_entrypoints; i++)
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
2024-01-24 09:21:00 +01:00
|
|
|
const entrypoint *entrypoint = &entrypoints_init[i];
|
|
|
|
|
PFN_vkVoidFunction ret = get_proc(instance, entrypoint->name);
|
|
|
|
|
if (!ret && entrypoint->required)
|
|
|
|
|
{
|
|
|
|
|
return VK_ERROR_INITIALIZATION_FAILED;
|
|
|
|
|
}
|
|
|
|
|
struct entrypoint e = *entrypoint;
|
|
|
|
|
e.fn = ret;
|
|
|
|
|
e.user_visible = false;
|
|
|
|
|
|
2024-04-10 18:30:21 +01:00
|
|
|
if (!m_entrypoints->try_insert(std::make_pair(e.name, e)).has_value())
|
2024-01-24 09:21:00 +01:00
|
|
|
{
|
|
|
|
|
WSI_LOG_ERROR("Failed to allocate memory for instance dispatch table entry.");
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
2024-01-24 09:21:00 +01:00
|
|
|
|
|
|
|
|
return VK_SUCCESS;
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
2019-05-24 15:57:50 +01:00
|
|
|
|
2024-01-24 09:21:00 +01:00
|
|
|
void dispatch_table::set_user_enabled_extensions(const char *const *extension_names, size_t extension_count)
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
2024-01-24 09:21:00 +01:00
|
|
|
for (size_t i = 0; i < extension_count; i++)
|
|
|
|
|
{
|
2024-04-10 18:30:21 +01:00
|
|
|
for (auto &entrypoint : *m_entrypoints)
|
2024-01-24 09:21:00 +01:00
|
|
|
{
|
|
|
|
|
if (!strcmp(entrypoint.second.ext_name, extension_names[i]))
|
|
|
|
|
{
|
|
|
|
|
entrypoint.second.user_visible = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 09:21:00 +01:00
|
|
|
PFN_vkVoidFunction instance_dispatch_table::get_user_enabled_entrypoint(VkInstance instance, uint32_t api_version,
|
|
|
|
|
const char *fn_name) const
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
2024-04-10 18:30:21 +01:00
|
|
|
auto item = m_entrypoints->find(fn_name);
|
|
|
|
|
if (item != m_entrypoints->end())
|
2024-01-24 09:21:00 +01:00
|
|
|
{
|
|
|
|
|
/* An entrypoint is allowed to use if it has been enabled by the user or is included in the core specficiation of the API version.
|
|
|
|
|
* Entrypoints included in API version 1.0 are allowed by default. */
|
|
|
|
|
if (item->second.user_visible || item->second.api_version <= api_version ||
|
|
|
|
|
item->second.api_version == VK_API_VERSION_1_0)
|
|
|
|
|
{
|
|
|
|
|
return item->second.fn;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetInstanceProcAddr(instance, fn_name).value_or(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkResult device_dispatch_table::populate(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc_fn)
|
|
|
|
|
{
|
|
|
|
|
static constexpr entrypoint entrypoints_init[] = {
|
|
|
|
|
#define DISPATCH_TABLE_ENTRY(name, ext_name, api_version, required) \
|
|
|
|
|
{ "vk" #name, ext_name, nullptr, api_version, false, required },
|
|
|
|
|
DEVICE_ENTRYPOINTS_LIST(DISPATCH_TABLE_ENTRY)
|
|
|
|
|
#undef DISPATCH_TABLE_ENTRY
|
|
|
|
|
};
|
|
|
|
|
static constexpr auto num_entrypoints = std::distance(std::begin(entrypoints_init), std::end(entrypoints_init));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_entrypoints; i++)
|
|
|
|
|
{
|
|
|
|
|
const entrypoint entrypoint = entrypoints_init[i];
|
|
|
|
|
PFN_vkVoidFunction ret = get_proc_fn(dev, entrypoint.name);
|
|
|
|
|
if (!ret && entrypoint.required)
|
|
|
|
|
{
|
|
|
|
|
return VK_ERROR_INITIALIZATION_FAILED;
|
|
|
|
|
}
|
|
|
|
|
struct entrypoint e = entrypoint;
|
|
|
|
|
e.fn = ret;
|
|
|
|
|
e.user_visible = false;
|
|
|
|
|
|
2024-04-10 18:30:21 +01:00
|
|
|
if (!m_entrypoints->try_insert(std::make_pair(e.name, e)).has_value())
|
2024-01-24 09:21:00 +01:00
|
|
|
{
|
|
|
|
|
WSI_LOG_ERROR("Failed to allocate memory for device dispatch table entry.");
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PFN_vkVoidFunction device_dispatch_table::get_user_enabled_entrypoint(VkDevice device, uint32_t api_version,
|
|
|
|
|
const char *fn_name) const
|
|
|
|
|
{
|
2024-04-10 18:30:21 +01:00
|
|
|
auto item = m_entrypoints->find(fn_name);
|
|
|
|
|
if (item != m_entrypoints->end())
|
2024-01-24 09:21:00 +01:00
|
|
|
{
|
|
|
|
|
/* An entrypoint is allowed to use if it has been enabled by the user or is included in the core specficiation of the API version.
|
|
|
|
|
* Entrypoints included in API version 1.0 are allowed by default. */
|
|
|
|
|
if (item->second.user_visible || item->second.api_version <= api_version ||
|
|
|
|
|
item->second.api_version == VK_API_VERSION_1_0)
|
|
|
|
|
{
|
|
|
|
|
return item->second.fn;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetDeviceProcAddr(device, fn_name).value_or(nullptr);
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 18:30:21 +01:00
|
|
|
instance_private_data::instance_private_data(instance_dispatch_table table, PFN_vkSetInstanceLoaderData set_loader_data,
|
2024-01-24 09:21:00 +01:00
|
|
|
util::wsi_platform_set enabled_layer_platforms, const uint32_t api_version,
|
2021-05-27 13:53:07 +01:00
|
|
|
const util::allocator &alloc)
|
2024-01-24 09:21:00 +01:00
|
|
|
: disp{ std::move(table) }
|
|
|
|
|
, api_version{ api_version }
|
2021-11-08 13:32:12 +00:00
|
|
|
, SetInstanceLoaderData{ set_loader_data }
|
|
|
|
|
, enabled_layer_platforms{ enabled_layer_platforms }
|
|
|
|
|
, allocator{ alloc }
|
|
|
|
|
, surfaces{ alloc }
|
|
|
|
|
, enabled_extensions{ allocator }
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Obtain the loader's dispatch table for the given dispatchable object.
|
|
|
|
|
* @note Dispatchable objects are structures that have a VkLayerDispatchTable as their first member.
|
2022-11-11 14:27:03 +00:00
|
|
|
We treat the dispatchable object as a void** and then dereference to use the VkLayerDispatchTable
|
|
|
|
|
as the key.
|
2021-05-27 13:53:07 +01:00
|
|
|
*/
|
2020-06-24 09:36:21 +01:00
|
|
|
template <typename dispatchable_type>
|
|
|
|
|
static inline void *get_key(dispatchable_type dispatchable_object)
|
|
|
|
|
{
|
|
|
|
|
return *reinterpret_cast<void **>(dispatchable_object);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 18:30:21 +01:00
|
|
|
VkResult instance_private_data::associate(VkInstance instance, instance_dispatch_table table,
|
2021-05-27 13:53:07 +01:00
|
|
|
PFN_vkSetInstanceLoaderData set_loader_data,
|
2024-01-24 09:21:00 +01:00
|
|
|
util::wsi_platform_set enabled_layer_platforms, const uint32_t api_version,
|
2022-11-11 14:27:03 +00:00
|
|
|
const util::allocator &allocator)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
2024-04-10 18:30:21 +01:00
|
|
|
auto instance_data = allocator.make_unique<instance_private_data>(std::move(table), set_loader_data,
|
|
|
|
|
enabled_layer_platforms, api_version, allocator);
|
2021-05-27 13:53:07 +01:00
|
|
|
|
|
|
|
|
if (instance_data == nullptr)
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_ERROR("Instance private data for instance(%p) could not be allocated. Out of memory.",
|
|
|
|
|
reinterpret_cast<void *>(instance));
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto key = get_key(instance);
|
2019-05-24 15:57:50 +01:00
|
|
|
scoped_mutex lock(g_data_lock);
|
2021-05-27 13:53:07 +01:00
|
|
|
|
|
|
|
|
auto it = g_instance_data.find(key);
|
|
|
|
|
if (it != g_instance_data.end())
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Hash collision when adding new instance (%p)", reinterpret_cast<void *>(instance));
|
|
|
|
|
|
|
|
|
|
destroy(it->second);
|
|
|
|
|
g_instance_data.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 17:34:54 +01:00
|
|
|
auto result = g_instance_data.try_insert(std::make_pair(key, instance_data.get()));
|
2021-05-27 13:53:07 +01:00
|
|
|
if (result.has_value())
|
|
|
|
|
{
|
2021-10-07 14:48:44 +01:00
|
|
|
instance_data.release(); // NOLINT(bugprone-unused-return-value)
|
2021-05-27 13:53:07 +01:00
|
|
|
return VK_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Failed to insert instance_private_data for instance (%p) as host is out of memory",
|
|
|
|
|
reinterpret_cast<void *>(instance));
|
|
|
|
|
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void instance_private_data::disassociate(VkInstance instance)
|
|
|
|
|
{
|
|
|
|
|
assert(instance != VK_NULL_HANDLE);
|
|
|
|
|
instance_private_data *instance_data = nullptr;
|
|
|
|
|
{
|
|
|
|
|
scoped_mutex lock(g_data_lock);
|
|
|
|
|
auto it = g_instance_data.find(get_key(instance));
|
|
|
|
|
if (it == g_instance_data.end())
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Failed to find private data for instance (%p)", reinterpret_cast<void *>(instance));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance_data = it->second;
|
|
|
|
|
g_instance_data.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy(instance_data);
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:36:21 +01:00
|
|
|
template <typename dispatchable_type>
|
|
|
|
|
static instance_private_data &get_instance_private_data(dispatchable_type dispatchable_object)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
scoped_mutex lock(g_data_lock);
|
2021-05-27 13:53:07 +01:00
|
|
|
return *g_instance_data.at(get_key(dispatchable_object));
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 20:19:52 +01:00
|
|
|
VkResult instance_private_data::add_surface(VkSurfaceKHR vk_surface, util::unique_ptr<wsi::surface> &wsi_surface)
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
2021-07-21 20:19:52 +01:00
|
|
|
scoped_mutex lock(surfaces_lock);
|
|
|
|
|
|
|
|
|
|
auto it = surfaces.find(vk_surface);
|
|
|
|
|
if (it != surfaces.end())
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Hash collision when adding new surface (%p). Old surface is replaced.",
|
|
|
|
|
reinterpret_cast<void *>(vk_surface));
|
|
|
|
|
surfaces.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = surfaces.try_insert(std::make_pair(vk_surface, nullptr));
|
|
|
|
|
if (result.has_value())
|
|
|
|
|
{
|
|
|
|
|
assert(result->second);
|
|
|
|
|
result->first->second = wsi_surface.release();
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsi::surface *instance_private_data::get_surface(VkSurfaceKHR vk_surface)
|
|
|
|
|
{
|
|
|
|
|
scoped_mutex lock(surfaces_lock);
|
|
|
|
|
auto it = surfaces.find(vk_surface);
|
|
|
|
|
if (it != surfaces.end())
|
|
|
|
|
{
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void instance_private_data::remove_surface(VkSurfaceKHR vk_surface, const util::allocator &alloc)
|
|
|
|
|
{
|
|
|
|
|
scoped_mutex lock(surfaces_lock);
|
|
|
|
|
auto it = surfaces.find(vk_surface);
|
|
|
|
|
if (it != surfaces.end())
|
|
|
|
|
{
|
|
|
|
|
alloc.destroy<wsi::surface>(1, it->second);
|
|
|
|
|
surfaces.erase(it);
|
|
|
|
|
}
|
|
|
|
|
/* Failing to find a surface is not an error. It could have been created by a WSI extension, which is not handled
|
|
|
|
|
* by this layer.
|
|
|
|
|
*/
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool instance_private_data::does_layer_support_surface(VkSurfaceKHR surface)
|
|
|
|
|
{
|
2021-07-21 20:19:52 +01:00
|
|
|
scoped_mutex lock(surfaces_lock);
|
|
|
|
|
auto it = surfaces.find(surface);
|
|
|
|
|
return it != surfaces.end();
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
void instance_private_data::destroy(instance_private_data *instance_data)
|
|
|
|
|
{
|
|
|
|
|
assert(instance_data);
|
|
|
|
|
|
|
|
|
|
auto alloc = instance_data->get_allocator();
|
|
|
|
|
alloc.destroy<instance_private_data>(1, instance_data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:36:21 +01:00
|
|
|
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;
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 12:05:46 +00:00
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
bool instance_private_data::has_image_compression_support(VkPhysicalDevice phys_dev)
|
|
|
|
|
{
|
|
|
|
|
VkPhysicalDeviceImageCompressionControlFeaturesEXT compression = {
|
|
|
|
|
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT, nullptr, VK_FALSE
|
|
|
|
|
};
|
2025-01-15 16:05:05 +00:00
|
|
|
VkPhysicalDeviceFeatures2KHR features = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR, &compression, {} };
|
2022-03-18 12:05:46 +00:00
|
|
|
|
|
|
|
|
disp.GetPhysicalDeviceFeatures2KHR(phys_dev, &features);
|
|
|
|
|
|
|
|
|
|
return compression.imageCompressionControl != VK_FALSE;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-10-01 15:18:22 +00:00
|
|
|
bool instance_private_data::has_frame_boundary_support(VkPhysicalDevice phys_dev)
|
|
|
|
|
{
|
|
|
|
|
VkPhysicalDeviceFrameBoundaryFeaturesEXT frame_boundary = {
|
|
|
|
|
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT, nullptr, VK_FALSE
|
|
|
|
|
};
|
2025-01-15 16:05:05 +00:00
|
|
|
VkPhysicalDeviceFeatures2KHR features = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR, &frame_boundary, {} };
|
2024-10-01 15:18:22 +00:00
|
|
|
|
|
|
|
|
disp.GetPhysicalDeviceFeatures2KHR(phys_dev, &features);
|
|
|
|
|
|
|
|
|
|
return frame_boundary.frameBoundary != VK_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 13:32:12 +00:00
|
|
|
VkResult instance_private_data::set_instance_enabled_extensions(const char *const *extension_names,
|
|
|
|
|
size_t extension_count)
|
|
|
|
|
{
|
2022-03-18 12:05:46 +00:00
|
|
|
return enabled_extensions.add(extension_names, extension_count);
|
2021-11-08 13:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool instance_private_data::is_instance_extension_enabled(const char *extension_name) const
|
|
|
|
|
{
|
|
|
|
|
return enabled_extensions.contains(extension_name);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 13:02:34 +00:00
|
|
|
void instance_private_data::set_maintainance1_support(bool enabled_unsupport_ext)
|
|
|
|
|
{
|
|
|
|
|
enabled_unsupported_swapchain_maintenance1_extensions = enabled_unsupport_ext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool instance_private_data::get_maintainance1_support()
|
|
|
|
|
{
|
|
|
|
|
return enabled_unsupported_swapchain_maintenance1_extensions;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
device_private_data::device_private_data(instance_private_data &inst_data, VkPhysicalDevice phys_dev, VkDevice dev,
|
2024-04-10 18:30:21 +01:00
|
|
|
device_dispatch_table table, PFN_vkSetDeviceLoaderData set_loader_data,
|
2021-05-27 13:53:07 +01:00
|
|
|
const util::allocator &alloc)
|
2024-01-24 09:21:00 +01:00
|
|
|
: disp{ std::move(table) }
|
2021-05-27 13:53:07 +01:00
|
|
|
, instance_data{ inst_data }
|
|
|
|
|
, SetDeviceLoaderData{ set_loader_data }
|
|
|
|
|
, physical_device{ phys_dev }
|
|
|
|
|
, device{ dev }
|
|
|
|
|
, allocator{ alloc }
|
2022-11-11 14:27:03 +00:00
|
|
|
, swapchains{ allocator } /* clang-format off */
|
2021-11-08 13:32:12 +00:00
|
|
|
, enabled_extensions{ allocator }
|
2022-03-18 12:05:46 +00:00
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
, compression_control_enabled{ false }
|
|
|
|
|
#endif /* WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN */
|
2024-08-20 11:46:07 +00:00
|
|
|
, present_id_enabled { false }
|
2024-01-16 11:22:29 +00:00
|
|
|
, swapchain_maintenance1_enabled{ false }
|
2022-11-11 14:27:03 +00:00
|
|
|
/* clang-format on */
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
VkResult device_private_data::associate(VkDevice dev, instance_private_data &inst_data, VkPhysicalDevice phys_dev,
|
2024-04-10 18:30:21 +01:00
|
|
|
device_dispatch_table table, PFN_vkSetDeviceLoaderData set_loader_data,
|
2021-05-27 13:53:07 +01:00
|
|
|
const util::allocator &allocator)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
2024-04-10 18:30:21 +01:00
|
|
|
auto device_data = allocator.make_unique<device_private_data>(inst_data, phys_dev, dev, std::move(table),
|
|
|
|
|
set_loader_data, allocator);
|
2021-05-27 13:53:07 +01:00
|
|
|
|
|
|
|
|
if (device_data == nullptr)
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_ERROR("Device private data for device(%p) could not be allocated. Out of memory.",
|
|
|
|
|
reinterpret_cast<void *>(dev));
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto key = get_key(dev);
|
|
|
|
|
scoped_mutex lock(g_data_lock);
|
|
|
|
|
|
|
|
|
|
auto it = g_device_data.find(key);
|
|
|
|
|
if (it != g_device_data.end())
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Hash collision when adding new device (%p)", reinterpret_cast<void *>(dev));
|
|
|
|
|
destroy(it->second);
|
|
|
|
|
g_device_data.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 17:34:54 +01:00
|
|
|
auto result = g_device_data.try_insert(std::make_pair(key, device_data.get()));
|
2021-05-27 13:53:07 +01:00
|
|
|
if (result.has_value())
|
|
|
|
|
{
|
2021-10-07 14:48:44 +01:00
|
|
|
device_data.release(); // NOLINT(bugprone-unused-return-value)
|
2021-05-27 13:53:07 +01:00
|
|
|
return VK_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Failed to insert device_private_data for device (%p) as host is out of memory",
|
|
|
|
|
reinterpret_cast<void *>(dev));
|
|
|
|
|
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
void device_private_data::disassociate(VkDevice dev)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
2021-05-27 13:53:07 +01:00
|
|
|
assert(dev != VK_NULL_HANDLE);
|
|
|
|
|
device_private_data *device_data = nullptr;
|
|
|
|
|
{
|
|
|
|
|
scoped_mutex lock(g_data_lock);
|
|
|
|
|
auto it = g_device_data.find(get_key(dev));
|
|
|
|
|
if (it == g_device_data.end())
|
|
|
|
|
{
|
|
|
|
|
WSI_LOG_WARNING("Failed to find private data for device (%p)", reinterpret_cast<void *>(dev));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_data = it->second;
|
|
|
|
|
g_device_data.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy(device_data);
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:36:21 +01:00
|
|
|
template <typename dispatchable_type>
|
|
|
|
|
static device_private_data &get_device_private_data(dispatchable_type dispatchable_object)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
|
|
|
|
scoped_mutex lock(g_data_lock);
|
2021-05-27 13:53:07 +01:00
|
|
|
|
|
|
|
|
return *g_device_data.at(get_key(dispatchable_object));
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:36:21 +01:00
|
|
|
device_private_data &device_private_data::get(VkDevice device)
|
|
|
|
|
{
|
|
|
|
|
return get_device_private_data(device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_private_data &device_private_data::get(VkQueue queue)
|
|
|
|
|
{
|
|
|
|
|
return get_device_private_data(queue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
VkResult device_private_data::add_layer_swapchain(VkSwapchainKHR swapchain)
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
|
|
|
|
scoped_mutex lock(swapchains_lock);
|
2021-05-27 13:53:07 +01:00
|
|
|
auto result = swapchains.try_insert(swapchain);
|
|
|
|
|
return result.has_value() ? VK_SUCCESS : VK_ERROR_OUT_OF_HOST_MEMORY;
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 15:00:14 +01:00
|
|
|
void device_private_data::remove_layer_swapchain(VkSwapchainKHR swapchain)
|
|
|
|
|
{
|
|
|
|
|
scoped_mutex lock(swapchains_lock);
|
|
|
|
|
auto it = swapchains.find(swapchain);
|
|
|
|
|
if (it != swapchains.end())
|
|
|
|
|
{
|
|
|
|
|
swapchains.erase(swapchain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:36:21 +01:00
|
|
|
bool device_private_data::layer_owns_all_swapchains(const VkSwapchainKHR *swapchain, uint32_t swapchain_count) const
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
2020-06-24 09:36:21 +01:00
|
|
|
scoped_mutex lock(swapchains_lock);
|
|
|
|
|
for (uint32_t i = 0; i < swapchain_count; i++)
|
2019-05-24 15:57:50 +01:00
|
|
|
{
|
2020-06-24 09:36:21 +01:00
|
|
|
if (swapchains.find(swapchain[i]) == swapchains.end())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
2020-06-24 09:36:21 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool device_private_data::should_layer_create_swapchain(VkSurfaceKHR vk_surface)
|
|
|
|
|
{
|
|
|
|
|
return instance_data.should_layer_handle_surface(physical_device, vk_surface);
|
2019-05-24 15:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:36:21 +01:00
|
|
|
bool device_private_data::can_icds_create_swapchain(VkSurfaceKHR vk_surface)
|
|
|
|
|
{
|
2025-01-15 16:05:05 +00:00
|
|
|
UNUSED(vk_surface);
|
2024-01-24 09:21:00 +01:00
|
|
|
return disp.get_fn<PFN_vkCreateSwapchainKHR>("vkCreateSwapchainKHR").has_value();
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 13:32:12 +00:00
|
|
|
VkResult device_private_data::set_device_enabled_extensions(const char *const *extension_names, size_t extension_count)
|
|
|
|
|
{
|
2022-03-18 12:05:46 +00:00
|
|
|
return enabled_extensions.add(extension_names, extension_count);
|
2021-11-08 13:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool device_private_data::is_device_extension_enabled(const char *extension_name) const
|
|
|
|
|
{
|
|
|
|
|
return enabled_extensions.contains(extension_name);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 13:53:07 +01:00
|
|
|
void device_private_data::destroy(device_private_data *device_data)
|
2020-06-24 09:36:21 +01:00
|
|
|
{
|
2021-05-27 13:53:07 +01:00
|
|
|
assert(device_data);
|
|
|
|
|
|
|
|
|
|
auto alloc = device_data->get_allocator();
|
|
|
|
|
alloc.destroy<device_private_data>(1, device_data);
|
2020-06-24 09:36:21 +01:00
|
|
|
}
|
2021-05-27 13:53:07 +01:00
|
|
|
|
2022-03-18 12:05:46 +00:00
|
|
|
#if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN
|
|
|
|
|
void device_private_data::set_swapchain_compression_control_enabled(bool enable)
|
|
|
|
|
{
|
|
|
|
|
compression_control_enabled = enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool device_private_data::is_swapchain_compression_control_enabled() const
|
|
|
|
|
{
|
|
|
|
|
return compression_control_enabled;
|
|
|
|
|
}
|
|
|
|
|
#endif /* WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN */
|
2024-01-16 11:22:29 +00:00
|
|
|
|
2024-10-01 15:18:22 +00:00
|
|
|
void device_private_data::set_layer_frame_boundary_handling_enabled(bool enable)
|
|
|
|
|
{
|
|
|
|
|
handle_frame_boundary_events = enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool device_private_data::should_layer_handle_frame_boundary_events() const
|
|
|
|
|
{
|
|
|
|
|
return handle_frame_boundary_events;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 11:46:07 +00:00
|
|
|
void device_private_data::set_present_id_feature_enabled(bool enable)
|
|
|
|
|
{
|
|
|
|
|
present_id_enabled = enable;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 11:22:29 +00:00
|
|
|
void device_private_data::set_swapchain_maintenance1_enabled(bool enable)
|
|
|
|
|
{
|
|
|
|
|
swapchain_maintenance1_enabled = enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool device_private_data::is_swapchain_maintenance1_enabled() const
|
|
|
|
|
{
|
|
|
|
|
return swapchain_maintenance1_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 20:09:53 +00:00
|
|
|
} /* namespace layer */
|