From 2a8abd04d8793349a4a5fe0083bbe722e1512818 Mon Sep 17 00:00:00 2001 From: legatt Date: Tue, 19 Aug 2025 16:32:26 +0300 Subject: [PATCH] Frame Views --- frontend/include/Renderer.hpp | 1 + frontend/src/Renderer.cpp | 52 ++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/frontend/include/Renderer.hpp b/frontend/include/Renderer.hpp index 7fe8cb8..764a2b6 100644 --- a/frontend/include/Renderer.hpp +++ b/frontend/include/Renderer.hpp @@ -71,6 +71,7 @@ private: VkDevice device; VkSwapchainKHR swapchain; std::vector frames; + std::vector frameViews; SurfaceInfo surfaceInfo; QueueFamiliesBundle queueFamilies; diff --git a/frontend/src/Renderer.cpp b/frontend/src/Renderer.cpp index 3025776..d73bea3 100644 --- a/frontend/src/Renderer.cpp +++ b/frontend/src/Renderer.cpp @@ -331,16 +331,16 @@ void Renderer::SetupSwapchain() (std::min)(surfaceInfo.capabilities.maxImageExtent.height, static_cast(height)) ); - uint32_t minImagesCount = surfaceInfo.capabilities.minImageCount + 1; + uint32_t imagesCount = surfaceInfo.capabilities.minImageCount + 1; // Clamp min images count - if (surfaceInfo.capabilities.maxImageCount > 0 && minImagesCount > surfaceInfo.capabilities.maxImageCount) - minImagesCount = surfaceInfo.capabilities.maxImageCount; + if (surfaceInfo.capabilities.maxImageCount > 0 && imagesCount > surfaceInfo.capabilities.maxImageCount) + imagesCount = surfaceInfo.capabilities.maxImageCount; VkSwapchainCreateInfoKHR swapchainCreateConfig { .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, .pNext = nullptr, .surface = surface, - .minImageCount = minImagesCount, + .minImageCount = imagesCount, .imageFormat = surfaceInfo.format.format, .imageColorSpace = surfaceInfo.format.colorSpace, .imageExtent = surfaceInfo.extent, @@ -367,6 +367,45 @@ void Renderer::SetupSwapchain() if (vkResult != VK_SUCCESS) LOG(CRITICAL, "Failed to create swapchain. Code: %d", vkResult); else LOG(INFO, "Swapchain is created"); + + vkResult = vkGetSwapchainImagesKHR(device, swapchain, &imagesCount, nullptr); + if (vkResult != VK_SUCCESS) + LOG(CRITICAL, "Failed to get frames number. Code: %d", vkResult); + frames.resize(imagesCount); + vkResult = vkGetSwapchainImagesKHR(device, swapchain, &imagesCount, frames.data()); + if (vkResult != VK_SUCCESS) + LOG(CRITICAL, "Failed to get frames descriptors. Code: %d", vkResult); + else LOG(INFO, "Frames are ready"); + + frameViews.resize(imagesCount); + for (size_t i = 0; i < frames.size(); i++) + { + VkImageViewCreateInfo frameViewConfig { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .pNext = nullptr, + .image = frames[i], + .viewType = VK_IMAGE_VIEW_TYPE_2D, + .format = surfaceInfo.format.format, + .components = { + .r = VK_COMPONENT_SWIZZLE_IDENTITY, + .g = VK_COMPONENT_SWIZZLE_IDENTITY, + .b = VK_COMPONENT_SWIZZLE_IDENTITY, + .a = VK_COMPONENT_SWIZZLE_IDENTITY + }, + .subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1 + } + }; + + vkResult = vkCreateImageView(device, &frameViewConfig, nullptr, &frameViews[i]); + if (vkResult != VK_SUCCESS) + LOG(CRITICAL, "Failed to create image view for frame. Code: %d", vkResult); + } + LOG(INFO, "Frame views are ready."); } void Renderer::Start() @@ -450,6 +489,11 @@ void Renderer::Request(RenderCommand::Type type) void Renderer::Release() { + for (VkImageView frameView : frameViews) + { + vkDestroyImageView(device, frameView, nullptr); + } + vkDestroySwapchainKHR(device, swapchain, nullptr); vkDestroyDevice(device, nullptr); vkDestroySurfaceKHR(vulkan, surface, nullptr);