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