RenderGraphBuilder.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.Experimental.Rendering.RenderGraphModule
  4. {
  5. /// <summary>
  6. /// Use this struct to set up a new Render Pass.
  7. /// </summary>
  8. public struct RenderGraphBuilder : IDisposable
  9. {
  10. RenderGraph.RenderPass m_RenderPass;
  11. RenderGraphResourceRegistry m_Resources;
  12. bool m_Disposed;
  13. #region Public Interface
  14. /// <summary>
  15. /// Specify that the pass will use a Texture resource as a color render target.
  16. /// This has the same effect as WriteTexture and also automatically sets the Texture to use as a render target.
  17. /// </summary>
  18. /// <param name="input">The Texture resource to use as a color render target.</param>
  19. /// <param name="index">Index for multiple render target usage.</param>
  20. /// <returns>An updated resource handle to the input resource.</returns>
  21. public RenderGraphMutableResource UseColorBuffer(in RenderGraphMutableResource input, int index)
  22. {
  23. if (input.type != RenderGraphResourceType.Texture)
  24. throw new ArgumentException("Trying to write to a resource that is not a texture or is invalid.");
  25. m_RenderPass.SetColorBuffer(input, index);
  26. m_Resources.UpdateTextureFirstWrite(input, m_RenderPass.index);
  27. return input;
  28. }
  29. /// <summary>
  30. /// Specify that the pass will use a Texture resource as a depth buffer.
  31. /// </summary>
  32. /// <param name="input">The Texture resource to use as a depth buffer during the pass.</param>
  33. /// <param name="flags">Specify the access level for the depth buffer. This allows you to say whether you will read from or write to the depth buffer, or do both.</param>
  34. /// <returns>An updated resource handle to the input resource.</returns>
  35. public RenderGraphMutableResource UseDepthBuffer(in RenderGraphMutableResource input, DepthAccess flags)
  36. {
  37. if (input.type != RenderGraphResourceType.Texture)
  38. throw new ArgumentException("Trying to write to a resource that is not a texture or is invalid.");
  39. m_RenderPass.SetDepthBuffer(input, flags);
  40. if ((flags | DepthAccess.Read) != 0)
  41. m_Resources.UpdateTextureLastRead(input, m_RenderPass.index);
  42. if ((flags | DepthAccess.Write) != 0)
  43. m_Resources.UpdateTextureFirstWrite(input, m_RenderPass.index);
  44. return input;
  45. }
  46. /// <summary>
  47. /// Specify a Texture resource to read from during the pass.
  48. /// </summary>
  49. /// <param name="input">The Texture resource to read from during the pass.</param>
  50. /// <returns>An updated resource handle to the input resource.</returns>
  51. public RenderGraphResource ReadTexture(in RenderGraphResource input)
  52. {
  53. if (input.type != RenderGraphResourceType.Texture)
  54. throw new ArgumentException("Trying to read a resource that is not a texture or is invalid.");
  55. m_RenderPass.resourceReadList.Add(input);
  56. m_Resources.UpdateTextureLastRead(input, m_RenderPass.index);
  57. return input;
  58. }
  59. /// <summary>
  60. /// Specify a Texture resource to write to during the pass.
  61. /// </summary>
  62. /// <param name="input">The Texture resource to write to during the pass.</param>
  63. /// <returns>An updated resource handle to the input resource.</returns>
  64. public RenderGraphMutableResource WriteTexture(in RenderGraphMutableResource input)
  65. {
  66. if (input.type != RenderGraphResourceType.Texture)
  67. throw new ArgumentException("Trying to write to a resource that is not a texture or is invalid.");
  68. // TODO: Manage resource "version" for debugging purpose
  69. m_RenderPass.resourceWriteList.Add(input);
  70. m_Resources.UpdateTextureFirstWrite(input, m_RenderPass.index);
  71. return input;
  72. }
  73. /// <summary>
  74. /// Specify a Renderer List resource to use during the pass.
  75. /// </summary>
  76. /// <param name="input">The Renderer List resource to use during the pass.</param>
  77. /// <returns>An updated resource handle to the input resource.</returns>
  78. public RenderGraphResource UseRendererList(in RenderGraphResource input)
  79. {
  80. if (input.type != RenderGraphResourceType.RendererList)
  81. throw new ArgumentException("Trying use a resource that is not a renderer list.");
  82. m_RenderPass.usedRendererListList.Add(input);
  83. return input;
  84. }
  85. /// <summary>
  86. /// Specify the render function to use for this pass.
  87. /// A call to this is mandatory for the pass to be valid.
  88. /// </summary>
  89. /// <typeparam name="PassData">The Type of the class that provides data to the Render Pass.</typeparam>
  90. /// <param name="renderFunc">Render function for the pass.</param>
  91. public void SetRenderFunc<PassData>(RenderFunc<PassData> renderFunc) where PassData : class, new()
  92. {
  93. ((RenderGraph.RenderPass<PassData>)m_RenderPass).renderFunc = renderFunc;
  94. }
  95. /// <summary>
  96. /// Enable asynchronous compute for this pass.
  97. /// </summary>
  98. /// <param name="value">Set to true to enable asynchronous compute.</param>
  99. public void EnableAsyncCompute(bool value)
  100. {
  101. m_RenderPass.enableAsyncCompute = value;
  102. }
  103. /// <summary>
  104. /// Dispose the RenderGraphBuilder instance.
  105. /// </summary>
  106. public void Dispose()
  107. {
  108. Dispose(true);
  109. }
  110. #endregion
  111. #region Internal Interface
  112. internal RenderGraphBuilder(RenderGraph.RenderPass renderPass, RenderGraphResourceRegistry resources)
  113. {
  114. m_RenderPass = renderPass;
  115. m_Resources = resources;
  116. m_Disposed = false;
  117. }
  118. void Dispose(bool disposing)
  119. {
  120. if (m_Disposed)
  121. return;
  122. m_Disposed = true;
  123. }
  124. #endregion
  125. }
  126. }