From 392bdbf78b688a9843e4e042efdacb775d870072 Mon Sep 17 00:00:00 2001 From: Benau Date: Wed, 30 Nov 2022 10:41:39 +0800 Subject: [PATCH] Move m_current_buffer_idx to GEVulkanDriver --- .../include/ge_vulkan_driver.hpp | 2 ++ .../src/ge_vulkan_draw_call.cpp | 29 +++++++++---------- .../src/ge_vulkan_draw_call.hpp | 4 +-- lib/graphics_engine/src/ge_vulkan_driver.cpp | 4 ++- .../src/ge_vulkan_scene_manager.cpp | 2 +- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/graphics_engine/include/ge_vulkan_driver.hpp b/lib/graphics_engine/include/ge_vulkan_driver.hpp index ba113538f..9b524c3c6 100644 --- a/lib/graphics_engine/include/ge_vulkan_driver.hpp +++ b/lib/graphics_engine/include/ge_vulkan_driver.hpp @@ -365,6 +365,7 @@ namespace GE void addDrawCallToCache(std::unique_ptr& dc); std::unique_ptr getDrawCallFromCache(); GESPM* getBillboardQuad() const { return m_billboard_quad; } + int getCurrentBufferIdx() const { return m_current_buffer_idx; } private: struct SwapChainSupportDetails { @@ -509,6 +510,7 @@ namespace GE std::vector > m_draw_calls_cache; GESPM* m_billboard_quad; + int m_current_buffer_idx; void createInstance(SDL_Window* window); void findPhysicalDevice(); diff --git a/lib/graphics_engine/src/ge_vulkan_draw_call.cpp b/lib/graphics_engine/src/ge_vulkan_draw_call.cpp index 278d4568c..3609e0d81 100644 --- a/lib/graphics_engine/src/ge_vulkan_draw_call.cpp +++ b/lib/graphics_engine/src/ge_vulkan_draw_call.cpp @@ -189,7 +189,6 @@ GEVulkanDrawCall::GEVulkanDrawCall() m_skinning_data_padded_size = 0; m_materials_padded_size = 0; m_update_data_descriptor_sets = true; - m_current_buffer_idx = 0; m_data_layout = VK_NULL_HANDLE; m_descriptor_pool = VK_NULL_HANDLE; m_pipeline_layout = VK_NULL_HANDLE; @@ -283,7 +282,7 @@ void GEVulkanDrawCall::addBillboardNode(irr::scene::ISceneNode* node, } // addBillboardNode // ---------------------------------------------------------------------------- -void GEVulkanDrawCall::generate() +void GEVulkanDrawCall::generate(GEVulkanDriver* vk) { if (!m_visible_nodes.empty() && m_data_layout == VK_NULL_HANDLE) createVulkanData(); @@ -330,8 +329,9 @@ start: m_update_data_descriptor_sets = m_sbo_data->resizeIfNeeded(min_size) || m_update_data_descriptor_sets; + int current_buffer_idx = vk->getCurrentBufferIdx(); uint8_t* mapped_addr = (uint8_t*)m_sbo_data->getMappedAddr() - [m_current_buffer_idx]; + [current_buffer_idx]; std::unordered_map skinning_offets; int added_joint = 1; @@ -1255,14 +1255,15 @@ void GEVulkanDrawCall::uploadDynamicData(GEVulkanDriver* vk, } dst_stage |= VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT; } + int current_buffer_idx = vk->getCurrentBufferIdx(); m_update_data_descriptor_sets = m_dynamic_data->setCurrentData(data_uploading, cmd, - m_current_buffer_idx) || m_update_data_descriptor_sets; + current_buffer_idx) || m_update_data_descriptor_sets; const size_t whole_size = m_skinning_data_padded_size + m_object_data_padded_size + m_materials_padded_size; vmaFlushAllocation(vk->getVmaAllocator(), - m_sbo_data->getHostMemory()[m_current_buffer_idx], 0, + m_sbo_data->getHostMemory()[current_buffer_idx], 0, whole_size); if (!GEVulkanDynamicBuffer::supportsHostTransfer()) @@ -1288,6 +1289,7 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, VkCommandBuffer cmd = custom_cmd ? custom_cmd : vk->getCurrentCommandBuffer(); + int current_buffer_idx = vk->getCurrentBufferIdx(); const bool use_base_vertex = GEVulkanFeatures::supportsBaseVertexRendering(); const bool bind_mesh_textures = GEVulkanFeatures::supportsBindMeshTexturesAtOnce(); @@ -1354,8 +1356,8 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, unsigned draw_count = 0; VkBuffer indirect_buffer = GEVulkanDynamicBuffer::supportsHostTransfer() ? - m_dynamic_data->getHostBuffer()[m_current_buffer_idx] : - m_dynamic_data->getLocalBuffer()[m_current_buffer_idx]; + m_dynamic_data->getHostBuffer()[current_buffer_idx] : + m_dynamic_data->getLocalBuffer()[current_buffer_idx]; for (unsigned i = 0; i < m_cmds.size(); i++) { if (m_cmds[i].m_shader != cur_pipeline) @@ -1364,7 +1366,7 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, material_offsets[3] = m_materials_data[cur_pipeline].first; vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline_layout, 1, 1, - &m_data_descriptor_sets[m_current_buffer_idx], + &m_data_descriptor_sets[current_buffer_idx], material_offsets.size(), material_offsets.data()); vkCmdDrawIndexedIndirect(cmd, indirect_buffer, indirect_offset, draw_count, indirect_size); @@ -1389,7 +1391,7 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, material_offsets[3] = m_materials_data[m_cmds.back().m_shader].first; vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline_layout, 1, 1, - &m_data_descriptor_sets[m_current_buffer_idx], + &m_data_descriptor_sets[current_buffer_idx], material_offsets.size(), material_offsets.data()); vkCmdDrawIndexedIndirect(cmd, indirect_buffer, indirect_offset, draw_count, indirect_size); @@ -1405,7 +1407,7 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, { vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline_layout, 1, 1, - &m_data_descriptor_sets[m_current_buffer_idx], 0, NULL); + &m_data_descriptor_sets[current_buffer_idx], 0, NULL); } for (unsigned i = 0; i < m_cmds.size(); i++) { @@ -1423,7 +1425,7 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, { vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline_layout, 1, - 1, &m_data_descriptor_sets[m_current_buffer_idx], 0, + 1, &m_data_descriptor_sets[current_buffer_idx], 0, NULL); } } @@ -1452,7 +1454,7 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, }}; vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline_layout, 1, 1, - &m_data_descriptor_sets[m_current_buffer_idx], + &m_data_descriptor_sets[current_buffer_idx], dynamic_offsets.size(), dynamic_offsets.data()); m_cmds[i].m_mb->bindVertexIndexBuffer(cmd); } @@ -1463,9 +1465,6 @@ void GEVulkanDrawCall::render(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, } if (!drawn_skybox) GEVulkanSkyBoxRenderer::render(cmd, cam); - - m_current_buffer_idx = (m_current_buffer_idx + 1) % - (GEVulkanDriver::getMaxFrameInFlight() + 1); } // render // ---------------------------------------------------------------------------- diff --git a/lib/graphics_engine/src/ge_vulkan_draw_call.hpp b/lib/graphics_engine/src/ge_vulkan_draw_call.hpp index 6c6547cc7..3efae3052 100644 --- a/lib/graphics_engine/src/ge_vulkan_draw_call.hpp +++ b/lib/graphics_engine/src/ge_vulkan_draw_call.hpp @@ -132,8 +132,6 @@ private: size_t m_materials_padded_size; - int m_current_buffer_idx; - bool m_update_data_descriptor_sets; VkDescriptorSetLayout m_data_layout; @@ -203,7 +201,7 @@ public: // ------------------------------------------------------------------------ void prepare(GEVulkanCameraSceneNode* cam); // ------------------------------------------------------------------------ - void generate(); + void generate(GEVulkanDriver* vk); // ------------------------------------------------------------------------ void uploadDynamicData(GEVulkanDriver* vk, GEVulkanCameraSceneNode* cam, VkCommandBuffer custom_cmd = VK_NULL_HANDLE); diff --git a/lib/graphics_engine/src/ge_vulkan_driver.cpp b/lib/graphics_engine/src/ge_vulkan_driver.cpp index a346d1fc0..1883b6dad 100644 --- a/lib/graphics_engine/src/ge_vulkan_driver.cpp +++ b/lib/graphics_engine/src/ge_vulkan_driver.cpp @@ -504,7 +504,7 @@ GEVulkanDriver::GEVulkanDriver(const SIrrlichtCreationParameters& params, m_depth_texture(NULL), m_mesh_texture_descriptor(NULL), m_rtt_texture(NULL), m_prev_rtt_texture(NULL), m_separate_rtt_texture(NULL), m_rtt_polycount(0), - m_billboard_quad(NULL) + m_billboard_quad(NULL), m_current_buffer_idx(0) { m_vk.reset(new VK()); m_physical_device = VK_NULL_HANDLE; @@ -1785,6 +1785,8 @@ bool GEVulkanDriver::endScene() present_info.pImageIndices = &m_image_index; m_current_frame = (m_current_frame + 1) % getMaxFrameInFlight(); + m_current_buffer_idx = + (m_current_buffer_idx + 1) % (getMaxFrameInFlight() + 1); if (m_present_queue) result = vkQueuePresentKHR(m_present_queue, &present_info); diff --git a/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp b/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp index a1cfe763d..36b8c9754 100644 --- a/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp +++ b/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp @@ -170,7 +170,7 @@ void GEVulkanSceneManager::drawAllInternal() it->second->prepare(cam); OnRegisterSceneNode(); - it->second->generate(); + it->second->generate(static_cast(getVideoDriver())); } } // drawAllInternal