暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TaaHistory.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine.Experimental.Rendering;
  2. namespace UnityEngine.Rendering.Universal
  3. {
  4. /// <summary>
  5. /// Temporal Anti-aliasing (TAA) persistent data for Universal Rendering Pipeline.
  6. /// Holds the TAA color history accumulation texture.
  7. /// </summary>
  8. public sealed class TaaHistory : CameraHistoryItem
  9. {
  10. private int[] m_TaaAccumulationTextureIds = new int[2];
  11. private int[] m_TaaAccumulationVersions = new int[2];
  12. private static readonly string[] m_TaaAccumulationNames = new[]
  13. {
  14. "TaaAccumulationTex0",
  15. "TaaAccumulationTex1"
  16. };
  17. private RenderTextureDescriptor m_Descriptor;
  18. private Hash128 m_DescKey;
  19. /// <summary>
  20. /// Called internally on instance creation.
  21. /// Sets up RTHandle ids.
  22. /// </summary>
  23. /// <param name="owner">BufferedRTHandleSystem of the owning camera.</param>
  24. /// <param name="typeId">Unique id given to TaaHistory by the owning camera.</param>
  25. public override void OnCreate(BufferedRTHandleSystem owner, uint typeId)
  26. {
  27. base.OnCreate(owner, typeId);
  28. m_TaaAccumulationTextureIds[0] = MakeId(0);
  29. m_TaaAccumulationTextureIds[1] = MakeId(1);
  30. }
  31. /// <summary>
  32. /// Release TAA accumulation textures.
  33. /// </summary>
  34. public override void Reset()
  35. {
  36. for (int i = 0; i < m_TaaAccumulationTextureIds.Length; i++)
  37. {
  38. ReleaseHistoryFrameRT(m_TaaAccumulationTextureIds[i]);
  39. m_TaaAccumulationVersions[i] = -1;
  40. }
  41. m_Descriptor.width = 0;
  42. m_Descriptor.height = 0;
  43. m_Descriptor.graphicsFormat = GraphicsFormat.None;
  44. m_DescKey = Hash128.Compute(0);
  45. }
  46. /// <summary>
  47. /// Get TAA accumulation texture.
  48. /// </summary>
  49. /// <param name="eyeIndex">Eye index for XR multi-pass.</param>
  50. /// <returns>Current frame RTHandle for TAA accumulation texture.</returns>
  51. public RTHandle GetAccumulationTexture(int eyeIndex = 0)
  52. {
  53. return GetCurrentFrameRT(m_TaaAccumulationTextureIds[eyeIndex]);
  54. }
  55. /// <summary>
  56. /// Get TAA accumulation texture version.
  57. /// Tracks which frame the accumulation was last updated.
  58. /// </summary>
  59. /// <param name="eyeIndex">Eye index for XR multi-pass.</param>
  60. /// <returns>Accumulation texture version.</returns>
  61. public int GetAccumulationVersion(int eyeIndex = 0)
  62. {
  63. return m_TaaAccumulationVersions[eyeIndex];
  64. }
  65. internal void SetAccumulationVersion(int eyeIndex, int version)
  66. {
  67. m_TaaAccumulationVersions[eyeIndex] = version;
  68. }
  69. // Check if the TAA accumulation texture is valid.
  70. private bool IsValid()
  71. {
  72. return GetAccumulationTexture(0) != null;
  73. }
  74. // True if the desc changed, graphicsFormat etc.
  75. private bool IsDirty(ref RenderTextureDescriptor desc)
  76. {
  77. return m_DescKey != Hash128.Compute(ref desc);
  78. }
  79. private void Alloc(ref RenderTextureDescriptor desc, bool xrMultipassEnabled)
  80. {
  81. AllocHistoryFrameRT(m_TaaAccumulationTextureIds[0], 1, ref desc, m_TaaAccumulationNames[0]);
  82. if (xrMultipassEnabled)
  83. AllocHistoryFrameRT(m_TaaAccumulationTextureIds[1], 1, ref desc, m_TaaAccumulationNames[1]);
  84. m_Descriptor = desc;
  85. m_DescKey = Hash128.Compute(ref desc);
  86. }
  87. // Return true if the RTHandles were reallocated.
  88. internal bool Update(ref RenderTextureDescriptor cameraDesc, bool xrMultipassEnabled = false)
  89. {
  90. if (cameraDesc.width > 0 && cameraDesc.height > 0 && cameraDesc.graphicsFormat != GraphicsFormat.None)
  91. {
  92. var taaDesc = TemporalAA.TemporalAADescFromCameraDesc(ref cameraDesc);
  93. if (IsDirty(ref taaDesc))
  94. Reset();
  95. if (!IsValid())
  96. {
  97. Alloc(ref taaDesc, xrMultipassEnabled);
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. }
  104. }