Added resizing + swap chain recreation
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#define DEBUG
|
||||
#define DEBUG 1
|
||||
|
||||
#include <iostream>
|
||||
#include "vulkan/vulkan_app.hpp"
|
||||
|
||||
+164
-111
@@ -5,43 +5,52 @@
|
||||
#include "vulkan_app.hpp"
|
||||
|
||||
namespace vapp{
|
||||
|
||||
const std::vector<const char*> validationLayers = {
|
||||
const std::vector<const char*> validationLayers = {
|
||||
"VK_LAYER_KHRONOS_validation"
|
||||
};
|
||||
};
|
||||
|
||||
const std::vector<const char*> deviceExtensions = {
|
||||
const std::vector<const char*> deviceExtensions = {
|
||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
||||
};
|
||||
};
|
||||
|
||||
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData){
|
||||
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
||||
VkDebugUtilsMessageTypeFlagsEXT messageType,
|
||||
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
|
||||
void *pUserData){
|
||||
std::cerr << "Validation Layer: " << pCallbackData->pMessage << std::endl;
|
||||
return VK_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT *pDebugMessenger){
|
||||
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
||||
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator,
|
||||
VkDebugUtilsMessengerEXT *pDebugMessenger){
|
||||
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
|
||||
instance, "vkCreateDebugUtilsMessengerEXT");
|
||||
if(func == nullptr) return VK_ERROR_EXTENSION_NOT_PRESENT;
|
||||
if(func == nullptr) return VK_ERROR_EXTENSION_NOT_PRESENT;
|
||||
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
||||
}
|
||||
}
|
||||
|
||||
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks *pAllocator){
|
||||
if(auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); func != nullptr){
|
||||
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger,
|
||||
const VkAllocationCallbacks *pAllocator){
|
||||
if(auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
|
||||
instance, "vkDestroyDebugUtilsMessengerEXT"); func != nullptr){
|
||||
func(instance, debugMessenger, pAllocator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT &createInfo){
|
||||
void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo){
|
||||
createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||
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;
|
||||
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;
|
||||
// set which type of messages the callback is notified about
|
||||
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;
|
||||
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;
|
||||
createInfo.pfnUserCallback = debugCallback;
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<char> readFile(const std::string &filename){
|
||||
static std::vector<char> readFile(const std::string& filename){
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
|
||||
if(!file.is_open()){
|
||||
@@ -56,16 +65,16 @@ static std::vector<char> readFile(const std::string &filename){
|
||||
|
||||
file.close();
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
bool Vulkan::checkValidationLayerSupport(){
|
||||
bool Vulkan::checkValidationLayerSupport(){
|
||||
uint32_t layerCount;
|
||||
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
||||
|
||||
std::vector<VkLayerProperties> availableLayers(layerCount);
|
||||
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
||||
|
||||
for(const char* layerName : validationLayers){
|
||||
for(const char *layerName : validationLayers){
|
||||
bool layerFound = false;
|
||||
|
||||
for(const auto& layerProperties : availableLayers){
|
||||
@@ -78,9 +87,9 @@ bool Vulkan::checkValidationLayerSupport(){
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<const char*> Vulkan::getRequiredExtensions(){
|
||||
std::vector<const char*> Vulkan::getRequiredExtensions(){
|
||||
uint32_t glfwExtensionCount = 0;
|
||||
const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
||||
|
||||
@@ -89,9 +98,9 @@ std::vector<const char*> Vulkan::getRequiredExtensions(){
|
||||
if(enableValidationLayers) extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
||||
|
||||
return extensions;
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createInstance(){
|
||||
void Vulkan::createInstance(){
|
||||
if(enableValidationLayers && !checkValidationLayerSupport()){
|
||||
throw std::runtime_error("Func: createInstance\nError: Validation Layers requested, but not available!\n");
|
||||
}
|
||||
@@ -139,9 +148,9 @@ void Vulkan::createInstance(){
|
||||
std::cout << '\t' << extension.extensionName << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::setupDebugMessenger(){
|
||||
void Vulkan::setupDebugMessenger(){
|
||||
if(!enableValidationLayers) return;
|
||||
VkDebugUtilsMessengerCreateInfoEXT createInfo{};
|
||||
populateDebugMessengerCreateInfo(createInfo);
|
||||
@@ -149,9 +158,9 @@ void Vulkan::setupDebugMessenger(){
|
||||
if(CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: setupDebugMessenger\nError: Failed to set up Debug Messenger!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::pickPhysicalDevice(){
|
||||
void Vulkan::pickPhysicalDevice(){
|
||||
uint32_t deviceCount = 0;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
||||
if(deviceCount == 0){
|
||||
@@ -171,13 +180,13 @@ void Vulkan::pickPhysicalDevice(){
|
||||
if(physicalDevice == VK_NULL_HANDLE){
|
||||
throw std::runtime_error("Func: pickPhysicalDevice\nError: Failed to find suitable GPU!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createLogicalDevice(){
|
||||
void Vulkan::createLogicalDevice(){
|
||||
QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
|
||||
|
||||
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
|
||||
std::set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
||||
std::set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
|
||||
|
||||
float queuePriority = 1.0f;
|
||||
for(uint32_t queueFamily : uniqueQueueFamilies){
|
||||
@@ -216,9 +225,9 @@ void Vulkan::createLogicalDevice(){
|
||||
|
||||
vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
|
||||
vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
||||
}
|
||||
}
|
||||
|
||||
QueueFamilyIndices Vulkan::findQueueFamilies(VkPhysicalDevice device){
|
||||
QueueFamilyIndices Vulkan::findQueueFamilies(VkPhysicalDevice device){
|
||||
QueueFamilyIndices indices;
|
||||
|
||||
uint32_t queueFamilyCount = 0;
|
||||
@@ -238,9 +247,9 @@ QueueFamilyIndices Vulkan::findQueueFamilies(VkPhysicalDevice device){
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
||||
}
|
||||
|
||||
bool Vulkan::checkDeviceExtensionSupport(VkPhysicalDevice device){
|
||||
bool Vulkan::checkDeviceExtensionSupport(VkPhysicalDevice device){
|
||||
uint32_t extensionCount;
|
||||
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
|
||||
|
||||
@@ -254,9 +263,9 @@ bool Vulkan::checkDeviceExtensionSupport(VkPhysicalDevice device){
|
||||
}
|
||||
|
||||
return requiredExtensions.empty();
|
||||
}
|
||||
}
|
||||
|
||||
bool Vulkan::isDeviceSuitable(VkPhysicalDevice device){
|
||||
bool Vulkan::isDeviceSuitable(VkPhysicalDevice device){
|
||||
/*
|
||||
VkPhysicalDeviceProperties deviceProperties;
|
||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||
@@ -274,15 +283,15 @@ bool Vulkan::isDeviceSuitable(VkPhysicalDevice device){
|
||||
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
||||
}
|
||||
return indices.isComplete() && extensionSupported && swapChainAdequate;
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createSurface(){
|
||||
void Vulkan::createSurface(){
|
||||
if(glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: createSurface\nError: Failed to create Window Surface!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SwapChainSupportDetails Vulkan::querySwapChainSupport(VkPhysicalDevice device){
|
||||
SwapChainSupportDetails Vulkan::querySwapChainSupport(VkPhysicalDevice device){
|
||||
SwapChainSupportDetails details;
|
||||
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
|
||||
@@ -304,29 +313,32 @@ SwapChainSupportDetails Vulkan::querySwapChainSupport(VkPhysicalDevice device){
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
}
|
||||
|
||||
VkSurfaceFormatKHR Vulkan::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats){
|
||||
VkSurfaceFormatKHR Vulkan::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats){
|
||||
for(const VkSurfaceFormatKHR availableFormat : availableFormats){
|
||||
if(availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR){
|
||||
if(availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace ==
|
||||
VK_COLOR_SPACE_SRGB_NONLINEAR_KHR){
|
||||
return availableFormat;
|
||||
}
|
||||
}
|
||||
|
||||
return availableFormats[0];
|
||||
}
|
||||
}
|
||||
|
||||
VkPresentModeKHR Vulkan::chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes){
|
||||
VkPresentModeKHR Vulkan::chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes){
|
||||
for(const VkPresentModeKHR availablePresentMode : availablePresentModes){
|
||||
if(availablePresentMode == WISHED_PRESENT_MODE){
|
||||
return availablePresentMode;
|
||||
}
|
||||
}
|
||||
return VK_PRESENT_MODE_FIFO_KHR; // always available
|
||||
}
|
||||
}
|
||||
|
||||
VkExtent2D Vulkan::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities){
|
||||
if(capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()){ return capabilities.currentExtent; }
|
||||
VkExtent2D Vulkan::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities){
|
||||
if(capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()){
|
||||
return capabilities.currentExtent;
|
||||
}
|
||||
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
@@ -336,13 +348,15 @@ VkExtent2D Vulkan::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities
|
||||
static_cast<uint32_t>(height)
|
||||
};
|
||||
|
||||
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
|
||||
actualExtent.width = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
||||
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width,
|
||||
capabilities.maxImageExtent.width);
|
||||
actualExtent.width = std::clamp(actualExtent.height, capabilities.minImageExtent.height,
|
||||
capabilities.maxImageExtent.height);
|
||||
|
||||
return actualExtent;
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createSwapChain(){
|
||||
void Vulkan::createSwapChain(){
|
||||
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
|
||||
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
|
||||
VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
|
||||
@@ -366,7 +380,7 @@ void Vulkan::createSwapChain(){
|
||||
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
|
||||
QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
|
||||
uint32_t queueFamiliyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
|
||||
uint32_t queueFamiliyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
|
||||
|
||||
if(indices.graphicsFamily != indices.presentFamily){
|
||||
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||
@@ -395,9 +409,9 @@ void Vulkan::createSwapChain(){
|
||||
|
||||
swapChainImageFormat = surfaceFormat.format;
|
||||
swapChainExtent = extent;
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createImageViews(){
|
||||
void Vulkan::createImageViews(){
|
||||
swapChainImageViews.resize(swapChainImages.size());
|
||||
|
||||
for(size_t i = 0; i < swapChainImages.size(); i++){
|
||||
@@ -423,9 +437,9 @@ void Vulkan::createImageViews(){
|
||||
throw std::runtime_error("Func: createImageViews\nError: Failed to create Image Views!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createGraphicsPipeline(){
|
||||
void Vulkan::createGraphicsPipeline(){
|
||||
// TODO: find solution for file location
|
||||
std::vector<char> vertShaderCode;
|
||||
std::vector<char> fragShaderCode;
|
||||
@@ -454,7 +468,7 @@ void Vulkan::createGraphicsPipeline(){
|
||||
fragShaderStageInfo.module = fragShaderModule;
|
||||
fragShaderStageInfo.pName = "main";
|
||||
|
||||
VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
|
||||
VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo};
|
||||
|
||||
// Dynamics are done to not reinitialize every pipeline for example resizes
|
||||
std::vector<VkDynamicState> dynamicStates = {
|
||||
@@ -563,15 +577,16 @@ void Vulkan::createGraphicsPipeline(){
|
||||
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
||||
*/
|
||||
|
||||
if(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS){
|
||||
if(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) !=
|
||||
VK_SUCCESS){
|
||||
throw std::runtime_error("Func; createGraphicsPipeline\nError: Failed to create graphics pipeline!\n");
|
||||
}
|
||||
|
||||
vkDestroyShaderModule(device, vertShaderModule, nullptr);
|
||||
vkDestroyShaderModule(device, fragShaderModule, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
VkShaderModule Vulkan::createShaderModule(const std::vector<char> &code){
|
||||
VkShaderModule Vulkan::createShaderModule(const std::vector<char>& code){
|
||||
VkShaderModuleCreateInfo createInfo{};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||
createInfo.codeSize = code.size();
|
||||
@@ -583,9 +598,9 @@ VkShaderModule Vulkan::createShaderModule(const std::vector<char> &code){
|
||||
}
|
||||
|
||||
return shaderModule;
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createRenderPass(){
|
||||
void Vulkan::createRenderPass(){
|
||||
VkAttachmentDescription colorAttachment{};
|
||||
colorAttachment.format = swapChainImageFormat;
|
||||
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
@@ -625,13 +640,13 @@ void Vulkan::createRenderPass(){
|
||||
if(vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: createRenderPass\nError: Failed to create render pass!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createFrameBuffers(){
|
||||
void Vulkan::createFrameBuffers(){
|
||||
swapChainFramebuffers.resize(swapChainImageViews.size());
|
||||
|
||||
for(size_t i = 0; i < swapChainImageViews.size(); i++){
|
||||
VkImageView attachements[] = { swapChainImageViews[i] };
|
||||
VkImageView attachements[] = {swapChainImageViews[i]};
|
||||
|
||||
VkFramebufferCreateInfo framebufferInfo{};
|
||||
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||||
@@ -646,9 +661,9 @@ void Vulkan::createFrameBuffers(){
|
||||
throw std::runtime_error("Func: createFrameBuffers\nError: Failed to create Framebuffer!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createCommandPool(){
|
||||
void Vulkan::createCommandPool(){
|
||||
QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
|
||||
VkCommandPoolCreateInfo poolInfo{};
|
||||
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||
@@ -658,9 +673,9 @@ void Vulkan::createCommandPool(){
|
||||
if(vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: createCommandPool\nError: Failed to create Command Pool!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createCommandBuffers(){
|
||||
void Vulkan::createCommandBuffers(){
|
||||
commandBuffers.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
|
||||
VkCommandBufferAllocateInfo allocInfo{};
|
||||
@@ -672,9 +687,9 @@ void Vulkan::createCommandBuffers(){
|
||||
if(vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: createCommandBuffer\nError: Failed to allocate Command Buffers!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex){
|
||||
void Vulkan::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex){
|
||||
VkCommandBufferBeginInfo beginInfo{};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
|
||||
@@ -686,7 +701,7 @@ void Vulkan::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIn
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
renderPassInfo.renderPass = renderPass;
|
||||
renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex];
|
||||
renderPassInfo.renderArea.offset = { 0, 0 };
|
||||
renderPassInfo.renderArea.offset = {0, 0};
|
||||
renderPassInfo.renderArea.extent = swapChainExtent;
|
||||
|
||||
VkClearValue clearColor = {{{0.01f, 0.01f, 0.01f, 1.0f}}}; // Background color after clear (Black 100%)
|
||||
@@ -706,7 +721,7 @@ void Vulkan::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIn
|
||||
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
||||
|
||||
VkRect2D scissor{};
|
||||
scissor.offset = { 0, 0 };
|
||||
scissor.offset = {0, 0};
|
||||
scissor.extent = swapChainExtent;
|
||||
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||
|
||||
@@ -716,9 +731,9 @@ void Vulkan::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIn
|
||||
if(vkEndCommandBuffer(commandBuffer) != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: recordCommandBuffer\nError: Failed to record command buffer!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::createSyncObjects(){
|
||||
void Vulkan::createSyncObjects(){
|
||||
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
@@ -737,14 +752,17 @@ void Vulkan::createSyncObjects(){
|
||||
throw std::runtime_error("Func: createSyncObjects\nError: Failed to create semaphores!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::drawFrame(){
|
||||
void Vulkan::drawFrame(){
|
||||
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
|
||||
vkResetFences(device, 1, &inFlightFences[currentFrame]);
|
||||
|
||||
uint32_t imageIndex;
|
||||
vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
||||
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
||||
|
||||
// INFO: is this right here?
|
||||
// vkResetFences(device, 1, &inFlightFences[currentFrame]);
|
||||
|
||||
vkResetCommandBuffer(commandBuffers[currentFrame], 0);
|
||||
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);
|
||||
@@ -752,8 +770,8 @@ void Vulkan::drawFrame(){
|
||||
VkSubmitInfo submitInfo{};
|
||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
|
||||
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
|
||||
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
|
||||
VkSemaphore waitSemaphores[] = {imageAvailableSemaphores[currentFrame]};
|
||||
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
|
||||
|
||||
submitInfo.waitSemaphoreCount = 1;
|
||||
submitInfo.pWaitSemaphores = waitSemaphores;
|
||||
@@ -761,7 +779,7 @@ void Vulkan::drawFrame(){
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &commandBuffers[currentFrame];
|
||||
|
||||
VkSemaphore signalSemaphore[] = { renderFinishedSemaphores[currentFrame] };
|
||||
VkSemaphore signalSemaphore[] = {renderFinishedSemaphores[currentFrame]};
|
||||
submitInfo.signalSemaphoreCount = 1;
|
||||
submitInfo.pSignalSemaphores = signalSemaphore;
|
||||
|
||||
@@ -774,24 +792,65 @@ void Vulkan::drawFrame(){
|
||||
presentInfo.waitSemaphoreCount = 1;
|
||||
presentInfo.pWaitSemaphores = signalSemaphore;
|
||||
|
||||
VkSwapchainKHR swapChains[] = { swapChain };
|
||||
VkSwapchainKHR swapChains[] = {swapChain};
|
||||
presentInfo.swapchainCount = 1;
|
||||
presentInfo.pSwapchains = swapChains;
|
||||
presentInfo.pImageIndices = &imageIndex;
|
||||
|
||||
vkQueuePresentKHR(presentQueue, &presentInfo);
|
||||
|
||||
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
||||
}
|
||||
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized){
|
||||
framebufferResized = false;
|
||||
recreateSwapChain();
|
||||
}
|
||||
else if(result != VK_SUCCESS){
|
||||
throw std::runtime_error("Func: drawFrame\nError: Failed to acquire swap chain image!\n");
|
||||
}
|
||||
|
||||
void Vulkan::initWindow(const char* windowName){
|
||||
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
|
||||
}
|
||||
|
||||
void Vulkan::cleanupSwapChain(){
|
||||
for(const auto & swapChainFramebuffer : swapChainFramebuffers)
|
||||
{ vkDestroyFramebuffer(device, swapChainFramebuffer, nullptr); }
|
||||
for(const auto & swapChainImageView : swapChainImageViews)
|
||||
{ vkDestroyImageView(device, swapChainImageView, nullptr); }
|
||||
|
||||
vkDestroySwapchainKHR(device, swapChain, nullptr);
|
||||
}
|
||||
|
||||
void Vulkan::recreateSwapChain(){
|
||||
int width = 0, height = 0;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
while(width == 0 || height == 0){
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
vkDeviceWaitIdle(device);
|
||||
|
||||
cleanupSwapChain();
|
||||
|
||||
createSwapChain();
|
||||
createImageViews();
|
||||
createFrameBuffers();
|
||||
}
|
||||
|
||||
static void framebufferResizeCallback(GLFWwindow *window, int width, int height){
|
||||
auto app = reinterpret_cast<Vulkan*>(glfwGetWindowUserPointer(window));
|
||||
app->framebufferResized = true;
|
||||
}
|
||||
|
||||
void Vulkan::initWindow(const char *windowName){
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // tell glfw to not use opengl
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // disable window resize
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // disable window resize
|
||||
this->window = glfwCreateWindow(static_cast<int>(_width), static_cast<int>(_height), windowName, nullptr, nullptr);
|
||||
}
|
||||
glfwSetWindowUserPointer(window, this);
|
||||
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
|
||||
}
|
||||
|
||||
void Vulkan::initVulkan(){
|
||||
void Vulkan::initVulkan(){
|
||||
createInstance();
|
||||
setupDebugMessenger();
|
||||
createSurface();
|
||||
@@ -805,19 +864,26 @@ void Vulkan::initVulkan(){
|
||||
createCommandPool();
|
||||
createCommandBuffers();
|
||||
createSyncObjects();
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::mainLoop(){
|
||||
void Vulkan::mainLoop(){
|
||||
while(!glfwWindowShouldClose(this->window)){
|
||||
// glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
drawFrame();
|
||||
}
|
||||
vkDeviceWaitIdle(device);
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::cleanup(){
|
||||
void Vulkan::cleanup(){
|
||||
// Vulkan
|
||||
cleanupSwapChain();
|
||||
|
||||
vkDestroyPipeline(device, graphicsPipeline, nullptr);
|
||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||
|
||||
vkDestroyRenderPass(device, renderPass, nullptr);
|
||||
|
||||
for(size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++){
|
||||
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
||||
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
||||
@@ -826,19 +892,6 @@ void Vulkan::cleanup(){
|
||||
|
||||
vkDestroyCommandPool(device, commandPool, nullptr);
|
||||
|
||||
for(VkFramebuffer framebuffer : swapChainFramebuffers){
|
||||
vkDestroyFramebuffer(device, framebuffer, nullptr);
|
||||
}
|
||||
|
||||
vkDestroyPipeline(device, graphicsPipeline, nullptr);
|
||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||
vkDestroyRenderPass(device, renderPass, nullptr);
|
||||
|
||||
for(VkImageView imageView : swapChainImageViews){
|
||||
vkDestroyImageView(device, imageView, nullptr);
|
||||
}
|
||||
|
||||
vkDestroySwapchainKHR(device, swapChain, nullptr);
|
||||
vkDestroyDevice(device, nullptr);
|
||||
|
||||
if(enableValidationLayers){
|
||||
@@ -853,9 +906,9 @@ void Vulkan::cleanup(){
|
||||
// GLFW
|
||||
glfwDestroyWindow(this->window);
|
||||
glfwTerminate();
|
||||
}
|
||||
}
|
||||
|
||||
void Vulkan::run(const char* windowName, const uint32_t width, const uint32_t height){
|
||||
void Vulkan::run(const char *windowName, const uint32_t width, const uint32_t height){
|
||||
this->_width = width;
|
||||
this->_height = height;
|
||||
|
||||
@@ -863,5 +916,5 @@ void Vulkan::run(const char* windowName, const uint32_t width, const uint32_t he
|
||||
initVulkan();
|
||||
mainLoop();
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,8 @@ private:
|
||||
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
||||
void createSyncObjects();
|
||||
void drawFrame();
|
||||
void cleanupSwapChain();
|
||||
void recreateSwapChain();
|
||||
void initWindow(const char* windowName);
|
||||
void initVulkan();
|
||||
void mainLoop();
|
||||
@@ -121,6 +123,8 @@ public:
|
||||
const bool enableValidationLayers = false;
|
||||
#endif
|
||||
|
||||
bool framebufferResized = false;
|
||||
|
||||
void run(const char* windowName, const uint32_t width = 800, const uint32_t height = 600);
|
||||
};
|
||||
} // vapp
|
||||
|
||||
Reference in New Issue
Block a user