Class OpenXRCustomLayerHandler<T>
Provides a base implementation for the OpenXRLayerProvider.ILayerHandler interface. You can implement the required methods of this abstract class to create a concrete layer handler.
Inherited Members
Namespace: UnityEngine.XR.OpenXR.CompositionLayers
Assembly: Unity.XR.OpenXR.dll
Syntax
public abstract class OpenXRCustomLayerHandler<T> : OpenXRLayerProvider.ILayerHandler, IDisposable where T : struct
Type Parameters
Name | Description |
---|---|
T | The native OpenXR structure of the composition layer to handle. |
Remarks
The OpenXRLayerProvider.ILayerHandler methods that this class implements handle adding and removing composition layers from native arrays, swap chain creation dispatching and other tasks required by the Unity side of the API.
The abstract methods that you must implement handle the custom aspects of your layer. These methods include:
- CreateSwapchain(LayerInfo, out SwapchainCreateInfo)
- CreateNativeLayer(LayerInfo, SwapchainCreatedOutput, out T)
- ModifyNativeLayer(LayerInfo, ref T)
You are not required to implement a custom layer handler based on this abstract class, but doing so should be easier than implementing the OpenXRLayerProvider.ILayerHandler interface in its entirety.
You must register your concrete layer handler object with RegisterLayerHandler(Type, ILayerHandler).
Constructors
OpenXRCustomLayerHandler()
Initializes and returns an instance of this OpenXRCustomLayerHandler<T>
while also setting the singleton instance member.
Declaration
protected OpenXRCustomLayerHandler()
Fields
Instance
Singleton instance of this specific handler.
Declaration
protected static OpenXRCustomLayerHandler<T> Instance
Field Value
Type | Description |
---|---|
OpenXRCustomLayerHandler<T> |
actionsForMainThread
Thread safe queue used to dispatch callbacks that may come from other threads such as the swapchain creation on the graphics thread.
Declaration
protected ConcurrentQueue<Action> actionsForMainThread
Field Value
Type | Description |
---|---|
ConcurrentQueue<Action> |
m_nativeLayers
Mapping of instance ids and native layer structs to help determine what layers are currently set to be active.
Declaration
protected Dictionary<int, T> m_nativeLayers
Field Value
Type | Description |
---|---|
Dictionary<int, T> |
Methods
ActiveNativeLayer(LayerInfo, ref T)
Override this method to modify a native composition layer struct in response to when it is active. An active compositon layer will invoke this every frame.
Declaration
protected virtual bool ActiveNativeLayer(CompositionLayerManager.LayerInfo layerInfo, ref T nativeLayer)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer that is active. |
T | nativeLayer | A reference to the native OpenXR structure of the composition layer that is active. |
Returns
Type | Description |
---|---|
bool | Bool indicating success or failure. A failure case will result in the native composition layer struct not being added into the final XrFrameEndInfo struct. |
CreateLayer(LayerInfo)
Implements the OpenXRLayerProvider.ILayerHandler method that is called by the OpenXRLayerProvider when a new layer has been created. This implementation triggers the creation of a swapchain before the actual native layer struct is created.
Declaration
public void CreateLayer(CompositionLayerManager.LayerInfo layerInfo)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer being created. |
CreateNativeLayer(LayerInfo, SwapchainCreatedOutput, out T)
Declaration
protected abstract bool CreateNativeLayer(CompositionLayerManager.LayerInfo layerInfo, OpenXRCustomLayerHandler<T>.SwapchainCreatedOutput swapchainOutput, out T nativeLayer)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | |
OpenXRCustomLayerHandler<T>.SwapchainCreatedOutput | swapchainOutput | |
T | nativeLayer |
Returns
Type | Description |
---|---|
bool |
CreateSwapchain(LayerInfo, out SwapchainCreateInfo)
Declaration
protected abstract bool CreateSwapchain(CompositionLayerManager.LayerInfo layerInfo, out OpenXRCustomLayerHandler<T>.SwapchainCreateInfo swapchainCreateInfo)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | |
OpenXRCustomLayerHandler<T>.SwapchainCreateInfo | swapchainCreateInfo |
Returns
Type | Description |
---|---|
bool |
CreateSwapchainAsync(LayerInfo)
Calls CreateSwapchain(LayerInfo, out SwapchainCreateInfo) to create a OpenXRCustomLayerHandler<T>.SwapchainCreateInfo struct that is then passed to the UnityOpenXR lib to actually create the swapchain on the graphics thread. The static OnCreatedSwapchainCallback(int, ulong) method is passed as a callback and invoked when the swapchain has been created.
Declaration
protected virtual void CreateSwapchainAsync(CompositionLayerManager.LayerInfo layerInfo)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer that was just created. |
Dispose()
Implements method from IDisposable that is called by the OpenXRLayerProvider when this custom layer handler instance is disposed.
Declaration
public void Dispose()
Dispose(bool)
Clears all maps and disposes any created native arrays.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | Determines if this method was called from the Dispose() method or the finalizer. |
~OpenXRCustomLayerHandler()
Declaration
protected ~OpenXRCustomLayerHandler()
ModifyLayer(LayerInfo)
Implements the OpenXRLayerProvider.ILayerHandler method that is called by the OpenXRLayerProvider when a layer or attached extension has been modified. This implementation asks the subclass for any changes that must be made to the layer via ModifyNativeLayer(LayerInfo, ref T) by sending a reference to the native layer struct.
Declaration
public virtual void ModifyLayer(CompositionLayerManager.LayerInfo layerInfo)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer that was modified. |
ModifyNativeLayer(LayerInfo, ref T)
Override this method to modify a native composition layer struct in response to changes on the associated Unity.XR.CompositionLayers.Layers.LayerData object or any extension components on the Unity.XR.CompositionLayers.CompositionLayer GameObject.
Declaration
protected abstract bool ModifyNativeLayer(CompositionLayerManager.LayerInfo layerInfo, ref T nativeLayer)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer that was modified. |
T | nativeLayer | A reference to the native OpenXR structure of the composition layer that was modified. The concrete implementation of this method should update the values of the structure as appropriate. |
Returns
Type | Description |
---|---|
bool | A bool indicating success or failure. |
Remarks
You must reinitialize the Next pointer with OpenXRLayerUtility.GetExtensionsChain(CompositionLayerManager.LayerInfo, Unity.XR.CompositionLayers.CompositionLayerExtension.ExtensionTarget) to get any potential updates from extension components.
Examples
Modifies an XrCompositionLayerQuad struct with new transform and extension information.
protected override bool ModifyNativeLayer(CompositionLayerManager.LayerInfo layerInfo, ref XrCompositionLayerQuad nativeLayer)
{
var data = layerInfo.Layer.LayerData as QuadLayerData;
var transform = layerInfo.Layer.GetComponent<Transform>();
nativeLayer.Pose = new XrPosef(transform.position, transform.rotation);
nativeLayer.Size = new XrExtend2Df()
{
width = data.GetScaledSize(transform.lossyScale).x,
height = data.GetScaledSize(transform.lossyScale).y
};
unsafe
{
nativeLayer.Next = OpenXRLayerUtility.GetExtensionsChain(layerInfo, CompositionLayerExtension.ExtensionTarget.Layer);
}
return true;
}
OnCreatedSwapchain(LayerInfo, SwapchainCreatedOutput)
This method is dispatched to the main thread inside OnCreatedSwapchainCallback(int, ulong) and asks this subclass to create the native layer struct by invoking CreateNativeLayer(LayerInfo, SwapchainCreatedOutput, out T).
Declaration
protected virtual void OnCreatedSwapchain(CompositionLayerManager.LayerInfo layerInfo, OpenXRCustomLayerHandler<T>.SwapchainCreatedOutput swapchainOutput)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer that was just created. |
OpenXRCustomLayerHandler<T>.SwapchainCreatedOutput | swapchainOutput | Information regarding the swapchain that was created for this layer, such as the associated swapchain handle. |
OnUpdate()
Implements the OpenXRLayerProvider.ILayerHandler method that is called by the OpenXRLayerProvider during the Unity update loop.
Declaration
public virtual void OnUpdate()
Remarks
This implementation carries out two tasks. It dequeues actions for the main thread like dispatch when
the swapchain has been
created and it adds all the active layers to the endFrameInfo
struct in the native UnityOpenXR lib.
RemoveLayer(int)
Implements the OpenXRLayerProvider.ILayerHandler method that is called by the OpenXRLayerProvider when a layer is destroyed or disabled.
Declaration
public virtual void RemoveLayer(int removedLayerId)
Parameters
Type | Name | Description |
---|---|---|
int | removedLayerId | The instance id of the CompositionLayer component that was removed. |
ResizeNativeArrays()
Ensures that the native arrays are of the same size as the m_nativeLayers map.
Declaration
protected virtual void ResizeNativeArrays()
SetActiveLayer(LayerInfo)
Implements the OpenXRLayerProvider.ILayerHandler method that is called by the OpenXRLayerProvider when a layer is considered to be currently active.
Declaration
public virtual void SetActiveLayer(CompositionLayerManager.LayerInfo layerInfo)
Parameters
Type | Name | Description |
---|---|---|
LayerInfo | layerInfo | Container for the instance id and CompositionLayer component of the composition layer being set to active. |