Skip to content

Commit

Permalink
fixing window resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Dec 4, 2024
1 parent 17fcb85 commit ec2c444
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 61 deletions.
2 changes: 1 addition & 1 deletion runtime/Includes/Core/Application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Error("invalid window ptr (NULL)"); \
return; \
} \
else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr<GraphicsSupport>& gs){ return *static_cast<int*>(win) == gs->GetID(); }) == m_graphics.end()) \
else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr<GraphicsSupport>& gs){ return gs && *static_cast<int*>(win) == gs->GetID(); }) == m_graphics.end()) \
{ \
Error("invalid window ptr"); \
return; \
Expand Down
4 changes: 2 additions & 2 deletions runtime/Includes/Renderer/Pipelines/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace mlx
GraphicPipeline() = default;

void Init(const GraphicPipelineDescriptor& descriptor, std::string_view debug_name);
bool BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array<float, 4> clear) noexcept;
void EndPipeline(VkCommandBuffer command_buffer) noexcept override;
bool BindPipeline(VkCommandBuffer cmd, std::size_t framebuffer_index, std::array<float, 4> clear) noexcept;
void EndPipeline(VkCommandBuffer cmd) noexcept override;
void Destroy() noexcept;

[[nodiscard]] inline VkPipeline GetPipeline() const override { return m_pipeline; }
Expand Down
15 changes: 10 additions & 5 deletions runtime/Sources/Core/Logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ namespace mlx

switch(type)
{
case LogType::Debug: std::cout << Ansi::blue << "[MLX Debug] " << Ansi::def << code_infos << message << std::endl; break;
case LogType::Message: std::cout << Ansi::blue << "[MLX Message] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::Warning: std::cout << Ansi::magenta << "[MLX Warning] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::Error: std::cerr << Ansi::red << "[MLX Error] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::FatalError: std::cerr << Ansi::red << "[MLX Fatal Error] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::Debug: std::cout << Ansi::blue << "[MLX Debug] "; break;
case LogType::Message: std::cout << Ansi::blue << "[MLX Message] "; break;
case LogType::Warning: std::cout << Ansi::magenta << "[MLX Warning] "; break;
case LogType::Error: std::cerr << Ansi::red << "[MLX Error] "; break;
case LogType::FatalError: std::cerr << Ansi::red << "[MLX Fatal Error] "; break;

default: break;
}

const std::chrono::zoned_time current_time{ std::chrono::current_zone(), std::chrono::floor<std::chrono::milliseconds>(std::chrono::system_clock::now()) };

std::cout << Ansi::yellow << std::format("[{0:%H:%M:%S}] ", current_time) << Ansi::def << code_infos << message << std::endl;

if(type == LogType::FatalError)
{
std::cout << Ansi::bg_red << "Fatal Error: emergency exit" << Ansi::bg_def << std::endl;
Expand Down
10 changes: 1 addition & 9 deletions runtime/Sources/Renderer/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@ namespace mlx
{
MLX_PROFILE_FUNCTION();
VkDescriptorPoolSize pool_sizes[] = {
{ VK_DESCRIPTOR_TYPE_SAMPLER, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 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_UNIFORM_BUFFER_DYNAMIC, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, MAX_SETS_PER_POOL },
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, MAX_SETS_PER_POOL }
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, MAX_SETS_PER_POOL }
};

VkDescriptorPoolCreateInfo pool_info{};
Expand Down
54 changes: 35 additions & 19 deletions runtime/Sources/Renderer/Pipelines/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ namespace mlx
kvfGPipelineBuilderSetVertexInputs(builder, binding_description, attributes_description.data(), attributes_description.size());
}

m_pipeline = kvfCreateGraphicsPipeline(RenderCore::Get().GetDevice(), m_pipeline_layout, builder, m_renderpass);
DebugLog("Vulkan: graphics pipeline created");
m_pipeline = kvfCreateGraphicsPipeline(RenderCore::Get().GetDevice(), VK_NULL_HANDLE, m_pipeline_layout, builder, m_renderpass);
#ifdef DEBUG
DebugLog("Vulkan: graphics pipeline created %", m_debug_name);
#endif
kvfDestroyGPipelineBuilder(builder);

