RenderGraph.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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/RenderGraph.h>
  6. #include <AnKi/Gr/GrManager.h>
  7. #include <AnKi/Gr/Texture.h>
  8. #include <AnKi/Gr/Sampler.h>
  9. #include <AnKi/Gr/Framebuffer.h>
  10. #include <AnKi/Gr/CommandBuffer.h>
  11. #include <AnKi/Util/Tracer.h>
  12. #include <AnKi/Util/BitSet.h>
  13. #include <AnKi/Util/File.h>
  14. #include <AnKi/Util/StringList.h>
  15. #include <AnKi/Util/HighRezTimer.h>
  16. namespace anki {
  17. #define ANKI_DBG_RENDER_GRAPH 0
  18. static inline U32 getTextureSurfOrVolCount(const TexturePtr& tex)
  19. {
  20. return tex->getMipmapCount() * tex->getLayerCount() * (textureTypeIsCube(tex->getTextureType()) ? 6 : 1);
  21. }
  22. /// Contains some extra things for render targets.
  23. class RenderGraph::RT
  24. {
  25. public:
  26. DynamicArray<TextureUsageBit> m_surfOrVolUsages;
  27. DynamicArray<U16> m_lastBatchThatTransitionedIt;
  28. TexturePtr m_texture; ///< Hold a reference.
  29. Bool m_imported;
  30. };
  31. /// Same as RT but for buffers.
  32. class RenderGraph::Buffer
  33. {
  34. public:
  35. BufferUsageBit m_usage;
  36. BufferPtr m_buffer; ///< Hold a reference.
  37. PtrSize m_offset;
  38. PtrSize m_range;
  39. };
  40. class RenderGraph::AS
  41. {
  42. public:
  43. AccelerationStructureUsageBit m_usage;
  44. AccelerationStructurePtr m_as; ///< Hold a reference.
  45. };
  46. /// Pipeline barrier.
  47. class RenderGraph::TextureBarrier
  48. {
  49. public:
  50. U32 m_idx;
  51. TextureUsageBit m_usageBefore;
  52. TextureUsageBit m_usageAfter;
  53. TextureSurfaceInfo m_surface;
  54. TextureBarrier(U32 rtIdx, TextureUsageBit usageBefore, TextureUsageBit usageAfter, const TextureSurfaceInfo& surf)
  55. : m_idx(rtIdx)
  56. , m_usageBefore(usageBefore)
  57. , m_usageAfter(usageAfter)
  58. , m_surface(surf)
  59. {
  60. }
  61. };
  62. /// Pipeline barrier.
  63. class RenderGraph::BufferBarrier
  64. {
  65. public:
  66. U32 m_idx;
  67. BufferUsageBit m_usageBefore;
  68. BufferUsageBit m_usageAfter;
  69. BufferBarrier(U32 buffIdx, BufferUsageBit usageBefore, BufferUsageBit usageAfter)
  70. : m_idx(buffIdx)
  71. , m_usageBefore(usageBefore)
  72. , m_usageAfter(usageAfter)
  73. {
  74. }
  75. };
  76. /// Pipeline barrier.
  77. class RenderGraph::ASBarrier
  78. {
  79. public:
  80. U32 m_idx;
  81. AccelerationStructureUsageBit m_usageBefore;
  82. AccelerationStructureUsageBit m_usageAfter;
  83. ASBarrier(U32 asIdx, AccelerationStructureUsageBit usageBefore, AccelerationStructureUsageBit usageAfter)
  84. : m_idx(asIdx)
  85. , m_usageBefore(usageBefore)
  86. , m_usageAfter(usageAfter)
  87. {
  88. }
  89. };
  90. /// Contains some extra things the RenderPassBase cannot hold.
  91. class RenderGraph::Pass
  92. {
  93. public:
  94. // WARNING!!!!!: Whatever you put here needs manual destruction in RenderGraph::reset()
  95. DynamicArray<U32> m_dependsOn;
  96. DynamicArray<RenderPassDependency::TextureInfo> m_consumedTextures;
  97. Function<void(RenderPassWorkContext&)> m_callback;
  98. DynamicArray<CommandBufferPtr> m_secondLevelCmdbs;
  99. /// Will reuse the m_secondLevelCmdbInitInfo.m_framebuffer to get the framebuffer.
  100. CommandBufferInitInfo m_secondLevelCmdbInitInfo;
  101. Array<U32, 4> m_fbRenderArea;
  102. Array<TextureUsageBit, MAX_COLOR_ATTACHMENTS> m_colorUsages = {}; ///< For beginRender pass
  103. TextureUsageBit m_dsUsage = TextureUsageBit::NONE; ///< For beginRender pass
  104. U32 m_batchIdx ANKI_DEBUG_CODE(= MAX_U32);
  105. Bool m_drawsToPresentable = false;
  106. FramebufferPtr& fb()
  107. {
  108. return m_secondLevelCmdbInitInfo.m_framebuffer;
  109. }
  110. const FramebufferPtr& fb() const
  111. {
  112. return m_secondLevelCmdbInitInfo.m_framebuffer;
  113. }
  114. };
  115. /// A batch of render passes. These passes can run in parallel.
  116. /// @warning It's POD. Destructor won't be called.
  117. class RenderGraph::Batch
  118. {
  119. public:
  120. DynamicArray<U32> m_passIndices;
  121. DynamicArray<TextureBarrier> m_textureBarriersBefore;
  122. DynamicArray<BufferBarrier> m_bufferBarriersBefore;
  123. DynamicArray<ASBarrier> m_asBarriersBefore;
  124. CommandBuffer* m_cmdb; ///< Someone else holds the ref already so have a ptr here.
  125. };
  126. /// The RenderGraph build context.
  127. class RenderGraph::BakeContext
  128. {
  129. public:
  130. StackAllocator<U8> m_alloc;
  131. DynamicArray<Pass> m_passes;
  132. BitSet<MAX_RENDER_GRAPH_PASSES, U64> m_passIsInBatch{false};
  133. DynamicArray<Batch> m_batches;
  134. DynamicArray<RT> m_rts;
  135. DynamicArray<Buffer> m_buffers;
  136. DynamicArray<AS> m_as;
  137. DynamicArray<CommandBufferPtr> m_graphicsCmdbs;
  138. Bool m_gatherStatistics = false;
  139. BakeContext(const StackAllocator<U8>& alloc)
  140. : m_alloc(alloc)
  141. {
  142. }
  143. };
  144. void FramebufferDescription::bake()
  145. {
  146. m_hash = 0;
  147. ANKI_ASSERT(m_colorAttachmentCount > 0 || !!m_depthStencilAttachment.m_aspect);
  148. // First the depth attachments
  149. if(m_colorAttachmentCount)
  150. {
  151. ANKI_BEGIN_PACKED_STRUCT
  152. struct ColorAttachment
  153. {
  154. TextureSurfaceInfo m_surf;
  155. U32 m_loadOp;
  156. U32 m_storeOp;
  157. Array<U32, 4> m_clearColor;
  158. };
  159. ANKI_END_PACKED_STRUCT
  160. static_assert(sizeof(ColorAttachment) == 4 * (4 + 1 + 1 + 4), "Wrong size");
  161. Array<ColorAttachment, MAX_COLOR_ATTACHMENTS> colorAttachments;
  162. for(U i = 0; i < m_colorAttachmentCount; ++i)
  163. {
  164. const FramebufferDescriptionAttachment& inAtt = m_colorAttachments[i];
  165. colorAttachments[i].m_surf = inAtt.m_surface;
  166. colorAttachments[i].m_loadOp = static_cast<U32>(inAtt.m_loadOperation);
  167. colorAttachments[i].m_storeOp = static_cast<U32>(inAtt.m_storeOperation);
  168. memcpy(&colorAttachments[i].m_clearColor[0], &inAtt.m_clearValue.m_coloru[0], sizeof(U32) * 4);
  169. }
  170. m_hash = computeHash(&colorAttachments[0], sizeof(ColorAttachment) * m_colorAttachmentCount);
  171. }
  172. // DS attachment
  173. if(!!m_depthStencilAttachment.m_aspect)
  174. {
  175. ANKI_BEGIN_PACKED_STRUCT
  176. class DSAttachment
  177. {
  178. public:
  179. TextureSurfaceInfo m_surf;
  180. U32 m_loadOp;
  181. U32 m_storeOp;
  182. U32 m_stencilLoadOp;
  183. U32 m_stencilStoreOp;
  184. U32 m_aspect;
  185. F32 m_depthClear;
  186. I32 m_stencilClear;
  187. } outAtt;
  188. ANKI_END_PACKED_STRUCT
  189. const FramebufferDescriptionAttachment& inAtt = m_depthStencilAttachment;
  190. const Bool hasDepth = !!(inAtt.m_aspect & DepthStencilAspectBit::DEPTH);
  191. const Bool hasStencil = !!(inAtt.m_aspect & DepthStencilAspectBit::STENCIL);
  192. outAtt.m_surf = inAtt.m_surface;
  193. outAtt.m_loadOp = (hasDepth) ? static_cast<U32>(inAtt.m_loadOperation) : 0;
  194. outAtt.m_storeOp = (hasDepth) ? static_cast<U32>(inAtt.m_storeOperation) : 0;
  195. outAtt.m_stencilLoadOp = (hasStencil) ? static_cast<U32>(inAtt.m_stencilLoadOperation) : 0;
  196. outAtt.m_stencilStoreOp = (hasStencil) ? static_cast<U32>(inAtt.m_stencilStoreOperation) : 0;
  197. outAtt.m_aspect = static_cast<U32>(inAtt.m_aspect);
  198. outAtt.m_depthClear = (hasDepth) ? inAtt.m_clearValue.m_depthStencil.m_depth : 0.0f;
  199. outAtt.m_stencilClear = (hasStencil) ? inAtt.m_clearValue.m_depthStencil.m_stencil : 0;
  200. m_hash = (m_hash != 0) ? appendHash(&outAtt, sizeof(outAtt), m_hash) : computeHash(&outAtt, sizeof(outAtt));
  201. }
  202. // SRI
  203. if(m_shadingRateAttachmentTexelWidth > 0 && m_shadingRateAttachmentTexelHeight > 0)
  204. {
  205. ANKI_BEGIN_PACKED_STRUCT
  206. class SriToHash
  207. {
  208. public:
  209. U32 m_sriTexelWidth;
  210. U32 m_sriTexelHeight;
  211. TextureSurfaceInfo m_surface;
  212. } sriToHash;
  213. ANKI_END_PACKED_STRUCT
  214. sriToHash.m_sriTexelWidth = m_shadingRateAttachmentTexelWidth;
  215. sriToHash.m_sriTexelHeight = m_shadingRateAttachmentTexelHeight;
  216. sriToHash.m_surface = m_shadingRateAttachmentSurface;
  217. m_hash = (m_hash != 0) ? appendHash(&sriToHash, sizeof(sriToHash), m_hash)
  218. : computeHash(&sriToHash, sizeof(sriToHash));
  219. }
  220. ANKI_ASSERT(m_hash != 0 && m_hash != 1);
  221. }
  222. RenderGraph::RenderGraph(GrManager* manager, CString name)
  223. : GrObject(manager, CLASS_TYPE, name)
  224. {
  225. ANKI_ASSERT(manager);
  226. }
  227. RenderGraph::~RenderGraph()
  228. {
  229. ANKI_ASSERT(m_ctx == nullptr);
  230. while(!m_renderTargetCache.isEmpty())
  231. {
  232. auto it = m_renderTargetCache.getBegin();
  233. RenderTargetCacheEntry& entry = *it;
  234. entry.m_textures.destroy(getAllocator());
  235. m_renderTargetCache.erase(getAllocator(), it);
  236. }
  237. m_fbCache.destroy(getAllocator());
  238. for(auto& it : m_importedRenderTargets)
  239. {
  240. it.m_surfOrVolLastUsages.destroy(getAllocator());
  241. }
  242. m_importedRenderTargets.destroy(getAllocator());
  243. }
  244. RenderGraph* RenderGraph::newInstance(GrManager* manager)
  245. {
  246. return manager->getAllocator().newInstance<RenderGraph>(manager, "N/A");
  247. }
  248. void RenderGraph::reset()
  249. {
  250. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_RESET);
  251. if(!m_ctx)
  252. {
  253. return;
  254. }
  255. if((m_version % PERIODIC_CLEANUP_EVERY) == 0)
  256. {
  257. // Do cleanup
  258. periodicCleanup();
  259. }
  260. // Extract the final usage of the imported RTs and clean all RTs
  261. for(RT& rt : m_ctx->m_rts)
  262. {
  263. if(rt.m_imported)
  264. {
  265. const U32 surfOrVolumeCount = getTextureSurfOrVolCount(rt.m_texture);
  266. // Create a new hash because our hash map dislikes concurent keys.
  267. const U64 uuid = rt.m_texture->getUuid();
  268. const U64 hash = computeHash(&uuid, sizeof(uuid));
  269. auto it = m_importedRenderTargets.find(hash);
  270. if(it != m_importedRenderTargets.getEnd())
  271. {
  272. // Found
  273. ANKI_ASSERT(it->m_surfOrVolLastUsages.getSize() == surfOrVolumeCount);
  274. ANKI_ASSERT(rt.m_surfOrVolUsages.getSize() == surfOrVolumeCount);
  275. }
  276. else
  277. {
  278. // Not found, create
  279. it = m_importedRenderTargets.emplace(getAllocator(), hash);
  280. it->m_surfOrVolLastUsages.create(getAllocator(), surfOrVolumeCount);
  281. }
  282. // Update the usage
  283. for(U32 surfOrVolIdx = 0; surfOrVolIdx < surfOrVolumeCount; ++surfOrVolIdx)
  284. {
  285. it->m_surfOrVolLastUsages[surfOrVolIdx] = rt.m_surfOrVolUsages[surfOrVolIdx];
  286. }
  287. }
  288. rt.m_texture.reset(nullptr);
  289. }
  290. for(Buffer& buff : m_ctx->m_buffers)
  291. {
  292. buff.m_buffer.reset(nullptr);
  293. }
  294. for(AS& as : m_ctx->m_as)
  295. {
  296. as.m_as.reset(nullptr);
  297. }
  298. for(auto& it : m_renderTargetCache)
  299. {
  300. it.m_texturesInUse = 0;
  301. }
  302. for(Pass& p : m_ctx->m_passes)
  303. {
  304. p.fb().reset(nullptr);
  305. p.m_secondLevelCmdbs.destroy(m_ctx->m_alloc);
  306. p.m_callback.destroy(m_ctx->m_alloc);
  307. }
  308. m_ctx->m_graphicsCmdbs.destroy(m_ctx->m_alloc);
  309. m_ctx->m_alloc = StackAllocator<U8>();
  310. m_ctx = nullptr;
  311. ++m_version;
  312. }
  313. TexturePtr RenderGraph::getOrCreateRenderTarget(const TextureInitInfo& initInf, U64 hash)
  314. {
  315. ANKI_ASSERT(hash);
  316. auto alloc = getManager().getAllocator();
  317. // Find a cache entry
  318. RenderTargetCacheEntry* entry = nullptr;
  319. auto it = m_renderTargetCache.find(hash);
  320. if(ANKI_UNLIKELY(it == m_renderTargetCache.getEnd()))
  321. {
  322. // Didn't found the entry, create a new one
  323. auto it2 = m_renderTargetCache.emplace(getAllocator(), hash);
  324. entry = &(*it2);
  325. }
  326. else
  327. {
  328. entry = &(*it);
  329. }
  330. ANKI_ASSERT(entry);
  331. // Create or pop one tex from the cache
  332. TexturePtr tex;
  333. const Bool createNewTex = entry->m_textures.getSize() == entry->m_texturesInUse;
  334. if(!createNewTex)
  335. {
  336. // Pop
  337. tex = entry->m_textures[entry->m_texturesInUse++];
  338. }
  339. else
  340. {
  341. // Create it
  342. tex = getManager().newTexture(initInf);
  343. ANKI_ASSERT(entry->m_texturesInUse == entry->m_textures.getSize());
  344. entry->m_textures.resize(alloc, entry->m_textures.getSize() + 1);
  345. entry->m_textures[entry->m_textures.getSize() - 1] = tex;
  346. ++entry->m_texturesInUse;
  347. }
  348. return tex;
  349. }
  350. FramebufferPtr RenderGraph::getOrCreateFramebuffer(const FramebufferDescription& fbDescr,
  351. const RenderTargetHandle* rtHandles, CString name,
  352. Bool& drawsToPresentable)
  353. {
  354. ANKI_ASSERT(rtHandles);
  355. U64 hash = fbDescr.m_hash;
  356. ANKI_ASSERT(hash > 0);
  357. drawsToPresentable = false;
  358. // Create a hash that includes the render targets
  359. Array<U64, MAX_COLOR_ATTACHMENTS + 2> uuids;
  360. U count = 0;
  361. for(U i = 0; i < fbDescr.m_colorAttachmentCount; ++i)
  362. {
  363. uuids[count++] = m_ctx->m_rts[rtHandles[i].m_idx].m_texture->getUuid();
  364. if(!!(m_ctx->m_rts[rtHandles[i].m_idx].m_texture->getTextureUsage() & TextureUsageBit::PRESENT))
  365. {
  366. drawsToPresentable = true;
  367. }
  368. }
  369. if(!!fbDescr.m_depthStencilAttachment.m_aspect)
  370. {
  371. uuids[count++] = m_ctx->m_rts[rtHandles[MAX_COLOR_ATTACHMENTS].m_idx].m_texture->getUuid();
  372. }
  373. if(fbDescr.m_shadingRateAttachmentTexelWidth > 0)
  374. {
  375. uuids[count++] = m_ctx->m_rts[rtHandles[MAX_COLOR_ATTACHMENTS + 1].m_idx].m_texture->getUuid();
  376. }
  377. hash = appendHash(&uuids[0], sizeof(U64) * count, hash);
  378. // Hash the name of the pass. If you don't the code bellow may fetch an FB with some another name and that will
  379. // cause problems with tools. The FB name is used as a debug marker
  380. hash = appendHash(name.cstr(), name.getLength(), hash);
  381. FramebufferPtr fb;
  382. auto it = m_fbCache.find(hash);
  383. if(it != m_fbCache.getEnd())
  384. {
  385. fb = *it;
  386. }
  387. else
  388. {
  389. // Create a complete fb init info
  390. FramebufferInitInfo fbInit;
  391. fbInit.m_colorAttachmentCount = fbDescr.m_colorAttachmentCount;
  392. for(U i = 0; i < fbInit.m_colorAttachmentCount; ++i)
  393. {
  394. FramebufferAttachmentInfo& outAtt = fbInit.m_colorAttachments[i];
  395. const FramebufferDescriptionAttachment& inAtt = fbDescr.m_colorAttachments[i];
  396. outAtt.m_clearValue = inAtt.m_clearValue;
  397. outAtt.m_loadOperation = inAtt.m_loadOperation;
  398. outAtt.m_storeOperation = inAtt.m_storeOperation;
  399. // Create texture view
  400. TextureViewInitInfo viewInit(m_ctx->m_rts[rtHandles[i].m_idx].m_texture,
  401. TextureSubresourceInfo(inAtt.m_surface), "RenderGraph");
  402. TextureViewPtr view = getManager().newTextureView(viewInit);
  403. outAtt.m_textureView = view;
  404. }
  405. if(!!fbDescr.m_depthStencilAttachment.m_aspect)
  406. {
  407. FramebufferAttachmentInfo& outAtt = fbInit.m_depthStencilAttachment;
  408. const FramebufferDescriptionAttachment& inAtt = fbDescr.m_depthStencilAttachment;
  409. outAtt.m_clearValue = inAtt.m_clearValue;
  410. outAtt.m_loadOperation = inAtt.m_loadOperation;
  411. outAtt.m_storeOperation = inAtt.m_storeOperation;
  412. outAtt.m_stencilLoadOperation = inAtt.m_stencilLoadOperation;
  413. outAtt.m_stencilStoreOperation = inAtt.m_stencilStoreOperation;
  414. // Create texture view
  415. TextureViewInitInfo viewInit(m_ctx->m_rts[rtHandles[MAX_COLOR_ATTACHMENTS].m_idx].m_texture,
  416. TextureSubresourceInfo(inAtt.m_surface, inAtt.m_aspect), "RenderGraph");
  417. TextureViewPtr view = getManager().newTextureView(viewInit);
  418. outAtt.m_textureView = view;
  419. }
  420. if(fbDescr.m_shadingRateAttachmentTexelWidth > 0)
  421. {
  422. TextureViewInitInfo viewInit(m_ctx->m_rts[rtHandles[MAX_COLOR_ATTACHMENTS + 1].m_idx].m_texture,
  423. fbDescr.m_shadingRateAttachmentSurface, "RenderGraph SRI");
  424. TextureViewPtr view = getManager().newTextureView(viewInit);
  425. fbInit.m_shadingRateImage.m_texelWidth = fbDescr.m_shadingRateAttachmentTexelWidth;
  426. fbInit.m_shadingRateImage.m_texelHeight = fbDescr.m_shadingRateAttachmentTexelHeight;
  427. fbInit.m_shadingRateImage.m_textureView = view;
  428. }
  429. // Set FB name
  430. fbInit.setName(name);
  431. // Create
  432. fb = getManager().newFramebuffer(fbInit);
  433. m_fbCache.emplace(getAllocator(), hash, fb);
  434. }
  435. return fb;
  436. }
  437. Bool RenderGraph::overlappingTextureSubresource(const TextureSubresourceInfo& suba, const TextureSubresourceInfo& subb)
  438. {
  439. #define ANKI_OVERLAPPING(first, count) \
  440. ((suba.first < subb.first + subb.count) && (subb.first < suba.first + suba.count))
  441. const Bool overlappingFaces = ANKI_OVERLAPPING(m_firstFace, m_faceCount);
  442. const Bool overlappingMips = ANKI_OVERLAPPING(m_firstMipmap, m_mipmapCount);
  443. const Bool overlappingLayers = ANKI_OVERLAPPING(m_firstLayer, m_layerCount);
  444. #undef ANKI_OVERLAPPING
  445. return overlappingFaces && overlappingLayers && overlappingMips;
  446. }
  447. Bool RenderGraph::passADependsOnB(const RenderPassDescriptionBase& a, const RenderPassDescriptionBase& b)
  448. {
  449. // Render targets
  450. {
  451. // Compute the 3 types of dependencies
  452. const BitSet<MAX_RENDER_GRAPH_RENDER_TARGETS, U64> aReadBWrite = a.m_readRtMask & b.m_writeRtMask;
  453. const BitSet<MAX_RENDER_GRAPH_RENDER_TARGETS, U64> aWriteBRead = a.m_writeRtMask & b.m_readRtMask;
  454. const BitSet<MAX_RENDER_GRAPH_RENDER_TARGETS, U64> aWriteBWrite = a.m_writeRtMask & b.m_writeRtMask;
  455. const BitSet<MAX_RENDER_GRAPH_RENDER_TARGETS, U64> fullDep = aReadBWrite | aWriteBRead | aWriteBWrite;
  456. if(fullDep.getAny())
  457. {
  458. // There might be an overlap
  459. for(const RenderPassDependency& aDep : a.m_rtDeps)
  460. {
  461. if(!fullDep.get(aDep.m_texture.m_handle.m_idx))
  462. {
  463. continue;
  464. }
  465. for(const RenderPassDependency& bDep : b.m_rtDeps)
  466. {
  467. if(aDep.m_texture.m_handle != bDep.m_texture.m_handle)
  468. {
  469. continue;
  470. }
  471. if(!((aDep.m_texture.m_usage | bDep.m_texture.m_usage) & TextureUsageBit::ALL_WRITE))
  472. {
  473. // Don't care about read to read deps
  474. continue;
  475. }
  476. if(overlappingTextureSubresource(aDep.m_texture.m_subresource, bDep.m_texture.m_subresource))
  477. {
  478. return true;
  479. }
  480. }
  481. }
  482. }
  483. }
  484. // Buffers
  485. if(a.m_readBuffMask || a.m_writeBuffMask)
  486. {
  487. const BitSet<MAX_RENDER_GRAPH_BUFFERS, U64> aReadBWrite = a.m_readBuffMask & b.m_writeBuffMask;
  488. const BitSet<MAX_RENDER_GRAPH_BUFFERS, U64> aWriteBRead = a.m_writeBuffMask & b.m_readBuffMask;
  489. const BitSet<MAX_RENDER_GRAPH_BUFFERS, U64> aWriteBWrite = a.m_writeBuffMask & b.m_writeBuffMask;
  490. const BitSet<MAX_RENDER_GRAPH_BUFFERS, U64> fullDep = aReadBWrite | aWriteBRead | aWriteBWrite;
  491. if(fullDep.getAny())
  492. {
  493. // There might be an overlap
  494. for(const RenderPassDependency& aDep : a.m_buffDeps)
  495. {
  496. if(!fullDep.get(aDep.m_buffer.m_handle.m_idx))
  497. {
  498. continue;
  499. }
  500. for(const RenderPassDependency& bDep : b.m_buffDeps)
  501. {
  502. if(aDep.m_buffer.m_handle != bDep.m_buffer.m_handle)
  503. {
  504. continue;
  505. }
  506. if(!((aDep.m_buffer.m_usage | bDep.m_buffer.m_usage) & BufferUsageBit::ALL_WRITE))
  507. {
  508. // Don't care about read to read deps
  509. continue;
  510. }
  511. // TODO: Take into account the ranges
  512. return true;
  513. }
  514. }
  515. }
  516. }
  517. // AS
  518. if(a.m_readAsMask || a.m_writeAsMask)
  519. {
  520. const BitSet<MAX_RENDER_GRAPH_ACCELERATION_STRUCTURES, U32> aReadBWrite = a.m_readAsMask & b.m_writeAsMask;
  521. const BitSet<MAX_RENDER_GRAPH_ACCELERATION_STRUCTURES, U32> aWriteBRead = a.m_writeAsMask & b.m_readAsMask;
  522. const BitSet<MAX_RENDER_GRAPH_ACCELERATION_STRUCTURES, U32> aWriteBWrite = a.m_writeAsMask & b.m_writeAsMask;
  523. const BitSet<MAX_RENDER_GRAPH_ACCELERATION_STRUCTURES, U32> fullDep = aReadBWrite | aWriteBRead | aWriteBWrite;
  524. if(fullDep)
  525. {
  526. for(const RenderPassDependency& aDep : a.m_asDeps)
  527. {
  528. if(!fullDep.get(aDep.m_as.m_handle.m_idx))
  529. {
  530. continue;
  531. }
  532. for(const RenderPassDependency& bDep : b.m_asDeps)
  533. {
  534. if(aDep.m_as.m_handle != bDep.m_as.m_handle)
  535. {
  536. continue;
  537. }
  538. if(!((aDep.m_as.m_usage | bDep.m_as.m_usage) & AccelerationStructureUsageBit::ALL_WRITE))
  539. {
  540. // Don't care about read to read deps
  541. continue;
  542. }
  543. return true;
  544. }
  545. }
  546. }
  547. }
  548. return false;
  549. }
  550. Bool RenderGraph::passHasUnmetDependencies(const BakeContext& ctx, U32 passIdx)
  551. {
  552. Bool depends = false;
  553. if(ctx.m_batches.getSize() > 0)
  554. {
  555. // Check if the deps of passIdx are all in a batch
  556. for(const U32 depPassIdx : ctx.m_passes[passIdx].m_dependsOn)
  557. {
  558. if(ctx.m_passIsInBatch.get(depPassIdx) == false)
  559. {
  560. // Dependency pass is not in a batch
  561. depends = true;
  562. break;
  563. }
  564. }
  565. }
  566. else
  567. {
  568. // First batch, check if passIdx depends on any pass
  569. depends = ctx.m_passes[passIdx].m_dependsOn.getSize() != 0;
  570. }
  571. return depends;
  572. }
  573. RenderGraph::BakeContext* RenderGraph::newContext(const RenderGraphDescription& descr, StackAllocator<U8>& alloc)
  574. {
  575. // Allocate
  576. BakeContext* ctx = alloc.newInstance<BakeContext>(alloc);
  577. // Init the resources
  578. ctx->m_rts.create(alloc, descr.m_renderTargets.getSize());
  579. for(U32 rtIdx = 0; rtIdx < ctx->m_rts.getSize(); ++rtIdx)
  580. {
  581. RT& outRt = ctx->m_rts[rtIdx];
  582. const RenderGraphDescription::RT& inRt = descr.m_renderTargets[rtIdx];
  583. const Bool imported = inRt.m_importedTex.isCreated();
  584. if(imported)
  585. {
  586. // It's imported
  587. outRt.m_texture = inRt.m_importedTex;
  588. }
  589. else
  590. {
  591. // Need to create new
  592. // Create a new TextureInitInfo with the derived usage
  593. TextureInitInfo initInf = inRt.m_initInfo;
  594. initInf.m_usage = inRt.m_usageDerivedByDeps;
  595. ANKI_ASSERT(initInf.m_usage != TextureUsageBit::NONE && "Probably not referenced by any pass");
  596. // Create the new hash
  597. const U64 hash = appendHash(&initInf.m_usage, sizeof(initInf.m_usage), inRt.m_hash);
  598. // Get or create the texture
  599. outRt.m_texture = getOrCreateRenderTarget(initInf, hash);
  600. }
  601. // Init the usage
  602. const U32 surfOrVolumeCount = getTextureSurfOrVolCount(outRt.m_texture);
  603. outRt.m_surfOrVolUsages.create(alloc, surfOrVolumeCount, TextureUsageBit::NONE);
  604. if(imported && inRt.m_importedAndUndefinedUsage)
  605. {
  606. // Get the usage from previous frames
  607. // Create a new hash because our hash map dislikes concurent keys.
  608. const U64 uuid = outRt.m_texture->getUuid();
  609. const U64 hash = computeHash(&uuid, sizeof(uuid));
  610. auto it = m_importedRenderTargets.find(hash);
  611. ANKI_ASSERT(it != m_importedRenderTargets.getEnd() && "Can't find the imported RT");
  612. ANKI_ASSERT(it->m_surfOrVolLastUsages.getSize() == surfOrVolumeCount);
  613. for(U32 surfOrVolIdx = 0; surfOrVolIdx < surfOrVolumeCount; ++surfOrVolIdx)
  614. {
  615. outRt.m_surfOrVolUsages[surfOrVolIdx] = it->m_surfOrVolLastUsages[surfOrVolIdx];
  616. }
  617. }
  618. else if(imported)
  619. {
  620. // Set the usage that was given by the user
  621. for(U32 surfOrVolIdx = 0; surfOrVolIdx < surfOrVolumeCount; ++surfOrVolIdx)
  622. {
  623. outRt.m_surfOrVolUsages[surfOrVolIdx] = inRt.m_importedLastKnownUsage;
  624. }
  625. }
  626. outRt.m_lastBatchThatTransitionedIt.create(alloc, surfOrVolumeCount, MAX_U16);
  627. outRt.m_imported = imported;
  628. }
  629. // Buffers
  630. ctx->m_buffers.create(alloc, descr.m_buffers.getSize());
  631. for(U32 buffIdx = 0; buffIdx < ctx->m_buffers.getSize(); ++buffIdx)
  632. {
  633. ctx->m_buffers[buffIdx].m_usage = descr.m_buffers[buffIdx].m_usage;
  634. ANKI_ASSERT(descr.m_buffers[buffIdx].m_importedBuff.isCreated());
  635. ctx->m_buffers[buffIdx].m_buffer = descr.m_buffers[buffIdx].m_importedBuff;
  636. ctx->m_buffers[buffIdx].m_offset = descr.m_buffers[buffIdx].m_offset;
  637. ctx->m_buffers[buffIdx].m_range = descr.m_buffers[buffIdx].m_range;
  638. }
  639. // AS
  640. ctx->m_as.create(alloc, descr.m_as.getSize());
  641. for(U32 i = 0; i < descr.m_as.getSize(); ++i)
  642. {
  643. ctx->m_as[i].m_usage = descr.m_as[i].m_usage;
  644. ctx->m_as[i].m_as = descr.m_as[i].m_importedAs;
  645. ANKI_ASSERT(ctx->m_as[i].m_as.isCreated());
  646. }
  647. ctx->m_gatherStatistics = descr.m_gatherStatistics;
  648. return ctx;
  649. }
  650. void RenderGraph::initRenderPassesAndSetDeps(const RenderGraphDescription& descr, StackAllocator<U8>& alloc)
  651. {
  652. BakeContext& ctx = *m_ctx;
  653. const U32 passCount = descr.m_passes.getSize();
  654. ANKI_ASSERT(passCount > 0);
  655. ctx.m_passes.create(alloc, passCount);
  656. for(U32 passIdx = 0; passIdx < passCount; ++passIdx)
  657. {
  658. const RenderPassDescriptionBase& inPass = *descr.m_passes[passIdx];
  659. Pass& outPass = ctx.m_passes[passIdx];
  660. outPass.m_callback.copy(inPass.m_callback, alloc);
  661. // Create consumer info
  662. outPass.m_consumedTextures.resize(alloc, inPass.m_rtDeps.getSize());
  663. for(U32 depIdx = 0; depIdx < inPass.m_rtDeps.getSize(); ++depIdx)
  664. {
  665. const RenderPassDependency& inDep = inPass.m_rtDeps[depIdx];
  666. ANKI_ASSERT(inDep.m_type == RenderPassDependency::Type::TEXTURE);
  667. RenderPassDependency::TextureInfo& inf = outPass.m_consumedTextures[depIdx];
  668. ANKI_ASSERT(sizeof(inf) == sizeof(inDep.m_texture));
  669. memcpy(&inf, &inDep.m_texture, sizeof(inf));
  670. }
  671. // Create command buffers and framebuffer
  672. if(inPass.m_type == RenderPassDescriptionBase::Type::GRAPHICS)
  673. {
  674. const GraphicsRenderPassDescription& graphicsPass =
  675. static_cast<const GraphicsRenderPassDescription&>(inPass);
  676. if(graphicsPass.hasFramebuffer())
  677. {
  678. Bool drawsToPresentable;
  679. outPass.fb() = getOrCreateFramebuffer(graphicsPass.m_fbDescr, &graphicsPass.m_rtHandles[0],
  680. inPass.m_name.cstr(), drawsToPresentable);
  681. outPass.m_fbRenderArea = graphicsPass.m_fbRenderArea;
  682. outPass.m_drawsToPresentable = drawsToPresentable;
  683. }
  684. else
  685. {
  686. ANKI_ASSERT(inPass.m_secondLevelCmdbsCount == 0 && "Can't have second level cmdbs");
  687. }
  688. }
  689. else
  690. {
  691. ANKI_ASSERT(inPass.m_secondLevelCmdbsCount == 0 && "Can't have second level cmdbs");
  692. }
  693. // Set dependencies by checking all previous subpasses.
  694. U32 prevPassIdx = passIdx;
  695. while(prevPassIdx--)
  696. {
  697. const RenderPassDescriptionBase& prevPass = *descr.m_passes[prevPassIdx];
  698. if(passADependsOnB(inPass, prevPass))
  699. {
  700. outPass.m_dependsOn.emplaceBack(alloc, prevPassIdx);
  701. }
  702. }
  703. }
  704. }
  705. void RenderGraph::initBatches()
  706. {
  707. ANKI_ASSERT(m_ctx);
  708. U passesAssignedToBatchCount = 0;
  709. const U passCount = m_ctx->m_passes.getSize();
  710. ANKI_ASSERT(passCount > 0);
  711. Bool setTimestamp = m_ctx->m_gatherStatistics;
  712. while(passesAssignedToBatchCount < passCount)
  713. {
  714. m_ctx->m_batches.emplaceBack(m_ctx->m_alloc);
  715. Batch& batch = m_ctx->m_batches.getBack();
  716. Bool drawsToPresentable = false;
  717. for(U32 i = 0; i < passCount; ++i)
  718. {
  719. if(!m_ctx->m_passIsInBatch.get(i) && !passHasUnmetDependencies(*m_ctx, i))
  720. {
  721. // Add to the batch
  722. ++passesAssignedToBatchCount;
  723. batch.m_passIndices.emplaceBack(m_ctx->m_alloc, i);
  724. // Will batch draw to the swapchain?
  725. drawsToPresentable = drawsToPresentable || m_ctx->m_passes[i].m_drawsToPresentable;
  726. }
  727. }
  728. // Get or create cmdb for the batch.
  729. // Create a new cmdb if the batch is writing to swapchain. This will help Vulkan to have a dependency of the
  730. // swap chain image acquire to the 2nd command buffer instead of adding it to a single big cmdb.
  731. if(m_ctx->m_graphicsCmdbs.isEmpty() || drawsToPresentable)
  732. {
  733. CommandBufferInitInfo cmdbInit;
  734. cmdbInit.m_flags = CommandBufferFlag::GENERAL_WORK;
  735. CommandBufferPtr cmdb = getManager().newCommandBuffer(cmdbInit);
  736. m_ctx->m_graphicsCmdbs.emplaceBack(m_ctx->m_alloc, cmdb);
  737. batch.m_cmdb = cmdb.get();
  738. // Maybe write a timestamp
  739. if(ANKI_UNLIKELY(setTimestamp))
  740. {
  741. setTimestamp = false;
  742. TimestampQueryPtr query = getManager().newTimestampQuery();
  743. cmdb->resetTimestampQuery(query);
  744. cmdb->writeTimestamp(query);
  745. m_statistics.m_nextTimestamp = (m_statistics.m_nextTimestamp + 1) % MAX_TIMESTAMPS_BUFFERED;
  746. m_statistics.m_timestamps[m_statistics.m_nextTimestamp * 2] = query;
  747. }
  748. }
  749. else
  750. {
  751. batch.m_cmdb = m_ctx->m_graphicsCmdbs.getBack().get();
  752. }
  753. // Mark batch's passes done
  754. for(U32 passIdx : m_ctx->m_batches.getBack().m_passIndices)
  755. {
  756. m_ctx->m_passIsInBatch.set(passIdx);
  757. m_ctx->m_passes[passIdx].m_batchIdx = m_ctx->m_batches.getSize() - 1;
  758. }
  759. }
  760. }
  761. void RenderGraph::initGraphicsPasses(const RenderGraphDescription& descr, StackAllocator<U8>& alloc)
  762. {
  763. BakeContext& ctx = *m_ctx;
  764. const U32 passCount = descr.m_passes.getSize();
  765. ANKI_ASSERT(passCount > 0);
  766. for(U32 passIdx = 0; passIdx < passCount; ++passIdx)
  767. {
  768. const RenderPassDescriptionBase& inPass = *descr.m_passes[passIdx];
  769. Pass& outPass = ctx.m_passes[passIdx];
  770. // Create command buffers and framebuffer
  771. if(inPass.m_type == RenderPassDescriptionBase::Type::GRAPHICS)
  772. {
  773. const GraphicsRenderPassDescription& graphicsPass =
  774. static_cast<const GraphicsRenderPassDescription&>(inPass);
  775. if(graphicsPass.hasFramebuffer())
  776. {
  777. // Init the usage bits
  778. TextureUsageBit usage;
  779. for(U i = 0; i < graphicsPass.m_fbDescr.m_colorAttachmentCount; ++i)
  780. {
  781. getCrntUsage(graphicsPass.m_rtHandles[i], outPass.m_batchIdx,
  782. TextureSubresourceInfo(graphicsPass.m_fbDescr.m_colorAttachments[i].m_surface), usage);
  783. outPass.m_colorUsages[i] = usage;
  784. }
  785. if(!!graphicsPass.m_fbDescr.m_depthStencilAttachment.m_aspect)
  786. {
  787. TextureSubresourceInfo subresource =
  788. TextureSubresourceInfo(graphicsPass.m_fbDescr.m_depthStencilAttachment.m_surface,
  789. graphicsPass.m_fbDescr.m_depthStencilAttachment.m_aspect);
  790. getCrntUsage(graphicsPass.m_rtHandles[MAX_COLOR_ATTACHMENTS], outPass.m_batchIdx, subresource,
  791. usage);
  792. outPass.m_dsUsage = usage;
  793. }
  794. // Do some pre-work for the second level command buffers
  795. if(inPass.m_secondLevelCmdbsCount)
  796. {
  797. outPass.m_secondLevelCmdbs.create(alloc, inPass.m_secondLevelCmdbsCount);
  798. CommandBufferInitInfo& cmdbInit = outPass.m_secondLevelCmdbInitInfo;
  799. cmdbInit.m_flags = CommandBufferFlag::GENERAL_WORK | CommandBufferFlag::SECOND_LEVEL;
  800. ANKI_ASSERT(cmdbInit.m_framebuffer.isCreated());
  801. cmdbInit.m_colorAttachmentUsages = outPass.m_colorUsages;
  802. cmdbInit.m_depthStencilAttachmentUsage = outPass.m_dsUsage;
  803. }
  804. }
  805. else
  806. {
  807. ANKI_ASSERT(inPass.m_secondLevelCmdbsCount == 0 && "Can't have second level cmdbs");
  808. }
  809. }
  810. else
  811. {
  812. ANKI_ASSERT(inPass.m_secondLevelCmdbsCount == 0 && "Can't have second level cmdbs");
  813. }
  814. }
  815. }
  816. template<typename TFunc>
  817. void RenderGraph::iterateSurfsOrVolumes(const TexturePtr& tex, const TextureSubresourceInfo& subresource, TFunc func)
  818. {
  819. for(U32 mip = subresource.m_firstMipmap; mip < subresource.m_firstMipmap + subresource.m_mipmapCount; ++mip)
  820. {
  821. for(U32 layer = subresource.m_firstLayer; layer < subresource.m_firstLayer + subresource.m_layerCount; ++layer)
  822. {
  823. for(U32 face = subresource.m_firstFace; face < U32(subresource.m_firstFace + subresource.m_faceCount);
  824. ++face)
  825. {
  826. // Compute surf or vol idx
  827. const U32 faceCount = textureTypeIsCube(tex->getTextureType()) ? 6 : 1;
  828. const U32 idx = (faceCount * tex->getLayerCount()) * mip + faceCount * layer + face;
  829. const TextureSurfaceInfo surf(mip, 0, face, layer);
  830. if(!func(idx, surf))
  831. {
  832. return;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. void RenderGraph::setTextureBarrier(Batch& batch, const RenderPassDependency& dep)
  839. {
  840. ANKI_ASSERT(dep.m_type == RenderPassDependency::Type::TEXTURE);
  841. BakeContext& ctx = *m_ctx;
  842. const U32 batchIdx = U32(&batch - &ctx.m_batches[0]);
  843. const U32 rtIdx = dep.m_texture.m_handle.m_idx;
  844. const TextureUsageBit depUsage = dep.m_texture.m_usage;
  845. RT& rt = ctx.m_rts[rtIdx];
  846. iterateSurfsOrVolumes(
  847. rt.m_texture, dep.m_texture.m_subresource, [&](U32 surfOrVolIdx, const TextureSurfaceInfo& surf) {
  848. TextureUsageBit& crntUsage = rt.m_surfOrVolUsages[surfOrVolIdx];
  849. if(crntUsage != depUsage)
  850. {
  851. // Check if we can merge barriers
  852. if(rt.m_lastBatchThatTransitionedIt[surfOrVolIdx] == batchIdx)
  853. {
  854. // Will merge the barriers
  855. crntUsage |= depUsage;
  856. [[maybe_unused]] Bool found = false;
  857. for(TextureBarrier& b : batch.m_textureBarriersBefore)
  858. {
  859. if(b.m_idx == rtIdx && b.m_surface == surf)
  860. {
  861. b.m_usageAfter |= depUsage;
  862. found = true;
  863. break;
  864. }
  865. }
  866. ANKI_ASSERT(found);
  867. }
  868. else
  869. {
  870. // Create a new barrier for this surface
  871. batch.m_textureBarriersBefore.emplaceBack(ctx.m_alloc, rtIdx, crntUsage, depUsage, surf);
  872. crntUsage = depUsage;
  873. rt.m_lastBatchThatTransitionedIt[surfOrVolIdx] = U16(batchIdx);
  874. }
  875. }
  876. return true;
  877. });
  878. }
  879. void RenderGraph::setBatchBarriers(const RenderGraphDescription& descr)
  880. {
  881. BakeContext& ctx = *m_ctx;
  882. const StackAllocator<U8>& alloc = ctx.m_alloc;
  883. // For all batches
  884. for(Batch& batch : ctx.m_batches)
  885. {
  886. BitSet<MAX_RENDER_GRAPH_BUFFERS, U64> buffHasBarrierMask(false);
  887. BitSet<MAX_RENDER_GRAPH_ACCELERATION_STRUCTURES, U32> asHasBarrierMask(false);
  888. // For all passes of that batch
  889. for(U32 passIdx : batch.m_passIndices)
  890. {
  891. const RenderPassDescriptionBase& pass = *descr.m_passes[passIdx];
  892. // Do textures
  893. for(const RenderPassDependency& dep : pass.m_rtDeps)
  894. {
  895. setTextureBarrier(batch, dep);
  896. }
  897. // Do buffers
  898. for(const RenderPassDependency& dep : pass.m_buffDeps)
  899. {
  900. const U32 buffIdx = dep.m_buffer.m_handle.m_idx;
  901. const BufferUsageBit depUsage = dep.m_buffer.m_usage;
  902. BufferUsageBit& crntUsage = ctx.m_buffers[buffIdx].m_usage;
  903. if(depUsage == crntUsage)
  904. {
  905. continue;
  906. }
  907. const Bool buffHasBarrier = buffHasBarrierMask.get(buffIdx);
  908. if(!buffHasBarrier)
  909. {
  910. // Buff hasn't had a barrier in this batch, add a new barrier
  911. batch.m_bufferBarriersBefore.emplaceBack(alloc, buffIdx, crntUsage, depUsage);
  912. crntUsage = depUsage;
  913. buffHasBarrierMask.set(buffIdx);
  914. }
  915. else
  916. {
  917. // Buff already in a barrier, merge the 2 barriers
  918. BufferBarrier* barrierToMergeTo = nullptr;
  919. for(BufferBarrier& b : batch.m_bufferBarriersBefore)
  920. {
  921. if(b.m_idx == buffIdx)
  922. {
  923. barrierToMergeTo = &b;
  924. break;
  925. }
  926. }
  927. ANKI_ASSERT(barrierToMergeTo);
  928. ANKI_ASSERT(!!barrierToMergeTo->m_usageAfter);
  929. barrierToMergeTo->m_usageAfter |= depUsage;
  930. crntUsage = barrierToMergeTo->m_usageAfter;
  931. }
  932. }
  933. // Do AS
  934. for(const RenderPassDependency& dep : pass.m_asDeps)
  935. {
  936. const U32 asIdx = dep.m_as.m_handle.m_idx;
  937. const AccelerationStructureUsageBit depUsage = dep.m_as.m_usage;
  938. AccelerationStructureUsageBit& crntUsage = ctx.m_as[asIdx].m_usage;
  939. if(depUsage == crntUsage)
  940. {
  941. continue;
  942. }
  943. const Bool asHasBarrierInThisBatch = asHasBarrierMask.get(asIdx);
  944. if(!asHasBarrierInThisBatch)
  945. {
  946. // AS doesn't have a barrier in this batch, create a new one
  947. batch.m_asBarriersBefore.emplaceBack(alloc, asIdx, crntUsage, depUsage);
  948. crntUsage = depUsage;
  949. asHasBarrierMask.set(asIdx);
  950. }
  951. else
  952. {
  953. // AS already has a barrier, merge the 2 barriers
  954. ASBarrier* barrierToMergeTo = nullptr;
  955. for(ASBarrier& other : batch.m_asBarriersBefore)
  956. {
  957. if(other.m_idx == asIdx)
  958. {
  959. barrierToMergeTo = &other;
  960. break;
  961. }
  962. }
  963. ANKI_ASSERT(barrierToMergeTo);
  964. ANKI_ASSERT(!!barrierToMergeTo->m_usageAfter);
  965. barrierToMergeTo->m_usageAfter |= depUsage;
  966. crntUsage = barrierToMergeTo->m_usageAfter;
  967. }
  968. }
  969. } // For all passes
  970. #if ANKI_DBG_RENDER_GRAPH
  971. // Sort the barriers to ease the dumped graph
  972. std::sort(batch.m_textureBarriersBefore.getBegin(), batch.m_textureBarriersBefore.getEnd(),
  973. [&](const TextureBarrier& a, const TextureBarrier& b) {
  974. const U aidx = a.m_idx;
  975. const U bidx = b.m_idx;
  976. if(aidx == bidx)
  977. {
  978. if(a.m_surface.m_level != b.m_surface.m_level)
  979. {
  980. return a.m_surface.m_level < b.m_surface.m_level;
  981. }
  982. else if(a.m_surface.m_face != b.m_surface.m_face)
  983. {
  984. return a.m_surface.m_face < b.m_surface.m_face;
  985. }
  986. else if(a.m_surface.m_layer != b.m_surface.m_layer)
  987. {
  988. return a.m_surface.m_layer < b.m_surface.m_layer;
  989. }
  990. else
  991. {
  992. return false;
  993. }
  994. }
  995. else
  996. {
  997. return aidx < bidx;
  998. }
  999. });
  1000. std::sort(batch.m_bufferBarriersBefore.getBegin(), batch.m_bufferBarriersBefore.getEnd(),
  1001. [&](const BufferBarrier& a, const BufferBarrier& b) {
  1002. return a.m_idx < b.m_idx;
  1003. });
  1004. std::sort(batch.m_asBarriersBefore.getBegin(), batch.m_asBarriersBefore.getEnd(),
  1005. [&](const ASBarrier& a, const ASBarrier& b) {
  1006. return a.m_idx < b.m_idx;
  1007. });
  1008. #endif
  1009. } // For all batches
  1010. }
  1011. void RenderGraph::compileNewGraph(const RenderGraphDescription& descr, StackAllocator<U8>& alloc)
  1012. {
  1013. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_COMPILE);
  1014. // Init the context
  1015. BakeContext& ctx = *newContext(descr, alloc);
  1016. m_ctx = &ctx;
  1017. // Init the passes and find the dependencies between passes
  1018. initRenderPassesAndSetDeps(descr, alloc);
  1019. // Walk the graph and create pass batches
  1020. initBatches();
  1021. // Now that we know the batches every pass belongs init the graphics passes
  1022. initGraphicsPasses(descr, alloc);
  1023. // Create barriers between batches
  1024. setBatchBarriers(descr);
  1025. #if ANKI_DBG_RENDER_GRAPH
  1026. if(dumpDependencyDotFile(descr, ctx, "./"))
  1027. {
  1028. ANKI_LOGF("Won't recover on debug code");
  1029. }
  1030. #endif
  1031. }
  1032. TexturePtr RenderGraph::getTexture(RenderTargetHandle handle) const
  1033. {
  1034. ANKI_ASSERT(m_ctx->m_rts[handle.m_idx].m_texture.isCreated());
  1035. return m_ctx->m_rts[handle.m_idx].m_texture;
  1036. }
  1037. BufferPtr RenderGraph::getBuffer(BufferHandle handle) const
  1038. {
  1039. ANKI_ASSERT(m_ctx->m_buffers[handle.m_idx].m_buffer.isCreated());
  1040. return m_ctx->m_buffers[handle.m_idx].m_buffer;
  1041. }
  1042. AccelerationStructurePtr RenderGraph::getAs(AccelerationStructureHandle handle) const
  1043. {
  1044. ANKI_ASSERT(m_ctx->m_as[handle.m_idx].m_as.isCreated());
  1045. return m_ctx->m_as[handle.m_idx].m_as;
  1046. }
  1047. void RenderGraph::runSecondLevel(U32 threadIdx)
  1048. {
  1049. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_2ND_LEVEL);
  1050. ANKI_ASSERT(m_ctx);
  1051. RenderPassWorkContext ctx;
  1052. ctx.m_rgraph = this;
  1053. ctx.m_currentSecondLevelCommandBufferIndex = threadIdx;
  1054. for(Pass& p : m_ctx->m_passes)
  1055. {
  1056. const U32 size = p.m_secondLevelCmdbs.getSize();
  1057. if(threadIdx < size)
  1058. {
  1059. ANKI_ASSERT(!p.m_secondLevelCmdbs[threadIdx].isCreated());
  1060. p.m_secondLevelCmdbs[threadIdx] = getManager().newCommandBuffer(p.m_secondLevelCmdbInitInfo);
  1061. ctx.m_commandBuffer = p.m_secondLevelCmdbs[threadIdx];
  1062. ctx.m_secondLevelCommandBufferCount = size;
  1063. ctx.m_passIdx = U32(&p - &m_ctx->m_passes[0]);
  1064. ctx.m_batchIdx = p.m_batchIdx;
  1065. ANKI_ASSERT(ctx.m_commandBuffer.isCreated());
  1066. {
  1067. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_CALLBACK);
  1068. p.m_callback(ctx);
  1069. }
  1070. ctx.m_commandBuffer->flush();
  1071. }
  1072. }
  1073. }
  1074. void RenderGraph::run() const
  1075. {
  1076. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_RUN);
  1077. ANKI_ASSERT(m_ctx);
  1078. RenderPassWorkContext ctx;
  1079. ctx.m_rgraph = this;
  1080. ctx.m_currentSecondLevelCommandBufferIndex = 0;
  1081. ctx.m_secondLevelCommandBufferCount = 0;
  1082. for(const Batch& batch : m_ctx->m_batches)
  1083. {
  1084. ctx.m_commandBuffer.reset(batch.m_cmdb);
  1085. CommandBufferPtr& cmdb = ctx.m_commandBuffer;
  1086. // Set the barriers
  1087. for(const TextureBarrier& barrier : batch.m_textureBarriersBefore)
  1088. {
  1089. cmdb->setTextureSurfaceBarrier(m_ctx->m_rts[barrier.m_idx].m_texture, barrier.m_usageBefore,
  1090. barrier.m_usageAfter, barrier.m_surface);
  1091. }
  1092. for(const BufferBarrier& barrier : batch.m_bufferBarriersBefore)
  1093. {
  1094. const Buffer& b = m_ctx->m_buffers[barrier.m_idx];
  1095. cmdb->setBufferBarrier(b.m_buffer, barrier.m_usageBefore, barrier.m_usageAfter, b.m_offset, b.m_range);
  1096. }
  1097. for(const ASBarrier& barrier : batch.m_asBarriersBefore)
  1098. {
  1099. cmdb->setAccelerationStructureBarrier(m_ctx->m_as[barrier.m_idx].m_as, barrier.m_usageBefore,
  1100. barrier.m_usageAfter);
  1101. }
  1102. // Call the passes
  1103. for(U32 passIdx : batch.m_passIndices)
  1104. {
  1105. const Pass& pass = m_ctx->m_passes[passIdx];
  1106. if(pass.fb().isCreated())
  1107. {
  1108. cmdb->beginRenderPass(pass.fb(), pass.m_colorUsages, pass.m_dsUsage, pass.m_fbRenderArea[0],
  1109. pass.m_fbRenderArea[1], pass.m_fbRenderArea[2], pass.m_fbRenderArea[3]);
  1110. }
  1111. const U size = pass.m_secondLevelCmdbs.getSize();
  1112. if(size == 0)
  1113. {
  1114. ctx.m_passIdx = passIdx;
  1115. ctx.m_batchIdx = pass.m_batchIdx;
  1116. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_CALLBACK);
  1117. pass.m_callback(ctx);
  1118. }
  1119. else
  1120. {
  1121. for(const CommandBufferPtr& cmdb2nd : pass.m_secondLevelCmdbs)
  1122. {
  1123. cmdb->pushSecondLevelCommandBuffer(cmdb2nd);
  1124. }
  1125. }
  1126. if(pass.fb().isCreated())
  1127. {
  1128. cmdb->endRenderPass();
  1129. }
  1130. }
  1131. }
  1132. }
  1133. void RenderGraph::flush()
  1134. {
  1135. ANKI_TRACE_SCOPED_EVENT(GR_RENDER_GRAPH_FLUSH);
  1136. for(U32 i = 0; i < m_ctx->m_graphicsCmdbs.getSize(); ++i)
  1137. {
  1138. if(ANKI_UNLIKELY(m_ctx->m_gatherStatistics && i == m_ctx->m_graphicsCmdbs.getSize() - 1))
  1139. {
  1140. // Write a timestamp before the last flush
  1141. TimestampQueryPtr query = getManager().newTimestampQuery();
  1142. m_ctx->m_graphicsCmdbs[i]->resetTimestampQuery(query);
  1143. m_ctx->m_graphicsCmdbs[i]->writeTimestamp(query);
  1144. m_statistics.m_timestamps[m_statistics.m_nextTimestamp * 2 + 1] = query;
  1145. m_statistics.m_cpuStartTimes[m_statistics.m_nextTimestamp] = HighRezTimer::getCurrentTime();
  1146. }
  1147. // Flush
  1148. m_ctx->m_graphicsCmdbs[i]->flush();
  1149. }
  1150. }
  1151. void RenderGraph::getCrntUsage(RenderTargetHandle handle, U32 batchIdx, const TextureSubresourceInfo& subresource,
  1152. TextureUsageBit& usage) const
  1153. {
  1154. usage = TextureUsageBit::NONE;
  1155. const Batch& batch = m_ctx->m_batches[batchIdx];
  1156. for(U32 passIdx : batch.m_passIndices)
  1157. {
  1158. for(const RenderPassDependency::TextureInfo& consumer : m_ctx->m_passes[passIdx].m_consumedTextures)
  1159. {
  1160. if(consumer.m_handle == handle && overlappingTextureSubresource(subresource, consumer.m_subresource))
  1161. {
  1162. usage |= consumer.m_usage;
  1163. break;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. void RenderGraph::periodicCleanup()
  1169. {
  1170. U32 rtsCleanedCount = 0;
  1171. for(RenderTargetCacheEntry& entry : m_renderTargetCache)
  1172. {
  1173. if(entry.m_texturesInUse < entry.m_textures.getSize())
  1174. {
  1175. // Should cleanup
  1176. rtsCleanedCount += entry.m_textures.getSize() - entry.m_texturesInUse;
  1177. // New array
  1178. DynamicArray<TexturePtr> newArray;
  1179. if(entry.m_texturesInUse > 0)
  1180. {
  1181. newArray.create(getAllocator(), entry.m_texturesInUse);
  1182. }
  1183. // Populate the new array
  1184. for(U32 i = 0; i < newArray.getSize(); ++i)
  1185. {
  1186. newArray[i] = std::move(entry.m_textures[i]);
  1187. }
  1188. // Destroy the old array and the rest of the textures
  1189. entry.m_textures.destroy(getAllocator());
  1190. // Move new array
  1191. entry.m_textures = std::move(newArray);
  1192. }
  1193. }
  1194. if(rtsCleanedCount > 0)
  1195. {
  1196. ANKI_GR_LOGI("Cleaned %u render targets", rtsCleanedCount);
  1197. }
  1198. }
  1199. void RenderGraph::getStatistics(RenderGraphStatistics& statistics) const
  1200. {
  1201. const U32 oldFrame = (m_statistics.m_nextTimestamp + 1) % MAX_TIMESTAMPS_BUFFERED;
  1202. if(m_statistics.m_timestamps[oldFrame * 2] && m_statistics.m_timestamps[oldFrame * 2 + 1])
  1203. {
  1204. Second start, end;
  1205. [[maybe_unused]] TimestampQueryResult res = m_statistics.m_timestamps[oldFrame * 2]->getResult(start);
  1206. ANKI_ASSERT(res == TimestampQueryResult::AVAILABLE);
  1207. res = m_statistics.m_timestamps[oldFrame * 2 + 1]->getResult(end);
  1208. ANKI_ASSERT(res == TimestampQueryResult::AVAILABLE);
  1209. const Second diff = end - start;
  1210. statistics.m_gpuTime = diff;
  1211. statistics.m_cpuStartTime = m_statistics.m_cpuStartTimes[oldFrame];
  1212. }
  1213. else
  1214. {
  1215. statistics.m_gpuTime = -1.0;
  1216. statistics.m_cpuStartTime = -1.0;
  1217. }
  1218. }
  1219. #if ANKI_DBG_RENDER_GRAPH
  1220. StringAuto RenderGraph::textureUsageToStr(StackAllocator<U8>& alloc, TextureUsageBit usage)
  1221. {
  1222. if(!usage)
  1223. {
  1224. return StringAuto(alloc, "None");
  1225. }
  1226. StringListAuto slist(alloc);
  1227. # define ANKI_TEX_USAGE(u) \
  1228. if(!!(usage & TextureUsageBit::u)) \
  1229. { \
  1230. slist.pushBackSprintf("%s", #u); \
  1231. }
  1232. ANKI_TEX_USAGE(SAMPLED_GEOMETRY);
  1233. ANKI_TEX_USAGE(SAMPLED_FRAGMENT);
  1234. ANKI_TEX_USAGE(SAMPLED_COMPUTE);
  1235. ANKI_TEX_USAGE(SAMPLED_TRACE_RAYS);
  1236. ANKI_TEX_USAGE(IMAGE_GEOMETRY_READ);
  1237. ANKI_TEX_USAGE(IMAGE_GEOMETRY_WRITE);
  1238. ANKI_TEX_USAGE(IMAGE_FRAGMENT_READ);
  1239. ANKI_TEX_USAGE(IMAGE_FRAGMENT_WRITE);
  1240. ANKI_TEX_USAGE(IMAGE_COMPUTE_READ);
  1241. ANKI_TEX_USAGE(IMAGE_COMPUTE_WRITE);
  1242. ANKI_TEX_USAGE(IMAGE_TRACE_RAYS_READ);
  1243. ANKI_TEX_USAGE(IMAGE_TRACE_RAYS_WRITE);
  1244. ANKI_TEX_USAGE(FRAMEBUFFER_ATTACHMENT_READ);
  1245. ANKI_TEX_USAGE(FRAMEBUFFER_ATTACHMENT_WRITE);
  1246. ANKI_TEX_USAGE(TRANSFER_DESTINATION);
  1247. ANKI_TEX_USAGE(GENERATE_MIPMAPS);
  1248. ANKI_TEX_USAGE(PRESENT);
  1249. ANKI_TEX_USAGE(FRAMEBUFFER_SHADING_RATE);
  1250. if(!usage)
  1251. {
  1252. slist.pushBackSprintf("?");
  1253. }
  1254. # undef ANKI_TEX_USAGE
  1255. ANKI_ASSERT(!slist.isEmpty());
  1256. StringAuto str(alloc);
  1257. slist.join(" | ", str);
  1258. return str;
  1259. }
  1260. StringAuto RenderGraph::bufferUsageToStr(StackAllocator<U8>& alloc, BufferUsageBit usage)
  1261. {
  1262. StringListAuto slist(alloc);
  1263. # define ANKI_BUFF_USAGE(u) \
  1264. if(!!(usage & BufferUsageBit::u)) \
  1265. { \
  1266. slist.pushBackSprintf("%s", #u); \
  1267. }
  1268. ANKI_BUFF_USAGE(UNIFORM_GEOMETRY);
  1269. ANKI_BUFF_USAGE(UNIFORM_FRAGMENT);
  1270. ANKI_BUFF_USAGE(UNIFORM_COMPUTE);
  1271. ANKI_BUFF_USAGE(UNIFORM_TRACE_RAYS);
  1272. ANKI_BUFF_USAGE(STORAGE_GEOMETRY_READ);
  1273. ANKI_BUFF_USAGE(STORAGE_GEOMETRY_WRITE);
  1274. ANKI_BUFF_USAGE(STORAGE_FRAGMENT_READ);
  1275. ANKI_BUFF_USAGE(STORAGE_FRAGMENT_WRITE);
  1276. ANKI_BUFF_USAGE(STORAGE_COMPUTE_READ);
  1277. ANKI_BUFF_USAGE(STORAGE_COMPUTE_WRITE);
  1278. ANKI_BUFF_USAGE(STORAGE_TRACE_RAYS_READ);
  1279. ANKI_BUFF_USAGE(STORAGE_TRACE_RAYS_WRITE);
  1280. ANKI_BUFF_USAGE(TEXTURE_GEOMETRY_READ);
  1281. ANKI_BUFF_USAGE(TEXTURE_GEOMETRY_WRITE);
  1282. ANKI_BUFF_USAGE(TEXTURE_FRAGMENT_READ);
  1283. ANKI_BUFF_USAGE(TEXTURE_FRAGMENT_WRITE);
  1284. ANKI_BUFF_USAGE(TEXTURE_COMPUTE_READ);
  1285. ANKI_BUFF_USAGE(TEXTURE_COMPUTE_WRITE);
  1286. ANKI_BUFF_USAGE(TEXTURE_TRACE_RAYS_READ);
  1287. ANKI_BUFF_USAGE(TEXTURE_TRACE_RAYS_WRITE);
  1288. ANKI_BUFF_USAGE(INDEX);
  1289. ANKI_BUFF_USAGE(VERTEX);
  1290. ANKI_BUFF_USAGE(INDIRECT_COMPUTE);
  1291. ANKI_BUFF_USAGE(INDIRECT_DRAW);
  1292. ANKI_BUFF_USAGE(INDIRECT_TRACE_RAYS);
  1293. ANKI_BUFF_USAGE(TRANSFER_SOURCE);
  1294. ANKI_BUFF_USAGE(TRANSFER_DESTINATION);
  1295. ANKI_BUFF_USAGE(ACCELERATION_STRUCTURE_BUILD);
  1296. if(!usage)
  1297. {
  1298. slist.pushBackSprintf("NONE");
  1299. }
  1300. # undef ANKI_BUFF_USAGE
  1301. ANKI_ASSERT(!slist.isEmpty());
  1302. StringAuto str(alloc);
  1303. slist.join(" | ", str);
  1304. return str;
  1305. }
  1306. StringAuto RenderGraph::asUsageToStr(StackAllocator<U8>& alloc, AccelerationStructureUsageBit usage)
  1307. {
  1308. StringListAuto slist(alloc);
  1309. # define ANKI_AS_USAGE(u) \
  1310. if(!!(usage & AccelerationStructureUsageBit::u)) \
  1311. { \
  1312. slist.pushBackSprintf("%s", #u); \
  1313. }
  1314. ANKI_AS_USAGE(BUILD);
  1315. ANKI_AS_USAGE(ATTACH);
  1316. ANKI_AS_USAGE(GEOMETRY_READ);
  1317. ANKI_AS_USAGE(FRAGMENT_READ);
  1318. ANKI_AS_USAGE(COMPUTE_READ);
  1319. ANKI_AS_USAGE(TRACE_RAYS_READ);
  1320. if(!usage)
  1321. {
  1322. slist.pushBackSprintf("NONE");
  1323. }
  1324. # undef ANKI_AS_USAGE
  1325. ANKI_ASSERT(!slist.isEmpty());
  1326. StringAuto str(alloc);
  1327. slist.join(" | ", str);
  1328. return str;
  1329. }
  1330. Error RenderGraph::dumpDependencyDotFile(const RenderGraphDescription& descr, const BakeContext& ctx,
  1331. CString path) const
  1332. {
  1333. ANKI_GR_LOGW("Running with debug code");
  1334. static const Array<const char*, 5> COLORS = {"red", "green", "blue", "magenta", "cyan"};
  1335. auto alloc = ctx.m_alloc;
  1336. StringListAuto slist(alloc);
  1337. slist.pushBackSprintf("digraph {\n");
  1338. slist.pushBackSprintf("\t//splines = ortho;\nconcentrate = true;\n");
  1339. for(U32 batchIdx = 0; batchIdx < ctx.m_batches.getSize(); ++batchIdx)
  1340. {
  1341. // Set same rank
  1342. slist.pushBackSprintf("\t{rank=\"same\";");
  1343. for(U32 passIdx : ctx.m_batches[batchIdx].m_passIndices)
  1344. {
  1345. slist.pushBackSprintf("\"%s\";", descr.m_passes[passIdx]->m_name.cstr());
  1346. }
  1347. slist.pushBackSprintf("}\n");
  1348. // Print passes
  1349. for(U32 passIdx : ctx.m_batches[batchIdx].m_passIndices)
  1350. {
  1351. CString passName = descr.m_passes[passIdx]->m_name.toCString();
  1352. slist.pushBackSprintf(
  1353. "\t\"%s\"[color=%s,style=%s,shape=box];\n", passName.cstr(), COLORS[batchIdx % COLORS.getSize()],
  1354. (descr.m_passes[passIdx]->m_type == RenderPassDescriptionBase::Type::GRAPHICS) ? "bold" : "dashed");
  1355. for(U32 depIdx : ctx.m_passes[passIdx].m_dependsOn)
  1356. {
  1357. slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", descr.m_passes[depIdx]->m_name.cstr(), passName.cstr());
  1358. }
  1359. if(ctx.m_passes[passIdx].m_dependsOn.getSize() == 0)
  1360. {
  1361. slist.pushBackSprintf("\tNONE->\"%s\";\n", descr.m_passes[passIdx]->m_name.cstr());
  1362. }
  1363. }
  1364. }
  1365. # if 0
  1366. // Color the resources
  1367. slist.pushBackSprintf("subgraph cluster_0 {\n");
  1368. for(U rtIdx = 0; rtIdx < descr.m_renderTargets.getSize(); ++rtIdx)
  1369. {
  1370. slist.pushBackSprintf(
  1371. "\t\"%s\"[color=%s];\n", &descr.m_renderTargets[rtIdx].m_name[0], COLORS[rtIdx % COLORS.getSize()]);
  1372. }
  1373. slist.pushBackSprintf("}\n");
  1374. # endif
  1375. // Barriers
  1376. // slist.pushBackSprintf("subgraph cluster_1 {\n");
  1377. StringAuto prevBubble(ctx.m_alloc);
  1378. prevBubble.create("START");
  1379. for(U32 batchIdx = 0; batchIdx < ctx.m_batches.getSize(); ++batchIdx)
  1380. {
  1381. const Batch& batch = ctx.m_batches[batchIdx];
  1382. StringAuto batchName(ctx.m_alloc);
  1383. batchName.sprintf("batch%u", batchIdx);
  1384. for(U32 barrierIdx = 0; barrierIdx < batch.m_textureBarriersBefore.getSize(); ++barrierIdx)
  1385. {
  1386. const TextureBarrier& barrier = batch.m_textureBarriersBefore[barrierIdx];
  1387. StringAuto barrierLabel(ctx.m_alloc);
  1388. barrierLabel.sprintf("<b>%s</b> (mip,dp,f,l)=(%u,%u,%u,%u)<br/>%s <b>to</b> %s",
  1389. &descr.m_renderTargets[barrier.m_idx].m_name[0], barrier.m_surface.m_level,
  1390. barrier.m_surface.m_depth, barrier.m_surface.m_face, barrier.m_surface.m_layer,
  1391. textureUsageToStr(alloc, barrier.m_usageBefore).cstr(),
  1392. textureUsageToStr(alloc, barrier.m_usageAfter).cstr());
  1393. StringAuto barrierName(ctx.m_alloc);
  1394. barrierName.sprintf("%s tex barrier%u", batchName.cstr(), barrierIdx);
  1395. slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold,shape=box,label=< %s >];\n", barrierName.cstr(),
  1396. COLORS[batchIdx % COLORS.getSize()], barrierLabel.cstr());
  1397. slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), barrierName.cstr());
  1398. prevBubble = barrierName;
  1399. }
  1400. for(U32 barrierIdx = 0; barrierIdx < batch.m_bufferBarriersBefore.getSize(); ++barrierIdx)
  1401. {
  1402. const BufferBarrier& barrier = batch.m_bufferBarriersBefore[barrierIdx];
  1403. StringAuto barrierLabel(ctx.m_alloc);
  1404. barrierLabel.sprintf("<b>%s</b><br/>%s <b>to</b> %s", &descr.m_buffers[barrier.m_idx].m_name[0],
  1405. bufferUsageToStr(alloc, barrier.m_usageBefore).cstr(),
  1406. bufferUsageToStr(alloc, barrier.m_usageAfter).cstr());
  1407. StringAuto barrierName(ctx.m_alloc);
  1408. barrierName.sprintf("%s buff barrier%u", batchName.cstr(), barrierIdx);
  1409. slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold,shape=box,label=< %s >];\n", barrierName.cstr(),
  1410. COLORS[batchIdx % COLORS.getSize()], barrierLabel.cstr());
  1411. slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), barrierName.cstr());
  1412. prevBubble = barrierName;
  1413. }
  1414. for(U32 barrierIdx = 0; barrierIdx < batch.m_asBarriersBefore.getSize(); ++barrierIdx)
  1415. {
  1416. const ASBarrier& barrier = batch.m_asBarriersBefore[barrierIdx];
  1417. StringAuto barrierLabel(ctx.m_alloc);
  1418. barrierLabel.sprintf("<b>%s</b><br/>%s <b>to</b> %s", descr.m_as[barrier.m_idx].m_name.getBegin(),
  1419. asUsageToStr(alloc, barrier.m_usageBefore).cstr(),
  1420. asUsageToStr(alloc, barrier.m_usageAfter).cstr());
  1421. StringAuto barrierName(ctx.m_alloc);
  1422. barrierName.sprintf("%s AS barrier%u", batchName.cstr(), barrierIdx);
  1423. slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold,shape=box,label=< %s >];\n", barrierName.cstr(),
  1424. COLORS[batchIdx % COLORS.getSize()], barrierLabel.cstr());
  1425. slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), barrierName.cstr());
  1426. prevBubble = barrierName;
  1427. }
  1428. for(U32 passIdx : batch.m_passIndices)
  1429. {
  1430. const RenderPassDescriptionBase& pass = *descr.m_passes[passIdx];
  1431. StringAuto passName(alloc);
  1432. passName.sprintf("%s pass", pass.m_name.cstr());
  1433. slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold];\n", passName.cstr(),
  1434. COLORS[batchIdx % COLORS.getSize()]);
  1435. slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), passName.cstr());
  1436. prevBubble = passName;
  1437. }
  1438. }
  1439. // slist.pushBackSprintf("}\n");
  1440. slist.pushBackSprintf("}");
  1441. File file;
  1442. ANKI_CHECK(file.open(StringAuto(alloc).sprintf("%s/rgraph_%05u.dot", &path[0], m_version).toCString(),
  1443. FileOpenFlag::WRITE));
  1444. for(const String& s : slist)
  1445. {
  1446. ANKI_CHECK(file.writeText("%s", &s[0]));
  1447. }
  1448. return Error::NONE;
  1449. }
  1450. #endif
  1451. } // end namespace anki