ios: Simplify MTKView interface (still doesn't work though)

This commit is contained in:
wheremyfoodat 2025-03-08 20:32:42 +02:00
parent fb59320829
commit e378a52b5a
4 changed files with 11 additions and 21 deletions

View file

@ -87,8 +87,8 @@ class Renderer {
virtual void setupGLES() {} virtual void setupGLES() {}
// Only relevant for Metal renderer on iOS // Only relevant for Metal renderer on iOS
// Passes a SwiftUI MTKView Drawable to the renderer // Passes a SwiftUI MTKView's layer (CAMetalLayer) to the renderer
virtual void setMTKDrawable(void* drawable) {}; virtual void setMTKLayer(void* layer) {};
// This function is called on every draw call before parsing vertex data. // This function is called on every draw call before parsing vertex data.
// It is responsible for things like looking up which vertex/fragment shaders to use, recompiling them if they don't exist, choosing between // It is responsible for things like looking up which vertex/fragment shaders to use, recompiling them if they don't exist, choosing between

View file

@ -42,11 +42,10 @@ class RendererMTL final : public Renderer {
virtual void initGraphicsContext([[maybe_unused]] GL::Context* context) override {} virtual void initGraphicsContext([[maybe_unused]] GL::Context* context) override {}
#endif #endif
virtual void setMTKDrawable(void* drawable) override; virtual void setMTKLayer(void* layer) override;
private: private:
CA::MetalLayer* metalLayer = nullptr; CA::MetalLayer* metalLayer = nullptr;
CA::MetalDrawable* metalDrawable = nullptr;
MTL::Device* device = nullptr; MTL::Device* device = nullptr;
MTL::CommandQueue* commandQueue = nullptr; MTL::CommandQueue* commandQueue = nullptr;

View file

@ -55,29 +55,21 @@ void RendererMTL::reset() {
colorRenderTargetCache.reset(); colorRenderTargetCache.reset();
} }
void RendererMTL::setMTKDrawable(void* drawable) { void RendererMTL::setMTKLayer(void* layer) {
metalDrawable = (CA::MetalDrawable*)drawable; metalLayer = (CA::MetalLayer*)layer;
// metalLayer->retain();
} }
void RendererMTL::display() { void RendererMTL::display() {
#ifdef PANDA3DS_IOS
CA::MetalDrawable* drawable = metalDrawable;
if (!drawable) {
return;
}
MTL::Texture* texture = drawable->texture();
#else
CA::MetalDrawable* drawable = metalLayer->nextDrawable(); CA::MetalDrawable* drawable = metalLayer->nextDrawable();
if (!drawable) { if (!drawable) {
return; return;
} }
MTL::Texture* texture = drawable->texture(); MTL::Texture* texture = drawable->texture();
#endif
using namespace PICA::ExternalRegs; using namespace PICA::ExternalRegs;
printf("Device pointer: %p\nDrawable pointer: %p\nTexture pointer: %p\n", device, drawable, texture); printf("Layer pointer: %p\nDevice pointer: %p\nDrawable pointer: %p\nTexture pointer: %p\n", metalLayer, device, drawable, texture);
// Top screen // Top screen
const u32 topActiveFb = externalRegs[Framebuffer0Select] & 1; const u32 topActiveFb = externalRegs[Framebuffer0Select] & 1;
@ -134,10 +126,9 @@ void RendererMTL::display() {
// Inform the vertex buffer cache that the frame ended // Inform the vertex buffer cache that the frame ended
vertexBufferCache.endFrame(); vertexBufferCache.endFrame();
// Release the drawable (not on iOS cause SwiftUI handles it there)
#ifndef PANDA3DS_IOS
drawable->release(); drawable->release();
#ifdef PANDA3DS_IOS
// metalLayer->release();
#endif #endif
} }

View file

@ -30,9 +30,9 @@ IOS_EXPORT void iosCreateEmulator() {
printf("Created emulator\n"); printf("Created emulator\n");
} }
IOS_EXPORT void iosRunFrame(void* drawable) { IOS_EXPORT void iosRunFrame(void* layer) {
printf("Running a frame\n"); printf("Running a frame\n");
emulator->getRenderer()->setMTKDrawable(drawable); emulator->getRenderer()->setMTKLayer(layer);
emulator->runFrame(); emulator->runFrame();
printf("Ran a frame\n"); printf("Ran a frame\n");
} }