上传YomovSDK
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace VIVE.OpenXR.Feature
|
||||
{
|
||||
public interface IViveFeatureWrapper
|
||||
{
|
||||
/// <summary>
|
||||
/// OnInstanceCreate might be called multiple times. Because many features might be using the same instance.
|
||||
/// </summary>
|
||||
/// <param name="xrInstance"></param>
|
||||
/// <param name="xrGetInstanceProcAddr"></param>
|
||||
/// <returns></returns>
|
||||
public bool OnInstanceCreate(XrInstance xrInstance, IntPtr xrGetInstanceProcAddr);
|
||||
|
||||
/// <summary>
|
||||
/// OnInstanceDestroy might be called multiple times. Because many features might be using the same instance.
|
||||
/// </summary>
|
||||
public void OnInstanceDestroy();
|
||||
}
|
||||
|
||||
public class ViveFeatureWrapperBase<T> where T : ViveFeatureWrapperBase<T>, new()
|
||||
{
|
||||
private static readonly Lazy<T> lazyInstance = new Lazy<T>(() => new T());
|
||||
|
||||
public static T Instance => lazyInstance.Value;
|
||||
|
||||
// Set true in yourfeature's OnInstanceCreate
|
||||
public bool IsInited { get; protected set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// If the feature is inited not successfully, Set this true. Use to avoid multiple inits.
|
||||
/// </summary>
|
||||
public bool TryInited { get; protected set; } = false;
|
||||
|
||||
public OpenXRHelper.xrGetInstanceProcAddrDelegate xrGetInstanceProcAddr;
|
||||
|
||||
/// <summary>
|
||||
/// Complete the xrGetInstanceProcAddr by set the pointer received in OnInstanceCreate
|
||||
/// </summary>
|
||||
/// <param name="intPtr"></param>
|
||||
public void SetGetInstanceProcAddrPtr(IntPtr intPtr)
|
||||
{
|
||||
if (intPtr == null || intPtr == IntPtr.Zero)
|
||||
throw new Exception("xrGetInstanceProcAddr is null");
|
||||
|
||||
xrGetInstanceProcAddr = Marshal.GetDelegateForFunctionPointer<OpenXRHelper.xrGetInstanceProcAddrDelegate>(intPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a27dc5505cdb29347aeda46676cedaa8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
228
Packages/com.htc.upm.vive.openxr/Runtime/Common/MemoryTools.cs
Normal file
228
Packages/com.htc.upm.vive.openxr/Runtime/Common/MemoryTools.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
internal static class MemoryTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Make sure the input ptr is a OpenXR XrBaseStructure derived struct.
|
||||
/// </summary>
|
||||
/// <param name="ptr">the struct to get its next.</param>
|
||||
/// <returns>the next's value</returns>
|
||||
public static unsafe IntPtr GetNext(IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return IntPtr.Zero;
|
||||
//Profiler.BeginSample("GetNext");
|
||||
XrBaseStructure* ptrToStruct = (XrBaseStructure*)ptr.ToPointer();
|
||||
//Profiler.EndSample();
|
||||
return ptrToStruct->next;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make sure the input ptr is a OpenXR XrBaseStructure derived struct.
|
||||
/// </summary>
|
||||
/// <param name="ptr">the struct to get its type</param>
|
||||
/// <returns>the struct's type</returns>
|
||||
public static unsafe XrStructureType GetType(IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
throw new Exception("The input pointer is null.");
|
||||
|
||||
//Profiler.BeginSample("GetType");
|
||||
XrBaseStructure* ptrToStruct = (XrBaseStructure*)ptr.ToPointer();
|
||||
//Profiler.EndSample();
|
||||
return ptrToStruct->type;
|
||||
}
|
||||
|
||||
public static unsafe XrBaseStructure ToBaseStructure(IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
throw new Exception("The input pointer is null.");
|
||||
|
||||
//Profiler.BeginSample("ToBaseStructure");
|
||||
XrBaseStructure* ptrToStruct = (XrBaseStructure*)ptr.ToPointer();
|
||||
//Profiler.EndSample();
|
||||
return *ptrToStruct;
|
||||
}
|
||||
|
||||
public static unsafe T PtrToStructure<T>(IntPtr ptr) where T : unmanaged
|
||||
{
|
||||
//Profiler.BeginSample("PtrToStructure");
|
||||
// Not to use Marshal.PtrToStructure<T> because it is slow.
|
||||
T t = default; // Use new T() will cause GC alloc.
|
||||
Buffer.MemoryCopy((void*)ptr, &t, sizeof(T), sizeof(T));
|
||||
//Profiler.EndSample();
|
||||
return t;
|
||||
}
|
||||
|
||||
public static unsafe void PtrToStructure<T>(IntPtr ptr, ref T t) where T : unmanaged
|
||||
{
|
||||
//Profiler.BeginSample("PtrToStructure");
|
||||
fixed (T* destinationPtr = &t)
|
||||
{
|
||||
Buffer.MemoryCopy((void*)ptr, destinationPtr, sizeof(T), sizeof(T));
|
||||
}
|
||||
//Profiler.EndSample();
|
||||
}
|
||||
|
||||
public static unsafe void StructureToPtr<T>(T t, IntPtr ptr) where T : unmanaged
|
||||
{
|
||||
//Profiler.BeginSample("StructureToPtr");
|
||||
// Not to use Marshal.StructureToPtr<T> because it is slow.
|
||||
Buffer.MemoryCopy(&t, (void*)ptr, sizeof(T), sizeof(T));
|
||||
//Profiler.EndSample();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert the enum array to IntPtr. Should call <see cref="ReleaseRawMemory(IntPtr)"/> after use.
|
||||
/// </summary>
|
||||
/// <param name="array"></param>
|
||||
/// <returns></returns>
|
||||
public static unsafe IntPtr ToIntPtr<T>(T[] array) where T : Enum
|
||||
{
|
||||
int size = sizeof(int) * array.Length;
|
||||
IntPtr ptr = Marshal.AllocHGlobal(size);
|
||||
|
||||
int* intPtr = (int*)ptr.ToPointer();
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
// Convert enum to int. This has better performance than Convert.ToInt32.
|
||||
intPtr[i] = (int)(object)array[i];
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert the struct to IntPtr. Should call <see cref="ReleaseRawMemory(IntPtr)"/> after use.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="structure"></param>
|
||||
/// <returns></returns>
|
||||
public static IntPtr ToIntPtr<T>(T structure) where T : struct
|
||||
{
|
||||
int size = Marshal.SizeOf(structure);
|
||||
IntPtr ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.StructureToPtr(structure, ptr, true);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make the same size raw buffer from input array.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Data type could be primitive type or struct. Should call <see cref="ReleaseRawMemory(IntPtr)"/> after use.</typeparam>
|
||||
/// <param name="refArray">The data array</param>
|
||||
/// <returns>The memory handle. Should release by <see cref="ReleaseRawMemory(IntPtr)"/></returns>
|
||||
public static unsafe IntPtr MakeRawMemory<T>(T[] refArray) where T : unmanaged
|
||||
{
|
||||
int size = Marshal.SizeOf(typeof(T)) * refArray.Length;
|
||||
return Marshal.AllocHGlobal(size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy the raw memory to the array. You should make sure the array has the same size as the raw memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convert the memory to this type array.</typeparam>
|
||||
/// <param name="array">The output array.</param>
|
||||
/// <param name="raw">The data source in raw memory form.</param>
|
||||
/// <param name="count">Specify the copy count. Count should be less than array length.</param>
|
||||
public static unsafe void CopyFromRawMemory<T>(T[] array, IntPtr raw, int count = 0) where T : unmanaged
|
||||
{
|
||||
//Profiler.BeginSample("CopyFromRawMemory");
|
||||
int N = array.Length;
|
||||
if (count > 0 && count < array.Length)
|
||||
N = count;
|
||||
int step = sizeof(T);
|
||||
int bufferSize = step * N;
|
||||
|
||||
// Pin array's address. Prevent GC move it.
|
||||
fixed (T* destPtr = array)
|
||||
{
|
||||
T* sourcePtr = (T*)raw.ToPointer();
|
||||
Buffer.MemoryCopy(sourcePtr, destPtr, bufferSize, bufferSize);
|
||||
}
|
||||
//Profiler.EndSample();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy all raw memory to the array. This has higher performance than <see cref="CopyFromRawMemory"/>.
|
||||
/// Use this method if you have frequent update requirements.
|
||||
/// You need prepare a byte buffer to store the raw memory. The byte buffer size should be tSize * array.Length.
|
||||
/// tSize is used for checking the byte buffer size. If tSize is 0, it will use Marshal.SizeOf(typeof(T)).
|
||||
/// You can save the size at your size to avoid the Marshal.Sizeof(typeof(T)) call repeatedly.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convert the memory to this type array.</typeparam>
|
||||
/// <param name="array">The output array.</param>
|
||||
/// <param name="raw">The data source in raw memory form.</param>
|
||||
public static unsafe void CopyAllFromRawMemory<T>(T[] array, IntPtr raw) where T : unmanaged
|
||||
{
|
||||
#if DEBUG
|
||||
if (array == null)
|
||||
throw new ArgumentNullException(nameof(array), "Output array cannot be null.");
|
||||
if (raw == IntPtr.Zero)
|
||||
throw new ArgumentNullException(nameof(raw), "Raw memory pointer cannot be null.");
|
||||
#endif
|
||||
|
||||
//Profiler.BeginSample("CopyAllFromRawMemory");
|
||||
int elementSize = sizeof(T);
|
||||
int requiredBufferSize = elementSize * array.Length;
|
||||
|
||||
// Pin array's address. Prevent GC move it.
|
||||
fixed (T* destPtr = array)
|
||||
{
|
||||
T* sourcePtr = (T*)raw.ToPointer();
|
||||
Buffer.MemoryCopy(sourcePtr, destPtr, requiredBufferSize, requiredBufferSize);
|
||||
}
|
||||
//Profiler.EndSample();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make the same size raw buffer from input array. Make sure the raw has enough size.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convert this type array to raw memory.</typeparam>
|
||||
/// <param name="raw">The output data in raw memory form</param>
|
||||
/// <param name="array">The data source</param>
|
||||
public static unsafe void CopyToRawMemory<T>(IntPtr raw, T[] array) where T : unmanaged
|
||||
{
|
||||
//Profiler.BeginSample("CopyToRawMemory");
|
||||
int step = sizeof(T);
|
||||
int bufferSize = step * array.Length;
|
||||
// Pin array's address. Prevent GC move it.
|
||||
fixed (T* destPtr = array)
|
||||
{
|
||||
void* ptr = raw.ToPointer();
|
||||
Buffer.MemoryCopy(destPtr, ptr, bufferSize, bufferSize);
|
||||
}
|
||||
//Profiler.EndSample();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release the raw memory handle which is created by <see cref="MakeRawMemory{T}(T[])"/>
|
||||
/// </summary>
|
||||
/// <param name="ptr"></param>
|
||||
public static void ReleaseRawMemory(IntPtr ptr)
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a pointer in the next chain. Make sure the input next pointer is a OpenXR XrBaseStructure derived struct.
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="next"></param>
|
||||
/// <returns>true if exist</returns>
|
||||
public static bool HasPtrInNextChain(IntPtr target, IntPtr next)
|
||||
{
|
||||
while (next != IntPtr.Zero)
|
||||
{
|
||||
if (next == target)
|
||||
return true;
|
||||
next = GetNext(next);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9a887cb158a37cf45b17458a4f27d7ee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,278 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.OpenXR;
|
||||
using UnityEngine.XR.OpenXR.Features;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.XR.OpenXR.Features;
|
||||
#endif
|
||||
namespace VIVE.OpenXR.Enterprise
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[OpenXRFeature(UiName = "VIVE XR Enterprise Command",
|
||||
Desc = "Support Enterprise request with special command",
|
||||
Company = "HTC",
|
||||
OpenxrExtensionStrings = kOpenxrExtensionString,
|
||||
Version = "0.1",
|
||||
BuildTargetGroups = new[] { BuildTargetGroup.Android },
|
||||
FeatureId = featureId,
|
||||
Hidden = true
|
||||
)]
|
||||
#endif
|
||||
public class ViveEnterpriseCommand : OpenXRFeature
|
||||
{
|
||||
#region Log
|
||||
const string LOG_TAG = "VIVE.OpenXR.Enterprise.Command ";
|
||||
private static void DEBUG(String msg) { Debug.Log(LOG_TAG + msg); }
|
||||
private static void ERROR(String msg) { Debug.LogError(LOG_TAG + msg); }
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// The feature id string. This is used to give the feature a well known id for reference.
|
||||
/// </summary>
|
||||
public const string featureId = "vive.openxr.feature.enterprise.command";
|
||||
|
||||
/// <summary>
|
||||
/// The extension string.
|
||||
/// </summary>
|
||||
public const string kOpenxrExtensionString = "XR_HTC_enterprise_command";
|
||||
|
||||
#region OpenXR Life Cycle
|
||||
private static bool m_XrInstanceCreated = false;
|
||||
private static bool m_XrSessionCreated = false;
|
||||
private static XrInstance m_XrInstance = 0;
|
||||
private static XrSession m_XrSession = 0;
|
||||
private static XrSystemId m_XrSystemId = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Called when <see href="https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#xrCreateInstance">xrCreateInstance</see> is done.
|
||||
/// </summary>
|
||||
/// <param name="xrInstance">The created instance.</param>
|
||||
/// <returns>True for valid <see cref="XrInstance">XrInstance</see></returns>
|
||||
protected override bool OnInstanceCreate(ulong xrInstance)
|
||||
{
|
||||
if (!OpenXRRuntime.IsExtensionEnabled(kOpenxrExtensionString))
|
||||
{
|
||||
ERROR($"OnInstanceCreate() {kOpenxrExtensionString} is NOT enabled.");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_XrInstanceCreated = true;
|
||||
m_XrInstance = xrInstance;
|
||||
DEBUG($"OnInstanceCreate() {m_XrInstance}");
|
||||
|
||||
return GetXrFunctionDelegates(m_XrInstance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when <see href="https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#xrDestroyInstance">xrDestroyInstance</see> is done.
|
||||
/// </summary>
|
||||
/// <param name="xrInstance">The instance to destroy.</param>
|
||||
protected override void OnInstanceDestroy(ulong xrInstance)
|
||||
{
|
||||
if (m_XrInstance == xrInstance)
|
||||
{
|
||||
m_XrInstanceCreated = false;
|
||||
m_XrInstance = 0;
|
||||
}
|
||||
DEBUG($"OnInstanceDestroy() {xrInstance}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the <see cref="XrSystemId">XrSystemId</see> retrieved by <see href="https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#xrGetSystem">xrGetSystem</see> is changed.
|
||||
/// </summary>
|
||||
/// <param name="xrSystem">The system id.</param>
|
||||
protected override void OnSystemChange(ulong xrSystem)
|
||||
{
|
||||
m_XrSystemId = xrSystem;
|
||||
DEBUG($"OnSystemChange() {m_XrSystemId}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when <see href="https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#xrCreateSession">xrCreateSession</see> is done.
|
||||
/// </summary>
|
||||
/// <param name="xrSession">The created session ID.</param>
|
||||
protected override void OnSessionCreate(ulong xrSession)
|
||||
{
|
||||
m_XrSession = xrSession;
|
||||
m_XrSessionCreated = true;
|
||||
DEBUG($"OnSessionCreate() {m_XrSession}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when <see href="https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#xrDestroySession">xrDestroySession</see> is done.
|
||||
/// </summary>
|
||||
/// <param name="xrSession">The session ID to destroy.</param>
|
||||
protected override void OnSessionDestroy(ulong xrSession)
|
||||
{
|
||||
DEBUG($"OnSessionDestroy() {xrSession}");
|
||||
|
||||
if (m_XrSession == xrSession)
|
||||
{
|
||||
m_XrSession = 0;
|
||||
m_XrSessionCreated = false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region OpenXR function delegates
|
||||
/// xrEnterpriseCommandHTC
|
||||
private static ViveEnterpriseCommandHelper.xrEnterpriseCommandHTCDelegate xrEnterpriseCommandHTC;
|
||||
/// xrGetInstanceProcAddr
|
||||
private static OpenXRHelper.xrGetInstanceProcAddrDelegate XrGetInstanceProcAddr;
|
||||
|
||||
/// <summary>
|
||||
/// Enterprise command request for special functionality.
|
||||
/// </summary>
|
||||
/// <param name="request">The request of enterprise command</param>
|
||||
/// <param name="result">The result of enterprise command</param>
|
||||
/// <returns>Return XR_SUCCESS if request successfully. False otherwise.</returns>
|
||||
private static XrResult EnterpriseCommandHTC(XrEnterpriseCommandBufferHTC request, ref XrEnterpriseCommandBufferHTC result)
|
||||
{
|
||||
if (!m_XrSessionCreated)
|
||||
{
|
||||
ERROR("EnterpriseCommandHTC() XR_ERROR_SESSION_LOST.");
|
||||
return XrResult.XR_ERROR_SESSION_LOST;
|
||||
}
|
||||
if (!m_XrInstanceCreated)
|
||||
{
|
||||
ERROR("EnterpriseCommandHTC() XR_ERROR_INSTANCE_LOST.");
|
||||
return XrResult.XR_ERROR_INSTANCE_LOST;
|
||||
}
|
||||
|
||||
DEBUG($"EnterpriseCommandHTC() code: {request.code}, data: {CharArrayToString(request.data)}");
|
||||
return xrEnterpriseCommandHTC(m_XrSession, request, ref result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the OpenXR function via XrInstance.
|
||||
/// </summary>
|
||||
/// <param name="xrInstance">The XrInstance is provided by the Unity OpenXR Plugin.</param>
|
||||
/// <returns>Return true if request successfully. False otherwise.</returns>
|
||||
private bool GetXrFunctionDelegates(XrInstance xrInstance)
|
||||
{
|
||||
/// xrGetInstanceProcAddr
|
||||
if (xrGetInstanceProcAddr != null && xrGetInstanceProcAddr != IntPtr.Zero)
|
||||
{
|
||||
DEBUG("Get function pointer of xrGetInstanceProcAddr.");
|
||||
XrGetInstanceProcAddr = Marshal.GetDelegateForFunctionPointer(
|
||||
xrGetInstanceProcAddr,
|
||||
typeof(OpenXRHelper.xrGetInstanceProcAddrDelegate)) as OpenXRHelper.xrGetInstanceProcAddrDelegate;
|
||||
}
|
||||
else
|
||||
{
|
||||
ERROR("xrGetInstanceProcAddr");
|
||||
return false;
|
||||
}
|
||||
|
||||
/// xrEnterpriseCommandHTC
|
||||
if (XrGetInstanceProcAddr(xrInstance, "xrEnterpriseCommandHTC", out IntPtr funcPtr) == XrResult.XR_SUCCESS)
|
||||
{
|
||||
if (funcPtr != IntPtr.Zero)
|
||||
{
|
||||
DEBUG("Get function pointer of xrEnterpriseCommandHTC.");
|
||||
xrEnterpriseCommandHTC = Marshal.GetDelegateForFunctionPointer(
|
||||
funcPtr,
|
||||
typeof(ViveEnterpriseCommandHelper.xrEnterpriseCommandHTCDelegate)) as ViveEnterpriseCommandHelper.xrEnterpriseCommandHTCDelegate;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ERROR("xrEnterpriseCommandHTC");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
private const int kCharLength = 256;
|
||||
private const char kEndChar = '\0';
|
||||
private static char[] charArray = new char[kCharLength];
|
||||
|
||||
/// <summary>
|
||||
/// Request special feature with command, it should take code and command string.
|
||||
/// </summary>
|
||||
/// <param name="requestCode">The type of request code is integer.</param>
|
||||
/// <param name="requestCommand">The maximum length of request command is 256.</param>
|
||||
/// <param name="resultCode">The output of result code.</param>
|
||||
/// <param name="resultCommand">The output of result command.</param>
|
||||
/// <returns>Return true if request successfully. False otherwise.</returns>
|
||||
public static bool CommandRequest(int requestCode, string requestCommand, out int resultCode, out string resultCommand)
|
||||
{
|
||||
resultCode = 0;
|
||||
resultCommand = string.Empty;
|
||||
XrEnterpriseCommandBufferHTC request = new XrEnterpriseCommandBufferHTC(requestCode, StringToCharArray(requestCommand));
|
||||
XrEnterpriseCommandBufferHTC result = new XrEnterpriseCommandBufferHTC(resultCode, StringToCharArray(resultCommand));
|
||||
if (EnterpriseCommandHTC(request, ref result) == XrResult.XR_SUCCESS)
|
||||
{
|
||||
resultCode = result.code;
|
||||
resultCommand = CharArrayToString(result.data);
|
||||
DEBUG($"CommandRequest Result code: {resultCode}, data: {resultCommand}");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
private static char[] StringToCharArray(string str)
|
||||
{
|
||||
Array.Clear(charArray, 0, kCharLength);
|
||||
if (!string.IsNullOrEmpty(str))
|
||||
{
|
||||
int arrayLength = Math.Min(str.Length, kCharLength);
|
||||
for (int i = 0; i < arrayLength; i++)
|
||||
{
|
||||
charArray[i] = str[i];
|
||||
}
|
||||
charArray[kCharLength - 1] = kEndChar;
|
||||
}
|
||||
return charArray;
|
||||
}
|
||||
|
||||
private static string CharArrayToString(char[] charArray)
|
||||
{
|
||||
int actualLength = Array.FindIndex(charArray, c => c == kEndChar);
|
||||
if (actualLength == -1)
|
||||
{
|
||||
actualLength = charArray.Length;
|
||||
}
|
||||
|
||||
return new string(charArray, 0, actualLength);
|
||||
}
|
||||
}
|
||||
|
||||
#region Helper
|
||||
public struct XrEnterpriseCommandBufferHTC
|
||||
{
|
||||
public Int32 code;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
|
||||
public char[] data;
|
||||
|
||||
public XrEnterpriseCommandBufferHTC(int in_code, char[] in_data)
|
||||
{
|
||||
code = (Int32)in_code;
|
||||
data = new char[in_data.Length];
|
||||
Array.Copy(in_data, data, in_data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public class ViveEnterpriseCommandHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// The function delegate of xrEnterpriseCommandHTC.
|
||||
/// </summary>
|
||||
/// <param name="session">An <see cref="XrSession">XrSession</see> in which the enterprise command will be active.</param>
|
||||
/// <param name="request">The request of enterprise command</param>
|
||||
/// <param name="result">The result of enterprise command</param>
|
||||
/// <returns>Return XR_SUCCESS if request successfully. False otherwise.</returns>
|
||||
public delegate XrResult xrEnterpriseCommandHTCDelegate(
|
||||
XrSession session,
|
||||
XrEnterpriseCommandBufferHTC request,
|
||||
ref XrEnterpriseCommandBufferHTC result);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5b2125d5dc73694eaed34e97a0962e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,252 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace VIVE.OpenXR.Feature
|
||||
{
|
||||
/// <summary>
|
||||
/// To use this wrapper, you need to call CommonWrapper.Instance.OnInstanceCreate() in your feature's OnInstanceCreate(),
|
||||
/// and call CommonWrapper.Instance.OnInstanceDestroy() in your feature's OnInstanceDestroy().
|
||||
///
|
||||
/// Note:
|
||||
/// In Standardalone's OpenXR MockRuntime, the CreateSwapchain and EnumerateSwapchainImages will work and return success,
|
||||
/// but the images's native pointer will be null.
|
||||
/// </summary>
|
||||
internal class CommonWrapper : ViveFeatureWrapperBase<CommonWrapper>, IViveFeatureWrapper
|
||||
{
|
||||
const string TAG = "CommonWrapper";
|
||||
OpenXRHelper.xrGetSystemPropertiesDelegate XrGetSystemProperties;
|
||||
OpenXRHelper.xrCreateSwapchainDelegate XrCreateSwapchain;
|
||||
OpenXRHelper.xrDestroySwapchainDelegate XrDestroySwapchain;
|
||||
OpenXRHelper.xrEnumerateSwapchainFormatsDelegate XrEnumerateSwapchainFormats;
|
||||
OpenXRHelper.xrEnumerateSwapchainImagesDelegate XrEnumerateSwapchainImages;
|
||||
OpenXRHelper.xrWaitSwapchainImageDelegate XrWaitSwapchainImage;
|
||||
OpenXRHelper.xrAcquireSwapchainImageDelegate XrAcquireSwapchainImage;
|
||||
OpenXRHelper.xrReleaseSwapchainImageDelegate XrReleaseSwapchainImage;
|
||||
|
||||
/// <summary>
|
||||
/// In feature's OnInstanceCreate(), call CommonWrapper.Instance.OnInstanceCreate() for init common APIs.
|
||||
/// </summary>
|
||||
/// <param name="xrInstance">Passed in feature's OnInstanceCreate.</param>
|
||||
/// <param name="xrGetInstanceProcAddr">Pass OpenXRFeature.xrGetInstanceProcAddr in.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception">If input data not valid.</exception>
|
||||
public bool OnInstanceCreate(XrInstance xrInstance, IntPtr xrGetInstanceProcAddrPtr)
|
||||
{
|
||||
if (IsInited) return true;
|
||||
if (TryInited) return false;
|
||||
TryInited = true;
|
||||
|
||||
if (xrInstance == 0)
|
||||
throw new Exception("CommonWrapper: xrInstance is null");
|
||||
|
||||
Log.D(TAG, "OnInstanceCreate()");
|
||||
SetGetInstanceProcAddrPtr(xrGetInstanceProcAddrPtr);
|
||||
|
||||
bool ret = true;
|
||||
IntPtr funcPtr = IntPtr.Zero;
|
||||
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrGetSystemProperties", out XrGetSystemProperties);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrCreateSwapchain", out XrCreateSwapchain);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrDestroySwapchain", out XrDestroySwapchain);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrEnumerateSwapchainFormats", out XrEnumerateSwapchainFormats);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrEnumerateSwapchainImages", out XrEnumerateSwapchainImages);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrWaitSwapchainImage", out XrWaitSwapchainImage);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrAcquireSwapchainImage", out XrAcquireSwapchainImage);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrReleaseSwapchainImage", out XrReleaseSwapchainImage);
|
||||
|
||||
if (!ret)
|
||||
throw new Exception("CommonWrapper: Get function pointers failed.");
|
||||
|
||||
IsInited = ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// In feature's OnInstanceDestroy(), call CommonWrapper.Instance.OnInstanceDestroy() for disable common APIs.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void OnInstanceDestroy()
|
||||
{
|
||||
// Do not destroy twice
|
||||
if (IsInited == false) return;
|
||||
IsInited = false;
|
||||
XrGetSystemProperties = null;
|
||||
Log.D(TAG, "OnInstanceDestroy()");
|
||||
}
|
||||
|
||||
public XrResult GetInstanceProcAddr(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (IsInited == false || xrGetInstanceProcAddr == null)
|
||||
{
|
||||
function = IntPtr.Zero;
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
return xrGetInstanceProcAddr(instance, name, out function);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper function to get system properties. Need input your features' xrInstance and xrSystemId. Fill the system properites in next for you feature.
|
||||
/// See <see href="https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#xrGetSystemProperties">xrGetSystemProperties</see>
|
||||
/// </summary>
|
||||
/// <param name="instance"></param>
|
||||
/// <param name="systemId"></param>
|
||||
/// <param name="properties"></param>
|
||||
/// <returns></returns>
|
||||
public XrResult GetSystemProperties(XrInstance instance, XrSystemId systemId, ref XrSystemProperties properties)
|
||||
{
|
||||
if (IsInited == false || XrGetSystemProperties == null)
|
||||
{
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
return XrGetSystemProperties(instance, systemId, ref properties);
|
||||
}
|
||||
|
||||
|
||||
public XrResult GetProperties<T>(XrInstance instance, XrSystemId systemId, ref T featureProperty)
|
||||
{
|
||||
XrSystemProperties systemProperties = new XrSystemProperties();
|
||||
systemProperties.type = XrStructureType.XR_TYPE_SYSTEM_PROPERTIES;
|
||||
systemProperties.next = Marshal.AllocHGlobal(Marshal.SizeOf(featureProperty));
|
||||
|
||||
long offset = 0;
|
||||
if (IntPtr.Size == 4)
|
||||
offset = systemProperties.next.ToInt32();
|
||||
else
|
||||
offset = systemProperties.next.ToInt64();
|
||||
|
||||
IntPtr pdPropertiesPtr = new IntPtr(offset);
|
||||
Marshal.StructureToPtr(featureProperty, pdPropertiesPtr, false);
|
||||
|
||||
var ret = GetSystemProperties(instance, systemId, ref systemProperties);
|
||||
if (ret == XrResult.XR_SUCCESS)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
offset = systemProperties.next.ToInt32();
|
||||
else
|
||||
offset = systemProperties.next.ToInt64();
|
||||
|
||||
pdPropertiesPtr = new IntPtr(offset);
|
||||
featureProperty = Marshal.PtrToStructure<T>(pdPropertiesPtr);
|
||||
}
|
||||
|
||||
Marshal.FreeHGlobal(systemProperties.next);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public XrResult CreateSwapchain(XrSession session, ref XrSwapchainCreateInfo createInfo, out XrSwapchain swapchain)
|
||||
{
|
||||
if (IsInited == false || XrCreateSwapchain == null)
|
||||
{
|
||||
swapchain = default;
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
return XrCreateSwapchain(session, ref createInfo, out swapchain);
|
||||
}
|
||||
|
||||
public XrResult DestroySwapchain(XrSwapchain swapchain)
|
||||
{
|
||||
if (IsInited == false || XrDestroySwapchain == null)
|
||||
{
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
return XrDestroySwapchain(swapchain);
|
||||
}
|
||||
|
||||
public XrResult EnumerateSwapchainFormats(XrSession session, uint formatCapacityInput, ref uint formatCountOutput, ref long[] formats)
|
||||
{
|
||||
if (IsInited == false || XrEnumerateSwapchainFormats == null)
|
||||
{
|
||||
formatCountOutput = 0;
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
if (formatCapacityInput != 0 && (formats == null || formats.Length < formatCapacityInput))
|
||||
return XrResult.XR_ERROR_SIZE_INSUFFICIENT;
|
||||
|
||||
if (formatCapacityInput == 0)
|
||||
{
|
||||
Log.D(TAG, "EnumerateSwapchainFormats(ci=" + formatCapacityInput + ")");
|
||||
return XrEnumerateSwapchainFormats(session, 0, ref formatCountOutput, IntPtr.Zero);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.D(TAG, "EnumerateSwapchainFormats(ci=" + formatCapacityInput + ", formats=long[" + formats.Length + "])");
|
||||
IntPtr formatsPtr = MemoryTools.MakeRawMemory(formats);
|
||||
var ret = XrEnumerateSwapchainFormats(session, formatCapacityInput, ref formatCountOutput, formatsPtr);
|
||||
if (ret == XrResult.XR_SUCCESS)
|
||||
MemoryTools.CopyFromRawMemory(formats, formatsPtr, (int)formatCountOutput);
|
||||
MemoryTools.ReleaseRawMemory(formatsPtr);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public XrResult EnumerateSwapchainImages(XrSwapchain swapchain, uint imageCapacityInput, ref uint imageCountOutput, IntPtr imagesPtr)
|
||||
{
|
||||
if (IsInited == false || XrEnumerateSwapchainImages == null)
|
||||
{
|
||||
imageCountOutput = 0;
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
return XrEnumerateSwapchainImages(swapchain, imageCapacityInput, ref imageCountOutput, imagesPtr);
|
||||
}
|
||||
|
||||
[DllImport("viveopenxr", EntryPoint = "CwAcquireSwapchainImage")]
|
||||
public static extern XrResult CwAcquireSwapchainImage(XrSwapchain swapchain, ref XrSwapchainImageAcquireInfo acquireInfo, out uint index);
|
||||
|
||||
public XrResult AcquireSwapchainImage(XrSwapchain swapchain, ref XrSwapchainImageAcquireInfo acquireInfo, out uint index)
|
||||
{
|
||||
if (IsInited == false || XrAcquireSwapchainImage == null)
|
||||
{
|
||||
index = 0;
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
Profiler.BeginSample("ASW:xrAcqScImg");
|
||||
var res = XrAcquireSwapchainImage(swapchain, ref acquireInfo, out index);
|
||||
Profiler.EndSample();
|
||||
return res;
|
||||
}
|
||||
|
||||
[DllImport("viveopenxr", EntryPoint = "CwWaitSwapchainImage")]
|
||||
public static extern XrResult CwWaitSwapchainImage(XrSwapchain swapchain, ref XrSwapchainImageWaitInfo waitInfo);
|
||||
|
||||
public XrResult WaitSwapchainImage(XrSwapchain swapchain, ref XrSwapchainImageWaitInfo waitInfo)
|
||||
{
|
||||
if (IsInited == false || XrWaitSwapchainImage == null)
|
||||
{
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
Profiler.BeginSample("ASW:xrWaitScImg");
|
||||
var res = XrWaitSwapchainImage(swapchain, ref waitInfo);
|
||||
Profiler.EndSample();
|
||||
return res;
|
||||
}
|
||||
|
||||
[DllImport("viveopenxr", EntryPoint = "CwReleaseSwapchainImage")]
|
||||
public static extern XrResult CwReleaseSwapchainImage(XrSwapchain swapchain, ref XrSwapchainImageReleaseInfo releaseInfo);
|
||||
|
||||
public XrResult ReleaseSwapchainImage(XrSwapchain swapchain, ref XrSwapchainImageReleaseInfo releaseInfo)
|
||||
{
|
||||
if (IsInited == false || XrReleaseSwapchainImage == null)
|
||||
{
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
}
|
||||
|
||||
// Add Profiler
|
||||
Profiler.BeginSample("ASW:xrRelScImg");
|
||||
var res = XrReleaseSwapchainImage(swapchain, ref releaseInfo);
|
||||
Profiler.EndSample();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b940cd65f52cd5c44bd79869c5d521b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,211 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.OpenXR;
|
||||
|
||||
namespace VIVE.OpenXR.Feature
|
||||
{
|
||||
using XrFutureEXT = System.IntPtr;
|
||||
|
||||
/// <summary>
|
||||
/// To use this wrapper,
|
||||
/// 1. Add the "XR_EXT_Future" extension to the instance's enabled extensions list.
|
||||
/// 2. Call FutureWrapper.Instance.OnInstanceCreate() in your feature's OnInstanceCreate().
|
||||
/// 3. Call FutureWrapper.Instance.OnInstanceDestroy() in your feature's OnInstanceDestroy().
|
||||
///
|
||||
/// <see cref="VIVE.OpenXR.Toolkits.FutureTask.Poll"/> function helps make async Task.
|
||||
/// </summary>
|
||||
public class FutureWrapper : ViveFeatureWrapperBase<FutureWrapper>, IViveFeatureWrapper
|
||||
{
|
||||
const string TAG = "ViveFuture";
|
||||
|
||||
public enum XrFutureStateEXT
|
||||
{
|
||||
None = 0, // Not defined in extension. A default value.
|
||||
Pending = 1,
|
||||
Ready = 2,
|
||||
MAX = 0x7FFFFFFF
|
||||
}
|
||||
|
||||
public struct XrFuturePollInfoEXT {
|
||||
public XrStructureType type; // XR_TYPE_FUTURE_POLL_INFO_EXT
|
||||
public IntPtr next;
|
||||
public XrFutureEXT future;
|
||||
}
|
||||
|
||||
public struct XrFuturePollResultEXT {
|
||||
public XrStructureType type; // XR_TYPE_FUTURE_POLL_RESULT_EXT
|
||||
public IntPtr next;
|
||||
public XrFutureStateEXT state;
|
||||
}
|
||||
|
||||
public struct XrFutureCancelInfoEXT
|
||||
{
|
||||
public XrStructureType type; // XR_TYPE_FUTURE_CANCEL_INFO_EXT
|
||||
public IntPtr next;
|
||||
public XrFutureEXT future;
|
||||
}
|
||||
|
||||
public struct XrFutureCompletionBaseHeaderEXT
|
||||
{
|
||||
public XrStructureType type; // XR_TYPE_FUTURE_COMPLETION_EXT
|
||||
public IntPtr next;
|
||||
public XrResult futureResult;
|
||||
}
|
||||
|
||||
public struct XrFutureCompletionEXT
|
||||
{
|
||||
public XrStructureType type; // XR_TYPE_FUTURE_COMPLETION_EXT
|
||||
public IntPtr next;
|
||||
public XrResult futureResult;
|
||||
}
|
||||
|
||||
public delegate XrResult XrPollFutureEXTDelegate(XrInstance instance, ref XrFuturePollInfoEXT pollInfo, out XrFuturePollResultEXT pollResult);
|
||||
public delegate XrResult XrCancelFutureEXTDelegate(XrInstance instance, ref XrFutureCancelInfoEXT cancelInfo);
|
||||
|
||||
XrPollFutureEXTDelegate XrPollFutureEXT;
|
||||
XrCancelFutureEXTDelegate XrCancelFutureEXT;
|
||||
|
||||
XrInstance xrInstance;
|
||||
|
||||
/// <summary>
|
||||
/// Features should call FutureWrapper.Instance.OnInstanceCreate() in their OnInstanceCreate().
|
||||
/// </summary>
|
||||
/// <param name="xrInstance"></param>
|
||||
/// <param name="xrGetInstanceProcAddrPtr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public bool OnInstanceCreate(XrInstance xrInstance, IntPtr xrGetInstanceProcAddrPtr)
|
||||
{
|
||||
if (IsInited) return true;
|
||||
if (TryInited) return false;
|
||||
TryInited = true;
|
||||
|
||||
if (xrInstance == null)
|
||||
throw new Exception("FutureWrapper: xrInstance is null");
|
||||
this.xrInstance = xrInstance;
|
||||
|
||||
if (xrGetInstanceProcAddrPtr == null)
|
||||
throw new Exception("FutureWrapper: xrGetInstanceProcAddr is null");
|
||||
SetGetInstanceProcAddrPtr(xrGetInstanceProcAddrPtr);
|
||||
|
||||
Log.D(TAG, "OnInstanceCreate()");
|
||||
|
||||
bool hasFuture = OpenXRRuntime.IsExtensionEnabled("XR_EXT_future");
|
||||
if (!hasFuture)
|
||||
{
|
||||
Log.E(TAG, "FutureWrapper: XR_EXT_future is not enabled. Check your feature's kOpenxrExtensionString.");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = true;
|
||||
IntPtr funcPtr = IntPtr.Zero;
|
||||
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrPollFutureEXT", out XrPollFutureEXT);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrCancelFutureEXT", out XrCancelFutureEXT);
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
Log.E(TAG,"FutureWrapper: Failed to get function pointer.");
|
||||
return false;
|
||||
}
|
||||
|
||||
IsInited = ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void OnInstanceDestroy()
|
||||
{
|
||||
Log.D(TAG, "OnInstanceDestroy()");
|
||||
IsInited = false;
|
||||
XrPollFutureEXT = null;
|
||||
XrCancelFutureEXT = null;
|
||||
xrInstance = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to get the state of a future. If Ready, Call complete functions to get the result.
|
||||
/// </summary>
|
||||
/// <param name="pollInfo"></param>
|
||||
/// <param name="pollResult"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public XrResult PollFuture(ref XrFuturePollInfoEXT pollInfo, out XrFuturePollResultEXT pollResult)
|
||||
{
|
||||
pollResult= new XrFuturePollResultEXT()
|
||||
{
|
||||
type = XrStructureType.XR_TYPE_FUTURE_POLL_RESULT_EXT,
|
||||
next = IntPtr.Zero,
|
||||
state = XrFutureStateEXT.None
|
||||
};
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
|
||||
return XrPollFutureEXT(xrInstance, ref pollInfo, out pollResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to get the state of a future. If Ready, Call complete functions to get the result.
|
||||
/// </summary>
|
||||
/// <param name="future"></param>
|
||||
/// <param name="pollResult"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public XrResult PollFuture(XrFutureEXT future, out XrFuturePollResultEXT pollResult)
|
||||
{
|
||||
pollResult = new XrFuturePollResultEXT()
|
||||
{
|
||||
type = XrStructureType.XR_TYPE_FUTURE_POLL_RESULT_EXT,
|
||||
next = IntPtr.Zero,
|
||||
state = XrFutureStateEXT.None
|
||||
};
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
|
||||
XrFuturePollInfoEXT pollInfo = new XrFuturePollInfoEXT()
|
||||
{
|
||||
type = XrStructureType.XR_TYPE_FUTURE_POLL_INFO_EXT,
|
||||
next = IntPtr.Zero,
|
||||
future = future
|
||||
};
|
||||
|
||||
return XrPollFutureEXT(xrInstance, ref pollInfo, out pollResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This function cancels the future and signals that the async operation is not required.
|
||||
/// After a future has been cancelled any functions using this future must return XR_ERROR_FUTURE_INVALID_EXT.
|
||||
/// </summary>
|
||||
/// <param name="cancelInfo"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public XrResult CancelFuture(ref XrFutureCancelInfoEXT cancelInfo)
|
||||
{
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
|
||||
return XrCancelFutureEXT(xrInstance, ref cancelInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CancelFuture(ref XrFutureCancelInfoEXT)"/>
|
||||
/// </summary>
|
||||
/// <param name="future"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public XrResult CancelFuture(XrFutureEXT future)
|
||||
{
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
|
||||
XrFutureCancelInfoEXT cancelInfo = new XrFutureCancelInfoEXT()
|
||||
{
|
||||
type = XrStructureType.XR_TYPE_FUTURE_CANCEL_INFO_EXT,
|
||||
next = IntPtr.Zero,
|
||||
future = future
|
||||
};
|
||||
|
||||
return XrCancelFutureEXT(xrInstance, ref cancelInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8522c7af0a4127409a8800e1ddd5985
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,227 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VIVE.OpenXR.Feature
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// To use this wrapper, you need to call CommonWrapper.Instance.OnInstanceCreate() in your feature's OnInstanceCreate(),
|
||||
/// and call CommonWrapper.Instance.OnInstanceDestroy() in your feature's OnInstanceDestroy().
|
||||
/// </summary>
|
||||
public class SpaceWrapper : ViveFeatureWrapperBase<SpaceWrapper>, IViveFeatureWrapper
|
||||
{
|
||||
const string TAG = "ViveSpaceWrapper";
|
||||
|
||||
public delegate XrResult DelegateXrEnumerateReferenceSpaces(XrSession session, uint spaceCapacityInput, out uint spaceCountOutput, [Out] XrReferenceSpaceType[] spaces);
|
||||
delegate XrResult DelegateXrLocateSpace(XrSpace space, XrSpace baseSpace, XrTime time, ref XrSpaceLocation location);
|
||||
delegate XrResult DelegateXrDestroySpace(XrSpace space);
|
||||
|
||||
DelegateXrEnumerateReferenceSpaces XrEnumerateReferenceSpaces;
|
||||
OpenXRHelper.xrCreateReferenceSpaceDelegate XrCreateReferenceSpace;
|
||||
DelegateXrLocateSpace XrLocateSpace;
|
||||
DelegateXrDestroySpace XrDestroySpace;
|
||||
|
||||
/// <summary>
|
||||
/// Features should call ViveSpaceWrapper.Instance.OnInstanceCreate() in their OnInstanceCreate().
|
||||
/// </summary>
|
||||
/// <param name="xrInstance"></param>
|
||||
/// <param name="GetAddr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public bool OnInstanceCreate(XrInstance xrInstance, IntPtr GetAddr)
|
||||
{
|
||||
if (IsInited) return true;
|
||||
if (TryInited) return false;
|
||||
TryInited = true;
|
||||
|
||||
if (xrInstance == null)
|
||||
throw new Exception("ViveSpaceWrapper: xrInstance is null");
|
||||
|
||||
SetGetInstanceProcAddrPtr(GetAddr);
|
||||
|
||||
Log.D(TAG, "OnInstanceCreate()");
|
||||
|
||||
bool ret = true;
|
||||
IntPtr funcPtr = IntPtr.Zero;
|
||||
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrEnumerateReferenceSpaces", out XrEnumerateReferenceSpaces);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrCreateReferenceSpace", out XrCreateReferenceSpace);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrLocateSpace", out XrLocateSpace);
|
||||
ret &= OpenXRHelper.GetXrFunctionDelegate(xrGetInstanceProcAddr, xrInstance, "xrDestroySpace", out XrDestroySpace);
|
||||
IsInited = ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void OnInstanceDestroy()
|
||||
{
|
||||
// Do not destroy twice
|
||||
if (IsInited == false) return;
|
||||
IsInited = false;
|
||||
XrEnumerateReferenceSpaces = null;
|
||||
XrCreateReferenceSpace = null;
|
||||
XrLocateSpace = null;
|
||||
XrDestroySpace = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="spaceCapacityInput"></param>
|
||||
/// <param name="spaceCountOutput"></param>
|
||||
/// <param name="spaces"></param>
|
||||
/// <returns></returns>
|
||||
public XrResult EnumerateReferenceSpaces(XrSession session, int spaceCapacityInput, ref int spaceCountOutput, ref XrReferenceSpaceType[] spaces)
|
||||
{
|
||||
spaceCountOutput = 0;
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
if (spaceCapacityInput != 0 && spaces != null && spaces.Length < spaceCapacityInput)
|
||||
return XrResult.XR_ERROR_SIZE_INSUFFICIENT;
|
||||
var ret = XrEnumerateReferenceSpaces(session, (uint)spaceCapacityInput, out uint spaceCountOutputXR, spaces);
|
||||
spaceCountOutput = (int)spaceCountOutputXR;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a reference space without create info.
|
||||
/// Example:
|
||||
/// CreateReferenceSpace(session, XrReferenceSpaceType.XR_REFERENCE_SPACE_TYPE_LOCAL, XrPosef.Identity, out space);
|
||||
/// CreateReferenceSpace(session, XrReferenceSpaceType.XR_REFERENCE_SPACE_TYPE_STAGE, XrPosef.Identity, out space);
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="referenceSpaceType"></param>
|
||||
/// <param name="pose"></param>
|
||||
/// <param name="space"></param>
|
||||
/// <returns></returns>
|
||||
public XrResult CreateReferenceSpace(XrSession session, XrReferenceSpaceType referenceSpaceType, XrPosef pose, out XrSpace space)
|
||||
{
|
||||
space = 0;
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
|
||||
var createInfo = new XrReferenceSpaceCreateInfo();
|
||||
createInfo.type = XrStructureType.XR_TYPE_REFERENCE_SPACE_CREATE_INFO;
|
||||
createInfo.next = IntPtr.Zero;
|
||||
createInfo.referenceSpaceType = referenceSpaceType;
|
||||
createInfo.poseInReferenceSpace = pose;
|
||||
return XrCreateReferenceSpace(session, ref createInfo, out space);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a reference space.
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="createInfo"></param>
|
||||
/// <param name="space"></param>
|
||||
/// <returns></returns>
|
||||
public XrResult CreateReferenceSpace(XrSession session, XrReferenceSpaceCreateInfo createInfo, out XrSpace space)
|
||||
{
|
||||
space = 0;
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
|
||||
return XrCreateReferenceSpace(session, ref createInfo, out space);
|
||||
}
|
||||
|
||||
public XrResult LocateSpace(XrSpace space, XrSpace baseSpace, XrTime time, ref XrSpaceLocation location)
|
||||
{
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
//Debug.Log($"LocateSpace(s={space}, bs={baseSpace}, t={time}");
|
||||
return XrLocateSpace(space, baseSpace, time, ref location);
|
||||
}
|
||||
|
||||
public XrResult DestroySpace(XrSpace space)
|
||||
{
|
||||
if (!IsInited)
|
||||
return XrResult.XR_ERROR_HANDLE_INVALID;
|
||||
Log.D(TAG, $"DestroySpace({space})");
|
||||
return XrDestroySpace(space);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The XrSpace's Unity wrapper. Input and output are in Unity coordinate system.
|
||||
/// After use it, you should call Dispose() to release the XrSpace.
|
||||
/// </summary>
|
||||
public class Space : IDisposable
|
||||
{
|
||||
protected XrSpace space;
|
||||
private bool disposed = false;
|
||||
|
||||
public Space(XrSpace space)
|
||||
{
|
||||
Log.D($"Space({space})");
|
||||
this.space = space;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the raw XrSpace. Only use it when class Space instance is alive.
|
||||
/// You should not try to store this XrSpace, because it may be destroyed.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public XrSpace GetXrSpace()
|
||||
{
|
||||
return space;
|
||||
}
|
||||
|
||||
public bool GetRelatedPose(XrSpace baseSpace, XrTime time, out UnityEngine.Pose pose)
|
||||
{
|
||||
// If the xrBaseSpace is changed, the pose will be updated.
|
||||
pose = default;
|
||||
XrSpaceLocation location = new XrSpaceLocation();
|
||||
location.type = XrStructureType.XR_TYPE_SPACE_LOCATION;
|
||||
location.next = IntPtr.Zero;
|
||||
var ret = SpaceWrapper.Instance.LocateSpace(space, baseSpace, time, ref location);
|
||||
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
{
|
||||
//Debug.Log("Space: LocateSpace ret=" + ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Debug.Log("Space: baseSpace=" + baseSpace + ", space=" + space + ", time=" + time + ", ret=" + ret);
|
||||
//Debug.Log("Space: location.locationFlags=" + location.locationFlags);
|
||||
//Debug.Log("Space: location.pose.position=" + location.pose.position.x + "," + location.pose.position.y + "," + location.pose.position.z);
|
||||
//Debug.Log("Space: location.pose.orientation=" + location.pose.orientation.x + "," + location.pose.orientation.y + "," + location.pose.orientation.z + "," + location.pose.orientation.w);
|
||||
if ((location.locationFlags & XrSpaceLocationFlags.XR_SPACE_LOCATION_POSITION_VALID_BIT) > 0 &&
|
||||
(location.locationFlags & XrSpaceLocationFlags.XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) > 0)
|
||||
{
|
||||
pose = new Pose(location.pose.position.ToUnityVector(), location.pose.orientation.ToUnityQuaternion());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
// Managered resource
|
||||
}
|
||||
// Non managered resource
|
||||
//Debug.Log($"Space: DestroySpace({space})");
|
||||
SpaceWrapper.Instance.DestroySpace(space);
|
||||
space = 0;
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
~Space()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8fbeadd31afbe14996c061ac261041d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,199 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using AOT;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||
internal class HookHandlerAttribute : Attribute
|
||||
{
|
||||
public string xrFuncName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Set this function to handle the hook process in <see cref="ViveInterceptors.XrGetInstanceProcAddrInterceptor" />
|
||||
/// </summary>
|
||||
/// <param name="xrFuncName">The hooked openxr function name</param>
|
||||
public HookHandlerAttribute(string xrFuncName)
|
||||
{
|
||||
this.xrFuncName = xrFuncName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This class is made for all features that need to intercept OpenXR API calls.
|
||||
/// Some APIs will be called by Unity internally, and we need to intercept them in c# to get some information.
|
||||
/// Append more interceptable functions for this class by adding a new partial class.
|
||||
/// The partial class can help the delegate name be nice to read and search.
|
||||
/// Please create per function in one partial class.
|
||||
///
|
||||
/// For all features want to use this class, please call <see cref="HookGetInstanceProcAddr" /> in your feature class.
|
||||
/// For example:
|
||||
/// protected override IntPtr HookGetInstanceProcAddr(IntPtr func)
|
||||
/// {
|
||||
/// return ViveInterceptors.Instance.HookGetInstanceProcAddr(func);
|
||||
/// }
|
||||
/// </summary>
|
||||
|
||||
// For extending the ViveInterceptors class, create a new partial class and implement the required functions.
|
||||
// For example:
|
||||
// public partial class ViveInterceptors
|
||||
// {
|
||||
// [HookHandler("xrYourFunction")]
|
||||
// private static XrResult OnHookXrYourFunction(XrInstance instance, string name, out IntPtr function)
|
||||
// { ... }
|
||||
// }
|
||||
partial class ViveInterceptors
|
||||
{
|
||||
public const string TAG = "VIVE.OpenXR.ViveInterceptors";
|
||||
static StringBuilder m_sb = null;
|
||||
static StringBuilder sb {
|
||||
get {
|
||||
if (m_sb == null) { m_sb = new StringBuilder(); }
|
||||
return m_sb;
|
||||
}
|
||||
}
|
||||
|
||||
public static ViveInterceptors instance = null;
|
||||
public static ViveInterceptors Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
instance = new ViveInterceptors();
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
public ViveInterceptors()
|
||||
{
|
||||
Log.D("ViveInterceptors");
|
||||
RegisterFunctions();
|
||||
}
|
||||
|
||||
delegate XrResult HookHandler(XrInstance instance, string name, out IntPtr function);
|
||||
static readonly Dictionary<string, HookHandler> interceptors = new Dictionary<string, HookHandler>();
|
||||
|
||||
private static void RegisterFunctions()
|
||||
{
|
||||
var methods = typeof(ViveInterceptors).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var attribute = method.GetCustomAttributes(typeof(HookHandlerAttribute), false).FirstOrDefault() as HookHandlerAttribute;
|
||||
if (attribute != null)
|
||||
{
|
||||
Log.I(TAG, $"Registering hook handler {attribute.xrFuncName}");
|
||||
interceptors.Add(attribute.xrFuncName, (HookHandler)method.CreateDelegate(typeof(HookHandler)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly OpenXRHelper.xrGetInstanceProcAddrDelegate hookXrGetInstanceProcAddrHandle = new OpenXRHelper.xrGetInstanceProcAddrDelegate(XrGetInstanceProcAddrInterceptor);
|
||||
private static readonly IntPtr hookGetInstanceProcAddrHandlePtr = Marshal.GetFunctionPointerForDelegate(hookXrGetInstanceProcAddrHandle);
|
||||
static OpenXRHelper.xrGetInstanceProcAddrDelegate XrGetInstanceProcAddrOriginal = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(OpenXRHelper.xrGetInstanceProcAddrDelegate))]
|
||||
private static XrResult XrGetInstanceProcAddrInterceptor(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
// Used to check if the original function is already hooked.
|
||||
if (instance == 0 && name == "ViveInterceptorHooked")
|
||||
{
|
||||
function = IntPtr.Zero;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
// Check if the function is intercepted by other features
|
||||
if (interceptors.ContainsKey(name))
|
||||
{
|
||||
// If no request for this function, call the original function directly.
|
||||
if (!requiredFunctions.Contains(name))
|
||||
return XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
|
||||
var ret = interceptors[name](instance, name, out function);
|
||||
if (ret == XrResult.XR_SUCCESS)
|
||||
Log.I(TAG, name + " is intercepted");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
}
|
||||
|
||||
public IntPtr HookGetInstanceProcAddr(IntPtr func)
|
||||
{
|
||||
Log.D(TAG, "HookGetInstanceProcAddr");
|
||||
if (XrGetInstanceProcAddrOriginal == null)
|
||||
{
|
||||
Log.D(TAG, "registering our own xrGetInstanceProcAddr");
|
||||
XrGetInstanceProcAddrOriginal = Marshal.GetDelegateForFunctionPointer<OpenXRHelper.xrGetInstanceProcAddrDelegate>(func);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (Application.isEditor) {
|
||||
// This is a trick to check if the original function is already hooked by this class. Sometimes, the static XrGetInstanceProcAddrOriginal didn't work as expected.
|
||||
Log.D(TAG, "Check if duplicate hooked by this script with instance=0 and \"ViveInterceptorHooked\" name. If following a loader error, ignore it.");
|
||||
// E OpenXR-Loader: Error [SPEC | xrGetInstanceProcAddr | VUID-xrGetInstanceProcAddr-instance-parameter] : XR_NULL_HANDLE for instance but query for ViveInterceptorHooked requires a valid instance
|
||||
|
||||
// Call XrGetInstanceProcAddrOriginal to check if the original function is already hooked by this class
|
||||
if (XrGetInstanceProcAddrOriginal(0, "ViveInterceptorHooked", out IntPtr function) == XrResult.XR_SUCCESS)
|
||||
{
|
||||
// If it is called successfully, it means the original function is already hooked. So we should return the original function.
|
||||
Log.D(TAG, "Already hooked");
|
||||
return func;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return hookGetInstanceProcAddrHandlePtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Dont return hookGetInstanceProcAddrHandlePtr again.
|
||||
// If this hook function is called by multiple features, it should only work at the first time.
|
||||
// If called by other features, it should return the original function.
|
||||
return func;
|
||||
}
|
||||
}
|
||||
|
||||
static readonly List<string> requiredFunctions = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Call before <see cref="HookGetInstanceProcAddr" /> to add required functions."/>
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
public void AddRequiredFunction(string name)
|
||||
{
|
||||
Log.D(TAG, $"AddRequiredFunction({name})");
|
||||
if (!interceptors.ContainsKey(name))
|
||||
{
|
||||
Log.E(TAG, $"AddRequiredFunction({name}) failed. No such function.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!requiredFunctions.Contains(name))
|
||||
requiredFunctions.Add(name);
|
||||
|
||||
// If your function support unregister, you can add the reference count here.
|
||||
if (name == "xrLocateViews")
|
||||
xrLocateViewsReferenceCount++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If no need to use this hooked function, call this will remove your requirement.
|
||||
/// If all requirements are removed, the original function will be called directly.
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
public void RemoveRequiredFunction(string name)
|
||||
{
|
||||
// If your function support unregister, you can add the reference count here.
|
||||
if (requiredFunctions.Contains(name))
|
||||
{
|
||||
if (name == "xrLocateViews")
|
||||
xrLocateViewsReferenceCount = Mathf.Max(xrLocateViewsReferenceCount--, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 118a2474c266d174d834b364821865b5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,102 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
|
||||
#define DEBUG
|
||||
|
||||
using AOT;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine.Profiling;
|
||||
using VIVE.OpenXR.FrameSynchronization;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
partial class ViveInterceptors
|
||||
{
|
||||
[HookHandler("xrBeginSession")]
|
||||
private static XrResult OnHookXrBeginSession(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (xrBeginSessionOrigin == null)
|
||||
{
|
||||
var ret = XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
return ret;
|
||||
xrBeginSessionOrigin = Marshal.GetDelegateForFunctionPointer<xrBeginSessionDelegate>(function);
|
||||
}
|
||||
function = xrBeginSessionPtr;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
#region xrBeginSession
|
||||
public delegate XrResult xrBeginSessionDelegate(XrSession session, ref XrSessionBeginInfo beginInfo);
|
||||
private static xrBeginSessionDelegate xrBeginSessionOrigin = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(xrBeginSessionDelegate))]
|
||||
private static XrResult xrBeginSessionInterceptor(XrSession session, ref XrSessionBeginInfo beginInfo)
|
||||
{
|
||||
Profiler.BeginSample("VI:BeginSession");
|
||||
XrResult result = XrResult.XR_ERROR_FUNCTION_UNSUPPORTED;
|
||||
|
||||
if (xrBeginSessionOrigin != null)
|
||||
{
|
||||
if (m_EnableFrameSynchronization)
|
||||
{
|
||||
frameSynchronizationSessionBeginInfo.mode = m_FrameSynchronizationMode;
|
||||
frameSynchronizationSessionBeginInfo.next = beginInfo.next;
|
||||
beginInfo.next = Marshal.AllocHGlobal(Marshal.SizeOf(frameSynchronizationSessionBeginInfo));
|
||||
|
||||
long offset = 0;
|
||||
if (IntPtr.Size == 4)
|
||||
offset = beginInfo.next.ToInt32();
|
||||
else
|
||||
offset = beginInfo.next.ToInt64();
|
||||
|
||||
IntPtr frame_synchronization_session_begin_info_ptr = new IntPtr(offset);
|
||||
Marshal.StructureToPtr(frameSynchronizationSessionBeginInfo, frame_synchronization_session_begin_info_ptr, false);
|
||||
|
||||
#if DEBUG
|
||||
if (IntPtr.Size == 4)
|
||||
offset = beginInfo.next.ToInt32();
|
||||
else
|
||||
offset = beginInfo.next.ToInt64();
|
||||
|
||||
IntPtr fs_begin_info_ptr = new IntPtr(offset);
|
||||
XrFrameSynchronizationSessionBeginInfoHTC fsBeginInfo = (XrFrameSynchronizationSessionBeginInfoHTC)Marshal.PtrToStructure(fs_begin_info_ptr, typeof(XrFrameSynchronizationSessionBeginInfoHTC));
|
||||
|
||||
sb.Clear().Append("xrBeginSessionInterceptor() beginInfo.next = (").Append(fsBeginInfo.type).Append(", ").Append(fsBeginInfo.mode).Append(")");
|
||||
Log.D(sb);
|
||||
#endif
|
||||
}
|
||||
|
||||
result = xrBeginSessionOrigin(session, ref beginInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.E("xrBeginSessionInterceptor() Not assign xrBeginSession!");
|
||||
}
|
||||
Profiler.EndSample();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static readonly xrBeginSessionDelegate xrBeginSession = new xrBeginSessionDelegate(xrBeginSessionInterceptor);
|
||||
private static readonly IntPtr xrBeginSessionPtr = Marshal.GetFunctionPointerForDelegate(xrBeginSession);
|
||||
#endregion
|
||||
|
||||
private static XrFrameSynchronizationSessionBeginInfoHTC frameSynchronizationSessionBeginInfo = XrFrameSynchronizationSessionBeginInfoHTC.identity;
|
||||
private static bool m_EnableFrameSynchronization = false;
|
||||
private static XrFrameSynchronizationModeHTC m_FrameSynchronizationMode = XrFrameSynchronizationModeHTC.XR_FRAME_SYNCHRONIZATION_MODE_STABILIZED_HTC;
|
||||
/// <summary>
|
||||
/// Activate or deactivate the Frame Synchronization feature.
|
||||
/// </summary>
|
||||
/// <param name="active">True for activate</param>
|
||||
/// <param name="mode">The <see cref="XrFrameSynchronizationModeHTC"/> used for Frame Synchronization.</param>
|
||||
public void ActivateFrameSynchronization(bool active, XrFrameSynchronizationModeHTC mode)
|
||||
{
|
||||
m_EnableFrameSynchronization = active;
|
||||
m_FrameSynchronizationMode = mode;
|
||||
sb.Clear().Append("ActivateFrameSynchronization() ").Append(active ? "enable " : "disable ").Append(mode);
|
||||
Log.D(sb);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c222b96d7eb4ca4bb6390e07b1967bb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,98 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using AOT;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
public partial class ViveInterceptors
|
||||
{
|
||||
[HookHandler("xrLocateViews")]
|
||||
private static XrResult OnHookXrLocateViews(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (xrLocateViewsOriginal == null)
|
||||
{
|
||||
var ret = XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
return ret;
|
||||
xrLocateViewsOriginal = Marshal.GetDelegateForFunctionPointer<DelegateXrLocateViews>(function);
|
||||
}
|
||||
function = xrLocateViewsInterceptorPtr;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
public struct XrViewLocateInfo
|
||||
{
|
||||
public XrStructureType type;
|
||||
public IntPtr next;
|
||||
public XrViewConfigurationType viewConfigurationType;
|
||||
public XrTime displayTime;
|
||||
public XrSpace space;
|
||||
}
|
||||
|
||||
public struct XrView
|
||||
{
|
||||
public XrStructureType type;
|
||||
public IntPtr next;
|
||||
public XrPosef pose;
|
||||
public XrFovf fov;
|
||||
}
|
||||
|
||||
public enum XrViewStateFlags {
|
||||
ORIENTATION_VALID_BIT = 0x00000001,
|
||||
POSITION_VALID_BIT = 0x00000002,
|
||||
ORIENTATION_TRACKED_BIT = 0x00000004,
|
||||
POSITION_TRACKED_BIT = 0x00000008,
|
||||
}
|
||||
|
||||
public struct XrViewState
|
||||
{
|
||||
public XrStructureType type;
|
||||
public IntPtr next;
|
||||
public XrViewStateFlags viewStateFlags;
|
||||
}
|
||||
|
||||
public delegate XrResult DelegateXrLocateViews(XrSession session, IntPtr /*XrViewLocateInfo*/ viewLocateInfo, IntPtr /*XrViewState*/ viewState, uint viewCapacityInput, ref uint viewCountOutput, IntPtr /*XrView*/ views);
|
||||
|
||||
private static readonly DelegateXrLocateViews xrLocateViewsInterceptorHandle = new DelegateXrLocateViews(XrLocateViewsInterceptor);
|
||||
private static readonly IntPtr xrLocateViewsInterceptorPtr = Marshal.GetFunctionPointerForDelegate(xrLocateViewsInterceptorHandle);
|
||||
static DelegateXrLocateViews xrLocateViewsOriginal = null;
|
||||
static int xrLocateViewsReferenceCount = 0;
|
||||
|
||||
[MonoPInvokeCallback(typeof(DelegateXrLocateViews))]
|
||||
private static XrResult XrLocateViewsInterceptor(XrSession session, IntPtr viewLocateInfo, IntPtr viewState, uint viewCapacityInput, ref uint viewCountOutput, IntPtr views)
|
||||
{
|
||||
// Call the original function if the reference count is less than or equal to 0
|
||||
if (xrLocateViewsReferenceCount <= 0)
|
||||
return xrLocateViewsOriginal(session, viewLocateInfo, viewState, viewCapacityInput, ref viewCountOutput, views);
|
||||
|
||||
Profiler.BeginSample("VI:LocateViewsA");
|
||||
XrResult result = XrResult.XR_SUCCESS;
|
||||
if (instance.BeforeOriginalLocateViews != null)
|
||||
instance.BeforeOriginalLocateViews(session, viewLocateInfo, viewState, viewCapacityInput, ref viewCountOutput, views);
|
||||
Profiler.EndSample();
|
||||
result = xrLocateViewsOriginal(session, viewLocateInfo, viewState, viewCapacityInput, ref viewCountOutput, views);
|
||||
Profiler.BeginSample("VI:LocateViewsB");
|
||||
instance.AfterOriginalLocateViews?.Invoke(session, viewLocateInfo, viewState, viewCapacityInput, ref viewCountOutput, views);
|
||||
Profiler.EndSample();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If you return false, the original function will not be called.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public delegate bool DelegateXrLocateViewsInterceptor(XrSession session, IntPtr viewLocateInfo, IntPtr viewState, uint viewCapacityInput, ref uint viewCountOutput, IntPtr views);
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called before the original function.
|
||||
/// </summary>
|
||||
public DelegateXrLocateViewsInterceptor BeforeOriginalLocateViews;
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called after the original function.
|
||||
/// </summary>
|
||||
public DelegateXrLocateViewsInterceptor AfterOriginalLocateViews;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5dfd24c69475c3740975bf5538de3869
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,143 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
|
||||
using AOT;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine.Profiling;
|
||||
using VIVE.OpenXR.DisplayRefreshRate;
|
||||
using VIVE.OpenXR.Passthrough;
|
||||
using VIVE.OpenXR.UserPresence;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
partial class ViveInterceptors
|
||||
{
|
||||
[HookHandler("xrPollEvent")]
|
||||
private static XrResult OnHookXrPollEvent(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (xrPollEventOrigin == null)
|
||||
{
|
||||
var ret = XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
return ret;
|
||||
xrPollEventOrigin = Marshal.GetDelegateForFunctionPointer<xrPollEventDelegate>(function);
|
||||
}
|
||||
function = xrPollEventPtr;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
#region xrPollEvent
|
||||
public delegate XrResult xrPollEventDelegate(XrInstance instance, ref XrEventDataBuffer eventData);
|
||||
private static xrPollEventDelegate xrPollEventOrigin = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(xrPollEventDelegate))]
|
||||
private static XrResult xrPollEventInterceptor(XrInstance instance, ref XrEventDataBuffer eventData)
|
||||
{
|
||||
Profiler.BeginSample("VI:PollEvent");
|
||||
XrResult result = XrResult.XR_SUCCESS;
|
||||
|
||||
if (xrPollEventOrigin != null)
|
||||
{
|
||||
result = xrPollEventOrigin(instance, ref eventData);
|
||||
|
||||
if (result == XrResult.XR_SUCCESS)
|
||||
{
|
||||
sb.Clear().Append("xrPollEventInterceptor() xrPollEvent ").Append(eventData.type); Log.D("PollEvent", sb);
|
||||
switch(eventData.type)
|
||||
{
|
||||
case XrStructureType.XR_TYPE_EVENT_DATA_PASSTHROUGH_CONFIGURATION_IMAGE_RATE_CHANGED_HTC:
|
||||
if (XrEventDataPassthroughConfigurationImageRateChangedHTC.Get(eventData, out XrEventDataPassthroughConfigurationImageRateChangedHTC eventDataPassthroughConfigurationImageRate))
|
||||
{
|
||||
fromImageRate = eventDataPassthroughConfigurationImageRate.fromImageRate;
|
||||
toImageRate = eventDataPassthroughConfigurationImageRate.toImageRate;
|
||||
sb.Clear().Append("xrPollEventInterceptor() XR_TYPE_EVENT_DATA_PASSTHROUGH_CONFIGURATION_IMAGE_RATE_CHANGED_HTC")
|
||||
.Append(", fromImageRate.srcImageRate: ").Append(fromImageRate.srcImageRate)
|
||||
.Append(", fromImageRatesrc.dstImageRate: ").Append(fromImageRate.dstImageRate)
|
||||
.Append(", toImageRate.srcImageRate: ").Append(toImageRate.srcImageRate)
|
||||
.Append(", toImageRate.dstImageRate: ").Append(toImageRate.dstImageRate);
|
||||
Log.D("PollEvent", sb.ToString());
|
||||
VivePassthroughImageRateChanged.Send(fromImageRate.srcImageRate, fromImageRate.dstImageRate, toImageRate.srcImageRate, toImageRate.dstImageRate);
|
||||
}
|
||||
break;
|
||||
case XrStructureType.XR_TYPE_EVENT_DATA_PASSTHROUGH_CONFIGURATION_IMAGE_QUALITY_CHANGED_HTC:
|
||||
if (XrEventDataPassthroughConfigurationImageQualityChangedHTC.Get(eventData, out XrEventDataPassthroughConfigurationImageQualityChangedHTC eventDataPassthroughConfigurationImageQuality))
|
||||
{
|
||||
fromImageQuality = eventDataPassthroughConfigurationImageQuality.fromImageQuality;
|
||||
toImageQuality = eventDataPassthroughConfigurationImageQuality.toImageQuality;
|
||||
sb.Clear().Append("xrPollEventInterceptor() XR_TYPE_EVENT_DATA_PASSTHROUGH_CONFIGURATION_IMAGE_QUALITY_CHANGED_HTC")
|
||||
.Append(", fromImageQuality: ").Append(fromImageQuality.scale)
|
||||
.Append(", toImageQuality: ").Append(toImageQuality.scale);
|
||||
Log.D("PollEvent", sb);
|
||||
VivePassthroughImageQualityChanged.Send(fromImageQuality.scale, toImageQuality.scale);
|
||||
}
|
||||
break;
|
||||
case XrStructureType.XR_TYPE_EVENT_DATA_DISPLAY_REFRESH_RATE_CHANGED_FB:
|
||||
if(XrEventDataDisplayRefreshRateChangedFB.Get(eventData, out XrEventDataDisplayRefreshRateChangedFB eventDataDisplayRefreshRate))
|
||||
{
|
||||
fromDisplayRefreshRate = eventDataDisplayRefreshRate.fromDisplayRefreshRate;
|
||||
toDisplayRefreshRate = eventDataDisplayRefreshRate.toDisplayRefreshRate;
|
||||
sb.Clear().Append("xrPollEventInterceptor() XR_TYPE_EVENT_DATA_DISPLAY_REFRESH_RATE_CHANGED_FB")
|
||||
.Append(", fromDisplayRefreshRate: ").Append(fromDisplayRefreshRate)
|
||||
.Append(", toDisplayRefreshRate: ").Append(toDisplayRefreshRate);
|
||||
Log.D("PollEvent", sb);
|
||||
ViveDisplayRefreshRateChanged.Send(fromDisplayRefreshRate, toDisplayRefreshRate);
|
||||
}
|
||||
break;
|
||||
case XrStructureType.XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED:
|
||||
if (XrEventDataSessionStateChanged.Get(eventData, out XrEventDataSessionStateChanged eventDataSession))
|
||||
{
|
||||
switch(eventDataSession.state)
|
||||
{
|
||||
case XrSessionState.XR_SESSION_STATE_READY:
|
||||
isUserPresent = true;
|
||||
break;
|
||||
case XrSessionState.XR_SESSION_STATE_STOPPING:
|
||||
isUserPresent = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
sb.Clear().Append("xrPollEventInterceptor() XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED")
|
||||
.Append(", session: ").Append(eventDataSession.session)
|
||||
.Append(", state: ").Append(eventDataSession.state)
|
||||
.Append(", isUserPresent: ").Append(isUserPresent);
|
||||
Log.D("PollEvent", sb);
|
||||
}
|
||||
break;
|
||||
case XrStructureType.XR_TYPE_EVENT_DATA_USER_PRESENCE_CHANGED_EXT:
|
||||
if (XrEventDataUserPresenceChangedEXT.Get(eventData, out XrEventDataUserPresenceChangedEXT eventDataUserPresence))
|
||||
{
|
||||
isUserPresent = eventDataUserPresence.isUserPresent;
|
||||
sb.Clear().Append("xrPollEventInterceptor() XR_TYPE_EVENT_DATA_USER_PRESENCE_CHANGED_EXT")
|
||||
.Append(", session: ").Append(eventDataUserPresence.session)
|
||||
.Append(", isUserPresent: ").Append(isUserPresent);
|
||||
Log.D("PollEvent", sb);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//sb.Clear().Append("xrPollEventInterceptor() xrPollEvent result: ").Append(result).Append(", isUserPresent: ").Append(isUserPresent); Log.d("PollEvent", sb);
|
||||
}
|
||||
Profiler.EndSample();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static readonly xrPollEventDelegate xrPollEvent = new xrPollEventDelegate(xrPollEventInterceptor);
|
||||
private static readonly IntPtr xrPollEventPtr = Marshal.GetFunctionPointerForDelegate(xrPollEvent);
|
||||
#endregion
|
||||
|
||||
private static bool isUserPresent = true;
|
||||
public bool IsUserPresent() { return isUserPresent; }
|
||||
|
||||
private static float fromDisplayRefreshRate, toDisplayRefreshRate;
|
||||
public float FromDisplayRefreshRate() { return fromDisplayRefreshRate; }
|
||||
public float ToDisplayRefreshRate() { return toDisplayRefreshRate; }
|
||||
|
||||
private static XrPassthroughConfigurationImageRateHTC fromImageRate, toImageRate;
|
||||
private static XrPassthroughConfigurationImageQualityHTC fromImageQuality, toImageQuality;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2cc5716d3f563f49a47da6c1bd8ccbe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,104 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using AOT;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
partial class ViveInterceptors
|
||||
{
|
||||
[HookHandler("xrEndFrame")]
|
||||
private static XrResult OnHookXrEndFrame(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (XrEndFrameOriginal == null)
|
||||
{
|
||||
var ret = XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
return ret;
|
||||
XrEndFrameOriginal = Marshal.GetDelegateForFunctionPointer<DelegateXrEndFrame>(function);
|
||||
}
|
||||
function = xrEndFrameInterceptorPtr;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
public struct XrCompositionLayerBaseHeader
|
||||
{
|
||||
public XrStructureType type; // This base structure itself has no associated XrStructureType value.
|
||||
public System.IntPtr next;
|
||||
public XrCompositionLayerFlags layerFlags;
|
||||
public XrSpace space;
|
||||
}
|
||||
|
||||
public struct XrFrameEndInfo
|
||||
{
|
||||
public XrStructureType type;
|
||||
public System.IntPtr next;
|
||||
public XrTime displayTime;
|
||||
public XrEnvironmentBlendMode environmentBlendMode;
|
||||
public uint layerCount;
|
||||
public IntPtr layers; // XrCompositionLayerBaseHeader IntPtr array
|
||||
}
|
||||
|
||||
public delegate XrResult DelegateXrEndFrame(XrSession session, ref XrFrameEndInfo frameEndInfo);
|
||||
private static readonly DelegateXrEndFrame xrEndFrameInterceptorHandle = new DelegateXrEndFrame(XrEndFrameInterceptor);
|
||||
private static readonly IntPtr xrEndFrameInterceptorPtr = Marshal.GetFunctionPointerForDelegate(xrEndFrameInterceptorHandle);
|
||||
static DelegateXrEndFrame XrEndFrameOriginal = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(DelegateXrEndFrame))]
|
||||
private static XrResult XrEndFrameInterceptor(XrSession session, ref XrFrameEndInfo frameEndInfo)
|
||||
{
|
||||
// instance must not null
|
||||
//if (instance == null)
|
||||
// return XrEndFrameOriginal(session, ref frameEndInfo);
|
||||
Profiler.BeginSample("VI:EndFrameB");
|
||||
XrResult result = XrResult.XR_SUCCESS;
|
||||
bool ret = true;
|
||||
if (instance.BeforeOriginalEndFrame != null)
|
||||
ret = instance.BeforeOriginalEndFrame(session, ref frameEndInfo, ref result);
|
||||
Profiler.EndSample();
|
||||
if (!ret)
|
||||
return result;
|
||||
result = XrEndFrameOriginal(session, ref frameEndInfo);
|
||||
Profiler.BeginSample("VI:EndFrameA");
|
||||
instance.AfterOriginalEndFrame?.Invoke(session, ref frameEndInfo, ref result);
|
||||
Profiler.EndSample();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If you return false, the original function will not be called.
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="frameEndInfo"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
public delegate bool DelegateXrEndFrameInterceptor(XrSession session, ref XrFrameEndInfo frameEndInfo, ref XrResult result);
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called before the original function.
|
||||
/// </summary>
|
||||
public DelegateXrEndFrameInterceptor BeforeOriginalEndFrame;
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called after the original function.
|
||||
/// </summary>
|
||||
public DelegateXrEndFrameInterceptor AfterOriginalEndFrame;
|
||||
|
||||
#if PERFORMANCE_TEST
|
||||
public delegate XrResult DelegateXrLocateSpace(XrSpace space, XrSpace baseSpace, XrTime time, ref XrSpaceLocation location);
|
||||
private static readonly DelegateXrLocateSpace xrLocateSpaceInterceptorHandle = new DelegateXrLocateSpace(XrLocateSpaceInterceptor);
|
||||
private static readonly IntPtr xrLocateSpaceInterceptorPtr = Marshal.GetFunctionPointerForDelegate(xrLocateSpaceInterceptorHandle);
|
||||
static DelegateXrLocateSpace XrLocateSpaceOriginal = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(DelegateXrLocateSpace))]
|
||||
public static XrResult XrLocateSpaceInterceptor(XrSpace space, XrSpace baseSpace, XrTime time, ref XrSpaceLocation location)
|
||||
{
|
||||
Profiler.BeginSample("VI:LocateSpace");
|
||||
var ret = XrLocateSpaceOriginal(space, baseSpace, time, ref location);
|
||||
Profiler.EndSample();
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6bf7cf55d82ac6343b4eda92d1197a66
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,91 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using AOT;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
public partial class ViveInterceptors
|
||||
{
|
||||
[HookHandler("xrGetVisibilityMaskKHR")]
|
||||
private static XrResult OnHookXrGetVisibilityMaskKHR(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (xrGetVisibilityMaskKHROriginal == null)
|
||||
{
|
||||
var ret = XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
return ret;
|
||||
xrGetVisibilityMaskKHROriginal = Marshal.GetDelegateForFunctionPointer<DelegateXrGetVisibilityMaskKHR>(function);
|
||||
}
|
||||
function = xrGetVisibilityMaskKHRInterceptorPtr;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
public enum XrVisibilityMaskTypeKHR
|
||||
{
|
||||
HIDDEN_TRIANGLE_MESH_KHR = 1,
|
||||
VISIBLE_TRIANGLE_MESH_KHR = 2,
|
||||
LINE_LOOP_KHR = 3,
|
||||
}
|
||||
|
||||
public struct XrVisibilityMaskKHR
|
||||
{
|
||||
public XrStructureType type;
|
||||
public IntPtr next;
|
||||
public uint vertexCapacityInput;
|
||||
public uint vertexCountOutput;
|
||||
public IntPtr vertices; // XrVector2f array
|
||||
public uint indexCapacityInput;
|
||||
public uint indexCountOutput;
|
||||
public IntPtr indices; // uint array
|
||||
}
|
||||
|
||||
// XrCompositionLayerSpaceWarpInfoFlagsFB bits
|
||||
public delegate XrResult DelegateXrGetVisibilityMaskKHR(XrSession session, XrViewConfigurationType viewConfigurationType, uint viewIndex, XrVisibilityMaskTypeKHR visibilityMaskType, ref XrVisibilityMaskKHR visibilityMask);
|
||||
|
||||
private static readonly DelegateXrGetVisibilityMaskKHR xrGetVisibilityMaskKHRInterceptorHandle = new DelegateXrGetVisibilityMaskKHR(XrGetVisibilityMaskKHRInterceptor);
|
||||
private static readonly IntPtr xrGetVisibilityMaskKHRInterceptorPtr = Marshal.GetFunctionPointerForDelegate(xrGetVisibilityMaskKHRInterceptorHandle);
|
||||
static DelegateXrGetVisibilityMaskKHR xrGetVisibilityMaskKHROriginal = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(DelegateXrGetVisibilityMaskKHR))]
|
||||
private static XrResult XrGetVisibilityMaskKHRInterceptor(XrSession session, XrViewConfigurationType viewConfigurationType, uint viewIndex, XrVisibilityMaskTypeKHR visibilityMaskType, ref XrVisibilityMaskKHR visibilityMask)
|
||||
{
|
||||
// instance must not null
|
||||
//if (instance == null)
|
||||
// return XrGetVisibilityMaskKHROriginal(session, ref frameEndInfo);
|
||||
Profiler.BeginSample("VI:GetVMB");
|
||||
XrResult result = XrResult.XR_SUCCESS;
|
||||
bool ret = true;
|
||||
if (instance.BeforeOriginalGetVisibilityMaskKHR != null)
|
||||
ret = instance.BeforeOriginalGetVisibilityMaskKHR(session, viewConfigurationType, viewIndex, visibilityMaskType, ref visibilityMask, ref result);
|
||||
Profiler.EndSample();
|
||||
if (!ret)
|
||||
return result;
|
||||
result = xrGetVisibilityMaskKHROriginal(session, viewConfigurationType, viewIndex, visibilityMaskType, ref visibilityMask);
|
||||
Profiler.BeginSample("VI:GetVMA");
|
||||
instance.AfterOriginalGetVisibilityMaskKHR?.Invoke(session, viewConfigurationType, viewIndex, visibilityMaskType, ref visibilityMask, ref result);
|
||||
Profiler.EndSample();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If you return false, the original function will not be called.
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="frameEndInfo"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
public delegate bool DelegateXrGetVisibilityMaskKHRInterceptor(XrSession session, XrViewConfigurationType viewConfigurationType, uint viewIndex, XrVisibilityMaskTypeKHR visibilityMaskType, ref XrVisibilityMaskKHR visibilityMask, ref XrResult result);
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called before the original function.
|
||||
/// </summary>
|
||||
public DelegateXrGetVisibilityMaskKHRInterceptor BeforeOriginalGetVisibilityMaskKHR;
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called after the original function.
|
||||
/// </summary>
|
||||
public DelegateXrGetVisibilityMaskKHRInterceptor AfterOriginalGetVisibilityMaskKHR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4c00b0b7df78d34d89cd728c9de0672
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,120 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using AOT;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
partial class ViveInterceptors
|
||||
{
|
||||
[HookHandler("xrWaitFrame")]
|
||||
private static XrResult OnHookXrWaitFrame(XrInstance instance, string name, out IntPtr function)
|
||||
{
|
||||
if (XrWaitFrameOriginal == null)
|
||||
{
|
||||
var ret = XrGetInstanceProcAddrOriginal(instance, name, out function);
|
||||
if (ret != XrResult.XR_SUCCESS)
|
||||
return ret;
|
||||
XrWaitFrameOriginal = Marshal.GetDelegateForFunctionPointer<DelegateXrWaitFrame>(function);
|
||||
}
|
||||
function = xrWaitFrameInterceptorPtr;
|
||||
return XrResult.XR_SUCCESS;
|
||||
}
|
||||
|
||||
public struct XrFrameWaitInfo
|
||||
{
|
||||
public XrStructureType type;
|
||||
public IntPtr next;
|
||||
}
|
||||
|
||||
public struct XrFrameState
|
||||
{
|
||||
public XrStructureType type;
|
||||
public IntPtr next;
|
||||
public XrTime predictedDisplayTime;
|
||||
public XrDuration predictedDisplayPeriod;
|
||||
public XrBool32 shouldRender;
|
||||
}
|
||||
|
||||
bool isWaitFrameIntercepted = false;
|
||||
|
||||
public delegate XrResult DelegateXrWaitFrame(XrSession session, ref XrFrameWaitInfo frameWaitInfo, ref XrFrameState frameState);
|
||||
private static readonly DelegateXrWaitFrame xrWaitFrameInterceptorHandle = new DelegateXrWaitFrame(XrWaitFrameInterceptor);
|
||||
private static readonly IntPtr xrWaitFrameInterceptorPtr = Marshal.GetFunctionPointerForDelegate(xrWaitFrameInterceptorHandle);
|
||||
static DelegateXrWaitFrame XrWaitFrameOriginal = null;
|
||||
|
||||
[MonoPInvokeCallback(typeof(DelegateXrWaitFrame))]
|
||||
private static XrResult XrWaitFrameInterceptor(XrSession session, ref XrFrameWaitInfo frameWaitInfo, ref XrFrameState frameState)
|
||||
{
|
||||
// instance must not null
|
||||
//if (instance == null)
|
||||
// return XrWaitFrameOriginal(session, ref frameWaitInfo, ref frameState);
|
||||
Profiler.BeginSample("VI:WaitFrame");
|
||||
instance.isWaitFrameIntercepted = true;
|
||||
XrResult result = XrResult.XR_SUCCESS;
|
||||
if (instance.BeforeOriginalWaitFrame != null &&
|
||||
!instance.BeforeOriginalWaitFrame(session, ref frameWaitInfo, ref frameState, ref result))
|
||||
{
|
||||
Profiler.EndSample();
|
||||
return result;
|
||||
}
|
||||
var ret = XrWaitFrameOriginal(session, ref frameWaitInfo, ref frameState);
|
||||
instance.AfterOriginalWaitFrame?.Invoke(session, ref frameWaitInfo, ref frameState, ref result);
|
||||
currentFrameState = frameState;
|
||||
Profiler.EndSample();
|
||||
return result;
|
||||
}
|
||||
|
||||
static XrFrameState currentFrameState = new XrFrameState() { predictedDisplayTime = 0 };
|
||||
|
||||
/// <summary>
|
||||
/// Get the waitframe's result: XrFrameState. This result used in update is not matching the current frame. Use it after onBeforeRender.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public XrFrameState GetCurrentFrameState()
|
||||
{
|
||||
if (!isWaitFrameIntercepted) throw new Exception("ViveInterceptors is not intercepted");
|
||||
|
||||
return currentFrameState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Must request xrWaitFrame before calling this function. This result used in update is not matching the current frame. Use it after onBeforeRender.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public XrTime GetPredictTime()
|
||||
{
|
||||
if (!isWaitFrameIntercepted) throw new Exception("ViveInterceptors is not intercepted");
|
||||
|
||||
//Debug.Log($"{TAG}: XrWaitFrameInterceptor(predictedDisplayTime={currentFrameState.predictedDisplayTime}");
|
||||
if (currentFrameState.predictedDisplayTime == 0)
|
||||
return new XrTime((long)(1000000L * (Time.unscaledTimeAsDouble + 0.011f)));
|
||||
else
|
||||
return currentFrameState.predictedDisplayTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register WaitFrame event
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="frameWaitInfo"></param>
|
||||
/// <param name="frameState"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
public delegate bool DelegateXrWaitFrameInterceptor(XrSession session, ref XrFrameWaitInfo frameWaitInfo, ref XrFrameState frameState, ref XrResult result);
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called before the original function.
|
||||
/// </summary>
|
||||
public DelegateXrWaitFrameInterceptor BeforeOriginalWaitFrame;
|
||||
|
||||
/// <summary>
|
||||
/// Use this to intercept the original function. This will be called after the original function.
|
||||
/// </summary>
|
||||
public DelegateXrWaitFrameInterceptor AfterOriginalWaitFrame;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bfc07f267ee39b47a4bcbb8c1c786cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
247
Packages/com.htc.upm.vive.openxr/Runtime/Common/ViveLog.cs
Normal file
247
Packages/com.htc.upm.vive.openxr/Runtime/Common/ViveLog.cs
Normal file
@@ -0,0 +1,247 @@
|
||||
// Copyright HTC Corporation All Rights Reserved.
|
||||
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
using System.Runtime.InteropServices;
|
||||
#endif
|
||||
using System.Text;
|
||||
// Non android will need UnityEngine
|
||||
using UnityEngine;
|
||||
|
||||
namespace VIVE.OpenXR
|
||||
{
|
||||
public static class Log
|
||||
{
|
||||
public const string TAG = "VIVE.OpenXR";
|
||||
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
[DllImport("liblog.so")]
|
||||
private static extern int __android_log_print(int prio, string tag, string fmt, string msg);
|
||||
#endif
|
||||
|
||||
// Use ("%s", message) instead of just (message) is because of the following reason:
|
||||
// In case message contains special characters like %, \n, \r, etc. It will be treated as format string.
|
||||
// This is a little waste of performance, but it's safer.
|
||||
|
||||
/// <summary>
|
||||
/// Not show in Standalone
|
||||
/// </summary>
|
||||
public static void D(string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(3, TAG, "%s", message); // Android Debug
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void I(string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(4, TAG, "%s", message); // Android Info
|
||||
#else
|
||||
Debug.Log(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void W(string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(5, TAG, "%s", message); // Android Warning
|
||||
#else
|
||||
Debug.LogWarning(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void E(string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(6, TAG, "%s", message); // Android Error
|
||||
#else
|
||||
Debug.LogError(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not show in Standalone
|
||||
/// </summary>
|
||||
public static void D(string tag, string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(3, tag, "%s", message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void I(string tag, string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(4, tag, "%s", message);
|
||||
#else
|
||||
Debug.LogFormat("{0}: {1}", tag, message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void W(string tag, string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(5, tag, "%s", message); // Android Warning
|
||||
#else
|
||||
Debug.LogWarningFormat("{0}: {1}", tag, message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void E(string tag, string message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(6, tag, "%s", message); // Android Error
|
||||
#else
|
||||
Debug.LogErrorFormat("{0}: {1}", tag, message);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not show in Standalone
|
||||
/// </summary>
|
||||
public static void D(StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(3, TAG, "%s", message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void I(StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(4, TAG, "%s", message.ToString());
|
||||
#else
|
||||
Debug.Log(message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void W(StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(5, TAG, "%s", message.ToString()); // Android Warning
|
||||
#else
|
||||
Debug.LogWarning(message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void E(StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(6, TAG, "%s", message.ToString()); // Android Error
|
||||
#else
|
||||
Debug.LogError(message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not show in Standalone
|
||||
/// </summary>
|
||||
public static void D(string tag, StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(3, tag, "%s", message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void I(string tag, StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(4, tag, "%s", message.ToString());
|
||||
#else
|
||||
Debug.LogFormat("{0}: {1}", tag, message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void W(string tag, StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(5, tag, "%s", message.ToString()); // Android Warning
|
||||
#else
|
||||
Debug.LogWarningFormat("{0}: {1}", tag, message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void E(string tag, StringBuilder message)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(6, tag, "%s", message.ToString()); // Android Error
|
||||
#else
|
||||
Debug.LogErrorFormat("{0}: {1}", tag, message.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not show in Standalone
|
||||
/// </summary>
|
||||
public static void DFmt(string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(3, TAG, "%s", string.Format(fmt, args));
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void IFmt(string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(4, TAG, "%s", string.Format(fmt, args));
|
||||
#else
|
||||
Debug.LogFormat(fmt, args);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void WFmt(string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(5, TAG, "%s", string.Format(fmt, args)); // Android Warning
|
||||
#else
|
||||
Debug.LogWarningFormat(fmt, args);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void EFmt(string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(6, TAG, "%s", string.Format(fmt, args)); // Android Error
|
||||
#else
|
||||
Debug.LogErrorFormat(fmt, args);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not show in Standalone
|
||||
/// </summary>
|
||||
public static void DFmt(string tag, string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(3, tag, "%s", string.Format(fmt, args));
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void IFmt(string tag, string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(4, tag, "%s", string.Format(fmt, args));
|
||||
#else
|
||||
Debug.LogFormat("{0}: {1}", tag, string.Format(fmt, args));
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void WFmt(string tag, string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(5, tag, "%s", string.Format(fmt, args)); // Android Warning
|
||||
#else
|
||||
Debug.LogWarningFormat("{0}: {1}", tag, fmt, string.Format(fmt, args));
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void EFmt(string tag, string fmt, params object[] args)
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
__android_log_print(6, tag, "%s", string.Format(fmt, args)); // Android Error
|
||||
#else
|
||||
Debug.LogErrorFormat("{0}: {1}", tag, fmt, string.Format(fmt, args));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9750d8d4e8eb4994088534cb111510d3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,338 @@
|
||||
using AOT;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
|
||||
namespace VIVE.OpenXR.Common.RenderThread
|
||||
{
|
||||
#region syncObject
|
||||
public class Message
|
||||
{
|
||||
public bool isFree = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MessagePool class manages a pool of message objects for reuse. You can enter any kind of message object.
|
||||
/// However when obtain, the message object will not able to cast to the type you want.
|
||||
/// You should only use one kind of message. Not mix different kind of message.
|
||||
/// </summary>
|
||||
public class MessagePool
|
||||
{
|
||||
// pool member is used to store message objects in a list.
|
||||
// Note that the size of this list will dynamically adjust as needed but will not automatically shrink.
|
||||
private readonly List<Message> pool = new List<Message>(2) { };
|
||||
private int index = 0;
|
||||
|
||||
public MessagePool() { }
|
||||
|
||||
// Next method calculates the next index value for cycling through message objects in the pool.
|
||||
private int Next(int value)
|
||||
{
|
||||
if (++value >= pool.Count)
|
||||
value = 0;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Obtain method retrieves a message object from the pool.
|
||||
// Ensure proper state setup for the message after retrieval and call Release() to the message after use.
|
||||
public T Obtain<T>() where T : Message, new()
|
||||
{
|
||||
int c = pool.Count;
|
||||
int i = index;
|
||||
for (int j = 0; j < c; i++, j++)
|
||||
{
|
||||
if (i >= c)
|
||||
i = 0;
|
||||
if (pool[i].isFree)
|
||||
{
|
||||
//Debug.LogError("Obtain idx=" + i);
|
||||
index = i;
|
||||
return (T)pool[i];
|
||||
}
|
||||
}
|
||||
index = Next(i);
|
||||
var newItem = new T()
|
||||
{
|
||||
isFree = true
|
||||
};
|
||||
pool.Insert(index, newItem);
|
||||
//Log.d("RT.MessagePool.Obtain<" + typeof(T) + ">() pool count=" + pool.Count); // Not to expose developer's type.
|
||||
Log.D("RT.MessagePool.Obtain() pool count=" + pool.Count);
|
||||
return newItem;
|
||||
}
|
||||
|
||||
// Lock method marks a message as "in use" to prevent other code from reusing it.
|
||||
// This is already called to the message obtained from the pool.
|
||||
public static void Lock(Message msg)
|
||||
{
|
||||
msg.isFree = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release method marks a message as "free" so that other code can reuse it.
|
||||
/// You can use it in RenderThread. It will not trigger the GC event.
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
public static void Release(Message msg)
|
||||
{
|
||||
msg.isFree = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// PreAllocatedQueue class is a message queue based on MessagePool for preallocating message objects.
|
||||
/// Its main functionality is to add message objects to the queue and retrieve them from the queue.
|
||||
/// Messages should be enqueued in GameThread and dequeued in RenderThread.
|
||||
/// In render thread, dequeue will not trigger the GC event. Because the queue is preallocated.
|
||||
/// The 'lock' expression is not used for list's size change. Because lock should be avoid used in RenderThread.
|
||||
/// Set the queueSize as the double count of message you want to pass to render thread in one frame, and the
|
||||
/// list will never change size during runtime. Therefore we don't need to use 'lock' to protect the list.
|
||||
/// </summary>
|
||||
public class PreAllocatedQueue : MessagePool
|
||||
{
|
||||
// list member is used to store preallocated message objects in a list.
|
||||
// Note that the size of this list is set during initialization and does not dynamically adjust.
|
||||
private List<Message> list = new List<Message>();
|
||||
private int queueBegin = 0;
|
||||
private int queueEnd = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The queueSize should be the double count of message you want to pass to render thread in one frame.
|
||||
/// </summary>
|
||||
/// <param name="queueSize"></param>
|
||||
public PreAllocatedQueue(int queueSize = 2) : base()
|
||||
{
|
||||
for (int i = 0; i < queueSize; i++)
|
||||
{
|
||||
list.Add(null);
|
||||
}
|
||||
}
|
||||
|
||||
private int Next(int value)
|
||||
{
|
||||
if (++value >= list.Count)
|
||||
value = 0;
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enqueue method adds a message object to the queue.
|
||||
/// If the queue is full, the new message is added to the end of the list.
|
||||
///
|
||||
/// This function is designed to use the message object obtained from the MessagePool.
|
||||
/// Ensure only one type of message object is used in the queue.
|
||||
///
|
||||
/// Enqueue will increase the queue size if the queue is full. This may trigger GC.Alloc.
|
||||
/// This function should be used in GameThread.
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
public void Enqueue(Message msg)
|
||||
{
|
||||
Lock(msg);
|
||||
queueEnd = Next(queueEnd);
|
||||
|
||||
// If the queue is full, add the message to the end of the list. Should not let it happen.
|
||||
// Use larger queue size to avoid this issue.
|
||||
// If you see the error log here, you should increase the queue size in your design.
|
||||
if (queueEnd == queueBegin)
|
||||
{
|
||||
// Should let Insert and queueBegin be atomic. No lock protection here.
|
||||
list.Insert(queueEnd, msg);
|
||||
queueBegin++;
|
||||
Debug.LogError("RT.MessagePool.Enqueue() list count=" + list.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
list[queueEnd] = msg;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dequeue method retrieves a message object from the queue.
|
||||
/// This method returns the first message object in the queue and removes it from the queue.
|
||||
/// This function will not trigger the GC event. Free to use in RenderThread.
|
||||
/// After use the Message, call Release() to the message.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Message Dequeue()
|
||||
{
|
||||
// No lock protection here. If list is not change size, it is safe.
|
||||
// However if list changed size, it is safe in most case.
|
||||
queueBegin = Next(queueBegin);
|
||||
return list[queueBegin];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RenderThreadTask class is used to execute specified tasks on the rendering thread.
|
||||
/// You don't need to develop a native function to run your task on the rendering thread.
|
||||
/// And you don't need to design how to pass data to render thread.
|
||||
/// This class can be run in Unity Editor since Unity 2021. Test your code in Unity Editor can save your time.
|
||||
///
|
||||
/// You should only create RenderThreadTask as static readonly. Do not create RenderThreadTask in dynamic.
|
||||
///
|
||||
/// You should not run Unity.Engine code in RenderThread. It will cause the Unity.Engine to hang.
|
||||
/// Any exception will not be caught and shown in RenderThread.
|
||||
/// You should print your error message out to clearify your issue.
|
||||
///
|
||||
/// The 'lock' expression is not used here. Because I believe the lock is not necessary in this case.
|
||||
/// And the lock will cause the performance issue. All the design here help you not to use 'lock'.
|
||||
/// </summary>
|
||||
public class RenderThreadTask
|
||||
{
|
||||
private static IntPtr GetFunctionPointerForDelegate(Delegate del)
|
||||
{
|
||||
return System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(del);
|
||||
}
|
||||
|
||||
public delegate void RenderEventDelegate(int e);
|
||||
private static readonly RenderEventDelegate handle = new RenderEventDelegate(RunSyncObjectInRenderThread);
|
||||
private static readonly IntPtr handlePtr = GetFunctionPointerForDelegate(handle);
|
||||
|
||||
public delegate void Receiver(PreAllocatedQueue dataQueue);
|
||||
|
||||
// CommandList is used to store all RenderThreadTask objects.
|
||||
// Do not create RenderThreadTask object in dynamic. It will cause the CommandList to increase infinitly.
|
||||
private static List<RenderThreadTask> CommandList = new List<RenderThreadTask>();
|
||||
|
||||
private PreAllocatedQueue queue;
|
||||
public PreAllocatedQueue Queue { get { return queue; } }
|
||||
|
||||
private readonly Receiver receiver;
|
||||
private readonly int id;
|
||||
|
||||
/// <summary>
|
||||
/// Input the receiver as render thread callback. The receiver will be executed in render thread.
|
||||
/// queueSize should be the double count of message you want to pass to render thread in one frame.
|
||||
/// </summary>
|
||||
/// <param name="render">The callback in render thread.</param>
|
||||
/// <param name="queueSize">If issue this event once in a frame, set queueSize as 2.</param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public RenderThreadTask(Receiver render, int queueSize = 2)
|
||||
{
|
||||
queue = new PreAllocatedQueue(queueSize);
|
||||
receiver = render;
|
||||
if (receiver == null)
|
||||
throw new ArgumentNullException("receiver should not be null");
|
||||
|
||||
CommandList.Add(this);
|
||||
id = CommandList.IndexOf(this);
|
||||
}
|
||||
|
||||
~RenderThreadTask()
|
||||
{
|
||||
// Remove could be in a random order, and will cause orderId change. DO not remove any of them.
|
||||
//try { CommandList.Remove(this); } finally { }
|
||||
}
|
||||
|
||||
void IssuePluginEvent(IntPtr callback, int eventID)
|
||||
{
|
||||
// Older version will hang after run script in render thread.
|
||||
GL.IssuePluginEvent(callback, eventID);
|
||||
return;
|
||||
}
|
||||
|
||||
void IssuePluginEvent(CommandBuffer cmdBuf, IntPtr callback, int eventID)
|
||||
{
|
||||
cmdBuf.IssuePluginEvent(callback, eventID);
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// IssueEvent method submits this task's receiver, which is set in constructor, to be executed on the rendering thread.
|
||||
/// </summary>
|
||||
public void IssueEvent()
|
||||
{
|
||||
// Let the render thread run the RunSyncObjectInRenderThread(id)
|
||||
IssuePluginEvent(handlePtr, id);
|
||||
}
|
||||
|
||||
public void IssueInCommandBuffer(CommandBuffer cmdBuf)
|
||||
{
|
||||
// Let the render thread run the RunSyncObjectInRenderThread(id)
|
||||
IssuePluginEvent(cmdBuf, handlePtr, id);
|
||||
}
|
||||
|
||||
// Called by RunSyncObjectInRenderThread()
|
||||
private void Receive()
|
||||
{
|
||||
receiver(queue);
|
||||
}
|
||||
|
||||
// RunSyncObjectInRenderThread method is a static method used to execute a specified task on the rendering thread.
|
||||
// This method is invoked by Unity's rendering event mechanism and does not need to be called directly by developers.
|
||||
[MonoPInvokeCallback(typeof(RenderEventDelegate))]
|
||||
private static void RunSyncObjectInRenderThread(int id)
|
||||
{
|
||||
CommandList[id].Receive();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region sample
|
||||
// Not to compile this sample into your application. Just for reference. You can run this sample in Unity Editor and it will work.
|
||||
#if UNITY_EDITOR
|
||||
public class ViveRenderThreadTaskSample : MonoBehaviour
|
||||
{
|
||||
// Create your own message class.
|
||||
internal class SampleMessage : Message
|
||||
{
|
||||
public int dataPassedToRenderThread;
|
||||
}
|
||||
|
||||
// Use static readonly to create RenderThreadTask. Keep internal to avoid miss use by other developers.
|
||||
internal static readonly RenderThreadTask sampleRenderThreadTask1 = new RenderThreadTask(SampleReceiver1);
|
||||
// Different task use different RenderThreadTask and different recevier.
|
||||
internal static readonly RenderThreadTask sampleRenderThreadTask2 = new RenderThreadTask(SampleReceiver2);
|
||||
|
||||
private static void SampleReceiver1(PreAllocatedQueue dataQueue)
|
||||
{
|
||||
var msg = dataQueue.Dequeue() as SampleMessage;
|
||||
if (msg != null)
|
||||
{
|
||||
// Keep data before release. Use local variable to keep data and release msg early. Should not keep the msg instance itself.
|
||||
var data = msg.dataPassedToRenderThread;
|
||||
// Make sure release the msg if finished. Other wise the memory will keep increasing when Obtain.
|
||||
MessagePool.Release(msg);
|
||||
Debug.Log("Task1, the data passed to render thread: " + data);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SampleReceiver2(PreAllocatedQueue dataQueue)
|
||||
{
|
||||
var msg = dataQueue.Dequeue() as SampleMessage;
|
||||
if (msg != null)
|
||||
{
|
||||
// Keep data before release. Use local variable to keep data and release msg early. Should not keep the msg instance itself.
|
||||
var data = msg.dataPassedToRenderThread;
|
||||
// Make sure release the msg if finished. Other wise the memory will keep increasing when Obtain.
|
||||
MessagePool.Release(msg);
|
||||
Debug.Log("Task2, the data passed to render thread: " + data);
|
||||
}
|
||||
}
|
||||
|
||||
// Send a message to the render thread every frame.
|
||||
private void Update()
|
||||
{
|
||||
// Make sure only one kind of message object is used in the queue.
|
||||
var msg = sampleRenderThreadTask1.Queue.Obtain<SampleMessage>();
|
||||
msg.dataPassedToRenderThread = 123;
|
||||
sampleRenderThreadTask1.Queue.Enqueue(msg);
|
||||
sampleRenderThreadTask1.IssueEvent();
|
||||
}
|
||||
|
||||
// Send a message to render thread when something clicked. Make sure only one click in one frame because the queue size is only two.
|
||||
public void OnClicked()
|
||||
{
|
||||
// Reuse the same message type is ok.
|
||||
var msg = sampleRenderThreadTask2.Queue.Obtain<SampleMessage>();
|
||||
msg.dataPassedToRenderThread = 234;
|
||||
sampleRenderThreadTask2.Queue.Enqueue(msg);
|
||||
sampleRenderThreadTask2.IssueEvent();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 251b4bedf6420fc4e84be778e501343f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user