From 891e178008f5fbeec1f5910dcb09fc81992268d9 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Tue, 14 Jul 2026 21:04:05 +0200 Subject: [PATCH] Added resize, started doing multiple shaders --- CMakeLists.txt | 1 + src/Engine/engine/engine.h | 99 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c48c09..ff040b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,7 @@ addBinary ( SOURCE Engine/main SHADERS shaders/gradient.comp + SHADERS shaders/gradient_color.comp LIBS glfw fmt vk-bootstrap vulkan IMGUI ) diff --git a/src/Engine/engine/engine.h b/src/Engine/engine/engine.h index cf500c6..060ab5d 100644 --- a/src/Engine/engine/engine.h +++ b/src/Engine/engine/engine.h @@ -21,6 +21,22 @@ struct FrameData { DeletionQueue _deletionQueue; }; +struct ComputePushConstants { + glm::vec4 data1; + glm::vec4 data2; + glm::vec4 data3; + glm::vec4 data4; +}; + +struct ComputeEffect { + const char* name; + + VkPipeline pipeline; + VkPipelineLayout layout; + + ComputePushConstants data; +}; + constexpr uint32_t FRAME_OVERLAP = 2; class Engine { @@ -75,6 +91,13 @@ class Engine { VkCommandBuffer _immCommandBuffer; VkCommandPool _immCommandPool; + // Resize + bool _requestResize; + + // Push constants + std::vector backgroundEffects; + int currentBackgroundEffect{0}; // Already initialized + void init(); @@ -114,6 +137,7 @@ class Engine { VkCommandBuffer cmd, VkImageView targetImageView ); + void resizeSwapchain(); }; @@ -185,7 +209,7 @@ void Engine::init() { glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); window = glfwCreateWindow( static_cast(_windowExtent.width), @@ -273,7 +297,15 @@ void Engine::draw() { // Request image from the swapchain uint32_t swapchainImageIndex; - VK_CHECK(vkAcquireNextImageKHR(_device, _swapchain, UINT64_MAX, getCurrentFrame()._swapchainSemaphore, nullptr, &swapchainImageIndex)); + VkResult aquireResult {vkAcquireNextImageKHR(_device, _swapchain, UINT64_MAX, getCurrentFrame()._swapchainSemaphore, nullptr, &swapchainImageIndex)}; + + if (aquireResult == VK_SUBOPTIMAL_KHR || aquireResult == VK_ERROR_OUT_OF_DATE_KHR) { + // Recreate swapchain + _requestResize = true; + return; + } else if (aquireResult != VK_SUCCESS) { + throw std::runtime_error("[Engine:Error] Failed to aquire swapchain image"); + } // Reset fences to redo sync VK_CHECK(vkResetFences(_device, 1, &getCurrentFrame()._renderFence)); @@ -370,7 +402,16 @@ void Engine::draw() { presentInfo.pImageIndices = &swapchainImageIndex; - VK_CHECK(vkQueuePresentKHR(_graphicsQueue, &presentInfo)); + VkResult presentResult {vkQueuePresentKHR(_graphicsQueue, &presentInfo)}; + + if (presentResult == VK_SUBOPTIMAL_KHR || presentResult == VK_ERROR_OUT_OF_DATE_KHR) { + // Recreate swapchain + _requestResize = true; + return; + } else if (presentResult != VK_SUCCESS) { + throw std::runtime_error("[Engine:Error] Failed to present swapchain image"); + } + _frameNumber++; } @@ -386,6 +427,8 @@ void Engine::run() { while (!glfwWindowShouldClose(window)) { glfwPollEvents(); + if (_requestResize) {resizeSwapchain();}; + if (glfwGetWindowAttrib(window, GLFW_ICONIFIED)) { stopRendering = true; } else { @@ -768,11 +811,21 @@ void Engine::initBackgroundPipelines() { #ifdef DEBUG fmt::println("[Engine:Init] Initializing compute pipelines..."); #endif + + + VkPushConstantRange pushConstant { + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, + .offset = 0, + .size = sizeof(ComputePushConstants) + }; + VkPipelineLayoutCreateInfo computeLayout { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .pNext = nullptr, .setLayoutCount = 1, - .pSetLayouts = &_drawImageDescriptorLayout + .pSetLayouts = &_drawImageDescriptorLayout, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &pushConstant }; VK_CHECK( @@ -788,7 +841,7 @@ void Engine::initBackgroundPipelines() { VkShaderModule computeDrawShader; if ( !vkutil::loadShaderModule( - "shaders/gradient.spv", + "shaders/gradient_color.spv", _device, &computeDrawShader ) @@ -1030,6 +1083,19 @@ void Engine::drawBackground( nullptr ); + ComputePushConstants pc; + pc.data1 = glm::vec4(1, 0, 0, 1); + pc.data2 = glm::vec4(0, 0, 1, 1); + + vkCmdPushConstants( + cmd, + _gradientPipelineLayout, + VK_SHADER_STAGE_COMPUTE_BIT, + 0, + sizeof(ComputePushConstants), + &pc + ); + vkCmdDispatch( cmd, std::ceil( @@ -1144,6 +1210,29 @@ void Engine::immediateSubmit( } +void Engine::resizeSwapchain() { + #ifdef DEBUG + fmt::println("[Engine:Utils] Resizing swapchain..."); + #endif + vkDeviceWaitIdle(_device); + + destroySwapchain(); + + int w, h; + glfwGetWindowSize(window, &w, &h); + + _windowExtent.width = w; + _windowExtent.height = h; + + createSwapchain(_windowExtent.width, _windowExtent.height); + + _requestResize = false; + + #ifdef DEBUG + fmt::println("[Engine:Utils] Swapchain resized !"); + #endif +} + #endif