Swift equivalent of ar_data_providers_create_with_data_providers and cp_time_to_cf_time_interval

This is an spinoff from forum post xrOS+Metal example code here

Am porting Warren Moore's example here

Have the most of the conversion to Swift complete but am stuck at a couple places

ar_data_providers_create_with_data_providers(...)

cp_time_to_cf_time_interval(...)

Any hints on how to track these down? Thanks!

Answered by 2time.net in 761105022

Hi Muse, I have tracked it down to this block. Cheers.

extension LayerRenderer.Clock.Instant {
    func toTimeInterval() -> TimeInterval {
        let duration = LayerRenderer.Clock.Instant.epoch.duration(to: self)
        let secondsPart = Double(duration.components.seconds)
        let attosecondsPart = Double(duration.components.attoseconds) / 1e18
        return secondsPart + attosecondsPart
    }
}
Accepted Answer

Hi Muse, I have tracked it down to this block. Cheers.

extension LayerRenderer.Clock.Instant {
    func toTimeInterval() -> TimeInterval {
        let duration = LayerRenderer.Clock.Instant.epoch.duration(to: self)
        let secondsPart = Double(duration.components.seconds)
        let attosecondsPart = Double(duration.components.attoseconds) / 1e18
        return secondsPart + attosecondsPart
    }
}

As for

ar_data_providers_create_with_data_providers(...)

From modifications to warrenm example (see above)

am calling

 private func runWorldTrackingARSession() async {
        guard let arSession else { return err("arSession") }
        try? await arSession.run([worldTracking]) //WorldTrackingProvider
    }

passing through the arSession and worldTracking via

public func SpatialRenderer_InitAndRun(_ layerRenderer: LayerRenderer,
                                       _ arSession: ARKitSession,
                                       _ worldTracking: WorldTrackingProvider) {
    let renderThread = RenderThread(layerRenderer, arSession, worldTracking)
    renderThread.name = "Spatial Renderer Thread"
    renderThread.start()
}

called from

@main
struct FullyImmersiveMetalApp: App {
    @State var session = ARKitSession() //??
    @State var worldTracking = WorldTrackingProvider() //??
    init() {}
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        ImmersiveSpace(id: "ImmersiveSpace") {
            CompositorLayer(configuration: MetalLayerConfiguration()) { layerRenderer in
                SpatialRenderer_InitAndRun(layerRenderer, session, worldTracking)
            }
        }.immersionStyle(selection: .constant(.full), in: .full)
    }
}

All swift code compile but fails at runtime. Probably in another code block. YMMV

Full port of C++ to Swift is working. Code is here

Thanks to Warren Moore for C++ version.

Swift equivalent of ar_data_providers_create_with_data_providers and cp_time_to_cf_time_interval
 
 
Q