From 0d0152d6e06620831604d7a9d8c3544eff9f3c73 Mon Sep 17 00:00:00 2001 From: Samuliak Date: Fri, 16 Aug 2024 15:33:49 +0200 Subject: [PATCH] fix: present --- CMakeLists.txt | 5 ++++- src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.h | 2 +- src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.mm | 8 +++++++- .../HW/Latte/Renderer/Metal/MetalRenderer.cpp | 15 +++++++++++++-- src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h | 2 ++ src/gui/canvas/MetalCanvas.cpp | 3 +++ 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 58d8d6a4..ee4f19bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,8 @@ cmake_minimum_required(VERSION 3.21.1) +# TODO: remove this +set(CMAKE_CXX_FLAGS_DEBUG "-g") + option(ENABLE_VCPKG "Enable the vcpkg package manager" ON) option(MACOS_BUNDLE "The executable when built on macOS will be created as an application bundle" OFF) set(EXPERIMENTAL_VERSION "" CACHE STRING "") # used by CI script to set experimental version @@ -23,7 +26,7 @@ if (ENABLE_VCPKG) OUTPUT_VARIABLE is_vcpkg_shallow OUTPUT_STRIP_TRAILING_WHITESPACE ) - + if(is_vcpkg_shallow STREQUAL "true") message(STATUS "vcpkg is shallow. Unshallowing it now...") execute_process( diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.h index 56a30224..d2b30667 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.h @@ -1,3 +1,3 @@ #pragma once -void* CreateMetalLayer(void* handle); +void* CreateMetalLayer(void* handle, float& scaleX, float& scaleY); diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.mm b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.mm index 8ce3202e..16a7aa67 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.mm +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalLayer.mm @@ -2,7 +2,7 @@ #include "Cafe/HW/Latte/Renderer/MetalView.h" -void* CreateMetalLayer(void* handle) +void* CreateMetalLayer(void* handle, float& scaleX, float& scaleY) { NSView* view = (NSView*)handle; @@ -12,5 +12,11 @@ void* CreateMetalLayer(void* handle) [view addSubview:childView]; + const NSRect points = [childView frame]; + const NSRect pixels = [childView convertRectToBacking:points]; + + scaleX = (float)(pixels.size.width / points.size.width); + scaleY = (float)(pixels.size.height / points.size.height); + return childView.layer; } diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index 4489b020..9454d62c 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -138,13 +138,20 @@ MetalRenderer::~MetalRenderer() m_device->release(); } -// TODO: don't ignore "mainWindow" argument and respect size +// TODO: don't ignore "mainWindow" argument void MetalRenderer::InitializeLayer(const Vector2i& size, bool mainWindow) { const auto& windowInfo = gui_getWindowInfo().window_main; - m_metalLayer = (CA::MetalLayer*)CreateMetalLayer(windowInfo.handle); + m_metalLayer = (CA::MetalLayer*)CreateMetalLayer(windowInfo.handle, m_layerScaleX, m_layerScaleY); m_metalLayer->setDevice(m_device); + m_metalLayer->setDrawableSize(CGSize{(float)size.x * m_layerScaleX, (float)size.y * m_layerScaleY}); +} + +// TODO: don't ignore "mainWindow" argument +void MetalRenderer::ResizeLayer(const Vector2i& size, bool mainWindow) +{ + m_metalLayer->setDrawableSize(CGSize{(float)size.x * m_layerScaleX, (float)size.y * m_layerScaleY}); } void MetalRenderer::Initialize() @@ -215,6 +222,7 @@ void MetalRenderer::SwapBuffers(bool swapTV, bool swapDRC) m_memoryManager->ResetTemporaryBuffers(); } +// TODO: use `shader` for drawing void MetalRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutputShader* shader, bool useLinearTexFilter, sint32 imageX, sint32 imageY, sint32 imageWidth, sint32 imageHeight, bool padView, bool clearBackground) @@ -240,6 +248,9 @@ void MetalRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutput renderCommandEncoder->setFragmentTexture(presentTexture, 0); renderCommandEncoder->setFragmentSamplerState((useLinearTexFilter ? m_linearSampler : m_nearestSampler), 0); + renderCommandEncoder->setViewport(MTL::Viewport{(double)imageX, (double)imageY, (double)imageWidth, (double)imageHeight, 0.0, 1.0}); + renderCommandEncoder->setScissorRect(MTL::ScissorRect{(uint32)imageX, (uint32)imageY, (uint32)imageWidth, (uint32)imageHeight}); + renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangle, NS::UInteger(0), NS::UInteger(3)); EndEncoding(); diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h index 451a29af..f315963e 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h @@ -141,6 +141,7 @@ public: } void InitializeLayer(const Vector2i& size, bool mainWindow); + void ResizeLayer(const Vector2i& size, bool mainWindow); void Initialize() override; void Shutdown() override; @@ -330,6 +331,7 @@ public: private: CA::MetalLayer* m_metalLayer; + float m_layerScaleX, m_layerScaleY; class MetalMemoryManager* m_memoryManager; class MetalPipelineCache* m_pipelineCache; diff --git a/src/gui/canvas/MetalCanvas.cpp b/src/gui/canvas/MetalCanvas.cpp index fe8dc4ee..2c89f882 100644 --- a/src/gui/canvas/MetalCanvas.cpp +++ b/src/gui/canvas/MetalCanvas.cpp @@ -60,4 +60,7 @@ void MetalCanvas::OnResize(wxSizeEvent& event) const wxRect refreshRect(size); RefreshRect(refreshRect, false); + + auto metal_renderer = MetalRenderer::GetInstance(); + metal_renderer->InitializeLayer({size.x, size.y}, m_is_main_window); }