From b83ca5e3ce99b79599d5e92c140536a51dc5d29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 26 Jun 2024 17:50:03 +0200 Subject: [PATCH] feat: add world tracking provider to render on acutal device --- include/bgfx/bgfx.h | 2 ++ include/bgfx/c99/bgfx.h | 5 +++++ src/renderer_mtl.mm | 28 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index 8b0f0ede7a..9aa782984f 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -645,6 +645,8 @@ namespace bgfx /// create back-buffer color surface. void* backBufferDS; //!< Backbuffer depth/stencil. If `NULL`, bgfx will create a back-buffer /// depth/stencil surface. + void *worldTracking; //!< Native world tracking provider used for visionOS. + NativeWindowHandleType::Enum type; //!< Handle type. Needed for platforms having more than one option. }; diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index 83354d46df..b4feae6d88 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -630,6 +630,11 @@ typedef struct bgfx_platform_data_s */ void* backBuffer; + /** + * Native world tracking provider used for visionOS. + */ + void* worldTracking; + /** * Backbuffer depth/stencil. If `NULL`, bgfx will create a back-buffer * depth/stencil surface. diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm index 1fb46e3601..0eda3521df 100644 --- a/src/renderer_mtl.mm +++ b/src/renderer_mtl.mm @@ -531,6 +531,15 @@ bool init(const Init& _init) , TextureFormat::Unknown , TextureFormat::UnknownDepth ); + +#if BX_PLATFORM_VISIONOS + if (g_platformData.worldTracking != NULL) { + m_worldTracking = (ar_world_tracking_provider_t)g_platformData.worldTracking; + m_deviceAnchor = ar_device_anchor_create(); + } else { + BX_WARN(false, "Unable to setup world tracking, this prevents drawables from being presented on the device."); + } +#endif m_numWindows = 1; #if BX_PLATFORM_VISIONOS @@ -1452,6 +1461,17 @@ bool isDeviceRemoved() override return false; } +#if BX_PLATFORM_VISIONOS + void createPoseForTiming(cp_frame_timing_t timing, ar_world_tracking_provider_t world_tracking) { + cp_time_t presentationTime = cp_frame_timing_get_presentation_time(timing); + CFTimeInterval queryTime = cp_time_to_cf_time_interval(presentationTime); + ar_device_anchor_query_status_t status = ar_world_tracking_provider_query_device_anchor_at_timestamp(world_tracking, queryTime, m_deviceAnchor); + if (status != ar_device_anchor_query_status_success) { + BX_WARN(false, "Device anchor query failed.") + } + } +#endif + void flip() override { if (NULL == m_commandBuffer) @@ -1470,6 +1490,10 @@ void flip() override if (NULL != frameBuffer.m_swapChain->m_drawable) { #if BX_PLATFORM_VISIONOS + + auto timingInfo = cp_drawable_get_frame_timing(frameBuffer.m_swapChain->m_drawable); + createPoseForTiming(timingInfo, m_worldTracking); + cp_drawable_set_device_anchor(frameBuffer.m_swapChain->m_drawable, m_deviceAnchor); cp_drawable_encode_present(frameBuffer.m_swapChain->m_drawable, m_commandBuffer); cp_frame_end_submission(frameBuffer.m_swapChain->m_frame); #else @@ -2706,6 +2730,10 @@ void endEncoding() Resolution m_resolution; void* m_capture; uint32_t m_captureSize; +#if BX_PLATFORM_VISIONOS + ar_world_tracking_provider_t m_worldTracking; + ar_device_anchor_t m_deviceAnchor; +#endif // descriptors RenderPipelineDescriptor m_renderPipelineDescriptor;