From 47bb1541efb4c8f8be0cc15d83e80bce3f033737 Mon Sep 17 00:00:00 2001 From: Jiga228 Date: Mon, 29 Sep 2025 20:00:42 +0700 Subject: [PATCH] Add create comand_pool and command_buffers. Fix CMake file for TestGame --- Core/Core/RenderEngine.cpp | 35 +++++++++++++++++++++++++++++++++-- Core/Core/RenderEngine.h | 9 ++++++++- TestGame/CMakeLists.txt | 2 -- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Core/Core/RenderEngine.cpp b/Core/Core/RenderEngine.cpp index 752d866..2577bc9 100644 --- a/Core/Core/RenderEngine.cpp +++ b/Core/Core/RenderEngine.cpp @@ -394,7 +394,7 @@ void RenderEngine::create_render_pass() VK_CHECK(vkCreateRenderPass(device_, &render_pass_info, nullptr, &render_pass_)); } -void RenderEngine::create_graphycs_pipeline() +void RenderEngine::create_graphics_pipeline() { std::ifstream vertex_shader_file("Shaders/vertex.spv", std::ios::binary); if (!vertex_shader_file.is_open()) @@ -550,6 +550,31 @@ void RenderEngine::create_framebuffers() } } +void RenderEngine::create_command_pool() +{ + QueueFamilyIndices queue_family_indices = find_queue_family_indices(physical_device_, surface_); + + VkCommandPoolCreateInfo command_pool_info{}; + command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + command_pool_info.queueFamilyIndex = queue_family_indices.graphycs_family.value(); + command_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; + + VK_CHECK(vkCreateCommandPool(device_, &command_pool_info, nullptr, &command_pool_)); +} + +void RenderEngine::allocate_command_buffers() +{ + command_buffers_.resize(MAX_FRAMES_IN_FLIGHT); + + VkCommandBufferAllocateInfo command_buffer_allocate_info{}; + command_buffer_allocate_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + command_buffer_allocate_info.commandPool = command_pool_; + command_buffer_allocate_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + command_buffer_allocate_info.commandBufferCount = static_cast(command_buffers_.size()); + + VK_CHECK(vkAllocateCommandBuffers(device_, &command_buffer_allocate_info, command_buffers_.data())); +} + void RenderEngine::create_surface() { VK_CHECK(glfwCreateWindowSurface(instance_, window_, nullptr, &surface_)); @@ -569,11 +594,17 @@ window_(window) create_swapchain(desired_present_mode); create_image_views(); create_render_pass(); - create_graphycs_pipeline(); + create_graphics_pipeline(); } RenderEngine::~RenderEngine() { + if (command_buffers_.size() > 0) + vkFreeCommandBuffers(device_, command_pool_, static_cast(command_buffers_.size()), command_buffers_.data()); + + if (command_pool_ != VK_NULL_HANDLE) + vkDestroyCommandPool(device_, command_pool_, nullptr); + for (auto& i : framebuffers_) vkDestroyFramebuffer(device_, i, nullptr); diff --git a/Core/Core/RenderEngine.h b/Core/Core/RenderEngine.h index 7f4dd0c..6857c86 100644 --- a/Core/Core/RenderEngine.h +++ b/Core/Core/RenderEngine.h @@ -35,6 +35,9 @@ class RenderEngine static const std::vector deviceExtensions; + const int MAX_FRAMES_IN_FLIGHT = 2; + int current_frame_ = 0; + CoreInstance& core_; GLFWwindow* window_; VkInstance instance_ = VK_NULL_HANDLE; @@ -52,6 +55,8 @@ class RenderEngine VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE; VkPipeline graphycs_pipeline_ = VK_NULL_HANDLE; std::vector framebuffers_; + VkCommandPool command_pool_ = VK_NULL_HANDLE; + std::vector command_buffers_; #ifdef _DEBUG VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info); @@ -75,8 +80,10 @@ class RenderEngine void create_swapchain(VkPresentModeKHR desired_present_mode); void create_image_views(); void create_render_pass(); - void create_graphycs_pipeline(); + void create_graphics_pipeline(); void create_framebuffers(); + void create_command_pool(); + void allocate_command_buffers(); public: // Can throw the exception diff --git a/TestGame/CMakeLists.txt b/TestGame/CMakeLists.txt index ae41ece..8a6a218 100644 --- a/TestGame/CMakeLists.txt +++ b/TestGame/CMakeLists.txt @@ -59,7 +59,6 @@ add_custom_command( COMMAND $ENV{VULKAN_SDK}/Bin/glslc.exe ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/vertex.vert -o ${OUTPUT_PATH}/Shaders/vertex.spv - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/vertex.vert COMMENT "Building vertex shader" ) @@ -69,6 +68,5 @@ add_custom_command( COMMAND $ENV{VULKAN_SDK}/Bin/glslc.exe ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/fragment.frag -o ${OUTPUT_PATH}/Shaders/fragment.spv - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/fragment.frag COMMENT "Building fragment shader" )