Improve import API and code

This commit is contained in:
Mathias Agopian
2019-08-15 16:58:32 -07:00
committed by Mathias Agopian
parent e22f02d228
commit 573e9220a2
3 changed files with 19 additions and 36 deletions

View File

@@ -370,7 +370,7 @@ FrameGraphResourceId<FrameGraphTexture> FrameGraph::importResource(const char* n
// create the resource that will be returned to the user
FrameGraphTexture::Descriptor desc{ .width = width, .height = height };
FrameGraphResourceId<FrameGraphTexture> rt = import<FrameGraphTexture>(name, desc);
FrameGraphResourceId<FrameGraphTexture> rt = import<FrameGraphTexture>(name, desc, {});
descriptor.attachments.textures[0] = rt;
// Populate the cache with a RenderTargetResource
@@ -388,17 +388,6 @@ FrameGraphResourceId<FrameGraphTexture> FrameGraph::importResource(const char* n
return rt;
}
FrameGraphResourceId<FrameGraphTexture> FrameGraph::importResource(
const char* name, FrameGraphTexture::Descriptor const& descriptor,
backend::Handle<backend::HwTexture> color) {
FrameGraphResourceId<FrameGraphTexture> r = import<FrameGraphTexture>(name, descriptor);
// FIXME: we need to pass a constructed resource here, e.g. a FrameGraphTexture
fg::ResourceNode& node = mResourceNodes[r.index];
static_cast<ResourceEntry<FrameGraphTexture> *>(node.resource)->getResource().texture = color;
return r;
}
TargetBufferFlags FrameGraph::computeDiscardFlags(DiscardPhase phase,
PassNode const* curr, PassNode const* first, fg::RenderTarget const& renderTarget) {
auto& resourceNodes = mResourceNodes;

View File

@@ -194,11 +194,13 @@ public:
backend::TargetBufferFlags discardStart = backend::TargetBufferFlags::NONE,
backend::TargetBufferFlags discardEnd = backend::TargetBufferFlags::NONE);
// Import a read-only render target from outside the framegraph and returns a handle to it.
FrameGraphResourceId<FrameGraphTexture> importResource(
const char* name, FrameGraphTexture::Descriptor const& descriptor,
backend::Handle<backend::HwTexture> color);
template<typename T>
FrameGraphResourceId<T> import(const char* name,
typename T::Descriptor const& desc, const T& resource) noexcept {
fg::ResourceEntryBase* pBase = mArena.make<fg::ResourceEntry<T>>(name, desc, resource, mId++);
return FrameGraphResourceId<T>(create(pBase));
}
// Moves the resource associated to the handle 'from' to the handle 'to'. After this call,
// all handles referring to the resource 'to' are redirected to the resource 'from'
@@ -275,14 +277,7 @@ private:
template<typename T>
FrameGraphResourceId<T> create(const char* name, typename T::Descriptor const& desc) noexcept {
fg::ResourceEntryBase* pBase = mArena.make<fg::ResourceEntry<T>>(name, desc, mId++, false);
FrameGraphResourceId<T> r(create(pBase));
return r;
}
template<typename T>
FrameGraphResourceId<T> import(const char* name, typename T::Descriptor const& desc) noexcept {
fg::ResourceEntryBase* pBase = mArena.make<fg::ResourceEntry<T>>(name, desc, mId++, true);
fg::ResourceEntryBase* pBase = mArena.make<fg::ResourceEntry<T>>(name, desc, mId++);
FrameGraphResourceId<T> r(create(pBase));
return r;
}

View File

@@ -19,8 +19,6 @@
#include "VirtualResource.h"
#include <type_traits>
#include <stdint.h>
namespace filament {
@@ -52,33 +50,34 @@ public:
template<typename T>
class ResourceEntry : public ResourceEntryBase {
using Storage = std::aligned_storage_t<sizeof(T), alignof(T)>;
Storage resource;
T resource{};
public:
using Descriptor = typename T::Descriptor;
Descriptor descriptor;
explicit ResourceEntry(const char* name, Descriptor const& desc, uint16_t id, bool imported) noexcept
: ResourceEntryBase(name, id, imported), descriptor(desc) {
ResourceEntry(const char* name, Descriptor const& desc, uint16_t id) noexcept
: ResourceEntryBase(name, id, false), descriptor(desc) {
}
T const& getResource() const noexcept { return reinterpret_cast<T const&>(resource); }
ResourceEntry(const char* name, Descriptor const& desc, const T& r, uint16_t id) noexcept
: ResourceEntryBase(name, id, true), resource(r), descriptor(desc) {
}
T& getResource() noexcept { return reinterpret_cast<T&>(resource); }
T const& getResource() const noexcept { return resource; }
T& getResource() noexcept { return resource; }
void create(FrameGraph& fg) noexcept override {
T* r = new(&resource) T();
if (!imported) {
r->create(fg, name, descriptor);
resource.create(fg, name, descriptor);
}
}
void destroy(FrameGraph& fg) noexcept override {
if (!imported) {
getResource().destroy(fg);
resource.destroy(fg);
}
reinterpret_cast<T*>(&resource)->~T();
}
};