Skip to content

Commit

Permalink
Move ShaderCache shader defs into pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
Shfty committed Mar 19, 2023
1 parent 2010164 commit cd9550f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
8 changes: 6 additions & 2 deletions crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use bevy_render::{
ShaderType, SpecializedMeshPipeline, SpecializedMeshPipelineError,
SpecializedMeshPipelines, StencilFaceState, StencilState, TextureDescriptor,
TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureViewDimension,
VertexState,
VertexState, platform_shader_defs,
},
renderer::RenderDevice,
texture::{FallbackImagesDepth, FallbackImagesMsaa, TextureCache},
Expand Down Expand Up @@ -161,6 +161,7 @@ pub struct PrepassPipeline<M: Material> {
pub material_vertex_shader: Option<Handle<Shader>>,
pub material_fragment_shader: Option<Handle<Shader>>,
pub material_pipeline: MaterialPipeline<M>,
pub shader_defs: Vec<ShaderDefVal>,
_marker: PhantomData<M>,
}

Expand Down Expand Up @@ -199,6 +200,8 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {

let mesh_pipeline = world.resource::<MeshPipeline>();

let shader_defs = platform_shader_defs(render_device);

PrepassPipeline {
view_layout,
mesh_layout: mesh_pipeline.mesh_layout.clone(),
Expand All @@ -215,6 +218,7 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {
},
material_layout: M::bind_group_layout(render_device),
material_pipeline: world.resource::<MaterialPipeline<M>>().clone(),
shader_defs,
_marker: PhantomData,
}
}
Expand All @@ -232,7 +236,7 @@ where
layout: &MeshVertexBufferLayout,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
let mut bind_group_layout = vec![self.view_layout.clone()];
let mut shader_defs = Vec::new();
let mut shader_defs = self.shader_defs.clone();
let mut vertex_attributes = Vec::new();

// NOTE: Eventually, it would be nice to only add this when the shaders are overloaded by the Material.
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ pub struct MeshPipeline {
// This dummy white texture is to be used in place of optional StandardMaterial textures
pub dummy_white_gpu_image: GpuImage,
pub clustered_forward_buffer_binding_type: BufferBindingType,
pub shader_defs: Vec<ShaderDefVal>,
}

impl FromWorld for MeshPipeline {
Expand Down Expand Up @@ -532,13 +533,17 @@ impl FromWorld for MeshPipeline {
}
};

// Shader defs
let shader_defs = platform_shader_defs(&render_device);

MeshPipeline {
view_layout,
view_layout_multisampled,
mesh_layout,
skinned_mesh_layout,
clustered_forward_buffer_binding_type,
dummy_white_gpu_image,
shader_defs,
}
}
}
Expand Down Expand Up @@ -654,7 +659,7 @@ impl SpecializedMeshPipeline for MeshPipeline {
key: Self::Key,
layout: &MeshVertexBufferLayout,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
let mut shader_defs = Vec::new();
let mut shader_defs = self.shader_defs.clone();
let mut vertex_attributes = Vec::new();

if layout.contains(Mesh::ATTRIBUTE_POSITION) {
Expand Down
36 changes: 22 additions & 14 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use parking_lot::Mutex;
use std::{hash::Hash, iter::FusedIterator, mem, ops::Deref};
use thiserror::Error;
use wgpu::{
PipelineLayoutDescriptor, PushConstantRange, VertexBufferLayout as RawVertexBufferLayout,
PipelineLayoutDescriptor, PushConstantRange,
VertexBufferLayout as RawVertexBufferLayout,
};

use crate::render_resource::resource_macros::*;
Expand Down Expand Up @@ -193,25 +194,13 @@ impl ShaderCache {
let module = match data.processed_shaders.entry(shader_defs.to_vec()) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let mut shader_defs = shader_defs.to_vec();
#[cfg(feature = "webgl")]
{
shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
}

shader_defs.push(ShaderDefVal::UInt(
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
render_device.limits().max_storage_buffers_per_shader_stage,
));

debug!(
"processing shader {:?}, with shader defs {:?}",
handle, shader_defs
);
let processed = self.processor.process(
shader,
&shader_defs,
shader_defs,
&self.shaders,
&self.import_path_shaders,
)?;
Expand Down Expand Up @@ -310,7 +299,26 @@ impl ShaderCache {
}
}

// Utility function for initializing a set of platform-driven shader defs from a render device
pub fn platform_shader_defs(render_device: &RenderDevice) -> Vec<ShaderDefVal> {
let mut shader_defs = Vec::new();

#[cfg(feature = "webgl")]
{
shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
}

shader_defs.push(ShaderDefVal::UInt(
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
render_device.limits().max_storage_buffers_per_shader_stage,
));

shader_defs
}

type LayoutCacheKey = (Vec<BindGroupLayoutId>, Vec<PushConstantRange>);

#[derive(Default)]
struct LayoutCache {
layouts: HashMap<LayoutCacheKey, ErasedPipelineLayout>,
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub struct Mesh2dPipeline {
pub mesh_layout: BindGroupLayout,
// This dummy white texture is to be used in place of optional textures
pub dummy_white_gpu_image: GpuImage,
pub shader_defs: Vec<ShaderDefVal>,
}

impl FromWorld for Mesh2dPipeline {
Expand Down Expand Up @@ -211,6 +212,10 @@ impl FromWorld for Mesh2dPipeline {
}],
label: Some("mesh2d_layout"),
});

// Shader defs
let shader_defs = platform_shader_defs(&render_device);

// A 1x1x1 'all 1.0' texture to use as a dummy texture to use in place of optional StandardMaterial textures
let dummy_white_gpu_image = {
let image = Image::default();
Expand Down Expand Up @@ -256,10 +261,12 @@ impl FromWorld for Mesh2dPipeline {
mip_level_count: image.texture_descriptor.mip_level_count,
}
};

Mesh2dPipeline {
view_layout,
mesh_layout,
dummy_white_gpu_image,
shader_defs,
}
}
}
Expand Down Expand Up @@ -362,7 +369,7 @@ impl SpecializedMeshPipeline for Mesh2dPipeline {
key: Self::Key,
layout: &MeshVertexBufferLayout,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
let mut shader_defs = Vec::new();
let mut shader_defs = self.shader_defs.clone();
let mut vertex_attributes = Vec::new();

if layout.contains(Mesh::ATTRIBUTE_POSITION) {
Expand Down

0 comments on commit cd9550f

Please sign in to comment.