CommandBuffer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // Copyright (C) 2009-2022, 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(GrManager* manager, const CommandBufferInitInfo& init)
  12. {
  13. ANKI_TRACE_SCOPED_EVENT(VK_NEW_CCOMMAND_BUFFER);
  14. CommandBufferImpl* impl = manager->getAllocator().newInstance<CommandBufferImpl>(manager, init.getName());
  15. const Error err = impl->init(init);
  16. if(err)
  17. {
  18. manager->getAllocator().deleteInstance(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. self.getGrManagerImpl().flushCommandBuffer(
  36. self.getMicroCommandBuffer(), self.renderedToDefaultFramebuffer(),
  37. WeakArray<MicroSemaphorePtr>(waitSemaphores.getBegin(), waitFences.getSize()),
  38. (signalFence) ? &signalSemaphore : nullptr);
  39. if(signalFence)
  40. {
  41. FenceImpl* fenceImpl =
  42. self.getGrManagerImpl().getAllocator().newInstance<FenceImpl>(&getManager(), "SignalFence");
  43. fenceImpl->m_semaphore = signalSemaphore;
  44. signalFence->reset(fenceImpl);
  45. }
  46. }
  47. else
  48. {
  49. ANKI_ASSERT(signalFence == nullptr);
  50. ANKI_ASSERT(waitFences.getSize() == 0);
  51. }
  52. }
  53. void CommandBuffer::bindVertexBuffer(U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize stride,
  54. VertexStepRate stepRate)
  55. {
  56. ANKI_VK_SELF(CommandBufferImpl);
  57. self.bindVertexBufferInternal(binding, buff, offset, stride, stepRate);
  58. }
  59. void CommandBuffer::setVertexAttribute(U32 location, U32 buffBinding, Format fmt, PtrSize relativeOffset)
  60. {
  61. ANKI_VK_SELF(CommandBufferImpl);
  62. self.setVertexAttributeInternal(location, buffBinding, fmt, relativeOffset);
  63. }
  64. void CommandBuffer::bindIndexBuffer(const BufferPtr& buff, PtrSize offset, IndexType type)
  65. {
  66. ANKI_VK_SELF(CommandBufferImpl);
  67. self.bindIndexBufferInternal(buff, offset, type);
  68. }
  69. void CommandBuffer::setPrimitiveRestart(Bool enable)
  70. {
  71. ANKI_VK_SELF(CommandBufferImpl);
  72. self.setPrimitiveRestartInternal(enable);
  73. }
  74. void CommandBuffer::setViewport(U32 minx, U32 miny, U32 width, U32 height)
  75. {
  76. ANKI_VK_SELF(CommandBufferImpl);
  77. self.setViewportInternal(minx, miny, width, height);
  78. }
  79. void CommandBuffer::setScissor(U32 minx, U32 miny, U32 width, U32 height)
  80. {
  81. ANKI_VK_SELF(CommandBufferImpl);
  82. self.setScissorInternal(minx, miny, width, height);
  83. }
  84. void CommandBuffer::setFillMode(FillMode mode)
  85. {
  86. ANKI_VK_SELF(CommandBufferImpl);
  87. self.setFillModeInternal(mode);
  88. }
  89. void CommandBuffer::setCullMode(FaceSelectionBit mode)
  90. {
  91. ANKI_VK_SELF(CommandBufferImpl);
  92. self.setCullModeInternal(mode);
  93. }
  94. void CommandBuffer::setPolygonOffset(F32 factor, F32 units)
  95. {
  96. ANKI_VK_SELF(CommandBufferImpl);
  97. self.setPolygonOffsetInternal(factor, units);
  98. }
  99. void CommandBuffer::setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail,
  100. StencilOperation stencilPassDepthFail, StencilOperation stencilPassDepthPass)
  101. {
  102. ANKI_VK_SELF(CommandBufferImpl);
  103. self.setStencilOperationsInternal(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  104. }
  105. void CommandBuffer::setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  106. {
  107. ANKI_VK_SELF(CommandBufferImpl);
  108. self.setStencilCompareOperationInternal(face, comp);
  109. }
  110. void CommandBuffer::setStencilCompareMask(FaceSelectionBit face, U32 mask)
  111. {
  112. ANKI_VK_SELF(CommandBufferImpl);
  113. self.setStencilCompareMaskInternal(face, mask);
  114. }
  115. void CommandBuffer::setStencilWriteMask(FaceSelectionBit face, U32 mask)
  116. {
  117. ANKI_VK_SELF(CommandBufferImpl);
  118. self.setStencilWriteMaskInternal(face, mask);
  119. }
  120. void CommandBuffer::setStencilReference(FaceSelectionBit face, U32 ref)
  121. {
  122. ANKI_VK_SELF(CommandBufferImpl);
  123. self.setStencilReferenceInternal(face, ref);
  124. }
  125. void CommandBuffer::setDepthWrite(Bool enable)
  126. {
  127. ANKI_VK_SELF(CommandBufferImpl);
  128. self.setDepthWriteInternal(enable);
  129. }
  130. void CommandBuffer::setDepthCompareOperation(CompareOperation op)
  131. {
  132. ANKI_VK_SELF(CommandBufferImpl);
  133. self.setDepthCompareOperationInternal(op);
  134. }
  135. void CommandBuffer::setAlphaToCoverage(Bool enable)
  136. {
  137. ANKI_VK_SELF(CommandBufferImpl);
  138. self.setAlphaToCoverageInternal(enable);
  139. }
  140. void CommandBuffer::setColorChannelWriteMask(U32 attachment, ColorBit mask)
  141. {
  142. ANKI_VK_SELF(CommandBufferImpl);
  143. self.setColorChannelWriteMaskInternal(attachment, mask);
  144. }
  145. void CommandBuffer::setBlendFactors(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA,
  146. BlendFactor dstA)
  147. {
  148. ANKI_VK_SELF(CommandBufferImpl);
  149. self.setBlendFactorsInternal(attachment, srcRgb, dstRgb, srcA, dstA);
  150. }
  151. void CommandBuffer::setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  152. {
  153. ANKI_VK_SELF(CommandBufferImpl);
  154. self.setBlendOperationInternal(attachment, funcRgb, funcA);
  155. }
  156. void CommandBuffer::bindTextureAndSampler(U32 set, U32 binding, const TextureViewPtr& texView,
  157. const SamplerPtr& sampler, U32 arrayIdx)
  158. {
  159. ANKI_VK_SELF(CommandBufferImpl);
  160. self.bindTextureAndSamplerInternal(set, binding, texView, sampler, arrayIdx);
  161. }
  162. void CommandBuffer::bindTexture(U32 set, U32 binding, const TextureViewPtr& texView, U32 arrayIdx)
  163. {
  164. ANKI_VK_SELF(CommandBufferImpl);
  165. self.bindTextureInternal(set, binding, texView, arrayIdx);
  166. }
  167. void CommandBuffer::bindSampler(U32 set, U32 binding, const SamplerPtr& sampler, U32 arrayIdx)
  168. {
  169. ANKI_VK_SELF(CommandBufferImpl);
  170. self.bindSamplerInternal(set, binding, sampler, arrayIdx);
  171. }
  172. void CommandBuffer::bindUniformBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range,
  173. U32 arrayIdx)
  174. {
  175. ANKI_VK_SELF(CommandBufferImpl);
  176. self.bindUniformBufferInternal(set, binding, buff, offset, range, arrayIdx);
  177. }
  178. void CommandBuffer::bindStorageBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset, PtrSize range,
  179. U32 arrayIdx)
  180. {
  181. ANKI_VK_SELF(CommandBufferImpl);
  182. self.bindStorageBufferInternal(set, binding, buff, offset, range, arrayIdx);
  183. }
  184. void CommandBuffer::bindImage(U32 set, U32 binding, const TextureViewPtr& img, U32 arrayIdx)
  185. {
  186. ANKI_VK_SELF(CommandBufferImpl);
  187. self.bindImageInternal(set, binding, img, arrayIdx);
  188. }
  189. void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, const AccelerationStructurePtr& as, U32 arrayIdx)
  190. {
  191. ANKI_VK_SELF(CommandBufferImpl);
  192. self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
  193. }
  194. void CommandBuffer::bindReadOnlyTextureBuffer(U32 set, U32 binding, const BufferPtr& buff, PtrSize offset,
  195. PtrSize range, Format fmt, U32 arrayIdx)
  196. {
  197. ANKI_VK_SELF(CommandBufferImpl);
  198. self.bindReadOnlyTextureBufferInternal(set, binding, buff, offset, range, fmt, arrayIdx);
  199. }
  200. void CommandBuffer::bindAllBindless(U32 set)
  201. {
  202. ANKI_VK_SELF(CommandBufferImpl);
  203. self.bindAllBindlessInternal(set);
  204. }
  205. void CommandBuffer::bindShaderProgram(const ShaderProgramPtr& prog)
  206. {
  207. ANKI_VK_SELF(CommandBufferImpl);
  208. self.bindShaderProgramInternal(prog);
  209. }
  210. void CommandBuffer::beginRenderPass(const FramebufferPtr& fb,
  211. const Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS>& colorAttachmentUsages,
  212. TextureUsageBit depthStencilAttachmentUsage, U32 minx, U32 miny, U32 width,
  213. U32 height)
  214. {
  215. ANKI_VK_SELF(CommandBufferImpl);
  216. self.beginRenderPassInternal(fb, colorAttachmentUsages, depthStencilAttachmentUsage, minx, miny, width, height);
  217. }
  218. void CommandBuffer::endRenderPass()
  219. {
  220. ANKI_VK_SELF(CommandBufferImpl);
  221. self.endRenderPassInternal();
  222. }
  223. void CommandBuffer::setVrsRate(VrsRate rate)
  224. {
  225. ANKI_VK_SELF(CommandBufferImpl);
  226. self.setVrsRateInternal(rate);
  227. }
  228. void CommandBuffer::drawElements(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex,
  229. U32 baseVertex, U32 baseInstance)
  230. {
  231. ANKI_VK_SELF(CommandBufferImpl);
  232. self.drawElementsInternal(topology, count, instanceCount, firstIndex, baseVertex, baseInstance);
  233. }
  234. void CommandBuffer::drawArrays(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  235. {
  236. ANKI_VK_SELF(CommandBufferImpl);
  237. self.drawArraysInternal(topology, count, instanceCount, first, baseInstance);
  238. }
  239. void CommandBuffer::drawArraysIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, const BufferPtr& buff)
  240. {
  241. ANKI_VK_SELF(CommandBufferImpl);
  242. self.drawArraysIndirectInternal(topology, drawCount, offset, buff);
  243. }
  244. void CommandBuffer::drawElementsIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset,
  245. const BufferPtr& buff)
  246. {
  247. ANKI_VK_SELF(CommandBufferImpl);
  248. self.drawElementsIndirectInternal(topology, drawCount, offset, buff);
  249. }
  250. void CommandBuffer::dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  251. {
  252. ANKI_VK_SELF(CommandBufferImpl);
  253. self.dispatchComputeInternal(groupCountX, groupCountY, groupCountZ);
  254. }
  255. void CommandBuffer::traceRays(const BufferPtr& sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize,
  256. U32 hitGroupSbtRecordCount, U32 rayTypeCount, U32 width, U32 height, U32 depth)
  257. {
  258. ANKI_VK_SELF(CommandBufferImpl);
  259. self.traceRaysInternal(sbtBuffer, sbtBufferOffset, sbtRecordSize, hitGroupSbtRecordCount, rayTypeCount, width,
  260. height, depth);
  261. }
  262. void CommandBuffer::generateMipmaps2d(const TextureViewPtr& texView)
  263. {
  264. ANKI_VK_SELF(CommandBufferImpl);
  265. self.generateMipmaps2dInternal(texView);
  266. }
  267. void CommandBuffer::generateMipmaps3d([[maybe_unused]] const TextureViewPtr& texView)
  268. {
  269. ANKI_ASSERT(!"TODO");
  270. }
  271. void CommandBuffer::blitTextureViews([[maybe_unused]] const TextureViewPtr& srcView,
  272. [[maybe_unused]] const TextureViewPtr& destView)
  273. {
  274. ANKI_ASSERT(!"TODO");
  275. }
  276. void CommandBuffer::clearTextureView(const TextureViewPtr& texView, const ClearValue& clearValue)
  277. {
  278. ANKI_VK_SELF(CommandBufferImpl);
  279. self.clearTextureViewInternal(texView, clearValue);
  280. }
  281. void CommandBuffer::copyBufferToTextureView(const BufferPtr& buff, PtrSize offset, PtrSize range,
  282. const TextureViewPtr& texView)
  283. {
  284. ANKI_VK_SELF(CommandBufferImpl);
  285. self.copyBufferToTextureViewInternal(buff, offset, range, texView);
  286. }
  287. void CommandBuffer::fillBuffer(const BufferPtr& buff, PtrSize offset, PtrSize size, U32 value)
  288. {
  289. ANKI_VK_SELF(CommandBufferImpl);
  290. self.fillBufferInternal(buff, offset, size, value);
  291. }
  292. void CommandBuffer::writeOcclusionQueryResultToBuffer(const OcclusionQueryPtr& query, PtrSize offset,
  293. const BufferPtr& buff)
  294. {
  295. ANKI_VK_SELF(CommandBufferImpl);
  296. self.writeOcclusionQueryResultToBufferInternal(query, offset, buff);
  297. }
  298. void CommandBuffer::copyBufferToBuffer(const BufferPtr& src, PtrSize srcOffset, const BufferPtr& dst, PtrSize dstOffset,
  299. PtrSize range)
  300. {
  301. ANKI_VK_SELF(CommandBufferImpl);
  302. self.copyBufferToBufferInternal(src, srcOffset, dst, dstOffset, range);
  303. }
  304. void CommandBuffer::buildAccelerationStructure(const AccelerationStructurePtr& as)
  305. {
  306. ANKI_VK_SELF(CommandBufferImpl);
  307. self.buildAccelerationStructureInternal(as);
  308. }
  309. void CommandBuffer::setTextureBarrier(const TexturePtr& tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  310. const TextureSubresourceInfo& subresource)
  311. {
  312. ANKI_VK_SELF(CommandBufferImpl);
  313. self.setTextureBarrierInternal(tex, prevUsage, nextUsage, subresource);
  314. }
  315. void CommandBuffer::setTextureSurfaceBarrier(const TexturePtr& tex, TextureUsageBit prevUsage,
  316. TextureUsageBit nextUsage, const TextureSurfaceInfo& surf)
  317. {
  318. ANKI_VK_SELF(CommandBufferImpl);
  319. self.setTextureSurfaceBarrierInternal(tex, prevUsage, nextUsage, surf);
  320. }
  321. void CommandBuffer::setTextureVolumeBarrier(const TexturePtr& tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  322. const TextureVolumeInfo& vol)
  323. {
  324. ANKI_VK_SELF(CommandBufferImpl);
  325. self.setTextureVolumeBarrierInternal(tex, prevUsage, nextUsage, vol);
  326. }
  327. void CommandBuffer::setBufferBarrier(const BufferPtr& buff, BufferUsageBit before, BufferUsageBit after, PtrSize offset,
  328. PtrSize size)
  329. {
  330. ANKI_VK_SELF(CommandBufferImpl);
  331. self.setBufferBarrierInternal(buff, before, after, offset, size);
  332. }
  333. void CommandBuffer::setAccelerationStructureBarrier(const AccelerationStructurePtr& as,
  334. AccelerationStructureUsageBit prevUsage,
  335. AccelerationStructureUsageBit nextUsage)
  336. {
  337. ANKI_VK_SELF(CommandBufferImpl);
  338. self.setAccelerationStructureBarrierInternal(as, prevUsage, nextUsage);
  339. }
  340. void CommandBuffer::resetOcclusionQuery(const OcclusionQueryPtr& query)
  341. {
  342. ANKI_VK_SELF(CommandBufferImpl);
  343. self.resetOcclusionQueryInternal(query);
  344. }
  345. void CommandBuffer::beginOcclusionQuery(const OcclusionQueryPtr& query)
  346. {
  347. ANKI_VK_SELF(CommandBufferImpl);
  348. self.beginOcclusionQueryInternal(query);
  349. }
  350. void CommandBuffer::endOcclusionQuery(const OcclusionQueryPtr& query)
  351. {
  352. ANKI_VK_SELF(CommandBufferImpl);
  353. self.endOcclusionQueryInternal(query);
  354. }
  355. void CommandBuffer::pushSecondLevelCommandBuffer(const CommandBufferPtr& cmdb)
  356. {
  357. ANKI_VK_SELF(CommandBufferImpl);
  358. self.pushSecondLevelCommandBufferInternal(cmdb);
  359. }
  360. void CommandBuffer::resetTimestampQuery(const TimestampQueryPtr& query)
  361. {
  362. ANKI_VK_SELF(CommandBufferImpl);
  363. self.resetTimestampQueryInternal(query);
  364. }
  365. void CommandBuffer::writeTimestamp(const TimestampQueryPtr& query)
  366. {
  367. ANKI_VK_SELF(CommandBufferImpl);
  368. self.writeTimestampInternal(query);
  369. }
  370. Bool CommandBuffer::isEmpty() const
  371. {
  372. ANKI_VK_SELF_CONST(CommandBufferImpl);
  373. return self.isEmpty();
  374. }
  375. void CommandBuffer::setPushConstants(const void* data, U32 dataSize)
  376. {
  377. ANKI_VK_SELF(CommandBufferImpl);
  378. self.setPushConstantsInternal(data, dataSize);
  379. }
  380. void CommandBuffer::setRasterizationOrder(RasterizationOrder order)
  381. {
  382. ANKI_VK_SELF(CommandBufferImpl);
  383. self.setRasterizationOrderInternal(order);
  384. }
  385. void CommandBuffer::setLineWidth(F32 width)
  386. {
  387. ANKI_VK_SELF(CommandBufferImpl);
  388. self.setLineWidthInternal(width);
  389. }
  390. } // end namespace anki