Skip to content

Commit

Permalink
ResourceGraphs - Read from Assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Daethalus committed May 11, 2024
1 parent cf10e2e commit b3f1c1f
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 47 deletions.
10 changes: 6 additions & 4 deletions Engine/Source/Fyrion/Assets/AssetTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ namespace Fyrion
struct GraphNodeValue
{
constexpr static u32 Input = 0;
constexpr static u32 Value = 1;
constexpr static u32 PublicValue = 1;
constexpr static u32 Value = 2;
};

struct GraphNodeAsset
{
constexpr static u32 NodeType = 0;
constexpr static u32 Position = 1;
constexpr static u32 InputValues = 2;
constexpr static u32 NodeFunction = 0;
constexpr static u32 NodeOutput = 1;
constexpr static u32 Position = 2;
constexpr static u32 InputValues = 3;
};

struct GraphNodeLinkAsset
Expand Down
10 changes: 3 additions & 7 deletions Engine/Source/Fyrion/Assets/Assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Fyrion
{
ResourceTypeBuilder<GraphNodeValue>::Builder()
.Value<GraphNodeValue::Input, String>("NodeType")
.Value<GraphNodeValue::PublicValue, bool>("PublicValue")
.SubObject<GraphNodeValue::Value>("Value")
.Build();

Expand All @@ -22,16 +23,11 @@ namespace Fyrion
.Build();

ResourceTypeBuilder<GraphNodeAsset>::Builder()
.Value<GraphNodeAsset::NodeType, String>("NodeType")
.Value<GraphNodeAsset::NodeFunction, String>("NodeFunction")
.Value<GraphNodeAsset::NodeOutput, String>("NodeOutput")
.Value<GraphNodeAsset::Position, Vec2>("Position")
.SubObjectSet<GraphNodeAsset::InputValues>("InputValues")
.Build();

ResourceTypeBuilder<GraphNodeAsset>::Builder()
.Value<GraphNodeAsset::NodeType, String>("NodeType")
.Value<GraphNodeAsset::Position, Vec2>("Position")
.Build();

}

void RegisterAssets()
Expand Down
74 changes: 72 additions & 2 deletions Engine/Source/Fyrion/Resource/ResourceGraph.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ResourceGraph.hpp"

#include "Repository.hpp"
#include "Fyrion/Assets/AssetTypes.hpp"
#include "Fyrion/Core/Graph.hpp"
#include "Fyrion/Core/GraphTypes.hpp"
#include "Fyrion/Core/Logger.hpp"
Expand Down Expand Up @@ -126,9 +128,10 @@ namespace Fyrion

//***************************************************************ResourceGraph*************************************************************

ResourceGraph::ResourceGraph(const Span<ResourceGraphNodeInfo>& nodes,
const Span<ResourceGraphLinkInfo>& links) : m_links(links)
void ResourceGraph::SetGraph(const Span<ResourceGraphNodeInfo>& nodes, const Span<ResourceGraphLinkInfo>& links)
{
m_links = links;

Graph<u32, SharedPtr<ResourceGraphNodeData>> graph{};

for (const ResourceGraphNodeInfo& info : nodes)
Expand Down Expand Up @@ -275,6 +278,73 @@ namespace Fyrion
}
}

