-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkvf.h
executable file
·3083 lines (2752 loc) · 125 KB
/
kvf.h
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
/***
* MIT License
*
* Copyright (c) 2023-2024 kbz_8
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* Do this:
* #define KVF_IMPLEMENTATION
* before you include this file in *one* C or C++ file to create the implementation.
*
* // i.e. it should look like this:
* #include ...
* #include ...
* #include ...
* #define KVF_IMPLEMENTATION
* #include "kvf.h"
*
* You can #define KVF_ASSERT(x) before the #include to avoid using assert.h.
* And #define KVF_MALLOC, KVF_REALLOC, and KVF_FREE to avoid using malloc, realloc, free.
*
* By default KVF exits the program if a call to the Vulkan API fails. You can avoid that
* by using #define KVF_NO_EXIT_ON_FAILURE
*
* If you are using Volk or any other meta loader you must define KVF_IMPL_VK_NO_PROTOTYPES
* before including this file to avoid conflicts with Vulkan prototypes.
* You will also need to pass the function pointers to kvf using dedicated functions.
*
* You can also #define KVF_ENABLE_VALIDATION_LAYERS to enable validation layers.
*
* Use #define KVF_NO_KHR to remove all functions that use KHR calls.
*/
#ifndef KBZ_8_VULKAN_FRAMEWORK_H
#define KBZ_8_VULKAN_FRAMEWORK_H
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
#define VK_NO_PROTOTYPES
#endif
#include <vulkan/vulkan_core.h>
#include <stdint.h>
#include <stdbool.h>
/* ============================================= Prototypes ============================================= */
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
KVF_GRAPHICS_QUEUE = 0,
KVF_PRESENT_QUEUE = 1,
KVF_COMPUTE_QUEUE = 2
} KvfQueueType;
typedef enum
{
KVF_IMAGE_COLOR = 0,
KVF_IMAGE_DEPTH = 1,
KVF_IMAGE_DEPTH_ARRAY = 2,
KVF_IMAGE_CUBE = 3,
KVF_IMAGE_OTHER = 4,
} KvfImageType;
typedef void (*KvfErrorCallback)(const char* message);
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
typedef struct KvfGlobalVulkanFunctions KvfGlobalVulkanFunctions;
typedef struct KvfDeviceVulkanFunctions KvfDeviceVulkanFunctions;
typedef struct KvfInstanceVulkanFunctions KvfInstanceVulkanFunctions;
#endif
typedef struct KvfGraphicsPipelineBuilder KvfGraphicsPipelineBuilder;
void kvfSetErrorCallback(KvfErrorCallback callback);
void kvfSetWarningCallback(KvfErrorCallback callback);
void kvfSetValidationErrorCallback(KvfErrorCallback callback);
void kvfSetValidationWarningCallback(KvfErrorCallback callback);
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
void kvfPassGlobalVulkanFunctionPointers(const KvfGlobalVulkanFunctions* fns);
void kvfPassInstanceVulkanFunctionPointers(const KvfInstanceVulkanFunctions* fns);
#endif
void kvfAddLayer(const char* layer);
VkInstance kvfCreateInstance(const char** extensions_enabled, uint32_t extensions_count);
void kvfDestroyInstance(VkInstance instance);
// If surfaces given to theses functions are VK_NULL_HANDLE no present queues will be searched and thus kvfQueuePresentKHR will not work
VkPhysicalDevice kvfPickFirstPhysicalDevice(VkInstance instance, VkSurfaceKHR surface);
VkPhysicalDevice kvfPickGoodDefaultPhysicalDevice(VkInstance instance, VkSurfaceKHR surface);
VkPhysicalDevice kvfPickGoodPhysicalDevice(VkInstance instance, VkSurfaceKHR surface, const char** device_extensions, uint32_t device_extensions_count);
VkQueue kvfGetDeviceQueue(VkDevice device, KvfQueueType queue);
uint32_t kvfGetDeviceQueueFamily(VkDevice device, KvfQueueType queue);
#ifndef KVF_NO_KHR
bool kvfQueuePresentKHR(VkDevice device, VkSemaphore wait, VkSwapchainKHR swapchain, uint32_t image_index); // return false when the swapchain must be recreated
#endif
// Meant to be used when creating a VkDevice with a custom VkPhysicalDevice
int32_t kvfFindDeviceQueueFamily(VkPhysicalDevice physical, KvfQueueType type); // This function cannot find present queue
#ifndef KVF_NO_KHR
int32_t kvfFindDeviceQueueFamilyKHR(VkPhysicalDevice physical, VkSurfaceKHR surface, KvfQueueType type); // This one can find present queue
#endif
VkDevice kvfCreateDefaultDevice(VkPhysicalDevice physical);
VkDevice kvfCreateDevice(VkPhysicalDevice physical, const char** extensions, uint32_t extensions_count, VkPhysicalDeviceFeatures* features);
VkDevice kvfCreateDefaultDevicePhysicalDeviceAndCustomQueues(VkPhysicalDevice physical, int32_t graphics_queue, int32_t present_queue, int32_t compute_queue);
VkDevice kvfCreateDeviceCustomPhysicalDeviceAndQueues(VkPhysicalDevice physical, const char** extensions, uint32_t extensions_count, VkPhysicalDeviceFeatures* features, int32_t graphics_queue, int32_t present_queue, int32_t compute_queue);
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
void kvfPassDeviceVulkanFunctionPointers(VkPhysicalDevice physical, VkDevice device, const KvfDeviceVulkanFunctions* fns);
#endif
void kvfSetAllocationCallbacks(VkDevice device, const VkAllocationCallbacks* callbacks);
void kvfDestroyDevice(VkDevice device);
VkFence kvfCreateFence(VkDevice device);
void kvfWaitForFence(VkDevice device, VkFence fence);
void kvfDestroyFence(VkDevice device, VkFence fence);
VkSemaphore kvfCreateSemaphore(VkDevice device);
void kvfDestroySemaphore(VkDevice device, VkSemaphore semaphore);
#ifndef KVF_NO_KHR
VkSwapchainKHR kvfCreateSwapchainKHR(VkDevice device, VkPhysicalDevice physical, VkSurfaceKHR surface, VkExtent2D extent, VkSwapchainKHR old_swapchain, bool try_vsync, bool srgb);
VkFormat kvfGetSwapchainImagesFormat(VkSwapchainKHR swapchain);
uint32_t kvfGetSwapchainImagesCount(VkSwapchainKHR swapchain);
uint32_t kvfGetSwapchainMinImagesCount(VkSwapchainKHR swapchain);
VkExtent2D kvfGetSwapchainImagesSize(VkSwapchainKHR swapchain);
void kvfDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain);
#endif
VkImage kvfCreateImage(VkDevice device, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, KvfImageType type);
void kvfCopyImageToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkImage src, size_t buffer_offset, VkImageAspectFlagBits aspect, VkExtent3D extent);
void kvfDestroyImage(VkDevice device, VkImage image);
VkImageView kvfCreateImageView(VkDevice device, VkImage image, VkFormat format, VkImageViewType type, VkImageAspectFlags aspect, int layer_count);
void kvfDestroyImageView(VkDevice device, VkImageView image_view);
void kvfTransitionImageLayout(VkDevice device, VkImage image, KvfImageType type, VkCommandBuffer cmd, VkFormat format, VkImageLayout old_layout, VkImageLayout new_layout, bool is_single_time_cmd_buffer);
VkSampler kvfCreateSampler(VkDevice device, VkFilter filters, VkSamplerAddressMode address_modes, VkSamplerMipmapMode mipmap_mode);
void kvfDestroySampler(VkDevice device, VkSampler sampler);
VkBuffer kvfCreateBuffer(VkDevice device, VkBufferUsageFlags usage, VkDeviceSize size);
void kvfCopyBufferToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkBuffer src, size_t size);
void kvfCopyBufferToImage(VkCommandBuffer cmd, VkImage dst, VkBuffer src, size_t buffer_offset, VkImageAspectFlagBits aspect, VkExtent3D extent);
void kvfDestroyBuffer(VkDevice device, VkBuffer buffer);
VkFramebuffer kvfCreateFramebuffer(VkDevice device, VkRenderPass renderpass, VkImageView* image_views, size_t image_views_count, VkExtent2D extent);
VkExtent2D kvfGetFramebufferSize(VkFramebuffer buffer);
void kvfDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer);
VkCommandBuffer kvfCreateCommandBuffer(VkDevice device); // Uses internal command pool, not thread safe
VkCommandBuffer kvfCreateCommandBufferLeveled(VkDevice device, VkCommandBufferLevel level); // Same
void kvfBeginCommandBuffer(VkCommandBuffer buffer, VkCommandBufferUsageFlags flags);
void kvfEndCommandBuffer(VkCommandBuffer buffer);
void kvfSubmitCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkSemaphore signal, VkSemaphore wait, VkFence fence, VkPipelineStageFlags* stages);
void kvfSubmitSingleTimeCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkFence fence);
void kvfDestroyCommandBuffer(VkDevice device, VkCommandBuffer buffer);
VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples);
#ifndef KVF_NO_KHR
VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR swapchain, bool clear);
#endif
VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point);
VkRenderPass kvfCreateRenderPassWithSubpassDependencies(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point, VkSubpassDependency* dependencies, size_t dependencies_count);
void kvfDestroyRenderPass(VkDevice device, VkRenderPass renderpass);
void kvfBeginRenderPass(VkRenderPass pass, VkCommandBuffer cmd, VkFramebuffer framebuffer, VkExtent2D framebuffer_extent, VkClearValue* clears, size_t clears_count);
VkShaderModule kvfCreateShaderModule(VkDevice device, uint32_t* code, size_t size);
void kvfDestroyShaderModule(VkDevice device, VkShaderModule shader);
const char* kvfVerbaliseVkResult(VkResult result);
bool kvfIsStencilFormat(VkFormat format);
bool kvfIsDepthFormat(VkFormat format);
uint32_t kvfFormatSize(VkFormat format);
VkPipelineStageFlags kvfLayoutToAccessMask(VkImageLayout layout, bool is_destination);
VkPipelineStageFlags kvfAccessFlagsToPipelineStage(VkAccessFlags access_flags, VkPipelineStageFlags stage_flags);
VkFormat kvfFindSupportFormatInCandidates(VkDevice device, VkFormat* candidates, size_t candidates_count, VkImageTiling tiling, VkFormatFeatureFlags flags);
VkDescriptorSetLayout kvfCreateDescriptorSetLayout(VkDevice device, VkDescriptorSetLayoutBinding* bindings, size_t bindings_count);
void kvfDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout layout);
VkDescriptorSet kvfAllocateDescriptorSet(VkDevice device, VkDescriptorSetLayout layout);
void kvfUpdateStorageBufferToDescriptorSet(VkDevice device, VkDescriptorSet set, const VkDescriptorBufferInfo* info, uint32_t binding);
void kvfUpdateUniformBufferToDescriptorSet(VkDevice device, VkDescriptorSet set, const VkDescriptorBufferInfo* info, uint32_t binding);
void kvfUpdateImageToDescriptorSet(VkDevice device, VkDescriptorSet set, const VkDescriptorImageInfo* info, uint32_t binding);
VkWriteDescriptorSet kvfWriteStorageBufferToDescriptorSet(VkDevice device, VkDescriptorSet set, const VkDescriptorBufferInfo* info, uint32_t binding);
VkWriteDescriptorSet kvfWriteUniformBufferToDescriptorSet(VkDevice device, VkDescriptorSet set, const VkDescriptorBufferInfo* info, uint32_t binding);
VkWriteDescriptorSet kvfWriteImageToDescriptorSet(VkDevice device, VkDescriptorSet set, const VkDescriptorImageInfo* info, uint32_t binding);
void kvfResetDeviceDescriptorPools(VkDevice device);
VkPipelineLayout kvfCreatePipelineLayout(VkDevice device, VkDescriptorSetLayout* set_layouts, size_t set_layouts_count, VkPushConstantRange* pc, size_t pc_count);
void kvfDestroyPipelineLayout(VkDevice device, VkPipelineLayout layout);
KvfGraphicsPipelineBuilder* kvfCreateGPipelineBuilder();
void kvfDestroyGPipelineBuilder(KvfGraphicsPipelineBuilder* builder);
void kvfGPipelineBuilderReset(KvfGraphicsPipelineBuilder* builder);
void kvfGPipelineBuilderSetInputTopology(KvfGraphicsPipelineBuilder* builder, VkPrimitiveTopology topology);
void kvfGPipelineBuilderSetPolygonMode(KvfGraphicsPipelineBuilder* builder, VkPolygonMode polygon, float line_width);
void kvfGPipelineBuilderSetCullMode(KvfGraphicsPipelineBuilder* builder, VkCullModeFlags cull, VkFrontFace face);
void kvfGPipelineBuilderSetMultisampling(KvfGraphicsPipelineBuilder* builder, VkSampleCountFlagBits count);
void kvfGPipelineBuilderSetMultisamplingShading(KvfGraphicsPipelineBuilder* builder, VkSampleCountFlagBits count, float min_sampling_shading);
void kvfGPipelineBuilderDisableBlending(KvfGraphicsPipelineBuilder* builder);
void kvfGPipelineBuilderEnableAdditiveBlending(KvfGraphicsPipelineBuilder* builder);
void kvfGPipelineBuilderEnableAlphaBlending(KvfGraphicsPipelineBuilder* builder);
void kvfGPipelineBuilderEnableDepthTest(KvfGraphicsPipelineBuilder* builder, VkCompareOp op, bool write_enabled);
void kvfGPipelineBuilderDisableDepthTest(KvfGraphicsPipelineBuilder* builder);
void kvfGPipelineBuilderSetVertexInputs(KvfGraphicsPipelineBuilder* builder, VkVertexInputBindingDescription binds, VkVertexInputAttributeDescription* attributes, size_t attributes_count);
void kvfGPipelineBuilderAddShaderStage(KvfGraphicsPipelineBuilder* builder, VkShaderStageFlagBits stage, VkShaderModule module, const char* entry);
void kvfGPipelineBuilderResetShaderStages(KvfGraphicsPipelineBuilder* builder);
VkPipeline kvfCreateGraphicsPipeline(VkDevice device, VkPipelineCache cache, VkPipelineLayout layout, KvfGraphicsPipelineBuilder* builder, VkRenderPass pass);
void kvfDestroyPipeline(VkDevice device, VkPipeline pipeline);
void kvfCheckVk(VkResult result);
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
#ifdef KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE
#undef KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE
#endif
#define KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(fn) PFN_##fn fn
struct KvfGlobalVulkanFunctions
{
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateInstance);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEnumerateInstanceExtensionProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEnumerateInstanceLayerProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetInstanceProcAddr);
};
struct KvfInstanceVulkanFunctions
{
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateDevice);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyInstance);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEnumerateDeviceExtensionProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEnumeratePhysicalDevices);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceFeatures);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceFormatProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceImageFormatProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceMemoryProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceProperties);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceQueueFamilyProperties);
#ifndef KVF_NO_KHR
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroySurfaceKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceSurfaceFormatsKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceSurfacePresentModesKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetPhysicalDeviceSurfaceSupportKHR);
#endif
};
struct KvfDeviceVulkanFunctions
{
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkAllocateCommandBuffers);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkAllocateDescriptorSets);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkBeginCommandBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdBeginRenderPass);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdCopyBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdCopyBufferToImage);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdCopyImage);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdCopyImageToBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdEndRenderPass);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCmdPipelineBarrier);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateCommandPool);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateDescriptorPool);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateDescriptorSetLayout);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateFence);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateFramebuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateGraphicsPipelines);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateImage);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateImageView);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreatePipelineLayout);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateRenderPass);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateSampler);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateSemaphore);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateShaderModule);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyCommandPool);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyDescriptorPool);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyDescriptorSetLayout);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyDevice);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyFence);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyFramebuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyImage);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyImageView);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyPipeline);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyPipelineLayout);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyRenderPass);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroySampler);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroySemaphore);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyShaderModule);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDeviceWaitIdle);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEndCommandBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkFreeCommandBuffers);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetDeviceQueue);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetImageSubresourceLayout);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkQueueSubmit);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkResetCommandBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkResetDescriptorPool);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkResetEvent);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkResetFences);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkUpdateDescriptorSets);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkWaitForFences);
#ifndef KVF_NO_KHR
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkCreateSwapchainKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroySwapchainKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetSwapchainImagesKHR);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkQueuePresentKHR);
#endif
};
#endif
#ifdef __cplusplus
}
#endif
#endif // KBZ_8_VULKAN_FRAMEWORK_H
/* ========================================== Implementation =========================================== */
#ifdef KVF_IMPLEMENTATION
#ifndef KVF_MALLOC
#define KVF_MALLOC(x) malloc(x)
#endif
#ifndef KVF_REALLOC
#define KVF_REALLOC(x, s) realloc(x, s)
#endif
#ifndef KVF_FREE
#define KVF_FREE(x) free(x)
#endif
#ifndef KVF_ASSERT
#include <assert.h>
#define KVF_ASSERT(x) assert(x)
#endif
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
#define KVF_GET_GLOBAL_FUNCTION(fn) __kvf_g_fns.fn
#define KVF_GET_INSTANCE_FUNCTION(fn) __kvf_i_fns.fn
#define KVF_GET_DEVICE_FUNCTION(fn) kvf_device->fns.fn
#else
#define KVF_GET_GLOBAL_FUNCTION(fn) fn
#define KVF_GET_INSTANCE_FUNCTION(fn) fn
#define KVF_GET_DEVICE_FUNCTION(fn) fn
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef KVF_DESCRIPTOR_POOL_CAPACITY
#undef KVF_DESCRIPTOR_POOL_CAPACITY
#endif
#define KVF_DESCRIPTOR_POOL_CAPACITY 512
#ifdef KVF_COMMAND_POOL_CAPACITY
#undef KVF_COMMAND_POOL_CAPACITY
#endif
#define KVF_COMMAND_POOL_CAPACITY 512
typedef struct
{
int32_t graphics;
int32_t present;
int32_t compute;
} __KvfQueueFamilies;
typedef struct __KvfDescriptorPool
{
VkDescriptorPool pool;
size_t capacity;
size_t size;
} __KvfDescriptorPool;
typedef struct __KvfDevice
{
__KvfQueueFamilies queues;
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
KvfDeviceVulkanFunctions fns;
#endif
VkDevice device;
VkAllocationCallbacks* callbacks;
VkPhysicalDevice physical;
VkCommandPool cmd_pool;
VkCommandBuffer* cmd_buffers;
__KvfDescriptorPool* sets_pools;
size_t cmd_buffers_size;
size_t cmd_buffers_capacity;
size_t sets_pools_size;
} __KvfDevice;
#ifndef KVF_NO_KHR
typedef struct __KvfSwapchainSupportInternal
{
VkSurfaceCapabilitiesKHR capabilities;
VkSurfaceFormatKHR* formats;
VkPresentModeKHR* present_modes;
uint32_t formats_count;
uint32_t present_modes_count;
} __KvfSwapchainSupportInternal;
typedef struct __KvfSwapchain
{
__KvfSwapchainSupportInternal support;
VkSwapchainKHR swapchain;
VkExtent2D images_extent;
VkFormat images_format;
uint32_t images_count;
} __KvfSwapchain;
#endif
typedef struct __KvfFramebuffer
{
VkFramebuffer framebuffer;
VkExtent2D extent;
} __KvfFramebuffer;
struct KvfGraphicsPipelineBuilder
{
VkPipelineShaderStageCreateInfo* shader_stages;
VkPipelineVertexInputStateCreateInfo vertex_input_state;
VkPipelineInputAssemblyStateCreateInfo input_assembly_state;
VkPipelineTessellationStateCreateInfo tessellation_state;
VkPipelineRasterizationStateCreateInfo rasterization_state;
VkPipelineDepthStencilStateCreateInfo depth_stencil_state;
VkPipelineColorBlendAttachmentState color_blend_attachment_state;
VkPipelineMultisampleStateCreateInfo multisampling;
size_t shader_stages_count;
};
// Dynamic arrays
static __KvfDevice* __kvf_internal_devices = NULL;
static size_t __kvf_internal_devices_size = 0;
static size_t __kvf_internal_devices_capacity = 0;
#ifndef KVF_NO_KHR
static __KvfSwapchain* __kvf_internal_swapchains = NULL;
static size_t __kvf_internal_swapchains_size = 0;
static size_t __kvf_internal_swapchains_capacity = 0;
#endif
static __KvfFramebuffer* __kvf_internal_framebuffers = NULL;
static size_t __kvf_internal_framebuffers_size = 0;
static size_t __kvf_internal_framebuffers_capacity = 0;
#ifdef KVF_ENABLE_VALIDATION_LAYERS
static VkDebugUtilsMessengerEXT __kvf_debug_messenger = VK_NULL_HANDLE;
static char** __kvf_extra_layers = NULL;
static size_t __kvf_extra_layers_count = 0;
#endif
static KvfErrorCallback __kvf_error_callback = NULL;
static KvfErrorCallback __kvf_warning_callback = NULL;
static KvfErrorCallback __kvf_validation_error_callback = NULL;
static KvfErrorCallback __kvf_validation_warning_callback = NULL;
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
static KvfGlobalVulkanFunctions __kvf_g_fns;
static KvfInstanceVulkanFunctions __kvf_i_fns;
#endif
void __kvfCheckVk(VkResult result, const char* function)
{
if(result < VK_SUCCESS)
{
if(__kvf_error_callback != NULL)
{
char buffer[1024];
snprintf(buffer, 1024, "KVF Vulkan error in '%s': %s", function, kvfVerbaliseVkResult(result));
__kvf_error_callback(buffer);
return;
}
fprintf(stderr, "KVF Vulkan error in '%s': %s\n", function, kvfVerbaliseVkResult(result));
#ifndef KVF_NO_EXIT_ON_FAILURE
exit(EXIT_FAILURE);
#endif
}
else if(result > VK_SUCCESS)
{
if(__kvf_warning_callback != NULL)
{
char buffer[1024];
snprintf(buffer, 1024, "KVF Vulkan warning in '%s': %s", function, kvfVerbaliseVkResult(result));
__kvf_warning_callback(buffer);
return;
}
printf("KVF Vulkan warning in '%s': %s\n", function, kvfVerbaliseVkResult(result));
}
}
#undef __kvfCheckVk
#define __kvfCheckVk(res) __kvfCheckVk(res, __FUNCTION__)
void kvfCheckVk(VkResult result)
{
__kvfCheckVk(result);
}
void __kvfAddDeviceToArray(VkPhysicalDevice device, int32_t graphics_queue, int32_t present_queue, int32_t compute_queue)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
if(__kvf_internal_devices_size == __kvf_internal_devices_capacity)
{
// Resize the dynamic array if necessary
__kvf_internal_devices_capacity += 2;
__kvf_internal_devices = (__KvfDevice*)KVF_REALLOC(__kvf_internal_devices, __kvf_internal_devices_capacity * sizeof(__KvfDevice));
}
__kvf_internal_devices[__kvf_internal_devices_size].physical = device;
__kvf_internal_devices[__kvf_internal_devices_size].queues.graphics = graphics_queue;
__kvf_internal_devices[__kvf_internal_devices_size].queues.compute = compute_queue;
__kvf_internal_devices[__kvf_internal_devices_size].queues.present = present_queue;
__kvf_internal_devices_size++;
}
void __kvfCompleteDevice(VkPhysicalDevice physical, VkDevice device)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
KVF_ASSERT(physical != VK_NULL_HANDLE);
__KvfDevice* kvf_device = NULL;
for(size_t i = 0; i < __kvf_internal_devices_size; i++)
{
if(__kvf_internal_devices[i].physical == physical)
kvf_device = &__kvf_internal_devices[i];
}
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
VkCommandPool pool;
VkCommandPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
pool_info.queueFamilyIndex = kvf_device->queues.graphics;
__kvfCheckVk(KVF_GET_DEVICE_FUNCTION(vkCreateCommandPool)(device, &pool_info, NULL, &pool));
kvf_device->device = device;
kvf_device->cmd_pool = pool;
kvf_device->sets_pools = NULL;
kvf_device->sets_pools_size = 0;
kvf_device->cmd_buffers_size = 0;
kvf_device->cmd_buffers_capacity = KVF_COMMAND_POOL_CAPACITY;
kvf_device->cmd_buffers = (VkCommandBuffer*)KVF_MALLOC(KVF_COMMAND_POOL_CAPACITY * sizeof(VkCommandBuffer));
KVF_ASSERT(kvf_device->cmd_buffers != NULL && "allocation failed :(");
}
void __kvfCompleteDeviceCustomPhysicalDeviceAndQueues(VkPhysicalDevice physical, VkDevice device, int32_t graphics_queue, int32_t present_queue, int32_t compute_queue)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
KVF_ASSERT(physical != VK_NULL_HANDLE);
__kvfAddDeviceToArray(physical, graphics_queue, present_queue, compute_queue);
__KvfDevice* kvf_device = NULL;
for(size_t i = 0; i < __kvf_internal_devices_size; i++)
{
if(__kvf_internal_devices[i].physical == physical)
kvf_device = &__kvf_internal_devices[i];
}
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
VkCommandPool pool;
VkCommandPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
pool_info.queueFamilyIndex = kvf_device->queues.graphics;
__kvfCheckVk(KVF_GET_DEVICE_FUNCTION(vkCreateCommandPool)(device, &pool_info, NULL, &pool));
kvf_device->device = device;
kvf_device->cmd_pool = pool;
kvf_device->sets_pools = NULL;
kvf_device->sets_pools_size = 0;
kvf_device->cmd_buffers_size = 0;
kvf_device->cmd_buffers_capacity = KVF_COMMAND_POOL_CAPACITY;
kvf_device->cmd_buffers = (VkCommandBuffer*)KVF_MALLOC(KVF_COMMAND_POOL_CAPACITY * sizeof(VkCommandBuffer));
kvf_device->callbacks = NULL;
KVF_ASSERT(kvf_device->cmd_buffers != NULL && "allocation failed :(");
}
void __kvfDestroyDescriptorPools(VkDevice device);
__KvfDevice* __kvfGetKvfDeviceFromVkPhysicalDevice(VkPhysicalDevice device)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
for(size_t i = 0; i < __kvf_internal_devices_size; i++)
{
if(__kvf_internal_devices[i].physical == device)
return &__kvf_internal_devices[i];
}
return NULL;
}
__KvfDevice* __kvfGetKvfDeviceFromVkDevice(VkDevice device)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
for(size_t i = 0; i < __kvf_internal_devices_size; i++)
{
if(__kvf_internal_devices[i].device == device)
return &__kvf_internal_devices[i];
}
return NULL;
}
__KvfDevice* __kvfGetKvfDeviceFromVkCommandBuffer(VkCommandBuffer cmd)
{
KVF_ASSERT(cmd != VK_NULL_HANDLE);
for(size_t i = 0; i < __kvf_internal_devices_size; i++)
{
for(size_t j = 0; j < __kvf_internal_devices[i].cmd_buffers_size; j++)
{
if(__kvf_internal_devices[i].cmd_buffers[j] == cmd)
return &__kvf_internal_devices[i];
}
}
return NULL;
}
void kvfSetAllocationCallbacks(VkDevice device, const VkAllocationCallbacks* callbacks)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
kvf_device->callbacks = (VkAllocationCallbacks*)KVF_MALLOC(sizeof(VkAllocationCallbacks));
KVF_ASSERT(kvf_device->callbacks && "allocation failed :(");
memcpy(kvf_device->callbacks, callbacks, sizeof(VkAllocationCallbacks));
}
void __kvfDestroyDevice(VkDevice device)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
for(size_t i = 0; i < __kvf_internal_devices_size; i++)
{
if(__kvf_internal_devices[i].device == device)
{
__KvfDevice* kvf_device = &__kvf_internal_devices[i];
KVF_FREE(kvf_device->cmd_buffers);
KVF_GET_DEVICE_FUNCTION(vkDestroyCommandPool)(device, kvf_device->cmd_pool, NULL);
__kvfDestroyDescriptorPools(device);
KVF_GET_DEVICE_FUNCTION(vkDestroyDevice)(device, NULL);
// Shift the elements to fill the gap
for(size_t j = i; j < __kvf_internal_devices_size - 1; j++)
__kvf_internal_devices[j] = __kvf_internal_devices[j + 1];
__kvf_internal_devices_size--;
if(__kvf_internal_devices_size == 0)
{
KVF_FREE(__kvf_internal_devices);
__kvf_internal_devices = NULL;
__kvf_internal_devices_capacity = 0;
}
return;
}
}
}
#ifndef KVF_NO_KHR
void __kvfAddSwapchainToArray(VkSwapchainKHR swapchain, __KvfSwapchainSupportInternal support, VkFormat format, uint32_t images_count, VkExtent2D extent)
{
KVF_ASSERT(swapchain != VK_NULL_HANDLE);
if(__kvf_internal_swapchains_size == __kvf_internal_swapchains_capacity)
{
// Resize the dynamic array if necessary
__kvf_internal_swapchains_capacity += 5;
__kvf_internal_swapchains = (__KvfSwapchain*)KVF_REALLOC(__kvf_internal_swapchains, __kvf_internal_swapchains_capacity * sizeof(__KvfSwapchain));
}
__kvf_internal_swapchains[__kvf_internal_swapchains_size].swapchain = swapchain;
__kvf_internal_swapchains[__kvf_internal_swapchains_size].support = support;
__kvf_internal_swapchains[__kvf_internal_swapchains_size].images_format = format;
__kvf_internal_swapchains[__kvf_internal_swapchains_size].images_count = images_count;
__kvf_internal_swapchains[__kvf_internal_swapchains_size].images_extent = extent;
__kvf_internal_swapchains_size++;
}
void __kvfDestroySwapchain(VkDevice device, VkSwapchainKHR swapchain)
{
KVF_ASSERT(swapchain != VK_NULL_HANDLE);
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
for(size_t i = 0; i < __kvf_internal_swapchains_size; i++)
{
if(__kvf_internal_swapchains[i].swapchain == swapchain)
{
KVF_GET_DEVICE_FUNCTION(vkDestroySwapchainKHR)(device, swapchain, kvf_device->callbacks);
// Shift the elements to fill the gap
for(size_t j = i; j < __kvf_internal_swapchains_size - 1; j++)
__kvf_internal_swapchains[j] = __kvf_internal_swapchains[j + 1];
__kvf_internal_swapchains_size--;
if(__kvf_internal_swapchains_size == 0)
{
KVF_FREE(__kvf_internal_swapchains);
__kvf_internal_swapchains = NULL;
__kvf_internal_swapchains_capacity = 0;
}
return;
}
}
}
__KvfSwapchain* __kvfGetKvfSwapchainFromVkSwapchainKHR(VkSwapchainKHR swapchain)
{
KVF_ASSERT(swapchain != VK_NULL_HANDLE);
for(size_t i = 0; i < __kvf_internal_swapchains_size; i++)
{
if(__kvf_internal_swapchains[i].swapchain == swapchain)
return &__kvf_internal_swapchains[i];
}
return NULL;
}
#endif
void __kvfAddFramebufferToArray(VkFramebuffer framebuffer, VkExtent2D extent)
{
KVF_ASSERT(framebuffer != VK_NULL_HANDLE);
if(__kvf_internal_framebuffers_size == __kvf_internal_framebuffers_capacity)
{
// Resize the dynamic array if necessary
__kvf_internal_framebuffers_capacity += 5;
__kvf_internal_framebuffers = (__KvfFramebuffer*)KVF_REALLOC(__kvf_internal_framebuffers, __kvf_internal_framebuffers_capacity * sizeof(__KvfFramebuffer));
}
__kvf_internal_framebuffers[__kvf_internal_framebuffers_size].framebuffer = framebuffer;
__kvf_internal_framebuffers[__kvf_internal_framebuffers_size].extent = extent;
__kvf_internal_framebuffers_size++;
}
void __kvfDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer)
{
KVF_ASSERT(framebuffer != VK_NULL_HANDLE);
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
for(size_t i = 0; i < __kvf_internal_framebuffers_size; i++)
{
if(__kvf_internal_framebuffers[i].framebuffer == framebuffer)
{
KVF_GET_DEVICE_FUNCTION(vkDestroyFramebuffer)(device, framebuffer, kvf_device->callbacks);
// Shift the elements to fill the gap
for(size_t j = i; j < __kvf_internal_framebuffers_size - 1; j++)
__kvf_internal_framebuffers[j] = __kvf_internal_framebuffers[j + 1];
__kvf_internal_framebuffers_size--;
if(__kvf_internal_framebuffers_size == 0)
{
KVF_FREE(__kvf_internal_framebuffers);
__kvf_internal_framebuffers = NULL;
__kvf_internal_framebuffers_capacity = 0;
}
return;
}
}
KVF_ASSERT(false && "could not find framebuffer");
}
__KvfFramebuffer* __kvfGetKvfFramebufferFromVkFramebuffer(VkFramebuffer framebuffer)
{
KVF_ASSERT(framebuffer != VK_NULL_HANDLE);
for(size_t i = 0; i < __kvf_internal_framebuffers_size; i++)
{
if(__kvf_internal_framebuffers[i].framebuffer == framebuffer)
return &__kvf_internal_framebuffers[i];
}
return NULL;
}
VkDescriptorPool __kvfDeviceCreateDescriptorPool(VkDevice device)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
kvf_device->sets_pools_size++;
kvf_device->sets_pools = (__KvfDescriptorPool*)KVF_REALLOC(kvf_device->sets_pools, kvf_device->sets_pools_size * sizeof(__KvfDescriptorPool));
memset(&kvf_device->sets_pools[kvf_device->sets_pools_size - 1], 0, sizeof(__KvfDescriptorPool));
VkDescriptorPoolSize pool_sizes[] = {
{ VK_DESCRIPTOR_TYPE_SAMPLER, 1024 },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1024 },
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024 },
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1024 },
{ VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1024 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024 },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1024 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1024 },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1024 },
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1024 }
};
VkDescriptorPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
pool_info.poolSizeCount = sizeof(pool_sizes) / sizeof(VkDescriptorPoolSize);
pool_info.pPoolSizes = pool_sizes;
pool_info.maxSets = KVF_DESCRIPTOR_POOL_CAPACITY;
pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
__kvfCheckVk(KVF_GET_DEVICE_FUNCTION(vkCreateDescriptorPool)(device, &pool_info, NULL, &kvf_device->sets_pools[kvf_device->sets_pools_size - 1].pool));
kvf_device->sets_pools[kvf_device->sets_pools_size - 1].capacity = KVF_DESCRIPTOR_POOL_CAPACITY;
return kvf_device->sets_pools[kvf_device->sets_pools_size - 1].pool;
}
void __kvfDestroyDescriptorPools(VkDevice device)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
for(size_t i = 0; i < kvf_device->sets_pools_size; i++)
KVF_GET_DEVICE_FUNCTION(vkDestroyDescriptorPool)(device, kvf_device->sets_pools[i].pool, NULL);
KVF_FREE(kvf_device->sets_pools);
kvf_device->sets_pools_size = 0;
}
void kvfSetErrorCallback(KvfErrorCallback callback)
{
__kvf_error_callback = callback;
}
void kvfSetWarningCallback(KvfErrorCallback callback)
{
__kvf_warning_callback = callback;
}
void kvfSetValidationErrorCallback(KvfErrorCallback callback)
{
__kvf_validation_error_callback = callback;
}
void kvfSetValidationWarningCallback(KvfErrorCallback callback)
{
__kvf_validation_warning_callback = callback;
}
#ifdef KVF_IMPL_VK_NO_PROTOTYPES
void kvfPassGlobalVulkanFunctionPointers(const KvfGlobalVulkanFunctions* fns)
{
KVF_ASSERT(fns != NULL);
__kvf_g_fns = *fns;
}
void kvfPassInstanceVulkanFunctionPointers(const KvfInstanceVulkanFunctions* fns)
{
KVF_ASSERT(fns != NULL);
__kvf_i_fns = *fns;
}
#endif
bool kvfIsStencilFormat(VkFormat format)
{
switch(format)
{
case VK_FORMAT_D32_SFLOAT_S8_UINT:
case VK_FORMAT_D24_UNORM_S8_UINT:
return true;
default: return false;
}
}
bool kvfIsDepthFormat(VkFormat format)
{
switch(format)
{
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_D32_SFLOAT:
case VK_FORMAT_D32_SFLOAT_S8_UINT:
case VK_FORMAT_D24_UNORM_S8_UINT:
case VK_FORMAT_D16_UNORM_S8_UINT:
return true;
default: return false;
}
}
VkPipelineStageFlags kvfLayoutToAccessMask(VkImageLayout layout, bool is_destination)
{
VkPipelineStageFlags access_mask = 0;
switch(layout)
{
case VK_IMAGE_LAYOUT_UNDEFINED:
if(is_destination)
KVF_ASSERT(false && "Vulkan : the new layout used in a transition must not be VK_IMAGE_LAYOUT_UNDEFINED");
break;
case VK_IMAGE_LAYOUT_GENERAL: access_mask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; break;
case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: access_mask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: access_mask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
access_mask = VK_ACCESS_SHADER_READ_BIT; // VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
break;
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: access_mask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; break;
case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: access_mask = VK_ACCESS_TRANSFER_READ_BIT; break;
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: access_mask = VK_ACCESS_TRANSFER_WRITE_BIT; break;
case VK_IMAGE_LAYOUT_PREINITIALIZED:
if(!is_destination)
access_mask = VK_ACCESS_HOST_WRITE_BIT;
else
KVF_ASSERT(false && "Vulkan : the new layout used in a transition must not be VK_IMAGE_LAYOUT_PREINITIALIZED");
break;
case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL: access_mask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; break;
case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL: access_mask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; break;
case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: access_mask = VK_ACCESS_MEMORY_READ_BIT; break;
default: KVF_ASSERT(false && "Vulkan : unexpected image layout"); break;
}
return access_mask;
}
VkPipelineStageFlags kvfAccessFlagsToPipelineStage(VkAccessFlags access_flags, VkPipelineStageFlags stage_flags)
{
VkPipelineStageFlags stages = 0;
while(access_flags != 0)
{
VkAccessFlagBits _access_flag = (VkAccessFlagBits)(access_flags & (~(access_flags - 1)));
if(_access_flag == 0 || (_access_flag & (_access_flag - 1)) != 0)
KVF_ASSERT(false && "Vulkan : an error has been caught during access flag to pipeline stage operation");
access_flags &= ~_access_flag;
switch(_access_flag)
{
case VK_ACCESS_INDIRECT_COMMAND_READ_BIT: stages |= VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT; break;
case VK_ACCESS_INDEX_READ_BIT: stages |= VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; break;
case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT: stages |= VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; break;
case VK_ACCESS_UNIFORM_READ_BIT: stages |= stage_flags | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; break;
case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT: stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; break;
case VK_ACCESS_SHADER_READ_BIT: stages |= stage_flags | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; break;
case VK_ACCESS_SHADER_WRITE_BIT: stages |= stage_flags | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; break;
case VK_ACCESS_COLOR_ATTACHMENT_READ_BIT: stages |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; break;
case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT: stages |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; break;
case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT: stages |= VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; break;
case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT: stages |= VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; break;
case VK_ACCESS_TRANSFER_READ_BIT: stages |= VK_PIPELINE_STAGE_TRANSFER_BIT; break;
case VK_ACCESS_TRANSFER_WRITE_BIT: stages |= VK_PIPELINE_STAGE_TRANSFER_BIT; break;
case VK_ACCESS_HOST_READ_BIT: stages |= VK_PIPELINE_STAGE_HOST_BIT; break;
case VK_ACCESS_HOST_WRITE_BIT: stages |= VK_PIPELINE_STAGE_HOST_BIT; break;
case VK_ACCESS_MEMORY_READ_BIT: break;
case VK_ACCESS_MEMORY_WRITE_BIT: break;
default: KVF_ASSERT(false && "Vulkan : unknown access flag"); break;
}
}
return stages;
}
VkFormat kvfFindSupportFormatInCandidates(VkDevice device, VkFormat* candidates, size_t candidates_count, VkImageTiling tiling, VkFormatFeatureFlags flags)
{
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL && "could not find VkDevice in registered devices");
for(size_t i = 0; i < candidates_count; i++)
{
VkFormatProperties props;
KVF_GET_INSTANCE_FUNCTION(vkGetPhysicalDeviceFormatProperties)(kvf_device->physical, candidates[i], &props);
if(tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & flags) == flags)
return candidates[i];
else if(tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & flags) == flags)
return candidates[i];
}
KVF_ASSERT(false && "Vulkan : failed to find image format");
return VK_FORMAT_R8G8B8A8_SRGB; // just to avoir warning
}
uint32_t kvfFormatSize(VkFormat format)
{
switch(format)
{
case VK_FORMAT_UNDEFINED: return 0;
case VK_FORMAT_R4G4_UNORM_PACK8: return 1;
case VK_FORMAT_R4G4B4A4_UNORM_PACK16: return 2;