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.
AnimGraphEditor
Martin Felis 2022-04-01 13:19:54 +02:00
parent abddbea62b
commit 08ae84fcb4
8 changed files with 559 additions and 431 deletions

View File

@ -5,39 +5,20 @@
#include "AnimGraph.h" #include "AnimGraph.h"
void AnimGraph::updateOrderedNodes() { void AnimGraph::updateOrderedNodes() {
std::vector<int> node_index_stack;
node_index_stack.push_back(0);
m_eval_ordered_nodes.clear(); m_eval_ordered_nodes.clear();
updateOrderedNodesRecursive(0);
}
while (node_index_stack.size() > 0) { void AnimGraph::updateOrderedNodesRecursive(int node_index) {
std::vector<NodeInput>& node_inputs = AnimNode* node = m_nodes[node_index];
m_node_inputs[node_index_stack.back()]; const std::vector<AnimGraphConnection> node_input_connections =
node_index_stack.pop_back(); m_node_input_connections[node_index];
for (size_t i = 0, n = node_input_connections.size(); i < n; i++) {
for (size_t i = 0, n = node_inputs.size(); i < n; i++) { int input_node_index = getAnimNodeIndex(node_input_connections[i].m_source_node);
AnimNode* input_node = node_inputs[i].m_node; updateOrderedNodesRecursive(input_node_index);
if (input_node == nullptr) {
continue;
}
int input_node_index = input_node->m_index;
bool is_node_processed = false;
for (size_t j = 0, m = m_eval_ordered_nodes.size(); j < m; j++) {
if (m_eval_ordered_nodes[j] == input_node) {
is_node_processed = true;
break;
}
}
if (is_node_processed) {
continue;
}
m_eval_ordered_nodes.push_back(input_node);
node_index_stack.push_back(input_node_index);
}
} }
m_eval_ordered_nodes.push_back(node);
} }
void AnimGraph::markActiveNodes() { void AnimGraph::markActiveNodes() {
@ -45,10 +26,10 @@ void AnimGraph::markActiveNodes() {
m_nodes[i]->m_state = AnimNodeEvalState::Deactivated; m_nodes[i]->m_state = AnimNodeEvalState::Deactivated;
} }
const std::vector<NodeInput> graph_output_inputs = m_node_inputs[0]; const std::vector<AnimGraphConnection>& graph_output_inputs = m_node_input_connections[0];
for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) { for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) {
const NodeInput& graph_input = graph_output_inputs[i]; const AnimGraphConnection& graph_input = graph_output_inputs[i];
AnimNode* node = graph_input.m_node; AnimNode* node = graph_input.m_source_node;
if (node != nullptr) { if (node != nullptr) {
node->m_state = AnimNodeEvalState::Activated; node->m_state = AnimNodeEvalState::Activated;
} }
@ -58,19 +39,69 @@ void AnimGraph::markActiveNodes() {
AnimNode* node = m_eval_ordered_nodes[i]; AnimNode* node = m_eval_ordered_nodes[i];
if (checkIsNodeActive(node)) { if (checkIsNodeActive(node)) {
int node_index = node->m_index; int node_index = node->m_index;
node->MarkActiveInputs(m_node_inputs[node_index]); node->MarkActiveInputs(m_node_input_connections[node_index]);
// Non-animation data inputs are always active. // Non-animation data inputs are always active.
for (size_t j = 0, nj = m_node_inputs[node_index].size(); j < nj; j++) { for (size_t j = 0, nj = m_node_input_connections[node_index].size(); j < nj; j++) {
const NodeInput& input = m_node_inputs[node_index][j]; const AnimGraphConnection& input = m_node_input_connections[node_index][j];
if (input.m_node != nullptr && input.m_type != SocketType::SocketTypeAnimation) { if (input.m_source_node != nullptr
input.m_node->m_state = AnimNodeEvalState::Activated; && input.m_target_socket.m_type != SocketType::SocketTypeAnimation) {
input.m_source_node->m_state = AnimNodeEvalState::Activated;
} }
} }
} }
} }
} }
void AnimGraph::prepareNodeEval(size_t node_index) {
AnimNode* node = m_nodes[node_index];
for (size_t i = 0, n = m_node_output_connections[node_index].size(); i < n;
i++) {
AnimGraphConnection& output_connection =
m_node_output_connections[node_index][i];
if (output_connection.m_source_socket.m_type
!= SocketType::SocketTypeAnimation) {
continue;
}
(*output_connection.m_source_socket.m_value.ptr_ptr) =
m_anim_data_work_buffer.peek();
m_anim_data_work_buffer.pop();
}
for (size_t i = 0, n = m_node_input_connections[node_index].size(); i < n;
i++) {
AnimGraphConnection& input_connection =
m_node_input_connections[node_index][i];
if (input_connection.m_source_socket.m_type
!= SocketType::SocketTypeAnimation) {
continue;
}
(*input_connection.m_target_socket.m_value.ptr_ptr) =
(*input_connection.m_source_socket.m_value.ptr_ptr);
}
}
void AnimGraph::finishNodeEval(size_t node_index) {
AnimNode* node = m_nodes[node_index];
for (size_t i = 0, n = m_node_input_connections[node_index].size(); i < n;
i++) {
AnimGraphConnection& input_connection =
m_node_input_connections[node_index][i];
if (input_connection.m_source_socket.m_type
!= SocketType::SocketTypeAnimation) {
continue;
}
m_anim_data_work_buffer.push(
static_cast<AnimData*>(input_connection.m_source_socket.m_value.ptr));
(*input_connection.m_source_socket.m_value.ptr_ptr) = nullptr;
}
}
void AnimGraph::evalSyncTracks() { void AnimGraph::evalSyncTracks() {
for (size_t i = m_eval_ordered_nodes.size() - 1; i >= 0; i--) { for (size_t i = m_eval_ordered_nodes.size() - 1; i >= 0; i--) {
AnimNode* node = m_eval_ordered_nodes[i]; AnimNode* node = m_eval_ordered_nodes[i];
@ -79,12 +110,12 @@ void AnimGraph::evalSyncTracks() {
continue; continue;
} }
node->CalcSyncTrack(m_node_inputs[node_index]); node->CalcSyncTrack(m_node_input_connections[node_index]);
} }
} }
void AnimGraph::updateTime(float dt) { void AnimGraph::updateTime(float dt) {
const std::vector<NodeInput> graph_output_inputs = m_node_inputs[0]; const std::vector<AnimGraphConnection> graph_output_inputs = m_node_input_connections[0];
for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) { for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) {
AnimNode* node = m_eval_ordered_nodes[i]; AnimNode* node = m_eval_ordered_nodes[i];
if (node != nullptr) { if (node != nullptr) {
@ -99,17 +130,16 @@ void AnimGraph::updateTime(float dt) {
} }
int node_index = node->m_index; int node_index = node->m_index;
const std::vector<NodeInput> node_inputs = const std::vector<AnimGraphConnection> node_input_connections = m_node_input_connections[node_index];
m_node_inputs[node_index];
float node_time_now = node->m_time_now; float node_time_now = node->m_time_now;
float node_time_last = node->m_time_last; float node_time_last = node->m_time_last;
for (size_t i = 0, n = node_inputs.size(); i < n; i++) { for (size_t i = 0, n = node_input_connections.size(); i < n; i++) {
AnimNode* input_node = node_inputs[i].m_node; AnimNode* input_node = node_input_connections[i].m_source_node;
// Only propagate time updates via animation sockets. // Only propagate time updates via animation sockets.
if (input_node != nullptr if (input_node != nullptr
&& node_inputs[i].m_type == SocketType::SocketTypeAnimation && node_input_connections[i].m_target_socket.m_type == SocketType::SocketTypeAnimation
&& input_node->m_state == AnimNodeEvalState::Activated) { && input_node->m_state == AnimNodeEvalState::Activated) {
input_node->UpdateTime(node_time_last, node_time_now); input_node->UpdateTime(node_time_last, node_time_now);
} }
@ -118,30 +148,49 @@ void AnimGraph::updateTime(float dt) {
} }
void AnimGraph::evaluate() { void AnimGraph::evaluate() {
constexpr int eval_stack_size = 5;
int eval_stack_index = eval_stack_size;
AnimData eval_buffers[eval_stack_size];
AnimData* eval_stack[eval_stack_size];
for (size_t i = 0; i < eval_stack_size; i++) {
eval_stack[i] = &eval_buffers[i];
}
for (size_t i = m_eval_ordered_nodes.size() - 1; i >= 0; i--) { for (size_t i = m_eval_ordered_nodes.size() - 1; i >= 0; i--) {
AnimNode* node = m_eval_ordered_nodes[i]; AnimNode* node = m_eval_ordered_nodes[i];
if (node->m_state == AnimNodeEvalState::Deactivated) { if (node->m_state == AnimNodeEvalState::Deactivated) {
continue; continue;
} }
prepareNodeEval(node->m_index);
node->Evaluate(); node->Evaluate();
finishNodeEval(node->m_index);
} }
} }
void* AnimGraph::getOutput(const std::string& name) const { Socket* AnimGraph::getInputSocket(const std::string& name) {
Socket* socket = m_socket_accessor->FindInputSocket(name); Socket* socket = nullptr;
if (socket == nullptr) { for (size_t i = 0, n = m_node_output_connections[1].size(); i < n; i++) {
return nullptr; AnimGraphConnection& connection = m_node_output_connections[1][i];
if (connection.m_target_socket.m_name == name) {
return &connection.m_target_socket;
}
} }
return socket->m_value.ptr; return nullptr;
} }
void* AnimGraph::getInput(const std::string& name) const { Socket* AnimGraph::getOutputSocket(const std::string& name) {
Socket* socket = m_socket_accessor->FindOutputSocket(name); Socket* socket = nullptr;
if (socket == nullptr) { for (size_t i = 0, n = m_node_input_connections[0].size(); i < n; i++) {
return nullptr; AnimGraphConnection& connection = m_node_input_connections[0][i];
if (connection.m_target_socket.m_name == name) {
return &connection.m_target_socket;
}
} }
return *(socket->m_value.ptr_ptr); return nullptr;
} }

View File

@ -8,6 +8,34 @@
#include "AnimGraphData.h" #include "AnimGraphData.h"
#include "AnimGraphNodes.h" #include "AnimGraphNodes.h"
struct AnimDataWorkBuffer {
std::vector<AnimData> m_eval_anim_data;
std::vector<AnimData*> m_available_data;
AnimDataWorkBuffer(size_t stack_size) {
m_eval_anim_data.resize(stack_size);
m_available_data.resize(stack_size);
for (size_t i = 0; i < stack_size; i++) {
m_available_data[i] = &m_eval_anim_data[i];
}
}
void push(AnimData* anim_data) {
assert (m_available_data.size() < m_eval_anim_data.size());
m_available_data.push_back(anim_data);
}
void pop() {
assert (m_available_data.size() > 0);
m_available_data.pop_back();
}
AnimData* peek() {
return m_available_data.back();
}
};
// //
// AnimGraph (Runtime) // AnimGraph (Runtime)
// //
@ -16,7 +44,8 @@ struct AnimGraph {
std::vector<AnimNode*> m_nodes; std::vector<AnimNode*> m_nodes;
std::vector<AnimNode*> m_eval_ordered_nodes; std::vector<AnimNode*> m_eval_ordered_nodes;
std::vector<std::vector<NodeInput> > m_node_inputs; std::vector<std::vector<AnimGraphConnection> > m_node_input_connections;
std::vector<std::vector<AnimGraphConnection> > m_node_output_connections;
NodeSocketAccessorBase* m_socket_accessor; NodeSocketAccessorBase* m_socket_accessor;
char* m_input_buffer = nullptr; char* m_input_buffer = nullptr;
char* m_output_buffer = nullptr; char* m_output_buffer = nullptr;
@ -24,6 +53,8 @@ struct AnimGraph {
std::vector<Socket>& getGraphOutputs() { return m_socket_accessor->m_inputs; } std::vector<Socket>& getGraphOutputs() { return m_socket_accessor->m_inputs; }
std::vector<Socket>& getGraphInputs() { return m_socket_accessor->m_outputs; } std::vector<Socket>& getGraphInputs() { return m_socket_accessor->m_outputs; }
AnimDataWorkBuffer m_anim_data_work_buffer = AnimDataWorkBuffer(5);
~AnimGraph() { ~AnimGraph() {
delete[] m_input_buffer; delete[] m_input_buffer;
delete[] m_output_buffer; delete[] m_output_buffer;
@ -36,10 +67,16 @@ struct AnimGraph {
} }
void updateOrderedNodes(); void updateOrderedNodes();
void updateOrderedNodesRecursive(int node_index);
void markActiveNodes(); void markActiveNodes();
bool checkIsNodeActive(AnimNode* node) { bool checkIsNodeActive(AnimNode* node) {
return node->m_state != AnimNodeEvalState::Deactivated; return node->m_state != AnimNodeEvalState::Deactivated;
} }
void prepareNodeEval(size_t node_index);
void finishNodeEval(size_t node_index);
void evalSyncTracks(); void evalSyncTracks();
void updateTime(float dt); void updateTime(float dt);
void evaluate(); void evaluate();
@ -51,8 +88,8 @@ struct AnimGraph {
} }
} }
void* getOutput(const std::string& name) const; Socket* getInputSocket(const std::string& name);
void* getInput(const std::string& name) const; Socket* getOutputSocket(const std::string& name);
int getNodeEvalOrderIndex(const AnimNode* node) { int getNodeEvalOrderIndex(const AnimNode* node) {
for (size_t i = 0, n = m_eval_ordered_nodes.size(); i < n; i++) { for (size_t i = 0, n = m_eval_ordered_nodes.size(); i < n; i++) {
@ -63,16 +100,15 @@ struct AnimGraph {
return -1; return -1;
} }
AnimNode* getAnimNodeForInput( const AnimNode* getAnimNodeForInput (
size_t node_index, size_t node_index,
const std::string& input_name) { const std::string& input_name) const {
assert(node_index < m_nodes.size()); assert(node_index < m_nodes.size());
assert(node_index < m_node_inputs.size());
std::vector<NodeInput>& node_inputs = m_node_inputs[node_index]; const std::vector<AnimGraphConnection>& input_connection = m_node_input_connections[node_index];
for (size_t i = 0, n = node_inputs.size(); i < n; i++) { for (size_t i = 0, n = input_connection.size(); i < n; i++) {
if (node_inputs[i].m_input_name == input_name) { if (input_connection[i].m_target_socket.m_name == input_name) {
return node_inputs[i].m_node; return input_connection[i].m_source_node;
} }
} }
@ -88,6 +124,16 @@ struct AnimGraph {
return nullptr; return nullptr;
} }
size_t getAnimNodeIndex (AnimNode* node) {
for (size_t i = 0; i < m_nodes.size(); i++) {
if (m_nodes[i] == node) {
return i;
}
}
return -1;
}
}; };
#endif //ANIMTESTBED_ANIMGRAPH_H #endif //ANIMTESTBED_ANIMGRAPH_H

View File

@ -204,7 +204,7 @@ struct NodeSocketAccessorBase {
} }
template <typename T> template <typename T>
bool RegisterInput(const std::string& name, T* value, int flags = 0) { bool RegisterInput(const std::string& name, T** value, int flags = 0) {
return RegisterSocket(m_inputs, name, value, flags); return RegisterSocket(m_inputs, name, value, flags);
} }
template <typename T> template <typename T>
@ -221,6 +221,10 @@ struct NodeSocketAccessorBase {
return GetSocketIndex(m_inputs, name); return GetSocketIndex(m_inputs, name);
} }
template <typename T>
bool RegisterOutput(const std::string& name, T* value, int flags = 0) {
return RegisterSocket(m_outputs, name, value, flags);
}
template <typename T> template <typename T>
bool RegisterOutput(const std::string& name, T** value, int flags = 0) { bool RegisterOutput(const std::string& name, T** value, int flags = 0) {
return RegisterSocket(m_outputs, name, value, flags); return RegisterSocket(m_outputs, name, value, flags);

View File

@ -44,17 +44,20 @@ void RemoveConnectionsForSocket(
AnimGraphResource& graph_resource, AnimGraphResource& graph_resource,
AnimNodeResource& node_resource, AnimNodeResource& node_resource,
Socket& socket) { Socket& socket) {
std::vector<AnimGraphConnection>::iterator iter = std::vector<AnimGraphConnectionResource>::iterator iter =
graph_resource.m_connections.begin(); graph_resource.m_connections.begin();
while (iter != graph_resource.m_connections.end()) { while (iter != graph_resource.m_connections.end()) {
AnimGraphConnection& connection = *iter; // TODO adjust for refactor
if (connection.m_source_node == &node_resource assert(false);
&& connection.m_source_socket == &socket) {
iter = graph_resource.m_connections.erase(iter); // AnimGraphConnectionResource& connection = *iter;
} else { // if (connection.m_source_node == &node_resource
iter++; // && connection.m_source_socket == &socket) {
} // iter = graph_resource.m_connections.erase(iter);
// } else {
// iter++;
// }
} }
} }
@ -320,23 +323,26 @@ void AnimGraphEditorUpdate() {
} }
for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) { for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) {
const AnimGraphConnection& connection = graph_resource.m_connections[i]; const AnimGraphConnectionResource& connection =
graph_resource.m_connections[i];
int start_attr, end_attr; int start_attr, end_attr;
int source_node_index = const AnimNodeResource& source_node =
graph_resource.getNodeIndex(*connection.m_source_node); graph_resource.m_nodes[connection.source_node_index];
int source_socket_index = int source_socket_index = source_node.m_socket_accessor->GetOutputIndex(
connection.m_source_node->m_socket_accessor->GetOutputIndex( connection.source_socket_name);
connection.m_source_socket->m_name);
start_attr =
GenerateOutputAttributeId(source_node_index, source_socket_index);
int target_node_index = const AnimNodeResource& target_node =
graph_resource.getNodeIndex(*connection.m_target_node); graph_resource.m_nodes[connection.target_node_index];
int target_socket_index = int target_socket_index = target_node.m_socket_accessor->GetInputIndex(
connection.m_target_node->m_socket_accessor->GetInputIndex( connection.target_socket_name);
connection.m_target_socket->m_name);
end_attr = GenerateInputAttributeId(target_node_index, target_socket_index); start_attr = GenerateOutputAttributeId(
connection.source_node_index,
source_socket_index);
end_attr = GenerateInputAttributeId(
connection.target_node_index,
target_socket_index);
ImNodes::Link(i, start_attr, end_attr); ImNodes::Link(i, start_attr, end_attr);
} }
@ -357,12 +363,17 @@ void AnimGraphEditorUpdate() {
int node_end_input_index; int node_end_input_index;
SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index); SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index);
AnimGraphConnection connection; AnimGraphConnectionResource connection;
connection.m_source_node = &graph_resource.m_nodes[node_start_id]; connection.source_node_index = node_start_id;
connection.m_source_socket = &connection.m_source_node->m_socket_accessor->m_outputs[node_start_output_index]; const AnimNodeResource& source_node = graph_resource.m_nodes[node_start_id];
connection.source_socket_name =
source_node.m_socket_accessor->m_outputs[node_start_output_index]
.m_name;
connection.m_target_node = &graph_resource.m_nodes[node_end_id]; connection.target_node_index = node_end_id;
connection.m_target_socket = &connection.m_target_node->m_socket_accessor->m_inputs[node_end_input_index]; const AnimNodeResource& target_node = graph_resource.m_nodes[node_end_id];
connection.target_socket_name =
target_node.m_socket_accessor->m_inputs[node_end_input_index].m_name;
graph_resource.m_connections.push_back(connection); graph_resource.m_connections.push_back(connection);
} }

