RenderGraph.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // Copyright (C) 2009-2017, 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/Enums.h>
  8. #include <anki/gr/Texture.h>
  9. #include <anki/gr/Buffer.h>
  10. #include <anki/gr/Framebuffer.h>
  11. #include <anki/util/HashMap.h>
  12. #include <anki/util/BitSet.h>
  13. namespace anki
  14. {
  15. // Forward
  16. class RenderGraph;
  17. /// @addtogroup graphics
  18. /// @{
  19. /// @name RenderGraph constants
  20. /// @{
  21. static constexpr U MAX_RENDER_GRAPH_PASSES = 128;
  22. static constexpr U MAX_RENDER_GRAPH_RENDER_TARGETS = 128; ///< Max imported or not render targets in RenderGraph.
  23. static constexpr U MAX_RENDER_GRAPH_BUFFERS = 64;
  24. /// @}
  25. /// Render target handle used in the RenderGraph.
  26. class RenderTargetHandle
  27. {
  28. friend class RenderPassDependency;
  29. friend class RenderGraphDescription;
  30. friend class RenderGraph;
  31. friend class RenderPassDescriptionBase;
  32. public:
  33. bool operator==(const RenderTargetHandle& b) const
  34. {
  35. return m_idx == b.m_idx;
  36. }
  37. private:
  38. U32 m_idx = MAX_U32;
  39. Bool valid() const
  40. {
  41. return m_idx != MAX_U32;
  42. }
  43. };
  44. /// Buffer handle used in the RenderGraph.
  45. class RenderPassBufferHandle
  46. {
  47. friend class RenderPassDependency;
  48. friend class RenderGraphDescription;
  49. friend class RenderGraph;
  50. friend class RenderPassDescriptionBase;
  51. public:
  52. operator BufferPtr() const;
  53. bool operator==(const RenderPassBufferHandle& b) const
  54. {
  55. return m_idx == b.m_idx;
  56. }
  57. private:
  58. U32 m_idx = MAX_U32;
  59. Bool valid() const
  60. {
  61. return m_idx != MAX_U32;
  62. }
  63. };
  64. /// Describes the render target.
  65. class RenderTargetDescription : public TextureInitInfo
  66. {
  67. friend class RenderGraphDescription;
  68. public:
  69. RenderTargetDescription()
  70. {
  71. }
  72. RenderTargetDescription(CString name)
  73. : TextureInitInfo(name)
  74. {
  75. }
  76. /// Create an internal hash.
  77. void bake()
  78. {
  79. m_hash = computeHash();
  80. }
  81. private:
  82. U64 m_hash = 0;
  83. };
  84. /// Work callback for a RenderGraph pass.
  85. using RenderPassWorkCallback = void (*)(
  86. void* userData, CommandBufferPtr cmdb, U32 secondLevelCmdbIdx, U32 secondLevelCmdbCount, const RenderGraph& rgraph);
  87. /// RenderGraph pass dependency.
  88. class RenderPassDependency
  89. {
  90. friend class RenderGraph;
  91. friend class RenderPassDescriptionBase;
  92. public:
  93. /// Dependency to an individual surface.
  94. RenderPassDependency(RenderTargetHandle handle, TextureUsageBit usage, const TextureSurfaceInfo& surface)
  95. : m_texture({handle, usage, surface, false})
  96. , m_isTexture(true)
  97. {
  98. ANKI_ASSERT(handle.valid());
  99. }
  100. /// Dependency to the whole texture.
  101. RenderPassDependency(RenderTargetHandle handle, TextureUsageBit usage)
  102. : m_texture({handle, usage, TextureSurfaceInfo(0, 0, 0, 0), true})
  103. , m_isTexture(true)
  104. {
  105. ANKI_ASSERT(handle.valid());
  106. }
  107. RenderPassDependency(RenderPassBufferHandle handle, BufferUsageBit usage)
  108. : m_buffer({handle, usage})
  109. , m_isTexture(false)
  110. {
  111. ANKI_ASSERT(handle.valid());
  112. }
  113. private:
  114. struct TextureInfo
  115. {
  116. RenderTargetHandle m_handle;
  117. TextureUsageBit m_usage;
  118. TextureSurfaceInfo m_surface;
  119. Bool8 m_wholeTex;
  120. };
  121. struct BufferInfo
  122. {
  123. RenderPassBufferHandle m_handle;
  124. BufferUsageBit m_usage;
  125. };
  126. union
  127. {
  128. TextureInfo m_texture;
  129. BufferInfo m_buffer;
  130. };
  131. Bool8 m_isTexture;
  132. };
  133. /// The base of compute/transfer and graphics renderpasses for RenderGraph.
  134. class RenderPassDescriptionBase
  135. {
  136. friend class RenderGraph;
  137. public:
  138. virtual ~RenderPassDescriptionBase()
  139. {
  140. m_name.destroy(m_alloc); // To avoid the assertion
  141. m_consumers.destroy(m_alloc);
  142. m_producers.destroy(m_alloc);
  143. }
  144. void setWork(RenderPassWorkCallback callback, void* userData, U32 secondLeveCmdbCount)
  145. {
  146. ANKI_ASSERT(callback);
  147. ANKI_ASSERT(m_type == Type::GRAPHICS || secondLeveCmdbCount != 0);
  148. m_callback = callback;
  149. m_userData = userData;
  150. m_secondLevelCmdbsCount = secondLeveCmdbCount;
  151. }
  152. /// Add new consumer dependency.
  153. void newConsumer(const RenderPassDependency& dep)
  154. {
  155. m_consumers.emplaceBack(m_alloc, dep);
  156. if(dep.m_isTexture && dep.m_texture.m_usage != TextureUsageBit::NONE)
  157. {
  158. m_consumerRtMask.set(dep.m_texture.m_handle.m_idx);
  159. }
  160. else if(dep.m_buffer.m_usage != BufferUsageBit::NONE)
  161. {
  162. m_consumerBufferMask.set(dep.m_buffer.m_handle.m_idx);
  163. }
  164. }
  165. /// Add new producer dependency.
  166. void newProducer(const RenderPassDependency& dep)
  167. {
  168. m_producers.emplaceBack(m_alloc, dep);
  169. if(dep.m_isTexture)
  170. {
  171. m_producerRtMask.set(dep.m_texture.m_handle.m_idx);
  172. }
  173. else
  174. {
  175. m_producerBufferMask.set(dep.m_buffer.m_handle.m_idx);
  176. }
  177. }
  178. protected:
  179. enum class Type : U8
  180. {
  181. GRAPHICS,
  182. NO_GRAPHICS
  183. };
  184. Type m_type;
  185. StackAllocator<U8> m_alloc;
  186. RenderPassWorkCallback m_callback = nullptr;
  187. void* m_userData = nullptr;
  188. U32 m_secondLevelCmdbsCount = 0;
  189. DynamicArray<RenderPassDependency> m_consumers;
  190. DynamicArray<RenderPassDependency> m_producers;
  191. BitSet<MAX_RENDER_GRAPH_RENDER_TARGETS> m_consumerRtMask = {false};
  192. BitSet<MAX_RENDER_GRAPH_RENDER_TARGETS> m_producerRtMask = {false};
  193. BitSet<MAX_RENDER_GRAPH_BUFFERS> m_consumerBufferMask = {false};
  194. BitSet<MAX_RENDER_GRAPH_BUFFERS> m_producerBufferMask = {false};
  195. String m_name;
  196. RenderPassDescriptionBase(Type t)
  197. : m_type(t)
  198. {
  199. }
  200. void setName(CString name)
  201. {
  202. m_name.create(m_alloc, (name.isEmpty()) ? "N/A" : name);
  203. }
  204. };
  205. /// Framebuffer attachment info.
  206. /// @memberof GraphicsRenderPassFramebufferDescription
  207. class GraphicsRenderPassFramebufferDescriptionAttachment
  208. {
  209. public:
  210. TextureSurfaceInfo m_surface;
  211. AttachmentLoadOperation m_loadOperation = AttachmentLoadOperation::CLEAR;
  212. AttachmentStoreOperation m_storeOperation = AttachmentStoreOperation::STORE;
  213. ClearValue m_clearValue;
  214. AttachmentLoadOperation m_stencilLoadOperation = AttachmentLoadOperation::CLEAR;
  215. AttachmentStoreOperation m_stencilStoreOperation = AttachmentStoreOperation::STORE;
  216. DepthStencilAspectBit m_aspect = DepthStencilAspectBit::NONE; ///< Relevant only for depth stencil textures.
  217. };
  218. /// Describes a framebuffer.
  219. /// @memberof GraphicsRenderPassDescription
  220. class GraphicsRenderPassFramebufferDescription
  221. {
  222. friend class GraphicsRenderPassDescription;
  223. public:
  224. Array<GraphicsRenderPassFramebufferDescriptionAttachment, MAX_COLOR_ATTACHMENTS> m_colorAttachments;
  225. U32 m_colorAttachmentCount = 0;
  226. GraphicsRenderPassFramebufferDescriptionAttachment m_depthStencilAttachment;
  227. void setDefaultFramebuffer()
  228. {
  229. m_defaultFb = true;
  230. }
  231. /// Calculate the hash for the framebuffer.
  232. void bake();
  233. Bool isBacked() const
  234. {
  235. return m_hash != 0;
  236. }
  237. private:
  238. FramebufferInitInfo m_fbInitInfo;
  239. Bool8 m_defaultFb = false;
  240. U64 m_hash = 0;
  241. };
  242. /// A graphics render pass for RenderGraph.
  243. class GraphicsRenderPassDescription : public RenderPassDescriptionBase
  244. {
  245. friend class RenderGraphDescription;
  246. friend class RenderGraph;
  247. public:
  248. GraphicsRenderPassDescription()
  249. : RenderPassDescriptionBase(Type::GRAPHICS)
  250. {
  251. }
  252. void setFramebufferInfo(const GraphicsRenderPassFramebufferDescription& fbInfo,
  253. const Array<RenderTargetHandle, MAX_COLOR_ATTACHMENTS>& colorRenderTargetHandles,
  254. RenderTargetHandle depthStencilRenderTargetHandle)
  255. {
  256. ANKI_ASSERT(fbInfo.m_hash != 0 && "Forgot call GraphicsRenderPassFramebufferInfo::bake");
  257. if(fbInfo.m_defaultFb)
  258. {
  259. m_fbInitInfo.m_colorAttachmentCount = 1;
  260. }
  261. else
  262. {
  263. m_fbInitInfo = fbInfo.m_fbInitInfo;
  264. memcpy(&m_rtHandles[0], &colorRenderTargetHandles[0], sizeof(colorRenderTargetHandles));
  265. m_rtHandles[MAX_COLOR_ATTACHMENTS] = depthStencilRenderTargetHandle;
  266. }
  267. m_fbInitInfo.setName(m_name.toCString());
  268. m_fbHash = fbInfo.m_hash;
  269. }
  270. private:
  271. Array<RenderTargetHandle, MAX_COLOR_ATTACHMENTS + 1> m_rtHandles = {};
  272. FramebufferInitInfo m_fbInitInfo;
  273. U64 m_fbHash = 0;
  274. Bool hasFramebuffer() const
  275. {
  276. return m_fbHash != 0;
  277. }
  278. };
  279. /// A compute render pass for RenderGraph.
  280. class ComputeRenderPassDescription : public RenderPassDescriptionBase
  281. {
  282. friend class RenderGraphDescription;
  283. public:
  284. ComputeRenderPassDescription()
  285. : RenderPassDescriptionBase(Type::NO_GRAPHICS)
  286. {
  287. }
  288. };
  289. /// Builds the description of the frame's render passes and their interactions.
  290. class RenderGraphDescription
  291. {
  292. friend class RenderGraph;
  293. public:
  294. RenderGraphDescription(const StackAllocator<U8>& alloc)
  295. : m_alloc(alloc)
  296. {
  297. }
  298. ~RenderGraphDescription()
  299. {
  300. for(RenderPassDescriptionBase* pass : m_passes)
  301. {
  302. m_alloc.deleteInstance(pass);
  303. }
  304. m_passes.destroy(m_alloc);
  305. m_renderTargets.destroy(m_alloc);
  306. }
  307. /// Create a new graphics render pass.
  308. GraphicsRenderPassDescription& newGraphicsRenderPass(CString name)
  309. {
  310. GraphicsRenderPassDescription* pass = m_alloc.newInstance<GraphicsRenderPassDescription>();
  311. pass->m_alloc = m_alloc;
  312. pass->setName(name);
  313. m_passes.emplaceBack(m_alloc, pass);
  314. return *pass;
  315. }
  316. /// Create a new compute render pass.
  317. ComputeRenderPassDescription& newComputeRenderPass(CString name)
  318. {
  319. ComputeRenderPassDescription* pass = m_alloc.newInstance<ComputeRenderPassDescription>();
  320. pass->m_alloc = m_alloc;
  321. pass->setName(name);
  322. m_passes.emplaceBack(m_alloc, pass);
  323. return *pass;
  324. }
  325. /// Import an existing render target.
  326. RenderTargetHandle importRenderTarget(CString name, TexturePtr tex, TextureUsageBit usage)
  327. {
  328. RT& rt = m_renderTargets.emplaceBack(m_alloc);
  329. rt.m_importedTex = tex;
  330. rt.m_usage = usage;
  331. rt.setName(name);
  332. RenderTargetHandle out;
  333. out.m_idx = m_renderTargets.getSize() - 1;
  334. return out;
  335. }
  336. /// Get or create a new render target.
  337. RenderTargetHandle newRenderTarget(const RenderTargetDescription& initInf)
  338. {
  339. ANKI_ASSERT(initInf.m_hash && "Forgot to call RenderTargetDescription::bake");
  340. RT& rt = m_renderTargets.emplaceBack(m_alloc);
  341. rt.m_initInfo = initInf;
  342. rt.m_hash = initInf.m_hash;
  343. rt.m_usage = TextureUsageBit::NONE;
  344. rt.setName(initInf.getName());
  345. RenderTargetHandle out;
  346. out.m_idx = m_renderTargets.getSize() - 1;
  347. return out;
  348. }
  349. /// Import a buffer.
  350. RenderPassBufferHandle importBuffer(CString name, BufferPtr buff, BufferUsageBit usage)
  351. {
  352. Buffer& b = m_buffers.emplaceBack(m_alloc);
  353. b.setName(name);
  354. b.m_usage = usage;
  355. b.m_importedBuff = buff;
  356. RenderPassBufferHandle out;
  357. out.m_idx = m_buffers.getSize() - 1;
  358. return out;
  359. }
  360. private:
  361. class Resource
  362. {
  363. public:
  364. Array<char, MAX_GR_OBJECT_NAME_LENGTH + 1> m_name;
  365. void setName(CString name)
  366. {
  367. const U len = name.getLength();
  368. ANKI_ASSERT(len <= MAX_GR_OBJECT_NAME_LENGTH);
  369. strcpy(&m_name[0], (len) ? &name[0] : "unnamed");
  370. }
  371. };
  372. class RT : public Resource
  373. {
  374. public:
  375. TextureInitInfo m_initInfo;
  376. U64 m_hash = 0;
  377. TexturePtr m_importedTex;
  378. TextureUsageBit m_usage;
  379. };
  380. class Buffer : public Resource
  381. {
  382. public:
  383. BufferUsageBit m_usage;
  384. BufferPtr m_importedBuff;
  385. };
  386. StackAllocator<U8> m_alloc;
  387. DynamicArray<RenderPassDescriptionBase*> m_passes;
  388. DynamicArray<RT> m_renderTargets;
  389. DynamicArray<Buffer> m_buffers;
  390. };
  391. /// Accepts a descriptor of the frame's render passes and sets the dependencies between them.
  392. ///
  393. /// The idea for the RenderGraph is to automate:
  394. /// - Synchronization (barriers, events etc) between passes.
  395. /// - Command buffer creation for primary and secondary command buffers.
  396. /// - Framebuffer creation.
  397. /// - Render target creation (optional since textures can be imported as well).
  398. ///
  399. /// It accepts a description of the frame's render passes (compute and graphics), compiles that description to calculate
  400. /// dependencies and then populates command buffers with the help of multiple RenderPassWorkCallback.
  401. class RenderGraph final : public GrObject
  402. {
  403. ANKI_GR_OBJECT
  404. public:
  405. static const GrObjectType CLASS_TYPE = GrObjectType::RENDER_GRAPH;
  406. RenderGraph(GrManager* manager, U64 hash, GrObjectCache* cache);
  407. // Non-copyable
  408. RenderGraph(const RenderGraph&) = delete;
  409. ~RenderGraph();
  410. // Non-copyable
  411. RenderGraph& operator=(const RenderGraph&) = delete;
  412. void init()
  413. {
  414. // Do nothing, implement the method to align with the general interface
  415. }
  416. /// @name 1st step methods
  417. /// @{
  418. void compileNewGraph(const RenderGraphDescription& descr, StackAllocator<U8>& alloc);
  419. /// @}
  420. /// @name 2nd step methods
  421. /// @{
  422. /// Will call a number of RenderPassWorkCallback that populate 2nd level command buffers.
  423. void runSecondLevel(U32 threadIdx) const;
  424. /// @}
  425. /// @name 3rd step methods
  426. /// @{
  427. /// Will call a number of RenderPassWorkCallback that populate 1st level command buffers.
  428. void run() const;
  429. /// @}
  430. /// @name 2nd and 3rd step methods
  431. /// @{
  432. TexturePtr getTexture(RenderTargetHandle handle) const;
  433. BufferPtr getBuffer(RenderPassBufferHandle handle) const;
  434. /// @}
  435. /// @name 4th step methods
  436. /// @{
  437. /// Reset the graph for a new frame. All previously created RenderGraphHandle are invalid after that call.
  438. void reset();
  439. /// @}
  440. private:
  441. /// Render targets of the same type+size+format.
  442. class RenderTargetCacheEntry
  443. {
  444. public:
  445. DynamicArray<TexturePtr> m_textures;
  446. U32 m_texturesInUse = 0;
  447. };
  448. HashMap<U64, RenderTargetCacheEntry> m_renderTargetCache; ///< Non-imported render targets.
  449. HashMap<U64, FramebufferPtr> m_fbCache; ///< Framebuffer cache.
  450. // Forward declarations
  451. class BakeContext;
  452. class Pass;
  453. class Batch;
  454. class RT;
  455. class Buffer;
  456. class Barrier;
  457. BakeContext* m_ctx = nullptr;
  458. BakeContext* newContext(const RenderGraphDescription& descr, StackAllocator<U8>& alloc);
  459. void initRenderPassesAndSetDeps(const RenderGraphDescription& descr, StackAllocator<U8>& alloc);
  460. void initBatches();
  461. void setBatchBarriers(const RenderGraphDescription& descr, BakeContext& ctx) const;
  462. TexturePtr getOrCreateRenderTarget(const TextureInitInfo& initInf, U64 hash);
  463. FramebufferPtr getOrCreateFramebuffer(
  464. const FramebufferInitInfo& fbInit, const RenderTargetHandle* rtHandles, U64 hash);
  465. static Bool passADependsOnB(
  466. BakeContext& ctx, const RenderPassDescriptionBase& a, const RenderPassDescriptionBase& b);
  467. static Bool passHasUnmetDependencies(const BakeContext& ctx, U32 passIdx);
  468. /// Dump the dependency graph into a file.
  469. ANKI_USE_RESULT Error dumpDependencyDotFile(
  470. const RenderGraphDescription& descr, const BakeContext& ctx, CString path) const;
  471. static StringAuto textureUsageToStr(StackAllocator<U8>& alloc, TextureUsageBit usage);
  472. static StringAuto bufferUsageToStr(StackAllocator<U8>& alloc, BufferUsageBit usage);
  473. };
  474. /// @}
  475. } // end namespace anki