Add create comand_pool and command_buffers. Fix CMake file for TestGame

This commit is contained in:
Jiga228
2025-09-29 20:00:42 +07:00
parent 958e13c6f5
commit 47bb1541ef
3 changed files with 41 additions and 5 deletions
+33 -2
View File
@@ -394,7 +394,7 @@ void RenderEngine::create_render_pass()
VK_CHECK(vkCreateRenderPass(device_, &render_pass_info, nullptr, &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); std::ifstream vertex_shader_file("Shaders/vertex.spv", std::ios::binary);
if (!vertex_shader_file.is_open()) 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<uint32_t>(command_buffers_.size());
VK_CHECK(vkAllocateCommandBuffers(device_, &command_buffer_allocate_info, command_buffers_.data()));
}
void RenderEngine::create_surface() void RenderEngine::create_surface()
{ {
VK_CHECK(glfwCreateWindowSurface(instance_, window_, nullptr, &surface_)); VK_CHECK(glfwCreateWindowSurface(instance_, window_, nullptr, &surface_));
@@ -569,11 +594,17 @@ window_(window)
create_swapchain(desired_present_mode); create_swapchain(desired_present_mode);
create_image_views(); create_image_views();
create_render_pass(); create_render_pass();
create_graphycs_pipeline(); create_graphics_pipeline();
} }
RenderEngine::~RenderEngine() RenderEngine::~RenderEngine()
{ {
if (command_buffers_.size() > 0)
vkFreeCommandBuffers(device_, command_pool_, static_cast<uint32_t>(command_buffers_.size()), command_buffers_.data());
if (command_pool_ != VK_NULL_HANDLE)
vkDestroyCommandPool(device_, command_pool_, nullptr);
for (auto& i : framebuffers_) for (auto& i : framebuffers_)
vkDestroyFramebuffer(device_, i, nullptr); vkDestroyFramebuffer(device_, i, nullptr);
+8 -1
View File
@@ -35,6 +35,9 @@ class RenderEngine
static const std::vector<const char*> deviceExtensions; static const std::vector<const char*> deviceExtensions;
const int MAX_FRAMES_IN_FLIGHT = 2;
int current_frame_ = 0;
CoreInstance& core_; CoreInstance& core_;
GLFWwindow* window_; GLFWwindow* window_;
VkInstance instance_ = VK_NULL_HANDLE; VkInstance instance_ = VK_NULL_HANDLE;
@@ -52,6 +55,8 @@ class RenderEngine
VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE; VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE;
VkPipeline graphycs_pipeline_ = VK_NULL_HANDLE; VkPipeline graphycs_pipeline_ = VK_NULL_HANDLE;
std::vector<VkFramebuffer> framebuffers_; std::vector<VkFramebuffer> framebuffers_;
VkCommandPool command_pool_ = VK_NULL_HANDLE;
std::vector<VkCommandBuffer> command_buffers_;
#ifdef _DEBUG #ifdef _DEBUG
VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info); VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info);
@@ -75,8 +80,10 @@ class RenderEngine
void create_swapchain(VkPresentModeKHR desired_present_mode); void create_swapchain(VkPresentModeKHR desired_present_mode);
void create_image_views(); void create_image_views();
void create_render_pass(); void create_render_pass();
void create_graphycs_pipeline(); void create_graphics_pipeline();
void create_framebuffers(); void create_framebuffers();
void create_command_pool();
void allocate_command_buffers();
public: public:
// Can throw the exception // Can throw the exception
-2
View File
@@ -59,7 +59,6 @@ add_custom_command(
COMMAND $ENV{VULKAN_SDK}/Bin/glslc.exe COMMAND $ENV{VULKAN_SDK}/Bin/glslc.exe
${CMAKE_CURRENT_SOURCE_DIR}/Shaders/vertex.vert ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/vertex.vert
-o ${OUTPUT_PATH}/Shaders/vertex.spv -o ${OUTPUT_PATH}/Shaders/vertex.spv
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/vertex.vert
COMMENT "Building vertex shader" COMMENT "Building vertex shader"
) )
@@ -69,6 +68,5 @@ add_custom_command(
COMMAND $ENV{VULKAN_SDK}/Bin/glslc.exe COMMAND $ENV{VULKAN_SDK}/Bin/glslc.exe
${CMAKE_CURRENT_SOURCE_DIR}/Shaders/fragment.frag ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/fragment.frag
-o ${OUTPUT_PATH}/Shaders/fragment.spv -o ${OUTPUT_PATH}/Shaders/fragment.spv
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Shaders/fragment.frag
COMMENT "Building fragment shader" COMMENT "Building fragment shader"
) )