Skip to content

Commit

Permalink
feat: add world tracking provider to render on acutal device (bkaradz…
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Aug 1, 2024
1 parent 36ef1a6 commit 4bbaa2a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/renderer_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,13 @@ bool init(const Init& _init)
, TextureFormat::Unknown
, TextureFormat::UnknownDepth
);

#if BX_PLATFORM_VISIONOS
m_deviceAnchor = ar_device_anchor_create();
m_worldTracking = ar_world_tracking_provider_create(ar_world_tracking_configuration_create());
m_arSession = ar_session_create();
ar_session_run(m_arSession, ar_data_providers_create_with_data_providers(m_worldTracking, nil));
#endif
m_numWindows = 1;

#if BX_PLATFORM_VISIONOS
Expand Down Expand Up @@ -920,6 +927,13 @@ void shutdown()
MTL_RELEASE(m_vertexDescriptor);
MTL_RELEASE(m_textureDescriptor);
MTL_RELEASE(m_samplerDescriptor);

#if BX_PLATFORM_VISIONOS
ar_session_stop(m_arSession);
MTL_RELEASE(m_arSession);
MTL_RELEASE(m_worldTracking);
MTL_RELEASE(m_deviceAnchor);
#endif

m_mainFrameBuffer.destroy();

Expand Down Expand Up @@ -1465,6 +1479,19 @@ 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)
Expand All @@ -1483,6 +1510,12 @@ void flip() override
if (NULL != frameBuffer.m_swapChain->m_drawable)
{
#if BX_PLATFORM_VISIONOS
if (m_worldTracking != NULL)
{
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 // BX_PLATFORM_VISIONOS
Expand Down Expand Up @@ -2719,6 +2752,11 @@ void endEncoding()
Resolution m_resolution;
void* m_capture;
uint32_t m_captureSize;
#if BX_PLATFORM_VISIONOS
ar_session_t m_arSession;
ar_world_tracking_provider_t m_worldTracking;
ar_device_anchor_t m_deviceAnchor;
#endif

// descriptors
RenderPipelineDescriptor m_renderPipelineDescriptor;
Expand Down

0 comments on commit 4bbaa2a

Please sign in to comment.