Debian Codegraph @

tests/framework/layer/test_layer.cpp

raw
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 #include "test_layer.h"
29
30 #include <assert.h>
31
32 #include <vulkan/vk_layer.h>
33
34 #include "util/test_defines.h"
35 #include "util/equality_helpers.h"
36
37 #include "generated/vk_dispatch_table_helper.h"
38
39 // export the enumeration functions instance|device+layer|extension
40 #if !defined(TEST_LAYER_EXPORT_ENUMERATE_FUNCTIONS)
41 #define TEST_LAYER_EXPORT_ENUMERATE_FUNCTIONS 0
42 #endif
43
44 // export test_layer_GetInstanceProcAddr
45 #if !defined(TEST_LAYER_EXPORT_LAYER_NAMED_GIPA)
46 #define TEST_LAYER_EXPORT_LAYER_NAMED_GIPA 0
47 #endif
48
49 // export test_override_GetInstanceProcAddr
50 #if !defined(TEST_LAYER_EXPORT_OVERRIDE_GIPA)
51 #define TEST_LAYER_EXPORT_OVERRIDE_GIPA 0
52 #endif
53
54 // export vkGetInstanceProcAddr
55 #if !defined(TEST_LAYER_EXPORT_LAYER_VK_GIPA)
56 #define TEST_LAYER_EXPORT_LAYER_VK_GIPA 0
57 #endif
58
59 // export test_layer_GetDeviceProcAddr
60 #if !defined(TEST_LAYER_EXPORT_LAYER_NAMED_GDPA)
61 #define TEST_LAYER_EXPORT_LAYER_NAMED_GDPA 0
62 #endif
63
64 // export test_override_GetDeviceProcAddr
65 #if !defined(TEST_LAYER_EXPORT_OVERRIDE_GDPA)
66 #define TEST_LAYER_EXPORT_OVERRIDE_GDPA 0
67 #endif
68
69 // export vkGetDeviceProcAddr
70 #if !defined(TEST_LAYER_EXPORT_LAYER_VK_GDPA)
71 #define TEST_LAYER_EXPORT_LAYER_VK_GDPA 0
72 #endif
73
74 // export vk_layerGetPhysicalDeviceProcAddr
75 #if !defined(TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR)
76 #define TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR 0
77 #endif
78
79 // export vkNegotiateLoaderLayerInterfaceVersion
80 #if !defined(LAYER_EXPORT_NEGOTIATE_LOADER_LAYER_INTERFACE_VERSION)
81 #define LAYER_EXPORT_NEGOTIATE_LOADER_LAYER_INTERFACE_VERSION 0
82 #endif
83
84 #if !defined(TEST_LAYER_NAME)
85 #define TEST_LAYER_NAME "VK_LAYER_LunarG_test_layer"
86 #endif
87
88 TestLayer layer;
89 extern "C" {
90 FRAMEWORK_EXPORT TestLayer* get_test_layer_func() { return &layer; }
91 FRAMEWORK_EXPORT TestLayer* reset_layer_func() {
92 layer.~TestLayer();
93 return new (&layer) TestLayer();
94 }
95 }
96
97 bool IsInstanceExtensionSupported(const char* extension_name) {
98 return layer.instance_extensions.end() !=
99 std::find_if(layer.instance_extensions.begin(), layer.instance_extensions.end(),
100 [extension_name](Extension const& ext) { return string_eq(&ext.extensionName[0], extension_name); });
101 }
102
103 bool IsInstanceExtensionEnabled(const char* extension_name) {
104 return layer.enabled_instance_extensions.end() !=
105 std::find_if(layer.enabled_instance_extensions.begin(), layer.enabled_instance_extensions.end(),
106 [extension_name](Extension const& ext) { return string_eq(&ext.extensionName[0], extension_name); });
107 }
108
109 bool IsDeviceExtensionAvailable(VkDevice dev, const char* extension_name) {
110 for (auto& device : layer.created_devices) {
111 if ((dev == VK_NULL_HANDLE || device.device_handle == dev) &&
112 device.enabled_extensions.end() !=
113 std::find_if(device.enabled_extensions.begin(), device.enabled_extensions.end(),
114 [extension_name](Extension const& ext) { return ext.extensionName == extension_name; })) {
115 return true;
116 }
117 }
118 return false;
119 }
120
121 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_instance_func(VkInstance instance, const char* pName);
122
123 VkLayerInstanceCreateInfo* get_chain_info(const VkInstanceCreateInfo* pCreateInfo, VkLayerFunction func) {
124 VkLayerInstanceCreateInfo* chain_info = (VkLayerInstanceCreateInfo*)pCreateInfo->pNext;
125 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == func)) {
126 chain_info = (VkLayerInstanceCreateInfo*)chain_info->pNext;
127 }
128 assert(chain_info != NULL);
129 return chain_info;
130 }
131
132 VkLayerDeviceCreateInfo* get_chain_info(const VkDeviceCreateInfo* pCreateInfo, VkLayerFunction func) {
133 VkLayerDeviceCreateInfo* chain_info = (VkLayerDeviceCreateInfo*)pCreateInfo->pNext;
134 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain_info->function == func)) {
135 chain_info = (VkLayerDeviceCreateInfo*)chain_info->pNext;
136 }
137 assert(chain_info != NULL);
138 return chain_info;
139 }
140
141 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumerateInstanceLayerProperties(uint32_t*, VkLayerProperties*) { return VK_SUCCESS; }
142
143 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount,
144 VkExtensionProperties* pProperties) {
145 if (pPropertyCount == nullptr) {
146 return VK_INCOMPLETE;
147 }
148 if (pLayerName && string_eq(pLayerName, TEST_LAYER_NAME)) {
149 if (pProperties) {
150 if (*pPropertyCount < layer.injected_instance_extensions.size()) {
151 return VK_INCOMPLETE;
152 }
153 for (size_t i = 0; i < layer.injected_instance_extensions.size(); i++) {
154 pProperties[i] = layer.injected_instance_extensions.at(i).get();
155 }
156 *pPropertyCount = static_cast<uint32_t>(layer.injected_instance_extensions.size());
157 } else {
158 *pPropertyCount = static_cast<uint32_t>(layer.injected_instance_extensions.size());
159 }
160 return VK_SUCCESS;
161 }
162
163 uint32_t hardware_prop_count = 0;
164 if (pProperties) {
165 hardware_prop_count = *pPropertyCount - static_cast<uint32_t>(layer.injected_instance_extensions.size());
166 }
167
168 VkResult res =
169 layer.instance_dispatch_table.EnumerateInstanceExtensionProperties(pLayerName, &hardware_prop_count, pProperties);
170 if (res < 0) {
171 return res;
172 }
173
174 if (pProperties == nullptr) {
175 *pPropertyCount = hardware_prop_count + static_cast<uint32_t>(layer.injected_instance_extensions.size());
176 } else {
177 if (hardware_prop_count + layer.injected_instance_extensions.size() > *pPropertyCount) {
178 *pPropertyCount = hardware_prop_count;
179 return VK_INCOMPLETE;
180 }
181 for (size_t i = 0; i < layer.injected_instance_extensions.size(); i++) {
182 pProperties[hardware_prop_count + i] = layer.injected_instance_extensions.at(i).get();
183 }
184 *pPropertyCount = hardware_prop_count + static_cast<uint32_t>(layer.injected_instance_extensions.size());
185 }
186 return res;
187 }
188
189 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumerateDeviceLayerProperties(VkPhysicalDevice, uint32_t*, VkLayerProperties*) {
190 return VK_SUCCESS;
191 }
192
193 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName,
194 uint32_t* pPropertyCount,
195 VkExtensionProperties* pProperties) {
196 if (pPropertyCount == nullptr) {
197 return VK_INCOMPLETE;
198 }
199
200 if (pLayerName && string_eq(pLayerName, TEST_LAYER_NAME)) {
201 if (pProperties) {
202 if (*pPropertyCount < static_cast<uint32_t>(layer.injected_device_extensions.size())) {
203 return VK_INCOMPLETE;
204 }
205 for (size_t i = 0; i < layer.injected_device_extensions.size(); i++) {
206 pProperties[i] = layer.injected_device_extensions.at(i).get();
207 }
208 *pPropertyCount = static_cast<uint32_t>(layer.injected_device_extensions.size());
209 } else {
210 *pPropertyCount = static_cast<uint32_t>(layer.injected_device_extensions.size());
211 }
212 return VK_SUCCESS;
213 }
214
215 uint32_t hardware_prop_count = 0;
216 if (pProperties) {
217 hardware_prop_count = *pPropertyCount - static_cast<uint32_t>(layer.injected_device_extensions.size());
218 }
219
220 VkResult res = layer.instance_dispatch_table.EnumerateDeviceExtensionProperties(physicalDevice, pLayerName,
221 &hardware_prop_count, pProperties);
222 if (res < 0) {
223 return res;
224 }
225
226 if (pProperties == nullptr) {
227 *pPropertyCount = hardware_prop_count + static_cast<uint32_t>(layer.injected_device_extensions.size());
228 } else {
229 if (hardware_prop_count + layer.injected_device_extensions.size() > *pPropertyCount) {
230 *pPropertyCount = hardware_prop_count;
231 return VK_INCOMPLETE;
232 }
233 for (size_t i = 0; i < layer.injected_device_extensions.size(); i++) {
234 pProperties[hardware_prop_count + i] = layer.injected_device_extensions.at(i).get();
235 }
236 *pPropertyCount = hardware_prop_count + static_cast<uint32_t>(layer.injected_device_extensions.size());
237 }
238 return res;
239 }
240
241 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumerateInstanceVersion(uint32_t* pApiVersion) {
242 if (pApiVersion != nullptr) {
243 *pApiVersion = VK_API_VERSION_1_0;
244 }
245 return VK_SUCCESS;
246 }
247
248 VKAPI_ATTR VkResult VKAPI_CALL test_vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo,
249 const VkAllocationCallbacks* pAllocator, VkInstance* pInstance) {
250 VkLayerInstanceCreateInfo* chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
251
252 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
253 PFN_GetPhysicalDeviceProcAddr fpGetPhysicalDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetPhysicalDeviceProcAddr;
254 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
255 if (fpCreateInstance == NULL) {
256 return VK_ERROR_INITIALIZATION_FAILED;
257 }
258
259 if (layer.call_create_device_while_create_device_is_called) {
260 auto* createDeviceCallback = get_chain_info(pCreateInfo, VK_LOADER_LAYER_CREATE_DEVICE_CALLBACK);
261 layer.callback_vkCreateDevice = createDeviceCallback->u.layerDevice.pfnLayerCreateDevice;
262 layer.callback_vkDestroyDevice = createDeviceCallback->u.layerDevice.pfnLayerDestroyDevice;
263 }
264
265 // Advance the link info for the next element of the chain
266 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
267 layer.next_vkGetInstanceProcAddr = fpGetInstanceProcAddr;
268
269 bool use_modified_create_info = false;
270 VkInstanceCreateInfo instance_create_info{};
271 VkApplicationInfo application_info{};
272 if (pCreateInfo) {
273 instance_create_info = *pCreateInfo;
274 if (pCreateInfo->pApplicationInfo) {
275 application_info = *pCreateInfo->pApplicationInfo;
276 }
277 }
278
279 // If the test needs to modify the api version, do it before we call down the chain
280 if (layer.alter_api_version != VK_API_VERSION_1_0 && pCreateInfo && pCreateInfo->pApplicationInfo) {
281 application_info.apiVersion = layer.alter_api_version;
282 instance_create_info.pApplicationInfo = &application_info;
283 use_modified_create_info = true;
284 }
285 const VkInstanceCreateInfo* create_info_pointer = use_modified_create_info ? &instance_create_info : pCreateInfo;
286
287 if (layer.clobber_pInstance) {
288 memset(*pInstance, 0, 128);
289 }
290 PFN_vkEnumerateInstanceLayerProperties fpEnumerateInstanceLayerProperties{};
291 PFN_vkEnumerateInstanceExtensionProperties fpEnumerateInstanceExtensionProperties{};
292 PFN_vkEnumerateInstanceVersion fpEnumerateInstanceVersion{};
293
294 if (layer.query_vkEnumerateInstanceLayerProperties) {
295 fpEnumerateInstanceLayerProperties = reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
296 fpGetInstanceProcAddr(nullptr, "vkEnumerateInstanceLayerProperties"));
297 uint32_t count = 0;
298 fpEnumerateInstanceLayerProperties(&count, nullptr);
299 if (count == 0) return VK_ERROR_LAYER_NOT_PRESENT;
300 std::vector<VkLayerProperties> layers{count, VkLayerProperties{}};
301 fpEnumerateInstanceLayerProperties(&count, layers.data());
302 if (count == 0) return VK_ERROR_LAYER_NOT_PRESENT;
303 }
304 if (layer.query_vkEnumerateInstanceExtensionProperties) {
305 fpEnumerateInstanceExtensionProperties = reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
306 fpGetInstanceProcAddr(nullptr, "vkEnumerateInstanceExtensionProperties"));
307 uint32_t count = 0;
308 fpEnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
309 if (count == 0) return VK_ERROR_LAYER_NOT_PRESENT;
310 std::vector<VkExtensionProperties> extensions{count, VkExtensionProperties{}};
311 fpEnumerateInstanceExtensionProperties(nullptr, &count, extensions.data());
312 if (count == 0) return VK_ERROR_LAYER_NOT_PRESENT;
313 }
314 if (layer.query_vkEnumerateInstanceVersion) {
315 fpEnumerateInstanceVersion =
316 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(fpGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
317 uint32_t version = 0;
318 fpEnumerateInstanceVersion(&version);
319 if (version == 0) return VK_ERROR_LAYER_NOT_PRESENT;
320 }
321
322 // Continue call down the chain
323 VkResult result = fpCreateInstance(create_info_pointer, pAllocator, pInstance);
324 if (result != VK_SUCCESS) {
325 return result;
326 }
327 layer.instance_handle = *pInstance;
328 if (layer.use_gipa_GetPhysicalDeviceProcAddr) {
329 layer.next_GetPhysicalDeviceProcAddr =
330 reinterpret_cast<PFN_GetPhysicalDeviceProcAddr>(fpGetInstanceProcAddr(*pInstance, "vk_layerGetPhysicalDeviceProcAddr"));
331 } else {
332 layer.next_GetPhysicalDeviceProcAddr = fpGetPhysicalDeviceProcAddr;
333 }
334 // Init layer's dispatch table using GetInstanceProcAddr of
335 // next layer in the chain.
336 layer_init_instance_dispatch_table(layer.instance_handle, &layer.instance_dispatch_table, fpGetInstanceProcAddr);
337
338 layer.enabled_instance_extensions.clear();
339 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
340 layer.enabled_instance_extensions.push_back({pCreateInfo->ppEnabledExtensionNames[i]});
341 }
342
343 if (layer.create_instance_callback) result = layer.create_instance_callback(layer);
344
345 for (auto& func : layer.custom_physical_device_interception_functions) {
346 auto next_func = layer.next_GetPhysicalDeviceProcAddr(*pInstance, func.name.c_str());
347 layer.custom_dispatch_functions.at(func.name.c_str()) = next_func;
348 }
349
350 for (auto& func : layer.custom_device_interception_functions) {
351 auto next_func = layer.next_vkGetInstanceProcAddr(*pInstance, func.name.c_str());
352 layer.custom_dispatch_functions.at(func.name.c_str()) = next_func;
353 }
354
355 if (layer.do_spurious_allocations_in_create_instance && pAllocator && pAllocator->pfnAllocation) {
356 layer.spurious_instance_memory_allocation =
357 pAllocator->pfnAllocation(pAllocator->pUserData, 100, 8, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
358 if (layer.spurious_instance_memory_allocation == nullptr) {
359 return VK_ERROR_OUT_OF_HOST_MEMORY;
360 }
361 }
362
363 if (!layer.make_spurious_log_in_create_instance.empty()) {
364 auto* chain = reinterpret_cast<const VkBaseInStructure*>(pCreateInfo->pNext);
365 while (chain) {
366 if (chain->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
367 auto* debug_messenger = reinterpret_cast<const VkDebugUtilsMessengerCreateInfoEXT*>(chain);
368 VkDebugUtilsMessengerCallbackDataEXT data{};
369 data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
370 data.pMessage = layer.make_spurious_log_in_create_instance.c_str();
371 debug_messenger->pfnUserCallback(VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT,
372 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &data, debug_messenger->pUserData);
373 }
374
375 chain = chain->pNext;
376 }
377 }
378
379 if (layer.buggy_query_of_vkCreateDevice) {
380 layer.instance_dispatch_table.CreateDevice =
381 reinterpret_cast<PFN_vkCreateDevice>(fpGetInstanceProcAddr(nullptr, "vkCreateDevice"));
382 }
383
384 if (layer.call_create_device_while_create_device_is_called) {
385 uint32_t phys_dev_count = 0;
386 result = layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count, nullptr);
387 if (result != VK_SUCCESS) {
388 return result;
389 }
390 layer.queried_physical_devices.resize(phys_dev_count);
391 result = layer.instance_dispatch_table.EnumeratePhysicalDevices(layer.instance_handle, &phys_dev_count,
392 layer.queried_physical_devices.data());
393 if (result != VK_SUCCESS) {
394 return result;
395 }
396 }
397
398 if (layer.check_if_EnumDevExtProps_is_same_as_queried_function) {
399 auto chain_info_EnumDeviceExtProps = reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
400 fpGetInstanceProcAddr(layer.instance_handle, "vkEnumerateDeviceExtensionProperties"));
401 if (chain_info_EnumDeviceExtProps != layer.instance_dispatch_table.EnumerateDeviceExtensionProperties) {
402 return VK_ERROR_INITIALIZATION_FAILED;
403 }
404 }
405
406 return result;
407 }
408
409 VKAPI_ATTR VkResult VKAPI_CALL test_override_vkCreateInstance(const VkInstanceCreateInfo*, const VkAllocationCallbacks*,
410 VkInstance*) {
411 return VK_ERROR_INVALID_SHADER_NV;
412 }
413
414 VKAPI_ATTR void VKAPI_CALL test_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator) {
415 if (layer.spurious_instance_memory_allocation && pAllocator && pAllocator->pfnFree) {
416 pAllocator->pfnFree(pAllocator->pUserData, layer.spurious_instance_memory_allocation);
417 layer.spurious_instance_memory_allocation = nullptr;
418 }
419 layer.enabled_instance_extensions.clear();
420
421 layer.instance_dispatch_table.DestroyInstance(instance, pAllocator);
422 }
423
424 VKAPI_ATTR VkResult VKAPI_CALL test_vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
425 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) {
426 VkLayerDeviceCreateInfo* chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
427
428 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
429 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
430 VkInstance instance_to_use = layer.buggy_query_of_vkCreateDevice ? NULL : layer.instance_handle;
431 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(instance_to_use, "vkCreateDevice");
432 if (fpCreateDevice == NULL) {
433 return VK_ERROR_INITIALIZATION_FAILED;
434 }
435
436 layer.next_vkGetDeviceProcAddr = fpGetDeviceProcAddr;
437
438 if (layer.check_if_EnumDevExtProps_is_same_as_queried_function) {
439 auto chain_info_EnumDeviceExtProps = reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
440 fpGetInstanceProcAddr(layer.instance_handle, "vkEnumerateDeviceExtensionProperties"));
441 if (chain_info_EnumDeviceExtProps != layer.instance_dispatch_table.EnumerateDeviceExtensionProperties) {
442 return VK_ERROR_INITIALIZATION_FAILED;
443 }
444 }
445 // Advance the link info for the next element on the chain
446 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
447
448 if (layer.clobber_pDevice) {
449 memset(*pDevice, 0, 128);
450 }
451
452 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
453 if (result != VK_SUCCESS) {
454 return result;
455 }
456 TestLayer::Device device{};
457 device.device_handle = *pDevice;
458
459 // initialize layer's dispatch table
460 layer_init_device_dispatch_table(device.device_handle, &device.dispatch_table, fpGetDeviceProcAddr);
461
462 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
463 device.enabled_extensions.push_back({pCreateInfo->ppEnabledExtensionNames[i]});
464 }
465
466 for (auto& func : layer.custom_device_interception_functions) {
467 auto next_func = layer.next_vkGetDeviceProcAddr(*pDevice, func.name.c_str());
468 layer.custom_dispatch_functions.at(func.name.c_str()) = next_func;
469 }
470
471 if (layer.create_device_callback) {
472 result = layer.create_device_callback(layer);
473 }
474
475 // Need to add the created devices to the list so it can be freed
476 layer.created_devices.push_back(device);
477
478 if (layer.do_spurious_allocations_in_create_device && pAllocator && pAllocator->pfnAllocation) {
479 void* allocation = pAllocator->pfnAllocation(pAllocator->pUserData, 110, 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
480 if (allocation == nullptr) {
481 return VK_ERROR_OUT_OF_HOST_MEMORY;
482 } else {
483 layer.spurious_device_memory_allocations.push_back({allocation, device.device_handle});
484 }
485 }
486
487 if (layer.call_create_device_while_create_device_is_called) {
488 PFN_vkGetDeviceProcAddr next_gdpa = layer.next_vkGetDeviceProcAddr;
489 result = layer.callback_vkCreateDevice(
490 instance_to_use, layer.queried_physical_devices.at(layer.physical_device_index_to_use_during_create_device),
491 pCreateInfo, pAllocator, &layer.second_device_created_during_create_device.device_handle, get_instance_func,
492 &next_gdpa);
493 if (result != VK_SUCCESS) {
494 return result;
495 }
496 // initialize the other device's dispatch table
497 layer_init_device_dispatch_table(layer.second_device_created_during_create_device.device_handle,
498 &layer.second_device_created_during_create_device.dispatch_table, next_gdpa);
499 }
500
501 return result;
502 }
503
504 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount,
505 VkPhysicalDevice* pPhysicalDevices) {
506 if (layer.add_phys_devs || layer.remove_phys_devs || layer.reorder_phys_devs) {
507 VkResult res = VK_SUCCESS;
508
509 if (layer.complete_physical_devices.size() == 0) {
510 // Get list of all physical devices from lower down
511 // NOTE: This only works if we don't test changing the number of devices
512 // underneath us when using this test.
513 uint32_t icd_count = 0;
514 layer.instance_dispatch_table.EnumeratePhysicalDevices(instance, &icd_count, nullptr);
515 std::vector<VkPhysicalDevice> tmp_vector;
516 tmp_vector.resize(icd_count);
517 layer.instance_dispatch_table.EnumeratePhysicalDevices(instance, &icd_count, tmp_vector.data());
518 layer.complete_physical_devices.clear();
519
520 if (layer.remove_phys_devs) {
521 // Erase the 3rd and 4th items
522 layer.removed_physical_devices.push_back(tmp_vector[3]);
523 layer.removed_physical_devices.push_back(tmp_vector[4]);
524 tmp_vector.erase(tmp_vector.begin() + 3);
525 tmp_vector.erase(tmp_vector.begin() + 3);
526 }
527
528 if (layer.add_phys_devs) {
529 // Insert a new device in the beginning, middle, and end
530 uint32_t middle = static_cast<uint32_t>(tmp_vector.size() / 2);
531 VkPhysicalDevice new_phys_dev = reinterpret_cast<VkPhysicalDevice>((size_t)(0xABCD0000));
532 layer.added_physical_devices.push_back(new_phys_dev);
533 tmp_vector.insert(tmp_vector.begin(), new_phys_dev);
534 new_phys_dev = reinterpret_cast<VkPhysicalDevice>((size_t)(0xBADC0000));
535 layer.added_physical_devices.push_back(new_phys_dev);
536 tmp_vector.insert(tmp_vector.begin() + middle, new_phys_dev);
537 new_phys_dev = reinterpret_cast<VkPhysicalDevice>((size_t)(0xDCBA0000));
538 layer.added_physical_devices.push_back(new_phys_dev);
539 tmp_vector.push_back(new_phys_dev);
540 }
541
542 if (layer.reorder_phys_devs) {
543 // Flip the order of items
544 for (int32_t dev = static_cast<int32_t>(tmp_vector.size() - 1); dev >= 0; --dev) {
545 layer.complete_physical_devices.push_back(tmp_vector[dev]);
546 }
547 } else {
548 // Otherwise, keep the order the same
549 for (uint32_t dev = 0; dev < tmp_vector.size(); ++dev) {
550 layer.complete_physical_devices.push_back(tmp_vector[dev]);
551 }
552 }
553 }
554
555 if (nullptr == pPhysicalDevices) {
556 *pPhysicalDeviceCount = static_cast<uint32_t>(layer.complete_physical_devices.size());
557 } else {
558 uint32_t adj_count = static_cast<uint32_t>(layer.complete_physical_devices.size());
559 if (*pPhysicalDeviceCount < adj_count) {
560 adj_count = *pPhysicalDeviceCount;
561 res = VK_INCOMPLETE;
562 }
563 for (uint32_t dev = 0; dev < adj_count; ++dev) {
564 pPhysicalDevices[dev] = layer.complete_physical_devices[dev];
565 }
566 *pPhysicalDeviceCount = adj_count;
567 }
568
569 return res;
570 } else {
571 return layer.instance_dispatch_table.EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
572 }
573 }
574
575 VKAPI_ATTR void VKAPI_CALL test_vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
576 VkPhysicalDeviceProperties* pProperties) {
577 if (std::find(layer.removed_physical_devices.begin(), layer.removed_physical_devices.end(), physicalDevice) !=
578 layer.removed_physical_devices.end()) {
579 // Should not get here since the application should not know about those devices
580 assert(false);
581 } else if (std::find(layer.added_physical_devices.begin(), layer.added_physical_devices.end(), physicalDevice) !=
582 layer.added_physical_devices.end()) {
583 // Added device so put in some placeholder info we can test against
584 pProperties->apiVersion = VK_API_VERSION_1_2;
585 pProperties->driverVersion = VK_MAKE_API_VERSION(0, 12, 14, 196);
586 pProperties->vendorID = 0xDECAFBAD;
587 pProperties->deviceID = 0xDEADBADD;
588 #if defined(_WIN32)
589 strncpy_s(pProperties->deviceName, VK_MAX_PHYSICAL_DEVICE_NAME_SIZE, "physdev_added_xx", 17);
590 #else
591 strncpy(pProperties->deviceName, "physdev_added_xx", VK_MAX_PHYSICAL_DEVICE_NAME_SIZE);
592 #endif
593 } else {
594 // Not an affected device so just return
595 layer.instance_dispatch_table.GetPhysicalDeviceProperties(physicalDevice, pProperties);
596 }
597 }
598
599 VKAPI_ATTR VkResult VKAPI_CALL test_vkEnumeratePhysicalDeviceGroups(
600 VkInstance instance, uint32_t* pPhysicalDeviceGroupCount, VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
601 if (layer.add_phys_devs || layer.remove_phys_devs || layer.reorder_phys_devs) {
602 VkResult res = VK_SUCCESS;
603
604 if (layer.complete_physical_device_groups.size() == 0) {
605 uint32_t fake_count = 1000;
606 // Call EnumerateDevices to add remove devices as needed
607 test_vkEnumeratePhysicalDevices(instance, &fake_count, nullptr);
608
609 // Get list of all physical devices from lower down
610 // NOTE: This only works if we don't test changing the number of devices
611 // underneath us when using this test.
612 uint32_t icd_group_count = 0;
613 layer.instance_dispatch_table.EnumeratePhysicalDeviceGroups(instance, &icd_group_count, nullptr);
614 std::vector<VkPhysicalDeviceGroupProperties> tmp_vector(icd_group_count,
615 {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES});
616 layer.instance_dispatch_table.EnumeratePhysicalDeviceGroups(instance, &icd_group_count, tmp_vector.data());
617 layer.complete_physical_device_groups.clear();
618
619 if (layer.remove_phys_devs) {
620 // Now, if a device has been removed, and it was the only group, we need to remove the group as well.
621 for (uint32_t rem_dev = 0; rem_dev < layer.removed_physical_devices.size(); ++rem_dev) {
622 for (uint32_t group = 0; group < icd_group_count; ++group) {
623 for (uint32_t grp_dev = 0; grp_dev < tmp_vector[group].physicalDeviceCount; ++grp_dev) {
624 if (tmp_vector[group].physicalDevices[grp_dev] == layer.removed_physical_devices[rem_dev]) {
625 for (uint32_t cp_item = grp_dev + 1; cp_item < tmp_vector[group].physicalDeviceCount; ++cp_item) {
626 tmp_vector[group].physicalDevices[grp_dev] = tmp_vector[group].physicalDevices[cp_item];
627 }
628 tmp_vector[group].physicalDeviceCount--;
629 }
630 }
631 }
632 }
633 for (uint32_t group = 0; group < tmp_vector.size(); ++group) {
634 if (tmp_vector[group].physicalDeviceCount == 0) {
635 layer.removed_physical_device_groups.push_back(tmp_vector[group]);
636 tmp_vector.erase(tmp_vector.begin() + group);
637 --group;
638 }
639 }
640 }
641
642 if (layer.add_phys_devs) {
643 // Add a new group for each physical device not associated with a current group
644 for (uint32_t dev = 0; dev < layer.added_physical_devices.size(); ++dev) {
645 VkPhysicalDeviceGroupProperties props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES};
646 props.physicalDeviceCount = 1;
647 props.physicalDevices[0] = layer.added_physical_devices[dev];
648 tmp_vector.push_back(props);
649 layer.added_physical_device_groups.push_back(props);
650 }
651 }
652
653 if (layer.reorder_phys_devs) {
654 // Flip the order of items
655 for (int32_t dev = static_cast<int32_t>(tmp_vector.size() - 1); dev >= 0; --dev) {
656 layer.complete_physical_device_groups.push_back(tmp_vector[dev]);
657 }
658 } else {
659 // Otherwise, keep the order the same
660 for (uint32_t dev = 0; dev < tmp_vector.size(); ++dev) {
661 layer.complete_physical_device_groups.push_back(tmp_vector[dev]);
662 }
663 }
664 }
665
666 if (nullptr == pPhysicalDeviceGroupProperties) {
667 *pPhysicalDeviceGroupCount = static_cast<uint32_t>(layer.complete_physical_device_groups.size());
668 } else {
669 uint32_t adj_count = static_cast<uint32_t>(layer.complete_physical_device_groups.size());
670 if (*pPhysicalDeviceGroupCount < adj_count) {
671 adj_count = *pPhysicalDeviceGroupCount;
672 res = VK_INCOMPLETE;
673 }
674 for (uint32_t dev = 0; dev < adj_count; ++dev) {
675 pPhysicalDeviceGroupProperties[dev] = layer.complete_physical_device_groups[dev];
676 }
677 *pPhysicalDeviceGroupCount = adj_count;
678 }
679
680 return res;
681 } else {
682 return layer.instance_dispatch_table.EnumeratePhysicalDeviceGroups(instance, pPhysicalDeviceGroupCount,
683 pPhysicalDeviceGroupProperties);
684 }
685 }
686
687 // device functions
688
689 VKAPI_ATTR void VKAPI_CALL test_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
690 for (uint32_t i = 0; i < layer.spurious_device_memory_allocations.size();) {
691 auto& allocation = layer.spurious_device_memory_allocations[i];
692 if (allocation.device == device && pAllocator && pAllocator->pfnFree) {
693 pAllocator->pfnFree(pAllocator->pUserData, allocation.allocation);
694 layer.spurious_device_memory_allocations.erase(layer.spurious_device_memory_allocations.begin() + i);
695 } else {
696 i++;
697 }
698 }
699
700 if (layer.call_create_device_while_create_device_is_called) {
701 layer.callback_vkDestroyDevice(layer.second_device_created_during_create_device.device_handle, pAllocator,
702 layer.second_device_created_during_create_device.dispatch_table.DestroyDevice);
703 }
704
705 auto it = std::find_if(std::begin(layer.created_devices), std::end(layer.created_devices),
706 [device](const TestLayer::Device& dev) { return device == dev.device_handle; });
707 if (it != std::end(layer.created_devices)) {
708 it->dispatch_table.DestroyDevice(device, pAllocator);
709 layer.created_devices.erase(it);
710 }
711 }
712
713 VKAPI_ATTR VkResult VKAPI_CALL test_vkCreateDebugUtilsMessengerEXT(VkInstance instance,
714 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
715 const VkAllocationCallbacks* pAllocator,
716 VkDebugUtilsMessengerEXT* pMessenger) {
717 if (layer.instance_dispatch_table.CreateDebugUtilsMessengerEXT) {
718 return layer.instance_dispatch_table.CreateDebugUtilsMessengerEXT(instance, pCreateInfo, pAllocator, pMessenger);
719 } else {
720 return VK_SUCCESS;
721 }
722 }
723
724 VKAPI_ATTR void VKAPI_CALL test_vkDestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
725 const VkAllocationCallbacks* pAllocator) {
726 if (layer.instance_dispatch_table.DestroyDebugUtilsMessengerEXT)
727 return layer.instance_dispatch_table.DestroyDebugUtilsMessengerEXT(instance, messenger, pAllocator);
728 }
729
730 // Debug utils & debug marker ext stubs
731 VKAPI_ATTR VkResult VKAPI_CALL test_vkDebugMarkerSetObjectTagEXT(VkDevice dev, const VkDebugMarkerObjectTagInfoEXT* pTagInfo) {
732 for (const auto& d : layer.created_devices) {
733 if (d.device_handle == dev) {
734 if (d.dispatch_table.DebugMarkerSetObjectTagEXT) {
735 return d.dispatch_table.DebugMarkerSetObjectTagEXT(dev, pTagInfo);
736 } else {
737 return VK_SUCCESS;
738 }
739 }
740 }
741 return VK_SUCCESS;
742 }
743 VKAPI_ATTR VkResult VKAPI_CALL test_vkDebugMarkerSetObjectNameEXT(VkDevice dev, const VkDebugMarkerObjectNameInfoEXT* pNameInfo) {
744 for (const auto& d : layer.created_devices) {
745 if (d.device_handle == dev) {
746 if (d.dispatch_table.DebugMarkerSetObjectNameEXT) {
747 return d.dispatch_table.DebugMarkerSetObjectNameEXT(dev, pNameInfo);
748 } else {
749 return VK_SUCCESS;
750 }
751 }
752 }
753 return VK_SUCCESS;
754 }
755 VKAPI_ATTR void VKAPI_CALL test_vkCmdDebugMarkerBeginEXT(VkCommandBuffer cmd_buf, const VkDebugMarkerMarkerInfoEXT* marker_info) {
756 // Just call the first device -
757 if (layer.created_devices[0].dispatch_table.CmdDebugMarkerBeginEXT)
758 layer.created_devices[0].dispatch_table.CmdDebugMarkerBeginEXT(cmd_buf, marker_info);
759 }
760 VKAPI_ATTR void VKAPI_CALL test_vkCmdDebugMarkerEndEXT(VkCommandBuffer cmd_buf) {
761 // Just call the first device -
762 if (layer.created_devices[0].dispatch_table.CmdDebugMarkerEndEXT)
763 layer.created_devices[0].dispatch_table.CmdDebugMarkerEndEXT(cmd_buf);
764 }
765 VKAPI_ATTR void VKAPI_CALL test_vkCmdDebugMarkerInsertEXT(VkCommandBuffer cmd_buf, const VkDebugMarkerMarkerInfoEXT* marker_info) {
766 // Just call the first device -
767 if (layer.created_devices[0].dispatch_table.CmdDebugMarkerInsertEXT)
768 layer.created_devices[0].dispatch_table.CmdDebugMarkerInsertEXT(cmd_buf, marker_info);
769 }
770
771 VKAPI_ATTR VkResult VKAPI_CALL test_vkSetDebugUtilsObjectNameEXT(VkDevice dev, const VkDebugUtilsObjectNameInfoEXT* pNameInfo) {
772 for (const auto& d : layer.created_devices) {
773 if (d.device_handle == dev) {
774 if (d.dispatch_table.SetDebugUtilsObjectNameEXT) {
775 return d.dispatch_table.SetDebugUtilsObjectNameEXT(dev, pNameInfo);
776 } else {
777 return VK_SUCCESS;
778 }
779 }
780 }
781 return VK_SUCCESS;
782 }
783 VKAPI_ATTR VkResult VKAPI_CALL test_vkSetDebugUtilsObjectTagEXT(VkDevice dev, const VkDebugUtilsObjectTagInfoEXT* pTagInfo) {
784 for (const auto& d : layer.created_devices) {
785 if (d.device_handle == dev) {
786 if (d.dispatch_table.SetDebugUtilsObjectTagEXT) {
787 return d.dispatch_table.SetDebugUtilsObjectTagEXT(dev, pTagInfo);
788 } else {
789 return VK_SUCCESS;
790 }
791 }
792 }
793 return VK_SUCCESS;
794 }
795 VKAPI_ATTR void VKAPI_CALL test_vkQueueBeginDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT* label) {
796 // Just call the first device -
797 if (layer.created_devices[0].dispatch_table.QueueBeginDebugUtilsLabelEXT)
798 layer.created_devices[0].dispatch_table.QueueBeginDebugUtilsLabelEXT(queue, label);
799 }
800 VKAPI_ATTR void VKAPI_CALL test_vkQueueEndDebugUtilsLabelEXT(VkQueue queue) {
801 // Just call the first device -
802 if (layer.created_devices[0].dispatch_table.QueueEndDebugUtilsLabelEXT)
803 layer.created_devices[0].dispatch_table.QueueEndDebugUtilsLabelEXT(queue);
804 }
805 VKAPI_ATTR void VKAPI_CALL test_vkQueueInsertDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT* label) {
806 // Just call the first device -
807 if (layer.created_devices[0].dispatch_table.QueueInsertDebugUtilsLabelEXT)
808 layer.created_devices[0].dispatch_table.QueueInsertDebugUtilsLabelEXT(queue, label);
809 }
810 VKAPI_ATTR void VKAPI_CALL test_vkCmdBeginDebugUtilsLabelEXT(VkCommandBuffer cmd_buf, const VkDebugUtilsLabelEXT* label) {
811 // Just call the first device -
812 if (layer.created_devices[0].dispatch_table.CmdBeginDebugUtilsLabelEXT)
813 layer.created_devices[0].dispatch_table.CmdBeginDebugUtilsLabelEXT(cmd_buf, label);
814 }
815 VKAPI_ATTR void VKAPI_CALL test_vkCmdEndDebugUtilsLabelEXT(VkCommandBuffer cmd_buf) {
816 // Just call the first device -
817 if (layer.created_devices[0].dispatch_table.CmdEndDebugUtilsLabelEXT)
818 layer.created_devices[0].dispatch_table.CmdEndDebugUtilsLabelEXT(cmd_buf);
819 }
820 VKAPI_ATTR void VKAPI_CALL test_vkCmdInsertDebugUtilsLabelEXT(VkCommandBuffer cmd_buf, const VkDebugUtilsLabelEXT* label) {
821 // Just call the first device -
822 if (layer.created_devices[0].dispatch_table.CmdInsertDebugUtilsLabelEXT)
823 layer.created_devices[0].dispatch_table.CmdInsertDebugUtilsLabelEXT(cmd_buf, label);
824 }
825
826 // forward declarations needed for trampolines
827 #if TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR
828 extern "C" {
829 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance, const char* pName);
830 }
831 #endif
832
833 // trampolines
834 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device_func(VkDevice device, const char* pName);
835 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device_func_impl([[maybe_unused]] VkDevice device, const char* pName) {
836 if (string_eq(pName, "vkGetDeviceProcAddr")) return to_vkVoidFunction(get_device_func);
837 if (string_eq(pName, "vkDestroyDevice")) return to_vkVoidFunction(test_vkDestroyDevice);
838
839 if (IsDeviceExtensionAvailable(device, VK_EXT_DEBUG_MARKER_EXTENSION_NAME)) {
840 if (string_eq(pName, "vkDebugMarkerSetObjectTagEXT")) return to_vkVoidFunction(test_vkDebugMarkerSetObjectTagEXT);
841 if (string_eq(pName, "vkDebugMarkerSetObjectNameEXT")) return to_vkVoidFunction(test_vkDebugMarkerSetObjectNameEXT);
842 if (string_eq(pName, "vkCmdDebugMarkerBeginEXT")) return to_vkVoidFunction(test_vkCmdDebugMarkerBeginEXT);
843 if (string_eq(pName, "vkCmdDebugMarkerEndEXT")) return to_vkVoidFunction(test_vkCmdDebugMarkerEndEXT);
844 if (string_eq(pName, "vkCmdDebugMarkerInsertEXT")) return to_vkVoidFunction(test_vkCmdDebugMarkerInsertEXT);
845 }
846 if (IsInstanceExtensionEnabled(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
847 if (string_eq(pName, "vkSetDebugUtilsObjectNameEXT")) return to_vkVoidFunction(test_vkSetDebugUtilsObjectNameEXT);
848 if (string_eq(pName, "vkSetDebugUtilsObjectTagEXT")) return to_vkVoidFunction(test_vkSetDebugUtilsObjectTagEXT);
849 if (string_eq(pName, "vkQueueBeginDebugUtilsLabelEXT")) return to_vkVoidFunction(test_vkQueueBeginDebugUtilsLabelEXT);
850 if (string_eq(pName, "vkQueueEndDebugUtilsLabelEXT")) return to_vkVoidFunction(test_vkQueueEndDebugUtilsLabelEXT);
851 if (string_eq(pName, "vkQueueInsertDebugUtilsLabelEXT")) return to_vkVoidFunction(test_vkQueueInsertDebugUtilsLabelEXT);
852 if (string_eq(pName, "vkCmdBeginDebugUtilsLabelEXT")) return to_vkVoidFunction(test_vkCmdBeginDebugUtilsLabelEXT);
853 if (string_eq(pName, "vkCmdEndDebugUtilsLabelEXT")) return to_vkVoidFunction(test_vkCmdEndDebugUtilsLabelEXT);
854 if (string_eq(pName, "vkCmdInsertDebugUtilsLabelEXT")) return to_vkVoidFunction(test_vkCmdInsertDebugUtilsLabelEXT);
855 }
856
857 for (auto& func : layer.custom_device_interception_functions) {
858 if (func.name == pName) {
859 return to_vkVoidFunction(func.function);
860 }
861 }
862
863 for (auto& func : layer.custom_device_implementation_functions) {
864 if (func.name == pName) {
865 return to_vkVoidFunction(func.function);
866 }
867 }
868
869 return nullptr;
870 }
871
872 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device_func([[maybe_unused]] VkDevice device, const char* pName) {
873 PFN_vkVoidFunction ret_dev = get_device_func_impl(device, pName);
874 if (ret_dev != nullptr) return ret_dev;
875
876 return layer.next_vkGetDeviceProcAddr(device, pName);
877 }
878
879 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_physical_device_func([[maybe_unused]] VkInstance instance, const char* pName) {
880 if (string_eq(pName, "vkEnumerateDeviceLayerProperties")) return to_vkVoidFunction(test_vkEnumerateDeviceLayerProperties);
881 if (string_eq(pName, "vkEnumerateDeviceExtensionProperties"))
882 return to_vkVoidFunction(test_vkEnumerateDeviceExtensionProperties);
883 if (string_eq(pName, "vkEnumeratePhysicalDevices")) return (PFN_vkVoidFunction)test_vkEnumeratePhysicalDevices;
884 if (string_eq(pName, "vkEnumeratePhysicalDeviceGroups")) return (PFN_vkVoidFunction)test_vkEnumeratePhysicalDeviceGroups;
885 if (string_eq(pName, "vkGetPhysicalDeviceProperties")) return (PFN_vkVoidFunction)test_vkGetPhysicalDeviceProperties;
886
887 for (auto& func : layer.custom_physical_device_interception_functions) {
888 if (func.name == pName) {
889 return to_vkVoidFunction(func.function);
890 }
891 }
892
893 for (auto& func : layer.custom_physical_device_implementation_functions) {
894 if (func.name == pName) {
895 return to_vkVoidFunction(func.function);
896 }
897 }
898
899 #if TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR
900 if (string_eq(pName, "vk_layerGetPhysicalDeviceProcAddr")) return to_vkVoidFunction(vk_layerGetPhysicalDeviceProcAddr);
901 #endif
902 return nullptr;
903 }
904 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_instance_func(VkInstance instance, const char* pName);
905 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_instance_func_impl(VkInstance instance, const char* pName) {
906 if (pName == nullptr) return nullptr;
907 if (string_eq(pName, "vkGetInstanceProcAddr")) return to_vkVoidFunction(get_instance_func);
908 if (string_eq(pName, "vkEnumerateInstanceLayerProperties")) return to_vkVoidFunction(test_vkEnumerateInstanceLayerProperties);
909 if (string_eq(pName, "vkEnumerateInstanceExtensionProperties"))
910 return to_vkVoidFunction(test_vkEnumerateInstanceExtensionProperties);
911 if (string_eq(pName, "vkEnumerateInstanceVersion")) return to_vkVoidFunction(test_vkEnumerateInstanceVersion);
912 if (string_eq(pName, "vkCreateInstance")) return to_vkVoidFunction(test_vkCreateInstance);
913 if (string_eq(pName, "vkDestroyInstance")) return to_vkVoidFunction(test_vkDestroyInstance);
914 if (string_eq(pName, "vkCreateDevice")) return to_vkVoidFunction(test_vkCreateDevice);
915 if (string_eq(pName, "vkGetDeviceProcAddr")) return to_vkVoidFunction(get_device_func);
916
917 PFN_vkVoidFunction ret_phys_dev = get_physical_device_func(instance, pName);
918 if (ret_phys_dev != nullptr) return ret_phys_dev;
919
920 PFN_vkVoidFunction ret_dev = get_device_func_impl(nullptr, pName);
921 if (ret_dev != nullptr) return ret_dev;
922
923 return nullptr;
924 }
925
926 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_instance_func(VkInstance instance, const char* pName) {
927 PFN_vkVoidFunction ret_dev = get_instance_func_impl(instance, pName);
928 if (ret_dev != nullptr) return ret_dev;
929
930 return layer.next_vkGetInstanceProcAddr(instance, pName);
931 }
932
933 // Exported functions
934 extern "C" {
935 #if TEST_LAYER_EXPORT_ENUMERATE_FUNCTIONS
936
937 // Pre-instance handling functions
938
939 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL test_preinst_vkEnumerateInstanceLayerProperties(
940 const VkEnumerateInstanceLayerPropertiesChain* pChain, uint32_t* pPropertyCount, VkLayerProperties* pProperties) {
941 VkResult res = pChain->pfnNextLayer(pChain->pNextLink, pPropertyCount, pProperties);
942 if (nullptr == pProperties) {
943 *pPropertyCount = layer.reported_layer_props;
944 } else {
945 uint32_t count = layer.reported_layer_props;
946 if (*pPropertyCount < layer.reported_layer_props) {
947 count = *pPropertyCount;
948 res = VK_INCOMPLETE;
949 }
950 for (uint32_t i = 0; i < count; ++i) {
951 snprintf(pProperties[i].layerName, VK_MAX_EXTENSION_NAME_SIZE, "%02d_layer", count);
952 pProperties[i].specVersion = count;
953 pProperties[i].implementationVersion = 0xABCD0000 + count;
954 }
955 }
956 return res;
957 }
958
959 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL test_preinst_vkEnumerateInstanceExtensionProperties(
960 const VkEnumerateInstanceExtensionPropertiesChain* pChain, const char* pLayerName, uint32_t* pPropertyCount,
961 VkExtensionProperties* pProperties) {
962 VkResult res = pChain->pfnNextLayer(pChain->pNextLink, pLayerName, pPropertyCount, pProperties);
963 if (nullptr == pProperties) {
964 *pPropertyCount = layer.reported_extension_props;
965 } else {
966 uint32_t count = layer.reported_extension_props;
967 if (*pPropertyCount < layer.reported_extension_props) {
968 count = *pPropertyCount;
969 res = VK_INCOMPLETE;
970 }
971 for (uint32_t i = 0; i < count; ++i) {
972 snprintf(pProperties[i].extensionName, VK_MAX_EXTENSION_NAME_SIZE, "%02d_ext", count);
973 pProperties[i].specVersion = count;
974 }
975 }
976 return res;
977 }
978
979 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
980 test_preinst_vkEnumerateInstanceVersion(const VkEnumerateInstanceVersionChain* pChain, uint32_t* pApiVersion) {
981 VkResult res = pChain->pfnNextLayer(pChain->pNextLink, pApiVersion);
982 *pApiVersion = layer.reported_instance_version;
983 return res;
984 }
985
986 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
987 VkLayerProperties* pProperties) {
988 return test_vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties);
989 }
990 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char* pLayerName,
991 uint32_t* pPropertyCount,
992 VkExtensionProperties* pProperties) {
993 return test_vkEnumerateInstanceExtensionProperties(pLayerName, pPropertyCount, pProperties);
994 }
995
996 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
997 uint32_t* pPropertyCount,
998 VkLayerProperties* pProperties) {
999 return test_vkEnumerateDeviceLayerProperties(physicalDevice, pPropertyCount, pProperties);
1000 }
1001 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1002 const char* pLayerName,
1003 uint32_t* pPropertyCount,
1004 VkExtensionProperties* pProperties) {
1005 return test_vkEnumerateDeviceExtensionProperties(physicalDevice, pLayerName, pPropertyCount, pProperties);
1006 }
1007 #endif
1008
1009 #if TEST_LAYER_EXPORT_LAYER_NAMED_GIPA
1010 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL test_layer_GetInstanceProcAddr(VkInstance instance, const char* pName) {
1011 return get_instance_func(instance, pName);
1012 }
1013 #endif
1014
1015 #if TEST_LAYER_EXPORT_OVERRIDE_GIPA
1016 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL test_override_vkGetInstanceProcAddr(VkInstance instance,
1017 const char* pName) {
1018 if (string_eq(pName, "vkCreateInstance")) return to_vkVoidFunction(test_override_vkCreateInstance);
1019 return get_instance_func(instance, pName);
1020 }
1021 #endif
1022
1023 #if TEST_LAYER_EXPORT_LAYER_VK_GIPA
1024 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName) {
1025 return get_instance_func(instance, pName);
1026 }
1027 #endif
1028
1029 #if TEST_LAYER_EXPORT_LAYER_NAMED_GDPA
1030 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL test_layer_GetDeviceProcAddr(VkDevice device, const char* pName) {
1031 return get_device_func(device, pName);
1032 }
1033 #endif
1034
1035 #if TEST_LAYER_EXPORT_OVERRIDE_GDPA
1036 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL test_override_GetDeviceProcAddr(VkDevice device, const char* pName) {
1037 return get_device_func(device, pName);
1038 }
1039 #endif
1040
1041 #if TEST_LAYER_EXPORT_LAYER_VK_GDPA
1042 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* pName) {
1043 return get_device_func(device, pName);
1044 }
1045 #endif
1046
1047 #if TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR
1048 FRAMEWORK_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1049 const char* pName) {
1050 auto func = get_physical_device_func(instance, pName);
1051 if (func != nullptr) return func;
1052 return layer.next_GetPhysicalDeviceProcAddr(instance, pName);
1053 }
1054 #endif
1055
1056 #if LAYER_EXPORT_NEGOTIATE_LOADER_LAYER_INTERFACE_VERSION
1057 // vk_layer.h has a forward declaration of vkNegotiateLoaderLayerInterfaceVersion, which doesn't have any attributes
1058 // Since FRAMEWORK_EXPORT adds __declspec(dllexport), we can't do that here, thus we need our own macro
1059 #if (defined(__GNUC__) && (__GNUC__ >= 4)) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
1060 #define EXPORT_NEGOTIATE_FUNCTION __attribute__((visibility("default")))
1061 #else
1062 #define EXPORT_NEGOTIATE_FUNCTION
1063 #endif
1064
1065 EXPORT_NEGOTIATE_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL
1066 vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface* pVersionStruct) {
1067 if (pVersionStruct) {
1068 if (pVersionStruct->loaderLayerInterfaceVersion < layer.min_implementation_version) {
1069 return VK_ERROR_INITIALIZATION_FAILED;
1070 }
1071
1072 pVersionStruct->loaderLayerInterfaceVersion = layer.implementation_version;
1073 pVersionStruct->pfnGetInstanceProcAddr = get_instance_func;
1074 pVersionStruct->pfnGetDeviceProcAddr = get_device_func;
1075 #if TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR
1076 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1077 #else
1078 pVersionStruct->pfnGetPhysicalDeviceProcAddr = nullptr;
1079 #endif
1080
1081 return VK_SUCCESS;
1082 }
1083 return VK_ERROR_INITIALIZATION_FAILED;
1084 }
1085 #endif
1086 } // extern "C"