From c905399f1f8b255ae858da730f893911efb7679d Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sun, 25 Aug 2024 10:09:18 +0200 Subject: [PATCH] fix: ImGui issues --- .../Latte/Renderer/Metal/MetalLayerHandle.cpp | 20 -------- .../Latte/Renderer/Metal/MetalLayerHandle.h | 7 --- .../HW/Latte/Renderer/Metal/MetalRenderer.cpp | 48 ++++++++++++++----- src/imgui/imgui_impl_metal.mm | 6 +-- 4 files changed, 37 insertions(+), 44 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.cpp index 9349dc895..f4d4490e1 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.cpp @@ -16,8 +16,6 @@ MetalLayerHandle::~MetalLayerHandle() { if (m_layer) m_layer->release(); - if (m_renderPassDescriptor) - m_renderPassDescriptor->release(); } void MetalLayerHandle::Resize(const Vector2i& size) @@ -37,27 +35,9 @@ bool MetalLayerHandle::AcquireDrawable() return false; } - if (m_renderPassDescriptor) - { - m_renderPassDescriptor->release(); - m_renderPassDescriptor = nullptr; - } - return true; } -void MetalLayerHandle::CreateRenderPassDescriptor(bool clear) -{ - if (m_renderPassDescriptor) - m_renderPassDescriptor->release(); - - m_renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init(); - auto colorAttachment = m_renderPassDescriptor->colorAttachments()->object(0); - colorAttachment->setTexture(m_drawable->texture()); - colorAttachment->setLoadAction(clear ? MTL::LoadActionClear : MTL::LoadActionLoad); - colorAttachment->setStoreAction(MTL::StoreActionStore); -} - void MetalLayerHandle::PresentDrawable(MTL::CommandBuffer* commandBuffer) { commandBuffer->presentDrawable(m_drawable); diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h index b4aff7dfb..39a7cd1f3 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h @@ -3,8 +3,6 @@ #include #include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h" -#include "QuartzCore/CAMetalDrawable.hpp" -#include "QuartzCore/CAMetalLayer.hpp" #include "util/math/vector2.h" class MetalLayerHandle @@ -19,20 +17,15 @@ class MetalLayerHandle bool AcquireDrawable(); - void CreateRenderPassDescriptor(bool clear); - void PresentDrawable(MTL::CommandBuffer* commandBuffer); CA::MetalLayer* GetLayer() const { return m_layer; } CA::MetalDrawable* GetDrawable() const { return m_drawable; } - MTL::RenderPassDescriptor* GetRenderPassDescriptor() const { return m_renderPassDescriptor; } - private: CA::MetalLayer* m_layer = nullptr; float m_layerScaleX, m_layerScaleY; CA::MetalDrawable* m_drawable = nullptr; - MTL::RenderPassDescriptor* m_renderPassDescriptor = nullptr; }; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index dfbbb2cca..ee15a5a56 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -20,6 +20,7 @@ #include "HW/Latte/Renderer/Metal/MetalCommon.h" #include "HW/Latte/Renderer/Metal/MetalLayerHandle.h" #include "HW/Latte/Renderer/Renderer.h" +#include "Metal/MTLRenderPass.hpp" #include "imgui.h" #define IMGUI_IMPL_METAL_CPP @@ -237,8 +238,8 @@ void MetalRenderer::SwapBuffers(bool swapTV, bool swapDRC) { if (swapTV) SwapBuffer(true); - if (swapDRC) - SwapBuffer(false); + //if (swapDRC) + // SwapBuffer(false); // Release all the command buffers CommitCommandBuffer(); @@ -262,9 +263,15 @@ void MetalRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutput // Create render pass auto& layer = GetLayer(!padView); - layer.CreateRenderPassDescriptor(clearBackground); - auto renderCommandEncoder = GetTemporaryRenderCommandEncoder(layer.GetRenderPassDescriptor()); + MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init(); + auto colorAttachment = renderPassDescriptor->colorAttachments()->object(0); + colorAttachment->setTexture(layer.GetDrawable()->texture()); + colorAttachment->setLoadAction(clearBackground ? MTL::LoadActionClear : MTL::LoadActionLoad); + colorAttachment->setStoreAction(MTL::StoreActionStore); + + auto renderCommandEncoder = GetTemporaryRenderCommandEncoder(renderPassDescriptor); + renderPassDescriptor->release(); // Draw to Metal layer renderCommandEncoder->setRenderPipelineState(m_state.m_usesSRGB ? m_presentPipelineSRGB : m_presentPipelineLinear); @@ -303,25 +310,36 @@ void MetalRenderer::NotifyLatteCommandProcessorIdle() bool MetalRenderer::ImguiBegin(bool mainWindow) { - EnsureImGuiBackend(); - if (!Renderer::ImguiBegin(mainWindow)) return false; if (!AcquireDrawable(mainWindow)) return false; + EnsureImGuiBackend(); + + // Check if the font texture needs to be built + ImGuiIO& io = ImGui::GetIO(); + if (!io.Fonts->IsBuilt()) + ImGui_ImplMetal_CreateFontsTexture(m_device); + auto& layer = GetLayer(mainWindow); - if (!layer.GetRenderPassDescriptor()) - layer.CreateRenderPassDescriptor(true); // TODO: should we clear? - ImGui_ImplMetal_CreateFontsTexture(m_device); - ImGui_ImplMetal_NewFrame(layer.GetRenderPassDescriptor()); + // Render pass descriptor + MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init(); + auto colorAttachment = renderPassDescriptor->colorAttachments()->object(0); + colorAttachment->setTexture(layer.GetDrawable()->texture()); + colorAttachment->setLoadAction(MTL::LoadActionLoad); + colorAttachment->setStoreAction(MTL::StoreActionStore); + + // New frame + ImGui_ImplMetal_NewFrame(renderPassDescriptor); ImGui_UpdateWindowInformation(mainWindow); ImGui::NewFrame(); if (m_encoderType != MetalEncoderType::Render) - GetTemporaryRenderCommandEncoder(layer.GetRenderPassDescriptor()); + GetTemporaryRenderCommandEncoder(renderPassDescriptor); + renderPassDescriptor->release(); return true; } @@ -401,7 +419,7 @@ void MetalRenderer::AppendOverlayDebugInfo() ImGui::Text("--- Metal info (per frame) ---"); ImGui::Text("Command buffers %zu", m_commandBuffers.size()); - ImGui::Text("Render passes %u", m_performanceMonitor.m_renderPasses); + ImGui::Text("Render passes %u", m_performanceMonitor.m_renderPasses); } // TODO: halfZ @@ -1289,6 +1307,9 @@ MTL::RenderCommandEncoder* MetalRenderer::GetTemporaryRenderCommandEncoder(MTL:: m_commandEncoder = renderCommandEncoder; m_encoderType = MetalEncoderType::Render; + // Debug + m_performanceMonitor.m_renderPasses++; + return renderCommandEncoder; } @@ -1348,6 +1369,9 @@ MTL::RenderCommandEncoder* MetalRenderer::GetRenderCommandEncoder(bool forceRecr ResetEncoderState(); + // Debug + m_performanceMonitor.m_renderPasses++; + return renderCommandEncoder; } diff --git a/src/imgui/imgui_impl_metal.mm b/src/imgui/imgui_impl_metal.mm index 8a070a6c2..5f0588573 100644 --- a/src/imgui/imgui_impl_metal.mm +++ b/src/imgui/imgui_impl_metal.mm @@ -311,12 +311,8 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c bool ImGui_ImplMetal_CreateFontsTexture(id device) { - // HACK: check if the font atlas has been built already - ImGuiIO& io = ImGui::GetIO(); - if (io.Fonts->IsBuilt()) - return true; - ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); + ImGuiIO& io = ImGui::GetIO(); // We are retrieving and uploading the font atlas as a 4-channels RGBA texture here. // In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.