-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1318 lines (1202 loc) · 54.4 KB
/
main.cpp
File metadata and controls
1318 lines (1202 loc) · 54.4 KB
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) 2025-2026, Johnny Klucinec
* SPDX-License-Identifier: MIT */
#define VOLK_IMPLEMENTATION
#include <SDL3/SDL_video.h>
#include <cassert>
#include <vulkan/vulkan_core.h>
#include <vulkan/vulkan.h>
#include <volk/volk.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_vulkan.h>
#include <vector>
#include <array>
#include <string>
#include <iostream>
#define VMA_IMPLEMENTATION
#include <vma/vk_mem_alloc.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include "slang/slang.h"
#include "slang/slang-com-ptr.h"
#include <ktx.h>
#include <ktxvulkan.h>
#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>
#include <source_location>
#include "systems/ow_cam.hpp"
#include "systems/ow_move.hpp"
#include "systems/ow_overlay.hpp"
#include "systems/fps_limiter.hpp"
// ========================================
// Globals
struct Settings
{
uint32_t device = 0; // GPU deviceIndex
float fov = 103.0f; // HFOV
float fps_max = 120.0f;
bool fullscreen = false;
bool reduce_buffering = false;
float sensitivity = 2.70f;
bool show_ui = true;
bool vsync = false;
};
constexpr uint32_t MAX_FRAMES_IN_FLIGHT{ 2 };
uint32_t framesInFlight{ 2 };
uint32_t imageIndex{ 0 };
uint32_t frameIndex{ 0 };
VkCommandPool commandPool{ VK_NULL_HANDLE };
VkDevice device{ VK_NULL_HANDLE };
VkImage depthImage;
VkImageView depthImageView;
VkDescriptorSetLayout descriptorSetLayoutTex{ VK_NULL_HANDLE };
VkDescriptorPool descriptorPool{ VK_NULL_HANDLE };
VkDescriptorSet descriptorSetTex{ VK_NULL_HANDLE };
VkInstance instance{ VK_NULL_HANDLE };
VkPipeline pipeline{ VK_NULL_HANDLE };
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
VkQueue queue{ VK_NULL_HANDLE };
VkSurfaceKHR surface{ VK_NULL_HANDLE };
VkSwapchainKHR swapchain{ VK_NULL_HANDLE };
bool updateSwapchain{ false };
bool updateFullscreen{ true };
VkBuffer vBuffer{ VK_NULL_HANDLE };
VmaAllocator allocator{ VK_NULL_HANDLE };
VmaAllocation depthImageAllocation;
VmaAllocation vBufferAllocation{ VK_NULL_HANDLE };
Slang::ComPtr<slang::IGlobalSession> slangGlobalSession;
std::array<VkCommandBuffer, MAX_FRAMES_IN_FLIGHT> commandBuffers;
std::array<VkFence, MAX_FRAMES_IN_FLIGHT> fences;
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> presentSemaphores;
std::vector<VkSemaphore> renderSemaphores;
std::vector<VkImage> swapchainImages;
std::vector<VkImageView> swapchainImageViews;
bool isSprinting = false;
ow_cam::Camera camera{
.pos = { 0.0f, 0.0f, -6.0f },
.sensitivity = 2.70f,
};
ow_move::Walker walker{};
glm::ivec2 windowSize{};
// ========================================
// Structs and Buffers
struct Vertex
{
glm::vec3 pos;
glm::vec3 normal;
glm::vec2 uv;
};
struct ShaderData // Look into scalarBlockLayout
{
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model[3];
glm::vec4 lightPos{ 0.0f, -10.0f, 10.0f, 0.0f };
uint32_t selected{ 1 };
} shaderData{};
struct ShaderDataBuffer
{
VmaAllocation allocation{ VK_NULL_HANDLE };
VmaAllocationInfo allocationInfo{};
VkBuffer buffer{ VK_NULL_HANDLE };
VkDeviceAddress deviceAddress{};
};
std::array<ShaderDataBuffer, MAX_FRAMES_IN_FLIGHT> shaderDataBuffers;
struct Texture
{
VmaAllocation allocation{ VK_NULL_HANDLE };
VkImage image{ VK_NULL_HANDLE };
VkImageView view{ VK_NULL_HANDLE };
VkSampler sampler{ VK_NULL_HANDLE };
};
std::array<Texture, 3> textures{};
// Helper Function Prototypes
static inline void chk(VkResult result, const std::source_location& loc = std::source_location::current());
static inline void chkSwapchain(VkResult result, const std::source_location& loc = std::source_location::current());
static inline void chk(bool result, const std::source_location& loc = std::source_location::current());
Settings parseArgs(int argc, char* argv[]);
int main(int argc, char* argv[])
{
Settings settings = parseArgs(argc, argv);
chk(SDL_Init(SDL_INIT_VIDEO));
chk(SDL_Vulkan_LoadLibrary(NULL));
volkInitialize();
// ========================================
// Instance Setup
VkApplicationInfo appInfo{
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "Kitsune Engine",
.apiVersion = VK_API_VERSION_1_4,
};
uint32_t instanceExtensionsCount{ 0 };
char const* const* instanceExtensions{ SDL_Vulkan_GetInstanceExtensions(&instanceExtensionsCount) };
VkInstanceCreateInfo instanceCI{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &appInfo,
.enabledExtensionCount = instanceExtensionsCount,
.ppEnabledExtensionNames = instanceExtensions,
};
chk(vkCreateInstance(&instanceCI, nullptr, &instance));
volkLoadInstance(instance);
// ========================================
// Device Selection
uint32_t deviceCount{ 0 };
chk(vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr));
std::vector<VkPhysicalDevice> devices(deviceCount);
chk(vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()));
// Ensure valid deviceIndex
assert(settings.device < deviceCount);
// Output device name
VkPhysicalDeviceProperties2 deviceProperties{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
};
vkGetPhysicalDeviceProperties2(devices[settings.device], &deviceProperties);
std::cout << "Selected device: " << deviceProperties.properties.deviceName << "\n";
// ========================================
// Find a Queue Family with Graphics Support
uint32_t queueFamilyCount{ 0 };
vkGetPhysicalDeviceQueueFamilyProperties(devices[settings.device], &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(devices[settings.device], &queueFamilyCount, queueFamilies.data());
uint32_t queueFamily{ 0 };
for(size_t i = 0; i < queueFamilies.size(); i++)
{
if(queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
queueFamily = i;
break;
}
}
chk(SDL_Vulkan_GetPresentationSupport(instance, devices[settings.device], queueFamily));
// ========================================
// Logical Device Setup
const float qfpriorities{ 1.0f };
VkDeviceQueueCreateInfo queueCI{
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = queueFamily,
.queueCount = 1,
.pQueuePriorities = &qfpriorities,
};
VkPhysicalDeviceVulkan12Features enabledVk12Features{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
.descriptorIndexing = true,
.shaderSampledImageArrayNonUniformIndexing = true,
.descriptorBindingVariableDescriptorCount = true,
.runtimeDescriptorArray = true,
.bufferDeviceAddress = true,
};
VkPhysicalDeviceVulkan13Features enabledVk13Features{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
.pNext = &enabledVk12Features,
.synchronization2 = true,
.dynamicRendering = true,
};
const VkPhysicalDeviceFeatures enabledVk10Features{
.samplerAnisotropy = VK_TRUE,
};
const std::vector<const char*> deviceExtensions{ VK_KHR_SWAPCHAIN_EXTENSION_NAME };
VkDeviceCreateInfo deviceCI{
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = &enabledVk13Features,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &queueCI,
.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size()),
.ppEnabledExtensionNames = deviceExtensions.data(),
.pEnabledFeatures = &enabledVk10Features,
};
chk(vkCreateDevice(devices[settings.device], &deviceCI, nullptr, &device));
vkGetDeviceQueue(device, queueFamily, 0, &queue); // request queue from device
// ========================================
// Setting up VMA
VmaVulkanFunctions vkFunctions{
.vkGetInstanceProcAddr = vkGetInstanceProcAddr,
.vkGetDeviceProcAddr = vkGetDeviceProcAddr,
.vkCreateImage = vkCreateImage,
};
VmaAllocatorCreateInfo allocatorCI{
.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT,
.physicalDevice = devices[settings.device],
.device = device,
.pVulkanFunctions = &vkFunctions,
.instance = instance,
};
chk(vmaCreateAllocator(&allocatorCI, &allocator));
// ========================================
// Window and Surface
SDL_Window* window = SDL_CreateWindow("KitsuneEngine", 1600u, 900u, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
assert(window);
if(settings.fullscreen)
{
SDL_DisplayID displayID = SDL_GetPrimaryDisplay();
assert(displayID != 0);
const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(displayID);
assert(mode);
chk(SDL_SetWindowFullscreenMode(window, mode)); // NULL = borderless; mode = exclusive
chk(SDL_SetWindowFullscreen(window, true));
chk(SDL_SyncWindow(window));
}
chk(SDL_Vulkan_CreateSurface(window, instance, nullptr, &surface));
chk(SDL_GetWindowSize(window, &windowSize.x, &windowSize.y)); // now returns fullscreen dims if applicable
SDL_SetWindowRelativeMouseMode(window, true);
VkSurfaceCapabilitiesKHR surfaceCaps{};
chk(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(devices[settings.device], surface, &surfaceCaps));
// ========================================
// Swapchain
const VkFormat imageFormat{ VK_FORMAT_B8G8R8A8_SRGB };
VkSwapchainCreateInfoKHR swapchainCI{
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.surface = surface,
.minImageCount = surfaceCaps.minImageCount,
.imageFormat = imageFormat,
.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR,
.imageExtent{ .width = surfaceCaps.currentExtent.width, .height = surfaceCaps.currentExtent.height },
.imageArrayLayers = 1,
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR, // VK_PRESENT_MODE_FIFO_KHR = VSYNC | VK_PRESENT_MODE_IMMEDIATE_KHR = Unlocked
};
chk(vkCreateSwapchainKHR(device, &swapchainCI, nullptr, &swapchain));
// request images from the swapchain
uint32_t imageCount{ 0 };
chk(vkGetSwapchainImagesKHR(device, swapchain, &imageCount, nullptr));
swapchainImages.resize(imageCount);
chk(vkGetSwapchainImagesKHR(device, swapchain, &imageCount, swapchainImages.data()));
swapchainImageViews.resize(imageCount);
// Create image views for each swapchain image
for(auto i = 0; i < imageCount; i++)
{
VkImageViewCreateInfo viewCI{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = swapchainImages[i],
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = imageFormat,
.subresourceRange{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = 1, .layerCount = 1 },
};
chk(vkCreateImageView(device, &viewCI, nullptr, &swapchainImageViews[i]));
}
// ========================================
// Depth Attachment
std::vector<VkFormat> depthFormatList{ VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT };
VkFormat depthFormat{ VK_FORMAT_UNDEFINED };
for(VkFormat& format : depthFormatList)
{
VkFormatProperties2 formatProperties{ .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2 };
vkGetPhysicalDeviceFormatProperties2(devices[settings.device], format, &formatProperties);
if(formatProperties.formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
depthFormat = format;
break;
}
}
assert(depthFormat != VK_FORMAT_UNDEFINED);
// Allocate depth buffer image and create image view
VkImageCreateInfo depthImageCI{
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.imageType = VK_IMAGE_TYPE_2D,
.format = depthFormat,
.extent{ .width = static_cast<uint32_t>(windowSize.x), .height = static_cast<uint32_t>(windowSize.y), .depth = 1 },
.mipLevels = 1,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
VmaAllocationCreateInfo allocCI{
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
chk(vmaCreateImage(allocator, &depthImageCI, &allocCI, &depthImage, &depthImageAllocation, nullptr));
VkImageViewCreateInfo depthViewCI{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = depthImage,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = depthFormat,
.subresourceRange{ .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .levelCount = 1, .layerCount = 1 }
};
chk(vkCreateImageView(device, &depthViewCI, nullptr, &depthImageView));
// ========================================
// Loading Meshes
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
chk(tinyobj::LoadObj(&attrib, &shapes, &materials, nullptr, nullptr, "assets/suzanne.obj"));
const VkDeviceSize indexCount{ shapes[0].mesh.indices.size() };
std::vector<Vertex> vertices{};
std::vector<uint16_t> indices{};
// Load vertex and index data
for(auto& index : shapes[0].mesh.indices)
{
Vertex v{
.pos = { attrib.vertices[index.vertex_index * 3], -attrib.vertices[index.vertex_index * 3 + 1], attrib.vertices[index.vertex_index * 3 + 2] },
.normal = { attrib.normals[index.normal_index * 3], -attrib.normals[index.normal_index * 3 + 1], attrib.normals[index.normal_index * 3 + 2] },
.uv = { attrib.texcoords[index.texcoord_index * 2], 1.0 - attrib.texcoords[index.texcoord_index * 2 + 1] }
};
vertices.push_back(v);
indices.push_back(indices.size());
}
// Allocate vertex + index buffer
VkDeviceSize vBufSize{ sizeof(Vertex) * vertices.size() };
VkDeviceSize iBufSize{ sizeof(uint16_t) * indices.size() };
VkBufferCreateInfo bufferCI{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = vBufSize + iBufSize,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
};
// Persistent Mapping
VmaAllocationCreateInfo vBufferAllocCI{
// NOTE: without ReBAR, host-visible VRAM is limited to 256MB. Exhausting it falls back to non-host-visible DEVICE_LOCAL via ALLOW_TRANSFER_INSTEAD
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT
| VMA_ALLOCATION_CREATE_MAPPED_BIT, // fallback fires once the 256MB BAR is exhausted, which is common on non-ReBAR discrete GPUs
.usage = VMA_MEMORY_USAGE_AUTO,
};
VmaAllocationInfo vBufferAllocInfo{};
chk(vmaCreateBuffer(allocator, &bufferCI, &vBufferAllocCI, &vBuffer, &vBufferAllocation, &vBufferAllocInfo));
// Write directly into VRAM via persistently mapped ReBAR memory
memcpy(vBufferAllocInfo.pMappedData, vertices.data(), vBufSize);
memcpy(((char*)vBufferAllocInfo.pMappedData) + vBufSize, indices.data(), iBufSize);
// ========================================
// Shader Data Buffers
for(auto i = 0; i < framesInFlight; i++) // One per max frame in flight
{
VkBufferCreateInfo uBufferCI{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(ShaderData),
.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, // Buffer accessible via raw pointer in shader
};
VmaAllocationCreateInfo uBufferAllocCI{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT
| VMA_ALLOCATION_CREATE_MAPPED_BIT, // Persistently mapped; CPU/GPU accessible
.usage = VMA_MEMORY_USAGE_AUTO,
};
chk(vmaCreateBuffer(allocator, &uBufferCI, &uBufferAllocCI, &shaderDataBuffers[i].buffer, &shaderDataBuffers[i].allocation, &shaderDataBuffers[i].allocationInfo));
// Grab and store buffer's device address
VkBufferDeviceAddressInfo uBufferBdaInfo{ .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, .buffer = shaderDataBuffers[i].buffer };
shaderDataBuffers[i].deviceAddress = vkGetBufferDeviceAddress(device, &uBufferBdaInfo);
}
// ========================================
// Synchronization Objects
VkSemaphoreCreateInfo semaphoreCI{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
VkFenceCreateInfo fenceCI{
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
};
for(auto i = 0; i < framesInFlight; i++) // One fence per frame in flight
{
chk(vkCreateFence(device, &fenceCI, nullptr, &fences[i]));
chk(vkCreateSemaphore(device, &semaphoreCI, nullptr, &presentSemaphores[i]));
}
renderSemaphores.resize(swapchainImages.size());
for(auto& semaphore : renderSemaphores) // One semephore per swapchain image
{
chk(vkCreateSemaphore(device, &semaphoreCI, nullptr, &semaphore));
}
// ========================================
// Command Buffers (pools)
VkCommandPoolCreateInfo commandPoolCI{
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = queueFamily,
};
chk(vkCreateCommandPool(device, &commandPoolCI, nullptr, &commandPool));
VkCommandBufferAllocateInfo cbAllocCI{
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = commandPool,
.commandBufferCount = framesInFlight, // One command buffer per frame in flight
};
chk(vkAllocateCommandBuffers(device, &cbAllocCI, commandBuffers.data()));
// ========================================
// Loading Textures
std::vector<VkDescriptorImageInfo> textureDescriptors{};
for(auto i = 0; i < textures.size(); i++)
{
ktxTexture* ktxTexture{ nullptr };
std::string filename = "assets/suzanne" + std::to_string(i) + ".ktx";
ktxTexture_CreateFromNamedFile(filename.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &ktxTexture);
// Creating the image (object)
VkImageCreateInfo texImgCI{
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.imageType = VK_IMAGE_TYPE_2D,
.format = ktxTexture_GetVkFormat(ktxTexture),
.extent = { .width = ktxTexture->baseWidth, .height = ktxTexture->baseHeight, .depth = 1 },
.mipLevels = ktxTexture->numLevels,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
VmaAllocationCreateInfo texImageAllocCI{ .usage = VMA_MEMORY_USAGE_AUTO };
chk(vmaCreateImage(allocator, &texImgCI, &texImageAllocCI, &textures[i].image, &textures[i].allocation, nullptr));
// Create the image view
VkImageViewCreateInfo texVewCI{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = textures[i].image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = texImgCI.format,
.subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = ktxTexture->numLevels, .layerCount = 1 }
};
chk(vkCreateImageView(device, &texVewCI, nullptr, &textures[i].view));
// ========================================
// Upload
VkBuffer imgSrcBuffer{};
VmaAllocation imgSrcAllocation{};
VkBufferCreateInfo imgSrcBufferCI{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = (uint32_t)ktxTexture->dataSize,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, // Temporary source for a buffer-to-image copy
};
VmaAllocationCreateInfo imgSrcAllocCI{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
VmaAllocationInfo imgSrcAllocInfo{};
chk(vmaCreateBuffer(allocator, &imgSrcBufferCI, &imgSrcAllocCI, &imgSrcBuffer, &imgSrcAllocation, &imgSrcAllocInfo));
memcpy(imgSrcAllocInfo.pMappedData, ktxTexture->pData, ktxTexture->dataSize); // Actually move image data now
// Copy image data to the optimal tiled image on the GPU
VkFenceCreateInfo fenceOneTimeCI{
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
};
VkFence fenceOneTime{};
chk(vkCreateFence(device, &fenceOneTimeCI, nullptr, &fenceOneTime));
VkCommandBuffer cbOneTime{};
VkCommandBufferAllocateInfo cbOneTimeAI{
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = commandPool,
.commandBufferCount = 1,
};
chk(vkAllocateCommandBuffers(device, &cbOneTimeAI, &cbOneTime));
// Start recording the commands required to get the image to its destination
VkCommandBufferBeginInfo cbOneTimeBI{
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
chk(vkBeginCommandBuffer(cbOneTime, &cbOneTimeBI));
// Transition mip levels from initial undefined layout to a transferable layout
VkImageMemoryBarrier2 barrierTexImage{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.srcStageMask = VK_PIPELINE_STAGE_2_NONE,
.srcAccessMask = VK_ACCESS_2_NONE,
.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT,
.dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // The good layout
.image = textures[i].image,
.subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = ktxTexture->numLevels, .layerCount = 1 }
};
VkDependencyInfo barrierTexInfo{
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &barrierTexImage,
};
vkCmdPipelineBarrier2(cbOneTime, &barrierTexInfo);
std::vector<VkBufferImageCopy> copyRegions{};
for(auto j = 0; j < ktxTexture->numLevels; j++)
{
ktx_size_t mipOffset{ 0 };
KTX_error_code ret = ktxTexture_GetImageOffset(ktxTexture, j, 0, 0, &mipOffset);
copyRegions.push_back({
.bufferOffset = mipOffset, // clang-format off
.imageSubresource{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .mipLevel = (uint32_t)j, .layerCount = 1 },
.imageExtent{ .width = ktxTexture->baseWidth >> j, .height = ktxTexture->baseHeight >> j, .depth = 1 },
}); // clang-format on
}
// Copy mip levels from temp buffer
vkCmdCopyBufferToImage(cbOneTime, imgSrcBuffer, textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<uint32_t>(copyRegions.size()),
copyRegions.data());
// Transition mip levels from transfer destination to shader readable layout
VkImageMemoryBarrier2 barrierTexRead{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.srcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL, // Yay, shaders can read
.image = textures[i].image,
.subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = ktxTexture->numLevels, .layerCount = 1 }
};
barrierTexInfo.pImageMemoryBarriers = &barrierTexRead;
vkCmdPipelineBarrier2(cbOneTime, &barrierTexInfo);
chk(vkEndCommandBuffer(cbOneTime));
VkSubmitInfo oneTimeSI{
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.commandBufferCount = 1,
.pCommandBuffers = &cbOneTime,
};
chk(vkQueueSubmit(queue, 1, &oneTimeSI, fenceOneTime));
chk(vkWaitForFences(device, 1, &fenceOneTime, VK_TRUE, UINT64_MAX));
vkDestroyFence(device, fenceOneTime, nullptr);
vmaDestroyBuffer(allocator, imgSrcBuffer, imgSrcAllocation);
// ========================================
// Sample Texture in Shader
VkSamplerCreateInfo samplerCI{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
.anisotropyEnable = VK_TRUE,
.maxAnisotropy = 8.0f, // 8 is a widely supported value for max anisotropy
.maxLod = (float)ktxTexture->numLevels,
};
chk(vkCreateSampler(device, &samplerCI, nullptr, &textures[i].sampler));
// Clean up and store descriptor info for texture to use later
ktxTexture_Destroy(ktxTexture);
textureDescriptors.push_back({
.sampler = textures[i].sampler,
.imageView = textures[i].view,
.imageLayout = VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
});
}
// ========================================
// Descriptor Indexing
VkDescriptorBindingFlags descVariableFlag{ VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT };
// Only using bindings for images
VkDescriptorSetLayoutBindingFlagsCreateInfo descBindingFlags{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
.bindingCount = 1,
.pBindingFlags = &descVariableFlag,
};
VkDescriptorSetLayoutBinding descLayoutBindingTex{
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // Will be combining textures and samplers
.descriptorCount = static_cast<uint32_t>(textures.size()),
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
};
VkDescriptorSetLayoutCreateInfo descLayoutTexCI{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &descBindingFlags,
.bindingCount = 1,
.pBindings = &descLayoutBindingTex,
};
chk(vkCreateDescriptorSetLayout(device, &descLayoutTexCI, nullptr, &descriptorSetLayoutTex)); // Defines the interface
// Allocate descriptors
VkDescriptorPoolSize poolSize{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = static_cast<uint32_t>(textures.size()) + 1, // As many descriptors for images + samplers per texture + 1 for ImGui
};
VkDescriptorPoolCreateInfo descPoolCI{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, // for ImGui
.maxSets = 2, // how mant descriptor sets, not descriptors + 1 for ImGui
.poolSizeCount = 1,
.pPoolSizes = &poolSize, // If wrong, allocations beyound requested counts will fail
};
chk(vkCreateDescriptorPool(device, &descPoolCI, nullptr, &descriptorPool));
// Initialize the ImGui overlay
ow_overlay::init(window, instance, devices[settings.device], device, queue, queueFamily, static_cast<uint32_t>(swapchainImages.size()), imageFormat,
descriptorPool, surfaceCaps.minImageCount);
uint32_t variableDescCount{ static_cast<uint32_t>(textures.size()) };
// Allocate descriptor sets from that pool
VkDescriptorSetVariableDescriptorCountAllocateInfo variableDescCountAI{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT,
.descriptorSetCount = 1,
.pDescriptorCounts = &variableDescCount,
};
VkDescriptorSetAllocateInfo texDescSetAlloc{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = &variableDescCountAI,
.descriptorPool = descriptorPool,
.descriptorSetCount = 1,
.pSetLayouts = &descriptorSetLayoutTex, // Interface to our shaders
};
chk(vkAllocateDescriptorSets(device, &texDescSetAlloc, &descriptorSetTex)); // Contains the actual descriptor data
// Put data in descriptor set so we can access it in the shader
VkWriteDescriptorSet writeDescSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = descriptorSetTex,
.dstBinding = 0,
.descriptorCount = static_cast<uint32_t>(textureDescriptors.size()),
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = textureDescriptors.data(),
};
vkUpdateDescriptorSets(device, 1, &writeDescSet, 0, nullptr); // put info in first binding slot of the descriptor set
// ========================================
// Loading Shaders
// Initialize Slang shader compiler
slang::createGlobalSession(slangGlobalSession.writeRef());
auto slangTargets{
std::to_array<slang::TargetDesc>({ {
.format = SLANG_SPIRV,
.profile = slangGlobalSession->findProfile("spirv_1_4"),
} })
};
auto slangOptions{
std::to_array<slang::CompilerOptionEntry>({ {
slang::CompilerOptionName::EmitSpirvDirectly,
{ slang::CompilerOptionValueKind::Int, 1 },
} })
};
slang::SessionDesc slangSessionDesc{
.targets = slangTargets.data(),
.targetCount = SlangInt(slangTargets.size()),
.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR, // Matches the layout of GLM
.compilerOptionEntries = slangOptions.data(),
.compilerOptionEntryCount = uint32_t(slangOptions.size()),
};
Slang::ComPtr<slang::ISession> slangSession;
slangGlobalSession->createSession(slangSessionDesc, slangSession.writeRef());
// Load the Shader
Slang::ComPtr<slang::IModule> slangModule{ slangSession->loadModuleFromSource("triangle", "assets/shader.slang", nullptr, nullptr) };
Slang::ComPtr<ISlangBlob> spirv;
slangModule->getTargetCode(0, spirv.writeRef());
// Create shader module for use in graphics pipeline
VkShaderModuleCreateInfo shaderModuleCI{
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = spirv->getBufferSize(),
.pCode = (uint32_t*)spirv->getBufferPointer(),
};
VkShaderModule shaderModule{};
chk(vkCreateShaderModule(device, &shaderModuleCI, nullptr, &shaderModule));
// ========================================
// Graphics Pipeline
VkPushConstantRange pushConstantRange{
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
.size = sizeof(VkDeviceAddress),
};
VkPipelineLayoutCreateInfo pipelineLayoutCI{
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &descriptorSetLayoutTex, // Interface to shader resources
.pushConstantRangeCount = 1,
.pPushConstantRanges = &pushConstantRange,
};
chk(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); // Creates pipeline layout
// Define basic vertex structures in Vulkan terms
std::vector<VkPipelineShaderStageCreateInfo> shaderStages{
{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = shaderModule, .pName = "main" },
{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = shaderModule, .pName = "main" }
};
VkVertexInputBindingDescription vertexBinding{ .binding = 0, .stride = sizeof(Vertex), .inputRate = VK_VERTEX_INPUT_RATE_VERTEX };
std::vector<VkVertexInputAttributeDescription> vertexAttributes{
{ .location = 0, .binding = 0, .format = VK_FORMAT_R32G32B32_SFLOAT },
{ .location = 1, .binding = 0, .format = VK_FORMAT_R32G32B32_SFLOAT, .offset = offsetof(Vertex, normal) },
{ .location = 2, .binding = 0, .format = VK_FORMAT_R32G32_SFLOAT, .offset = offsetof(Vertex, uv) },
};
// Setup all the pipeline states
VkPipelineVertexInputStateCreateInfo vertexInputState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &vertexBinding,
.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexAttributes.size()),
.pVertexAttributeDescriptions = vertexAttributes.data(),
};
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // we be rendering a list of triangles
};
std::vector<VkDynamicState> dynamicStates{ VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VkPipelineDynamicStateCreateInfo dynamicState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.dynamicStateCount = 2,
.pDynamicStates = dynamicStates.data(),
};
VkPipelineViewportStateCreateInfo viewportState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.viewportCount = 1,
.scissorCount = 1,
};
VkPipelineRasterizationStateCreateInfo rasterizationState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.lineWidth = 1.0f, // Not used (default state)
};
VkPipelineMultisampleStateCreateInfo multisampleState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT, // Not used (default state)
};
VkPipelineDepthStencilStateCreateInfo depthStencilState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
.depthTestEnable = VK_TRUE,
.depthWriteEnable = VK_TRUE,
.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL,
};
VkPipelineColorBlendAttachmentState blendAttachment{
.colorWriteMask = 0xF, // Not used (default state)
};
VkPipelineColorBlendStateCreateInfo colorBlendState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &blendAttachment,
};
VkPipelineRenderingCreateInfo renderingCI{
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, // We are using dynamic rendering
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &imageFormat,
.depthAttachmentFormat = depthFormat,
};
// Create the Graphics Pipeline
VkGraphicsPipelineCreateInfo pipelineCI{
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.pNext = &renderingCI,
.stageCount = 2,
.pStages = shaderStages.data(),
.pVertexInputState = &vertexInputState,
.pInputAssemblyState = &inputAssemblyState,
.pViewportState = &viewportState,
.pRasterizationState = &rasterizationState,
.pMultisampleState = &multisampleState,
.pDepthStencilState = &depthStencilState,
.pColorBlendState = &colorBlendState,
.pDynamicState = &dynamicState,
.layout = pipelineLayout,
};
chk(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineCI, nullptr, &pipeline));
// ========================================
// Render Loop
fps::Limiter limiter(settings.fps_max);
bool quit{ false };
while(!quit)
{
// ==== Wait on Fence (sync) ====
chk(vkWaitForFences(device, 1, &fences[frameIndex], true, UINT64_MAX)); // Wait for last frame GPU is working on
chk(vkResetFences(device, 1, &fences[frameIndex])); // Reset for next submission
// Setup frame pacing
limiter.wait();
float elapsedTime = limiter.deltaTime();
float fps = limiter.currentFPS();
if(settings.show_ui)
ow_overlay::begin_frame(); // begin frame for ImGui
// ==== Aquire Next Image ====
chkSwapchain(vkAcquireNextImageKHR(device, swapchain, UINT64_MAX, presentSemaphores[frameIndex], VK_NULL_HANDLE, &imageIndex));
// === Polling Events ===
constexpr int maxShaderIndex = 2;
static bool keyState[SDL_SCANCODE_COUNT]{};
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_EVENT_QUIT:
quit = true;
break;
case SDL_EVENT_MOUSE_MOTION:
camera.apply_mouse_delta(event.motion.xrel, event.motion.yrel);
break;
case SDL_EVENT_KEY_DOWN:
keyState[event.key.scancode] = true;
switch(event.key.key)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_LEFT:
shaderData.selected = (shaderData.selected > 0) ? shaderData.selected - 1 : maxShaderIndex;
break;
case SDLK_RIGHT:
shaderData.selected = (shaderData.selected < maxShaderIndex) ? shaderData.selected + 1 : 0;
break;
default:
break;
}
if(event.key.mod & SDL_KMOD_LALT)
{
switch(event.key.key)
{
case SDLK_RETURN:
settings.fullscreen = !settings.fullscreen;
updateFullscreen = true;
break;
case SDLK_Z:
settings.show_ui = !settings.show_ui;
break;
}
}
break;
case SDL_EVENT_KEY_UP:
keyState[event.key.scancode] = false;
break;
case SDL_EVENT_WINDOW_RESIZED:
updateSwapchain = true;
break;
default:
break;
}
}
float fwd = 0.0f, side = 0.0f;
if(keyState[SDL_SCANCODE_W])
fwd += 1.0f;
if(keyState[SDL_SCANCODE_S])
fwd -= 1.0f;
if(keyState[SDL_SCANCODE_A])
side += 1.0f;
if(keyState[SDL_SCANCODE_D])
side -= 1.0f;
isSprinting = keyState[SDL_SCANCODE_LSHIFT];
walker.update(fwd, side, isSprinting); // Update movement
walker.integrate_world(camera.pos.x, camera.pos.z, camera.yaw_f(), elapsedTime); // Update velocity, then integrate
// ==== Update Shader Data ====
float vfov = ow_cam::vfov_from_hfov_cfg(settings.fov);
shaderData.projection = glm::perspective(vfov, (float)windowSize.x / (float)windowSize.y, 0.1f, 32.0f);
// View matrix built from OW camera yaw/pitch
glm::mat4 rotYaw = glm::rotate(glm::mat4(1.0f), camera.yaw_f(), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 rotPitch = glm::rotate(glm::mat4(1.0f), -camera.pitch_f(), glm::vec3(1.0f, 0.0f, 0.0f));
shaderData.view = rotPitch * rotYaw * glm::translate(glm::mat4(1.0f), camera.pos);
for(auto i = 0; i < 3; i++)
{
auto instancePos = glm::vec3((float)(i - 1) * 3.0f, 0.0f, 0.0f);
shaderData.model[i] = glm::translate(glm::mat4(1.0f), instancePos); // static, no rotation
}
memcpy(shaderDataBuffers[frameIndex].allocationInfo.pMappedData, &shaderData, sizeof(ShaderData));
// ==== Record Command Buffer (build command buffer) ====
auto cb = commandBuffers[frameIndex];
chk(vkResetCommandBuffer(cb, 0)); // Reset command buffer
VkCommandBufferBeginInfo cbBI{
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
chk(vkBeginCommandBuffer(cb, &cbBI)); // Start recording to cb
// Issue layout transitions for swapchain image and depth image
std::array<VkImageMemoryBarrier2, 2> outputBarriers{
VkImageMemoryBarrier2{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, // pipeline stage(s) to wait on
.srcAccessMask = 0, // memory writes to make available to the GPU
.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, // where and what those writes must be visible to
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, // dont need previous contents
.newLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL, // use as color attachment for rendering
.image = swapchainImages[imageIndex],
.subresourceRange{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = 1, .layerCount = 1 },
},
VkImageMemoryBarrier2{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.srcStageMask = VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT, // pipeline stage(s) to wait on
.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, // memory writes to make available to the GPU
.dstStageMask = VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT, // where and what those writes must be visible to
.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, // dont need previous contents
.newLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL, // use as color attachment for rendering
.image = depthImage,
.subresourceRange{ .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, .levelCount = 1, .layerCount = 1 },
}
};
VkDependencyInfo barrierDependencyInfo{
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.imageMemoryBarrierCount = 2,
.pImageMemoryBarriers = outputBarriers.data(),
};
vkCmdPipelineBarrier2(cb, &barrierDependencyInfo); // Insert those two barriers into the current command buffer
// Define how attachments are used: Dynamic Rendering
VkRenderingAttachmentInfo colorAttachmentInfo{
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
.imageView = swapchainImageViews[imageIndex],
.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.clearValue{ .color{ 0.0f, 0.0f, 0.0f, 1.0f } },
};
VkRenderingAttachmentInfo depthAttachmentInfo{
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
.imageView = depthImageView,
.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.clearValue = { .depthStencil = { 1.0f, 0 } },
};
// Start dynamic render pass
VkRenderingInfo renderingInfo{
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
.renderArea{ .extent{ .width = static_cast<uint32_t>(windowSize.x), .height = static_cast<uint32_t>(windowSize.y) } },
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &colorAttachmentInfo,
.pDepthAttachment = &depthAttachmentInfo,
};
vkCmdBeginRendering(cb, &renderingInfo);