blob: 79ec520c14f2152fd1b498eb2c8d88f8fd87aae3 [file] [log] [blame]
Greg Daniel35970ec2017-11-10 10:03:05 -05001/*
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 "VkTestUtils.h"
9
10#ifdef SK_VULKAN
11
Greg Danielf730c182018-07-02 20:15:37 +000012#include "SkAutoMalloc.h"
13#include "vk/GrVkBackendContext.h"
14#include "vk/GrVkExtensions.h"
Greg Daniel35970ec2017-11-10 10:03:05 -050015#include "../ports/SkOSLibrary.h"
16
17namespace sk_gpu_test {
18
19bool LoadVkLibraryAndGetProcAddrFuncs(PFN_vkGetInstanceProcAddr* instProc,
Greg Danield3e65aa2018-08-01 09:19:45 -040020 PFN_vkGetDeviceProcAddr* devProc) {
Chris Dalton3a67b8e2018-05-03 09:30:29 -060021#ifdef SK_MOLTENVK
22 // MoltenVK is a statically linked framework, so there is no Vulkan library to load.
23 *instProc = &vkGetInstanceProcAddr;
24 *devProc = &vkGetDeviceProcAddr;
25 return true;
26#else
Greg Daniel35970ec2017-11-10 10:03:05 -050027 static void* vkLib = nullptr;
28 static PFN_vkGetInstanceProcAddr localInstProc = nullptr;
29 static PFN_vkGetDeviceProcAddr localDevProc = nullptr;
30 if (!vkLib) {
31#if defined _WIN32
32 vkLib = DynamicLoadLibrary("vulkan-1.dll");
33#else
34 vkLib = DynamicLoadLibrary("libvulkan.so");
35#endif
36 if (!vkLib) {
37 return false;
38 }
39 localInstProc = (PFN_vkGetInstanceProcAddr) GetProcedureAddress(vkLib,
40 "vkGetInstanceProcAddr");
41 localDevProc = (PFN_vkGetDeviceProcAddr) GetProcedureAddress(vkLib,
42 "vkGetDeviceProcAddr");
43 }
44 if (!localInstProc || !localDevProc) {
45 return false;
46 }
47 *instProc = localInstProc;
48 *devProc = localDevProc;
49 return true;
Chris Dalton3a67b8e2018-05-03 09:30:29 -060050#endif
Greg Daniel35970ec2017-11-10 10:03:05 -050051}
Greg Danielf730c182018-07-02 20:15:37 +000052
53////////////////////////////////////////////////////////////////////////////////
54// Helper code to set up Vulkan context objects
55
56#ifdef SK_ENABLE_VK_LAYERS
57const char* kDebugLayerNames[] = {
58 // elements of VK_LAYER_LUNARG_standard_validation
59 "VK_LAYER_GOOGLE_threading",
60 "VK_LAYER_LUNARG_parameter_validation",
61 "VK_LAYER_LUNARG_object_tracker",
62 "VK_LAYER_LUNARG_image",
63 "VK_LAYER_LUNARG_core_validation",
64 "VK_LAYER_LUNARG_swapchain",
65 "VK_LAYER_GOOGLE_unique_objects",
66 // not included in standard_validation
67 //"VK_LAYER_LUNARG_api_dump",
68 //"VK_LAYER_LUNARG_vktrace",
69 //"VK_LAYER_LUNARG_screenshot",
70};
71#endif
72
73// the minimum version of Vulkan supported
74#ifdef SK_BUILD_FOR_ANDROID
75const uint32_t kGrVkMinimumVersion = VK_MAKE_VERSION(1, 0, 3);
76#else
77const uint32_t kGrVkMinimumVersion = VK_MAKE_VERSION(1, 0, 8);
78#endif
79
80#define ACQUIRE_VK_PROC(name, instance, device) \
81 PFN_vk##name grVk##name = \
82 reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \
83 if (grVk##name == nullptr) { \
84 SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \
Greg Daniel37329b32018-07-02 20:16:44 +000085 if (device != VK_NULL_HANDLE) { \
86 destroy_instance(getProc, inst, debugCallback, hasDebugExtension); \
87 } \
Greg Danielf730c182018-07-02 20:15:37 +000088 return false; \
89 }
90
Greg Daniel37329b32018-07-02 20:16:44 +000091#ifdef SK_ENABLE_VK_LAYERS
92VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(
93 VkDebugReportFlagsEXT flags,
94 VkDebugReportObjectTypeEXT objectType,
95 uint64_t object,
96 size_t location,
97 int32_t messageCode,
98 const char* pLayerPrefix,
99 const char* pMessage,
100 void* pUserData) {
101 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
102 SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
103 return VK_TRUE; // skip further layers
104 } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
105 SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
106 } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
107 SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
108 } else {
109 SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
110 }
111 return VK_FALSE;
112}
113#endif
114
115#define ACQUIRE_VK_PROC_LOCAL(name, instance, device) \
116 PFN_vk##name grVk##name = \
117 reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \
118 if (grVk##name == nullptr) { \
119 SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \
120 return; \
121 }
122
Greg Danield3e65aa2018-08-01 09:19:45 -0400123static void destroy_instance(GrVkGetProc getProc, VkInstance inst,
Greg Daniel37329b32018-07-02 20:16:44 +0000124 VkDebugReportCallbackEXT* debugCallback,
125 bool hasDebugExtension) {
126 if (hasDebugExtension && *debugCallback != VK_NULL_HANDLE) {
127 ACQUIRE_VK_PROC_LOCAL(DestroyDebugReportCallbackEXT, inst, VK_NULL_HANDLE);
128 grVkDestroyDebugReportCallbackEXT(inst, *debugCallback, nullptr);
129 *debugCallback = VK_NULL_HANDLE;
130 }
131 ACQUIRE_VK_PROC_LOCAL(DestroyInstance, inst, VK_NULL_HANDLE);
132 grVkDestroyInstance(inst, nullptr);
133}
134
Greg Danield3e65aa2018-08-01 09:19:45 -0400135bool CreateVkBackendContext(GrVkGetProc getProc,
Greg Danielf730c182018-07-02 20:15:37 +0000136 GrVkBackendContext* ctx,
Greg Daniel37329b32018-07-02 20:16:44 +0000137 VkDebugReportCallbackEXT* debugCallback,
Greg Danielf730c182018-07-02 20:15:37 +0000138 uint32_t* presentQueueIndexPtr,
139 CanPresentFn canPresent) {
Greg Danielf730c182018-07-02 20:15:37 +0000140 VkPhysicalDevice physDev;
141 VkDevice device;
142 VkInstance inst;
143 VkResult err;
144
145 const VkApplicationInfo app_info = {
146 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType
147 nullptr, // pNext
148 "vktest", // pApplicationName
149 0, // applicationVersion
150 "vktest", // pEngineName
151 0, // engineVerison
152 kGrVkMinimumVersion, // apiVersion
153 };
154
155 GrVkExtensions extensions(getProc);
156 extensions.initInstance(kGrVkMinimumVersion);
157
158 SkTArray<const char*> instanceLayerNames;
159 SkTArray<const char*> instanceExtensionNames;
160 uint32_t extensionFlags = 0;
Greg Daniel37329b32018-07-02 20:16:44 +0000161 bool hasDebugExtension = false;
Greg Danielf730c182018-07-02 20:15:37 +0000162#ifdef SK_ENABLE_VK_LAYERS
163 for (size_t i = 0; i < SK_ARRAY_COUNT(kDebugLayerNames); ++i) {
164 if (extensions.hasInstanceLayer(kDebugLayerNames[i])) {
165 instanceLayerNames.push_back(kDebugLayerNames[i]);
166 }
167 }
168 if (extensions.hasInstanceExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) {
169 instanceExtensionNames.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
170 extensionFlags |= kEXT_debug_report_GrVkExtensionFlag;
Greg Daniel37329b32018-07-02 20:16:44 +0000171 hasDebugExtension = true;
Greg Danielf730c182018-07-02 20:15:37 +0000172 }
173#endif
174
175 if (extensions.hasInstanceExtension(VK_KHR_SURFACE_EXTENSION_NAME)) {
176 instanceExtensionNames.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
177 extensionFlags |= kKHR_surface_GrVkExtensionFlag;
178 }
179 if (extensions.hasInstanceExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
180 instanceExtensionNames.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
181 extensionFlags |= kKHR_swapchain_GrVkExtensionFlag;
182 }
183#ifdef SK_BUILD_FOR_WIN
184 if (extensions.hasInstanceExtension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME)) {
185 instanceExtensionNames.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
186 extensionFlags |= kKHR_win32_surface_GrVkExtensionFlag;
187 }
188#elif defined(SK_BUILD_FOR_ANDROID)
189 if (extensions.hasInstanceExtension(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) {
190 instanceExtensionNames.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
191 extensionFlags |= kKHR_android_surface_GrVkExtensionFlag;
192 }
193#elif defined(SK_BUILD_FOR_UNIX) && !defined(__Fuchsia__)
194 if (extensions.hasInstanceExtension(VK_KHR_XCB_SURFACE_EXTENSION_NAME)) {
195 instanceExtensionNames.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
196 extensionFlags |= kKHR_xcb_surface_GrVkExtensionFlag;
197 }
198#endif
199
200 const VkInstanceCreateInfo instance_create = {
201 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
202 nullptr, // pNext
203 0, // flags
204 &app_info, // pApplicationInfo
205 (uint32_t) instanceLayerNames.count(), // enabledLayerNameCount
206 instanceLayerNames.begin(), // ppEnabledLayerNames
207 (uint32_t) instanceExtensionNames.count(), // enabledExtensionNameCount
208 instanceExtensionNames.begin(), // ppEnabledExtensionNames
209 };
210
211 ACQUIRE_VK_PROC(CreateInstance, VK_NULL_HANDLE, VK_NULL_HANDLE);
212 err = grVkCreateInstance(&instance_create, nullptr, &inst);
213 if (err < 0) {
214 SkDebugf("vkCreateInstance failed: %d\n", err);
215 return false;
216 }
217
Greg Daniel37329b32018-07-02 20:16:44 +0000218#ifdef SK_ENABLE_VK_LAYERS
219 *debugCallback = VK_NULL_HANDLE;
220 for (int i = 0; i < instanceExtensionNames.count() && !hasDebugExtension; ++i) {
221 if (!strcmp(instanceExtensionNames[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) {
222 hasDebugExtension = true;
223 }
224 }
225 if (hasDebugExtension) {
226 // Setup callback creation information
227 VkDebugReportCallbackCreateInfoEXT callbackCreateInfo;
228 callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
229 callbackCreateInfo.pNext = nullptr;
230 callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
231 VK_DEBUG_REPORT_WARNING_BIT_EXT |
232 // VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
233 // VK_DEBUG_REPORT_DEBUG_BIT_EXT |
234 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
235 callbackCreateInfo.pfnCallback = &DebugReportCallback;
236 callbackCreateInfo.pUserData = nullptr;
237
238 ACQUIRE_VK_PROC(CreateDebugReportCallbackEXT, inst, VK_NULL_HANDLE);
239 // Register the callback
240 grVkCreateDebugReportCallbackEXT(inst, &callbackCreateInfo, nullptr, debugCallback);
241 }
242#endif
243
Greg Danielf730c182018-07-02 20:15:37 +0000244 ACQUIRE_VK_PROC(DestroyInstance, inst, VK_NULL_HANDLE);
245 ACQUIRE_VK_PROC(EnumeratePhysicalDevices, inst, VK_NULL_HANDLE);
246 ACQUIRE_VK_PROC(GetPhysicalDeviceQueueFamilyProperties, inst, VK_NULL_HANDLE);
247 ACQUIRE_VK_PROC(GetPhysicalDeviceFeatures, inst, VK_NULL_HANDLE);
248 ACQUIRE_VK_PROC(CreateDevice, inst, VK_NULL_HANDLE);
249 ACQUIRE_VK_PROC(GetDeviceQueue, inst, VK_NULL_HANDLE);
250 ACQUIRE_VK_PROC(DeviceWaitIdle, inst, VK_NULL_HANDLE);
251 ACQUIRE_VK_PROC(DestroyDevice, inst, VK_NULL_HANDLE);
252
253 uint32_t gpuCount;
254 err = grVkEnumeratePhysicalDevices(inst, &gpuCount, nullptr);
255 if (err) {
256 SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err);
Greg Daniel37329b32018-07-02 20:16:44 +0000257 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000258 return false;
259 }
260 if (!gpuCount) {
261 SkDebugf("vkEnumeratePhysicalDevices returned no supported devices.\n");
Greg Daniel37329b32018-07-02 20:16:44 +0000262 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000263 return false;
264 }
265 // Just returning the first physical device instead of getting the whole array.
266 // TODO: find best match for our needs
267 gpuCount = 1;
268 err = grVkEnumeratePhysicalDevices(inst, &gpuCount, &physDev);
269 // VK_INCOMPLETE is returned when the count we provide is less than the total device count.
270 if (err && VK_INCOMPLETE != err) {
271 SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err);
Greg Daniel37329b32018-07-02 20:16:44 +0000272 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000273 return false;
274 }
275
276 // query to get the initial queue props size
277 uint32_t queueCount;
278 grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, nullptr);
279 if (!queueCount) {
280 SkDebugf("vkGetPhysicalDeviceQueueFamilyProperties returned no queues.\n");
Greg Daniel37329b32018-07-02 20:16:44 +0000281 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000282 return false;
283 }
284
285 SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties));
286 // now get the actual queue props
287 VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAlloc.get();
288
289 grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, queueProps);
290
291 // iterate to find the graphics queue
292 uint32_t graphicsQueueIndex = queueCount;
293 for (uint32_t i = 0; i < queueCount; i++) {
294 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
295 graphicsQueueIndex = i;
296 break;
297 }
298 }
299 if (graphicsQueueIndex == queueCount) {
300 SkDebugf("Could not find any supported graphics queues.\n");
Greg Daniel37329b32018-07-02 20:16:44 +0000301 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000302 return false;
303 }
304
305 // iterate to find the present queue, if needed
306 uint32_t presentQueueIndex = queueCount;
307 if (presentQueueIndexPtr && canPresent) {
308 for (uint32_t i = 0; i < queueCount; i++) {
309 if (canPresent(inst, physDev, i)) {
310 presentQueueIndex = i;
311 break;
312 }
313 }
314 if (presentQueueIndex == queueCount) {
315 SkDebugf("Could not find any supported present queues.\n");
Greg Daniel37329b32018-07-02 20:16:44 +0000316 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000317 return false;
318 }
319 *presentQueueIndexPtr = presentQueueIndex;
320 } else {
321 // Just setting this so we end up make a single queue for graphics since there was no
322 // request for a present queue.
323 presentQueueIndex = graphicsQueueIndex;
324 }
325
326 extensions.initDevice(kGrVkMinimumVersion, inst, physDev);
327
328 SkTArray<const char*> deviceLayerNames;
329 SkTArray<const char*> deviceExtensionNames;
Greg Danielf730c182018-07-02 20:15:37 +0000330 if (extensions.hasDeviceExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
331 deviceExtensionNames.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
332 extensionFlags |= kKHR_swapchain_GrVkExtensionFlag;
333 }
334 if (extensions.hasDeviceExtension("VK_NV_glsl_shader")) {
335 deviceExtensionNames.push_back("VK_NV_glsl_shader");
336 extensionFlags |= kNV_glsl_shader_GrVkExtensionFlag;
337 }
338
339 // query to get the physical device properties
340 VkPhysicalDeviceFeatures deviceFeatures;
341 grVkGetPhysicalDeviceFeatures(physDev, &deviceFeatures);
342 // this looks like it would slow things down,
343 // and we can't depend on it on all platforms
344 deviceFeatures.robustBufferAccess = VK_FALSE;
345
346 uint32_t featureFlags = 0;
347 if (deviceFeatures.geometryShader) {
348 featureFlags |= kGeometryShader_GrVkFeatureFlag;
349 }
350 if (deviceFeatures.dualSrcBlend) {
351 featureFlags |= kDualSrcBlend_GrVkFeatureFlag;
352 }
353 if (deviceFeatures.sampleRateShading) {
354 featureFlags |= kSampleRateShading_GrVkFeatureFlag;
355 }
356
357 float queuePriorities[1] = { 0.0 };
358 // Here we assume no need for swapchain queue
359 // If one is needed, the client will need its own setup code
360 const VkDeviceQueueCreateInfo queueInfo[2] = {
361 {
362 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
363 nullptr, // pNext
364 0, // VkDeviceQueueCreateFlags
365 graphicsQueueIndex, // queueFamilyIndex
366 1, // queueCount
367 queuePriorities, // pQueuePriorities
368 },
369 {
370 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
371 nullptr, // pNext
372 0, // VkDeviceQueueCreateFlags
373 presentQueueIndex, // queueFamilyIndex
374 1, // queueCount
375 queuePriorities, // pQueuePriorities
376 }
377 };
378 uint32_t queueInfoCount = (presentQueueIndex != graphicsQueueIndex) ? 2 : 1;
379
380 const VkDeviceCreateInfo deviceInfo = {
381 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
382 nullptr, // pNext
383 0, // VkDeviceCreateFlags
384 queueInfoCount, // queueCreateInfoCount
385 queueInfo, // pQueueCreateInfos
386 (uint32_t) deviceLayerNames.count(), // layerCount
387 deviceLayerNames.begin(), // ppEnabledLayerNames
388 (uint32_t) deviceExtensionNames.count(), // extensionCount
389 deviceExtensionNames.begin(), // ppEnabledExtensionNames
390 &deviceFeatures // ppEnabledFeatures
391 };
392
393 err = grVkCreateDevice(physDev, &deviceInfo, nullptr, &device);
394 if (err) {
395 SkDebugf("CreateDevice failed: %d\n", err);
Greg Daniel37329b32018-07-02 20:16:44 +0000396 destroy_instance(getProc, inst, debugCallback, hasDebugExtension);
Greg Danielf730c182018-07-02 20:15:37 +0000397 return false;
398 }
399
Greg Danielf730c182018-07-02 20:15:37 +0000400 VkQueue queue;
401 grVkGetDeviceQueue(device, graphicsQueueIndex, 0, &queue);
402
403 ctx->fInstance = inst;
404 ctx->fPhysicalDevice = physDev;
405 ctx->fDevice = device;
406 ctx->fQueue = queue;
407 ctx->fGraphicsQueueIndex = graphicsQueueIndex;
408 ctx->fMinAPIVersion = kGrVkMinimumVersion;
409 ctx->fExtensions = extensionFlags;
410 ctx->fFeatures = featureFlags;
Greg Danielc8cd45a2018-07-12 10:02:37 -0400411 ctx->fGetProc = getProc;
Greg Danielf730c182018-07-02 20:15:37 +0000412 ctx->fOwnsInstanceAndDevice = false;
413
414 return true;
Greg Danielf730c182018-07-02 20:15:37 +0000415}
416
Greg Daniel35970ec2017-11-10 10:03:05 -0500417}
418
419#endif