From 13d1d5214d7d7043f22dee0837fd6600aaa50797 Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:23 +0100 Subject: refactor(all): various cleanup --- stockton-render/src/buffers/mod.rs | 2 +- stockton-render/src/builders/pipeline.rs | 4 ++-- stockton-render/src/context.rs | 2 +- stockton-render/src/draw_passes/util.rs | 2 +- stockton-render/src/error.rs | 1 - stockton-render/src/queue_negotiator.rs | 24 ++++++++++++++---------- stockton-render/src/texture/image.rs | 28 +++++++++++++++------------- stockton-render/src/texture/loader.rs | 15 +++++++-------- stockton-render/src/texture/repo.rs | 6 ++---- 9 files changed, 43 insertions(+), 41 deletions(-) (limited to 'stockton-render') 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 { 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 TargetSpecificResources { } /// 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>; + /// Used to find appropriate queue families and share queues from them as needed. pub struct QueueNegotiator { family_ids: HashMap, - already_allocated: HashMap>>, usize)>, + already_allocated: HashMap, usize)>, all: Vec, } @@ -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(&mut self, adapter: &Adapter, filter: &T) -> Result<()> { if self.family_ids.contains_key(&TypeId::of::()) { return Ok(()); @@ -113,7 +107,7 @@ impl QueueNegotiator { pub fn family_spec<'a, T: QueueFamilySelector>( &self, - queue_families: &'a Vec, + queue_families: &'a [QueueFamilyT], count: usize, ) -> Option<(&'a QueueFamilyT, Vec)> { let qf_id = self.family::()?; @@ -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 = 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 TextureLoader { pub fn new( adapter: &Adapter, device_lock: Arc>, - family: QueueFamilyId, - queue_lock: Arc>, + (family, queue_lock): (QueueFamilyId, Arc>), ds_layout: Arc>, - request_channel: Receiver, - return_channel: Sender>, + (request_channel, return_channel): ( + Receiver, + Sender>, + ), config: TextureLoadConfig, ) -> Result { let mut device = device_lock @@ -300,8 +301,7 @@ impl TextureLoader { &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 TextureLoader { device: &mut DeviceT, buf: &mut CommandBufferT, queue_lock: &Arc>, - staging_allocator: &mut DynamicAllocator, - tex_allocator: &mut DynamicAllocator, + (staging_allocator, tex_allocator): (&mut DynamicAllocator, &mut DynamicAllocator), staging_memory_type: MemoryTypeId, obcpa: u64, config: &TextureLoadConfig, 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, )?; -- cgit v1.2.3