-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_mini.c
1482 lines (1282 loc) · 45.6 KB
/
gpu_mini.c
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gpu_mini.h"
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
struct similarity_t
{
int index1;
int index2;
};
// NOTE: returns 1 if not different
// returns 0 if different
#define predicate_t int(*)(void* a, void* b)
// NOTE: returns 1 if memcmp(a, b, elementSize) == 0
// returns 0 otherwise
// NOTE: for consistency with am_predicate_not_memcmp
#define predicate_memcmp NULL
// NOTE: returns 1 if memcmp(a, b, elementSize) != 0
// returns 0 otherwise
static int predicate_not_memcmp(void* a, void* b)
{
fputs("error: predicate_not_memcmp should not be called (see comment in array_mini.c)\n", stderr);
return 0;
}
static int predicate2(int elementSize, int(*predicate)(void*, void*), void* element, void* query)
{
if(predicate == NULL)
{
return memcmp(element, query, elementSize) == 0 ? 1 : 0;
}
else if(predicate == predicate_not_memcmp)
{
return memcmp(element, query, elementSize) != 0 ? 1 : 0;
}
else
{
return predicate(element, query);
}
}
// NOTE: assumes..
// .. numElements > 0
// .. index != NULL
// NOTE: int(*predicate)(void* element, void* query)
static int search_first_in(int elementSize, int(*predicate)(void*, void*), int numElements, void* elements, void* query, int* index)
{
for(int i = 0; i < numElements; ++i)
{
void* element = elements + elementSize * i;
int bAreNotDifferent;
if(predicate == NULL)
{
bAreNotDifferent = memcmp(element, query, elementSize) == 0 ? 1 : 0;
}
else if (predicate == predicate_not_memcmp)
{
bAreNotDifferent = memcmp(element, query, elementSize) != 0 ? 1 : 0;
}
else
{
bAreNotDifferent = predicate(element, query);
}
if(bAreNotDifferent == 1)
{
*index = i;
return 1;
}
}
return 0;
}
//#define search_first_in2(predicate, numElements, elements, query, index) search_first_in(sizeof *elements, (predicate_t)predicate, numElements, (void*)elements, (void*)query, index)
// NOTE: assumes..
// .. numElements > 0
// NOTE: int(*predicate)(void* element, void* query)
static int is_in(int elementSize, int(*predicate)(void*, void*), int numElements, void* elements, void* query)
{
int a;
return search_first_in(elementSize, predicate, numElements, elements, query, &a);
}
#define is_in2(predicate, numElements, elements, query) is_in(sizeof *elements, (predicate_t)predicate, numElements, (void*)elements, (void*)query)
// NOTE: assumes..
// .. numElementsToAppend > 0
static void add_or_append_elements(int elementSize, int* numElements, void** elements, int numElementsToAppendOrAdd, void* elementsToAppendOrAdd)
{
void* a = *elements;
//*elements = (void*)new char[elementSize * ((*numElements) + numElementsToAppendOrAdd)];
*elements = malloc(elementSize * ((*numElements) + numElementsToAppendOrAdd));
if(*numElements > 0)
{
memcpy(*elements, a, elementSize * (*numElements));
//delete a;
free(a);
}
memcpy(*elements + (elementSize * (*numElements)), elementsToAppendOrAdd, elementSize * numElementsToAppendOrAdd);
*numElements += numElementsToAppendOrAdd;
}
#define add_or_append_element(elementSize, numElements, elements, elementToAppendOrAdd) add_or_append_elements(elementSize, numElements, elements, 1, elementToAppendOrAdd)
#define add_or_append_element2(numElements, elements, elementToAppendOrAdd) add_or_append_element(sizeof **elements, numElements, (void**)elements, (void*)elementToAppendOrAdd)
// NOTE: uniques within one array
// NOTE: assumes..
// .. numElements > 0
// .. numUniques != NULL
// .. lengthof uniques == numElements
// NOTE: int(*predicate)(void* element, void* query)
void get_uniques(int elementSize, int(*predicate)(void*, void*), int numElements, void* elements, int* numUniques, int** indexPerUnique)
{
*numUniques = 0;
for(int i = 0; i < numElements; ++i)
{
void* element1 = elements + elementSize * i;
int bIsDuplicate = 0;
for(int j = 0; j < numElements; ++j)
{
if(i == j)
{
continue;
}
void* element2 = elements + elementSize * j;
if(predicate2(elementSize, predicate, element1, element2) == 1)
{
bIsDuplicate = 1;
break;
}
}
if(bIsDuplicate == 1)
{
continue;
}
(*indexPerUnique)[*numUniques] = i;
++(*numUniques);
}
}
#define get_uniques2(predicate, numElements, elements, numUniques, indexPerUnique) get_uniques(sizeof *elements, (predicate_t)predicate, numElements, (void*)elements, numUniques, indexPerUnique)
// NOTE: assumes..
// .. numElements > 0
// NOTE: int(*predicate)(void* element, void* query)
void move_to_back(int elementSize, int(*predicate)(void*, void*), int numElements, void** elements, void* query, int* numElementsMovedToBack)
{
*numElementsMovedToBack = 0;
for(int i = 0; i < numElements - *numElementsMovedToBack;)
{
void* element1 = *elements + elementSize * i;
int bMoveToBack1 = predicate2(elementSize, predicate, element1, query);
if(bMoveToBack1 == 1)
{
int indexToNextElementToNotMoveToBack = -1;
for(int j = i + 1; j < numElements - *numElementsMovedToBack; ++j)
{
void* element2 = *elements + elementSize * j;
int bMoveToBack2 = predicate2(elementSize, predicate, element2, query);
if(bMoveToBack2 == 0)
{
indexToNextElementToNotMoveToBack = j;
break;
}
}
if(indexToNextElementToNotMoveToBack != -1)
{
int numElementsToMoveToBack = indexToNextElementToNotMoveToBack - i;
char elementsToMoveToBack[elementSize * numElementsToMoveToBack]; //< 0 length array is not officially allowed in C, but guaranteed that numElementsToMoveToBack is >= 1 here
/// backup front elements
memcpy(elementsToMoveToBack, *elements + elementSize * i, elementSize * numElementsToMoveToBack);
int lastNumElementsToMoveToFront = numElements - indexToNextElementToNotMoveToBack;
// move back elements to front
memcpy(*elements + elementSize * i, *elements + elementSize * indexToNextElementToNotMoveToBack, elementSize * lastNumElementsToMoveToFront);
// move front elements from backup to back
memcpy(*elements + elementSize * (i + lastNumElementsToMoveToFront), elementsToMoveToBack, elementSize * numElementsToMoveToBack);
*numElementsMovedToBack += numElementsToMoveToBack;
}
else
{
int numElementsToMoveBack = (numElements - *numElementsMovedToBack) - i;
// ^
// because if i == 0.. numElementsToMoveToBack should ==..
// .. numElements - *numElementsMovedToBack
// ~don't actually move these elements as already at back~
// ^
// do move these back (but not to back as already at back)..
// .. to assure order is preserved
int lastNumElementsToMoveFront = *numElementsMovedToBack;
if(lastNumElementsToMoveFront > 0)
{
char elementsToMoveBack[elementSize * numElementsToMoveBack]; //< 0 length array is not officially allowed in C, but guaranteed that numElementsToMoveToBack is >= 1 here
memcpy(elementsToMoveBack, *elements + elementSize * i, elementSize * numElementsToMoveBack);
memcpy(*elements + elementSize * i, *elements + elementSize * (i + numElementsToMoveBack), elementSize * lastNumElementsToMoveFront);
memcpy(*elements + elementSize * (i + lastNumElementsToMoveFront), elementsToMoveBack, elementSize * numElementsToMoveBack);
}
*numElementsMovedToBack += numElementsToMoveBack;
}
}
else
{
++i;
}
}
}
#define move_to_back2(predicate, numElements, elements, query, numElementsMovedToBack) move_to_back(sizeof **elements, (predicate_t)predicate, numElements, (void**)elements, (void*)query, numElementsMovedToBack)
//*****************************************************************************
int predicate$char$(char* a, char* b)
{
return strcmp(a, b) == 0 ? 1 : 0;
}
//*****************************************************************************
static void(*on_print)(char* a, FILE* b) = NULL;
#define on_print2(a, b) if(on_print != NULL) { on_print(a, b); }
void on_printf(FILE* a, char* b, ...)
{
va_list c;
va_start(c, b);
int d = vsnprintf(NULL, 0, b, c);
char e[d + 1];
vsprintf(e, b, c);
va_end(c);
on_print(e, a);
}
#define on_printf2(a, b, ...) if(on_print != NULL) { on_printf(a, b __VA_OPT__(,) __VA_ARGS__); }
void gm_set_on_print(void(*a)(char*, FILE*))
{
on_print = a;
}
void gm_unset_on_print()
{
on_print = NULL;
}
//*****************************************************************************
// vulkan
//*****************************************************************************
typedef struct
{
VkStructureType sType;
const void* pNext;
} VkStructure;
static const VkDebugUtilsMessengerCreateInfoEXT VK_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
.pNext = NULL,
.flags = 0,
.pUserData = NULL
};
static const VkApplicationInfo VK_APPLICATION_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = NULL,
.pApplicationName = NULL,
.applicationVersion = 0,
.pEngineName = NULL,
.engineVersion = 0
};
static const VkInstanceCreateInfo VK_INSTANCE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.pApplicationInfo = NULL,
.enabledLayerCount = 0,
.ppEnabledLayerNames = NULL,
.enabledExtensionCount = 0,
.ppEnabledExtensionNames = NULL
};
static const VkPhysicalDeviceFeatures VK_PHYSICAL_DEVICE_FEATURES_DEFAULT = {};
static const VkDeviceQueueCreateInfo VK_DEVICE_QUEUE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkDeviceCreateInfo VK_DEVICE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.enabledLayerCount = 0,
.ppEnabledLayerNames = NULL,
.enabledExtensionCount = 0,
.ppEnabledExtensionNames = NULL,
.pEnabledFeatures = NULL
};
#if defined(_WIN32)
static const VkWin32SurfaceCreateInfoKHR VK_WIN32_SURFACE_CREATE_INFO_KHR_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
.pNext = NULL,
.flags = 0
};
#else //< #elif defined(__linux__)
static const VkXlibSurfaceCreateInfoKHR VK_XLIB_SURFACE_CREATE_INFO_KHR_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
.pNext = NULL,
.flags = 0
};
#endif
static const VkSwapchainCreateInfoKHR VK_SWAPCHAIN_CREATE_INFO_KHR_WINDOW_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.pNext = NULL,
.flags = 0,
.imageArrayLayers = 1,
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
//.queueFamilyIndexCount = 0,
//.pQueueFamilyIndices= NULL,
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
.clipped = VK_TRUE,
.oldSwapchain = VK_NULL_HANDLE
};
static const VkSwapchainCreateInfoKHR VK_SWAPCHAIN_CREATE_INFO_KHR_VR_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.pNext = NULL,
.flags = 0,
.imageArrayLayers = 2,
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
//.queueFamilyIndexCount = 0,
//.pQueueFamilyIndices = NULL,
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
.clipped = VK_FALSE,
.oldSwapchain = VK_NULL_HANDLE
};
static const VkComponentMapping VK_COMPONENT_MAPPING_DEFAULT = {
.r = VK_COMPONENT_SWIZZLE_IDENTITY,
.g = VK_COMPONENT_SWIZZLE_IDENTITY,
.b = VK_COMPONENT_SWIZZLE_IDENTITY,
.a = VK_COMPONENT_SWIZZLE_IDENTITY
};
static const VkImageViewCreateInfo VK_IMAGE_VIEW_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.components = VK_COMPONENT_MAPPING_DEFAULT,
.subresourceRange.baseMipLevel = 0,
.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS,
.subresourceRange.baseArrayLayer = 0,
.subresourceRange.layerCount= VK_REMAINING_ARRAY_LAYERS
};
static const VkShaderModuleCreateInfo VK_SHADER_MODULE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkPipelineShaderStageCreateInfo VK_PIPELINE_SHADER_STAGE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.pName = "main",
.pSpecializationInfo = NULL
};
static const VkPipelineVertexInputStateCreateInfo VK_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.vertexBindingDescriptionCount = 0,
.pVertexBindingDescriptions = NULL,
.vertexAttributeDescriptionCount = 0,
.pVertexAttributeDescriptions = NULL
};
static const VkPipelineInputAssemblyStateCreateInfo VK_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.primitiveRestartEnable = VK_FALSE
};
static const VkViewport VK_VIEWPORT_DEFAULT = {
.x = 0.0f,
.y = 0.0f,
.minDepth = 0.0f,
.maxDepth = 1.0f
};
static const VkPipelineDynamicStateCreateInfo VK_PIPELINE_DYNAMIC_STATE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.dynamicStateCount = 0,
.pDynamicStates = NULL
};
static const VkOffset2D VK_OFFSET2D_NONE = {
.x = 0,
.y = 0
};
static const VkRect2D VK_RECT2D_DEFAULT = {
.offset = VK_OFFSET2D_NONE
};
static const VkPipelineViewportStateCreateInfo VK_PIPELINE_VIEWPORT_STATE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkPipelineRasterizationStateCreateInfo VK_PIPELINE_RASTERIZATION_STATE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.depthClampEnable = VK_FALSE,
.rasterizerDiscardEnable = VK_FALSE,
.polygonMode = VK_POLYGON_MODE_FILL,
.cullMode = VK_CULL_MODE_NONE,
.frontFace = VK_FRONT_FACE_CLOCKWISE,
.depthBiasEnable = VK_FALSE,
.lineWidth = 1.0f
};
static const VkPipelineMultisampleStateCreateInfo VK_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO_DISABLED = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
.sampleShadingEnable = VK_FALSE
};
static const VkPipelineDepthStencilStateCreateInfo VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO_DISABLED = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.depthTestEnable = VK_FALSE,
.stencilTestEnable = VK_FALSE
};
static const VkColorComponentFlagBits VK_COLOR_COMPONENT_FLAG_BITS_ALL = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
static const VkPipelineColorBlendAttachmentState VK_PIPELINE_COLOR_BLEND_ATTACHMENT_STATE_OPAQUE = {
.blendEnable = VK_FALSE,
.colorWriteMask = VK_COLOR_COMPONENT_FLAG_BITS_ALL
};
static const VkPipelineColorBlendStateCreateInfo VK_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO_OPAQUE = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.logicOpEnable = VK_FALSE,
.attachmentCount = 1,
.pAttachments = &VK_PIPELINE_COLOR_BLEND_ATTACHMENT_STATE_OPAQUE,
};
static const VkPipelineLayoutCreateInfo VK_PIPELINE_LAYOUT_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.setLayoutCount = 0,
.pSetLayouts = NULL,
.pushConstantRangeCount = 0,
.pPushConstantRanges = NULL
};
static const VkAttachmentDescription VK_ATTACHMENT_DESCRIPTION_DEFAULT = {
.flags = 0,
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED
};
static const VkSubpassDescription VK_SUBPASS_DESCRIPTION_DEFAULT = {
.flags = 0,
.inputAttachmentCount = 0,
.pInputAttachments = NULL,
.colorAttachmentCount = 0,
.pColorAttachments = NULL,
.pResolveAttachments = NULL,
.pDepthStencilAttachment = NULL,
.preserveAttachmentCount = 0,
.pPreserveAttachments = NULL
};
static const VkRenderPassCreateInfo VK_RENDER_PASS_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.attachmentCount = 0,
.pAttachments = NULL,
.dependencyCount = 0,
.pDependencies = NULL
};
static const VkGraphicsPipelineCreateInfo VK_GRAPHICS_PIPELINE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.pVertexInputState = NULL,
.pInputAssemblyState = NULL,
.pTessellationState = NULL,
.pViewportState = NULL,
.pRasterizationState = NULL,
.pMultisampleState = NULL,
.pDepthStencilState = NULL,
.pColorBlendState = NULL,
.pDynamicState = NULL,
.subpass = 0,
.basePipelineHandle = VK_NULL_HANDLE,
.basePipelineIndex = -1
};
static const VkFramebufferCreateInfo VK_FRAMEBUFFER_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.layers = 1
};
static const VkCommandPoolCreateInfo VK_COMMAND_POOL_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkCommandBufferAllocateInfo VK_COMMAND_BUFFER_ALLOCATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.pNext = NULL,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY
};
static const VkCommandBufferBeginInfo VK_COMMAND_BUFFER_BEGIN_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.pNext = NULL,
.flags = 0,
.pInheritanceInfo = NULL
};
static const VkClearValue VK_CLEAR_VALUE_DEFAULT = {
.color.float32 = { 0.0f, 0.0f, 0.0f, 1.0f },
.depthStencil.depth = 1.0f,
.depthStencil.stencil = 1
};
static const VkRenderPassBeginInfo VK_RENDER_PASS_BEGIN_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.pNext = NULL,
.renderArea.offset = VK_OFFSET2D_NONE,
.clearValueCount = 0,
.pClearValues = NULL
};
static const VkSemaphoreCreateInfo VK_SEMAPHORE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkFenceCreateInfo VK_FENCE_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkSubmitInfo VK_SUBMIT_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.pNext = NULL,
.waitSemaphoreCount = 0,
.pWaitSemaphores = NULL,
.pWaitDstStageMask = NULL,
.commandBufferCount = 0,
.pCommandBuffers = NULL,
.signalSemaphoreCount = 0,
.pSignalSemaphores = NULL
};
static const VkPresentInfoKHR VK_PRESENT_INFO_KHR_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.pNext = NULL,
.waitSemaphoreCount = 0,
.pWaitSemaphores = NULL,
.pResults = NULL
};
static const VkBufferCreateInfo VK_BUFFER_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE//,
//.queueFamilyIndexCount = 0,
//.pQueueFamilyIndices = NULL
};
static const VkMemoryAllocateInfo VK_MEMORY_ALLOCATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.pNext = NULL
};
static const VkDescriptorSetLayoutBinding VK_DESCRIPTOR_SET_LAYOUT_BINDING_DEFAULT = {
.pImmutableSamplers = NULL
};
static const VkDescriptorSetLayoutCreateInfo VK_DESCRIPTOR_SET_LAYOUT_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.bindingCount = 0,
.pBindings = NULL
};
static const VkDescriptorPoolCreateInfo VK_DESCRIPTOR_POOL_CREATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pNext = NULL,
.flags = 0
};
static const VkDescriptorSetAllocateInfo VK_DESCRIPTOR_SET_ALLOCATE_INFO_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = NULL
};
static const VkDescriptorBufferInfo VK_DESCRIPTOR_BUFFER_INFO_DEFAULT = {
.buffer = VK_NULL_HANDLE,
.offset = 0,
.range = VK_WHOLE_SIZE
};
static const VkWriteDescriptorSet VK_WRITE_DESCRIPTOR_SET_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = NULL,
.dstArrayElement = 0,
.descriptorCount = 1,
.pImageInfo = NULL,
.pBufferInfo = NULL,
.pTexelBufferView = NULL
};
static const VkMappedMemoryRange VK_MAPPED_MEMORY_RANGE_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
.pNext = NULL,
.offset = 0,
.size = VK_WHOLE_SIZE
};
static const VkBufferCopy VK_BUFFER_COPY_DEFAULT = {
.srcOffset = 0,
.dstOffset = 0
};
static const VkBufferMemoryBarrier VK_BUFFER_MEMORY_BARRIER_DEFAULT = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.pNext = NULL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.offset = 0,
.size = VK_WHOLE_SIZE
};
//*****************************************************************************
enum
{
EVulkanInstanceLayers_Validation = 0 //< "VK_LAYER_KHRONOS_validation"
};
#define NUM_VULKAN_INSTANCE_LAYERS (EVulkanInstanceLayers_Validation + 1)
static char* layerNamePerVulkanInstanceLayer[NUM_VULKAN_INSTANCE_LAYERS] = {
"VK_LAYER_KHRONOS_validation"
};
enum
{
EVulkanInstanceExtension_Vkkhrdebugutils = 0 //< "VK_EXT_debug_utils" (VK_EXT_DEBUG_UTILS_EXTENSION_NAME)
};
#define NUM_VULKAN_INSTANCE_EXTENSIONS (EVulkanInstanceExtension_Vkkhrdebugutils + 1)
static struct
{
int layer; //< may == -1 for layerName == NULL
char* extensionName;
} perVulkanInstanceExtension[NUM_VULKAN_INSTANCE_EXTENSIONS] = {
{ EVulkanInstanceLayers_Validation, VK_EXT_DEBUG_UTILS_EXTENSION_NAME }
};
// NOTE: ^
// VkInstanceCreateInfo contains layers and extensions, not "layer per..
// .. extension" thus guaranteed that extension name is unique
static int bIsVulkanLoaded = 0;
struct vulkan_instance_t
{
struct
{
VkInstance a;
} vkinstance;
struct
{
VkDebugUtilsMessengerEXT a;
} vkdebugutilsmessengerext;
};
static const struct vulkan_instance_t vulkan_instance_default = {
.vkinstance.a = VK_NULL_HANDLE,
.vkdebugutilsmessengerext.a = VK_NULL_HANDLE
};
struct vulkan_t
{
int flags;
int apiVersion;
struct vulkan_instance_t instance;
int bIsEnabledPerInstanceLayer[NUM_VULKAN_INSTANCE_LAYERS];
int bIsEnabledPerInstanceExtension[NUM_VULKAN_INSTANCE_EXTENSIONS];
};
static const struct vulkan_t vulkan_default = {
.instance = vulkan_instance_default
};
static struct vulkan_t vulkan = vulkan_default;
//*****************************************************************************
static uint32_t get_vulkan_api_version()
{
PFN_vkEnumerateInstanceVersion a = (PFN_vkEnumerateInstanceVersion)vkGetInstanceProcAddr(NULL, "vkEnumerateInstanceVersion");
if(a == NULL)
{
// NOTE: "Vulkan 1.0 implementations were required to return..
// .. VK_ERROR_INCOMPATIBLE_DRIVER if apiVersion was larger..
// .. than 1.0",..
// .. https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkApplicationInfo.html
// "Because Vulkan 1.0 implementations may fail with..
// .. VK_ERROR_INCOMPATIBLE_DRIVER, applications should..
// .. determine the version of Vulkan available before calling..
// .. vkCreateInstance. If the vkGetInstanceProcAddr returns..
// .. NULL for vkEnumerateInstanceVersion, it is a Vulkan 1.0..
// .. implementation",..
// .. https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkApplicationInfo.html
return VK_API_VERSION_1_0;
}
uint32_t b;
if(vkEnumerateInstanceVersion(&b) != VK_SUCCESS)
{
on_printf2(stderr, "warning: vkEnumerateInstanceVersion != VK_SUCCESS in %s\n", __FUNCTION__);
return VK_API_VERSION_1_0;
}
return b;
}
// NOTE: returns 1 if succeeded
// returns 0 if failed
// NOTE: if a == stderr.. print error if failed
// if a == stdout.. print warning if failed
// requiredLayers[..] may == -1 to indicate a gap
// (*optionalLayers)[..] may == -1 to indicate a gap
// if succeeded.. will write to *numOptionalLayersAvailable
// NOTE: no such as "instance layer" as "Since version 1.0.13 of the Vulkan..
// .. API specification [...] the Vulkan SDK device layers have been..
// .. deprecated"
// ^
// https://gpuopen.com/learn/using-the-vulkan-validation-layers/
static int test_instance_layers(FILE* a, int numRequiredLayersTheresRoomFor, int* requiredLayers, int numOptionalLayersTheresRoomFor, int** optionalLayers, int* numOptionalLayersAvailable)
{
char* warningOrError = a == stderr ? "error" : "warning";
uint32_t b;
if(vkEnumerateInstanceLayerProperties(&b, NULL) != VK_SUCCESS)
{
on_printf2(a, "%s: vkEnumerateInstanceLayerProperties != VK_SUCCESS in %s\n", warningOrError, __FUNCTION__);
return 0;
}
VkLayerProperties c[b];
if(vkEnumerateInstanceLayerProperties(&b, c) != VK_SUCCESS)
{
on_printf2(a, "%s: vkEnumerateInstanceLayerProperties != VK_SUCCESS in %s\n", warningOrError, __FUNCTION__);
return 0;
}
int bIsAnyRequiredLayerNotAvailable = 0;
for(int i = 0; i < numRequiredLayersTheresRoomFor; ++i)
{
if(requiredLayers[i] == -1)
{
continue; //< ignore gap
}
int layer = requiredLayers[i];
char* layerName = layerNamePerVulkanInstanceLayer[layer];
int bIsLayerAvailable = 0;
for(int j = 0; j < b; ++j)
{
if(strcmp(layerName, c[j].layerName) == 0)
{
bIsLayerAvailable = 1;
break;
}
}
if(bIsLayerAvailable == 0)
{
if(a == stderr)
{
on_printf2(stderr, "error: required instance layer %s missing in %s\n", layerName, __FUNCTION__);
}
return 0;
}
}
if(numOptionalLayersTheresRoomFor > 0)
{
int d = 0;
for(int i = 0; i < numOptionalLayersTheresRoomFor; ++i)
{
if((*optionalLayers)[i] == -1)
{
continue; //< ignore gap
}
int layer = (*optionalLayers)[i];
char* layerName = layerNamePerVulkanInstanceLayer[layer];
int bIsLayerAvailable = 0;
for(int j = 0; j < b; ++j)
{
if(strcmp(layerName, c[j].layerName) == 0)
{
bIsLayerAvailable = 1;
++d;
break;
}
}
if(bIsLayerAvailable == 0)
{
(*optionalLayers)[i] = -1;
}
}
*numOptionalLayersAvailable = d;
}
return 1;
}
// NOTE: returns 1 if succeeded
// returns 0 if failed
// NOTE: if a == stderr.. print error if failed
// if a == stdout.. print warning if failed
// layerName may == NULL
// NOTE: assumes that layerName if any is tested
static int test_instance_extension(FILE* a, char* layerName, char* extensionName)
{
char* warningOrError = a == stderr ? "error" : "warning";
uint32_t b;
// "[If] pLayerName parameter is NULL, only extensions provided by..
// .. the Vulkan implementation or by implicitly enabled layers are..
// .. returned",
// https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkEnumerateInstanceExtensionProperties.html
// v
if(vkEnumerateInstanceExtensionProperties(layerName, &b, NULL) != VK_SUCCESS)
{
on_printf2(a, "%s: vkEnumerateInstanceExtensionProperties != VK_SUCCESS in %s\n", warningOrError, __FUNCTION__);
return 0;
}
VkExtensionProperties c[b];
if(vkEnumerateInstanceExtensionProperties(layerName, &b, c) != VK_SUCCESS)
{
on_printf2(a, "%s: vkEnumerateInstanceExtensionProperties != VK_SUCCESS in %s\n", warningOrError, __FUNCTION__);
return 0;
}
for(int i = 0; i < b; ++i)
{
if(strcmp(extensionName, c[i].extensionName) == 0)
{
return 1;
}
}
return 0;
}
struct test_instance_extensions_per_extension_t
{
int layer;
int extension;
};
// NOTE: returns 1 if succeeded
// returns 0 if failed
// NOTE: if a == stderr.. print error
// if a == stdout.. print warning
// requiredExtensions[..] may == -1 to indicate a gap
// (*optionalExtensions)[..] may == -1 to indicate a gap
// if succeeded..
// .. if an optional extension at index .. is not available will set..
// .. optionalExtensions[..] == -1
// .. will write to *numOptionalExtensionsAvailable
// NOTE: assumes that each layer if any is tested even for optional layer(s)..
// .. if any
static int test_instance_extensions(FILE* a, int bIsAvailablePerInstanceLayer[NUM_VULKAN_INSTANCE_EXTENSIONS], int numRequiredExtensionsTheresRoomFor, int* requiredExtensions, int numOptionalExtensionsTheresRoomFor, int** optionalExtensions, int* numOptionalExtensionsAvailable)
{
for(int i = 0; i < numRequiredExtensionsTheresRoomFor; ++i)
{
if(requiredExtensions[i] == -1)
{
continue; //< ignore gap
}
int extension = requiredExtensions[i];
char* extensionName = perVulkanInstanceExtension[extension].extensionName;
int layer = perVulkanInstanceExtension[extension].layer;
char* layerName = NULL;
if(layer != -1)
{
layerName = layerNamePerVulkanInstanceLayer[layer];
}
if(test_instance_extension(a, layerName, extensionName) == 0)
{
// only print warning about external library call, thus in..
// .. test_instance_extension..
// .. vkEnumerateInstanceExtensionProperties warning is fine,..
// .. but don't print warning about test_instance_extension fail
// v
if(a == stderr)
{
on_printf2(stderr, "error: required instance extension %s is missing (layerName == %s) in %s\n", extensionName, layerName == NULL ? "NULL" : layerName, __FUNCTION__);
}
return 0;
}
}
if(numOptionalExtensionsTheresRoomFor != 0)
{
int b = 0;
for(int i = 0; i < numOptionalExtensionsTheresRoomFor; ++i)
{
if((*optionalExtensions)[i] == -1)
{
continue; //< ignore gap
}
int extension = (*optionalExtensions)[i];
char* extensionName = perVulkanInstanceExtension[extension].extensionName;
int layer = perVulkanInstanceExtension[extension].layer;
char* layerName = NULL;
if(layer != -1)
{
if(bIsAvailablePerInstanceLayer[layer] == 0)
{
// disable extension because requires layer that is not..
// .. available
(*optionalExtensions)[i] = -1;
continue;
}
layerName = layerNamePerVulkanInstanceLayer[layer];
}
// optional instance extension should never print error
// v
if(test_instance_extension(stdout, layerName, extensionName) == 0)
{