| [99d44b2] | 1 | #include "vulkan-game.hpp"
|
|---|
| [850e84c] | 2 |
|
|---|
| [0df3c9a] | 3 | #include <iostream>
|
|---|
| [c1c2021] | 4 | #include <set>
|
|---|
| [0df3c9a] | 5 |
|
|---|
| [5edbd58] | 6 | #include "consts.hpp"
|
|---|
| [c559904] | 7 | #include "logger.hpp"
|
|---|
| [5edbd58] | 8 |
|
|---|
| [c1d9b2a] | 9 | #include "vulkan-utils.hpp"
|
|---|
| 10 |
|
|---|
| [0df3c9a] | 11 | using namespace std;
|
|---|
| 12 |
|
|---|
| [99d44b2] | 13 | VulkanGame::VulkanGame() {
|
|---|
| [0df3c9a] | 14 | gui = nullptr;
|
|---|
| 15 | window = nullptr;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| [99d44b2] | 18 | VulkanGame::~VulkanGame() {
|
|---|
| [0df3c9a] | 19 | }
|
|---|
| 20 |
|
|---|
| [b6e60b4] | 21 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
|---|
| [2e77b3f] | 22 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
|---|
| 23 |
|
|---|
| 24 | cout << "Vulkan Game" << endl;
|
|---|
| 25 |
|
|---|
| [c559904] | 26 | // This gets the runtime version, use SDL_VERSION() for the comppile-time version
|
|---|
| 27 | // TODO: Create a game-gui function to get the gui version and retrieve it that way
|
|---|
| 28 | SDL_GetVersion(&sdlVersion);
|
|---|
| 29 |
|
|---|
| 30 | // TODO: Refactor the logger api to be more flexible,
|
|---|
| 31 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
|---|
| 32 | restart_gl_log();
|
|---|
| 33 | gl_log("starting SDL\n%s.%s.%s",
|
|---|
| 34 | to_string(sdlVersion.major).c_str(),
|
|---|
| 35 | to_string(sdlVersion.minor).c_str(),
|
|---|
| 36 | to_string(sdlVersion.patch).c_str());
|
|---|
| 37 |
|
|---|
| 38 | open_log();
|
|---|
| 39 | get_log() << "starting SDL" << endl;
|
|---|
| 40 | get_log() <<
|
|---|
| 41 | (int)sdlVersion.major << "." <<
|
|---|
| 42 | (int)sdlVersion.minor << "." <<
|
|---|
| 43 | (int)sdlVersion.patch << endl;
|
|---|
| 44 |
|
|---|
| [5edbd58] | 45 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
|---|
| [0df3c9a] | 46 | return;
|
|---|
| 47 | }
|
|---|
| [b6e60b4] | 48 |
|
|---|
| [0df3c9a] | 49 | initVulkan();
|
|---|
| 50 | mainLoop();
|
|---|
| 51 | cleanup();
|
|---|
| [c559904] | 52 |
|
|---|
| 53 | close_log();
|
|---|
| [0df3c9a] | 54 | }
|
|---|
| 55 |
|
|---|
| [c559904] | 56 | // TODO: Make some more initi functions, or call this initUI if the
|
|---|
| 57 | // amount of things initialized here keeps growing
|
|---|
| [b6e60b4] | 58 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
|---|
| [c559904] | 59 | // TODO: Put all fonts, textures, and images in the assets folder
|
|---|
| [0df3c9a] | 60 | gui = new GameGui_SDL();
|
|---|
| 61 |
|
|---|
| [b6e60b4] | 62 | if (gui->init() == RTWO_ERROR) {
|
|---|
| [c559904] | 63 | // TODO: Also print these sorts of errors to the log
|
|---|
| [0df3c9a] | 64 | cout << "UI library could not be initialized!" << endl;
|
|---|
| [b6e60b4] | 65 | cout << gui->getError() << endl;
|
|---|
| [0df3c9a] | 66 | return RTWO_ERROR;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| [b6e60b4] | 69 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
|---|
| [0df3c9a] | 70 | if (window == nullptr) {
|
|---|
| 71 | cout << "Window could not be created!" << endl;
|
|---|
| [ed7c953] | 72 | cout << gui->getError() << endl;
|
|---|
| [0df3c9a] | 73 | return RTWO_ERROR;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| [b6e60b4] | 76 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
|---|
| [a6f6833] | 77 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
|---|
| [b6e60b4] | 78 |
|
|---|
| [c1d9b2a] | 79 | renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|---|
| 80 | if (renderer == nullptr) {
|
|---|
| 81 | cout << "Renderer could not be created!" << endl;
|
|---|
| 82 | cout << gui->getError() << endl;
|
|---|
| 83 | return RTWO_ERROR;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| [0df3c9a] | 86 | return RTWO_SUCCESS;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| [99d44b2] | 89 | void VulkanGame::initVulkan() {
|
|---|
| [c1d9b2a] | 90 | const vector<const char*> validationLayers = {
|
|---|
| 91 | "VK_LAYER_KHRONOS_validation"
|
|---|
| 92 | };
|
|---|
| [fe5c3ba] | 93 | const vector<const char*> deviceExtensions = {
|
|---|
| 94 | VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
|---|
| 95 | };
|
|---|
| [c1d9b2a] | 96 |
|
|---|
| 97 | createVulkanInstance(validationLayers);
|
|---|
| 98 | setupDebugMessenger();
|
|---|
| [90a424f] | 99 | createVulkanSurface();
|
|---|
| [fe5c3ba] | 100 | pickPhysicalDevice(deviceExtensions);
|
|---|
| [c1c2021] | 101 | createLogicalDevice(validationLayers, deviceExtensions);
|
|---|
| [502bd0b] | 102 | createSwapChain();
|
|---|
| [f94eea9] | 103 | createImageViews();
|
|---|
| [0df3c9a] | 104 | }
|
|---|
| 105 |
|
|---|
| [99d44b2] | 106 | void VulkanGame::mainLoop() {
|
|---|
| [f6521fb] | 107 | UIEvent e;
|
|---|
| [0df3c9a] | 108 | bool quit = false;
|
|---|
| 109 |
|
|---|
| [c1d9b2a] | 110 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|---|
| 111 |
|
|---|
| [0df3c9a] | 112 | while (!quit) {
|
|---|
| [27c40ce] | 113 | gui->processEvents();
|
|---|
| 114 |
|
|---|
| [f6521fb] | 115 | while (gui->pollEvent(&e)) {
|
|---|
| 116 | switch(e.type) {
|
|---|
| 117 | case UI_EVENT_QUIT:
|
|---|
| 118 | cout << "Quit event detected" << endl;
|
|---|
| 119 | quit = true;
|
|---|
| 120 | break;
|
|---|
| 121 | case UI_EVENT_WINDOW:
|
|---|
| 122 | cout << "Window event detected" << endl;
|
|---|
| 123 | // Currently unused
|
|---|
| 124 | break;
|
|---|
| 125 | case UI_EVENT_KEY:
|
|---|
| 126 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
|---|
| 127 | quit = true;
|
|---|
| 128 | } else {
|
|---|
| 129 | cout << "Key event detected" << endl;
|
|---|
| 130 | }
|
|---|
| 131 | break;
|
|---|
| 132 | case UI_EVENT_MOUSEBUTTONDOWN:
|
|---|
| 133 | cout << "Mouse button down event detected" << endl;
|
|---|
| 134 | break;
|
|---|
| 135 | case UI_EVENT_MOUSEBUTTONUP:
|
|---|
| 136 | cout << "Mouse button up event detected" << endl;
|
|---|
| 137 | break;
|
|---|
| 138 | case UI_EVENT_MOUSEMOTION:
|
|---|
| 139 | break;
|
|---|
| [c61323a] | 140 | default:
|
|---|
| 141 | cout << "Unhandled UI event: " << e.type << endl;
|
|---|
| [0df3c9a] | 142 | }
|
|---|
| 143 | }
|
|---|
| [c1d9b2a] | 144 |
|
|---|
| [a0c5f28] | 145 | renderUI();
|
|---|
| 146 | renderScene();
|
|---|
| [0df3c9a] | 147 | }
|
|---|
| [c1c2021] | 148 |
|
|---|
| 149 | vkDeviceWaitIdle(device);
|
|---|
| [0df3c9a] | 150 | }
|
|---|
| 151 |
|
|---|
| [a0c5f28] | 152 | void VulkanGame::renderUI() {
|
|---|
| 153 | SDL_RenderClear(renderer);
|
|---|
| 154 | SDL_RenderPresent(renderer);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void VulkanGame::renderScene() {
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| [99d44b2] | 160 | void VulkanGame::cleanup() {
|
|---|
| [c1c2021] | 161 | cleanupSwapChain();
|
|---|
| 162 |
|
|---|
| 163 | vkDestroyDevice(device, nullptr);
|
|---|
| 164 | vkDestroySurfaceKHR(instance, surface, nullptr);
|
|---|
| 165 |
|
|---|
| [c1d9b2a] | 166 | if (ENABLE_VALIDATION_LAYERS) {
|
|---|
| 167 | VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
|---|
| 168 | }
|
|---|
| [c1c2021] | 169 |
|
|---|
| [c1d9b2a] | 170 | vkDestroyInstance(instance, nullptr);
|
|---|
| 171 |
|
|---|
| 172 | SDL_DestroyRenderer(renderer);
|
|---|
| 173 | renderer = nullptr;
|
|---|
| 174 |
|
|---|
| [b6e60b4] | 175 | gui->destroyWindow();
|
|---|
| 176 | gui->shutdown();
|
|---|
| [0df3c9a] | 177 | delete gui;
|
|---|
| [c1d9b2a] | 178 | }
|
|---|
| 179 |
|
|---|
| 180 | void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
|
|---|
| 181 | if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
|
|---|
| 182 | throw runtime_error("validation layers requested, but not available!");
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | VkApplicationInfo appInfo = {};
|
|---|
| 186 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
|---|
| 187 | appInfo.pApplicationName = "Vulkan Game";
|
|---|
| 188 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
|---|
| 189 | appInfo.pEngineName = "No Engine";
|
|---|
| 190 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
|---|
| 191 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
|---|
| 192 |
|
|---|
| 193 | VkInstanceCreateInfo createInfo = {};
|
|---|
| 194 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|---|
| 195 | createInfo.pApplicationInfo = &appInfo;
|
|---|
| 196 |
|
|---|
| 197 | vector<const char*> extensions = gui->getRequiredExtensions();
|
|---|
| 198 | if (ENABLE_VALIDATION_LAYERS) {
|
|---|
| 199 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
|---|
| 203 | createInfo.ppEnabledExtensionNames = extensions.data();
|
|---|
| 204 |
|
|---|
| 205 | cout << endl << "Extensions:" << endl;
|
|---|
| 206 | for (const char* extensionName : extensions) {
|
|---|
| 207 | cout << extensionName << endl;
|
|---|
| 208 | }
|
|---|
| 209 | cout << endl;
|
|---|
| 210 |
|
|---|
| 211 | VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
|
|---|
| 212 | if (ENABLE_VALIDATION_LAYERS) {
|
|---|
| 213 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
|---|
| 214 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
|---|
| 215 |
|
|---|
| 216 | populateDebugMessengerCreateInfo(debugCreateInfo);
|
|---|
| 217 | createInfo.pNext = &debugCreateInfo;
|
|---|
| 218 | } else {
|
|---|
| 219 | createInfo.enabledLayerCount = 0;
|
|---|
| 220 |
|
|---|
| 221 | createInfo.pNext = nullptr;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
|---|
| 225 | throw runtime_error("failed to create instance!");
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void VulkanGame::setupDebugMessenger() {
|
|---|
| 230 | if (!ENABLE_VALIDATION_LAYERS) return;
|
|---|
| 231 |
|
|---|
| 232 | VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
|---|
| 233 | populateDebugMessengerCreateInfo(createInfo);
|
|---|
| 234 |
|
|---|
| 235 | if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
|
|---|
| 236 | throw runtime_error("failed to set up debug messenger!");
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
|
|---|
| 241 | createInfo = {};
|
|---|
| 242 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
|---|
| 243 | 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;
|
|---|
| 244 | 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;
|
|---|
| 245 | createInfo.pfnUserCallback = debugCallback;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
|
|---|
| 249 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|---|
| 250 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|---|
| 251 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|---|
| 252 | void* pUserData) {
|
|---|
| 253 | cerr << "validation layer: " << pCallbackData->pMessage << endl;
|
|---|
| 254 |
|
|---|
| 255 | return VK_FALSE;
|
|---|
| 256 | }
|
|---|
| [90a424f] | 257 |
|
|---|
| 258 | void VulkanGame::createVulkanSurface() {
|
|---|
| 259 | if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
|
|---|
| 260 | throw runtime_error("failed to create window surface!");
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| [fe5c3ba] | 264 | void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
|
|---|
| [90a424f] | 265 | uint32_t deviceCount = 0;
|
|---|
| 266 | vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
|---|
| 267 |
|
|---|
| 268 | if (deviceCount == 0) {
|
|---|
| 269 | throw runtime_error("failed to find GPUs with Vulkan support!");
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | vector<VkPhysicalDevice> devices(deviceCount);
|
|---|
| 273 | vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
|---|
| 274 |
|
|---|
| 275 | cout << endl << "Graphics cards:" << endl;
|
|---|
| 276 | for (const VkPhysicalDevice& device : devices) {
|
|---|
| [fe5c3ba] | 277 | if (isDeviceSuitable(device, deviceExtensions)) {
|
|---|
| [90a424f] | 278 | physicalDevice = device;
|
|---|
| 279 | break;
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 | cout << endl;
|
|---|
| 283 |
|
|---|
| 284 | if (physicalDevice == VK_NULL_HANDLE) {
|
|---|
| 285 | throw runtime_error("failed to find a suitable GPU!");
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| [fe5c3ba] | 289 | bool VulkanGame::isDeviceSuitable(VkPhysicalDevice device, const vector<const char*>& deviceExtensions) {
|
|---|
| [90a424f] | 290 | VkPhysicalDeviceProperties deviceProperties;
|
|---|
| 291 | vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
|---|
| 292 |
|
|---|
| 293 | cout << "Device: " << deviceProperties.deviceName << endl;
|
|---|
| 294 |
|
|---|
| 295 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(device, surface);
|
|---|
| 296 | bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(device, deviceExtensions);
|
|---|
| 297 | bool swapChainAdequate = false;
|
|---|
| 298 |
|
|---|
| 299 | if (extensionsSupported) {
|
|---|
| 300 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(device, surface);
|
|---|
| 301 | swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | VkPhysicalDeviceFeatures supportedFeatures;
|
|---|
| 305 | vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
|
|---|
| 306 |
|
|---|
| 307 | return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
|
|---|
| [c1c2021] | 308 | }
|
|---|
| 309 |
|
|---|
| 310 | void VulkanGame::createLogicalDevice(
|
|---|
| 311 | const vector<const char*> validationLayers,
|
|---|
| 312 | const vector<const char*>& deviceExtensions) {
|
|---|
| 313 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
|---|
| 314 |
|
|---|
| 315 | vector<VkDeviceQueueCreateInfo> queueCreateInfos;
|
|---|
| 316 | set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
|---|
| 317 |
|
|---|
| 318 | float queuePriority = 1.0f;
|
|---|
| 319 | for (uint32_t queueFamily : uniqueQueueFamilies) {
|
|---|
| 320 | VkDeviceQueueCreateInfo queueCreateInfo = {};
|
|---|
| 321 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|---|
| 322 | queueCreateInfo.queueFamilyIndex = queueFamily;
|
|---|
| 323 | queueCreateInfo.queueCount = 1;
|
|---|
| 324 | queueCreateInfo.pQueuePriorities = &queuePriority;
|
|---|
| 325 |
|
|---|
| 326 | queueCreateInfos.push_back(queueCreateInfo);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | VkPhysicalDeviceFeatures deviceFeatures = {};
|
|---|
| 330 | deviceFeatures.samplerAnisotropy = VK_TRUE;
|
|---|
| 331 |
|
|---|
| 332 | VkDeviceCreateInfo createInfo = {};
|
|---|
| 333 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|---|
| 334 | createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
|
|---|
| 335 | createInfo.pQueueCreateInfos = queueCreateInfos.data();
|
|---|
| 336 |
|
|---|
| 337 | createInfo.pEnabledFeatures = &deviceFeatures;
|
|---|
| 338 |
|
|---|
| 339 | createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
|
|---|
| 340 | createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
|---|
| 341 |
|
|---|
| 342 | // These fields are ignored by up-to-date Vulkan implementations,
|
|---|
| 343 | // but it's a good idea to set them for backwards compatibility
|
|---|
| 344 | if (ENABLE_VALIDATION_LAYERS) {
|
|---|
| 345 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
|---|
| 346 | createInfo.ppEnabledLayerNames = validationLayers.data();
|
|---|
| 347 | } else {
|
|---|
| 348 | createInfo.enabledLayerCount = 0;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
|
|---|
| 352 | throw runtime_error("failed to create logical device!");
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
|
|---|
| 356 | vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
|---|
| [502bd0b] | 357 | }
|
|---|
| 358 |
|
|---|
| 359 | void VulkanGame::createSwapChain() {
|
|---|
| 360 | SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
|
|---|
| 361 |
|
|---|
| 362 | VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats);
|
|---|
| 363 | VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes);
|
|---|
| 364 | VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
|
|---|
| 365 |
|
|---|
| 366 | uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
|---|
| 367 | if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
|
|---|
| 368 | imageCount = swapChainSupport.capabilities.maxImageCount;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | VkSwapchainCreateInfoKHR createInfo = {};
|
|---|
| 372 | createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|---|
| 373 | createInfo.surface = surface;
|
|---|
| 374 | createInfo.minImageCount = imageCount;
|
|---|
| 375 | createInfo.imageFormat = surfaceFormat.format;
|
|---|
| 376 | createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
|---|
| 377 | createInfo.imageExtent = extent;
|
|---|
| 378 | createInfo.imageArrayLayers = 1;
|
|---|
| 379 | createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|---|
| 380 |
|
|---|
| 381 | QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
|
|---|
| 382 | uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
|---|
| 383 |
|
|---|
| 384 | if (indices.graphicsFamily != indices.presentFamily) {
|
|---|
| 385 | createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
|---|
| 386 | createInfo.queueFamilyIndexCount = 2;
|
|---|
| 387 | createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
|---|
| [f94eea9] | 388 | } else {
|
|---|
| [502bd0b] | 389 | createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|---|
| 390 | createInfo.queueFamilyIndexCount = 0;
|
|---|
| 391 | createInfo.pQueueFamilyIndices = nullptr;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
|---|
| 395 | createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|---|
| 396 | createInfo.presentMode = presentMode;
|
|---|
| 397 | createInfo.clipped = VK_TRUE;
|
|---|
| 398 | createInfo.oldSwapchain = VK_NULL_HANDLE;
|
|---|
| 399 |
|
|---|
| 400 | if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
|
|---|
| 401 | throw runtime_error("failed to create swap chain!");
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
|---|
| 405 | swapChainImages.resize(imageCount);
|
|---|
| 406 | vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
|---|
| 407 |
|
|---|
| 408 | swapChainImageFormat = surfaceFormat.format;
|
|---|
| 409 | swapChainExtent = extent;
|
|---|
| [f94eea9] | 410 | }
|
|---|
| 411 |
|
|---|
| 412 | void VulkanGame::createImageViews() {
|
|---|
| 413 | swapChainImageViews.resize(swapChainImages.size());
|
|---|
| 414 |
|
|---|
| 415 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
|---|
| 416 | swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainImageFormat,
|
|---|
| 417 | VK_IMAGE_ASPECT_COLOR_BIT);
|
|---|
| 418 | }
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | void VulkanGame::cleanupSwapChain() {
|
|---|
| 422 | for (auto imageView : swapChainImageViews) {
|
|---|
| 423 | vkDestroyImageView(device, imageView, nullptr);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | vkDestroySwapchainKHR(device, swapChain, nullptr);
|
|---|
| [90a424f] | 427 | }
|
|---|