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:
@@ -43,7 +43,7 @@ json sSocketToJson(const Socket& socket) {
|
||||
result["value"][2] = socket.m_value.quat.v[2];
|
||||
result["value"][3] = socket.m_value.quat.v[3];
|
||||
} else if (socket.m_type == SocketType::SocketTypeString) {
|
||||
result["value"] = *static_cast<std::string*>(socket.m_reference.ptr);
|
||||
result["value"] = socket.m_value_string;
|
||||
} else {
|
||||
std::cerr << "Invalid socket type '" << static_cast<int>(socket.m_type)
|
||||
<< "'." << std::endl;
|
||||
@@ -357,13 +357,11 @@ void AnimGraphResource::createRuntimeNodeInstances(AnimGraph& instance) const {
|
||||
}
|
||||
|
||||
void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
instance.m_socket_accessor =
|
||||
instance.m_node_descriptor =
|
||||
AnimNodeDescriptorFactory("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;
|
||||
instance.m_socket_accessor->m_outputs =
|
||||
instance.m_node_descriptor->m_outputs =
|
||||
m_nodes[1].m_socket_accessor->m_outputs;
|
||||
instance.m_node_descriptor->m_inputs = m_nodes[0].m_socket_accessor->m_inputs;
|
||||
|
||||
// inputs
|
||||
int input_block_size = 0;
|
||||
@@ -381,6 +379,8 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
for (int i = 0; i < graph_inputs.size(); i++) {
|
||||
graph_inputs[i].m_reference.ptr =
|
||||
(void*)&instance.m_input_buffer[input_block_offset];
|
||||
instance.m_node_descriptor->m_outputs[i].m_reference.ptr =
|
||||
&instance.m_input_buffer[input_block_offset];
|
||||
input_block_offset += sizeof(void*);
|
||||
}
|
||||
|
||||
@@ -388,7 +388,7 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
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;
|
||||
output_block_size += sizeof(void*);
|
||||
}
|
||||
|
||||
if (output_block_size > 0) {
|
||||
@@ -398,12 +398,13 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
|
||||
int output_block_offset = 0;
|
||||
for (int i = 0; i < graph_outputs.size(); i++) {
|
||||
graph_outputs[i].m_reference.ptr =
|
||||
instance.m_node_descriptor->m_inputs[i].m_reference.ptr =
|
||||
&instance.m_output_buffer[output_block_offset];
|
||||
output_block_offset += graph_outputs[i].m_type_size;
|
||||
output_block_offset += sizeof(void*);
|
||||
}
|
||||
|
||||
// connections: make source and target sockets point to the same address in the connection data storage.
|
||||
// TODO: instead of every connection, only create data blocks for the source sockets and make sure every source socket gets allocated once.
|
||||
int connection_data_storage_size = 0;
|
||||
for (int i = 0; i < m_connections.size(); i++) {
|
||||
const AnimGraphConnectionResource& connection = m_connections[i];
|
||||
@@ -427,16 +428,13 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
instance.m_nodes[i]);
|
||||
}
|
||||
|
||||
instance_node_descriptors[0]->m_inputs = instance.getGraphOutputs();
|
||||
instance_node_descriptors[1]->m_outputs = instance.getGraphInputs();
|
||||
instance_node_descriptors[0]->m_inputs = instance.m_node_descriptor->m_inputs;
|
||||
instance_node_descriptors[1]->m_outputs =
|
||||
instance.m_node_descriptor->m_outputs;
|
||||
|
||||
int connection_data_offset = 0;
|
||||
for (int i = 0; i < m_connections.size(); i++) {
|
||||
const AnimGraphConnectionResource& connection = m_connections[i];
|
||||
const AnimNodeResource& source_node_resource =
|
||||
m_nodes[connection.source_node_index];
|
||||
const AnimNodeResource& target_node_resource =
|
||||
m_nodes[connection.target_node_index];
|
||||
|
||||
NodeDescriptorBase* source_node_descriptor =
|
||||
instance_node_descriptors[connection.source_node_index];
|
||||
@@ -470,8 +468,9 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
&instance.m_connection_data_storage[connection_data_offset]);
|
||||
|
||||
if (source_socket->m_type == SocketType::SocketTypeAnimation) {
|
||||
instance.m_animdata_blocks.push_back((AnimData*)(
|
||||
&instance.m_connection_data_storage[connection_data_offset]));
|
||||
instance.m_animdata_blocks.push_back(
|
||||
(AnimData*)(&instance
|
||||
.m_connection_data_storage[connection_data_offset]));
|
||||
}
|
||||
|
||||
connection_data_offset += source_socket->m_type_size;
|
||||
@@ -482,119 +481,6 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
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.
|
||||
//
|
||||
// Skip animation connections and connections to the output node as the
|
||||
// pointers are already set up in AnimGraphResource::prepareGraphIOData().
|
||||
if (target_socket->m_type != SocketType::SocketTypeAnimation
|
||||
&& connection.target_node_index != 0) {
|
||||
(*target_socket->m_reference.ptr_ptr) = source_socket->m_reference.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void AnimGraphResource::setRuntimeNodeProperties(AnimGraph& instance) const {
|
||||
for (int i = 2; i < m_nodes.size(); i++) {
|
||||
const AnimNodeResource& node_resource = m_nodes[i];
|
||||
|
||||
Reference in New Issue
Block a user