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

Loading improvements #44

Merged
merged 3 commits into from
Oct 24, 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
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
47 changes: 29 additions & 18 deletions blade-render/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
use std::{
borrow::Cow,
collections::hash_map::{Entry, HashMap},

Check warning on line 3 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused imports: `Entry`, `HashMap`

Check warning on line 3 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused imports: `Entry`, `HashMap`

Check warning on line 3 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused imports: `Entry`, `HashMap`
fmt, hash, mem,
ops::Range,
ptr, str,
sync::{Arc, Mutex},
};

const PRELOAD_TEXTURES: bool = false;

const META_BASE_COLOR: crate::texture::Meta = crate::texture::Meta {
format: blade_graphics::TextureFormat::Bc1UnormSrgb,
generate_mips: true,
y_flip: false,
};
const META_NORMAL: crate::texture::Meta = crate::texture::Meta {
format: blade_graphics::TextureFormat::Bc5Snorm,
generate_mips: false,
y_flip: false,
};

fn pack4x8snorm(v: [f32; 4]) -> u32 {
v.iter().rev().fold(0u32, |u, f| {
(u << 8) | (f.clamp(-1.0, 1.0) * 127.0 + 0.5) as i8 as u8 as u32
Expand Down Expand Up @@ -335,11 +348,11 @@

fn cook(
&self,
source: &[u8],

Check warning on line 351 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `source`

Check warning on line 351 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `source`

Check warning on line 351 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `source`
extension: &str,
meta: Meta,

Check warning on line 353 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `meta`

Check warning on line 353 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `meta`

Check warning on line 353 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `meta`
cooker: Arc<blade_asset::Cooker<Self>>,

Check warning on line 354 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `cooker`

Check warning on line 354 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `cooker`

Check warning on line 354 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `cooker`
exe_context: choir::ExecutionContext,

Check warning on line 355 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `exe_context`

Check warning on line 355 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `exe_context`

Check warning on line 355 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `exe_context`
) {
match extension {
#[cfg(feature = "asset")]
Expand Down Expand Up @@ -404,12 +417,24 @@
let pbr = g_material.pbr_metallic_roughness();
model.materials.push(CookedMaterial {
base_color_path: Cow::Owned(match pbr.base_color_texture() {
Some(info) => texture_paths[info.texture().index()].as_bytes().to_vec(),
Some(info) => {
let path = &texture_paths[info.texture().index()];
if PRELOAD_TEXTURES {
self.asset_textures.load(path, META_BASE_COLOR);
}
path.as_bytes().to_vec()
}
None => Vec::new(),
}),
base_color_factor: pbr.base_color_factor(),
normal_path: Cow::Owned(match g_material.normal_texture() {
Some(info) => texture_paths[info.texture().index()].as_bytes().to_vec(),
Some(info) => {
let path = &texture_paths[info.texture().index()];
if PRELOAD_TEXTURES {
self.asset_textures.load(path, META_BASE_COLOR);
}
path.as_bytes().to_vec()
}
None => Vec::new(),
}),
transparent: g_material.alpha_mode() != gltf::material::AlphaMode::Opaque,
Expand Down Expand Up @@ -468,29 +493,15 @@
None
} else {
let path_str = str::from_utf8(&material.base_color_path).unwrap();
let (handle, task) = self.asset_textures.load(
path_str,
crate::texture::Meta {
format: blade_graphics::TextureFormat::Bc1UnormSrgb,
generate_mips: true,
y_flip: false,
},
);
let (handle, task) = self.asset_textures.load(path_str, META_BASE_COLOR);
exe_context.add_fork(&task);
Some(handle)
};
let normal_texture = if material.normal_path.is_empty() {
None
} else {
let path_str = str::from_utf8(&material.normal_path).unwrap();
let (handle, task) = self.asset_textures.load(
path_str,
crate::texture::Meta {
format: blade_graphics::TextureFormat::Bc5Snorm,
generate_mips: false,
y_flip: false,
},
);
let (handle, task) = self.asset_textures.load(path_str, META_NORMAL);
exe_context.add_fork(&task);
Some(handle)
};
Expand Down
23 changes: 20 additions & 3 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 @@ -761,13 +767,14 @@ fn main() {
winit::event::WindowEvent::CursorMoved { position, .. } => {
last_mouse_pos = [position.x as i32, position.y as i32];
if let Some(ref mut drag) = drag_start {
// This is rotation around the world UP, which is assumed to be Y
let qx = glam::Quat::from_rotation_y(
(drag.screen_pos.x - last_mouse_pos[0]) as f32 * rotate_speed,
);
let qy = glam::Quat::from_rotation_x(
(drag.screen_pos.y - last_mouse_pos[1]) as f32 * rotate_speed,
);
example.camera.rot = (drag.rotation * qx * qy).into();
example.camera.rot = (qx * drag.rotation * qy).into();
example.debug.mouse_pos = None;
}
}
Expand All @@ -778,7 +785,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
Loading