No Description
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.

XRBuiltinShaderConstants.cs 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEngine.Rendering;
  2. namespace UnityEngine.Experimental.Rendering
  3. {
  4. /// <summary>
  5. /// Helper static class used by render pipelines to setup stereo constants accessed by builtin shaders.
  6. /// </summary>
  7. public static class XRBuiltinShaderConstants
  8. {
  9. /// <summary>
  10. /// Cached unique id for unity_StereoCameraProjection
  11. /// </summary>
  12. static public readonly int unity_StereoCameraProjection = Shader.PropertyToID("unity_StereoCameraProjection");
  13. /// <summary>
  14. /// Cached unique id for unity_StereoCameraInvProjection
  15. /// </summary>
  16. static public readonly int unity_StereoCameraInvProjection = Shader.PropertyToID("unity_StereoCameraInvProjection");
  17. /// <summary>
  18. /// Cached unique id for unity_StereoMatrixV
  19. /// </summary>
  20. static public readonly int unity_StereoMatrixV = Shader.PropertyToID("unity_StereoMatrixV");
  21. /// <summary>
  22. /// Cached unique id for unity_StereoMatrixInvV
  23. /// </summary>
  24. static public readonly int unity_StereoMatrixInvV = Shader.PropertyToID("unity_StereoMatrixInvV");
  25. /// <summary>
  26. /// Cached unique id for unity_StereoMatrixP
  27. /// </summary>
  28. static public readonly int unity_StereoMatrixP = Shader.PropertyToID("unity_StereoMatrixP");
  29. /// <summary>
  30. /// Cached unique id for unity_StereoMatrixInvP
  31. /// </summary>
  32. static public readonly int unity_StereoMatrixInvP = Shader.PropertyToID("unity_StereoMatrixInvP");
  33. /// <summary>
  34. /// Cached unique id for unity_StereoMatrixVP
  35. /// </summary>
  36. static public readonly int unity_StereoMatrixVP = Shader.PropertyToID("unity_StereoMatrixVP");
  37. /// <summary>
  38. /// Cached unique id for unity_StereoMatrixInvVP
  39. /// </summary>
  40. static public readonly int unity_StereoMatrixInvVP = Shader.PropertyToID("unity_StereoMatrixInvVP");
  41. /// <summary>
  42. /// Cached unique id for unity_StereoWorldSpaceCameraPos
  43. /// </summary>
  44. static public readonly int unity_StereoWorldSpaceCameraPos = Shader.PropertyToID("unity_StereoWorldSpaceCameraPos");
  45. // Pre-allocate arrays to avoid GC
  46. static Matrix4x4[] s_cameraProjMatrix = new Matrix4x4[2];
  47. static Matrix4x4[] s_invCameraProjMatrix = new Matrix4x4[2];
  48. static Matrix4x4[] s_viewMatrix = new Matrix4x4[2];
  49. static Matrix4x4[] s_invViewMatrix = new Matrix4x4[2];
  50. static Matrix4x4[] s_projMatrix = new Matrix4x4[2];
  51. static Matrix4x4[] s_invProjMatrix = new Matrix4x4[2];
  52. static Matrix4x4[] s_viewProjMatrix = new Matrix4x4[2];
  53. static Matrix4x4[] s_invViewProjMatrix = new Matrix4x4[2];
  54. static Vector4[] s_worldSpaceCameraPos = new Vector4[2];
  55. /// <summary>
  56. /// Update the shader constant data used by the C++ builtin renderer.
  57. /// </summary>
  58. /// <param name="viewMatrix"> The new view matrix that XR shaders constant should update to use. </param>
  59. /// <param name="projMatrix"> The new projection matrix that XR shaders constant should update to use. </param>
  60. /// <param name="renderIntoTexture"> Determines the yflip state for the projection matrix. </param>
  61. /// <param name="viewIndex"> Index of the XR shader constant to update. </param>
  62. public static void UpdateBuiltinShaderConstants(Matrix4x4 viewMatrix, Matrix4x4 projMatrix, bool renderIntoTexture, int viewIndex)
  63. {
  64. #if ENABLE_VR && ENABLE_XR_MODULE
  65. var gpuProjMatrix = GL.GetGPUProjectionMatrix(projMatrix, renderIntoTexture);
  66. var gpuViewProjMatrix = gpuProjMatrix * viewMatrix;
  67. s_cameraProjMatrix[viewIndex] = projMatrix;
  68. s_projMatrix[viewIndex] = gpuProjMatrix;
  69. s_viewMatrix[viewIndex] = viewMatrix;
  70. Matrix4x4.Inverse3DAffine(viewMatrix, ref s_invViewMatrix[viewIndex]);
  71. s_viewProjMatrix[viewIndex] = gpuViewProjMatrix;
  72. s_invCameraProjMatrix[viewIndex] = Matrix4x4.Inverse(projMatrix);
  73. s_invProjMatrix[viewIndex] = Matrix4x4.Inverse(gpuProjMatrix);
  74. s_invViewProjMatrix[viewIndex] = Matrix4x4.Inverse(gpuViewProjMatrix);
  75. s_worldSpaceCameraPos[viewIndex] = s_invViewMatrix[viewIndex].GetColumn(3);
  76. #endif
  77. }
  78. /// <summary>
  79. /// Bind the shader constants used by the C++ builtin renderer via a command buffer. `UpdateBuiltinShaderConstants` should be called before to update the constants.
  80. /// This is required to maintain compatibility with legacy code and shaders.
  81. /// </summary>
  82. /// <param name="cmd"> Commandbuffer on which to set XR shader constants. </param>
  83. public static void SetBuiltinShaderConstants(CommandBuffer cmd)
  84. {
  85. #if ENABLE_VR && ENABLE_XR_MODULE
  86. cmd.SetGlobalMatrixArray(unity_StereoCameraProjection, s_cameraProjMatrix);
  87. cmd.SetGlobalMatrixArray(unity_StereoCameraInvProjection, s_invCameraProjMatrix);
  88. cmd.SetGlobalMatrixArray(unity_StereoMatrixV, s_viewMatrix);
  89. cmd.SetGlobalMatrixArray(unity_StereoMatrixInvV, s_invViewMatrix);
  90. cmd.SetGlobalMatrixArray(unity_StereoMatrixP, s_projMatrix);
  91. cmd.SetGlobalMatrixArray(unity_StereoMatrixInvP, s_invProjMatrix);
  92. cmd.SetGlobalMatrixArray(unity_StereoMatrixVP, s_viewProjMatrix);
  93. cmd.SetGlobalMatrixArray(unity_StereoMatrixInvVP, s_invViewProjMatrix);
  94. cmd.SetGlobalVectorArray(unity_StereoWorldSpaceCameraPos, s_worldSpaceCameraPos);
  95. #endif
  96. }
  97. /// <summary>
  98. /// Bind the shader constants used by the C++ builtin renderer via a raster command buffer. `UpdateBuiltinShaderConstants` should be called before to update the constants.
  99. /// This is required to maintain compatibility with legacy code and shaders.
  100. /// </summary>
  101. /// <param name="cmd"> RasterCommandbuffer on which to set XR shader constants. </param>
  102. public static void SetBuiltinShaderConstants(RasterCommandBuffer cmd)
  103. {
  104. SetBuiltinShaderConstants(cmd.m_WrappedCommandBuffer);
  105. }
  106. /// <summary>
  107. /// Update and bind shader constants used by the C++ builtin renderer given the XRPass. For better control of setting up builtin shader constants, see `UpdateBuiltinShaderConstants`
  108. /// and `SetBuiltinShaderConstants` which do the same logic but could take in custom projection and view matricies instead.
  109. /// This is required to maintain compatibility with legacy code and shaders.
  110. /// </summary>
  111. /// <param name="xrPass"> The new XRPass that XR shader constants should update to use. </param>
  112. /// <param name="cmd"> CommandBuffer on which to set XR shader constants. </param>
  113. /// <param name="renderIntoTexture"> Determines the yflip state for the projection matrix. </param>
  114. public static void Update(XRPass xrPass, CommandBuffer cmd, bool renderIntoTexture)
  115. {
  116. #if ENABLE_VR && ENABLE_XR_MODULE
  117. if (xrPass.enabled)
  118. {
  119. cmd.SetViewProjectionMatrices(xrPass.GetViewMatrix(), xrPass.GetProjMatrix());
  120. if (xrPass.singlePassEnabled)
  121. {
  122. for (int viewIndex = 0; viewIndex < 2; ++viewIndex)
  123. {
  124. s_cameraProjMatrix[viewIndex] = xrPass.GetProjMatrix(viewIndex);
  125. s_viewMatrix[viewIndex] = xrPass.GetViewMatrix(viewIndex);
  126. s_projMatrix[viewIndex] = GL.GetGPUProjectionMatrix(s_cameraProjMatrix[viewIndex], renderIntoTexture);
  127. s_viewProjMatrix[viewIndex] = s_projMatrix[viewIndex] * s_viewMatrix[viewIndex];
  128. s_invCameraProjMatrix[viewIndex] = Matrix4x4.Inverse(s_cameraProjMatrix[viewIndex]);
  129. Matrix4x4.Inverse3DAffine(s_viewMatrix[viewIndex], ref s_invViewMatrix[viewIndex]);
  130. s_invProjMatrix[viewIndex] = Matrix4x4.Inverse(s_projMatrix[viewIndex]);
  131. s_invViewProjMatrix[viewIndex] = Matrix4x4.Inverse(s_viewProjMatrix[viewIndex]);
  132. s_worldSpaceCameraPos[viewIndex] = s_invViewMatrix[viewIndex].GetColumn(3);
  133. }
  134. cmd.SetGlobalMatrixArray(unity_StereoCameraProjection, s_cameraProjMatrix);
  135. cmd.SetGlobalMatrixArray(unity_StereoCameraInvProjection, s_invCameraProjMatrix);
  136. cmd.SetGlobalMatrixArray(unity_StereoMatrixV, s_viewMatrix);
  137. cmd.SetGlobalMatrixArray(unity_StereoMatrixInvV, s_invViewMatrix);
  138. cmd.SetGlobalMatrixArray(unity_StereoMatrixP, s_projMatrix);
  139. cmd.SetGlobalMatrixArray(unity_StereoMatrixInvP, s_invProjMatrix);
  140. cmd.SetGlobalMatrixArray(unity_StereoMatrixVP, s_viewProjMatrix);
  141. cmd.SetGlobalMatrixArray(unity_StereoMatrixInvVP, s_invViewProjMatrix);
  142. cmd.SetGlobalVectorArray(unity_StereoWorldSpaceCameraPos, s_worldSpaceCameraPos);
  143. }
  144. }
  145. #endif
  146. }
  147. }
  148. }