FoveationFeature.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using UnityEditor;
  2. using UnityEngine.XR.OpenXR.Features;
  3. using System.Runtime.InteropServices;
  4. using System;
  5. using Unity.XR.OpenXR.Features.PICOSupport;
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor.XR.OpenXR.Features;
  9. [OpenXRFeature(UiName = "OpenXR Foveation",
  10. BuildTargetGroups = new[] { BuildTargetGroup.Android },
  11. OpenxrExtensionStrings = extensionList,
  12. Company = "PICO",
  13. Version = "1.0.0",
  14. FeatureId = featureId)]
  15. #endif
  16. public class FoveationFeature : OpenXRFeatureBase
  17. {
  18. public const string extensionList = "XR_FB_foveation " +
  19. "XR_FB_foveation_configuration " +
  20. "XR_FB_foveation_vulkan " +
  21. "XR_META_foveation_eye_tracked " +
  22. "XR_META_vulkan_swapchain_create_info " +
  23. "XR_FB_swapchain_update_state ";
  24. public const string featureId = "com.pico.openxr.feature.foveation";
  25. public enum FoveatedRenderingLevel
  26. {
  27. Off = 0,
  28. Low = 1,
  29. Medium = 2,
  30. High = 3
  31. }
  32. public enum FoveatedRenderingMode
  33. {
  34. FixedFoveatedRendering = 0,
  35. EyeTrackedFoveatedRendering = 1
  36. }
  37. private static string TAG = "FoveationFeature";
  38. private static UInt32 _foveatedRenderingLevel = 0;
  39. private static UInt32 _useDynamicFoveation = 0;
  40. public static bool isExtensionEnable =false;
  41. public override string GetExtensionString()
  42. {
  43. return extensionList;
  44. }
  45. public override void Initialize(IntPtr intPtr)
  46. {
  47. isExtensionEnable=_isExtensionEnable;
  48. }
  49. public override void SessionCreate()
  50. {
  51. if (!isExtensionEnable)
  52. {
  53. return ;
  54. }
  55. PICOProjectSetting projectConfig = PICOProjectSetting.GetProjectConfig();
  56. if (projectConfig.foveationEnable)
  57. {
  58. setFoveationEyeTracked(projectConfig.foveatedRenderingMode ==
  59. FoveatedRenderingMode.EyeTrackedFoveatedRendering);
  60. foveatedRenderingLevel = projectConfig.foveatedRenderingLevel;
  61. setSubsampledEnabled(projectConfig.isSubsampledEnabled);
  62. }
  63. }
  64. public static FoveatedRenderingLevel foveatedRenderingLevel
  65. {
  66. get
  67. {
  68. if (!isExtensionEnable)
  69. {
  70. return FoveatedRenderingLevel.Off;
  71. }
  72. UInt32 level;
  73. FBGetFoveationLevel(out level);
  74. PLog.i($" foveatedRenderingLevel get if level= {level}");
  75. return (FoveatedRenderingLevel)level;
  76. }
  77. set
  78. {
  79. if (!isExtensionEnable)
  80. {
  81. return;
  82. }
  83. PLog.i($" foveatedRenderingLevel set if value= {value}");
  84. _foveatedRenderingLevel = (UInt32)value;
  85. FBSetFoveationLevel(xrSession, _foveatedRenderingLevel, 0.0f, _useDynamicFoveation);
  86. }
  87. }
  88. public static bool useDynamicFoveatedRendering
  89. {
  90. get
  91. {
  92. if (!isExtensionEnable)
  93. {
  94. return false;
  95. }
  96. UInt32 dynamic;
  97. FBGetFoveationLevel(out dynamic);
  98. return dynamic != 0;
  99. }
  100. set
  101. {
  102. if (!isExtensionEnable)
  103. {
  104. return ;
  105. }
  106. if (value)
  107. _useDynamicFoveation = 1;
  108. else
  109. _useDynamicFoveation = 0;
  110. FBSetFoveationLevel(xrSession, _foveatedRenderingLevel, 0.0f, _useDynamicFoveation);
  111. }
  112. }
  113. public static bool supportsFoveationEyeTracked
  114. {
  115. get
  116. {
  117. if (!isExtensionEnable)
  118. {
  119. return false;
  120. }
  121. return isSupportsFoveationEyeTracked(xrInstance);
  122. }
  123. }
  124. #region OpenXR Plugin DLL Imports
  125. [DllImport("UnityOpenXR", EntryPoint = "FBSetFoveationLevel")]
  126. private static extern void FBSetFoveationLevel(UInt64 session, UInt32 level, float verticalOffset, UInt32 dynamic);
  127. [DllImport("UnityOpenXR", EntryPoint = "FBGetFoveationLevel")]
  128. private static extern void FBGetFoveationLevel(out UInt32 level);
  129. [DllImport("UnityOpenXR", EntryPoint = "FBGetFoveationDynamic")]
  130. private static extern void FBGetFoveationDynamic(out UInt32 dynamic);
  131. #endregion
  132. const string extLib = "openxr_pico";
  133. [DllImport(extLib, EntryPoint = "PICO_isSupportsFoveationEyeTracked", CallingConvention = CallingConvention.Cdecl)]
  134. private static extern bool isSupportsFoveationEyeTracked(ulong xrInstance);
  135. [DllImport(extLib, EntryPoint = "PICO_setFoveationEyeTracked", CallingConvention = CallingConvention.Cdecl)]
  136. private static extern void setFoveationEyeTracked(bool value);
  137. [DllImport(extLib, EntryPoint = "PICO_setSubsampledEnabled", CallingConvention = CallingConvention.Cdecl)]
  138. private static extern void setSubsampledEnabled(bool value);
  139. }