From 820703d0825355fb186b7a732536fb35097117ee Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Mon, 13 Feb 2023 18:27:56 +0800 Subject: [PATCH] resource states & fgd signal split end barrier (#14207) --- .../cocos/renderer/frame-graph/DevicePass.cpp | 6 +- .../cocos/renderer/gfx-agent/DeviceAgent.cpp | 10 + native/cocos/renderer/gfx-agent/DeviceAgent.h | 2 + native/cocos/renderer/gfx-base/GFXDevice.h | 3 +- .../gfx-validator/DeviceValidator.cpp | 4 + .../renderer/gfx-validator/DeviceValidator.h | 2 + .../renderer/gfx-vulkan/VKCommandBuffer.cpp | 19 +- native/cocos/renderer/gfx-vulkan/VKDevice.cpp | 26 +-- .../renderer/gfx-vulkan/VKGPUContext.cpp | 8 +- .../pipeline/custom/FrameGraphDispatcher.cpp | 189 +++++++++++++----- .../pipeline/custom/NativeExecutor.cpp | 90 +-------- .../pipeline/custom/NativePipeline.cpp | 3 + .../pipeline/custom/NativeResourceGraph.cpp | 5 + .../renderer/pipeline/custom/test/test.h | 2 +- .../src/complicated_barrier_case.cpp | 16 +- .../src/simple_closed_barrier_test.cpp | 4 +- 16 files changed, 214 insertions(+), 175 deletions(-) diff --git a/native/cocos/renderer/frame-graph/DevicePass.cpp b/native/cocos/renderer/frame-graph/DevicePass.cpp index dd6749dfd3..0c4d0e3609 100644 --- a/native/cocos/renderer/frame-graph/DevicePass.cpp +++ b/native/cocos/renderer/frame-graph/DevicePass.cpp @@ -51,11 +51,7 @@ DevicePass::DevicePass(const FrameGraph &graph, ccstd::vector const // _enableAutoBarrier: auto barrier in framegraph // barrierDeduce: deduce barrier gfx internally // to avoid redundant instructions, either inside or outside - auto opts = device->getOptions(); - opts.enableBarrierDeduce = !gfx::ENABLE_GRAPH_AUTO_BARRIER; - device->setOptions(opts); - - CC_ASSERT(gfx::ENABLE_GRAPH_AUTO_BARRIER ^ gfx::Device::getInstance()->getOptions().enableBarrierDeduce); + device->enableAutoBarrier(!gfx::ENABLE_GRAPH_AUTO_BARRIER); // Important Notice: // here attchment index has changed. diff --git a/native/cocos/renderer/gfx-agent/DeviceAgent.cpp b/native/cocos/renderer/gfx-agent/DeviceAgent.cpp index 5f9f3749ec..d429b8300c 100644 --- a/native/cocos/renderer/gfx-agent/DeviceAgent.cpp +++ b/native/cocos/renderer/gfx-agent/DeviceAgent.cpp @@ -421,6 +421,16 @@ void DeviceAgent::getQueryPoolResults(QueryPool *queryPool) { queryPoolAgent->_results = actorQueryPoolAgent->_results; } +void DeviceAgent::enableAutoBarrier(bool en) { + ENQUEUE_MESSAGE_2( + _mainMessageQueue, enableAutoBarrier, + actor, getActor(), + en, en, + { + actor->enableAutoBarrier(en); + }); +} + void DeviceAgent::presentSignal() { _frameBoundarySemaphore.signal(); } diff --git a/native/cocos/renderer/gfx-agent/DeviceAgent.h b/native/cocos/renderer/gfx-agent/DeviceAgent.h index 5ecd9d544b..e9390fb8e2 100644 --- a/native/cocos/renderer/gfx-agent/DeviceAgent.h +++ b/native/cocos/renderer/gfx-agent/DeviceAgent.h @@ -103,6 +103,8 @@ public: void presentWait(); void presentSignal(); + void enableAutoBarrier(bool en) override; + protected: static DeviceAgent *instance; diff --git a/native/cocos/renderer/gfx-base/GFXDevice.h b/native/cocos/renderer/gfx-base/GFXDevice.h index 5c993f72fa..2d6ad58490 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.h +++ b/native/cocos/renderer/gfx-base/GFXDevice.h @@ -118,8 +118,7 @@ public: template void registerOnAcquireCallback(ExecuteMethod &&execute); - inline void setOptions(const DeviceOptions &opts) { _options = opts; } - inline const DeviceOptions &getOptions() const { return _options; } + virtual void enableAutoBarrier(bool en) { _options.enableBarrierDeduce = en; } protected: static Device *instance; diff --git a/native/cocos/renderer/gfx-validator/DeviceValidator.cpp b/native/cocos/renderer/gfx-validator/DeviceValidator.cpp index ca6ba46709..d78ecb4f7c 100644 --- a/native/cocos/renderer/gfx-validator/DeviceValidator.cpp +++ b/native/cocos/renderer/gfx-validator/DeviceValidator.cpp @@ -360,5 +360,9 @@ void DeviceValidator::getQueryPoolResults(QueryPool *queryPool) { queryPoolValidator->_results = actorQueryPoolValidator->_results; } +void DeviceValidator::enableAutoBarrier(bool en) { + _actor->enableAutoBarrier(en); +} + } // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/gfx-validator/DeviceValidator.h b/native/cocos/renderer/gfx-validator/DeviceValidator.h index ebe84a9b11..cedf313dab 100644 --- a/native/cocos/renderer/gfx-validator/DeviceValidator.h +++ b/native/cocos/renderer/gfx-validator/DeviceValidator.h @@ -91,6 +91,8 @@ public: inline bool isRecording() const { return _recording; } inline uint64_t currentFrame() const { return _currentFrame; } + void enableAutoBarrier(bool enable) override; + protected: static DeviceValidator *instance; diff --git a/native/cocos/renderer/gfx-vulkan/VKCommandBuffer.cpp b/native/cocos/renderer/gfx-vulkan/VKCommandBuffer.cpp index b943ba8ed3..9a6e70320e 100644 --- a/native/cocos/renderer/gfx-vulkan/VKCommandBuffer.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKCommandBuffer.cpp @@ -63,6 +63,22 @@ void CCVKCommandBuffer::doInit(const CommandBufferInfo & /*info*/) { } void CCVKCommandBuffer::doDestroy() { + if (_gpuCommandBuffer) { + auto cleanEvent = [this](VkEvent event) { + auto res = vkResetEvent(CCVKDevice::getInstance()->gpuDevice()->vkDevice, event); + CC_ASSERT(res == VK_SUCCESS); + vkDestroyEvent(CCVKDevice::getInstance()->gpuDevice()->vkDevice, event, nullptr); + }; + while (!_availableEvents.empty()) { + VkEvent event = _availableEvents.front(); + cleanEvent(event); + _availableEvents.pop(); + } + for (auto pair : _barrierEvents) { + cleanEvent(pair.second); + } + } + _gpuCommandBuffer = nullptr; } @@ -649,7 +665,7 @@ void CCVKCommandBuffer::pipelineBarrier(const GeneralBarrier *barrier, const Buf VkEventCreateInfo eventInfo = { VK_STRUCTURE_TYPE_EVENT_CREATE_INFO, nullptr, - VK_EVENT_CREATE_DEVICE_ONLY_BIT_KHR, + 0, }; VkResult res = vkCreateEvent(CCVKDevice::getInstance()->gpuDevice()->vkDevice, &eventInfo, @@ -673,7 +689,6 @@ void CCVKCommandBuffer::pipelineBarrier(const GeneralBarrier *barrier, const Buf } else { bool fullBarrier = ccBarrier->getInfo().type == BarrierType::FULL; if (!fullBarrier) { - auto key = ccstd::hash_value(gpuTexture); CC_ASSERT(_barrierEvents.find(ccTexture) != _barrierEvents.end()); VkEvent event = _barrierEvents.at(ccTexture); scheduledEvents.push_back(event); diff --git a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp index bef0a58551..40af2f1297 100644 --- a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp @@ -648,19 +648,21 @@ void CCVKDevice::acquire(Swapchain *const *swapchains, uint32_t count) { vkPresentBarriers[i].image = gpuSwapchains[i]->swapchainImages[vkSwapchainIndices[i]]; } - _gpuTransportHub->checkIn( - [&](const CCVKGPUCommandBuffer *gpuCommandBuffer) { - vkCmdPipelineBarrier(gpuCommandBuffer->vkCommandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, - 0, 0, nullptr, 0, nullptr, utils::toUint(vkSwapchains.size()), vkAcquireBarriers.data()); - }, - false, false); + if (this->_options.enableBarrierDeduce) { + _gpuTransportHub->checkIn( + [&](const CCVKGPUCommandBuffer *gpuCommandBuffer) { + vkCmdPipelineBarrier(gpuCommandBuffer->vkCommandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + 0, 0, nullptr, 0, nullptr, utils::toUint(vkSwapchains.size()), vkAcquireBarriers.data()); + }, + false, false); - _gpuTransportHub->checkIn( - [&](const CCVKGPUCommandBuffer *gpuCommandBuffer) { - vkCmdPipelineBarrier(gpuCommandBuffer->vkCommandBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, - 0, 0, nullptr, 0, nullptr, utils::toUint(vkSwapchains.size()), vkPresentBarriers.data()); - }, - false, true); + _gpuTransportHub->checkIn( + [&](const CCVKGPUCommandBuffer *gpuCommandBuffer) { + vkCmdPipelineBarrier(gpuCommandBuffer->vkCommandBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, + 0, 0, nullptr, 0, nullptr, utils::toUint(vkSwapchains.size()), vkPresentBarriers.data()); + }, + false, true); + } } void CCVKDevice::present() { diff --git a/native/cocos/renderer/gfx-vulkan/VKGPUContext.cpp b/native/cocos/renderer/gfx-vulkan/VKGPUContext.cpp index ecdb9bc85c..d040192c68 100644 --- a/native/cocos/renderer/gfx-vulkan/VKGPUContext.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKGPUContext.cpp @@ -54,11 +54,11 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugUtilsMessengerCallback(VkDebugUtilsMessageSe return VK_FALSE; } if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) { - //CC_LOG_INFO("%s: %s", callbackData->pMessageIdName, callbackData->pMessage); + // CC_LOG_INFO("%s: %s", callbackData->pMessageIdName, callbackData->pMessage); return VK_FALSE; } if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) { - //CC_LOG_DEBUG("%s: %s", callbackData->pMessageIdName, callbackData->pMessage); + // CC_LOG_DEBUG("%s: %s", callbackData->pMessageIdName, callbackData->pMessage); return VK_FALSE; } CC_LOG_ERROR("%s: %s", callbackData->pMessageIdName, callbackData->pMessage); @@ -83,11 +83,11 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugReportCallback(VkDebugReportFlagsEXT flags, return VK_FALSE; } if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) { - //CC_LOG_INFO("%s: %s", layerPrefix, message); + // CC_LOG_INFO("%s: %s", layerPrefix, message); return VK_FALSE; } if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) { - //CC_LOG_DEBUG("%s: %s", layerPrefix, message); + // CC_LOG_DEBUG("%s: %s", layerPrefix, message); return VK_FALSE; } CC_LOG_ERROR("%s: %s", layerPrefix, message); diff --git a/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp b/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp index a0b2a26797..0787b86d8c 100644 --- a/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp +++ b/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp @@ -111,7 +111,7 @@ using ComputeViewsMap = PmrTransparentMap; struct Graphs { - const ResourceGraph &resourceGraph; + ResourceGraph &resourceGraph; const LayoutGraphData &layoutGraphData; ResourceAccessGraph &resourceAccessGraph; RelationGraph &relationGraph; @@ -177,6 +177,10 @@ bool isPassExecAdjecent(uint32_t passLID, uint32_t passRID) { return std::abs(static_cast(passLID) - static_cast(passRID)) <= 1; } +inline bool isReadOnlyAccess(gfx::AccessFlagBit flag) { + return flag < gfx::AccessFlagBit::PRESENT; +} + bool isTransitionStatusDependent(const AccessStatus &lhs, const AccessStatus &rhs); template bool tryAddEdge(uint32_t srcVertex, uint32_t dstVertex, Graph &graph); @@ -304,7 +308,7 @@ void buildAccessGraph(const RenderGraph &renderGraph, const Graphs &graphs) { bool isExternal = pass.second.isExternal; bool needCulling = pass.second.needCulling; - if(pass.first != resourceAccessGraph.presentPassID) { + if (pass.first != resourceAccessGraph.presentPassID) { if (isExternal && !needCulling) { add_edge(pass.first, resourceAccessGraph.presentPassID, resourceAccessGraph); } else { @@ -399,7 +403,8 @@ struct BarrierVisitor : public boost::bfs_visitor<> { AccessNodeInfo from = {srcAccess->attachmentStatus, srcRearBarriers, u}; AccessNodeInfo to = {dstAccess->attachmentStatus, dstFrontBarriers, u}; - fillBarrier(from, to, isAdjacent); + std::set noUseSet; + fillBarrier(from, to, isAdjacent, noUseSet); srcAccess = dstAccess; dstAccess = dstAccess->nextSubpass; @@ -427,7 +432,7 @@ struct BarrierVisitor : public boost::bfs_visitor<> { } } - void fillBarrier(const AccessNodeInfo &from, const AccessNodeInfo &to, bool isAdjacent) { + void fillBarrier(const AccessNodeInfo &from, const AccessNodeInfo &to, bool isAdjacent, std::set &subpassResourceSet) { const auto &[srcStatus, srcRearBarriers, srcVert] = from; const auto &[dstStatus, dstFrontBarriers, dstVert] = to; std::vector commonResources; @@ -446,6 +451,10 @@ struct BarrierVisitor : public boost::bfs_visitor<> { // NOLINTNEXTLINE for (uint32_t i = 0; i < commonResources.size(); ++i) { uint32_t resourceID = commonResources[i].vertID; + if (subpassResourceSet.find(resourceID) != subpassResourceSet.end()) { + continue; + } + subpassResourceSet.emplace(resourceID); auto findAccessByID = [resourceID](const AccessStatus &resAccess) { return resAccess.vertID == resourceID; }; auto fromIter = std::find_if(srcStatus.begin(), srcStatus.end(), findAccessByID); auto toIter = std::find_if(dstStatus.begin(), dstStatus.end(), findAccessByID); @@ -566,15 +575,18 @@ struct BarrierVisitor : public boost::bfs_visitor<> { uint32_t rescID = resourcecAccess.vertID; bool externalRes = get(get(ResourceGraph::Traits, resourceGraph), rescID).hasSideEffects(); if (externalRes) { + const auto &states = get(ResourceGraph::States, resourceGraph, rescID); const PmrString &resName = get(ResourceGraph::Name, resourceGraph, rescID); - auto iter = externalMap.find(resName); + auto resIter = externalMap.find(resName); // first meet in this frame - if (externalResNames.find(resName) == externalResNames.end()) { - // first meet in whole program - if (iter == externalMap.end()) { + if (resIter == externalMap.end()) { + // first meet in this program + if (states.states == gfx::AccessFlagBit::NONE) { auto lastRescAccess = resourcecAccess; - lastRescAccess.vertID = EXPECT_START_ID; auto currRescAccess = resourcecAccess; + + // resource id in access -> pass id in barrier + lastRescAccess.vertID = vert; currRescAccess.vertID = vert; externalMap.insert({resName, @@ -583,26 +595,37 @@ struct BarrierVisitor : public boost::bfs_visitor<> { currRescAccess, }}); } else { - externalMap[resName].lastStatus = externalMap[resName].currStatus; + externalMap[resName].lastStatus = {}; + externalMap[resName].lastStatus.vertID = INVALID_ID; + externalMap[resName].lastStatus.accessFlag = states.states; + // deprecated + externalMap[resName].lastStatus.usage = gfx::TextureUsageBit::NONE; + externalMap[resName].lastStatus.range = TextureRange{}; + externalMap[resName].currStatus = resourcecAccess; externalMap[resName].currStatus.vertID = vert; + const auto &traits = get(ResourceGraph::Traits, resourceGraph, rescID); + // transit status from last frame end to this frame begin if (isTransitionStatusDependent(externalMap[resName].lastStatus, externalMap[resName].currStatus)) { barrierMap[vert].blockBarrier.frontBarriers.emplace_back(Barrier{ rescID, - gfx::BarrierType::SPLIT_END, + traits.residency == ResourceResidency::BACKBUFFER ? gfx::BarrierType::FULL : gfx::BarrierType::SPLIT_END, nullptr, externalMap[resName].lastStatus, externalMap[resName].currStatus, }); + externalResNames.emplace(resName); } } - externalResNames.insert(resName); } else { - if (iter->second.currStatus.vertID < vert) { + if (resIter->second.currStatus.vertID < vert) { //[pass: vert] is later access than in iter. externalMap[resName].currStatus = resourcecAccess; - externalMap[resName].currStatus.vertID = INVALID_ID; + externalMap[resName].currStatus.vertID = vert; + if (isReadOnlyAccess(resourcecAccess.accessFlag)) { + externalResNames.emplace(resName); + } } } } @@ -636,12 +659,20 @@ struct BarrierVisitor : public boost::bfs_visitor<> { const auto *srcHead = &fromAccess; const auto *dstHead = &toAccess; - uint32_t srcSubpassIndex = 0; bool srcHasSubpass = srcHead->nextSubpass; bool dstHasSubpass = dstHead->nextSubpass; srcHead = srcHasSubpass ? srcHead->nextSubpass : srcHead; + std::stack reverseSubpassQ; while (srcHead) { + reverseSubpassQ.push(srcHead); + srcHead = srcHead->nextSubpass; + } + ccstd::set subpassResourceSet; + while (!reverseSubpassQ.empty()) { + uint32_t srcSubpassIndex = reverseSubpassQ.size() - 1; + srcHead = reverseSubpassQ.top(); + reverseSubpassQ.pop(); const std::vector &fromStatus = srcHead->attachmentStatus; std::vector &srcRearBarriers = srcHasSubpass ? barrierMap[from].subpassBarriers[srcSubpassIndex].rearBarriers : barrierMap[from].blockBarrier.rearBarriers; uint32_t dstSubpassIndex = 0; @@ -654,16 +685,16 @@ struct BarrierVisitor : public boost::bfs_visitor<> { AccessNodeInfo fromInfo = {fromStatus, srcRearBarriers, from}; AccessNodeInfo toInfo = {toStatus, dstFrontBarriers, to}; bool isExecAdjacent = isAdjacent && (!srcHead->nextSubpass && !dstSubpassIndex); - fillBarrier(fromInfo, toInfo, isAdjacent); + fillBarrier(fromInfo, toInfo, isAdjacent, subpassResourceSet); dstHead = dstHead->nextSubpass; ++dstSubpassIndex; } - srcHead = srcHead->nextSubpass; - ++srcSubpassIndex; } auto &rearBarriers = barrierMap[from].blockBarrier.rearBarriers; auto &frontBarriers = barrierMap[from].blockBarrier.frontBarriers; + + uint32_t subpassIdx = 0; for (const auto &barriers : barrierMap[from].subpassBarriers) { for (const auto &barrier : barriers.rearBarriers) { auto resID = barrier.resourceID; @@ -674,10 +705,41 @@ struct BarrierVisitor : public boost::bfs_visitor<> { auto resFinalPassID = accessRecord.at(resID).currStatus.vertID; if (resFinalPassID > from) { - if (iter == rearBarriers.end()) { - rearBarriers.emplace_back(barrier); - } else { - (*iter) = barrier; + const auto *dstHead = dstHasSubpass ? toAccess.nextSubpass : &toAccess; + const AccessStatus *dstAccess{nullptr}; + while (dstHead) { + auto iter = std::find_if(dstHead->attachmentStatus.begin(), dstHead->attachmentStatus.end(), [resID](const AccessStatus &access) { + return access.vertID == resID; + }); + if (iter != dstHead->attachmentStatus.end()) { + dstAccess = &(*iter); + break; + } + } + + const auto *srcHead = srcHasSubpass ? fromAccess.nextSubpass : &fromAccess; + uint32_t step = 0; + while (step <= subpassIdx) { + srcHead = srcHead->nextSubpass; + ++step; + } + + bool laterUse = false; + while (srcHead) { + const auto &attachments = srcHead->attachmentStatus; + laterUse |= std::any_of(attachments.begin(), attachments.end(), [resID](const AccessStatus &access) { + return access.vertID == resID; + }); + srcHead = srcHead->nextSubpass; + } + + const auto &srcAccess = laterUse ? barrier.endStatus : barrier.beginStatus; + if (isTransitionStatusDependent(srcAccess, *dstAccess)) { + if (iter == rearBarriers.end()) { + rearBarriers.emplace_back(barrier); + } else if (barrier.endStatus.vertID >= dstAccess->vertID) { + (*iter) = barrier; + } } } } @@ -695,6 +757,8 @@ struct BarrierVisitor : public boost::bfs_visitor<> { frontBarriers.emplace_back(barrier); } } + + subpassIdx++; } } @@ -710,7 +774,7 @@ void buildBarriers(FrameGraphDispatcher &fgDispatcher) { auto *scratch = fgDispatcher.scratch; const auto &graph = fgDispatcher.graph; const auto &layoutGraph = fgDispatcher.layoutGraph; - const auto &resourceGraph = fgDispatcher.resourceGraph; + auto &resourceGraph = fgDispatcher.resourceGraph; auto &relationGraph = fgDispatcher.relationGraph; auto &externalResMap = fgDispatcher.externalResMap; auto &rag = fgDispatcher.resourceAccessGraph; @@ -744,6 +808,23 @@ void buildBarriers(FrameGraphDispatcher &fgDispatcher) { for (const auto &externalPair : externalResMap) { const auto &transition = externalPair.second; auto resID = resourceGraph.valueIndex.at(externalPair.first); + const auto &resTraits = get(ResourceGraph::Traits, resourceGraph, resID); + auto &rescStates = get(ResourceGraph::States, resourceGraph, resID); + + bool backBuffer = resTraits.residency == ResourceResidency::BACKBUFFER; + // when to dispatch a barrier on persistent: + // 1. resource been written in this frame; + // 2. first meet in this frame (no idea if any writes in next frame) + // 3. backbuffer present + bool needNextBarrier = (namesSet.find(externalPair.first) != namesSet.end()) || (rescStates.states == gfx::AccessFlagBit::NONE); + + // persistant resource states cached here + rescStates.states = transition.currStatus.accessFlag; + + if (!backBuffer && !needNextBarrier) { + continue; + } + auto passID = transition.currStatus.vertID; if (batchedBarriers.find(passID) == batchedBarriers.end()) { batchedBarriers.emplace(passID, BarrierNode{}); @@ -756,8 +837,21 @@ void buildBarriers(FrameGraphDispatcher &fgDispatcher) { externalResMap[externalPair.first].currStatus, {}}; - bool hasSubpass = !batchedBarriers[passID].subpassBarriers.empty(); + if (backBuffer) { + nextFrameResBarrier.endStatus = { + INVALID_ID, + gfx::ShaderStageFlagBit::NONE, + gfx::MemoryAccessBit::NONE, + gfx::PassType::PRESENT, + gfx::AccessFlagBit::PRESENT, + gfx::TextureUsageBit::NONE, + TextureRange{}, + }; + nextFrameResBarrier.type = gfx::BarrierType::FULL; + rescStates.states = gfx::AccessFlagBit::PRESENT; + } + bool hasSubpass = !batchedBarriers[passID].subpassBarriers.empty(); if (hasSubpass) { auto &subpassBarriers = batchedBarriers[passID].subpassBarriers; for (int i = static_cast(subpassBarriers.size()) - 1; i >= 0; --i) { @@ -770,8 +864,7 @@ void buildBarriers(FrameGraphDispatcher &fgDispatcher) { auto found = std::find_if(frontBarriers.begin(), frontBarriers.end(), findBarrierByResID) != frontBarriers.end() || std::find_if(rearBarriers.begin(), rearBarriers.end(), findBarrierByResID) != rearBarriers.end(); if (found) { - // put into rear barriers in order not to stuck - rearBarriers.push_back({}); + rearBarriers.push_back(nextFrameResBarrier); break; } } @@ -1228,7 +1321,7 @@ void passReorder(FrameGraphDispatcher &fgDispatcher) { auto *scratch = fgDispatcher.scratch; const auto &graph = fgDispatcher.graph; const auto &layoutGraph = fgDispatcher.layoutGraph; - const auto &resourceGraph = fgDispatcher.resourceGraph; + auto &resourceGraph = fgDispatcher.resourceGraph; auto &relationGraph = fgDispatcher.relationGraph; auto &rag = fgDispatcher.resourceAccessGraph; @@ -1376,14 +1469,8 @@ bool tryAddEdge(uint32_t srcVertex, uint32_t dstVertex, Graph &graph) { } bool isTransitionStatusDependent(const AccessStatus &lhs, const AccessStatus &rhs) { - bool res = true; - if (lhs.passType == rhs.passType && - lhs.visibility == rhs.visibility && - lhs.access == rhs.access && - lhs.usage == rhs.usage) { - res = false; - } - return res; + return (!(isReadOnlyAccess(lhs.accessFlag) && isReadOnlyAccess(rhs.accessFlag))) && + (lhs.accessFlag != rhs.accessFlag); } auto getResourceStatus(PassType passType, const PmrString &name, gfx::MemoryAccess memAccess, gfx::ShaderStageFlags visibility, const ResourceGraph &resourceGraph) { @@ -1509,6 +1596,8 @@ AccessVertex dependencyCheck(RAG &rag, AccessVertex curVertID, const ResourceGra const auto &[name, passType, visibility, access, accessFlag, usage] = viewStatus; auto &accessRecord = rag.accessRecord; + bool readOnly = isReadOnlyAccess(accessFlag); + AccessVertex lastVertID = INVALID_ID; CC_EXPECTS(rag.resourceIndex.find(name) != rag.resourceIndex.end()); auto resourceID = rag.resourceIndex[name]; @@ -1525,23 +1614,27 @@ AccessVertex dependencyCheck(RAG &rag, AccessVertex curVertID, const ResourceGra } } else { ResourceTransition &trans = iter->second; - if (access == gfx::MemoryAccessBit::READ_ONLY && - trans.currStatus.access == gfx::MemoryAccessBit::READ_ONLY && - (trans.currStatus.passType == passType || passType == PassType::PRESENT) && - trans.currStatus.visibility == visibility && - trans.currStatus.usage == usage) { - // current READ, no WRITE before in this frame, it's expected to be external. - bool dirtyExternalRes = trans.lastStatus.vertID == INVALID_ID; - if (!dirtyExternalRes) { - tryAddEdge(trans.lastStatus.vertID, curVertID, rag); - if (rag.leafPasses.find(trans.lastStatus.vertID) != rag.leafPasses.end()) { - rag.leafPasses.erase(trans.lastStatus.vertID); - } - } + auto &currAccessStatus = trans.currStatus; + auto lastReadOnly = isReadOnlyAccess(currAccessStatus.accessFlag) && (currAccessStatus.access == gfx::MemoryAccessBit::READ_ONLY); + if (readOnly && lastReadOnly) { if (isExternalPass) { // only external res will be manually record here, leaf pass with transient resource will be culled by default, // those leaf passes with ALL read access on external(or with transients) res can be culled. rag.leafPasses[curVertID].needCulling &= (access == gfx::MemoryAccessBit::READ_ONLY && passType != PassType::PRESENT); + + // current READ, no WRITE before in this frame, it's expected to be external. + bool dirtyExternalRes = trans.lastStatus.vertID == INVALID_ID; + if (!dirtyExternalRes) { + tryAddEdge(EXPECT_START_ID, curVertID, rag); + if (rag.leafPasses.find(EXPECT_START_ID) != rag.leafPasses.end()) { + rag.leafPasses.erase(EXPECT_START_ID); + } + } + } else { + tryAddEdge(trans.lastStatus.vertID, curVertID, rag); + if (rag.leafPasses.find(trans.lastStatus.vertID) != rag.leafPasses.end()) { + rag.leafPasses.erase(trans.lastStatus.vertID); + } } trans.currStatus = {curVertID, visibility, access, passType, accessFlag, usage, Range{}}; lastVertID = trans.lastStatus.vertID; diff --git a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp index f5b166f36b..a324a41905 100644 --- a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp @@ -289,94 +289,6 @@ PersistentRenderPassAndFramebuffer& fetchOrCreateFramebuffer( return iter->second; } -gfx::BufferBarrierInfo getBufferBarrier(const render::Barrier& barrier) { - gfx::MemoryUsage memUsage = gfx::MemoryUsage::DEVICE; - - const auto& beginUsage = get(barrier.beginStatus.usage); - const auto& endUsage = get(barrier.endStatus.usage); - - const auto& bufferRange = get(barrier.beginStatus.range); - CC_EXPECTS(bufferRange.size); - - return { - gfx::getAccessFlags( - beginUsage, memUsage, - barrier.beginStatus.access, - barrier.beginStatus.visibility), - gfx::getAccessFlags( - endUsage, memUsage, - barrier.endStatus.access, - barrier.endStatus.visibility), - barrier.type, - bufferRange.offset, bufferRange.size}; -} - -std::pair getTextureBarrier( - ResourceGraph& resg, ResourceGraph::vertex_descriptor resID, - const render::Barrier& barrier) { - gfx::Texture* texture = resg.getTexture(resID); - CC_ENSURES(texture); - - const auto& desc = get(ResourceGraph::DescTag{}, resg, resID); - - const bool isExternal = barrier.endStatus.vertID == ResourceAccessGraph::null_vertex(); - - const auto barrierType = isExternal ? gfx::BarrierType::FULL : barrier.type; - const auto beginUsage = get(barrier.beginStatus.usage); - const auto endUsage = [&]() { - if (!isExternal) { - return get(barrier.endStatus.usage); - } - return gfx::TextureUsage::NONE; - }(); - - auto beginAccesFlags = gfx::getAccessFlags( - beginUsage, - barrier.beginStatus.access, - barrier.beginStatus.visibility); - - auto endAccessFlags = [&]() { - if (!isExternal) { - return gfx::getAccessFlags( - endUsage, - barrier.endStatus.access, - barrier.endStatus.visibility); - } - auto& states = get(ResourceGraph::States, resg, resID); - if (holds(resID, resg)) { - states.states = gfx::AccessFlagBit::PRESENT; - return states.states; - } - states.states = - gfx::AccessFlagBit::FRAGMENT_SHADER_READ_TEXTURE | - gfx::AccessFlagBit::VERTEX_SHADER_READ_TEXTURE | - gfx::AccessFlagBit::COMPUTE_SHADER_READ_TEXTURE; - - return states.states; - }(); - - CC_ENSURES(beginAccesFlags != gfx::INVALID_ACCESS_FLAGS); - CC_ENSURES(endAccessFlags != gfx::INVALID_ACCESS_FLAGS); - - const auto& textureRange = get(barrier.beginStatus.range); - CC_EXPECTS(textureRange.levelCount); - CC_EXPECTS(textureRange.numSlices); - - return { - gfx::TextureBarrierInfo{ - beginAccesFlags, - endAccessFlags, - barrierType, - textureRange.mipLevel, - textureRange.levelCount, - textureRange.firstSlice, - textureRange.numSlices, - 0, - nullptr, - nullptr}, - texture}; -} - struct RenderGraphFilter { bool operator()(RenderGraph::vertex_descriptor u) const { return validPasses->operator[](u); @@ -900,7 +812,7 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { case ResourceDimension::TEXTURE2D: case ResourceDimension::TEXTURE3D: default: { - auto [info, texture] = getTextureBarrier(resg, resID, barrier); + auto* texture = resg.getTexture(resID); const auto* textureBarrier = static_cast(barrier.barrier); textures.emplace_back(texture); textureBarriers.emplace_back(textureBarrier); diff --git a/native/cocos/renderer/pipeline/custom/NativePipeline.cpp b/native/cocos/renderer/pipeline/custom/NativePipeline.cpp index 7dd5098424..bd6bab3fae 100644 --- a/native/cocos/renderer/pipeline/custom/NativePipeline.cpp +++ b/native/cocos/renderer/pipeline/custom/NativePipeline.cpp @@ -490,6 +490,9 @@ void buildLayoutGraphNodeBuffer( bool NativePipeline::activate(gfx::Swapchain *swapchainIn) { // setMacroInt("CC_PIPELINE_TYPE", 1); + // disable gfx internal deduce + gfx::Device::getInstance()->enableAutoBarrier(false); + swapchain = swapchainIn; globalDSManager->activate(device); pipelineSceneData->activate(device); diff --git a/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp b/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp index 3c2444666a..e9741363dc 100644 --- a/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp @@ -237,6 +237,11 @@ void ResourceGraph::unmount(uint64_t completedFenceValue) { if (texture.texture && texture.fenceValue <= completedFenceValue) { invalidatePersistentRenderPassAndFramebuffer(texture.texture.get()); texture.texture.reset(); + const auto& traits = get(ResourceGraph::Traits, resg, vertID); + if (traits.hasSideEffects()) { + auto& states = get(ResourceGraph::States, resg, vertID); + states.states = cc::gfx::AccessFlagBit::NONE; + } } } } diff --git a/native/cocos/renderer/pipeline/custom/test/test.h b/native/cocos/renderer/pipeline/custom/test/test.h index 498ff712f1..78321c465d 100644 --- a/native/cocos/renderer/pipeline/custom/test/test.h +++ b/native/cocos/renderer/pipeline/custom/test/test.h @@ -68,7 +68,7 @@ static void fillTestGraph(const ViewInfo &rasterData, const ResourceInfo &rescIn auto rescVertexID = add_vertex(rescGraph, ManagedTag{}, name.c_str()); rescGraph.descs[rescVertexID] = std::get<1>(resc); rescGraph.traits[rescVertexID] = std::get<2>(resc); - rescGraph.states[rescVertexID] = std::get<3>(resc); + rescGraph.states[rescVertexID].states = gfx::AccessFlagBit::NONE; } const auto &mem_resource = layoutGraphData.get_allocator(); diff --git a/native/tests/unit-test/src/complicated_barrier_case.cpp b/native/tests/unit-test/src/complicated_barrier_case.cpp index 802d10450f..346dc0cde5 100644 --- a/native/tests/unit-test/src/complicated_barrier_case.cpp +++ b/native/tests/unit-test/src/complicated_barrier_case.cpp @@ -100,7 +100,7 @@ TEST(complicatedBarrierTest, test12) { //node2 const auto& node2 = barrierMap.at(2); ExpectEq(node2.blockBarrier.frontBarriers.empty(), true); - ExpectEq(node2.blockBarrier.rearBarriers.size() == 2, true); + ExpectEq(node2.blockBarrier.rearBarriers.size() == 1, true); ExpectEq(node2.subpassBarriers.size() == 2, true); const auto& node2blockRear = node2.blockBarrier.rearBarriers; @@ -130,7 +130,7 @@ TEST(complicatedBarrierTest, test12) { //node3 const auto& node3 = barrierMap.at(3); ExpectEq(node3.blockBarrier.frontBarriers.empty(), true); - ExpectEq(node3.blockBarrier.rearBarriers.size() == 2, true); + ExpectEq(node3.blockBarrier.rearBarriers.size() == 1, true); ExpectEq(node3.subpassBarriers.empty(), true); const auto& node3block = node3.blockBarrier; @@ -144,16 +144,12 @@ TEST(complicatedBarrierTest, test12) { //node4 const auto& node4 = barrierMap.at(4); ExpectEq(node4.blockBarrier.frontBarriers.empty(), true); - ExpectEq(node4.blockBarrier.rearBarriers.size() == 2, true); + ExpectEq(node4.blockBarrier.rearBarriers.size() == 1, true); ExpectEq(node4.subpassBarriers.empty(), false); const auto& node4block = node4.blockBarrier; auto iter5in4 = findBarrierByResID(node4block.rearBarriers, 5); - ExpectEq(iter5in4 != node4block.rearBarriers.end(), true); - ExpectEq((*iter5in4).type == cc::gfx::BarrierType::FULL, true); - ExpectEq((*iter5in4).resourceID == 5, true); - ExpectEq((*iter5in4).beginStatus.vertID == 4, true); - ExpectEq((*iter5in4).endStatus.vertID == 4, true); + ExpectEq(iter5in4 == node4block.rearBarriers.end(), true); auto iter6in4 = findBarrierByResID(node4block.rearBarriers, 6); ExpectEq(iter6in4 != node4block.rearBarriers.end(), true); @@ -183,7 +179,7 @@ TEST(complicatedBarrierTest, test12) { ExpectEq(node4subpass1.rearBarriers[0].endStatus.access == MemoryAccessBit::READ_ONLY, true); const auto& node5 = barrierMap.at(5); - ExpectEq(node5.blockBarrier.frontBarriers.empty(), false); + ExpectEq(node5.blockBarrier.frontBarriers.empty(), true); ExpectEq(node5.blockBarrier.rearBarriers.size() == 1, true); ExpectEq(node5.subpassBarriers.empty(), true); @@ -216,7 +212,7 @@ TEST(complicatedBarrierTest, test12) { // node7 const auto& node7 = barrierMap.at(7); - ExpectEq(node7.blockBarrier.frontBarriers.empty(), false); + ExpectEq(node7.blockBarrier.frontBarriers.empty(), true); ExpectEq(node7.blockBarrier.rearBarriers.empty(), false); ExpectEq(node7.subpassBarriers.empty(), true); diff --git a/native/tests/unit-test/src/simple_closed_barrier_test.cpp b/native/tests/unit-test/src/simple_closed_barrier_test.cpp index bb189e0d64..108829e0b7 100644 --- a/native/tests/unit-test/src/simple_closed_barrier_test.cpp +++ b/native/tests/unit-test/src/simple_closed_barrier_test.cpp @@ -63,7 +63,7 @@ TEST(simpleClosedBarrierTest, test11) { const auto& node2 = barrierMap.at(2); ExpectEq(node2.blockBarrier.frontBarriers.empty(), true); - ExpectEq(node2.blockBarrier.rearBarriers.size() == 3, true); + ExpectEq(node2.blockBarrier.rearBarriers.size() == 2, true); ExpectEq(node2.subpassBarriers.empty(), true); // res3 @@ -138,7 +138,7 @@ TEST(simpleClosedBarrierTest, test11) { const auto& node7 = barrierMap.at(7); ExpectEq(node7.subpassBarriers.empty(), true); - ExpectEq(node7.blockBarrier.frontBarriers.empty(), false); + ExpectEq(node7.blockBarrier.frontBarriers.empty(), true); ExpectEq(node7.blockBarrier.rearBarriers.size() == 1, true); ExpectEq(node7.blockBarrier.rearBarriers[0].resourceID == 9, true);