diff options
-rw-r--r-- | examples/render-quad/src/main.rs | 9 | ||||
-rw-r--r-- | stockton-levels/src/parts/vertices.rs | 3 | ||||
-rw-r--r-- | stockton-passes/src/ui.rs | 22 | ||||
-rw-r--r-- | stockton-passes/src/window.rs | 46 | ||||
-rw-r--r-- | stockton-render/src/buffers/mod.rs | 2 | ||||
-rw-r--r-- | stockton-render/src/builders/pipeline.rs | 4 | ||||
-rw-r--r-- | stockton-render/src/context.rs | 2 | ||||
-rw-r--r-- | stockton-render/src/draw_passes/util.rs | 2 | ||||
-rw-r--r-- | stockton-render/src/error.rs | 1 | ||||
-rw-r--r-- | stockton-render/src/queue_negotiator.rs | 24 | ||||
-rw-r--r-- | stockton-render/src/texture/image.rs | 28 | ||||
-rw-r--r-- | stockton-render/src/texture/loader.rs | 15 | ||||
-rw-r--r-- | stockton-render/src/texture/repo.rs | 6 |
13 files changed, 88 insertions, 76 deletions
diff --git a/examples/render-quad/src/main.rs b/examples/render-quad/src/main.rs index 0cc8b83..6e192dc 100644 --- a/examples/render-quad/src/main.rs +++ b/examples/render-quad/src/main.rs @@ -80,7 +80,7 @@ fn main() { } } -fn try_main<'a>() -> Result<()> { +fn try_main() -> Result<()> { // Initialise logger simplelog::TermLogger::init( log::LevelFilter::Debug, @@ -134,7 +134,7 @@ fn try_main<'a>() -> Result<()> { })); // Create the UI State - let ui = UiState::new(); + let ui = UiState::default(); // Create the input manager let manager = { @@ -196,10 +196,7 @@ fn try_main<'a>() -> Result<()> { ( LevelDrawPassConfig { active_camera: player, - tex_resolver: FsResolver::new( - Path::new("./examples/render-quad/textures"), - map.clone(), - ), + tex_resolver: FsResolver::new(Path::new("./examples/render-quad/textures"), map), }, (), ), diff --git a/stockton-levels/src/parts/vertices.rs b/stockton-levels/src/parts/vertices.rs index f2f5a2d..d797afd 100644 --- a/stockton-levels/src/parts/vertices.rs +++ b/stockton-levels/src/parts/vertices.rs @@ -42,8 +42,7 @@ impl<'de> Deserialize<'de> for Vertex { TexV, Color, } - const FIELDS: &[&str] = - &["pos_x", "pos_y", "pos_z", "tex_x", "tex_y", "color"]; + const FIELDS: &[&str] = &["pos_x", "pos_y", "pos_z", "tex_x", "tex_y", "color"]; struct VertexVisitor; diff --git a/stockton-passes/src/ui.rs b/stockton-passes/src/ui.rs index 4b4e1c2..bc1ba69 100644 --- a/stockton-passes/src/ui.rs +++ b/stockton-passes/src/ui.rs @@ -117,7 +117,9 @@ impl<'a> DrawPass for UiDrawPass<'a> { } let (_out, shapes) = ui.end_frame(); - let screen = ui.dimensions().ok_or(anyhow!("UI not set up properly."))?; + let screen = ui + .dimensions() + .ok_or_else(|| anyhow!("UI not set up properly."))?; let shapes = ui.ctx().tessellate(shapes); for ClippedMesh(rect, tris) in shapes.iter() { @@ -376,23 +378,15 @@ impl LoadableImage for UiTexture { fn height(&self) -> u32 { self.0.height as u32 } - fn copy_row(&self, y: u32, ptr: *mut u8) { + unsafe fn copy_row(&self, y: u32, ptr: *mut u8) { let row_size = self.0.width as u32; let pixels = &self.0.pixels[(y * row_size) as usize..((y + 1) * row_size) as usize]; for (i, x) in pixels.iter().enumerate() { - unsafe { - *ptr.offset(i as isize * 4) = 255; - *ptr.offset((i as isize * 4) + 1) = 255; - *ptr.offset((i as isize * 4) + 2) = 255; - *ptr.offset((i as isize * 4) + 3) = *x; - } - } - } - - unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) { - for y in 0..self.height() { - self.copy_row(y, ptr.offset((row_size * y as usize).try_into().unwrap())); + *ptr.offset(i as isize * 4) = 255; + *ptr.offset((i as isize * 4) + 1) = 255; + *ptr.offset((i as isize * 4) + 2) = 255; + *ptr.offset((i as isize * 4) + 3) = *x; } } } diff --git a/stockton-passes/src/window.rs b/stockton-passes/src/window.rs index c806fa0..46ba1a1 100644 --- a/stockton-passes/src/window.rs +++ b/stockton-passes/src/window.rs @@ -84,17 +84,7 @@ pub struct UiState { } impl UiState { - pub fn new() -> Self { - UiState { - ctx: CtxRef::default(), - raw_input: RawInput::default(), - frame_active: false, - modifiers: Default::default(), - pointer_pos: Pos2::new(0.0, 0.0), - } - } - - pub fn populate_initial_state<'a, T: DrawPass>(&mut self, renderer: &Renderer<T>) { + pub fn populate_initial_state<T: DrawPass>(&mut self, renderer: &Renderer<T>) { let props = &renderer.context().target_chain().properties(); self.set_dimensions(props.extent.width, props.extent.height); self.set_pixels_per_point(Some(renderer.context().pixels_per_point())); @@ -163,19 +153,49 @@ impl UiState { fn handle_action(&mut self, action: KBAction) { // TODO match action { - KBAction::MousePress(stockton_input::MouseButton::Left) => { + KBAction::MousePress(btn) => { self.raw_input.events.push(Event::PointerButton { pos: self.pointer_pos, - button: egui::PointerButton::Primary, + button: match btn { + stockton_input::MouseButton::Left => egui::PointerButton::Primary, + stockton_input::MouseButton::Right => egui::PointerButton::Secondary, + stockton_input::MouseButton::Middle => egui::PointerButton::Middle, + stockton_input::MouseButton::Other(_) => todo!(), + }, pressed: true, modifiers: self.modifiers, }); } + KBAction::MouseRelease(btn) => { + self.raw_input.events.push(Event::PointerButton { + pos: self.pointer_pos, + button: match btn { + stockton_input::MouseButton::Left => egui::PointerButton::Primary, + stockton_input::MouseButton::Right => egui::PointerButton::Secondary, + stockton_input::MouseButton::Middle => egui::PointerButton::Middle, + stockton_input::MouseButton::Other(_) => todo!(), + }, + pressed: false, + modifiers: self.modifiers, + }); + } _ => (), } } } +impl Default for UiState { + fn default() -> Self { + UiState { + ctx: CtxRef::default(), + raw_input: RawInput::default(), + frame_active: false, + modifiers: Default::default(), + pointer_pos: Pos2::new(0.0, 0.0), + } + } +} + pub struct WindowFlow { window_events: Receiver<WindowEvent>, update_control_flow: Arc<RwLock<ControlFlow>>, diff --git a/stockton-render/src/buffers/mod.rs b/stockton-render/src/buffers/mod.rs index 82ca62a..74c5aab 100644 --- a/stockton-render/src/buffers/mod.rs +++ b/stockton-render/src/buffers/mod.rs @@ -59,5 +59,5 @@ pub trait ModifiableBuffer: IndexMut<usize> { fn get_buffer(&mut self) -> &BufferT; /// Record the command(s) required to commit changes to this buffer to the given command buffer. - fn record_commit_cmds<'a>(&'a mut self, cmd_buffer: &mut CommandBufferT) -> Result<()>; + fn record_commit_cmds(&mut self, cmd_buffer: &mut CommandBufferT) -> Result<()>; } diff --git a/stockton-render/src/builders/pipeline.rs b/stockton-render/src/builders/pipeline.rs index 25a4dc6..f68d9d6 100644 --- a/stockton-render/src/builders/pipeline.rs +++ b/stockton-render/src/builders/pipeline.rs @@ -275,10 +275,10 @@ impl CompletePipeline { if let Some(x) = self.gm_module.take() { device.destroy_shader_module(x) } - self.ts_module.take().map(|(a, b)| { + if let Some((a, b)) = self.ts_module.take() { device.destroy_shader_module(a); device.destroy_shader_module(b); - }); + } device.destroy_graphics_pipeline(ManuallyDrop::into_inner(read(&self.pipeline))); diff --git a/stockton-render/src/context.rs b/stockton-render/src/context.rs index 3985c47..802b8ca 100644 --- a/stockton-render/src/context.rs +++ b/stockton-render/src/context.rs @@ -77,7 +77,7 @@ impl RenderingContext { let (mut queue_negotiator, surface) = { let dq: DrawQueue = DrawQueue { surface }; - let mut qn = QueueNegotiator::new(); + let mut qn = QueueNegotiator::default(); // Draw Queue qn.find(&adapter, &dq) diff --git a/stockton-render/src/draw_passes/util.rs b/stockton-render/src/draw_passes/util.rs index 5a4eb1a..7e82209 100644 --- a/stockton-render/src/draw_passes/util.rs +++ b/stockton-render/src/draw_passes/util.rs @@ -27,7 +27,7 @@ impl<T> TargetSpecificResources<T> { } /// Get the next resource, wrapping around if necessary. - pub fn get_next<'a>(&'a mut self) -> &'a T { + pub fn get_next(&mut self) -> &T { let el = &self.elements[self.next_idx]; self.next_idx = (self.next_idx + 1) % self.elements.len(); el diff --git a/stockton-render/src/error.rs b/stockton-render/src/error.rs index 6c895eb..1f57892 100644 --- a/stockton-render/src/error.rs +++ b/stockton-render/src/error.rs @@ -1,6 +1,5 @@ //! Error types -use anyhow; use thiserror::Error; #[derive(Error, Debug)] diff --git a/stockton-render/src/queue_negotiator.rs b/stockton-render/src/queue_negotiator.rs index 65c7aa4..879a935 100644 --- a/stockton-render/src/queue_negotiator.rs +++ b/stockton-render/src/queue_negotiator.rs @@ -8,10 +8,12 @@ use std::{ sync::{Arc, RwLock}, }; +type SharedQueue = Arc<RwLock<QueueT>>; + /// Used to find appropriate queue families and share queues from them as needed. pub struct QueueNegotiator { family_ids: HashMap<TypeId, QueueFamilyId>, - already_allocated: HashMap<TypeId, (Vec<Arc<RwLock<QueueT>>>, usize)>, + already_allocated: HashMap<TypeId, (Vec<SharedQueue>, usize)>, all: Vec<QueueGroup>, } @@ -22,14 +24,6 @@ pub trait QueueFamilySelector: 'static { } impl QueueNegotiator { - pub fn new() -> Self { - QueueNegotiator { - family_ids: HashMap::new(), - already_allocated: HashMap::new(), - all: vec![], - } - } - pub fn find<T: QueueFamilySelector>(&mut self, adapter: &Adapter, filter: &T) -> Result<()> { if self.family_ids.contains_key(&TypeId::of::<T>()) { return Ok(()); @@ -113,7 +107,7 @@ impl QueueNegotiator { pub fn family_spec<'a, T: QueueFamilySelector>( &self, - queue_families: &'a Vec<QueueFamilyT>, + queue_families: &'a [QueueFamilyT], count: usize, ) -> Option<(&'a QueueFamilyT, Vec<f32>)> { let qf_id = self.family::<T>()?; @@ -125,6 +119,16 @@ impl QueueNegotiator { } } +impl Default for QueueNegotiator { + fn default() -> Self { + QueueNegotiator { + family_ids: HashMap::new(), + already_allocated: HashMap::new(), + all: vec![], + } + } +} + pub struct DrawQueue { pub surface: SurfaceT, } diff --git a/stockton-render/src/texture/image.rs b/stockton-render/src/texture/image.rs index 0e272e9..f984b72 100644 --- a/stockton-render/src/texture/image.rs +++ b/stockton-render/src/texture/image.rs @@ -9,8 +9,19 @@ use image::RgbaImage; pub trait LoadableImage { fn width(&self) -> u32; fn height(&self) -> u32; - fn copy_row(&self, y: u32, ptr: *mut u8); - unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize); + + /// # Safety + /// Ensure the ptr is at least width() * PIXEL_SIZE bytes. + unsafe fn copy_row(&self, y: u32, ptr: *mut u8); + + /// # Safety + /// Ensure the ptr is at least row_size * height() * PIXEL_SIZE bytes. + unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) { + for y in 0..self.height() as usize { + let dest_base: isize = (y * row_size).try_into().unwrap(); + self.copy_row(y as u32, ptr.offset(dest_base)); + } + } } impl LoadableImage for RgbaImage { @@ -22,20 +33,11 @@ impl LoadableImage for RgbaImage { self.height() } - fn copy_row(&self, y: u32, ptr: *mut u8) { + unsafe fn copy_row(&self, y: u32, ptr: *mut u8) { let row_size_bytes = self.width() as usize * PIXEL_SIZE; let raw: &Vec<u8> = self.as_raw(); let row = &raw[y as usize * row_size_bytes..(y as usize + 1) * row_size_bytes]; - unsafe { - copy_nonoverlapping(row.as_ptr(), ptr, row.len()); - } - } - - unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) { - for y in 0..self.height() as usize { - let dest_base: isize = (y * row_size).try_into().unwrap(); - self.copy_row(y as u32, ptr.offset(dest_base)); - } + copy_nonoverlapping(row.as_ptr(), ptr, row.len()); } } diff --git a/stockton-render/src/texture/loader.rs b/stockton-render/src/texture/loader.rs index f9c643c..5c85fd3 100644 --- a/stockton-render/src/texture/loader.rs +++ b/stockton-render/src/texture/loader.rs @@ -184,11 +184,12 @@ impl<R: TextureResolver> TextureLoader<R> { pub fn new( adapter: &Adapter, device_lock: Arc<RwLock<DeviceT>>, - family: QueueFamilyId, - queue_lock: Arc<RwLock<QueueT>>, + (family, queue_lock): (QueueFamilyId, Arc<RwLock<QueueT>>), ds_layout: Arc<RwLock<DescriptorSetLayoutT>>, - request_channel: Receiver<LoaderRequest>, - return_channel: Sender<TexturesBlock<DynamicBlock>>, + (request_channel, return_channel): ( + Receiver<LoaderRequest>, + Sender<TexturesBlock<DynamicBlock>>, + ), config: TextureLoadConfig<R>, ) -> Result<Self> { let mut device = device_lock @@ -300,8 +301,7 @@ impl<R: TextureResolver> TextureLoader<R> { &mut device, &mut buffers[0].1, &queue_lock, - &mut staging_allocator, - &mut tex_allocator, + (&mut staging_allocator, &mut tex_allocator), staging_memory_type, optimal_buffer_copy_pitch_alignment, &config, @@ -535,8 +535,7 @@ impl<R: TextureResolver> TextureLoader<R> { device: &mut DeviceT, buf: &mut CommandBufferT, queue_lock: &Arc<RwLock<QueueT>>, - staging_allocator: &mut DynamicAllocator, - tex_allocator: &mut DynamicAllocator, + (staging_allocator, tex_allocator): (&mut DynamicAllocator, &mut DynamicAllocator), staging_memory_type: MemoryTypeId, obcpa: u64, config: &TextureLoadConfig<R>, diff --git a/stockton-render/src/texture/repo.rs b/stockton-render/src/texture/repo.rs index e29625b..341d355 100644 --- a/stockton-render/src/texture/repo.rs +++ b/stockton-render/src/texture/repo.rs @@ -94,11 +94,9 @@ impl TextureRepo { let loader = TextureLoader::new( adapter, device_lock.clone(), - family, - queue, + (family, queue), ds_lock.clone(), - req_recv, - resp_send, + (req_recv, resp_send), config, )?; |