Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite how transforms are allocated #122

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions application/scene_viewer_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ void SceneViewerApplication::read_lights()

auto node = scene.create_node();
for (int i = 0; i < 3; i++)
node->transform.translation[i] = spot["position"][i].GetFloat();
node->get_transform().translation[i] = spot["position"][i].GetFloat();

auto &dir = spot["direction"];
node->transform.rotation = conjugate(look_at_arbitrary_up(vec3(
node->get_transform().rotation = conjugate(look_at_arbitrary_up(vec3(
dir[0].GetFloat(), dir[1].GetFloat(), dir[2].GetFloat())));
scene.get_root_node()->add_child(node);
auto entity = scene.create_light(info, node.get());
Expand All @@ -118,7 +118,7 @@ void SceneViewerApplication::read_lights()

auto node = scene.create_node();
for (int i = 0; i < 3; i++)
node->transform.translation[i] = point["position"][i].GetFloat();
node->get_transform().translation[i] = point["position"][i].GetFloat();
scene.get_root_node()->add_child(node);
auto entity = scene.create_light(info, node.get());
entity->allocate_component<IrradianceAffectingComponent>();
Expand Down Expand Up @@ -304,8 +304,8 @@ SceneViewerApplication::SceneViewerApplication(const std::string &path, const st
{
auto &scene = scene_loader.get_scene();
auto node = scene.create_node();
node->transform.scale = vec3(32.0f, 8.0f, 32.0f);
node->transform.translation = vec3(0.0f, 3.5f, 0.0f);
node->get_transform().scale = vec3(32.0f, 8.0f, 32.0f);
node->get_transform().translation = vec3(0.0f, 3.5f, 0.0f);
node->invalidate_cached_transform();
scene.create_volumetric_diffuse_light(uvec3(32, 8, 32), node.get());
scene.get_root_node()->add_child(std::move(node));
Expand All @@ -315,8 +315,8 @@ SceneViewerApplication::SceneViewerApplication(const std::string &path, const st
{
auto &scene = scene_loader.get_scene();
auto node = scene.create_node();
node->transform.scale = vec3(40.0f);
node->transform.translation = vec3(0.0f, 20.0f, 0.0f);
node->get_transform().scale = vec3(40.0f);
node->get_transform().translation = vec3(0.0f, 20.0f, 0.0f);
node->invalidate_cached_transform();
scene.create_volumetric_fog_region(node.get());
scene.get_root_node()->add_child(std::move(node));
Expand Down Expand Up @@ -529,12 +529,12 @@ void SceneViewerApplication::rescale_scene(float radius)
.get_entity_pool()
.get_component_group<RenderInfoComponent, RenderableComponent>();
for (auto &caster : objects)
aabb.expand(get_component<RenderInfoComponent>(caster)->world_aabb);
aabb.expand(get_component<RenderInfoComponent>(caster)->get_aabb());

float scale_factor = radius / aabb.get_radius();
auto root_node = scene_loader.get_scene().get_root_node();
auto new_root_node = scene_loader.get_scene().create_node();
new_root_node->transform.scale = vec3(scale_factor);
new_root_node->get_transform().scale = vec3(scale_factor);
new_root_node->add_child(root_node);
scene_loader.get_scene().set_root_node(new_root_node);
}
Expand Down Expand Up @@ -575,8 +575,8 @@ bool SceneViewerApplication::on_key_down(const KeyboardEvent &e)
light.inner_cone = 0.92f;
light.color = vec3(10.0f);

node->transform.translation = pos;
node->transform.rotation = conjugate(look_at_arbitrary_up(selected_camera->get_front()));
node->get_transform().translation = pos;
node->get_transform().rotation = conjugate(look_at_arbitrary_up(selected_camera->get_front()));

auto *entity = scene.create_light(light, node.get());
entity->allocate_component<IrradianceAffectingComponent>();
Expand All @@ -593,7 +593,7 @@ bool SceneViewerApplication::on_key_down(const KeyboardEvent &e)
SceneFormats::LightInfo light;
light.type = SceneFormats::LightInfo::Type::Point;
light.color = vec3(10.0f);
node->transform.translation = pos;
node->get_transform().translation = pos;

auto *entity = scene.create_light(light, node.get());
entity->allocate_component<IrradianceAffectingComponent>();
Expand Down Expand Up @@ -1261,7 +1261,7 @@ void SceneViewerApplication::update_shadow_scene_aabb()
.get_component_group<RenderInfoComponent, RenderableComponent, CastsStaticShadowComponent>();
AABB aabb(vec3(FLT_MAX), vec3(-FLT_MAX));
for (auto &caster : shadow_casters)
aabb.expand(get_component<RenderInfoComponent>(caster)->world_aabb);
aabb.expand(get_component<RenderInfoComponent>(caster)->get_aabb());
shadow_scene_aabb = aabb;
}

Expand Down
2 changes: 1 addition & 1 deletion renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ add_granite_internal_lib(granite-renderer
camera.hpp camera.cpp
material.hpp
abstract_renderable.hpp
render_components.hpp
render_components.hpp render_components.cpp
mesh_util.hpp mesh_util.cpp
material_util.hpp material_util.cpp
renderer.hpp renderer.cpp
Expand Down
58 changes: 38 additions & 20 deletions renderer/animation_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uint32_t AnimationUnrolled::get_multi_node_index(unsigned channel) const
return multi_node_indices[channel];
}

void AnimationUnrolled::animate(Transform *const *transforms, unsigned num_transforms, float offset_time) const
void AnimationUnrolled::animate(Transform *transforms, const uint32_t *transform_indices, unsigned num_transforms, float offset_time) const
{
if (num_transforms != get_num_channels())
throw std::logic_error("Incorrect number of transforms.");
Expand All @@ -102,7 +102,7 @@ void AnimationUnrolled::animate(Transform *const *transforms, unsigned num_trans

for (unsigned i = 0; i < num_transforms; i++)
{
auto *t = transforms[i];
auto *t = &transforms[transform_indices[i]];
animate_single(*t, i, lo, hi, l);
}
}
Expand Down Expand Up @@ -302,9 +302,11 @@ AnimationStateID AnimationSystem::start_animation(Node &node, Granite::Animation
return 0;
}

Util::SmallVector<Transform *> target_transforms = { &node.transform };
Util::SmallVector<uint32_t> target_transforms = { node.transform.offset };
Util::SmallVector<Node *> nodes = { &node };
id = animation_state_pool.emplace(*animation, std::move(target_transforms), std::move(nodes), start_time);
id = animation_state_pool.emplace(*animation,
node.get_transform_base(),
std::move(target_transforms), std::move(nodes), start_time);
}

auto *state = &animation_state_pool.get(id);
Expand All @@ -331,7 +333,9 @@ void AnimationSystem::set_fixed_pose(Node &node, Granite::AnimationID id, float
return;
}

animation->animate(node.get_skin()->skin.data(), node.get_skin()->skin.size(), offset);
animation->animate(node.get_transform_base(),
node.get_skin()->skin.data(), node.get_skin()->skin.size(), offset);

node.invalidate_cached_transform();
}
else
Expand All @@ -342,8 +346,8 @@ void AnimationSystem::set_fixed_pose(Node &node, Granite::AnimationID id, float
return;
}

Transform *t = &node.transform;
animation->animate(&t, 1, offset);
uint32_t transform_index = node.transform.offset;
animation->animate(node.get_transform_base(), &transform_index, 1, offset);
node.invalidate_cached_transform();
}
}
Expand All @@ -364,11 +368,12 @@ void AnimationSystem::set_fixed_pose_multi(NodeHandle *nodes, unsigned num_nodes
return;
}

if (!num_nodes)
return;

// Not very efficient.
Util::SmallVector<Transform *> target_transforms;
Util::SmallVector<Node *> target_nodes;
Util::SmallVector<uint32_t> target_transforms;
target_transforms.reserve(animation->get_num_channels());
target_nodes.reserve(animation->get_num_channels());

for (unsigned channel = 0; channel < animation->get_num_channels(); channel++)
{
Expand All @@ -379,11 +384,12 @@ void AnimationSystem::set_fixed_pose_multi(NodeHandle *nodes, unsigned num_nodes
return;
}

target_transforms.push_back(&nodes[index]->transform);
target_transforms.push_back(nodes[index]->transform.offset);
nodes[index]->invalidate_cached_transform();
}

animation->animate(target_transforms.data(), target_transforms.size(), offset);
animation->animate(nodes[0]->get_transform_base(),
target_transforms.data(), target_transforms.size(), offset);
}

AnimationStateID AnimationSystem::start_animation_multi(NodeHandle *nodes, unsigned num_nodes,
Expand All @@ -402,7 +408,13 @@ AnimationStateID AnimationSystem::start_animation_multi(NodeHandle *nodes, unsig
return 0;
}

Util::SmallVector<Transform *> target_transforms;
if (!num_nodes)
{
LOGE("Number of nodes must not be 0.\n");
return 0;
}

Util::SmallVector<uint32_t> target_transforms;
Util::SmallVector<Node *> target_nodes;
target_transforms.reserve(animation->get_num_channels());
target_nodes.reserve(animation->get_num_channels());
Expand All @@ -416,11 +428,12 @@ AnimationStateID AnimationSystem::start_animation_multi(NodeHandle *nodes, unsig
return 0;
}

target_transforms.push_back(&nodes[index]->transform);
target_transforms.push_back(nodes[index]->transform.offset);
target_nodes.push_back(nodes[index].get());
}

auto id = animation_state_pool.emplace(*animation, std::move(target_transforms), std::move(target_nodes), start_time);
auto id = animation_state_pool.emplace(*animation, target_nodes.front()->get_transform_base(),
std::move(target_transforms), std::move(target_nodes), start_time);
auto *state = &animation_state_pool.get(id);
state->id = id;
active_animation.add(state);
Expand Down Expand Up @@ -472,12 +485,14 @@ void AnimationSystem::update(AnimationState *anim, double frame_time, double ela
if (anim->animation.is_skinned())
{
auto *node = anim->skinned_node;
anim->animation.animate(node->get_skin()->skin.data(), node->get_skin()->skin.size(), float(offset));
anim->animation.animate(anim->transforms_base,
node->get_skin()->skin.data(), node->get_skin()->skin.size(), float(offset));
node->invalidate_cached_transform();
}
else
{
anim->animation.animate(anim->channel_transforms.data(), anim->channel_transforms.size(), float(offset));
anim->animation.animate(anim->transforms_base,
anim->channel_transforms.data(), anim->channel_transforms.size(), float(offset));
for (auto *node : anim->channel_nodes)
node->invalidate_cached_transform();
}
Expand Down Expand Up @@ -538,10 +553,12 @@ void AnimationSystem::animate(TaskComposer &composer, double frame_time, double
}

AnimationSystem::AnimationState::AnimationState(const AnimationUnrolled &anim,
Util::SmallVector<Transform *> channel_transforms_,
Transform *transforms_base_,
Util::SmallVector<uint32_t> channel_transforms_,
Util::SmallVector<Node *> channel_nodes_,
double start_time_)
: channel_transforms(std::move(channel_transforms_)),
: transforms_base(transforms_base_),
channel_transforms(std::move(channel_transforms_)),
channel_nodes(std::move(channel_nodes_)),
animation(anim),
start_time(start_time_)
Expand All @@ -550,7 +567,8 @@ AnimationSystem::AnimationState::AnimationState(const AnimationUnrolled &anim,

AnimationSystem::AnimationState::AnimationState(const Granite::AnimationUnrolled &anim, Node *node,
double start_time_)
: skinned_node(node), animation(anim), start_time(start_time_)
: transforms_base(node->get_transform_base()),
skinned_node(node), animation(anim), start_time(start_time_)
{
}
}
8 changes: 5 additions & 3 deletions renderer/animation_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AnimationUnrolled : public Util::IntrusiveHashMapEnabled<AnimationUnrolled
{
public:
AnimationUnrolled(const SceneFormats::Animation &animation, float key_frame_rate);
void animate(Transform * const *transforms, unsigned num_transforms, float offset_time) const;
void animate(Transform *transforms, const uint32_t *transform_indices, unsigned num_transforms, float offset_time) const;

unsigned get_num_channels() const;

Expand Down Expand Up @@ -105,17 +105,19 @@ class AnimationSystem
struct AnimationState : Util::IntrusiveUnorderedArrayEnabled
{
AnimationState(const AnimationUnrolled &anim,
Util::SmallVector<Transform *> channel_transforms_,
Transform *transforms_base_,
Util::SmallVector<uint32_t> channel_transforms_,
Util::SmallVector<Node *> channel_nodes_,
double start_time_);

AnimationState(const AnimationUnrolled &anim,
Node *node,
double start_time_);

Transform *transforms_base;
Node *skinned_node = nullptr;
AnimationStateID id = 0;
Util::SmallVector<Transform *> channel_transforms;
Util::SmallVector<uint32_t> channel_transforms;
Util::SmallVector<Node *> channel_nodes;
const AnimationUnrolled &animation;
double start_time = 0.0;
Expand Down
4 changes: 2 additions & 2 deletions renderer/ground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ GroundPatch::~GroundPatch()

void GroundPatch::refresh(const RenderContext &context, const RenderInfoComponent *transform, TaskComposer &)
{
vec3 center = transform->world_aabb.get_center();
vec3 center = transform->get_aabb().get_center();
const auto &camera_pos = context.get_render_parameters().camera_position;
vec3 diff = center - camera_pos;
float dist_log2 = 0.5f * muglm::log2(dot(diff, diff) + 0.001f);
Expand Down Expand Up @@ -348,7 +348,7 @@ void Ground::get_render_info(const RenderContext &context, const RenderInfoCompo
hasher.s32(base_lod);
hasher.s32(info.bandlimited_pixel);
auto sorting_key = RenderInfo::get_sort_key(context, Queue::Opaque, pipe_hash, hasher.get(),
transform->world_aabb.get_center(),
transform->get_aabb().get_center(),
StaticLayer::Last);

hasher.u64(heightmap->get_cookie());
Expand Down
4 changes: 2 additions & 2 deletions renderer/lights/clusterer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1470,8 +1470,8 @@ void LightClusterer::refresh_bindless(const RenderContext &context_, TaskCompose
static void sort_decals(VolumetricDecalList &list, const vec3 &front)
{
std::sort(list.begin(), list.end(), [&front](const VolumetricDecalInfo &a, const VolumetricDecalInfo &b) -> bool {
float a_dist = dot(front, a.transform->world_aabb.get_center());
float b_dist = dot(front, b.transform->world_aabb.get_center());
float a_dist = dot(front, a.transform->get_aabb().get_center());
float b_dist = dot(front, b.transform->get_aabb().get_center());
return a_dist < b_dist;
});
}
Expand Down
4 changes: 2 additions & 2 deletions renderer/lights/deferred_lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void DeferredLights::refresh(const RenderContext &context, TaskComposer &)
float cluster_max = 0.0f;
for (auto &light : visible)
{
auto &aabb = light.transform->world_aabb;
auto &aabb = light.transform->get_aabb();
float to_center = dot(aabb.get_center() - params.camera_position, params.camera_front);
cluster_min = min(to_center, cluster_min);
cluster_max = max(to_center, cluster_max);
Expand All @@ -73,7 +73,7 @@ void DeferredLights::refresh(const RenderContext &context, TaskComposer &)
// Assign each renderable to a cluster index based on their position.
for (auto &light : visible)
{
auto &aabb = light.transform->world_aabb;
auto &aabb = light.transform->get_aabb();
float to_center = dot(aabb.get_center() - params.camera_position, params.camera_front);
int cluster_index = clamp(int((to_center - cluster_min) * cluster_inv_range), 0, NumClusters - 1);
clusters[cluster_index].push_back(light);
Expand Down
17 changes: 9 additions & 8 deletions renderer/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void StaticMesh::get_render_info(const RenderContext &context, const RenderInfoC
h.u64(vbo_position->get_cookie());

auto instance_key = get_baked_instance_key();
auto sorting_key = RenderInfo::get_sort_key(context, type, pipe_hash, h.get(), transform->world_aabb.get_center());
auto sorting_key = RenderInfo::get_sort_key(context, type, pipe_hash, h.get(), transform->get_aabb().get_center());

auto *instance_data = queue.allocate_one<StaticMeshInstanceInfo>();
instance_data->vertex.Model = transform->get_world_transform();
Expand All @@ -268,7 +268,6 @@ void StaticMesh::get_render_info(const RenderContext &context, const RenderInfoC
instance_data->vertex.PrevModel = queue.allocate_one<mat4>();
*instance_data->vertex.PrevModel = transform->get_prev_world_transform();
}
//instance_data->vertex.Normal = t->normal_transform;

auto *mesh_info = queue.push<StaticMeshInfo>(type, instance_key, sorting_key,
RenderFunctions::static_mesh_render,
Expand Down Expand Up @@ -328,22 +327,24 @@ void SkinnedMesh::get_render_info(const RenderContext &context, const RenderInfo
h.u64(vbo_position->get_cookie());

auto instance_key = get_baked_instance_key() ^ 1;
auto sorting_key = RenderInfo::get_sort_key(context, type, pipe_hash, h.get(), transform->world_aabb.get_center());
auto sorting_key = RenderInfo::get_sort_key(context, type, pipe_hash, h.get(), transform->get_aabb().get_center());

auto *instance_data = queue.allocate_one<SkinnedMeshInstanceInfo>();

auto *skin = transform->get_skin();
unsigned num_bones = skin->cached_skin_transform.bone_world_transforms.size();
unsigned num_bones = skin->transform.count;
instance_data->num_bones = num_bones;
instance_data->world_transforms = queue.allocate_many<mat4>(num_bones);
//instance_data->normal_transforms = queue.allocate_many<mat4>(num_bones);
memcpy(instance_data->world_transforms, skin->cached_skin_transform.bone_world_transforms.data(), num_bones * sizeof(mat4));
//memcpy(instance_data->normal_transforms, transform->skin_transform->bone_normal_transforms.data(), num_bones * sizeof(mat4));
memcpy(instance_data->world_transforms,
transform->scene_node->parent_scene.get_transforms().get_cached_transforms() + skin->transform.offset,
num_bones * sizeof(mat4));

if (mv)
{
instance_data->prev_world_transforms = queue.allocate_many<mat4>(num_bones);
memcpy(instance_data->prev_world_transforms, skin->prev_cached_skin_transform.bone_world_transforms.data(), num_bones * sizeof(mat4));
memcpy(instance_data->prev_world_transforms,
transform->scene_node->parent_scene.get_transforms().get_cached_prev_transforms() + skin->transform.offset,
num_bones * sizeof(mat4));
}

auto *mesh_info = queue.push<StaticMeshInfo>(type, instance_key, sorting_key,
Expand Down
Loading
Loading