Skip to content

Commit

Permalink
feat(world): Entity buffer cleaning (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
suspistew authored Jan 17, 2025
1 parent e3e417d commit 00fd885
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 24 deletions.
16 changes: 10 additions & 6 deletions src/core/scion_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::sync::{Arc, mpsc};
use std::sync::mpsc::Receiver;
use std::thread;
use std::time::Instant;

use hecs::Entity;
use winit::dpi::{PhysicalSize, Size};
use winit::window::Window;

use crate::core::resources::time::Time;
use crate::core::scene::{SceneAction, SceneMachine};
use crate::core::scheduler::Scheduler;
use crate::core::world::GameData;
use crate::core::world::{GameData, World};
use crate::graphics::rendering::{RendererCallbackEvent, RendererEvent, RenderingInfos, RenderingUpdate};
use crate::graphics::rendering::scion2d::pre_renderer::Scion2DPreRenderer;
use crate::graphics::rendering::scion2d::rendering_thread::ScionRenderingThread;
Expand All @@ -33,7 +33,7 @@ impl ScionRunner {
pub(crate) fn launch_game_loop(mut self) {
self.setup();
let mut frame_limiter = FrameLimiter::new(FrameLimiterConfig::default());
let (render_sender, render_receiver) = mpsc::channel::<(Vec<RendererEvent>, Vec<RenderingUpdate>, Vec<RenderingInfos>)>();
let (render_sender, render_receiver) = mpsc::channel::<(Vec<RendererEvent>, Vec<RenderingUpdate>, Vec<RenderingInfos>, Vec<Entity>)>();
let window_rendering_manager = self.window_rendering_manager.take();

thread::spawn(move || { ScionRenderingThread::new(window_rendering_manager, render_receiver).run() });
Expand All @@ -54,7 +54,7 @@ impl ScionRunner {
.expect("Time is an internal resource and can't be missing")
.frame();
self.game_data.timers().add_delta_duration(frame_duration);
let _r = render_sender.send((handle_window_event(&mut self), vec![], vec![]));
let _r = render_sender.send((handle_window_event(&mut self), vec![], vec![],vec![]));
self.layer_machine.apply_scene_action(SceneAction::Update, &mut self.game_data);
self.scheduler.execute(&mut self.game_data);
self.layer_machine.apply_scene_action(SceneAction::LateUpdate, &mut self.game_data);
Expand All @@ -72,7 +72,7 @@ impl ScionRunner {

let updates = self.scion_pre_renderer.prepare_update(&mut self.game_data);
let rendering_infos = Scion2DPreRenderer::prepare_rendering(&mut self.game_data);
let _r = render_sender.send((vec![], updates, rendering_infos));
let _r = render_sender.send((vec![], updates, rendering_infos,vec![]));

frame_limiter.render();
}
Expand All @@ -83,11 +83,15 @@ impl ScionRunner {
self.layer_machine
.apply_scene_action(SceneAction::EndFrame, &mut self.game_data);
frame_limiter.tick(&start_tick);
if let Some(e) = self.game_data.take_despawned(){
render_sender.send((vec![], vec![], vec![], e)).unwrap();
}
}
if let Some(status) = self.game_data.resources.game_state_mut().take_picking_update(){
render_sender.send((vec![RendererEvent::CursorPickingStatusUpdate(status)],vec![], vec![])).unwrap();
render_sender.send((vec![RendererEvent::CursorPickingStatusUpdate(status)],vec![], vec![], vec![])).unwrap();
}


thread::sleep(frame_limiter.min_tick_duration.clone());
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/core/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ impl GameData {
pub(crate) fn has_camera(&self)-> bool{
self.subworld.query::<&Camera>().iter().count() > 0
}

pub(crate) fn take_despawned(&mut self) -> Option<Vec<Entity>>{
self.subworld.entity_cleaner.take()
}
}

impl World for GameData {
Expand All @@ -179,7 +183,7 @@ impl World for GameData {
}

fn remove(&mut self, entity: Entity) -> Result<(), NoSuchEntity> {
self.subworld.internal_world.despawn(entity)
self.subworld.remove(entity)
}

fn add_components(
Expand Down Expand Up @@ -222,6 +226,7 @@ impl World for GameData {
#[derive(Default)]
pub struct SubWorld {
internal_world: hecs::World,
entity_cleaner: Option<Vec<Entity>>,
}

#[derive(Default)]
Expand All @@ -243,6 +248,12 @@ impl World for SubWorld {
}

fn remove(&mut self, entity: Entity) -> Result<(), NoSuchEntity> {
match &mut self.entity_cleaner {
Some(vec) => vec.push(entity),
None => {
self.entity_cleaner = Some(vec![entity]);
}
}
self.internal_world.despawn(entity)
}

Expand Down
1 change: 0 additions & 1 deletion src/graphics/rendering/scion2d/pre_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ impl Scion2DPreRenderer {
self.vertex_buffer.retain(|&k| data.contains(k));
self.indexes_buffer.retain(|&k| data.contains(k));
self.transform_uniform.retain(|&k| data.contains(k));
// TODO transfer a clean buffer update to the rendering thread
}
}
8 changes: 7 additions & 1 deletion src/graphics/rendering/scion2d/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ impl Scion2D {
}
}
}
}

// FIXME : self.clean_buffers(data);
pub(crate) fn clean_entities(&mut self, mut entities: Vec<Entity>) {
entities.drain(0..entities.len()).for_each(|entity| {
self.index_buffers.remove(&entity);
self.vertex_buffers.remove(&entity);
self.transform_uniform_bind_groups.remove(&entity);
});
}

pub(crate) fn render(
Expand Down
9 changes: 6 additions & 3 deletions src/graphics/rendering/scion2d/rendering_thread.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::sync::mpsc::{Receiver};
use hecs::Entity;
use log::{info};
use crate::graphics::rendering::{RendererEvent, RenderingInfos, RenderingUpdate};
use crate::graphics::rendering::scion2d::window_rendering_manager::ScionWindowRenderingManager;

pub(crate) struct ScionRenderingThread {
pub(crate) window_rendering_manager: Option<ScionWindowRenderingManager>,
pub(crate) render_receiver: Receiver<(Vec<RendererEvent>, Vec<RenderingUpdate>, Vec<RenderingInfos>)>,
pub(crate) render_receiver: Receiver<(Vec<RendererEvent>, Vec<RenderingUpdate>, Vec<RenderingInfos>, Vec<Entity>)>,
}

impl ScionRenderingThread{
pub(crate) fn run(mut self) {
info!("Initializing rendering thread");
let mut update_accumulator: Vec<RenderingUpdate> = Vec::new();
loop {
if let Ok((mut events, mut updates, rendering_infos)) = self.render_receiver.recv() {
if let Ok((mut events, mut updates, rendering_infos, cleaner)) = self.render_receiver.recv() {
events.drain(0..events.len()).for_each(|event|{
match event {
RendererEvent::ForceRedraw => {
Expand All @@ -31,6 +32,8 @@ impl ScionRenderingThread{
}
});

self.window_rendering_manager.as_mut().unwrap().clean_entities(cleaner);

if !updates.is_empty(){
update_accumulator.append(&mut updates);
}
Expand All @@ -48,7 +51,7 @@ impl ScionRenderingThread{
}
}

pub fn new(window_rendering_manager: Option<ScionWindowRenderingManager>, render_receiver: Receiver<(Vec<RendererEvent>, Vec<RenderingUpdate>, Vec<RenderingInfos>)>) -> Self{
pub fn new(window_rendering_manager: Option<ScionWindowRenderingManager>, render_receiver: Receiver<(Vec<RendererEvent>, Vec<RenderingUpdate>, Vec<RenderingInfos>, Vec<Entity>)>) -> Self{
Self{
window_rendering_manager,
render_receiver,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use std::collections::BTreeMap;
use hecs::{Component, Entity};
use log::info;
use wgpu::BufferUsages;

use crate::graphics::components::{Square, Triangle};
use crate::graphics::components::material::Material;
use crate::core::components::maths::transform::Transform;
use crate::core::world::{GameData, World};
use crate::graphics::components::material::Material;
use crate::graphics::components::shapes::line::Line;
use crate::graphics::components::shapes::polygon::Polygon;
use crate::graphics::components::shapes::rectangle::Rectangle;
use crate::graphics::components::tiles::sprite::Sprite;
use crate::graphics::components::tiles::tilemap::{Tile, Tilemap};
use crate::graphics::components::ui::ui_image::UiImage;
use crate::graphics::components::ui::ui_text::UiTextImage;
use crate::core::world::{GameData, World};
use crate::graphics::rendering::{Renderable2D, RenderableUi, RenderingUpdate};
use crate::graphics::rendering::shaders::gl_representations::TexturedGlVertexWithLayer;
use crate::graphics::components::{Square, Triangle};
use crate::graphics::rendering::scion2d::pre_renderer::Scion2DPreRenderer;
use crate::graphics::rendering::shaders::gl_representations::TexturedGlVertexWithLayer;
use crate::graphics::rendering::{Renderable2D, RenderableUi, RenderingUpdate};

pub(crate) fn call(renderer: &mut Scion2DPreRenderer, data: &mut GameData) -> Vec<RenderingUpdate> {
let mut updates = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::path::Path;
use std::time::SystemTime;



use crate::core::world::{GameData, World};
use crate::graphics::components::color::Color;
use crate::graphics::components::material::{Material, Texture, TextureArray};
use crate::graphics::components::tiles::tileset::Tileset;
use crate::core::world::{GameData, World};
use crate::graphics::rendering::{DiffuseBindGroupUpdate, RenderingUpdate};
use crate::graphics::rendering::scion2d::pre_renderer::Scion2DPreRenderer;
use crate::utils::file::{FileReaderError, read_file_modification_time};
use crate::graphics::rendering::{DiffuseBindGroupUpdate, RenderingUpdate};
use crate::utils::file::{read_file_modification_time, FileReaderError};

///
/// This function has the responsability to track material creation or updates
Expand Down
7 changes: 6 additions & 1 deletion src/graphics/rendering/scion2d/window_rendering_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;
use std::sync::mpsc::Sender;
use hecs::Entity;
use wgpu::{Limits, Surface, SurfaceConfiguration};
use winit::window::Window;

Expand Down Expand Up @@ -71,7 +72,7 @@ impl ScionWindowRenderingManager {
let mut scion_renderer = Scion2D::default();
scion_renderer.start(&device, &config);

Self { surface, device, queue, config, scion_renderer, default_background_color: default_background, should_render: true, should_compute_cursor_color_picking: false, cursor_position: None, render_callback_sender }
Self { surface, device, queue, config, scion_renderer, default_background_color: default_background, should_render: false, should_compute_cursor_color_picking: false, cursor_position: None, render_callback_sender }
}

pub(crate) fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>, _scale_factor: f64) {
Expand All @@ -97,6 +98,10 @@ impl ScionWindowRenderingManager {
self.should_compute_cursor_color_picking = should_update;
}

pub(crate) fn clean_entities(&mut self, entities: Vec<Entity>) {
self.scion_renderer.clean_entities(entities);
}

pub(crate) fn render(
&mut self,
data: Vec<RenderingInfos>,
Expand Down

0 comments on commit 00fd885

Please sign in to comment.