void ResourceGraph::SetGraph(RID assetGraph)
{
ResourceObject graphAsset = Repository::Read(assetGraph);

Array<RID> nodes = graphAsset.GetSubObjectSetAsArray(ResourceGraphAsset::Nodes);
Array<RID> links = graphAsset.GetSubObjectSetAsArray(ResourceGraphAsset::Links);

Array<ResourceGraphNodeInfo> nodesInfo;
Array<ResourceGraphLinkInfo> nodeLinks;

nodesInfo.Reserve(nodes.Size());
nodeLinks.Reserve(links.Size());

HashMap<RID, u32> nodeIds;
u32 idCount = 0;

for (const RID& node : nodes)
{
nodeIds[node] = idCount;

ResourceGraphNodeInfo nodeInfo;
nodeInfo.id = idCount;

ResourceObject nodeObject = Repository::Read(node);
if (nodeObject.Has(GraphNodeAsset::NodeFunction))
{
nodeInfo.functionHandler = Registry::FindFunctionByName(nodeObject[GraphNodeAsset::NodeFunction].As<String>());
}
else if (nodeObject.Has(GraphNodeAsset::NodeOutput))
{
nodeInfo.typeHandler = Registry::FindTypeByName(nodeObject[GraphNodeAsset::NodeOutput].As<String>());
}

Array<RID> inputValues = nodeObject.GetSubObjectSetAsArray(GraphNodeAsset::InputValues);

for (const RID& inputValue : inputValues)
{
ResourceObject valueObject = Repository::Read(inputValue);

RID valueSuboject = valueObject.GetSubObject(GraphNodeValue::Value);
nodeInfo.values.EmplaceBack(ResourceGraphNodeValue{
.name = valueObject[GraphNodeValue::Input].Value<String>(),
.typeHandler = Repository::GetResourceTypeHandler(valueSuboject),
.value = Repository::ReadData(valueSuboject),
.publicValue = valueObject[GraphNodeValue::PublicValue].Value<bool>(),
});
}

nodesInfo.EmplaceBack(nodeInfo);
idCount++;
}

for(const RID& link : links)
{
ResourceObject linkObject = Repository::Read(link);
nodeLinks.EmplaceBack(
ResourceGraphLinkInfo{
.outputNodeId = nodeIds[linkObject[GraphNodeLinkAsset::OutputNode].Value<RID>()],
.outputPin = linkObject[GraphNodeLinkAsset::OutputPin].As<String>(),
.inputNodeId = nodeIds[linkObject[GraphNodeLinkAsset::InputNode].Value<RID>()],
.inputPin = linkObject[GraphNodeLinkAsset::InputPin].As<String>(),
});
}

SetGraph(nodesInfo, nodeLinks);
}

ResourceGraphInstance* ResourceGraph::CreateInstance()
{
return m_allocator.Alloc<ResourceGraphInstance>(this);
Expand Down
55 changes: 30 additions & 25 deletions Engine/Source/Fyrion/Resource/ResourceGraph.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "ResourceTypes.hpp"
#include "Fyrion/Common.hpp"
#include "Fyrion/Core/Allocator.hpp"
#include "Fyrion/Core/Registry.hpp"
Expand All @@ -21,34 +22,34 @@ namespace Fyrion

struct ResourceGraphNodeValue
{
String name;
TypeHandler* typeHandler;
ConstPtr value;
bool publicValue;
String name{};
TypeHandler* typeHandler{};
ConstPtr value{};
bool publicValue{};
};

struct ResourceGraphNodeInfo
{
u32 id;
FunctionHandler* functionHandler;
TypeHandler* typeHandler;
Array<ResourceGraphNodeValue> values;
u32 id{};
FunctionHandler* functionHandler{};
TypeHandler* typeHandler{};
Array<ResourceGraphNodeValue> values{};
};

struct ResourceGraphLinkInfo
{
u32 outputNodeId;
String outputPin;
u32 inputNodeId;
String inputPin;
u32 outputNodeId{};
String outputPin{};
u32 inputNodeId{};
String inputPin{};
};


struct ResourceGraphNodeParamData
{
u32 offset = U32_MAX;
TypeHandler* typeHandler;
VoidPtr defaultValue;
TypeHandler* typeHandler{};
VoidPtr defaultValue{};
u32 copyOffset = U32_MAX;
};

Expand All @@ -57,21 +58,23 @@ namespace Fyrion
public:
ResourceGraphNodeData(ResourceGraph* resourceGraph, const ResourceGraphNodeInfo& info);

FY_FINLINE u32 GetId() const { return m_id; }
FY_FINLINE u32 GetId() const { return m_id; }
FY_FINLINE TypeHandler* GetTypeHandler() { return m_typeHandler; }
FY_FINLINE FunctionHandler* GetFunctionHandler() { return m_functionHandler; }

friend class ResourceGraphInstance;
friend class ResourceGraph;

private:
ResourceGraph* m_resourceGraph;
ResourceGraphNodeInfo m_info;
u32 m_id;
ResourceGraph* m_resourceGraph{};
ResourceGraphNodeInfo m_info{};
u32 m_id{};
bool m_valid = true;
FunctionHandler* m_functionHandler;
TypeHandler* m_typeHandler;
FunctionHandler* m_functionHandler{};
TypeHandler* m_typeHandler{};
u32 m_outputOffset = U32_MAX;
HashMap<String, u32> m_offsets;
HashMap<String, ResourceGraphLinkInfo> m_inputLinks;
HashMap<String, u32> m_offsets{};
HashMap<String, ResourceGraphLinkInfo> m_inputLinks{};
};

