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.

SingleHistoryBase.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using UnityEngine.Experimental.Rendering;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. // TODO: Draft. A convenience base class to wrap single texture into a type.
  6. internal abstract class SingleHistoryBase : CameraHistoryItem
  7. {
  8. private int m_Id;
  9. private RenderTextureDescriptor m_Descriptor;
  10. private Hash128 m_DescKey;
  11. /// <summary>
  12. /// Called internally on instance creation.
  13. /// Sets up RTHandle ids.
  14. /// </summary>
  15. public override void OnCreate(BufferedRTHandleSystem owner, uint typeId)
  16. {
  17. base.OnCreate(owner, typeId);
  18. m_Id = MakeId(0);
  19. }
  20. /// <summary>
  21. /// Get the history texture(s).
  22. /// </summary>
  23. public RTHandle GetTexture(int frameIndex = 0)
  24. {
  25. if ((uint)frameIndex >= GetHistoryFrameCount())
  26. return null;
  27. return storage.GetFrameRT(m_Id, frameIndex);
  28. }
  29. public RTHandle GetCurrentTexture()
  30. {
  31. return GetCurrentFrameRT(m_Id);
  32. }
  33. public RTHandle GetPreviousTexture()
  34. {
  35. return GetTexture(1);
  36. }
  37. internal bool IsAllocated()
  38. {
  39. return GetTexture() != null;
  40. }
  41. // True if the desc changed, graphicsFormat etc.
  42. internal bool IsDirty(ref RenderTextureDescriptor desc)
  43. {
  44. return m_DescKey != Hash128.Compute(ref desc);
  45. }
  46. private void Alloc(ref RenderTextureDescriptor desc)
  47. {
  48. AllocHistoryFrameRT(m_Id, GetHistoryFrameCount(), ref desc, GetHistoryName());
  49. m_Descriptor = desc;
  50. m_DescKey = Hash128.Compute(ref desc);
  51. }
  52. /// <summary>
  53. /// Release the history texture(s).
  54. /// </summary>
  55. public override void Reset()
  56. {
  57. ReleaseHistoryFrameRT(m_Id);
  58. }
  59. // Return true if the RTHandles were reallocated.
  60. internal bool Update(ref RenderTextureDescriptor cameraDesc)
  61. {
  62. if (cameraDesc.width > 0 && cameraDesc.height > 0 && cameraDesc.graphicsFormat != GraphicsFormat.None)
  63. {
  64. var historyDesc = GetHistoryDescriptor(ref cameraDesc);
  65. if (IsDirty(ref historyDesc))
  66. Reset();
  67. if (!IsAllocated())
  68. {
  69. Alloc(ref historyDesc);
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. protected abstract int GetHistoryFrameCount();
  76. protected abstract string GetHistoryName();
  77. protected abstract RenderTextureDescriptor GetHistoryDescriptor(ref RenderTextureDescriptor cameraDesc);
  78. }
  79. }