| 1 |
/* |
| 2 |
* Copyright (c) 2021-2022 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2021-2022 Valve Corporation |
| 4 |
* Copyright (c) 2021-2022 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
* Author: Charles Giessen <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#pragma once |
| 29 |
|
| 30 |
#include <functional> |
| 31 |
#include <filesystem> |
| 32 |
|
| 33 |
#include <vulkan/vulkan_core.h> |
| 34 |
#include <vulkan/vk_layer.h> |
| 35 |
|
| 36 |
#include "loader/generated/vk_layer_dispatch_table.h" |
| 37 |
|
| 38 |
#include "util/functions.h" |
| 39 |
|
| 40 |
#include "layer_util.h" |
| 41 |
|
| 42 |
/* |
| 43 |
Interface Version 0 |
| 44 |
*/ |
| 45 |
|
| 46 |
/* |
| 47 |
must export the following: -- always exported |
| 48 |
vkEnumerateInstanceLayerProperties |
| 49 |
vkEnumerateInstanceExtensionProperties |
| 50 |
Must export the following but nothing -- always exported |
| 51 |
vkEnumerateDeviceLayerProperties |
| 52 |
vkEnumerateDeviceExtensionProperties |
| 53 |
*/ |
| 54 |
|
| 55 |
// export test_layer_GetInstanceProcAddr(instance, pName) |
| 56 |
// TEST_LAYER_EXPORT_LAYER_NAMED_GIPA |
| 57 |
// or (single layer binary) |
| 58 |
// export vkGetInstanceProcAddr |
| 59 |
// TEST_LAYER_EXPORT_NO_NAME_GIPA |
| 60 |
|
| 61 |
// export test_layer_GetDeviceProcAddr(device, pName) |
| 62 |
// TEST_LAYER_EXPORT_LAYER_NAMED_GDPA |
| 63 |
// or (single layer binary) |
| 64 |
// export vkGetDeviceProcAddr |
| 65 |
// TEST_LAYER_EXPORT_NO_NAME_GDPA |
| 66 |
|
| 67 |
/* |
| 68 |
Interface Version 1 |
| 69 |
*/ |
| 70 |
// export GetInstanceProcAddr |
| 71 |
// TEST_LAYER_EXPORT_NO_PREFIX_GIPA |
| 72 |
|
| 73 |
// export GetDeviceProcAddr |
| 74 |
// TEST_LAYER_EXPORT_NO_PREFIX_GDPA |
| 75 |
|
| 76 |
// Layer Manifest can override the names of the GetInstanceProcAddr and GetDeviceProcAddrfunctions |
| 77 |
|
| 78 |
/* |
| 79 |
Interface Version 2 |
| 80 |
*/ |
| 81 |
// export vk_layerGetPhysicalDeviceProcAddr |
| 82 |
// TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR |
| 83 |
|
| 84 |
// export vkNegotiateLoaderLayerInterfaceVersion |
| 85 |
// TEST_LAYER_EXPORT_NEGOTIATE_LOADER_LAYER_INTERFACE_VERSION |
| 86 |
|
| 87 |
// Added manifest version 1.1.0 |
| 88 |
|
| 89 |
struct TestLayer; |
| 90 |
|
| 91 |
// Callbacks allow tests to implement custom functionality without modifying the layer binary |
| 92 |
// TestLayer* layer - Access to the TestLayer object itself |
| 93 |
// void* data - pointer to test specific thing, used to pass data from the test into the TestLayer |
| 94 |
// Returns VkResult - This value will be used as the return value of the function |
| 95 |
using FP_layer_callback = VkResult (*)(TestLayer& layer, void* data); |
| 96 |
|
| 97 |
struct TestLayer { |
| 98 |
std::filesystem::path manifest_file_path; |
| 99 |
uint32_t manifest_version = VK_MAKE_API_VERSION(0, 1, 1, 2); |
| 100 |
|
| 101 |
BUILDER_VALUE(bool, is_meta_layer) |
| 102 |
|
| 103 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, api_version, VK_API_VERSION_1_0) |
| 104 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, reported_layer_props, 1) |
| 105 |
BUILDER_VALUE(uint32_t, reported_extension_props) |
| 106 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, reported_instance_version, VK_API_VERSION_1_0) |
| 107 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, implementation_version, 2) |
| 108 |
BUILDER_VALUE(uint32_t, min_implementation_version) |
| 109 |
BUILDER_VALUE(std::string, description) |
| 110 |
|
| 111 |
// Some layers may try to change the API version during instance creation - we should allow testing of such behavior |
| 112 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, alter_api_version, VK_API_VERSION_1_0) |
| 113 |
|
| 114 |
BUILDER_VECTOR(std::string, alternative_function_names, alternative_function_name) |
| 115 |
|
| 116 |
BUILDER_VECTOR(Extension, instance_extensions, instance_extension) |
| 117 |
std::vector<Extension> enabled_instance_extensions; |
| 118 |
|
| 119 |
BUILDER_VECTOR(Extension, device_extensions, device_extension) |
| 120 |
|
| 121 |
BUILDER_VALUE(std::string, enable_environment); |
| 122 |
BUILDER_VALUE(std::string, disable_environment); |
| 123 |
|
| 124 |
// Modifies the extension list returned by vkEnumerateInstanceExtensionProperties to include what is in this vector |
| 125 |
BUILDER_VECTOR(Extension, injected_instance_extensions, injected_instance_extension) |
| 126 |
// Modifies the extension list returned by vkEnumerateDeviceExtensionProperties to include what is in this vector |
| 127 |
BUILDER_VECTOR(Extension, injected_device_extensions, injected_device_extension) |
| 128 |
|
| 129 |
BUILDER_VECTOR(LayerDefinition, meta_component_layers, meta_component_layer); |
| 130 |
|
| 131 |
BUILDER_VALUE(bool, intercept_vkEnumerateInstanceExtensionProperties) |
| 132 |
BUILDER_VALUE(bool, intercept_vkEnumerateInstanceLayerProperties) |
| 133 |
// Called in vkCreateInstance after calling down the chain & returning |
| 134 |
BUILDER_VALUE(std::function<VkResult(TestLayer& layer)>, create_instance_callback) |
| 135 |
// Called in vkCreateDevice after calling down the chain & returning |
| 136 |
BUILDER_VALUE(std::function<VkResult(TestLayer& layer)>, create_device_callback) |
| 137 |
|
| 138 |
// Physical device modifier test flags and members. This data is primarily used to test adding, removing and |
| 139 |
// re-ordering physical device data in a layer. |
| 140 |
BUILDER_VALUE(bool, add_phys_devs) |
| 141 |
BUILDER_VALUE(bool, remove_phys_devs) |
| 142 |
BUILDER_VALUE(bool, reorder_phys_devs) |
| 143 |
BUILDER_VECTOR(VkPhysicalDevice, complete_physical_devices, complete_physical_device) |
| 144 |
BUILDER_VECTOR(VkPhysicalDevice, removed_physical_devices, removed_physical_device) |
| 145 |
BUILDER_VECTOR(VkPhysicalDevice, added_physical_devices, added_physical_device) |
| 146 |
BUILDER_VECTOR(VkPhysicalDeviceGroupProperties, complete_physical_device_groups, complete_physical_device_group) |
| 147 |
BUILDER_VECTOR(VkPhysicalDeviceGroupProperties, removed_physical_device_groups, removed_physical_device_group) |
| 148 |
BUILDER_VECTOR(VkPhysicalDeviceGroupProperties, added_physical_device_groups, added_physical_device_group) |
| 149 |
|
| 150 |
BUILDER_VECTOR(VulkanFunction, custom_physical_device_implementation_functions, custom_physical_device_implementation_function) |
| 151 |
BUILDER_VECTOR(VulkanFunction, custom_device_implementation_functions, custom_device_implementation_function) |
| 152 |
|
| 153 |
// Only need a single map for all 'custom' function - assumes that all function names are distinct, IE there cannot be a |
| 154 |
// physical device and device level function with the same name |
| 155 |
std::unordered_map<std::string, PFN_vkVoidFunction> custom_dispatch_functions; |
| 156 |
std::vector<VulkanFunction> custom_physical_device_interception_functions; |
| 157 |
TestLayer& add_custom_physical_device_intercept_function(std::string func_name, PFN_vkVoidFunction function) { |
| 158 |
custom_physical_device_interception_functions.push_back({func_name, function}); |
| 159 |
custom_dispatch_functions[func_name] = nullptr; |
| 160 |
return *this; |
| 161 |
} |
| 162 |
std::vector<VulkanFunction> custom_device_interception_functions; |
| 163 |
TestLayer& add_custom_device_interception_function(std::string func_name, PFN_vkVoidFunction function) { |
| 164 |
custom_device_interception_functions.push_back({func_name, function}); |
| 165 |
custom_dispatch_functions[func_name] = nullptr; |
| 166 |
return *this; |
| 167 |
} |
| 168 |
PFN_vkVoidFunction get_custom_intercept_function(const char* name) { |
| 169 |
if (custom_dispatch_functions.count(name) > 0) { |
| 170 |
return custom_dispatch_functions.at(name); |
| 171 |
} |
| 172 |
return nullptr; |
| 173 |
} |
| 174 |
|
| 175 |
// Allows distinguishing different layers (that use the same binary) |
| 176 |
BUILDER_VALUE(std::string, make_spurious_log_in_create_instance) |
| 177 |
BUILDER_VALUE(bool, do_spurious_allocations_in_create_instance) |
| 178 |
void* spurious_instance_memory_allocation = nullptr; |
| 179 |
BUILDER_VALUE(bool, do_spurious_allocations_in_create_device) |
| 180 |
struct DeviceMemAlloc { |
| 181 |
void* allocation; |
| 182 |
VkDevice device; |
| 183 |
}; |
| 184 |
std::vector<DeviceMemAlloc> spurious_device_memory_allocations; |
| 185 |
|
| 186 |
// By default query GPDPA from GIPA, don't use value given from pNext |
| 187 |
BUILDER_VALUE_WITH_DEFAULT(bool, use_gipa_GetPhysicalDeviceProcAddr, true) |
| 188 |
|
| 189 |
// Have a layer query for vkCreateDevice with a NULL instance handle |
| 190 |
BUILDER_VALUE(bool, buggy_query_of_vkCreateDevice) |
| 191 |
|
| 192 |
// Makes the layer try to create a device using the loader provided function in the layer chain |
| 193 |
BUILDER_VALUE(bool, call_create_device_while_create_device_is_called) |
| 194 |
BUILDER_VALUE(uint32_t, physical_device_index_to_use_during_create_device) |
| 195 |
|
| 196 |
BUILDER_VALUE(bool, check_if_EnumDevExtProps_is_same_as_queried_function) |
| 197 |
|
| 198 |
// Clober the data pointed to by pInstance to overwrite the magic value |
| 199 |
BUILDER_VALUE(bool, clobber_pInstance) |
| 200 |
// Clober the data pointed to by pDevice to overwrite the magic value |
| 201 |
BUILDER_VALUE(bool, clobber_pDevice) |
| 202 |
|
| 203 |
BUILDER_VALUE(bool, query_vkEnumerateInstanceLayerProperties) |
| 204 |
BUILDER_VALUE(bool, query_vkEnumerateInstanceExtensionProperties) |
| 205 |
BUILDER_VALUE(bool, query_vkEnumerateInstanceVersion) |
| 206 |
|
| 207 |
PFN_vkGetInstanceProcAddr next_vkGetInstanceProcAddr = VK_NULL_HANDLE; |
| 208 |
PFN_GetPhysicalDeviceProcAddr next_GetPhysicalDeviceProcAddr = VK_NULL_HANDLE; |
| 209 |
PFN_vkGetDeviceProcAddr next_vkGetDeviceProcAddr = VK_NULL_HANDLE; |
| 210 |
|
| 211 |
VkInstance instance_handle = VK_NULL_HANDLE; |
| 212 |
VkLayerInstanceDispatchTable instance_dispatch_table{}; |
| 213 |
|
| 214 |
struct Device { |
| 215 |
VkDevice device_handle = VK_NULL_HANDLE; |
| 216 |
VkLayerDispatchTable dispatch_table{}; |
| 217 |
std::vector<Extension> enabled_extensions; |
| 218 |
}; |
| 219 |
std::vector<Device> created_devices; |
| 220 |
|
| 221 |
// Stores the callback that allows layers to create devices on their own |
| 222 |
PFN_vkLayerCreateDevice callback_vkCreateDevice{}; |
| 223 |
PFN_vkLayerDestroyDevice callback_vkDestroyDevice{}; |
| 224 |
std::vector<VkPhysicalDevice> queried_physical_devices; |
| 225 |
Device second_device_created_during_create_device{}; |
| 226 |
}; |
| 227 |
|
| 228 |
using GetTestLayerFunc = TestLayer* (*)(); |
| 229 |
#define GET_TEST_LAYER_FUNC_STR "get_test_layer_func" |
| 230 |
|
| 231 |
using GetNewTestLayerFunc = TestLayer* (*)(); |
| 232 |
#define RESET_LAYER_FUNC_STR "reset_layer_func" |