Skip to content

Commit adf8211

Browse files
Zeophliteatlv24
andauthored
Add BindGroupLayout caching via descriptors (#21205)
# Objective - Defer creating `BindGroupLayout` by using a `BindGroupLayoutDescriptor` and cache the results - Unblocks `bevy_material` (render-less material definitions) - Blocked by #21533 ## Solution - Reviewers, look at first commit for mechanism, and following for usage ## Testing - CI --------- Co-authored-by: atlas <[email protected]>
1 parent c224bae commit adf8211

File tree

69 files changed

+924
-660
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+924
-660
lines changed

crates/bevy_anti_alias/src/contrast_adaptive_sharpening/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl Plugin for CasPlugin {
157157

158158
#[derive(Resource)]
159159
pub struct CasPipeline {
160-
texture_bind_group: BindGroupLayout,
160+
texture_bind_group: BindGroupLayoutDescriptor,
161161
sampler: Sampler,
162162
fullscreen_shader: FullscreenShader,
163163
fragment_shader: Handle<Shader>,
@@ -169,7 +169,7 @@ pub fn init_cas_pipeline(
169169
fullscreen_shader: Res<FullscreenShader>,
170170
asset_server: Res<AssetServer>,
171171
) {
172-
let texture_bind_group = render_device.create_bind_group_layout(
172+
let texture_bind_group = BindGroupLayoutDescriptor::new(
173173
"sharpening_texture_bind_group_layout",
174174
&BindGroupLayoutEntries::sequential(
175175
ShaderStages::FRAGMENT,

crates/bevy_anti_alias/src/contrast_adaptive_sharpening/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Node for CasNode {
8383
cached_bind_group => {
8484
let bind_group = render_context.render_device().create_bind_group(
8585
"cas_bind_group",
86-
&sharpening_pipeline.texture_bind_group,
86+
&pipeline_cache.get_bind_group_layout(&sharpening_pipeline.texture_bind_group),
8787
&BindGroupEntries::sequential((
8888
view_target.source,
8989
&sharpening_pipeline.sampler,

crates/bevy_anti_alias/src/fxaa/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Plugin for FxaaPlugin {
123123

124124
#[derive(Resource)]
125125
pub struct FxaaPipeline {
126-
texture_bind_group: BindGroupLayout,
126+
texture_bind_group: BindGroupLayoutDescriptor,
127127
sampler: Sampler,
128128
fullscreen_shader: FullscreenShader,
129129
fragment_shader: Handle<Shader>,
@@ -135,7 +135,7 @@ pub fn init_fxaa_pipeline(
135135
fullscreen_shader: Res<FullscreenShader>,
136136
asset_server: Res<AssetServer>,
137137
) {
138-
let texture_bind_group = render_device.create_bind_group_layout(
138+
let texture_bind_group = BindGroupLayoutDescriptor::new(
139139
"fxaa_texture_bind_group_layout",
140140
&BindGroupLayoutEntries::sequential(
141141
ShaderStages::FRAGMENT,

crates/bevy_anti_alias/src/fxaa/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl ViewNode for FxaaNode {
5454
cached_bind_group => {
5555
let bind_group = render_context.render_device().create_bind_group(
5656
None,
57-
&fxaa_pipeline.texture_bind_group,
57+
&pipeline_cache.get_bind_group_layout(&fxaa_pipeline.texture_bind_group),
5858
&BindGroupEntries::sequential((source, &fxaa_pipeline.sampler)),
5959
);
6060

crates/bevy_anti_alias/src/smaa/mod.rs

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ use bevy_render::{
6161
},
6262
render_resource::{
6363
binding_types::{sampler, texture_2d, uniform_buffer},
64-
AddressMode, BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries,
65-
CachedRenderPipelineId, ColorTargetState, ColorWrites, CompareFunction, DepthStencilState,
66-
DynamicUniformBuffer, FilterMode, FragmentState, LoadOp, Operations, PipelineCache,
67-
RenderPassColorAttachment, RenderPassDepthStencilAttachment, RenderPassDescriptor,
68-
RenderPipeline, RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor,
69-
ShaderStages, ShaderType, SpecializedRenderPipeline, SpecializedRenderPipelines,
70-
StencilFaceState, StencilOperation, StencilState, StoreOp, TextureDescriptor,
71-
TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureView,
72-
VertexState,
64+
AddressMode, BindGroup, BindGroupEntries, BindGroupLayoutDescriptor,
65+
BindGroupLayoutEntries, CachedRenderPipelineId, ColorTargetState, ColorWrites,
66+
CompareFunction, DepthStencilState, DynamicUniformBuffer, FilterMode, FragmentState,
67+
LoadOp, Operations, PipelineCache, RenderPassColorAttachment,
68+
RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline,
69+
RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderStages, ShaderType,
70+
SpecializedRenderPipeline, SpecializedRenderPipelines, StencilFaceState, StencilOperation,
71+
StencilState, StoreOp, TextureDescriptor, TextureDimension, TextureFormat,
72+
TextureSampleType, TextureUsages, TextureView, VertexState,
7373
},
7474
renderer::{RenderContext, RenderDevice, RenderQueue},
7575
texture::{CachedTexture, GpuImage, TextureCache},
@@ -143,29 +143,29 @@ pub struct SmaaPipelines {
143143
/// The pipeline data for phase 1 of SMAA: edge detection.
144144
struct SmaaEdgeDetectionPipeline {
145145
/// The bind group layout common to all passes.
146-
postprocess_bind_group_layout: BindGroupLayout,
146+
postprocess_bind_group_layout: BindGroupLayoutDescriptor,
147147
/// The bind group layout for data specific to this pass.
148-
edge_detection_bind_group_layout: BindGroupLayout,
148+
edge_detection_bind_group_layout: BindGroupLayoutDescriptor,
149149
/// The shader asset handle.
150150
shader: Handle<Shader>,
151151
}
152152

153153
/// The pipeline data for phase 2 of SMAA: blending weight calculation.
154154
struct SmaaBlendingWeightCalculationPipeline {
155155
/// The bind group layout common to all passes.
156-
postprocess_bind_group_layout: BindGroupLayout,
156+
postprocess_bind_group_layout: BindGroupLayoutDescriptor,
157157
/// The bind group layout for data specific to this pass.
158-
blending_weight_calculation_bind_group_layout: BindGroupLayout,
158+
blending_weight_calculation_bind_group_layout: BindGroupLayoutDescriptor,
159159
/// The shader asset handle.
160160
shader: Handle<Shader>,
161161
}
162162

163163
/// The pipeline data for phase 3 of SMAA: neighborhood blending.
164164
struct SmaaNeighborhoodBlendingPipeline {
165165
/// The bind group layout common to all passes.
166-
postprocess_bind_group_layout: BindGroupLayout,
166+
postprocess_bind_group_layout: BindGroupLayoutDescriptor,
167167
/// The bind group layout for data specific to this pass.
168-
neighborhood_blending_bind_group_layout: BindGroupLayout,
168+
neighborhood_blending_bind_group_layout: BindGroupLayoutDescriptor,
169169
/// The shader asset handle.
170170
shader: Handle<Shader>,
171171
}
@@ -373,13 +373,9 @@ impl Plugin for SmaaPlugin {
373373
}
374374
}
375375

376-
pub fn init_smaa_pipelines(
377-
mut commands: Commands,
378-
render_device: Res<RenderDevice>,
379-
asset_server: Res<AssetServer>,
380-
) {
376+
pub fn init_smaa_pipelines(mut commands: Commands, asset_server: Res<AssetServer>) {
381377
// Create the postprocess bind group layout (all passes, bind group 0).
382-
let postprocess_bind_group_layout = render_device.create_bind_group_layout(
378+
let postprocess_bind_group_layout = BindGroupLayoutDescriptor::new(
383379
"SMAA postprocess bind group layout",
384380
&BindGroupLayoutEntries::sequential(
385381
ShaderStages::FRAGMENT,
@@ -391,7 +387,7 @@ pub fn init_smaa_pipelines(
391387
);
392388

393389
// Create the edge detection bind group layout (pass 1, bind group 1).
394-
let edge_detection_bind_group_layout = render_device.create_bind_group_layout(
390+
let edge_detection_bind_group_layout = BindGroupLayoutDescriptor::new(
395391
"SMAA edge detection bind group layout",
396392
&BindGroupLayoutEntries::sequential(
397393
ShaderStages::FRAGMENT,
@@ -400,7 +396,7 @@ pub fn init_smaa_pipelines(
400396
);
401397

402398
// Create the blending weight calculation bind group layout (pass 2, bind group 1).
403-
let blending_weight_calculation_bind_group_layout = render_device.create_bind_group_layout(
399+
let blending_weight_calculation_bind_group_layout = BindGroupLayoutDescriptor::new(
404400
"SMAA blending weight calculation bind group layout",
405401
&BindGroupLayoutEntries::sequential(
406402
ShaderStages::FRAGMENT,
@@ -414,7 +410,7 @@ pub fn init_smaa_pipelines(
414410
);
415411

416412
// Create the neighborhood blending bind group layout (pass 3, bind group 1).
417-
let neighborhood_blending_bind_group_layout = render_device.create_bind_group_layout(
413+
let neighborhood_blending_bind_group_layout = BindGroupLayoutDescriptor::new(
418414
"SMAA neighborhood blending bind group layout",
419415
&BindGroupLayoutEntries::sequential(
420416
ShaderStages::FRAGMENT,
@@ -745,6 +741,7 @@ fn prepare_smaa_bind_groups(
745741
smaa_pipelines: Res<SmaaPipelines>,
746742
smaa_luts: Res<SmaaLuts>,
747743
images: Res<RenderAssets<GpuImage>>,
744+
pipeline_cache: Res<PipelineCache>,
748745
view_targets: Query<(Entity, &SmaaTextures), (With<ExtractedView>, With<Smaa>)>,
749746
) {
750747
// Fetch the two lookup textures. These are bundled in this library.
@@ -771,16 +768,20 @@ fn prepare_smaa_bind_groups(
771768
commands.entity(entity).insert(SmaaBindGroups {
772769
edge_detection_bind_group: render_device.create_bind_group(
773770
Some("SMAA edge detection bind group"),
774-
&smaa_pipelines
775-
.edge_detection
776-
.edge_detection_bind_group_layout,
771+
&pipeline_cache.get_bind_group_layout(
772+
&smaa_pipelines
773+
.edge_detection
774+
.edge_detection_bind_group_layout,
775+
),
777776
&BindGroupEntries::sequential((&sampler,)),
778777
),
779778
blending_weight_calculation_bind_group: render_device.create_bind_group(
780779
Some("SMAA blending weight calculation bind group"),
781-
&smaa_pipelines
782-
.blending_weight_calculation
783-
.blending_weight_calculation_bind_group_layout,
780+
&pipeline_cache.get_bind_group_layout(
781+
&smaa_pipelines
782+
.blending_weight_calculation
783+
.blending_weight_calculation_bind_group_layout,
784+
),
784785
&BindGroupEntries::sequential((
785786
&smaa_textures.edge_detection_color_texture.default_view,
786787
&sampler,
@@ -790,9 +791,11 @@ fn prepare_smaa_bind_groups(
790791
),
791792
neighborhood_blending_bind_group: render_device.create_bind_group(
792793
Some("SMAA neighborhood blending bind group"),
793-
&smaa_pipelines
794-
.neighborhood_blending
795-
.neighborhood_blending_bind_group_layout,
794+
&pipeline_cache.get_bind_group_layout(
795+
&smaa_pipelines
796+
.neighborhood_blending
797+
.neighborhood_blending_bind_group_layout,
798+
),
796799
&BindGroupEntries::sequential((
797800
&smaa_textures.blend_texture.default_view,
798801
&sampler,
@@ -854,6 +857,7 @@ impl ViewNode for SmaaNode {
854857
// Stage 1: Edge detection pass.
855858
perform_edge_detection(
856859
render_context,
860+
pipeline_cache,
857861
smaa_pipelines,
858862
smaa_textures,
859863
view_smaa_bind_groups,
@@ -866,6 +870,7 @@ impl ViewNode for SmaaNode {
866870
// Stage 2: Blending weight calculation pass.
867871
perform_blending_weight_calculation(
868872
render_context,
873+
pipeline_cache,
869874
smaa_pipelines,
870875
smaa_textures,
871876
view_smaa_bind_groups,
@@ -878,6 +883,7 @@ impl ViewNode for SmaaNode {
878883
// Stage 3: Neighborhood blending pass.
879884
perform_neighborhood_blending(
880885
render_context,
886+
pipeline_cache,
881887
smaa_pipelines,
882888
view_smaa_bind_groups,
883889
smaa_info_uniform_buffer,
@@ -902,6 +908,7 @@ impl ViewNode for SmaaNode {
902908
/// examine them.
903909
fn perform_edge_detection(
904910
render_context: &mut RenderContext,
911+
pipeline_cache: &PipelineCache,
905912
smaa_pipelines: &SmaaPipelines,
906913
smaa_textures: &SmaaTextures,
907914
view_smaa_bind_groups: &SmaaBindGroups,
@@ -913,7 +920,8 @@ fn perform_edge_detection(
913920
// Create the edge detection bind group.
914921
let postprocess_bind_group = render_context.render_device().create_bind_group(
915922
None,
916-
&smaa_pipelines.edge_detection.postprocess_bind_group_layout,
923+
&pipeline_cache
924+
.get_bind_group_layout(&smaa_pipelines.edge_detection.postprocess_bind_group_layout),
917925
&BindGroupEntries::sequential((source, &**smaa_info_uniform_buffer)),
918926
);
919927

@@ -956,6 +964,7 @@ fn perform_edge_detection(
956964
/// pixels it doesn't need to examine.
957965
fn perform_blending_weight_calculation(
958966
render_context: &mut RenderContext,
967+
pipeline_cache: &PipelineCache,
959968
smaa_pipelines: &SmaaPipelines,
960969
smaa_textures: &SmaaTextures,
961970
view_smaa_bind_groups: &SmaaBindGroups,
@@ -967,9 +976,11 @@ fn perform_blending_weight_calculation(
967976
// Create the blending weight calculation bind group.
968977
let postprocess_bind_group = render_context.render_device().create_bind_group(
969978
None,
970-
&smaa_pipelines
971-
.blending_weight_calculation
972-
.postprocess_bind_group_layout,
979+
&pipeline_cache.get_bind_group_layout(
980+
&smaa_pipelines
981+
.blending_weight_calculation
982+
.postprocess_bind_group_layout,
983+
),
973984
&BindGroupEntries::sequential((source, &**smaa_info_uniform_buffer)),
974985
);
975986

@@ -1015,6 +1026,7 @@ fn perform_blending_weight_calculation(
10151026
/// texture. It's the only phase that writes to the postprocessing destination.
10161027
fn perform_neighborhood_blending(
10171028
render_context: &mut RenderContext,
1029+
pipeline_cache: &PipelineCache,
10181030
smaa_pipelines: &SmaaPipelines,
10191031
view_smaa_bind_groups: &SmaaBindGroups,
10201032
smaa_info_uniform_buffer: &SmaaInfoUniformBuffer,
@@ -1025,9 +1037,11 @@ fn perform_neighborhood_blending(
10251037
) {
10261038
let postprocess_bind_group = render_context.render_device().create_bind_group(
10271039
None,
1028-
&smaa_pipelines
1029-
.neighborhood_blending
1030-
.postprocess_bind_group_layout,
1040+
&pipeline_cache.get_bind_group_layout(
1041+
&smaa_pipelines
1042+
.neighborhood_blending
1043+
.postprocess_bind_group_layout,
1044+
),
10311045
&BindGroupEntries::sequential((source, &**smaa_info_uniform_buffer)),
10321046
);
10331047

crates/bevy_anti_alias/src/taa/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ use bevy_render::{
2424
render_graph::{NodeRunError, RenderGraphContext, RenderGraphExt, ViewNode, ViewNodeRunner},
2525
render_resource::{
2626
binding_types::{sampler, texture_2d, texture_depth_2d},
27-
BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries, CachedRenderPipelineId,
28-
ColorTargetState, ColorWrites, FilterMode, FragmentState, Operations, PipelineCache,
29-
RenderPassColorAttachment, RenderPassDescriptor, RenderPipelineDescriptor, Sampler,
30-
SamplerBindingType, SamplerDescriptor, ShaderStages, SpecializedRenderPipeline,
31-
SpecializedRenderPipelines, TextureDescriptor, TextureDimension, TextureFormat,
32-
TextureSampleType, TextureUsages,
27+
BindGroupEntries, BindGroupLayoutDescriptor, BindGroupLayoutEntries,
28+
CachedRenderPipelineId, ColorTargetState, ColorWrites, FilterMode, FragmentState,
29+
Operations, PipelineCache, RenderPassColorAttachment, RenderPassDescriptor,
30+
RenderPipelineDescriptor, Sampler, SamplerBindingType, SamplerDescriptor, ShaderStages,
31+
SpecializedRenderPipeline, SpecializedRenderPipelines, TextureDescriptor, TextureDimension,
32+
TextureFormat, TextureSampleType, TextureUsages,
3333
},
3434
renderer::{RenderContext, RenderDevice},
3535
sync_component::SyncComponentPlugin,
@@ -186,7 +186,7 @@ impl ViewNode for TemporalAntiAliasNode {
186186

187187
let taa_bind_group = render_context.render_device().create_bind_group(
188188
"taa_bind_group",
189-
&pipelines.taa_bind_group_layout,
189+
&pipeline_cache.get_bind_group_layout(&pipelines.taa_bind_group_layout),
190190
&BindGroupEntries::sequential((
191191
view_target.source,
192192
&taa_history_textures.read.default_view,
@@ -236,7 +236,7 @@ impl ViewNode for TemporalAntiAliasNode {
236236

237237
#[derive(Resource)]
238238
struct TaaPipeline {
239-
taa_bind_group_layout: BindGroupLayout,
239+
taa_bind_group_layout: BindGroupLayoutDescriptor,
240240
nearest_sampler: Sampler,
241241
linear_sampler: Sampler,
242242
fullscreen_shader: FullscreenShader,
@@ -262,7 +262,7 @@ fn init_taa_pipeline(
262262
..SamplerDescriptor::default()
263263
});
264264

265-
let taa_bind_group_layout = render_device.create_bind_group_layout(
265+
let taa_bind_group_layout = BindGroupLayoutDescriptor::new(
266266
"taa_bind_group_layout",
267267
&BindGroupLayoutEntries::sequential(
268268
ShaderStages::FRAGMENT,

crates/bevy_core_pipeline/src/blit/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Plugin for BlitPlugin {
3333

3434
#[derive(Resource)]
3535
pub struct BlitPipeline {
36-
pub layout: BindGroupLayout,
36+
pub layout: BindGroupLayoutDescriptor,
3737
pub sampler: Sampler,
3838
pub fullscreen_shader: FullscreenShader,
3939
pub fragment_shader: Handle<Shader>,
@@ -45,7 +45,7 @@ pub fn init_blit_pipeline(
4545
fullscreen_shader: Res<FullscreenShader>,
4646
asset_server: Res<AssetServer>,
4747
) {
48-
let layout = render_device.create_bind_group_layout(
48+
let layout = BindGroupLayoutDescriptor::new(
4949
"blit_bind_group_layout",
5050
&BindGroupLayoutEntries::sequential(
5151
ShaderStages::FRAGMENT,
@@ -71,10 +71,11 @@ impl BlitPipeline {
7171
&self,
7272
render_device: &RenderDevice,
7373
src_texture: &TextureView,
74+
pipeline_cache: &PipelineCache,
7475
) -> BindGroup {
7576
render_device.create_bind_group(
7677
None,
77-
&self.layout,
78+
&pipeline_cache.get_bind_group_layout(&self.layout),
7879
&BindGroupEntries::sequential((src_texture, &self.sampler)),
7980
)
8081
}

0 commit comments

Comments
 (0)