/// This file contains the public C# API surface for the OpenXR package.
using System;
using System.Runtime.InteropServices;
using UnityEngine.XR.OpenXR.NativeTypes;
namespace UnityEngine.XR.OpenXR.API
{
using uint32_t = UInt32;
using UnityXRRenderTextureID = UInt32;
// Display scripting types are based off of IUnityXRDisplay v10 types.
// GUID 0x7dee4aab20644831ULL, 0x92dddc65493b46bfULL
///
/// Format for color texture.
///
public enum UnityXRRenderTextureFormat
{
///
/// R8 G8 B8 A8
///
kUnityXRRenderTextureFormatRGBA32,
///
/// B8 G8 R8 A8
///
kUnityXRRenderTextureFormatBGRA32,
///
/// R5 G6 B5
///
kUnityXRRenderTextureFormatRGB565,
///
/// R16 G16 B16 A16 signed half-float
///
kUnityXRRenderTextureFormatR16G16B16A16_SFloat,
///
/// R10 G10 B10 A2 Unorm
///
kUnityXRRenderTextureFormatRGBA1010102,
///
/// B10 G10 R10 A2 Unorm
///
kUnityXRRenderTextureFormatBGRA1010102,
///
/// R11 G11 B10 unsigned small floating point
///
kUnityXRRenderTextureFormatR11G11B10_UFloat,
///
/// Don't create a color texture, instead create a reference to another color texture that's
/// already been created. Must fill out UnityTextureData::referenceTextureId.
///
kUnityXRRenderTextureFormatReference = 64,
///
/// Don't create a whole new color texture; soft-alias the MSAA attachment, yet still
/// construct the MSAA-resolved 1x texture as unique. This allows memory sharing for MSAA
/// when mobile clients need to both autoresolve yet also share the MSAA textures.
///
kUnityXRRenderTextureFormatSoftReferenceMSAA,
///
/// No color texture.
///
kUnityXRRenderTextureFormatNone,
};
///
/// Container for different ways of representing texture data.
/// - If the format (#UnityXRRenderTextureDesc::colorFormat or
/// #UnityXRRenderTextureDesc::depthFormat) is a 'Reference' format, referenceTextureId
/// must be set.
/// - If the format is kUnityXRRenderTextureFormatSoftReferenceMSAA, both fields are used.
/// Otherwise, nativePtr is used.
///
public struct UnityXRTextureData
{
///
/// @brief Native texture ID if you've allocated it yourself. The texture ID varies by
/// graphics API. For example:
/// - GL: texture name that comes from glGenTextures
/// - DX11: ID3D11Texture2D*
/// - Vulkan: vkImage*
///
/// You can pass in #kUnityXRRenderTextureIdDontCare and have Unity allocate one for you.
///
public IntPtr nativePtr;
///
/// Texture ID to share color / depth with in the case of reference color / depth format.
///
public UnityXRRenderTextureID referenceTextureId;
};
///
/// Precision of depth texture.
///
public enum UnityXRDepthTextureFormat
{
///
/// 24-bit or greater depth texture. Unity prefers 32 bit floating point Z buffer if available on the platform.
/// DX11: DXGI_FORMAT_D32_FLOAT_S8X24_UINT
/// DX12: DXGI_FORMAT_D32_FLOAT_S8X24_UINT
/// Vulkan: VK_FORMAT_D24_UNORM_S8_UINT
/// OpenGL: Unsupported
///
kUnityXRDepthTextureFormat24bitOrGreater,
///
/// If possible, use a 16-bit texture format to save bandwidth.
/// DX11: DXGI_FORMAT_D16_UNORM
/// DX12: DXGI_FORMAT_D16_UNORM
/// Vulkan: VK_FORMAT_D16_UNORM
/// OpenGL: Unsupported
///
kUnityXRDepthTextureFormat16bit,
///
/// Don't create a depth texture, instead create a reference to another depth texture that's
/// already been created. Must fill out UnityTextureData::referenceTextureId. This is
/// useful for sharing a single depth texture between double/triple buffered color textures
/// (of the same width/height).
///
kUnityXRDepthTextureFormatReference,
///
/// No depth texture.
///
kUnityXRDepthTextureFormatNone
};
///
/// Format for shading rate texture.
///
public enum UnityXRShadingRateFormat
{
///
/// No shading rate texture.
///
kUnityXRShadingRateFormatNone,
///
/// R8G8 shading rate texture format.
///
kUnityXRShadingRateR8G8
};
///
/// Flags that can be set on a UnityXRRenderTextureDesc before creation to modify behavior.
///
public enum UnityXRRenderTextureFlags
{
///
/// By default, Unity expects texture coordinates in OpenGL mapping with (0,0) in lower left
/// hand corner. This flag specifies that (0,0) is in the upper left hand corner for this
/// texture. Unity will flip the texture at the appropriate time.
///
kUnityXRRenderTextureFlagsUVDirectionTopToBottom = 1 << 0,
///
/// This texture can be an unresolved MSAA texture. Accepting unresolved textures lowers
/// the bandwidth needed by tile-based architectures.
///
kUnityXRRenderTextureFlagsMultisampleAutoResolve = 1 << 1,
///
/// Specifies that the resources backing this texture can't be resized. No control over
/// width / height of texture. Unity might render to a separate texture of a more convenient
/// size, then blit into this one. For Example, HoloLens backbuffer size can't be changed.
///
kUnityXRRenderTextureFlagsLockedWidthHeight = 1 << 2,
///
/// Texture can only be written to and can't be read from. Unity needs to create
/// intermediate textures to do post-processing work.
///
kUnityXRRenderTextureFlagsWriteOnly = 1 << 3,
///
/// Use sRGB texture formats if possible.
///
kUnityXRRenderTextureFlagsSRGB = 1 << 4,
///
/// Opt-in to always discarding depth and resolving MSAA color to improve performance on
/// tile-based architectures at the expense of rarely-used effects which require depth
/// resolve or MSAA color store, such as camera stacking. This only affects Vulkan. Note
/// that this may break user content - use with care and consider giving the developer a way
/// to turn it off.
///
kUnityXRRenderTextureFlagsOptimizeBufferDiscards = 1 << 5,
///
/// Texture is used for storing motion-vector information.
///
kUnityXRRenderTextureFlagsMotionVectorTexture = 1 << 6,
///
/// Texture is a "GFR texture", or more generally one which uses foveation offset
///
kUnityXRRenderTextureFlagsFoveationOffset = 1 << 7,
///
/// Renderpass for this texture uses the Viewport Rect to define the Render Area
///
kUnityXRRenderTextureFlagsViewportAsRenderArea = 1 << 8,
///
/// Texture is used for storing an HDR output surface
///
kUnityXRRenderTextureFlagsHDR = 1 << 9,
};
///
/// Description of a texture that the plugin can request to be allocated via
/// IUnityXRDisplayInterface::CreateTexture. Encapsulates both color and depth surfaces.
///
public struct UnityXRRenderTextureDesc
{
///
/// Color format of the texture. Format is sRGB if kUnityXRRenderTextureFlagsSRGB is set
/// and there is an equivalent sRGB native format.
///
public UnityXRRenderTextureFormat colorFormat;
///
/// Data for color texture.
///
public UnityXRTextureData color;
///
/// Depth format of the texture.
///
public UnityXRDepthTextureFormat depthFormat;
///
/// Data for depth texture.
///
public UnityXRTextureData depth;
///
/// Shading rate texture format.
///
public UnityXRShadingRateFormat shadingRateFormat;
///
/// Data for shading rate texture.
///
public UnityXRTextureData shadingRate;
///
/// Width of the texture in pixels.
///
public uint32_t width;
///
/// Height of the texture in pixels.
///
public uint32_t height;
///
/// If requesting a texture array, the length of the texture array.
///
public uint32_t textureArrayLength;
///
/// Combination of #UnityXRRenderTextureFlags.
///
public uint32_t flags;
};
///
/// static container for XRDisplay-related scripting functionality.
///
public static class UnityXRDisplay
{
///
/// Unity will allocate the texture if needed. #kUnityXRRenderTextureIdDontCare can be set
/// on UnityXRRenderTextureDesc.nativeColorTexPtr or
/// UnityXRRenderTextureDesc.nativeDepthTexPtr.
///
public const UnityXRRenderTextureID kUnityXRRenderTextureIdDontCare = 0;
private const string k_UnityOpenXRLib = "UnityOpenXR";
///
/// Create a UnityXRRenderTextureId given a UnityXRRenderTextureDesc.
///
/// Descriptor of the texture to be created.
/// Returned Texture ID representing a unique instance of a texture.
///
/// true Successfully initialized
/// false Error
///
[DllImport(k_UnityOpenXRLib, EntryPoint = "Display_CreateTexture")]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool CreateTexture(UnityXRRenderTextureDesc desc, out uint32_t id);
}
} // namespace