No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RenderTargetHandle.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using UnityEngine.Scripting.APIUpdating;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. /// <summary>
  6. /// Class for render target handles in URP.
  7. /// Deprecated in favor of RTHandle.
  8. /// </summary>
  9. // RenderTargetHandle can be thought of as a kind of ShaderProperty string hash
  10. [Obsolete("Deprecated in favor of RTHandle", true)]
  11. public struct RenderTargetHandle
  12. {
  13. /// <summary>
  14. /// The ID of the handle for the handle.
  15. /// </summary>
  16. public int id { set; get; }
  17. /// <summary>
  18. /// The render target ID for the handle.
  19. /// </summary>
  20. private RenderTargetIdentifier rtid { set; get; }
  21. /// <summary>
  22. /// The render target handle for the Camera target.
  23. /// </summary>
  24. public static readonly RenderTargetHandle CameraTarget = new RenderTargetHandle { id = -1 };
  25. /// <summary>
  26. /// Constructor for a render target handle.
  27. /// </summary>
  28. /// <param name="renderTargetIdentifier">The render target ID for the new handle.</param>
  29. public RenderTargetHandle(RenderTargetIdentifier renderTargetIdentifier)
  30. {
  31. id = -2;
  32. rtid = renderTargetIdentifier;
  33. }
  34. /// <summary>
  35. /// Constructor for a render target handle.
  36. /// </summary>
  37. /// <param name="rtHandle">The rt handle for the new handle.</param>
  38. public RenderTargetHandle(RTHandle rtHandle)
  39. {
  40. if (rtHandle.nameID == BuiltinRenderTextureType.CameraTarget)
  41. id = -1;
  42. else if (rtHandle.name.Length == 0)
  43. id = -2;
  44. else
  45. id = Shader.PropertyToID(rtHandle.name);
  46. rtid = rtHandle.nameID;
  47. if (rtHandle.rt != null && id != rtid)
  48. id = -2;
  49. }
  50. internal static RenderTargetHandle GetCameraTarget(ref CameraData cameraData)
  51. {
  52. #if ENABLE_VR && ENABLE_XR_MODULE
  53. if (cameraData.xr.enabled)
  54. return new RenderTargetHandle(cameraData.xr.renderTarget);
  55. #endif
  56. return CameraTarget;
  57. }
  58. /// <summary>
  59. /// Initializes the ID for the handle.
  60. /// </summary>
  61. /// <param name="shaderProperty">The shader property to initialize with.</param>
  62. public void Init(string shaderProperty)
  63. {
  64. // Shader.PropertyToID returns what is internally referred to as a "ShaderLab::FastPropertyName".
  65. // It is a value coming from an internal global std::map<char*,int> that converts shader property strings into unique integer handles (that are faster to work with).
  66. id = Shader.PropertyToID(shaderProperty);
  67. }
  68. /// <summary>
  69. /// Initializes the render target ID for the handle.
  70. /// </summary>
  71. /// <param name="renderTargetIdentifier">The render target ID to initialize with.</param>
  72. public void Init(RenderTargetIdentifier renderTargetIdentifier)
  73. {
  74. id = -2;
  75. rtid = renderTargetIdentifier;
  76. }
  77. /// <summary>
  78. /// The render target ID for this render target handle.
  79. /// </summary>
  80. /// <returns>The render target ID for this render target handle.</returns>
  81. public RenderTargetIdentifier Identifier()
  82. {
  83. if (id == -1)
  84. {
  85. return BuiltinRenderTextureType.CameraTarget;
  86. }
  87. if (id == -2)
  88. {
  89. return rtid;
  90. }
  91. return new RenderTargetIdentifier(id, 0, CubemapFace.Unknown, -1);
  92. }
  93. /// <summary>
  94. /// Does this handle have internal render target ID?
  95. /// </summary>
  96. /// <returns>True if it has internal render target ID.</returns>
  97. public bool HasInternalRenderTargetId()
  98. {
  99. return id == -2;
  100. }
  101. /// <summary>
  102. /// Equality check with another render target handle.
  103. /// </summary>
  104. /// <param name="other">Other render target handle to compare with.</param>
  105. /// <returns>True if the handles have the same ID, otherwise false.</returns>
  106. public bool Equals(RenderTargetHandle other)
  107. {
  108. if (id == -2 || other.id == -2)
  109. return Identifier() == other.Identifier();
  110. return id == other.id;
  111. }
  112. /// <inheritdoc/>
  113. public override bool Equals(object obj)
  114. {
  115. if (ReferenceEquals(null, obj)) return false;
  116. return obj is RenderTargetHandle && Equals((RenderTargetHandle)obj);
  117. }
  118. /// <inheritdoc/>
  119. public override int GetHashCode()
  120. {
  121. return id;
  122. }
  123. /// <summary>
  124. /// Equality check between two render target handles.
  125. /// </summary>
  126. /// <param name="c1">First handle for the check.</param>
  127. /// <param name="c2">Second handle for the check.</param>
  128. /// <returns>True if the handles have the same ID, otherwise false.</returns>
  129. public static bool operator ==(RenderTargetHandle c1, RenderTargetHandle c2)
  130. {
  131. return c1.Equals(c2);
  132. }
  133. /// <summary>
  134. /// Equality check between two render target handles.
  135. /// </summary>
  136. /// <param name="c1">First handle for the check.</param>
  137. /// <param name="c2">Second handle for the check.</param>
  138. /// <returns>True if the handles do not have the same ID, otherwise false.</returns>
  139. public static bool operator !=(RenderTargetHandle c1, RenderTargetHandle c2)
  140. {
  141. return !c1.Equals(c2);
  142. }
  143. }
  144. }