CommandBuffer.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/GrObject.h>
  7. #include <AnKi/Gr/Framebuffer.h>
  8. #include <AnKi/Util/Functions.h>
  9. namespace anki
  10. {
  11. /// @addtogroup graphics
  12. /// @{
  13. /// The draw indirect structure for index drawing, also the parameters of a regular drawcall
  14. class DrawElementsIndirectInfo
  15. {
  16. public:
  17. U32 m_count = MAX_U32;
  18. U32 m_instanceCount = 1;
  19. U32 m_firstIndex = 0;
  20. U32 m_baseVertex = 0;
  21. U32 m_baseInstance = 0;
  22. DrawElementsIndirectInfo() = default;
  23. DrawElementsIndirectInfo(const DrawElementsIndirectInfo&) = default;
  24. DrawElementsIndirectInfo(U32 count, U32 instanceCount, U32 firstIndex, U32 baseVertex, U32 baseInstance)
  25. : m_count(count)
  26. , m_instanceCount(instanceCount)
  27. , m_firstIndex(firstIndex)
  28. , m_baseVertex(baseVertex)
  29. , m_baseInstance(baseInstance)
  30. {
  31. }
  32. Bool operator==(const DrawElementsIndirectInfo& b) const
  33. {
  34. return m_count == b.m_count && m_instanceCount == b.m_instanceCount && m_firstIndex == b.m_firstIndex
  35. && m_baseVertex == b.m_baseVertex && m_baseInstance == b.m_baseInstance;
  36. }
  37. Bool operator!=(const DrawElementsIndirectInfo& b) const
  38. {
  39. return !(operator==(b));
  40. }
  41. };
  42. /// The draw indirect structure for arrays drawing, also the parameters of a regular drawcall
  43. class DrawArraysIndirectInfo
  44. {
  45. public:
  46. U32 m_count = MAX_U32;
  47. U32 m_instanceCount = 1;
  48. U32 m_first = 0;
  49. U32 m_baseInstance = 0;
  50. DrawArraysIndirectInfo() = default;
  51. DrawArraysIndirectInfo(const DrawArraysIndirectInfo&) = default;
  52. DrawArraysIndirectInfo(U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  53. : m_count(count)
  54. , m_instanceCount(instanceCount)
  55. , m_first(first)
  56. , m_baseInstance(baseInstance)
  57. {
  58. }
  59. Bool operator==(const DrawArraysIndirectInfo& b) const
  60. {
  61. return m_count == b.m_count && m_instanceCount == b.m_instanceCount && m_first == b.m_first
  62. && m_baseInstance == b.m_baseInstance;
  63. }
  64. Bool operator!=(const DrawArraysIndirectInfo& b) const
  65. {
  66. return !(operator==(b));
  67. }
  68. };
  69. /// Command buffer initialization flags.
  70. enum class CommandBufferFlag : U8
  71. {
  72. NONE = 0,
  73. SECOND_LEVEL = 1 << 0,
  74. /// It will contain a handfull of commands.
  75. SMALL_BATCH = 1 << 3,
  76. /// Will contain graphics work.
  77. GRAPHICS_WORK = 1 << 4,
  78. /// Will contain transfer commands.
  79. TRANSFER_WORK = 1 << 5,
  80. /// Will contain compute work.
  81. COMPUTE_WORK = 1 << 6,
  82. };
  83. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(CommandBufferFlag)
  84. /// Command buffer init info.
  85. class CommandBufferInitInfo : public GrBaseInitInfo
  86. {
  87. public:
  88. FramebufferPtr m_framebuffer; ///< For second level command buffers.
  89. Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS> m_colorAttachmentUsages{};
  90. TextureUsageBit m_depthStencilAttachmentUsage = TextureUsageBit::NONE;
  91. CommandBufferFlag m_flags = CommandBufferFlag::NONE;
  92. CommandBufferInitInfo(CString name = {})
  93. : GrBaseInitInfo(name)
  94. {
  95. }
  96. };
  97. /// Command buffer.
  98. class CommandBuffer : public GrObject
  99. {
  100. ANKI_GR_OBJECT
  101. public:
  102. static const GrObjectType CLASS_TYPE = GrObjectType::COMMAND_BUFFER;
  103. /// Finalize and submit if it's primary command buffer and just finalize if it's second level.
  104. /// @param[out] fence Optionaly create fence.
  105. void flush(FencePtr* fence = nullptr);
  106. /// @name State manipulation
  107. /// @{
  108. /// Bind vertex buffer.
  109. void bindVertexBuffer(U32 binding, BufferPtr buff, PtrSize offset, PtrSize stride,
  110. VertexStepRate stepRate = VertexStepRate::VERTEX);
  111. /// Setup a vertex attribute.
  112. void setVertexAttribute(U32 location, U32 buffBinding, Format fmt, PtrSize relativeOffset);
  113. /// Bind index buffer.
  114. void bindIndexBuffer(BufferPtr buff, PtrSize offset, IndexType type);
  115. /// Enable primitive restart.
  116. void setPrimitiveRestart(Bool enable);
  117. /// Set the viewport.
  118. void setViewport(U32 minx, U32 miny, U32 width, U32 height);
  119. /// Set the scissor rect. To disable the scissor just set a rect bigger than the viewport. By default it's disabled.
  120. void setScissor(U32 minx, U32 miny, U32 width, U32 height);
  121. /// Set fill mode.
  122. void setFillMode(FillMode mode);
  123. /// Set cull mode.
  124. /// By default it's FaceSelectionBit::BACK.
  125. void setCullMode(FaceSelectionBit mode);
  126. /// Set depth offset and units. Set zeros to both to disable it.
  127. void setPolygonOffset(F32 factor, F32 units);
  128. /// Set stencil operations. To disable stencil test put StencilOperation::KEEP to all operations.
  129. void setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail,
  130. StencilOperation stencilPassDepthFail, StencilOperation stencilPassDepthPass);
  131. /// Set stencil compare operation.
  132. void setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp);
  133. /// Set the stencil compare mask.
  134. void setStencilCompareMask(FaceSelectionBit face, U32 mask);
  135. /// Set the stencil write mask.
  136. void setStencilWriteMask(FaceSelectionBit face, U32 mask);
  137. /// Set the stencil reference.
  138. void setStencilReference(FaceSelectionBit face, U32 ref);
  139. /// Enable/disable depth write. To disable depth testing set depth write to false and depth compare operation to
  140. /// always.
  141. void setDepthWrite(Bool enable);
  142. /// Set depth compare operation.
  143. void setDepthCompareOperation(CompareOperation op);
  144. /// Enable/disable alpha to coverage.
  145. void setAlphaToCoverage(Bool enable);
  146. /// Set color channel write mask.
  147. void setColorChannelWriteMask(U32 attachment, ColorBit mask);
  148. /// Set blend factors seperate.
  149. /// By default the values of srcRgb, dstRgb, srcA and dstA are BlendFactor::ONE, BlendFactor::ZERO,
  150. /// BlendFactor::ONE, BlendFactor::ZERO respectively.
  151. void setBlendFactors(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA, BlendFactor dstA);
  152. /// Set blend factors.
  153. void setBlendFactors(U32 attachment, BlendFactor src, BlendFactor dst)
  154. {
  155. setBlendFactors(attachment, src, dst, src, dst);
  156. }
  157. /// Set the blend operation seperate.
  158. /// By default the values of funcRgb and funcA are BlendOperation::ADD, BlendOperation::ADD respectively.
  159. void setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA);
  160. /// Set the blend operation.
  161. void setBlendOperation(U32 attachment, BlendOperation func)
  162. {
  163. setBlendOperation(attachment, func, func);
  164. }
  165. /// Set the rasterizatin order. By default it's RasterizationOrder::ORDERED.
  166. void setRasterizationOrder(RasterizationOrder order);
  167. /// Set the line width. By default it's undefined.
  168. void setLineWidth(F32 lineWidth);
  169. /// Bind texture and sample.
  170. /// @param set The set to bind to.
  171. /// @param binding The binding to bind to.
  172. /// @param texView The texture view to bind.
  173. /// @param sampler The sampler to override the default sampler of the tex.
  174. /// @param usage The state the tex is in.
  175. /// @param arrayIdx The array index if the binding is an array.
  176. void bindTextureAndSampler(U32 set, U32 binding, TextureViewPtr texView, SamplerPtr sampler, TextureUsageBit usage,
  177. U32 arrayIdx = 0);
  178. /// Bind sampler.
  179. /// @param set The set to bind to.
  180. /// @param binding The binding to bind to.
  181. /// @param sampler The sampler to override the default sampler of the tex.
  182. /// @param arrayIdx The array index if the binding is an array.
  183. void bindSampler(U32 set, U32 binding, SamplerPtr sampler, U32 arrayIdx = 0);
  184. /// Bind a texture.
  185. /// @param set The set to bind to.
  186. /// @param binding The binding to bind to.
  187. /// @param texView The texture view to bind.
  188. /// @param usage The state the tex is in.
  189. /// @param arrayIdx The array index if the binding is an array.
  190. void bindTexture(U32 set, U32 binding, TextureViewPtr texView, TextureUsageBit usage, U32 arrayIdx = 0);
  191. /// Bind uniform buffer.
  192. /// @param set The set to bind to.
  193. /// @param binding The binding to bind to.
  194. /// @param[in,out] buff The buffer to bind.
  195. /// @param offset The base of the binding.
  196. /// @param range The bytes to bind starting from the offset. If it's MAX_PTR_SIZE then map from offset to the end
  197. /// of the buffer.
  198. /// @param arrayIdx The array index if the binding is an array.
  199. void bindUniformBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, U32 arrayIdx = 0);
  200. /// Bind storage buffer.
  201. /// @param set The set to bind to.
  202. /// @param binding The binding to bind to.
  203. /// @param[in,out] buff The buffer to bind.
  204. /// @param offset The base of the binding.
  205. /// @param range The bytes to bind starting from the offset. If it's MAX_PTR_SIZE then map from offset to the end
  206. /// of the buffer.
  207. /// @param arrayIdx The array index if the binding is an array.
  208. void bindStorageBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, U32 arrayIdx = 0);
  209. /// Bind load/store image.
  210. /// @param set The set to bind to.
  211. /// @param binding The binding to bind to.
  212. /// @param img The view to bind.
  213. /// @param arrayIdx The array index if the binding is an array.
  214. void bindImage(U32 set, U32 binding, TextureViewPtr img, U32 arrayIdx = 0);
  215. /// Bind texture buffer.
  216. /// @param set The set to bind to.
  217. /// @param binding The binding to bind to.
  218. /// @param[in,out] buff The buffer to bind.
  219. /// @param offset The base of the binding.
  220. /// @param range The bytes to bind starting from the offset. If it's MAX_PTR_SIZE then map from offset to the end
  221. /// of the buffer.
  222. /// @param fmt The format of the buffer.
  223. /// @param arrayIdx The array index if the binding is an array.
  224. void bindTextureBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, Format fmt,
  225. U32 arrayIdx = 0);
  226. /// Bind an acceleration structure.
  227. /// @param set The set to bind to.
  228. /// @param binding The binding to bind to.
  229. /// @param[in,out] as The AS to bind.
  230. /// @param arrayIdx The array index if the binding is an array.
  231. void bindAccelerationStructure(U32 set, U32 binding, AccelerationStructurePtr as, U32 arrayIdx = 0);
  232. /// Bind the bindless descriptor set into a slot.
  233. void bindAllBindless(U32 set);
  234. /// Set push constants.
  235. void setPushConstants(const void* data, U32 dataSize);
  236. /// Bind a program.
  237. void bindShaderProgram(ShaderProgramPtr prog);
  238. /// Begin renderpass.
  239. /// The minx, miny, width, height control the area that the load and store operations will happen. If the scissor is
  240. /// bigger than the render area the results are undefined.
  241. void beginRenderPass(FramebufferPtr fb, const Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS>& colorAttachmentUsages,
  242. TextureUsageBit depthStencilAttachmentUsage, U32 minx = 0, U32 miny = 0, U32 width = MAX_U32,
  243. U32 height = MAX_U32);
  244. /// End renderpass.
  245. void endRenderPass();
  246. /// @}
  247. /// @name Jobs
  248. /// @{
  249. void drawElements(PrimitiveTopology topology, U32 count, U32 instanceCount = 1, U32 firstIndex = 0,
  250. U32 baseVertex = 0, U32 baseInstance = 0);
  251. void drawArrays(PrimitiveTopology topology, U32 count, U32 instanceCount = 1, U32 first = 0, U32 baseInstance = 0);
  252. void drawElementsIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, BufferPtr indirectBuff);
  253. void drawArraysIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, BufferPtr indirectBuff);
  254. void dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ);
  255. /// Trace rays.
  256. ///
  257. /// The 1st thing in the sbtBuffer is the ray gen shader group handle:
  258. /// @code RG = RG_offset @endcode
  259. /// The RG_offset is equal to the stbBufferOffset.
  260. ///
  261. /// Then the sbtBuffer contains the miss shader group handles and their data. The indexing is as follows:
  262. /// @code M = M_offset + M_stride * R_miss @endcode
  263. /// The M_offset is equal to stbBufferOffset + GpuDeviceCapabilities::m_sbtRecordSize.
  264. /// The M_stride is equal to GpuDeviceCapabilities::m_sbtRecordSize.
  265. /// The R_miss is defined in the traceRayEXT and it's the "ray type".
  266. ///
  267. /// After the miss shaders the sbtBuffer has the hit group shader group handles and their data. The indexing is:
  268. /// @code HG = HG_offset + (HG_stride * (R_offset + R_stride * G_id + I_offset)) @endcode
  269. /// The HG_offset is equal to sbtBufferOffset + GpuDeviceCapabilities::m_sbtRecordSize * (missShaderCount + 1).
  270. /// The HG_stride is equal GpuDeviceCapabilities::m_sbtRecordSize * rayTypecount.
  271. /// The R_offset and R_stride are provided in traceRayEXT. The R_offset is the "ray type" and R_stride the number of
  272. /// ray types.
  273. /// The G_id is always 0 ATM.
  274. /// The I_offset is the AccelerationStructureInstance::m_hitgroupSbtRecordIndex.
  275. ///
  276. /// @param[in] sbtBuffer The SBT buffer.
  277. /// @param sbtBufferOffset Offset inside the sbtBuffer where SBT records start.
  278. /// @param hitGroupSbtRecordCount The number of SBT records that contain hit groups.
  279. /// @param sbtRecordSize The size of an SBT record
  280. /// @param rayTypecount The number of ray types hosted in the pipeline. See above on how it's been used.
  281. /// @param width Width.
  282. /// @param height Height.
  283. /// @param depth Depth.
  284. void traceRays(BufferPtr sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize, U32 hitGroupSbtRecordCount,
  285. U32 rayTypeCount, U32 width, U32 height, U32 depth);
  286. /// Generate mipmaps for non-3D textures. You have to transition all the mip levels of this face and layer to
  287. /// TextureUsageBit::GENERATE_MIPMAPS before calling this method.
  288. /// @param texView The texture view to generate mips. It should point to a subresource that contains the whole
  289. /// mip chain and only one face and one layer.
  290. void generateMipmaps2d(TextureViewPtr texView);
  291. /// Generate mipmaps only for 3D textures.
  292. /// @param texView The texture view to generate mips.
  293. void generateMipmaps3d(TextureViewPtr tex);
  294. /// Blit from surface to surface.
  295. /// @param srcView The source view that points to a surface.
  296. /// @param dstView The destination view that points to a surface.
  297. void blitTextureViews(TextureViewPtr srcView, TextureViewPtr destView);
  298. /// Clear a single texture surface. Can be used for all textures except 3D.
  299. /// @param[in,out] texView The texture view to clear.
  300. /// @param[in] clearValue The value to clear it with.
  301. void clearTextureView(TextureViewPtr texView, const ClearValue& clearValue);
  302. /// Copy a buffer to a texture surface or volume.
  303. /// @param buff The source buffer to copy from.
  304. /// @param offset The offset in the buffer to start reading from.
  305. /// @param range The size of the buffer to read.
  306. /// @param texView The texture view that points to a surface or volume to write to.
  307. void copyBufferToTextureView(BufferPtr buff, PtrSize offset, PtrSize range, TextureViewPtr texView);
  308. /// Fill a buffer with some value.
  309. /// @param[in,out] buff The buffer to fill.
  310. /// @param offset From where to start filling. Must be multiple of 4.
  311. /// @param size The bytes to fill. Must be multiple of 4 or MAX_PTR_SIZE to indicate the whole buffer.
  312. /// @param value The value to fill the buffer with.
  313. void fillBuffer(BufferPtr buff, PtrSize offset, PtrSize size, U32 value);
  314. /// Write the occlusion result to buffer.
  315. /// @param[in] query The query to get the result from.
  316. /// @param offset The offset inside the buffer to write the result.
  317. /// @param buff The buffer to update.
  318. void writeOcclusionQueryResultToBuffer(OcclusionQueryPtr query, PtrSize offset, BufferPtr buff);
  319. /// Copy buffer to buffer.
  320. /// @param[in] src Source buffer.
  321. /// @param srcOffset Offset in the src buffer.
  322. /// @param[out] dst Destination buffer.
  323. /// @param dstOffset Offset in the destination buffer.
  324. /// @param range Size to copy.
  325. void copyBufferToBuffer(BufferPtr src, PtrSize srcOffset, BufferPtr dst, PtrSize dstOffset, PtrSize range);
  326. /// Build the acceleration structure.
  327. void buildAccelerationStructure(AccelerationStructurePtr as);
  328. /// @}
  329. /// @name Sync
  330. /// @{
  331. void setTextureBarrier(TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  332. const TextureSubresourceInfo& subresource);
  333. void setTextureSurfaceBarrier(TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  334. const TextureSurfaceInfo& surf);
  335. void setTextureVolumeBarrier(TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  336. const TextureVolumeInfo& vol);
  337. void setBufferBarrier(BufferPtr buff, BufferUsageBit prevUsage, BufferUsageBit nextUsage, PtrSize offset,
  338. PtrSize size);
  339. void setAccelerationStructureBarrier(AccelerationStructurePtr as, AccelerationStructureUsageBit prevUsage,
  340. AccelerationStructureUsageBit nextUsage);
  341. /// @}
  342. /// @name Other
  343. /// @{
  344. /// Reset query before beginOcclusionQuery.
  345. void resetOcclusionQuery(OcclusionQueryPtr query);
  346. /// Begin query.
  347. void beginOcclusionQuery(OcclusionQueryPtr query);
  348. /// End query.
  349. void endOcclusionQuery(OcclusionQueryPtr query);
  350. /// Reset timestamp query before writeTimestamp.
  351. void resetTimestampQuery(TimestampQueryPtr query);
  352. /// Write a timestamp.
  353. void writeTimestamp(TimestampQueryPtr query);
  354. /// Append a second level command buffer.
  355. void pushSecondLevelCommandBuffer(CommandBufferPtr cmdb);
  356. Bool isEmpty() const;
  357. /// The command buffer will co-own a pointer. Useful to track the lifetime of bindless resources.
  358. void addReference(GrObjectPtr ptr);
  359. /// @}
  360. protected:
  361. /// Construct.
  362. CommandBuffer(GrManager* manager, CString name)
  363. : GrObject(manager, CLASS_TYPE, name)
  364. {
  365. }
  366. /// Destroy.
  367. ~CommandBuffer()
  368. {
  369. }
  370. private:
  371. /// Allocate and initialize new instance.
  372. static ANKI_USE_RESULT CommandBuffer* newInstance(GrManager* manager, const CommandBufferInitInfo& init);
  373. };
  374. /// @}
  375. } // end namespace anki