| 1 |
/* |
| 2 |
* Copyright (c) 2021-2023 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2021-2023 Valve Corporation |
| 4 |
* Copyright (c) 2021-2023 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 <array> |
| 31 |
#include <filesystem> |
| 32 |
#include <ostream> |
| 33 |
#include <unordered_map> |
| 34 |
|
| 35 |
#include "util/dispatchable_handle.h" |
| 36 |
#include "util/platform_wsi.h" |
| 37 |
#include "util/functions.h" |
| 38 |
|
| 39 |
#include "layer/layer_util.h" |
| 40 |
|
| 41 |
enum class CalledICDGIPA { not_called, vk_icd_gipa, vk_gipa }; |
| 42 |
|
| 43 |
enum class CalledNegotiateInterface { not_called, vk_icd_negotiate, vk_icd_gipa_first }; |
| 44 |
|
| 45 |
enum class InterfaceVersionCheck { |
| 46 |
not_called, |
| 47 |
loader_version_too_old, |
| 48 |
loader_version_too_new, |
| 49 |
icd_version_too_new, |
| 50 |
version_is_supported |
| 51 |
}; |
| 52 |
|
| 53 |
// clang-format off |
| 54 |
inline std::ostream& operator<<(std::ostream& os, const CalledICDGIPA& result) { |
| 55 |
switch (result) { |
| 56 |
case (CalledICDGIPA::not_called): return os << "CalledICDGIPA::not_called"; |
| 57 |
case (CalledICDGIPA::vk_icd_gipa): return os << "CalledICDGIPA::vk_icd_gipa"; |
| 58 |
case (CalledICDGIPA::vk_gipa): return os << "CalledICDGIPA::vk_gipa"; |
| 59 |
} |
| 60 |
return os << static_cast<uint32_t>(result); |
| 61 |
} |
| 62 |
inline std::ostream& operator<<(std::ostream& os, const CalledNegotiateInterface& result) { |
| 63 |
switch (result) { |
| 64 |
case (CalledNegotiateInterface::not_called): return os << "CalledNegotiateInterface::not_called"; |
| 65 |
case (CalledNegotiateInterface::vk_icd_negotiate): return os << "CalledNegotiateInterface::vk_icd_negotiate"; |
| 66 |
case (CalledNegotiateInterface::vk_icd_gipa_first): return os << "CalledNegotiateInterface::vk_icd_gipa_first"; |
| 67 |
} |
| 68 |
return os << static_cast<uint32_t>(result); |
| 69 |
} |
| 70 |
inline std::ostream& operator<<(std::ostream& os, const InterfaceVersionCheck& result) { |
| 71 |
switch (result) { |
| 72 |
case (InterfaceVersionCheck::not_called): return os << "InterfaceVersionCheck::not_called"; |
| 73 |
case (InterfaceVersionCheck::loader_version_too_old): return os << "InterfaceVersionCheck::loader_version_too_old"; |
| 74 |
case (InterfaceVersionCheck::loader_version_too_new): return os << "InterfaceVersionCheck::loader_version_too_new"; |
| 75 |
case (InterfaceVersionCheck::icd_version_too_new): return os << "InterfaceVersionCheck::icd_version_too_new"; |
| 76 |
case (InterfaceVersionCheck::version_is_supported): return os << "InterfaceVersionCheck::version_is_supported"; |
| 77 |
} |
| 78 |
return os << static_cast<uint32_t>(result); |
| 79 |
} |
| 80 |
|
| 81 |
using VulkanUUID = std::array<uint8_t, VK_UUID_SIZE>; |
| 82 |
|
| 83 |
// clang-format on |
| 84 |
|
| 85 |
// Move only type because it holds a DispatchableHandle<VkPhysicalDevice> |
| 86 |
struct PhysicalDevice { |
| 87 |
PhysicalDevice() {} |
| 88 |
PhysicalDevice(std::string name) : deviceName(name) {} |
| 89 |
PhysicalDevice(const char* name) : deviceName(name) {} |
| 90 |
|
| 91 |
DispatchableHandle<VkPhysicalDevice> vk_physical_device; |
| 92 |
BUILDER_VALUE(std::string, deviceName) |
| 93 |
BUILDER_VALUE(VulkanUUID, deviceUUID) |
| 94 |
BUILDER_VALUE(VulkanUUID, driverUUID) |
| 95 |
BUILDER_VALUE(VkPhysicalDeviceProperties, properties) |
| 96 |
BUILDER_VALUE(VkPhysicalDeviceFeatures, features) |
| 97 |
BUILDER_VALUE(VkPhysicalDeviceMemoryProperties, memory_properties) |
| 98 |
BUILDER_VALUE(VkPhysicalDeviceDriverProperties, driver_properties) |
| 99 |
BUILDER_VALUE(VkImageFormatProperties, image_format_properties) |
| 100 |
BUILDER_VALUE(VkExternalMemoryProperties, external_memory_properties) |
| 101 |
BUILDER_VALUE(VkExternalSemaphoreProperties, external_semaphore_properties) |
| 102 |
BUILDER_VALUE(VkExternalFenceProperties, external_fence_properties) |
| 103 |
BUILDER_VALUE(uint32_t, pci_bus) |
| 104 |
|
| 105 |
BUILDER_VECTOR(MockQueueFamilyProperties, queue_family_properties, queue_family_properties) |
| 106 |
BUILDER_VECTOR(VkFormatProperties, format_properties, format_properties) |
| 107 |
BUILDER_VECTOR(VkSparseImageFormatProperties, sparse_image_format_properties, sparse_image_format_properties) |
| 108 |
|
| 109 |
BUILDER_VECTOR(Extension, extensions, extension) |
| 110 |
|
| 111 |
BUILDER_VALUE(VkSurfaceCapabilitiesKHR, surface_capabilities) |
| 112 |
BUILDER_VECTOR(VkSurfaceFormatKHR, surface_formats, surface_format) |
| 113 |
BUILDER_VECTOR(VkPresentModeKHR, surface_present_modes, surface_present_mode) |
| 114 |
BUILDER_VALUE(VkSurfacePresentScalingCapabilitiesEXT, surface_present_scaling_capabilities) |
| 115 |
// No good way to make this a builder value. Each std::vector<VkPresentModeKHR> corresponds to each surface_present_modes |
| 116 |
// element |
| 117 |
std::vector<std::vector<VkPresentModeKHR>> surface_present_mode_compatibility{}; |
| 118 |
|
| 119 |
BUILDER_VECTOR(VkDisplayPropertiesKHR, display_properties, display_properties) |
| 120 |
BUILDER_VECTOR(VkDisplayPlanePropertiesKHR, display_plane_properties, display_plane_properties) |
| 121 |
BUILDER_VECTOR(VkDisplayKHR, displays, displays) |
| 122 |
BUILDER_VECTOR(VkDisplayModePropertiesKHR, display_mode_properties, display_mode_properties) |
| 123 |
BUILDER_VALUE(VkDisplayModeKHR, display_mode) |
| 124 |
BUILDER_VALUE(VkDisplayPlaneCapabilitiesKHR, display_plane_capabilities) |
| 125 |
|
| 126 |
BUILDER_VALUE_WITH_DEFAULT(VkLayeredDriverUnderlyingApiMSFT, layered_driver_underlying_api, |
| 127 |
VK_LAYERED_DRIVER_UNDERLYING_API_NONE_MSFT) |
| 128 |
|
| 129 |
PhysicalDevice& set_api_version(uint32_t version) { |
| 130 |
properties.apiVersion = version; |
| 131 |
return *this; |
| 132 |
} |
| 133 |
|
| 134 |
PhysicalDevice&& finish() { return std::move(*this); } |
| 135 |
|
| 136 |
// Defines the order this physical device appears in vkEnumeratePhysicalDevices |
| 137 |
uint32_t iteration_order = 0; |
| 138 |
|
| 139 |
// Objects created from this physical device |
| 140 |
std::vector<VkDevice> device_handles; |
| 141 |
std::vector<DeviceCreateInfo> device_create_infos; |
| 142 |
std::vector<DispatchableHandle<VkQueue>> queue_handles; |
| 143 |
|
| 144 |
// Unknown physical device functions. Add a `VulkanFunction` to this list which will be searched in |
| 145 |
// vkGetInstanceProcAddr for custom_instance_functions and vk_icdGetPhysicalDeviceProcAddr for custom_physical_device_functions. |
| 146 |
// To add unknown device functions, add it to the PhysicalDevice directly (in the known_device_functions member) |
| 147 |
BUILDER_VECTOR(VulkanFunction, custom_physical_device_functions, custom_physical_device_function) |
| 148 |
|
| 149 |
// List of function names which are 'known' to the physical device but have test defined implementations |
| 150 |
// The purpose of this list is so that vkGetDeviceProcAddr returns 'a real function pointer' in tests |
| 151 |
// without actually implementing any of the logic inside of it. |
| 152 |
BUILDER_VECTOR(VulkanFunction, known_device_functions, device_function) |
| 153 |
}; |
| 154 |
|
| 155 |
struct PhysicalDeviceGroup { |
| 156 |
PhysicalDeviceGroup() {} |
| 157 |
PhysicalDeviceGroup(PhysicalDevice const& physical_device) { physical_device_handles.push_back(&physical_device); } |
| 158 |
PhysicalDeviceGroup(PhysicalDevice const* physical_device) { physical_device_handles.push_back(physical_device); } |
| 159 |
PhysicalDeviceGroup(std::vector<PhysicalDevice*> const& physical_devices) { |
| 160 |
physical_device_handles.insert(physical_device_handles.end(), physical_devices.begin(), physical_devices.end()); |
| 161 |
} |
| 162 |
PhysicalDeviceGroup& use_physical_device(PhysicalDevice const* physical_device) { |
| 163 |
physical_device_handles.push_back(physical_device); |
| 164 |
return *this; |
| 165 |
} |
| 166 |
PhysicalDeviceGroup& use_physical_device(PhysicalDevice const& physical_device) { |
| 167 |
physical_device_handles.push_back(&physical_device); |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
std::vector<PhysicalDevice const*> physical_device_handles; |
| 172 |
VkBool32 subset_allocation = false; |
| 173 |
}; |
| 174 |
|
| 175 |
struct TestICD { |
| 176 |
std::filesystem::path manifest_file_path; |
| 177 |
|
| 178 |
BUILDER_VALUE_WITH_DEFAULT(bool, exposes_vk_icdNegotiateLoaderICDInterfaceVersion, true) |
| 179 |
BUILDER_VALUE_WITH_DEFAULT(bool, exposes_vkEnumerateInstanceExtensionProperties, true) |
| 180 |
BUILDER_VALUE_WITH_DEFAULT(bool, exposes_vkCreateInstance, true) |
| 181 |
BUILDER_VALUE_WITH_DEFAULT(bool, exposes_vk_icdGetPhysicalDeviceProcAddr, true) |
| 182 |
#if defined(WIN32) |
| 183 |
BUILDER_VALUE_WITH_DEFAULT(bool, exposes_vk_icdEnumerateAdapterPhysicalDevices, true) |
| 184 |
#endif |
| 185 |
|
| 186 |
CalledICDGIPA called_vk_icd_gipa = CalledICDGIPA::not_called; |
| 187 |
CalledNegotiateInterface called_negotiate_interface = CalledNegotiateInterface::not_called; |
| 188 |
|
| 189 |
InterfaceVersionCheck interface_version_check = InterfaceVersionCheck::not_called; |
| 190 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, min_icd_interface_version, 0) |
| 191 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, max_icd_interface_version, 7) |
| 192 |
uint32_t icd_interface_version_received = 0; |
| 193 |
|
| 194 |
bool called_enumerate_adapter_physical_devices = false; |
| 195 |
|
| 196 |
BUILDER_VALUE(bool, enable_icd_wsi); |
| 197 |
bool is_using_icd_wsi = false; |
| 198 |
|
| 199 |
TestICD& setup_WSI(const char* api_selection = nullptr) { |
| 200 |
enable_icd_wsi = true; |
| 201 |
add_instance_extensions({"VK_KHR_surface", get_platform_wsi_extension(api_selection)}); |
| 202 |
min_icd_interface_version = (3U < min_icd_interface_version) ? min_icd_interface_version : 3U; |
| 203 |
return *this; |
| 204 |
} |
| 205 |
|
| 206 |
BUILDER_VALUE_WITH_DEFAULT(uint32_t, icd_api_version, VK_API_VERSION_1_0) |
| 207 |
BUILDER_VECTOR(LayerDefinition, instance_layers, instance_layer) |
| 208 |
BUILDER_VECTOR(Extension, instance_extensions, instance_extension) |
| 209 |
std::vector<Extension> enabled_instance_extensions; |
| 210 |
|
| 211 |
std::unordered_map<VkPhysicalDevice, PhysicalDevice> physical_devices; |
| 212 |
TestICD& add_physical_device(PhysicalDevice&& physical_device) { |
| 213 |
physical_device.iteration_order = physical_devices.size(); |
| 214 |
physical_devices.emplace(physical_device.vk_physical_device.handle, std::move(physical_device)); |
| 215 |
return *this; |
| 216 |
} |
| 217 |
|
| 218 |
PhysicalDevice& add_and_get_physical_device(PhysicalDevice&& physical_device) { |
| 219 |
VkPhysicalDevice pd = physical_device.vk_physical_device.handle; |
| 220 |
physical_device.iteration_order = physical_devices.size(); |
| 221 |
physical_devices.emplace(physical_device.vk_physical_device.handle, std::move(physical_device)); |
| 222 |
return physical_devices.at(pd); |
| 223 |
} |
| 224 |
|
| 225 |
PhysicalDevice& add_physical_device_at_index(size_t index, PhysicalDevice&& physical_device) { |
| 226 |
VkPhysicalDevice pd = physical_device.vk_physical_device.handle; |
| 227 |
physical_device.iteration_order = index; |
| 228 |
for (auto& [handle, phys_dev] : physical_devices) { |
| 229 |
if (phys_dev.iteration_order >= index) { |
| 230 |
phys_dev.iteration_order++; |
| 231 |
} |
| 232 |
} |
| 233 |
physical_devices.emplace(physical_device.vk_physical_device.handle, std::move(physical_device)); |
| 234 |
return physical_devices.at(pd); |
| 235 |
} |
| 236 |
|
| 237 |
BUILDER_VECTOR(PhysicalDeviceGroup, physical_device_groups, physical_device_group); |
| 238 |
|
| 239 |
DispatchableHandle<VkInstance> instance_handle; |
| 240 |
std::vector<DispatchableHandle<VkDevice>> device_handles; |
| 241 |
std::vector<uint64_t> surface_handles; |
| 242 |
std::vector<uint64_t> messenger_handles; |
| 243 |
std::vector<uint64_t> callback_handles; |
| 244 |
std::vector<uint64_t> swapchain_handles; |
| 245 |
|
| 246 |
BUILDER_VALUE_WITH_DEFAULT(bool, can_query_vkEnumerateInstanceVersion, true); |
| 247 |
BUILDER_VALUE_WITH_DEFAULT(bool, can_query_GetPhysicalDeviceFuncs, true); |
| 248 |
|
| 249 |
// Unknown instance functions Add a `VulkanFunction` to this list which will be searched in |
| 250 |
// vkGetInstanceProcAddr for custom_instance_functions and vk_icdGetPhysicalDeviceProcAddr for |
| 251 |
// custom_physical_device_functions. To add unknown device functions, add it to the PhysicalDevice directly (in the |
| 252 |
// known_device_functions member) |
| 253 |
BUILDER_VECTOR(VulkanFunction, custom_instance_functions, custom_instance_function) |
| 254 |
|
| 255 |
// Must explicitely state support for the tooling info extension, that way we can control if vkGetInstanceProcAddr returns a |
| 256 |
// function pointer for vkGetPhysicalDeviceToolPropertiesEXT or vkGetPhysicalDeviceToolProperties (core version) |
| 257 |
BUILDER_VALUE(bool, supports_tooling_info_ext); |
| 258 |
BUILDER_VALUE(bool, supports_tooling_info_core); |
| 259 |
// List of tooling properties that this driver 'supports' |
| 260 |
BUILDER_VECTOR(VkPhysicalDeviceToolPropertiesEXT, tooling_properties, tooling_property) |
| 261 |
std::vector<DispatchableHandle<VkCommandBuffer>> allocated_command_buffers; |
| 262 |
|
| 263 |
VkInstanceCreateFlags passed_in_instance_create_flags{}; |
| 264 |
|
| 265 |
BUILDER_VALUE_WITH_DEFAULT(VkResult, enum_physical_devices_return_code, VK_SUCCESS); |
| 266 |
BUILDER_VALUE_WITH_DEFAULT(VkResult, enum_adapter_physical_devices_return_code, VK_SUCCESS); |
| 267 |
|
| 268 |
InstanceCreateInfo GetVkInstanceCreateInfo() { |
| 269 |
InstanceCreateInfo info; |
| 270 |
for (auto& layer : instance_layers) info.enabled_layers.push_back(layer.layerName.data()); |
| 271 |
for (auto& ext : instance_extensions) info.enabled_extensions.push_back(ext.extensionName.data()); |
| 272 |
return info; |
| 273 |
} |
| 274 |
|
| 275 |
// Speedup looking for physical devices by not having to iterate through the entire physical_device map to find a particular |
| 276 |
// physical device |
| 277 |
std::unordered_map<VkDevice, VkPhysicalDevice> device_to_physical_device_map; |
| 278 |
|
| 279 |
#if defined(WIN32) |
| 280 |
BUILDER_VALUE(LUID, adapterLUID) |
| 281 |
#endif // defined(WIN32) |
| 282 |
}; |
| 283 |
|
| 284 |
using GetTestICDFunc = TestICD* (*)(); |
| 285 |
#define GET_TEST_ICD_FUNC_STR "get_test_icd_func" |
| 286 |
|
| 287 |
using GetNewTestICDFunc = TestICD* (*)(); |
| 288 |
#define RESET_ICD_FUNC_STR "reset_icd_func" |