Skip to content

Commit

Permalink
Zephyr: Renderer: fix entity removal bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jun 13, 2024
1 parent 5ac66ef commit 6168dc2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ namespace zephyr {
};

struct RenderBundleItem {
RenderBundleItem(const Matrix4& local_to_world, u32 draw_command_id, u32 material_id) : local_to_world{local_to_world}, draw_command_id{draw_command_id}, material_id{material_id} {}
RenderBundleItem(const Matrix4& local_to_world, u32 draw_command_id, u32 material_id, u64 entity_id) : local_to_world{local_to_world}, draw_command_id{draw_command_id}, material_id{material_id}, entity_id{entity_id} {}
Matrix4 local_to_world;
u32 draw_command_id;
u32 material_id;
u32 padding[2]; // Padding for std430 buffer layout
u64 entity_id;
};

virtual ~RenderBackend() = default;
Expand Down
2 changes: 1 addition & 1 deletion zephyr/renderer/include/zephyr/renderer/render_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace zephyr {

private:
using Entity = u32;
using EntityID = size_t;
using EntityID = u64;

enum ComponentFlag : Entity {
COMPONENT_FLAG_MESH = 1ul << 0,
Expand Down
11 changes: 7 additions & 4 deletions zephyr/renderer/src/render_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ namespace zephyr {
for(const RenderScenePatch& render_scene_patch : m_render_scene_patches) {
switch(render_scene_patch.type) {
case RenderScenePatch::Type::MeshMounted: {
const Transform& entity_transform = m_components_transform[render_scene_patch.entity_id];
const EntityID entity_id = render_scene_patch.entity_id;
const Transform& entity_transform = m_components_transform[entity_id];
const Mesh& entity_mesh = m_components_mesh[render_scene_patch.entity_id];

// TODO(fleroviux): get rid of unsafe size_t to u32 conversion.
Expand All @@ -69,17 +70,19 @@ namespace zephyr {
render_bundle_key.geometry_layout = render_geometry->GetLayout().key;

std::vector<RenderBackend::RenderBundleItem>& render_bundle = m_render_bundles[render_bundle_key];
render_bundle.emplace_back(entity_transform.local_to_world, (u32)render_geometry->GetGeometryID(), (u32)0u);
render_bundle.emplace_back(entity_transform.local_to_world, (u32)render_geometry->GetGeometryID(), (u32)0u, entity_id);

m_entity_to_render_item_location[render_scene_patch.entity_id] = { render_bundle_key, render_bundle.size() - 1u };
m_entity_to_render_item_location[entity_id] = { render_bundle_key, render_bundle.size() - 1u };
break;
}
case RenderScenePatch::Type::MeshRemoved: {
const auto match = m_entity_to_render_item_location.find(render_scene_patch.entity_id);
const RenderBundleItemLocation& location = match->second;

std::vector<RenderBackend::RenderBundleItem>& render_bundle = m_render_bundles[location.key];
render_bundle.erase(render_bundle.begin() + location.index);
render_bundle[location.index] = render_bundle.back();
m_entity_to_render_item_location[render_bundle.back().entity_id].index = location.index;
render_bundle.pop_back();

m_entity_to_render_item_location.erase(match);
break;
Expand Down

0 comments on commit 6168dc2

Please sign in to comment.