From 09dda7038ad78b5305169de7fdae74bdfe53eff6 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Fri, 26 Feb 2021 09:19:00 +0100 Subject: Store bindings --- include/vk-api.hpp | 1 + include/vk-renderer.hpp | 3 +++ src/vk-api.cpp | 4 ++++ src/vk-renderer.cpp | 39 ++++++++------------------------------- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 67a5855..feefdd9 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -98,6 +98,7 @@ class vkapi { void destroy_swapchain(Swapchain& swapchain); void update_descriptor_set_buffer(VkDescriptorSet set, VkDescriptorSetLayoutBinding binding, Buffer& buffer); + void update_descriptor_set_buffer(VkDescriptorSet set, VkDescriptorSetLayoutBinding binding, Buffer& buffer, VkDeviceSize offset, VkDeviceSize range); void update_descriptor_set_image(VkDescriptorSet set, VkDescriptorSetLayoutBinding binding, VkImageView view); diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 7a804b5..9a0df94 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -41,9 +41,12 @@ class vkrenderer { std::vector command_buffers; + // TODO: Maybe use a map in the future + std::vector compute_sets_bindings; std::vector compute_shader_sets; Pipeline compute_pipeline; + std::vector ui_sets_bindings; std::vector>ui_vertex_buffers; std::vector>ui_index_buffers; std::vector ui_sets; diff --git a/src/vk-api.cpp b/src/vk-api.cpp index ca72862..f051025 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -742,6 +742,10 @@ void vkapi::destroy_swapchain(Swapchain& swapchain) { void vkapi::update_descriptor_set_buffer(VkDescriptorSet set, VkDescriptorSetLayoutBinding binding, Buffer& buffer) { + update_descriptor_set_buffer(set, binding, buffer, 0, VK_WHOLE_SIZE); +} + +void vkapi::update_descriptor_set_buffer(VkDescriptorSet set, VkDescriptorSetLayoutBinding binding, Buffer& buffer, VkDeviceSize offset, VkDeviceSize range) { VkDescriptorBufferInfo descriptor_buffer_info = {}; descriptor_buffer_info.buffer = buffer.handle; descriptor_buffer_info.offset = 0; diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index e886a74..6cd1616 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -22,7 +22,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { std::nullopt ); - std::vector bindings { + compute_sets_bindings = { { .binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, @@ -45,7 +45,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { .pImmutableSamplers = VK_NULL_HANDLE, } }; - compute_pipeline = api.create_compute_pipeline("compute", bindings); + compute_pipeline = api.create_compute_pipeline("compute", compute_sets_bindings); compute_shader_sets = api.create_descriptor_sets(compute_pipeline.descriptor_set_layout, swapchain.image_count); command_buffers = api.create_command_buffers(swapchain.image_count); @@ -65,7 +65,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { std::memcpy(compute_shader_buffer.mapped_ptr, &inputs, sizeof(inputs)); for (size_t index = 0; index < swapchain.image_count; index++) { - api.update_descriptor_set_buffer(compute_shader_sets[index], bindings[0], compute_shader_buffer); + api.update_descriptor_set_buffer(compute_shader_sets[index], compute_sets_bindings[0], compute_shader_buffer); } std::vector attachments_format { swapchain.surface_format.format }; @@ -76,7 +76,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { framebuffers.push_back(api.create_framebuffer(render_pass, attachments, swapchain.extent)); } - std::vector ui_pipeline_bindings { + ui_sets_bindings = { { .binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, @@ -85,7 +85,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { .pImmutableSamplers = VK_NULL_HANDLE, }, }; - ui_pipeline = api.create_graphics_pipeline("ui", ui_pipeline_bindings, (VkShaderStageFlagBits)(VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT), render_pass); + ui_pipeline = api.create_graphics_pipeline("ui", ui_sets_bindings, (VkShaderStageFlagBits)(VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT), render_pass); ui_sets = api.create_descriptor_sets(ui_pipeline.descriptor_set_layout, swapchain.image_count); ImGui::CreateContext(); @@ -210,23 +210,8 @@ void vkrenderer::begin_frame() { auto output_image = accumulation_images[0]; auto accumulation_image = accumulation_images[1]; - VkDescriptorSetLayoutBinding images[2] { - { - .binding = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, - .pImmutableSamplers = VK_NULL_HANDLE, - }, { - .binding = 2, - .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, - .pImmutableSamplers = VK_NULL_HANDLE, - } - }; - api.update_descriptor_set_image(compute_shader_sets[swapchain_image_index], images[0], output_image.view); - api.update_descriptor_set_image(compute_shader_sets[swapchain_image_index], images[1], accumulation_image.view); + api.update_descriptor_set_image(compute_shader_sets[swapchain_image_index], compute_sets_bindings[1], output_image.view); + api.update_descriptor_set_image(compute_shader_sets[swapchain_image_index], compute_sets_bindings[2], accumulation_image.view); api.start_record(cmd_buf); @@ -261,18 +246,10 @@ void vkrenderer::ui() { vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_pipeline.handle); - VkDescriptorSetLayoutBinding binding = { - .binding = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, - .pImmutableSamplers = VK_NULL_HANDLE, - }; - for (size_t cmd_index = 0; cmd_index < draw_data->CmdListsCount; cmd_index++) { ImDrawList* draw_list = draw_data->CmdLists[cmd_index]; - api.update_descriptor_set_buffer(ui_sets[frame_index], binding, ui_vertex_buffers[frame_index][cmd_index]); + api.update_descriptor_set_buffer(ui_sets[frame_index], ui_sets_bindings[0], ui_vertex_buffers[frame_index][cmd_index]); vkCmdBindIndexBuffer(cmd_buf, ui_index_buffers[frame_index][cmd_index].handle, 0, VK_INDEX_TYPE_UINT16); -- cgit v1.3