From d7d0f0277c437004ed476393542da39c84c5cf9f Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:22 +0100 Subject: chore(render): update hal and fix some errors --- stockton-render/src/draw/texture/repo.rs | 75 ++++++++++++++------------------ 1 file changed, 32 insertions(+), 43 deletions(-) (limited to 'stockton-render/src/draw/texture/repo.rs') diff --git a/stockton-render/src/draw/texture/repo.rs b/stockton-render/src/draw/texture/repo.rs index 2316dc4..c37da11 100644 --- a/stockton-render/src/draw/texture/repo.rs +++ b/stockton-render/src/draw/texture/repo.rs @@ -6,12 +6,14 @@ use super::{ resolver::TextureResolver, LoadableImage, }; +use crate::error::LockPoisoned; use crate::types::*; use std::{ + array::IntoIter, collections::HashMap, + iter::empty, marker::PhantomData, - mem::drop, mem::ManuallyDrop, sync::{ mpsc::{channel, Receiver, Sender}, @@ -22,12 +24,10 @@ use std::{ use anyhow::{Context, Result}; use hal::{ - prelude::*, - pso::{DescriptorSetLayoutBinding, DescriptorType, ShaderStageFlags}, - Features, + pso::{DescriptorSetLayoutBinding, DescriptorType, ImageDescriptorType, ShaderStageFlags}, + queue::family::QueueFamilyId, }; use log::debug; -use thiserror::Error; /// The number of textures in one 'block' /// The textures of the loaded file are divided into blocks of this size. @@ -36,7 +36,7 @@ pub const BLOCK_SIZE: usize = 8; pub struct TextureRepo<'a> { joiner: ManuallyDrop>>, - ds_layout: Arc>, + ds_layout: Arc>, req_send: Sender, resp_recv: Receiver>, blocks: HashMap>>, @@ -44,55 +44,43 @@ pub struct TextureRepo<'a> { _a: PhantomData<&'a ()>, } -#[derive(Error, Debug)] -pub enum TextureRepoError { - #[error("No suitable queue family")] - NoQueueFamilies, - - #[error("Lock poisoned")] - LockPoisoned, -} - impl<'a> TextureRepo<'a> { + pub fn queue_family_filter(family: &&QueueFamilyT) -> bool { + family.queue_type().supports_transfer() && family.max_queues() >= NUM_SIMULTANEOUS_CMDS + } + pub fn new< T: 'static + HasTextures + Send + Sync, R: 'static + TextureResolver + Send + Sync, I: 'static + LoadableImage + Send, >( - device_lock: Arc>, + device_lock: Arc>, + family: QueueFamilyId, + queue: Arc>, adapter: &Adapter, texs_lock: Arc>, resolver: R, ) -> Result { + // Create Channels let (req_send, req_recv) = channel(); let (resp_send, resp_recv) = channel(); - let family = adapter - .queue_families - .iter() - .find(|family| { - family.queue_type().supports_transfer() - && family.max_queues() >= NUM_SIMULTANEOUS_CMDS - }) - .ok_or(TextureRepoError::NoQueueFamilies)?; - - let gpu = unsafe { - adapter - .physical_device - .open(&[(family, &[1.0])], Features::empty())? - }; - let device = device_lock .write() - .map_err(|_| TextureRepoError::LockPoisoned) + .map_err(|_| LockPoisoned::Device) .context("Error getting device lock")?; + // Create descriptor set layout let ds_lock = Arc::new(RwLock::new( unsafe { device.create_descriptor_set_layout( - &[ + IntoIter::new([ DescriptorSetLayoutBinding { binding: 0, - ty: DescriptorType::SampledImage, + ty: DescriptorType::Image { + ty: ImageDescriptorType::Sampled { + with_sampler: false, + }, + }, count: BLOCK_SIZE, stage_flags: ShaderStageFlags::FRAGMENT, immutable_samplers: false, @@ -104,22 +92,23 @@ impl<'a> TextureRepo<'a> { stage_flags: ShaderStageFlags::FRAGMENT, immutable_samplers: false, }, - ], - &[], + ]), + empty(), ) } - .map_err::(|e| e.into()) .context("Error creating descriptor set layout")?, )); + debug!("Created descriptor set layout {:?}", ds_lock); + drop(device); let joiner = { let loader = TextureLoader::new( - device_lock, adapter, - family.id(), - gpu, + device_lock.clone(), + family, + queue, ds_lock.clone(), req_recv, resp_send, @@ -140,7 +129,7 @@ impl<'a> TextureRepo<'a> { }) } - pub fn get_ds_layout(&self) -> RwLockReadGuard { + pub fn get_ds_layout(&self) -> RwLockReadGuard { self.ds_layout.read().unwrap() } @@ -162,7 +151,7 @@ impl<'a> TextureRepo<'a> { Ok(()) } - pub fn attempt_get_descriptor_set(&mut self, block_id: BlockRef) -> Option<&DescriptorSet> { + pub fn attempt_get_descriptor_set(&mut self, block_id: BlockRef) -> Option<&DescriptorSetT> { self.blocks .get(&block_id) .and_then(|opt| opt.as_ref().map(|z| z.descriptor_set.raw())) @@ -176,7 +165,7 @@ impl<'a> TextureRepo<'a> { } } - pub fn deactivate(mut self, device_lock: &mut Arc>) { + pub fn deactivate(mut self, device_lock: &mut Arc>) { unsafe { use std::ptr::read; -- cgit v1.2.3