View File

@ -15,6 +15,7 @@ struct AnimNode;
struct NodeInput { struct NodeInput {
AnimNode* m_node; AnimNode* m_node;
SocketType m_type = SocketType::SocketTypeUndefined; SocketType m_type = SocketType::SocketTypeUndefined;
Socket* m_node_output_socket;
std::string m_input_name; std::string m_input_name;
}; };
@ -27,6 +28,13 @@ enum class AnimNodeEvalState {
Evaluated Evaluated
}; };
struct AnimGraphConnection {
AnimNode* m_source_node = nullptr;
Socket m_source_socket;
AnimNode* m_target_node = nullptr;
Socket m_target_socket;
};
struct AnimNode { struct AnimNode {
std::string m_name; std::string m_name;
std::string m_node_type_name; std::string m_node_type_name;
@ -38,20 +46,20 @@ struct AnimNode {
virtual ~AnimNode(){}; virtual ~AnimNode(){};
virtual void MarkActiveInputs(const std::vector<NodeInput>& inputs) { virtual void MarkActiveInputs(const std::vector<AnimGraphConnection>& inputs) {
for (size_t i = 0, n = inputs.size(); i < n; i++) { for (size_t i = 0, n = inputs.size(); i < n; i++) {
AnimNode* input_node = inputs[i].m_node; AnimNode* input_node = inputs[i].m_source_node;
if (input_node != nullptr) { if (input_node != nullptr) {
input_node->m_state = AnimNodeEvalState::Activated; input_node->m_state = AnimNodeEvalState::Activated;
} }
} }
} }
virtual void CalcSyncTrack(const std::vector<NodeInput>& inputs) { virtual void CalcSyncTrack(const std::vector<AnimGraphConnection>& inputs) {
for (size_t i = 0, n = inputs.size(); i < n; i++) { for (size_t i = 0, n = inputs.size(); i < n; i++) {
AnimNode* input_node = inputs[i].m_node; AnimNode* input_node = inputs[i].m_source_node;
if (input_node != nullptr if (input_node != nullptr
&& inputs[i].m_type == SocketType::SocketTypeAnimation && inputs[i].m_source_socket.m_type == SocketType::SocketTypeAnimation
&& input_node->m_state != AnimNodeEvalState::Deactivated) { && input_node->m_state != AnimNodeEvalState::Deactivated) {
m_sync_track = input_node->m_sync_track; m_sync_track = input_node->m_sync_track;
return; return;
@ -83,25 +91,25 @@ struct NodeSocketAccessor<BlendTreeNode> : public NodeSocketAccessorBase {
// Blend2Node // Blend2Node
// //
struct Blend2Node : public AnimNode { struct Blend2Node : public AnimNode {
AnimData m_input0; AnimData* i_input0 = nullptr;
AnimData m_input1; AnimData* i_input1 = nullptr;
AnimData* m_output = nullptr; AnimData* o_output = nullptr;
float m_blend_weight = 0.f; float* i_blend_weight = nullptr;
bool m_sync_blend = false; bool m_sync_blend = false;
virtual void MarkActiveInputs(const std::vector<NodeInput>& inputs) override { virtual void MarkActiveInputs(const std::vector<AnimGraphConnection>& inputs) override {
for (size_t i = 0, n = inputs.size(); i < n; i++) { for (size_t i = 0, n = inputs.size(); i < n; i++) {
AnimNode* input_node = inputs[i].m_node; AnimNode* input_node = inputs[i].m_source_node;
if (input_node == nullptr) { if (input_node == nullptr) {
continue; continue;
} }
if (inputs[i].m_input_name == "Input0" && m_blend_weight < 0.999) { if (inputs[i].m_target_socket.m_name == "Input0" && *i_blend_weight < 0.999) {
input_node->m_state = AnimNodeEvalState::Activated; input_node->m_state = AnimNodeEvalState::Activated;
continue; continue;
} }
if (inputs[i].m_input_name == "Input1" && m_blend_weight > 0.001) { if (inputs[i].m_target_socket.m_name == "Input1" && *i_blend_weight > 0.001) {
input_node->m_state = AnimNodeEvalState::Activated; input_node->m_state = AnimNodeEvalState::Activated;
continue; continue;
} }
@ -134,14 +142,14 @@ template <>
struct NodeSocketAccessor<Blend2Node> : public NodeSocketAccessorBase { struct NodeSocketAccessor<Blend2Node> : public NodeSocketAccessorBase {
NodeSocketAccessor(AnimNode* node_) { NodeSocketAccessor(AnimNode* node_) {
Blend2Node* node = dynamic_cast<Blend2Node*>(node_); Blend2Node* node = dynamic_cast<Blend2Node*>(node_);
RegisterInput("Input0", &node->m_input0); RegisterInput("Input0", &node->i_input0);
RegisterInput("Input1", &node->m_input1); RegisterInput("Input1", &node->i_input1);
RegisterInput( RegisterInput(
"Weight", "Weight",
&node->m_blend_weight, &node->i_blend_weight,
SocketFlags::SocketFlagAffectsTime); SocketFlags::SocketFlagAffectsTime);
RegisterOutput("Output", &node->m_output); RegisterOutput("Output", &node->o_output);
RegisterProperty("Sync", &node->m_sync_blend); RegisterProperty("Sync", &node->m_sync_blend);
} }
@ -162,13 +170,13 @@ struct NodeSocketAccessor<Blend2Node> : public NodeSocketAccessorBase {
// SpeedScaleNode // SpeedScaleNode
// //
struct SpeedScaleNode : public AnimNode { struct SpeedScaleNode : public AnimNode {
AnimData m_input; AnimData* i_input = nullptr;
AnimData* m_output = nullptr; AnimData* i_output = nullptr;
float m_speed_scale = 0.f; float* i_speed_scale = nullptr;
virtual void UpdateTime(float time_last, float time_now) { virtual void UpdateTime(float time_last, float time_now) {
m_time_last = time_last; m_time_last = time_last;
m_time_now = time_last + (time_now - time_last) * m_speed_scale; m_time_now = time_last + (time_now - time_last) * (*i_speed_scale);
m_state = AnimNodeEvalState::TimeUpdated; m_state = AnimNodeEvalState::TimeUpdated;
} }
}; };
@ -179,11 +187,11 @@ struct NodeSocketAccessor<SpeedScaleNode> : public NodeSocketAccessorBase {
SpeedScaleNode* node = dynamic_cast<SpeedScaleNode*>(node_); SpeedScaleNode* node = dynamic_cast<SpeedScaleNode*>(node_);
RegisterInput( RegisterInput(
"SpeedScale", "SpeedScale",
&node->m_speed_scale, &node->i_speed_scale,
SocketFlags::SocketFlagAffectsTime); SocketFlags::SocketFlagAffectsTime);
RegisterInput("Input", &node->m_input); RegisterInput("Input", &node->i_input);
RegisterOutput("Output", &node->m_output); RegisterOutput("Output", &node->i_output);
} }
}; };
@ -191,7 +199,7 @@ struct NodeSocketAccessor<SpeedScaleNode> : public NodeSocketAccessorBase {
// AnimSamplerNode // AnimSamplerNode
// //
struct AnimSamplerNode : public AnimNode { struct AnimSamplerNode : public AnimNode {
AnimData* m_output = nullptr; AnimData* o_output = nullptr;
std::string m_filename; std::string m_filename;
}; };
@ -199,12 +207,38 @@ template <>
struct NodeSocketAccessor<AnimSamplerNode> : public NodeSocketAccessorBase { struct NodeSocketAccessor<AnimSamplerNode> : public NodeSocketAccessorBase {
NodeSocketAccessor(AnimNode* node_) { NodeSocketAccessor(AnimNode* node_) {
AnimSamplerNode* node = dynamic_cast<AnimSamplerNode*>(node_); AnimSamplerNode* node = dynamic_cast<AnimSamplerNode*>(node_);
RegisterOutput("Output", &node->m_output); RegisterOutput("Output", &node->o_output);
RegisterProperty("Filename", &node->m_filename); RegisterProperty("Filename", &node->m_filename);
} }
}; };
//
// MathAddNode
//
struct MathAddNode : public AnimNode {
float* i_input0 = nullptr;
float* i_input1 = nullptr;
float o_output = 0.f;
void Evaluate() override {
assert (i_input0 != nullptr);
assert (i_input1 != nullptr);
o_output = *i_input0 + *i_input1;
}
};
template <>
struct NodeSocketAccessor<MathAddNode> : public NodeSocketAccessorBase {
NodeSocketAccessor(AnimNode* node_) {
MathAddNode* node = dynamic_cast<MathAddNode*>(node_);
RegisterInput("Input0", &node->i_input0);
RegisterInput("Input1", &node->i_input1);
RegisterOutput("Output", &node->o_output);
}
};
static inline AnimNode* AnimNodeFactory(const std::string& name) { static inline AnimNode* AnimNodeFactory(const std::string& name) {
AnimNode* result; AnimNode* result;

View File

@ -169,38 +169,34 @@ AnimNodeResource sAnimGraphNodeFromJson(const json& json_node) {
} }
// //
// AnimGraphConnection <-> Json // AnimGraphConnectionResource <-> Json
// //
json sAnimGraphConnectionToJson(const AnimGraphResource& graph_resource, const AnimGraphConnection& connection) { json sAnimGraphConnectionToJson(
const AnimGraphResource& graph_resource,
const AnimGraphConnectionResource& connection) {
json result; json result;
result["type"] = "AnimGraphConnection"; result["type"] = "AnimGraphConnectionResource";
const AnimNodeResource* source_node = connection.m_source_node; result["source_node_index"] = connection.source_node_index;
result["source_node_index"] = graph_resource.getNodeIndex(*source_node); result["source_socket_name"] = connection.source_socket_name;
result["source_socket_index"] = source_node->m_socket_accessor->GetOutputIndex(connection.m_source_socket->m_name);
const AnimNodeResource* target_node = connection.m_source_node; result["target_node_index"] = connection.target_node_index;
result["source_node_index"] = graph_resource.getNodeIndex(*target_node); result["target_socket_name"] = connection.target_socket_name;
result["source_socket_index"] = target_node->m_socket_accessor->GetInputIndex(connection.m_target_node->m_name);
return result; return result;
} }
AnimGraphConnection sAnimGraphConnectionFromJson(const AnimGraphResource& graph_resource, const json& json_node) { AnimGraphConnectionResource sAnimGraphConnectionFromJson(
AnimGraphConnection connection; const AnimGraphResource& graph_resource,
const json& json_node) {
AnimGraphConnectionResource connection;
int source_node_index = json_node["source_node_index"]; connection.source_node_index = json_node["source_node_index"];
connection.m_source_node = &graph_resource.m_nodes[source_node_index]; connection.source_socket_name = json_node["source_socket_name"];
int source_socket_index = json_node["source_socket_index"];
connection.m_source_socket = &connection.m_source_node->m_socket_accessor->m_outputs[source_socket_index];
connection.target_node_index = json_node["target_node_index"];
int target_node_index = json_node["target_node_index"]; connection.target_socket_name = json_node["target_socket_name"];
connection.m_target_node = &graph_resource.m_nodes[target_node_index];
int target_socket_index = json_node["target_socket_index"];
connection.m_target_socket = &connection.m_target_node->m_socket_accessor->m_outputs[target_socket_index];
return connection; return connection;
} }
@ -243,7 +239,7 @@ bool AnimGraphResource::saveToFile(const char* filename) const {
} }
for (size_t i = 0; i < m_connections.size(); i++) { for (size_t i = 0; i < m_connections.size(); i++) {
const AnimGraphConnection& connection = m_connections[i]; const AnimGraphConnectionResource& connection = m_connections[i];
result["connections"][i] = sAnimGraphConnectionToJson(*this, connection); result["connections"][i] = sAnimGraphConnectionToJson(*this, connection);
} }
@ -327,17 +323,196 @@ bool AnimGraphResource::loadFromFile(const char* filename) {
// Load connections // Load connections
for (size_t i = 0; i < json_data["connections"].size(); i++) { for (size_t i = 0; i < json_data["connections"].size(); i++) {
const json& json_connection = json_data["connections"][i]; const json& json_connection = json_data["connections"][i];
if (json_connection["type"] != "AnimGraphConnection") { if (json_connection["type"] != "AnimGraphConnectionResource") {
std::cerr << "Invalid json object. Expected type 'AnimGraphConnection' " std::cerr
"but got '" << "Invalid json object. Expected type 'AnimGraphConnectionResource' "
<< json_connection["type"] << "'." << std::endl; "but got '"
<< json_connection["type"] << "'." << std::endl;
return false; return false;
} }
AnimGraphConnection connection = AnimGraphConnectionResource connection =
sAnimGraphConnectionFromJson(*this, json_connection); sAnimGraphConnectionFromJson(*this, json_connection);
m_connections.push_back(connection); m_connections.push_back(connection);
} }
return true; return true;
} }
AnimGraph AnimGraphResource::createInstance() const {
AnimGraph result;
createRuntimeNodeInstances(result);
prepareGraphIOData(result);
connectRuntimeNodes(result);
result.updateOrderedNodes();
result.reset();
return result;
}
void AnimGraphResource::createRuntimeNodeInstances(AnimGraph& instance) const {
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;
instance.m_nodes.push_back(node);
// runtime node connections
instance.m_node_input_connections.push_back(
std::vector<AnimGraphConnection>());
instance.m_node_output_connections.push_back(
std::vector<AnimGraphConnection>());
}
}
void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
instance.m_socket_accessor =
AnimNodeAccessorFactory("BlendTree", instance.m_nodes[0]);
instance.m_socket_accessor->m_outputs =
m_nodes[1].m_socket_accessor->m_outputs;
instance.m_socket_accessor->m_inputs = m_nodes[0].m_socket_accessor->m_inputs;
// inputs
int input_block_size = 0;
std::vector<Socket>& graph_inputs = instance.getGraphInputs();
for (int i = 0; i < graph_inputs.size(); i++) {
input_block_size += sizeof(void*);
}
instance.m_input_buffer = new char[input_block_size];
memset(instance.m_input_buffer, 0, input_block_size);
int input_block_offset = 0;
for (int i = 0; i < graph_inputs.size(); i++) {
graph_inputs[i].m_value.ptr =
(void*)&instance.m_input_buffer[input_block_offset];
input_block_offset += sizeof(void*);
}
// outputs
int output_block_size = 0;
std::vector<Socket>& graph_outputs = instance.getGraphOutputs();
for (int i = 0; i < graph_outputs.size(); i++) {
output_block_size += graph_outputs[i].m_type_size;
}
instance.m_output_buffer = new char[output_block_size];
memset(instance.m_output_buffer, 0, output_block_size);
int output_block_offset = 0;
for (int i = 0; i < graph_outputs.size(); i++) {
graph_outputs[i].m_value.ptr =
(void*)&instance.m_output_buffer[output_block_offset];
output_block_offset += graph_outputs[i].m_type_size;
}
}
void AnimGraphResource::connectRuntimeNodes(AnimGraph& instance) const {
for (int i = 0; i < m_connections.size(); i++) {
const AnimGraphConnectionResource& connection = m_connections[i];
std::string source_node_type = "";
std::string target_node_type = "";
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.source_node_index < 0
|| connection.source_node_index >= m_nodes.size()) {
std::cerr << "Could not find source node index." << std::endl;
continue;
}
source_node = instance.m_nodes[connection.source_node_index];
source_node_type = source_node->m_node_type_name;
if (connection.source_node_index == 1) {
source_node_accessor = instance.m_socket_accessor;
} else {
source_node_accessor =
AnimNodeAccessorFactory(source_node_type, source_node);
}
if (connection.target_node_index < 0
|| connection.target_node_index >= m_nodes.size()) {
std::cerr << "Could not find source node index." << std::endl;
continue;
}
target_node = instance.m_nodes[connection.target_node_index];
target_node_type = target_node->m_node_type_name;
if (connection.target_node_index == 0) {
target_node_accessor = instance.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
//
source_socket_index =
source_node_accessor->GetOutputIndex(connection.source_socket_name);
if (source_socket_index == -1) {
std::cerr << "Invalid source socket " << connection.source_socket_name
<< " for node " << source_node->m_name << "." << std::endl;
continue;
}
Socket* source_socket =
&source_node_accessor->m_outputs[source_socket_index];
target_socket_index =
target_node_accessor->GetInputIndex(connection.target_socket_name);
if (target_socket_index == -1) {
std::cerr << "Invalid target socket " << connection.target_socket_name
<< " for node " << target_node->m_name << "." << std::endl;
continue;
}
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.
//
(*target_socket->m_value.ptr_ptr) = source_socket->m_value.ptr;
// (*source_socket->m_value.ptr_ptr) = target_socket->m_value.ptr;
size_t target_node_index = target_node->m_index;
// Register the runtime connection
AnimGraphConnection runtime_connection = {
source_node,
*source_socket,
target_node,
*target_socket};
std::vector<AnimGraphConnection>& target_input_connections =
instance.m_node_input_connections[target_node_index];
target_input_connections.push_back(runtime_connection);
std::vector<AnimGraphConnection>& source_output_connections =
instance.m_node_output_connections[source_node->m_index];
source_output_connections.push_back(runtime_connection);
if (target_node_accessor != instance.m_socket_accessor) {
delete target_node_accessor;
}
if (source_node_accessor != instance.m_socket_accessor) {
delete source_node_accessor;
}
}
}

View File

@ -12,10 +12,10 @@
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include "SyncTrack.h" #include "AnimGraph.h"
#include "AnimGraphData.h" #include "AnimGraphData.h"
#include "AnimGraphNodes.h" #include "AnimGraphNodes.h"
#include "AnimGraph.h" #include "SyncTrack.h"
struct AnimNode; struct AnimNode;
struct NodeSocketAccessorBase; struct NodeSocketAccessorBase;
@ -41,17 +41,17 @@ static inline AnimNodeResource AnimNodeResourceFactory(
// //
// AnimGraphResource // AnimGraphResource
// //
struct AnimGraphConnection { struct AnimGraphConnectionResource {
const AnimNodeResource* m_source_node = nullptr; size_t source_node_index = -1;
const Socket* m_source_socket = nullptr; std::string source_socket_name = "";
const AnimNodeResource* m_target_node = nullptr; size_t target_node_index = -1;
const Socket* m_target_socket = nullptr; std::string target_socket_name = "";
}; };
struct AnimGraphResource { struct AnimGraphResource {
std::string m_name; std::string m_name;
std::vector<AnimNodeResource> m_nodes; std::vector<AnimNodeResource> m_nodes;
std::vector<AnimGraphConnection> m_connections; std::vector<AnimGraphConnectionResource> m_connections;
~AnimGraphResource() { ~AnimGraphResource() {
for (size_t i = 0, n = m_nodes.size(); i < n; i++) { 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 std::string& source_socket_name,
const AnimNodeResource& target_node, const AnimNodeResource& target_node,
const std::string& target_socket_name) { const std::string& target_socket_name) {
size_t source_index = -1; size_t source_node_index = getNodeIndex(source_node);
size_t target_index = -1; size_t target_node_index = getNodeIndex(target_node);
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
if (&source_node == &m_nodes[i]) {
source_index = i;
}
if (&target_node == &m_nodes[i]) { if (source_node_index >= m_nodes.size()
target_index = i; || target_node_index >= m_nodes.size()) {
}
if (source_index < m_nodes.size() && target_index < m_nodes.size()) {
break;
}
}
if (source_index >= m_nodes.size() || target_index >= m_nodes.size()) {
std::cerr << "Cannot connect nodes: could not find nodes." << std::endl; std::cerr << "Cannot connect nodes: could not find nodes." << std::endl;
return false; return false;
} }
@ -125,216 +113,20 @@ struct AnimGraphResource {
return false; return false;
} }
AnimGraphConnection connection; AnimGraphConnectionResource connection;
connection.m_source_node = &source_node; connection.source_node_index = source_node_index;
connection.m_source_socket = source_socket; connection.source_socket_name = source_socket_name;
connection.m_target_node = &target_node; connection.target_node_index = target_node_index;
connection.m_target_socket = target_socket; connection.target_socket_name = target_socket_name;
m_connections.push_back(connection); m_connections.push_back(connection);
return true; return true;
} }
AnimGraph createInstance() const;
AnimGraph createInstance() { void createRuntimeNodeInstances(AnimGraph& instance) const;
AnimGraph result; void prepareGraphIOData(AnimGraph& instance) const;
void connectRuntimeNodes(AnimGraph& instance) const;
// 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;
}
}; };
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H #endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H

View File

@ -25,9 +25,9 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index]; AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index];
walk_node.m_name = "WalkAnim"; walk_node.m_name = "WalkAnim";
AnimNodeResource& run_node = graph_resource.m_nodes[run_node_index]; AnimNodeResource& run_node = graph_resource.m_nodes[run_node_index];
walk_node.m_name = "RunAnim"; run_node.m_name = "RunAnim";
AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index]; AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index];
walk_node.m_name = "BlendWalkRun"; blend_node.m_name = "BlendWalkRun";
AnimNodeResource& graph_node = graph_resource.m_nodes[0]; AnimNodeResource& graph_node = graph_resource.m_nodes[0];
graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr); graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
@ -36,31 +36,25 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input0") == 0); REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input0") == 0);
REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input1") == 1); REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input1") == 1);
AnimGraphConnection walk_to_blend; AnimGraphConnectionResource walk_to_blend;
walk_to_blend.m_source_node = &walk_node; walk_to_blend.source_node_index = walk_node_index;
walk_to_blend.m_source_socket = walk_to_blend.source_socket_name = "Output";
walk_node.m_socket_accessor->FindOutputSocket("Output"); walk_to_blend.target_node_index = blend_node_index;
walk_to_blend.m_target_node = &blend_node; walk_to_blend.target_socket_name = "Input0";
walk_to_blend.m_target_socket =
blend_node.m_socket_accessor->FindInputSocket("Input0");
graph_resource.m_connections.push_back(walk_to_blend); graph_resource.m_connections.push_back(walk_to_blend);
AnimGraphConnection run_to_blend; AnimGraphConnectionResource run_to_blend;
run_to_blend.m_source_node = &run_node; run_to_blend.source_node_index = run_node_index;
run_to_blend.m_source_socket = run_to_blend.source_socket_name = "Output";
run_node.m_socket_accessor->FindOutputSocket("Output"); run_to_blend.target_node_index = blend_node_index;
run_to_blend.m_target_node = &blend_node; run_to_blend.target_socket_name = "Input1";
run_to_blend.m_target_socket =
blend_node.m_socket_accessor->FindInputSocket("Input1");
graph_resource.m_connections.push_back(run_to_blend); graph_resource.m_connections.push_back(run_to_blend);
AnimGraphConnection blend_to_output; AnimGraphConnectionResource blend_to_output;
blend_to_output.m_source_node = &blend_node; blend_to_output.source_node_index = blend_node_index;
blend_to_output.m_source_socket = blend_to_output.source_socket_name = "Output";
blend_node.m_socket_accessor->FindOutputSocket("Output"); blend_to_output.target_node_index = 0;
blend_to_output.m_target_node = &graph_resource.m_nodes[0]; blend_to_output.target_socket_name = "GraphOutput";
blend_to_output.m_target_socket =
graph_node.m_socket_accessor->FindInputSocket("GraphOutput");
graph_resource.m_connections.push_back(blend_to_output); graph_resource.m_connections.push_back(blend_to_output);
graph_resource.saveToFile("WalkGraph.animgraph.json"); graph_resource.saveToFile("WalkGraph.animgraph.json");
@ -75,34 +69,55 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
REQUIRE(graph.m_nodes[4]->m_node_type_name == "Blend2"); REQUIRE(graph.m_nodes[4]->m_node_type_name == "Blend2");
// connections within the graph // connections within the graph
AnimSamplerNode* anim_sampler_instance0 = AnimSamplerNode* anim_sampler_walk =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[2]); dynamic_cast<AnimSamplerNode*>(graph.m_nodes[2]);
AnimSamplerNode* anim_sampler_instance1 = AnimSamplerNode* anim_sampler_run =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[3]); dynamic_cast<AnimSamplerNode*>(graph.m_nodes[3]);
Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(graph.m_nodes[4]); Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(graph.m_nodes[4]);
CHECK(anim_sampler_instance0->m_output == &blend2_instance->m_input0);
CHECK(anim_sampler_instance1->m_output == &blend2_instance->m_input1);
// connections from graph to the graph node
CHECK(graph.m_socket_accessor->m_inputs.size() == 1);
CHECK(graph.m_socket_accessor->FindInputSocket("GraphOutput"));
CHECK(
reinterpret_cast<char*>(blend2_instance->m_output)
== graph.getOutput("GraphOutput"));
// check node input dependencies // check node input dependencies
size_t anim_sampler_index0 = anim_sampler_instance0->m_index; size_t anim_sampler_index0 = anim_sampler_walk->m_index;
size_t anim_sampler_index1 = anim_sampler_instance1->m_index; size_t anim_sampler_index1 = anim_sampler_run->m_index;
size_t blend_index = blend2_instance->m_index; size_t blend_index = blend2_instance->m_index;
CHECK(graph.m_node_inputs[anim_sampler_index0].size() == 0); REQUIRE(graph.m_node_input_connections[blend_index].size() == 2);
CHECK(graph.m_node_inputs[anim_sampler_index1].size() == 0); CHECK(graph.m_node_input_connections[blend_index][0].m_source_node == anim_sampler_walk);
CHECK(graph.m_node_inputs[blend_index].size() == 3); CHECK(graph.m_node_input_connections[blend_index][1].m_source_node == anim_sampler_run);
CHECK(graph.m_node_inputs[blend_index][0].m_node == anim_sampler_instance0);
CHECK(graph.m_node_inputs[blend_index][1].m_node == anim_sampler_instance1); REQUIRE(graph.m_node_output_connections[anim_sampler_index0].size() == 1);
CHECK(graph.m_node_inputs[blend_index][2].m_node == nullptr); CHECK(graph.m_node_output_connections[anim_sampler_index0][0].m_target_node == blend2_instance);
REQUIRE(graph.m_node_output_connections[anim_sampler_index1].size() == 1);
CHECK(graph.m_node_output_connections[anim_sampler_index1][0].m_target_node == blend2_instance);
// Emulate evaluation
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 5);
graph.prepareNodeEval(walk_node_index);
graph.finishNodeEval(walk_node_index);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 4);
graph.prepareNodeEval(run_node_index);
graph.finishNodeEval(run_node_index);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 3);
graph.prepareNodeEval(blend_node_index);
CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output);
CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 2);
graph.finishNodeEval(blend_node_index);
CHECK(anim_sampler_walk->o_output == nullptr);
CHECK(anim_sampler_run->o_output == nullptr);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 4);
graph.prepareNodeEval(0);
Socket* graph_output_socket = graph.getOutputSocket("GraphOutput");
CHECK(blend2_instance->o_output == (*graph_output_socket->m_value.ptr_ptr));
AnimData* graph_output = static_cast<AnimData*>(*graph_output_socket->m_value.ptr_ptr);
graph.finishNodeEval(0);
CHECK(blend2_instance->o_output == nullptr);
CHECK(graph_output == (*graph_output_socket->m_value.ptr_ptr));
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 5);
} }
/*
TEST_CASE("InputAttributeConversion", "[AnimGraphResource]") { TEST_CASE("InputAttributeConversion", "[AnimGraphResource]") {
int node_id = 3321; int node_id = 3321;
int input_index = 221; int input_index = 221;
@ -299,9 +314,9 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
REQUIRE( REQUIRE(
*anim_graph.m_socket_accessor->m_outputs[0].m_value.ptr_ptr *anim_graph.m_socket_accessor->m_outputs[0].m_value.ptr_ptr
== &blend2_node->m_blend_weight); == blend2_node->i_blend_weight);
float* float_input_ptr = (float*)anim_graph.getInput("GraphFloatInput"); float* float_input_ptr = (float*)anim_graph.getInput("GraphFloatInput");
REQUIRE(float_input_ptr == &blend2_node->m_blend_weight); REQUIRE(float_input_ptr == blend2_node->i_blend_weight);
} }
WHEN( WHEN(
@ -335,21 +350,21 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
AnimData* graph_input0 = AnimData* graph_input0 =
(AnimData*)anim_graph.getInput("GraphAnimInput0"); (AnimData*)anim_graph.getInput("GraphAnimInput0");
REQUIRE(graph_input0 == &blend2_node->m_input0); REQUIRE(graph_input0 == blend2_node->i_input0);
REQUIRE( REQUIRE(
anim_graph.m_nodes[1] anim_graph.m_nodes[1]
== anim_graph.getAnimNodeForInput(blend2_node_index, "Input0")); == anim_graph.getAnimNodeForInput(blend2_node_index, "Input0"));
AnimData* graph_input1 = AnimData* graph_input1 =
(AnimData*)anim_graph.getInput("GraphAnimInput1"); (AnimData*)anim_graph.getInput("GraphAnimInput1");
REQUIRE(graph_input1 == &blend2_node->m_input1); REQUIRE(graph_input1 == blend2_node->i_input1);
REQUIRE( REQUIRE(
anim_graph.m_nodes[1] anim_graph.m_nodes[1]
== anim_graph.getAnimNodeForInput(blend2_node_index, "Input1")); == anim_graph.getAnimNodeForInput(blend2_node_index, "Input1"));
AnimData* graph_output = AnimData* graph_output =
(AnimData*)anim_graph.getOutput("GraphAnimOutput"); (AnimData*)anim_graph.getOutput("GraphAnimOutput");
REQUIRE(graph_output == blend2_node->m_output); REQUIRE(graph_output == blend2_node->o_output);
REQUIRE( REQUIRE(
anim_graph.m_nodes[blend2_node_index] anim_graph.m_nodes[blend2_node_index]
== anim_graph.getAnimNodeForInput(0, "GraphAnimOutput")); == anim_graph.getAnimNodeForInput(0, "GraphAnimOutput"));
@ -422,7 +437,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
// //
AnimData* graph_input0 = AnimData* graph_input0 =
(AnimData*)anim_graph.getInput("GraphAnimInput0"); (AnimData*)anim_graph.getInput("GraphAnimInput0");
REQUIRE(graph_input0 == &blend2_node->m_input0); REQUIRE(graph_input0 == blend2_node->i_input0);
REQUIRE( REQUIRE(
anim_graph.m_nodes[1] anim_graph.m_nodes[1]
== anim_graph.getAnimNodeForInput(blend2_node_index, "Input0")); == anim_graph.getAnimNodeForInput(blend2_node_index, "Input0"));
@ -431,19 +446,19 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
(AnimData*)anim_graph.getInput("GraphAnimInput1"); (AnimData*)anim_graph.getInput("GraphAnimInput1");
REQUIRE(graph_input1 == nullptr); REQUIRE(graph_input1 == nullptr);
REQUIRE(sampler_node->m_output == &speed_scale_node->m_input); REQUIRE(sampler_node->o_output == speed_scale_node->i_input);
REQUIRE( REQUIRE(
sampler_node sampler_node
== anim_graph.getAnimNodeForInput(speed_scale_node_index, "Input")); == anim_graph.getAnimNodeForInput(speed_scale_node_index, "Input"));
REQUIRE(speed_scale_node->m_output == &blend2_node->m_input1); REQUIRE(speed_scale_node->i_output == blend2_node->i_input1);
REQUIRE( REQUIRE(
speed_scale_node speed_scale_node
== anim_graph.getAnimNodeForInput(blend2_node_index, "Input1")); == anim_graph.getAnimNodeForInput(blend2_node_index, "Input1"));
AnimData* graph_output = AnimData* graph_output =
(AnimData*)anim_graph.getOutput("GraphAnimOutput"); (AnimData*)anim_graph.getOutput("GraphAnimOutput");
REQUIRE(graph_output == blend2_node->m_output); REQUIRE(graph_output == blend2_node->o_output);
REQUIRE( REQUIRE(
anim_graph.m_nodes[blend2_node_index] anim_graph.m_nodes[blend2_node_index]
== anim_graph.getAnimNodeForInput(0, "GraphAnimOutput")); == anim_graph.getAnimNodeForInput(0, "GraphAnimOutput"));
@ -536,3 +551,5 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
} }
} }
} }
*/