diff --git a/example/__pycache__/main.cpython-313.pyc b/example/__pycache__/main.cpython-313.pyc deleted file mode 100644 index 7cc1e6c..0000000 Binary files a/example/__pycache__/main.cpython-313.pyc and /dev/null differ diff --git a/macrolibpy/__pycache__/__init__.cpython-313.pyc b/macrolibpy/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..f0d4749 Binary files /dev/null and b/macrolibpy/__pycache__/__init__.cpython-313.pyc differ diff --git a/runtime/Includes/Graphics/Drawable.h b/runtime/Includes/Graphics/Drawable.h index 803fd5f..19b0c61 100644 --- a/runtime/Includes/Graphics/Drawable.h +++ b/runtime/Includes/Graphics/Drawable.h @@ -38,7 +38,7 @@ namespace mlx inline void UpdateDescriptorSet(std::shared_ptr set) { - p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType()); + p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool(set->GetShaderLayout(), set->GetShaderType()).RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType()); } virtual void Bind(std::size_t frame_index, VkCommandBuffer cmd) = 0; diff --git a/runtime/Includes/Renderer/Descriptor.h b/runtime/Includes/Renderer/Descriptor.h index 9a839da..c40b2dd 100644 --- a/runtime/Includes/Renderer/Descriptor.h +++ b/runtime/Includes/Renderer/Descriptor.h @@ -26,9 +26,9 @@ namespace mlx std::shared_ptr RequestDescriptorSet(const ShaderSetLayout& layout, ShaderType shader_type); void ReturnDescriptorSet(std::shared_ptr set); + bool CanAllocate(const ShaderSetLayout& layout, ShaderType shader_type) const; [[nodiscard]] inline VkDescriptorPool Get() const noexcept { return m_pool; } - [[nodiscard]] MLX_FORCEINLINE std::size_t GetNumberOfSetsAllocated() const noexcept { return m_allocation_count; } ~DescriptorPool() = default; @@ -36,7 +36,6 @@ namespace mlx std::vector> m_free_sets; std::vector> m_used_sets; VkDescriptorPool m_pool; - std::size_t m_allocation_count = 0; }; class DescriptorPoolManager @@ -44,7 +43,7 @@ namespace mlx public: DescriptorPoolManager() = default; - DescriptorPool& GetAvailablePool(); + DescriptorPool& GetAvailablePool(const ShaderSetLayout& layout, ShaderType shader_type); void Destroy(); ~DescriptorPoolManager() = default; diff --git a/runtime/Sources/Renderer/Descriptor.cpp b/runtime/Sources/Renderer/Descriptor.cpp index 8be61e0..763ad2c 100644 --- a/runtime/Sources/Renderer/Descriptor.cpp +++ b/runtime/Sources/Renderer/Descriptor.cpp @@ -9,7 +9,7 @@ namespace mlx { - constexpr std::size_t MAX_SETS_PER_POOL = MAX_FRAMES_IN_FLIGHT * 1024; + constexpr std::size_t MAX_SETS_PER_POOL = 1024; void TransitionImageToCorrectLayout(Image& image, VkCommandBuffer cmd) { @@ -26,19 +26,19 @@ namespace mlx { MLX_PROFILE_FUNCTION(); VkDescriptorPoolSize pool_sizes[] = { - { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_SETS_PER_POOL }, - { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, MAX_SETS_PER_POOL }, - { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, MAX_SETS_PER_POOL } + { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_FRAMES_IN_FLIGHT * MAX_SETS_PER_POOL }, + { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, MAX_FRAMES_IN_FLIGHT * MAX_SETS_PER_POOL }, + { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, MAX_FRAMES_IN_FLIGHT * MAX_SETS_PER_POOL } }; VkDescriptorPoolCreateInfo pool_info{}; pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; pool_info.poolSizeCount = sizeof(pool_sizes) / sizeof(pool_sizes[0]); pool_info.pPoolSizes = pool_sizes; - pool_info.maxSets = MAX_SETS_PER_POOL; + pool_info.maxSets = MAX_FRAMES_IN_FLIGHT * MAX_SETS_PER_POOL; pool_info.flags = 0; kvfCheckVk(RenderCore::Get().vkCreateDescriptorPool(RenderCore::Get().GetDevice(), &pool_info, nullptr, &m_pool)); - m_allocation_count = 0; + DebugLog("Vulkan: created new descriptor pool"); } void DescriptorPool::Destroy() noexcept @@ -52,7 +52,6 @@ namespace mlx kvfDestroyDescriptorSetLayout(RenderCore::Get().GetDevice(), set->m_set_layout); RenderCore::Get().vkDestroyDescriptorPool(RenderCore::Get().GetDevice(), m_pool, nullptr); m_pool = VK_NULL_HANDLE; - m_allocation_count = 0; m_free_sets.clear(); m_used_sets.clear(); } @@ -103,7 +102,6 @@ namespace mlx alloc_info.pSetLayouts = &vulkan_layout; VkDescriptorSet vulkan_set; kvfCheckVk(RenderCore::Get().vkAllocateDescriptorSets(RenderCore::Get().GetDevice(), &alloc_info, &vulkan_set)); - m_allocation_count++; vulkan_sets[i] = vulkan_set; } @@ -124,12 +122,23 @@ namespace mlx m_free_sets.push_back(set); } - DescriptorPool& DescriptorPoolManager::GetAvailablePool() + bool DescriptorPool::CanAllocate(const ShaderSetLayout& layout, ShaderType shader_type) const + { + auto it = std::find_if(m_free_sets.begin(), m_free_sets.end(), [&](std::shared_ptr set) + { + return shader_type == set->GetShaderType() && layout == set->GetShaderLayout(); + }); + if(it != m_free_sets.end()) + return true; + return m_used_sets.size() + m_free_sets.size() < MAX_SETS_PER_POOL; + } + + DescriptorPool& DescriptorPoolManager::GetAvailablePool(const ShaderSetLayout& layout, ShaderType shader_type) { MLX_PROFILE_FUNCTION(); for(auto& pool : m_pools) { - if(pool->GetNumberOfSetsAllocated() < MAX_SETS_PER_POOL) + if(pool->CanAllocate(layout, shader_type)) return *pool; } m_pools.emplace_back(std::make_unique())->Init(); diff --git a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp index c3aff3f..59893c7 100644 --- a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp @@ -52,8 +52,8 @@ namespace mlx }; EventBus::RegisterListener({ functor, "mlx_2d_render_pass_" + std::to_string(reinterpret_cast(this)) }); - p_viewer_data_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_vertex_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Vertex); - p_texture_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment); + p_viewer_data_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool(p_vertex_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Vertex).RequestDescriptorSet(p_vertex_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Vertex); + p_texture_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment).RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment); p_viewer_data_buffer = std::make_shared(); p_viewer_data_buffer->Init(sizeof(ViewerData), "mlx_2d_pass_viewer_data"); diff --git a/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp b/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp index b3e5ae8..5518f02 100644 --- a/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp @@ -38,7 +38,7 @@ namespace mlx }; EventBus::RegisterListener({ functor, "mlx_final_pass_" + std::to_string(reinterpret_cast(this)) }); - p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment); + p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment).RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment); } void FinalPass::Pass([[maybe_unused]] Scene& scene, Renderer& renderer, Texture& render_target, NonOwningPtr final_target)