Skip to content

Commit e516011

Browse files
committed
Move ShaderCache shader defs into PipelineCache
1 parent 6dda873 commit e516011

File tree

21 files changed

+132
-65
lines changed

21 files changed

+132
-65
lines changed

crates/bevy_core_pipeline/src/blit/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ pub struct BlitPipelineKey {
7777
impl SpecializedRenderPipeline for BlitPipeline {
7878
type Key = BlitPipelineKey;
7979

80-
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
80+
fn specialize(
81+
&self,
82+
key: Self::Key,
83+
shader_defs: Vec<ShaderDefVal>,
84+
) -> RenderPipelineDescriptor {
8185
RenderPipelineDescriptor {
8286
label: Some("blit pipeline".into()),
8387
layout: vec![self.texture_bind_group.clone()],
84-
vertex: fullscreen_shader_vertex_state(),
88+
vertex: fullscreen_shader_vertex_state(shader_defs.clone()),
8589
fragment: Some(FragmentState {
8690
shader: BLIT_SHADER_HANDLE.typed(),
87-
shader_defs: vec![],
91+
shader_defs,
8892
entry_point: "fs_main".into(),
8993
targets: vec![Some(ColorTargetState {
9094
format: key.texture_format,

crates/bevy_core_pipeline/src/bloom/downsampling_pipeline.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ impl FromWorld for BloomDownsamplingPipeline {
9999
impl SpecializedRenderPipeline for BloomDownsamplingPipeline {
100100
type Key = BloomDownsamplingPipelineKeys;
101101

102-
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
102+
fn specialize(
103+
&self,
104+
key: Self::Key,
105+
mut shader_defs: Vec<ShaderDefVal>,
106+
) -> RenderPipelineDescriptor {
103107
let layout = vec![self.bind_group_layout.clone()];
104108

105109
let entry_point = if key.first_downsample {
@@ -108,8 +112,6 @@ impl SpecializedRenderPipeline for BloomDownsamplingPipeline {
108112
"downsample".into()
109113
};
110114

111-
let mut shader_defs = vec![];
112-
113115
if key.first_downsample {
114116
shader_defs.push("FIRST_DOWNSAMPLE".into());
115117
}
@@ -128,7 +130,7 @@ impl SpecializedRenderPipeline for BloomDownsamplingPipeline {
128130
.into(),
129131
),
130132
layout,
131-
vertex: fullscreen_shader_vertex_state(),
133+
vertex: fullscreen_shader_vertex_state(shader_defs.clone()),
132134
fragment: Some(FragmentState {
133135
shader: BLOOM_SHADER_HANDLE.typed::<Shader>(),
134136
shader_defs,

crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ impl FromWorld for BloomUpsamplingPipeline {
7474
impl SpecializedRenderPipeline for BloomUpsamplingPipeline {
7575
type Key = BloomUpsamplingPipelineKeys;
7676

77-
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
77+
fn specialize(
78+
&self,
79+
key: Self::Key,
80+
shader_defs: Vec<ShaderDefVal>,
81+
) -> RenderPipelineDescriptor {
7882
let texture_format = if key.final_pipeline {
7983
ViewTarget::TEXTURE_FORMAT_HDR
8084
} else {
@@ -116,10 +120,10 @@ impl SpecializedRenderPipeline for BloomUpsamplingPipeline {
116120
RenderPipelineDescriptor {
117121
label: Some("bloom_upsampling_pipeline".into()),
118122
layout: vec![self.bind_group_layout.clone()],
119-
vertex: fullscreen_shader_vertex_state(),
123+
vertex: fullscreen_shader_vertex_state(shader_defs.clone()),
120124
fragment: Some(FragmentState {
121125
shader: BLOOM_SHADER_HANDLE.typed::<Shader>(),
122-
shader_defs: vec![],
126+
shader_defs,
123127
entry_point: "upsample".into(),
124128
targets: vec![Some(ColorTargetState {
125129
format: texture_format,

crates/bevy_core_pipeline/src/fullscreen_vertex_shader/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use bevy_asset::HandleUntyped;
22
use bevy_reflect::TypeUuid;
3-
use bevy_render::{prelude::Shader, render_resource::VertexState};
3+
use bevy_render::{
4+
prelude::Shader,
5+
render_resource::{ShaderDefVal, VertexState},
6+
};
47

58
pub const FULLSCREEN_SHADER_HANDLE: HandleUntyped =
69
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 7837534426033940724);
@@ -16,10 +19,10 @@ pub const FULLSCREEN_SHADER_HANDLE: HandleUntyped =
1619
/// ```
1720
/// from the vertex shader.
1821
/// The draw call should render one triangle: `render_pass.draw(0..3, 0..1);`
19-
pub fn fullscreen_shader_vertex_state() -> VertexState {
22+
pub fn fullscreen_shader_vertex_state(shader_defs: Vec<ShaderDefVal>) -> VertexState {
2023
VertexState {
2124
shader: FULLSCREEN_SHADER_HANDLE.typed(),
22-
shader_defs: Vec::new(),
25+
shader_defs,
2326
entry_point: "fullscreen_vertex_shader".into(),
2427
buffers: Vec::new(),
2528
}

crates/bevy_core_pipeline/src/fxaa/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,24 @@ pub struct FxaaPipelineKey {
177177
impl SpecializedRenderPipeline for FxaaPipeline {
178178
type Key = FxaaPipelineKey;
179179

180-
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
180+
fn specialize(
181+
&self,
182+
key: Self::Key,
183+
shader_defs: Vec<ShaderDefVal>,
184+
) -> RenderPipelineDescriptor {
181185
RenderPipelineDescriptor {
182186
label: Some("fxaa".into()),
183187
layout: vec![self.texture_bind_group.clone()],
184-
vertex: fullscreen_shader_vertex_state(),
188+
vertex: fullscreen_shader_vertex_state(shader_defs.clone()),
185189
fragment: Some(FragmentState {
186190
shader: FXAA_SHADER_HANDLE.typed(),
187-
shader_defs: vec![
188-
format!("EDGE_THRESH_{}", key.edge_threshold.get_str()).into(),
189-
format!("EDGE_THRESH_MIN_{}", key.edge_threshold_min.get_str()).into(),
190-
],
191+
shader_defs: shader_defs
192+
.into_iter()
193+
.chain([
194+
format!("EDGE_THRESH_{}", key.edge_threshold.get_str()).into(),
195+
format!("EDGE_THRESH_MIN_{}", key.edge_threshold_min.get_str()).into(),
196+
])
197+
.collect(),
191198
entry_point: "fragment".into(),
192199
targets: vec![Some(ColorTargetState {
193200
format: key.texture_format,

crates/bevy_core_pipeline/src/tonemapping/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ pub struct TonemappingPipelineKey {
184184
impl SpecializedRenderPipeline for TonemappingPipeline {
185185
type Key = TonemappingPipelineKey;
186186

187-
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
188-
let mut shader_defs = Vec::new();
187+
fn specialize(
188+
&self,
189+
key: Self::Key,
190+
mut shader_defs: Vec<ShaderDefVal>,
191+
) -> RenderPipelineDescriptor {
189192
if let DebandDither::Enabled = key.deband_dither {
190193
shader_defs.push("DEBAND_DITHER".into());
191194
}
@@ -208,7 +211,7 @@ impl SpecializedRenderPipeline for TonemappingPipeline {
208211
RenderPipelineDescriptor {
209212
label: Some("tonemapping pipeline".into()),
210213
layout: vec![self.texture_bind_group.clone()],
211-
vertex: fullscreen_shader_vertex_state(),
214+
vertex: fullscreen_shader_vertex_state(shader_defs.clone()),
212215
fragment: Some(FragmentState {
213216
shader: TONEMAPPING_SHADER_HANDLE.typed(),
214217
shader_defs,

crates/bevy_gizmos/src/pipeline_2d.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl SpecializedMeshPipeline for GizmoLinePipeline {
4141
&self,
4242
key: Self::Key,
4343
layout: &MeshVertexBufferLayout,
44+
shader_defs: Vec<ShaderDefVal>,
4445
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
4546
let vertex_buffer_layout = layout.get_layout(&[
4647
Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
@@ -57,12 +58,12 @@ impl SpecializedMeshPipeline for GizmoLinePipeline {
5758
vertex: VertexState {
5859
shader: self.shader.clone_weak(),
5960
entry_point: "vertex".into(),
60-
shader_defs: vec![],
61+
shader_defs: shader_defs.clone(),
6162
buffers: vec![vertex_buffer_layout],
6263
},
6364
fragment: Some(FragmentState {
6465
shader: self.shader.clone_weak(),
65-
shader_defs: vec![],
66+
shader_defs,
6667
entry_point: "fragment".into(),
6768
targets: vec![Some(ColorTargetState {
6869
format,

crates/bevy_gizmos/src/pipeline_3d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl SpecializedMeshPipeline for GizmoPipeline {
4545
&self,
4646
(depth_test, key): Self::Key,
4747
layout: &MeshVertexBufferLayout,
48+
mut shader_defs: Vec<ShaderDefVal>,
4849
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
49-
let mut shader_defs = Vec::new();
5050
shader_defs.push("GIZMO_LINES_3D".into());
5151
shader_defs.push(ShaderDefVal::Int(
5252
"MAX_DIRECTIONAL_LIGHTS".to_string(),

crates/bevy_pbr/src/material.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use bevy_render::{
2929
},
3030
render_resource::{
3131
AsBindGroup, AsBindGroupError, BindGroup, BindGroupLayout, OwnedBindingResource,
32-
PipelineCache, RenderPipelineDescriptor, Shader, ShaderRef, SpecializedMeshPipeline,
33-
SpecializedMeshPipelineError, SpecializedMeshPipelines,
32+
PipelineCache, RenderPipelineDescriptor, Shader, ShaderDefVal, ShaderRef,
33+
SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
3434
},
3535
renderer::RenderDevice,
3636
texture::FallbackImage,
@@ -292,8 +292,11 @@ where
292292
&self,
293293
key: Self::Key,
294294
layout: &MeshVertexBufferLayout,
295+
shader_defs: Vec<ShaderDefVal>,
295296
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
296-
let mut descriptor = self.mesh_pipeline.specialize(key.mesh_key, layout)?;
297+
let mut descriptor = self
298+
.mesh_pipeline
299+
.specialize(key.mesh_key, layout, shader_defs)?;
297300
if let Some(vertex_shader) = &self.vertex_shader {
298301
descriptor.vertex.shader = vertex_shader.clone();
299302
}

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ where
230230
&self,
231231
key: Self::Key,
232232
layout: &MeshVertexBufferLayout,
233+
mut shader_defs: Vec<ShaderDefVal>,
233234
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
234235
let mut bind_group_layout = vec![self.view_layout.clone()];
235-
let mut shader_defs = Vec::new();
236236
let mut vertex_attributes = Vec::new();
237237

238238
// NOTE: Eventually, it would be nice to only add this when the shaders are overloaded by the Material.

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,8 @@ impl SpecializedMeshPipeline for MeshPipeline {
653653
&self,
654654
key: Self::Key,
655655
layout: &MeshVertexBufferLayout,
656+
mut shader_defs: Vec<ShaderDefVal>,
656657
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
657-
let mut shader_defs = Vec::new();
658658
let mut vertex_attributes = Vec::new();
659659

660660
if layout.contains(Mesh::ATTRIBUTE_POSITION) {

crates/bevy_pbr/src/wireframe.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bevy_ecs::{prelude::*, reflect::ReflectComponent};
77
use bevy_reflect::std_traits::ReflectDefault;
88
use bevy_reflect::{Reflect, TypeUuid};
99
use bevy_render::extract_component::{ExtractComponent, ExtractComponentPlugin};
10+
use bevy_render::render_resource::ShaderDefVal;
1011
use bevy_render::Render;
1112
use bevy_render::{
1213
extract_resource::{ExtractResource, ExtractResourcePlugin},
@@ -86,8 +87,9 @@ impl SpecializedMeshPipeline for WireframePipeline {
8687
&self,
8788
key: Self::Key,
8889
layout: &MeshVertexBufferLayout,
90+
shader_defs: Vec<ShaderDefVal>,
8991
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
90-
let mut descriptor = self.mesh_pipeline.specialize(key, layout)?;
92+
let mut descriptor = self.mesh_pipeline.specialize(key, layout, shader_defs)?;
9193
descriptor.vertex.shader = self.shader.clone_weak();
9294
descriptor.fragment.as_mut().unwrap().shader = self.shader.clone_weak();
9395
descriptor.primitive.polygon_mode = PolygonMode::Line;

crates/bevy_render/src/render_resource/pipeline_cache.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,13 @@ impl ShaderCache {
193193
let module = match data.processed_shaders.entry(shader_defs.to_vec()) {
194194
Entry::Occupied(entry) => entry.into_mut(),
195195
Entry::Vacant(entry) => {
196-
let mut shader_defs = shader_defs.to_vec();
197-
#[cfg(feature = "webgl")]
198-
{
199-
shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
200-
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
201-
}
202-
203-
shader_defs.push(ShaderDefVal::UInt(
204-
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
205-
render_device.limits().max_storage_buffers_per_shader_stage,
206-
));
207-
208196
debug!(
209197
"processing shader {:?}, with shader defs {:?}",
210198
handle, shader_defs
211199
);
212200
let processed = self.processor.process(
213201
shader,
214-
&shader_defs,
202+
shader_defs,
215203
&self.shaders,
216204
&self.import_path_shaders,
217205
)?;
@@ -311,6 +299,7 @@ impl ShaderCache {
311299
}
312300

313301
type LayoutCacheKey = (Vec<BindGroupLayoutId>, Vec<PushConstantRange>);
302+
314303
#[derive(Default)]
315304
struct LayoutCache {
316305
layouts: HashMap<LayoutCacheKey, ErasedPipelineLayout>,
@@ -362,22 +351,41 @@ pub struct PipelineCache {
362351
pipelines: Vec<CachedPipeline>,
363352
waiting_pipelines: HashSet<CachedPipelineId>,
364353
new_pipelines: Mutex<Vec<CachedPipeline>>,
354+
base_shader_defs: Vec<ShaderDefVal>,
365355
}
366356

367357
impl PipelineCache {
368358
pub fn pipelines(&self) -> impl Iterator<Item = &CachedPipeline> {
369359
self.pipelines.iter()
370360
}
371361

362+
pub fn base_shader_defs(&self) -> Vec<ShaderDefVal> {
363+
self.base_shader_defs.clone()
364+
}
365+
372366
/// Create a new pipeline cache associated with the given render device.
373367
pub fn new(device: RenderDevice) -> Self {
368+
let mut base_shader_defs = Vec::new();
369+
370+
#[cfg(feature = "webgl")]
371+
{
372+
base_shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into());
373+
base_shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
374+
}
375+
376+
base_shader_defs.push(ShaderDefVal::UInt(
377+
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
378+
device.limits().max_storage_buffers_per_shader_stage,
379+
));
380+
374381
Self {
375382
device,
376383
layout_cache: default(),
377384
shader_cache: default(),
378385
waiting_pipelines: default(),
379386
new_pipelines: default(),
380387
pipelines: default(),
388+
base_shader_defs,
381389
}
382390
}
383391

crates/bevy_render/src/render_resource/pipeline_specializer.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ use bevy_utils::{
1414
use std::{fmt::Debug, hash::Hash};
1515
use thiserror::Error;
1616

17+
use super::ShaderDefVal;
18+
1719
pub trait SpecializedRenderPipeline {
1820
type Key: Clone + Hash + PartialEq + Eq;
19-
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor;
21+
fn specialize(
22+
&self,
23+
key: Self::Key,
24+
shader_defs: Vec<ShaderDefVal>,
25+
) -> RenderPipelineDescriptor;
2026
}
2127

2228
#[derive(Resource)]
@@ -38,15 +44,19 @@ impl<S: SpecializedRenderPipeline> SpecializedRenderPipelines<S> {
3844
key: S::Key,
3945
) -> CachedRenderPipelineId {
4046
*self.cache.entry(key.clone()).or_insert_with(|| {
41-
let descriptor = specialize_pipeline.specialize(key);
47+
let descriptor = specialize_pipeline.specialize(key, cache.base_shader_defs());
4248
cache.queue_render_pipeline(descriptor)
4349
})
4450
}
4551
}
4652

4753
pub trait SpecializedComputePipeline {
4854
type Key: Clone + Hash + PartialEq + Eq;
49-
fn specialize(&self, key: Self::Key) -> ComputePipelineDescriptor;
55+
fn specialize(
56+
&self,
57+
key: Self::Key,
58+
shader_defs: Vec<ShaderDefVal>,
59+
) -> ComputePipelineDescriptor;
5060
}
5161

5262
#[derive(Resource)]
@@ -68,7 +78,7 @@ impl<S: SpecializedComputePipeline> SpecializedComputePipelines<S> {
6878
key: S::Key,
6979
) -> CachedComputePipelineId {
7080
*self.cache.entry(key.clone()).or_insert_with(|| {
71-
let descriptor = specialize_pipeline.specialize(key);
81+
let descriptor = specialize_pipeline.specialize(key, cache.base_shader_defs());
7282
cache.queue_compute_pipeline(descriptor)
7383
})
7484
}
@@ -80,6 +90,7 @@ pub trait SpecializedMeshPipeline {
8090
&self,
8191
key: Self::Key,
8292
layout: &MeshVertexBufferLayout,
93+
shader_defs: Vec<ShaderDefVal>,
8394
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError>;
8495
}
8596

@@ -115,7 +126,7 @@ impl<S: SpecializedMeshPipeline> SpecializedMeshPipelines<S> {
115126
Entry::Occupied(entry) => Ok(*entry.into_mut()),
116127
Entry::Vacant(entry) => {
117128
let descriptor = specialize_pipeline
118-
.specialize(key.clone(), layout)
129+
.specialize(key.clone(), layout, cache.base_shader_defs())
119130
.map_err(|mut err| {
120131
{
121132
let SpecializedMeshPipelineError::MissingVertexAttribute(err) =

0 commit comments

Comments
 (0)