CommandBuffer.cpp 13 KB

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