Skip to content

Commit

Permalink
List running tasks, improve transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Oct 24, 2023
1 parent 3181ba4 commit 7d12c46
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
33 changes: 31 additions & 2 deletions blade-asset/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Hash, Ord)]
struct Address {
index: u32,
chunk: NonZeroU8,
Expand Down Expand Up @@ -81,6 +81,10 @@ impl<T: Default> Arena<T> {
}
}

fn chunk_size(&self, chunk: NonZeroU8) -> usize {
self.min_size << chunk.get()
}

pub fn alloc(&self, value: T) -> Handle<T> {
let mut freeman = self.freeman.lock().unwrap();
let (address, chunk_start) = match freeman.free_list.pop() {
Expand All @@ -93,7 +97,7 @@ impl<T: Default> Arena<T> {
index: 0,
chunk: NonZeroU8::new(freeman.chunk_bases.len() as _).unwrap(),
};
let size = self.min_size << freeman.chunk_bases.len();
let size = self.chunk_size(address.chunk);
let mut data = (0..size).map(|_| T::default()).collect::<Box<[T]>>();
let chunk_start: *mut T = data.first_mut().unwrap();
self.chunks[address.chunk.get() as usize].store(chunk_start, Ordering::Release);
Expand Down Expand Up @@ -126,6 +130,31 @@ impl<T: Default> Arena<T> {
let ptr = self.get_mut_ptr(handle);
unsafe { mem::take(&mut *ptr) }
}

pub fn for_each(&self, mut fun: impl FnMut(Handle<T>, &T)) {
let mut freeman = self.freeman.lock().unwrap();
freeman.free_list.sort(); // enables fast search
for (chunk_index, chunk_start) in self.chunks[..freeman.chunk_bases.len()]
.iter()
.enumerate()
.skip(1)
{
let first_ptr = chunk_start.load(Ordering::Acquire);
let chunk = NonZeroU8::new(chunk_index as _).unwrap();
for index in 0..self.chunk_size(chunk) {
let address = Address {
index: index as u32,
chunk,
};
if freeman.free_list.binary_search(&address).is_err() {
//Note: this is only safe if `get_mut_ptr` isn't called
// for example, during hot reloading.
let item = unsafe { &*first_ptr.add(index) };
fun(Handle(address, PhantomData), item);
}
}
}
}
}

impl<T> Drop for FreeManager<T> {
Expand Down
10 changes: 10 additions & 0 deletions blade-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,14 @@ impl<B: Baker> AssetManager<B> {
task
})
}

pub fn list_running_tasks(&self, list: &mut Vec<choir::RunningTask>) {
self.slots.for_each(|_, slot| {
if let Some(ref task) = slot.load_task {
if !task.is_done() {
list.push(task.clone());
}
}
});
}
}
2 changes: 1 addition & 1 deletion blade-render/code/debug-draw.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn debug_vs(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) inst
let ndc = local_dir.xy / tan(0.5 * camera.fov);

var out: DebugVarying;
out.pos = vec4<f32>(ndc, 0.0, local_dir.z);
out.pos = vec4<f32>(ndc, 0.0, -local_dir.z);
out.color = unpack4x8unorm(point.color);
out.dir = world_dir;
return out;
Expand Down
8 changes: 8 additions & 0 deletions blade-render/src/asset_hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ impl AssetHub {
finish_task: self.shaders.choir.spawn(name).init_dummy(),
}
}

pub fn list_running_tasks(&self) -> Vec<choir::RunningTask> {
let mut list = Vec::new();
self.textures.list_running_tasks(&mut list);
self.models.list_running_tasks(&mut list);
self.shaders.list_running_tasks(&mut list);
list
}
}

impl LoadContext<'_> {
Expand Down
20 changes: 18 additions & 2 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,14 @@ impl Example {
self.render_times.push_front(delta.as_millis() as u32);

if self.pending_scene.is_some() {
ui.spinner();
ui.horizontal(|ui| {
ui.label("Loading...");
ui.spinner();
});
//TODO: seeing GPU Device Lost issues without this
for task in self.asset_hub.list_running_tasks() {
ui.label(format!("{}", task.as_ref()));
}
return;
}

Expand Down Expand Up @@ -778,7 +784,17 @@ fn main() {
let mut quit = false;
let raw_input = egui_winit.take_egui_input(&window);
let egui_output = egui_ctx.run(raw_input, |egui_ctx| {
let frame = egui::Frame::default().fill(egui::Color32::from_white_alpha(0x80));
let frame = {
let mut frame = egui::Frame::side_top_panel(&egui_ctx.style());
let mut fill = frame.fill.to_array();
for f in fill.iter_mut() {
*f = (*f as u32 * 7 / 8) as u8;
}
frame.fill = egui::Color32::from_rgba_premultiplied(
fill[0], fill[1], fill[2], fill[3],
);
frame
};
egui::SidePanel::right("control_panel")
.frame(frame)
.show(egui_ctx, |ui| {
Expand Down

0 comments on commit 7d12c46

Please sign in to comment.