Added resize, started doing multiple shaders

This commit is contained in:
Alexandre 2026-07-14 21:04:05 +02:00
parent bf2e15fcd3
commit 891e178008
2 changed files with 95 additions and 5 deletions

View File

@ -199,6 +199,7 @@ addBinary (
SOURCE Engine/main
SHADERS shaders/gradient.comp
SHADERS shaders/gradient_color.comp
LIBS glfw fmt vk-bootstrap vulkan
IMGUI
)

View File

@ -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<ComputeEffect> 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<uint32_t>(_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