render: add RenderPipeline and begin moving logic there

This commit is contained in:
Carter Anderson 2020-06-15 00:08:50 -07:00
parent 97edaa7343
commit f799d3ac93
17 changed files with 229 additions and 230 deletions

View File

@ -1,8 +1,8 @@
use crate::{light::Light, material::StandardMaterial}; use crate::{light::Light, material::StandardMaterial};
use bevy_asset::Handle; use bevy_asset::Handle;
use bevy_derive::EntityArchetype; use bevy_derive::EntityArchetype;
use bevy_render::{mesh::Mesh, draw::{RenderPipelines, Draw}}; use bevy_render::{draw::Draw, mesh::Mesh, pipeline::RenderPipelines};
use bevy_transform::prelude::{Transform, Rotation, Scale, Translation}; use bevy_transform::prelude::{Rotation, Scale, Transform, Translation};
#[derive(EntityArchetype, Default)] #[derive(EntityArchetype, Default)]
pub struct MeshEntity { pub struct MeshEntity {

View File

@ -58,26 +58,6 @@ impl Default for Draw {
} }
} }
#[derive(Properties)]
pub struct RenderPipelines {
pub pipelines: Vec<Handle<PipelineDescriptor>>,
// TODO: make these pipeline specific
#[property(ignore)]
pub render_resource_bindings: RenderResourceBindings,
#[property(ignore)]
pub compiled_pipelines: Vec<Handle<PipelineDescriptor>>,
}
impl Default for RenderPipelines {
fn default() -> Self {
Self {
render_resource_bindings: Default::default(),
compiled_pipelines: Default::default(),
pipelines: vec![Handle::default()],
}
}
}
impl Draw { impl Draw {
pub fn get_context<'a>( pub fn get_context<'a>(
&'a mut self, &'a mut self,
@ -201,54 +181,6 @@ pub trait Drawable {
fn draw(&mut self, draw: &mut DrawContext) -> Result<(), DrawError>; fn draw(&mut self, draw: &mut DrawContext) -> Result<(), DrawError>;
} }
impl Drawable for RenderPipelines {
fn draw(&mut self, draw: &mut DrawContext) -> Result<(), DrawError> {
for pipeline_handle in self.compiled_pipelines.iter() {
let pipeline = draw.pipelines.get(pipeline_handle).unwrap();
let layout = pipeline.get_layout().unwrap();
draw.set_pipeline(*pipeline_handle)?;
for bind_group_descriptor in layout.bind_groups.iter() {
if let Some(local_bind_group) = self
.render_resource_bindings
.get_descriptor_bind_group(bind_group_descriptor.id)
{
draw.set_bind_group(bind_group_descriptor, local_bind_group);
} else if let Some(global_bind_group) = draw
.render_resource_bindings
.get_descriptor_bind_group(bind_group_descriptor.id)
{
draw.set_bind_group(bind_group_descriptor, global_bind_group);
}
}
let mut indices = 0..0;
for (slot, vertex_buffer_descriptor) in
layout.vertex_buffer_descriptors.iter().enumerate()
{
if let Some((vertex_buffer, index_buffer)) = self
.render_resource_bindings
.get_vertex_buffer(&vertex_buffer_descriptor.name)
{
draw.set_vertex_buffer(slot as u32, vertex_buffer, 0);
if let Some(index_buffer) = index_buffer {
if let Some(buffer_info) =
draw.render_resource_context.get_buffer_info(index_buffer)
{
indices = 0..(buffer_info.size / 2) as u32;
} else {
panic!("expected buffer type");
}
draw.set_index_buffer(index_buffer, 0);
}
}
}
draw.draw_indexed(indices, 0, 0..1);
}
Ok(())
}
}
pub fn draw_system<T: Drawable + Component>( pub fn draw_system<T: Drawable + Component>(
pipelines: Res<Assets<PipelineDescriptor>>, pipelines: Res<Assets<PipelineDescriptor>>,
render_resource_bindings: Res<RenderResourceBindings>, render_resource_bindings: Res<RenderResourceBindings>,

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
base_render_graph, mesh::Mesh, Camera, OrthographicProjection, PerspectiveProjection, base_render_graph, mesh::Mesh, Camera, OrthographicProjection, PerspectiveProjection,
RenderPipelines, WindowOrigin, draw::Draw, WindowOrigin, draw::Draw, pipeline::RenderPipelines,
}; };
use bevy_asset::Handle; use bevy_asset::Handle;
use bevy_derive::EntityArchetype; use bevy_derive::EntityArchetype;

View File

@ -35,10 +35,10 @@ use base_render_graph::{BaseRenderGraphBuilder, BaseRenderGraphConfig};
use bevy_app::{AppBuilder, AppPlugin}; use bevy_app::{AppBuilder, AppPlugin};
use bevy_asset::AddAsset; use bevy_asset::AddAsset;
use bevy_type_registry::RegisterType; use bevy_type_registry::RegisterType;
use draw::{clear_draw_system, draw_system, Draw, RenderPipelines}; use draw::{clear_draw_system, draw_system, Draw};
use legion::prelude::IntoSystem; use legion::prelude::IntoSystem;
use mesh::mesh_resource_provider_system; use mesh::mesh_resource_provider_system;
use pipeline::compile_pipelines_system; use pipeline::{RenderPipelines, compile_pipelines_system};
use render_graph::{system::render_graph_schedule_executor_system, RenderGraph}; use render_graph::{system::render_graph_schedule_executor_system, RenderGraph};
use render_resource::bind_groups_system; use render_resource::bind_groups_system;
use std::ops::Range; use std::ops::Range;

View File

@ -408,16 +408,17 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
// TODO: remove this once batches are pipeline specific and deprecate assigned_meshes draw target // TODO: remove this once batches are pipeline specific and deprecate assigned_meshes draw target
for (handle, mut render_pipelines) in query.iter_mut(world) { for (handle, mut render_pipelines) in query.iter_mut(world) {
if let Some(mesh) = meshes.get(&handle) { if let Some(mesh) = meshes.get(&handle) {
render_pipelines for render_pipeline in render_pipelines.pipelines.iter_mut() {
.render_resource_bindings render_pipeline
.pipeline_specialization .specialization
.primitive_topology = mesh.primitive_topology; .primitive_topology = mesh.primitive_topology;
} }
}
if let Some(RenderResourceId::Buffer(vertex_buffer)) = if let Some(RenderResourceId::Buffer(vertex_buffer)) =
render_resource_context.get_asset_resource(*handle, VERTEX_BUFFER_ASSET_INDEX) render_resource_context.get_asset_resource(*handle, VERTEX_BUFFER_ASSET_INDEX)
{ {
render_pipelines.render_resource_bindings.set_vertex_buffer( render_pipelines.bindings.set_vertex_buffer(
"Vertex", "Vertex",
vertex_buffer, vertex_buffer,
render_resource_context render_resource_context

View File

@ -5,6 +5,7 @@ mod pipeline_compiler;
mod pipeline_layout; mod pipeline_layout;
pub mod state_descriptors; pub mod state_descriptors;
mod vertex_buffer_descriptor; mod vertex_buffer_descriptor;
mod render_pipelines;
mod vertex_format; mod vertex_format;
pub use bind_group::*; pub use bind_group::*;
@ -12,5 +13,6 @@ pub use binding::*;
pub use pipeline::*; pub use pipeline::*;
pub use pipeline_compiler::*; pub use pipeline_compiler::*;
pub use pipeline_layout::*; pub use pipeline_layout::*;
pub use render_pipelines::*;
pub use vertex_buffer_descriptor::*; pub use vertex_buffer_descriptor::*;
pub use vertex_format::*; pub use vertex_format::*;

View File

@ -1,8 +1,10 @@
use super::{state_descriptors::PrimitiveTopology, PipelineDescriptor, VertexBufferDescriptors}; use super::{
state_descriptors::PrimitiveTopology, PipelineDescriptor, RenderPipeline, RenderPipelines,
VertexBufferDescriptors,
};
use crate::{ use crate::{
draw::RenderPipelines,
renderer::RenderResourceContext, renderer::RenderResourceContext,
shader::{Shader, ShaderSource}, shader::{Shader, ShaderSource}, render_resource::RenderResourceBindings,
}; };
use bevy_asset::{Assets, Handle}; use bevy_asset::{Assets, Handle};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -25,7 +27,7 @@ pub struct ShaderSpecialization {
pub struct PipelineCompiler { pub struct PipelineCompiler {
pub shader_source_to_compiled: pub shader_source_to_compiled:
HashMap<Handle<Shader>, Vec<(ShaderSpecialization, Handle<Shader>)>>, HashMap<Handle<Shader>, Vec<(ShaderSpecialization, Handle<Shader>)>>,
pub pipeline_source_to_compiled: HashMap< pub specialized_pipelines: HashMap<
Handle<PipelineDescriptor>, Handle<PipelineDescriptor>,
Vec<(PipelineSpecialization, Handle<PipelineDescriptor>)>, Vec<(PipelineSpecialization, Handle<PipelineDescriptor>)>,
>, >,
@ -78,17 +80,15 @@ impl PipelineCompiler {
vertex_buffer_descriptors: &VertexBufferDescriptors, vertex_buffer_descriptors: &VertexBufferDescriptors,
shaders: &mut Assets<Shader>, shaders: &mut Assets<Shader>,
pipeline_descriptor: &PipelineDescriptor, pipeline_descriptor: &PipelineDescriptor,
render_pipelines: &RenderPipelines, render_pipeline: &RenderPipeline,
render_resource_bindings: &RenderResourceBindings,
) -> PipelineDescriptor { ) -> PipelineDescriptor {
let mut compiled_pipeline_descriptor = pipeline_descriptor.clone(); let mut compiled_pipeline_descriptor = pipeline_descriptor.clone();
compiled_pipeline_descriptor.shader_stages.vertex = self.compile_shader( compiled_pipeline_descriptor.shader_stages.vertex = self.compile_shader(
shaders, shaders,
&pipeline_descriptor.shader_stages.vertex, &pipeline_descriptor.shader_stages.vertex,
&render_pipelines &render_pipeline.specialization.shader_specialization,
.render_resource_bindings
.pipeline_specialization
.shader_specialization,
); );
compiled_pipeline_descriptor.shader_stages.fragment = pipeline_descriptor compiled_pipeline_descriptor.shader_stages.fragment = pipeline_descriptor
.shader_stages .shader_stages
@ -98,10 +98,7 @@ impl PipelineCompiler {
self.compile_shader( self.compile_shader(
shaders, shaders,
fragment, fragment,
&render_pipelines &render_pipeline.specialization.shader_specialization,
.render_resource_bindings
.pipeline_specialization
.shader_specialization,
) )
}); });
@ -109,13 +106,11 @@ impl PipelineCompiler {
shaders, shaders,
true, true,
Some(vertex_buffer_descriptors), Some(vertex_buffer_descriptors),
Some(&render_pipelines.render_resource_bindings), Some(render_resource_bindings),
); );
compiled_pipeline_descriptor.primitive_topology = render_pipelines compiled_pipeline_descriptor.primitive_topology =
.render_resource_bindings render_pipeline.specialization.primitive_topology;
.pipeline_specialization
.primitive_topology;
compiled_pipeline_descriptor compiled_pipeline_descriptor
} }
@ -127,31 +122,29 @@ impl PipelineCompiler {
render_pipelines: &mut RenderPipelines, render_pipelines: &mut RenderPipelines,
render_resource_context: &dyn RenderResourceContext, render_resource_context: &dyn RenderResourceContext,
) { ) {
for (i, pipeline_handle) in render_pipelines.pipelines.iter().enumerate() { for render_pipeline in render_pipelines.pipelines.iter_mut() {
if let None = self.pipeline_source_to_compiled.get(pipeline_handle) { let source_pipeline = render_pipeline.pipeline;
self.pipeline_source_to_compiled if let None = self.specialized_pipelines.get(&source_pipeline) {
.insert(*pipeline_handle, Vec::new()); self.specialized_pipelines
.insert(source_pipeline, Vec::new());
} }
let compiled_pipeline_handle = if let Some((_shader_defs, compiled_pipeline_handle)) = let compiled_pipeline_handle = if let Some((_shader_defs, compiled_pipeline_handle)) =
self.pipeline_source_to_compiled self.specialized_pipelines
.get_mut(pipeline_handle) .get_mut(&source_pipeline)
.unwrap() .unwrap()
.iter() .iter()
.find(|(pipeline_specialization, _compiled_pipeline_handle)| { .find(|(pipeline_specialization, _compiled_pipeline_handle)| {
*pipeline_specialization *pipeline_specialization == render_pipeline.specialization
== render_pipelines
.render_resource_bindings
.pipeline_specialization
}) { }) {
*compiled_pipeline_handle *compiled_pipeline_handle
} else { } else {
let pipeline_descriptor = pipelines.get(pipeline_handle).unwrap(); let pipeline_descriptor = pipelines.get(&source_pipeline).unwrap();
let compiled_pipeline_descriptor = self.compile_pipeline( let compiled_pipeline_descriptor = self.compile_pipeline(
vertex_buffer_descriptors, vertex_buffer_descriptors,
shaders, shaders,
pipeline_descriptor, pipeline_descriptor,
render_pipelines, render_pipeline,
&render_pipelines.bindings,
); );
let compiled_pipeline_handle = pipelines.add(compiled_pipeline_descriptor); let compiled_pipeline_handle = pipelines.add(compiled_pipeline_descriptor);
render_resource_context.create_render_pipeline( render_resource_context.create_render_pipeline(
@ -161,26 +154,17 @@ impl PipelineCompiler {
); );
let compiled_pipelines = self let compiled_pipelines = self
.pipeline_source_to_compiled .specialized_pipelines
.get_mut(pipeline_handle) .get_mut(&source_pipeline)
.unwrap(); .unwrap();
compiled_pipelines.push(( compiled_pipelines.push((
render_pipelines render_pipeline.specialization.clone(),
.render_resource_bindings
.pipeline_specialization
.clone(),
compiled_pipeline_handle, compiled_pipeline_handle,
)); ));
compiled_pipeline_handle compiled_pipeline_handle
}; };
if i == render_pipelines.compiled_pipelines.len() { render_pipeline.specialized_pipeline = Some(compiled_pipeline_handle);
render_pipelines
.compiled_pipelines
.push(compiled_pipeline_handle);
} else {
render_pipelines.compiled_pipelines[i] = compiled_pipeline_handle;
}
} }
} }
@ -188,7 +172,7 @@ impl PipelineCompiler {
&self, &self,
pipeline_handle: Handle<PipelineDescriptor>, pipeline_handle: Handle<PipelineDescriptor>,
) -> Option<impl Iterator<Item = &Handle<PipelineDescriptor>>> { ) -> Option<impl Iterator<Item = &Handle<PipelineDescriptor>>> {
if let Some(compiled_pipelines) = self.pipeline_source_to_compiled.get(&pipeline_handle) { if let Some(compiled_pipelines) = self.specialized_pipelines.get(&pipeline_handle) {
Some(compiled_pipelines.iter().map(|(_, handle)| handle)) Some(compiled_pipelines.iter().map(|(_, handle)| handle))
} else { } else {
None None
@ -196,7 +180,7 @@ impl PipelineCompiler {
} }
pub fn iter_all_compiled_pipelines(&self) -> impl Iterator<Item = &Handle<PipelineDescriptor>> { pub fn iter_all_compiled_pipelines(&self) -> impl Iterator<Item = &Handle<PipelineDescriptor>> {
self.pipeline_source_to_compiled self.specialized_pipelines
.values() .values()
.map(|compiled_pipelines| { .map(|compiled_pipelines| {
compiled_pipelines compiled_pipelines
@ -230,11 +214,12 @@ pub fn compile_pipelines_system(
); );
// reset shader_defs so they can be changed next frame // reset shader_defs so they can be changed next frame
render_pipelines for render_pipeline in render_pipelines.pipelines.iter_mut() {
.render_resource_bindings render_pipeline
.pipeline_specialization .specialization
.shader_specialization .shader_specialization
.shader_defs .shader_defs
.clear(); .clear();
} }
}
} }

View File

@ -0,0 +1,107 @@
use super::{PipelineDescriptor, PipelineSpecialization};
use crate::{
draw::{DrawContext, DrawError, Drawable},
render_resource::RenderResourceBindings,
};
use bevy_asset::Handle;
use bevy_property::Properties;
#[derive(Properties, Default, Clone)]
pub struct RenderPipeline {
pub pipeline: Handle<PipelineDescriptor>,
#[property(ignore)]
pub specialized_pipeline: Option<Handle<PipelineDescriptor>>,
#[property(ignore)]
pub specialization: PipelineSpecialization,
}
impl RenderPipeline {
pub fn new(pipeline: Handle<PipelineDescriptor>) -> Self {
RenderPipeline {
pipeline,
..Default::default()
}
}
}
#[derive(Properties)]
pub struct RenderPipelines {
pub pipelines: Vec<RenderPipeline>,
#[property(ignore)]
pub bindings: RenderResourceBindings,
}
impl RenderPipelines {
pub fn from_handles<'a, T: IntoIterator<Item = &'a Handle<PipelineDescriptor>>>(
handles: T,
) -> Self {
RenderPipelines {
pipelines: handles
.into_iter()
.map(|pipeline| RenderPipeline::new(*pipeline))
.collect::<Vec<RenderPipeline>>(),
..Default::default()
}
}
}
impl Default for RenderPipelines {
fn default() -> Self {
Self {
bindings: Default::default(),
pipelines: vec![RenderPipeline::default()],
}
}
}
impl Drawable for RenderPipelines {
fn draw(&mut self, draw: &mut DrawContext) -> Result<(), DrawError> {
for render_pipeline in self.pipelines.iter() {
let specialized_handle = if let Some(handle) = render_pipeline.specialized_pipeline {
handle
} else {
continue;
};
let pipeline = draw.pipelines.get(&specialized_handle).unwrap();
let layout = pipeline.get_layout().unwrap();
draw.set_pipeline(specialized_handle)?;
for bind_group_descriptor in layout.bind_groups.iter() {
if let Some(local_bind_group) = self
.bindings
.get_descriptor_bind_group(bind_group_descriptor.id)
{
draw.set_bind_group(bind_group_descriptor, local_bind_group);
} else if let Some(global_bind_group) = draw
.render_resource_bindings
.get_descriptor_bind_group(bind_group_descriptor.id)
{
draw.set_bind_group(bind_group_descriptor, global_bind_group);
}
}
let mut indices = 0..0;
for (slot, vertex_buffer_descriptor) in
layout.vertex_buffer_descriptors.iter().enumerate()
{
if let Some((vertex_buffer, index_buffer)) = self
.bindings
.get_vertex_buffer(&vertex_buffer_descriptor.name)
{
draw.set_vertex_buffer(slot as u32, vertex_buffer, 0);
if let Some(index_buffer) = index_buffer {
if let Some(buffer_info) =
draw.render_resource_context.get_buffer_info(index_buffer)
{
indices = 0..(buffer_info.size / 2) as u32;
} else {
panic!("expected buffer type");
}
draw.set_index_buffer(index_buffer, 0);
}
}
}
draw.draw_indexed(indices, 0, 0..1);
}
Ok(())
}
}

View File

@ -1,5 +1,6 @@
use crate::{ use crate::{
draw::{Draw, RenderPipelines}, draw::Draw,
pipeline::RenderPipelines,
render_graph::{CommandQueue, Node, ResourceSlots, SystemNode}, render_graph::{CommandQueue, Node, ResourceSlots, SystemNode},
render_resource::{ render_resource::{
self, BufferInfo, BufferUsage, RenderResourceBinding, RenderResourceBindings, self, BufferInfo, BufferUsage, RenderResourceBinding, RenderResourceBindings,
@ -373,8 +374,7 @@ where
(move |world: &mut SubWorld, (move |world: &mut SubWorld,
render_resource_context: Res<Box<dyn RenderResourceContext>>, render_resource_context: Res<Box<dyn RenderResourceContext>>,
query: &mut Query<(Read<T>, Read<Draw>, Write<RenderPipelines>)>| { query: &mut Query<(Read<T>, Read<Draw>, Write<RenderPipelines>)>| {
let render_resource_context = &**render_resource_context;
let render_resource_context = &** render_resource_context;
uniform_buffer_arrays.reset_changed_item_counts(); uniform_buffer_arrays.reset_changed_item_counts();
// update uniforms info // update uniforms info
for (uniforms, draw, _render_pipelines) in query.iter_mut(world) { for (uniforms, draw, _render_pipelines) in query.iter_mut(world) {
@ -396,7 +396,7 @@ where
setup_uniform_texture_resources::<T>( setup_uniform_texture_resources::<T>(
&uniforms, &uniforms,
render_resource_context, render_resource_context,
&mut render_pipelines.render_resource_bindings, &mut render_pipelines.bindings,
) )
} }
@ -411,7 +411,7 @@ where
&uniforms, &uniforms,
dynamic_uniforms, dynamic_uniforms,
render_resource_context, render_resource_context,
&mut render_pipelines.render_resource_bindings, &mut render_pipelines.bindings,
&mut staging_buffer, &mut staging_buffer,
); );
} }
@ -432,7 +432,7 @@ where
&uniforms, &uniforms,
dynamic_uniforms, dynamic_uniforms,
render_resource_context, render_resource_context,
&mut render_pipelines.render_resource_bindings, &mut render_pipelines.bindings,
&mut staging_buffer, &mut staging_buffer,
); );
} }
@ -598,9 +598,7 @@ where
for (asset_handle, _draw, mut render_pipelines) in query.iter_mut(world) { for (asset_handle, _draw, mut render_pipelines) in query.iter_mut(world) {
if let Some(asset_bindings) = asset_render_resource_bindings.get(&asset_handle) { if let Some(asset_bindings) = asset_render_resource_bindings.get(&asset_handle) {
render_pipelines render_pipelines.bindings.extend(asset_bindings);
.render_resource_bindings
.extend(asset_bindings);
} }
} }
}) })

View File

@ -1,5 +1,5 @@
use super::{BufferId, RenderResourceId, BindGroup, BindGroupId, SamplerId, TextureId}; use super::{BindGroup, BindGroupId, BufferId, RenderResourceId, SamplerId, TextureId};
use crate::pipeline::{BindGroupDescriptor, BindGroupDescriptorId, PipelineSpecialization}; use crate::pipeline::{BindGroupDescriptor, BindGroupDescriptorId};
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
hash::Hash, hash::Hash,
@ -20,7 +20,7 @@ pub enum RenderResourceBinding {
impl RenderResourceBinding { impl RenderResourceBinding {
pub fn get_texture(&self) -> Option<TextureId> { pub fn get_texture(&self) -> Option<TextureId> {
if let RenderResourceBinding::Texture(texture) = self{ if let RenderResourceBinding::Texture(texture) = self {
Some(*texture) Some(*texture)
} else { } else {
None None
@ -28,7 +28,7 @@ impl RenderResourceBinding {
} }
pub fn get_buffer(&self) -> Option<BufferId> { pub fn get_buffer(&self) -> Option<BufferId> {
if let RenderResourceBinding::Buffer{ buffer, ..} = self{ if let RenderResourceBinding::Buffer { buffer, .. } = self {
Some(*buffer) Some(*buffer)
} else { } else {
None None
@ -36,7 +36,7 @@ impl RenderResourceBinding {
} }
pub fn get_sampler(&self) -> Option<SamplerId> { pub fn get_sampler(&self) -> Option<SamplerId> {
if let RenderResourceBinding::Sampler(sampler) = self{ if let RenderResourceBinding::Sampler(sampler) = self {
Some(*sampler) Some(*sampler)
} else { } else {
None None
@ -73,16 +73,18 @@ pub enum BindGroupStatus {
} }
// PERF: if the bindings are scoped to a specific pipeline layout, then names could be replaced with indices here for a perf boost // PERF: if the bindings are scoped to a specific pipeline layout, then names could be replaced with indices here for a perf boost
#[derive(Eq, PartialEq, Debug, Default)] #[derive(Eq, PartialEq, Debug, Default, Clone)]
pub struct RenderResourceBindings { pub struct RenderResourceBindings {
// TODO: remove this. it shouldn't be needed anymore // TODO: remove this. it shouldn't be needed anymore
pub id: RenderResourceBindingsId, pub id: RenderResourceBindingsId,
bindings: HashMap<String, RenderResourceBinding>, bindings: HashMap<String, RenderResourceBinding>,
// TODO: remove this
vertex_buffers: HashMap<String, (BufferId, Option<BufferId>)>, vertex_buffers: HashMap<String, (BufferId, Option<BufferId>)>,
bind_groups: HashMap<BindGroupId, BindGroup>, bind_groups: HashMap<BindGroupId, BindGroup>,
bind_group_descriptors: HashMap<BindGroupDescriptorId, BindGroupId>, bind_group_descriptors: HashMap<BindGroupDescriptorId, BindGroupId>,
dirty_bind_groups: HashSet<BindGroupId>, dirty_bind_groups: HashSet<BindGroupId>,
pub pipeline_specialization: PipelineSpecialization, // TODO: remove this
// pub pipeline_specialization: PipelineSpecialization,
} }
impl RenderResourceBindings { impl RenderResourceBindings {
@ -111,8 +113,7 @@ impl RenderResourceBindings {
self.set(name, binding.clone()); self.set(name, binding.clone());
} }
for (name, (vertex_buffer, index_buffer)) in for (name, (vertex_buffer, index_buffer)) in render_resource_bindings.vertex_buffers.iter()
render_resource_bindings.vertex_buffers.iter()
{ {
self.set_vertex_buffer(name, *vertex_buffer, index_buffer.clone()); self.set_vertex_buffer(name, *vertex_buffer, index_buffer.clone());
} }
@ -132,16 +133,12 @@ impl RenderResourceBindings {
.insert(name.to_string(), (vertex_buffer, index_buffer)); .insert(name.to_string(), (vertex_buffer, index_buffer));
} }
fn create_bind_group( fn create_bind_group(&mut self, descriptor: &BindGroupDescriptor) -> BindGroupStatus {
&mut self,
descriptor: &BindGroupDescriptor,
) -> BindGroupStatus {
let bind_group = self.build_bind_group(descriptor); let bind_group = self.build_bind_group(descriptor);
if let Some(bind_group) = bind_group { if let Some(bind_group) = bind_group {
let id = bind_group.id; let id = bind_group.id;
self.bind_groups.insert(id, bind_group); self.bind_groups.insert(id, bind_group);
self.bind_group_descriptors self.bind_group_descriptors.insert(descriptor.id, id);
.insert(descriptor.id, id);
BindGroupStatus::Changed(id) BindGroupStatus::Changed(id)
} else { } else {
BindGroupStatus::NoMatch BindGroupStatus::NoMatch
@ -152,10 +149,7 @@ impl RenderResourceBindings {
&mut self, &mut self,
bind_group_descriptor: &BindGroupDescriptor, bind_group_descriptor: &BindGroupDescriptor,
) -> BindGroupStatus { ) -> BindGroupStatus {
if let Some(id) = self if let Some(id) = self.bind_group_descriptors.get(&bind_group_descriptor.id) {
.bind_group_descriptors
.get(&bind_group_descriptor.id)
{
if self.dirty_bind_groups.contains(id) { if self.dirty_bind_groups.contains(id) {
self.dirty_bind_groups.remove(id); self.dirty_bind_groups.remove(id);
self.create_bind_group(bind_group_descriptor) self.create_bind_group(bind_group_descriptor)
@ -171,26 +165,18 @@ impl RenderResourceBindings {
self.bind_groups.get(&id) self.bind_groups.get(&id)
} }
pub fn get_descriptor_bind_group( pub fn get_descriptor_bind_group(&self, id: BindGroupDescriptorId) -> Option<&BindGroup> {
&self,
id: BindGroupDescriptorId,
) -> Option<&BindGroup> {
self.bind_group_descriptors self.bind_group_descriptors
.get(&id) .get(&id)
.and_then(|bind_group_id| { .and_then(|bind_group_id| self.get_bind_group(*bind_group_id))
self.get_bind_group(*bind_group_id)
})
} }
fn build_bind_group( fn build_bind_group(&self, bind_group_descriptor: &BindGroupDescriptor) -> Option<BindGroup> {
&self,
bind_group_descriptor: &BindGroupDescriptor,
) -> Option<BindGroup> {
let mut bind_group_builder = BindGroup::build(); let mut bind_group_builder = BindGroup::build();
for binding_descriptor in bind_group_descriptor.bindings.iter() { for binding_descriptor in bind_group_descriptor.bindings.iter() {
if let Some(binding) = self.get(&binding_descriptor.name) { if let Some(binding) = self.get(&binding_descriptor.name) {
bind_group_builder = bind_group_builder bind_group_builder =
.add_binding(binding_descriptor.index, binding.clone()); bind_group_builder.add_binding(binding_descriptor.index, binding.clone());
} else { } else {
return None; return None;
} }
@ -271,7 +257,8 @@ mod tests {
panic!("expected a changed bind group"); panic!("expected a changed bind group");
}; };
let different_bind_group_status = different_bindings.update_bind_group(&bind_group_descriptor); let different_bind_group_status =
different_bindings.update_bind_group(&bind_group_descriptor);
if let BindGroupStatus::Changed(different_bind_group_id) = different_bind_group_status { if let BindGroupStatus::Changed(different_bind_group_id) = different_bind_group_status {
assert_ne!( assert_ne!(
id, different_bind_group_id, id, different_bind_group_id,
@ -284,14 +271,18 @@ mod tests {
let equal_bind_group_status = equal_bindings.update_bind_group(&bind_group_descriptor); let equal_bind_group_status = equal_bindings.update_bind_group(&bind_group_descriptor);
if let BindGroupStatus::Changed(equal_bind_group_id) = equal_bind_group_status { if let BindGroupStatus::Changed(equal_bind_group_id) = equal_bind_group_status {
assert_eq!(id, equal_bind_group_id, "equal bind group should have the same id"); assert_eq!(
id, equal_bind_group_id,
"equal bind group should have the same id"
);
} else { } else {
panic!("expected a changed bind group"); panic!("expected a changed bind group");
}; };
let mut unmatched_bindings = RenderResourceBindings::default(); let mut unmatched_bindings = RenderResourceBindings::default();
unmatched_bindings.set("a", resource1.clone()); unmatched_bindings.set("a", resource1.clone());
let unmatched_bind_group_status = unmatched_bindings.update_bind_group(&bind_group_descriptor); let unmatched_bind_group_status =
unmatched_bindings.update_bind_group(&bind_group_descriptor);
assert_eq!(unmatched_bind_group_status, BindGroupStatus::NoMatch); assert_eq!(unmatched_bind_group_status, BindGroupStatus::NoMatch);
} }
} }

View File

@ -1,6 +1,5 @@
use crate::{ use crate::{
draw::RenderPipelines, pipeline::{PipelineCompiler, PipelineDescriptor, RenderPipelines},
pipeline::{PipelineCompiler, PipelineDescriptor},
render_resource::{BindGroupStatus, RenderResourceBindings}, render_resource::{BindGroupStatus, RenderResourceBindings},
renderer::RenderResourceContext, renderer::RenderResourceContext,
}; };
@ -56,11 +55,11 @@ pub fn bind_groups_system(
} }
for mut render_pipelines in query.iter_mut(world) { for mut render_pipelines in query.iter_mut(world) {
let render_pipelines = render_pipelines.as_mut(); let render_pipelines = render_pipelines.as_mut();
for pipeline in render_pipelines.compiled_pipelines.iter() { for render_pipeline in render_pipelines.pipelines.iter_mut() {
let pipeline = pipelines.get(pipeline).unwrap(); let pipeline = pipelines.get(&render_pipeline.specialized_pipeline.unwrap()).unwrap();
update_bind_groups( update_bind_groups(
pipeline, pipeline,
&mut render_pipelines.render_resource_bindings, &mut render_pipelines.bindings,
render_resource_context, render_resource_context,
) )
} }

View File

@ -60,13 +60,14 @@ where
T: ShaderDefs + Send + Sync + 'static, T: ShaderDefs + Send + Sync + 'static,
{ {
for shader_def in shader_defs.iter_shader_defs() { for shader_def in shader_defs.iter_shader_defs() {
render_pipelines for render_pipeline in render_pipelines.pipelines.iter_mut() {
.render_resource_bindings render_pipeline
.pipeline_specialization .specialization
.shader_specialization .shader_specialization
.shader_defs .shader_defs
.insert(shader_def.to_string()); .insert(shader_def.to_string());
} }
}
} }
pub fn asset_shader_def_system<T>( pub fn asset_shader_def_system<T>(
@ -78,11 +79,12 @@ pub fn asset_shader_def_system<T>(
{ {
let shader_defs = assets.get(&asset_handle).unwrap(); let shader_defs = assets.get(&asset_handle).unwrap();
for shader_def in shader_defs.iter_shader_defs() { for shader_def in shader_defs.iter_shader_defs() {
render_pipelines for render_pipeline in render_pipelines.pipelines.iter_mut() {
.render_resource_bindings render_pipeline
.pipeline_specialization .specialization
.shader_specialization .shader_specialization
.shader_defs .shader_defs
.insert(shader_def.to_string()); .insert(shader_def.to_string());
} }
}
} }

View File

@ -2,9 +2,9 @@ use crate::{
render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, Quad, TextureAtlas, render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, Quad, TextureAtlas,
TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE, TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE,
}; };
use bevy_asset::Handle;
use bevy_app::EntityArchetype; use bevy_app::EntityArchetype;
use bevy_render::{mesh::Mesh, draw::{Draw, RenderPipelines}}; use bevy_asset::Handle;
use bevy_render::{draw::Draw, mesh::Mesh, pipeline::RenderPipelines};
#[derive(EntityArchetype)] #[derive(EntityArchetype)]
pub struct SpriteEntity { pub struct SpriteEntity {
@ -24,10 +24,7 @@ impl Default for SpriteEntity {
mesh: QUAD_HANDLE, mesh: QUAD_HANDLE,
material: Default::default(), material: Default::default(),
draw: Default::default(), draw: Default::default(),
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[SPRITE_PIPELINE_HANDLE]),
pipelines: vec![SPRITE_PIPELINE_HANDLE],
..Default::default()
},
} }
} }
} }
@ -51,10 +48,7 @@ impl Default for SpriteSheetEntity {
sprite: Default::default(), sprite: Default::default(),
texture_atlas: Default::default(), texture_atlas: Default::default(),
draw: Default::default(), draw: Default::default(),
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[SPRITE_SHEET_PIPELINE_HANDLE]),
pipelines: vec![SPRITE_SHEET_PIPELINE_HANDLE],
..Default::default()
},
mesh: QUAD_HANDLE, mesh: QUAD_HANDLE,
// transform: Default::default(), // transform: Default::default(),
// translation: Default::default(), // translation: Default::default(),

View File

@ -2,7 +2,7 @@ use super::Node;
use crate::{render::UI_PIPELINE_HANDLE, widget::Label}; use crate::{render::UI_PIPELINE_HANDLE, widget::Label};
use bevy_asset::Handle; use bevy_asset::Handle;
use bevy_derive::EntityArchetype; use bevy_derive::EntityArchetype;
use bevy_render::{draw::{Draw, RenderPipelines}, mesh::Mesh}; use bevy_render::{draw::Draw, mesh::Mesh, pipeline::RenderPipelines};
use bevy_sprite::{ColorMaterial, Quad, QUAD_HANDLE}; use bevy_sprite::{ColorMaterial, Quad, QUAD_HANDLE};
#[derive(EntityArchetype)] #[derive(EntityArchetype)]
@ -23,10 +23,7 @@ impl Default for UiEntity {
mesh: QUAD_HANDLE, mesh: QUAD_HANDLE,
material: Default::default(), material: Default::default(),
draw: Default::default(), draw: Default::default(),
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[UI_PIPELINE_HANDLE]),
pipelines: vec![UI_PIPELINE_HANDLE],
..Default::default()
},
} }
} }
} }
@ -51,10 +48,7 @@ impl Default for LabelEntity {
// NOTE: labels each get their own material. // NOTE: labels each get their own material.
material: Handle::new(), // TODO: maybe abstract this out material: Handle::new(), // TODO: maybe abstract this out
draw: Default::default(), draw: Default::default(),
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[UI_PIPELINE_HANDLE]),
pipelines: vec![UI_PIPELINE_HANDLE],
..Default::default()
},
label: Label::default(), label: Label::default(),
} }
} }

View File

@ -53,7 +53,10 @@ fn setup(
fragment: Some(shaders.add(Shader::from_glsl(ShaderStage::Fragment, FRAGMENT_SHADER))), fragment: Some(shaders.add(Shader::from_glsl(ShaderStage::Fragment, FRAGMENT_SHADER))),
})); }));
render_graph.add_system_node("my_material", AssetRenderResourcesNode::<MyMaterial>::new(true)); render_graph.add_system_node(
"my_material",
AssetRenderResourcesNode::<MyMaterial>::new(true),
);
pipeline_handle pipeline_handle
}; };
@ -68,10 +71,7 @@ fn setup(
// cube // cube
.add_entity(MeshMaterialEntity::<MyMaterial> { .add_entity(MeshMaterialEntity::<MyMaterial> {
mesh: cube_handle, mesh: cube_handle,
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[pipeline_handle]),
pipelines: vec![pipeline_handle],
..Default::default()
},
material, material,
translation: Translation::new(0.0, 0.0, 0.0), translation: Translation::new(0.0, 0.0, 0.0),
..Default::default() ..Default::default()

View File

@ -85,10 +85,7 @@ fn setup(
// cube // cube
.add_entity(MeshMaterialEntity::<MyMaterial> { .add_entity(MeshMaterialEntity::<MyMaterial> {
mesh: cube_handle, mesh: cube_handle,
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[pipeline_handle]),
pipelines: vec![pipeline_handle],
..Default::default()
},
material: green_material, material: green_material,
translation: Translation::new(-2.0, 0.0, 0.0), translation: Translation::new(-2.0, 0.0, 0.0),
..Default::default() ..Default::default()
@ -96,10 +93,7 @@ fn setup(
// cube // cube
.add_entity(MeshMaterialEntity::<MyMaterial> { .add_entity(MeshMaterialEntity::<MyMaterial> {
mesh: cube_handle, mesh: cube_handle,
render_pipelines: RenderPipelines { render_pipelines: RenderPipelines::from_handles(&[pipeline_handle]),
pipelines: vec![pipeline_handle],
..Default::default()
},
material: red_material, material: red_material,
translation: Translation::new(2.0, 0.0, 0.0), translation: Translation::new(2.0, 0.0, 0.0),
..Default::default() ..Default::default()

View File

@ -14,20 +14,20 @@ pub use crate::{
pbr::{entity::*, light::Light, material::StandardMaterial}, pbr::{entity::*, light::Light, material::StandardMaterial},
property::{DynamicProperties, Properties, PropertiesVal, Property, PropertyVal}, property::{DynamicProperties, Properties, PropertiesVal, Property, PropertyVal},
render::{ render::{
draw::Draw,
entity::*, entity::*,
mesh::{shape, Mesh}, mesh::{shape, Mesh},
pipeline::PipelineDescriptor, pipeline::{PipelineDescriptor, RenderPipelines},
render_graph::{ render_graph::{
nodes::{ nodes::{
AssetRenderResourcesNode, CameraNode, MainPassNode, RenderResourcesNode, WindowSwapChainNode, AssetRenderResourcesNode, CameraNode, MainPassNode, RenderResourcesNode,
WindowTextureNode, WindowSwapChainNode, WindowTextureNode,
}, },
RenderGraph, RenderGraph,
}, },
render_resource::RenderResources, render_resource::RenderResources,
shader::{Shader, ShaderDefs, ShaderStage, ShaderStages}, shader::{Shader, ShaderDefs, ShaderStage, ShaderStages},
texture::Texture, texture::Texture,
draw::{Draw, RenderPipelines},
Camera, Color, ColorSource, OrthographicProjection, PerspectiveProjection, Camera, Color, ColorSource, OrthographicProjection, PerspectiveProjection,
}, },
scene::{Scene, SceneSpawner}, scene::{Scene, SceneSpawner},