Remove resources created by entities after they die

This commit is contained in:
SeanOMik 2023-09-04 19:07:34 -04:00
parent ef4426a991
commit 25aff0cc1f
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
1 changed files with 12 additions and 3 deletions

View File

@ -1,5 +1,5 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque, HashSet};
use std::mem; use std::mem;
use std::num::NonZeroU64; use std::num::NonZeroU64;
use std::sync::Arc; use std::sync::Arc;
@ -551,13 +551,16 @@ impl BasicRenderer {
impl Renderer for BasicRenderer { impl Renderer for BasicRenderer {
fn prepare(&mut self, main_world: &mut edict::World) { fn prepare(&mut self, main_world: &mut edict::World) {
let last_epoch = main_world.epoch(); let last_epoch = main_world.epoch();
// render_limits.max_uniform_buffer_binding_size
let mut alive_entities = HashSet::new();
for (entity, model, model_epoch, transform) in main_world.query::<(Entities, &MeshComponent, EpochOf<MeshComponent>, &TransformComponent)>().iter() { for (entity, model, model_epoch, transform) in main_world.query::<(Entities, &MeshComponent, EpochOf<MeshComponent>, &TransformComponent)>().iter() {
// Create the render job and push it to the queue // Create the render job and push it to the queue
let job = RenderJob::new(model.mesh.clone(), model.material.clone(), entity, transform.transform, None); let job = RenderJob::new(model.mesh.clone(), model.material.clone(), entity, transform.transform, None);
self.render_jobs.push_back(job); self.render_jobs.push_back(job);
// TODO: Delete dead entites from buffer_storage alive_entities.insert(entity);
if self.buffer_storage.get(&entity).is_none() { if self.buffer_storage.get(&entity).is_none() {
// check if the transform buffers need to be expanded // check if the transform buffers need to be expanded
if self.transform_buffers.should_expand() { if self.transform_buffers.should_expand() {
@ -586,6 +589,12 @@ impl Renderer for BasicRenderer {
// collect dead entities // collect dead entities
self.transform_buffers.tick(); self.transform_buffers.tick();
// when buffer storage length does not match the amount of iterated entities,
// remove all dead entities, and their buffers, if they weren't iterated over
if self.buffer_storage.len() != alive_entities.len() {
self.buffer_storage.retain(|e, _| alive_entities.contains(e));
}
if let Some(camera) = main_world.query_mut::<(&mut CameraComponent,)>().into_iter().next() { if let Some(camera) = main_world.query_mut::<(&mut CameraComponent,)>().into_iter().next() {
let view_proj = self.inuse_camera.update_view_projection(camera); let view_proj = self.inuse_camera.update_view_projection(camera);
self.queue.write_buffer(&self.camera_buffer, 0, bytemuck::cast_slice(&[view_proj.clone()])); self.queue.write_buffer(&self.camera_buffer, 0, bytemuck::cast_slice(&[view_proj.clone()]));