New evaluation scheme.
- Animation Data are always referenced via a pointer in nodes. - Animation Data storage pointers are injected from graph when node is evaluated. - Node value outputs always stored in node. - Node value inputs always referenced via pointer. References are created when instantiating the graph.
This commit is contained in:
@@ -12,10 +12,10 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "SyncTrack.h"
|
||||
#include "AnimGraph.h"
|
||||
#include "AnimGraphData.h"
|
||||
#include "AnimGraphNodes.h"
|
||||
#include "AnimGraph.h"
|
||||
#include "SyncTrack.h"
|
||||
|
||||
struct AnimNode;
|
||||
struct NodeSocketAccessorBase;
|
||||
@@ -41,17 +41,17 @@ static inline AnimNodeResource AnimNodeResourceFactory(
|
||||
//
|
||||
// AnimGraphResource
|
||||
//
|
||||
struct AnimGraphConnection {
|
||||
const AnimNodeResource* m_source_node = nullptr;
|
||||
const Socket* m_source_socket = nullptr;
|
||||
const AnimNodeResource* m_target_node = nullptr;
|
||||
const Socket* m_target_socket = nullptr;
|
||||
struct AnimGraphConnectionResource {
|
||||
size_t source_node_index = -1;
|
||||
std::string source_socket_name = "";
|
||||
size_t target_node_index = -1;
|
||||
std::string target_socket_name = "";
|
||||
};
|
||||
|
||||
struct AnimGraphResource {
|
||||
std::string m_name;
|
||||
std::vector<AnimNodeResource> m_nodes;
|
||||
std::vector<AnimGraphConnection> m_connections;
|
||||
std::vector<AnimGraphConnectionResource> m_connections;
|
||||
|
||||
~AnimGraphResource() {
|
||||
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||||
@@ -94,23 +94,11 @@ struct AnimGraphResource {
|
||||
const std::string& source_socket_name,
|
||||
const AnimNodeResource& target_node,
|
||||
const std::string& target_socket_name) {
|
||||
size_t source_index = -1;
|
||||
size_t target_index = -1;
|
||||
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||||
if (&source_node == &m_nodes[i]) {
|
||||
source_index = i;
|
||||
}
|
||||
size_t source_node_index = getNodeIndex(source_node);
|
||||
size_t target_node_index = getNodeIndex(target_node);
|
||||
|
||||
if (&target_node == &m_nodes[i]) {
|
||||
target_index = i;
|
||||
}
|
||||
|
||||
if (source_index < m_nodes.size() && target_index < m_nodes.size()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (source_index >= m_nodes.size() || target_index >= m_nodes.size()) {
|
||||
if (source_node_index >= m_nodes.size()
|
||||
|| target_node_index >= m_nodes.size()) {
|
||||
std::cerr << "Cannot connect nodes: could not find nodes." << std::endl;
|
||||
return false;
|
||||
}
|
||||
@@ -125,216 +113,20 @@ struct AnimGraphResource {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimGraphConnection connection;
|
||||
connection.m_source_node = &source_node;
|
||||
connection.m_source_socket = source_socket;
|
||||
connection.m_target_node = &target_node;
|
||||
connection.m_target_socket = target_socket;
|
||||
AnimGraphConnectionResource connection;
|
||||
connection.source_node_index = source_node_index;
|
||||
connection.source_socket_name = source_socket_name;
|
||||
connection.target_node_index = target_node_index;
|
||||
connection.target_socket_name = target_socket_name;
|
||||
m_connections.push_back(connection);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
AnimGraph createInstance() {
|
||||
AnimGraph result;
|
||||
|
||||
// create nodes
|
||||
for (int i = 0; i < m_nodes.size(); i++) {
|
||||
const AnimNodeResource& node_resource = m_nodes[i];
|
||||
AnimNode* node = AnimNodeFactory(node_resource.m_type_name.c_str());
|
||||
node->m_name = node_resource.m_name;
|
||||
node->m_node_type_name = node_resource.m_type_name;
|
||||
node->m_index = i;
|
||||
result.m_nodes.push_back(node);
|
||||
|
||||
assert(node_resource.m_socket_accessor != nullptr);
|
||||
result.m_node_inputs.push_back(std::vector<NodeInput>());
|
||||
std::vector<NodeInput>& node_inputs = result.m_node_inputs.back();
|
||||
|
||||
for (int j = 0, n = node_resource.m_socket_accessor->m_inputs.size();
|
||||
j < n;
|
||||
j++) {
|
||||
const Socket& input_socket =
|
||||
node_resource.m_socket_accessor->m_inputs[j];
|
||||
|
||||
NodeInput input;
|
||||
input.m_node = nullptr;
|
||||
input.m_type = input_socket.m_type;
|
||||
input.m_input_name = input_socket.m_name;
|
||||
|
||||
node_inputs.push_back(input);
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare graph inputs
|
||||
result.m_socket_accessor =
|
||||
AnimNodeAccessorFactory("BlendTree", result.m_nodes[0]);
|
||||
result.m_socket_accessor->m_outputs =
|
||||
m_nodes[1].m_socket_accessor->m_outputs;
|
||||
result.m_socket_accessor->m_inputs =
|
||||
m_nodes[0].m_socket_accessor->m_inputs;
|
||||
|
||||
// inputs
|
||||
int input_block_size = 0;
|
||||
std::vector<Socket>& graph_inputs = result.getGraphInputs();
|
||||
for (int i = 0; i < graph_inputs.size(); i++) {
|
||||
input_block_size += sizeof(void*);
|
||||
}
|
||||
result.m_input_buffer = new char[input_block_size];
|
||||
memset(result.m_input_buffer, 0, input_block_size);
|
||||
|
||||
int input_block_offset = 0;
|
||||
for (int i = 0; i < graph_inputs.size(); i++) {
|
||||
if (graph_inputs[i].m_type == SocketType::SocketTypeAnimation) {
|
||||
}
|
||||
graph_inputs[i].m_value.ptr =
|
||||
(void*)&result.m_input_buffer[input_block_offset];
|
||||
input_block_offset += sizeof(void*);
|
||||
}
|
||||
|
||||
// outputs
|
||||
int output_block_size = 0;
|
||||
std::vector<Socket>& graph_outputs = result.getGraphOutputs();
|
||||
for (int i = 0; i < graph_outputs.size(); i++) {
|
||||
output_block_size += graph_outputs[i].m_type_size;
|
||||
}
|
||||
result.m_output_buffer = new char[output_block_size];
|
||||
memset(result.m_output_buffer, 0, output_block_size);
|
||||
|
||||
int output_block_offset = 0;
|
||||
for (int i = 0; i < graph_outputs.size(); i++) {
|
||||
if (graph_outputs[i].m_type == SocketType::SocketTypeAnimation) {
|
||||
}
|
||||
graph_outputs[i].m_value.ptr =
|
||||
(void*)&result.m_output_buffer[output_block_offset];
|
||||
output_block_offset += graph_outputs[i].m_type_size;
|
||||
}
|
||||
|
||||
// connect the nodes
|
||||
for (int i = 0; i < m_connections.size(); i++) {
|
||||
const AnimGraphConnection& connection = m_connections[i];
|
||||
std::string source_node_type = "";
|
||||
std::string target_node_type = "";
|
||||
std::string source_node_name = "";
|
||||
std::string target_node_name = "";
|
||||
AnimNode* source_node = nullptr;
|
||||
AnimNode* target_node = nullptr;
|
||||
NodeSocketAccessorBase* source_node_accessor = nullptr;
|
||||
NodeSocketAccessorBase* target_node_accessor = nullptr;
|
||||
SocketType source_type;
|
||||
SocketType target_type;
|
||||
size_t source_socket_index = -1;
|
||||
size_t target_socket_index = -1;
|
||||
|
||||
if (connection.m_source_node != nullptr) {
|
||||
size_t node_index = getNodeIndex(*connection.m_source_node);
|
||||
if (node_index == -1) {
|
||||
std::cerr << "Could not find source node index." << std::endl;
|
||||
continue;
|
||||
}
|
||||
source_node = result.m_nodes[node_index];
|
||||
source_node_name = source_node->m_name;
|
||||
source_node_type = source_node->m_node_type_name;
|
||||
if (node_index == 1) {
|
||||
source_node_accessor = result.m_socket_accessor;
|
||||
} else {
|
||||
source_node_accessor =
|
||||
AnimNodeAccessorFactory(source_node_type, source_node);
|
||||
}
|
||||
}
|
||||
|
||||
if (connection.m_target_node != nullptr) {
|
||||
size_t node_index = getNodeIndex(*connection.m_target_node);
|
||||
if (node_index == -1) {
|
||||
std::cerr << "Could not find source node index." << std::endl;
|
||||
continue;
|
||||
}
|
||||
target_node = result.m_nodes[node_index];
|
||||
target_node_name = target_node->m_name;
|
||||
target_node_type = target_node->m_node_type_name;
|
||||
if (node_index == 0) {
|
||||
target_node_accessor = result.m_socket_accessor;
|
||||
} else {
|
||||
target_node_accessor =
|
||||
AnimNodeAccessorFactory(target_node_type, target_node);
|
||||
}
|
||||
}
|
||||
|
||||
assert(source_node != nullptr);
|
||||
assert(target_node != nullptr);
|
||||
|
||||
//
|
||||
// Map resource node sockets to graph instance node sockets
|
||||
//
|
||||
if (connection.m_source_socket == nullptr) {
|
||||
std::cerr << "Invalid source socket for connection " << i << "."
|
||||
<< std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connection.m_target_socket == nullptr) {
|
||||
std::cerr << "Invalid source socket for connection " << i << "."
|
||||
<< std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
source_socket_index = source_node_accessor->GetOutputIndex(
|
||||
connection.m_source_socket->m_name);
|
||||
if (source_socket_index == -1) {
|
||||
std::cerr << "Invalid source socket "
|
||||
<< connection.m_source_socket->m_name << " for node "
|
||||
<< connection.m_source_node->m_name << "." << std::endl;
|
||||
continue;
|
||||
}
|
||||
const Socket* source_socket =
|
||||
&source_node_accessor->m_outputs[source_socket_index];
|
||||
|
||||
target_socket_index = target_node_accessor->GetInputIndex(
|
||||
connection.m_target_socket->m_name);
|
||||
if (target_socket_index == -1) {
|
||||
std::cerr << "Invalid target socket "
|
||||
<< connection.m_target_socket->m_name << " for node "
|
||||
<< connection.m_target_node->m_name << "." << std::endl;
|
||||
continue;
|
||||
}
|
||||
const Socket* target_socket =
|
||||
&target_node_accessor->m_inputs[target_socket_index];
|
||||
|
||||
if (source_socket->m_type != target_socket->m_type) {
|
||||
std::cerr << "Cannot connect sockets: invalid types!" << std::endl;
|
||||
}
|
||||
|
||||
//
|
||||
// Wire up outputs to inputs.
|
||||
//
|
||||
(*source_socket->m_value.ptr_ptr) = target_socket->m_value.ptr;
|
||||
|
||||
size_t target_node_index = target_node->m_index;
|
||||
|
||||
std::vector<NodeInput>& node_inputs =
|
||||
result.m_node_inputs[target_node_index];
|
||||
for (int j = 0, n = node_inputs.size(); j < n; j++) {
|
||||
if (node_inputs[j].m_input_name == target_socket->m_name) {
|
||||
node_inputs[j].m_node = source_node;
|
||||
}
|
||||
}
|
||||
|
||||
if (target_node_accessor != result.m_socket_accessor) {
|
||||
delete target_node_accessor;
|
||||
}
|
||||
|
||||
if (source_node_accessor != result.m_socket_accessor) {
|
||||
delete source_node_accessor;
|
||||
}
|
||||
}
|
||||
|
||||
result.updateOrderedNodes();
|
||||
result.reset();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AnimGraph createInstance() const;
|
||||
void createRuntimeNodeInstances(AnimGraph& instance) const;
|
||||
void prepareGraphIOData(AnimGraph& instance) const;
|
||||
void connectRuntimeNodes(AnimGraph& instance) const;
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
||||
|
||||
Reference in New Issue
Block a user