| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Gr/RenderGraph.h>
- #include <AnKi/Gr/GrManager.h>
- #include <AnKi/Gr/Texture.h>
- #include <AnKi/Gr/Sampler.h>
- #include <AnKi/Gr/CommandBuffer.h>
- #include <AnKi/Util/Tracer.h>
- #include <AnKi/Util/BitSet.h>
- #include <AnKi/Util/File.h>
- #include <AnKi/Util/StringList.h>
- #include <AnKi/Util/HighRezTimer.h>
- #include <AnKi/Core/Common.h>
- namespace anki {
- #define ANKI_DBG_RENDER_GRAPH 0
- static inline U32 getTextureSurfOrVolCount(const TextureInternalPtr& tex)
- {
- return tex->getMipmapCount() * tex->getLayerCount() * (textureTypeIsCube(tex->getTextureType()) ? 6 : 1);
- }
- /// Contains some extra things for render targets.
- class RenderGraph::RT
- {
- public:
- DynamicArray<TextureUsageBit, MemoryPoolPtrWrapper<StackMemoryPool>> m_surfOrVolUsages;
- DynamicArray<U16, MemoryPoolPtrWrapper<StackMemoryPool>> m_lastBatchThatTransitionedIt;
- TextureInternalPtr m_texture; ///< Hold a reference.
- Bool m_imported;
- RT(StackMemoryPool* pool)
- : m_surfOrVolUsages(pool)
- , m_lastBatchThatTransitionedIt(pool)
- {
- }
- };
- /// Same as RT but for buffers.
- class RenderGraph::BufferRange
- {
- public:
- BufferUsageBit m_usage;
- BufferInternalPtr m_buffer; ///< Hold a reference.
- PtrSize m_offset;
- PtrSize m_range;
- };
- class RenderGraph::AS
- {
- public:
- AccelerationStructureUsageBit m_usage;
- AccelerationStructurePtr m_as; ///< Hold a reference.
- };
- /// Pipeline barrier.
- class RenderGraph::TextureBarrier
- {
- public:
- U32 m_idx;
- TextureUsageBit m_usageBefore;
- TextureUsageBit m_usageAfter;
- TextureSubresourceDesc m_subresource;
- TextureBarrier(U32 rtIdx, TextureUsageBit usageBefore, TextureUsageBit usageAfter, const TextureSubresourceDesc& sub)
- : m_idx(rtIdx)
- , m_usageBefore(usageBefore)
- , m_usageAfter(usageAfter)
- , m_subresource(sub)
- {
- }
- };
- /// Pipeline barrier.
- class RenderGraph::BufferBarrier
- {
- public:
- U32 m_idx;
- BufferUsageBit m_usageBefore;
- BufferUsageBit m_usageAfter;
- BufferBarrier(U32 buffIdx, BufferUsageBit usageBefore, BufferUsageBit usageAfter)
- : m_idx(buffIdx)
- , m_usageBefore(usageBefore)
- , m_usageAfter(usageAfter)
- {
- }
- };
- /// Pipeline barrier.
- class RenderGraph::ASBarrier
- {
- public:
- U32 m_idx;
- AccelerationStructureUsageBit m_usageBefore;
- AccelerationStructureUsageBit m_usageAfter;
- ASBarrier(U32 asIdx, AccelerationStructureUsageBit usageBefore, AccelerationStructureUsageBit usageAfter)
- : m_idx(asIdx)
- , m_usageBefore(usageBefore)
- , m_usageAfter(usageAfter)
- {
- }
- };
- /// Contains some extra things the RenderPassBase cannot hold.
- class RenderGraph::Pass
- {
- public:
- // WARNING!!!!!: Whatever you put here needs manual destruction in RenderGraph::reset()
- DynamicArray<U32, MemoryPoolPtrWrapper<StackMemoryPool>> m_dependsOn;
- DynamicArray<RenderPassDependency::TextureInfo, MemoryPoolPtrWrapper<StackMemoryPool>> m_consumedTextures;
- Function<void(RenderPassWorkContext&), MemoryPoolPtrWrapper<StackMemoryPool>> m_callback;
- class
- {
- public:
- Array<RenderTarget, kMaxColorRenderTargets> m_colorRts;
- RenderTarget m_dsRt;
- TextureView m_vrsRt;
- U8 m_colorRtCount = 0;
- U8 m_vrsTexelSizeX = 0;
- U8 m_vrsTexelSizeY = 0;
- Bool m_hasRenderpass = false;
- Array<TextureInternalPtr, kMaxColorRenderTargets + 2> m_refs;
- } m_beginRenderpassInfo;
- BaseString<MemoryPoolPtrWrapper<StackMemoryPool>> m_name;
- U32 m_batchIdx ANKI_DEBUG_CODE(= kMaxU32);
- Bool m_writesToSwapchain = false;
- Pass(StackMemoryPool* pool)
- : m_dependsOn(pool)
- , m_consumedTextures(pool)
- , m_name(pool)
- {
- }
- };
- /// A batch of render passes. These passes can run in parallel.
- /// @warning It's POD. Destructor won't be called.
- class RenderGraph::Batch
- {
- public:
- DynamicArray<U32, MemoryPoolPtrWrapper<StackMemoryPool>> m_passIndices;
- DynamicArray<TextureBarrier, MemoryPoolPtrWrapper<StackMemoryPool>> m_textureBarriersBefore;
- DynamicArray<BufferBarrier, MemoryPoolPtrWrapper<StackMemoryPool>> m_bufferBarriersBefore;
- DynamicArray<ASBarrier, MemoryPoolPtrWrapper<StackMemoryPool>> m_asBarriersBefore;
- Batch(StackMemoryPool* pool)
- : m_passIndices(pool)
- , m_textureBarriersBefore(pool)
- , m_bufferBarriersBefore(pool)
- , m_asBarriersBefore(pool)
- {
- }
- Batch(Batch&& b)
- {
- *this = std::move(b);
- }
- Batch& operator=(Batch&& b)
- {
- m_passIndices = std::move(b.m_passIndices);
- m_textureBarriersBefore = std::move(b.m_textureBarriersBefore);
- m_bufferBarriersBefore = std::move(b.m_bufferBarriersBefore);
- m_asBarriersBefore = std::move(b.m_asBarriersBefore);
- return *this;
- }
- };
- /// The RenderGraph build context.
- class RenderGraph::BakeContext
- {
- public:
- DynamicArray<Pass, MemoryPoolPtrWrapper<StackMemoryPool>> m_passes;
- BitSet<kMaxRenderGraphPasses, U64> m_passIsInBatch{false};
- DynamicArray<Batch, MemoryPoolPtrWrapper<StackMemoryPool>> m_batches;
- DynamicArray<RT, MemoryPoolPtrWrapper<StackMemoryPool>> m_rts;
- DynamicArray<BufferRange, MemoryPoolPtrWrapper<StackMemoryPool>> m_buffers;
- DynamicArray<AS, MemoryPoolPtrWrapper<StackMemoryPool>> m_as;
- Bool m_gatherStatistics = false;
- BakeContext(StackMemoryPool* pool)
- : m_passes(pool)
- , m_batches(pool)
- , m_rts(pool)
- , m_buffers(pool)
- , m_as(pool)
- {
- }
- };
- RenderGraph::RenderGraph(CString name)
- : GrObject(kClassType, name)
- {
- }
- RenderGraph::~RenderGraph()
- {
- ANKI_ASSERT(m_ctx == nullptr);
- }
- RenderGraph* RenderGraph::newInstance()
- {
- return anki::newInstance<RenderGraph>(GrMemoryPool::getSingleton(), "N/A");
- }
- void RenderGraph::reset()
- {
- ANKI_TRACE_SCOPED_EVENT(GrRenderGraphReset);
- if(!m_ctx)
- {
- return;
- }
- if((m_version % kPeriodicCleanupEvery) == 0)
- {
- // Do cleanup
- periodicCleanup();
- }
- // Extract the final usage of the imported RTs and clean all RTs
- for(RT& rt : m_ctx->m_rts)
- {
- if(rt.m_imported)
- {
- const U32 surfOrVolumeCount = getTextureSurfOrVolCount(rt.m_texture);
- // Create a new hash because our hash map dislikes concurent keys.
- const U64 uuid = rt.m_texture->getUuid();
- const U64 hash = computeHash(&uuid, sizeof(uuid));
- auto it = m_importedRenderTargets.find(hash);
- if(it != m_importedRenderTargets.getEnd())
- {
- // Found
- ANKI_ASSERT(it->m_surfOrVolLastUsages.getSize() == surfOrVolumeCount);
- ANKI_ASSERT(rt.m_surfOrVolUsages.getSize() == surfOrVolumeCount);
- }
- else
- {
- // Not found, create
- it = m_importedRenderTargets.emplace(hash);
- it->m_surfOrVolLastUsages.resize(surfOrVolumeCount);
- }
- // Update the usage
- for(U32 surfOrVolIdx = 0; surfOrVolIdx < surfOrVolumeCount; ++surfOrVolIdx)
- {
- it->m_surfOrVolLastUsages[surfOrVolIdx] = rt.m_surfOrVolUsages[surfOrVolIdx];
- }
- }
- rt.m_texture.reset(nullptr);
- }
- for(BufferRange& buff : m_ctx->m_buffers)
- {
- buff.m_buffer.reset(nullptr);
- }
- for(AS& as : m_ctx->m_as)
- {
- as.m_as.reset(nullptr);
- }
- for(auto& it : m_renderTargetCache)
- {
- it.m_texturesInUse = 0;
- }
- for(Pass& p : m_ctx->m_passes)
- {
- p.m_beginRenderpassInfo.m_refs.fill(TextureInternalPtr(nullptr));
- p.m_callback.destroy();
- p.m_name.destroy();
- }
- m_ctx = nullptr;
- ++m_version;
- }
- TextureInternalPtr RenderGraph::getOrCreateRenderTarget(const TextureInitInfo& initInf, U64 hash)
- {
- ANKI_ASSERT(hash);
- // Find a cache entry
- RenderTargetCacheEntry* entry = nullptr;
- auto it = m_renderTargetCache.find(hash);
- if(it == m_renderTargetCache.getEnd()) [[unlikely]]
- {
- // Didn't found the entry, create a new one
- auto it2 = m_renderTargetCache.emplace(hash);
- entry = &(*it2);
- }
- else
- {
- entry = &(*it);
- }
- ANKI_ASSERT(entry);
- // Create or pop one tex from the cache
- TextureInternalPtr tex;
- const Bool createNewTex = entry->m_textures.getSize() == entry->m_texturesInUse;
- if(!createNewTex)
- {
- // Pop
- tex = entry->m_textures[entry->m_texturesInUse++];
- }
- else
- {
- // Create it
- tex = GrManager::getSingleton().newTexture(initInf);
- ANKI_ASSERT(entry->m_texturesInUse == entry->m_textures.getSize());
- entry->m_textures.resize(entry->m_textures.getSize() + 1);
- entry->m_textures[entry->m_textures.getSize() - 1] = tex;
- ++entry->m_texturesInUse;
- }
- return tex;
- }
- Bool RenderGraph::passADependsOnB(const RenderPassBase& a, const RenderPassBase& b)
- {
- // Render targets
- {
- // Compute the 3 types of dependencies
- const BitSet<kMaxRenderGraphRenderTargets, U64> aReadBWrite = a.m_readRtMask & b.m_writeRtMask;
- const BitSet<kMaxRenderGraphRenderTargets, U64> aWriteBRead = a.m_writeRtMask & b.m_readRtMask;
- const BitSet<kMaxRenderGraphRenderTargets, U64> aWriteBWrite = a.m_writeRtMask & b.m_writeRtMask;
- const BitSet<kMaxRenderGraphRenderTargets, U64> fullDep = aReadBWrite | aWriteBRead | aWriteBWrite;
- if(fullDep.getAnySet())
- {
- // There might be an overlap
- for(const RenderPassDependency& aDep : a.m_rtDeps)
- {
- if(!fullDep.get(aDep.m_texture.m_handle.m_idx))
- {
- continue;
- }
- for(const RenderPassDependency& bDep : b.m_rtDeps)
- {
- if(aDep.m_texture.m_handle != bDep.m_texture.m_handle)
- {
- continue;
- }
- if(!((aDep.m_texture.m_usage | bDep.m_texture.m_usage) & TextureUsageBit::kAllWrite))
- {
- // Don't care about read to read deps
- continue;
- }
- if(aDep.m_texture.m_subresource.overlapsWith(bDep.m_texture.m_subresource))
- {
- return true;
- }
- }
- }
- }
- }
- // Buffers
- if(a.m_readBuffMask || a.m_writeBuffMask)
- {
- const BitSet<kMaxRenderGraphBuffers, U64> aReadBWrite = a.m_readBuffMask & b.m_writeBuffMask;
- const BitSet<kMaxRenderGraphBuffers, U64> aWriteBRead = a.m_writeBuffMask & b.m_readBuffMask;
- const BitSet<kMaxRenderGraphBuffers, U64> aWriteBWrite = a.m_writeBuffMask & b.m_writeBuffMask;
- const BitSet<kMaxRenderGraphBuffers, U64> fullDep = aReadBWrite | aWriteBRead | aWriteBWrite;
- if(fullDep.getAnySet())
- {
- // There might be an overlap
- for(const RenderPassDependency& aDep : a.m_buffDeps)
- {
- if(!fullDep.get(aDep.m_buffer.m_handle.m_idx))
- {
- continue;
- }
- for(const RenderPassDependency& bDep : b.m_buffDeps)
- {
- if(aDep.m_buffer.m_handle != bDep.m_buffer.m_handle)
- {
- continue;
- }
- if(!((aDep.m_buffer.m_usage | bDep.m_buffer.m_usage) & BufferUsageBit::kAllWrite))
- {
- // Don't care about read to read deps
- continue;
- }
- // TODO: Take into account the ranges
- return true;
- }
- }
- }
- }
- // AS
- if(a.m_readAsMask || a.m_writeAsMask)
- {
- const BitSet<kMaxRenderGraphAccelerationStructures, U32> aReadBWrite = a.m_readAsMask & b.m_writeAsMask;
- const BitSet<kMaxRenderGraphAccelerationStructures, U32> aWriteBRead = a.m_writeAsMask & b.m_readAsMask;
- const BitSet<kMaxRenderGraphAccelerationStructures, U32> aWriteBWrite = a.m_writeAsMask & b.m_writeAsMask;
- const BitSet<kMaxRenderGraphAccelerationStructures, U32> fullDep = aReadBWrite | aWriteBRead | aWriteBWrite;
- if(fullDep)
- {
- for(const RenderPassDependency& aDep : a.m_asDeps)
- {
- if(!fullDep.get(aDep.m_as.m_handle.m_idx))
- {
- continue;
- }
- for(const RenderPassDependency& bDep : b.m_asDeps)
- {
- if(aDep.m_as.m_handle != bDep.m_as.m_handle)
- {
- continue;
- }
- if(!((aDep.m_as.m_usage | bDep.m_as.m_usage) & AccelerationStructureUsageBit::kAllWrite))
- {
- // Don't care about read to read deps
- continue;
- }
- return true;
- }
- }
- }
- }
- return false;
- }
- Bool RenderGraph::passHasUnmetDependencies(const BakeContext& ctx, U32 passIdx)
- {
- Bool depends = false;
- if(ctx.m_batches.getSize() > 0)
- {
- // Check if the deps of passIdx are all in a batch
- for(const U32 depPassIdx : ctx.m_passes[passIdx].m_dependsOn)
- {
- if(!ctx.m_passIsInBatch.get(depPassIdx))
- {
- // Dependency pass is not in a batch
- depends = true;
- break;
- }
- }
- }
- else
- {
- // First batch, check if passIdx depends on any pass
- depends = ctx.m_passes[passIdx].m_dependsOn.getSize() != 0;
- }
- return depends;
- }
- RenderGraph::BakeContext* RenderGraph::newContext(const RenderGraphBuilder& descr, StackMemoryPool& pool)
- {
- ANKI_TRACE_FUNCTION();
- // Allocate
- BakeContext* ctx = anki::newInstance<BakeContext>(pool, &pool);
- // Init the resources
- ctx->m_rts.resizeStorage(descr.m_renderTargets.getSize());
- for(U32 rtIdx = 0; rtIdx < descr.m_renderTargets.getSize(); ++rtIdx)
- {
- RT& outRt = *ctx->m_rts.emplaceBack(&pool);
- const RenderGraphBuilder::RT& inRt = descr.m_renderTargets[rtIdx];
- const Bool imported = inRt.m_importedTex.isCreated();
- if(imported)
- {
- // It's imported
- outRt.m_texture = inRt.m_importedTex;
- }
- else
- {
- // Need to create new
- // Create a new TextureInitInfo with the derived usage
- TextureInitInfo initInf = inRt.m_initInfo;
- initInf.m_usage = inRt.m_usageDerivedByDeps;
- ANKI_ASSERT(initInf.m_usage != TextureUsageBit::kNone && "Probably not referenced by any pass");
- // Create the new hash
- const U64 hash = appendHash(&initInf.m_usage, sizeof(initInf.m_usage), inRt.m_hash);
- // Get or create the texture
- outRt.m_texture = getOrCreateRenderTarget(initInf, hash);
- }
- // Init the usage
- const U32 surfOrVolumeCount = getTextureSurfOrVolCount(outRt.m_texture);
- outRt.m_surfOrVolUsages.resize(surfOrVolumeCount, TextureUsageBit::kNone);
- if(imported && inRt.m_importedAndUndefinedUsage)
- {
- // Get the usage from previous frames
- // Create a new hash because our hash map dislikes concurent keys.
- const U64 uuid = outRt.m_texture->getUuid();
- const U64 hash = computeHash(&uuid, sizeof(uuid));
- auto it = m_importedRenderTargets.find(hash);
- ANKI_ASSERT(it != m_importedRenderTargets.getEnd() && "Can't find the imported RT");
- ANKI_ASSERT(it->m_surfOrVolLastUsages.getSize() == surfOrVolumeCount);
- for(U32 surfOrVolIdx = 0; surfOrVolIdx < surfOrVolumeCount; ++surfOrVolIdx)
- {
- outRt.m_surfOrVolUsages[surfOrVolIdx] = it->m_surfOrVolLastUsages[surfOrVolIdx];
- }
- }
- else if(imported)
- {
- // Set the usage that was given by the user
- for(U32 surfOrVolIdx = 0; surfOrVolIdx < surfOrVolumeCount; ++surfOrVolIdx)
- {
- outRt.m_surfOrVolUsages[surfOrVolIdx] = inRt.m_importedLastKnownUsage;
- }
- }
- outRt.m_lastBatchThatTransitionedIt.resize(surfOrVolumeCount, kMaxU16);
- outRt.m_imported = imported;
- }
- // Buffers
- ctx->m_buffers.resize(descr.m_buffers.getSize());
- for(U32 buffIdx = 0; buffIdx < ctx->m_buffers.getSize(); ++buffIdx)
- {
- ctx->m_buffers[buffIdx].m_usage = descr.m_buffers[buffIdx].m_usage;
- ANKI_ASSERT(descr.m_buffers[buffIdx].m_importedBuff.isCreated());
- ctx->m_buffers[buffIdx].m_buffer = descr.m_buffers[buffIdx].m_importedBuff;
- ctx->m_buffers[buffIdx].m_offset = descr.m_buffers[buffIdx].m_offset;
- ctx->m_buffers[buffIdx].m_range = descr.m_buffers[buffIdx].m_range;
- }
- // AS
- ctx->m_as.resize(descr.m_as.getSize());
- for(U32 i = 0; i < descr.m_as.getSize(); ++i)
- {
- ctx->m_as[i].m_usage = descr.m_as[i].m_usage;
- ctx->m_as[i].m_as = descr.m_as[i].m_importedAs;
- ANKI_ASSERT(ctx->m_as[i].m_as.isCreated());
- }
- ctx->m_gatherStatistics = descr.m_gatherStatistics;
- return ctx;
- }
- void RenderGraph::initRenderPassesAndSetDeps(const RenderGraphBuilder& descr)
- {
- ANKI_TRACE_FUNCTION();
- BakeContext& ctx = *m_ctx;
- const U32 passCount = descr.m_passes.getSize();
- ANKI_ASSERT(passCount > 0);
- ctx.m_passes.resizeStorage(passCount);
- for(U32 passIdx = 0; passIdx < passCount; ++passIdx)
- {
- const RenderPassBase& inPass = *descr.m_passes[passIdx];
- Pass& outPass = *ctx.m_passes.emplaceBack(ctx.m_as.getMemoryPool().m_pool);
- outPass.m_callback = inPass.m_callback;
- outPass.m_name = inPass.m_name;
- outPass.m_writesToSwapchain = inPass.m_writesToSwapchain;
- // Create consumer info
- outPass.m_consumedTextures.resize(inPass.m_rtDeps.getSize());
- for(U32 depIdx = 0; depIdx < inPass.m_rtDeps.getSize(); ++depIdx)
- {
- const RenderPassDependency& inDep = inPass.m_rtDeps[depIdx];
- ANKI_ASSERT(inDep.m_type == RenderPassDependency::Type::kTexture);
- RenderPassDependency::TextureInfo& inf = outPass.m_consumedTextures[depIdx];
- ANKI_ASSERT(sizeof(inf) == sizeof(inDep.m_texture));
- memcpy(&inf, &inDep.m_texture, sizeof(inf));
- }
- // Set dependencies by checking all previous subpasses.
- U32 prevPassIdx = passIdx;
- while(prevPassIdx--)
- {
- const RenderPassBase& prevPass = *descr.m_passes[prevPassIdx];
- if(passADependsOnB(inPass, prevPass))
- {
- outPass.m_dependsOn.emplaceBack(prevPassIdx);
- }
- }
- }
- }
- void RenderGraph::initBatches()
- {
- ANKI_TRACE_FUNCTION();
- ANKI_ASSERT(m_ctx);
- U passesAssignedToBatchCount = 0;
- const U passCount = m_ctx->m_passes.getSize();
- ANKI_ASSERT(passCount > 0);
- while(passesAssignedToBatchCount < passCount)
- {
- Batch batch(m_ctx->m_as.getMemoryPool().m_pool);
- for(U32 i = 0; i < passCount; ++i)
- {
- if(!m_ctx->m_passIsInBatch.get(i) && !passHasUnmetDependencies(*m_ctx, i))
- {
- // Add to the batch
- ++passesAssignedToBatchCount;
- batch.m_passIndices.emplaceBack(i);
- }
- }
- // Mark batch's passes done
- for(U32 passIdx : batch.m_passIndices)
- {
- m_ctx->m_passIsInBatch.set(passIdx);
- m_ctx->m_passes[passIdx].m_batchIdx = m_ctx->m_batches.getSize();
- }
- m_ctx->m_batches.emplaceBack(std::move(batch));
- }
- }
- void RenderGraph::initGraphicsPasses(const RenderGraphBuilder& descr)
- {
- ANKI_TRACE_FUNCTION();
- BakeContext& ctx = *m_ctx;
- const U32 passCount = descr.m_passes.getSize();
- ANKI_ASSERT(passCount > 0);
- for(U32 passIdx = 0; passIdx < passCount; ++passIdx)
- {
- const RenderPassBase& baseInPass = *descr.m_passes[passIdx];
- Pass& outPass = ctx.m_passes[passIdx];
- // Create command buffers and framebuffer
- if(baseInPass.m_type == RenderPassBase::Type::kGraphics)
- {
- const GraphicsRenderPass& inPass = static_cast<const GraphicsRenderPass&>(baseInPass);
- if(inPass.m_hasRenderpass)
- {
- outPass.m_beginRenderpassInfo.m_hasRenderpass = true;
- outPass.m_beginRenderpassInfo.m_colorRtCount = inPass.m_colorRtCount;
- // Init the usage bits
- for(U32 i = 0; i < inPass.m_colorRtCount; ++i)
- {
- const GraphicsRenderPassTargetDesc& inAttachment = inPass.m_rts[i];
- RenderTarget& outAttachment = outPass.m_beginRenderpassInfo.m_colorRts[i];
- getCrntUsage(inAttachment.m_handle, outPass.m_batchIdx, inAttachment.m_subresource, outAttachment.m_usage);
- outAttachment.m_textureView = TextureView(m_ctx->m_rts[inAttachment.m_handle.m_idx].m_texture.get(), inAttachment.m_subresource);
- outPass.m_beginRenderpassInfo.m_refs[i] = m_ctx->m_rts[inAttachment.m_handle.m_idx].m_texture;
- outAttachment.m_loadOperation = inAttachment.m_loadOperation;
- outAttachment.m_storeOperation = inAttachment.m_storeOperation;
- outAttachment.m_clearValue = inAttachment.m_clearValue;
- }
- if(!!inPass.m_rts[kMaxColorRenderTargets].m_subresource.m_depthStencilAspect)
- {
- const GraphicsRenderPassTargetDesc& inAttachment = inPass.m_rts[kMaxColorRenderTargets];
- RenderTarget& outAttachment = outPass.m_beginRenderpassInfo.m_dsRt;
- getCrntUsage(inAttachment.m_handle, outPass.m_batchIdx, inAttachment.m_subresource, outAttachment.m_usage);
- outAttachment.m_textureView = TextureView(m_ctx->m_rts[inAttachment.m_handle.m_idx].m_texture.get(), inAttachment.m_subresource);
- outPass.m_beginRenderpassInfo.m_refs[kMaxColorRenderTargets] = m_ctx->m_rts[inAttachment.m_handle.m_idx].m_texture;
- outAttachment.m_loadOperation = inAttachment.m_loadOperation;
- outAttachment.m_storeOperation = inAttachment.m_storeOperation;
- outAttachment.m_stencilLoadOperation = inAttachment.m_stencilLoadOperation;
- outAttachment.m_stencilStoreOperation = inAttachment.m_stencilStoreOperation;
- outAttachment.m_clearValue = inAttachment.m_clearValue;
- }
- if(inPass.m_vrsRtTexelSizeX > 0)
- {
- const GraphicsRenderPassTargetDesc& inAttachment = inPass.m_rts[kMaxColorRenderTargets + 1];
- outPass.m_beginRenderpassInfo.m_vrsRt =
- TextureView(m_ctx->m_rts[inAttachment.m_handle.m_idx].m_texture.get(), inAttachment.m_subresource);
- outPass.m_beginRenderpassInfo.m_refs[kMaxColorRenderTargets + 1] = m_ctx->m_rts[inAttachment.m_handle.m_idx].m_texture;
- outPass.m_beginRenderpassInfo.m_vrsTexelSizeX = inPass.m_vrsRtTexelSizeX;
- outPass.m_beginRenderpassInfo.m_vrsTexelSizeY = inPass.m_vrsRtTexelSizeY;
- }
- }
- }
- }
- }
- template<typename TFunc>
- void RenderGraph::iterateSurfsOrVolumes(const Texture& tex, const TextureSubresourceDesc& subresource, TFunc func)
- {
- subresource.validate(tex);
- const U32 faceCount = textureTypeIsCube(tex.getTextureType()) ? 6 : 1;
- if(subresource.m_allSurfacesOrVolumes)
- {
- for(U32 mip = 0; mip < tex.getMipmapCount(); ++mip)
- {
- for(U32 layer = 0; layer < tex.getLayerCount(); ++layer)
- {
- for(U32 face = 0; face < faceCount; ++face)
- {
- // Compute surf or vol idx
- const U32 idx = (faceCount * tex.getLayerCount()) * mip + faceCount * layer + face;
- if(!func(idx, TextureSubresourceDesc::surface(mip, face, layer, subresource.m_depthStencilAspect)))
- {
- return;
- }
- }
- }
- }
- }
- else
- {
- const U32 idx = (faceCount * tex.getLayerCount()) * subresource.m_mipmap + faceCount * subresource.m_layer + subresource.m_face;
- func(idx, subresource);
- }
- }
- void RenderGraph::setTextureBarrier(Batch& batch, const RenderPassDependency& dep)
- {
- ANKI_ASSERT(dep.m_type == RenderPassDependency::Type::kTexture);
- BakeContext& ctx = *m_ctx;
- const U32 batchIdx = U32(&batch - &ctx.m_batches[0]);
- const U32 rtIdx = dep.m_texture.m_handle.m_idx;
- const TextureUsageBit depUsage = dep.m_texture.m_usage;
- RT& rt = ctx.m_rts[rtIdx];
- iterateSurfsOrVolumes(*rt.m_texture, dep.m_texture.m_subresource, [&](U32 surfOrVolIdx, const TextureSubresourceDesc& subresource) {
- TextureUsageBit& crntUsage = rt.m_surfOrVolUsages[surfOrVolIdx];
- const Bool skipBarrier = crntUsage == depUsage && !(crntUsage & TextureUsageBit::kAllWrite);
- if(!skipBarrier)
- {
- // Check if we can merge barriers
- if(rt.m_lastBatchThatTransitionedIt[surfOrVolIdx] == batchIdx)
- {
- // Will merge the barriers
- crntUsage |= depUsage;
- [[maybe_unused]] Bool found = false;
- for(TextureBarrier& b : batch.m_textureBarriersBefore)
- {
- if(b.m_idx == rtIdx && b.m_subresource == subresource)
- {
- b.m_usageAfter |= depUsage;
- found = true;
- break;
- }
- }
- ANKI_ASSERT(found);
- }
- else
- {
- // Create a new barrier for this surface
- batch.m_textureBarriersBefore.emplaceBack(rtIdx, crntUsage, depUsage, subresource);
- crntUsage = depUsage;
- rt.m_lastBatchThatTransitionedIt[surfOrVolIdx] = U16(batchIdx);
- }
- }
- return true;
- });
- }
- void RenderGraph::setBatchBarriers(const RenderGraphBuilder& descr)
- {
- ANKI_TRACE_FUNCTION();
- BakeContext& ctx = *m_ctx;
- // For all batches
- for(Batch& batch : ctx.m_batches)
- {
- BitSet<kMaxRenderGraphBuffers, U64> buffHasBarrierMask(false);
- BitSet<kMaxRenderGraphAccelerationStructures, U32> asHasBarrierMask(false);
- // For all passes of that batch
- for(U32 passIdx : batch.m_passIndices)
- {
- const RenderPassBase& pass = *descr.m_passes[passIdx];
- // Do textures
- for(const RenderPassDependency& dep : pass.m_rtDeps)
- {
- setTextureBarrier(batch, dep);
- }
- // Do buffers
- for(const RenderPassDependency& dep : pass.m_buffDeps)
- {
- const U32 buffIdx = dep.m_buffer.m_handle.m_idx;
- const BufferUsageBit depUsage = dep.m_buffer.m_usage;
- BufferUsageBit& crntUsage = ctx.m_buffers[buffIdx].m_usage;
- const Bool skipBarrier = crntUsage == depUsage && !(crntUsage & BufferUsageBit::kAllWrite);
- if(skipBarrier)
- {
- continue;
- }
- const Bool buffHasBarrier = buffHasBarrierMask.get(buffIdx);
- if(!buffHasBarrier)
- {
- // Buff hasn't had a barrier in this batch, add a new barrier
- batch.m_bufferBarriersBefore.emplaceBack(buffIdx, crntUsage, depUsage);
- crntUsage = depUsage;
- buffHasBarrierMask.set(buffIdx);
- }
- else
- {
- // Buff already in a barrier, merge the 2 barriers
- BufferBarrier* barrierToMergeTo = nullptr;
- for(BufferBarrier& b : batch.m_bufferBarriersBefore)
- {
- if(b.m_idx == buffIdx)
- {
- barrierToMergeTo = &b;
- break;
- }
- }
- ANKI_ASSERT(barrierToMergeTo);
- ANKI_ASSERT(!!barrierToMergeTo->m_usageAfter);
- barrierToMergeTo->m_usageAfter |= depUsage;
- crntUsage = barrierToMergeTo->m_usageAfter;
- }
- }
- // Do AS
- for(const RenderPassDependency& dep : pass.m_asDeps)
- {
- const U32 asIdx = dep.m_as.m_handle.m_idx;
- const AccelerationStructureUsageBit depUsage = dep.m_as.m_usage;
- AccelerationStructureUsageBit& crntUsage = ctx.m_as[asIdx].m_usage;
- const Bool skipBarrier = crntUsage == depUsage && !(crntUsage & AccelerationStructureUsageBit::kAllWrite);
- if(skipBarrier)
- {
- continue;
- }
- const Bool asHasBarrierInThisBatch = asHasBarrierMask.get(asIdx);
- if(!asHasBarrierInThisBatch)
- {
- // AS doesn't have a barrier in this batch, create a new one
- batch.m_asBarriersBefore.emplaceBack(asIdx, crntUsage, depUsage);
- crntUsage = depUsage;
- asHasBarrierMask.set(asIdx);
- }
- else
- {
- // AS already has a barrier, merge the 2 barriers
- ASBarrier* barrierToMergeTo = nullptr;
- for(ASBarrier& other : batch.m_asBarriersBefore)
- {
- if(other.m_idx == asIdx)
- {
- barrierToMergeTo = &other;
- break;
- }
- }
- ANKI_ASSERT(barrierToMergeTo);
- ANKI_ASSERT(!!barrierToMergeTo->m_usageAfter);
- barrierToMergeTo->m_usageAfter |= depUsage;
- crntUsage = barrierToMergeTo->m_usageAfter;
- }
- }
- } // For all passes
- ANKI_ASSERT(batch.m_bufferBarriersBefore.getSize() || batch.m_textureBarriersBefore.getSize() || batch.m_asBarriersBefore.getSize());
- #if ANKI_DBG_RENDER_GRAPH
- // Sort the barriers to ease the dumped graph
- std::sort(batch.m_textureBarriersBefore.getBegin(), batch.m_textureBarriersBefore.getEnd(),
- [&](const TextureBarrier& a, const TextureBarrier& b) {
- const U aidx = a.m_idx;
- const U bidx = b.m_idx;
- if(aidx == bidx)
- {
- if(a.m_surface.m_level != b.m_surface.m_level)
- {
- return a.m_surface.m_level < b.m_surface.m_level;
- }
- else if(a.m_surface.m_face != b.m_surface.m_face)
- {
- return a.m_surface.m_face < b.m_surface.m_face;
- }
- else if(a.m_surface.m_layer != b.m_surface.m_layer)
- {
- return a.m_surface.m_layer < b.m_surface.m_layer;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return aidx < bidx;
- }
- });
- std::sort(batch.m_bufferBarriersBefore.getBegin(), batch.m_bufferBarriersBefore.getEnd(),
- [&](const BufferBarrier& a, const BufferBarrier& b) {
- return a.m_idx < b.m_idx;
- });
- std::sort(batch.m_asBarriersBefore.getBegin(), batch.m_asBarriersBefore.getEnd(), [&](const ASBarrier& a, const ASBarrier& b) {
- return a.m_idx < b.m_idx;
- });
- #endif
- } // For all batches
- }
- void RenderGraph::minimizeSubchannelSwitches()
- {
- BakeContext& ctx = *m_ctx;
- Bool computeFirst = true;
- for(Batch& batch : ctx.m_batches)
- {
- U32 graphicsPasses = 0;
- U32 computePasses = 0;
- std::sort(batch.m_passIndices.getBegin(), batch.m_passIndices.getEnd(), [&](U32 a, U32 b) {
- const Bool aIsCompute = !ctx.m_passes[a].m_beginRenderpassInfo.m_hasRenderpass;
- const Bool bIsCompute = !ctx.m_passes[b].m_beginRenderpassInfo.m_hasRenderpass;
- graphicsPasses += !aIsCompute + !bIsCompute;
- computePasses += aIsCompute + bIsCompute;
- if(computeFirst)
- {
- return !aIsCompute < !bIsCompute;
- }
- else
- {
- return aIsCompute < bIsCompute;
- }
- });
- if(graphicsPasses && !computePasses)
- {
- // Only graphics passes in this batch, start next batch from graphics
- computeFirst = false;
- }
- else if(computePasses && !graphicsPasses)
- {
- // Only compute passes in this batch, start next batch from compute
- computeFirst = true;
- }
- else
- {
- // This batch ends in compute start next batch in compute and if it ends with graphics start next in graphics
- computeFirst = !computeFirst;
- }
- }
- }
- void RenderGraph::sortBatchPasses()
- {
- BakeContext& ctx = *m_ctx;
- for(Batch& batch : ctx.m_batches)
- {
- std::sort(batch.m_passIndices.getBegin(), batch.m_passIndices.getEnd(), [&](U32 a, U32 b) {
- const Bool aIsCompute = !ctx.m_passes[a].m_beginRenderpassInfo.m_hasRenderpass;
- const Bool bIsCompute = !ctx.m_passes[b].m_beginRenderpassInfo.m_hasRenderpass;
- return aIsCompute < bIsCompute;
- });
- }
- }
- void RenderGraph::compileNewGraph(const RenderGraphBuilder& descr, StackMemoryPool& pool)
- {
- ANKI_TRACE_SCOPED_EVENT(GrRenderGraphCompile);
- // Init the context
- BakeContext& ctx = *newContext(descr, pool);
- m_ctx = &ctx;
- // Init the passes and find the dependencies between passes
- initRenderPassesAndSetDeps(descr);
- // Walk the graph and create pass batches
- initBatches();
- // Now that we know the batches every pass belongs init the graphics passes
- initGraphicsPasses(descr);
- // Create barriers between batches
- setBatchBarriers(descr);
- // Sort passes in batches
- if(GrManager::getSingleton().getDeviceCapabilities().m_gpuVendor == GpuVendor::kNvidia)
- {
- minimizeSubchannelSwitches();
- }
- else
- {
- sortBatchPasses();
- }
- #if ANKI_DBG_RENDER_GRAPH
- if(dumpDependencyDotFile(descr, ctx, "./"))
- {
- ANKI_LOGF("Won't recover on debug code");
- }
- #endif
- }
- Texture& RenderGraph::getTexture(RenderTargetHandle handle) const
- {
- ANKI_ASSERT(m_ctx->m_rts[handle.m_idx].m_texture.isCreated());
- return *m_ctx->m_rts[handle.m_idx].m_texture;
- }
- void RenderGraph::getCachedBuffer(BufferHandle handle, Buffer*& buff, PtrSize& offset, PtrSize& range) const
- {
- const BufferRange& record = m_ctx->m_buffers[handle.m_idx];
- buff = record.m_buffer.get();
- offset = record.m_offset;
- range = record.m_range;
- }
- AccelerationStructure* RenderGraph::getAs(AccelerationStructureHandle handle) const
- {
- ANKI_ASSERT(m_ctx->m_as[handle.m_idx].m_as.isCreated());
- return m_ctx->m_as[handle.m_idx].m_as.get();
- }
- void RenderGraph::recordAndSubmitCommandBuffers(FencePtr* optionalFence)
- {
- ANKI_TRACE_SCOPED_EVENT(GrRenderGraphRecordAndSubmit);
- ANKI_ASSERT(m_ctx);
- const U32 batchGroupCount = min(CoreThreadJobManager::getSingleton().getThreadCount(), m_ctx->m_batches.getSize());
- StackMemoryPool* pool = m_ctx->m_rts.getMemoryPool().m_pool;
- DynamicArray<CommandBufferPtr, MemoryPoolPtrWrapper<StackMemoryPool>> cmdbs(pool);
- cmdbs.resize(batchGroupCount);
- SpinLock cmdbsMtx;
- Atomic<U32> firstGroupThatWroteToSwapchain(kMaxU32);
- for(U32 group = 0; group < batchGroupCount; ++group)
- {
- U32 start, end;
- splitThreadedProblem(group, batchGroupCount, m_ctx->m_batches.getSize(), start, end);
- if(start == end)
- {
- continue;
- }
- CoreThreadJobManager::getSingleton().dispatchTask(
- [this, start, end, pool, &cmdbs, &cmdbsMtx, group, batchGroupCount, &firstGroupThatWroteToSwapchain]([[maybe_unused]] U32 tid) {
- ANKI_TRACE_SCOPED_EVENT(GrRenderGraphTask);
- Array<Char, 32> name;
- snprintf(name.getBegin(), name.getSize(), "RenderGraph cmdb %u-%u", start, end);
- CommandBufferInitInfo cmdbInit(name.getBegin());
- cmdbInit.m_flags = CommandBufferFlag::kGeneralWork;
- CommandBufferPtr cmdb = GrManager::getSingleton().newCommandBuffer(cmdbInit);
- // Write timestamp
- const Bool setPreQuery = m_ctx->m_gatherStatistics && group == 0;
- const Bool setPostQuery = m_ctx->m_gatherStatistics && group == batchGroupCount - 1;
- TimestampQueryInternalPtr preQuery, postQuery;
- if(setPreQuery)
- {
- preQuery = GrManager::getSingleton().newTimestampQuery();
- cmdb->writeTimestamp(preQuery.get());
- }
- if(setPostQuery)
- {
- postQuery = GrManager::getSingleton().newTimestampQuery();
- }
- // Bookkeeping
- {
- LockGuard lock(cmdbsMtx);
- cmdbs[group] = cmdb;
- if(preQuery.isCreated())
- {
- m_statistics.m_timestamps[m_statistics.m_nextTimestamp][0] = preQuery;
- }
- if(postQuery.isCreated())
- {
- m_statistics.m_timestamps[m_statistics.m_nextTimestamp][1] = postQuery;
- m_statistics.m_cpuStartTimes[m_statistics.m_nextTimestamp] = HighRezTimer::getCurrentTime();
- }
- }
- RenderPassWorkContext ctx;
- ctx.m_rgraph = this;
- for(U32 i = start; i < end; ++i)
- {
- const Batch& batch = m_ctx->m_batches[i];
- // Set the barriers
- DynamicArray<TextureBarrierInfo, MemoryPoolPtrWrapper<StackMemoryPool>> texBarriers(pool);
- texBarriers.resizeStorage(batch.m_textureBarriersBefore.getSize());
- for(const TextureBarrier& barrier : batch.m_textureBarriersBefore)
- {
- const Texture& tex = *m_ctx->m_rts[barrier.m_idx].m_texture;
- TextureBarrierInfo& inf = *texBarriers.emplaceBack();
- inf.m_previousUsage = barrier.m_usageBefore;
- inf.m_nextUsage = barrier.m_usageAfter;
- inf.m_textureView = TextureView(&tex, barrier.m_subresource);
- }
- DynamicArray<BufferBarrierInfo, MemoryPoolPtrWrapper<StackMemoryPool>> buffBarriers(pool);
- buffBarriers.resizeStorage(batch.m_bufferBarriersBefore.getSize());
- for(const BufferBarrier& barrier : batch.m_bufferBarriersBefore)
- {
- BufferBarrierInfo& inf = *buffBarriers.emplaceBack();
- inf.m_previousUsage = barrier.m_usageBefore;
- inf.m_nextUsage = barrier.m_usageAfter;
- inf.m_bufferView = BufferView(m_ctx->m_buffers[barrier.m_idx].m_buffer.get(), m_ctx->m_buffers[barrier.m_idx].m_offset,
- m_ctx->m_buffers[barrier.m_idx].m_range);
- }
- // Sort them for the command buffer to merge as many as possible
- std::sort(buffBarriers.getBegin(), buffBarriers.getEnd(), [](const BufferBarrierInfo& a, const BufferBarrierInfo& b) {
- return a.m_bufferView.getBuffer().getUuid() < b.m_bufferView.getBuffer().getUuid();
- });
- DynamicArray<AccelerationStructureBarrierInfo, MemoryPoolPtrWrapper<StackMemoryPool>> asBarriers(pool);
- for(const ASBarrier& barrier : batch.m_asBarriersBefore)
- {
- AccelerationStructureBarrierInfo& inf = *asBarriers.emplaceBack();
- inf.m_previousUsage = barrier.m_usageBefore;
- inf.m_nextUsage = barrier.m_usageAfter;
- inf.m_as = m_ctx->m_as[barrier.m_idx].m_as.get();
- }
- cmdb->pushDebugMarker("Barrier", Vec3(1.0f, 0.0f, 0.0f));
- cmdb->setPipelineBarrier(texBarriers, buffBarriers, asBarriers);
- cmdb->popDebugMarker();
- ctx.m_commandBuffer = cmdb.get();
- ctx.m_batchIdx = i;
- // Call the passes
- for(U32 passIdx : batch.m_passIndices)
- {
- Pass& pass = m_ctx->m_passes[passIdx];
- if(pass.m_writesToSwapchain)
- {
- firstGroupThatWroteToSwapchain.min(group);
- }
- const Vec3 passColor = (pass.m_beginRenderpassInfo.m_hasRenderpass) ? Vec3(0.0f, 1.0f, 0.0f) : Vec3(1.0f, 1.0f, 0.0f);
- cmdb->pushDebugMarker(pass.m_name, passColor);
- if(pass.m_beginRenderpassInfo.m_hasRenderpass)
- {
- cmdb->beginRenderPass({pass.m_beginRenderpassInfo.m_colorRts.getBegin(), U32(pass.m_beginRenderpassInfo.m_colorRtCount)},
- pass.m_beginRenderpassInfo.m_dsRt.m_textureView.isValid() ? &pass.m_beginRenderpassInfo.m_dsRt
- : nullptr,
- pass.m_beginRenderpassInfo.m_vrsRt, pass.m_beginRenderpassInfo.m_vrsTexelSizeX,
- pass.m_beginRenderpassInfo.m_vrsTexelSizeY);
- }
- {
- ANKI_TRACE_SCOPED_EVENT(GrRenderGraphCallback);
- ctx.m_passIdx = passIdx;
- pass.m_callback(ctx);
- }
- if(pass.m_beginRenderpassInfo.m_hasRenderpass)
- {
- cmdb->endRenderPass();
- }
- cmdb->popDebugMarker();
- }
- } // end for batches
- if(setPostQuery)
- {
- // Write a timestamp before the last flush
- cmdb->writeTimestamp(postQuery.get());
- }
- cmdb->endRecording();
- });
- }
- CoreThreadJobManager::getSingleton().waitForAllTasksToFinish();
- // Submit
- DynamicArray<CommandBuffer*, MemoryPoolPtrWrapper<StackMemoryPool>> pCmdbs(pool);
- pCmdbs.resize(cmdbs.getSize());
- for(U32 i = 0; i < cmdbs.getSize(); ++i)
- {
- pCmdbs[i] = cmdbs[i].get();
- }
- const U32 firstGroupThatWroteToSwapchain2 = firstGroupThatWroteToSwapchain.getNonAtomically();
- if(firstGroupThatWroteToSwapchain2 == 0 || firstGroupThatWroteToSwapchain2 == kMaxU32)
- {
- GrManager::getSingleton().submit(WeakArray(pCmdbs), {}, optionalFence);
- }
- else
- {
- // 2 submits. The 1st contains all the batches that don't write to swapchain
- GrManager::getSingleton().submit(WeakArray(pCmdbs).subrange(0, firstGroupThatWroteToSwapchain2), {}, nullptr);
- GrManager::getSingleton().submit(
- WeakArray(pCmdbs).subrange(firstGroupThatWroteToSwapchain2, batchGroupCount - firstGroupThatWroteToSwapchain2), {}, optionalFence);
- }
- }
- void RenderGraph::getCrntUsage(RenderTargetHandle handle, U32 batchIdx, const TextureSubresourceDesc& subresource, TextureUsageBit& usage) const
- {
- usage = TextureUsageBit::kNone;
- const Batch& batch = m_ctx->m_batches[batchIdx];
- for(U32 passIdx : batch.m_passIndices)
- {
- for(const RenderPassDependency::TextureInfo& consumer : m_ctx->m_passes[passIdx].m_consumedTextures)
- {
- if(consumer.m_handle == handle && subresource.overlapsWith(consumer.m_subresource))
- {
- usage |= consumer.m_usage;
- break;
- }
- }
- }
- }
- void RenderGraph::periodicCleanup()
- {
- U32 rtsCleanedCount = 0;
- for(RenderTargetCacheEntry& entry : m_renderTargetCache)
- {
- if(entry.m_texturesInUse < entry.m_textures.getSize())
- {
- // Should cleanup
- rtsCleanedCount += entry.m_textures.getSize() - entry.m_texturesInUse;
- // New array
- GrDynamicArray<TextureInternalPtr> newArray;
- if(entry.m_texturesInUse > 0)
- {
- newArray.resize(entry.m_texturesInUse);
- }
- // Populate the new array
- for(U32 i = 0; i < newArray.getSize(); ++i)
- {
- newArray[i] = std::move(entry.m_textures[i]);
- }
- // Destroy the old array and the rest of the textures
- entry.m_textures.destroy();
- // Move new array
- entry.m_textures = std::move(newArray);
- }
- }
- if(rtsCleanedCount > 0)
- {
- ANKI_GR_LOGI("Cleaned %u render targets", rtsCleanedCount);
- }
- }
- void RenderGraph::getStatistics(RenderGraphStatistics& statistics)
- {
- m_statistics.m_nextTimestamp = (m_statistics.m_nextTimestamp + 1) % kMaxBufferedTimestamps;
- const U32 oldFrame = m_statistics.m_nextTimestamp;
- if(m_statistics.m_timestamps[oldFrame][0].isCreated() && m_statistics.m_timestamps[oldFrame][1].isCreated())
- {
- Second start, end;
- [[maybe_unused]] TimestampQueryResult res = m_statistics.m_timestamps[oldFrame][0]->getResult(start);
- ANKI_ASSERT(res == TimestampQueryResult::kAvailable);
- m_statistics.m_timestamps[oldFrame][0].reset(nullptr);
- res = m_statistics.m_timestamps[oldFrame][1]->getResult(end);
- ANKI_ASSERT(res == TimestampQueryResult::kAvailable);
- m_statistics.m_timestamps[oldFrame][1].reset(nullptr);
- const Second diff = end - start;
- statistics.m_gpuTime = diff;
- statistics.m_cpuStartTime = m_statistics.m_cpuStartTimes[oldFrame];
- }
- else
- {
- statistics.m_gpuTime = -1.0;
- statistics.m_cpuStartTime = -1.0;
- }
- }
- #if ANKI_DBG_RENDER_GRAPH
- StringRaii RenderGraph::textureUsageToStr(StackMemoryPool& pool, TextureUsageBit usage)
- {
- if(!usage)
- {
- return StringRaii(&pool, "None");
- }
- StringListRaii slist(&pool);
- # define ANKI_TEX_USAGE(u) \
- if(!!(usage & TextureUsageBit::u)) \
- { \
- slist.pushBackSprintf("%s", #u); \
- }
- ANKI_TEX_USAGE(kSampledGeometry);
- ANKI_TEX_USAGE(kSampledFragment);
- ANKI_TEX_USAGE(kSampledCompute);
- ANKI_TEX_USAGE(kSampledTraceRays);
- ANKI_TEX_USAGE(kUavGeometryRead);
- ANKI_TEX_USAGE(kUavGeometryWrite);
- ANKI_TEX_USAGE(kUavFragmentRead);
- ANKI_TEX_USAGE(kUavFragmentWrite);
- ANKI_TEX_USAGE(kUavComputeRead);
- ANKI_TEX_USAGE(kUavComputeWrite);
- ANKI_TEX_USAGE(kUavTraceRaysRead);
- ANKI_TEX_USAGE(kUavTraceRaysWrite);
- ANKI_TEX_USAGE(kFramebufferRead);
- ANKI_TEX_USAGE(kFramebufferWrite);
- ANKI_TEX_USAGE(kTransferDestination);
- ANKI_TEX_USAGE(kGenerateMipmaps);
- ANKI_TEX_USAGE(kPresent);
- ANKI_TEX_USAGE(kFramebufferShadingRate);
- if(!usage)
- {
- slist.pushBackSprintf("?");
- }
- # undef ANKI_TEX_USAGE
- ANKI_ASSERT(!slist.isEmpty());
- StringRaii str(&pool);
- slist.join(" | ", str);
- return str;
- }
- StringRaii RenderGraph::bufferUsageToStr(StackMemoryPool& pool, BufferUsageBit usage)
- {
- StringListRaii slist(&pool);
- # define ANKI_BUFF_USAGE(u) \
- if(!!(usage & BufferUsageBit::u)) \
- { \
- slist.pushBackSprintf("%s", #u); \
- }
- ANKI_BUFF_USAGE(kConstantGeometry);
- ANKI_BUFF_USAGE(kConstantPixel);
- ANKI_BUFF_USAGE(kConstantCompute);
- ANKI_BUFF_USAGE(kConstantDispatchRays);
- ANKI_BUFF_USAGE(kStorageGeometryRead);
- ANKI_BUFF_USAGE(kStorageGeometryWrite);
- ANKI_BUFF_USAGE(kStorageFragmentRead);
- ANKI_BUFF_USAGE(kStorageFragmentWrite);
- ANKI_BUFF_USAGE(kStorageComputeRead);
- ANKI_BUFF_USAGE(kStorageComputeWrite);
- ANKI_BUFF_USAGE(kStorageTraceRaysRead);
- ANKI_BUFF_USAGE(kStorageTraceRaysWrite);
- ANKI_BUFF_USAGE(kTextureGeometryRead);
- ANKI_BUFF_USAGE(kTextureGeometryWrite);
- ANKI_BUFF_USAGE(kTextureFragmentRead);
- ANKI_BUFF_USAGE(kTextureFragmentWrite);
- ANKI_BUFF_USAGE(kTextureComputeRead);
- ANKI_BUFF_USAGE(kTextureComputeWrite);
- ANKI_BUFF_USAGE(kTextureTraceRaysRead);
- ANKI_BUFF_USAGE(kTextureTraceRaysWrite);
- ANKI_BUFF_USAGE(kIndex);
- ANKI_BUFF_USAGE(kVertex);
- ANKI_BUFF_USAGE(kIndirectCompute);
- ANKI_BUFF_USAGE(kIndirectDraw);
- ANKI_BUFF_USAGE(kIndirectDispatchRays);
- ANKI_BUFF_USAGE(kTransferSource);
- ANKI_BUFF_USAGE(kTransferDestination);
- ANKI_BUFF_USAGE(kAccelerationStructureBuild);
- if(!usage)
- {
- slist.pushBackSprintf("NONE");
- }
- # undef ANKI_BUFF_USAGE
- ANKI_ASSERT(!slist.isEmpty());
- StringRaii str(&pool);
- slist.join(" | ", str);
- return str;
- }
- StringRaii RenderGraph::asUsageToStr(StackMemoryPool& pool, AccelerationStructureUsageBit usage)
- {
- StringListRaii slist(&pool);
- # define ANKI_AS_USAGE(u) \
- if(!!(usage & AccelerationStructureUsageBit::u)) \
- { \
- slist.pushBackSprintf("%s", #u); \
- }
- ANKI_AS_USAGE(kBuild);
- ANKI_AS_USAGE(kAttach);
- ANKI_AS_USAGE(kGeometryRead);
- ANKI_AS_USAGE(kFragmentRead);
- ANKI_AS_USAGE(kComputeRead);
- ANKI_AS_USAGE(kTraceRaysRead);
- if(!usage)
- {
- slist.pushBackSprintf("NONE");
- }
- # undef ANKI_AS_USAGE
- ANKI_ASSERT(!slist.isEmpty());
- StringRaii str(&pool);
- slist.join(" | ", str);
- return str;
- }
- Error RenderGraph::dumpDependencyDotFile(const RenderGraphBuilder& descr, const BakeContext& ctx, CString path) const
- {
- ANKI_GR_LOGW("Running with debug code");
- static constexpr Array<const char*, 5> COLORS = {"red", "green", "blue", "magenta", "cyan"};
- StackMemoryPool& pool = *ctx.m_pool;
- StringListRaii slist(&pool);
- slist.pushBackSprintf("digraph {\n");
- slist.pushBackSprintf("\t//splines = ortho;\nconcentrate = true;\n");
- for(U32 batchIdx = 0; batchIdx < ctx.m_batches.getSize(); ++batchIdx)
- {
- // Set same rank
- slist.pushBackSprintf("\t{rank=\"same\";");
- for(U32 passIdx : ctx.m_batches[batchIdx].m_passIndices)
- {
- slist.pushBackSprintf("\"%s\";", descr.m_passes[passIdx]->m_name.cstr());
- }
- slist.pushBackSprintf("}\n");
- // Print passes
- for(U32 passIdx : ctx.m_batches[batchIdx].m_passIndices)
- {
- CString passName = descr.m_passes[passIdx]->m_name.toCString();
- slist.pushBackSprintf("\t\"%s\"[color=%s,style=%s,shape=box];\n", passName.cstr(), COLORS[batchIdx % COLORS.getSize()],
- (descr.m_passes[passIdx]->m_type == RenderPassBase::Type::kGraphics) ? "bold" : "dashed");
- for(U32 depIdx : ctx.m_passes[passIdx].m_dependsOn)
- {
- slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", descr.m_passes[depIdx]->m_name.cstr(), passName.cstr());
- }
- if(ctx.m_passes[passIdx].m_dependsOn.getSize() == 0)
- {
- slist.pushBackSprintf("\tNONE->\"%s\";\n", descr.m_passes[passIdx]->m_name.cstr());
- }
- }
- }
- # if 0
- // Color the resources
- slist.pushBackSprintf("subgraph cluster_0 {\n");
- for(U rtIdx = 0; rtIdx < descr.m_renderTargets.getSize(); ++rtIdx)
- {
- slist.pushBackSprintf(
- "\t\"%s\"[color=%s];\n", &descr.m_renderTargets[rtIdx].m_name[0], COLORS[rtIdx % COLORS.getSize()]);
- }
- slist.pushBackSprintf("}\n");
- # endif
- // Barriers
- // slist.pushBackSprintf("subgraph cluster_1 {\n");
- StringRaii prevBubble(&pool);
- prevBubble.create("START");
- for(U32 batchIdx = 0; batchIdx < ctx.m_batches.getSize(); ++batchIdx)
- {
- const Batch& batch = ctx.m_batches[batchIdx];
- StringRaii batchName(&pool);
- batchName.sprintf("batch%u", batchIdx);
- for(U32 barrierIdx = 0; barrierIdx < batch.m_textureBarriersBefore.getSize(); ++barrierIdx)
- {
- const TextureBarrier& barrier = batch.m_textureBarriersBefore[barrierIdx];
- StringRaii barrierLabel(&pool);
- barrierLabel.sprintf("<b>%s</b> (mip,dp,f,l)=(%u,%u,%u,%u)<br/>%s <b>to</b> %s", &descr.m_renderTargets[barrier.m_idx].m_name[0],
- barrier.m_surface.m_level, barrier.m_surface.m_depth, barrier.m_surface.m_face, barrier.m_surface.m_layer,
- textureUsageToStr(pool, barrier.m_usageBefore).cstr(), textureUsageToStr(pool, barrier.m_usageAfter).cstr());
- StringRaii barrierName(&pool);
- barrierName.sprintf("%s tex barrier%u", batchName.cstr(), barrierIdx);
- slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold,shape=box,label=< %s >];\n", barrierName.cstr(), COLORS[batchIdx % COLORS.getSize()],
- barrierLabel.cstr());
- slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), barrierName.cstr());
- prevBubble = barrierName;
- }
- for(U32 barrierIdx = 0; barrierIdx < batch.m_bufferBarriersBefore.getSize(); ++barrierIdx)
- {
- const BufferBarrier& barrier = batch.m_bufferBarriersBefore[barrierIdx];
- StringRaii barrierLabel(&pool);
- barrierLabel.sprintf("<b>%s</b><br/>%s <b>to</b> %s", &descr.m_buffers[barrier.m_idx].m_name[0],
- bufferUsageToStr(pool, barrier.m_usageBefore).cstr(), bufferUsageToStr(pool, barrier.m_usageAfter).cstr());
- StringRaii barrierName(&pool);
- barrierName.sprintf("%s buff barrier%u", batchName.cstr(), barrierIdx);
- slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold,shape=box,label=< %s >];\n", barrierName.cstr(), COLORS[batchIdx % COLORS.getSize()],
- barrierLabel.cstr());
- slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), barrierName.cstr());
- prevBubble = barrierName;
- }
- for(U32 barrierIdx = 0; barrierIdx < batch.m_asBarriersBefore.getSize(); ++barrierIdx)
- {
- const ASBarrier& barrier = batch.m_asBarriersBefore[barrierIdx];
- StringRaii barrierLabel(&pool);
- barrierLabel.sprintf("<b>%s</b><br/>%s <b>to</b> %s", descr.m_as[barrier.m_idx].m_name.getBegin(),
- asUsageToStr(pool, barrier.m_usageBefore).cstr(), asUsageToStr(pool, barrier.m_usageAfter).cstr());
- StringRaii barrierName(&pool);
- barrierName.sprintf("%s AS barrier%u", batchName.cstr(), barrierIdx);
- slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold,shape=box,label=< %s >];\n", barrierName.cstr(), COLORS[batchIdx % COLORS.getSize()],
- barrierLabel.cstr());
- slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), barrierName.cstr());
- prevBubble = barrierName;
- }
- for(U32 passIdx : batch.m_passIndices)
- {
- const RenderPassBase& pass = *descr.m_passes[passIdx];
- StringRaii passName(&pool);
- passName.sprintf("%s pass", pass.m_name.cstr());
- slist.pushBackSprintf("\t\"%s\"[color=%s,style=bold];\n", passName.cstr(), COLORS[batchIdx % COLORS.getSize()]);
- slist.pushBackSprintf("\t\"%s\"->\"%s\";\n", prevBubble.cstr(), passName.cstr());
- prevBubble = passName;
- }
- }
- // slist.pushBackSprintf("}\n");
- slist.pushBackSprintf("}");
- File file;
- ANKI_CHECK(file.open(StringRaii(&pool).sprintf("%s/rgraph_%05u.dot", &path[0], m_version).toCString(), FileOpenFlag::kWrite));
- for(const String& s : slist)
- {
- ANKI_CHECK(file.writeTextf("%s", &s[0]));
- }
- return Error::kNone;
- }
- #endif
- } // end namespace anki
|