diff --git a/src/core/render/resources/pipeline/loader.rs b/src/core/render/resources/pipeline/loader.rs index c20a1de..9719c8c 100644 --- a/src/core/render/resources/pipeline/loader.rs +++ b/src/core/render/resources/pipeline/loader.rs @@ -7,15 +7,14 @@ use std::{ use vulkano::{device::Device, format::Format, pipeline::GraphicsPipeline}; +use super::{GraphicsPipelineLoadFn, LoadableGraphicsPipeline}; + #[derive(PartialEq, Eq)] pub enum PipelineState { NeedBuild, Loaded, } -pub type GraphicsPipelineLoadFn = - fn(&Arc, Format, Format) -> Result, Box>; - pub struct PipelineLoader { device: Arc, swapchain_image_format: Format, @@ -29,6 +28,7 @@ pub struct PipelineLoader { // But only the pipeline loader is allowed to load a pipeline when it's needed. pipelines: Vec>>>>, pipelines_state: Vec>>, + pipelines_name: Vec<&'static str>, } impl PipelineLoader { @@ -43,20 +43,21 @@ impl PipelineLoader { depth_image_format, pipelines: Vec::new(), pipelines_load_fn: Vec::new(), + pipelines_name: Vec::new(), pipelines_id: Vec::new(), pipelines_state: Vec::new(), pipelines_index: HashMap::new(), } } - pub fn register( + pub fn register( &mut self, - load_fn: GraphicsPipelineLoadFn, ) -> Result<(), Box> { let id = TypeId::of::(); self.pipelines_index.insert(id, self.pipelines.len()); self.pipelines_id.push(id); - self.pipelines_load_fn.push(load_fn); + self.pipelines_load_fn.push(T::load); + self.pipelines_name.push(T::pipeline_name()); self.pipelines_state .push(Arc::new(RwLock::new(PipelineState::NeedBuild))); self.pipelines.push(Arc::new(RwLock::new(None))); @@ -65,7 +66,7 @@ impl PipelineLoader { pub fn load_pipelines(&self) -> Result<(), Box> { let iter = self - .pipelines_id + .pipelines_name .iter() .zip(self.pipelines.iter()) .zip(self.pipelines_load_fn.iter()) @@ -75,7 +76,7 @@ impl PipelineLoader { *state == PipelineState::NeedBuild }); - for (((id, pipeline), load_fn), state) in iter { + for (((name, pipeline), load_fn), state) in iter { let new_pipeline = load_fn( &self.device, self.swapchain_image_format, @@ -85,7 +86,7 @@ impl PipelineLoader { *pipeline = Some(new_pipeline); let mut state = state.write().unwrap(); *state = PipelineState::Loaded; - tracing::trace!("Pipeline {id:?} loaded"); + tracing::trace!("Pipeline {name} loaded"); } Ok(()) } diff --git a/src/core/render/resources/pipeline/mod.rs b/src/core/render/resources/pipeline/mod.rs index 3026927..9fa9c79 100644 --- a/src/core/render/resources/pipeline/mod.rs +++ b/src/core/render/resources/pipeline/mod.rs @@ -1,2 +1,18 @@ mod loader; -pub use loader::{GraphicsPipelineLoadFn, PipelineLoader}; +use std::{error::Error, sync::Arc}; + +pub use loader::PipelineLoader; +use vulkano::{device::Device, format::Format, pipeline::GraphicsPipeline}; + +type GraphicsPipelineLoadFn = + fn(&Arc, Format, Format) -> Result, Box>; + +pub trait LoadableGraphicsPipeline { + fn load( + device: &Arc, + swapchain_image_format: Format, + depth_image_format: Format, + ) -> Result, Box>; + + fn pipeline_name() -> &'static str; +} diff --git a/src/game/assets/pipelines/simple.rs b/src/game/assets/pipelines/simple.rs index 0e9cfcd..3f3a4db 100644 --- a/src/game/assets/pipelines/simple.rs +++ b/src/game/assets/pipelines/simple.rs @@ -33,13 +33,13 @@ use crate::core::render::{ AsDescriptorSet, AsDescriptorSetLayoutBindings, AsRecordable, AsRenderableMesh, AsRenderableMeshInstance, mvp::Mvp, transform::TransformRaw, vertex::Vertex3D, }, - resources::texture::Texture, + resources::{pipeline::LoadableGraphicsPipeline, texture::Texture}, }; pub struct SimplePipeline; -impl SimplePipeline { - pub fn new( +impl LoadableGraphicsPipeline for SimplePipeline { + fn load( device: &Arc, swapchain_format: Format, depth_format: Format, @@ -114,6 +114,10 @@ impl SimplePipeline { Ok(pipeline) } + + fn pipeline_name() -> &'static str { + "SimplePipeline" + } } impl AsRecordable for SimplePipeline { diff --git a/src/game/scenes/main_scene.rs b/src/game/scenes/main_scene.rs index 4f0b421..67ea2e0 100644 --- a/src/game/scenes/main_scene.rs +++ b/src/game/scenes/main_scene.rs @@ -61,7 +61,7 @@ impl Scene for MainScene { swapchain_image_view.format(), depth_image_view.format(), ); - pipeline_loader.register::(SimplePipeline::new)?; + pipeline_loader.register::()?; pipeline_loader.load_pipelines()?; let mut texture_loader = TextureLoader::new(app_context);