CommandBuffer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/CommandBuffer.h>
  6. #include <AnKi/Gr/Vulkan/CommandBufferImpl.h>
  7. #include <AnKi/Gr/Vulkan/GrManagerImpl.h>
  8. #include <AnKi/Gr/AccelerationStructure.h>
  9. #include <AnKi/Gr/Vulkan/FenceImpl.h>
  10. namespace anki {
  11. CommandBuffer* CommandBuffer::newInstance(const CommandBufferInitInfo& init)
  12. {
  13. ANKI_TRACE_SCOPED_EVENT(VkNewCommandBuffer);
  14. CommandBufferImpl* impl = anki::newInstance<CommandBufferImpl>(GrMemoryPool::getSingleton(), init.getName());
  15. const Error err = impl->init(init);
  16. if(err)
  17. {
  18. deleteInstance(GrMemoryPool::getSingleton(), impl);
  19. impl = nullptr;
  20. }
  21. return impl;
  22. }
  23. void CommandBuffer::flush(ConstWeakArray<FencePtr> waitFences, FencePtr* signalFence)
  24. {
  25. ANKI_VK_SELF(CommandBufferImpl);
  26. self.endRecording();
  27. if(!self.isSecondLevel())
  28. {
  29. Array<MicroSemaphorePtr, 8> waitSemaphores;
  30. for(U32 i = 0; i < waitFences.getSize(); ++i)
  31. {
  32. waitSemaphores[i] = static_cast<const FenceImpl&>(*waitFences[i]).m_semaphore;
  33. }
  34. MicroSemaphorePtr signalSemaphore;
  35. getGrManagerImpl().flushCommandBuffer(self.getMicroCommandBuffer(), self.renderedToDefaultFramebuffer(),
  36. WeakArray<MicroSemaphorePtr>(waitSemaphores.getBegin(), waitFences.getSize()),
  37. (signalFence) ? &signalSemaphore : nullptr);
  38. if(signalFence)
  39. {
  40. FenceImpl* fenceImpl = anki::newInstance<FenceImpl>(GrMemoryPool::getSingleton(), "SignalFence");
  41. fenceImpl->m_semaphore = signalSemaphore;
  42. signalFence->reset(fenceImpl);
  43. }
  44. }
  45. else
  46. {
  47. ANKI_ASSERT(signalFence == nullptr);
  48. ANKI_ASSERT(waitFences.getSize() == 0);
  49. }
  50. }
  51. void CommandBuffer::bindVertexBuffer(U32 binding, Buffer* buff, PtrSize offset, PtrSize stride, VertexStepRate stepRate)
  52. {
  53. ANKI_VK_SELF(CommandBufferImpl);
  54. self.bindVertexBufferInternal(binding, buff, offset, stride, stepRate);
  55. }
  56. void CommandBuffer::setVertexAttribute(U32 location, U32 buffBinding, Format fmt, PtrSize relativeOffset)
  57. {
  58. ANKI_VK_SELF(CommandBufferImpl);
  59. self.setVertexAttributeInternal(location, buffBinding, fmt, relativeOffset);
  60. }
  61. void CommandBuffer::bindIndexBuffer(Buffer* buff, PtrSize offset, IndexType type)
  62. {
  63. ANKI_VK_SELF(CommandBufferImpl);
  64. self.bindIndexBufferInternal(buff, offset, type);
  65. }
  66. void CommandBuffer::setPrimitiveRestart(Bool enable)
  67. {
  68. ANKI_VK_SELF(CommandBufferImpl);
  69. self.setPrimitiveRestartInternal(enable);
  70. }
  71. void CommandBuffer::setViewport(U32 minx, U32 miny, U32 width, U32 height)
  72. {
  73. ANKI_VK_SELF(CommandBufferImpl);
  74. self.setViewportInternal(minx, miny, width, height);
  75. }
  76. void CommandBuffer::setScissor(U32 minx, U32 miny, U32 width, U32 height)
  77. {
  78. ANKI_VK_SELF(CommandBufferImpl);
  79. self.setScissorInternal(minx, miny, width, height);
  80. }
  81. void CommandBuffer::setFillMode(FillMode mode)
  82. {
  83. ANKI_VK_SELF(CommandBufferImpl);
  84. self.setFillModeInternal(mode);
  85. }
  86. void CommandBuffer::setCullMode(FaceSelectionBit mode)
  87. {
  88. ANKI_VK_SELF(CommandBufferImpl);
  89. self.setCullModeInternal(mode);
  90. }
  91. void CommandBuffer::setPolygonOffset(F32 factor, F32 units)
  92. {
  93. ANKI_VK_SELF(CommandBufferImpl);
  94. self.setPolygonOffsetInternal(factor, units);
  95. }
  96. void CommandBuffer::setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail, StencilOperation stencilPassDepthFail,
  97. StencilOperation stencilPassDepthPass)
  98. {
  99. ANKI_VK_SELF(CommandBufferImpl);
  100. self.setStencilOperationsInternal(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  101. }
  102. void CommandBuffer::setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  103. {
  104. ANKI_VK_SELF(CommandBufferImpl);
  105. self.setStencilCompareOperationInternal(face, comp);
  106. }
  107. void CommandBuffer::setStencilCompareMask(FaceSelectionBit face, U32 mask)
  108. {
  109. ANKI_VK_SELF(CommandBufferImpl);
  110. self.setStencilCompareMaskInternal(face, mask);
  111. }
  112. void CommandBuffer::setStencilWriteMask(FaceSelectionBit face, U32 mask)
  113. {
  114. ANKI_VK_SELF(CommandBufferImpl);
  115. self.setStencilWriteMaskInternal(face, mask);
  116. }
  117. void CommandBuffer::setStencilReference(FaceSelectionBit face, U32 ref)
  118. {
  119. ANKI_VK_SELF(CommandBufferImpl);
  120. self.setStencilReferenceInternal(face, ref);
  121. }
  122. void CommandBuffer::setDepthWrite(Bool enable)
  123. {
  124. ANKI_VK_SELF(CommandBufferImpl);
  125. self.setDepthWriteInternal(enable);
  126. }
  127. void CommandBuffer::setDepthCompareOperation(CompareOperation op)
  128. {
  129. ANKI_VK_SELF(CommandBufferImpl);
  130. self.setDepthCompareOperationInternal(op);
  131. }
  132. void CommandBuffer::setAlphaToCoverage(Bool enable)
  133. {
  134. ANKI_VK_SELF(CommandBufferImpl);
  135. self.setAlphaToCoverageInternal(enable);
  136. }
  137. void CommandBuffer::setColorChannelWriteMask(U32 attachment, ColorBit mask)
  138. {
  139. ANKI_VK_SELF(CommandBufferImpl);
  140. self.setColorChannelWriteMaskInternal(attachment, mask);
  141. }
  142. void CommandBuffer::setBlendFactors(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA, BlendFactor dstA)
  143. {
  144. ANKI_VK_SELF(CommandBufferImpl);
  145. self.setBlendFactorsInternal(attachment, srcRgb, dstRgb, srcA, dstA);
  146. }
  147. void CommandBuffer::setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  148. {
  149. ANKI_VK_SELF(CommandBufferImpl);
  150. self.setBlendOperationInternal(attachment, funcRgb, funcA);
  151. }
  152. void CommandBuffer::bindTextureAndSampler(U32 set, U32 binding, TextureView* texView, Sampler* sampler, U32 arrayIdx)
  153. {
  154. ANKI_VK_SELF(CommandBufferImpl);
  155. self.bindTextureAndSamplerInternal(set, binding, texView, sampler, arrayIdx);
  156. }
  157. void CommandBuffer::bindTexture(U32 set, U32 binding, TextureView* texView, U32 arrayIdx)
  158. {
  159. ANKI_VK_SELF(CommandBufferImpl);
  160. self.bindTextureInternal(set, binding, texView, arrayIdx);
  161. }
  162. void CommandBuffer::bindSampler(U32 set, U32 binding, Sampler* sampler, U32 arrayIdx)
  163. {
  164. ANKI_VK_SELF(CommandBufferImpl);
  165. self.bindSamplerInternal(set, binding, sampler, arrayIdx);
  166. }
  167. void CommandBuffer::bindUniformBuffer(U32 set, U32 binding, Buffer* buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  168. {
  169. ANKI_VK_SELF(CommandBufferImpl);
  170. self.bindUniformBufferInternal(set, binding, buff, offset, range, arrayIdx);
  171. }
  172. void CommandBuffer::bindStorageBuffer(U32 set, U32 binding, Buffer* buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  173. {
  174. ANKI_VK_SELF(CommandBufferImpl);
  175. self.bindStorageBufferInternal(set, binding, buff, offset, range, arrayIdx);
  176. }
  177. void CommandBuffer::bindImage(U32 set, U32 binding, TextureView* img, U32 arrayIdx)
  178. {
  179. ANKI_VK_SELF(CommandBufferImpl);
  180. self.bindImageInternal(set, binding, img, arrayIdx);
  181. }
  182. void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, AccelerationStructure* as, U32 arrayIdx)
  183. {
  184. ANKI_VK_SELF(CommandBufferImpl);
  185. self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
  186. }
  187. void CommandBuffer::bindReadOnlyTextureBuffer(U32 set, U32 binding, Buffer* buff, PtrSize offset, PtrSize range, Format fmt, U32 arrayIdx)
  188. {
  189. ANKI_VK_SELF(CommandBufferImpl);
  190. self.bindReadOnlyTextureBufferInternal(set, binding, buff, offset, range, fmt, arrayIdx);
  191. }
  192. void CommandBuffer::bindAllBindless(U32 set)
  193. {
  194. ANKI_VK_SELF(CommandBufferImpl);
  195. self.bindAllBindlessInternal(set);
  196. }
  197. void CommandBuffer::bindShaderProgram(ShaderProgram* prog)
  198. {
  199. ANKI_VK_SELF(CommandBufferImpl);
  200. self.bindShaderProgramInternal(prog);
  201. }
  202. void CommandBuffer::beginRenderPass(Framebuffer* fb, const Array<TextureUsageBit, kMaxColorRenderTargets>& colorAttachmentUsages,
  203. TextureUsageBit depthStencilAttachmentUsage, U32 minx, U32 miny, U32 width, U32 height)
  204. {
  205. ANKI_VK_SELF(CommandBufferImpl);
  206. self.beginRenderPassInternal(fb, colorAttachmentUsages, depthStencilAttachmentUsage, minx, miny, width, height);
  207. }
  208. void CommandBuffer::endRenderPass()
  209. {
  210. ANKI_VK_SELF(CommandBufferImpl);
  211. self.endRenderPassInternal();
  212. }
  213. void CommandBuffer::setVrsRate(VrsRate rate)
  214. {
  215. ANKI_VK_SELF(CommandBufferImpl);
  216. self.setVrsRateInternal(rate);
  217. }
  218. void CommandBuffer::drawIndexed(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex, U32 baseVertex, U32 baseInstance)
  219. {
  220. ANKI_VK_SELF(CommandBufferImpl);
  221. self.drawIndexedInternal(topology, count, instanceCount, firstIndex, baseVertex, baseInstance);
  222. }
  223. void CommandBuffer::draw(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  224. {
  225. ANKI_VK_SELF(CommandBufferImpl);
  226. self.drawInternal(topology, count, instanceCount, first, baseInstance);
  227. }
  228. void CommandBuffer::drawIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, Buffer* buff)
  229. {
  230. ANKI_VK_SELF(CommandBufferImpl);
  231. self.drawIndirectInternal(topology, drawCount, offset, buff);
  232. }
  233. void CommandBuffer::drawIndexedIndirectCount(PrimitiveTopology topology, Buffer* argBuffer, PtrSize argBufferOffset, U32 argBufferStride,
  234. Buffer* countBuffer, PtrSize countBufferOffset, U32 maxDrawCount)
  235. {
  236. ANKI_VK_SELF(CommandBufferImpl);
  237. self.drawIndexedIndirectCountInternal(topology, argBuffer, argBufferOffset, argBufferStride, countBuffer, countBufferOffset, maxDrawCount);
  238. }
  239. void CommandBuffer::drawIndirectCount(PrimitiveTopology topology, Buffer* argBuffer, PtrSize argBufferOffset, U32 argBufferStride,
  240. Buffer* countBuffer, PtrSize countBufferOffset, U32 maxDrawCount)
  241. {
  242. ANKI_VK_SELF(CommandBufferImpl);
  243. self.drawIndirectCountInternal(topology, argBuffer, argBufferOffset, argBufferStride, countBuffer, countBufferOffset, maxDrawCount);
  244. }
  245. void CommandBuffer::drawIndexedIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, Buffer* buff)
  246. {
  247. ANKI_VK_SELF(CommandBufferImpl);
  248. self.drawIndexedIndirectInternal(topology, drawCount, offset, buff);
  249. }
  250. void CommandBuffer::drawMeshTasks(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  251. {
  252. ANKI_VK_SELF(CommandBufferImpl);
  253. self.drawMeshTasksInternal(groupCountX, groupCountY, groupCountZ);
  254. }
  255. void CommandBuffer::dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  256. {
  257. ANKI_VK_SELF(CommandBufferImpl);
  258. self.dispatchComputeInternal(groupCountX, groupCountY, groupCountZ);
  259. }
  260. void CommandBuffer::dispatchComputeIndirect(Buffer* argBuffer, PtrSize argBufferOffset)
  261. {
  262. ANKI_VK_SELF(CommandBufferImpl);
  263. self.dispatchComputeIndirectInternal(argBuffer, argBufferOffset);
  264. }
  265. void CommandBuffer::traceRays(Buffer* sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize, U32 hitGroupSbtRecordCount, U32 rayTypeCount, U32 width,
  266. U32 height, U32 depth)
  267. {
  268. ANKI_VK_SELF(CommandBufferImpl);
  269. self.traceRaysInternal(sbtBuffer, sbtBufferOffset, sbtRecordSize, hitGroupSbtRecordCount, rayTypeCount, width, height, depth);
  270. }
  271. void CommandBuffer::generateMipmaps2d(TextureView* texView)
  272. {
  273. ANKI_VK_SELF(CommandBufferImpl);
  274. self.generateMipmaps2dInternal(texView);
  275. }
  276. void CommandBuffer::generateMipmaps3d([[maybe_unused]] TextureView* texView)
  277. {
  278. ANKI_ASSERT(!"TODO");
  279. }
  280. void CommandBuffer::blitTextureViews([[maybe_unused]] TextureView* srcView, [[maybe_unused]] TextureView* destView)
  281. {
  282. ANKI_ASSERT(!"TODO");
  283. }
  284. void CommandBuffer::clearTextureView(TextureView* texView, const ClearValue& clearValue)
  285. {
  286. ANKI_VK_SELF(CommandBufferImpl);
  287. self.clearTextureViewInternal(texView, clearValue);
  288. }
  289. void CommandBuffer::copyBufferToTextureView(Buffer* buff, PtrSize offset, PtrSize range, TextureView* texView)
  290. {
  291. ANKI_VK_SELF(CommandBufferImpl);
  292. self.copyBufferToTextureViewInternal(buff, offset, range, texView);
  293. }
  294. void CommandBuffer::fillBuffer(Buffer* buff, PtrSize offset, PtrSize size, U32 value)
  295. {
  296. ANKI_VK_SELF(CommandBufferImpl);
  297. self.fillBufferInternal(buff, offset, size, value);
  298. }
  299. void CommandBuffer::writeOcclusionQueriesResultToBuffer(ConstWeakArray<OcclusionQuery*> queries, PtrSize offset, Buffer* buff)
  300. {
  301. ANKI_VK_SELF(CommandBufferImpl);
  302. self.writeOcclusionQueriesResultToBufferInternal(queries, offset, buff);
  303. }
  304. void CommandBuffer::copyBufferToBuffer(Buffer* src, Buffer* dst, ConstWeakArray<CopyBufferToBufferInfo> copies)
  305. {
  306. ANKI_VK_SELF(CommandBufferImpl);
  307. self.copyBufferToBufferInternal(src, dst, copies);
  308. }
  309. void CommandBuffer::buildAccelerationStructure(AccelerationStructure* as, Buffer* scratchBuffer, PtrSize scratchBufferOffset)
  310. {
  311. ANKI_VK_SELF(CommandBufferImpl);
  312. self.buildAccelerationStructureInternal(as, scratchBuffer, scratchBufferOffset);
  313. }
  314. void CommandBuffer::upscale(GrUpscaler* upscaler, TextureView* inColor, TextureView* outUpscaledColor, TextureView* motionVectors, TextureView* depth,
  315. TextureView* exposure, Bool resetAccumulation, const Vec2& jitterOffset, const Vec2& motionVectorsScale)
  316. {
  317. ANKI_VK_SELF(CommandBufferImpl);
  318. self.upscaleInternal(upscaler, inColor, outUpscaledColor, motionVectors, depth, exposure, resetAccumulation, jitterOffset, motionVectorsScale);
  319. }
  320. void CommandBuffer::setPipelineBarrier(ConstWeakArray<TextureBarrierInfo> textures, ConstWeakArray<BufferBarrierInfo> buffers,
  321. ConstWeakArray<AccelerationStructureBarrierInfo> accelerationStructures)
  322. {
  323. ANKI_VK_SELF(CommandBufferImpl);
  324. self.setPipelineBarrierInternal(textures, buffers, accelerationStructures);
  325. }
  326. void CommandBuffer::resetOcclusionQueries(ConstWeakArray<OcclusionQuery*> queries)
  327. {
  328. ANKI_VK_SELF(CommandBufferImpl);
  329. self.resetOcclusionQueriesInternal(queries);
  330. }
  331. void CommandBuffer::beginOcclusionQuery(OcclusionQuery* query)
  332. {
  333. ANKI_VK_SELF(CommandBufferImpl);
  334. self.beginOcclusionQueryInternal(query);
  335. }
  336. void CommandBuffer::endOcclusionQuery(OcclusionQuery* query)
  337. {
  338. ANKI_VK_SELF(CommandBufferImpl);
  339. self.endOcclusionQueryInternal(query);
  340. }
  341. void CommandBuffer::pushSecondLevelCommandBuffers(ConstWeakArray<CommandBuffer*> cmdbs)
  342. {
  343. ANKI_VK_SELF(CommandBufferImpl);
  344. self.pushSecondLevelCommandBuffersInternal(cmdbs);
  345. }
  346. void CommandBuffer::resetTimestampQueries(ConstWeakArray<TimestampQuery*> queries)
  347. {
  348. ANKI_VK_SELF(CommandBufferImpl);
  349. self.resetTimestampQueriesInternal(queries);
  350. }
  351. void CommandBuffer::writeTimestamp(TimestampQuery* query)
  352. {
  353. ANKI_VK_SELF(CommandBufferImpl);
  354. self.writeTimestampInternal(query);
  355. }
  356. Bool CommandBuffer::isEmpty() const
  357. {
  358. ANKI_VK_SELF_CONST(CommandBufferImpl);
  359. return self.isEmpty();
  360. }
  361. void CommandBuffer::setPushConstants(const void* data, U32 dataSize)
  362. {
  363. ANKI_VK_SELF(CommandBufferImpl);
  364. self.setPushConstantsInternal(data, dataSize);
  365. }
  366. void CommandBuffer::setRasterizationOrder(RasterizationOrder order)
  367. {
  368. ANKI_VK_SELF(CommandBufferImpl);
  369. self.setRasterizationOrderInternal(order);
  370. }
  371. void CommandBuffer::setLineWidth(F32 width)
  372. {
  373. ANKI_VK_SELF(CommandBufferImpl);
  374. self.setLineWidthInternal(width);
  375. }
  376. void CommandBuffer::pushDebugMarker(CString name, Vec3 color)
  377. {
  378. ANKI_VK_SELF(CommandBufferImpl);
  379. self.pushDebugMarkerInternal(name, color);
  380. }
  381. void CommandBuffer::popDebugMarker()
  382. {
  383. ANKI_VK_SELF(CommandBufferImpl);
  384. self.popDebugMarkerInternal();
  385. }
  386. } // end namespace anki