-
Notifications
You must be signed in to change notification settings - Fork 17
/
NVK.h
2596 lines (2533 loc) · 113 KB
/
NVK.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
/*
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
/*
This header is a wrapper over vulkan.h : it allows to take advantage
of constructors and functors in order to 'nest' structure declaration
so that the code is more compact.
NOTE: only working for 1 single Vulkan Device, for now
Example on RenderPass Info declaration:
NVK::RenderPassCreateInfo rpinfo = NVK::RenderPassCreateInfo(
NVK::AttachmentDescription
( VK_FORMAT_R8G8B8A8_UNORM, (VkSampleCountFlagBits)MSAA, //format, samples
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, //loadOp, storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, //stencilLoadOp, stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL //initialLayout, finalLayout
)
( VK_FORMAT_D24_UNORM_S8_UINT, (VkSampleCountFlagBits)MSAA,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
)
( VK_FORMAT_R8G8B8A8_UNORM, (VkSampleCountFlagBits)1, //format, samples
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, //loadOp, storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, //stencilLoadOp, stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL //initialLayout, finalLayout
),
NVK::SubpassDescription
( VK_PIPELINE_BIND_POINT_GRAPHICS, //pipelineBindPoint
NVK::AttachmentReference(), //inputAttachments
NVK::AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL), //colorAttachments
NVK::AttachmentReference(2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL), //resolveAttachments
NVK::AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),//depthStencilAttachment
NVK::Uint32Array(), //preserveAttachments
0 //flags
),
NVK::SubpassDependency()
);
*/
//--------------------------------------------------------------------
#ifndef _NVK_H_
#define _NVK_H_
#ifdef _DEBUG
#define CHECK(a) assert(a == VK_SUCCESS)
#else
#define CHECK(a) a
#endif
#include <vector>
#include <map>
#include <set>
#include <string>
#include <vulkan/vulkan.h>
#include <nvvk/swapchain_vk.hpp>
#include <nvvk/context_vk.hpp>
#include "window_surface_vk.hpp"
//------------------------------------------------------------------------------
// Forward declarations
//------------------------------------------------------------------------------
struct NVPWindowInternal;
namespace nvvk {
class ContextWindowVK;
class SwapChain;
} // namespace nvvk
//------------------------------------------------------------------------------
// Vulkan functions in a more "friendly" way
// NOTE: vulkan-like functions don't start with vk<Capital Letter>
// to avoid confusion...
// If a method is too far away from what Vulkan would expose, the methor will
// start with 'ut', as 'utility'
// Types that mimic Vulkan types don't have either VkXXX and start right away with XXX
// in other words, consider the NVK class as a replacement for the prefix vk in functions
// and as a replacemenet of 'Vk' in types
//------------------------------------------------------------------------------
class NVK
{
private:
std::map<unsigned long, VkShaderModule> m_shaderModules;
public:
PFN_vkDebugMarkerSetObjectTagEXT pfnDebugMarkerSetObjectTagEXT;
PFN_vkDebugMarkerSetObjectNameEXT pfnDebugMarkerSetObjectNameEXT;
PFN_vkCmdDebugMarkerBeginEXT pfnCmdDebugMarkerBeginEXT;
PFN_vkCmdDebugMarkerEndEXT pfnCmdDebugMarkerEndEXT;
PFN_vkCmdDebugMarkerInsertEXT pfnCmdDebugMarkerInsertEXT;
class MemoryChunk;
class BufferImageCopy;
class BufferCreateInfo;
class FramebufferCreateInfo;
class RenderPassCreateInfo;
class SamplerCreateInfo;
class DescriptorSetAllocateInfo;
class SubmitInfo;
class ImageViewCreateInfo;
class CommandBufferInheritanceInfo;
//
// class for Command Buffer
//
class CommandBuffer
{
public:
CommandBuffer(VkCommandBuffer _cmdbuffer = VK_NULL_HANDLE)
: m_cmdbuffer(_cmdbuffer)
{
}
void resetCommandBuffer(VkCommandBufferResetFlags flags);
void endCommandBuffer();
void beginCommandBuffer(bool singleshot, bool bRenderpassContinue, NVK::CommandBufferInheritanceInfo& cmdIInfo) const;
void beginCommandBuffer(bool singleshot, bool bRenderpassContinue, const VkCommandBufferInheritanceInfo* pInheritanceInfo = NULL) const;
void cmdBindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline);
void cmdSetViewport(uint32_t firstViewport, uint32_t viewportCount, const VkViewport* pViewports);
void cmdSetScissor(uint32_t firstScissor, uint32_t scissorCount, const VkRect2D* pScissors);
void cmdSetLineWidth(float lineWidth);
void cmdSetDepthBias(float depthBiasConstantFactor, float depthBiasClamp, float depthBiasSlopeFactor);
void cmdSetBlendConstants(const float blendConstants[4]);
void cmdSetDepthBounds(float minDepthBounds, float maxDepthBounds);
void cmdSetStencilCompareMask(VkStencilFaceFlags faceMask, uint32_t compareMask);
void cmdSetStencilWriteMask(VkStencilFaceFlags faceMask, uint32_t writeMask);
void cmdSetStencilReference(VkStencilFaceFlags faceMask, uint32_t reference);
void cmdBindDescriptorSets(VkPipelineBindPoint pipelineBindPoint,
VkPipelineLayout layout,
uint32_t firstSet,
uint32_t descriptorSetCount,
const VkDescriptorSet* pDescriptorSets,
uint32_t dynamicOffsetCount,
const uint32_t* pDynamicOffsets);
void cmdBindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType);
void cmdBindVertexBuffers(uint32_t firstBinding, uint32_t bindingCount, const VkBuffer* pBuffers, const VkDeviceSize* pOffsets);
void cmdDraw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance);
void cmdDrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance);
void cmdDrawIndirect(VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount, uint32_t stride);
void cmdDrawIndexedIndirect(VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount, uint32_t stride);
void cmdDispatch(uint32_t x, uint32_t y, uint32_t z);
void cmdDispatchIndirect(VkBuffer buffer, VkDeviceSize offset);
void cmdCopyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size, VkDeviceSize destOffset = 0, VkDeviceSize srcOffset = 0);
void cmdCopyImage(VkImage srcImage,
VkImageLayout srcImageLayout,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkImageCopy* pRegions);
void cmdBlitImage(VkImage srcImage,
VkImageLayout srcImageLayout,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkImageBlit* pRegions,
VkFilter filter);
void cmdCopyBufferToImage(VkBuffer srcBuffer, VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, const VkBufferImageCopy* pRegions);
void cmdCopyImageToBuffer(VkImage srcImage, VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions);
void cmdUpdateBuffer(VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize dataSize, const uint32_t* pData);
void cmdFillBuffer(VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size, uint32_t data);
void cmdClearColorImage(VkImage image,
VkImageLayout imageLayout,
const VkClearColorValue* pColor,
uint32_t rangeCount,
const VkImageSubresourceRange* pRanges);
void cmdClearDepthStencilImage(VkImage image,
VkImageLayout imageLayout,
const VkClearDepthStencilValue* pDepthStencil,
uint32_t rangeCount,
const VkImageSubresourceRange* pRanges);
void cmdClearAttachments(uint32_t attachmentCount, const VkClearAttachment* pAttachments, uint32_t rectCount, const VkClearRect* pRects);
void cmdResolveImage(VkImage srcImage,
VkImageLayout srcImageLayout,
VkImage dstImage,
VkImageLayout dstImageLayout,
uint32_t regionCount,
const VkImageResolve* pRegions);
void cmdSetEvent(VkEvent event, VkPipelineStageFlags stageMask);
void cmdResetEvent(VkEvent event, VkPipelineStageFlags stageMask);
void cmdWaitEvents(uint32_t eventCount,
const VkEvent* pEvents,
VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
uint32_t memoryBarrierCount,
const VkMemoryBarrier* pMemoryBarriers,
uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier* pBufferMemoryBarriers,
uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier* pImageMemoryBarriers);
void cmdPipelineBarrier(VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
VkDependencyFlags dependencyFlags,
uint32_t memoryBarrierCount,
const VkMemoryBarrier* pMemoryBarriers,
uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier* pBufferMemoryBarriers,
uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier* pImageMemoryBarriers);
void cmdBeginQuery(VkQueryPool queryPool, uint32_t query, VkQueryControlFlags flags);
void cmdEndQuery(VkQueryPool queryPool, uint32_t query);
void cmdResetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount);
void cmdWriteTimestamp(VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t query);
void cmdCopyQueryPoolResults(VkQueryPool queryPool,
uint32_t firstQuery,
uint32_t queryCount,
VkBuffer dstBuffer,
VkDeviceSize dstOffset,
VkDeviceSize stride,
VkQueryResultFlags flags);
void cmdPushConstants(VkPipelineLayout layout, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size, const void* pValues);
void cmdBeginRenderPass(const VkRenderPassBeginInfo* pRenderPassBegin, VkSubpassContents contents);
void cmdNextSubpass(VkSubpassContents contents);
void cmdEndRenderPass();
void cmdExecuteCommands(uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers);
// ut... : methods that don't really correspond to VK API
void utCmdSetImageLayout(VkImage image,
VkImageAspectFlags aspectMask,
VkImageLayout oldImageLayout,
VkImageLayout newImageLayout,
VkImageSubresourceRange subresourceRange);
// Debug Markers. See https://github.com/KhronosGroup/Vulkan-Docs/blob/1.0-VK_EXT_debug_marker/doc/specs/vulkan/appendices/VK_EXT_debug_marker.txt
void debugMarkerBeginEXT(NVK& nvk, const char* pMarkerName, float r, float g, float b)
{
if(nvk.pfnCmdDebugMarkerBeginEXT)
{
VkDebugMarkerMarkerInfoEXT markerInfo = {};
float color[4] = {r, g, b, 0.0f};
markerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_MARKER_MARKER_INFO_EXT;
memcpy(markerInfo.color, color, sizeof(float) * 4);
markerInfo.pMarkerName = pMarkerName;
nvk.pfnCmdDebugMarkerBeginEXT(m_cmdbuffer, &markerInfo);
}
}
void debugMarkerInsertEXT(NVK& nvk, std::string markerName, float r, float g, float b)
{
if(nvk.pfnCmdDebugMarkerInsertEXT)
{
VkDebugMarkerMarkerInfoEXT markerInfo = {};
float color[4] = {r, g, b, 0.0f};
markerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_MARKER_MARKER_INFO_EXT;
memcpy(markerInfo.color, color, sizeof(float) * 4);
markerInfo.pMarkerName = markerName.c_str();
nvk.pfnCmdDebugMarkerInsertEXT(m_cmdbuffer, &markerInfo);
}
}
void debugMarkerEndEXT(NVK& nvk)
{
if(nvk.pfnCmdDebugMarkerEndEXT)
{
nvk.pfnCmdDebugMarkerEndEXT(m_cmdbuffer);
}
}
operator VkCommandBuffer() { return m_cmdbuffer; }
operator const VkCommandBuffer() const { return m_cmdbuffer; }
operator VkCommandBuffer*() { return &m_cmdbuffer; }
operator const VkCommandBuffer*() const { return &m_cmdbuffer; }
operator uint64_t() { return (uint64_t)m_cmdbuffer; }
//private:
VkCommandBuffer m_cmdbuffer;
};
//
// class for Command Pool
//
class CommandPool
{
public:
CommandPool(VkDevice device = VK_NULL_HANDLE, VkCommandPool cmdPool = VK_NULL_HANDLE);
CommandPool(const CommandPool& srcPool);
// remember: resetCommandPool() will reset every command-buffer related to it
void resetCommandPool(VkCommandPoolResetFlags flags);
void destroyCommandPool();
VkCommandBuffer allocateCommandBuffers(bool primary, uint32_t nCmdBuffers, VkCommandBuffer* cmdBuffers);
VkCommandBuffer allocateCommandBuffer(bool primary);
void freeCommandBuffers(uint32_t nCmdBuffers, VkCommandBuffer* cmdBuffers);
void freeCommandBuffer(VkCommandBuffer cmdBuffer);
operator VkCommandPool() { return m_cmdPool; }
operator const VkCommandPool() const { return m_cmdPool; }
operator VkCommandPool*() { return &m_cmdPool; }
operator const VkCommandPool*() const { return &m_cmdPool; }
operator uint64_t() { return (uint64_t)m_cmdPool; }
//private:
VkCommandPool m_cmdPool;
VkDevice m_device;
int m_currentCmdBufferSlot[2];
#define BUFFERPRIMARY 0
#define BUFFERSECONDARY 1
std::vector<VkCommandBuffer> m_allocatedCmdBuffers[2];
};
bool m_deviceExternal;
nvvk::SwapChain* m_swapChain;
VkDevice m_device;
VkInstance m_instance;
//vk_ext_debug_report.h:
PFN_vkCreateDebugReportCallbackEXT m_CreateDebugReportCallback;
PFN_vkDestroyDebugReportCallbackEXT m_DestroyDebugReportCallback;
VkDebugReportCallbackEXT m_msg_callback;
PFN_vkDebugReportMessageEXT m_DebugReportMessage;
//
// Physical device stuff
//
struct GPU
{
VkPhysicalDevice device;
VkPhysicalDeviceMemoryProperties memoryProperties;
VkPhysicalDeviceProperties properties;
VkPhysicalDeviceFeatures2 features2;
std::vector<VkQueueFamilyProperties> queueProperties;
void clear()
{
memset(&device, 0, sizeof(VkPhysicalDevice));
memset(&memoryProperties, 0, sizeof(VkPhysicalDeviceMemoryProperties));
memset(&properties, 0, sizeof(VkPhysicalDeviceProperties));
memset(&features2, 0, sizeof(VkPhysicalDeviceFeatures2));
queueProperties.clear();
}
};
GPU m_gpu;
VkQueue m_queue;
//
// Initialization utilities
//
bool utInitialize(bool bValidationLayer, WindowSurface* pWindowSurface = NULL);
bool utDestroy();
//
// ut... : methods that don't really correspond to VK API
//
//VkDeviceMemory utAllocMemAndBindObject(VkObject obj, VkObjectType type, VkFlags memProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkDeviceMemory utAllocMemAndBindBuffer(VkBuffer obj, VkFlags memProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkDeviceMemory utAllocMemAndBindImage(VkImage obj, VkFlags memProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
MemoryChunk utAllocateMemory(size_t size, VkFlags usage, VkFlags memProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkResult utFillBuffer(CommandPool* cmdPool, size_t size, VkResult result, const void* data, VkBuffer buffer, VkDeviceSize offset = 0);
void utFillImage(CommandPool* cmdPool, BufferImageCopy& bufferImageCopy, const void* data, VkDeviceSize dataSz, VkImage image);
VkBuffer utCreateAndFillBuffer(CommandPool* cmdPool,
size_t size,
const void* data,
VkFlags usage,
VkDeviceMemory& bufferMem,
VkFlags memProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkImage utCreateImage1D(uint32_t width,
VkDeviceMemory& colorMemory,
VkFormat format,
VkSampleCountFlagBits depthSamples = VK_SAMPLE_COUNT_1_BIT,
VkSampleCountFlagBits colorSamples = VK_SAMPLE_COUNT_1_BIT,
uint32_t mipLevels = 1,
bool asAttachment = false);
VkImage utCreateImage2D(uint32_t width,
uint32_t height,
VkDeviceMemory& colorMemory,
VkFormat format,
VkSampleCountFlagBits depthSamples = VK_SAMPLE_COUNT_1_BIT,
VkSampleCountFlagBits colorSamples = VK_SAMPLE_COUNT_1_BIT,
uint32_t mipLevels = 1,
bool asAttachment = false);
VkImage utCreateImage3D(uint32_t width,
uint32_t height,
uint32_t depth,
VkDeviceMemory& colorMemory,
VkFormat format,
VkSampleCountFlagBits depthSamples = VK_SAMPLE_COUNT_1_BIT,
VkSampleCountFlagBits colorSamples = VK_SAMPLE_COUNT_1_BIT,
uint32_t mipLevels = 1,
bool asAttachment = false);
VkImage utCreateImageCube(uint32_t width,
VkDeviceMemory& colorMemory,
VkFormat format,
VkSampleCountFlagBits depthSamples = VK_SAMPLE_COUNT_1_BIT,
VkSampleCountFlagBits colorSamples = VK_SAMPLE_COUNT_1_BIT,
uint32_t mipLevels = 1,
bool asAttachment = false);
void utMemcpy(VkDeviceMemory dstMem, const void* srcData, VkDeviceSize size);
//
// VULKAN function Overrides: similar to Vulkan API but simplified arguments (using structs...)
// NON-EXHAUSTIVE LIST: need to add missing ones when needed
//
VkBuffer createBuffer(BufferCreateInfo& bci);
VkResult bindBufferMemory(VkBuffer& buffer, VkDeviceMemory mem, VkDeviceSize offset);
VkShaderModule createShaderModule(const char* shaderCode, size_t size);
VkBufferView createBufferView(VkBuffer buffer, VkFormat format, VkDeviceSize size);
VkFramebuffer createFramebuffer(FramebufferCreateInfo& fbinfo);
VkResult createCommandPool(const VkCommandPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, CommandPool* commandPool);
void freeMemory(VkDeviceMemory mem);
void* mapMemory(VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags);
void unmapMemory(VkDeviceMemory mem);
void destroyBufferView(VkBufferView bufferView);
void destroyBuffer(VkBuffer buffer);
void destroyRenderPass(VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator);
void destroyFramebuffer(VkFramebuffer framebuffer, const VkAllocationCallbacks* pAllocator);
void destroyDescriptorSetLayout(VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks* pAllocator);
void destroyDescriptorPool(VkDescriptorPool descriptorPool, const VkAllocationCallbacks* pAllocator);
void destroyPipelineLayout(VkPipelineLayout pipelineLayout, const VkAllocationCallbacks* pAllocator);
void destroyPipeline(VkPipeline pipeline, const VkAllocationCallbacks* pAllocator);
VkRenderPass createRenderPass(RenderPassCreateInfo& rpinfo);
VkSemaphore createSemaphore(VkSemaphoreCreateFlags flags = 0);
void destroySemaphore(VkSemaphore s);
VkFence createFence(VkFenceCreateFlags flags = 0);
void destroyFence(VkFence fence);
bool waitForFences(uint32_t fenceCount, const VkFence* pFences, VkBool32 waitAll, uint64_t timeout);
void resetFences(uint32_t fenceCount, const VkFence* pFences);
void allocateDescriptorSets(const NVK::DescriptorSetAllocateInfo& allocateInfo, VkDescriptorSet* pDescriptorSets);
VkResult freeDescriptorSets(VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, const VkDescriptorSet* pDescriptorSets);
VkSampler createSampler(const NVK::SamplerCreateInfo& createInfo);
void destroySampler(VkSampler s);
VkImageView createImageView(const NVK::ImageViewCreateInfo& createInfo);
void destroyImageView(VkImageView s);
void destroyImage(VkImage s);
void queueSubmit(const NVK::SubmitInfo& submits, VkFence fence);
VkResult queueWaitIdle();
VkResult deviceWaitIdle();
VkResult createQueryPool(const VkQueryPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkQueryPool* pQueryPool);
void destroyQueryPool(VkQueryPool queryPool, const VkAllocationCallbacks* pAllocator);
VkResult getQueryPoolResults(VkQueryPool queryPool,
uint32_t firstQuery,
uint32_t queryCount,
size_t dataSize,
void* pData,
VkDeviceSize stride,
VkQueryResultFlags flags);
//
// Debug Markers
// Extension spec can be found at https://github.com/KhronosGroup/Vulkan-Docs/blob/1.0-VK_EXT_debug_marker/doc/specs/vulkan/appendices/VK_EXT_debug_marker.txt
//
void debugMarkerSetObjectNameEXT(uint64_t object, VkDebugReportObjectTypeEXT objectType, const char* name)
{
// Check for valid function pointer (may not be present if not running in a debugging application)
if(pfnDebugMarkerSetObjectNameEXT)
{
VkDebugMarkerObjectNameInfoEXT nameInfo = {};
nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_MARKER_OBJECT_NAME_INFO_EXT;
nameInfo.objectType = objectType;
nameInfo.object = object;
nameInfo.pObjectName = name;
pfnDebugMarkerSetObjectNameEXT(m_device, &nameInfo);
}
}
// Set the tag for an object
void debugMarkerSetObjectTagEXT(uint64_t object, VkDebugReportObjectTypeEXT objectType, uint64_t name, size_t tagSize, const void* tag)
{
// Check for valid function pointer (may not be present if not running in a debugging application)
if(pfnDebugMarkerSetObjectTagEXT)
{
VkDebugMarkerObjectTagInfoEXT tagInfo = {};
tagInfo.sType = VK_STRUCTURE_TYPE_DEBUG_MARKER_OBJECT_TAG_INFO_EXT;
tagInfo.objectType = objectType;
tagInfo.object = object;
tagInfo.tagName = name;
tagInfo.tagSize = tagSize;
tagInfo.pTag = tag;
pfnDebugMarkerSetObjectTagEXT(m_device, &tagInfo);
}
}
//
// overloadind strcuts for better constructors
//
struct Offset2D : VkOffset2D
{
Offset2D(int32_t _x, int32_t _y)
{
x = _x;
y = _y;
}
};
struct Offset3D : VkOffset3D
{
Offset3D(int32_t _x, int32_t _y, int32_t _z)
{
x = _x;
y = _y;
z = _z;
}
};
struct Extent2D : VkExtent2D
{
Extent2D(int32_t _width, int32_t _height)
{
width = _width;
height = _height;
}
};
struct Rect2D : VkRect2D
{
Rect2D() {}
Rect2D(const VkRect2D& r)
{
offset = r.offset;
extent = r.extent;
}
Rect2D(const Offset2D& _offset, const Extent2D& _extent)
{
offset = _offset;
extent = _extent;
}
Rect2D(int32_t originX, int32_t originY, uint32_t width, uint32_t height)
{
offset.x = originX;
offset.y = originY;
extent.width = width;
extent.height = height;
}
operator VkRect2D*() { return this; }
Rect2D& operator=(const VkRect2D& r)
{
offset = r.offset;
extent = r.extent;
return *this;
}
};
struct Extent3D : VkExtent3D
{
Extent3D() {}
Extent3D(int32_t _width, int32_t _height, int32_t _depth)
{
width = _width;
height = _height;
depth = _depth;
}
};
struct Float4
{
Float4() { memset(v, 0, sizeof(float) * 4); }
Float4(float x, float y, float z, float w)
{
v[0] = x;
v[1] = y;
v[2] = z;
v[3] = w;
}
operator float*() { return v; }
float v[4];
};
//---------------------------------
struct ImageSubresource : VkImageSubresource
{
ImageSubresource(VkImageAspectFlags _aspectMask, uint32_t _mipLevel, uint32_t _arrayLayer)
{
aspectMask = _aspectMask;
mipLevel = _mipLevel;
arrayLayer = _arrayLayer;
}
ImageSubresource() {}
};
//---------------------------------
struct ImageSubresourceLayers : VkImageSubresourceLayers
{
ImageSubresourceLayers(VkImageAspectFlags _aspectMask, uint32_t _mipLevel, uint32_t _baseArrayLayer, uint32_t _layerCount)
{
aspectMask = _aspectMask;
mipLevel = _mipLevel;
baseArrayLayer = _baseArrayLayer;
layerCount = _layerCount;
}
ImageSubresourceLayers() {}
};
//---------------------------------
class BufferImageCopy
{
public:
BufferImageCopy()
{
mipLevels = 0;
layerCount = 0;
}
BufferImageCopy(const BufferImageCopy& src) { s = src.s; }
BufferImageCopy(VkDeviceSize bufferOffset,
uint32_t bufferRowLength,
uint32_t bufferImageHeight,
ImageSubresourceLayers const& imageSubresource,
Offset3D const& imageOffset,
Extent3D const& imageExtent,
int miplevel = 0,
int layer = 0)
{
operator()(bufferOffset, bufferRowLength, bufferImageHeight, imageSubresource, imageOffset, imageExtent, miplevel, layer);
}
inline BufferImageCopy& operator()(VkDeviceSize bufferOffset,
uint32_t bufferRowLength,
uint32_t bufferImageHeight,
ImageSubresourceLayers const& imageSubresource,
Offset3D const& imageOffset,
Extent3D const& imageExtent,
int miplevel = 0,
int layer = 0)
{
VkBufferImageCopy b = {bufferOffset, bufferRowLength, bufferImageHeight,
imageSubresource, imageOffset, imageExtent};
miplevel++;
layer++;
if(mipLevels < miplevel)
mipLevels = miplevel;
if(layerCount < layer)
layerCount = layer;
s.push_back(b);
return *this;
}
void add(VkDeviceSize bufferOffset,
uint32_t bufferRowLength,
uint32_t bufferImageHeight,
ImageSubresourceLayers const& imageSubresource,
Offset3D const& imageOffset,
Extent3D const& imageExtent,
int miplevel = 0,
int layer = 0)
{
operator()(bufferOffset, bufferRowLength, bufferImageHeight, imageSubresource, imageOffset, imageExtent, miplevel, layer);
}
inline BufferImageCopy& operator=(const BufferImageCopy& src)
{
s = src.s;
return *this;
}
inline size_t size() { return s.size(); }
inline VkBufferImageCopy* getItem(int i = 0) { return &(s[i]); }
inline const VkBufferImageCopy* getItemCst(int i = 0) const { return &(s[i]); }
int mipLevels;
int layerCount;
private:
std::vector<VkBufferImageCopy> s;
};
//---------------------------------
class ImageCreateInfo
{
public:
ImageCreateInfo(VkFormat format,
Extent3D extent,
VkImageType imageType = VK_IMAGE_TYPE_2D,
uint32_t mipLevels = 1,
uint32_t arrayLayers = 1,
VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT,
VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL,
VkImageUsageFlags usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
VkImageCreateFlags flags = 0,
VkSharingMode sharingMode = VK_SHARING_MODE_EXCLUSIVE,
uint32_t queueFamilyIndexCount = 0,
const uint32_t* pQueueFamilyIndices = NULL)
{
s.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
s.pNext = NULL;
s.imageType = imageType;
s.format = format;
s.extent = extent;
s.mipLevels = mipLevels;
s.arrayLayers = arrayLayers;
s.samples = samples;
s.tiling = tiling;
s.usage = usage;
s.flags = flags;
s.sharingMode = sharingMode;
s.queueFamilyIndexCount = queueFamilyIndexCount;
s.pQueueFamilyIndices = pQueueFamilyIndices;
}
private:
VkImageCreateInfo s;
};
//---------------------------------
class SamplerCreateInfo
{
public:
SamplerCreateInfo() {}
SamplerCreateInfo(VkFilter magFilter,
VkFilter minFilter,
VkSamplerMipmapMode mipmapMode,
VkSamplerAddressMode addressModeU,
VkSamplerAddressMode addressModeV,
VkSamplerAddressMode addressModeW,
float mipLodBias,
VkBool32 anisotropyEnable,
float maxAnisotropy,
VkBool32 compareEnable,
VkCompareOp compareOp,
float minLod,
float maxLod,
VkBorderColor borderColor,
VkBool32 unnormalizedCoordinates)
{
s.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
s.pNext = NULL;
s.magFilter = magFilter;
s.minFilter = minFilter;
s.mipmapMode = mipmapMode;
s.addressModeU = addressModeU;
s.addressModeV = addressModeV;
s.addressModeW = addressModeW;
s.mipLodBias = mipLodBias;
s.anisotropyEnable = anisotropyEnable;
s.maxAnisotropy = maxAnisotropy;
s.compareEnable = compareEnable;
s.compareOp = compareOp;
s.minLod = minLod;
s.maxLod = maxLod;
s.borderColor = borderColor;
s.unnormalizedCoordinates = unnormalizedCoordinates;
s.flags = 0;
}
inline VkSamplerCreateInfo* getItem() { return &s; }
inline const VkSamplerCreateInfo* getItemCst() const { return &s; }
private:
VkSamplerCreateInfo s;
};
//---------------------------------
class ComponentMapping
{
public:
ComponentMapping()
{
s.r = VK_COMPONENT_SWIZZLE_R;
s.g = VK_COMPONENT_SWIZZLE_G;
s.b = VK_COMPONENT_SWIZZLE_B;
s.a = VK_COMPONENT_SWIZZLE_A;
}
ComponentMapping(VkComponentSwizzle r, VkComponentSwizzle g, VkComponentSwizzle b, VkComponentSwizzle a)
{
s.r = r;
s.g = g;
s.b = b;
s.a = a;
}
inline VkComponentMapping* getItem() { return &s; }
inline const VkComponentMapping* getItemCst() const { return &s; }
VkComponentMapping s;
};
//---------------------------------
class BufferCreateInfo : public VkBufferCreateInfo
{
public:
BufferCreateInfo(VkDeviceSize size_,
VkBufferUsageFlags usage_,
VkBufferCreateFlags flags_ = 0,
VkSharingMode sharingMode_ = VK_SHARING_MODE_EXCLUSIVE,
uint32_t queueFamilyIndexCount_ = 0,
const uint32_t* pQueueFamilyIndices_ = NULL)
{
sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
pNext = NULL;
size = size_;
usage = usage_;
flags = flags_;
sharingMode = sharingMode_;
queueFamilyIndexCount = queueFamilyIndexCount_;
pQueueFamilyIndices = pQueueFamilyIndices_;
}
inline VkBufferCreateInfo* getItem() { return this; }
inline const VkBufferCreateInfo* getItemCst() const { return this; }
operator VkBufferCreateInfo*() { return this; }
operator const VkBufferCreateInfo*() const { return this; }
};
//---------------------------------
class ImageSubresourceRange
{
public:
ImageSubresourceRange(VkImageAspectFlags aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
uint32_t baseMipLevel = 0,
uint32_t levelCount = 1,
uint32_t baseArrayLayer = 0,
uint32_t layerCount = 1)
{
s.aspectMask = aspectMask;
s.baseMipLevel = baseMipLevel;
s.levelCount = levelCount;
s.baseArrayLayer = baseArrayLayer;
s.layerCount = layerCount;
}
inline VkImageSubresourceRange* getItem() { return &s; }
inline const VkImageSubresourceRange* getItemCst() const { return &s; }
operator VkImageSubresourceRange*() { return &s; }
operator const VkImageSubresourceRange*() const { return &s; }
VkImageSubresourceRange s;
};
//---------------------------------
class ImageViewCreateInfo
{
public:
ImageViewCreateInfo() {}
ImageViewCreateInfo(VkImage image, VkImageViewType viewType, VkFormat format, const ComponentMapping& components, const ImageSubresourceRange& subresourceRange)
{
s.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
s.pNext = NULL;
s.image = image;
s.viewType = viewType;
s.format = format;
s.components = components.s;
s.subresourceRange = subresourceRange.s;
s.flags = 0;
}
inline VkImageViewCreateInfo* getItem() { return &s; }
inline const VkImageViewCreateInfo* getItemCst() const { return &s; }
private:
VkImageViewCreateInfo s;
};
//---------------------------------
class DescriptorSetAllocateInfo
{
public:
DescriptorSetAllocateInfo(VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, const VkDescriptorSetLayout* pSetLayouts)
{
s.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
s.pNext = NULL;
s.descriptorPool = descriptorPool;
s.descriptorSetCount = descriptorSetCount;
s.pSetLayouts = pSetLayouts;
}
inline VkDescriptorSetAllocateInfo* getItem() { return &s; }
inline const VkDescriptorSetAllocateInfo* getItemCst() const { return &s; }
inline operator VkDescriptorSetAllocateInfo*() { return &s; }
inline operator const VkDescriptorSetAllocateInfo*() const { return &s; }
private:
VkDescriptorSetAllocateInfo s;
};
//---------------------------------
class DescriptorSetLayoutBinding
{
public:
DescriptorSetLayoutBinding() {}
DescriptorSetLayoutBinding(uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags)
{
operator()(binding, descriptorType, descriptorCount, stageFlags);
}
inline DescriptorSetLayoutBinding& operator()(uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags)
{
VkDescriptorSetLayoutBinding b = {binding, descriptorType, descriptorCount, stageFlags, NULL /*pImmutableSamplers*/};
bindings.push_back(b);
return *this;
}
inline int size() const { return (int)bindings.size(); }
inline VkDescriptorSetLayoutBinding* getBindings() { return &(bindings[0]); }
private:
std::vector<VkDescriptorSetLayoutBinding> bindings;
};
//---------------------------------
class DescriptorSetLayoutCreateInfo
{
public:
DescriptorSetLayoutCreateInfo(const DescriptorSetLayoutBinding& dslb_, VkDescriptorSetLayoutCreateFlags flags = 0)
{
dslb = dslb_;
descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutCreateInfo.pNext = NULL;
descriptorSetLayoutCreateInfo.flags = flags;
descriptorSetLayoutCreateInfo.bindingCount = dslb.size(); // NUM_UBOS;//sizeof(bindings)/sizeof(bindings[0]);
descriptorSetLayoutCreateInfo.pBindings = dslb.getBindings();
}
VkDescriptorSetLayoutCreateInfo* getItem() { return &descriptorSetLayoutCreateInfo; }
const VkDescriptorSetLayoutCreateInfo* getItemCst() const { return &descriptorSetLayoutCreateInfo; }
private:
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo;
DescriptorSetLayoutBinding dslb;
};
//----------------------------------
VkDescriptorSetLayout createDescriptorSetLayout(const DescriptorSetLayoutCreateInfo& descriptorSetLayoutCreateInfo);
VkPipelineLayout createPipelineLayout(VkDescriptorSetLayout* dsls, uint32_t count);
//---------------------------------
class DescriptorPoolSize
{
public:
DescriptorPoolSize() {}
DescriptorPoolSize(VkDescriptorType t, uint32_t cnt = 1)
{
VkDescriptorPoolSize b = {t, cnt};
typecount.push_back(b);
}
inline DescriptorPoolSize& operator()(VkDescriptorType t, uint32_t cnt = 1)
{
VkDescriptorPoolSize b = {t, cnt};
typecount.push_back(b);
return *this;
}
inline int size() const { return (int)typecount.size(); }
inline VkDescriptorPoolSize* getItem(int n = 0) { return &(typecount[n]); }
inline const VkDescriptorPoolSize* getItemCst(int n = 0) const { return &(typecount[n]); }
private:
std::vector<VkDescriptorPoolSize> typecount;
};
//---------------------------------
class DescriptorPoolCreateInfo
{
public:
DescriptorPoolCreateInfo(uint32_t maxSets, const DescriptorPoolSize& typeCount, VkDescriptorPoolCreateFlags flags = 0)
{
s.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
s.pNext = NULL;
s.flags = flags;
s.maxSets = maxSets;
t = typeCount;
s.poolSizeCount = t.size();
s.pPoolSizes = t.getItem(0);
}
inline VkDescriptorPoolCreateInfo* getItem() { return &s; }
inline const VkDescriptorPoolCreateInfo* getItemCst() const { return &s; }
private:
VkDescriptorPoolCreateInfo s;
DescriptorPoolSize t;
};
//----------------------------------
VkDescriptorPool createDescriptorPool(const DescriptorPoolCreateInfo& descriptorPoolCreateInfo);
//---------------------------------
class DescriptorImageInfo
{
public:
DescriptorImageInfo() {}
DescriptorImageInfo(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout)
{
operator()(sampler, imageView, imageLayout);
}
inline DescriptorImageInfo& operator()(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout)
{
VkDescriptorImageInfo b;
b.sampler = sampler;
b.imageView = imageView;
b.imageLayout = imageLayout;
attachInfos.push_back(b);
return *this;
}
inline int size() const { return (int)attachInfos.size(); }
inline VkDescriptorImageInfo* getItem(int n = 0) { return &(attachInfos[n]); }
private:
std::vector<VkDescriptorImageInfo> attachInfos;
};
class DescriptorBufferInfo
{
public:
DescriptorBufferInfo() {}
//VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
DescriptorBufferInfo(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize range)
{
operator()(buffer, offset, range);
}
inline DescriptorBufferInfo& operator()(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize range)
{
VkDescriptorBufferInfo b;
b.buffer = buffer;
b.offset = offset;
b.range = range;
attachInfos.push_back(b);
return *this;
}
inline int size() const { return (int)attachInfos.size(); }
inline VkDescriptorBufferInfo* getItem(int n = 0) { return &(attachInfos[n]); }
inline const VkDescriptorBufferInfo* getItemCst(int n = 0) const { return &(attachInfos[n]); }
private:
std::vector<VkDescriptorBufferInfo> attachInfos;
};
//----------------------------------
class WriteDescriptorSet
{
public:
WriteDescriptorSet() {}
WriteDescriptorSet(VkDescriptorSet descSetDest, uint32_t binding, uint32_t arrayIndex, DescriptorBufferInfo& bufferInfos, VkDescriptorType descriptorType)
{
operator()(descSetDest, binding, arrayIndex, bufferInfos, descriptorType);
}
inline WriteDescriptorSet& operator()(VkDescriptorSet descSetDest,
uint32_t binding,
uint32_t arrayIndex,
DescriptorBufferInfo& bufferInfos,
VkDescriptorType descriptorType)
{
VkWriteDescriptorSet ub = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
ub.pNext = NULL;
ub.dstSet = descSetDest;
ub.dstBinding = binding;
ub.dstArrayElement = arrayIndex;
ub.descriptorCount = bufferInfos.size();
ub.descriptorType = descriptorType;
ub.pImageInfo = NULL;
ub.pBufferInfo = bufferInfos.getItem(0);