暫無描述
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.

RawDepthHistory.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using UnityEngine.Experimental.Rendering;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. /// <summary>
  6. /// Raw render depth history. The depth snapshot is taken before post processing.
  7. /// A matching color is RawColorHistory.
  8. /// Format is the camera depth format or R32Float on platforms with limitations.
  9. /// If TemporalAA is enabled the depth is jittered.
  10. /// No mips. No depth pyramid.
  11. /// MSAA is not supported and is resolved for the history.
  12. /// XR is supported.
  13. /// </summary>
  14. public sealed class RawDepthHistory : CameraHistoryItem
  15. {
  16. private int[] m_Ids = new int[2];
  17. private static readonly string[] m_Names = new[]
  18. {
  19. "RawDepthHistory0",
  20. "RawDepthHistory1"
  21. };
  22. private RenderTextureDescriptor m_Descriptor;
  23. private Hash128 m_DescKey;
  24. /// <inheritdoc />
  25. public override void OnCreate(BufferedRTHandleSystem owner, uint typeId)
  26. {
  27. base.OnCreate(owner, typeId);
  28. m_Ids[0] = MakeId(0);
  29. m_Ids[1] = MakeId(1);
  30. }
  31. /// <summary>
  32. /// Get the current history texture.
  33. /// Current history might not be valid yet. It is valid only after executing the producing render pass.
  34. /// </summary>
  35. /// <param name="eyeIndex">Eye index, typically XRPass.multipassId.</param>
  36. /// <returns>The texture.</returns>
  37. public RTHandle GetCurrentTexture(int eyeIndex = 0)
  38. {
  39. if ((uint)eyeIndex >= m_Ids.Length)
  40. return null;
  41. return GetCurrentFrameRT(m_Ids[eyeIndex]);
  42. }
  43. /// <summary>
  44. /// Get the previous history texture.
  45. /// </summary>
  46. /// <param name="eyeIndex">Eye index, typically XRPass.multipassId.</param>
  47. /// <returns>The texture.</returns>
  48. public RTHandle GetPreviousTexture(int eyeIndex = 0)
  49. {
  50. if ((uint)eyeIndex >= m_Ids.Length)
  51. return null;
  52. return GetPreviousFrameRT(m_Ids[eyeIndex]);
  53. }
  54. private bool IsAllocated()
  55. {
  56. return GetCurrentTexture() != null;
  57. }
  58. // True if the desc changed, graphicsFormat etc.
  59. private bool IsDirty(ref RenderTextureDescriptor desc)
  60. {
  61. return m_DescKey != Hash128.Compute(ref desc);
  62. }
  63. private void Alloc(ref RenderTextureDescriptor desc, bool xrMultipassEnabled)
  64. {
  65. // Generic type, we need double buffering.
  66. AllocHistoryFrameRT(m_Ids[0], 2, ref desc, m_Names[0]);
  67. if(xrMultipassEnabled)
  68. AllocHistoryFrameRT(m_Ids[1], 2, ref desc, m_Names[1]);
  69. m_Descriptor = desc;
  70. m_DescKey = Hash128.Compute(ref desc);
  71. }
  72. /// <summary>
  73. /// Release the history texture(s).
  74. /// </summary>
  75. public override void Reset()
  76. {
  77. for(int i = 0; i < m_Ids.Length; i++)
  78. ReleaseHistoryFrameRT(m_Ids[i]);
  79. }
  80. internal RenderTextureDescriptor GetHistoryDescriptor(ref RenderTextureDescriptor cameraDesc)
  81. {
  82. var depthDesc = cameraDesc;
  83. depthDesc.mipCount = 0;
  84. depthDesc.msaaSamples = 1; // History copy should not have MSAA.
  85. return depthDesc;
  86. }
  87. // Return true if the RTHandles were reallocated.
  88. internal bool Update(ref RenderTextureDescriptor cameraDesc, bool xrMultipassEnabled)
  89. {
  90. if (cameraDesc.width > 0 && cameraDesc.height > 0 && cameraDesc.graphicsFormat != GraphicsFormat.None)
  91. {
  92. var historyDesc = GetHistoryDescriptor(ref cameraDesc);
  93. if (IsDirty(ref historyDesc))
  94. Reset();
  95. if (!IsAllocated())
  96. {
  97. Alloc(ref historyDesc, xrMultipassEnabled);
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. }
  104. }