Skip to content

Commit

Permalink
Small node editor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
goossens committed Jan 2, 2024
1 parent afd6f70 commit 97a3ef5
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 73 deletions.
93 changes: 78 additions & 15 deletions examples/graphs/test.otg
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
"id": 2524307594,
"to": 3420868335
},
{
"from": 753009503,
"id": 903463477,
"to": 3883694873
},
{
"from": 753009503,
"id": 593269723,
Expand All @@ -23,12 +18,32 @@
{
"from": 1182013951,
"id": 3965693708,
"to": 1832054022
"to": 3389012718
},
{
"from": 3612936401,
"id": 2747130417,
"to": 963047489
},
{
"from": 753009503,
"id": 903463477,
"to": 3883694873
},
{
"from": 4061513768,
"id": 3716355614,
"to": 1832054022
},
{
"from": 1182013951,
"id": 2982816052,
"to": 2538487069
},
{
"from": 3051670333,
"id": 3233047404,
"to": 778511389
}
],
"metadata": {},
Expand All @@ -37,29 +52,29 @@
"id": 37376410,
"inputs": [],
"name": "Float Input",
"value": 2.0,
"outputs": [
{
"id": 3348868259,
"name": "Value",
"type": "float"
}
],
"value": 2.0,
"x": 56.0,
"y": 151.0
},
{
"id": 2507790077,
"inputs": [],
"name": "Float Input",
"value": 1.0,
"outputs": [
{
"id": 362673415,
"name": "Value",
"type": "float"
}
],
"value": 1.0,
"x": 50.0,
"y": 267.0
},
Expand Down Expand Up @@ -92,14 +107,14 @@
"id": 87380946,
"inputs": [],
"name": "Float Input",
"value": 5.0,
"outputs": [
{
"id": 3612936401,
"name": "Value",
"type": "float"
}
],
"value": 5.0,
"x": 233.0,
"y": 305.0
},
Expand Down Expand Up @@ -139,8 +154,28 @@
],
"name": "Float Output",
"outputs": [],
"x": 592.0,
"y": 40.0
"x": 691.0,
"y": 44.0
},
{
"id": 3921401313,
"inputs": [
{
"id": 3389012718,
"name": "A",
"type": "float"
}
],
"name": "Abs",
"outputs": [
{
"id": 4061513768,
"name": "Result",
"type": "float"
}
],
"x": 573.0,
"y": 327.0
},
{
"id": 1110028080,
Expand All @@ -153,23 +188,51 @@
],
"name": "Float Output",
"outputs": [],
"x": 592.0,
"y": 113.0
"x": 692.0,
"y": 230.0
},
{
"id": 1137274146,
"image": "../scenes/earth/assets/earth.png",
"inputs": [],
"name": "Image Input",
"outputs": [
{
"id": 3051670333,
"name": "Value",
"name": "Image",
"type": "image"
}
],
"value": "../scenes/earth/assets/earth.png",
"x": 419.0,
"y": 451.0
},
{
"id": 1000952904,
"inputs": [
{
"id": 2538487069,
"name": "Value",
"type": "float"
}
],
"name": "Float Output",
"outputs": [],
"x": 693.0,
"y": 135.0
},
{
"id": 3964610701,
"inputs": [
{
"id": 778511389,
"name": "Image",
"type": "image"
}
],
"name": "Image Output",
"outputs": [],
"x": 693.0,
"y": 454.0
}
]
}
2 changes: 1 addition & 1 deletion gfx/assets/OtAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OtAsset {
acquire();
}

