Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Rendering;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. namespace UnityEngine.Rendering
  8. {
  9. /// <summary>
  10. /// This struct contains some static helpers that can be used when converting RTid to RThandle
  11. /// The common use case is to convert rtId to rtHandle and use the handle with other handle compatible core APIs
  12. /// </summary>
  13. public struct RTHandleStaticHelpers
  14. {
  15. /// <summary>
  16. /// Static RTHandle wrapper around RenderTargetIdentifier to avoid gc.alloc
  17. /// Set this wrapper through `RTHandleStaticHelpers.SetRTHandleStaticWrapper`
  18. /// </summary>
  19. public static RTHandle s_RTHandleWrapper;
  20. /// <summary>
  21. /// Set static RTHandle wrapper given a RTid. The static RTHandle wrapper is treated as external handle in RTHandleSystem
  22. /// Get the static wrapper through `RTHandleStaticHelpers.s_RTHandleWrapper`.
  23. /// </summary>
  24. /// <param name="rtId">Input render target identifier to be converted.</param>
  25. public static void SetRTHandleStaticWrapper(RenderTargetIdentifier rtId)
  26. {
  27. if (s_RTHandleWrapper == null)
  28. s_RTHandleWrapper = RTHandles.Alloc(rtId);
  29. else
  30. s_RTHandleWrapper.SetTexture(rtId);
  31. }
  32. /// <summary>
  33. /// Set user managed RTHandle wrapper given a RTid. The wrapper is treated as external handle in RTHandleSystem
  34. /// </summary>
  35. /// <param name="rtWrapper">User managed RTHandle wrapper.</param>
  36. /// <param name="rtId">Input render target identifier to be set.</param>
  37. public static void SetRTHandleUserManagedWrapper(ref RTHandle rtWrapper, RenderTargetIdentifier rtId)
  38. {
  39. // User managed wrapper is null, just return here.
  40. if (rtWrapper == null)
  41. return;
  42. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  43. // Check user managed RTHandle wrapper is actually a wrapper around RTid
  44. if (rtWrapper.m_RT != null)
  45. throw new ArgumentException($"Input wrapper must be a wrapper around RenderTargetIdentifier. Passed in wrapper contains valid RenderTexture {rtWrapper.m_RT.name} and cannot be used as wrapper.");
  46. if (rtWrapper.m_ExternalTexture != null)
  47. throw new ArgumentException($"Input wrapper must be a wrapper around RenderTargetIdentifier. Passed in wrapper contains valid Texture {rtWrapper.m_ExternalTexture.name} and cannot be used as wrapper.");
  48. #endif
  49. rtWrapper.SetTexture(rtId);
  50. }
  51. }
  52. /// <summary>
  53. /// A RTHandle is a RenderTexture that scales automatically with the camera size.
  54. /// This allows proper reutilization of RenderTexture memory when different cameras with various sizes are used during rendering.
  55. /// <see cref="RTHandleSystem"/>
  56. /// </summary>
  57. public class RTHandle
  58. {
  59. internal RTHandleSystem m_Owner;
  60. internal RenderTexture m_RT;
  61. internal Texture m_ExternalTexture;
  62. internal RenderTargetIdentifier m_NameID;
  63. internal bool m_EnableMSAA = false;
  64. internal bool m_EnableRandomWrite = false;
  65. internal bool m_EnableHWDynamicScale = false;
  66. internal bool m_RTHasOwnership = true;
  67. internal string m_Name;
  68. internal bool m_UseCustomHandleScales = false;
  69. internal RTHandleProperties m_CustomHandleProperties;
  70. /// <summary>
  71. /// By default, rtHandleProperties gets the global state of scalers against the global reference mode.
  72. /// This method lets the current RTHandle use a local custom RTHandleProperties. This function is being used
  73. /// by scalers such as TAAU and DLSS, which require to have a different resolution for color (independent of the RTHandleSystem).
  74. /// </summary>
  75. /// <param name="properties">Properties to set.</param>
  76. public void SetCustomHandleProperties(in RTHandleProperties properties)
  77. {
  78. m_UseCustomHandleScales = true;
  79. m_CustomHandleProperties = properties;
  80. }
  81. /// <summary>
  82. /// Method that clears any custom handle property being set.
  83. /// </summary>
  84. public void ClearCustomHandleProperties()
  85. {
  86. m_UseCustomHandleScales = false;
  87. }
  88. /// <summary>
  89. /// Scale factor applied to the RTHandle reference size.
  90. /// </summary>
  91. public Vector2 scaleFactor { get; internal set; }
  92. internal ScaleFunc scaleFunc;
  93. /// <summary>
  94. /// Returns true if the RTHandle uses automatic scaling.
  95. /// </summary>
  96. public bool useScaling { get; internal set; }
  97. /// <summary>
  98. /// Reference size of the RTHandle System associated with the RTHandle
  99. /// </summary>
  100. public Vector2Int referenceSize { get; internal set; }
  101. /// <summary>
  102. /// Current properties of the RTHandle System. If a custom property has been set through SetCustomHandleProperties method, it will be used that one instead.
  103. /// </summary>
  104. public RTHandleProperties rtHandleProperties { get { return m_UseCustomHandleScales ? m_CustomHandleProperties : m_Owner.rtHandleProperties; } }
  105. /// <summary>
  106. /// RenderTexture associated with the RTHandle
  107. /// </summary>
  108. public RenderTexture rt { get { return m_RT; } }
  109. /// <summary>
  110. /// Texture associated with the RTHandle when constructed from an external Texture or RenderTexture
  111. /// </summary>
  112. public Texture externalTexture { get { return m_ExternalTexture; } }
  113. /// <summary>
  114. /// RenderTargetIdentifier associated with the RTHandle
  115. /// </summary>
  116. public RenderTargetIdentifier nameID { get { return m_NameID; } }
  117. /// <summary>
  118. /// Name of the RTHandle
  119. /// </summary>
  120. public string name { get { return m_Name; } }
  121. /// <summary>
  122. /// Returns true is MSAA is enabled, false otherwise.
  123. /// </summary>
  124. public bool isMSAAEnabled { get { return m_EnableMSAA; } }
  125. // Keep constructor private
  126. internal RTHandle(RTHandleSystem owner)
  127. {
  128. m_Owner = owner;
  129. }
  130. /// <summary>
  131. /// Implicit conversion operator to RenderTargetIdentifier
  132. /// </summary>
  133. /// <param name="handle">Input RTHandle</param>
  134. /// <returns>RenderTargetIdentifier representation of the RTHandle.</returns>
  135. public static implicit operator RenderTargetIdentifier(RTHandle handle)
  136. {
  137. return handle != null ? handle.nameID : default(RenderTargetIdentifier);
  138. }
  139. /// <summary>
  140. /// Implicit conversion operator to Texture
  141. /// </summary>
  142. /// <param name="handle">Input RTHandle</param>
  143. /// <returns>Texture representation of the RTHandle.</returns>
  144. public static implicit operator Texture(RTHandle handle)
  145. {
  146. // If RTHandle is null then conversion should give a null Texture
  147. if (handle == null)
  148. return null;
  149. Debug.Assert(handle.m_ExternalTexture != null || handle.rt != null);
  150. return (handle.rt != null) ? handle.rt : handle.m_ExternalTexture;
  151. }
  152. /// <summary>
  153. /// Implicit conversion operator to RenderTexture
  154. /// </summary>
  155. /// <param name="handle">Input RTHandle</param>
  156. /// <returns>RenderTexture representation of the RTHandle.</returns>
  157. public static implicit operator RenderTexture(RTHandle handle)
  158. {
  159. // If RTHandle is null then conversion should give a null RenderTexture
  160. if (handle == null)
  161. return null;
  162. Debug.Assert(handle.rt != null, "RTHandle was created using a regular Texture and is used as a RenderTexture");
  163. return handle.rt;
  164. }
  165. internal void SetRenderTexture(RenderTexture rt, bool transferOwnership = true)
  166. {
  167. m_RT = rt;
  168. m_ExternalTexture = null;
  169. m_RTHasOwnership = transferOwnership;
  170. m_NameID = new RenderTargetIdentifier(rt);
  171. }
  172. internal void SetTexture(Texture tex)
  173. {
  174. m_RT = null;
  175. m_ExternalTexture = tex;
  176. m_NameID = new RenderTargetIdentifier(tex);
  177. }
  178. internal void SetTexture(RenderTargetIdentifier tex)
  179. {
  180. m_RT = null;
  181. m_ExternalTexture = null;
  182. m_NameID = tex;
  183. }
  184. /// <summary>
  185. /// Get the Instance ID of the RTHandle.
  186. /// </summary>
  187. /// <returns>The RTHandle Instance ID.</returns>
  188. public int GetInstanceID()
  189. {
  190. if (m_RT != null)
  191. return m_RT.GetInstanceID();
  192. else if (m_ExternalTexture != null)
  193. return m_ExternalTexture.GetInstanceID();
  194. else
  195. return m_NameID.GetHashCode(); // No instance ID so we return the hash code.
  196. }
  197. /// <summary>
  198. /// Release the RTHandle
  199. /// </summary>
  200. public void Release()
  201. {
  202. m_Owner.Remove(this);
  203. if (m_RTHasOwnership)
  204. CoreUtils.Destroy(m_RT);
  205. m_NameID = BuiltinRenderTextureType.None;
  206. m_RT = null;
  207. m_ExternalTexture = null;
  208. m_RTHasOwnership = true;
  209. }
  210. /// <summary>
  211. /// Return the input size, scaled by the RTHandle scale factor.
  212. /// </summary>
  213. /// <param name="refSize">Input size</param>
  214. /// <returns>Input size scaled by the RTHandle scale factor.</returns>
  215. public Vector2Int GetScaledSize(Vector2Int refSize)
  216. {
  217. if (!useScaling)
  218. return refSize;
  219. if (scaleFunc != null)
  220. {
  221. return scaleFunc(refSize);
  222. }
  223. else
  224. {
  225. return new Vector2Int(
  226. x: Mathf.RoundToInt(scaleFactor.x * refSize.x),
  227. y: Mathf.RoundToInt(scaleFactor.y * refSize.y)
  228. );
  229. }
  230. }
  231. /// <summary>
  232. /// Return the scaled size of the RTHandle.
  233. /// </summary>
  234. /// <returns>The scaled size of the RTHandle.</returns>
  235. public Vector2Int GetScaledSize()
  236. {
  237. if (!useScaling)
  238. return referenceSize;
  239. if (scaleFunc != null)
  240. {
  241. return scaleFunc(referenceSize);
  242. }
  243. else
  244. {
  245. return new Vector2Int(
  246. x: Mathf.RoundToInt(scaleFactor.x * referenceSize.x),
  247. y: Mathf.RoundToInt(scaleFactor.y * referenceSize.y)
  248. );
  249. }
  250. }
  251. #if UNITY_2020_2_OR_NEWER
  252. /// <summary>
  253. /// Switch the render target to fast memory on platform that have it.
  254. /// </summary>
  255. /// <param name="cmd">Command buffer used for rendering.</param>
  256. /// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
  257. /// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
  258. /// <param name="copyContents">Whether the content of render target are copied or not when switching to fast memory.</param>
  259. public void SwitchToFastMemory(CommandBuffer cmd,
  260. float residencyFraction = 1.0f,
  261. FastMemoryFlags flags = FastMemoryFlags.SpillTop,
  262. bool copyContents = false
  263. )
  264. {
  265. residencyFraction = Mathf.Clamp01(residencyFraction);
  266. cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents);
  267. }
  268. /// <summary>
  269. /// Switch the render target to fast memory on platform that have it and copies the content.
  270. /// </summary>
  271. /// <param name="cmd">Command buffer used for rendering.</param>
  272. /// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
  273. /// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
  274. public void CopyToFastMemory(CommandBuffer cmd,
  275. float residencyFraction = 1.0f,
  276. FastMemoryFlags flags = FastMemoryFlags.SpillTop
  277. )
  278. {
  279. SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true);
  280. }
  281. /// <summary>
  282. /// Switch out the render target from fast memory back to main memory on platforms that have fast memory.
  283. /// </summary>
  284. /// <param name="cmd">Command buffer used for rendering.</param>
  285. /// <param name="copyContents">Whether the content of render target are copied or not when switching out fast memory.</param>
  286. public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true)
  287. {
  288. cmd.SwitchOutOfFastMemory(m_RT, copyContents);
  289. }
  290. #endif
  291. }
  292. }