class FY_API ResourceGraphInstance
Expand All @@ -85,10 +88,10 @@ namespace Fyrion
void Execute();

template <typename T>
Span<T> GetOutputs() const
const T* GetOutput(u32 index) const
{
Span<ConstPtr> outputs = GetOutputs(GetTypeID<T>());
return Span<T>{*static_cast<const T*>(*outputs.begin()), *static_cast<const T*>(*outputs.end())};
return static_cast<const T*>(outputs[index]);
}

private:
Expand All @@ -102,9 +105,11 @@ namespace Fyrion
class FY_API ResourceGraph
{
public:
ResourceGraph(const Span<ResourceGraphNodeInfo>& nodes,
void SetGraph(const Span<ResourceGraphNodeInfo>& nodes,
const Span<ResourceGraphLinkInfo>& links);

void SetGraph(RID assetGraph);

ResourceGraphInstance* CreateInstance();
Span<SharedPtr<ResourceGraphNodeData>> GetNodes() const;

Expand Down
37 changes: 28 additions & 9 deletions Engine/Test/Fyrion/Resource/ResourceGraphTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace
String vl = "valuevalue1";
Repository::Commit(value, &vl);
ResourceObject object = Repository::Write(vl1);
object[GraphNodeValue::Input] = "input1"; //TODO string?
object[GraphNodeValue::Input] = "input1";
object.SetSubObject(GraphNodeValue::Value, value);
object.Commit();
}
Expand All @@ -106,7 +106,7 @@ namespace


ResourceObject object = Repository::Write(contentString1);
object[GraphNodeAsset::NodeType] = "Fyrion::ConcatString";
object[GraphNodeAsset::NodeFunction] = "Fyrion::ConcatString";
object.AddToSubObjectSet(GraphNodeAsset::InputValues, vl1);
object.AddToSubObjectSet(GraphNodeAsset::InputValues, vl2);
object.Commit();
Expand All @@ -116,17 +116,17 @@ namespace
RID output = Repository::CreateResource<GraphNodeAsset>();
{
ResourceObject object = Repository::Write(output);
object[GraphNodeAsset::NodeType] = "Fyrion::TestOutputNode";
object[GraphNodeAsset::NodeOutput] = "Fyrion::TestOutputNode";
object.Commit();
}

RID link1 = Repository::CreateResource<GraphNodeLinkAsset>();
{
ResourceObject object = Repository::Write(link1);
object[GraphNodeLinkAsset::InputNode] = contentString1;
object[GraphNodeLinkAsset::InputPin] = "out";
object[GraphNodeLinkAsset::OutputNode] = output;
object[GraphNodeLinkAsset::OutputPin] = "stringValue";
object[GraphNodeLinkAsset::OutputNode] = contentString1;
object[GraphNodeLinkAsset::OutputPin] = "out";
object[GraphNodeLinkAsset::InputNode] = output;
object[GraphNodeLinkAsset::InputPin] = "stringValue";
object.Commit();
}

Expand All @@ -138,7 +138,25 @@ namespace
object.Commit();
}
}
//TODO make tests with assets

ResourceGraph resourceGraph;
resourceGraph.SetGraph(rid);

auto graphNodes = resourceGraph.GetNodes();
CHECK(graphNodes.Size() == 2);
CHECK(graphNodes[0]->GetFunctionHandler() != nullptr);
CHECK(graphNodes[0]->GetTypeHandler() == nullptr);

CHECK(graphNodes[1]->GetFunctionHandler() == nullptr);
CHECK(graphNodes[1]->GetTypeHandler() != nullptr);

ResourceGraphInstance* instance = resourceGraph.CreateInstance();
instance->Execute();

const TestOutputNode* testOutputNode = instance->GetOutput<TestOutputNode>(0);
CHECK(testOutputNode->stringValue == "valuevalue1valuevalue2");

instance->Destroy();
}
Engine::Destroy();
}
Expand Down Expand Up @@ -178,7 +196,8 @@ namespace
ResourceGraphLinkInfo{.outputNodeId = 3, .outputPin = "out", .inputNodeId = 4, .inputPin = "stringSize",}
};

ResourceGraph resourceGraph = {nodes, links};
ResourceGraph resourceGraph;
resourceGraph.SetGraph(nodes, links);

auto graphNodes = resourceGraph.GetNodes();
CHECK(graphNodes[0]->GetId() == 0);
Expand Down

0 comments on commit b3f1c1f

Please sign in to comment.