RTHandle.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Keep constructor private
  50. internal RTHandle(RTHandleSystem owner)
  51. {
  52. m_Owner = owner;
  53. }
  54. /// <summary>
  55. /// Implicit conversion operator to RenderTexture
  56. /// </summary>
  57. /// <param name="handle">Input RTHandle</param>
  58. /// <returns>RenderTexture representation of the RTHandle.</returns>
  59. public static implicit operator RenderTexture(RTHandle handle)
  60. {
  61. Debug.Assert(handle.rt != null, "RTHandle was created using a regular Texture and is used as a RenderTexture");
  62. return handle.rt;
  63. }
  64. /// <summary>
  65. /// Implicit conversion operator to Texture
  66. /// </summary>
  67. /// <param name="handle">Input RTHandle</param>
  68. /// <returns>Texture representation of the RTHandle.</returns>
  69. public static implicit operator Texture(RTHandle handle)
  70. {
  71. Debug.Assert(handle.m_ExternalTexture != null || handle.rt != null);
  72. return (handle.rt != null) ? handle.rt : handle.m_ExternalTexture;
  73. }
  74. /// <summary>
  75. /// Implicit conversion operator to RenderTargetIdentifier
  76. /// </summary>
  77. /// <param name="handle">Input RTHandle</param>
  78. /// <returns>RenderTargetIdentifier representation of the RTHandle.</returns>
  79. public static implicit operator RenderTargetIdentifier(RTHandle handle)
  80. {
  81. return handle.nameID;
  82. }
  83. internal void SetRenderTexture(RenderTexture rt)
  84. {
  85. m_RT= rt;
  86. m_ExternalTexture = null;
  87. m_NameID = new RenderTargetIdentifier(rt);
  88. }
  89. internal void SetTexture(Texture tex)
  90. {
  91. m_RT = null;
  92. m_ExternalTexture = tex;
  93. m_NameID = new RenderTargetIdentifier(tex);
  94. }
  95. /// <summary>
  96. /// Release the RTHandle
  97. /// </summary>
  98. public void Release()
  99. {
  100. m_Owner.Remove(this);
  101. CoreUtils.Destroy(m_RT);
  102. m_NameID = BuiltinRenderTextureType.None;
  103. m_RT = null;
  104. m_ExternalTexture = null;
  105. }
  106. /// <summary>
  107. /// Return the input size, scaled by the RTHandle scale factor.
  108. /// </summary>
  109. /// <param name="refSize">Input size</param>
  110. /// <returns>Input size scaled by the RTHandle scale factor.</returns>
  111. public Vector2Int GetScaledSize(Vector2Int refSize)
  112. {
  113. if (scaleFunc != null)
  114. {
  115. return scaleFunc(refSize);
  116. }
  117. else
  118. {
  119. return new Vector2Int(
  120. x: Mathf.RoundToInt(scaleFactor.x * refSize.x),
  121. y: Mathf.RoundToInt(scaleFactor.y * refSize.y)
  122. );
  123. }
  124. }
  125. }
  126. }