CommandBuffer.h 17 KB

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