TextureXR.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using UnityEngine.Experimental.Rendering;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// Utility class providing default textures compatible in any XR setup.
  6. /// </summary>
  7. public static class TextureXR
  8. {
  9. // Property set by XRSystem
  10. private static int m_MaxViews = 1;
  11. /// <summary>
  12. /// Maximum number of views handled by the XR system.
  13. /// </summary>
  14. public static int maxViews
  15. {
  16. set
  17. {
  18. m_MaxViews = value;
  19. }
  20. }
  21. // Property accessed when allocating a render target
  22. /// <summary>
  23. /// Number of slices used by the XR system.
  24. /// </summary>
  25. public static int slices { get => m_MaxViews; }
  26. // Must be in sync with shader define in TextureXR.hlsl
  27. /// <summary>
  28. /// Returns true if the XR system uses texture arrays.
  29. /// </summary>
  30. public static bool useTexArray
  31. {
  32. get
  33. {
  34. switch (SystemInfo.graphicsDeviceType)
  35. {
  36. case GraphicsDeviceType.Direct3D11:
  37. case GraphicsDeviceType.Direct3D12:
  38. return SystemInfo.graphicsDeviceType != GraphicsDeviceType.XboxOne;
  39. case GraphicsDeviceType.PlayStation4:
  40. return true;
  41. case GraphicsDeviceType.Vulkan:
  42. return true;
  43. }
  44. return false;
  45. }
  46. }
  47. /// <summary>
  48. /// Dimension of XR textures.
  49. /// </summary>
  50. public static TextureDimension dimension
  51. {
  52. get
  53. {
  54. // TEXTURE2D_X macros will now expand to TEXTURE2D or TEXTURE2D_ARRAY
  55. return useTexArray ? TextureDimension.Tex2DArray : TextureDimension.Tex2D;
  56. }
  57. }
  58. // Need to keep both the Texture and the RTHandle in order to be able to track lifetime properly.
  59. static Texture m_BlackUIntTexture2DArray;
  60. static Texture m_BlackUIntTexture;
  61. static RTHandle m_BlackUIntTexture2DArrayRTH;
  62. static RTHandle m_BlackUIntTextureRTH;
  63. /// <summary>
  64. /// Default black unsigned integer texture.
  65. /// </summary>
  66. /// <returns>The default black unsigned integer texture.</returns>
  67. public static RTHandle GetBlackUIntTexture() { return useTexArray ? m_BlackUIntTexture2DArrayRTH : m_BlackUIntTextureRTH; }
  68. static Texture2DArray m_ClearTexture2DArray;
  69. static Texture2D m_ClearTexture;
  70. static RTHandle m_ClearTexture2DArrayRTH;
  71. static RTHandle m_ClearTextureRTH;
  72. /// <summary>
  73. /// Default clear color (0, 0, 0, 1) texture.
  74. /// </summary>
  75. /// <returns>The default clear color texture.</returns>
  76. public static RTHandle GetClearTexture() { return useTexArray ? m_ClearTexture2DArrayRTH : m_ClearTextureRTH; }
  77. static Texture2DArray m_MagentaTexture2DArray;
  78. static Texture2D m_MagentaTexture;
  79. static RTHandle m_MagentaTexture2DArrayRTH;
  80. static RTHandle m_MagentaTextureRTH;
  81. /// <summary>
  82. /// Default magenta texture.
  83. /// </summary>
  84. /// <returns>The default magenta texture.</returns>
  85. public static RTHandle GetMagentaTexture() { return useTexArray ? m_MagentaTexture2DArrayRTH : m_MagentaTextureRTH; }
  86. static Texture2DArray m_BlackTexture2DArray;
  87. static RTHandle m_BlackTexture2DArrayRTH;
  88. static RTHandle m_BlackTextureRTH;
  89. /// <summary>
  90. /// Default black texture.
  91. /// </summary>
  92. /// <returns>The default black texture.</returns>
  93. public static RTHandle GetBlackTexture() { return useTexArray ? m_BlackTexture2DArrayRTH : m_BlackTextureRTH; }
  94. /// <summary>
  95. /// Default black texture array.
  96. /// </summary>
  97. /// <returns>The default black texture array.</returns>
  98. public static RTHandle GetBlackTextureArray() { return m_BlackTexture2DArrayRTH; }
  99. static Texture2DArray m_WhiteTexture2DArray;
  100. static RTHandle m_WhiteTexture2DArrayRTH;
  101. static RTHandle m_WhiteTextureRTH;
  102. /// <summary>
  103. /// Default white texture.
  104. /// </summary>
  105. /// <returns>The default white texture.</returns>
  106. public static RTHandle GetWhiteTexture() { return useTexArray ? m_WhiteTexture2DArrayRTH : m_WhiteTextureRTH; }
  107. /// <summary>
  108. /// Initialize XR textures. Must be called at least once.
  109. /// </summary>
  110. /// <param name="cmd">Command Buffer used to initialize textures.</param>
  111. /// <param name="clearR32_UIntShader">Compute shader used to intitialize unsigned integer textures.</param>
  112. public static void Initialize(CommandBuffer cmd, ComputeShader clearR32_UIntShader)
  113. {
  114. if (m_BlackUIntTexture2DArray == null) // We assume that everything is invalid if one is invalid.
  115. {
  116. // Black UINT
  117. RTHandles.Release(m_BlackUIntTexture2DArrayRTH);
  118. m_BlackUIntTexture2DArray = CreateBlackUIntTextureArray(cmd, clearR32_UIntShader);
  119. m_BlackUIntTexture2DArrayRTH = RTHandles.Alloc(m_BlackUIntTexture2DArray);
  120. RTHandles.Release(m_BlackUIntTextureRTH);
  121. m_BlackUIntTexture = CreateBlackUintTexture(cmd, clearR32_UIntShader);
  122. m_BlackUIntTextureRTH = RTHandles.Alloc(m_BlackUIntTexture);
  123. // Clear
  124. RTHandles.Release(m_ClearTextureRTH);
  125. m_ClearTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Clear Texture" };
  126. m_ClearTexture.SetPixel(0, 0, Color.clear);
  127. m_ClearTexture.Apply();
  128. m_ClearTextureRTH = RTHandles.Alloc(m_ClearTexture);
  129. RTHandles.Release(m_ClearTexture2DArrayRTH);
  130. m_ClearTexture2DArray = CreateTexture2DArrayFromTexture2D(m_ClearTexture, "Clear Texture2DArray");
  131. m_ClearTexture2DArrayRTH = RTHandles.Alloc(m_ClearTexture2DArray);
  132. // Magenta
  133. RTHandles.Release(m_MagentaTextureRTH);
  134. m_MagentaTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Magenta Texture" };
  135. m_MagentaTexture.SetPixel(0, 0, Color.magenta);
  136. m_MagentaTexture.Apply();
  137. m_MagentaTextureRTH = RTHandles.Alloc(m_MagentaTexture);
  138. RTHandles.Release(m_MagentaTexture2DArrayRTH);
  139. m_MagentaTexture2DArray = CreateTexture2DArrayFromTexture2D(m_MagentaTexture, "Magenta Texture2DArray");
  140. m_MagentaTexture2DArrayRTH = RTHandles.Alloc(m_MagentaTexture2DArray);
  141. // Black
  142. RTHandles.Release(m_BlackTextureRTH);
  143. m_BlackTextureRTH = RTHandles.Alloc(Texture2D.blackTexture);
  144. RTHandles.Release(m_BlackTexture2DArrayRTH);
  145. m_BlackTexture2DArray = CreateTexture2DArrayFromTexture2D(Texture2D.blackTexture, "Black Texture2DArray");
  146. m_BlackTexture2DArrayRTH = RTHandles.Alloc(m_BlackTexture2DArray);
  147. // White
  148. RTHandles.Release(m_WhiteTextureRTH);
  149. m_WhiteTextureRTH = RTHandles.Alloc(Texture2D.whiteTexture);
  150. RTHandles.Release(m_WhiteTexture2DArrayRTH);
  151. m_WhiteTexture2DArray = CreateTexture2DArrayFromTexture2D(Texture2D.whiteTexture, "White Texture2DArray");
  152. m_WhiteTexture2DArrayRTH = RTHandles.Alloc(m_WhiteTexture2DArray);
  153. }
  154. }
  155. static Texture2DArray CreateTexture2DArrayFromTexture2D(Texture2D source, string name)
  156. {
  157. Texture2DArray texArray = new Texture2DArray(source.width, source.height, slices, source.format, false) { name = name };
  158. for (int i = 0; i < slices; ++i)
  159. Graphics.CopyTexture(source, 0, 0, texArray, i, 0);
  160. return texArray;
  161. }
  162. static Texture CreateBlackUIntTextureArray(CommandBuffer cmd, ComputeShader clearR32_UIntShader)
  163. {
  164. RenderTexture blackUIntTexture2DArray = new RenderTexture(1, 1, 0, GraphicsFormat.R32_UInt)
  165. {
  166. dimension = TextureDimension.Tex2DArray,
  167. volumeDepth = slices,
  168. useMipMap = false,
  169. autoGenerateMips = false,
  170. enableRandomWrite = true,
  171. name = "Black UInt Texture Array"
  172. };
  173. blackUIntTexture2DArray.Create();
  174. // Workaround because we currently can't create a Texture2DArray using an R32_UInt format
  175. // So we create a R32_UInt RenderTarget and clear it using a compute shader, because we can't
  176. // Clear this type of target on metal devices (output type nor compatible: float4 vs uint)
  177. int kernel = clearR32_UIntShader.FindKernel("ClearUIntTextureArray");
  178. cmd.SetComputeTextureParam(clearR32_UIntShader, kernel, "_TargetArray", blackUIntTexture2DArray);
  179. cmd.DispatchCompute(clearR32_UIntShader, kernel, 1, 1, slices);
  180. return blackUIntTexture2DArray as Texture;
  181. }
  182. static Texture CreateBlackUintTexture(CommandBuffer cmd, ComputeShader clearR32_UIntShader)
  183. {
  184. RenderTexture blackUIntTexture2D = new RenderTexture(1, 1, 0, GraphicsFormat.R32_UInt)
  185. {
  186. dimension = TextureDimension.Tex2D,
  187. volumeDepth = slices,
  188. useMipMap = false,
  189. autoGenerateMips = false,
  190. enableRandomWrite = true,
  191. name = "Black UInt Texture Array"
  192. };
  193. blackUIntTexture2D.Create();
  194. // Workaround because we currently can't create a Texture2DArray using an R32_UInt format
  195. // So we create a R32_UInt RenderTarget and clear it using a compute shader, because we can't
  196. // Clear this type of target on metal devices (output type nor compatible: float4 vs uint)
  197. int kernel = clearR32_UIntShader.FindKernel("ClearUIntTexture");
  198. cmd.SetComputeTextureParam(clearR32_UIntShader, kernel, "_Target", blackUIntTexture2D);
  199. cmd.DispatchCompute(clearR32_UIntShader, kernel, 1, 1, slices);
  200. return blackUIntTexture2D as Texture;
  201. }
  202. }
  203. }