OpenXRExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.XR.OpenXR.Features;
  6. #if UNITY_EDITOR
  7. using UnityEditor.XR.OpenXR.Features;
  8. #endif
  9. namespace Unity.XR.OpenXR.Features.PICOSupport
  10. {
  11. #if UNITY_EDITOR
  12. public class FeatureConfig
  13. {
  14. public const string OpenXrExtensionList = "XR_EXT_local_floor " +
  15. "XR_FB_triangle_mesh " +
  16. "XR_FB_composition_layer_alpha_blend " +
  17. "XR_KHR_composition_layer_color_scale_bias " +
  18. "XR_KHR_composition_layer_cylinder" +
  19. "XR_KHR_composition_layer_equirect" +
  20. "XR_KHR_composition_layer_cube";
  21. }
  22. [OpenXRFeature(UiName = "PICO OpenXR Features",
  23. Desc = "PICO XR Features for OpenXR.",
  24. Company = "PICO",
  25. Version = "1.0.0",
  26. BuildTargetGroups = new[] { BuildTargetGroup.Android },
  27. OpenxrExtensionStrings = FeatureConfig.OpenXrExtensionList,
  28. FeatureId = featureId
  29. )]
  30. #endif
  31. public class OpenXRExtensions : OpenXRFeature
  32. {
  33. public const string featureId = "com.unity.openxr.pico.features";
  34. private static ulong xrInstance = 0ul;
  35. private static ulong xrSession = 0ul;
  36. public static bool isPicoSupport = false;
  37. protected override bool OnInstanceCreate(ulong instance)
  38. {
  39. xrInstance = instance;
  40. xrSession = 0ul;
  41. return true;
  42. }
  43. /// <inheritdoc/>
  44. protected override void OnSessionCreate(ulong xrSessionId)
  45. {
  46. xrSession = xrSessionId;
  47. Initialize(xrGetInstanceProcAddr, xrInstance, xrSession);
  48. setColorSpace((int)QualitySettings.activeColorSpace);
  49. base.OnSessionCreate(xrSessionId);
  50. }
  51. /// <inheritdoc/>
  52. protected override void OnInstanceDestroy(ulong xrInstance)
  53. {
  54. base.OnInstanceDestroy(xrInstance);
  55. xrInstance = 0ul;
  56. }
  57. // HookGetInstanceProcAddr
  58. protected override IntPtr HookGetInstanceProcAddr(IntPtr func)
  59. {
  60. // return base.HookGetInstanceProcAddr(func);
  61. return HookCreateInstance(func);
  62. }
  63. /// <inheritdoc/>
  64. protected override void OnSessionDestroy(ulong xrSessionId)
  65. {
  66. base.OnSessionDestroy(xrSessionId);
  67. xrSession = 0ul;
  68. }
  69. protected override void OnAppSpaceChange(ulong xrSpace)
  70. {
  71. SpaceChange(xrSpace);
  72. base.OnAppSpaceChange(xrSpace);
  73. }
  74. public static int GetReferenceSpaceBoundsRect(XrReferenceSpaceType referenceSpace, ref XrExtent2Df extent2D)
  75. {
  76. return xrGetReferenceSpaceBoundsRect(
  77. xrSession, referenceSpace, ref extent2D);
  78. }
  79. public static XrReferenceSpaceType[] EnumerateReferenceSpaces()
  80. {
  81. UInt32 Output = 0;
  82. XrReferenceSpaceType[] outSpaces = null;
  83. xrEnumerateReferenceSpaces(xrSession, 0, ref Output, outSpaces);
  84. if (Output <= 0)
  85. {
  86. return null;
  87. }
  88. outSpaces = new XrReferenceSpaceType[Output];
  89. xrEnumerateReferenceSpaces(xrSession, Output, ref Output, outSpaces);
  90. return outSpaces;
  91. }
  92. public static void CreateLayerParam(PxrLayerParam layerParam)
  93. {
  94. PLog.i("POXR_CreateLayerParam() ");
  95. #if UNITY_ANDROID && !UNITY_EDITOR
  96. xrCreateLayerParam(layerParam);
  97. #endif
  98. }
  99. public static int GetLayerImageCount(int layerId, EyeType eye, ref UInt32 imageCount)
  100. {
  101. int result = 0;
  102. #if UNITY_ANDROID && !UNITY_EDITOR
  103. result = xrGetLayerImageCount(layerId, eye, ref imageCount);
  104. #endif
  105. PLog.i("GetLayerImageCount() layerId:" + layerId + " eye:" + eye + " imageCount:" + imageCount +
  106. " result:" + result);
  107. return result;
  108. }
  109. public static void GetLayerImagePtr(int layerId, EyeType eye, int imageIndex, ref IntPtr image)
  110. {
  111. #if UNITY_ANDROID && !UNITY_EDITOR
  112. xrGetLayerImagePtr(layerId, eye, imageIndex, ref image);
  113. #endif
  114. PLog.i("GetLayerImagePtr() layerId:" + layerId + " eye:" + eye + " imageIndex:" + imageIndex + " image:" +
  115. image);
  116. }
  117. public static void DestroyLayerByRender(int layerId)
  118. {
  119. PLog.i("DestroyLayerByRender() layerId:" + layerId);
  120. #if UNITY_ANDROID && !UNITY_EDITOR
  121. xrDestroyLayerByRender(layerId);
  122. #endif
  123. }
  124. public static bool SubmitLayerQuad(IntPtr ptr)
  125. {
  126. int result = 0;
  127. #if UNITY_ANDROID && !UNITY_EDITOR
  128. result = xrSubmitLayerQuad(ptr);
  129. #endif
  130. PLog.d("SubmitLayerQuad() ptr:" + ptr + " result:" + result);
  131. return result == -8;
  132. }
  133. public static bool SubmitLayerCylinder(IntPtr ptr)
  134. {
  135. int result = 0;
  136. #if UNITY_ANDROID && !UNITY_EDITOR
  137. result = xrSubmitLayerCylinder(ptr);
  138. #endif
  139. PLog.d("SubmitLayerCylinder() ptr:" + ptr + " result:" + result);
  140. return result == -8;
  141. }
  142. public static bool SubmitLayerEquirect(IntPtr ptr)
  143. {
  144. int result = 0;
  145. #if UNITY_ANDROID && !UNITY_EDITOR
  146. result = xrSubmitLayerEquirect(ptr);
  147. #endif
  148. PLog.d("SubmitLayerEquirect() ptr:" + ptr + " result:" + result);
  149. return result == -8;
  150. }
  151. public static bool SubmitLayerCube(IntPtr ptr)
  152. {
  153. int result = 0;
  154. #if UNITY_ANDROID && !UNITY_EDITOR
  155. result = xrSubmitLayerCube(ptr);
  156. #endif
  157. PLog.d("xrSubmitLayerCube() ptr:" + ptr + " result:" + result);
  158. return result == -8;
  159. }
  160. public static int GetLayerNextImageIndex(int layerId, ref int imageIndex)
  161. {
  162. int result = 0;
  163. #if UNITY_ANDROID && !UNITY_EDITOR
  164. result = xrGetLayerNextImageIndex(layerId, ref imageIndex);
  165. #endif
  166. PLog.d("GetLayerNextImageIndex() layerId:" + layerId + " imageIndex:" + imageIndex + " result:" + result);
  167. return result;
  168. }
  169. protected override void OnSystemChange(ulong xrSystem)
  170. {
  171. base.OnSystemChange(xrSystem);
  172. SystemChange(xrSystem);
  173. }
  174. public static float GetLocationHeight()
  175. {
  176. float height = 0;
  177. getLocationHeight( ref height);
  178. return height;
  179. }
  180. const string extLib = "openxr_pico";
  181. [DllImport(extLib, EntryPoint = "PICO_Initialize", CallingConvention = CallingConvention.Cdecl)]
  182. private static extern void Initialize(IntPtr xrGetInstanceProcAddr, ulong xrInstance, ulong xrSession);
  183. [DllImport(extLib, EntryPoint = "PICO_HookCreateInstance", CallingConvention = CallingConvention.Cdecl)]
  184. public static extern IntPtr HookCreateInstance(IntPtr func);
  185. [DllImport(extLib, EntryPoint = "PICO_OnAppSpaceChange", CallingConvention = CallingConvention.Cdecl)]
  186. private static extern void SpaceChange(ulong xrSession);
  187. [DllImport(extLib, EntryPoint = "PICO_OnSystemChange", CallingConvention = CallingConvention.Cdecl)]
  188. public static extern void SystemChange(ulong xrSystem);
  189. [DllImport(extLib, EntryPoint = "PICO_xrEnumerateReferenceSpaces", CallingConvention = CallingConvention.Cdecl)]
  190. private static extern int xrEnumerateReferenceSpaces(ulong xrSession, UInt32 CountInput, ref UInt32 CountOutput,
  191. XrReferenceSpaceType[] Spaces);
  192. [DllImport(extLib, EntryPoint = "PICO_xrGetReferenceSpaceBoundsRect", CallingConvention = CallingConvention.Cdecl)]
  193. private static extern int xrGetReferenceSpaceBoundsRect(ulong xrSession, XrReferenceSpaceType referenceSpace,
  194. ref XrExtent2Df extent2D);
  195. [DllImport(extLib, EntryPoint = "PICO_CreateLayerParam", CallingConvention = CallingConvention.Cdecl)]
  196. private static extern void xrCreateLayerParam(PxrLayerParam layerParam);
  197. [DllImport(extLib, EntryPoint = "PICO_GetLayerImageCount", CallingConvention = CallingConvention.Cdecl)]
  198. private static extern int xrGetLayerImageCount(int layerId, EyeType eye, ref UInt32 imageCount);
  199. [DllImport(extLib, EntryPoint = "PICO_GetLayerImagePtr", CallingConvention = CallingConvention.Cdecl)]
  200. public static extern void xrGetLayerImagePtr(int layerId, EyeType eye, int imageIndex, ref IntPtr image);
  201. [DllImport(extLib, EntryPoint = "PICO_DestroyLayerByRender", CallingConvention = CallingConvention.Cdecl)]
  202. private static extern void xrDestroyLayerByRender(int layerId);
  203. [DllImport(extLib, EntryPoint = "PICO_SubmitLayerQuad", CallingConvention = CallingConvention.Cdecl)]
  204. private static extern int xrSubmitLayerQuad(IntPtr ptr);
  205. [DllImport(extLib, EntryPoint = "PICO_SubmitLayerCylinder", CallingConvention = CallingConvention.Cdecl)]
  206. private static extern int xrSubmitLayerCylinder(IntPtr ptr);
  207. [DllImport(extLib, EntryPoint = "PICO_SubmitLayerEquirect", CallingConvention = CallingConvention.Cdecl)]
  208. private static extern int xrSubmitLayerEquirect(IntPtr ptr);
  209. [DllImport(extLib, EntryPoint = "PICO_SubmitLayerCube", CallingConvention = CallingConvention.Cdecl)]
  210. private static extern int xrSubmitLayerCube(IntPtr ptr);
  211. [DllImport(extLib, EntryPoint = "PICO_GetLayerNextImageIndex", CallingConvention = CallingConvention.Cdecl)]
  212. private static extern int xrGetLayerNextImageIndex(int layerId, ref int imageIndex);
  213. [DllImport(extLib, EntryPoint = "PICO_SetColorSpace", CallingConvention = CallingConvention.Cdecl)]
  214. private static extern int setColorSpace(int colorSpace);
  215. [DllImport(extLib, EntryPoint = "PICO_GetLocationHeight", CallingConvention = CallingConvention.Cdecl)]
  216. private static extern XrResult getLocationHeight(ref float delaY);
  217. [DllImport(extLib, EntryPoint = "PICO_SetMarkMode", CallingConvention = CallingConvention.Cdecl)]
  218. public static extern void SetMarkMode();
  219. }
  220. }