Skip to content

Commit

Permalink
rebased onto main
Browse files Browse the repository at this point in the history
  • Loading branch information
Ku95 committed Mar 6, 2022
1 parent dbfc07a commit be2b972
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ pub fn queue_shadows(
casting_meshes: Query<&Handle<Mesh>, Without<NotShadowCaster>>,
render_meshes: Res<RenderAssets<Mesh>>,
mut pipelines: ResMut<SpecializedMeshPipelines<ShadowPipeline>>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut pipeline_cache: ResMut<PipelineCache>,
view_lights: Query<&ViewLightEntities>,
mut view_light_shadow_phases: Query<(&LightEntity, &mut RenderPhase<Shadow>)>,
point_light_entities: Query<&CubemapVisibleEntities, With<ExtractedPointLight>>,
Expand Down
13 changes: 6 additions & 7 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use bevy_asset::{load_internal_asset, Handle, HandleUntyped};
use bevy_core_pipeline::Opaque3d;
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::mesh::MeshVertexBufferLayout;
use bevy_render::render_resource::{
PolygonMode, RenderPipelineDescriptor, SpecializedMeshPipeline, SpecializedMeshPipelineError,
SpecializedMeshPipelines,
};
use bevy_render::{
mesh::Mesh,
mesh::MeshVertexBufferLayout,
render_asset::RenderAssets,
render_phase::{AddRenderCommand, DrawFunctions, RenderPhase, SetItemPipeline},
render_resource::{RenderPipelineCache, Shader},
render_resource::{
PipelineCache, PolygonMode, RenderPipelineDescriptor, Shader, SpecializedMeshPipeline,
SpecializedMeshPipelineError, SpecializedMeshPipelines,
},
view::{ExtractedView, Msaa},
RenderApp, RenderStage,
};
Expand Down Expand Up @@ -109,7 +108,7 @@ fn queue_wireframes(
render_meshes: Res<RenderAssets<Mesh>>,
wireframe_config: Res<WireframeConfig>,
wireframe_pipeline: Res<WireframePipeline>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut pipeline_cache: ResMut<PipelineCache>,
mut specialized_pipelines: ResMut<SpecializedMeshPipelines<WireframePipeline>>,
msaa: Res<Msaa>,
mut material_meshes: QuerySet<(
Expand Down
41 changes: 32 additions & 9 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::render_resource::{ComputePipeline, RawComputePipelineDescriptor};
use crate::{
render_resource::{
AsModuleDescriptorError, BindGroupLayout, BindGroupLayoutId, ComputePipelineDescriptor,
ProcessShaderError, ProcessedShader, RawFragmentState, RawRenderPipelineDescriptor,
AsModuleDescriptorError, BindGroupLayout, BindGroupLayoutId, ComputePipeline,
ComputePipelineDescriptor, ProcessShaderError, ProcessedShader,
RawComputePipelineDescriptor, RawFragmentState, RawRenderPipelineDescriptor,
RawVertexState, RenderPipeline, RenderPipelineDescriptor, Shader, ShaderImport,
ShaderProcessor, ShaderReflectError,
},
Expand All @@ -18,8 +18,8 @@ use thiserror::Error;
use wgpu::{PipelineLayoutDescriptor, ShaderModule, VertexBufferLayout as RawVertexBufferLayout};

enum PipelineDescriptor {
RenderPipelineDescriptor(RenderPipelineDescriptor),
ComputePipelineDescriptor(ComputePipelineDescriptor),
RenderPipelineDescriptor(Box<RenderPipelineDescriptor>),
ComputePipelineDescriptor(Box<ComputePipelineDescriptor>),
}

#[derive(Debug)]
Expand Down Expand Up @@ -242,8 +242,31 @@ impl PipelineCache {
}

#[inline]
pub fn get_descriptor(&self, id: CachedPipelineId) -> &RenderPipelineDescriptor {
&self.pipelines[id.0].descriptor
pub fn get_render_pipeline_descriptor(
&self,
id: CachedPipelineId,
) -> Option<&RenderPipelineDescriptor> {
if let PipelineDescriptor::RenderPipelineDescriptor(descriptor) =
&self.pipelines[id.0].descriptor
{
Some(descriptor)
} else {
None
}
}

#[inline]
pub fn get_compute_pipeline_descriptor(
&self,
id: CachedPipelineId,
) -> Option<&ComputePipelineDescriptor> {
if let PipelineDescriptor::ComputePipelineDescriptor(descriptor) =
&self.pipelines[id.0].descriptor
{
Some(descriptor)
} else {
None
}
}

#[inline]
Expand Down Expand Up @@ -274,7 +297,7 @@ impl PipelineCache {
) -> CachedPipelineId {
let id = CachedPipelineId(self.pipelines.len());
self.pipelines.push(CachedPipeline {
descriptor: PipelineDescriptor::RenderPipelineDescriptor(descriptor),
descriptor: PipelineDescriptor::RenderPipelineDescriptor(Box::new(descriptor)),
state: CachedPipelineState::Queued,
});
self.waiting_pipelines.insert(id);
Expand All @@ -287,7 +310,7 @@ impl PipelineCache {
) -> CachedPipelineId {
let id = CachedPipelineId(self.pipelines.len());
self.pipelines.push(CachedPipeline {
descriptor: PipelineDescriptor::ComputePipelineDescriptor(descriptor),
descriptor: PipelineDescriptor::ComputePipelineDescriptor(Box::new(descriptor)),
state: CachedPipelineState::Queued,
});
self.waiting_pipelines.insert(id);
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_render/src/render_resource/pipeline_specializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
use bevy_utils::{
hashbrown::hash_map::RawEntryMut, tracing::error, Entry, HashMap, PreHashMap, PreHashMapExt,
};
use std::fmt::Debug;
use std::hash::Hash;
use std::{fmt::Debug, hash::Hash};
use thiserror::Error;

pub trait SpecializedRenderPipeline {
Expand Down Expand Up @@ -74,12 +73,6 @@ impl<S: SpecializedComputePipeline> SpecializedComputePipelines<S> {
}
}

#[derive(Error, Debug)]
pub enum SpecializedMeshPipelineError {
#[error(transparent)]
MissingVertexAttribute(#[from] MissingVertexAttributeError),
}

pub trait SpecializedMeshPipeline {
type Key: Clone + Hash + PartialEq + Eq;
fn specialize(
Expand Down Expand Up @@ -107,7 +100,7 @@ impl<S: SpecializedMeshPipeline> SpecializedMeshPipelines<S> {
#[inline]
pub fn specialize(
&mut self,
cache: &mut RenderPipelineCache,
cache: &mut PipelineCache,
specialize_pipeline: &S,
key: S::Key,
layout: &MeshVertexBufferLayout,
Expand Down Expand Up @@ -145,18 +138,25 @@ impl<S: SpecializedMeshPipeline> SpecializedMeshPipelines<S> {
Ok(*entry.insert(match layout_map.entry(key) {
Entry::Occupied(entry) => {
if cfg!(debug_assertions) {
let stored_descriptor = cache.get_descriptor(*entry.get());
let stored_descriptor =
cache.get_render_pipeline_descriptor(*entry.get()).expect("The cached pipeline is not a render pipeline.");
if stored_descriptor != &descriptor {
error!("The cached pipeline descriptor for {} is not equal to the generated descriptor for the given key. This means the SpecializePipeline implementation uses 'unused' MeshVertexBufferLayout information to specialize the pipeline. This is not allowed because it would invalidate the pipeline cache.", std::any::type_name::<S>());
}
}
*entry.into_mut()
}
Entry::Vacant(entry) => {
*entry.insert(cache.queue(descriptor))
*entry.insert(cache.queue_render_pipeline(descriptor))
}
}))
}
}
}
}

#[derive(Error, Debug)]
pub enum SpecializedMeshPipelineError {
#[error(transparent)]
MissingVertexAttribute(#[from] MissingVertexAttributeError),
}
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/mesh2d/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ pub fn queue_material2d_meshes<M: SpecializedMaterial2d>(
transparent_draw_functions: Res<DrawFunctions<Transparent2d>>,
material2d_pipeline: Res<Material2dPipeline<M>>,
mut pipelines: ResMut<SpecializedMeshPipelines<Material2dPipeline<M>>>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut pipeline_cache: ResMut<PipelineCache>,
msaa: Res<Msaa>,
render_meshes: Res<RenderAssets<Mesh>>,
render_materials: Res<RenderAssets<M>>,
Expand Down
10 changes: 7 additions & 3 deletions examples/shader/compute_shader_game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,13 @@ impl Default for DispatchGameOfLife {

impl render_graph::Node for DispatchGameOfLife {
fn update(&mut self, world: &mut World) {
let mut pipelines = world.resource_mut::<SpecializedComputePipelines<GameOfLifePipeline>>();
let mut pipeline_cache = world.resource_mut::<PipelineCache>();
let game_of_life_pipeline = world.resource::<GameOfLifePipeline>();
let world = world.cell();

let mut pipelines = world
.get_resource_mut::<SpecializedComputePipelines<GameOfLifePipeline>>()
.unwrap();
let mut pipeline_cache = world.get_resource_mut::<PipelineCache>().unwrap();
let game_of_life_pipeline = world.get_resource::<GameOfLifePipeline>().unwrap();

self.init_pipeline = pipelines.specialize(
&mut pipeline_cache,
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn queue_custom(
custom_pipeline: Res<IsRedPipeline>,
msaa: Res<Msaa>,
mut pipelines: ResMut<SpecializedMeshPipelines<IsRedPipeline>>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut pipeline_cache: ResMut<PipelineCache>,
material_meshes: Query<(Entity, &Handle<Mesh>, &MeshUniform, &IsRed)>,
mut views: Query<(&ExtractedView, &mut RenderPhase<Transparent3d>)>,
) {
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn queue_custom(
custom_pipeline: Res<CustomPipeline>,
msaa: Res<Msaa>,
mut pipelines: ResMut<SpecializedMeshPipelines<CustomPipeline>>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut pipeline_cache: ResMut<PipelineCache>,
meshes: Res<RenderAssets<Mesh>>,
material_meshes: Query<
(Entity, &MeshUniform, &Handle<Mesh>),
Expand Down

0 comments on commit be2b972

Please sign in to comment.