From 3b3b03ca453bb1b58785d1863343b74f2f04d3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 24 Jul 2024 14:01:02 +0200 Subject: [PATCH] feat: move render loop to cpp --- Apps/Playground/visionOS/App.swift | 47 +++----------------- Apps/Playground/visionOS/LibNativeBridge.hpp | 21 +++++---- 2 files changed, 16 insertions(+), 52 deletions(-) diff --git a/Apps/Playground/visionOS/App.swift b/Apps/Playground/visionOS/App.swift index a920a6329..354b90760 100644 --- a/Apps/Playground/visionOS/App.swift +++ b/Apps/Playground/visionOS/App.swift @@ -16,45 +16,6 @@ struct ContentStageConfiguration: CompositorLayerConfiguration { } } -class Renderer { - let layerRenderer: LayerRenderer - var libNativeBridge: LibNativeBridge - - init(_ layerRenderer: LayerRenderer) { - self.layerRenderer = layerRenderer - self.libNativeBridge = LibNativeBridge(layerRenderer) - } - - func startRenderLoop() { - let renderThread = Thread { - self.renderLoop() - } - renderThread.name = "Render Thread" - renderThread.start() - } - - - func renderLoop() { - while true { - if layerRenderer.state == .invalidated { - print("Layer is invalidated") - - libNativeBridge.shutdown() - return - } else if layerRenderer.state == .paused { - layerRenderer.waitUntilRunning() - continue - } else { - autoreleasepool { - libNativeBridge.initialize() - libNativeBridge.render() - } - } - } - } -} - - @main struct ExampleApp: App { @State private var showImmersiveSpace = false @@ -92,8 +53,12 @@ struct ExampleApp: App { } ImmersiveSpace(id: "ImmersiveSpace") { CompositorLayer(configuration: ContentStageConfiguration()) { layerRenderer in - let renderer = Renderer(layerRenderer) - renderer.startRenderLoop() + let renderThread = Thread { + var engine = LibNativeBridge(layerRenderer) + engine.renderLoop() + } + renderThread.name = "Render Thread" + renderThread.start() } }.immersionStyle(selection: .constant(.full), in: .full) } diff --git a/Apps/Playground/visionOS/LibNativeBridge.hpp b/Apps/Playground/visionOS/LibNativeBridge.hpp index c3b2c033f..55043665c 100644 --- a/Apps/Playground/visionOS/LibNativeBridge.hpp +++ b/Apps/Playground/visionOS/LibNativeBridge.hpp @@ -5,23 +5,22 @@ #include class LibNativeBridge { -private: - bool m_initialized = false; - cp_layer_renderer_t m_layerRenderer = NULL; - ar_world_tracking_provider_t world_tracking = NULL; - ar_session_t ar_session = NULL; public: - LibNativeBridge(cp_layer_renderer_t layerRenderer) : m_layerRenderer(layerRenderer) { - } - - ~LibNativeBridge() { - shutdown(); - } + LibNativeBridge(cp_layer_renderer_t layerRenderer) : m_layerRenderer(layerRenderer) {} + ~LibNativeBridge() { shutdown(); } void runARSession(void); bool initialize(void); void shutdown(void); + void renderLoop(void); + +private: + bool m_initialized = false; + cp_layer_renderer_t m_layerRenderer = NULL; + ar_world_tracking_provider_t world_tracking = NULL; + ar_session_t ar_session = NULL; + void render(void); };