fix: ImGui issues

This commit is contained in:
Samuliak 2024-08-25 10:09:18 +02:00
parent b105a383aa
commit c905399f1f
4 changed files with 38 additions and 45 deletions

View File

@ -16,8 +16,6 @@ MetalLayerHandle::~MetalLayerHandle()
{ {
if (m_layer) if (m_layer)
m_layer->release(); m_layer->release();
if (m_renderPassDescriptor)
m_renderPassDescriptor->release();
} }
void MetalLayerHandle::Resize(const Vector2i& size) void MetalLayerHandle::Resize(const Vector2i& size)
@ -37,27 +35,9 @@ bool MetalLayerHandle::AcquireDrawable()
return false; return false;
} }
if (m_renderPassDescriptor)
{
m_renderPassDescriptor->release();
m_renderPassDescriptor = nullptr;
}
return true; 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) void MetalLayerHandle::PresentDrawable(MTL::CommandBuffer* commandBuffer)
{ {
commandBuffer->presentDrawable(m_drawable); commandBuffer->presentDrawable(m_drawable);

View File

@ -3,8 +3,6 @@
#include <QuartzCore/QuartzCore.hpp> #include <QuartzCore/QuartzCore.hpp>
#include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h" #include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h"
#include "QuartzCore/CAMetalDrawable.hpp"
#include "QuartzCore/CAMetalLayer.hpp"
#include "util/math/vector2.h" #include "util/math/vector2.h"
class MetalLayerHandle class MetalLayerHandle
@ -19,20 +17,15 @@ public:
bool AcquireDrawable(); bool AcquireDrawable();
void CreateRenderPassDescriptor(bool clear);
void PresentDrawable(MTL::CommandBuffer* commandBuffer); void PresentDrawable(MTL::CommandBuffer* commandBuffer);
CA::MetalLayer* GetLayer() const { return m_layer; } CA::MetalLayer* GetLayer() const { return m_layer; }
CA::MetalDrawable* GetDrawable() const { return m_drawable; } CA::MetalDrawable* GetDrawable() const { return m_drawable; }
MTL::RenderPassDescriptor* GetRenderPassDescriptor() const { return m_renderPassDescriptor; }
private: private:
CA::MetalLayer* m_layer = nullptr; CA::MetalLayer* m_layer = nullptr;
float m_layerScaleX, m_layerScaleY; float m_layerScaleX, m_layerScaleY;
CA::MetalDrawable* m_drawable = nullptr; CA::MetalDrawable* m_drawable = nullptr;
MTL::RenderPassDescriptor* m_renderPassDescriptor = nullptr;
}; };

View File

@ -20,6 +20,7 @@
#include "HW/Latte/Renderer/Metal/MetalCommon.h" #include "HW/Latte/Renderer/Metal/MetalCommon.h"
#include "HW/Latte/Renderer/Metal/MetalLayerHandle.h" #include "HW/Latte/Renderer/Metal/MetalLayerHandle.h"
#include "HW/Latte/Renderer/Renderer.h" #include "HW/Latte/Renderer/Renderer.h"
#include "Metal/MTLRenderPass.hpp"
#include "imgui.h" #include "imgui.h"
#define IMGUI_IMPL_METAL_CPP #define IMGUI_IMPL_METAL_CPP
@ -237,8 +238,8 @@ void MetalRenderer::SwapBuffers(bool swapTV, bool swapDRC)
{ {
if (swapTV) if (swapTV)
SwapBuffer(true); SwapBuffer(true);
if (swapDRC) //if (swapDRC)
SwapBuffer(false); // SwapBuffer(false);
// Release all the command buffers // Release all the command buffers
CommitCommandBuffer(); CommitCommandBuffer();
@ -262,9 +263,15 @@ void MetalRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutput
// Create render pass // Create render pass
auto& layer = GetLayer(!padView); 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 // Draw to Metal layer
renderCommandEncoder->setRenderPipelineState(m_state.m_usesSRGB ? m_presentPipelineSRGB : m_presentPipelineLinear); renderCommandEncoder->setRenderPipelineState(m_state.m_usesSRGB ? m_presentPipelineSRGB : m_presentPipelineLinear);
@ -303,25 +310,36 @@ void MetalRenderer::NotifyLatteCommandProcessorIdle()
bool MetalRenderer::ImguiBegin(bool mainWindow) bool MetalRenderer::ImguiBegin(bool mainWindow)
{ {
EnsureImGuiBackend();
if (!Renderer::ImguiBegin(mainWindow)) if (!Renderer::ImguiBegin(mainWindow))
return false; return false;
if (!AcquireDrawable(mainWindow)) if (!AcquireDrawable(mainWindow))
return false; return false;
auto& layer = GetLayer(mainWindow); EnsureImGuiBackend();
if (!layer.GetRenderPassDescriptor())
layer.CreateRenderPassDescriptor(true); // TODO: should we clear?
ImGui_ImplMetal_CreateFontsTexture(m_device); // Check if the font texture needs to be built
ImGui_ImplMetal_NewFrame(layer.GetRenderPassDescriptor()); ImGuiIO& io = ImGui::GetIO();
if (!io.Fonts->IsBuilt())
ImGui_ImplMetal_CreateFontsTexture(m_device);
auto& layer = GetLayer(mainWindow);
// 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_UpdateWindowInformation(mainWindow);
ImGui::NewFrame(); ImGui::NewFrame();
if (m_encoderType != MetalEncoderType::Render) if (m_encoderType != MetalEncoderType::Render)
GetTemporaryRenderCommandEncoder(layer.GetRenderPassDescriptor()); GetTemporaryRenderCommandEncoder(renderPassDescriptor);
renderPassDescriptor->release();
return true; return true;
} }
@ -401,7 +419,7 @@ void MetalRenderer::AppendOverlayDebugInfo()
ImGui::Text("--- Metal info (per frame) ---"); ImGui::Text("--- Metal info (per frame) ---");
ImGui::Text("Command buffers %zu", m_commandBuffers.size()); 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 // TODO: halfZ
@ -1289,6 +1307,9 @@ MTL::RenderCommandEncoder* MetalRenderer::GetTemporaryRenderCommandEncoder(MTL::
m_commandEncoder = renderCommandEncoder; m_commandEncoder = renderCommandEncoder;
m_encoderType = MetalEncoderType::Render; m_encoderType = MetalEncoderType::Render;
// Debug
m_performanceMonitor.m_renderPasses++;
return renderCommandEncoder; return renderCommandEncoder;
} }
@ -1348,6 +1369,9 @@ MTL::RenderCommandEncoder* MetalRenderer::GetRenderCommandEncoder(bool forceRecr
ResetEncoderState(); ResetEncoderState();
// Debug
m_performanceMonitor.m_renderPasses++;
return renderCommandEncoder; return renderCommandEncoder;
} }

View File

@ -311,12 +311,8 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id<MTLCommandBuffer> c
bool ImGui_ImplMetal_CreateFontsTexture(id<MTLDevice> device) bool ImGui_ImplMetal_CreateFontsTexture(id<MTLDevice> 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(); 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. // 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. // In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.