// clear the asset refence
// clear the asset reference
inline void clear() {
path.clear();
ptr = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions gfx/assets/OtAssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void OtAssetManager::start() {
asset->assetState = asset->load(asset->assetPath) ? OtAssetBase::readyState : OtAssetBase::invalidState;

} else {
// a null asset means we shut down the loader
// a null asset means we shutdown the loader
running = false;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ OtAssetBase* OtAssetManager::lookup(const std::filesystem::path& path) {
// nope, let's instantiate it now and schedule it for loading
auto instance = OtAssetFactory::instance()->instantiate(fullPath);

// ensure the factory could create an asset for the path
// ensure the factory can create an asset for this path
if (instance) {
instance->assetPath = fullPath;
instance->assetState = OtAssetBase::scheduledState;
Expand Down
9 changes: 7 additions & 2 deletions gfx/resources/OtFrameBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "OtBgfxHandle.h"

#include "OtSampler.h"
#include "OtTexture.h"


//
Expand Down Expand Up @@ -56,8 +57,12 @@ class OtFrameBuffer {
inline int getHeight() { return height; }

// get textures
inline bgfx::TextureHandle getColorTexture() { return colorTexture.getHandle(); }
inline bgfx::TextureHandle getDepthTexture() { return depthTexture.getHandle(); }
inline OtTexture getColorTexture() { return OtTexture(colorTexture, width, height); }
inline OtTexture getDepthTexture() { return OtTexture(depthTexture, width, height); }

// get texture handles
inline bgfx::TextureHandle getColorTextureHandle() { return colorTexture.getHandle(); }
inline bgfx::TextureHandle getDepthTextureHandle() { return depthTexture.getHandle(); }

// get texture indices
inline int getColorTextureIndex() { return colorTexture.getIndex(); }
Expand Down
1 change: 1 addition & 0 deletions gfx/resources/OtTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class OtTexture {

// constructor
OtTexture() = default;
OtTexture(OtBgfxHandle<bgfx::TextureHandle> t, size_t w, size_t h) : texture(t), width(w), height(h) {}
OtTexture(const std::filesystem::path& path);

// clear the resources
Expand Down
62 changes: 39 additions & 23 deletions graph/core/OtGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@
#include "OtGraphUtils.h"


//
// OtGraph::~OtGraph
//

OtGraph::~OtGraph() {
// ensure we dispose in the right order due to circular dependencies
clear();
}


//
// OtGraph::clear
//

void OtGraph::clear() {
nodes.clear();
links.clear();
nodeIndex.clear();
linkIndex.clear();
pinIndex.clear();
nodeIndex.clear();
links.clear();
nodes.clear();
}


//
// OtGraph::load
//
Expand Down Expand Up @@ -78,7 +88,18 @@ void OtGraph::load(const std::filesystem::path& path, nlohmann::json* metadata)

// restore links
for (auto& link : data["links"]) {
createLink(link["from"], link["to"], link["id"]);
uint32_t linkId = link["id"];
uint32_t fromId = link["from"];
uint32_t toId = link["to"];

if (pinIndex.count(fromId) == 0) {
OtError("Invalid 'from' pin ID [%s] in link [%d]", fromId, linkId);

} else if (pinIndex.count(toId) == 0) {
OtError("Invalid 'to' pin ID [%s] in link [%d]", toId, linkId);
}

createLink(fromId, toId, linkId);
}

// set the flag
Expand Down Expand Up @@ -192,10 +213,10 @@ void OtGraph::deleteNodes(const std::vector<uint32_t>& nodes) {


//
// hasCycle
// OtGraph::hasCycle
//

static bool hasCycle(OtGraphNode node, OtGraphNode newTarget=nullptr) {
bool OtGraph::hasCycle(OtGraphNodeClass* node, OtGraphNodeClass* newTarget) {
// function result
bool cycle = false;

Expand All @@ -217,7 +238,7 @@ static bool hasCycle(OtGraphNode node, OtGraphNode newTarget=nullptr) {
});

// also check the possible new connection (if required)
if (!cycle && newTarget != nullptr) {
if (!cycle && newTarget) {
cycle = hasCycle(newTarget);
}

Expand Down Expand Up @@ -464,7 +485,7 @@ std::string OtGraph::archiveNodes(const std::vector<uint32_t>& selection) {
nodes.push_back(node->serialize());

eachLink([&](OtGraphLink& link) {
if (link->from->node == node || link->to->node == node) {
if (link->from->node->id == id || link->to->node->id == id) {
associatedLinks.insert(link);
}
});
Expand All @@ -489,10 +510,12 @@ OtGraphNode OtGraph::restoreNode(nlohmann::json data, bool restoreIDs, std::file
// create a new node
auto node = factory.createNode(data["name"]);
node->deserialize(data, restoreIDs, basedir);
node->needsPlacement = true;
nodes.emplace_back(node);
needsSorting = true;

node->needsPlacement = true;
node->needsRunning = true;

// index node and pins
indexNode(node);

Expand Down Expand Up @@ -549,18 +572,10 @@ void OtGraph::indexNode(OtGraphNode node) {
nodeIndex[node->id] = node;

node->eachInput([this](OtGraphPin& pin) {
if (!pin) {
int i = 0;
}

pinIndex[pin->id] = pin;
});

node->eachOutput([this](OtGraphPin& pin) {
if (!pin) {
int i = 0;
}

pinIndex[pin->id] = pin;
});
}
Expand All @@ -584,10 +599,10 @@ void OtGraph::unindexNode(OtGraphNode node) {


//
// visitNode
// OtGraph::visitNode
//

static bool visitNode(OtGraphNode& node, std::vector<OtGraphNode>& nodes) {
bool OtGraph::visitNode(OtGraphNode& node, std::vector<OtGraphNode>& nodes) {
// function result
bool cycle = false;

Expand All @@ -604,7 +619,7 @@ static bool visitNode(OtGraphNode& node, std::vector<OtGraphNode>& nodes) {
// visit all nodes it depends on
node->eachInput([&] (OtGraphPin& pin) {
if (!cycle && pin->sourcePin != nullptr) {
cycle = visitNode(pin->sourcePin->node, nodes);
cycle = visitNode(nodeIndex[pin->sourcePin->node->id], nodes);
}
});

Expand All @@ -622,10 +637,10 @@ static bool visitNode(OtGraphNode& node, std::vector<OtGraphNode>& nodes) {


//
// sortNodesTopologically
// OtGraph::sortNodesTopologically
//

static bool sortNodesTopologically(std::vector<OtGraphNode>& nodes) {
bool OtGraph::sortNodesTopologically(std::vector<OtGraphNode>& nodes) {
// based on https://en.wikipedia.org/wiki/Topological_sorting

// clear all flags
Expand All @@ -644,6 +659,7 @@ static bool sortNodesTopologically(std::vector<OtGraphNode>& nodes) {
}
}

// sort was succesful
nodes.swap(sortedNodes);
return true;
}
Expand Down
Loading

0 comments on commit 97a3ef5

Please sign in to comment.