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 <charles@lunarg.com> 26 */ 27 28 #pragma once 29 30 #include "test_util.h" 31 32 #include "layer/layer_util.h" 33 34 #include "loader/generated/vk_layer_dispatch_table.h" 35 36 /* 37 Interface Version 0 38 */ 39 40 /* 41 must export the following: -- always exported 42 vkEnumerateInstanceLayerProperties 43 vkEnumerateInstanceExtensionProperties 44 Must export the following but nothing -- always exported 45 vkEnumerateDeviceLayerProperties 46 vkEnumerateDeviceExtensionProperties 47 */ 48 49 // export test_layer_GetInstanceProcAddr(instance, pName) 50 // TEST_LAYER_EXPORT_LAYER_NAMED_GIPA 51 // or (single layer binary) 52 // export vkGetInstanceProcAddr 53 // TEST_LAYER_EXPORT_NO_NAME_GIPA 54 55 // export test_layer_GetDeviceProcAddr(device, pName) 56 // TEST_LAYER_EXPORT_LAYER_NAMED_GDPA 57 // or (single layer binary) 58 // export vkGetDeviceProcAddr 59 // TEST_LAYER_EXPORT_NO_NAME_GDPA 60 61 /* 62 Interface Version 1 63 */ 64 // export GetInstanceProcAddr 65 // TEST_LAYER_EXPORT_NO_PREFIX_GIPA 66 67 // export GetDeviceProcAddr 68 // TEST_LAYER_EXPORT_NO_PREFIX_GDPA 69 70 // Layer Manifest can override the names of the GetInstanceProcAddr and GetDeviceProcAddrfunctions 71 72 /* 73 Interface Version 2 74 */ 75 // export vk_layerGetPhysicalDeviceProcAddr 76 // TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR 77 78 // export vkNegotiateLoaderLayerInterfaceVersion 79 // TEST_LAYER_EXPORT_NEGOTIATE_LOADER_LAYER_INTERFACE_VERSION 80 81 // Added manifest version 1.1.0 82 83 struct TestLayer; 84 85 // Callbacks allow tests to implement custom functionality without modifying the layer binary 86 // TestLayer* layer - Access to the TestLayer object itself 87 // void* data - pointer to test specific thing, used to pass data from the test into the TestLayer 88 // Returns VkResult - This value will be used as the return value of the function 89 using FP_layer_callback = VkResult (*)(TestLayer& layer, void* data); 90 91 struct TestLayer { 92 fs::path manifest_file_path; 93 uint32_t manifest_version = VK_MAKE_API_VERSION(0, 1, 1, 2); 94 95 BUILDER_VALUE(TestLayer, bool, is_meta_layer, false) 96 97 BUILDER_VALUE(TestLayer, uint32_t, api_version, VK_API_VERSION_1_0) 98 BUILDER_VALUE(TestLayer, uint32_t, reported_layer_props, 1) 99 BUILDER_VALUE(TestLayer, uint32_t, reported_extension_props, 0) 100 BUILDER_VALUE(TestLayer, uint32_t, reported_instance_version, VK_API_VERSION_1_0) 101 BUILDER_VALUE(TestLayer, uint32_t, implementation_version, 2) 102 BUILDER_VALUE(TestLayer, uint32_t, min_implementation_version, 0) 103 BUILDER_VALUE(TestLayer, std::string, description, {}) 104 105 BUILDER_VECTOR(TestLayer, std::string, alternative_function_names, alternative_function_name) 106 107 BUILDER_VECTOR(TestLayer, Extension, instance_extensions, instance_extension) 108 BUILDER_VECTOR(TestLayer, Extension, device_extensions, device_extension) 109 110 BUILDER_VALUE(TestLayer, std::string, enable_environment, {}); 111 BUILDER_VALUE(TestLayer, std::string, disable_environment, {}); 112 113 BUILDER_VECTOR(TestLayer, LayerDefinition, meta_component_layers, meta_component_layer); 114 115 BUILDER_VALUE(TestLayer, bool, intercept_vkEnumerateInstanceExtensionProperties, false) 116 BUILDER_VALUE(TestLayer, bool, intercept_vkEnumerateInstanceLayerProperties, false) 117 // Called in vkCreateInstance after calling down the chain & returning 118 BUILDER_VALUE(TestLayer, std::function<VkResult(TestLayer& layer)>, create_instance_callback, {}) 119 // Called in vkCreateDevice after calling down the chain & returning 120 BUILDER_VALUE(TestLayer, std::function<VkResult(TestLayer& layer)>, create_device_callback, {}) 121 122 // Physical device modifier test flags and members. This data is primarily used to test adding, removing and 123 // re-ordering physical device data in a layer. 124 BUILDER_VALUE(TestLayer, bool, add_phys_devs, false) 125 BUILDER_VALUE(TestLayer, bool, remove_phys_devs, false) 126 BUILDER_VALUE(TestLayer, bool, reorder_phys_devs, false) 127 BUILDER_VECTOR(TestLayer, VkPhysicalDevice, complete_physical_devices, complete_physical_device) 128 BUILDER_VECTOR(TestLayer, VkPhysicalDevice, removed_physical_devices, removed_physical_device) 129 BUILDER_VECTOR(TestLayer, VkPhysicalDevice, added_physical_devices, added_physical_device) 130 BUILDER_VECTOR(TestLayer, VkPhysicalDeviceGroupProperties, complete_physical_device_groups, complete_physical_device_group) 131 BUILDER_VECTOR(TestLayer, VkPhysicalDeviceGroupProperties, removed_physical_device_groups, removed_physical_device_group) 132 BUILDER_VECTOR(TestLayer, VkPhysicalDeviceGroupProperties, added_physical_device_groups, added_physical_device_group) 133 134 BUILDER_VECTOR(TestLayer, VulkanFunction, custom_physical_device_implementation_functions, 135 custom_physical_device_implementation_function) 136 BUILDER_VECTOR(TestLayer, VulkanFunction, custom_device_implementation_functions, custom_device_implementation_function) 137 138 // Only need a single map for all 'custom' function - assumes that all function names are distinct, IE there cannot be a 139 // physical device and device level function with the same name 140 std::unordered_map<std::string, PFN_vkVoidFunction> custom_dispatch_functions; 141 std::vector<VulkanFunction> custom_physical_device_interception_functions; add_custom_physical_device_intercept_functionTestLayer142 TestLayer& add_custom_physical_device_intercept_function(std::string func_name, PFN_vkVoidFunction function) { 143 custom_physical_device_interception_functions.push_back({func_name, function}); 144 custom_dispatch_functions[func_name] = nullptr; 145 return *this; 146 } 147 std::vector<VulkanFunction> custom_device_interception_functions; add_custom_device_interception_functionTestLayer148 TestLayer& add_custom_device_interception_function(std::string func_name, PFN_vkVoidFunction function) { 149 custom_device_interception_functions.push_back({func_name, function}); 150 custom_dispatch_functions[func_name] = nullptr; 151 return *this; 152 } get_custom_intercept_functionTestLayer153 PFN_vkVoidFunction get_custom_intercept_function(const char* name) { 154 if (custom_dispatch_functions.count(name) > 0) { 155 return custom_dispatch_functions.at(name); 156 } 157 return nullptr; 158 } 159 160 BUILDER_VALUE(TestLayer, bool, do_spurious_allocations_in_create_instance, false) 161 void* spurious_instance_memory_allocation = nullptr; 162 BUILDER_VALUE(TestLayer, bool, do_spurious_allocations_in_create_device, false) 163 struct DeviceMemAlloc { 164 void* allocation; 165 VkDevice device; 166 }; 167 std::vector<DeviceMemAlloc> spurious_device_memory_allocations; 168 169 // By default query GPDPA from GIPA, don't use value given from pNext 170 BUILDER_VALUE(TestLayer, bool, use_gipa_GetPhysicalDeviceProcAddr, true) 171 172 PFN_vkGetInstanceProcAddr next_vkGetInstanceProcAddr = VK_NULL_HANDLE; 173 PFN_GetPhysicalDeviceProcAddr next_GetPhysicalDeviceProcAddr = VK_NULL_HANDLE; 174 PFN_vkGetDeviceProcAddr next_vkGetDeviceProcAddr = VK_NULL_HANDLE; 175 176 VkInstance instance_handle = VK_NULL_HANDLE; 177 VkLayerInstanceDispatchTable instance_dispatch_table{}; 178 179 struct Device { 180 VkDevice device_handle = VK_NULL_HANDLE; 181 VkLayerDispatchTable dispatch_table{}; 182 }; 183 std::vector<Device> created_devices; 184 }; 185 186 using GetTestLayerFunc = TestLayer* (*)(); 187 #define GET_TEST_LAYER_FUNC_STR "get_test_layer_func" 188 189 using GetNewTestLayerFunc = TestLayer* (*)(); 190 #define RESET_LAYER_FUNC_STR "reset_layer_func" 191