forked from nvpro-samples/gl_vk_threaded_cadscene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvk_initlib.cpp
582 lines (451 loc) · 17.9 KB
/
vk_initlib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*-----------------------------------------------------------------------
Copyright (c) 2014-2016, NVIDIA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of its contributors may be used to endorse
or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------*/
#include <assert.h>
#include <main.h>
#include "nvdrawvulkanimage.h"
#include <vulkan/vulkan.h>
#include "vk_nvx_device_generated_commands.h"
#if VK_EXT_debug_report && defined(_DEBUG)
VKAPI_ATTR VkBool32 VKAPI_CALL vulkanDebugReportCallback(
VkDebugReportFlagsEXT msgFlags,
VkDebugReportObjectTypeEXT objType,
uint64_t object,
size_t location,
int32_t msgCode,
const char* pLayerPrefix,
const char* pMsg,
void* pUserData)
{
const char* messageCodeString = "UNKNOWN";
bool isError = false;
if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)
{
messageCodeString = "INFO";
}
else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT)
{
// We know that we're submitting queues without fences, ignore this warning
if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
return false;
}
messageCodeString = "WARN";
}
else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)
{
messageCodeString = "PERF";
}
else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
{
messageCodeString = "ERROR";
isError = true;
}
else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT)
{
messageCodeString = "DEBUG";
}
char *message = (char *)malloc(strlen(pMsg) + 100);
sprintf(message, "%s: [%s] Code %d : %s", messageCodeString, pLayerPrefix, msgCode, pMsg);
LOGW("%s\n",message);
free(message);
/*
* false indicates that layer should not bail-out of an
* API call that had validation failures. This may mean that the
* app dies inside the driver due to invalid parameter(s).
* That's what would happen without validation layers, so we'll
* keep that behavior here.
*/
return false;
}
// not exactly clean
static PFN_vkCreateDebugReportCallbackEXT p_vkCreateDebugReportCallbackEXT;
static PFN_vkDestroyDebugReportCallbackEXT p_vkDestroyDebugReportCallbackEXT;
static VkDebugReportCallbackEXT s_dbgMsgCallback = NULL;
static void initDebugCallback(VkInstance instance)
{
p_vkCreateDebugReportCallbackEXT =
(PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
instance, "vkCreateDebugReportCallbackEXT");
p_vkDestroyDebugReportCallbackEXT =
(PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
instance, "vkDestroyDebugReportCallbackEXT");
s_dbgMsgCallback = NULL;
if (!p_vkCreateDebugReportCallbackEXT || !p_vkDestroyDebugReportCallbackEXT) {
return;
}
VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
dbgCreateInfo.pNext = NULL;
dbgCreateInfo.pfnCallback = vulkanDebugReportCallback;
dbgCreateInfo.pUserData = NULL;
dbgCreateInfo.flags =
VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
VkResult res = p_vkCreateDebugReportCallbackEXT(instance, &dbgCreateInfo, NULL,
&s_dbgMsgCallback);
assert(res == VK_SUCCESS);
}
void vulkanContextCleanup(VkDevice device, VkPhysicalDevice physicalDevice, VkInstance instance)
{
if (s_dbgMsgCallback){
p_vkDestroyDebugReportCallbackEXT(instance, s_dbgMsgCallback, NULL);
}
}
#else
static void initDebugCallback(VkInstance instance)
{
}
void vulkanContextCleanup(VkDevice device, VkPhysicalDevice physicalDevice, VkInstance instance)
{
}
#endif
bool vulkanInitLibrary()
{
if (!init_NV_draw_vulkan_image(NVPWindow::sysGetProcAddress)) return false;
#if USEVULKANSDK
return true;
#else
if (__nvkglGetVkProcAddrNV){
vkLoadProcs( __nvkglGetVkProcAddrNV );
}
if (pfn_vkCreateDevice != NULL)
return true;
return false;
#endif
}
#if !USEVULKANSDK
bool vulkanCreateContext(VkDevice &device, VkPhysicalDevice& physicalDevice, VkInstance &instance, const char* appTitle, const char* engineName)
{
VkResult result;
VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
VkInstanceCreateInfo instanceInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
VkDeviceQueueCreateInfo queueInfo = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
uint32_t physicalDeviceCount = 1;
VkDeviceCreateInfo devInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
appInfo.pApplicationName = appTitle;
appInfo.pEngineName = engineName;
appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0);
instanceInfo.pApplicationInfo = &appInfo;
#if defined(_DEBUG) && VK_EXT_debug_report
const char* extensions[] = { VK_EXT_DEBUG_REPORT_EXTENSION_NAME };
instanceInfo.enabledExtensionCount = 1;
instanceInfo.ppEnabledExtensionNames = extensions;
#endif
result = vkCreateInstance(&instanceInfo, NULL, &instance);
if (result != VK_SUCCESS) {
return false;
}
result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, &physicalDevice);
if (result != VK_SUCCESS) {
return false;
}
queueInfo.queueFamilyIndex = 0;
queueInfo.queueCount = 1;
devInfo.queueCreateInfoCount = 1;
devInfo.pQueueCreateInfos = &queueInfo;
result = vkCreateDevice(physicalDevice, &devInfo, NULL, &device);
if (result != VK_SUCCESS) {
return false;
}
#if defined(_DEBUG) && VK_EXT_debug_report
initDebugCallback(instance);
#endif
return true;
}
#else
#include <assert.h>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using std::vector;
using std::string;
using std::set;
#define CHECK_VK_RESULT() assert(result == VK_SUCCESS)
std::vector<VkLayerProperties> GetGlobalLayerProperties()
{
VkResult result = VK_ERROR_INITIALIZATION_FAILED;
uint32_t count = 0;
result = vkEnumerateInstanceLayerProperties(&count, NULL);
CHECK_VK_RESULT();
std::vector<VkLayerProperties> layers(count);
result = vkEnumerateInstanceLayerProperties(&count, layers.data());
CHECK_VK_RESULT();
return layers;
}
std::vector<VkExtensionProperties> GetGlobalExtensionProperties()
{
VkResult result = VK_ERROR_INITIALIZATION_FAILED;
uint32_t count = 0;
result = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
CHECK_VK_RESULT();
std::vector<VkExtensionProperties> extensions(count);
result = vkEnumerateInstanceExtensionProperties(NULL, &count, extensions.data());
CHECK_VK_RESULT();
return extensions;
}
std::vector<VkExtensionProperties> GetPhysicalDeviceExtensionProperties(VkPhysicalDevice physicalDevice)
{
VkResult result = VK_ERROR_INITIALIZATION_FAILED;
uint32_t count = 0;
result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, NULL);
CHECK_VK_RESULT();
std::vector<VkExtensionProperties> extensions(count);
result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, extensions.data());
CHECK_VK_RESULT();
return extensions;
}
std::vector<VkLayerProperties> GetPhysicalDeviceLayerProperties(VkPhysicalDevice physicalDevice)
{
VkResult result = VK_ERROR_INITIALIZATION_FAILED;
uint32_t count = 0;
result = vkEnumerateDeviceLayerProperties(physicalDevice, &count, NULL);
CHECK_VK_RESULT();
std::vector<VkLayerProperties> layers(count);
result = vkEnumerateDeviceLayerProperties(physicalDevice, &count, layers.data());
CHECK_VK_RESULT();
return layers;
}
// TODO, put this elsewhere
#ifdef NDEBUG
#define USE_VALIDATION 0
#else
// disable for now, opengl context interaction breaks validation layers
// need clean vulkan-only version of sample to make use of it
#define USE_VALIDATION 0
#endif
std::vector<std::string> requestedLayers(bool global)
{
vector<string> layers;
#if USE_VALIDATION
//layers.push_back("VK_LAYER_LUNARG_api_dump");
//layers.push_back("VK_LAYER_LUNARG_draw_state"); // doesn't like non-SPIR-V shader
//layers.push_back("VK_LAYER_LUNARG_param_checker"); // crashing in SDK 1.0.2
//layers.push_back("VK_LAYER_LUNARG_device_limits");
//layers.push_back("VK_LAYER_LUNARG_image");
//layers.push_back("VK_LAYER_LUNARG_object_tracker");
//layers.push_back("VK_LAYER_LUNARG_mem_tracker");
//layers.push_back("VK_LAYER_LUNARG_threading");
#endif
return layers;
}
std::vector<std::string> requestedExtensions(bool global)
{
vector<string> extensions;
if (global)
{
#ifdef _DEBUG
extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
#endif
}
else
{
extensions.push_back(VK_NV_GLSL_SHADER_EXTENSION_NAME);
extensions.push_back(VK_NVX_DEVICE_GENERATED_COMMANDS_EXTENSION_NAME);
}
return extensions;
}
std::vector<std::string> filterLayers(const std::vector<VkLayerProperties>& supportedLayers, const std::vector<std::string>& layersToEnable)
{
vector<string> supportedLayerNames;
for (size_t i = 0; i < layersToEnable.size(); ++i)
{
for (size_t j = 0; j < supportedLayers.size(); ++j)
{
if (layersToEnable[i] == supportedLayers[j].layerName)
{
supportedLayerNames.push_back(layersToEnable[i]);
break;
}
}
}
return supportedLayerNames;
}
std::vector<std::string> filterExtensions(const std::vector<VkExtensionProperties>& supportedExtensions, const std::vector<std::string>& extensionsToEnable)
{
vector<string> supportedExtensionNames;
for (size_t i = 0; i < extensionsToEnable.size(); ++i)
{
for (size_t j = 0; j < supportedExtensions.size(); ++j)
{
if (extensionsToEnable[i] == supportedExtensions[j].extensionName)
{
supportedExtensionNames.push_back(extensionsToEnable[i]);
break;
}
}
}
return supportedExtensionNames;
}
bool vulkanCreateContext(VkDevice &device, VkPhysicalDevice& physicalDevice, VkInstance &instance, const char* appTitle, const char* engineName)
{
VkResult result;
VkApplicationInfo applicationInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
applicationInfo.pApplicationName = appTitle;
applicationInfo.pEngineName = engineName;
applicationInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0);
// global layers and extensions
const vector<VkLayerProperties> globalLayerProperties = GetGlobalLayerProperties();
const vector<VkExtensionProperties> globalExtentionsProperties = GetGlobalExtensionProperties();
const vector<string> globalLayersToEnable = filterLayers(globalLayerProperties, requestedLayers(true));
vector<const char*> globalLayerNames;
for (size_t i = 0; i < globalLayersToEnable.size(); ++i)
globalLayerNames.push_back(globalLayersToEnable[i].c_str());
const vector<string> globlalExtensionsToEnable = filterExtensions(globalExtentionsProperties, requestedExtensions(true));
bool useDebugExt = false;
vector<const char*> globalExtensionNames;
for (size_t i = 0; i < globlalExtensionsToEnable.size(); ++i){
globalExtensionNames.push_back(globlalExtensionsToEnable[i].c_str());
if ( strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, globlalExtensionsToEnable[i].c_str()) == 0 ){
useDebugExt = true;
}
}
VkInstanceCreateInfo instanceCreateInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
instanceCreateInfo.pApplicationInfo = &applicationInfo;
instanceCreateInfo.enabledExtensionCount = (uint32_t)globalExtensionNames.size();
instanceCreateInfo.ppEnabledExtensionNames = globalExtensionNames.data();
instanceCreateInfo.enabledLayerCount = (uint32_t)globalLayerNames.size();
instanceCreateInfo.ppEnabledLayerNames = globalLayerNames.data();
result = vkCreateInstance(&instanceCreateInfo, NULL, &instance);
if (result != VK_SUCCESS) {
return false;
}
if (useDebugExt){
initDebugCallback(instance);
}
uint32_t physicalDeviceCount = 0;
result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, NULL);
if (result != VK_SUCCESS) {
return false;
}
if (physicalDeviceCount == 0) {
LOGW("could not find Vulkan device")
return false;
}
std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
if (result != VK_SUCCESS) {
return false;
}
// pick first device
physicalDevice = physicalDevices[0];
std::vector<VkDeviceQueueCreateInfo> queues;
std::vector<float> priorites;
uint32_t queueFamilyGraphicsCompute = ~0u;
{
uint32_t queueFamilyPropertiesCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyPropertiesCount, NULL);
std::vector<VkQueueFamilyProperties> queueFamilyProperties(queueFamilyPropertiesCount);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyPropertiesCount, queueFamilyProperties.data());
for (uint32_t i = 0; i < queueFamilyProperties.size(); ++i) {
if (queueFamilyProperties[i].queueFlags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT)) {
queueFamilyGraphicsCompute = i;
}
if (queueFamilyProperties[i].queueCount > priorites.size()){
priorites.resize(queueFamilyProperties[i].queueCount, 1.0f);
}
}
for (uint32_t i = 0; i < queueFamilyProperties.size(); ++i) {
VkDeviceQueueCreateInfo queueInfo = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
queueInfo.queueFamilyIndex = i;
queueInfo.queueCount = queueFamilyProperties[i].queueCount;
queueInfo.pQueuePriorities = priorites.data();
queues.push_back(queueInfo);
}
}
if (queueFamilyGraphicsCompute == ~0u) {
LOGW("could not find queue that supports graphics and compute")
return false;
}
// allow all queues
VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
deviceCreateInfo.queueCreateInfoCount = (uint32_t)queues.size();
deviceCreateInfo.pQueueCreateInfos = queues.data();
// physical device layers and extensions
const vector<VkLayerProperties> physicalDeviceLayerProperties = GetPhysicalDeviceLayerProperties(physicalDevice);
const vector<VkExtensionProperties> physicalDeviceExtentionsProperties = GetPhysicalDeviceExtensionProperties(physicalDevice);
const vector<string> physicalDeviceLayersToEnable = filterLayers(physicalDeviceLayerProperties, requestedLayers(false));
vector<const char*> physicalDeviceLayerNames;
for (size_t i = 0; i < physicalDeviceLayersToEnable.size(); ++i)
physicalDeviceLayerNames.push_back(physicalDeviceLayersToEnable[i].c_str());
const vector<string> physicalDeviceExtensionsToEnable = filterExtensions(physicalDeviceExtentionsProperties, requestedExtensions(false));
vector<const char*> physicalDeviceExtensionNames;
for (size_t i = 0; i < physicalDeviceExtensionsToEnable.size(); ++i)
physicalDeviceExtensionNames.push_back(physicalDeviceExtensionsToEnable[i].c_str());
deviceCreateInfo.enabledExtensionCount = (uint32_t)physicalDeviceExtensionNames.size();
deviceCreateInfo.ppEnabledExtensionNames = physicalDeviceExtensionNames.data();
deviceCreateInfo.enabledLayerCount = (uint32_t)physicalDeviceLayerNames.size();
deviceCreateInfo.ppEnabledLayerNames = physicalDeviceLayerNames.data();
VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceFeatures( physicalDevice, &deviceFeatures );
deviceFeatures.robustBufferAccess = 0;
deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
result = vkCreateDevice(physicalDevice, &deviceCreateInfo, NULL, &device);
if (result != VK_SUCCESS) {
return false;
}
return true;
}
bool vulkanIsExtensionSupported(const char* name)
{
VkResult result;
VkInstance instance;
VkInstanceCreateInfo instanceCreateInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
result = vkCreateInstance(&instanceCreateInfo, NULL, &instance);
if (result != VK_SUCCESS) {
return false;
}
uint32_t physicalDeviceCount = 0;
result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, NULL);
if (result != VK_SUCCESS || physicalDeviceCount == 0) {
return false;
}
std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
if (result != VK_SUCCESS) {
vkDestroyInstance(instance, NULL);
return false;
}
// pick first device
VkPhysicalDevice physicalDevice = physicalDevices[0];
uint32_t count = 0;
result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, NULL);
if (result != VK_SUCCESS) {
vkDestroyInstance(instance, NULL);
return false;
}
std::vector<VkExtensionProperties> extensions(count);
result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &count, extensions.data());
if (result != VK_SUCCESS) {
vkDestroyInstance(instance, NULL);
return false;
}
bool found = false;
for (uint32_t i = 0; i < count; i++){
if (strcmp(extensions[i].extensionName, name) == 0){
found = true;
break;
}
}
vkDestroyInstance(instance, NULL);
return found;
}
#endif