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)
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);

View File

@ -3,8 +3,6 @@
#include <QuartzCore/QuartzCore.hpp>
#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 @@ public:
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;
};

View File

@ -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;
auto& layer = GetLayer(mainWindow);
if (!layer.GetRenderPassDescriptor())
layer.CreateRenderPassDescriptor(true); // TODO: should we clear?
EnsureImGuiBackend();
// Check if the font texture needs to be built
ImGuiIO& io = ImGui::GetIO();
if (!io.Fonts->IsBuilt())
ImGui_ImplMetal_CreateFontsTexture(m_device);
ImGui_ImplMetal_NewFrame(layer.GetRenderPassDescriptor());
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::NewFrame();
if (m_encoderType != MetalEncoderType::Render)
GetTemporaryRenderCommandEncoder(layer.GetRenderPassDescriptor());
GetTemporaryRenderCommandEncoder(renderPassDescriptor);
renderPassDescriptor->release();
return true;
}
@ -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;
}

View File

@ -311,12 +311,8 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id<MTLCommandBuffer> c
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();
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.