1 | #include "vulkan-game.hpp"
|
---|
2 |
|
---|
3 | #include <array>
|
---|
4 | #include <iostream>
|
---|
5 | #include <set>
|
---|
6 |
|
---|
7 | #include "consts.hpp"
|
---|
8 | #include "logger.hpp"
|
---|
9 |
|
---|
10 | #include "utils.hpp"
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | struct UniformBufferObject {
|
---|
15 | alignas(16) mat4 model;
|
---|
16 | alignas(16) mat4 view;
|
---|
17 | alignas(16) mat4 proj;
|
---|
18 | };
|
---|
19 |
|
---|
20 | VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
|
---|
21 | gui = nullptr;
|
---|
22 | window = nullptr;
|
---|
23 | }
|
---|
24 |
|
---|
25 | VulkanGame::~VulkanGame() {
|
---|
26 | }
|
---|
27 |
|
---|
28 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
29 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
30 |
|
---|
31 | cout << "Vulkan Game" << endl;
|
---|
32 |
|
---|
33 | // This gets the runtime version, use SDL_VERSION() for the comppile-time version
|
---|
34 | // TODO: Create a game-gui function to get the gui version and retrieve it that way
|
---|
35 | SDL_GetVersion(&sdlVersion);
|
---|
36 |
|
---|
37 | // TODO: Refactor the logger api to be more flexible,
|
---|
38 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
---|
39 | restart_gl_log();
|
---|
40 | gl_log("starting SDL\n%s.%s.%s",
|
---|
41 | to_string(sdlVersion.major).c_str(),
|
---|
42 | to_string(sdlVersion.minor).c_str(),
|
---|
43 | to_string(sdlVersion.patch).c_str());
|
---|
44 |
|
---|
45 | open_log();
|
---|
46 | get_log() << "starting SDL" << endl;
|
---|
47 | get_log() <<
|
---|
48 | (int)sdlVersion.major << "." <<
|
---|
49 | (int)sdlVersion.minor << "." <<
|
---|
50 | (int)sdlVersion.patch << endl;
|
---|
51 |
|
---|
52 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | initVulkan();
|
---|
57 | mainLoop();
|
---|
58 | cleanup();
|
---|
59 |
|
---|
60 | close_log();
|
---|
61 | }
|
---|
62 |
|
---|
63 | // TODO: Make some more initi functions, or call this initUI if the
|
---|
64 | // amount of things initialized here keeps growing
|
---|
65 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
66 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
67 | gui = new GameGui_SDL();
|
---|
68 |
|
---|
69 | if (gui->init() == RTWO_ERROR) {
|
---|
70 | // TODO: Also print these sorts of errors to the log
|
---|
71 | cout << "UI library could not be initialized!" << endl;
|
---|
72 | cout << gui->getError() << endl;
|
---|
73 | return RTWO_ERROR;
|
---|
74 | }
|
---|
75 |
|
---|
76 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
77 | if (window == nullptr) {
|
---|
78 | cout << "Window could not be created!" << endl;
|
---|
79 | cout << gui->getError() << endl;
|
---|
80 | return RTWO_ERROR;
|
---|
81 | }
|
---|
82 |
|
---|
83 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
84 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
85 |
|
---|
86 | renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
---|
87 | if (renderer == nullptr) {
|
---|
88 | cout << "Renderer could not be created!" << endl;
|
---|
89 | cout << gui->getError() << endl;
|
---|
90 | return RTWO_ERROR;
|
---|
91 | }
|
---|
92 |
|
---|
93 | SDL_VERSION(&sdlVersion);
|
---|
94 |
|
---|
95 | // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
|
---|
96 | // However, the latest SDL version available through homebrew on Mac is 2.0.9, which requires SDL_TEXTUREACCESS_STREAMING
|
---|
97 | // 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
|
---|
98 | // until the homebrew recipe is updated
|
---|
99 | if (sdlVersion.major == 2 && sdlVersion.minor == 0 && sdlVersion.patch == 9) {
|
---|
100 | uiOverlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
|
---|
101 | gui->getWindowWidth(), gui->getWindowHeight());
|
---|
102 | } else {
|
---|
103 | uiOverlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
---|
104 | gui->getWindowWidth(), gui->getWindowHeight());
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (uiOverlay == nullptr) {
|
---|
108 | cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
|
---|
109 | }
|
---|
110 | if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
|
---|
111 | cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
|
---|
112 | }
|
---|
113 |
|
---|
114 | return RTWO_SUCCESS;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void VulkanGame::initVulkan() {
|
---|
118 | const vector<const char*> validationLayers = {
|
---|
119 | "VK_LAYER_KHRONOS_validation"
|
---|
120 | };
|
---|
121 | const vector<const char*> deviceExtensions = {
|
---|
122 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
---|
123 | };
|
---|
124 |
|
---|
125 | createVulkanInstance(validationLayers);
|
---|
126 | setupDebugMessenger();
|
---|
127 | createVulkanSurface();
|
---|
128 | pickPhysicalDevice(deviceExtensions);
|
---|
129 | createLogicalDevice(validationLayers, deviceExtensions);
|
---|
130 | createSwapChain();
|
---|
131 | createImageViews();
|
---|
132 | createRenderPass();
|
---|
133 | createCommandPool();
|
---|
134 |
|
---|
135 | createImageResources();
|
---|
136 |
|
---|
137 | createFramebuffers();
|
---|
138 | createUniformBuffers();
|
---|
139 |
|
---|
140 | graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass,
|
---|
141 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(Vertex)));
|
---|
142 |
|
---|
143 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
|
---|
144 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
|
---|
145 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
|
---|
146 |
|
---|
147 | graphicsPipelines.back().addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
---|
148 | VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList);
|
---|
149 | graphicsPipelines.back().addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
150 | VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
|
---|
151 |
|
---|
152 | graphicsPipelines.back().createDescriptorSetLayout();
|
---|
153 | graphicsPipelines.back().createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
|
---|
154 | graphicsPipelines.back().createDescriptorPool(swapChainImages);
|
---|
155 | graphicsPipelines.back().createDescriptorSets(swapChainImages);
|
---|
156 |
|
---|
157 | graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass,
|
---|
158 | { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(OverlayVertex)));
|
---|
159 |
|
---|
160 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
|
---|
161 | graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
|
---|
162 |
|
---|
163 | graphicsPipelines.back().addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
164 | VK_SHADER_STAGE_FRAGMENT_BIT, &sdlOverlayImageDescriptor);
|
---|
165 |
|
---|
166 | graphicsPipelines.back().createDescriptorSetLayout();
|
---|
167 | graphicsPipelines.back().createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
|
---|
168 | graphicsPipelines.back().createDescriptorPool(swapChainImages);
|
---|
169 | graphicsPipelines.back().createDescriptorSets(swapChainImages);
|
---|
170 |
|
---|
171 | // TODO: Creating the descriptor pool and descriptor sets might need to be redone when the
|
---|
172 | // swap chain is recreated
|
---|
173 |
|
---|
174 | cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl;
|
---|
175 |
|
---|
176 | createCommandBuffers();
|
---|
177 |
|
---|
178 | createSyncObjects();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void VulkanGame::mainLoop() {
|
---|
182 | UIEvent e;
|
---|
183 | bool quit = false;
|
---|
184 |
|
---|
185 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
---|
186 |
|
---|
187 | while (!quit) {
|
---|
188 | gui->processEvents();
|
---|
189 |
|
---|
190 | while (gui->pollEvent(&e)) {
|
---|
191 | switch(e.type) {
|
---|
192 | case UI_EVENT_QUIT:
|
---|
193 | cout << "Quit event detected" << endl;
|
---|
194 | quit = true;
|
---|
195 | break;
|
---|
196 | case UI_EVENT_WINDOW:
|
---|
197 | cout << "Window event detected" << endl;
|
---|
198 | // Currently unused
|
---|
199 | break;
|
---|
200 | case UI_EVENT_WINDOWRESIZE:
|
---|
201 | cout << "Window resize event detected" << endl;
|
---|
202 | framebufferResized = true;
|
---|
203 | break;
|
---|
204 | case UI_EVENT_KEY:
|
---|
205 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
---|
206 | quit = true;
|
---|
207 | } else {
|
---|
208 | cout << "Key event detected" << endl;
|
---|
209 | }
|
---|
210 | break;
|
---|
211 | case UI_EVENT_MOUSEBUTTONDOWN:
|
---|
212 | cout << "Mouse button down event detected" << endl;
|
---|
213 | break;
|
---|
214 | case UI_EVENT_MOUSEBUTTONUP:
|
---|
215 | cout << "Mouse button up event detected" << endl;
|
---|
216 | break;
|
---|
217 | case UI_EVENT_MOUSEMOTION:
|
---|
218 | break;
|
---|
219 | case UI_EVENT_UNKNOWN:
|
---|
220 | cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl;
|
---|
221 | break;
|
---|
222 | default:
|
---|
223 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | renderUI();
|
---|
228 | renderScene();
|
---|
229 | }
|
---|
230 |
|
---|
231 | vkDeviceWaitIdle(device);
|
---|
232 | }
|
---|
233 |
|
---|
234 | void VulkanGame::renderUI() {
|
---|
235 | SDL_RenderClear(renderer);
|
---|
236 | SDL_RenderPresent(renderer);
|
---|
237 | }
|
---|
238 |
|
---|
239 | void VulkanGame::renderScene() {
|
---|
240 | }
|
---|
241 |
|
---|
242 | void VulkanGame::cleanup() {
|
---|
243 | cleanupSwapChain();
|
---|
244 |
|
---|
245 | VulkanUtils::destroyVulkanImage(device, floorTextureImage);
|
---|
246 | VulkanUtils::destroyVulkanImage(device, sdlOverlayImage);
|
---|
247 |
|
---|
248 | vkDestroySampler(device, textureSampler, nullptr);
|
---|
249 |
|
---|
250 | for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
|
---|
251 | pipeline.cleanupBuffers();
|
---|
252 | }
|
---|
253 |
|
---|
254 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
255 | vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
---|
256 | vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
---|
257 | vkDestroyFence(device, inFlightFences[i], nullptr);
|
---|
258 | }
|
---|
259 |
|
---|
260 | vkDestroyCommandPool(device, commandPool, nullptr);
|
---|
261 | vkDestroyDevice(device, nullptr);
|
---|
262 | vkDestroySurfaceKHR(instance, surface, nullptr);
|
---|
263 |
|
---|
264 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
265 | VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
---|
266 | }
|
---|
267 |
|
---|
268 | vkDestroyInstance(instance, nullptr);
|
---|
269 |
|
---|
270 | // TODO: Check if any of these functions accept null parameters
|
---|
271 | // If they do, I don't need to check for that
|
---|
272 |
|
---|
273 | if (uiOverlay != nullptr) {
|
---|
274 | SDL_DestroyTexture(uiOverlay);
|
---|
275 | uiOverlay = nullptr;
|
---|
276 | }
|
---|
277 |
|
---|
278 | SDL_DestroyRenderer(renderer);
|
---|
279 | renderer = nullptr;
|
---|
280 |
|
---|
281 | gui->destroyWindow();
|
---|
282 | gui->shutdown();
|
---|
283 | delete gui;
|
---|
284 | }
|
---|
285 |
|
---|
286 | void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
|
---|
287 | if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
|
---|
288 | throw runtime_error("validation layers requested, but not available!");
|
---|
289 | }
|
---|
290 |
|
---|
291 | VkApplicationInfo appInfo = {};
|
---|
292 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
---|
293 | appInfo.pApplicationName = "Vulkan Game";
|
---|
294 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
295 | appInfo.pEngineName = "No Engine";
|
---|
296 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
---|
297 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
---|
298 |
|
---|
299 | VkInstanceCreateInfo createInfo = {};
|
---|
300 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
---|
301 | createInfo.pApplicationInfo = &appInfo;
|
---|
302 |
|
---|
303 | vector<const char*> extensions = gui->getRequiredExtensions();
|
---|
304 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
305 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
---|
306 | }
|
---|
307 |
|
---|
308 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
---|
309 | createInfo.ppEnabledExtensionNames = extensions.data();
|
---|
310 |
|
---|
311 | cout << endl << "Extensions:" << endl;
|
---|
312 | for (const char* extensionName : extensions) {
|
---|
313 | cout << extensionName << endl;
|
---|
314 | }
|
---|
315 | cout << endl;
|
---|
316 |
|
---|
317 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
---|
318 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
319 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
320 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
321 |
|
---|
322 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
---|
323 | createInfo.pNext = &debugCreateInfo;
|
---|
324 | } else {
|
---|
325 | createInfo.enabledLayerCount = 0;
|
---|
326 |
|
---|
327 | createInfo.pNext = nullptr;
|
---|
328 | }
|
---|
329 |
|
---|
330 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
---|
331 | throw runtime_error("failed to create instance!");
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | void VulkanGame::setupDebugMessenger() {
|
---|
336 | if (!ENABLE_VALIDATION_LAYERS) return;
|
---|
337 |
|
---|
338 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
---|
339 | populateDebugMessengerCreateInfo(createInfo);
|
---|
340 |
|
---|
341 | if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
---|
342 | throw runtime_error("failed to set up debug messenger!");
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
---|
347 | createInfo = {};
|
---|
348 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
---|
349 | 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;
|
---|
350 | 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;
|
---|
351 | createInfo.pfnUserCallback = debugCallback;
|
---|
352 | }
|
---|
353 |
|
---|
354 | VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
|
---|
355 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
356 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
357 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
358 | void* pUserData) {
|
---|
359 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
---|
360 |
|
---|
361 | return VK_FALSE;
|
---|
362 | }
|
---|
363 |
|
---|
364 | void VulkanGame::createVulkanSurface() {
|
---|
365 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
---|
366 | throw runtime_error("failed to create window surface!");
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
|
---|
371 | uint32_t deviceCount = 0;
|
---|
372 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
---|
373 |
|
---|
374 | if (deviceCount == 0) {
|
---|
375 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
---|
376 | }
|
---|
377 |
|
---|
378 | vector<VkPhysicalDevice> devices(deviceCount);
|
---|
379 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
---|
380 |
|
---|
381 | cout << endl << "Graphics cards:" << endl;
|
---|
382 | for (const VkPhysicalDevice& device : devices) {
|
---|
383 | if (isDeviceSuitable(device, deviceExtensions)) {
|
---|
384 | physicalDevice = device;
|
---|
385 | break;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | cout << endl;
|
---|
389 |
|
---|
390 | if (physicalDevice == VK_NULL_HANDLE) {
|
---|
391 | throw runtime_error("failed to find a suitable GPU!");
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice physicalDevice,
|
---|
396 | const vector<const char*>& deviceExtensions) {
|
---|
397 | VkPhysicalDeviceProperties deviceProperties;
|
---|
398 | vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
|
---|
399 |
|
---|
400 | cout << "Device: " << deviceProperties.deviceName << endl;
|
---|
401 |
|
---|
402 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
403 | bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(physicalDevice, deviceExtensions);
|
---|
404 | bool swapChainAdequate = false;
|
---|
405 |
|
---|
406 | if (extensionsSupported) {
|
---|
407 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
---|
408 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
---|
409 | }
|
---|
410 |
|
---|
411 | VkPhysicalDeviceFeatures supportedFeatures;
|
---|
412 | vkGetPhysicalDeviceFeatures(physicalDevice, &supportedFeatures);
|
---|
413 |
|
---|
414 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
---|
415 | }
|
---|
416 |
|
---|
417 | void VulkanGame::createLogicalDevice(
|
---|
418 | const vector<const char*> validationLayers,
|
---|
419 | const vector<const char*>& deviceExtensions) {
|
---|
420 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
421 |
|
---|
422 | vector<VkDeviceQueueCreateInfo> queueCreateInfoList;
|
---|
423 | set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
---|
424 |
|
---|
425 | float queuePriority = 1.0f;
|
---|
426 | for (uint32_t queueFamily : uniqueQueueFamilies) {
|
---|
427 | VkDeviceQueueCreateInfo queueCreateInfo = {};
|
---|
428 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
---|
429 | queueCreateInfo.queueFamilyIndex = queueFamily;
|
---|
430 | queueCreateInfo.queueCount = 1;
|
---|
431 | queueCreateInfo.pQueuePriorities = &queuePriority;
|
---|
432 |
|
---|
433 | queueCreateInfoList.push_back(queueCreateInfo);
|
---|
434 | }
|
---|
435 |
|
---|
436 | VkPhysicalDeviceFeatures deviceFeatures = {};
|
---|
437 | deviceFeatures.samplerAnisotropy = VK_TRUE;
|
---|
438 |
|
---|
439 | VkDeviceCreateInfo createInfo = {};
|
---|
440 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
---|
441 | createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfoList.size());
|
---|
442 | createInfo.pQueueCreateInfos = queueCreateInfoList.data();
|
---|
443 |
|
---|
444 | createInfo.pEnabledFeatures = &deviceFeatures;
|
---|
445 |
|
---|
446 | createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
|
---|
447 | createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
---|
448 |
|
---|
449 | // These fields are ignored by up-to-date Vulkan implementations,
|
---|
450 | // but it's a good idea to set them for backwards compatibility
|
---|
451 | if (ENABLE_VALIDATION_LAYERS) {
|
---|
452 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
---|
453 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
---|
454 | } else {
|
---|
455 | createInfo.enabledLayerCount = 0;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
|
---|
459 | throw runtime_error("failed to create logical device!");
|
---|
460 | }
|
---|
461 |
|
---|
462 | vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
|
---|
463 | vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
---|
464 | }
|
---|
465 |
|
---|
466 | void VulkanGame::createSwapChain() {
|
---|
467 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
---|
468 |
|
---|
469 | VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats);
|
---|
470 | VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes);
|
---|
471 | VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
|
---|
472 |
|
---|
473 | uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
---|
474 | if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
|
---|
475 | imageCount = swapChainSupport.capabilities.maxImageCount;
|
---|
476 | }
|
---|
477 |
|
---|
478 | VkSwapchainCreateInfoKHR createInfo = {};
|
---|
479 | createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
---|
480 | createInfo.surface = surface;
|
---|
481 | createInfo.minImageCount = imageCount;
|
---|
482 | createInfo.imageFormat = surfaceFormat.format;
|
---|
483 | createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
---|
484 | createInfo.imageExtent = extent;
|
---|
485 | createInfo.imageArrayLayers = 1;
|
---|
486 | createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
---|
487 |
|
---|
488 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
---|
489 | uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
---|
490 |
|
---|
491 | if (indices.graphicsFamily != indices.presentFamily) {
|
---|
492 | createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
---|
493 | createInfo.queueFamilyIndexCount = 2;
|
---|
494 | createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
---|
495 | } else {
|
---|
496 | createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
497 | createInfo.queueFamilyIndexCount = 0;
|
---|
498 | createInfo.pQueueFamilyIndices = nullptr;
|
---|
499 | }
|
---|
500 |
|
---|
501 | createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
---|
502 | createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
---|
503 | createInfo.presentMode = presentMode;
|
---|
504 | createInfo.clipped = VK_TRUE;
|
---|
505 | createInfo.oldSwapchain = VK_NULL_HANDLE;
|
---|
506 |
|
---|
507 | if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
|
---|
508 | throw runtime_error("failed to create swap chain!");
|
---|
509 | }
|
---|
510 |
|
---|
511 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
---|
512 | swapChainImages.resize(imageCount);
|
---|
513 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
---|
514 |
|
---|
515 | swapChainImageFormat = surfaceFormat.format;
|
---|
516 | swapChainExtent = extent;
|
---|
517 | }
|
---|
518 |
|
---|
519 | void VulkanGame::createImageViews() {
|
---|
520 | swapChainImageViews.resize(swapChainImages.size());
|
---|
521 |
|
---|
522 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
523 | swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainImageFormat,
|
---|
524 | VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | void VulkanGame::createRenderPass() {
|
---|
529 | VkAttachmentDescription colorAttachment = {};
|
---|
530 | colorAttachment.format = swapChainImageFormat;
|
---|
531 | colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
532 | colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
533 | colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
---|
534 | colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
535 | colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
536 | colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
537 | colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
---|
538 |
|
---|
539 | VkAttachmentReference colorAttachmentRef = {};
|
---|
540 | colorAttachmentRef.attachment = 0;
|
---|
541 | colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
---|
542 |
|
---|
543 | VkAttachmentDescription depthAttachment = {};
|
---|
544 | depthAttachment.format = findDepthFormat();
|
---|
545 | depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
546 | depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
---|
547 | depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
548 | depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
---|
549 | depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
---|
550 | depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
551 | depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
552 |
|
---|
553 | VkAttachmentReference depthAttachmentRef = {};
|
---|
554 | depthAttachmentRef.attachment = 1;
|
---|
555 | depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
---|
556 |
|
---|
557 | VkSubpassDescription subpass = {};
|
---|
558 | subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
---|
559 | subpass.colorAttachmentCount = 1;
|
---|
560 | subpass.pColorAttachments = &colorAttachmentRef;
|
---|
561 | subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
---|
562 |
|
---|
563 | VkSubpassDependency dependency = {};
|
---|
564 | dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
---|
565 | dependency.dstSubpass = 0;
|
---|
566 | dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
567 | dependency.srcAccessMask = 0;
|
---|
568 | dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
---|
569 | dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
---|
570 |
|
---|
571 | array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
|
---|
572 | VkRenderPassCreateInfo renderPassInfo = {};
|
---|
573 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
---|
574 | renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
575 | renderPassInfo.pAttachments = attachments.data();
|
---|
576 | renderPassInfo.subpassCount = 1;
|
---|
577 | renderPassInfo.pSubpasses = &subpass;
|
---|
578 | renderPassInfo.dependencyCount = 1;
|
---|
579 | renderPassInfo.pDependencies = &dependency;
|
---|
580 |
|
---|
581 | if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
|
---|
582 | throw runtime_error("failed to create render pass!");
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | VkFormat VulkanGame::findDepthFormat() {
|
---|
587 | return VulkanUtils::findSupportedFormat(
|
---|
588 | physicalDevice,
|
---|
589 | { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
|
---|
590 | VK_IMAGE_TILING_OPTIMAL,
|
---|
591 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
|
---|
592 | );
|
---|
593 | }
|
---|
594 |
|
---|
595 | void VulkanGame::createCommandPool() {
|
---|
596 | QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface);;
|
---|
597 |
|
---|
598 | VkCommandPoolCreateInfo poolInfo = {};
|
---|
599 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
---|
600 | poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
|
---|
601 | poolInfo.flags = 0;
|
---|
602 |
|
---|
603 | if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
|
---|
604 | throw runtime_error("failed to create graphics command pool!");
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | void VulkanGame::createImageResources() {
|
---|
609 | VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
|
---|
610 | depthImage, graphicsQueue);
|
---|
611 |
|
---|
612 | createTextureSampler();
|
---|
613 |
|
---|
614 | VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",
|
---|
615 | floorTextureImage, graphicsQueue);
|
---|
616 |
|
---|
617 | floorTextureImageDescriptor = {};
|
---|
618 | floorTextureImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
619 | floorTextureImageDescriptor.imageView = floorTextureImage.imageView;
|
---|
620 | floorTextureImageDescriptor.sampler = textureSampler;
|
---|
621 |
|
---|
622 | VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
|
---|
623 |
|
---|
624 | sdlOverlayImageDescriptor = {};
|
---|
625 | sdlOverlayImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
---|
626 | sdlOverlayImageDescriptor.imageView = sdlOverlayImage.imageView;
|
---|
627 | sdlOverlayImageDescriptor.sampler = textureSampler;
|
---|
628 | }
|
---|
629 |
|
---|
630 | void VulkanGame::createTextureSampler() {
|
---|
631 | VkSamplerCreateInfo samplerInfo = {};
|
---|
632 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
---|
633 | samplerInfo.magFilter = VK_FILTER_LINEAR;
|
---|
634 | samplerInfo.minFilter = VK_FILTER_LINEAR;
|
---|
635 |
|
---|
636 | samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
637 | samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
638 | samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
---|
639 |
|
---|
640 | samplerInfo.anisotropyEnable = VK_TRUE;
|
---|
641 | samplerInfo.maxAnisotropy = 16;
|
---|
642 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
---|
643 | samplerInfo.unnormalizedCoordinates = VK_FALSE;
|
---|
644 | samplerInfo.compareEnable = VK_FALSE;
|
---|
645 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
---|
646 | samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
---|
647 | samplerInfo.mipLodBias = 0.0f;
|
---|
648 | samplerInfo.minLod = 0.0f;
|
---|
649 | samplerInfo.maxLod = 0.0f;
|
---|
650 |
|
---|
651 | if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
|
---|
652 | throw runtime_error("failed to create texture sampler!");
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | void VulkanGame::createFramebuffers() {
|
---|
657 | swapChainFramebuffers.resize(swapChainImageViews.size());
|
---|
658 |
|
---|
659 | for (size_t i = 0; i < swapChainImageViews.size(); i++) {
|
---|
660 | array<VkImageView, 2> attachments = {
|
---|
661 | swapChainImageViews[i],
|
---|
662 | depthImage.imageView
|
---|
663 | };
|
---|
664 |
|
---|
665 | VkFramebufferCreateInfo framebufferInfo = {};
|
---|
666 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
---|
667 | framebufferInfo.renderPass = renderPass;
|
---|
668 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
---|
669 | framebufferInfo.pAttachments = attachments.data();
|
---|
670 | framebufferInfo.width = swapChainExtent.width;
|
---|
671 | framebufferInfo.height = swapChainExtent.height;
|
---|
672 | framebufferInfo.layers = 1;
|
---|
673 |
|
---|
674 | if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
|
---|
675 | throw runtime_error("failed to create framebuffer!");
|
---|
676 | }
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | void VulkanGame::createUniformBuffers() {
|
---|
681 | VkDeviceSize bufferSize = sizeof(UniformBufferObject);
|
---|
682 |
|
---|
683 | uniformBuffers.resize(swapChainImages.size());
|
---|
684 | uniformBuffersMemory.resize(swapChainImages.size());
|
---|
685 | uniformBufferInfoList.resize(swapChainImages.size());
|
---|
686 |
|
---|
687 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
688 | VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
---|
689 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
690 | uniformBuffers[i], uniformBuffersMemory[i]);
|
---|
691 |
|
---|
692 | uniformBufferInfoList[i].buffer = uniformBuffers[i];
|
---|
693 | uniformBufferInfoList[i].offset = 0;
|
---|
694 | uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
|
---|
695 | }
|
---|
696 | }
|
---|
697 |
|
---|
698 | void VulkanGame::createCommandBuffers() {
|
---|
699 | commandBuffers.resize(swapChainImages.size());
|
---|
700 |
|
---|
701 | VkCommandBufferAllocateInfo allocInfo = {};
|
---|
702 | allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
---|
703 | allocInfo.commandPool = commandPool;
|
---|
704 | allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
---|
705 | allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
|
---|
706 |
|
---|
707 | if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
|
---|
708 | throw runtime_error("failed to allocate command buffers!");
|
---|
709 | }
|
---|
710 |
|
---|
711 | for (size_t i = 0; i < commandBuffers.size(); i++) {
|
---|
712 | VkCommandBufferBeginInfo beginInfo = {};
|
---|
713 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
---|
714 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
---|
715 | beginInfo.pInheritanceInfo = nullptr;
|
---|
716 |
|
---|
717 | if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
|
---|
718 | throw runtime_error("failed to begin recording command buffer!");
|
---|
719 | }
|
---|
720 |
|
---|
721 | VkRenderPassBeginInfo renderPassInfo = {};
|
---|
722 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
---|
723 | renderPassInfo.renderPass = renderPass;
|
---|
724 | renderPassInfo.framebuffer = swapChainFramebuffers[i];
|
---|
725 | renderPassInfo.renderArea.offset = { 0, 0 };
|
---|
726 | renderPassInfo.renderArea.extent = swapChainExtent;
|
---|
727 |
|
---|
728 | array<VkClearValue, 2> clearValues = {};
|
---|
729 | clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
|
---|
730 | clearValues[1].depthStencil = { 1.0f, 0 };
|
---|
731 |
|
---|
732 | renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
|
---|
733 | renderPassInfo.pClearValues = clearValues.data();
|
---|
734 |
|
---|
735 | vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
---|
736 |
|
---|
737 | // reateGraphicsPipelineCommands(scenePipeline, i);
|
---|
738 | // createGraphicsPipelineCommands(overlayPipeline, i);
|
---|
739 | for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
|
---|
740 | pipeline.createRenderCommands(commandBuffers[i], i);
|
---|
741 | }
|
---|
742 |
|
---|
743 | vkCmdEndRenderPass(commandBuffers[i]);
|
---|
744 |
|
---|
745 | if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
|
---|
746 | throw runtime_error("failed to record command buffer!");
|
---|
747 | }
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 | void VulkanGame::createSyncObjects() {
|
---|
752 | imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
753 | renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
754 | inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
---|
755 |
|
---|
756 | VkSemaphoreCreateInfo semaphoreInfo = {};
|
---|
757 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
---|
758 |
|
---|
759 | VkFenceCreateInfo fenceInfo = {};
|
---|
760 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
---|
761 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
---|
762 |
|
---|
763 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
---|
764 | if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
|
---|
765 | vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
|
---|
766 | vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
|
---|
767 | throw runtime_error("failed to create synchronization objects for a frame!");
|
---|
768 | }
|
---|
769 | }
|
---|
770 | }
|
---|
771 |
|
---|
772 | void VulkanGame::cleanupSwapChain() {
|
---|
773 | VulkanUtils::destroyVulkanImage(device, depthImage);
|
---|
774 |
|
---|
775 | for (VkFramebuffer framebuffer : swapChainFramebuffers) {
|
---|
776 | vkDestroyFramebuffer(device, framebuffer, nullptr);
|
---|
777 | }
|
---|
778 |
|
---|
779 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
780 |
|
---|
781 | for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
|
---|
782 | pipeline.cleanup();
|
---|
783 | }
|
---|
784 |
|
---|
785 | vkDestroyRenderPass(device, renderPass, nullptr);
|
---|
786 |
|
---|
787 | for (VkImageView imageView : swapChainImageViews) {
|
---|
788 | vkDestroyImageView(device, imageView, nullptr);
|
---|
789 | }
|
---|
790 |
|
---|
791 | vkDestroySwapchainKHR(device, swapChain, nullptr);
|
---|
792 |
|
---|
793 | for (size_t i = 0; i < uniformBuffers.size(); i++) {
|
---|
794 | vkDestroyBuffer(device, uniformBuffers[i], nullptr);
|
---|
795 | vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
|
---|
796 | }
|
---|
797 | }
|
---|