From b351e709c18c831ec4dd09b858fa3889136b3979 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Thu, 9 Jan 2020 17:40:57 -0800 Subject: [PATCH] Framegraph improvements - added a way to compare two handle's underlaying resource (i.e. do they refer to the same resource) - minor refactoring and comment fixes --- filament/CMakeLists.txt | 1 + filament/src/fg/FrameGraph.cpp | 17 +++++- filament/src/fg/FrameGraph.h | 7 ++- filament/src/fg/FrameGraphHandle.cpp | 2 + filament/src/fg/FrameGraphHandle.h | 2 + filament/src/fg/fg/RenderTargetResource.cpp | 66 +++++++++++++++++++++ filament/src/fg/fg/RenderTargetResource.h | 32 +--------- 7 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 filament/src/fg/fg/RenderTargetResource.cpp diff --git a/filament/CMakeLists.txt b/filament/CMakeLists.txt index 3de27a70a2..3d2c93e0f4 100644 --- a/filament/CMakeLists.txt +++ b/filament/CMakeLists.txt @@ -48,6 +48,7 @@ set(SRCS src/fg/FrameGraph.cpp src/fg/FrameGraphHandle.cpp src/fg/fg/RenderTarget.cpp + src/fg/fg/RenderTargetResource.cpp src/fg/fg/ResourceEntry.cpp src/fg/ResourceAllocator.cpp src/Box.cpp diff --git a/filament/src/fg/FrameGraph.cpp b/filament/src/fg/FrameGraph.cpp index 2141740e3a..d47bb53d83 100644 --- a/filament/src/fg/FrameGraph.cpp +++ b/filament/src/fg/FrameGraph.cpp @@ -199,6 +199,21 @@ bool FrameGraph::isValid(FrameGraphHandle handle) const noexcept { return node.version == node.resource->version; } +bool FrameGraph::equal(FrameGraphHandle lhs, FrameGraphHandle rhs) const noexcept { + if (lhs == rhs) { + return true; + } + if (lhs.isValid() != rhs.isValid()) { + return false; + } + auto const& registry = mResourceNodes; + assert(lhs.index < registry.size()); + assert(rhs.index < registry.size()); + assert(registry[lhs.index].resource); + assert(registry[rhs.index].resource); + return registry[lhs.index].resource == registry[rhs.index].resource; +} + FrameGraphHandle FrameGraph::createResourceNode(fg::ResourceEntryBase* resource) noexcept { auto& resourceNodes = mResourceNodes; size_t index = resourceNodes.size(); @@ -288,7 +303,7 @@ bool FrameGraph::equals(FrameGraphRenderTarget::Descriptor const& cacheEntry, FrameGraphRenderTarget::Descriptor const& rt) const noexcept { const Vector& resourceNodes = mResourceNodes; - // if the rendertarget we're looking up doesn't has the sample field set to 0, it means the + // if the rendertarget we're looking up doesn't have the sample field set to 0, it means the // user didn't specify it, and it's okay to match it to any sample count. // Otherwise, sample count must match, with the caveat that 0 or 1 are treated the same. const bool samplesMatch = (!rt.samples) || diff --git a/filament/src/fg/FrameGraph.h b/filament/src/fg/FrameGraph.h index 09bbf1f424..e7ba1e103b 100644 --- a/filament/src/fg/FrameGraph.h +++ b/filament/src/fg/FrameGraph.h @@ -148,8 +148,11 @@ public: FrameGraph& operator = (FrameGraph const&) = delete; ~FrameGraph(); + // returns the default Blackboard Blackboard& getBlackboard() noexcept { return mBlackboard; } - Blackboard const& getDefaultBlackboard() const noexcept { return mBlackboard; } + + // returns the default Blackboard + Blackboard const& getBlackboard() const noexcept { return mBlackboard; } struct Empty{}; @@ -195,6 +198,8 @@ public: // it's used to declare a resource write (see Builder::write()). bool isValid(FrameGraphHandle r) const noexcept; + bool equal(FrameGraphHandle lhs, FrameGraphHandle rhs) const noexcept; + // Return the Descriptor associated to this resource handle. The handle must be valid. template typename T::Descriptor& getDescriptor(FrameGraphId r) { diff --git a/filament/src/fg/FrameGraphHandle.cpp b/filament/src/fg/FrameGraphHandle.cpp index fc54fb7ed2..105aae3241 100644 --- a/filament/src/fg/FrameGraphHandle.cpp +++ b/filament/src/fg/FrameGraphHandle.cpp @@ -49,6 +49,8 @@ void FrameGraphTexture::create(FrameGraph& fg, const char* name, texture = fg.getResourceAllocator().createTexture(name, desc.type, desc.levels, desc.format, samples, desc.width, desc.height, desc.depth, desc.usage); + + assert(texture); } void FrameGraphTexture::destroy(FrameGraph& fg) noexcept { diff --git a/filament/src/fg/FrameGraphHandle.h b/filament/src/fg/FrameGraphHandle.h index f6db4458c5..aae2c42989 100644 --- a/filament/src/fg/FrameGraphHandle.h +++ b/filament/src/fg/FrameGraphHandle.h @@ -83,6 +83,8 @@ class FrameGraphHandle { public: bool isValid() const noexcept { return index != UNINITIALIZED; } + void clear() noexcept { index = UNINITIALIZED; } + bool operator < (const FrameGraphHandle& rhs) const noexcept { return index < rhs.index; } diff --git a/filament/src/fg/fg/RenderTargetResource.cpp b/filament/src/fg/fg/RenderTargetResource.cpp new file mode 100644 index 0000000000..85089debd5 --- /dev/null +++ b/filament/src/fg/fg/RenderTargetResource.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "RenderTargetResource.h" + +namespace filament { + +using namespace backend; + +namespace fg { + +void RenderTargetResource::create(FrameGraph& fg) noexcept { + if (!imported) { + if (any(attachments)) { + // devirtualize our texture handles. By this point these handles have been + // remapped to their alias if any. + backend::TargetBufferInfo infos[FrameGraphRenderTarget::Attachments::COUNT]; + for (size_t i = 0, c = desc.attachments.textures.size(); i < c; i++) { + + static constexpr TargetBufferFlags flags[] = { + TargetBufferFlags::COLOR, + TargetBufferFlags::DEPTH, + TargetBufferFlags::STENCIL }; + + auto const& attachmentInfo = desc.attachments.textures[i]; + assert(bool(attachments & flags[i]) == attachmentInfo.isValid()); + + if (attachmentInfo.isValid()) { + fg::ResourceEntry const& entry = + fg.getResourceEntryUnchecked(attachmentInfo.getHandle()); + infos[i].handle = entry.getResource().texture; + infos[i].level = attachmentInfo.getLevel(); + assert(infos[i].handle); + } + } + targetInfo.target = fg.getResourceAllocator().createRenderTarget(name, + attachments, width, height, desc.samples, + infos[0], infos[1], {}); + } + } +} + +void RenderTargetResource::destroy(FrameGraph& fg) noexcept { + if (!imported) { + if (targetInfo.target) { + fg.getResourceAllocator().destroyRenderTarget(targetInfo.target); + targetInfo.target.clear(); + } + } +} + +} // namespace fg +} // namespace filament diff --git a/filament/src/fg/fg/RenderTargetResource.h b/filament/src/fg/fg/RenderTargetResource.h index 5158509a2a..104f35b23d 100644 --- a/filament/src/fg/fg/RenderTargetResource.h +++ b/filament/src/fg/fg/RenderTargetResource.h @@ -68,37 +68,9 @@ struct RenderTargetResource final : public VirtualResource { // 104 // updated during execute with the current pass' discard flags FrameGraphPassResources::RenderTargetInfo targetInfo; - void create(FrameGraph& fg) noexcept override { - if (!imported) { - if (any(attachments)) { - // devirtualize our texture handles. By this point these handles have been - // remapped to their alias if any. - backend::TargetBufferInfo infos[FrameGraphRenderTarget::Attachments::COUNT]; - for (size_t i = 0, c = desc.attachments.textures.size(); i < c; i++) { - auto const& attachmentInfo = desc.attachments.textures[i]; - if (attachmentInfo.isValid()) { - fg::ResourceEntry const& entry = - fg.getResourceEntryUnchecked(attachmentInfo.getHandle()); - infos[i].handle = entry.getResource().texture; - infos[i].level = attachmentInfo.getLevel(); - } - } + void create(FrameGraph& fg) noexcept override; - // create the concrete rendertarget - targetInfo.target = fg.getResourceAllocator().createRenderTarget(name, - attachments, width, height, desc.samples, infos[0], infos[1], {}); - } - } - } - - void destroy(FrameGraph& fg) noexcept override { - if (!imported) { - if (targetInfo.target) { - fg.getResourceAllocator().destroyRenderTarget(targetInfo.target); - targetInfo.target.clear(); - } - } - } + void destroy(FrameGraph& fg) noexcept override; }; } // namespace fg