|
| 1 | +/* |
| 2 | + * Copyright 2026 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +/// \file |
| 30 | +/// CompositorServices session wrapper for visionOS. |
| 31 | +/// |
| 32 | +/// `CompositorServicesSession` encapsulates the visionOS frame protocol |
| 33 | +/// (frame query, update, timing wait, drawable iteration, present) and the |
| 34 | +/// optional ARKit world-tracking session, exposing a small, idiomatic |
| 35 | +/// C++ API on top of the C/Objective-C `CompositorServices` framework. |
| 36 | + |
| 37 | +#include <functional> |
| 38 | +#include <memory> |
| 39 | + |
| 40 | +#include <CompositorServices/CompositorServices.h> |
| 41 | + |
| 42 | +#include "../../GraphicsEngine/interface/RenderDevice.h" |
| 43 | +#include "../../GraphicsEngine/interface/DeviceContext.h" |
| 44 | +#include "../../../Primitives/interface/BasicTypes.h" |
| 45 | +#include "../../../Common/interface/BasicMath.hpp" |
| 46 | +#include "../../../Common/interface/RefCntAutoPtr.hpp" |
| 47 | + |
| 48 | +namespace Diligent |
| 49 | +{ |
| 50 | + |
| 51 | +/// High-level wrapper around the visionOS CompositorServices frame protocol. |
| 52 | +/// |
| 53 | +/// The session owns: |
| 54 | +/// * a `cp_layer_renderer_t` received from the SwiftUI `CompositorLayer` entry |
| 55 | +/// point, |
| 56 | +/// * an optional ARKit world-tracking session that supplies device anchors |
| 57 | +/// for each drawable, |
| 58 | +/// * references to the Diligent device and immediate context used for |
| 59 | +/// wrapping drawable textures and presenting drawables. |
| 60 | +/// |
| 61 | +/// All ARKit / simd / Metal headers are confined to the implementation file; |
| 62 | +/// only the `<CompositorServices/CompositorServices.h>` types appear in this |
| 63 | +/// public header. |
| 64 | +class CompositorServicesSession |
| 65 | +{ |
| 66 | +public: |
| 67 | + /// \param Renderer Layer renderer from the SwiftUI |
| 68 | + /// `CompositorLayer` entry point. Must not be null. |
| 69 | + /// \param pDevice Diligent render device. Must not be null. |
| 70 | + /// \param pContext Diligent immediate context. Must not be null. |
| 71 | + /// \param EnableWorldTracking When `true`, an ARKit world-tracking session |
| 72 | + /// is started and used to attach device anchors |
| 73 | + /// to every drawable. When `false`, the scene |
| 74 | + /// is rigidly attached to the headset. |
| 75 | + CompositorServicesSession(cp_layer_renderer_t Renderer, |
| 76 | + IRenderDevice* pDevice, |
| 77 | + IDeviceContext* pContext, |
| 78 | + bool EnableWorldTracking = true); |
| 79 | + |
| 80 | + ~CompositorServicesSession(); |
| 81 | + |
| 82 | + CompositorServicesSession(const CompositorServicesSession&) = delete; |
| 83 | + CompositorServicesSession(CompositorServicesSession&&) = delete; |
| 84 | + CompositorServicesSession& operator=(const CompositorServicesSession&) = delete; |
| 85 | + CompositorServicesSession& operator=(CompositorServicesSession&&) = delete; |
| 86 | + |
| 87 | + /// Drives a single CompositorServices frame from start to finish. |
| 88 | + /// |
| 89 | + /// Handles the full frame protocol mandated by visionOS: |
| 90 | + /// 1. Queries the next frame from the layer renderer. |
| 91 | + /// 2. Wraps app-side state updates in `cp_frame_start_update` / |
| 92 | + /// `cp_frame_end_update` by invoking `Update`. |
| 93 | + /// 3. Predicts timing after the update phase and waits until the |
| 94 | + /// compositor's optimal input time. |
| 95 | + /// 4. Enters the submission phase, iterates over all drawables, |
| 96 | + /// attaches a device anchor (when world tracking is enabled) and |
| 97 | + /// invokes `RenderDrawable` for each. |
| 98 | + /// 5. Ends the submission phase. |
| 99 | + /// |
| 100 | + /// All null / cancelled-frame paths are handled internally. |
| 101 | + /// |
| 102 | + /// `RenderDrawable` is responsible for setting up render targets, drawing |
| 103 | + /// the scene and calling `PresentDrawable` on the session. |
| 104 | + /// |
| 105 | + /// Either callback may be empty. |
| 106 | + void RenderFrame(std::function<void()> Update, |
| 107 | + std::function<void(cp_drawable_t Drawable)> RenderDrawable); |
| 108 | + |
| 109 | + /// Returns the number of views (eyes) in a drawable (typically 2 for |
| 110 | + /// stereo rendering). |
| 111 | + Uint32 GetViewCount(cp_drawable_t Drawable) const; |
| 112 | + |
| 113 | + /// Wraps the Metal color texture of a drawable view as a Diligent texture. |
| 114 | + /// Returns null on failure. |
| 115 | + RefCntAutoPtr<ITexture> GetColorSwapchainImage(cp_drawable_t Drawable, Uint32 ViewIndex); |
| 116 | + |
| 117 | + /// Wraps the Metal depth texture of a drawable view as a Diligent texture. |
| 118 | + /// Returns null on failure. |
| 119 | + RefCntAutoPtr<ITexture> GetDepthSwapchainImage(cp_drawable_t Drawable, Uint32 ViewIndex); |
| 120 | + |
| 121 | + /// Returns the reverse-Z projection matrix for a view in a drawable. |
| 122 | + /// |
| 123 | + /// The compositor mandates reverse-Z depth: the matrix maps the near |
| 124 | + /// plane to 1 and the far plane to 0. Clear the depth buffer to 0 and use |
| 125 | + /// `COMPARISON_FUNC_GREATER_EQUAL` in the PSO. |
| 126 | + float4x4 GetProjectionMatrix(cp_drawable_t Drawable, Uint32 ViewIndex, |
| 127 | + float NearZ, float FarZ) const; |
| 128 | + |
| 129 | + /// Returns the world-to-eye view matrix for a view in a drawable. |
| 130 | + /// |
| 131 | + /// When world tracking is enabled and the ARKit session has produced at |
| 132 | + /// least one anchor, the matrix accounts for the device pose in the |
| 133 | + /// world. Otherwise it yields a drawable-local view matrix (scene rigidly |
| 134 | + /// attached to the headset). |
| 135 | + float4x4 GetViewMatrix(cp_drawable_t Drawable, Uint32 ViewIndex) const; |
| 136 | + |
| 137 | + /// Encodes a present command for the drawable into the immediate context's |
| 138 | + /// Metal command buffer. Call after issuing all draw calls for the |
| 139 | + /// drawable, before `Flush`. |
| 140 | + void PresentDrawable(cp_drawable_t Drawable); |
| 141 | + |
| 142 | +private: |
| 143 | + struct Impl; |
| 144 | + std::unique_ptr<Impl> m_Impl; |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace Diligent |
0 commit comments