RTHandles.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using UnityEngine.Experimental.Rendering;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// Default instance of a RTHandleSystem
  6. /// </summary>
  7. public static class RTHandles
  8. {
  9. static RTHandleSystem s_DefaultInstance = new RTHandleSystem();
  10. /// <summary>
  11. /// Maximum allocated width of the default RTHandle System
  12. /// </summary>
  13. public static int maxWidth { get { return s_DefaultInstance.GetMaxWidth(); } }
  14. /// <summary>
  15. /// Maximum allocated height of the default RTHandle System
  16. /// </summary>
  17. public static int maxHeight { get { return s_DefaultInstance.GetMaxHeight(); } }
  18. /// <summary>
  19. /// Current properties of the default RTHandle System
  20. /// </summary>
  21. public static RTHandleProperties rtHandleProperties { get { return s_DefaultInstance.rtHandleProperties; } }
  22. /// <summary>
  23. /// Allocate a new fixed sized RTHandle with the default RTHandle System.
  24. /// </summary>
  25. /// <param name="width">With of the RTHandle.</param>
  26. /// <param name="height">Heigh of the RTHandle.</param>
  27. /// <param name="slices">Number of slices of the RTHandle.</param>
  28. /// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
  29. /// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
  30. /// <param name="filterMode">Filtering mode of the RTHandle.</param>
  31. /// <param name="wrapMode">Addressing mode of the RTHandle.</param>
  32. /// <param name="dimension">Texture dimension of the RTHandle.</param>
  33. /// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
  34. /// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
  35. /// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
  36. /// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
  37. /// <param name="anisoLevel">Anisotropic filtering level.</param>
  38. /// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
  39. /// <param name="msaaSamples">Number of MSAA samples for the RTHandle.</param>
  40. /// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
  41. /// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
  42. /// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
  43. /// <param name="name">Name of the RTHandle.</param>
  44. /// <returns></returns>
  45. public static RTHandle Alloc(
  46. int width,
  47. int height,
  48. int slices = 1,
  49. DepthBits depthBufferBits = DepthBits.None,
  50. GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
  51. FilterMode filterMode = FilterMode.Point,
  52. TextureWrapMode wrapMode = TextureWrapMode.Repeat,
  53. TextureDimension dimension = TextureDimension.Tex2D,
  54. bool enableRandomWrite = false,
  55. bool useMipMap = false,
  56. bool autoGenerateMips = true,
  57. bool isShadowMap = false,
  58. int anisoLevel = 1,
  59. float mipMapBias = 0,
  60. MSAASamples msaaSamples = MSAASamples.None,
  61. bool bindTextureMS = false,
  62. bool useDynamicScale = false,
  63. RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
  64. string name = ""
  65. )
  66. {
  67. return s_DefaultInstance.Alloc(
  68. width,
  69. height,
  70. slices,
  71. depthBufferBits,
  72. colorFormat,
  73. filterMode,
  74. wrapMode,
  75. dimension,
  76. enableRandomWrite,
  77. useMipMap,
  78. autoGenerateMips,
  79. isShadowMap,
  80. anisoLevel,
  81. mipMapBias,
  82. msaaSamples,
  83. bindTextureMS,
  84. useDynamicScale,
  85. memoryless,
  86. name
  87. );
  88. }
  89. /// <summary>
  90. /// Allocate a new automatically sized RTHandle for the default RTHandle System.
  91. /// </summary>
  92. /// <param name="scaleFactor">Constant scale for the RTHandle size computation.</param>
  93. /// <param name="slices">Number of slices of the RTHandle.</param>
  94. /// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
  95. /// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
  96. /// <param name="filterMode">Filtering mode of the RTHandle.</param>
  97. /// <param name="wrapMode">Addressing mode of the RTHandle.</param>
  98. /// <param name="dimension">Texture dimension of the RTHandle.</param>
  99. /// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
  100. /// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
  101. /// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
  102. /// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
  103. /// <param name="anisoLevel">Anisotropic filtering level.</param>
  104. /// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
  105. /// <param name="enableMSAA">Enable MSAA for this RTHandle.</param>
  106. /// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
  107. /// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
  108. /// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
  109. /// <param name="name">Name of the RTHandle.</param>
  110. /// <returns></returns>
  111. public static RTHandle Alloc(
  112. Vector2 scaleFactor,
  113. int slices = 1,
  114. DepthBits depthBufferBits = DepthBits.None,
  115. GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
  116. FilterMode filterMode = FilterMode.Point,
  117. TextureWrapMode wrapMode = TextureWrapMode.Repeat,
  118. TextureDimension dimension = TextureDimension.Tex2D,
  119. bool enableRandomWrite = false,
  120. bool useMipMap = false,
  121. bool autoGenerateMips = true,
  122. bool isShadowMap = false,
  123. int anisoLevel = 1,
  124. float mipMapBias = 0,
  125. bool enableMSAA = false,
  126. bool bindTextureMS = false,
  127. bool useDynamicScale = false,
  128. RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
  129. string name = ""
  130. )
  131. {
  132. return s_DefaultInstance.Alloc(
  133. scaleFactor,
  134. slices,
  135. depthBufferBits,
  136. colorFormat,
  137. filterMode,
  138. wrapMode,
  139. dimension,
  140. enableRandomWrite,
  141. useMipMap,
  142. autoGenerateMips,
  143. isShadowMap,
  144. anisoLevel,
  145. mipMapBias,
  146. enableMSAA,
  147. bindTextureMS,
  148. useDynamicScale,
  149. memoryless,
  150. name
  151. );
  152. }
  153. /// <summary>
  154. /// Allocate a new automatically sized RTHandle for the default RTHandle System.
  155. /// </summary>
  156. /// <param name="scaleFunc">Function used for the RTHandle size computation.</param>
  157. /// <param name="slices">Number of slices of the RTHandle.</param>
  158. /// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
  159. /// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
  160. /// <param name="filterMode">Filtering mode of the RTHandle.</param>
  161. /// <param name="wrapMode">Addressing mode of the RTHandle.</param>
  162. /// <param name="dimension">Texture dimension of the RTHandle.</param>
  163. /// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
  164. /// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
  165. /// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
  166. /// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
  167. /// <param name="anisoLevel">Anisotropic filtering level.</param>
  168. /// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
  169. /// <param name="enableMSAA">Enable MSAA for this RTHandle.</param>
  170. /// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
  171. /// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
  172. /// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
  173. /// <param name="name">Name of the RTHandle.</param>
  174. /// <returns></returns>
  175. public static RTHandle Alloc(
  176. ScaleFunc scaleFunc,
  177. int slices = 1,
  178. DepthBits depthBufferBits = DepthBits.None,
  179. GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
  180. FilterMode filterMode = FilterMode.Point,
  181. TextureWrapMode wrapMode = TextureWrapMode.Repeat,
  182. TextureDimension dimension = TextureDimension.Tex2D,
  183. bool enableRandomWrite = false,
  184. bool useMipMap = false,
  185. bool autoGenerateMips = true,
  186. bool isShadowMap = false,
  187. int anisoLevel = 1,
  188. float mipMapBias = 0,
  189. bool enableMSAA = false,
  190. bool bindTextureMS = false,
  191. bool useDynamicScale = false,
  192. RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
  193. string name = ""
  194. )
  195. {
  196. return s_DefaultInstance.Alloc(
  197. scaleFunc,
  198. slices,
  199. depthBufferBits,
  200. colorFormat,
  201. filterMode,
  202. wrapMode,
  203. dimension,
  204. enableRandomWrite,
  205. useMipMap,
  206. autoGenerateMips,
  207. isShadowMap,
  208. anisoLevel,
  209. mipMapBias,
  210. enableMSAA,
  211. bindTextureMS,
  212. useDynamicScale,
  213. memoryless,
  214. name
  215. );
  216. }
  217. /// <summary>
  218. /// Allocate a RTHandle from a regular Texture for the default RTHandle system.
  219. /// </summary>
  220. /// <param name="tex">Input texture</param>
  221. /// <returns>A new RTHandle referencing the input texture.</returns>
  222. public static RTHandle Alloc(Texture tex)
  223. {
  224. return s_DefaultInstance.Alloc(tex);
  225. }
  226. /// <summary>
  227. /// Allocate a RTHandle from a regular RenderTexture for the default RTHandle system.
  228. /// </summary>
  229. /// <param name="tex">Input texture</param>
  230. /// <returns>A new RTHandle referencing the input texture.</returns>
  231. public static RTHandle Alloc(RenderTexture tex)
  232. {
  233. return s_DefaultInstance.Alloc(tex);
  234. }
  235. /// <summary>
  236. /// Allocate a RTHandle from a regular render target identifier for the default RTHandle system.
  237. /// </summary>
  238. /// <param name="tex">Input render target identifier.</param>
  239. /// <returns>A new RTHandle referencing the input render target identifier.</returns>
  240. public static RTHandle Alloc(RenderTargetIdentifier tex)
  241. {
  242. return s_DefaultInstance.Alloc(tex);
  243. }
  244. /// <summary>
  245. /// Allocate a RTHandle from a regular render target identifier for the default RTHandle system.
  246. /// </summary>
  247. /// <param name="tex">Input render target identifier.</param>
  248. /// <param name="name">Name of the render target.</param>
  249. /// <returns>A new RTHandle referencing the input render target identifier.</returns>
  250. public static RTHandle Alloc(RenderTargetIdentifier tex, string name)
  251. {
  252. return s_DefaultInstance.Alloc(tex, name);
  253. }
  254. private static RTHandle Alloc(RTHandle tex)
  255. {
  256. Debug.LogError("Allocation a RTHandle from another one is forbidden.");
  257. return null;
  258. }
  259. /// <summary>
  260. /// Initialize the default RTHandle system.
  261. /// </summary>
  262. /// <param name="width">Initial reference rendering width.</param>
  263. /// <param name="height">Initial reference rendering height.</param>
  264. /// <param name="scaledRTsupportsMSAA">Set to true if automatically scaled RTHandles should support MSAA</param>
  265. /// <param name="scaledRTMSAASamples">Number of MSAA samples for automatically scaled RTHandles.</param>
  266. public static void Initialize(
  267. int width,
  268. int height,
  269. bool scaledRTsupportsMSAA,
  270. MSAASamples scaledRTMSAASamples
  271. )
  272. {
  273. s_DefaultInstance.Initialize(
  274. width,
  275. height,
  276. scaledRTsupportsMSAA,
  277. scaledRTMSAASamples
  278. );
  279. }
  280. /// <summary>
  281. /// Release memory of a RTHandle from the default RTHandle System
  282. /// </summary>
  283. /// <param name="rth">RTHandle that should be released.</param>
  284. public static void Release(RTHandle rth)
  285. {
  286. s_DefaultInstance.Release(rth);
  287. }
  288. /// <summary>
  289. /// Enable or disable hardware dynamic resolution for the default RTHandle System
  290. /// </summary>
  291. /// <param name="hwDynamicResRequested">State of hardware dynamic resolution.</param>
  292. public static void SetHardwareDynamicResolutionState(bool hwDynamicResRequested)
  293. {
  294. s_DefaultInstance.SetHardwareDynamicResolutionState(hwDynamicResRequested);
  295. }
  296. /// <summary>
  297. /// Sets the reference rendering size for subsequent rendering for the default RTHandle System
  298. /// </summary>
  299. /// <param name="width">Reference rendering width for subsequent rendering.</param>
  300. /// <param name="height">Reference rendering height for subsequent rendering.</param>
  301. /// <param name="msaaSamples">Number of MSAA samples for multisampled textures for subsequent rendering.</param>
  302. public static void SetReferenceSize(
  303. int width,
  304. int height,
  305. MSAASamples msaaSamples
  306. )
  307. {
  308. s_DefaultInstance.SetReferenceSize(
  309. width,
  310. height,
  311. msaaSamples
  312. );
  313. }
  314. /// <summary>
  315. /// Reset the reference size of the system and reallocate all textures.
  316. /// </summary>
  317. /// <param name="width">New width.</param>
  318. /// <param name="height">New height.</param>
  319. public static void ResetReferenceSize(int width, int height)
  320. {
  321. s_DefaultInstance.ResetReferenceSize(width, height);
  322. }
  323. }
  324. }