#ifdef MLX_HAS_DEBUG_UTILS_FUNCTIONS
Expand Down Expand Up @@ -82,10 +84,10 @@ namespace mlx
#endif
}

bool GraphicPipeline::BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array<float, 4> clear) noexcept
bool GraphicPipeline::BindPipeline(VkCommandBuffer cmd, std::size_t framebuffer_index, std::array<float, 4> clear) noexcept
{
MLX_PROFILE_FUNCTION();
TransitionAttachments(command_buffer);
TransitionAttachments(cmd);
VkFramebuffer fb = m_framebuffers[framebuffer_index];
VkExtent2D fb_extent = kvfGetFramebufferSize(fb);

Expand All @@ -96,12 +98,12 @@ namespace mlx
viewport.height = fb_extent.height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
RenderCore::Get().vkCmdSetViewport(command_buffer, 0, 1, &viewport);
RenderCore::Get().vkCmdSetViewport(cmd, 0, 1, &viewport);

VkRect2D scissor{};
scissor.offset = { 0, 0 };
scissor.extent = fb_extent;
RenderCore::Get().vkCmdSetScissor(command_buffer, 0, 1, &scissor);
RenderCore::Get().vkCmdSetScissor(cmd, 0, 1, &scissor);

for(std::size_t i = 0; i < m_clears.size(); i++)
{
Expand All @@ -111,41 +113,49 @@ namespace mlx
m_clears[i].color.float32[3] = clear[3];
}

kvfBeginRenderPass(m_renderpass, command_buffer, fb, fb_extent, m_clears.data(), m_clears.size());
RenderCore::Get().vkCmdBindPipeline(command_buffer, GetPipelineBindPoint(), GetPipeline());
kvfBeginRenderPass(m_renderpass, cmd, fb, fb_extent, m_clears.data(), m_clears.size());
RenderCore::Get().vkCmdBindPipeline(cmd, GetPipelineBindPoint(), GetPipeline());
return true;
}

void GraphicPipeline::EndPipeline(VkCommandBuffer command_buffer) noexcept
void GraphicPipeline::EndPipeline(VkCommandBuffer cmd) noexcept
{
MLX_PROFILE_FUNCTION();
RenderCore::Get().vkCmdEndRenderPass(command_buffer);
RenderCore::Get().vkCmdEndRenderPass(cmd);
}

void GraphicPipeline::Destroy() noexcept
{
MLX_PROFILE_FUNCTION();
p_vertex_shader.reset();
p_fragment_shader.reset();
for(auto& fb : m_framebuffers)
for(auto fb : m_framebuffers)
{
kvfDestroyFramebuffer(RenderCore::Get().GetDevice(), fb);
DebugLog("Vulkan: framebuffer destroyed");
#ifdef DEBUG
DebugLog("Vulkan: framebuffer destroyed in %", m_debug_name);
#endif
}
m_framebuffers.clear();

kvfDestroyPipelineLayout(RenderCore::Get().GetDevice(), m_pipeline_layout);
m_pipeline_layout = VK_NULL_HANDLE;
DebugLog("Vulkan: graphics pipeline layout destroyed");
#ifdef DEBUG
DebugLog("Vulkan: graphics pipeline layout destroyed %", m_debug_name);
#endif

kvfDestroyRenderPass(RenderCore::Get().GetDevice(), m_renderpass);
m_renderpass = VK_NULL_HANDLE;
DebugLog("Vulkan: renderpass destroyed");
#ifdef DEBUG
DebugLog("Vulkan: renderpass destroyed for %", m_debug_name);
#endif

kvfDestroyPipeline(RenderCore::Get().GetDevice(), m_pipeline);
m_pipeline = VK_NULL_HANDLE;
DebugLog("Vulkan: graphics pipeline destroyed");

#ifdef DEBUG
DebugLog("Vulkan: graphics pipeline destroyed %", m_debug_name);
#endif

p_renderer = nullptr;
m_clears.clear();
m_attachments.clear();
Expand Down Expand Up @@ -192,21 +202,27 @@ namespace mlx
m_renderpass = kvfCreateRenderPassWithSubpassDependencies(RenderCore::Get().GetDevice(), attachments.data(), attachments.size(), GetPipelineBindPoint(), dependencies.data(), dependencies.size());
m_clears.clear();
m_clears.resize(attachments.size());
DebugLog("Vulkan: renderpass created");
#ifdef DEBUG
DebugLog("Vulkan: renderpass created for %", m_debug_name);
#endif

if(p_renderer)
{
for(const Image& image : p_renderer->GetSwapchain().GetSwapchainImages())
{
attachment_views[0] = image.GetImageView();
m_framebuffers.push_back(kvfCreateFramebuffer(RenderCore::Get().GetDevice(), m_renderpass, attachment_views.data(), attachment_views.size(), { .width = image.GetWidth(), .height = image.GetHeight() }));
DebugLog("Vulkan: framebuffer created");
#ifdef DEBUG
DebugLog("Vulkan: framebuffer created for %", m_debug_name);
#endif
}
}
for(NonOwningPtr<Texture> image : render_targets)
{
m_framebuffers.push_back(kvfCreateFramebuffer(RenderCore::Get().GetDevice(), m_renderpass, attachment_views.data(), attachment_views.size(), { .width = image->GetWidth(), .height = image->GetHeight() }));
DebugLog("Vulkan: framebuffer created");
#ifdef DEBUG
DebugLog("Vulkan: framebuffer created for %", m_debug_name);
#endif
}
}

