| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/private/base/SkMacros.h" |
| 9 |
#include "src/gpu/vk/VulkanInterface.h" |
| 10 |
#include "tools/gpu/vk/VkTestMemoryAllocator.h" |
| 11 |
#include "tools/gpu/vk/VkTestUtils.h" |
| 12 |
|
| 13 |
#ifdef SK_VULKAN |
| 14 |
|
| 15 |
#ifndef SK_GPU_TOOLS_VK_LIBRARY_NAME |
| 16 |
#if defined _WIN32 |
| 17 |
#define SK_GPU_TOOLS_VK_LIBRARY_NAME vulkan-1.dll |
| 18 |
#elif defined SK_BUILD_FOR_MAC |
| 19 |
#define SK_GPU_TOOLS_VK_LIBRARY_NAME libvk_swiftshader.dylib |
| 20 |
#else |
| 21 |
#define SK_GPU_TOOLS_VK_LIBRARY_NAME libvulkan.so |
| 22 |
#define SK_GPU_TOOLS_VK_LIBRARY_NAME_BACKUP libvulkan.so.1 |
| 23 |
#endif |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
|
| 28 |
#if defined(__GLIBC__) |
| 29 |
#include <execinfo.h> |
| 30 |
#endif |
| 31 |
#include "include/gpu/vk/VulkanBackendContext.h" |
| 32 |
#include "include/gpu/vk/VulkanExtensions.h" |
| 33 |
#include "src/base/SkAutoMalloc.h" |
| 34 |
#include "tools/library/LoadDynamicLibrary.h" |
| 35 |
|
| 36 |
#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 37 |
#include <sanitizer/lsan_interface.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
using namespace skia_private; |
| 41 |
|
| 42 |
namespace sk_gpu_test { |
| 43 |
|
| 44 |
bool LoadVkLibraryAndGetProcAddrFuncs(PFN_vkGetInstanceProcAddr* instProc) { |
| 45 |
static void* vkLib = nullptr; |
| 46 |
static PFN_vkGetInstanceProcAddr localInstProc = nullptr; |
| 47 |
if (!vkLib) { |
| 48 |
vkLib = SkLoadDynamicLibrary(SK_MACRO_STRINGIFY(SK_GPU_TOOLS_VK_LIBRARY_NAME)); |
| 49 |
if (!vkLib) { |
| 50 |
// vulkaninfo tries to load the library from two places, so we do as well |
| 51 |
// https://github.com/KhronosGroup/Vulkan-Tools/blob/078d44e4664b7efa0b6c96ebced1995c4425d57a/vulkaninfo/vulkaninfo.h#L249 |
| 52 |
#ifdef SK_GPU_TOOLS_VK_LIBRARY_NAME_BACKUP |
| 53 |
vkLib = SkLoadDynamicLibrary(SK_MACRO_STRINGIFY(SK_GPU_TOOLS_VK_LIBRARY_NAME_BACKUP)); |
| 54 |
if (!vkLib) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
#else |
| 58 |
return false; |
| 59 |
#endif |
| 60 |
} |
| 61 |
localInstProc = (PFN_vkGetInstanceProcAddr) SkGetProcedureAddress(vkLib, |
| 62 |
"vkGetInstanceProcAddr"); |
| 63 |
} |
| 64 |
if (!localInstProc) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
*instProc = localInstProc; |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
//////////////////////////////////////////////////////////////////////////////// |
| 72 |
// Helper code to set up Vulkan context objects |
| 73 |
|
| 74 |
#ifdef SK_ENABLE_VK_LAYERS |
| 75 |
const char* kDebugLayerNames[] = { |
| 76 |
// single merged layer |
| 77 |
"VK_LAYER_KHRONOS_validation", |
| 78 |
// not included in standard_validation |
| 79 |
//"VK_LAYER_LUNARG_api_dump", |
| 80 |
//"VK_LAYER_LUNARG_vktrace", |
| 81 |
//"VK_LAYER_LUNARG_screenshot", |
| 82 |
}; |
| 83 |
|
| 84 |
static uint32_t remove_patch_version(uint32_t specVersion) { |
| 85 |
return (specVersion >> 12) << 12; |
| 86 |
} |
| 87 |
|
| 88 |
// Returns the index into layers array for the layer we want. Returns -1 if not supported. |
| 89 |
static int should_include_debug_layer(const char* layerName, |
| 90 |
uint32_t layerCount, VkLayerProperties* layers, |
| 91 |
uint32_t version) { |
| 92 |
for (uint32_t i = 0; i < layerCount; ++i) { |
| 93 |
if (!strcmp(layerName, layers[i].layerName)) { |
| 94 |
// Since the layers intercept the vulkan calls and forward them on, we need to make sure |
| 95 |
// layer was written against a version that isn't older than the version of Vulkan we're |
| 96 |
// using so that it has all the api entry points. |
| 97 |
if (version <= remove_patch_version(layers[i].specVersion)) { |
| 98 |
return i; |
| 99 |
} |
| 100 |
return -1; |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
static void print_backtrace() { |
| 108 |
#if defined(__GLIBC__) |
| 109 |
void* stack[64]; |
| 110 |
int count = backtrace(stack, std::size(stack)); |
| 111 |
backtrace_symbols_fd(stack, count, 2); |
| 112 |
#else |
| 113 |
// Please add implementations for other platforms. |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
VKAPI_ATTR VkBool32 VKAPI_CALL |
| 118 |
DebugUtilsMessenger(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, |
| 119 |
VkDebugUtilsMessageTypeFlagsEXT messageTypes, |
| 120 |
const VkDebugUtilsMessengerCallbackDataEXT* callbackData, |
| 121 |
void* userData) { |
| 122 |
// VUID-VkDebugUtilsMessengerCallbackDataEXT-pMessage-parameter |
| 123 |
// pMessage must be a null-terminated UTF-8 string |
| 124 |
SkASSERT(callbackData->pMessage != nullptr); |
| 125 |
|
| 126 |
static constexpr const char* kSkippedMessages[] = { |
| 127 |
"Nothing for now, this string works around msvc bug with empty array", |
| 128 |
}; |
| 129 |
|
| 130 |
// See if it's an issue we are aware of and don't want to be spammed about. |
| 131 |
// Always report the debug message if message ID is missing |
| 132 |
if (callbackData->pMessageIdName != nullptr) { |
| 133 |
for (const char* skipped : kSkippedMessages) { |
| 134 |
if (strstr(callbackData->pMessageIdName, skipped) != nullptr) { |
| 135 |
return VK_FALSE; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
bool printStackTrace = true; |
| 141 |
bool fail = false; |
| 142 |
|
| 143 |
const char* severity = "message"; |
| 144 |
if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) { |
| 145 |
severity = "error"; |
| 146 |
fail = true; |
| 147 |
} else if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) { |
| 148 |
severity = "warning"; |
| 149 |
} else if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) { |
| 150 |
severity = "info"; |
| 151 |
printStackTrace = false; |
| 152 |
} |
| 153 |
|
| 154 |
std::string type; |
| 155 |
if ((messageTypes & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) != 0) { |
| 156 |
type += " <general>"; |
| 157 |
} |
| 158 |
if ((messageTypes & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0) { |
| 159 |
type += " <validation>"; |
| 160 |
} |
| 161 |
if ((messageTypes & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) { |
| 162 |
type += " <performance>"; |
| 163 |
} |
| 164 |
|
| 165 |
SkDebugf("Vulkan %s%s [%s]: %s\n", |
| 166 |
severity, |
| 167 |
type.c_str(), |
| 168 |
callbackData->pMessageIdName ? callbackData->pMessageIdName : "<no id>", |
| 169 |
callbackData->pMessage); |
| 170 |
|
| 171 |
if (printStackTrace) { |
| 172 |
print_backtrace(); |
| 173 |
} |
| 174 |
|
| 175 |
if (fail) { |
| 176 |
SkDEBUGFAIL("Vulkan debug layer error"); |
| 177 |
} |
| 178 |
|
| 179 |
return VK_FALSE; |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
#define ACQUIRE_VK_INST_PROC_LOCAL(name, instance) \ |
| 184 |
PFN_vk##name grVk##name = \ |
| 185 |
reinterpret_cast<PFN_vk##name>(getInstProc(instance, "vk" #name)); \ |
| 186 |
do { \ |
| 187 |
if (grVk##name == nullptr) { \ |
| 188 |
SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 189 |
return false; \ |
| 190 |
} \ |
| 191 |
} while (0) |
| 192 |
|
| 193 |
// Returns the index into layers array for the layer we want. Returns -1 if not supported. |
| 194 |
static bool should_include_extension(const char* extensionName) { |
| 195 |
const char* kValidExtensions[] = { |
| 196 |
// single merged layer |
| 197 |
VK_ARM_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXTENSION_NAME, |
| 198 |
VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, |
| 199 |
VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, |
| 200 |
VK_EXT_DEBUG_UTILS_EXTENSION_NAME, |
| 201 |
VK_EXT_DEVICE_FAULT_EXTENSION_NAME, |
| 202 |
VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME, |
| 203 |
VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME, |
| 204 |
VK_EXT_FRAME_BOUNDARY_EXTENSION_NAME, |
| 205 |
VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME, |
| 206 |
VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME, |
| 207 |
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, |
| 208 |
VK_EXT_LAYER_SETTINGS_EXTENSION_NAME, |
| 209 |
VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, |
| 210 |
VK_EXT_PIPELINE_CREATION_CACHE_CONTROL_EXTENSION_NAME, |
| 211 |
VK_EXT_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXTENSION_NAME, |
| 212 |
VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME, |
| 213 |
VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME, |
| 214 |
VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, |
| 215 |
VK_KHR_COPY_COMMANDS_2_EXTENSION_NAME, |
| 216 |
VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, |
| 217 |
VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME, |
| 218 |
VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, |
| 219 |
VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, |
| 220 |
VK_KHR_FORMAT_FEATURE_FLAGS_2_EXTENSION_NAME, |
| 221 |
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, |
| 222 |
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, |
| 223 |
VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, |
| 224 |
VK_KHR_MAINTENANCE1_EXTENSION_NAME, |
| 225 |
VK_KHR_MAINTENANCE2_EXTENSION_NAME, |
| 226 |
VK_KHR_MAINTENANCE3_EXTENSION_NAME, |
| 227 |
VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME, |
| 228 |
VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, |
| 229 |
VK_KHR_SURFACE_EXTENSION_NAME, |
| 230 |
VK_KHR_SWAPCHAIN_EXTENSION_NAME, |
| 231 |
// Below are all platform specific extensions. The name macros like we use above are |
| 232 |
// all defined in platform specific vulkan headers. We currently don't include these |
| 233 |
// headers as they are a little bit of a pain (e.g. windows headers requires including |
| 234 |
// <windows.h> which causes all sorts of fun annoyances/problems. So instead we are |
| 235 |
// just listing the strings these macros are defined to. This really shouldn't cause |
| 236 |
// any long term issues as the chances of the strings connected to the name macros |
| 237 |
// changing is next to zero. |
| 238 |
"VK_KHR_win32_surface", // VK_KHR_WIN32_SURFACE_EXTENSION_NAME |
| 239 |
"VK_KHR_xcb_surface", // VK_KHR_XCB_SURFACE_EXTENSION_NAME, |
| 240 |
"VK_ANDROID_external_memory_android_hardware_buffer", |
| 241 |
// VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, |
| 242 |
"VK_KHR_android_surface", // VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 243 |
}; |
| 244 |
|
| 245 |
for (size_t i = 0; i < std::size(kValidExtensions); ++i) { |
| 246 |
if (!strcmp(extensionName, kValidExtensions[i])) { |
| 247 |
return true; |
| 248 |
} |
| 249 |
} |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
static bool init_instance_extensions_and_layers(PFN_vkGetInstanceProcAddr getInstProc, |
| 254 |
uint32_t specVersion, |
| 255 |
TArray<VkExtensionProperties>* instanceExtensions, |
| 256 |
TArray<VkLayerProperties>* instanceLayers) { |
| 257 |
if (getInstProc == nullptr) { |
| 258 |
return false; |
| 259 |
} |
| 260 |
|
| 261 |
ACQUIRE_VK_INST_PROC_LOCAL(EnumerateInstanceExtensionProperties, VK_NULL_HANDLE); |
| 262 |
ACQUIRE_VK_INST_PROC_LOCAL(EnumerateInstanceLayerProperties, VK_NULL_HANDLE); |
| 263 |
|
| 264 |
VkResult res; |
| 265 |
uint32_t layerCount = 0; |
| 266 |
#ifdef SK_ENABLE_VK_LAYERS |
| 267 |
// instance layers |
| 268 |
res = grVkEnumerateInstanceLayerProperties(&layerCount, nullptr); |
| 269 |
if (VK_SUCCESS != res) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
VkLayerProperties* layers = new VkLayerProperties[layerCount]; |
| 273 |
res = grVkEnumerateInstanceLayerProperties(&layerCount, layers); |
| 274 |
if (VK_SUCCESS != res) { |
| 275 |
delete[] layers; |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
uint32_t nonPatchVersion = remove_patch_version(specVersion); |
| 280 |
for (size_t i = 0; i < std::size(kDebugLayerNames); ++i) { |
| 281 |
int idx = should_include_debug_layer(kDebugLayerNames[i], layerCount, layers, |
| 282 |
nonPatchVersion); |
| 283 |
if (idx != -1) { |
| 284 |
instanceLayers->push_back() = layers[idx]; |
| 285 |
} |
| 286 |
} |
| 287 |
delete[] layers; |
| 288 |
#endif |
| 289 |
|
| 290 |
// instance extensions |
| 291 |
// via Vulkan implementation and implicitly enabled layers |
| 292 |
{ |
| 293 |
uint32_t extensionCount = 0; |
| 294 |
res = grVkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); |
| 295 |
if (VK_SUCCESS != res) { |
| 296 |
return false; |
| 297 |
} |
| 298 |
VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount]; |
| 299 |
res = grVkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions); |
| 300 |
if (VK_SUCCESS != res) { |
| 301 |
delete[] extensions; |
| 302 |
return false; |
| 303 |
} |
| 304 |
for (uint32_t i = 0; i < extensionCount; ++i) { |
| 305 |
if (should_include_extension(extensions[i].extensionName)) { |
| 306 |
instanceExtensions->push_back() = extensions[i]; |
| 307 |
} |
| 308 |
} |
| 309 |
delete [] extensions; |
| 310 |
} |
| 311 |
|
| 312 |
// via explicitly enabled layers |
| 313 |
layerCount = instanceLayers->size(); |
| 314 |
for (uint32_t layerIndex = 0; layerIndex < layerCount; ++layerIndex) { |
| 315 |
uint32_t extensionCount = 0; |
| 316 |
res = grVkEnumerateInstanceExtensionProperties((*instanceLayers)[layerIndex].layerName, |
| 317 |
&extensionCount, nullptr); |
| 318 |
if (VK_SUCCESS != res) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount]; |
| 322 |
res = grVkEnumerateInstanceExtensionProperties((*instanceLayers)[layerIndex].layerName, |
| 323 |
&extensionCount, extensions); |
| 324 |
if (VK_SUCCESS != res) { |
| 325 |
delete[] extensions; |
| 326 |
return false; |
| 327 |
} |
| 328 |
for (uint32_t i = 0; i < extensionCount; ++i) { |
| 329 |
if (should_include_extension(extensions[i].extensionName)) { |
| 330 |
instanceExtensions->push_back() = extensions[i]; |
| 331 |
} |
| 332 |
} |
| 333 |
delete[] extensions; |
| 334 |
} |
| 335 |
|
| 336 |
return true; |
| 337 |
} |
| 338 |
|
| 339 |
#define GET_PROC_LOCAL(F, inst, device) PFN_vk ## F F = (PFN_vk ## F) getProc("vk" #F, inst, device) |
| 340 |
|
| 341 |
static bool init_device_extensions_and_layers(const skgpu::VulkanGetProc& getProc, |
| 342 |
uint32_t specVersion, VkInstance inst, |
| 343 |
VkPhysicalDevice physDev, |
| 344 |
TArray<VkExtensionProperties>* deviceExtensions, |
| 345 |
TArray<VkLayerProperties>* deviceLayers) { |
| 346 |
if (getProc == nullptr) { |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
GET_PROC_LOCAL(EnumerateDeviceExtensionProperties, inst, VK_NULL_HANDLE); |
| 351 |
GET_PROC_LOCAL(EnumerateDeviceLayerProperties, inst, VK_NULL_HANDLE); |
| 352 |
|
| 353 |
if (!EnumerateDeviceExtensionProperties || |
| 354 |
!EnumerateDeviceLayerProperties) { |
| 355 |
return false; |
| 356 |
} |
| 357 |
|
| 358 |
VkResult res; |
| 359 |
// device layers |
| 360 |
uint32_t layerCount = 0; |
| 361 |
#ifdef SK_ENABLE_VK_LAYERS |
| 362 |
res = EnumerateDeviceLayerProperties(physDev, &layerCount, nullptr); |
| 363 |
if (VK_SUCCESS != res) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
VkLayerProperties* layers = new VkLayerProperties[layerCount]; |
| 367 |
res = EnumerateDeviceLayerProperties(physDev, &layerCount, layers); |
| 368 |
if (VK_SUCCESS != res) { |
| 369 |
delete[] layers; |
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
uint32_t nonPatchVersion = remove_patch_version(specVersion); |
| 374 |
for (size_t i = 0; i < std::size(kDebugLayerNames); ++i) { |
| 375 |
int idx = should_include_debug_layer(kDebugLayerNames[i], layerCount, layers, |
| 376 |
nonPatchVersion); |
| 377 |
if (idx != -1) { |
| 378 |
deviceLayers->push_back() = layers[idx]; |
| 379 |
} |
| 380 |
} |
| 381 |
delete[] layers; |
| 382 |
#endif |
| 383 |
|
| 384 |
// device extensions |
| 385 |
// via Vulkan implementation and implicitly enabled layers |
| 386 |
{ |
| 387 |
uint32_t extensionCount = 0; |
| 388 |
res = EnumerateDeviceExtensionProperties(physDev, nullptr, &extensionCount, nullptr); |
| 389 |
if (VK_SUCCESS != res) { |
| 390 |
return false; |
| 391 |
} |
| 392 |
VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount]; |
| 393 |
res = EnumerateDeviceExtensionProperties(physDev, nullptr, &extensionCount, extensions); |
| 394 |
if (VK_SUCCESS != res) { |
| 395 |
delete[] extensions; |
| 396 |
return false; |
| 397 |
} |
| 398 |
for (uint32_t i = 0; i < extensionCount; ++i) { |
| 399 |
if (should_include_extension(extensions[i].extensionName)) { |
| 400 |
deviceExtensions->push_back() = extensions[i]; |
| 401 |
} |
| 402 |
} |
| 403 |
delete[] extensions; |
| 404 |
} |
| 405 |
|
| 406 |
// via explicitly enabled layers |
| 407 |
layerCount = deviceLayers->size(); |
| 408 |
for (uint32_t layerIndex = 0; layerIndex < layerCount; ++layerIndex) { |
| 409 |
uint32_t extensionCount = 0; |
| 410 |
res = EnumerateDeviceExtensionProperties(physDev, |
| 411 |
(*deviceLayers)[layerIndex].layerName, |
| 412 |
&extensionCount, nullptr); |
| 413 |
if (VK_SUCCESS != res) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount]; |
| 417 |
res = EnumerateDeviceExtensionProperties(physDev, |
| 418 |
(*deviceLayers)[layerIndex].layerName, |
| 419 |
&extensionCount, extensions); |
| 420 |
if (VK_SUCCESS != res) { |
| 421 |
delete[] extensions; |
| 422 |
return false; |
| 423 |
} |
| 424 |
for (uint32_t i = 0; i < extensionCount; ++i) { |
| 425 |
if (should_include_extension(extensions[i].extensionName)) { |
| 426 |
deviceExtensions->push_back() = extensions[i]; |
| 427 |
} |
| 428 |
} |
| 429 |
delete[] extensions; |
| 430 |
} |
| 431 |
|
| 432 |
return true; |
| 433 |
} |
| 434 |
|
| 435 |
#define ACQUIRE_VK_INST_PROC_NOCHECK(name, instance) \ |
| 436 |
PFN_vk##name grVk##name = reinterpret_cast<PFN_vk##name>(getInstProc(instance, "vk" #name)) |
| 437 |
|
| 438 |
#define ACQUIRE_VK_INST_PROC(name, instance) \ |
| 439 |
PFN_vk##name grVk##name = reinterpret_cast<PFN_vk##name>(getInstProc(instance, "vk" #name)); \ |
| 440 |
do { \ |
| 441 |
if (grVk##name == nullptr) { \ |
| 442 |
SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 443 |
if (inst != VK_NULL_HANDLE) { \ |
| 444 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); \ |
| 445 |
} \ |
| 446 |
return false; \ |
| 447 |
} \ |
| 448 |
} while (0) |
| 449 |
|
| 450 |
#define ACQUIRE_VK_PROC_NOCHECK(name, instance, device) \ |
| 451 |
PFN_vk##name grVk##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)) |
| 452 |
|
| 453 |
#define ACQUIRE_VK_PROC(name, instance, device) \ |
| 454 |
PFN_vk##name grVk##name = \ |
| 455 |
reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \ |
| 456 |
do { \ |
| 457 |
if (grVk##name == nullptr) { \ |
| 458 |
SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 459 |
if (inst != VK_NULL_HANDLE) { \ |
| 460 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); \ |
| 461 |
} \ |
| 462 |
return false; \ |
| 463 |
} \ |
| 464 |
} while (0) |
| 465 |
|
| 466 |
#define ACQUIRE_VK_PROC_LOCAL(name, instance, device) \ |
| 467 |
PFN_vk##name grVk##name = \ |
| 468 |
reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \ |
| 469 |
do { \ |
| 470 |
if (grVk##name == nullptr) { \ |
| 471 |
SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 472 |
return false; \ |
| 473 |
} \ |
| 474 |
} while (0) |
| 475 |
|
| 476 |
static bool destroy_instance(PFN_vkGetInstanceProcAddr getInstProc, |
| 477 |
VkInstance inst, |
| 478 |
VkDebugUtilsMessengerEXT* debugMessenger, |
| 479 |
bool hasDebugExtension) { |
| 480 |
if (hasDebugExtension && *debugMessenger != VK_NULL_HANDLE) { |
| 481 |
ACQUIRE_VK_INST_PROC_LOCAL(DestroyDebugUtilsMessengerEXT, inst); |
| 482 |
grVkDestroyDebugUtilsMessengerEXT(inst, *debugMessenger, nullptr); |
| 483 |
*debugMessenger = VK_NULL_HANDLE; |
| 484 |
} |
| 485 |
ACQUIRE_VK_INST_PROC_LOCAL(DestroyInstance, inst); |
| 486 |
grVkDestroyInstance(inst, nullptr); |
| 487 |
return true; |
| 488 |
} |
| 489 |
|
| 490 |
static bool setup_features(const skgpu::VulkanGetProc& getProc, |
| 491 |
VkInstance inst, |
| 492 |
VkPhysicalDevice physDev, |
| 493 |
uint32_t physDeviceVersion, |
| 494 |
const TArray<VkExtensionProperties>& deviceExtensions, |
| 495 |
TestVkFeatures& testVkFeatures, |
| 496 |
bool isProtected) { |
| 497 |
SkASSERT(physDeviceVersion >= VK_API_VERSION_1_1); |
| 498 |
|
| 499 |
testVkFeatures.deviceFeatures = {}; |
| 500 |
testVkFeatures.deviceFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 501 |
|
| 502 |
// Add any features that are needed by tests before calling skiaFeatures->addFeaturesToQuery. |
| 503 |
|
| 504 |
void** tailPNext = &testVkFeatures.deviceFeatures.pNext; |
| 505 |
|
| 506 |
// If |isProtected| is given, attach that first |
| 507 |
testVkFeatures.protectedMemoryFeatures = {}; |
| 508 |
if (isProtected) { |
| 509 |
testVkFeatures.protectedMemoryFeatures.sType = |
| 510 |
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES; |
| 511 |
*tailPNext = &testVkFeatures.protectedMemoryFeatures; |
| 512 |
tailPNext = &testVkFeatures.protectedMemoryFeatures.pNext; |
| 513 |
} |
| 514 |
|
| 515 |
testVkFeatures.skiaFeatures.addFeaturesToQuery( |
| 516 |
deviceExtensions.begin(), deviceExtensions.size(), testVkFeatures.deviceFeatures); |
| 517 |
|
| 518 |
ACQUIRE_VK_PROC_LOCAL(GetPhysicalDeviceFeatures2, inst, VK_NULL_HANDLE); |
| 519 |
grVkGetPhysicalDeviceFeatures2(physDev, &testVkFeatures.deviceFeatures); |
| 520 |
|
| 521 |
// Robustness has adverse effect on performance on a few GPUs, and besides we can't depend on it |
| 522 |
// on all platforms. |
| 523 |
testVkFeatures.deviceFeatures.features.robustBufferAccess = VK_FALSE; |
| 524 |
|
| 525 |
// If we want to disable any extension features do so here. |
| 526 |
|
| 527 |
if (isProtected) { |
| 528 |
if (!testVkFeatures.protectedMemoryFeatures.protectedMemory) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
} |
| 532 |
return true; |
| 533 |
} |
| 534 |
|
| 535 |
bool CreateVkBackendContext(PFN_vkGetInstanceProcAddr getInstProc, |
| 536 |
skgpu::VulkanBackendContext* ctx, |
| 537 |
skgpu::VulkanExtensions* extensions, |
| 538 |
TestVkFeatures* testVkFeatures, |
| 539 |
VkDebugUtilsMessengerEXT* debugMessenger, |
| 540 |
uint32_t* presentQueueIndexPtr, |
| 541 |
const CanPresentFn& canPresent, |
| 542 |
bool isProtected) { |
| 543 |
VkResult err; |
| 544 |
|
| 545 |
ACQUIRE_VK_INST_PROC_NOCHECK(EnumerateInstanceVersion, VK_NULL_HANDLE); |
| 546 |
uint32_t instanceVersion = 0; |
| 547 |
// Vulkan 1.1 is required, so vkEnumerateInstanceVersion should always be available. |
| 548 |
SkASSERT(grVkEnumerateInstanceVersion != nullptr); |
| 549 |
err = grVkEnumerateInstanceVersion(&instanceVersion); |
| 550 |
if (err) { |
| 551 |
SkDebugf("failed to enumerate instance version. Err: %d\n", err); |
| 552 |
return false; |
| 553 |
} |
| 554 |
SkASSERT(instanceVersion >= VK_API_VERSION_1_1); |
| 555 |
// We can set the apiVersion to be whatever the highest api we may use in skia. For now we |
| 556 |
// set it to 1.1 since that is the most common Vulkan version on Android devices. |
| 557 |
const uint32_t apiVersion = VK_API_VERSION_1_1; |
| 558 |
|
| 559 |
instanceVersion = std::min(instanceVersion, apiVersion); |
| 560 |
|
| 561 |
STArray<2, VkPhysicalDevice> physDevs; |
| 562 |
VkDevice device; |
| 563 |
VkInstance inst = VK_NULL_HANDLE; |
| 564 |
|
| 565 |
const VkApplicationInfo app_info = { |
| 566 |
VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType |
| 567 |
nullptr, // pNext |
| 568 |
"vktest", // pApplicationName |
| 569 |
0, // applicationVersion |
| 570 |
"vktest", // pEngineName |
| 571 |
0, // engineVerison |
| 572 |
apiVersion, // apiVersion |
| 573 |
}; |
| 574 |
|
| 575 |
TArray<VkLayerProperties> instanceLayers; |
| 576 |
TArray<VkExtensionProperties> instanceExtensions; |
| 577 |
|
| 578 |
if (!init_instance_extensions_and_layers(getInstProc, instanceVersion, |
| 579 |
&instanceExtensions, |
| 580 |
&instanceLayers)) { |
| 581 |
return false; |
| 582 |
} |
| 583 |
|
| 584 |
TArray<const char*> instanceLayerNames; |
| 585 |
std::vector<const char*> instanceExtensionNames; |
| 586 |
for (int i = 0; i < instanceLayers.size(); ++i) { |
| 587 |
instanceLayerNames.push_back(instanceLayers[i].layerName); |
| 588 |
} |
| 589 |
for (int i = 0; i < instanceExtensions.size(); ++i) { |
| 590 |
instanceExtensionNames.push_back(instanceExtensions[i].extensionName); |
| 591 |
} |
| 592 |
|
| 593 |
testVkFeatures->skiaFeatures.init(apiVersion); |
| 594 |
testVkFeatures->skiaFeatures.addToInstanceExtensions( |
| 595 |
instanceExtensions.begin(), instanceExtensions.size(), instanceExtensionNames); |
| 596 |
|
| 597 |
VkInstanceCreateInfo instance_create = { |
| 598 |
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType |
| 599 |
nullptr, // pNext |
| 600 |
0, // flags |
| 601 |
&app_info, // pApplicationInfo |
| 602 |
(uint32_t)instanceLayerNames.size(), // enabledLayerNameCount |
| 603 |
instanceLayerNames.begin(), // ppEnabledLayerNames |
| 604 |
(uint32_t)instanceExtensionNames.size(), // enabledExtensionNameCount |
| 605 |
instanceExtensionNames.data(), // ppEnabledExtensionNames |
| 606 |
}; |
| 607 |
|
| 608 |
bool hasDebugExtension = false; |
| 609 |
*debugMessenger = VK_NULL_HANDLE; |
| 610 |
|
| 611 |
#ifdef SK_ENABLE_VK_LAYERS |
| 612 |
bool hasLayerSettingsExt = false; |
| 613 |
for (size_t i = 0; i < instanceExtensionNames.size() |
| 614 |
&& !hasDebugExtension && !hasLayerSettingsExt; ++i) { |
| 615 |
if (!strcmp(instanceExtensionNames[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) { |
| 616 |
hasDebugExtension = true; |
| 617 |
} else if (!strcmp(instanceExtensionNames[i], VK_EXT_LAYER_SETTINGS_EXTENSION_NAME)) { |
| 618 |
hasLayerSettingsExt = true; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
// Fine grain control of validation layer features |
| 623 |
const char* name = "VK_LAYER_KHRONOS_validation"; |
| 624 |
const VkBool32 settingValidateCore = VK_TRUE; |
| 625 |
// Syncval is disabled for now, but would be useful to enable eventually. |
| 626 |
const VkBool32 settingValidateSync = VK_FALSE; |
| 627 |
const VkBool32 settingThreadSafety = VK_TRUE; |
| 628 |
// Shader validation could be useful (previously broken on Android, might already be fixed: |
| 629 |
// http://anglebug.com/42265520). |
| 630 |
const VkBool32 settingCheckShaders = VK_FALSE; |
| 631 |
// If syncval is enabled, submit time validation could stay disabled due to performance issues: |
| 632 |
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/7285 |
| 633 |
const VkBool32 settingSyncvalSubmitTimeValidation = VK_FALSE; |
| 634 |
// Extra properties in syncval make it easier to filter the messages. |
| 635 |
const VkBool32 settingSyncvalMessageExtraProperties = VK_TRUE; |
| 636 |
const VkLayerSettingEXT layerSettings[] = { |
| 637 |
{name, "validate_core", VK_LAYER_SETTING_TYPE_BOOL32_EXT, 1, &settingValidateCore}, |
| 638 |
{name, "validate_sync", VK_LAYER_SETTING_TYPE_BOOL32_EXT, 1, &settingValidateSync}, |
| 639 |
{name, "thread_safety", VK_LAYER_SETTING_TYPE_BOOL32_EXT, 1, &settingThreadSafety}, |
| 640 |
{name, "check_shaders", VK_LAYER_SETTING_TYPE_BOOL32_EXT, 1, &settingCheckShaders}, |
| 641 |
{name, |
| 642 |
"syncval_submit_time_validation", |
| 643 |
VK_LAYER_SETTING_TYPE_BOOL32_EXT, |
| 644 |
1, |
| 645 |
&settingSyncvalSubmitTimeValidation}, |
| 646 |
{name, |
| 647 |
"syncval_message_extra_properties", |
| 648 |
VK_LAYER_SETTING_TYPE_BOOL32_EXT, |
| 649 |
1, |
| 650 |
&settingSyncvalMessageExtraProperties}, |
| 651 |
}; |
| 652 |
VkLayerSettingsCreateInfoEXT layerSettingsCreateInfo = {}; |
| 653 |
layerSettingsCreateInfo.sType = VK_STRUCTURE_TYPE_LAYER_SETTINGS_CREATE_INFO_EXT; |
| 654 |
layerSettingsCreateInfo.settingCount = static_cast<uint32_t>(std::size(layerSettings)); |
| 655 |
layerSettingsCreateInfo.pSettings = layerSettings; |
| 656 |
if (hasDebugExtension && hasLayerSettingsExt) { |
| 657 |
instance_create.pNext = &layerSettingsCreateInfo; |
| 658 |
} |
| 659 |
#endif |
| 660 |
|
| 661 |
ACQUIRE_VK_INST_PROC(CreateInstance, VK_NULL_HANDLE); |
| 662 |
err = grVkCreateInstance(&instance_create, nullptr, &inst); |
| 663 |
if (err < 0) { |
| 664 |
SkDebugf("vkCreateInstance failed: %d\n", err); |
| 665 |
return false; |
| 666 |
} |
| 667 |
|
| 668 |
ACQUIRE_VK_INST_PROC(GetDeviceProcAddr, inst); |
| 669 |
auto getProc = [getInstProc, grVkGetDeviceProcAddr](const char* proc_name, |
| 670 |
VkInstance instance, VkDevice device) { |
| 671 |
if (device != VK_NULL_HANDLE) { |
| 672 |
return grVkGetDeviceProcAddr(device, proc_name); |
| 673 |
} |
| 674 |
return getInstProc(instance, proc_name); |
| 675 |
}; |
| 676 |
|
| 677 |
#ifdef SK_ENABLE_VK_LAYERS |
| 678 |
if (hasDebugExtension) { |
| 679 |
VkDebugUtilsMessengerCreateInfoEXT messengerInfo = {}; |
| 680 |
|
| 681 |
constexpr VkDebugUtilsMessageSeverityFlagsEXT kSeveritiesToLog = |
| 682 |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | |
| 683 |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; |
| 684 |
|
| 685 |
constexpr VkDebugUtilsMessageTypeFlagsEXT kMessagesToLog = |
| 686 |
VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | |
| 687 |
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | |
| 688 |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; |
| 689 |
|
| 690 |
messengerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; |
| 691 |
messengerInfo.messageSeverity = kSeveritiesToLog; |
| 692 |
messengerInfo.messageType = kMessagesToLog; |
| 693 |
messengerInfo.pfnUserCallback = &DebugUtilsMessenger; |
| 694 |
|
| 695 |
ACQUIRE_VK_PROC(CreateDebugUtilsMessengerEXT, inst, VK_NULL_HANDLE); |
| 696 |
// Register the callback |
| 697 |
grVkCreateDebugUtilsMessengerEXT(inst, &messengerInfo, nullptr, debugMessenger); |
| 698 |
} |
| 699 |
#endif |
| 700 |
|
| 701 |
ACQUIRE_VK_PROC(EnumeratePhysicalDevices, inst, VK_NULL_HANDLE); |
| 702 |
ACQUIRE_VK_PROC(GetPhysicalDeviceProperties, inst, VK_NULL_HANDLE); |
| 703 |
ACQUIRE_VK_PROC(GetPhysicalDeviceQueueFamilyProperties, inst, VK_NULL_HANDLE); |
| 704 |
ACQUIRE_VK_PROC(CreateDevice, inst, VK_NULL_HANDLE); |
| 705 |
ACQUIRE_VK_PROC(GetDeviceQueue, inst, VK_NULL_HANDLE); |
| 706 |
ACQUIRE_VK_PROC(DeviceWaitIdle, inst, VK_NULL_HANDLE); |
| 707 |
ACQUIRE_VK_PROC(DestroyDevice, inst, VK_NULL_HANDLE); |
| 708 |
|
| 709 |
uint32_t gpuCount; |
| 710 |
err = grVkEnumeratePhysicalDevices(inst, &gpuCount, nullptr); |
| 711 |
if (err) { |
| 712 |
SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err); |
| 713 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 714 |
return false; |
| 715 |
} |
| 716 |
if (!gpuCount) { |
| 717 |
SkDebugf("vkEnumeratePhysicalDevices returned no supported devices.\n"); |
| 718 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 719 |
return false; |
| 720 |
} |
| 721 |
// Allocate enough storage for all available physical devices. We should be able to just ask for |
| 722 |
// the first one, but a bug in RenderDoc (https://github.com/baldurk/renderdoc/issues/2766) |
| 723 |
// will smash the stack if we do that. |
| 724 |
physDevs.resize(gpuCount); |
| 725 |
err = grVkEnumeratePhysicalDevices(inst, &gpuCount, physDevs.data()); |
| 726 |
if (err) { |
| 727 |
SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err); |
| 728 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 729 |
return false; |
| 730 |
} |
| 731 |
// We just use the first physical device. |
| 732 |
// TODO: find best match for our needs |
| 733 |
VkPhysicalDevice physDev = physDevs.front(); |
| 734 |
|
| 735 |
VkPhysicalDeviceProperties physDeviceProperties; |
| 736 |
grVkGetPhysicalDeviceProperties(physDev, &physDeviceProperties); |
| 737 |
uint32_t physDeviceVersion = std::min(physDeviceProperties.apiVersion, apiVersion); |
| 738 |
|
| 739 |
// query to get the initial queue props size |
| 740 |
uint32_t queueCount; |
| 741 |
grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, nullptr); |
| 742 |
if (!queueCount) { |
| 743 |
SkDebugf("vkGetPhysicalDeviceQueueFamilyProperties returned no queues.\n"); |
| 744 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 745 |
return false; |
| 746 |
} |
| 747 |
|
| 748 |
SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties)); |
| 749 |
// now get the actual queue props |
| 750 |
VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAlloc.get(); |
| 751 |
|
| 752 |
grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, queueProps); |
| 753 |
|
| 754 |
// iterate to find the graphics queue |
| 755 |
uint32_t graphicsQueueIndex = queueCount; |
| 756 |
for (uint32_t i = 0; i < queueCount; i++) { |
| 757 |
if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
| 758 |
graphicsQueueIndex = i; |
| 759 |
break; |
| 760 |
} |
| 761 |
} |
| 762 |
if (graphicsQueueIndex == queueCount) { |
| 763 |
SkDebugf("Could not find any supported graphics queues.\n"); |
| 764 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 765 |
return false; |
| 766 |
} |
| 767 |
|
| 768 |
// iterate to find the present queue, if needed |
| 769 |
uint32_t presentQueueIndex = queueCount; |
| 770 |
if (presentQueueIndexPtr && canPresent) { |
| 771 |
for (uint32_t i = 0; i < queueCount; i++) { |
| 772 |
if (canPresent(inst, physDev, i)) { |
| 773 |
presentQueueIndex = i; |
| 774 |
break; |
| 775 |
} |
| 776 |
} |
| 777 |
if (presentQueueIndex == queueCount) { |
| 778 |
SkDebugf("Could not find any supported present queues.\n"); |
| 779 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 780 |
return false; |
| 781 |
} |
| 782 |
*presentQueueIndexPtr = presentQueueIndex; |
| 783 |
} else { |
| 784 |
// Just setting this so we end up make a single queue for graphics since there was no |
| 785 |
// request for a present queue. |
| 786 |
presentQueueIndex = graphicsQueueIndex; |
| 787 |
} |
| 788 |
|
| 789 |
TArray<VkLayerProperties> deviceLayers; |
| 790 |
TArray<VkExtensionProperties> deviceExtensions; |
| 791 |
if (!init_device_extensions_and_layers(getProc, physDeviceVersion, |
| 792 |
inst, physDev, |
| 793 |
&deviceExtensions, |
| 794 |
&deviceLayers)) { |
| 795 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
TArray<const char*> deviceLayerNames; |
| 800 |
std::vector<const char*> deviceExtensionNames; |
| 801 |
for (int i = 0; i < deviceLayers.size(); ++i) { |
| 802 |
deviceLayerNames.push_back(deviceLayers[i].layerName); |
| 803 |
} |
| 804 |
|
| 805 |
for (int i = 0; i < deviceExtensions.size(); ++i) { |
| 806 |
deviceExtensionNames.push_back(deviceExtensions[i].extensionName); |
| 807 |
} |
| 808 |
|
| 809 |
// Note: Any struct that setup_features chains must stay in scope until vkCreateDevice. This is |
| 810 |
// why these structs are located in TestVkFeatures and passed in this function. |
| 811 |
if (!setup_features(getProc, |
| 812 |
inst, |
| 813 |
physDev, |
| 814 |
physDeviceVersion, |
| 815 |
deviceExtensions, |
| 816 |
*testVkFeatures, |
| 817 |
isProtected)) { |
| 818 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
// Enable features and extensions that are desired by Skia. This _adds_ to features and |
| 823 |
// extensions already enabled in deviceExtensionNames and features that aren't done by |
| 824 |
// skiaFeatures itself. |
| 825 |
testVkFeatures->skiaFeatures.addFeaturesToEnable(deviceExtensionNames, |
| 826 |
testVkFeatures->deviceFeatures); |
| 827 |
|
| 828 |
extensions->init(getProc, |
| 829 |
inst, |
| 830 |
physDev, |
| 831 |
(uint32_t)instanceExtensionNames.size(), |
| 832 |
instanceExtensionNames.data(), |
| 833 |
(uint32_t)deviceExtensionNames.size(), |
| 834 |
deviceExtensionNames.data()); |
| 835 |
|
| 836 |
VkDeviceQueueCreateFlags flags = isProtected ? VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT : 0; |
| 837 |
float queuePriorities[1] = { 0.0 }; |
| 838 |
// Here we assume no need for swapchain queue |
| 839 |
// If one is needed, the client will need its own setup code |
| 840 |
const VkDeviceQueueCreateInfo queueInfo[2] = { |
| 841 |
{ |
| 842 |
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 843 |
nullptr, // pNext |
| 844 |
flags, // VkDeviceQueueCreateFlags |
| 845 |
graphicsQueueIndex, // queueFamilyIndex |
| 846 |
1, // queueCount |
| 847 |
queuePriorities, // pQueuePriorities |
| 848 |
|
| 849 |
}, |
| 850 |
{ |
| 851 |
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 852 |
nullptr, // pNext |
| 853 |
0, // VkDeviceQueueCreateFlags |
| 854 |
presentQueueIndex, // queueFamilyIndex |
| 855 |
1, // queueCount |
| 856 |
queuePriorities, // pQueuePriorities |
| 857 |
} |
| 858 |
}; |
| 859 |
uint32_t queueInfoCount = (presentQueueIndex != graphicsQueueIndex) ? 2 : 1; |
| 860 |
|
| 861 |
const VkDeviceCreateInfo deviceInfo = { |
| 862 |
VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType |
| 863 |
&testVkFeatures->deviceFeatures, // pNext |
| 864 |
0, // VkDeviceCreateFlags |
| 865 |
queueInfoCount, // queueCreateInfoCount |
| 866 |
queueInfo, // pQueueCreateInfos |
| 867 |
(uint32_t) deviceLayerNames.size(), // layerCount |
| 868 |
deviceLayerNames.begin(), // ppEnabledLayerNames |
| 869 |
(uint32_t) deviceExtensionNames.size(), // extensionCount |
| 870 |
deviceExtensionNames.data(), // ppEnabledExtensionNames |
| 871 |
nullptr, // ppEnabledFeatures |
| 872 |
}; |
| 873 |
|
| 874 |
{ |
| 875 |
#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 876 |
// skbug.com/40040003 |
| 877 |
__lsan::ScopedDisabler lsanDisabler; |
| 878 |
#endif |
| 879 |
err = grVkCreateDevice(physDev, &deviceInfo, nullptr, &device); |
| 880 |
} |
| 881 |
if (err) { |
| 882 |
SkDebugf("CreateDevice failed: %d\n", err); |
| 883 |
destroy_instance(getInstProc, inst, debugMessenger, hasDebugExtension); |
| 884 |
return false; |
| 885 |
} |
| 886 |
|
| 887 |
VkQueue queue; |
| 888 |
if (isProtected) { |
| 889 |
ACQUIRE_VK_PROC(GetDeviceQueue2, inst, device); |
| 890 |
SkASSERT(grVkGetDeviceQueue2 != nullptr); |
| 891 |
VkDeviceQueueInfo2 queue_info2 = { |
| 892 |
VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2, // sType |
| 893 |
nullptr, // pNext |
| 894 |
VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT, // flags |
| 895 |
graphicsQueueIndex, // queueFamilyIndex |
| 896 |
0 // queueIndex |
| 897 |
}; |
| 898 |
grVkGetDeviceQueue2(device, &queue_info2, &queue); |
| 899 |
} else { |
| 900 |
grVkGetDeviceQueue(device, graphicsQueueIndex, 0, &queue); |
| 901 |
} |
| 902 |
|
| 903 |
skgpu::VulkanInterface interface = skgpu::VulkanInterface( |
| 904 |
getProc, inst, device, instanceVersion, physDeviceVersion, extensions); |
| 905 |
SkASSERT(interface.validate(instanceVersion, physDeviceVersion, extensions)); |
| 906 |
|
| 907 |
sk_sp<skgpu::VulkanMemoryAllocator> memoryAllocator = VkTestMemoryAllocator::Make( |
| 908 |
inst, physDev, device, physDeviceVersion, extensions, &interface); |
| 909 |
|
| 910 |
ctx->fInstance = inst; |
| 911 |
ctx->fPhysicalDevice = physDev; |
| 912 |
ctx->fDevice = device; |
| 913 |
ctx->fQueue = queue; |
| 914 |
ctx->fGraphicsQueueIndex = graphicsQueueIndex; |
| 915 |
ctx->fMaxAPIVersion = apiVersion; |
| 916 |
ctx->fVkExtensions = extensions; |
| 917 |
ctx->fDeviceFeatures2 = &testVkFeatures->deviceFeatures; |
| 918 |
ctx->fGetProc = getProc; |
| 919 |
ctx->fProtectedContext = skgpu::Protected(isProtected); |
| 920 |
ctx->fMemoryAllocator = memoryAllocator; |
| 921 |
|
| 922 |
return true; |
| 923 |
} |
| 924 |
|
| 925 |
} // namespace sk_gpu_test |
| 926 |
|
| 927 |
#endif |