CommandBuffer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #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, BufferPtr buff, PtrSize offset, PtrSize stride,
  54. VertexStepRate stepRate)
  55. {
  56. ANKI_VK_SELF(CommandBufferImpl);
  57. self.bindVertexBuffer(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.setVertexAttribute(location, buffBinding, fmt, relativeOffset);
  63. }
  64. void CommandBuffer::bindIndexBuffer(BufferPtr buff, PtrSize offset, IndexType type)
  65. {
  66. ANKI_VK_SELF(CommandBufferImpl);
  67. self.bindIndexBuffer(buff, offset, type);
  68. }
  69. void CommandBuffer::setPrimitiveRestart(Bool enable)
  70. {
  71. ANKI_VK_SELF(CommandBufferImpl);
  72. self.setPrimitiveRestart(enable);
  73. }
  74. void CommandBuffer::setViewport(U32 minx, U32 miny, U32 width, U32 height)
  75. {
  76. ANKI_VK_SELF(CommandBufferImpl);
  77. self.setViewport(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.setScissor(minx, miny, width, height);
  83. }
  84. void CommandBuffer::setFillMode(FillMode mode)
  85. {
  86. ANKI_VK_SELF(CommandBufferImpl);
  87. self.setFillMode(mode);
  88. }
  89. void CommandBuffer::setCullMode(FaceSelectionBit mode)
  90. {
  91. ANKI_VK_SELF(CommandBufferImpl);
  92. self.setCullMode(mode);
  93. }
  94. void CommandBuffer::setPolygonOffset(F32 factor, F32 units)
  95. {
  96. ANKI_VK_SELF(CommandBufferImpl);
  97. self.setPolygonOffset(factor, units);
  98. }
  99. void CommandBuffer::setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail,
  100. StencilOperation stencilPassDepthFail, StencilOperation stencilPassDepthPass)
  101. {
  102. ANKI_VK_SELF(CommandBufferImpl);
  103. self.setStencilOperations(face, stencilFail, stencilPassDepthFail, stencilPassDepthPass);
  104. }
  105. void CommandBuffer::setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  106. {
  107. ANKI_VK_SELF(CommandBufferImpl);
  108. self.setStencilCompareOperation(face, comp);
  109. }
  110. void CommandBuffer::setStencilCompareMask(FaceSelectionBit face, U32 mask)
  111. {
  112. ANKI_VK_SELF(CommandBufferImpl);
  113. self.setStencilCompareMask(face, mask);
  114. }
  115. void CommandBuffer::setStencilWriteMask(FaceSelectionBit face, U32 mask)
  116. {
  117. ANKI_VK_SELF(CommandBufferImpl);
  118. self.setStencilWriteMask(face, mask);
  119. }
  120. void CommandBuffer::setStencilReference(FaceSelectionBit face, U32 ref)
  121. {
  122. ANKI_VK_SELF(CommandBufferImpl);
  123. self.setStencilReference(face, ref);
  124. }
  125. void CommandBuffer::setDepthWrite(Bool enable)
  126. {
  127. ANKI_VK_SELF(CommandBufferImpl);
  128. self.setDepthWrite(enable);
  129. }
  130. void CommandBuffer::setDepthCompareOperation(CompareOperation op)
  131. {
  132. ANKI_VK_SELF(CommandBufferImpl);
  133. self.setDepthCompareOperation(op);
  134. }
  135. void CommandBuffer::setAlphaToCoverage(Bool enable)
  136. {
  137. ANKI_VK_SELF(CommandBufferImpl);
  138. self.setAlphaToCoverage(enable);
  139. }
  140. void CommandBuffer::setColorChannelWriteMask(U32 attachment, ColorBit mask)
  141. {
  142. ANKI_VK_SELF(CommandBufferImpl);
  143. self.setColorChannelWriteMask(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.setBlendFactors(attachment, srcRgb, dstRgb, srcA, dstA);
  150. }
  151. void CommandBuffer::setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  152. {
  153. ANKI_VK_SELF(CommandBufferImpl);
  154. self.setBlendOperation(attachment, funcRgb, funcA);
  155. }
  156. void CommandBuffer::bindTextureAndSampler(U32 set, U32 binding, TextureViewPtr texView, SamplerPtr sampler,
  157. 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, 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, 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, BufferPtr buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  173. {
  174. ANKI_VK_SELF(CommandBufferImpl);
  175. self.bindUniformBufferInternal(set, binding, buff, offset, range, arrayIdx);
  176. }
  177. void CommandBuffer::bindStorageBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, U32 arrayIdx)
  178. {
  179. ANKI_VK_SELF(CommandBufferImpl);
  180. self.bindStorageBufferInternal(set, binding, buff, offset, range, arrayIdx);
  181. }
  182. void CommandBuffer::bindImage(U32 set, U32 binding, TextureViewPtr img, U32 arrayIdx)
  183. {
  184. ANKI_VK_SELF(CommandBufferImpl);
  185. self.bindImageInternal(set, binding, img, arrayIdx);
  186. }
  187. void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, AccelerationStructurePtr as, U32 arrayIdx)
  188. {
  189. ANKI_VK_SELF(CommandBufferImpl);
  190. self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
  191. }
  192. void CommandBuffer::bindTextureBuffer(U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, Format fmt,
  193. U32 arrayIdx)
  194. {
  195. ANKI_ASSERT(!"TODO");
  196. }
  197. void CommandBuffer::bindAllBindless(U32 set)
  198. {
  199. ANKI_VK_SELF(CommandBufferImpl);
  200. self.bindAllBindlessInternal(set);
  201. }
  202. void CommandBuffer::bindShaderProgram(ShaderProgramPtr prog)
  203. {
  204. ANKI_VK_SELF(CommandBufferImpl);
  205. self.bindShaderProgram(prog);
  206. }
  207. void CommandBuffer::beginRenderPass(FramebufferPtr fb,
  208. const Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS>& colorAttachmentUsages,
  209. TextureUsageBit depthStencilAttachmentUsage, U32 minx, U32 miny, U32 width,
  210. U32 height)
  211. {
  212. ANKI_VK_SELF(CommandBufferImpl);
  213. self.beginRenderPass(fb, colorAttachmentUsages, depthStencilAttachmentUsage, minx, miny, width, height);
  214. }
  215. void CommandBuffer::endRenderPass()
  216. {
  217. ANKI_VK_SELF(CommandBufferImpl);
  218. self.endRenderPass();
  219. }
  220. void CommandBuffer::drawElements(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 firstIndex,
  221. U32 baseVertex, U32 baseInstance)
  222. {
  223. ANKI_VK_SELF(CommandBufferImpl);
  224. self.drawElements(topology, count, instanceCount, firstIndex, baseVertex, baseInstance);
  225. }
  226. void CommandBuffer::drawArrays(PrimitiveTopology topology, U32 count, U32 instanceCount, U32 first, U32 baseInstance)
  227. {
  228. ANKI_VK_SELF(CommandBufferImpl);
  229. self.drawArrays(topology, count, instanceCount, first, baseInstance);
  230. }
  231. void CommandBuffer::drawArraysIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, BufferPtr buff)
  232. {
  233. ANKI_VK_SELF(CommandBufferImpl);
  234. self.drawArraysIndirect(topology, drawCount, offset, buff);
  235. }
  236. void CommandBuffer::drawElementsIndirect(PrimitiveTopology topology, U32 drawCount, PtrSize offset, BufferPtr buff)
  237. {
  238. ANKI_VK_SELF(CommandBufferImpl);
  239. self.drawElementsIndirect(topology, drawCount, offset, buff);
  240. }
  241. void CommandBuffer::dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ)
  242. {
  243. ANKI_VK_SELF(CommandBufferImpl);
  244. self.dispatchCompute(groupCountX, groupCountY, groupCountZ);
  245. }
  246. void CommandBuffer::traceRays(BufferPtr sbtBuffer, PtrSize sbtBufferOffset, U32 sbtRecordSize,
  247. U32 hitGroupSbtRecordCount, U32 rayTypeCount, U32 width, U32 height, U32 depth)
  248. {
  249. ANKI_VK_SELF(CommandBufferImpl);
  250. self.traceRaysInternal(sbtBuffer, sbtBufferOffset, sbtRecordSize, hitGroupSbtRecordCount, rayTypeCount, width,
  251. height, depth);
  252. }
  253. void CommandBuffer::generateMipmaps2d(TextureViewPtr texView)
  254. {
  255. ANKI_VK_SELF(CommandBufferImpl);
  256. self.generateMipmaps2d(texView);
  257. }
  258. void CommandBuffer::generateMipmaps3d(TextureViewPtr texView)
  259. {
  260. ANKI_ASSERT(!"TODO");
  261. }
  262. void CommandBuffer::blitTextureViews(TextureViewPtr srcView, TextureViewPtr destView)
  263. {
  264. ANKI_ASSERT(!"TODO");
  265. }
  266. void CommandBuffer::clearTextureView(TextureViewPtr texView, const ClearValue& clearValue)
  267. {
  268. ANKI_VK_SELF(CommandBufferImpl);
  269. self.clearTextureView(texView, clearValue);
  270. }
  271. void CommandBuffer::copyBufferToTextureView(BufferPtr buff, PtrSize offset, PtrSize range, TextureViewPtr texView)
  272. {
  273. ANKI_VK_SELF(CommandBufferImpl);
  274. self.copyBufferToTextureViewInternal(buff, offset, range, texView);
  275. }
  276. void CommandBuffer::fillBuffer(BufferPtr buff, PtrSize offset, PtrSize size, U32 value)
  277. {
  278. ANKI_VK_SELF(CommandBufferImpl);
  279. self.fillBuffer(buff, offset, size, value);
  280. }
  281. void CommandBuffer::writeOcclusionQueryResultToBuffer(OcclusionQueryPtr query, PtrSize offset, BufferPtr buff)
  282. {
  283. ANKI_VK_SELF(CommandBufferImpl);
  284. self.writeOcclusionQueryResultToBuffer(query, offset, buff);
  285. }
  286. void CommandBuffer::copyBufferToBuffer(BufferPtr src, PtrSize srcOffset, BufferPtr dst, PtrSize dstOffset,
  287. PtrSize range)
  288. {
  289. ANKI_VK_SELF(CommandBufferImpl);
  290. self.copyBufferToBuffer(src, srcOffset, dst, dstOffset, range);
  291. }
  292. void CommandBuffer::buildAccelerationStructure(AccelerationStructurePtr as)
  293. {
  294. ANKI_VK_SELF(CommandBufferImpl);
  295. self.buildAccelerationStructureInternal(as);
  296. }
  297. void CommandBuffer::setTextureBarrier(TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  298. const TextureSubresourceInfo& subresource)
  299. {
  300. ANKI_VK_SELF(CommandBufferImpl);
  301. self.setTextureBarrier(tex, prevUsage, nextUsage, subresource);
  302. }
  303. void CommandBuffer::setTextureSurfaceBarrier(TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  304. const TextureSurfaceInfo& surf)
  305. {
  306. ANKI_VK_SELF(CommandBufferImpl);
  307. self.setTextureSurfaceBarrier(tex, prevUsage, nextUsage, surf);
  308. }
  309. void CommandBuffer::setTextureVolumeBarrier(TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage,
  310. const TextureVolumeInfo& vol)
  311. {
  312. ANKI_VK_SELF(CommandBufferImpl);
  313. self.setTextureVolumeBarrier(tex, prevUsage, nextUsage, vol);
  314. }
  315. void CommandBuffer::setBufferBarrier(BufferPtr buff, BufferUsageBit before, BufferUsageBit after, PtrSize offset,
  316. PtrSize size)
  317. {
  318. ANKI_VK_SELF(CommandBufferImpl);
  319. self.setBufferBarrier(buff, before, after, offset, size);
  320. }
  321. void CommandBuffer::setAccelerationStructureBarrier(AccelerationStructurePtr as,
  322. AccelerationStructureUsageBit prevUsage,
  323. AccelerationStructureUsageBit nextUsage)
  324. {
  325. ANKI_VK_SELF(CommandBufferImpl);
  326. self.setAccelerationStructureBarrierInternal(as, prevUsage, nextUsage);
  327. }
  328. void CommandBuffer::resetOcclusionQuery(OcclusionQueryPtr query)
  329. {
  330. ANKI_VK_SELF(CommandBufferImpl);
  331. self.resetOcclusionQuery(query);
  332. }
  333. void CommandBuffer::beginOcclusionQuery(OcclusionQueryPtr query)
  334. {
  335. ANKI_VK_SELF(CommandBufferImpl);
  336. self.beginOcclusionQuery(query);
  337. }
  338. void CommandBuffer::endOcclusionQuery(OcclusionQueryPtr query)
  339. {
  340. ANKI_VK_SELF(CommandBufferImpl);
  341. self.endOcclusionQuery(query);
  342. }
  343. void CommandBuffer::pushSecondLevelCommandBuffer(CommandBufferPtr cmdb)
  344. {
  345. ANKI_VK_SELF(CommandBufferImpl);
  346. self.pushSecondLevelCommandBuffer(cmdb);
  347. }
  348. void CommandBuffer::resetTimestampQuery(TimestampQueryPtr query)
  349. {
  350. ANKI_VK_SELF(CommandBufferImpl);
  351. self.resetTimestampQueryInternal(query);
  352. }
  353. void CommandBuffer::writeTimestamp(TimestampQueryPtr query)
  354. {
  355. ANKI_VK_SELF(CommandBufferImpl);
  356. self.writeTimestampInternal(query);
  357. }
  358. Bool CommandBuffer::isEmpty() const
  359. {
  360. ANKI_VK_SELF_CONST(CommandBufferImpl);
  361. return self.isEmpty();
  362. }
  363. void CommandBuffer::setPushConstants(const void* data, U32 dataSize)
  364. {
  365. ANKI_VK_SELF(CommandBufferImpl);
  366. self.setPushConstants(data, dataSize);
  367. }
  368. void CommandBuffer::setRasterizationOrder(RasterizationOrder order)
  369. {
  370. ANKI_VK_SELF(CommandBufferImpl);
  371. self.setRasterizationOrder(order);
  372. }
  373. void CommandBuffer::setLineWidth(F32 width)
  374. {
  375. ANKI_VK_SELF(CommandBufferImpl);
  376. self.setLineWidth(width);
  377. }
  378. void CommandBuffer::addReference(GrObjectPtr ptr)
  379. {
  380. ANKI_VK_SELF(CommandBufferImpl);
  381. self.addReference(ptr);
  382. }
  383. } // end namespace anki