Skip to content

Commit

Permalink
[animation_renderer] drastically improved the performance of interpol…
Browse files Browse the repository at this point in the history
…ate_keyframes
  • Loading branch information
harrand committed Feb 1, 2024
1 parent 6bdbb23 commit d7d16b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/tz/core/data/transform_hierarchy.inl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ namespace tz
template<typename T>
const transform_node<T>& transform_hierarchy<T>::get_node(unsigned int id) const
{
TZ_PROFZONE("transform_hierarchy - get node", 0xFF0000AA);
// dont instrument this. this gets called a **ton** even on tiny scenes.
//TZ_PROFZONE("transform_hierarchy - get node", 0xFF0000AA);
tz::assert(id < this->nodes.size(), "Invalid node id %u", id);
return this->nodes[id];
}
Expand Down
6 changes: 4 additions & 2 deletions src/tz/io/gltf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ namespace tz::io
mutable tz::vec4 transform = tz::vec4::zero();

bool operator<(const keyframe_data_element& rhs) const{return this->time_point < rhs.time_point;}
bool operator<(float time) const{return this->time_point < time;}
};
using keyframe_data = std::tuple<std::set<keyframe_data_element>, std::set<keyframe_data_element>, std::set<keyframe_data_element>>;
using keyframe_iterator = std::set<keyframe_data_element>::iterator;
using keyframe_container = std::set<keyframe_data_element>;
using keyframe_data = std::tuple<keyframe_container, keyframe_container, keyframe_container>;
using keyframe_iterator = keyframe_container::iterator;
std::string name = "Unnamed";
std::vector<gltf_animation_channel> channels = {};
std::vector<gltf_animation_sampler> samplers = {};
Expand Down
32 changes: 16 additions & 16 deletions src/tz/ren/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "tz/gl/imported_shaders.hpp"
#include "tz/io/gltf.hpp"

#include ImportedShaderHeader(animation, vertex)
#include ImportedShaderHeader(animation, fragment)

Expand Down Expand Up @@ -427,26 +428,25 @@ namespace tz::ren
//--------------------------------------------------------------------------------------------------

// implementation detail for single_animation_advance
using keyframe_iterator = std::set<tz::io::gltf_animation::keyframe_data_element>::iterator;
std::pair<std::size_t, std::size_t> interpolate_animation_keyframes(keyframe_iterator front, keyframe_iterator back, float time)
using keyframe_container = tz::io::gltf_animation::keyframe_container;
std::pair<std::size_t, std::size_t> interpolate_animation_keyframes(const keyframe_container& set, float time)
{
TZ_PROFZONE("animation - interpolate keyframes", 0xFFE54550);
keyframe_iterator iter = front;
while(iter != back && iter->time_point <= time)
{
iter++;
}
std::size_t idx = std::distance(front, iter);
if(idx == 0)
auto iter = set.upper_bound({.time_point = time});
// if all time points are greater than the given time point, return the first 1 indices.
if(iter == set.begin())
{
return {0u, 1u};
}
auto dist = std::distance(front, back);
if(std::cmp_greater_equal(idx, dist))
// if there are no time points greater, then return the last 2.
if(iter == set.end())
{
return {dist - 2, dist - 1};
auto sz = set.size();
return {sz - 2, sz - 1};
}
return {idx - 1, idx};
// otherwise simply return the iterator-before-this and this.
auto dist = std::distance(set.begin(), iter);
return {dist - 1, dist - 0};
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -516,9 +516,9 @@ namespace tz::ren


const auto& [kf_positions, kf_rotations, kf_scales] = anim.node_animation_data[nid];
auto [pos_before_id, pos_after_id] = interpolate_animation_keyframes(kf_positions.begin(), kf_positions.end(), t);
auto [rot_before_id, rot_after_id] = interpolate_animation_keyframes(kf_rotations.begin(), kf_rotations.end(), t);
auto [scale_before_id, scale_after_id] = interpolate_animation_keyframes(kf_scales.begin(), kf_scales.end(), t);
auto [pos_before_id, pos_after_id] = interpolate_animation_keyframes(kf_positions, t);
auto [rot_before_id, rot_after_id] = interpolate_animation_keyframes(kf_rotations, t);
auto [scale_before_id, scale_after_id] = interpolate_animation_keyframes(kf_scales, t);
if(kf_positions.size() > 1)
{
TZ_PROFZONE("update objects - position", 0xFFE54550);
Expand Down

0 comments on commit d7d16b0

Please sign in to comment.