Skip to content

Commit

Permalink
fix: ImGui issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SamoZ256 committed Aug 25, 2024
1 parent b105a38 commit c905399
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
20 changes: 0 additions & 20 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ MetalLayerHandle::~MetalLayerHandle()
{
if (m_layer)
m_layer->release();
if (m_renderPassDescriptor)
m_renderPassDescriptor->release();
}

void MetalLayerHandle::Resize(const Vector2i& size)
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalLayerHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
};
48 changes: 36 additions & 12 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1289,6 +1307,9 @@ MTL::RenderCommandEncoder* MetalRenderer::GetTemporaryRenderCommandEncoder(MTL::
m_commandEncoder = renderCommandEncoder;
m_encoderType = MetalEncoderType::Render;

// Debug
m_performanceMonitor.m_renderPasses++;

return renderCommandEncoder;
}

Expand Down Expand Up @@ -1348,6 +1369,9 @@ MTL::RenderCommandEncoder* MetalRenderer::GetRenderCommandEncoder(bool forceRecr

ResetEncoderState();

// Debug
m_performanceMonitor.m_renderPasses++;

return renderCommandEncoder;
}

Expand Down
6 changes: 1 addition & 5 deletions src/imgui/imgui_impl_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c905399

Please sign in to comment.