Changeset 621664a in opengl-game


Ignore:
Timestamp:
Jul 29, 2019, 4:01:12 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
fba08f2
Parents:
f5d5686
Message:

Reformat vulkan-game.cpp and fix a seg fault

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rf5d5686 r621664a  
    9797
    9898struct UniformBufferObject {
    99    glm::mat4 model;
    100    glm::mat4 view;
    101    glm::mat4 proj;
     99   alignas(16) glm::mat4 model;
     100   alignas(16) glm::mat4 view;
     101   alignas(16) glm::mat4 proj;
    102102};
    103103
     
    110110
    111111const vector<uint16_t> indices = {
    112    0, 1, 2, 2, 3, 0 
     112   0, 1, 2, 2, 3, 0
    113113};
    114114
     
    117117      const VkAllocationCallbacks* pAllocator,
    118118      VkDebugUtilsMessengerEXT* pDebugMessenger) {
    119    auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(
    120       instance, "vkCreateDebugUtilsMessengerEXT");
     119   auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
    121120
    122121   if (func != nullptr) {
     
    130129      VkDebugUtilsMessengerEXT debugMessenger,
    131130      const VkAllocationCallbacks* pAllocator) {
    132    auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(
    133       instance, "vkDestroyDebugUtilsMessengerEXT");
     131   auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
    134132
    135133   if (func != nullptr) {
     
    148146         cleanup();
    149147      }
     148
    150149   private:
    151150      GameGui* gui = new GameGui_SDL();
     
    167166      VkFormat swapChainImageFormat;
    168167      VkExtent2D swapChainExtent;
    169 
    170168      vector<VkImageView> swapChainImageViews;
     169      vector<VkFramebuffer> swapChainFramebuffers;
     170
    171171      VkRenderPass renderPass;
    172172      VkDescriptorSetLayout descriptorSetLayout;
     
    190190      vector<VkDeviceMemory> uniformBuffersMemory;
    191191
    192       vector<VkFramebuffer> swapChainFramebuffers;
    193192      vector<VkCommandBuffer> commandBuffers;
    194193
     
    240239      }
    241240
    242       void recreateSwapChain() {
    243          int width = 0, height = 0;
    244          gui->GetWindowSize(&width, &height);
    245 
    246          while (width == 0 || height == 0 ||
    247             (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
    248             SDL_WaitEvent(nullptr);
    249             gui->GetWindowSize(&width, &height);
    250          }
    251 
    252          vkDeviceWaitIdle(device);
    253 
    254          cleanupSwapChain();
    255 
    256          createSwapChain();
    257          createImageViews();
    258          createRenderPass();
    259          createGraphicsPipeline();
    260          createFramebuffers();
    261          createUniformBuffers();
    262          createDescriptorPool();
    263          createDescriptorSets();
    264          createCommandBuffers();
    265       }
    266 
    267       void cleanupSwapChain() {
    268          for (auto framebuffer : swapChainFramebuffers) {
    269             vkDestroyFramebuffer(device, framebuffer, nullptr);
    270          }
    271 
    272          vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    273 
    274          vkDestroyPipeline(device, graphicsPipeline, nullptr);
    275          vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
    276          vkDestroyRenderPass(device, renderPass, nullptr);
    277 
    278          for (auto imageView : swapChainImageViews) {
    279             vkDestroyImageView(device, imageView, nullptr);
    280          }
    281 
    282          vkDestroySwapchainKHR(device, swapChain, nullptr);
    283 
    284          for (size_t i = 0; i < swapChainImages.size(); i++) {
    285             vkDestroyBuffer(device, uniformBuffers[i], nullptr);
    286             vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
    287          }
    288 
    289          vkDestroyDescriptorPool(device, descriptorPool, nullptr);
    290       }
    291 
    292241      void createInstance() {
    293242         if (enableValidationLayers && !checkValidationLayerSupport()) {
     
    335284      }
    336285
     286      bool checkValidationLayerSupport() {
     287         uint32_t layerCount;
     288         vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
     289
     290         vector<VkLayerProperties> availableLayers(layerCount);
     291         vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
     292
     293         for (const char* layerName : validationLayers) {
     294            bool layerFound = false;
     295
     296            for (const auto& layerProperties : availableLayers) {
     297               if (strcmp(layerName, layerProperties.layerName) == 0) {
     298                  layerFound = true;
     299                  break;
     300               }
     301            }
     302
     303            if (!layerFound) {
     304               return false;
     305            }
     306         }
     307
     308         return true;
     309      }
     310
     311      vector<const char*> getRequiredExtensions() {
     312         vector<const char*> extensions = gui->GetRequiredExtensions();
     313
     314         if (enableValidationLayers) {
     315            extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
     316         }
     317
     318         return extensions;
     319      }
     320
    337321      void setupDebugMessenger() {
    338322         if (!enableValidationLayers) return;
     
    342326
    343327         if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
    344             throw runtime_error("failed to setup debug messenger!");
    345          }
     328            throw runtime_error("failed to set up debug messenger!");
     329         }
     330      }
     331
     332      void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
     333         createInfo = {};
     334         createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
     335         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;
     336         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;
     337         createInfo.pfnUserCallback = debugCallback;
    346338      }
    347339
     
    350342
    351343         if (sdlSurface == nullptr) {
    352              cout << "Could not get SDL Surface! =(" << endl;
     344            cout << "Could not get SDL Surface! =(" << endl;
    353345         }
    354346
     
    393385
    394386         QueueFamilyIndices indices = findQueueFamilies(device);
    395 
    396387         bool extensionsSupported = checkDeviceExtensionSupport(device);
    397 
    398388         bool swapChainAdequate = false;
    399389
     
    431421         for (uint32_t queueFamily : uniqueQueueFamilies) {
    432422            VkDeviceQueueCreateInfo queueCreateInfo = {};
    433 
    434423            queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    435424            queueCreateInfo.queueFamilyIndex = queueFamily;
     
    444433         VkDeviceCreateInfo createInfo = {};
    445434         createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    446 
    447          createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());;
     435         createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
    448436         createInfo.pQueueCreateInfos = queueCreateInfos.data();
    449437
     
    470458      }
    471459
    472       bool checkValidationLayerSupport() {
    473          uint32_t layerCount;
    474          vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
    475 
    476          vector<VkLayerProperties> availableLayers(layerCount);
    477          vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
    478 
    479          for (const char* layerName : validationLayers) {
    480             bool layerFound = false;
    481 
    482             for (const auto& layerProperties : availableLayers) {
    483                if (strcmp(layerName, layerProperties.layerName) == 0) {
    484                   layerFound = true;
    485                   break;
    486                }
    487             }
    488 
    489             if (!layerFound) {
    490                return false;
    491             }
    492          }
    493 
    494          return true;
    495       }
    496 
    497       QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
    498          QueueFamilyIndices indices;
    499 
    500          uint32_t queueFamilyCount = 0;
    501          vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
    502 
    503          vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
    504          vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
    505 
    506          int i = 0;
    507          for (const auto& queueFamily : queueFamilies) {
    508             if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
    509                indices.graphicsFamily = i;
    510             }
    511 
    512             VkBool32 presentSupport = false;
    513             vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
    514 
    515             if (queueFamily.queueCount > 0 && presentSupport) {
    516                indices.presentFamily = i;
    517             }
    518 
    519             if (indices.isComplete()) {
    520                break;
    521             }
    522 
    523             i++;
    524          }
    525 
    526          return indices;
    527       }
    528 
    529       SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
    530          SwapChainSupportDetails details;
    531 
    532          vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
    533 
    534          uint32_t formatCount;
    535          vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
    536 
    537          if (formatCount != 0) {
    538             details.formats.resize(formatCount);
    539             vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
    540          }
    541 
    542          uint32_t presentModeCount;
    543          vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
    544 
    545          if (presentModeCount != 0) {
    546             details.presentModes.resize(presentModeCount);
    547             vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
    548          }
    549 
    550          return details;
    551       }
    552 
    553       VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
    554          for (const auto& availableFormat : availableFormats) {
    555             if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
    556                return availableFormat;
    557             }
    558          }
    559 
    560          return availableFormats[0];
    561       }
    562 
    563       VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
    564          VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
    565 
    566          for (const auto& availablePresentMode : availablePresentModes) {
    567             if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
    568                return availablePresentMode;
    569             } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
    570                bestMode = availablePresentMode;
    571             }
    572          }
    573 
    574          return bestMode;
    575       }
    576 
    577       VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
    578          if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
    579             return capabilities.currentExtent;
    580          } else {
    581             int width, height;
    582             gui->GetWindowSize(&width, &height);
    583 
    584             VkExtent2D actualExtent = {
    585                static_cast<uint32_t>(width),
    586                static_cast<uint32_t>(height)
    587             };
    588 
    589             actualExtent.width = max(capabilities.minImageExtent.width, min(capabilities.maxImageExtent.width, actualExtent.width));
    590             actualExtent.height = max(capabilities.minImageExtent.height, min(capabilities.maxImageExtent.height, actualExtent.height));
    591 
    592             return actualExtent;
    593          }
    594       }
    595 
    596       void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
    597          createInfo = {};
    598          createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
    599          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;
    600          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;
    601          createInfo.pfnUserCallback = debugCallback;
    602       }
    603 
    604       vector<const char*> getRequiredExtensions() {
    605          vector<const char*> extensions = gui->GetRequiredExtensions();
    606 
    607          if (enableValidationLayers) {
    608             extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
    609          }
    610 
    611          return extensions;
    612       }
    613 
    614460      void createSwapChain() {
    615461         SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
     
    620466
    621467         uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
    622          if (swapChainSupport.capabilities.maxImageCount > 0 &&
    623                imageCount > swapChainSupport.capabilities.maxImageCount) {
     468         if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
    624469            imageCount = swapChainSupport.capabilities.maxImageCount;
    625470         }
    626471
    627472         VkSwapchainCreateInfoKHR createInfo = {};
    628 
    629473         createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    630474         createInfo.surface = surface;
     
    645489         } else {
    646490            createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    647             createInfo.queueFamilyIndexCount = 0; // Optional
     491            createInfo.queueFamilyIndexCount = 0;
    648492            createInfo.pQueueFamilyIndices = nullptr;
    649493         }
     
    667511      }
    668512
     513      SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
     514         SwapChainSupportDetails details;
     515
     516         vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
     517
     518         uint32_t formatCount;
     519         vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
     520
     521         if (formatCount != 0) {
     522            details.formats.resize(formatCount);
     523            vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
     524         }
     525
     526         uint32_t presentModeCount;
     527         vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
     528
     529         if (presentModeCount != 0) {
     530            details.presentModes.resize(presentModeCount);
     531            vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
     532         }
     533
     534         return details;
     535      }
     536
     537      VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
     538         for (const auto& availableFormat : availableFormats) {
     539            if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
     540               return availableFormat;
     541            }
     542         }
     543
     544         return availableFormats[0];
     545      }
     546
     547      VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
     548         VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
     549
     550         for (const auto& availablePresentMode : availablePresentModes) {
     551            if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
     552               return availablePresentMode;
     553            }
     554            else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
     555               bestMode = availablePresentMode;
     556            }
     557         }
     558
     559         return bestMode;
     560      }
     561
     562      VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
     563         if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
     564            return capabilities.currentExtent;
     565         }
     566         else {
     567            int width, height;
     568            gui->GetWindowSize(&width, &height);
     569
     570            VkExtent2D actualExtent = {
     571               static_cast<uint32_t>(width),
     572               static_cast<uint32_t>(height)
     573            };
     574
     575            actualExtent.width = max(capabilities.minImageExtent.width, min(capabilities.maxImageExtent.width, actualExtent.width));
     576            actualExtent.height = max(capabilities.minImageExtent.height, min(capabilities.maxImageExtent.height, actualExtent.height));
     577
     578            return actualExtent;
     579         }
     580      }
     581
    669582      void createImageViews() {
    670583         swapChainImageViews.resize(swapChainImages.size());
    671584
    672          for (size_t i=0; i<swapChainImages.size(); i++) {
     585         for (size_t i = 0; i < swapChainImages.size(); i++) {
    673586            VkImageViewCreateInfo createInfo = {};
    674587            createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    675588            createInfo.image = swapChainImages[i];
    676 
    677589            createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
    678590            createInfo.format = swapChainImageFormat;
     
    715627         subpass.pColorAttachments = &colorAttachmentRef;
    716628
    717          VkSubpassDependency  dependency = {};
     629         VkSubpassDependency dependency = {};
    718630         dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
    719631         dependency.dstSubpass = 0;
     
    819731         rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
    820732         rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
    821          rasterizer.depthBiasEnable = false;
     733         rasterizer.depthBiasEnable = VK_FALSE;
    822734
    823735         VkPipelineMultisampleStateCreateInfo multisampling = {};
     
    923835
    924836         if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
    925             throw runtime_error("failed to create command pool!");
    926          }
     837            throw runtime_error("failed to create graphics command pool!");
     838         }
     839      }
     840
     841      QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
     842         QueueFamilyIndices indices;
     843
     844         uint32_t queueFamilyCount = 0;
     845         vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
     846
     847         vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
     848         vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
     849
     850         int i = 0;
     851         for (const auto& queueFamily : queueFamilies) {
     852            if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
     853               indices.graphicsFamily = i;
     854            }
     855
     856            VkBool32 presentSupport = false;
     857            vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
     858
     859            if (queueFamily.queueCount > 0 && presentSupport) {
     860               indices.presentFamily = i;
     861            }
     862
     863            if (indices.isComplete()) {
     864               break;
     865            }
     866
     867            i++;
     868         }
     869
     870         return indices;
    927871      }
    928872
     
    964908      }
    965909
    966       void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
    967             VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image,
    968             VkDeviceMemory& imageMemory) {
     910      void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
     911            VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
    969912         VkImageCreateInfo imageInfo = {};
    970913         imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
     
    989932         vkGetImageMemoryRequirements(device, image, &memRequirements);
    990933
    991          VkMemoryAllocateInfo allocInfo;
     934         VkMemoryAllocateInfo allocInfo = {};
    992935         allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    993936         allocInfo.allocationSize = memRequirements.size;
     
    1001944      }
    1002945
    1003       void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout,
    1004             VkImageLayout newLayout) {
     946      void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
    1005947         VkCommandBuffer commandBuffer = beginSingleTimeCommands();
    1006948
     
    10801022         VkBuffer stagingBuffer;
    10811023         VkDeviceMemory stagingBufferMemory;
    1082          createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 
     1024         createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
    10831025            VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    10841026            stagingBuffer, stagingBufferMemory);
     
    10861028         void* data;
    10871029         vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
    1088          memcpy(data, vertices.data(), (size_t)bufferSize);
     1030         memcpy(data, vertices.data(), (size_t) bufferSize);
    10891031         vkUnmapMemory(device, stagingBufferMemory);
    10901032
     
    11091051         void* data;
    11101052         vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
    1111          memcpy(data, indices.data(), (size_t)bufferSize);
     1053         memcpy(data, indices.data(), (size_t) bufferSize);
    11121054         vkUnmapMemory(device, stagingBufferMemory);
    11131055
     
    11211063      }
    11221064
    1123       void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties,
    1124             VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
     1065      void createUniformBuffers() {
     1066         VkDeviceSize bufferSize = sizeof(UniformBufferObject);
     1067
     1068         uniformBuffers.resize(swapChainImages.size());
     1069         uniformBuffersMemory.resize(swapChainImages.size());
     1070
     1071         for (size_t i = 0; i < swapChainImages.size(); i++) {
     1072            createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     1073               VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     1074               uniformBuffers[i], uniformBuffersMemory[i]);
     1075         }
     1076      }
     1077
     1078      void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
    11251079         VkBufferCreateInfo bufferInfo = {};
    11261080         bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
     
    11401094         allocInfo.allocationSize = memRequirements.size;
    11411095         allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
    1142      
     1096
    11431097         if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
    11441098            throw runtime_error("failed to allocate buffer memory!");
     
    12041158      }
    12051159
    1206       void createUniformBuffers() {
    1207          VkDeviceSize bufferSize = sizeof(UniformBufferObject);
    1208 
    1209          uniformBuffers.resize(swapChainImages.size());
    1210          uniformBuffersMemory.resize(swapChainImages.size());
    1211 
    1212          for (size_t i = 0; i < swapChainImages.size(); i++) {
    1213             createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    1214                VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    1215                uniformBuffers[i], uniformBuffersMemory[i]);
    1216          }
    1217       }
    1218 
    12191160      void createDescriptorPool() {
    12201161         VkDescriptorPoolSize poolSize = {};
     
    12351176      void createDescriptorSets() {
    12361177         vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);
     1178
    12371179         VkDescriptorSetAllocateInfo allocInfo = {};
    12381180         allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
     
    12741216         allocInfo.commandPool = commandPool;
    12751217         allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    1276          allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
     1218         allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
    12771219
    12781220         if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
    1279             throw runtime_error("failed to create command buffers!");
     1221            throw runtime_error("failed to allocate command buffers!");
    12801222         }
    12811223
     
    13021244
    13031245            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
     1246
    13041247            vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
    13051248
     
    13121255
    13131256            vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
     1257
    13141258            vkCmdEndRenderPass(commandBuffers[i]);
    13151259
     
    13801324         uint32_t imageIndex;
    13811325
    1382          VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
     1326         VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
     1327            imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
    13831328
    13841329         if (result == VK_ERROR_OUT_OF_DATE_KHR) {
     
    14161361         VkPresentInfoKHR presentInfo = {};
    14171362         presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    1418 
    14191363         presentInfo.waitSemaphoreCount = 1;
    14201364         presentInfo.pWaitSemaphores = signalSemaphores;
     
    14471391         ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
    14481392         ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
    1449          ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float) swapChainExtent.height, 0.1f, 10.0f);
     1393         ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
    14501394         ubo.proj[1][1] *= -1;
    14511395
     
    14561400      }
    14571401
     1402      void recreateSwapChain() {
     1403         int width = 0, height = 0;
     1404
     1405         gui->GetWindowSize(&width, &height);
     1406
     1407         while (width == 0 || height == 0 ||
     1408            (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
     1409            SDL_WaitEvent(nullptr);
     1410            gui->GetWindowSize(&width, &height);
     1411         }
     1412
     1413         vkDeviceWaitIdle(device);
     1414
     1415         cleanupSwapChain();
     1416
     1417         createSwapChain();
     1418         createImageViews();
     1419         createRenderPass();
     1420         createGraphicsPipeline();
     1421         createFramebuffers();
     1422         createUniformBuffers();
     1423         createDescriptorPool();
     1424         createDescriptorSets();
     1425         createCommandBuffers();
     1426      }
     1427
    14581428      void cleanup() {
    14591429         cleanupSwapChain();
     
    14711441
    14721442         for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
     1443            vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
    14731444            vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
    1474             vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
    14751445            vkDestroyFence(device, inFlightFences[i], nullptr);
    14761446         }
     
    14921462      }
    14931463
     1464      void cleanupSwapChain() {
     1465         for (auto framebuffer : swapChainFramebuffers) {
     1466            vkDestroyFramebuffer(device, framebuffer, nullptr);
     1467         }
     1468
     1469         vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
     1470
     1471         vkDestroyPipeline(device, graphicsPipeline, nullptr);
     1472         vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
     1473         vkDestroyRenderPass(device, renderPass, nullptr);
     1474
     1475         for (auto imageView : swapChainImageViews) {
     1476            vkDestroyImageView(device, imageView, nullptr);
     1477         }
     1478
     1479         vkDestroySwapchainKHR(device, swapChain, nullptr);
     1480
     1481         for (size_t i = 0; i < swapChainImages.size(); i++) {
     1482            vkDestroyBuffer(device, uniformBuffers[i], nullptr);
     1483            vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
     1484         }
     1485
     1486         vkDestroyDescriptorPool(device, descriptorPool, nullptr);
     1487      }
     1488
    14941489      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
    1495          VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
    1496          VkDebugUtilsMessageTypeFlagsEXT messageType,
    1497          const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
    1498          void* pUserData) {
     1490            VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
     1491            VkDebugUtilsMessageTypeFlagsEXT messageType,
     1492            const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
     1493            void* pUserData) {
    14991494         cerr << "validation layer: " << pCallbackData->pMessage << endl;
    15001495
     
    15091504         }
    15101505
    1511          size_t fileSize = (size_t)file.tellg();
     1506         size_t fileSize = (size_t) file.tellg();
    15121507         vector<char> buffer(fileSize);
    15131508
Note: See TracChangeset for help on using the changeset viewer.