RenderTargetBufferSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace UnityEngine.Rendering.Universal.Internal
  8. {
  9. //NOTE: This class is meant to be removed when RTHandles get implemented in urp
  10. internal sealed class RenderTargetBufferSystem
  11. {
  12. struct SwapBuffer
  13. {
  14. public RenderTargetHandle rt;
  15. public int name;
  16. public int msaa;
  17. }
  18. SwapBuffer m_A, m_B;
  19. static bool m_AisBackBuffer = true;
  20. static RenderTextureDescriptor m_Desc;
  21. FilterMode m_FilterMode;
  22. bool m_AllowMSAA = true;
  23. bool m_RTisAllocated = false;
  24. SwapBuffer backBuffer { get { return m_AisBackBuffer ? m_A : m_B; } }
  25. SwapBuffer frontBuffer { get { return m_AisBackBuffer ? m_B : m_A; } }
  26. public RenderTargetBufferSystem(string name)
  27. {
  28. m_A.name = Shader.PropertyToID(name + "A");
  29. m_B.name = Shader.PropertyToID(name + "B");
  30. m_A.rt.Init(name + "A");
  31. m_B.rt.Init(name + "B");
  32. }
  33. public RenderTargetHandle GetBackBuffer()
  34. {
  35. return backBuffer.rt;
  36. }
  37. public RenderTargetHandle GetBackBuffer(CommandBuffer cmd)
  38. {
  39. if (!m_RTisAllocated)
  40. Initialize(cmd);
  41. return backBuffer.rt;
  42. }
  43. public RenderTargetHandle GetFrontBuffer(CommandBuffer cmd)
  44. {
  45. if (!m_RTisAllocated)
  46. Initialize(cmd);
  47. int pipelineMSAA = m_Desc.msaaSamples;
  48. int bufferMSAA = frontBuffer.msaa;
  49. if (m_AllowMSAA && bufferMSAA != pipelineMSAA)
  50. {
  51. //We don't want a depth buffer on B buffer
  52. var desc = m_Desc;
  53. if (m_AisBackBuffer)
  54. desc.depthBufferBits = 0;
  55. cmd.ReleaseTemporaryRT(frontBuffer.name);
  56. cmd.GetTemporaryRT(frontBuffer.name, desc, m_FilterMode);
  57. if (m_AisBackBuffer)
  58. m_B.msaa = desc.msaaSamples;
  59. else m_A.msaa = desc.msaaSamples;
  60. }
  61. else if (!m_AllowMSAA && bufferMSAA > 1)
  62. {
  63. //We don't want a depth buffer on B buffer
  64. var desc = m_Desc;
  65. desc.msaaSamples = 1;
  66. if (m_AisBackBuffer)
  67. desc.depthBufferBits = 0;
  68. cmd.ReleaseTemporaryRT(frontBuffer.name);
  69. cmd.GetTemporaryRT(frontBuffer.name, desc, m_FilterMode);
  70. if (m_AisBackBuffer)
  71. m_B.msaa = desc.msaaSamples;
  72. else m_A.msaa = desc.msaaSamples;
  73. }
  74. return frontBuffer.rt;
  75. }
  76. public void Swap()
  77. {
  78. m_AisBackBuffer = !m_AisBackBuffer;
  79. }
  80. void Initialize(CommandBuffer cmd)
  81. {
  82. m_A.msaa = m_Desc.msaaSamples;
  83. m_B.msaa = m_Desc.msaaSamples;
  84. cmd.GetTemporaryRT(m_A.name, m_Desc, m_FilterMode);
  85. var descB = m_Desc;
  86. //descB.depthBufferBits = 0;
  87. cmd.GetTemporaryRT(m_B.name, descB, m_FilterMode);
  88. m_RTisAllocated = true;
  89. }
  90. public void Clear(CommandBuffer cmd)
  91. {
  92. cmd.ReleaseTemporaryRT(m_A.name);
  93. cmd.ReleaseTemporaryRT(m_B.name);
  94. m_AisBackBuffer = true;
  95. m_AllowMSAA = true;
  96. }
  97. public void SetCameraSettings(CommandBuffer cmd, RenderTextureDescriptor desc, FilterMode filterMode)
  98. {
  99. Clear(cmd); //SetCameraSettings is called when new stack starts rendering. Make sure the targets are updated to use the new descriptor.
  100. m_Desc = desc;
  101. m_FilterMode = filterMode;
  102. Initialize(cmd);
  103. }
  104. public RenderTargetHandle GetBufferA()
  105. {
  106. return m_A.rt;
  107. }
  108. public void EnableMSAA(bool enable)
  109. {
  110. m_AllowMSAA = enable;
  111. }
  112. }
  113. }