Skip to content

Commit

Permalink
feat: add world tracking provider to render on acutal device
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Jul 17, 2024
1 parent aed66e9 commit db109d2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/bgfx/bgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
};

Expand Down
5 changes: 5 additions & 0 deletions include/bgfx/c99/bgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions src/renderer_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -1470,6 +1490,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
Expand Down Expand Up @@ -2706,6 +2732,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;
Expand Down

0 comments on commit db109d2

Please sign in to comment.