VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <sys/types.h>
10#include <volk.h>
11#include <vk_mem_alloc.h>
12#include <SDL3/SDL.h>
13#include <SDL3/SDL_vulkan.h>
14#include <vector>
15#include <memory>
16#include <glm/glm.hpp>
17#include <unordered_map>
18#include <components/Types.hpp>
19#include <string>
20#include <queue>
21#include "components/Environment.hpp"
22
23namespace vex {
27 VkInstance instance;
28 VkPhysicalDevice physicalDevice;
29 VkDevice device;
30 VkQueue graphicsQueue;
31 VkQueue presentQueue;
32 VmaAllocator allocator;
33 VkSurfaceKHR surface;
34
35 VkSwapchainKHR swapchain= VK_NULL_HANDLE;
36 std::vector<VkImage> swapchainImages;
37 VkFormat swapchainImageFormat;
38 VkExtent2D swapchainExtent;
39 std::vector<VkImageView> swapchainImageViews;
40
41 VkImage depthImage = VK_NULL_HANDLE;
42 VmaAllocation depthAllocation = VK_NULL_HANDLE;
43 VkImageView depthImageView = VK_NULL_HANDLE;
44 VkFormat depthFormat;
45
46 VkImage lowResColorImage = VK_NULL_HANDLE;
47 VmaAllocation lowResColorAlloc = VK_NULL_HANDLE;
48 VkImageView lowResColorView = VK_NULL_HANDLE;
49
50 VkImage compositeImage = VK_NULL_HANDLE;
51 VmaAllocation compositeAlloc = VK_NULL_HANDLE;
52 VkImageView compositeView = VK_NULL_HANDLE;
53
54 VkImage uiImage = VK_NULL_HANDLE;
55 VmaAllocation uiAlloc = VK_NULL_HANDLE;
56 VkImageView uiView = VK_NULL_HANDLE;
57 VkFormat lowResColorFormat = VK_FORMAT_UNDEFINED;
58
59 VkImage accumImage = VK_NULL_HANDLE;
60 VmaAllocation accumAlloc = VK_NULL_HANDLE;
61 VkImageView accumView = VK_NULL_HANDLE;
62
63 VkImage revealImage = VK_NULL_HANDLE;
64 VmaAllocation revealAlloc = VK_NULL_HANDLE;
65 VkImageView revealView = VK_NULL_HANDLE;
66
67 VkImage gameViewImage = VK_NULL_HANDLE;
68 VmaAllocation gameViewAlloc = VK_NULL_HANDLE;
69 VkImageView gameViewView = VK_NULL_HANDLE;
70
71 VkImage colorLutImage = VK_NULL_HANDLE;
72 VmaAllocation colorLutAllocation = VK_NULL_HANDLE;
73 VkImageView colorLutView = VK_NULL_HANDLE;
74
75 VkPipelineLayout pipelineLayout;
76
77 std::vector<VkCommandPool> commandPools;
78 std::vector<VkCommandBuffer> commandBuffers;
79
80 VkCommandPool singleTimePool;
81
84 VkCommandBuffer beginSingleTimeCommands() {
85 VkCommandBufferAllocateInfo allocInfo{};
86 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
87 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
88 allocInfo.commandPool = singleTimePool;
89 allocInfo.commandBufferCount = 1;
90
91 VkCommandBuffer commandBuffer;
92 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
93
94 VkCommandBufferBeginInfo beginInfo{};
95 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
96 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
97
98 vkBeginCommandBuffer(commandBuffer, &beginInfo);
99 return commandBuffer;
100 }
101
104 void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
105 vkEndCommandBuffer(commandBuffer);
106
107 VkSubmitInfo submitInfo{};
108 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
109 submitInfo.commandBufferCount = 1;
110 submitInfo.pCommandBuffers = &commandBuffer;
111
112 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
113 vkQueueWaitIdle(graphicsQueue);
114
115 vkFreeCommandBuffers(device, singleTimePool, 1, &commandBuffer);
116 }
117
118 uint32_t graphicsQueueFamily;
119 uint32_t presentQueueFamily;
120
124 uint32_t currentFrame = 0;
125
128 uint32_t currentImageIndex = 0;
129
134
137 std::vector<VkSemaphore> imageAvailableSemaphores;
138
141 std::vector<VkSemaphore> renderFinishedSemaphores;
142
145 std::vector<VkFence> inFlightFences;
146
147 VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
148 VkDescriptorSetLayout uboDescriptorSetLayout = VK_NULL_HANDLE;
149 VkDescriptorSetLayout textureDescriptorSetLayout = VK_NULL_HANDLE;
150 VkDescriptorSetLayout screenDescriptorSetLayout = VK_NULL_HANDLE;
151 VkDescriptorSetLayout particleDescriptorSetLayout = VK_NULL_HANDLE;
152
156 bool isValidFrameIndex(uint32_t frameIndex) const {
157 if (frameIndex >= MAX_FRAMES_IN_FLIGHT) {
158 SDL_LogError(SDL_LOG_CATEGORY_ERROR,
159 "Invalid frame index %u (MAX_FRAMES_IN_FLIGHT: %u)",
160 frameIndex, MAX_FRAMES_IN_FLIGHT);
161 return false;
162 }
163 return true;
164 }
165
169 bool isValidImageIndex(uint32_t imageIndex) const {
170 if (imageIndex >= swapchainImages.size()) {
171 SDL_LogError(SDL_LOG_CATEGORY_ERROR,
172 "Invalid image index %u (swapchain image count: %zu)",
173 imageIndex, swapchainImages.size());
174 return false;
175 }
176 return true;
177 }
178
183 "imageAvailableSemaphores size mismatch");
185 "renderFinishedSemaphores size mismatch");
186 assert(inFlightFences.size() == MAX_FRAMES_IN_FLIGHT &&
187 "inFlightFences size mismatch");
188 assert(commandPools.size() == MAX_FRAMES_IN_FLIGHT &&
189 "commandPools size mismatch");
190 assert(commandBuffers.size() == MAX_FRAMES_IN_FLIGHT &&
191 "commandBuffers size mismatch");
192 }
193
194 vex_map<std::string, uint32_t> textureIndices;
195 uint32_t nextTextureIndex = 0;
196
197 std::queue<uint32_t> recycledTextureIndices;
198
199 glm::uvec2 currentRenderResolution;
200
201 environment m_environment;
202
203 bool supportsMultiDraw = false;
204 bool supportsIndirectDraw = false;
205 bool supportsBindlessTextures = false;
206 bool supportsShaderDrawParameters = false;
207
208 VkDescriptorSetLayout bindlessDescriptorSetLayout = VK_NULL_HANDLE;
209 VkDescriptorPool bindlessDescriptorPool = VK_NULL_HANDLE;
210 VkDescriptorSet bindlessDescriptorSet = VK_NULL_HANDLE;
211
212 uint32_t maxDrawIndirectCount = 1;
213 uint32_t maxMultiDrawCount = 1;
214 bool requestSwapchainRecreation = false;
215
216 uint32_t vulkanVersion = 0;
217 };
218}
Struct holding all vulkan data, like device, surface, swapchain, images, views, and more.
Definition context.hpp:26
uint32_t currentImageIndex
Current swapchain image index (acquired from vkAcquireNextImageKHR).
Definition context.hpp:128
uint32_t MAX_FRAMES_IN_FLIGHT
Maximum frames in flight (triple-buffering).
Definition context.hpp:133
VkCommandBuffer beginSingleTimeCommands()
Function to begin single time command, used for stuff like creating vulkan image when loading texture...
Definition context.hpp:84
bool isValidFrameIndex(uint32_t frameIndex) const
Validates a frame index against MAX_FRAMES_IN_FLIGHT.
Definition context.hpp:156
std::vector< VkSemaphore > imageAvailableSemaphores
Synchronization semaphores - must be sized to MAX_FRAMES_IN_FLIGHT.
Definition context.hpp:137
bool isValidImageIndex(uint32_t imageIndex) const
Validates an image index against swapchain image count.
Definition context.hpp:169
std::vector< VkFence > inFlightFences
In-flight fences - must be sized to MAX_FRAMES_IN_FLIGHT.
Definition context.hpp:145
void validateSyncObjectSizes() const
Asserts that all frame-dependent synchronization vectors are properly sized.
Definition context.hpp:181
uint32_t currentFrame
Current frame index for triple-buffering.
Definition context.hpp:124
void endSingleTimeCommands(VkCommandBuffer commandBuffer)
Function to end single time command.
Definition context.hpp:104
std::vector< VkSemaphore > renderFinishedSemaphores
Render finished semaphores - must be sized to MAX_FRAMES_IN_FLIGHT.
Definition context.hpp:141
This struct is used to hold all environment settings. eg. shading details or lighting setup.