named pipelines (makes custom shaders easier)
This commit is contained in:
		
							parent
							
								
									fb4752532b
								
							
						
					
					
						commit
						7342f96174
					
				| @ -12,7 +12,6 @@ struct MyMaterial { | |||||||
| fn main() { | fn main() { | ||||||
|     AppBuilder::new() |     AppBuilder::new() | ||||||
|         .add_defaults() |         .add_defaults() | ||||||
|         .setup_world(setup) |  | ||||||
|         .setup_render_graph(|builder, pipeline_storage, shader_storage| { |         .setup_render_graph(|builder, pipeline_storage, shader_storage| { | ||||||
|             builder |             builder | ||||||
|                 .add_resource_provider(UniformResourceProvider::<MyMaterial>::new()) |                 .add_resource_provider(UniformResourceProvider::<MyMaterial>::new()) | ||||||
| @ -20,6 +19,7 @@ fn main() { | |||||||
|                     resource_name::pass::MAIN, |                     resource_name::pass::MAIN, | ||||||
|                     pipeline_storage, |                     pipeline_storage, | ||||||
|                     PipelineDescriptor::build( |                     PipelineDescriptor::build( | ||||||
|  |                         "MyMaterial", | ||||||
|                         shader_storage, |                         shader_storage, | ||||||
|                         Shader::from_glsl( |                         Shader::from_glsl( | ||||||
|                             ShaderStage::Vertex, |                             ShaderStage::Vertex, | ||||||
| @ -62,6 +62,7 @@ fn main() { | |||||||
|                     .finish(), |                     .finish(), | ||||||
|                 ) |                 ) | ||||||
|         }) |         }) | ||||||
|  |         .setup_world(setup) | ||||||
|         .run(); |         .run(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -69,13 +70,16 @@ fn setup(world: &mut World, resources: &mut Resources) { | |||||||
|     let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap(); |     let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap(); | ||||||
|     let cube_handle = mesh_storage.add(Mesh::load(MeshType::Cube)); |     let cube_handle = mesh_storage.add(Mesh::load(MeshType::Cube)); | ||||||
| 
 | 
 | ||||||
|  |     let mut pipeline_storage = resources.get_mut::<AssetStorage<PipelineDescriptor>>().unwrap(); | ||||||
|  |     let material_handle = pipeline_storage.get_named("MyMaterial").unwrap(); | ||||||
|  | 
 | ||||||
|     world |     world | ||||||
|         .build() |         .build() | ||||||
|         // cube
 |         // cube
 | ||||||
|         .add_entity(MeshMaterialEntity::<MyMaterial> { |         .add_entity(MeshMaterialEntity::<MyMaterial> { | ||||||
|             mesh: cube_handle, |             mesh: cube_handle, | ||||||
|             renderable: Renderable { |             renderable: Renderable { | ||||||
|                 pipelines: vec![Handle::new(2)], // TODO: make this pipeline assignment cleaner
 |                 pipelines: vec![material_handle], | ||||||
|                 ..Default::default() |                 ..Default::default() | ||||||
|             }, |             }, | ||||||
|             material: MyMaterial { |             material: MyMaterial { | ||||||
| @ -89,7 +93,7 @@ fn setup(world: &mut World, resources: &mut Resources) { | |||||||
|         .add_entity(MeshMaterialEntity::<MyMaterial> { |         .add_entity(MeshMaterialEntity::<MyMaterial> { | ||||||
|             mesh: cube_handle, |             mesh: cube_handle, | ||||||
|             renderable: Renderable { |             renderable: Renderable { | ||||||
|                 pipelines: vec![Handle::new(2)], // TODO: make this pipeline assignment cleaner
 |                 pipelines: vec![material_handle], | ||||||
|                 ..Default::default() |                 ..Default::default() | ||||||
|             }, |             }, | ||||||
|             material: MyMaterial { |             material: MyMaterial { | ||||||
|  | |||||||
| @ -73,7 +73,7 @@ pub trait Asset<D> { | |||||||
| 
 | 
 | ||||||
| pub struct AssetStorage<T> { | pub struct AssetStorage<T> { | ||||||
|     assets: HashMap<usize, T>, |     assets: HashMap<usize, T>, | ||||||
|     names: HashMap<String, usize>, |     names: HashMap<String, Handle<T>>, | ||||||
|     current_index: usize, |     current_index: usize, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -86,11 +86,8 @@ impl<T> AssetStorage<T> { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn get_named(&mut self, name: &str) -> Option<&mut T> { |     pub fn get_named(&mut self, name: &str) -> Option<Handle<T>> { | ||||||
|         match self.names.get(name) { |         self.names.get(name).map(|handle| *handle) | ||||||
|             Some(id) => self.assets.get_mut(id), |  | ||||||
|             None => None, |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn add(&mut self, asset: T) -> Handle<T> { |     pub fn add(&mut self, asset: T) -> Handle<T> { | ||||||
| @ -103,10 +100,8 @@ impl<T> AssetStorage<T> { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn add_named(&mut self, asset: T, name: &str) -> Handle<T> { |     pub fn set_name(&mut self, name: &str, handle: Handle<T>) { | ||||||
|         let handle = self.add(asset); |         self.names.insert(name.to_string(), handle); | ||||||
|         self.names.insert(name.to_string(), handle.id); |  | ||||||
|         handle |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn get_id(&self, id: usize) -> Option<&T> { |     pub fn get_id(&self, id: usize) -> Option<&T> { | ||||||
|  | |||||||
| @ -16,6 +16,7 @@ pub enum PipelineLayoutType { | |||||||
| 
 | 
 | ||||||
| #[derive(Clone, Debug)] | #[derive(Clone, Debug)] | ||||||
| pub struct PipelineDescriptor { | pub struct PipelineDescriptor { | ||||||
|  |     pub name: Option<String>, | ||||||
|     pub draw_targets: Vec<String>, |     pub draw_targets: Vec<String>, | ||||||
|     pub layout: PipelineLayoutType, |     pub layout: PipelineLayoutType, | ||||||
|     pub shader_stages: ShaderStages, |     pub shader_stages: ShaderStages, | ||||||
| @ -51,8 +52,9 @@ pub struct PipelineDescriptor { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl PipelineDescriptor { | impl PipelineDescriptor { | ||||||
|     fn new(vertex_shader: Handle<Shader>) -> Self { |     fn new(name: Option<&str>, vertex_shader: Handle<Shader>) -> Self { | ||||||
|         PipelineDescriptor { |         PipelineDescriptor { | ||||||
|  |             name: name.map(|name| name.to_string()), | ||||||
|             layout: PipelineLayoutType::Reflected(None), |             layout: PipelineLayoutType::Reflected(None), | ||||||
|             color_states: Vec::new(), |             color_states: Vec::new(), | ||||||
|             depth_stencil_state: None, |             depth_stencil_state: None, | ||||||
| @ -90,11 +92,12 @@ impl PipelineDescriptor { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl PipelineDescriptor { | impl PipelineDescriptor { | ||||||
|     pub fn build( |     pub fn build<'a>( | ||||||
|         shader_storage: &mut AssetStorage<Shader>, |         name: &str, | ||||||
|  |         shader_storage: &'a mut AssetStorage<Shader>, | ||||||
|         vertex_shader: Shader, |         vertex_shader: Shader, | ||||||
|     ) -> PipelineBuilder { |     ) -> PipelineBuilder<'a> { | ||||||
|         PipelineBuilder::new(shader_storage, vertex_shader) |         PipelineBuilder::new(name, shader_storage, vertex_shader) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -104,10 +107,10 @@ pub struct PipelineBuilder<'a> { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<'a> PipelineBuilder<'a> { | impl<'a> PipelineBuilder<'a> { | ||||||
|     pub fn new(shader_storage: &'a mut AssetStorage<Shader>, vertex_shader: Shader) -> Self { |     pub fn new(name: &str, shader_storage: &'a mut AssetStorage<Shader>, vertex_shader: Shader) -> Self { | ||||||
|         let vertex_shader_handle = shader_storage.add(vertex_shader); |         let vertex_shader_handle = shader_storage.add(vertex_shader); | ||||||
|         PipelineBuilder { |         PipelineBuilder { | ||||||
|             pipeline: PipelineDescriptor::new(vertex_shader_handle), |             pipeline: PipelineDescriptor::new(Some(name), vertex_shader_handle), | ||||||
|             shader_storage, |             shader_storage, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -25,6 +25,7 @@ impl ForwardPipelineBuilder for RenderGraphBuilder { | |||||||
|         self.add_pipeline( |         self.add_pipeline( | ||||||
|             pipeline_descriptor_storage, |             pipeline_descriptor_storage, | ||||||
|             PipelineDescriptor::build( |             PipelineDescriptor::build( | ||||||
|  |                 resource_name::pipeline::FORWARD, | ||||||
|                 shader_storage, |                 shader_storage, | ||||||
|                 Shader::from_glsl(ShaderStage::Vertex, include_str!("forward.vert")), |                 Shader::from_glsl(ShaderStage::Vertex, include_str!("forward.vert")), | ||||||
|             ) |             ) | ||||||
|  | |||||||
| @ -26,6 +26,7 @@ impl ForwardFlatPipelineBuilder for RenderGraphBuilder { | |||||||
|         self.add_pipeline( |         self.add_pipeline( | ||||||
|             pipeline_descriptor_storage, |             pipeline_descriptor_storage, | ||||||
|             PipelineDescriptor::build( |             PipelineDescriptor::build( | ||||||
|  |                 resource_name::pipeline::FORWARD_FLAT, | ||||||
|                 shader_storage, |                 shader_storage, | ||||||
|                 Shader::from_glsl(ShaderStage::Vertex, include_str!("forward_flat.vert")), |                 Shader::from_glsl(ShaderStage::Vertex, include_str!("forward_flat.vert")), | ||||||
|             ) |             ) | ||||||
|  | |||||||
| @ -28,6 +28,7 @@ impl UiPipelineBuilder for RenderGraphBuilder { | |||||||
|         self.add_pipeline( |         self.add_pipeline( | ||||||
|             pipeline_descriptor_storage, |             pipeline_descriptor_storage, | ||||||
|             PipelineDescriptor::build( |             PipelineDescriptor::build( | ||||||
|  |                 resource_name::pipeline::UI, | ||||||
|                 shader_storage, |                 shader_storage, | ||||||
|                 Shader::from_glsl(ShaderStage::Vertex, include_str!("ui.vert")), |                 Shader::from_glsl(ShaderStage::Vertex, include_str!("ui.vert")), | ||||||
|             ) |             ) | ||||||
|  | |||||||
| @ -34,7 +34,9 @@ impl RenderGraphBuilder { | |||||||
|         pipeline: PipelineDescriptor, |         pipeline: PipelineDescriptor, | ||||||
|     ) -> Self { |     ) -> Self { | ||||||
|         if let Some(ref pass) = self.current_pass { |         if let Some(ref pass) = self.current_pass { | ||||||
|  |             let name = pipeline.name.clone(); | ||||||
|             let pipeline_descriptor_handle = pipeline_descriptor_storage.add(pipeline); |             let pipeline_descriptor_handle = pipeline_descriptor_storage.add(pipeline); | ||||||
|  |             pipeline_descriptor_storage.set_name(name.unwrap().as_str(), pipeline_descriptor_handle); | ||||||
|             self.render_graph |             self.render_graph | ||||||
|                 .add_pipeline(&pass, pipeline_descriptor_handle); |                 .add_pipeline(&pass, pipeline_descriptor_handle); | ||||||
|         } |         } | ||||||
| @ -48,7 +50,9 @@ impl RenderGraphBuilder { | |||||||
|         pipeline_descriptor_storage: &mut AssetStorage<PipelineDescriptor>, |         pipeline_descriptor_storage: &mut AssetStorage<PipelineDescriptor>, | ||||||
|         pipeline: PipelineDescriptor, |         pipeline: PipelineDescriptor, | ||||||
|     ) -> Self { |     ) -> Self { | ||||||
|  |         let name = pipeline.name.clone(); | ||||||
|         let pipeline_descriptor_handle = pipeline_descriptor_storage.add(pipeline); |         let pipeline_descriptor_handle = pipeline_descriptor_storage.add(pipeline); | ||||||
|  |         pipeline_descriptor_storage.set_name(name.unwrap().as_str(), pipeline_descriptor_handle); | ||||||
|         self.render_graph |         self.render_graph | ||||||
|             .add_pipeline(pass, pipeline_descriptor_handle); |             .add_pipeline(pass, pipeline_descriptor_handle); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -24,3 +24,9 @@ pub mod draw_target { | |||||||
| pub mod pass { | pub mod pass { | ||||||
|     pub const MAIN: &str = "Main"; |     pub const MAIN: &str = "Main"; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | pub mod pipeline { | ||||||
|  |     pub const FORWARD: &str = "Forward"; | ||||||
|  |     pub const FORWARD_FLAT: &str = "ForwardFlat"; | ||||||
|  |     pub const UI: &str = "Ui"; | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Carter Anderson
						Carter Anderson