PostProcessPasses.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Rendering.Universal.Internal;
  6. namespace UnityEngine.Rendering.Universal
  7. {
  8. /// <summary>
  9. /// Type acts as wrapper for post process passes. Can we be recreated and destroyed at any point during runtime with post process data.
  10. /// </summary>
  11. internal struct PostProcessPasses : IDisposable
  12. {
  13. ColorGradingLutPass m_ColorGradingLutPass;
  14. PostProcessPass m_PostProcessPass;
  15. PostProcessPass m_FinalPostProcessPass;
  16. RenderTargetHandle m_AfterPostProcessColor;
  17. RenderTargetHandle m_ColorGradingLut;
  18. PostProcessData m_RendererPostProcessData;
  19. PostProcessData m_CurrentPostProcessData;
  20. Material m_BlitMaterial;
  21. public ColorGradingLutPass colorGradingLutPass { get => m_ColorGradingLutPass; }
  22. public PostProcessPass postProcessPass { get => m_PostProcessPass; }
  23. public PostProcessPass finalPostProcessPass { get => m_FinalPostProcessPass; }
  24. public RenderTargetHandle afterPostProcessColor { get => m_AfterPostProcessColor; }
  25. public RenderTargetHandle colorGradingLut { get => m_ColorGradingLut; }
  26. public bool isCreated { get => m_CurrentPostProcessData != null; }
  27. public PostProcessPasses(PostProcessData rendererPostProcessData, Material blitMaterial)
  28. {
  29. m_ColorGradingLutPass = null;
  30. m_PostProcessPass = null;
  31. m_FinalPostProcessPass = null;
  32. m_AfterPostProcessColor = new RenderTargetHandle();
  33. m_ColorGradingLut = new RenderTargetHandle();
  34. m_CurrentPostProcessData = null;
  35. m_AfterPostProcessColor.Init("_AfterPostProcessTexture");
  36. m_ColorGradingLut.Init("_InternalGradingLut");
  37. m_RendererPostProcessData = rendererPostProcessData;
  38. m_BlitMaterial = blitMaterial;
  39. Recreate(rendererPostProcessData);
  40. }
  41. /// <summary>
  42. /// Recreates post process passes with supplied data. If already contains valid post process passes, they will be replaced by new ones.
  43. /// </summary>
  44. /// <param name="data">Resources used for creating passes. In case of the null, no passes will be created.</param>
  45. public void Recreate(PostProcessData data)
  46. {
  47. if (m_RendererPostProcessData)
  48. data = m_RendererPostProcessData;
  49. if (data == m_CurrentPostProcessData)
  50. return;
  51. if (m_CurrentPostProcessData != null)
  52. {
  53. m_ColorGradingLutPass?.Cleanup();
  54. m_PostProcessPass?.Cleanup();
  55. m_FinalPostProcessPass?.Cleanup();
  56. // We need to null post process passes to avoid using them
  57. m_ColorGradingLutPass = null;
  58. m_PostProcessPass = null;
  59. m_FinalPostProcessPass = null;
  60. m_CurrentPostProcessData = null;
  61. }
  62. if (data != null)
  63. {
  64. m_ColorGradingLutPass = new ColorGradingLutPass(RenderPassEvent.BeforeRenderingPrePasses, data);
  65. m_PostProcessPass = new PostProcessPass(RenderPassEvent.BeforeRenderingPostProcessing, data, m_BlitMaterial);
  66. m_FinalPostProcessPass = new PostProcessPass(RenderPassEvent.AfterRenderingPostProcessing, data, m_BlitMaterial);
  67. m_CurrentPostProcessData = data;
  68. }
  69. }
  70. public void Dispose()
  71. {
  72. // always dispose unmanaged resources
  73. m_ColorGradingLutPass?.Cleanup();
  74. m_PostProcessPass?.Cleanup();
  75. m_FinalPostProcessPass?.Cleanup();
  76. }
  77. }
  78. }