-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathgraphicsplugin_vulkan.cpp
2000 lines (1685 loc) · 83.2 KB
/
graphicsplugin_vulkan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
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) 2017-2025 The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0
#include "pch.h"
#include "common.h"
#include "geometry.h"
#include "graphicsplugin.h"
#include "options.h"
#ifdef XR_USE_GRAPHICS_API_VULKAN
#include <common/vulkan_debug_object_namer.hpp>
#include <common/xr_linear.h>
#ifdef USE_ONLINE_VULKAN_SHADERC
#include <shaderc/shaderc.hpp>
#endif
#if defined(VK_USE_PLATFORM_WIN32_KHR)
// Define USE_MIRROR_WINDOW to open a otherwise-unused window for e.g. RenderDoc
#define USE_MIRROR_WINDOW
#endif
// glslangValidator doesn't wrap its output in brackets if you don't have it define the whole array.
#if defined(USE_GLSLANGVALIDATOR)
#define SPV_PREFIX {
#define SPV_SUFFIX }
#else
#define SPV_PREFIX
#define SPV_SUFFIX
#endif
namespace {
static std::string vkResultString(VkResult res) {
switch (res) {
case VK_SUCCESS:
return "SUCCESS";
case VK_NOT_READY:
return "NOT_READY";
case VK_TIMEOUT:
return "TIMEOUT";
case VK_EVENT_SET:
return "EVENT_SET";
case VK_EVENT_RESET:
return "EVENT_RESET";
case VK_INCOMPLETE:
return "INCOMPLETE";
case VK_ERROR_OUT_OF_HOST_MEMORY:
return "ERROR_OUT_OF_HOST_MEMORY";
case VK_ERROR_OUT_OF_DEVICE_MEMORY:
return "ERROR_OUT_OF_DEVICE_MEMORY";
case VK_ERROR_INITIALIZATION_FAILED:
return "ERROR_INITIALIZATION_FAILED";
case VK_ERROR_DEVICE_LOST:
return "ERROR_DEVICE_LOST";
case VK_ERROR_MEMORY_MAP_FAILED:
return "ERROR_MEMORY_MAP_FAILED";
case VK_ERROR_LAYER_NOT_PRESENT:
return "ERROR_LAYER_NOT_PRESENT";
case VK_ERROR_EXTENSION_NOT_PRESENT:
return "ERROR_EXTENSION_NOT_PRESENT";
case VK_ERROR_FEATURE_NOT_PRESENT:
return "ERROR_FEATURE_NOT_PRESENT";
case VK_ERROR_INCOMPATIBLE_DRIVER:
return "ERROR_INCOMPATIBLE_DRIVER";
case VK_ERROR_TOO_MANY_OBJECTS:
return "ERROR_TOO_MANY_OBJECTS";
case VK_ERROR_FORMAT_NOT_SUPPORTED:
return "ERROR_FORMAT_NOT_SUPPORTED";
case VK_ERROR_SURFACE_LOST_KHR:
return "ERROR_SURFACE_LOST_KHR";
case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
return "ERROR_NATIVE_WINDOW_IN_USE_KHR";
case VK_SUBOPTIMAL_KHR:
return "SUBOPTIMAL_KHR";
case VK_ERROR_OUT_OF_DATE_KHR:
return "ERROR_OUT_OF_DATE_KHR";
case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
return "ERROR_INCOMPATIBLE_DISPLAY_KHR";
case VK_ERROR_VALIDATION_FAILED_EXT:
return "ERROR_VALIDATION_FAILED_EXT";
case VK_ERROR_INVALID_SHADER_NV:
return "ERROR_INVALID_SHADER_NV";
default:
return std::to_string(res);
}
}
[[noreturn]] inline void ThrowVkResult(VkResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
Throw(Fmt("VkResult failure [%s]", vkResultString(res).c_str()), originator, sourceLocation);
}
inline VkResult CheckVkResult(VkResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
if ((res) < VK_SUCCESS) {
ThrowVkResult(res, originator, sourceLocation);
}
return res;
}
// XXX These really shouldn't have trailing ';'s
#define THROW_VK(res, cmd) ThrowVkResult(res, #cmd, FILE_AND_LINE);
#define CHECK_VKCMD(cmd) CheckVkResult(cmd, #cmd, FILE_AND_LINE);
#define CHECK_VKRESULT(res, cmdStr) CheckVkResult(res, cmdStr, FILE_AND_LINE);
#ifdef USE_ONLINE_VULKAN_SHADERC
constexpr char VertexShaderGlsl[] =
R"_(
#version 430
#extension GL_ARB_separate_shader_objects : enable
layout (std140, push_constant) uniform buf
{
mat4 mvp;
} ubuf;
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Color;
layout (location = 0) out vec4 oColor;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
oColor.rgba = Color.rgba;
gl_Position = ubuf.mvp * Position;
}
)_";
constexpr char FragmentShaderGlsl[] =
R"_(
#version 430
#extension GL_ARB_separate_shader_objects : enable
layout (location = 0) in vec4 oColor;
layout (location = 0) out vec4 FragColor;
void main()
{
FragColor = oColor;
}
)_";
#endif // USE_ONLINE_VULKAN_SHADERC
struct MemoryAllocator {
void Init(VkPhysicalDevice physicalDevice, VkDevice device) {
m_vkDevice = device;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &m_memProps);
}
static const VkFlags defaultFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
void Allocate(VkMemoryRequirements const& memReqs, VkDeviceMemory* mem, VkFlags flags = defaultFlags,
const void* pNext = nullptr) const {
// Search memtypes to find first index with those properties
for (uint32_t i = 0; i < m_memProps.memoryTypeCount; ++i) {
if ((memReqs.memoryTypeBits & (1 << i)) != 0u) {
// Type is available, does it match user properties?
if ((m_memProps.memoryTypes[i].propertyFlags & flags) == flags) {
VkMemoryAllocateInfo memAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, pNext};
memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = i;
CHECK_VKCMD(vkAllocateMemory(m_vkDevice, &memAlloc, nullptr, mem));
return;
}
}
}
THROW("Memory format not supported");
}
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
VkPhysicalDeviceMemoryProperties m_memProps{};
};
// CmdBuffer - manage VkCommandBuffer state
struct CmdBuffer {
#define LIST_CMDBUFFER_STATES(_) \
_(Undefined) \
_(Initialized) \
_(Recording) \
_(Executable) \
_(Executing)
enum class CmdBufferState {
#define MK_ENUM(name) name,
LIST_CMDBUFFER_STATES(MK_ENUM)
#undef MK_ENUM
};
CmdBufferState state{CmdBufferState::Undefined};
VkCommandPool pool{VK_NULL_HANDLE};
VkCommandBuffer buf{VK_NULL_HANDLE};
VkFence execFence{VK_NULL_HANDLE};
CmdBuffer() = default;
CmdBuffer(const CmdBuffer&) = delete;
CmdBuffer& operator=(const CmdBuffer&) = delete;
CmdBuffer(CmdBuffer&&) = delete;
CmdBuffer& operator=(CmdBuffer&&) = delete;
~CmdBuffer() {
SetState(CmdBufferState::Undefined);
if (m_vkDevice != nullptr) {
if (buf != VK_NULL_HANDLE) {
vkFreeCommandBuffers(m_vkDevice, pool, 1, &buf);
}
if (pool != VK_NULL_HANDLE) {
vkDestroyCommandPool(m_vkDevice, pool, nullptr);
}
if (execFence != VK_NULL_HANDLE) {
vkDestroyFence(m_vkDevice, execFence, nullptr);
}
}
buf = VK_NULL_HANDLE;
pool = VK_NULL_HANDLE;
execFence = VK_NULL_HANDLE;
m_vkDevice = nullptr;
}
std::string StateString(CmdBufferState s) {
switch (s) {
#define MK_CASE(name) \
case CmdBufferState::name: \
return #name;
LIST_CMDBUFFER_STATES(MK_CASE)
#undef MK_CASE
}
return "(Unknown)";
}
#define CHECK_CBSTATE(s) \
do \
if (state != (s)) { \
Log::Write(Log::Level::Error, \
std::string("Expecting state " #s " from ") + __FUNCTION__ + ", in " + StateString(state)); \
return false; \
} \
while (0)
bool Init(const VulkanDebugObjectNamer& namer, VkDevice device, uint32_t queueFamilyIndex) {
CHECK_CBSTATE(CmdBufferState::Undefined);
m_vkDevice = device;
// Create a command pool to allocate our command buffer from
VkCommandPoolCreateInfo cmdPoolInfo{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
cmdPoolInfo.queueFamilyIndex = queueFamilyIndex;
CHECK_VKCMD(vkCreateCommandPool(m_vkDevice, &cmdPoolInfo, nullptr, &pool));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_COMMAND_POOL, (uint64_t)pool, "hello_xr command pool"));
// Create the command buffer from the command pool
VkCommandBufferAllocateInfo cmd{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
cmd.commandPool = pool;
cmd.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmd.commandBufferCount = 1;
CHECK_VKCMD(vkAllocateCommandBuffers(m_vkDevice, &cmd, &buf));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)buf, "hello_xr command buffer"));
VkFenceCreateInfo fenceInfo{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
CHECK_VKCMD(vkCreateFence(m_vkDevice, &fenceInfo, nullptr, &execFence));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_FENCE, (uint64_t)execFence, "hello_xr fence"));
SetState(CmdBufferState::Initialized);
return true;
}
bool Begin() {
CHECK_CBSTATE(CmdBufferState::Initialized);
VkCommandBufferBeginInfo cmdBeginInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
CHECK_VKCMD(vkBeginCommandBuffer(buf, &cmdBeginInfo));
SetState(CmdBufferState::Recording);
return true;
}
bool End() {
CHECK_CBSTATE(CmdBufferState::Recording);
CHECK_VKCMD(vkEndCommandBuffer(buf));
SetState(CmdBufferState::Executable);
return true;
}
bool Exec(VkQueue queue) {
CHECK_CBSTATE(CmdBufferState::Executable);
VkSubmitInfo submitInfo{VK_STRUCTURE_TYPE_SUBMIT_INFO};
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &buf;
CHECK_VKCMD(vkQueueSubmit(queue, 1, &submitInfo, execFence));
SetState(CmdBufferState::Executing);
return true;
}
bool Wait() {
// Waiting on a not-in-flight command buffer is a no-op
if (state == CmdBufferState::Initialized) {
return true;
}
CHECK_CBSTATE(CmdBufferState::Executing);
const uint32_t timeoutNs = 1 * 1000 * 1000 * 1000;
for (int i = 0; i < 5; ++i) {
auto res = vkWaitForFences(m_vkDevice, 1, &execFence, VK_TRUE, timeoutNs);
if (res == VK_SUCCESS) {
// Buffer can be executed multiple times...
SetState(CmdBufferState::Executable);
return true;
}
Log::Write(Log::Level::Info, "Waiting for CmdBuffer fence timed out, retrying...");
}
return false;
}
bool Reset() {
if (state != CmdBufferState::Initialized) {
CHECK_CBSTATE(CmdBufferState::Executable);
CHECK_VKCMD(vkResetFences(m_vkDevice, 1, &execFence));
CHECK_VKCMD(vkResetCommandBuffer(buf, 0));
SetState(CmdBufferState::Initialized);
}
return true;
}
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
void SetState(CmdBufferState newState) { state = newState; }
#undef CHECK_CBSTATE
#undef LIST_CMDBUFFER_STATES
};
// ShaderProgram to hold a pair of vertex & fragment shaders
struct ShaderProgram {
std::array<VkPipelineShaderStageCreateInfo, 2> shaderInfo{
{{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}, {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}}};
ShaderProgram() = default;
~ShaderProgram() {
if (m_vkDevice != nullptr) {
for (auto& si : shaderInfo) {
if (si.module != VK_NULL_HANDLE) {
vkDestroyShaderModule(m_vkDevice, shaderInfo[0].module, nullptr);
}
si.module = VK_NULL_HANDLE;
}
}
shaderInfo = {};
m_vkDevice = nullptr;
}
ShaderProgram(const ShaderProgram&) = delete;
ShaderProgram& operator=(const ShaderProgram&) = delete;
ShaderProgram(ShaderProgram&&) = delete;
ShaderProgram& operator=(ShaderProgram&&) = delete;
void LoadVertexShader(const std::vector<uint32_t>& code) { Load(0, code); }
void LoadFragmentShader(const std::vector<uint32_t>& code) { Load(1, code); }
void Init(VkDevice device) { m_vkDevice = device; }
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
void Load(uint32_t index, const std::vector<uint32_t>& code) {
VkShaderModuleCreateInfo modInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
auto& si = shaderInfo[index];
si.pName = "main";
std::string name;
switch (index) {
case 0:
si.stage = VK_SHADER_STAGE_VERTEX_BIT;
name = "vertex";
break;
case 1:
si.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
name = "fragment";
break;
default:
THROW(Fmt("Unknown code index %d", index));
}
modInfo.codeSize = code.size() * sizeof(code[0]);
modInfo.pCode = &code[0];
CHECK_MSG((modInfo.codeSize > 0) && modInfo.pCode, Fmt("Invalid %s shader ", name.c_str()));
CHECK_VKCMD(vkCreateShaderModule(m_vkDevice, &modInfo, nullptr, &si.module));
Log::Write(Log::Level::Info, Fmt("Loaded %s shader", name.c_str()));
}
};
// VertexBuffer base class
struct VertexBufferBase {
VkBuffer idxBuf{VK_NULL_HANDLE};
VkDeviceMemory idxMem{VK_NULL_HANDLE};
VkBuffer vtxBuf{VK_NULL_HANDLE};
VkDeviceMemory vtxMem{VK_NULL_HANDLE};
VkVertexInputBindingDescription bindDesc{};
std::vector<VkVertexInputAttributeDescription> attrDesc{};
struct {
uint32_t idx;
uint32_t vtx;
} count = {0, 0};
VertexBufferBase() = default;
~VertexBufferBase() {
if (m_vkDevice != nullptr) {
if (idxBuf != VK_NULL_HANDLE) {
vkDestroyBuffer(m_vkDevice, idxBuf, nullptr);
}
if (idxMem != VK_NULL_HANDLE) {
vkFreeMemory(m_vkDevice, idxMem, nullptr);
}
if (vtxBuf != VK_NULL_HANDLE) {
vkDestroyBuffer(m_vkDevice, vtxBuf, nullptr);
}
if (vtxMem != VK_NULL_HANDLE) {
vkFreeMemory(m_vkDevice, vtxMem, nullptr);
}
}
idxBuf = VK_NULL_HANDLE;
idxMem = VK_NULL_HANDLE;
vtxBuf = VK_NULL_HANDLE;
vtxMem = VK_NULL_HANDLE;
bindDesc = {};
attrDesc.clear();
count = {0, 0};
m_vkDevice = nullptr;
}
VertexBufferBase(const VertexBufferBase&) = delete;
VertexBufferBase& operator=(const VertexBufferBase&) = delete;
VertexBufferBase(VertexBufferBase&&) = delete;
VertexBufferBase& operator=(VertexBufferBase&&) = delete;
void Init(VkDevice device, const MemoryAllocator* memAllocator, const std::vector<VkVertexInputAttributeDescription>& attr) {
m_vkDevice = device;
m_memAllocator = memAllocator;
attrDesc = attr;
}
protected:
VkDevice m_vkDevice{VK_NULL_HANDLE};
void AllocateBufferMemory(VkBuffer buf, VkDeviceMemory* mem) const {
VkMemoryRequirements memReq = {};
vkGetBufferMemoryRequirements(m_vkDevice, buf, &memReq);
m_memAllocator->Allocate(memReq, mem);
}
private:
const MemoryAllocator* m_memAllocator{nullptr};
};
// VertexBuffer template to wrap the indices and vertices
template <typename T>
struct VertexBuffer : public VertexBufferBase {
bool Create(uint32_t idxCount, uint32_t vtxCount) {
VkBufferCreateInfo bufInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bufInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
bufInfo.size = sizeof(uint16_t) * idxCount;
CHECK_VKCMD(vkCreateBuffer(m_vkDevice, &bufInfo, nullptr, &idxBuf));
AllocateBufferMemory(idxBuf, &idxMem);
CHECK_VKCMD(vkBindBufferMemory(m_vkDevice, idxBuf, idxMem, 0));
bufInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufInfo.size = sizeof(T) * vtxCount;
CHECK_VKCMD(vkCreateBuffer(m_vkDevice, &bufInfo, nullptr, &vtxBuf));
AllocateBufferMemory(vtxBuf, &vtxMem);
CHECK_VKCMD(vkBindBufferMemory(m_vkDevice, vtxBuf, vtxMem, 0));
bindDesc.binding = 0;
bindDesc.stride = sizeof(T);
bindDesc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
count = {idxCount, vtxCount};
return true;
}
void UpdateIndices(const uint16_t* data, uint32_t elements, uint32_t offset = 0) {
uint16_t* map = nullptr;
CHECK_VKCMD(vkMapMemory(m_vkDevice, idxMem, sizeof(map[0]) * offset, sizeof(map[0]) * elements, 0, (void**)&map));
for (size_t i = 0; i < elements; ++i) {
map[i] = data[i];
}
vkUnmapMemory(m_vkDevice, idxMem);
}
void UpdateVertices(const T* data, uint32_t elements, uint32_t offset = 0) {
T* map = nullptr;
CHECK_VKCMD(vkMapMemory(m_vkDevice, vtxMem, sizeof(map[0]) * offset, sizeof(map[0]) * elements, 0, (void**)&map));
for (size_t i = 0; i < elements; ++i) {
map[i] = data[i];
}
vkUnmapMemory(m_vkDevice, vtxMem);
}
};
// RenderPass wrapper
struct RenderPass {
VkFormat colorFmt{};
VkFormat depthFmt{};
VkRenderPass pass{VK_NULL_HANDLE};
RenderPass() = default;
bool Create(const VulkanDebugObjectNamer& namer, VkDevice device, VkFormat aColorFmt, VkFormat aDepthFmt) {
m_vkDevice = device;
colorFmt = aColorFmt;
depthFmt = aDepthFmt;
VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
VkAttachmentReference colorRef = {0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkAttachmentReference depthRef = {1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
std::array<VkAttachmentDescription, 2> at = {};
VkRenderPassCreateInfo rpInfo{VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO};
rpInfo.attachmentCount = 0;
rpInfo.pAttachments = at.data();
rpInfo.subpassCount = 1;
rpInfo.pSubpasses = &subpass;
if (colorFmt != VK_FORMAT_UNDEFINED) {
colorRef.attachment = rpInfo.attachmentCount++;
at[colorRef.attachment].format = colorFmt;
at[colorRef.attachment].samples = VK_SAMPLE_COUNT_1_BIT;
at[colorRef.attachment].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
at[colorRef.attachment].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
at[colorRef.attachment].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
at[colorRef.attachment].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
at[colorRef.attachment].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
at[colorRef.attachment].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorRef;
}
if (depthFmt != VK_FORMAT_UNDEFINED) {
depthRef.attachment = rpInfo.attachmentCount++;
at[depthRef.attachment].format = depthFmt;
at[depthRef.attachment].samples = VK_SAMPLE_COUNT_1_BIT;
at[depthRef.attachment].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
at[depthRef.attachment].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
at[depthRef.attachment].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
at[depthRef.attachment].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
at[depthRef.attachment].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
at[depthRef.attachment].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
subpass.pDepthStencilAttachment = &depthRef;
}
CHECK_VKCMD(vkCreateRenderPass(m_vkDevice, &rpInfo, nullptr, &pass));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)pass, "hello_xr render pass"));
return true;
}
~RenderPass() {
if (m_vkDevice != nullptr) {
if (pass != VK_NULL_HANDLE) {
vkDestroyRenderPass(m_vkDevice, pass, nullptr);
}
}
pass = VK_NULL_HANDLE;
m_vkDevice = nullptr;
}
RenderPass(const RenderPass&) = delete;
RenderPass& operator=(const RenderPass&) = delete;
RenderPass(RenderPass&&) = delete;
RenderPass& operator=(RenderPass&&) = delete;
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
};
// VkImage + framebuffer wrapper
struct RenderTarget {
VkImage colorImage{VK_NULL_HANDLE};
VkImage depthImage{VK_NULL_HANDLE};
VkImageView colorView{VK_NULL_HANDLE};
VkImageView depthView{VK_NULL_HANDLE};
VkFramebuffer fb{VK_NULL_HANDLE};
RenderTarget() = default;
~RenderTarget() {
if (m_vkDevice != nullptr) {
if (fb != VK_NULL_HANDLE) {
vkDestroyFramebuffer(m_vkDevice, fb, nullptr);
}
if (colorView != VK_NULL_HANDLE) {
vkDestroyImageView(m_vkDevice, colorView, nullptr);
}
if (depthView != VK_NULL_HANDLE) {
vkDestroyImageView(m_vkDevice, depthView, nullptr);
}
}
// Note we don't own color/depthImage, it will get destroyed when xrDestroySwapchain is called
colorImage = VK_NULL_HANDLE;
depthImage = VK_NULL_HANDLE;
colorView = VK_NULL_HANDLE;
depthView = VK_NULL_HANDLE;
fb = VK_NULL_HANDLE;
m_vkDevice = nullptr;
}
RenderTarget(RenderTarget&& other) noexcept : RenderTarget() {
using std::swap;
swap(colorImage, other.colorImage);
swap(depthImage, other.depthImage);
swap(colorView, other.colorView);
swap(depthView, other.depthView);
swap(fb, other.fb);
swap(m_vkDevice, other.m_vkDevice);
}
RenderTarget& operator=(RenderTarget&& other) noexcept {
if (&other == this) {
return *this;
}
// Clean up ourselves.
this->~RenderTarget();
using std::swap;
swap(colorImage, other.colorImage);
swap(depthImage, other.depthImage);
swap(colorView, other.colorView);
swap(depthView, other.depthView);
swap(fb, other.fb);
swap(m_vkDevice, other.m_vkDevice);
return *this;
}
void Create(const VulkanDebugObjectNamer& namer, VkDevice device, VkImage aColorImage, VkImage aDepthImage, VkExtent2D size,
RenderPass& renderPass) {
m_vkDevice = device;
colorImage = aColorImage;
depthImage = aDepthImage;
std::array<VkImageView, 2> attachments{};
uint32_t attachmentCount = 0;
// Create color image view
if (colorImage != VK_NULL_HANDLE) {
VkImageViewCreateInfo colorViewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
colorViewInfo.image = colorImage;
colorViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
colorViewInfo.format = renderPass.colorFmt;
colorViewInfo.components.r = VK_COMPONENT_SWIZZLE_R;
colorViewInfo.components.g = VK_COMPONENT_SWIZZLE_G;
colorViewInfo.components.b = VK_COMPONENT_SWIZZLE_B;
colorViewInfo.components.a = VK_COMPONENT_SWIZZLE_A;
colorViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
colorViewInfo.subresourceRange.baseMipLevel = 0;
colorViewInfo.subresourceRange.levelCount = 1;
colorViewInfo.subresourceRange.baseArrayLayer = 0;
colorViewInfo.subresourceRange.layerCount = 1;
CHECK_VKCMD(vkCreateImageView(m_vkDevice, &colorViewInfo, nullptr, &colorView));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_IMAGE_VIEW, (uint64_t)colorView, "hello_xr color image view"));
attachments[attachmentCount++] = colorView;
}
// Create depth image view
if (depthImage != VK_NULL_HANDLE) {
VkImageViewCreateInfo depthViewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
depthViewInfo.image = depthImage;
depthViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
depthViewInfo.format = renderPass.depthFmt;
depthViewInfo.components.r = VK_COMPONENT_SWIZZLE_R;
depthViewInfo.components.g = VK_COMPONENT_SWIZZLE_G;
depthViewInfo.components.b = VK_COMPONENT_SWIZZLE_B;
depthViewInfo.components.a = VK_COMPONENT_SWIZZLE_A;
depthViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
depthViewInfo.subresourceRange.baseMipLevel = 0;
depthViewInfo.subresourceRange.levelCount = 1;
depthViewInfo.subresourceRange.baseArrayLayer = 0;
depthViewInfo.subresourceRange.layerCount = 1;
CHECK_VKCMD(vkCreateImageView(m_vkDevice, &depthViewInfo, nullptr, &depthView));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_IMAGE_VIEW, (uint64_t)depthView, "hello_xr depth image view"));
attachments[attachmentCount++] = depthView;
}
VkFramebufferCreateInfo fbInfo{VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO};
fbInfo.renderPass = renderPass.pass;
fbInfo.attachmentCount = attachmentCount;
fbInfo.pAttachments = attachments.data();
fbInfo.width = size.width;
fbInfo.height = size.height;
fbInfo.layers = 1;
CHECK_VKCMD(vkCreateFramebuffer(m_vkDevice, &fbInfo, nullptr, &fb));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_FRAMEBUFFER, (uint64_t)fb, "hello_xr framebuffer"));
}
RenderTarget(const RenderTarget&) = delete;
RenderTarget& operator=(const RenderTarget&) = delete;
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
};
// Simple vertex MVP xform & color fragment shader layout
struct PipelineLayout {
VkPipelineLayout layout{VK_NULL_HANDLE};
PipelineLayout() = default;
~PipelineLayout() {
if (m_vkDevice != nullptr) {
if (layout != VK_NULL_HANDLE) {
vkDestroyPipelineLayout(m_vkDevice, layout, nullptr);
}
}
layout = VK_NULL_HANDLE;
m_vkDevice = nullptr;
}
void Create(VkDevice device) {
m_vkDevice = device;
// MVP matrix is a push_constant
VkPushConstantRange pcr = {};
pcr.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
pcr.offset = 0;
pcr.size = 4 * 4 * sizeof(float);
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
pipelineLayoutCreateInfo.pPushConstantRanges = &pcr;
CHECK_VKCMD(vkCreatePipelineLayout(m_vkDevice, &pipelineLayoutCreateInfo, nullptr, &layout));
}
PipelineLayout(const PipelineLayout&) = delete;
PipelineLayout& operator=(const PipelineLayout&) = delete;
PipelineLayout(PipelineLayout&&) = delete;
PipelineLayout& operator=(PipelineLayout&&) = delete;
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
};
// Pipeline wrapper for rendering pipeline state
struct Pipeline {
VkPipeline pipe{VK_NULL_HANDLE};
VkPrimitiveTopology topology{VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST};
std::vector<VkDynamicState> dynamicStateEnables;
Pipeline() = default;
void Dynamic(VkDynamicState state) { dynamicStateEnables.emplace_back(state); }
void Create(VkDevice device, VkExtent2D size, const PipelineLayout& layout, const RenderPass& rp, const ShaderProgram& sp,
const VertexBufferBase& vb) {
m_vkDevice = device;
VkPipelineDynamicStateCreateInfo dynamicState{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
dynamicState.dynamicStateCount = (uint32_t)dynamicStateEnables.size();
dynamicState.pDynamicStates = dynamicStateEnables.data();
VkPipelineVertexInputStateCreateInfo vi{VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
vi.vertexBindingDescriptionCount = 1;
vi.pVertexBindingDescriptions = &vb.bindDesc;
vi.vertexAttributeDescriptionCount = (uint32_t)vb.attrDesc.size();
vi.pVertexAttributeDescriptions = vb.attrDesc.data();
VkPipelineInputAssemblyStateCreateInfo ia{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
ia.primitiveRestartEnable = VK_FALSE;
ia.topology = topology;
VkPipelineRasterizationStateCreateInfo rs{VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO};
rs.polygonMode = VK_POLYGON_MODE_FILL;
rs.cullMode = VK_CULL_MODE_BACK_BIT;
rs.frontFace = VK_FRONT_FACE_CLOCKWISE;
rs.depthClampEnable = VK_FALSE;
rs.rasterizerDiscardEnable = VK_FALSE;
rs.depthBiasEnable = VK_FALSE;
rs.depthBiasConstantFactor = 0;
rs.depthBiasClamp = 0;
rs.depthBiasSlopeFactor = 0;
rs.lineWidth = 1.0f;
VkPipelineColorBlendAttachmentState attachState{};
attachState.blendEnable = 0;
attachState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
attachState.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
attachState.colorBlendOp = VK_BLEND_OP_ADD;
attachState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
attachState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
attachState.alphaBlendOp = VK_BLEND_OP_ADD;
attachState.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
cb.attachmentCount = 1;
cb.pAttachments = &attachState;
cb.logicOpEnable = VK_FALSE;
cb.logicOp = VK_LOGIC_OP_NO_OP;
cb.blendConstants[0] = 1.0f;
cb.blendConstants[1] = 1.0f;
cb.blendConstants[2] = 1.0f;
cb.blendConstants[3] = 1.0f;
VkRect2D scissor = {{0, 0}, size};
#if defined(ORIGIN_BOTTOM_LEFT)
// Flipped view so origin is bottom-left like GL (requires VK_KHR_maintenance1)
VkViewport viewport = {0.0f, (float)size.height, (float)size.width, -(float)size.height, 0.0f, 1.0f};
#else
// Will invert y after projection
VkViewport viewport = {0.0f, 0.0f, (float)size.width, (float)size.height, 0.0f, 1.0f};
#endif
VkPipelineViewportStateCreateInfo vp{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
vp.viewportCount = 1;
vp.pViewports = &viewport;
vp.scissorCount = 1;
vp.pScissors = &scissor;
VkPipelineDepthStencilStateCreateInfo ds{VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO};
ds.depthTestEnable = VK_TRUE;
ds.depthWriteEnable = VK_TRUE;
ds.depthCompareOp = VK_COMPARE_OP_LESS;
ds.depthBoundsTestEnable = VK_FALSE;
ds.stencilTestEnable = VK_FALSE;
ds.front.failOp = VK_STENCIL_OP_KEEP;
ds.front.passOp = VK_STENCIL_OP_KEEP;
ds.front.depthFailOp = VK_STENCIL_OP_KEEP;
ds.front.compareOp = VK_COMPARE_OP_ALWAYS;
ds.back = ds.front;
ds.minDepthBounds = 0.0f;
ds.maxDepthBounds = 1.0f;
VkPipelineMultisampleStateCreateInfo ms{VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO};
ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkGraphicsPipelineCreateInfo pipeInfo{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO};
pipeInfo.stageCount = (uint32_t)sp.shaderInfo.size();
pipeInfo.pStages = sp.shaderInfo.data();
pipeInfo.pVertexInputState = &vi;
pipeInfo.pInputAssemblyState = &ia;
pipeInfo.pTessellationState = nullptr;
pipeInfo.pViewportState = &vp;
pipeInfo.pRasterizationState = &rs;
pipeInfo.pMultisampleState = &ms;
pipeInfo.pDepthStencilState = &ds;
pipeInfo.pColorBlendState = &cb;
if (dynamicState.dynamicStateCount > 0) {
pipeInfo.pDynamicState = &dynamicState;
}
pipeInfo.layout = layout.layout;
pipeInfo.renderPass = rp.pass;
pipeInfo.subpass = 0;
CHECK_VKCMD(vkCreateGraphicsPipelines(m_vkDevice, VK_NULL_HANDLE, 1, &pipeInfo, nullptr, &pipe));
}
void Release() {
if (m_vkDevice != nullptr) {
if (pipe != VK_NULL_HANDLE) {
vkDestroyPipeline(m_vkDevice, pipe, nullptr);
}
}
pipe = VK_NULL_HANDLE;
m_vkDevice = nullptr;
}
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
};
struct DepthBuffer {
VkDeviceMemory depthMemory{VK_NULL_HANDLE};
VkImage depthImage{VK_NULL_HANDLE};
DepthBuffer() = default;
~DepthBuffer() {
if (m_vkDevice != nullptr) {
if (depthImage != VK_NULL_HANDLE) {
vkDestroyImage(m_vkDevice, depthImage, nullptr);
}
if (depthMemory != VK_NULL_HANDLE) {
vkFreeMemory(m_vkDevice, depthMemory, nullptr);
}
}
depthImage = VK_NULL_HANDLE;
depthMemory = VK_NULL_HANDLE;
m_vkDevice = nullptr;
}
DepthBuffer(DepthBuffer&& other) noexcept : DepthBuffer() {
using std::swap;
swap(depthImage, other.depthImage);
swap(depthMemory, other.depthMemory);
swap(m_vkDevice, other.m_vkDevice);
}
DepthBuffer& operator=(DepthBuffer&& other) noexcept {
if (&other == this) {
return *this;
}
// clean up self
this->~DepthBuffer();
using std::swap;
swap(depthImage, other.depthImage);
swap(depthMemory, other.depthMemory);
swap(m_vkDevice, other.m_vkDevice);
return *this;
}
void Create(const VulkanDebugObjectNamer& namer, VkDevice device, MemoryAllocator* memAllocator, VkFormat depthFormat,
const XrSwapchainCreateInfo& swapchainCreateInfo) {
m_vkDevice = device;
VkExtent2D size = {swapchainCreateInfo.width, swapchainCreateInfo.height};
// Create a D32 depthbuffer
VkImageCreateInfo imageInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = size.width;
imageInfo.extent.height = size.height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.format = depthFormat;
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
imageInfo.samples = (VkSampleCountFlagBits)swapchainCreateInfo.sampleCount;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
CHECK_VKCMD(vkCreateImage(device, &imageInfo, nullptr, &depthImage));
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_IMAGE, (uint64_t)depthImage, "hello_xr fallback depth image"));
VkMemoryRequirements memRequirements{};
vkGetImageMemoryRequirements(device, depthImage, &memRequirements);
memAllocator->Allocate(memRequirements, &depthMemory, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
CHECK_VKCMD(namer.SetName(VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)depthMemory, "hello_xr fallback depth image memory"));
CHECK_VKCMD(vkBindImageMemory(device, depthImage, depthMemory, 0));
}
void TransitionLayout(CmdBuffer* cmdBuffer, VkImageLayout newLayout) {
if (newLayout == m_vkLayout) {
return;
}
VkImageMemoryBarrier depthBarrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
depthBarrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
depthBarrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
depthBarrier.oldLayout = m_vkLayout;
depthBarrier.newLayout = newLayout;
depthBarrier.image = depthImage;
depthBarrier.subresourceRange = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, 1};
vkCmdPipelineBarrier(cmdBuffer->buf, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, 0, 0, nullptr,
0, nullptr, 1, &depthBarrier);
m_vkLayout = newLayout;
}
DepthBuffer(const DepthBuffer&) = delete;
DepthBuffer& operator=(const DepthBuffer&) = delete;
private:
VkDevice m_vkDevice{VK_NULL_HANDLE};
VkImageLayout m_vkLayout = VK_IMAGE_LAYOUT_UNDEFINED;
};
struct SwapchainImageContext {
SwapchainImageContext(XrStructureType _swapchainImageType) : swapchainImageType(_swapchainImageType) {}
// A packed array of XrSwapchainImageVulkan2KHR's for xrEnumerateSwapchainImages
std::vector<XrSwapchainImageVulkan2KHR> swapchainImages;
std::vector<RenderTarget> renderTarget;
VkExtent2D size{};
DepthBuffer depthBuffer{};
RenderPass rp{};
Pipeline pipe{};
XrStructureType swapchainImageType;
SwapchainImageContext() = default;
std::vector<XrSwapchainImageBaseHeader*> Create(const VulkanDebugObjectNamer& namer, VkDevice device,
MemoryAllocator* memAllocator, uint32_t capacity,