RTHandle.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections.Generic;
  2. using UnityEngine.Rendering;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// A RTHandle is a RenderTexture that scales automatically with the camera size.
  7. /// This allows proper reutilization of RenderTexture memory when different cameras with various sizes are used during rendering.
  8. /// <seealso cref="RTHandleSystem"/>
  9. /// </summary>
  10. public class RTHandle
  11. {
  12. internal RTHandleSystem m_Owner;
  13. internal RenderTexture m_RT;
  14. internal Texture m_ExternalTexture;
  15. internal RenderTargetIdentifier m_NameID;
  16. internal bool m_EnableMSAA = false;
  17. internal bool m_EnableRandomWrite = false;
  18. internal bool m_EnableHWDynamicScale = false;
  19. internal string m_Name;
  20. /// <summary>
  21. /// Scale factor applied to the RTHandle reference size.
  22. /// </summary>
  23. public Vector2 scaleFactor { get; internal set; }
  24. internal ScaleFunc scaleFunc;
  25. /// <summary>
  26. /// Returns true if the RTHandle uses automatic scaling.
  27. /// </summary>
  28. public bool useScaling { get; internal set; }
  29. /// <summary>
  30. /// Reference size of the RTHandle System associated with the RTHandle
  31. /// </summary>
  32. public Vector2Int referenceSize {get; internal set; }
  33. /// <summary>
  34. /// Current properties of the RTHandle System
  35. /// </summary>
  36. public RTHandleProperties rtHandleProperties { get { return m_Owner.rtHandleProperties; } }
  37. /// <summary>
  38. /// RenderTexture associated with the RTHandle
  39. /// </summary>
  40. public RenderTexture rt { get { return m_RT; } }
  41. /// <summary>
  42. /// RenderTargetIdentifier associated with the RTHandle
  43. /// </summary>
  44. public RenderTargetIdentifier nameID { get { return m_NameID; } }
  45. /// <summary>
  46. /// Name of the RTHandle
  47. /// </summary>
  48. public string name { get { return m_Name; } }
  49. /// <summary>
  50. /// Returns true is MSAA is enabled, false otherwise.
  51. /// </summary>
  52. public bool isMSAAEnabled { get { return m_EnableMSAA; } }
  53. // Keep constructor private
  54. internal RTHandle(RTHandleSystem owner)
  55. {
  56. m_Owner = owner;
  57. }
  58. /// <summary>
  59. /// Implicit conversion operator to RenderTexture
  60. /// </summary>
  61. /// <param name="handle">Input RTHandle</param>
  62. /// <returns>RenderTexture representation of the RTHandle.</returns>
  63. public static implicit operator RenderTexture(RTHandle handle)
  64. {
  65. // If RTHandle is null then conversion should give a null RenderTexture
  66. if (handle == null)
  67. return null;
  68. Debug.Assert(handle.rt != null, "RTHandle was created using a regular Texture and is used as a RenderTexture");
  69. return handle.rt;
  70. }
  71. /// <summary>
  72. /// Implicit conversion operator to Texture
  73. /// </summary>
  74. /// <param name="handle">Input RTHandle</param>
  75. /// <returns>Texture representation of the RTHandle.</returns>
  76. public static implicit operator Texture(RTHandle handle)
  77. {
  78. // If RTHandle is null then conversion should give a null Texture
  79. if (handle == null)
  80. return null;
  81. Debug.Assert(handle.m_ExternalTexture != null || handle.rt != null);
  82. return (handle.rt != null) ? handle.rt : handle.m_ExternalTexture;
  83. }
  84. /// <summary>
  85. /// Implicit conversion operator to RenderTargetIdentifier
  86. /// </summary>
  87. /// <param name="handle">Input RTHandle</param>
  88. /// <returns>RenderTargetIdentifier representation of the RTHandle.</returns>
  89. public static implicit operator RenderTargetIdentifier(RTHandle handle)
  90. {
  91. return handle != null ? handle.nameID : default(RenderTargetIdentifier);
  92. }
  93. internal void SetRenderTexture(RenderTexture rt)
  94. {
  95. m_RT= rt;
  96. m_ExternalTexture = null;
  97. m_NameID = new RenderTargetIdentifier(rt);
  98. }
  99. internal void SetTexture(Texture tex)
  100. {
  101. m_RT = null;
  102. m_ExternalTexture = tex;
  103. m_NameID = new RenderTargetIdentifier(tex);
  104. }
  105. internal void SetTexture(RenderTargetIdentifier tex)
  106. {
  107. m_RT = null;
  108. m_ExternalTexture = null;
  109. m_NameID = tex;
  110. }
  111. /// <summary>
  112. /// Release the RTHandle
  113. /// </summary>
  114. public void Release()
  115. {
  116. m_Owner.Remove(this);
  117. CoreUtils.Destroy(m_RT);
  118. m_NameID = BuiltinRenderTextureType.None;
  119. m_RT = null;
  120. m_ExternalTexture = null;
  121. }
  122. /// <summary>
  123. /// Return the input size, scaled by the RTHandle scale factor.
  124. /// </summary>
  125. /// <param name="refSize">Input size</param>
  126. /// <returns>Input size scaled by the RTHandle scale factor.</returns>
  127. public Vector2Int GetScaledSize(Vector2Int refSize)
  128. {
  129. if (!useScaling)
  130. return refSize;
  131. if (scaleFunc != null)
  132. {
  133. return scaleFunc(refSize);
  134. }
  135. else
  136. {
  137. return new Vector2Int(
  138. x: Mathf.RoundToInt(scaleFactor.x * refSize.x),
  139. y: Mathf.RoundToInt(scaleFactor.y * refSize.y)
  140. );
  141. }
  142. }
  143. #if UNITY_2020_2_OR_NEWER
  144. /// <summary>
  145. /// Switch the render target to fast memory on platform that have it.
  146. /// </summary>
  147. /// <param name="cmd">Command buffer used for rendering.</param>
  148. /// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
  149. /// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
  150. /// <param name="copyContents">Whether the content of render target are copied or not when switching to fast memory.</param>
  151. public void SwitchToFastMemory(CommandBuffer cmd,
  152. float residencyFraction = 1.0f,
  153. FastMemoryFlags flags = FastMemoryFlags.SpillTop,
  154. bool copyContents = false
  155. )
  156. {
  157. residencyFraction = Mathf.Clamp01(residencyFraction);
  158. cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents);
  159. }
  160. /// <summary>
  161. /// Switch the render target to fast memory on platform that have it and copies the content.
  162. /// </summary>
  163. /// <param name="cmd">Command buffer used for rendering.</param>
  164. /// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
  165. /// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
  166. public void CopyToFastMemory(CommandBuffer cmd,
  167. float residencyFraction = 1.0f,
  168. FastMemoryFlags flags = FastMemoryFlags.SpillTop
  169. )
  170. {
  171. SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true);
  172. }
  173. /// <summary>
  174. /// Switch out the render target from fast memory back to main memory on platforms that have fast memory.
  175. /// </summary>
  176. /// <param name="cmd">Command buffer used for rendering.</param>
  177. /// <param name="copyContents">Whether the content of render target are copied or not when switching out fast memory.</param>
  178. public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true)
  179. {
  180. cmd.SwitchOutOfFastMemory(m_RT, copyContents);
  181. }
  182. #endif
  183. }
  184. }