BufferedRTHandleSystem.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Assertions;
  4. using UnityEngine.Rendering;
  5. namespace UnityEngine.Rendering
  6. {
  7. /// <summary>
  8. /// Implement a multiple buffering for RenderTextures.
  9. /// </summary>
  10. /// <exemple>
  11. /// <code>
  12. /// enum BufferType
  13. /// {
  14. /// Color,
  15. /// Depth
  16. /// }
  17. ///
  18. /// void Render()
  19. /// {
  20. /// var camera = GetCamera();
  21. /// var buffers = GetFrameHistoryBuffersFor(camera);
  22. ///
  23. /// // Set reference size in case the rendering size changed this frame
  24. /// buffers.SetReferenceSize(
  25. /// GetCameraWidth(camera), GetCameraHeight(camera),
  26. /// GetCameraUseMSAA(camera), GetCameraMSAASamples(camera)
  27. /// );
  28. /// buffers.Swap();
  29. ///
  30. /// var currentColor = buffer.GetFrameRT((int)BufferType.Color, 0);
  31. /// if (currentColor == null) // Buffer was not allocated
  32. /// {
  33. /// buffer.AllocBuffer(
  34. /// (int)BufferType.Color, // Color buffer id
  35. /// ColorBufferAllocator, // Custom functor to implement allocation
  36. /// 2 // Use 2 RT for this buffer for double buffering
  37. /// );
  38. /// currentColor = buffer.GetFrameRT((int)BufferType.Color, 0);
  39. /// }
  40. ///
  41. /// var previousColor = buffers.GetFrameRT((int)BufferType.Color, 1);
  42. ///
  43. /// // Use previousColor and write into currentColor
  44. /// }
  45. /// </code>
  46. /// </exemple>
  47. public class BufferedRTHandleSystem : IDisposable
  48. {
  49. Dictionary<int, RTHandle[]> m_RTHandles = new Dictionary<int, RTHandle[]>();
  50. RTHandleSystem m_RTHandleSystem = new RTHandleSystem();
  51. bool m_DisposedValue = false;
  52. /// <summary>
  53. /// Maximum allocated width of the Buffered RTHandle System
  54. /// </summary>
  55. public int maxWidth { get { return m_RTHandleSystem.GetMaxWidth(); } }
  56. /// <summary>
  57. /// Maximum allocated height of the Buffered RTHandle System
  58. /// </summary>
  59. public int maxHeight { get { return m_RTHandleSystem.GetMaxHeight(); } }
  60. /// <summary>
  61. /// Current properties of the Buffered RTHandle System
  62. /// </summary>
  63. public RTHandleProperties rtHandleProperties { get { return m_RTHandleSystem.rtHandleProperties; } }
  64. /// <summary>
  65. /// Return the frame RT or null.
  66. /// </summary>
  67. /// <param name="bufferId">Defines the buffer to use.</param>
  68. /// <param name="frameIndex"></param>
  69. /// <returns>The frame RT or null when the <paramref name="bufferId"/> was not previously allocated (<see cref="BufferedRTHandleSystem.AllocBuffer(int, Func{RTHandleSystem, int, RTHandle}, int)" />).</returns>
  70. public RTHandle GetFrameRT(int bufferId, int frameIndex)
  71. {
  72. if (!m_RTHandles.ContainsKey(bufferId))
  73. return null;
  74. Assert.IsTrue(frameIndex >= 0 && frameIndex < m_RTHandles[bufferId].Length);
  75. return m_RTHandles[bufferId][frameIndex];
  76. }
  77. /// <summary>
  78. /// Allocate RT handles for a buffer.
  79. /// </summary>
  80. /// <param name="bufferId">The buffer to allocate.</param>
  81. /// <param name="allocator">The functor to use for allocation.</param>
  82. /// <param name="bufferCount">The number of RT handles for this buffer.</param>
  83. public void AllocBuffer(
  84. int bufferId,
  85. Func<RTHandleSystem, int, RTHandle> allocator,
  86. int bufferCount
  87. )
  88. {
  89. var buffer = new RTHandle[bufferCount];
  90. m_RTHandles.Add(bufferId, buffer);
  91. // First is autoresized
  92. buffer[0] = allocator(m_RTHandleSystem, 0);
  93. // Other are resized on demand
  94. for (int i = 1, c = buffer.Length; i < c; ++i)
  95. {
  96. buffer[i] = allocator(m_RTHandleSystem, i);
  97. m_RTHandleSystem.SwitchResizeMode(buffer[i], RTHandleSystem.ResizeMode.OnDemand);
  98. }
  99. }
  100. /// <summary>
  101. /// Release a buffer
  102. /// </summary>
  103. /// <param name="bufferId">Id of the buffer that needs to be released.</param>
  104. public void ReleaseBuffer(int bufferId)
  105. {
  106. if (m_RTHandles.TryGetValue(bufferId, out var buffers))
  107. {
  108. foreach (var rt in buffers)
  109. m_RTHandleSystem.Release(rt);
  110. }
  111. m_RTHandles.Remove(bufferId);
  112. }
  113. /// <summary>
  114. /// Swap buffers Set the reference size for this RT Handle System (<see cref="RTHandleSystem.SetReferenceSize(int, int, bool, MSAASamples)"/>)
  115. /// </summary>
  116. /// <param name="width">The width of the RTs of this buffer.</param>
  117. /// <param name="height">The height of the RTs of this buffer.</param>
  118. /// <param name="msaaSamples">Number of MSAA samples for this buffer.</param>
  119. public void SwapAndSetReferenceSize(int width, int height, MSAASamples msaaSamples)
  120. {
  121. Swap();
  122. m_RTHandleSystem.SetReferenceSize(width, height, msaaSamples);
  123. }
  124. /// <summary>
  125. /// Reset the reference size of the system and reallocate all textures.
  126. /// </summary>
  127. /// <param name="width">New width.</param>
  128. /// <param name="height">New height.</param>
  129. public void ResetReferenceSize(int width, int height)
  130. {
  131. m_RTHandleSystem.ResetReferenceSize(width, height);
  132. }
  133. /// <summary>
  134. /// Queries the number of RT handle buffers allocated for a buffer ID.
  135. /// </summary>
  136. /// <param name="bufferId">The buffer ID to query.</param>
  137. public int GetNumFramesAllocated(int bufferId)
  138. {
  139. if (!m_RTHandles.ContainsKey(bufferId))
  140. return 0;
  141. return m_RTHandles[bufferId].Length;
  142. }
  143. void Swap()
  144. {
  145. foreach (var item in m_RTHandles)
  146. {
  147. // Do not index out of bounds...
  148. if (item.Value.Length > 1)
  149. {
  150. var nextFirst = item.Value[item.Value.Length - 1];
  151. for (int i = 0, c = item.Value.Length - 1; i < c; ++i)
  152. item.Value[i + 1] = item.Value[i];
  153. item.Value[0] = nextFirst;
  154. // First is autoresize, other are on demand
  155. m_RTHandleSystem.SwitchResizeMode(item.Value[0], RTHandleSystem.ResizeMode.Auto);
  156. m_RTHandleSystem.SwitchResizeMode(item.Value[1], RTHandleSystem.ResizeMode.OnDemand);
  157. }
  158. else
  159. {
  160. m_RTHandleSystem.SwitchResizeMode(item.Value[0], RTHandleSystem.ResizeMode.Auto);
  161. }
  162. }
  163. }
  164. void Dispose(bool disposing)
  165. {
  166. if (!m_DisposedValue)
  167. {
  168. if (disposing)
  169. {
  170. ReleaseAll();
  171. m_RTHandleSystem.Dispose();
  172. m_RTHandleSystem = null;
  173. }
  174. m_DisposedValue = true;
  175. }
  176. }
  177. /// <summary>
  178. /// Dispose implementation
  179. /// </summary>
  180. public void Dispose()
  181. {
  182. Dispose(true);
  183. }
  184. /// <summary>
  185. /// Deallocate and clear all buffers.
  186. /// </summary>
  187. public void ReleaseAll()
  188. {
  189. foreach (var item in m_RTHandles)
  190. {
  191. for (int i = 0, c = item.Value.Length; i < c; ++i)
  192. {
  193. m_RTHandleSystem.Release(item.Value[i]);
  194. }
  195. }
  196. m_RTHandles.Clear();
  197. }
  198. }
  199. }