diff options
-rw-r--r-- | src/vk/renderer.cpp | 71 | ||||
-rw-r--r-- | src/vk/renderer.hpp | 1 | ||||
-rw-r--r-- | src/vk/view.cpp | 60 | ||||
-rw-r--r-- | src/vk/view.hpp | 6 |
4 files changed, 87 insertions, 51 deletions
diff --git a/src/vk/renderer.cpp b/src/vk/renderer.cpp index e806716..7531e19 100644 --- a/src/vk/renderer.cpp +++ b/src/vk/renderer.cpp @@ -24,6 +24,48 @@ namespace { void +load_descriptor_pool(void *obj) +{ + auto self = static_cast<VK::Renderer*>(obj); + + uint32_t uniform_buffer_count = 0; + for(auto &view : self->views) + uniform_buffer_count += (view->ub_3d.size() + view->ub_2d.size()); + + VkDescriptorPoolSize descriptor_pool_size{}; + descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptor_pool_size.descriptorCount = uniform_buffer_count; + + VkDescriptorPoolCreateInfo pool_info{}; + pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + pool_info.pNext = nullptr; + pool_info.flags = 0; + pool_info.maxSets = uniform_buffer_count; + pool_info.poolSizeCount = 1; + pool_info.pPoolSizes = &descriptor_pool_size; + + if(vkCreateDescriptorPool( + cg_core.vk_device_with_swapchain->device, &pool_info, nullptr, + &self->descriptor_pool) != VK_SUCCESS) + throw CommandError{"Failed to create a Vulkan descriptor pool."}; + + for(auto &view : self->views) + view->load_descriptor_sets(self->descriptor_pool); +} + +void +unload_descriptor_pool(void *obj) +{ + auto self = static_cast<VK::Renderer*>(obj); + + for(auto &view : self->views) view->unload_descriptor_sets(); + + vkDestroyDescriptorPool( + cg_core.vk_device_with_swapchain->device, self->descriptor_pool, + nullptr); +} + +void load_queue_family(void *obj) { auto self = static_cast<VK::Renderer*>(obj); @@ -54,9 +96,9 @@ unload_command_pool(void *obj) auto self = static_cast<VK::Renderer*>(obj); vkWaitForFences(cg_core.vk_device_with_swapchain->device, - VK::Swapchain::max_frames_in_flight, - cg_core.vk_swapchain->in_flight_fences.data(), VK_TRUE, - std::numeric_limits<uint64_t>::max()); + VK::Swapchain::max_frames_in_flight, + cg_core.vk_swapchain->in_flight_fences.data(), VK_TRUE, + std::numeric_limits<uint64_t>::max()); vkDestroyCommandPool( self->queue_family->device->device, self->command_pool, nullptr); } @@ -83,6 +125,7 @@ load_draw_command_buffer(void *obj) } const CommandChain loader{ + {&load_descriptor_pool, &unload_descriptor_pool}, {&load_queue_family, nullptr}, {&load_command_pool, &unload_command_pool}, {&load_draw_command_buffer, nullptr} @@ -114,19 +157,19 @@ void Renderer::draw() { vkWaitForFences(cg_core.vk_device_with_swapchain->device, 1, - &cg_core.vk_swapchain->in_flight_fences[ - cg_core.vk_swapchain->current_frame], VK_TRUE, - std::numeric_limits<uint64_t>::max()); + &cg_core.vk_swapchain->in_flight_fences[ + cg_core.vk_swapchain->current_frame], VK_TRUE, + std::numeric_limits<uint64_t>::max()); vkResetFences(cg_core.vk_device_with_swapchain->device, 1, &cg_core.vk_swapchain->in_flight_fences[ - cg_core.vk_swapchain->current_frame]); + cg_core.vk_swapchain->current_frame]); uint32_t image_index; vkAcquireNextImageKHR( cg_core.vk_device_with_swapchain->device, cg_core.vk_swapchain->swapchain, std::numeric_limits<uint64_t>::max(), cg_core.vk_swapchain->image_available_semaphores[ - cg_core.vk_swapchain->current_frame], VK_NULL_HANDLE, &image_index); + cg_core.vk_swapchain->current_frame], VK_NULL_HANDLE, &image_index); VkCommandBuffer draw_command_buffer = this->draw_command_buffers[cg_core.vk_swapchain->current_frame]; @@ -171,8 +214,8 @@ Renderer::draw() for(auto &view: this->views) cg_core.vk_graphics_pipeline_3d->draw( - view, draw_command_buffer, cg_core.vk_swapchain->current_frame, - image_index); + view, draw_command_buffer, cg_core.vk_swapchain->current_frame, + image_index); vkCmdEndRenderPass(draw_command_buffer); } @@ -214,12 +257,12 @@ Renderer::draw() VkSemaphore wait_semaphores[]{ cg_core.vk_swapchain->image_available_semaphores[ - cg_core.vk_swapchain->current_frame]}; + cg_core.vk_swapchain->current_frame]}; VkPipelineStageFlags wait_stages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT}; VkSemaphore signal_semaphores[]{ cg_core.vk_swapchain->render_finished_semaphores[ - cg_core.vk_swapchain->current_frame]}; + cg_core.vk_swapchain->current_frame]}; VkSubmitInfo submit_info{}; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; @@ -233,8 +276,8 @@ Renderer::draw() submit_info.pSignalSemaphores = signal_semaphores; if(vkQueueSubmit( - queue.queue, 1, &submit_info, cg_core.vk_swapchain->in_flight_fences[ - cg_core.vk_swapchain->current_frame]) != VK_SUCCESS) + queue.queue, 1, &submit_info, cg_core.vk_swapchain->in_flight_fences[ + cg_core.vk_swapchain->current_frame]) != VK_SUCCESS) throw std::runtime_error{"Failed to submit draw command buffer."}; VkSwapchainKHR swap_chains[]{cg_core.vk_swapchain->swapchain}; diff --git a/src/vk/renderer.hpp b/src/vk/renderer.hpp index 4cd7739..2b913fe 100644 --- a/src/vk/renderer.hpp +++ b/src/vk/renderer.hpp @@ -36,6 +36,7 @@ struct Renderer std::shared_ptr<Model>, std::vector<ModelInstance>>> models_to_draw; + VkDescriptorPool descriptor_pool; std::vector<std::shared_ptr<View>> views; QueueFamily *queue_family; VkCommandPool command_pool; diff --git a/src/vk/view.cpp b/src/vk/view.cpp index 16ee60c..b4ef722 100644 --- a/src/vk/view.cpp +++ b/src/vk/view.cpp @@ -76,41 +76,6 @@ unload_2d_uniform_buffer(void *obj) } void -load_descriptor_pool(void *obj) -{ - auto self = static_cast<VK::View*>(obj); - - uint32_t uniform_buffer_count = self->ub_3d.size() + self->ub_2d.size(); - - VkDescriptorPoolSize descriptor_pool_size{}; - descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptor_pool_size.descriptorCount = uniform_buffer_count; - - VkDescriptorPoolCreateInfo pool_info{}; - pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - pool_info.pNext = nullptr; - pool_info.flags = 0; - pool_info.maxSets = uniform_buffer_count; - pool_info.poolSizeCount = 1; - pool_info.pPoolSizes = &descriptor_pool_size; - - if(vkCreateDescriptorPool( - cg_core.vk_device_with_swapchain->device, &pool_info, nullptr, - &self->descriptor_pool) != VK_SUCCESS) - throw CommandError{"Failed to create a Vulkan descriptor pool."}; -} - -void -unload_descriptor_pool(void *obj) -{ - auto self = static_cast<VK::View*>(obj); - - vkDestroyDescriptorPool( - cg_core.vk_device_with_swapchain->device, self->descriptor_pool, - nullptr); -} - -void load_descriptor_sets_3d(void *obj) { auto self = static_cast<VK::View*>(obj); @@ -228,9 +193,11 @@ load_resources_to_descriptor_sets_2d(void *obj) const CommandChain loader{ {&load_3d_uniform_buffer, &unload_3d_uniform_buffer}, - {&load_2d_uniform_buffer, &unload_2d_uniform_buffer}, - {&load_descriptor_pool, &unload_descriptor_pool}, + {&load_2d_uniform_buffer, &unload_2d_uniform_buffer} // By destroying the pool the sets are also destroyed. +}; + +const CommandChain descriptor_sets_loader{ {&load_descriptor_sets_3d, nullptr}, {&load_descriptor_sets_2d, nullptr}, {&load_resources_to_descriptor_sets_3d, nullptr}, @@ -244,6 +211,7 @@ namespace VK View::View(glm::vec4 region): region{region}, + descriptor_pool{VK_NULL_HANDLE}, camera_position{std::make_shared<glm::vec3>(0.0f, 0.0f, 0.0f)}, camera_rotation{std::make_shared<glm::vec3>(0.0f, 0.0f, 0.0f)}, sprites_to_draw{cg_core.vk_swapchain->images_count} @@ -256,4 +224,22 @@ View::~View() loader.revert(this); } +void +View::load_descriptor_sets(VkDescriptorPool descriptor_pool) +{ + if(this->descriptor_pool != VK_NULL_HANDLE) return; + + this->descriptor_pool = descriptor_pool; + descriptor_sets_loader.execute(this); +} + +void +View::unload_descriptor_sets() +{ + if(this->descriptor_pool == VK_NULL_HANDLE) return; + + this->descriptor_pool = VK_NULL_HANDLE; + descriptor_sets_loader.revert(this); +} + } diff --git a/src/vk/view.hpp b/src/vk/view.hpp index 3936413..5d133a9 100644 --- a/src/vk/view.hpp +++ b/src/vk/view.hpp @@ -48,6 +48,12 @@ struct View View(glm::vec4 region); ~View(); + + void + load_descriptor_sets(VkDescriptorPool descriptor_pool); + + void + unload_descriptor_sets(); }; } |