暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IRenderGraphBuilder.cs 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using UnityEngine.Scripting.APIUpdating;
  3. namespace UnityEngine.Rendering.RenderGraphModule
  4. {
  5. /// <summary>
  6. /// Common base interface for the different render graph builders. These functions are supported on all builders.
  7. /// </summary>
  8. [MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
  9. public interface IBaseRenderGraphBuilder : IDisposable
  10. {
  11. /// <summary>
  12. /// Declare that this pass uses the input texture.
  13. /// </summary>
  14. /// <param name="input">The texture resource to use during the pass.</param>
  15. /// <param name="flags">A combination of flags indicating how the resource will be used during the pass. Default value is set to AccessFlag.Read </param>
  16. public void UseTexture(in TextureHandle input, AccessFlags flags = AccessFlags.Read);
  17. /// <summary>
  18. /// Declare that this pass uses the texture assigned to the global texture slot. The actual texture referenced is indirectly specified here it depends
  19. /// on the value previous passes that were added to the graph set for the global texture slot. If no previous pass set a texture to the global slot an
  20. /// exception will be raised.
  21. /// </summary>
  22. /// <param name="propertyId">The global texture slot read by shaders in this pass. Use Shader.PropertyToID to generate these ids.</param>
  23. /// <param name="flags">A combination of flags indicating how the resource will be used during the pass. Default value is set to AccessFlag.Read </param>
  24. public void UseGlobalTexture(int propertyId, AccessFlags flags = AccessFlags.Read);
  25. /// <summary>
  26. /// Indicate that this pass will reference all textures in global texture slots known to the graph. The default setting is false.
  27. /// It is highly recommended if you know which globals you pass will access to use UseTexture(glboalTextureSlotId) with individual texture slots instead of
  28. /// UseAllGlobalTextures(true) to ensure the graph can maximally optimize resource use and lifetimes.
  29. ///
  30. /// This function should only be used in cases where it is difficult/impossible to know which globals a pass will access. This is for example true if your pass
  31. /// renders objects in the scene (e.g. using CommandBuffer.DrawRendererList) that might be using arbitrary shaders which in turn may access arbitrary global textures.
  32. /// To avoid having to do a UseAllGlobalTextures(true) in this situation, you will either have to ensure *all* shaders are well behaved and do not access spurious
  33. /// globals our make sure your renderer list filters allow only shaders that are known to be well behaved to pass.
  34. /// </summary>
  35. /// <param name="enable">If true the pass from which this is called will reference all global textures.</param>
  36. public void UseAllGlobalTextures(bool enable);
  37. /// <summary>
  38. /// Make this pass set a global texture slot *at the end* of this pass. During this pass the global texture will still have it's
  39. /// old value. Only after this pass the global texture slot will take on the new value specified.
  40. /// Generally this pass will also do a UseTexture(write) on this texture handle to indicate it is generating this texture, but this is not really a requirement
  41. /// you can have a pass that simply sets up a new value in a global texture slot but doesn't write it.
  42. /// Although counter-intuitive at first, this call doesn't actually have a dependency on the passed in texture handle. It's only when a subsequent pass has
  43. /// a dependency on the global texture slot that subsequent pass will get a dependency on the currently set global texture for that slot. This means
  44. /// globals slots can be set without overhead if you're unsure if a resource will be used or not, the graph will still maintain the correct lifetimes.
  45. /// </summary>
  46. /// <param name="input">The texture value to set in the global texture slot. This can be an null handle to clear the global texture slot.</param>
  47. /// <param name="propertyId">The global texture slot to set the value for. Use Shader.PropertyToID to generate the id.</param>
  48. public void SetGlobalTextureAfterPass(in TextureHandle input, int propertyId);
  49. /// <summary>
  50. /// Declare that this pass uses the input compute buffer.
  51. /// </summary>
  52. /// <param name="input">The compute buffer resource to use during the pass.</param>
  53. /// <param name="flags">A combination of flags indicating how the resource will be used during the pass. Default value is set to AccessFlag.Read </param>
  54. /// <returns>The value passed to 'input'. You should not use the returned value it will be removed in the future.</returns>
  55. public BufferHandle UseBuffer(in BufferHandle input, AccessFlags flags = AccessFlags.Read);
  56. /// <summary>
  57. /// Create a new Render Graph Texture resource.
  58. /// This texture will only be available for the current pass and will be assumed to be both written and read so users don't need to add explicit read/write declarations.
  59. /// </summary>
  60. /// <param name="desc">Texture descriptor.</param>
  61. /// <returns>A new transient TextureHandle.</returns>
  62. public TextureHandle CreateTransientTexture(in TextureDesc desc);
  63. /// <summary>
  64. /// Create a new Render Graph Texture resource using the descriptor from another texture.
  65. /// This texture will only be available for the current pass and will be assumed to be both written and read so users don't need to add explicit read/write declarations.
  66. /// </summary>
  67. /// <param name="texture">Texture from which the descriptor should be used.</param>
  68. /// <returns>A new transient TextureHandle.</returns>
  69. public TextureHandle CreateTransientTexture(in TextureHandle texture);
  70. /// <summary>
  71. /// Create a new Render Graph Graphics Buffer resource.
  72. /// This Graphics Buffer will only be available for the current pass and will be assumed to be both written and read so users don't need to add explicit read/write declarations.
  73. /// </summary>
  74. /// <param name="desc">Compute Buffer descriptor.</param>
  75. /// <returns>A new transient BufferHandle.</returns>
  76. public BufferHandle CreateTransientBuffer(in BufferDesc desc);
  77. /// <summary>
  78. /// Create a new Render Graph Graphics Buffer resource using the descriptor from another Graphics Buffer.
  79. /// This Graphics Buffer will only be available for the current pass and will be assumed to be both written and read so users don't need to add explicit read/write declarations.
  80. /// </summary>
  81. /// <param name="computebuffer">Graphics Buffer from which the descriptor should be used.</param>
  82. /// <returns>A new transient BufferHandle.</returns>
  83. public BufferHandle CreateTransientBuffer(in BufferHandle computebuffer);
  84. /// <summary>
  85. /// This pass will read from this renderer list. RendererLists are always read-only in the graph so have no access flags.
  86. /// </summary>
  87. /// <param name="input">The Renderer List resource to use during the pass.</param>
  88. public void UseRendererList(in RendererListHandle input);
  89. /// <summary>
  90. /// Enable asynchronous compute for this pass.
  91. /// </summary>
  92. /// <param name="value">Set to true to enable asynchronous compute.</param>
  93. public void EnableAsyncCompute(bool value);
  94. /// <summary>
  95. /// Allow or not pass culling
  96. /// By default all passes can be culled out if the render graph detects it's not actually used.
  97. /// In some cases, a pass may not write or read any texture but rather do something with side effects (like setting a global texture parameter for example).
  98. /// This function can be used to tell the system that it should not cull this pass.
  99. /// </summary>
  100. /// <param name="value">True to allow pass culling.</param>
  101. public void AllowPassCulling(bool value);
  102. /// <summary>
  103. /// Allow commands in the command buffer to modify global state. This will introduce a render graph sync-point in the frame and cause all passes after this pass to never be
  104. /// reordered before this pass. This may nave negative impact on performance and memory use if not used carefully so it is recommended to only allow this in specific use cases.
  105. /// This will also set AllowPassCulling to false.
  106. /// </summary>
  107. /// <param name="value">True to allow global state modification.</param>
  108. public void AllowGlobalStateModification(bool value);
  109. /// <summary>
  110. /// Enable foveated rendering for this pass.
  111. /// </summary>
  112. /// <param name="value">True to enable foveated rendering.</param>
  113. public void EnableFoveatedRasterization(bool value);
  114. }
  115. /// <summary>
  116. /// A builder for a compute render pass
  117. /// <see cref="RenderGraph.AddComputePass"/>
  118. /// </summary>
  119. [MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
  120. public interface IComputeRenderGraphBuilder : IBaseRenderGraphBuilder
  121. {
  122. /// <summary>
  123. /// Specify the render function to use for this pass.
  124. /// A call to this is mandatory for the pass to be valid.
  125. /// </summary>
  126. /// <typeparam name="PassData">The Type of the class that provides data to the Render Pass.</typeparam>
  127. /// <param name="renderFunc">Render function for the pass.</param>
  128. public void SetRenderFunc<PassData>(BaseRenderFunc<PassData, ComputeGraphContext> renderFunc)
  129. where PassData : class, new();
  130. }
  131. /// <summary>
  132. /// A builder for an unsafe render pass.
  133. /// <see cref="RenderGraph.AddUnsafePass"/>
  134. /// </summary>
  135. [MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
  136. public interface IUnsafeRenderGraphBuilder : IBaseRenderGraphBuilder
  137. {
  138. /// <summary>
  139. /// Specify the render function to use for this pass.
  140. /// A call to this is mandatory for the pass to be valid.
  141. /// </summary>
  142. /// <typeparam name="PassData">The Type of the class that provides data to the Render Pass.</typeparam>
  143. /// <param name="renderFunc">Render function for the pass.</param>
  144. public void SetRenderFunc<PassData>(BaseRenderFunc<PassData, UnsafeGraphContext> renderFunc)
  145. where PassData : class, new();
  146. }
  147. /// <summary>
  148. /// A builder for a raster render pass
  149. /// <see cref="RenderGraph.AddRasterRenderPass"/>
  150. /// </summary>
  151. [MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
  152. public interface IRasterRenderGraphBuilder : IBaseRenderGraphBuilder
  153. {
  154. /// <summary>
  155. /// Use the texture as an rendertarget attachment.
  156. ///
  157. /// Writing:
  158. /// Indicate this pass will write a texture through rendertarget rasterization writes.
  159. /// The graph will automatically bind the texture as an MRT output on the indicated index slot.
  160. /// Write in shader as float4 out : SV_Target{index} = value; This texture always needs to be written as an
  161. /// render target (SV_Targetx) writing using other methods (like `operator[] =` ) may not work even if
  162. /// using the current fragment+sampleIdx pos. When using operator[] please use the UseTexture function instead.
  163. /// Reading:
  164. /// Indicates this pass will read a texture on the current fragment position but not unnecessarily modify it. Although not explicitly visible in shader code
  165. /// Reading may happen depending on the rasterization state, e.g. Blending (read and write) or Z-Testing (read only) may read the buffer.
  166. ///
  167. /// Note: The rendergraph does not know what content will be rendered in the bound texture. By default it assumes only partial data
  168. /// is written (e.g. a small rectangle is drawn on the screen) so it will preserve the existing rendertarget content (e.g. behind/around the triangle)
  169. /// if you know you will write the full screen the AccessFlags.WriteAll should be used instead as it will give better performance.
  170. /// </summary>
  171. /// <param name="tex">Texture to use during this pass.</param>
  172. /// <param name="index">Index the shader will use to access this texture.</param>
  173. /// <param name="flags">How this pass will access the texture. Default value is set to AccessFlag.Write </param>
  174. void SetRenderAttachment(TextureHandle tex, int index, AccessFlags flags = AccessFlags.Write)
  175. {
  176. SetRenderAttachment(tex, index, flags, 0, -1);
  177. }
  178. /// <summary>
  179. /// Use the texture as an rendertarget attachment.
  180. ///
  181. /// Writing:
  182. /// Indicate this pass will write a texture through rendertarget rasterization writes.
  183. /// The graph will automatically bind the texture as an MRT output on the indicated index slot.
  184. /// Write in shader as float4 out : SV_Target{index} = value; This texture always needs to be written as an
  185. /// render target (SV_Targetx) writing using other methods (like `operator[] =` ) may not work even if
  186. /// using the current fragment+sampleIdx pos. When using operator[] please use the UseTexture function instead.
  187. /// Reading:
  188. /// Indicates this pass will read a texture on the current fragment position but not unnecessarily modify it. Although not explicitly visible in shader code
  189. /// Reading may happen depending on the rasterization state, e.g. Blending (read and write) or Z-Testing (read only) may read the buffer.
  190. ///
  191. /// Note: The rendergraph does not know what content will be rendered in the bound texture. By default it assumes only partial data
  192. /// is written (e.g. a small rectangle is drawn on the screen) so it will preserve the existing rendertarget content (e.g. behind/around the triangle)
  193. /// if you know you will write the full screen the AccessFlags.WriteAll should be used instead as it will give better performance.
  194. /// </summary>
  195. /// <param name="tex">Texture to use during this pass.</param>
  196. /// <param name="index">Index the shader will use to access this texture.</param>
  197. /// <param name="flags">How this pass will access the texture. </param>
  198. /// <param name="mipLevel">Selects which mip map to used.</param>
  199. /// <param name="depthSlice">Used to index into a texture array. Use -1 to use bind all slices.</param>
  200. void SetRenderAttachment(TextureHandle tex, int index, AccessFlags flags, int mipLevel, int depthSlice);
  201. /// <summary>
  202. /// Use the texture as an input attachment.
  203. ///
  204. /// This informs the graph that any shaders in pass will only read from this texture at the current fragment position using the
  205. /// LOAD_FRAMEBUFFER_INPUT(idx)/LOAD_FRAMEBUFFER_INPUT_MS(idx,sampleIdx) macros. The index passed to LOAD_FRAMEBUFFER_INPUT needs
  206. /// to match the index passed to SetInputAttachment for this texture.
  207. ///
  208. /// </summary>
  209. /// <param name="tex">Texture to use during this pass.</param>
  210. /// <param name="index">Index the shader will use to access this texture.</param>
  211. /// <param name="flags">How this pass will access the texture. Default value is set to AccessFlag.Read. Writing is currently not supported on any platform. </param>
  212. void SetInputAttachment(TextureHandle tex, int index, AccessFlags flags = AccessFlags.Read)
  213. {
  214. SetInputAttachment(tex, index, flags, 0, -1);
  215. }
  216. /// <summary>
  217. /// Use the texture as an input attachment.
  218. ///
  219. /// This informs the graph that any shaders in pass will only read from this texture at the current fragment position using the
  220. /// LOAD_FRAMEBUFFER_INPUT(idx)/LOAD_FRAMEBUFFER_INPUT_MS(idx,sampleIdx) macros. The index passed to LOAD_FRAMEBUFFER_INPUT needs
  221. /// to match the index passed to SetInputAttachment for this texture.
  222. ///
  223. /// </summary>
  224. /// <param name="tex">Texture to use during this pass.</param>
  225. /// <param name="index">Index the shader will use to access this texture.</param>
  226. /// <param name="flags">How this pass will access the texture. Writing is currently not supported on any platform. </param>
  227. /// <param name="mipLevel">Selects which mip map to used.</param>
  228. /// <param name="depthSlice">Used to index into a texture array. Use -1 to use bind all slices.</param>
  229. void SetInputAttachment(TextureHandle tex, int index, AccessFlags flags, int mipLevel, int depthSlice);
  230. /// <summary>
  231. /// Use the texture as a depth buffer for the Z-Buffer hardware. Note you can only test-against and write-to a single depth texture in a pass.
  232. /// If you want to write depth to more than one texture you will need to register the second texture as SetRenderAttachment and manually calculate
  233. /// and write the depth value in the shader.
  234. /// Calling SetRenderAttachmentDepth twice on the same builder is an error.
  235. /// Write:
  236. /// Indicate a texture will be written with the current fragment depth by the ROPs (but not for depth reading (i.e. z-test == always)).
  237. /// Read:
  238. /// Indicate a texture will be used as an input for the depth testing unit.
  239. /// </summary>
  240. /// <param name="tex">Texture to use during this pass.</param>
  241. /// <param name="flags">How this pass will access the texture. Default value is set to AccessFlag.Write </param>
  242. void SetRenderAttachmentDepth(TextureHandle tex, AccessFlags flags = AccessFlags.Write)
  243. {
  244. SetRenderAttachmentDepth(tex, flags, 0, -1);
  245. }
  246. /// <summary>
  247. /// Use the texture as a depth buffer for the Z-Buffer hardware. Note you can only test-against and write-to a single depth texture in a pass.
  248. /// If you want to write depth to more than one texture you will need to register the second texture as SetRenderAttachment and manually calculate
  249. /// and write the depth value in the shader.
  250. /// Calling SetRenderAttachmentDepth twice on the same builder is an error.
  251. /// Write:
  252. /// Indicate a texture will be written with the current fragment depth by the ROPs (but not for depth reading (i.e. z-test == always)).
  253. /// Read:
  254. /// Indicate a texture will be used as an input for the depth testing unit.
  255. /// </summary>
  256. /// <param name="tex">Texture to use during this pass.</param>
  257. /// <param name="flags">How this pass will access the texture.</param>
  258. /// <param name="mipLevel">Selects which mip map to used.</param>
  259. /// <param name="depthSlice">Used to index into a texture array. Use -1 to use bind all slices.</param>
  260. void SetRenderAttachmentDepth(TextureHandle tex, AccessFlags flags, int mipLevel, int depthSlice);
  261. /// <summary>
  262. /// Use the texture as an random access attachment. This is called "Unordered Access View" in DX12 and "Storage Image" in Vulkan.
  263. ///
  264. /// This informs the graph that any shaders in the pass will access the texture as a random access attachment through RWTexture2d&lt;T&gt;, RWTexture3d&lt;T&gt;,...
  265. /// The texture can then be read/written by regular HLSL commands (including atomics, etc.).
  266. ///
  267. /// As in other parts of the Unity graphics APIs random access textures share the index-based slots with render targets and input attachments. See CommandBuffer.SetRandomWriteTarget for details.
  268. /// </summary>
  269. /// <param name="tex">Texture to use during this pass.</param>
  270. /// <param name="index">Index the shader will use to access this texture. This is set in the shader through the `register(ux)` keyword.</param>
  271. /// <param name="flags">How this pass will access the texture. Default value is set to AccessFlag.ReadWrite.</param>
  272. /// <returns>The value passed to 'input'. You should not use the returned value it will be removed in the future.</returns>
  273. TextureHandle SetRandomAccessAttachment(TextureHandle tex, int index, AccessFlags flags = AccessFlags.ReadWrite);
  274. /// <summary>
  275. /// Use the buffer as an random access attachment. This is called "Unordered Access View" in DX12 and "Storage Buffer" in Vulkan.
  276. ///
  277. /// This informs the graph that any shaders in the pass will access the buffer as a random access attachment through RWStructuredBuffer, RWByteAddressBuffer,...
  278. /// The buffer can then be read/written by regular HLSL commands (including atomics, etc.).
  279. ///
  280. /// As in other parts of the Unity graphics APIs random access buffers share the index-based slots with render targets and input attachments. See CommandBuffer.SetRandomWriteTarget for details.
  281. /// </summary>
  282. /// <param name="tex">Buffer to use during this pass.</param>
  283. /// <param name="index">Index the shader will use to access this texture. This is set in the shader through the `register(ux)` keyword.</param>
  284. /// <param name="flags">How this pass will access the buffer. Default value is set to AccessFlag.Read.</param>
  285. /// <returns>The value passed to 'input'. You should not use the returned value it will be removed in the future.</returns>
  286. BufferHandle UseBufferRandomAccess(BufferHandle tex, int index, AccessFlags flags = AccessFlags.Read);
  287. /// <summary>
  288. /// Use the buffer as an random access attachment. This is called "Unordered Access View" in DX12 and "Storage Buffer" in Vulkan.
  289. ///
  290. /// This informs the graph that any shaders in the pass will access the buffer as a random access attachment through RWStructuredBuffer, RWByteAddressBuffer,...
  291. /// The buffer can then be read/written by regular HLSL commands (including atomics, etc.).
  292. ///
  293. /// As in other parts of the Unity graphics APIs random access buffers share the index-based slots with render targets and input attachments. See CommandBuffer.SetRandomWriteTarget for details.
  294. /// </summary>
  295. /// <param name="tex">Buffer to use during this pass.</param>
  296. /// <param name="index">Index the shader will use to access this texture. This is set in the shader through the `register(ux)` keyword.</param>
  297. /// <param name="preserveCounterValue">Whether to leave the append/consume counter value unchanged. The default is to preserve the value.</param>
  298. /// <param name="flags">How this pass will access the buffer. Default value is set to AccessFlag.Read.</param>
  299. /// <returns>The value passed to 'input'. You should not use the returned value it will be removed in the future.</returns>
  300. BufferHandle UseBufferRandomAccess(BufferHandle tex, int index, bool preserveCounterValue, AccessFlags flags = AccessFlags.Read);
  301. /// <summary>
  302. /// Specify the render function to use for this pass.
  303. /// A call to this is mandatory for the pass to be valid.
  304. /// </summary>
  305. /// <typeparam name="PassData">The Type of the class that provides data to the Render Pass.</typeparam>
  306. /// <param name="renderFunc">Render function for the pass.</param>
  307. public void SetRenderFunc<PassData>(BaseRenderFunc<PassData, RasterGraphContext> renderFunc)
  308. where PassData : class, new();
  309. }
  310. }