Expand Down
7 changes: 5 additions & 2 deletions runtime/Sources/Renderer/RenderPasses/2DPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace mlx
if(event.What() == Event::ResizeEventCode)
m_pipeline.Destroy();
};
EventBus::RegisterListener({ functor, "mlx_2d_render_pass" });
EventBus::RegisterListener({ functor, "mlx_2d_render_pass_" + std::to_string(reinterpret_cast<std::uintptr_t>(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);
Expand All @@ -76,7 +76,10 @@ namespace mlx
pipeline_descriptor.color_attachments = { &render_target };
pipeline_descriptor.clear_color_attachments = false;
#ifdef DEBUG
m_pipeline.Init(pipeline_descriptor, "mlx_2D_pass");
if(renderer.GetWindow())
m_pipeline.Init(pipeline_descriptor, "mlx_2D_pass_" + renderer.GetWindow()->GetName());
else
m_pipeline.Init(pipeline_descriptor, "mlx_2D_pass");
#else
m_pipeline.Init(pipeline_descriptor, {});
#endif
Expand Down
7 changes: 5 additions & 2 deletions runtime/Sources/Renderer/RenderPasses/FinalPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace mlx
if(event.What() == Event::ResizeEventCode)
m_pipeline.Destroy();
};
EventBus::RegisterListener({ functor, "mlx_final_pass" });
EventBus::RegisterListener({ functor, "mlx_final_pass_" + std::to_string(reinterpret_cast<std::uintptr_t>(this)) });

p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment);
}
Expand All @@ -55,7 +55,10 @@ namespace mlx
pipeline_descriptor.renderer = &renderer;
pipeline_descriptor.no_vertex_inputs = true;
#ifdef DEBUG
m_pipeline.Init(pipeline_descriptor, "mlx_final_pass");
if(final_target)
m_pipeline.Init(pipeline_descriptor, "mlx_final_pass");
else
m_pipeline.Init(pipeline_descriptor, "mlx_final_pass_" + renderer.GetWindow()->GetName());
#else
m_pipeline.Init(pipeline_descriptor, {});
#endif
Expand Down
3 changes: 1 addition & 2 deletions runtime/Sources/Renderer/Swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace mlx
if(m_resize)
{
RenderCore::Get().WaitDeviceIdle();
Destroy();
CreateSwapchain();
EventBus::SendBroadcast(Internal::ResizeEventBroadcast{});
}
Expand Down Expand Up @@ -76,8 +77,6 @@ namespace mlx
extent = { size.x, size.y };
} while(extent.width == 0 || extent.height == 0);

Destroy();

m_surface = p_window->CreateVulkanSurface(RenderCore::Get().GetInstance());
DebugLog("Vulkan: surface created");
m_swapchain = kvfCreateSwapchainKHR(RenderCore::Get().GetDevice(), RenderCore::Get().GetPhysicalDevice(), m_surface, extent, VK_NULL_HANDLE, false);
Expand Down
Loading

0 comments on commit ec2c444

Please sign in to comment.