Refactored anim graph data usage and evaluation.

- Refactored NodeSocketAccessor to NodeDescriptor.
- Connections are wired up during AnimGraph instantiation.
  - Output and input sockets point to the same memory location.
  - No re-wiring needed during evaluation.
  - AnimGraph are pre-allocated (refactoring for less memory usage postponed).
- Evaluation of AnimGraph now possible from the editor.
This commit is contained in:
Martin Felis
2023-04-01 14:16:20 +02:00
parent 91607baa9d
commit 3d55b748e6
8 changed files with 312 additions and 309 deletions
+10 -12
View File
@@ -153,11 +153,11 @@ TEST_CASE_METHOD(
// Setup nodes
AnimNodeResource& trans_x_node = graph_resource.m_nodes[trans_x_node_index];
trans_x_node.m_socket_accessor->SetProperty("Filename", std::string("trans_x"));
trans_x_node.m_socket_accessor->SetPropertyValue("Filename", std::string("trans_x"));
trans_x_node.m_name = "trans_x";
AnimNodeResource& trans_y_node = graph_resource.m_nodes[trans_y_node_index];
trans_y_node.m_socket_accessor->SetProperty("Filename", std::string("trans_y"));
trans_y_node.m_socket_accessor->SetPropertyValue("Filename", std::string("trans_y"));
trans_y_node.m_name = "trans_y";
AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index];
@@ -195,17 +195,15 @@ TEST_CASE_METHOD(
graph.init(graph_context);
// Get runtime graph inputs and outputs
float* graph_float_input = nullptr;
graph_float_input =
static_cast<float*>(graph.getInputPtr("GraphFloatInput"));
float graph_float_input = 0.f;
graph.SetInput("GraphFloatInput", &graph_float_input);
Socket* anim_output_socket =
graph.getOutputSocket("GraphOutput");
AnimData* graph_anim_output = static_cast<AnimData*>(graph.getOutputPtr("GraphOutput"));
AnimData graph_anim_output;
graph_anim_output.m_local_matrices.resize(skeleton->num_joints());
graph.SetOutput("GraphOutput", &graph_anim_output);
// Evaluate graph
*graph_float_input = 0.1f;
graph_float_input = 0.1f;
graph.markActiveNodes();
CHECK(graph.m_nodes[trans_x_node_index]->m_state == AnimNodeEvalState::Activated);
@@ -215,6 +213,6 @@ TEST_CASE_METHOD(
graph.updateTime(0.5f);
graph.evaluate(graph_context);
CHECK(graph_anim_output->m_local_matrices[0].translation.x[0] == Approx(0.5).margin(0.1));
CHECK(graph_anim_output->m_local_matrices[0].translation.y[0] == Approx(0.05).margin(0.01));
CHECK(graph_anim_output.m_local_matrices[0].translation.x[0] == Approx(0.5).margin(0.1));
CHECK(graph_anim_output.m_local_matrices[0].translation.y[0] == Approx(0.05).margin(0.01));
}
+113 -61
View File
@@ -32,12 +32,90 @@ bool load_skeleton (ozz::animation::Skeleton& skeleton, const char* filename) {
return true;
}
TEST_CASE("BasicGraph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
graph_resource.clear();
graph_resource.m_name = "WalkRunBlendGraph";
// Prepare graph inputs and outputs
size_t walk_node_index =
graph_resource.addNode(AnimNodeResourceFactory("AnimSampler"));
AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index];
walk_node.m_name = "WalkAnim";
walk_node.m_socket_accessor->SetPropertyValue("Filename", std::string("data/walk.anim.ozz"));
AnimNodeResource& graph_node = graph_resource.m_nodes[0];
graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
graph_resource.connectSockets(
walk_node,
"Output",
graph_resource.getGraphOutputNode(),
"GraphOutput");
graph_resource.saveToFile("WalkGraph.animgraph.json");
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.loadFromFile("WalkGraph.animgraph.json");
AnimGraph graph;
graph_resource_loaded.createInstance(graph);
AnimGraphContext graph_context;
ozz::animation::Skeleton skeleton;
REQUIRE(load_skeleton(skeleton, "data/skeleton.ozz"));
graph_context.m_skeleton = &skeleton;
REQUIRE(graph.init(graph_context));
REQUIRE(graph.m_nodes.size() == 3);
REQUIRE(graph.m_nodes[0]->m_node_type_name == "BlendTree");
REQUIRE(graph.m_nodes[1]->m_node_type_name == "BlendTree");
REQUIRE(graph.m_nodes[2]->m_node_type_name == "AnimSampler");
// connections within the graph
AnimSamplerNode* anim_sampler_walk =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[2]);
BlendTreeNode* graph_output_node =
dynamic_cast<BlendTreeNode*>(graph.m_nodes[0]);
// check node input dependencies
size_t anim_sampler_index = anim_sampler_walk->m_index;
REQUIRE(graph.m_node_output_connections[anim_sampler_index].size() == 1);
CHECK(
graph.m_node_output_connections[anim_sampler_index][0].m_target_node
== graph_output_node);
// Ensure animation sampler nodes use the correct files
REQUIRE(anim_sampler_walk->m_filename == "data/walk.anim.ozz");
REQUIRE(anim_sampler_walk->m_animation != nullptr);
// Ensure that outputs are properly propagated.
AnimData output;
output.m_local_matrices.resize(skeleton.num_soa_joints());
graph.SetOutput("GraphOutput", &output);
REQUIRE(anim_sampler_walk->o_output == &output);
WHEN("Emulating Graph Evaluation") {
CHECK(graph.m_anim_data_allocator.size() == 0);
anim_sampler_walk->Evaluate(graph_context);
}
graph_context.freeAnimations();
}
TEST_CASE("Blend2Graph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
graph_resource.clear();
graph_resource.m_name = "WalkRunBlendGraph";
// Prepare graph inputs and outputs
size_t walk_node_index =
graph_resource.addNode(AnimNodeResourceFactory("AnimSampler"));
@@ -48,9 +126,9 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index];
walk_node.m_name = "WalkAnim";
walk_node.m_socket_accessor->SetProperty("Filename", std::string("data/walk.anim.ozz"));
walk_node.m_socket_accessor->SetPropertyValue("Filename", std::string("data/walk.anim.ozz"));
AnimNodeResource& run_node = graph_resource.m_nodes[run_node_index];
run_node.m_socket_accessor->SetProperty("Filename", std::string("data/run.anim.ozz"));
run_node.m_socket_accessor->SetPropertyValue("Filename", std::string("data/run.anim.ozz"));
run_node.m_name = "RunAnim";
AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index];
blend_node.m_name = "BlendWalkRun";
@@ -70,9 +148,9 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
graph_resource.getGraphOutputNode(),
"GraphOutput");
graph_resource.saveToFile("WalkGraph.animgraph.json");
graph_resource.saveToFile("Blend2Graph.animgraph.json");
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.loadFromFile("WalkGraph.animgraph.json");
graph_resource_loaded.loadFromFile("Blend2Graph.animgraph.json");
AnimGraph graph;
graph_resource_loaded.createInstance(graph);
@@ -130,22 +208,9 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
WHEN("Emulating Graph Evaluation") {
CHECK(graph.m_anim_data_allocator.size() == 0);
graph.prepareNodeEval(graph_context, walk_node_index);
graph.finishNodeEval(walk_node_index);
graph.prepareNodeEval(graph_context, run_node_index);
graph.finishNodeEval(run_node_index);
graph.prepareNodeEval(graph_context, blend_node_index);
CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output);
CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output);
graph.finishNodeEval(blend_node_index);
// Evaluate output node.
graph.evalOutputNode();
graph.finishNodeEval(0);
const Socket* graph_output_socket = graph.getOutputSocket("GraphOutput");
AnimData* graph_output =
static_cast<AnimData*>(*graph_output_socket->m_reference.ptr_ptr);
@@ -279,30 +344,30 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
anim_graph.getInputPtr("GraphFloatInput")
== anim_graph.m_input_buffer);
float* graph_float_input = nullptr;
graph_float_input =
static_cast<float*>(anim_graph.getInputPtr("GraphFloatInput"));
*graph_float_input = 123.456f;
float graph_float_input = 123.456f;
anim_graph.SetInput("GraphFloatInput", &graph_float_input);
AND_WHEN("Evaluating Graph") {
AnimGraphContext context;
context.m_graph = &anim_graph;
anim_graph.init(context);
// GraphFloatOutput is directly connected to GraphFloatInput therefore
// we need to get the pointer here.
float* graph_float_ptr = nullptr;
graph_float_ptr = anim_graph.GetOutputPtr<float>("GraphFloatOutput");
Vec3 graph_vec3_output;
anim_graph.SetOutput("GraphVec3Output", &graph_vec3_output);
anim_graph.updateTime(0.f);
anim_graph.evaluate(context);
Socket* float_output_socket =
anim_graph.getOutputSocket("GraphFloatOutput");
Socket* vec3_output_socket =
anim_graph.getOutputSocket("GraphVec3Output");
Vec3& vec3_output =
*static_cast<Vec3*>(vec3_output_socket->m_reference.ptr);
THEN("output vector components equal the graph input vaulues") {
CHECK(vec3_output.v[0] == *graph_float_input);
CHECK(vec3_output.v[1] == *graph_float_input);
CHECK(vec3_output.v[2] == *graph_float_input);
CHECK(graph_float_ptr == &graph_float_input);
CHECK(graph_vec3_output.v[0] == graph_float_input);
CHECK(graph_vec3_output.v[1] == graph_float_input);
CHECK(graph_vec3_output.v[2] == graph_float_input);
}
context.freeAnimations();
@@ -312,7 +377,6 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
}
}
/*
TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
AnimGraphResource graph_resource_origin;
@@ -416,41 +480,31 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
anim_graph.getInputPtr("GraphFloatInput")
== anim_graph.m_input_buffer);
float* graph_float_input = nullptr;
graph_float_input =
static_cast<float*>(anim_graph.getInputPtr("GraphFloatInput"));
*graph_float_input = 123.456f;
float graph_float_input = 123.456f;
anim_graph.SetInput("GraphFloatInput", &graph_float_input);
AND_WHEN("Evaluating Graph") {
AnimGraphContext context;
context.m_graph = &anim_graph;
// float0 output is directly connected to the graph input, therefore
// we have to get a ptr to the input data here.
float* float0_output_ptr = nullptr;
float float1_output = -1.f;
float float2_output = -1.f;
float0_output_ptr = anim_graph.GetOutputPtr<float>("GraphFloat0Output");
anim_graph.SetOutput("GraphFloat1Output", &float1_output);
anim_graph.SetOutput("GraphFloat2Output", &float2_output);
anim_graph.updateTime(0.f);
anim_graph.evaluate(context);
Socket* float0_output_socket =
anim_graph.getOutputSocket("GraphFloat0Output");
Socket* float1_output_socket =
anim_graph.getOutputSocket("GraphFloat1Output");
Socket* float2_output_socket =
anim_graph.getOutputSocket("GraphFloat2Output");
REQUIRE(float0_output_socket != nullptr);
REQUIRE(float1_output_socket != nullptr);
REQUIRE(float2_output_socket != nullptr);
float& float0_output =
*static_cast<float*>(float0_output_socket->m_reference.ptr);
float& float1_output =
*static_cast<float*>(float1_output_socket->m_reference.ptr);
float& float2_output =
*static_cast<float*>(float2_output_socket->m_reference.ptr);
THEN("output vector components equal the graph input vaulues") {
CHECK(float0_output == Approx(*graph_float_input));
CHECK(float1_output == Approx(*graph_float_input * 2.));
CHECK(float2_output == Approx(*graph_float_input * 3.));
CHECK(*float0_output_ptr == Approx(graph_float_input));
CHECK(float1_output == Approx(graph_float_input * 2.f));
REQUIRE_THAT(float2_output, Catch::Matchers::WithinAbs(graph_float_input * 3.f, 10));
}
context.freeAnimations();
@@ -458,5 +512,3 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
}
}
}
*/