Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XRPass.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine.Rendering;
  5. namespace UnityEngine.Experimental.Rendering
  6. {
  7. /// <summary>
  8. /// Set of data used to create a XRPass object.
  9. /// </summary>
  10. public struct XRPassCreateInfo
  11. {
  12. internal RenderTargetIdentifier renderTarget;
  13. internal RenderTextureDescriptor renderTargetDesc;
  14. internal RenderTargetIdentifier motionVectorRenderTarget;
  15. internal RenderTextureDescriptor motionVectorRenderTargetDesc;
  16. internal ScriptableCullingParameters cullingParameters;
  17. internal Material occlusionMeshMaterial;
  18. internal float occlusionMeshScale;
  19. internal IntPtr foveatedRenderingInfo;
  20. internal int multipassId;
  21. internal int cullingPassId;
  22. internal bool copyDepth;
  23. internal bool hasMotionVectorPass;
  24. #if ENABLE_VR && ENABLE_XR_MODULE
  25. internal UnityEngine.XR.XRDisplaySubsystem.XRRenderPass xrSdkRenderPass;
  26. #endif
  27. }
  28. /// <summary>
  29. /// XRPass holds the render target information and a list of XRView.
  30. /// XRView contains the parameters required to render (projection and view matrices, viewport, etc)
  31. /// When a pass has 2 views or more, single-pass will be active if the platform supports it.
  32. /// To avoid allocating every frame, XRView is a struct and XRPass is pooled.
  33. /// </summary>
  34. public class XRPass
  35. {
  36. readonly List<XRView> m_Views;
  37. readonly XROcclusionMesh m_OcclusionMesh;
  38. /// <summary>
  39. /// Parameterless constructor.
  40. /// Note: in order to avoid GC, the render pipeline should use XRPass.Create instead of this method.
  41. /// </summary>
  42. public XRPass()
  43. {
  44. m_Views = new List<XRView>(2);
  45. m_OcclusionMesh = new XROcclusionMesh(this);
  46. }
  47. /// <summary>
  48. /// Default allocator method for XRPass.
  49. /// </summary>
  50. /// <param name="createInfo"> A descriptor used to create and initialize the XRPass. </param>
  51. /// <returns> Default XRPass created from createInfo descriptor. </returns>
  52. public static XRPass CreateDefault(XRPassCreateInfo createInfo)
  53. {
  54. XRPass pass = GenericPool<XRPass>.Get();
  55. pass.InitBase(createInfo);
  56. return pass;
  57. }
  58. /// <summary>
  59. /// Default release method. Can be overridden by render pipelines.
  60. /// </summary>
  61. virtual public void Release()
  62. {
  63. GenericPool<XRPass>.Release(this);
  64. }
  65. /// <summary>
  66. /// Returns true if the pass contains at least one view.
  67. /// </summary>
  68. public bool enabled
  69. {
  70. #if ENABLE_VR && ENABLE_XR_MODULE
  71. get => viewCount > 0;
  72. #else
  73. get => false;
  74. #endif
  75. }
  76. /// <summary>
  77. /// Returns true if the pass can use foveated rendering commands.
  78. /// </summary>
  79. public bool supportsFoveatedRendering
  80. {
  81. #if ENABLE_VR && ENABLE_XR_MODULE
  82. get => enabled && foveatedRenderingInfo != IntPtr.Zero && XRSystem.foveatedRenderingCaps != FoveatedRenderingCaps.None;
  83. #else
  84. get => false;
  85. #endif
  86. }
  87. /// <summary>
  88. /// If true, the render pipeline is expected to output a valid depth buffer to the renderTarget.
  89. /// </summary>
  90. public bool copyDepth { get; private set; }
  91. /// <summary>
  92. /// If true, the render pipeline is expected to generate motion data and output to the motionVectorRenderTarget.
  93. /// </summary>
  94. public bool hasMotionVectorPass { get; private set; }
  95. /// <summary>
  96. /// If true, is the first pass of a xr camera
  97. /// </summary>
  98. public bool isFirstCameraPass => multipassId == 0;
  99. /// <summary>
  100. /// If true, is the last pass of a xr camera
  101. /// </summary>
  102. public bool isLastCameraPass => (multipassId == 0 && viewCount <= 1) || (multipassId == 1 && viewCount > 1);
  103. /// <summary>
  104. /// Index of the pass inside the frame.
  105. /// </summary>
  106. public int multipassId { get; private set; }
  107. /// <summary>
  108. /// Index used for culling. It can be shared between multiple passes.
  109. /// </summary>
  110. public int cullingPassId { get; private set; }
  111. /// <summary>
  112. /// Destination render target.
  113. /// </summary>
  114. public RenderTargetIdentifier renderTarget { get; private set; }
  115. /// <summary>
  116. /// Destination render target descriptor.
  117. /// </summary>
  118. public RenderTextureDescriptor renderTargetDesc { get; private set; }
  119. /// <summary>
  120. /// Destination render target for motion vectors
  121. /// </summary>
  122. public RenderTargetIdentifier motionVectorRenderTarget { get; private set; }
  123. /// <summary>
  124. /// Destination render target descriptor for motion vectors.
  125. /// </summary>
  126. public RenderTextureDescriptor motionVectorRenderTargetDesc { get; private set; }
  127. /// <summary>
  128. /// Parameters used for culling.
  129. /// </summary>
  130. public ScriptableCullingParameters cullingParams { get; private set; }
  131. /// <summary>
  132. /// Returns the number of views inside this pass.
  133. /// </summary>
  134. public int viewCount { get => m_Views.Count; }
  135. /// <summary>
  136. /// If true, the render pipeline is expected to use single-pass techniques to save CPU time.
  137. /// </summary>
  138. public bool singlePassEnabled { get => viewCount > 1; }
  139. /// <summary>
  140. /// Native pointer from the XR plugin to be consumed by ConfigureFoveatedRendering.
  141. /// </summary>
  142. public IntPtr foveatedRenderingInfo { get; private set; }
  143. /// <summary>
  144. /// Returns true when the active display has HDR enabled.
  145. /// </summary>
  146. public bool isHDRDisplayOutputActive
  147. {
  148. #if ENABLE_VR && ENABLE_XR_MODULE
  149. get => XRSystem.GetActiveDisplay().hdrOutputSettings?.active ?? false;
  150. #else
  151. get => false;
  152. #endif
  153. }
  154. /// <summary>
  155. /// Returns color gamut of the active HDR display.
  156. /// </summary>
  157. public ColorGamut hdrDisplayOutputColorGamut
  158. {
  159. #if ENABLE_VR && ENABLE_XR_MODULE
  160. get => XRSystem.GetActiveDisplay().hdrOutputSettings?.displayColorGamut ?? ColorGamut.sRGB;
  161. #else
  162. get => ColorGamut.sRGB;
  163. #endif
  164. }
  165. /// <summary>
  166. /// Returns HDR display information of the active HDR display.
  167. /// </summary>
  168. public HDROutputUtils.HDRDisplayInformation hdrDisplayOutputInformation
  169. {
  170. #if ENABLE_VR && ENABLE_XR_MODULE
  171. get => new HDROutputUtils.HDRDisplayInformation(
  172. XRSystem.GetActiveDisplay().hdrOutputSettings?.maxFullFrameToneMapLuminance ?? -1,
  173. XRSystem.GetActiveDisplay().hdrOutputSettings?.maxToneMapLuminance ?? -1,
  174. XRSystem.GetActiveDisplay().hdrOutputSettings?.minToneMapLuminance ?? -1,
  175. XRSystem.GetActiveDisplay().hdrOutputSettings?.paperWhiteNits ?? 160.0f
  176. );
  177. #else
  178. get => new HDROutputUtils.HDRDisplayInformation(-1, -1, -1, 160.0f);
  179. #endif
  180. }
  181. /// <summary>
  182. /// Scaling factor used when drawing the occlusion mesh.
  183. /// </summary>
  184. public float occlusionMeshScale { get; private set; }
  185. /// <summary>
  186. /// Returns the projection matrix for a given view.
  187. /// </summary>
  188. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  189. /// <returns> XR projection matrix for the specified XRView. </returns>
  190. public Matrix4x4 GetProjMatrix(int viewIndex = 0)
  191. {
  192. return m_Views[viewIndex].projMatrix;
  193. }
  194. /// <summary>
  195. /// Returns the view matrix for a given view.
  196. /// </summary>
  197. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  198. /// <returns> XR view matrix for the specified XRView. </returns>
  199. public Matrix4x4 GetViewMatrix(int viewIndex = 0)
  200. {
  201. return m_Views[viewIndex].viewMatrix;
  202. }
  203. /// <summary>
  204. /// Returns true if the previous frame view matrix for a given view is valid.
  205. /// </summary>
  206. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  207. /// <returns> Boolean describing if previous frame view matrix for a given view is valid. </returns>
  208. public bool GetPrevViewValid(int viewIndex = 0)
  209. {
  210. return m_Views[viewIndex].isPrevViewMatrixValid;
  211. }
  212. /// <summary>
  213. /// Returns the previous frame view matrix for a given view.
  214. /// </summary>
  215. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  216. /// <returns> Previous frame XR view matrix for the specified XRView. </returns>
  217. public Matrix4x4 GetPrevViewMatrix(int viewIndex = 0)
  218. {
  219. return m_Views[viewIndex].prevViewMatrix;
  220. }
  221. /// <summary>
  222. /// Returns the viewport for a given view.
  223. /// </summary>
  224. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  225. /// <returns> XR viewport rect for the specified XRView. </returns>
  226. public Rect GetViewport(int viewIndex = 0)
  227. {
  228. return m_Views[viewIndex].viewport;
  229. }
  230. /// <summary>
  231. /// Returns the occlusion mesh for a given view.
  232. /// </summary>
  233. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  234. /// <returns> XR occlusion mesh for the specified XRView. </returns>
  235. public Mesh GetOcclusionMesh(int viewIndex = 0)
  236. {
  237. return m_Views[viewIndex].occlusionMesh;
  238. }
  239. /// <summary>
  240. /// Returns the destination slice index (for texture array) for a given view.
  241. /// </summary>
  242. /// <param name="viewIndex"> Index of XRView to retrieve the data from. </param>
  243. /// <returns> XR target slice index for the specified XRView. </returns>
  244. public int GetTextureArraySlice(int viewIndex = 0)
  245. {
  246. return m_Views[viewIndex].textureArraySlice;
  247. }
  248. /// <summary>
  249. /// Queue up render commands to enable single-pass techniques.
  250. /// Note: depending on the platform and settings, either single-pass instancing or the multiview extension will be used.
  251. /// </summary>
  252. /// <param name="cmd">CommandBuffer to modify</param>
  253. public void StartSinglePass(CommandBuffer cmd)
  254. {
  255. if (enabled)
  256. {
  257. if (singlePassEnabled)
  258. {
  259. if (viewCount <= TextureXR.slices)
  260. {
  261. if (SystemInfo.supportsMultiview)
  262. {
  263. cmd.EnableKeyword(SinglepassKeywords.STEREO_MULTIVIEW_ON);
  264. }
  265. else
  266. {
  267. cmd.EnableKeyword(SinglepassKeywords.STEREO_INSTANCING_ON);
  268. cmd.SetInstanceMultiplier((uint)viewCount);
  269. }
  270. }
  271. else
  272. {
  273. throw new NotImplementedException($"Invalid XR setup for single-pass, trying to render too many views! Max supported: {TextureXR.slices}");
  274. }
  275. }
  276. }
  277. }
  278. /// <summary>
  279. /// Queue up render commands to disable single-pass techniques.
  280. /// </summary>
  281. /// <param name="cmd">IRasterCommandBuffer compatible command buffer to modify (This can be a RasterCommandBuffer or an UnsafeCommandBuffer)</param>
  282. public void StartSinglePass(IRasterCommandBuffer cmd)
  283. {
  284. StartSinglePass((cmd as BaseCommandBuffer).m_WrappedCommandBuffer);
  285. }
  286. /// <summary>
  287. /// Queue up render commands to disable single-pass techniques.
  288. /// </summary>
  289. /// <param name="cmd">CommandBuffer to modify.</param>
  290. public void StopSinglePass(CommandBuffer cmd)
  291. {
  292. if (enabled)
  293. {
  294. if (singlePassEnabled)
  295. {
  296. if (SystemInfo.supportsMultiview)
  297. {
  298. cmd.DisableKeyword(SinglepassKeywords.STEREO_MULTIVIEW_ON);
  299. }
  300. else
  301. {
  302. cmd.DisableKeyword(SinglepassKeywords.STEREO_INSTANCING_ON);
  303. cmd.SetInstanceMultiplier(1);
  304. }
  305. }
  306. }
  307. }
  308. /// <summary>
  309. /// Queue up render commands to disable single-pass techniques.
  310. /// </summary>
  311. /// <param name="cmd">BaseCommandBuffer to modify</param>
  312. public void StopSinglePass(BaseCommandBuffer cmd)
  313. {
  314. StopSinglePass(cmd.m_WrappedCommandBuffer);
  315. }
  316. /// <summary>
  317. /// Returns true if the pass was setup with expected mesh and material.
  318. /// </summary>
  319. public bool hasValidOcclusionMesh { get => m_OcclusionMesh.hasValidOcclusionMesh; }
  320. /// <summary>
  321. /// Generate commands to render the occlusion mesh for this pass.
  322. /// In single-pass mode : the meshes for all views are combined into one mesh,
  323. /// where the corresponding view index is encoded into each vertex. The keyword
  324. /// "XR_OCCLUSION_MESH_COMBINED" is also enabled when rendering the combined mesh.
  325. /// </summary>
  326. /// <param name="cmd">CommandBuffer to modify</param>
  327. /// <param name="renderIntoTexture">Set to true when rendering into a render texture. Used for handling Unity yflip.</param>
  328. public void RenderOcclusionMesh(CommandBuffer cmd, bool renderIntoTexture = false)
  329. {
  330. if(occlusionMeshScale > 0)
  331. m_OcclusionMesh.RenderOcclusionMesh(cmd, occlusionMeshScale, renderIntoTexture);
  332. }
  333. /// <summary>
  334. /// Generate commands to render the occlusion mesh for this pass.
  335. /// In single-pass mode : the meshes for all views are combined into one mesh,
  336. /// where the corresponding view index is encoded into each vertex. The keyword
  337. /// "XR_OCCLUSION_MESH_COMBINED" is also enabled when rendering the combined mesh.
  338. /// </summary>
  339. /// <param name="cmd">RasterCommandBuffer to modify</param>
  340. /// <param name="renderIntoTexture">Set to true when rendering into a render texture. Used for handling Unity yflip.</param>
  341. public void RenderOcclusionMesh(RasterCommandBuffer cmd, bool renderIntoTexture = false)
  342. {
  343. if (occlusionMeshScale > 0)
  344. m_OcclusionMesh.RenderOcclusionMesh(cmd.m_WrappedCommandBuffer, occlusionMeshScale, renderIntoTexture);
  345. }
  346. /// <summary>
  347. /// Draw debug line for all XR views.
  348. /// </summary>
  349. public void RenderDebugXRViewsFrustum()
  350. {
  351. for(int i = 0; i < m_Views.Count; i++)
  352. {
  353. const float k_DebugVeiwsFrustumDepthZ = 10.0f;
  354. var view = m_Views[i];
  355. var corners = CoreUtils.CalculateViewSpaceCorners(view.projMatrix, k_DebugVeiwsFrustumDepthZ);
  356. // Get world space camera pos
  357. Vector3 worldSpaceCameraPos = -(view.viewMatrix).GetColumn(3);
  358. for(int j = 0; j < 4; j++)
  359. Debug.DrawLine(worldSpaceCameraPos, view.viewMatrix.MultiplyPoint(corners[j]), i == 0 ? Color.green : Color.red);
  360. }
  361. }
  362. /// <summary>
  363. /// Take a point that is center-relative (0.5, 0.5) and modify it to be placed relative to the view's center instead, respecting the asymmetric FOV (if it is used)
  364. /// </summary>
  365. /// <param name="center"> Center relative point for symmetric FOV. </param>
  366. /// <returns> View center relative points. First view center is stored in x,y components and second view center is stored in z,w components. </returns>
  367. public Vector4 ApplyXRViewCenterOffset(Vector2 center)
  368. {
  369. Vector4 result = Vector4.zero;
  370. float centerDeltaX = 0.5f - center.x;
  371. float centerDeltaY = 0.5f - center.y;
  372. result.x = m_Views[0].eyeCenterUV.x - centerDeltaX;
  373. result.y = m_Views[0].eyeCenterUV.y - centerDeltaY;
  374. if (singlePassEnabled)
  375. {
  376. // With single-pass XR, we need to add the data for the 2nd view
  377. result.z = m_Views[1].eyeCenterUV.x - centerDeltaX;
  378. result.w = m_Views[1].eyeCenterUV.y - centerDeltaY;
  379. }
  380. return result;
  381. }
  382. internal void AssignView(int viewId, XRView xrView)
  383. {
  384. if (viewId < 0 || viewId >= m_Views.Count)
  385. throw new ArgumentOutOfRangeException(nameof(viewId));
  386. m_Views[viewId] = xrView;
  387. }
  388. internal void AssignCullingParams(int cullingPassId, ScriptableCullingParameters cullingParams)
  389. {
  390. // Disable legacy stereo culling path
  391. cullingParams.cullingOptions &= ~CullingOptions.Stereo;
  392. this.cullingPassId = cullingPassId;
  393. this.cullingParams = cullingParams;
  394. }
  395. internal void UpdateCombinedOcclusionMesh()
  396. {
  397. m_OcclusionMesh.UpdateCombinedMesh();
  398. }
  399. /// <summary>
  400. /// Initialize the base class fields.
  401. /// </summary>
  402. /// <param name="createInfo"> A descriptor used to create and initialize the XRPass. </param>
  403. public void InitBase(XRPassCreateInfo createInfo)
  404. {
  405. m_Views.Clear();
  406. copyDepth = createInfo.copyDepth;
  407. multipassId = createInfo.multipassId;
  408. AssignCullingParams(createInfo.cullingPassId, createInfo.cullingParameters);
  409. renderTarget = new RenderTargetIdentifier(createInfo.renderTarget, 0, CubemapFace.Unknown, -1);
  410. renderTargetDesc = createInfo.renderTargetDesc;
  411. motionVectorRenderTarget = new RenderTargetIdentifier(createInfo.motionVectorRenderTarget, 0, CubemapFace.Unknown, -1);
  412. motionVectorRenderTargetDesc = createInfo.motionVectorRenderTargetDesc;
  413. hasMotionVectorPass = createInfo.hasMotionVectorPass;
  414. m_OcclusionMesh.SetMaterial(createInfo.occlusionMeshMaterial);
  415. occlusionMeshScale = createInfo.occlusionMeshScale;
  416. foveatedRenderingInfo = createInfo.foveatedRenderingInfo;
  417. }
  418. internal void AddView(XRView xrView)
  419. {
  420. if (m_Views.Count < TextureXR.slices)
  421. {
  422. m_Views.Add(xrView);
  423. }
  424. else
  425. {
  426. throw new NotImplementedException($"Invalid XR setup for single-pass, trying to add too many views! Max supported: {TextureXR.slices}");
  427. }
  428. }
  429. }
  430. }