1 | #define STB_IMAGE_IMPLEMENTATION
|
---|
2 | #include "stb_image.h" // TODO: Probably switch to SDL_image
|
---|
3 |
|
---|
4 | //#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath>
|
---|
5 |
|
---|
6 | #define GLM_FORCE_RADIANS
|
---|
7 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
---|
8 |
|
---|
9 | #include <glm/glm.hpp>
|
---|
10 | #include <glm/gtc/matrix_transform.hpp>
|
---|
11 |
|
---|
12 | #include <iostream>
|
---|
13 | #include <fstream>
|
---|
14 | #include <algorithm>
|
---|
15 | #include <vector>
|
---|
16 | #include <array>
|
---|
17 | #include <set>
|
---|
18 | #include <optional>
|
---|
19 | #include <chrono>
|
---|
20 |
|
---|
21 | #include "consts.hpp"
|
---|
22 | #include "utils.hpp"
|
---|
23 |
|
---|
24 | #include "game-gui-sdl.hpp"
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 | using namespace glm;
|
---|
28 |
|
---|
29 | const int SCREEN_WIDTH = 800;
|
---|
30 | const int SCREEN_HEIGHT = 600;
|
---|
31 |
|
---|
32 | const int MAX_FRAMES_IN_FLIGHT = 2;
|
---|
33 |
|
---|
34 | /*** START OF REFACTORED CODE ***/
|
---|
35 | #ifdef NDEBUG
|
---|
36 | const bool enableValidationLayers = false;
|
---|
37 | #else
|
---|
38 | const bool enableValidationLayers = true;
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | const vector<const char*> validationLayers = {
|
---|
42 | "VK_LAYER_KHRONOS_validation"
|
---|
43 | };
|
---|
44 |
|
---|
45 | const vector<const char*> deviceExtensions = {
|
---|
46 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
47 | };
|
---|
48 |
|
---|
49 | struct QueueFamilyIndices {
|
---|
50 | optional<uint32_t> graphicsFamily;
|
---|
51 | optional<uint32_t> presentFamily;
|
---|
52 |
|
---|
53 | bool isComplete() {
|
---|
54 | return graphicsFamily.has_value() && presentFamily.has_value();
|
---|
55 | }
|
---|
56 | };
|
---|
57 |
|
---|
58 | struct SwapChainSupportDetails {
|
---|
59 | VkSurfaceCapabilitiesKHR capabilities;
|
---|
60 | vector<VkSurfaceFormatKHR> formats;
|
---|
61 | vector<VkPresentModeKHR> presentModes;
|
---|
62 | };
|
---|
63 | /*** END OF REFACTORED CODE ***/
|
---|
64 |
|
---|
65 | struct Vertex {
|
---|
66 | glm::vec3 pos;
|
---|
67 | glm::vec3 color;
|
---|
68 | glm::vec2 texCoord;
|
---|
69 | };
|
---|
70 |
|
---|
71 | struct OverlayVertex {
|
---|
72 | glm::vec3 pos;
|
---|
73 | glm::vec2 texCoord;
|
---|
74 | };
|
---|
75 |
|
---|
76 | struct UniformBufferObject {
|
---|
77 | alignas(16) mat4 model;
|
---|
78 | alignas(16) mat4 view;
|
---|
79 | alignas(16) mat4 proj;
|
---|
80 | };
|
---|
81 |
|
---|
82 | struct DescriptorInfo {
|
---|
83 | VkDescriptorType type;
|
---|
84 | VkShaderStageFlags stageFlags;
|
---|
85 |
|
---|
86 | vector<VkDescriptorBufferInfo>* bufferDataList;
|
---|
87 | VkDescriptorImageInfo* imageData;
|
---|
88 | };
|
---|
89 |
|
---|
90 | struct GraphicsPipelineInfo {
|
---|
91 | VkPipelineLayout pipelineLayout;
|
---|
92 | VkPipeline pipeline;
|
---|
93 |
|
---|
94 | VkVertexInputBindingDescription bindingDescription;
|
---|
95 | vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
---|
96 |
|
---|
97 | vector<DescriptorInfo> descriptorInfoList;
|
---|
98 |
|
---|
99 | VkDescriptorPool descriptorPool;
|
---|
100 | VkDescriptorSetLayout descriptorSetLayout;
|
---|
101 | vector<VkDescriptorSet> descriptorSets;
|
---|
102 |
|
---|
103 | size_t numVertices; // Currently unused
|
---|
104 | VkBuffer vertexBuffer;
|
---|
105 | VkDeviceMemory vertexBufferMemory;
|
---|
106 |
|
---|
107 | size_t numIndices;
|
---|
108 | VkBuffer indexBuffer;
|
---|
109 | VkDeviceMemory indexBufferMemory;
|
---|
110 | };
|
---|
111 |
|
---|
112 | /*** START OF REFACTORED CODE ***/
|
---|
113 | VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
|
---|
114 | const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
---|
115 | const VkAllocationCallbacks* pAllocator,
|
---|
116 | VkDebugUtilsMessengerEXT* pDebugMessenger) {
|
---|
117 | auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
---|
118 |
|
---|
119 | if (func != nullptr) {
|
---|
120 | return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
---|
121 | } else {
|
---|
122 | return VK_ERROR_EXTENSION_NOT_PRESENT;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | void DestroyDebugUtilsMessengerEXT(VkInstance instance,
|
---|
127 | VkDebugUtilsMessengerEXT debugMessenger,
|
---|
128 | const VkAllocationCallbacks* pAllocator) {
|
---|
129 | auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
|
---|
130 |
|
---|
131 | if (func != nullptr) {
|
---|
132 | func(instance, debugMessenger, pAllocator);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | class VulkanGame {
|
---|
137 | public:
|
---|
138 | void run() {
|
---|
139 | if (initWindow() == RTWO_ERROR) {
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | initVulkan();
|
---|
143 | mainLoop();
|
---|
144 | cleanup();
|
---|
145 | }
|
---|
146 |
|
---|
147 | private:
|
---|
148 | GameGui* gui = new GameGui_SDL();
|
---|
149 |
|
---|
150 | SDL_version sdlVersion;
|
---|
151 | SDL_Window* window = nullptr;
|
---|
152 | SDL_Renderer* gRenderer = nullptr;
|
---|
153 | /*** END OF REFACTORED CODE ***/
|
---|
154 | SDL_Texture* uiOverlay = nullptr;
|
---|
155 |
|
---|
156 | TTF_Font* gFont = nullptr;
|
---|
157 | SDL_Texture* uiText = nullptr;
|
---|
158 | SDL_Texture* uiImage = nullptr;
|
---|
159 |
|
---|
160 | /*** START OF REFACTORED CODE ***/
|
---|
161 | VkInstance instance;
|
---|
162 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
163 | VkSurfaceKHR surface;
|
---|
164 |
|
---|
165 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
166 | VkDevice device;
|
---|
167 |
|
---|
168 | VkQueue graphicsQueue;
|
---|
169 | VkQueue presentQueue;
|
---|
170 |
|
---|
171 | VkSwapchainKHR swapChain;
|
---|
172 | vector<VkImage> swapChainImages;
|
---|
173 | VkFormat swapChainImageFormat;
|
---|
174 | VkExtent2D swapChainExtent;
|
---|
175 | vector<VkImageView> swapChainImageViews;
|
---|
176 | /*** END OF REFACTORED CODE ***/
|
---|
177 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
178 |
|
---|
179 | /*** START OF REFACTORED CODE ***/
|
---|
180 | VkRenderPass renderPass;
|
---|
181 |
|
---|
182 | VkCommandPool commandPool;
|
---|
183 | /*** END OF REFACTORED CODE ***/
|
---|
184 | vector<VkCommandBuffer> commandBuffers;
|
---|
185 |
|
---|
186 | // The images and the sampler are used to store data for specific attributes. I probably
|
---|
187 | // want to keep them separate from the GraphicsPipelineInfo objects and start passing
|
---|
188 | // references to them once I start defining uniform and varying attributes in GraphicsPipelineInfo objects
|
---|
189 |
|
---|
190 | VkImage depthImage;
|
---|
191 | VkDeviceMemory depthImageMemory;
|
---|
192 | VkImageView depthImageView;
|
---|
193 |
|
---|
194 | VkImage textureImage;
|
---|
195 | VkDeviceMemory textureImageMemory;
|
---|
196 | VkImageView textureImageView;
|
---|
197 |
|
---|
198 | VkImage overlayImage;
|
---|
199 | VkDeviceMemory overlayImageMemory;
|
---|
200 | VkImageView overlayImageView;
|
---|
201 |
|
---|
202 | VkImage sdlOverlayImage;
|
---|
203 | VkDeviceMemory sdlOverlayImageMemory;
|
---|
204 | VkImageView sdlOverlayImageView;
|
---|
205 |
|
---|
206 | VkSampler textureSampler;
|
---|
207 |
|
---|
208 | // These are currently to store the MVP matrix
|
---|
209 | // I should figure out if it makes sense to use them for other uniforms in the future
|
---|
210 | // If not, I should rename them to better indicate their puprose.
|
---|
211 | // I should also decide if I can use these for all shaders, or if I need a separapte set of buffers for each one
|
---|
212 | vector<VkBuffer> uniformBuffers;
|
---|
213 | vector<VkDeviceMemory> uniformBuffersMemory;
|
---|
214 |
|
---|
215 | VkDescriptorImageInfo sceneImageInfo;
|
---|
216 | VkDescriptorImageInfo overlayImageInfo;
|
---|
217 |
|
---|
218 | vector<VkDescriptorBufferInfo> uniformBufferInfoList;
|
---|
219 |
|
---|
220 | GraphicsPipelineInfo scenePipeline;
|
---|
221 | GraphicsPipelineInfo overlayPipeline;
|
---|
222 |
|
---|
223 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
224 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
225 | vector<VkFence> inFlightFences;
|
---|
226 |
|
---|
227 | size_t currentFrame = 0;
|
---|
228 |
|
---|
229 | /*** START OF REFACTORED CODE ***/
|
---|
230 | bool framebufferResized = false;
|
---|
231 |
|
---|
232 | bool initWindow() {
|
---|
233 | if (gui->init() == RTWO_ERROR) {
|
---|
234 | cout << "UI library could not be initialized!" << endl;
|
---|
235 | cout << SDL_GetError() << endl;
|
---|
236 | return RTWO_ERROR;
|
---|
237 | }
|
---|
238 | cout << "GUI init succeeded" << endl;
|
---|
239 |
|
---|
240 | window = (SDL_Window*) gui->createWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT, true);
|
---|
241 | if (window == nullptr) {
|
---|
242 | cout << "Window could not be created!" << endl;
|
---|
243 | return RTWO_ERROR;
|
---|
244 | }
|
---|
245 |
|
---|
246 | gRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
---|
247 | if (gRenderer == nullptr) {
|
---|
248 | cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << endl;
|
---|
249 | return RTWO_ERROR;
|
---|
250 | }
|
---|
251 | /*** END OF REFACTORED CODE ***/
|
---|
252 |
|
---|
253 | SDL_VERSION(&sdlVersion);
|
---|
254 |
|
---|
255 | // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
|
---|
256 | // However, the latest SDL version available through homebrew on Mac is 2.0.9, which requires SDL_TEXTUREACCESS_STREAMING
|
---|
257 | // I tried building sdl 2.0.10 (and sdl_image and sdl_ttf) from source on Mac, but had some issues, so this is easier
|
---|
258 | // until the homebrew recipe is updated
|
---|
259 | if (sdlVersion.major == 2 && sdlVersion.minor == 0 && sdlVersion.patch == 9) {
|
---|
260 | uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
|
---|
261 | } else {
|
---|
262 | uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (uiOverlay == nullptr) {
|
---|
266 | cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
|
---|
267 | }
|
---|
268 | if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
|
---|
269 | cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
|
---|
270 | }
|
---|
271 |
|
---|
272 | gFont = TTF_OpenFont("fonts/lazy.ttf", 28);
|
---|
273 | if (gFont == nullptr) {
|
---|
274 | cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
|
---|
275 | return RTWO_ERROR;
|
---|
276 | }
|
---|
277 |
|
---|
278 | SDL_Color textColor = { 0, 0, 0 };
|
---|
279 |
|
---|
280 | SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Great sucess!", textColor);
|
---|
281 | if (textSurface == nullptr) {
|
---|
282 | cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
|
---|
283 | return RTWO_ERROR;
|
---|
284 | }
|
---|
285 |
|
---|
286 | uiText = SDL_CreateTextureFromSurface(gRenderer, textSurface);
|
---|
287 | if (uiText == nullptr) {
|
---|
288 | cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
|
---|
289 | SDL_FreeSurface(textSurface);
|
---|
290 | return RTWO_ERROR;
|
---|
291 | }
|
---|
292 |
|
---|
293 | SDL_FreeSurface(textSurface);
|
---|
294 |
|
---|
295 | // TODO: Load a PNG instead
|
---|
296 | SDL_Surface* uiImageSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
|
---|
297 | if (uiImageSurface == nullptr) {
|
---|
298 | cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
|
---|
299 | return RTWO_ERROR;
|
---|
300 | }
|
---|
301 |
|
---|
302 | uiImage = SDL_CreateTextureFromSurface(gRenderer, uiImageSurface);
|
---|
303 | if (uiImage == nullptr) {
|
---|
304 | cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
|
---|
305 | SDL_FreeSurface(uiImageSurface);
|
---|
306 | return RTWO_ERROR;
|
---|
307 | }
|
---|
308 |
|
---|
309 | SDL_FreeSurface(uiImageSurface);
|
---|
310 |
|
---|
311 | return RTWO_SUCCESS;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*** START OF REFACTORED CODE ***/
|
---|
315 | void initVulkan() {
|
---|
316 | createInstance();
|
---|
317 | setupDebugMessenger();
|
---|
318 | createSurface();
|
---|
319 | pickPhysicalDevice();
|
---|
320 | createLogicalDevice();
|
---|
321 | createSwapChain();
|
---|
322 | createImageViews();
|
---|
323 | createRenderPass();
|
---|
324 |
|
---|
325 | createCommandPool();
|
---|
326 | /*** END OF REFACTORED CODE ***/
|
---|
327 |
|
---|
328 | // THIS SECTION IS WHERE TEXTURES ARE CREATED, MAYBE SPLIT IT OFF INTO A SEPARATE FUNCTION
|
---|
329 | // MAY WANT TO CREATE A STRUCT TO HOLD SIMILAR VARIABLES< LIKE THOSE FOR A TEXTURE
|
---|
330 |
|
---|
331 | createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
|
---|
332 | createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView);
|
---|
333 | createTextureSampler();
|
---|
334 |
|
---|
335 | sceneImageInfo = {};
|
---|
336 | sceneImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
337 | sceneImageInfo.imageView = textureImageView;
|
---|
338 | sceneImageInfo.sampler = textureSampler;
|
---|
339 |
|
---|
340 | overlayImageInfo = {};
|
---|
341 | overlayImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
342 | overlayImageInfo.imageView = sdlOverlayImageView;
|
---|
343 | overlayImageInfo.sampler = textureSampler;
|
---|
344 |
|
---|
345 | // SHADER-SPECIFIC STUFF STARTS HERE
|
---|
346 |
|
---|
347 | vector<Vertex> sceneVertices = {
|
---|
348 | {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
349 | {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
350 | {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
351 | {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
|
---|
352 |
|
---|
353 | {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
354 | {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
355 | {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
---|
356 | {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
|
---|
357 | };
|
---|
358 | vector<uint16_t> sceneIndices = {
|
---|
359 | 0, 1, 2, 2, 3, 0,
|
---|
360 | 4, 5, 6, 6, 7, 4
|
---|
361 | };
|
---|
362 |
|
---|
363 | initGraphicsPipelineInfo(scenePipeline,
|
---|
364 | sceneVertices.data(), sizeof(Vertex), sceneVertices.size(),
|
---|
365 | sceneIndices.data(), sizeof(uint16_t), sceneIndices.size());
|
---|
366 |
|
---|
367 | addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
|
---|
368 | addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
|
---|
369 | addAttributeDescription(scenePipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
|
---|
370 |
|
---|
371 | addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList, nullptr);
|
---|
372 | addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &sceneImageInfo);
|
---|
373 |
|
---|
374 | createDescriptorSetLayout(scenePipeline);
|
---|
375 |
|
---|
376 |
|
---|
377 | vector<OverlayVertex> overlayVertices = {
|
---|
378 | {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}},
|
---|
379 | {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
|
---|
380 | {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}},
|
---|
381 | {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}
|
---|
382 | };
|
---|
383 | vector<uint16_t> overlayIndices = {
|
---|
384 | 0, 1, 2, 2, 3, 0
|
---|
385 | };
|
---|
386 |
|
---|
387 | initGraphicsPipelineInfo(overlayPipeline,
|
---|
388 | overlayVertices.data(), sizeof(OverlayVertex), overlayVertices.size(),
|
---|
389 | overlayIndices.data(), sizeof(uint16_t), overlayIndices.size());
|
---|
390 |
|
---|
391 | addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
|
---|
392 | addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
|
---|
393 |
|
---|
394 | addDescriptorInfo(overlayPipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &overlayImageInfo);
|
---|
395 |
|
---|
396 | createDescriptorSetLayout(overlayPipeline);
|
---|
397 |
|
---|
398 | createBufferResources();
|
---|
399 |
|
---|
400 | createSyncObjects();
|
---|
401 | }
|
---|
402 |
|
---|
403 | void createInstance() {
|
---|
404 | if (enableValidationLayers && !checkValidationLayerSupport()) {
|
---|
405 | throw runtime_error("validation layers requested, but not available!");
|
---|
406 | }
|
---|
407 |
|
---|
408 | VkApplicationInfo appInfo = {};
|
---|
409 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
---|
410 | appInfo.pApplicationName = "Vulkan Game";
|
---|
411 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
412 | appInfo.pEngineName = "No Engine";
|
---|
413 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
414 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
---|
415 |
|
---|
416 | VkInstanceCreateInfo createInfo = {};
|
---|
417 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
---|
418 | createInfo.pApplicationInfo = &appInfo;
|
---|
419 |
|
---|
420 | vector<const char*> extensions = getRequiredExtensions();
|
---|
421 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
---|
422 | createInfo.ppEnabledExtensionNames = extensions.data();
|
---|
423 |
|
---|
424 | cout << endl << "Extensions:" << endl;
|
---|
425 | for (const char* extensionName : extensions) {
|
---|
426 | cout << extensionName << endl;
|
---|
427 | }
|
---|
428 | cout << endl;
|
---|
429 |
|
---|
430 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
---|
431 | if (enableValidationLayers) {
|
---|
432 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
433 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
434 |
|
---|
435 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
---|
436 | createInfo.pNext = &debugCreateInfo;
|
---|
437 | } else {
|
---|
438 | createInfo.enabledLayerCount = 0;
|
---|
439 |
|
---|
440 | createInfo.pNext = nullptr;
|
---|
441 | }
|
---|
442 |
|
---|
443 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
---|
444 | throw runtime_error("failed to create instance!");
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | bool checkValidationLayerSupport() {
|
---|
449 | uint32_t layerCount;
|
---|
450 | vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
---|
451 |
|
---|
452 | vector<VkLayerProperties> availableLayers(layerCount);
|
---|
453 | vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
---|
454 |
|
---|
455 | for (const char* layerName : validationLayers) {
|
---|
456 | bool layerFound = false;
|
---|
457 |
|
---|
458 | for (const auto& layerProperties : availableLayers) {
|
---|
459 | if (strcmp(layerName, layerProperties.layerName) == 0) {
|
---|
460 | layerFound = true;
|
---|
461 | break;
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (!layerFound) {
|
---|
466 | return false;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | return true;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /*** START OF REFACTORED CODE ***/
|
---|
474 | vector<const char*> getRequiredExtensions() {
|
---|
475 | vector<const char*> extensions = gui->getRequiredExtensions();
|
---|
476 |
|
---|
477 | if (enableValidationLayers) {
|
---|
478 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
---|
479 | }
|
---|
480 |
|
---|
481 | return extensions;
|
---|
482 | }
|
---|
483 |
|
---|
484 | void setupDebugMessenger() {
|
---|
485 | if (!enableValidationLayers) return;
|
---|
486 |
|
---|
487 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
---|
488 | populateDebugMessengerCreateInfo(createInfo);
|
---|
489 |
|
---|
490 | if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
---|
491 | throw runtime_error("failed to set up debug messenger!");
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
---|
496 | createInfo = {};
|
---|
497 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
---|
498 | createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
---|
499 | createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
---|
500 | createInfo.pfnUserCallback = debugCallback;
|
---|
501 | }
|
---|
502 |
|
---|
503 | void createSurface() {
|
---|
504 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
---|
505 | throw runtime_error("failed to create window surface!");
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | void pickPhysicalDevice() {
|
---|
510 | uint32_t deviceCount = 0;
|
---|
511 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
---|
512 |
|
---|
513 | if (deviceCount == 0) {
|
---|
514 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
---|
515 | }
|
---|
516 |
|
---|
517 | vector<VkPhysicalDevice> devices(deviceCount);
|
---|
518 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
---|
519 |
|
---|
520 | cout << endl << "Graphics cards:" << endl;
|
---|
521 | for (const VkPhysicalDevice& device : devices) {
|
---|
522 | if (isDeviceSuitable(device)) {
|
---|
523 | physicalDevice = device;
|
---|
524 | break;
|
---|
525 | }
|
---|
526 | }
|
---|
527 | cout << endl;
|
---|
528 |
|
---|
529 | if (physicalDevice == VK_NULL_HANDLE) {
|
---|
530 | throw runtime_error("failed to find a suitable GPU!");
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | bool isDeviceSuitable(VkPhysicalDevice device) {
|
---|
535 | VkPhysicalDeviceProperties deviceProperties;
|
---|
536 | vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
---|
537 |
|
---|
538 | cout << "Device: " << deviceProperties.deviceName << endl;
|
---|
539 |
|
---|
540 | QueueFamilyIndices indices = findQueueFamilies(device);
|
---|
541 | bool extensionsSupported = checkDeviceExtensionSupport(device);
|
---|
542 | bool swapChainAdequate = false;
|
---|
543 |
|
---|
544 | if (extensionsSupported) {
|
---|
545 | SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
|
---|
546 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
---|
547 | }
|
---|
548 |
|
---|
549 | VkPhysicalDeviceFeatures supportedFeatures;
|
---|
550 | vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
|
---|
551 |
|
---|
552 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
---|
553 | }
|
---|
554 |
|
---|
555 | bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
|
---|
556 | uint32_t extensionCount;
|
---|
557 | vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
|
---|
558 |
|
---|
559 | vector<VkExtensionProperties> availableExtensions(extensionCount);
|
---|
560 | vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
|
---|
561 |
|
---|
562 | set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
|
---|
563 |
|
---|
564 | for (const auto& extension : availableExtensions) {
|
---|
565 | requiredExtensions.erase(extension.extensionName);
|
---|
566 | }
|
---|
567 |
|
---|
568 | return requiredExtensions.empty();
|
---|
569 | }
|
---|
570 |
|
---|
571 | void createLogicalDevice() {
|
---|
572 | QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
|
---|
573 |
|
---|
574 | vector<VkDeviceQueueCreateInfo> queueCreateInfos;
|
---|
575 | set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
|
---|
576 |
|
---|
577 | float queuePriority = 1.0f;
|
---|
578 | for (uint32_t queueFamily : uniqueQueueFamilies) {
|
---|
579 | VkDeviceQueueCreateInfo queueCreateInfo = {};
|
---|
580 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
---|
581 | queueCreateInfo.queueFamilyIndex = queueFamily;
|
---|
582 | queueCreateInfo.queueCount = 1;
|
---|
583 | queueCreateInfo.pQueuePriorities = &queuePriority;
|
---|
584 |
|
---|
585 | queueCreateInfos.push_back(queueCreateInfo);
|
---|
586 | }
|
---|
587 |
|
---|
588 | VkPhysicalDeviceFeatures deviceFeatures = {};
|
---|
589 | deviceFeatures.samplerAnisotropy = VK_TRUE;
|
---|
590 |
|
---|
591 | VkDeviceCreateInfo createInfo = {};
|
---|
592 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
---|
593 | createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
|
---|
594 | createInfo.pQueueCreateInfos = queueCreateInfos.data();
|
---|
595 |
|
---|
596 | createInfo.pEnabledFeatures = &deviceFeatures;
|
---|
597 |
|
---|
598 | createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
|
---|
599 | createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
---|
600 |
|
---|
601 | // These fields are ignored by up-to-date Vulkan implementations,
|
---|
602 | // but it's a good idea to set them for backwards compatibility
|
---|
603 | if (enableValidationLayers) {
|
---|
604 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
605 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
606 | } else {
|
---|
607 | createInfo.enabledLayerCount = 0;
|
---|
608 | }
|
---|
609 |
|
---|
610 | if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
|
---|
611 | throw runtime_error("failed to create logical device!");
|
---|
612 | }
|
---|
613 |
|
---|
614 | vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
|
---|
615 | vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
---|
616 | }
|
---|
617 |
|
---|
618 | void createSwapChain() {
|
---|
619 | SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
|
---|
620 |
|
---|
621 | VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
|
---|
622 | VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
|
---|
623 | VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
|
---|
624 |
|
---|
625 | uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
---|
626 | if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
|
---|
627 | imageCount = swapChainSupport.capabilities.maxImageCount;
|
---|
628 | }
|
---|
629 |
|
---|
630 | VkSwapchainCreateInfoKHR createInfo = {};
|
---|
631 | createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
---|
632 | createInfo.surface = surface;
|
---|
633 | createInfo.minImageCount = imageCount;
|
---|
634 | createInfo.imageFormat = surfaceFormat.format;
|
---|
635 | createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
---|
636 | createInfo.imageExtent = extent;
|
---|
637 | createInfo.imageArrayLayers = 1;
|
---|
638 | createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
---|
639 |
|
---|
640 | QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
|
---|
641 | uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
|
---|
642 |
|
---|
643 | if (indices.graphicsFamily != indices.presentFamily) {
|
---|
644 | createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
---|
645 | createInfo.queueFamilyIndexCount = 2;
|
---|
646 | createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
---|
647 | } else {
|
---|
648 | createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
649 | createInfo.queueFamilyIndexCount = 0;
|
---|
650 | createInfo.pQueueFamilyIndices = nullptr;
|
---|
651 | }
|
---|
652 |
|
---|
653 | createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
---|
654 | createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
---|
655 | createInfo.presentMode = presentMode;
|
---|
656 | createInfo.clipped = VK_TRUE;
|
---|
657 | createInfo.oldSwapchain = VK_NULL_HANDLE;
|
---|
658 |
|
---|
659 | if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
|
---|
660 | throw runtime_error("failed to create swap chain!");
|
---|
661 | }
|
---|
662 |
|
---|
663 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
---|
664 | swapChainImages.resize(imageCount);
|
---|
665 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
---|
666 |
|
---|
667 | swapChainImageFormat = surfaceFormat.format;
|
---|
668 | swapChainExtent = extent;
|
---|
669 | }
|
---|
670 |
|
---|
671 | SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
|
---|
672 | SwapChainSupportDetails details;
|
---|
673 |
|
---|
674 | vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
|
---|
675 |
|
---|
676 | uint32_t formatCount;
|
---|
677 | vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
|
---|
678 |
|
---|
679 | if (formatCount != 0) {
|
---|
680 | details.formats.resize(formatCount);
|
---|
681 | vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
|
---|
682 | }
|
---|
683 |
|
---|
684 | uint32_t presentModeCount;
|
---|
685 | vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
|
---|
686 |
|
---|
687 | if (presentModeCount != 0) {
|
---|
688 | details.presentModes.resize(presentModeCount);
|
---|
689 | vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
|
---|
690 | }
|
---|
691 |
|
---|
692 | return details;
|
---|
693 | }
|
---|
694 |
|
---|
695 | VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
|
---|
696 | for (const auto& availableFormat : availableFormats) {
|
---|
697 | if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
|
---|
698 | return availableFormat;
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | return availableFormats[0];
|
---|
703 | }
|
---|
704 |
|
---|
705 | VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
|
---|
706 | VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
|
---|
707 |
|
---|
708 | for (const auto& availablePresentMode : availablePresentModes) {
|
---|
709 | if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
---|
710 | return availablePresentMode;
|
---|
711 | }
|
---|
712 | else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
|
---|
713 | bestMode = availablePresentMode;
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | return bestMode;
|
---|
718 | }
|
---|
719 |
|
---|
720 | VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
|
---|
721 | if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
|
---|
722 | return capabilities.currentExtent;
|
---|
723 | } else {
|
---|
724 | VkExtent2D actualExtent = {
|
---|
725 | static_cast<uint32_t>(gui->getWindowWidth()),
|
---|
726 | static_cast<uint32_t>(gui->getWindowHeight())
|
---|
727 | };
|
---|
728 |
|
---|
729 | actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
|
---|
730 | actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
|
---|
731 |
|
---|
732 | return actualExtent;
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | void createImageViews() {
|
---|
737 | swapChainImageViews.resize(swapChainImages.size());
|
---|
738 |
|
---|
739 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
740 | swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | void createRenderPass() {
|
---|
745 | VkAttachmentDescription colorAttachment = {};
|
---|
746 | colorAttachment.format = swapChainImageFormat;
|
---|
747 | colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
748 | colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
749 | colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
---|
750 | colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
751 | colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
752 | colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
753 | colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
---|
754 |
|
---|
755 | VkAttachmentReference colorAttachmentRef = {};
|
---|
756 | colorAttachmentRef.attachment = 0;
|
---|
757 | colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
---|
758 |
|
---|
759 | VkAttachmentDescription depthAttachment = {};
|
---|
760 | depthAttachment.format = findDepthFormat();
|
---|
761 | depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
762 | depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
763 | depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
764 | depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
765 | depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
766 | depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
767 | depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
768 |
|
---|
769 | VkAttachmentReference depthAttachmentRef = {};
|
---|
770 | depthAttachmentRef.attachment = 1;
|
---|
771 | depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
772 |
|
---|
773 | VkSubpassDescription subpass = {};
|
---|
774 | subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
---|
775 | subpass.colorAttachmentCount = 1;
|
---|
776 | subpass.pColorAttachments = &colorAttachmentRef;
|
---|
777 | subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
---|
778 |
|
---|
779 | VkSubpassDependency dependency = {};
|
---|
780 | dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
---|
781 | dependency.dstSubpass = 0;
|
---|
782 | dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
783 | dependency.srcAccessMask = 0;
|
---|
784 | dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
785 | dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
---|
786 |
|
---|
787 | array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
|
---|
788 | VkRenderPassCreateInfo renderPassInfo = {};
|
---|
789 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
---|
790 | renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
791 | renderPassInfo.pAttachments = attachments.data();
|
---|
792 | renderPassInfo.subpassCount = 1;
|
---|
793 | renderPassInfo.pSubpasses = &subpass;
|
---|
794 | renderPassInfo.dependencyCount = 1;
|
---|
795 | renderPassInfo.pDependencies = &dependency;
|
---|
796 |
|
---|
797 | if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
|
---|
798 | throw runtime_error("failed to create render pass!");
|
---|
799 | }
|
---|
800 | }
|
---|
801 | /*** END OF REFACTORED CODE ***/
|
---|
802 |
|
---|
803 | void initGraphicsPipelineInfo(GraphicsPipelineInfo& info,
|
---|
804 | const void* vertexData, int vertexSize, size_t numVertices,
|
---|
805 | const void* indexData, int indexSize, size_t numIndices) {
|
---|
806 | // Since there is only one array of vertex data, we use binding = 0
|
---|
807 | // I'll probably do that for the foreseeable future
|
---|
808 | // I can calculate the stride myself given info about all the varying attributes
|
---|
809 |
|
---|
810 | info.bindingDescription.binding = 0;
|
---|
811 | info.bindingDescription.stride = vertexSize;
|
---|
812 | info.bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
---|
813 |
|
---|
814 | info.numVertices = numVertices;
|
---|
815 | createVertexBuffer(info, vertexData, vertexSize * numVertices);
|
---|
816 |
|
---|
817 | info.numIndices = numIndices;
|
---|
818 | createIndexBuffer(info, indexData, indexSize * numIndices);
|
---|
819 | }
|
---|
820 |
|
---|
821 | void addAttributeDescription(GraphicsPipelineInfo& info, VkFormat format, size_t offset) {
|
---|
822 | VkVertexInputAttributeDescription attributeDesc = {};
|
---|
823 |
|
---|
824 | attributeDesc.binding = 0;
|
---|
825 | attributeDesc.location = info.attributeDescriptions.size();
|
---|
826 | attributeDesc.format = format;
|
---|
827 | attributeDesc.offset = offset;
|
---|
828 |
|
---|
829 | info.attributeDescriptions.push_back(attributeDesc);
|
---|
830 | }
|
---|
831 |
|
---|
832 | void addDescriptorInfo(GraphicsPipelineInfo& info, VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData, VkDescriptorImageInfo* imageData) {
|
---|
833 | info.descriptorInfoList.push_back({ type, stageFlags, bufferData, imageData });
|
---|
834 | }
|
---|
835 |
|
---|
836 | void createDescriptorSetLayout(GraphicsPipelineInfo& info) {
|
---|
837 | vector<VkDescriptorSetLayoutBinding> bindings(info.descriptorInfoList.size());
|
---|
838 |
|
---|
839 | for (size_t i = 0; i < bindings.size(); i++) {
|
---|
840 | bindings[i].binding = i;
|
---|
841 | bindings[i].descriptorCount = 1;
|
---|
842 | bindings[i].descriptorType = info.descriptorInfoList[i].type;
|
---|
843 | bindings[i].stageFlags = info.descriptorInfoList[i].stageFlags;
|
---|
844 | bindings[i].pImmutableSamplers = nullptr;
|
---|
845 | }
|
---|
846 |
|
---|
847 | VkDescriptorSetLayoutCreateInfo layoutInfo = {};
|
---|
848 | layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
---|
849 | layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
|
---|
850 | layoutInfo.pBindings = bindings.data();
|
---|
851 |
|
---|
852 | if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
|
---|
853 | throw runtime_error("failed to create descriptor set layout!");
|
---|
854 | }
|
---|
855 | }
|
---|
856 |
|
---|
857 | void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, GraphicsPipelineInfo& info) {
|
---|
858 | auto vertShaderCode = readFile(vertShaderFile);
|
---|
859 | auto fragShaderCode = readFile(fragShaderFile);
|
---|
860 |
|
---|
861 | VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
---|
862 | VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
---|
863 |
|
---|
864 | VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
---|
865 | vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
866 | vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
---|
867 | vertShaderStageInfo.module = vertShaderModule;
|
---|
868 | vertShaderStageInfo.pName = "main";
|
---|
869 |
|
---|
870 | VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
---|
871 | fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
872 | fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
---|
873 | fragShaderStageInfo.module = fragShaderModule;
|
---|
874 | fragShaderStageInfo.pName = "main";
|
---|
875 |
|
---|
876 | VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
|
---|
877 |
|
---|
878 | VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
---|
879 | vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
---|
880 |
|
---|
881 | vertexInputInfo.vertexBindingDescriptionCount = 1;
|
---|
882 | vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(info.attributeDescriptions.size());
|
---|
883 | vertexInputInfo.pVertexBindingDescriptions = &info.bindingDescription;
|
---|
884 | vertexInputInfo.pVertexAttributeDescriptions = info.attributeDescriptions.data();
|
---|
885 |
|
---|
886 | VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
---|
887 | inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
---|
888 | inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
---|
889 | inputAssembly.primitiveRestartEnable = VK_FALSE;
|
---|
890 |
|
---|
891 | VkViewport viewport = {};
|
---|
892 | viewport.x = 0.0f;
|
---|
893 | viewport.y = 0.0f;
|
---|
894 | viewport.width = (float) swapChainExtent.width;
|
---|
895 | viewport.height = (float) swapChainExtent.height;
|
---|
896 | viewport.minDepth = 0.0f;
|
---|
897 | viewport.maxDepth = 1.0f;
|
---|
898 |
|
---|
899 | VkRect2D scissor = {};
|
---|
900 | scissor.offset = { 0, 0 };
|
---|
901 | scissor.extent = swapChainExtent;
|
---|
902 |
|
---|
903 | VkPipelineViewportStateCreateInfo viewportState = {};
|
---|
904 | viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
---|
905 | viewportState.viewportCount = 1;
|
---|
906 | viewportState.pViewports = &viewport;
|
---|
907 | viewportState.scissorCount = 1;
|
---|
908 | viewportState.pScissors = &scissor;
|
---|
909 |
|
---|
910 | VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
---|
911 | rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
---|
912 | rasterizer.depthClampEnable = VK_FALSE;
|
---|
913 | rasterizer.rasterizerDiscardEnable = VK_FALSE;
|
---|
914 | rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
---|
915 | rasterizer.lineWidth = 1.0f;
|
---|
916 | rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
---|
917 | rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
---|
918 | rasterizer.depthBiasEnable = VK_FALSE;
|
---|
919 |
|
---|
920 | VkPipelineMultisampleStateCreateInfo multisampling = {};
|
---|
921 | multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
---|
922 | multisampling.sampleShadingEnable = VK_FALSE;
|
---|
923 | multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
---|
924 |
|
---|
925 | VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
---|
926 | colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
---|
927 | colorBlendAttachment.blendEnable = VK_TRUE;
|
---|
928 | colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
---|
929 | colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
930 | colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
931 | colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
---|
932 | colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
933 | colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
934 |
|
---|
935 | VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
---|
936 | colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
---|
937 | colorBlending.logicOpEnable = VK_FALSE;
|
---|
938 | colorBlending.logicOp = VK_LOGIC_OP_COPY;
|
---|
939 | colorBlending.attachmentCount = 1;
|
---|
940 | colorBlending.pAttachments = &colorBlendAttachment;
|
---|
941 | colorBlending.blendConstants[0] = 0.0f;
|
---|
942 | colorBlending.blendConstants[1] = 0.0f;
|
---|
943 | colorBlending.blendConstants[2] = 0.0f;
|
---|
944 | colorBlending.blendConstants[3] = 0.0f;
|
---|
945 |
|
---|
946 | VkPipelineDepthStencilStateCreateInfo depthStencil = {};
|
---|
947 | depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
---|
948 | depthStencil.depthTestEnable = VK_TRUE;
|
---|
949 | depthStencil.depthWriteEnable = VK_TRUE;
|
---|
950 | depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
|
---|
951 | depthStencil.depthBoundsTestEnable = VK_FALSE;
|
---|
952 | depthStencil.minDepthBounds = 0.0f;
|
---|
953 | depthStencil.maxDepthBounds = 1.0f;
|
---|
954 | depthStencil.stencilTestEnable = VK_FALSE;
|
---|
955 | depthStencil.front = {};
|
---|
956 | depthStencil.back = {};
|
---|
957 |
|
---|
958 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
---|
959 | pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
---|
960 | pipelineLayoutInfo.setLayoutCount = 1;
|
---|
961 | pipelineLayoutInfo.pSetLayouts = &info.descriptorSetLayout;
|
---|
962 | pipelineLayoutInfo.pushConstantRangeCount = 0;
|
---|
963 |
|
---|
964 | if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &info.pipelineLayout) != VK_SUCCESS) {
|
---|
965 | throw runtime_error("failed to create pipeline layout!");
|
---|
966 | }
|
---|
967 |
|
---|
968 | VkGraphicsPipelineCreateInfo pipelineInfo = {};
|
---|
969 | pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
---|
970 | pipelineInfo.stageCount = 2;
|
---|
971 | pipelineInfo.pStages = shaderStages;
|
---|
972 | pipelineInfo.pVertexInputState = &vertexInputInfo;
|
---|
973 | pipelineInfo.pInputAssemblyState = &inputAssembly;
|
---|
974 | pipelineInfo.pViewportState = &viewportState;
|
---|
975 | pipelineInfo.pRasterizationState = &rasterizer;
|
---|
976 | pipelineInfo.pMultisampleState = &multisampling;
|
---|
977 | pipelineInfo.pDepthStencilState = &depthStencil;
|
---|
978 | pipelineInfo.pColorBlendState = &colorBlending;
|
---|
979 | pipelineInfo.pDynamicState = nullptr;
|
---|
980 | pipelineInfo.layout = info.pipelineLayout;
|
---|
981 | pipelineInfo.renderPass = renderPass;
|
---|
982 | pipelineInfo.subpass = 0;
|
---|
983 | pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
---|
984 | pipelineInfo.basePipelineIndex = -1;
|
---|
985 |
|
---|
986 | if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &info.pipeline) != VK_SUCCESS) {
|
---|
987 | throw runtime_error("failed to create graphics pipeline!");
|
---|
988 | }
|
---|
989 |
|
---|
990 | vkDestroyShaderModule(device, vertShaderModule, nullptr);
|
---|
991 | vkDestroyShaderModule(device, fragShaderModule, nullptr);
|
---|
992 | }
|
---|
993 |
|
---|
994 | VkShaderModule createShaderModule(const vector<char>& code) {
|
---|
995 | VkShaderModuleCreateInfo createInfo = {};
|
---|
996 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
---|
997 | createInfo.codeSize = code.size();
|
---|
998 | createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
---|
999 |
|
---|
1000 | VkShaderModule shaderModule;
|
---|
1001 | if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
|
---|
1002 | throw runtime_error("failed to create shader module!");
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | return shaderModule;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | void createFramebuffers() {
|
---|
1009 | swapChainFramebuffers.resize(swapChainImageViews.size());
|
---|
1010 |
|
---|
1011 | for (size_t i = 0; i < swapChainImageViews.size(); i++) {
|
---|
1012 | array <VkImageView, 2> attachments = {
|
---|
1013 | swapChainImageViews[i],
|
---|
1014 | depthImageView
|
---|
1015 | };
|
---|
1016 |
|
---|
1017 | VkFramebufferCreateInfo framebufferInfo = {};
|
---|
1018 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
---|
1019 | framebufferInfo.renderPass = renderPass;
|
---|
1020 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
1021 | framebufferInfo.pAttachments = attachments.data();
|
---|
1022 | framebufferInfo.width = swapChainExtent.width;
|
---|
1023 | framebufferInfo.height = swapChainExtent.height;
|
---|
1024 | framebufferInfo.layers = 1;
|
---|
1025 |
|
---|
1026 | if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
|
---|
1027 | throw runtime_error("failed to create framebuffer!");
|
---|
1028 | }
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | /*** START OF REFACTORED CODE ***/
|
---|
1033 | void createCommandPool() {
|
---|
1034 | QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
|
---|
1035 |
|
---|
1036 | VkCommandPoolCreateInfo poolInfo = {};
|
---|
1037 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
---|
1038 | poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
|
---|
1039 | poolInfo.flags = 0;
|
---|
1040 |
|
---|
1041 | if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
|
---|
1042 | throw runtime_error("failed to create graphics command pool!");
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
|
---|
1047 | QueueFamilyIndices indices;
|
---|
1048 |
|
---|
1049 | uint32_t queueFamilyCount = 0;
|
---|
1050 | vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
---|
1051 |
|
---|
1052 | vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
---|
1053 | vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
---|
1054 |
|
---|
1055 | int i = 0;
|
---|
1056 | for (const auto& queueFamily : queueFamilies) {
|
---|
1057 | if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
---|
1058 | indices.graphicsFamily = i;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | VkBool32 presentSupport = false;
|
---|
1062 | vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
---|
1063 |
|
---|
1064 | if (queueFamily.queueCount > 0 && presentSupport) {
|
---|
1065 | indices.presentFamily = i;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | if (indices.isComplete()) {
|
---|
1069 | break;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | i++;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | return indices;
|
---|
1076 | }
|
---|
1077 | /*** END OF REFACTORED CODE ***/
|
---|
1078 |
|
---|
1079 | void createDepthResources() {
|
---|
1080 | VkFormat depthFormat = findDepthFormat();
|
---|
1081 |
|
---|
1082 | createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
|
---|
1083 | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
|
---|
1084 | depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
|
---|
1085 |
|
---|
1086 | transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | /*** START OF REFACTORED CODE ***/
|
---|
1090 | VkFormat findDepthFormat() {
|
---|
1091 | return findSupportedFormat(
|
---|
1092 | { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
|
---|
1093 | VK_IMAGE_TILING_OPTIMAL,
|
---|
1094 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
|
---|
1095 | );
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | VkFormat findSupportedFormat(const vector<VkFormat>& candidates, VkImageTiling tiling,
|
---|
1099 | VkFormatFeatureFlags features) {
|
---|
1100 | for (VkFormat format : candidates) {
|
---|
1101 | VkFormatProperties props;
|
---|
1102 | vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
|
---|
1103 |
|
---|
1104 | if (tiling == VK_IMAGE_TILING_LINEAR &&
|
---|
1105 | (props.linearTilingFeatures & features) == features) {
|
---|
1106 | return format;
|
---|
1107 | } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
|
---|
1108 | (props.optimalTilingFeatures & features) == features) {
|
---|
1109 | return format;
|
---|
1110 | }
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | throw runtime_error("failed to find supported format!");
|
---|
1114 | }
|
---|
1115 | /*** END OF REFACTORED CODE ***/
|
---|
1116 |
|
---|
1117 | bool hasStencilComponent(VkFormat format) {
|
---|
1118 | return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
|
---|
1122 | int texWidth, texHeight, texChannels;
|
---|
1123 |
|
---|
1124 | stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
---|
1125 | VkDeviceSize imageSize = texWidth * texHeight * 4;
|
---|
1126 |
|
---|
1127 | if (!pixels) {
|
---|
1128 | throw runtime_error("failed to load texture image!");
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | VkBuffer stagingBuffer;
|
---|
1132 | VkDeviceMemory stagingBufferMemory;
|
---|
1133 |
|
---|
1134 | createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
1135 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
1136 | stagingBuffer, stagingBufferMemory);
|
---|
1137 |
|
---|
1138 | void* data;
|
---|
1139 |
|
---|
1140 | vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
|
---|
1141 | memcpy(data, pixels, static_cast<size_t>(imageSize));
|
---|
1142 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
1143 |
|
---|
1144 | stbi_image_free(pixels);
|
---|
1145 |
|
---|
1146 | createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
|
---|
1147 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
---|
1148 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
|
---|
1149 |
|
---|
1150 | transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
---|
1151 | copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
|
---|
1152 | transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
---|
1153 |
|
---|
1154 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
1155 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
1156 |
|
---|
1157 | view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | void createImageResourcesFromSDLTexture(SDL_Texture* texture, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
|
---|
1161 | int a, w, h;
|
---|
1162 |
|
---|
1163 | // I only need this here for the width and height, which are constants, so just use those instead
|
---|
1164 | SDL_QueryTexture(texture, nullptr, &a, &w, &h);
|
---|
1165 |
|
---|
1166 | createImage(w, h, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
|
---|
1167 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
---|
1168 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
|
---|
1169 |
|
---|
1170 | view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | void populateImageFromSDLTexture(SDL_Texture* texture, VkImage& image) {
|
---|
1174 | int a, w, h;
|
---|
1175 |
|
---|
1176 | SDL_QueryTexture(texture, nullptr, &a, &w, &h);
|
---|
1177 |
|
---|
1178 | VkDeviceSize imageSize = w * h * 4;
|
---|
1179 | unsigned char* pixels = new unsigned char[imageSize];
|
---|
1180 |
|
---|
1181 | SDL_RenderReadPixels(gRenderer, nullptr, SDL_PIXELFORMAT_ABGR8888, pixels, w * 4);
|
---|
1182 |
|
---|
1183 | VkBuffer stagingBuffer;
|
---|
1184 | VkDeviceMemory stagingBufferMemory;
|
---|
1185 |
|
---|
1186 | createBuffer(imageSize,
|
---|
1187 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
1188 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
---|
1189 | stagingBuffer, stagingBufferMemory);
|
---|
1190 |
|
---|
1191 | void* data;
|
---|
1192 |
|
---|
1193 | vkMapMemory(device, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data);
|
---|
1194 | memcpy(data, pixels, static_cast<size_t>(imageSize));
|
---|
1195 |
|
---|
1196 | VkMappedMemoryRange mappedMemoryRange = {};
|
---|
1197 | mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
---|
1198 | mappedMemoryRange.memory = stagingBufferMemory;
|
---|
1199 | mappedMemoryRange.offset = 0;
|
---|
1200 | mappedMemoryRange.size = VK_WHOLE_SIZE;
|
---|
1201 |
|
---|
1202 | // TODO: Should probably check that the function succeeded
|
---|
1203 | vkFlushMappedMemoryRanges(device, 1, &mappedMemoryRange);
|
---|
1204 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
1205 |
|
---|
1206 | delete[] pixels;
|
---|
1207 |
|
---|
1208 | transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
---|
1209 | copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(w), static_cast<uint32_t>(h));
|
---|
1210 | transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
---|
1211 |
|
---|
1212 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
1213 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
|
---|
1217 | VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
|
---|
1218 | VkImageCreateInfo imageInfo = {};
|
---|
1219 | imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
---|
1220 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
---|
1221 | imageInfo.extent.width = width;
|
---|
1222 | imageInfo.extent.height = height;
|
---|
1223 | imageInfo.extent.depth = 1;
|
---|
1224 | imageInfo.mipLevels = 1;
|
---|
1225 | imageInfo.arrayLayers = 1;
|
---|
1226 | imageInfo.format = format;
|
---|
1227 | imageInfo.tiling = tiling;
|
---|
1228 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
1229 | imageInfo.usage = usage;
|
---|
1230 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
1231 | imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
1232 |
|
---|
1233 | if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
|
---|
1234 | throw runtime_error("failed to create image!");
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | VkMemoryRequirements memRequirements;
|
---|
1238 | vkGetImageMemoryRequirements(device, image, &memRequirements);
|
---|
1239 |
|
---|
1240 | VkMemoryAllocateInfo allocInfo = {};
|
---|
1241 | allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
---|
1242 | allocInfo.allocationSize = memRequirements.size;
|
---|
1243 | allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
|
---|
1244 |
|
---|
1245 | if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
|
---|
1246 | throw runtime_error("failed to allocate image memory!");
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | vkBindImageMemory(device, image, imageMemory, 0);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
|
---|
1253 | VkCommandBuffer commandBuffer = beginSingleTimeCommands();
|
---|
1254 |
|
---|
1255 | VkImageMemoryBarrier barrier = {};
|
---|
1256 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
---|
1257 | barrier.oldLayout = oldLayout;
|
---|
1258 | barrier.newLayout = newLayout;
|
---|
1259 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
---|
1260 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
---|
1261 | barrier.image = image;
|
---|
1262 |
|
---|
1263 | if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
---|
1264 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
---|
1265 |
|
---|
1266 | if (hasStencilComponent(format)) {
|
---|
1267 | barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
|
---|
1268 | }
|
---|
1269 | } else {
|
---|
1270 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | barrier.subresourceRange.baseMipLevel = 0;
|
---|
1274 | barrier.subresourceRange.levelCount = 1;
|
---|
1275 | barrier.subresourceRange.baseArrayLayer = 0;
|
---|
1276 | barrier.subresourceRange.layerCount = 1;
|
---|
1277 |
|
---|
1278 | VkPipelineStageFlags sourceStage;
|
---|
1279 | VkPipelineStageFlags destinationStage;
|
---|
1280 |
|
---|
1281 | if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
|
---|
1282 | barrier.srcAccessMask = 0;
|
---|
1283 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
---|
1284 |
|
---|
1285 | sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
---|
1286 | destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
---|
1287 | } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
|
---|
1288 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
---|
1289 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
---|
1290 |
|
---|
1291 | sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
---|
1292 | destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
---|
1293 | } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
---|
1294 | barrier.srcAccessMask = 0;
|
---|
1295 | barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
---|
1296 |
|
---|
1297 | sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
---|
1298 | destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
---|
1299 | } else {
|
---|
1300 | throw invalid_argument("unsupported layout transition!");
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | vkCmdPipelineBarrier(
|
---|
1304 | commandBuffer,
|
---|
1305 | sourceStage, destinationStage,
|
---|
1306 | 0,
|
---|
1307 | 0, nullptr,
|
---|
1308 | 0, nullptr,
|
---|
1309 | 1, &barrier
|
---|
1310 | );
|
---|
1311 |
|
---|
1312 | endSingleTimeCommands(commandBuffer);
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
|
---|
1316 | VkCommandBuffer commandBuffer = beginSingleTimeCommands();
|
---|
1317 |
|
---|
1318 | VkBufferImageCopy region = {};
|
---|
1319 | region.bufferOffset = 0;
|
---|
1320 | region.bufferRowLength = 0;
|
---|
1321 | region.bufferImageHeight = 0;
|
---|
1322 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
---|
1323 | region.imageSubresource.mipLevel = 0;
|
---|
1324 | region.imageSubresource.baseArrayLayer = 0;
|
---|
1325 | region.imageSubresource.layerCount = 1;
|
---|
1326 | region.imageOffset = { 0, 0, 0 };
|
---|
1327 | region.imageExtent = { width, height, 1 };
|
---|
1328 |
|
---|
1329 | vkCmdCopyBufferToImage(
|
---|
1330 | commandBuffer,
|
---|
1331 | buffer,
|
---|
1332 | image,
|
---|
1333 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
---|
1334 | 1,
|
---|
1335 | ®ion
|
---|
1336 | );
|
---|
1337 |
|
---|
1338 | endSingleTimeCommands(commandBuffer);
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | /*** START OF REFACTORED CODE ***/
|
---|
1342 | VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
|
---|
1343 | VkImageViewCreateInfo viewInfo = {};
|
---|
1344 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
---|
1345 | viewInfo.image = image;
|
---|
1346 | viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
---|
1347 | viewInfo.format = format;
|
---|
1348 |
|
---|
1349 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
1350 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
1351 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
1352 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
1353 |
|
---|
1354 | viewInfo.subresourceRange.aspectMask = aspectFlags;
|
---|
1355 | viewInfo.subresourceRange.baseMipLevel = 0;
|
---|
1356 | viewInfo.subresourceRange.levelCount = 1;
|
---|
1357 | viewInfo.subresourceRange.baseArrayLayer = 0;
|
---|
1358 | viewInfo.subresourceRange.layerCount = 1;
|
---|
1359 |
|
---|
1360 | VkImageView imageView;
|
---|
1361 | if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
|
---|
1362 | throw runtime_error("failed to create texture image view!");
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | return imageView;
|
---|
1366 | }
|
---|
1367 | /*** END OF REFACTORED CODE ***/
|
---|
1368 |
|
---|
1369 | void createTextureSampler() {
|
---|
1370 | VkSamplerCreateInfo samplerInfo = {};
|
---|
1371 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
---|
1372 | samplerInfo.magFilter = VK_FILTER_LINEAR;
|
---|
1373 | samplerInfo.minFilter = VK_FILTER_LINEAR;
|
---|
1374 |
|
---|
1375 | samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
1376 | samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
1377 | samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
1378 |
|
---|
1379 | samplerInfo.anisotropyEnable = VK_TRUE;
|
---|
1380 | samplerInfo.maxAnisotropy = 16;
|
---|
1381 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
---|
1382 | samplerInfo.unnormalizedCoordinates = VK_FALSE;
|
---|
1383 | samplerInfo.compareEnable = VK_FALSE;
|
---|
1384 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
---|
1385 | samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
---|
1386 | samplerInfo.mipLodBias = 0.0f;
|
---|
1387 | samplerInfo.minLod = 0.0f;
|
---|
1388 | samplerInfo.maxLod = 0.0f;
|
---|
1389 |
|
---|
1390 | if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
|
---|
1391 | throw runtime_error("failed to create texture sampler!");
|
---|
1392 | }
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, VkDeviceSize bufferSize) {
|
---|
1396 | VkBuffer stagingBuffer;
|
---|
1397 | VkDeviceMemory stagingBufferMemory;
|
---|
1398 | createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
1399 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
1400 | stagingBuffer, stagingBufferMemory);
|
---|
1401 |
|
---|
1402 | void* data;
|
---|
1403 | vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
|
---|
1404 | memcpy(data, vertexData, (size_t) bufferSize);
|
---|
1405 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
1406 |
|
---|
1407 | createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
---|
1408 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory);
|
---|
1409 |
|
---|
1410 | copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);
|
---|
1411 |
|
---|
1412 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
1413 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, VkDeviceSize bufferSize) {
|
---|
1417 | VkBuffer stagingBuffer;
|
---|
1418 | VkDeviceMemory stagingBufferMemory;
|
---|
1419 | createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
1420 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
1421 | stagingBuffer, stagingBufferMemory);
|
---|
1422 |
|
---|
1423 | void* data;
|
---|
1424 | vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
|
---|
1425 | memcpy(data, indexData, (size_t) bufferSize);
|
---|
1426 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
1427 |
|
---|
1428 | createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
---|
1429 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory);
|
---|
1430 |
|
---|
1431 | copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);
|
---|
1432 |
|
---|
1433 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
1434 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | void createUniformBuffers() {
|
---|
1438 | VkDeviceSize bufferSize = sizeof(UniformBufferObject);
|
---|
1439 |
|
---|
1440 | uniformBuffers.resize(swapChainImages.size());
|
---|
1441 | uniformBuffersMemory.resize(swapChainImages.size());
|
---|
1442 | uniformBufferInfoList.resize(swapChainImages.size());
|
---|
1443 |
|
---|
1444 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
1445 | createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
1446 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
1447 | uniformBuffers[i], uniformBuffersMemory[i]);
|
---|
1448 |
|
---|
1449 | uniformBufferInfoList[i].buffer = uniformBuffers[i];
|
---|
1450 | uniformBufferInfoList[i].offset = 0;
|
---|
1451 | uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
|
---|
1452 | }
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
|
---|
1456 | VkBufferCreateInfo bufferInfo = {};
|
---|
1457 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
---|
1458 | bufferInfo.size = size;
|
---|
1459 | bufferInfo.usage = usage;
|
---|
1460 | bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
1461 |
|
---|
1462 | if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
|
---|
1463 | throw runtime_error("failed to create buffer!");
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | VkMemoryRequirements memRequirements;
|
---|
1467 | vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
|
---|
1468 |
|
---|
1469 | VkMemoryAllocateInfo allocInfo = {};
|
---|
1470 | allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
---|
1471 | allocInfo.allocationSize = memRequirements.size;
|
---|
1472 | allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
|
---|
1473 |
|
---|
1474 | if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
|
---|
1475 | throw runtime_error("failed to allocate buffer memory!");
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | vkBindBufferMemory(device, buffer, bufferMemory, 0);
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
|
---|
1482 | VkCommandBuffer commandBuffer = beginSingleTimeCommands();
|
---|
1483 |
|
---|
1484 | VkBufferCopy copyRegion = {};
|
---|
1485 | copyRegion.size = size;
|
---|
1486 | vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region);
|
---|
1487 |
|
---|
1488 | endSingleTimeCommands(commandBuffer);
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | VkCommandBuffer beginSingleTimeCommands() {
|
---|
1492 | VkCommandBufferAllocateInfo allocInfo = {};
|
---|
1493 | allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
---|
1494 | allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
---|
1495 | allocInfo.commandPool = commandPool;
|
---|
1496 | allocInfo.commandBufferCount = 1;
|
---|
1497 |
|
---|
1498 | VkCommandBuffer commandBuffer;
|
---|
1499 | vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
|
---|
1500 |
|
---|
1501 | VkCommandBufferBeginInfo beginInfo = {};
|
---|
1502 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
---|
1503 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
---|
1504 |
|
---|
1505 | vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
---|
1506 |
|
---|
1507 | return commandBuffer;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
|
---|
1511 | vkEndCommandBuffer(commandBuffer);
|
---|
1512 |
|
---|
1513 | VkSubmitInfo submitInfo = {};
|
---|
1514 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
---|
1515 | submitInfo.commandBufferCount = 1;
|
---|
1516 | submitInfo.pCommandBuffers = &commandBuffer;
|
---|
1517 |
|
---|
1518 | vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
|
---|
1519 | vkQueueWaitIdle(graphicsQueue);
|
---|
1520 |
|
---|
1521 | vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
|
---|
1525 | VkPhysicalDeviceMemoryProperties memProperties;
|
---|
1526 | vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
|
---|
1527 |
|
---|
1528 | for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
|
---|
1529 | if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
|
---|
1530 | return i;
|
---|
1531 | }
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | throw runtime_error("failed to find suitable memory type!");
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | void createDescriptorPool(GraphicsPipelineInfo& info) {
|
---|
1538 | vector<VkDescriptorPoolSize> poolSizes(info.descriptorInfoList.size());
|
---|
1539 |
|
---|
1540 | for (size_t i = 0; i < poolSizes.size(); i++) {
|
---|
1541 | poolSizes[i].type = info.descriptorInfoList[i].type;
|
---|
1542 | poolSizes[i].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | VkDescriptorPoolCreateInfo poolInfo = {};
|
---|
1546 | poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
---|
1547 | poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
|
---|
1548 | poolInfo.pPoolSizes = poolSizes.data();
|
---|
1549 | poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
|
---|
1550 |
|
---|
1551 | if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) {
|
---|
1552 | throw runtime_error("failed to create descriptor pool!");
|
---|
1553 | }
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | void createDescriptorSets(GraphicsPipelineInfo& info) {
|
---|
1557 | vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout);
|
---|
1558 |
|
---|
1559 | VkDescriptorSetAllocateInfo allocInfo = {};
|
---|
1560 | allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
---|
1561 | allocInfo.descriptorPool = info.descriptorPool;
|
---|
1562 | allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
|
---|
1563 | allocInfo.pSetLayouts = layouts.data();
|
---|
1564 |
|
---|
1565 | info.descriptorSets.resize(swapChainImages.size());
|
---|
1566 | if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) {
|
---|
1567 | throw runtime_error("failed to allocate descriptor sets!");
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
1571 | vector<VkWriteDescriptorSet> descriptorWrites(info.descriptorInfoList.size());
|
---|
1572 |
|
---|
1573 | for (size_t j = 0; j < descriptorWrites.size(); j++) {
|
---|
1574 | descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
---|
1575 | descriptorWrites[j].dstSet = info.descriptorSets[i];
|
---|
1576 | descriptorWrites[j].dstBinding = j;
|
---|
1577 | descriptorWrites[j].dstArrayElement = 0;
|
---|
1578 | descriptorWrites[j].descriptorType = info.descriptorInfoList[j].type;
|
---|
1579 | descriptorWrites[j].descriptorCount = 1;
|
---|
1580 | descriptorWrites[j].pBufferInfo = nullptr;
|
---|
1581 | descriptorWrites[j].pImageInfo = nullptr;
|
---|
1582 | descriptorWrites[j].pTexelBufferView = nullptr;
|
---|
1583 |
|
---|
1584 | switch (descriptorWrites[j].descriptorType) {
|
---|
1585 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
---|
1586 | descriptorWrites[j].pBufferInfo = &(*info.descriptorInfoList[j].bufferDataList)[i];
|
---|
1587 | break;
|
---|
1588 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
|
---|
1589 | descriptorWrites[j].pImageInfo = info.descriptorInfoList[j].imageData;
|
---|
1590 | break;
|
---|
1591 | default:
|
---|
1592 | cout << "Unknown descriptor type: " << descriptorWrites[j].descriptorType << endl;
|
---|
1593 | }
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | void createCommandBuffers() {
|
---|
1601 | commandBuffers.resize(swapChainFramebuffers.size());
|
---|
1602 |
|
---|
1603 | VkCommandBufferAllocateInfo allocInfo = {};
|
---|
1604 | allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
---|
1605 | allocInfo.commandPool = commandPool;
|
---|
1606 | allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
---|
1607 | allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
|
---|
1608 |
|
---|
1609 | if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
|
---|
1610 | throw runtime_error("failed to allocate command buffers!");
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | for (size_t i = 0; i < commandBuffers.size(); i++) {
|
---|
1614 | VkCommandBufferBeginInfo beginInfo = {};
|
---|
1615 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
---|
1616 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
---|
1617 | beginInfo.pInheritanceInfo = nullptr;
|
---|
1618 |
|
---|
1619 | if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
|
---|
1620 | throw runtime_error("failed to begin recording command buffer!");
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | VkRenderPassBeginInfo renderPassInfo = {};
|
---|
1624 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
---|
1625 | renderPassInfo.renderPass = renderPass;
|
---|
1626 | renderPassInfo.framebuffer = swapChainFramebuffers[i];
|
---|
1627 | renderPassInfo.renderArea.offset = { 0, 0 };
|
---|
1628 | renderPassInfo.renderArea.extent = swapChainExtent;
|
---|
1629 |
|
---|
1630 | array<VkClearValue, 2> clearValues = {};
|
---|
1631 | clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
|
---|
1632 | clearValues[1].depthStencil = { 1.0f, 0 };
|
---|
1633 |
|
---|
1634 | renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
|
---|
1635 | renderPassInfo.pClearValues = clearValues.data();
|
---|
1636 |
|
---|
1637 | vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
---|
1638 |
|
---|
1639 | createGraphicsPipelineCommands(scenePipeline, i);
|
---|
1640 | createGraphicsPipelineCommands(overlayPipeline, i);
|
---|
1641 |
|
---|
1642 | vkCmdEndRenderPass(commandBuffers[i]);
|
---|
1643 |
|
---|
1644 | if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
|
---|
1645 | throw runtime_error("failed to record command buffer!");
|
---|
1646 | }
|
---|
1647 | }
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | void createGraphicsPipelineCommands(GraphicsPipelineInfo& info, uint32_t currentImage) {
|
---|
1651 | vkCmdBindPipeline(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline);
|
---|
1652 | vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1,
|
---|
1653 | &info.descriptorSets[currentImage], 0, nullptr);
|
---|
1654 |
|
---|
1655 | VkBuffer vertexBuffers[] = { info.vertexBuffer };
|
---|
1656 | VkDeviceSize offsets[] = { 0 };
|
---|
1657 | vkCmdBindVertexBuffers(commandBuffers[currentImage], 0, 1, vertexBuffers, offsets);
|
---|
1658 |
|
---|
1659 | vkCmdBindIndexBuffer(commandBuffers[currentImage], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
---|
1660 |
|
---|
1661 | vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0);
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 | void createSyncObjects() {
|
---|
1665 | imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
1666 | renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
1667 | inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
1668 |
|
---|
1669 | VkSemaphoreCreateInfo semaphoreInfo = {};
|
---|
1670 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
---|
1671 |
|
---|
1672 | VkFenceCreateInfo fenceInfo = {};
|
---|
1673 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
---|
1674 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
---|
1675 |
|
---|
1676 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
1677 | if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
|
---|
1678 | vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
|
---|
1679 | vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
|
---|
1680 | throw runtime_error("failed to create synchronization objects for a frame!");
|
---|
1681 | }
|
---|
1682 | }
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 | /*** START OF REFACTORED CODE ***/
|
---|
1686 | void mainLoop() {
|
---|
1687 | // TODO: Create some generic event-handling functions in game-gui-*
|
---|
1688 | SDL_Event e;
|
---|
1689 | bool quit = false;
|
---|
1690 |
|
---|
1691 | while (!quit) {
|
---|
1692 | while (SDL_PollEvent(&e)) {
|
---|
1693 | if (e.type == SDL_QUIT) {
|
---|
1694 | quit = true;
|
---|
1695 | }
|
---|
1696 | if (e.type == SDL_KEYDOWN) {
|
---|
1697 | if (e.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
|
---|
1698 | quit = true;
|
---|
1699 | }
|
---|
1700 | }
|
---|
1701 | if (e.type == SDL_MOUSEBUTTONDOWN) {
|
---|
1702 | quit = true;
|
---|
1703 | }
|
---|
1704 | if (e.type == SDL_WINDOWEVENT) {
|
---|
1705 | if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
|
---|
1706 | e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
|
---|
1707 | framebufferResized = true;
|
---|
1708 | }
|
---|
1709 | }
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | drawUI();
|
---|
1713 |
|
---|
1714 | drawFrame();
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | vkDeviceWaitIdle(device);
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | void drawFrame() {
|
---|
1721 | /*** END OF REFACTORED CODE ***/
|
---|
1722 | vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
|
---|
1723 |
|
---|
1724 | uint32_t imageIndex;
|
---|
1725 |
|
---|
1726 | VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
|
---|
1727 | imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
---|
1728 |
|
---|
1729 | if (result == VK_ERROR_OUT_OF_DATE_KHR) {
|
---|
1730 | recreateSwapChain();
|
---|
1731 | return;
|
---|
1732 | } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
|
---|
1733 | throw runtime_error("failed to acquire swap chain image!");
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | updateUniformBuffer(imageIndex);
|
---|
1737 |
|
---|
1738 | VkSubmitInfo submitInfo = {};
|
---|
1739 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
---|
1740 |
|
---|
1741 | VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
|
---|
1742 | VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
|
---|
1743 |
|
---|
1744 | submitInfo.waitSemaphoreCount = 1;
|
---|
1745 | submitInfo.pWaitSemaphores = waitSemaphores;
|
---|
1746 | submitInfo.pWaitDstStageMask = waitStages;
|
---|
1747 | submitInfo.commandBufferCount = 1;
|
---|
1748 | submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
|
---|
1749 |
|
---|
1750 | VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
|
---|
1751 |
|
---|
1752 | submitInfo.signalSemaphoreCount = 1;
|
---|
1753 | submitInfo.pSignalSemaphores = signalSemaphores;
|
---|
1754 |
|
---|
1755 | vkResetFences(device, 1, &inFlightFences[currentFrame]);
|
---|
1756 |
|
---|
1757 | if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
|
---|
1758 | throw runtime_error("failed to submit draw command buffer!");
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | VkPresentInfoKHR presentInfo = {};
|
---|
1762 | presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
---|
1763 | presentInfo.waitSemaphoreCount = 1;
|
---|
1764 | presentInfo.pWaitSemaphores = signalSemaphores;
|
---|
1765 |
|
---|
1766 | VkSwapchainKHR swapChains[] = { swapChain };
|
---|
1767 | presentInfo.swapchainCount = 1;
|
---|
1768 | presentInfo.pSwapchains = swapChains;
|
---|
1769 | presentInfo.pImageIndices = &imageIndex;
|
---|
1770 | presentInfo.pResults = nullptr;
|
---|
1771 |
|
---|
1772 | result = vkQueuePresentKHR(presentQueue, &presentInfo);
|
---|
1773 |
|
---|
1774 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
|
---|
1775 | framebufferResized = false;
|
---|
1776 | recreateSwapChain();
|
---|
1777 | } else if (result != VK_SUCCESS) {
|
---|
1778 | throw runtime_error("failed to present swap chain image!");
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
---|
1782 | currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
---|
1783 | /*** START OF REFACTORED CODE ***/
|
---|
1784 | }
|
---|
1785 |
|
---|
1786 | void drawUI() {
|
---|
1787 | /*** END OF REFACTORED CODE ***/
|
---|
1788 | // TODO: Since I currently don't use any other render targets,
|
---|
1789 | // I may as well set this once before the render loop
|
---|
1790 | SDL_SetRenderTarget(gRenderer, uiOverlay);
|
---|
1791 |
|
---|
1792 | SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
|
---|
1793 | SDL_RenderClear(gRenderer);
|
---|
1794 |
|
---|
1795 | SDL_Rect rect;
|
---|
1796 |
|
---|
1797 | rect = {280, 220, 100, 100};
|
---|
1798 | SDL_SetRenderDrawColor(gRenderer, 0x00, 0xFF, 0x00, 0xFF);
|
---|
1799 | SDL_RenderFillRect(gRenderer, &rect);
|
---|
1800 | SDL_SetRenderDrawColor(gRenderer, 0x00, 0x9F, 0x9F, 0xFF);
|
---|
1801 |
|
---|
1802 | rect = {10, 10, 0, 0};
|
---|
1803 | SDL_QueryTexture(uiText, nullptr, nullptr, &(rect.w), &(rect.h));
|
---|
1804 | SDL_RenderCopy(gRenderer, uiText, nullptr, &rect);
|
---|
1805 |
|
---|
1806 | rect = {10, 80, 0, 0};
|
---|
1807 | SDL_QueryTexture(uiImage, nullptr, nullptr, &(rect.w), &(rect.h));
|
---|
1808 | SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect);
|
---|
1809 |
|
---|
1810 | SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
|
---|
1811 | SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
|
---|
1812 |
|
---|
1813 | populateImageFromSDLTexture(uiOverlay, sdlOverlayImage);
|
---|
1814 | /*** START OF REFACTORED CODE ***/
|
---|
1815 | }
|
---|
1816 | /*** END OF REFACTORED CODE ***/
|
---|
1817 |
|
---|
1818 | void updateUniformBuffer(uint32_t currentImage) {
|
---|
1819 | static auto startTime = chrono::high_resolution_clock::now();
|
---|
1820 |
|
---|
1821 | auto currentTime = chrono::high_resolution_clock::now();
|
---|
1822 | float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
|
---|
1823 |
|
---|
1824 | UniformBufferObject ubo = {};
|
---|
1825 | ubo.model = rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
---|
1826 | ubo.view = lookAt(glm::vec3(0.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
---|
1827 | ubo.proj = perspective(radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
|
---|
1828 | ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
|
---|
1829 |
|
---|
1830 | void* data;
|
---|
1831 | vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
|
---|
1832 | memcpy(data, &ubo, sizeof(ubo));
|
---|
1833 | vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 | void recreateSwapChain() {
|
---|
1837 | gui->refreshWindowSize();
|
---|
1838 |
|
---|
1839 | while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
|
---|
1840 | (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
|
---|
1841 | SDL_WaitEvent(nullptr);
|
---|
1842 | gui->refreshWindowSize();
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | vkDeviceWaitIdle(device);
|
---|
1846 |
|
---|
1847 | cleanupSwapChain();
|
---|
1848 |
|
---|
1849 | createSwapChain();
|
---|
1850 | createImageViews();
|
---|
1851 | createRenderPass();
|
---|
1852 |
|
---|
1853 | createBufferResources();
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | void createBufferResources() {
|
---|
1857 | createDepthResources();
|
---|
1858 | createFramebuffers();
|
---|
1859 | createUniformBuffers();
|
---|
1860 |
|
---|
1861 | createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline);
|
---|
1862 | createDescriptorPool(scenePipeline);
|
---|
1863 | createDescriptorSets(scenePipeline);
|
---|
1864 |
|
---|
1865 | createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline);
|
---|
1866 | createDescriptorPool(overlayPipeline);
|
---|
1867 | createDescriptorSets(overlayPipeline);
|
---|
1868 |
|
---|
1869 | createCommandBuffers();
|
---|
1870 | }
|
---|
1871 |
|
---|
1872 | /*** START OF REFACTORED CODE ***/
|
---|
1873 | void cleanup() {
|
---|
1874 | cleanupSwapChain();
|
---|
1875 | /*** END OF REFACTORED CODE ***/
|
---|
1876 |
|
---|
1877 | vkDestroySampler(device, textureSampler, nullptr);
|
---|
1878 |
|
---|
1879 | vkDestroyImageView(device, textureImageView, nullptr);
|
---|
1880 | vkDestroyImage(device, textureImage, nullptr);
|
---|
1881 | vkFreeMemory(device, textureImageMemory, nullptr);
|
---|
1882 |
|
---|
1883 | vkDestroyImageView(device, overlayImageView, nullptr);
|
---|
1884 | vkDestroyImage(device, overlayImage, nullptr);
|
---|
1885 | vkFreeMemory(device, overlayImageMemory, nullptr);
|
---|
1886 |
|
---|
1887 | vkDestroyImageView(device, sdlOverlayImageView, nullptr);
|
---|
1888 | vkDestroyImage(device, sdlOverlayImage, nullptr);
|
---|
1889 | vkFreeMemory(device, sdlOverlayImageMemory, nullptr);
|
---|
1890 |
|
---|
1891 | cleanupPipelineBuffers(scenePipeline);
|
---|
1892 | cleanupPipelineBuffers(overlayPipeline);
|
---|
1893 |
|
---|
1894 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
1895 | vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
---|
1896 | vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
---|
1897 | vkDestroyFence(device, inFlightFences[i], nullptr);
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | /*** START OF REFACTORED CODE ***/
|
---|
1901 | vkDestroyCommandPool(device, commandPool, nullptr);
|
---|
1902 | vkDestroyDevice(device, nullptr);
|
---|
1903 | vkDestroySurfaceKHR(instance, surface, nullptr);
|
---|
1904 |
|
---|
1905 | if (enableValidationLayers) {
|
---|
1906 | DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | vkDestroyInstance(instance, nullptr);
|
---|
1910 | /*** END OF REFACTORED CODE ***/
|
---|
1911 |
|
---|
1912 | // TODO: Check if any of these functions accept null parameters
|
---|
1913 | // If they do, I don't need to check for that
|
---|
1914 |
|
---|
1915 | if (uiOverlay != nullptr) {
|
---|
1916 | SDL_DestroyTexture(uiOverlay);
|
---|
1917 | uiOverlay = nullptr;
|
---|
1918 | }
|
---|
1919 |
|
---|
1920 | TTF_CloseFont(gFont);
|
---|
1921 | gFont = nullptr;
|
---|
1922 |
|
---|
1923 | if (uiText != nullptr) {
|
---|
1924 | SDL_DestroyTexture(uiText);
|
---|
1925 | uiText = nullptr;
|
---|
1926 | }
|
---|
1927 |
|
---|
1928 | if (uiImage != nullptr) {
|
---|
1929 | SDL_DestroyTexture(uiImage);
|
---|
1930 | uiImage = nullptr;
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | /*** START OF REFACTORED CODE ***/
|
---|
1934 | SDL_DestroyRenderer(gRenderer);
|
---|
1935 | gRenderer = nullptr;
|
---|
1936 |
|
---|
1937 | gui->destroyWindow();
|
---|
1938 | gui->shutdown();
|
---|
1939 | delete gui;
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | void cleanupSwapChain() {
|
---|
1943 | /*** END OF REFACTORED CODE ***/
|
---|
1944 | vkDestroyImageView(device, depthImageView, nullptr);
|
---|
1945 | vkDestroyImage(device, depthImage, nullptr);
|
---|
1946 | vkFreeMemory(device, depthImageMemory, nullptr);
|
---|
1947 |
|
---|
1948 | for (auto framebuffer : swapChainFramebuffers) {
|
---|
1949 | vkDestroyFramebuffer(device, framebuffer, nullptr);
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
1953 |
|
---|
1954 | cleanupPipeline(scenePipeline);
|
---|
1955 | cleanupPipeline(overlayPipeline);
|
---|
1956 |
|
---|
1957 | /*** START OF REFACTORED CODE ***/
|
---|
1958 | vkDestroyRenderPass(device, renderPass, nullptr);
|
---|
1959 |
|
---|
1960 | for (auto imageView : swapChainImageViews) {
|
---|
1961 | vkDestroyImageView(device, imageView, nullptr);
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | vkDestroySwapchainKHR(device, swapChain, nullptr);
|
---|
1965 | /*** END OF REFACTORED CODE ***/
|
---|
1966 |
|
---|
1967 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
1968 | vkDestroyBuffer(device, uniformBuffers[i], nullptr);
|
---|
1969 | vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
|
---|
1970 | }
|
---|
1971 | /*** START OF REFACTORED CODE ***/
|
---|
1972 | }
|
---|
1973 | /*** END OF REFACTORED CODE ***/
|
---|
1974 |
|
---|
1975 | void cleanupPipeline(GraphicsPipelineInfo& pipeline) {
|
---|
1976 | vkDestroyPipeline(device, pipeline.pipeline, nullptr);
|
---|
1977 | vkDestroyDescriptorPool(device, pipeline.descriptorPool, nullptr);
|
---|
1978 | vkDestroyPipelineLayout(device, pipeline.pipelineLayout, nullptr);
|
---|
1979 | }
|
---|
1980 |
|
---|
1981 | void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) {
|
---|
1982 | vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr);
|
---|
1983 |
|
---|
1984 | vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr);
|
---|
1985 | vkFreeMemory(device, pipeline.vertexBufferMemory, nullptr);
|
---|
1986 | vkDestroyBuffer(device, pipeline.indexBuffer, nullptr);
|
---|
1987 | vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
1991 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
1992 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
1993 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
1994 | void* pUserData) {
|
---|
1995 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
---|
1996 |
|
---|
1997 | return VK_FALSE;
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | static vector<char> readFile(const string& filename) {
|
---|
2001 | ifstream file(filename, ios::ate | ios::binary);
|
---|
2002 |
|
---|
2003 | if (!file.is_open()) {
|
---|
2004 | throw runtime_error("failed to open file!");
|
---|
2005 | }
|
---|
2006 |
|
---|
2007 | size_t fileSize = (size_t) file.tellg();
|
---|
2008 | vector<char> buffer(fileSize);
|
---|
2009 |
|
---|
2010 | file.seekg(0);
|
---|
2011 | file.read(buffer.data(), fileSize);
|
---|
2012 |
|
---|
2013 | file.close();
|
---|
2014 |
|
---|
2015 | return buffer;
|
---|
2016 | }
|
---|
2017 | };
|
---|
2018 |
|
---|
2019 | /*** START OF REFACTORED CODE ***/
|
---|
2020 | int main(int argc, char* argv[]) {
|
---|
2021 |
|
---|
2022 | #ifdef NDEBUG
|
---|
2023 | cout << "DEBUGGING IS OFF" << endl;
|
---|
2024 | #else
|
---|
2025 | cout << "DEBUGGING IS ON" << endl;
|
---|
2026 | #endif
|
---|
2027 |
|
---|
2028 | cout << "Starting Vulkan game..." << endl;
|
---|
2029 |
|
---|
2030 | VulkanGame game;
|
---|
2031 |
|
---|
2032 | try {
|
---|
2033 | game.run();
|
---|
2034 | } catch (const exception& e) {
|
---|
2035 | cerr << e.what() << endl;
|
---|
2036 | return EXIT_FAILURE;
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | cout << "Finished running the game" << endl;
|
---|
2040 |
|
---|
2041 | return EXIT_SUCCESS;
|
---|
2042 | }
|
---|
2043 | /*** END OF REFACTORED CODE ***/
|
---|