1
+ use core:: result:: Result ;
2
+
1
3
use crate :: FullscreenShader ;
2
4
use bevy_app:: { App , Plugin } ;
3
- use bevy_asset:: { embedded_asset, load_embedded_asset, Handle } ;
5
+ use bevy_asset:: { embedded_asset, load_embedded_asset} ;
4
6
use bevy_ecs:: prelude:: * ;
5
7
use bevy_render:: {
6
8
render_resource:: {
@@ -20,26 +22,23 @@ impl Plugin for BlitPlugin {
20
22
embedded_asset ! ( app, "blit.wgsl" ) ;
21
23
22
24
if let Some ( render_app) = app. get_sub_app_mut ( RenderApp ) {
23
- render_app. allow_ambiguous_resource :: < SpecializedRenderPipelines < BlitPipeline > > ( ) ;
25
+ render_app. allow_ambiguous_resource :: < BlitPipeline > ( ) ;
24
26
}
25
27
}
26
28
27
29
fn finish ( & self , app : & mut App ) {
28
30
let Some ( render_app) = app. get_sub_app_mut ( RenderApp ) else {
29
31
return ;
30
32
} ;
31
- render_app
32
- . init_resource :: < BlitPipeline > ( )
33
- . init_resource :: < SpecializedRenderPipelines < BlitPipeline > > ( ) ;
33
+ render_app. init_resource :: < BlitPipeline > ( ) ;
34
34
}
35
35
}
36
36
37
37
#[ derive( Resource ) ]
38
38
pub struct BlitPipeline {
39
- pub texture_bind_group : BindGroupLayout ,
39
+ pub layout : BindGroupLayout ,
40
40
pub sampler : Sampler ,
41
- pub fullscreen_shader : FullscreenShader ,
42
- pub fragment_shader : Handle < Shader > ,
41
+ pub specialized_cache : SpecializedCache < RenderPipeline , BlitSpecializer > ,
43
42
}
44
43
45
44
impl FromWorld for BlitPipeline {
@@ -59,44 +58,59 @@ impl FromWorld for BlitPipeline {
59
58
60
59
let sampler = render_device. create_sampler ( & SamplerDescriptor :: default ( ) ) ;
61
60
61
+ let fullscreen_shader = render_world. resource :: < FullscreenShader > ( ) . clone ( ) ;
62
+ let fragment_shader = load_embedded_asset ! ( render_world, "blit.wgsl" ) ;
63
+
64
+ let base_descriptor = RenderPipelineDescriptor {
65
+ label : Some ( "blit pipeline" . into ( ) ) ,
66
+ layout : vec ! [ texture_bind_group. clone( ) ] ,
67
+ vertex : fullscreen_shader. to_vertex_state ( ) ,
68
+ fragment : Some ( FragmentState {
69
+ shader : fragment_shader,
70
+ targets : vec ! [ ] ,
71
+ ..default ( )
72
+ } ) ,
73
+ ..default ( )
74
+ } ;
75
+
76
+ let specialized_cache = SpecializedCache :: new ( BlitSpecializer , None , base_descriptor) ;
77
+
62
78
BlitPipeline {
63
- texture_bind_group,
79
+ layout : texture_bind_group,
64
80
sampler,
65
- fullscreen_shader : render_world. resource :: < FullscreenShader > ( ) . clone ( ) ,
66
- fragment_shader : load_embedded_asset ! ( render_world, "blit.wgsl" ) ,
81
+ specialized_cache,
67
82
}
68
83
}
69
84
}
70
85
71
- #[ derive( PartialEq , Eq , Hash , Clone , Copy ) ]
72
- pub struct BlitPipelineKey {
73
- pub texture_format : TextureFormat ,
74
- pub blend_state : Option < BlendState > ,
75
- pub samples : u32 ,
76
- }
86
+ pub struct BlitSpecializer ;
77
87
78
- impl SpecializedRenderPipeline for BlitPipeline {
79
- type Key = BlitPipelineKey ;
88
+ impl Specializer < RenderPipeline > for BlitSpecializer {
89
+ type Key = BlitKey ;
80
90
81
- fn specialize ( & self , key : Self :: Key ) -> RenderPipelineDescriptor {
82
- RenderPipelineDescriptor {
83
- label : Some ( "blit pipeline" . into ( ) ) ,
84
- layout : vec ! [ self . texture_bind_group. clone( ) ] ,
85
- vertex : self . fullscreen_shader . to_vertex_state ( ) ,
86
- fragment : Some ( FragmentState {
87
- shader : self . fragment_shader . clone ( ) ,
88
- targets : vec ! [ Some ( ColorTargetState {
89
- format: key. texture_format,
90
- blend: key. blend_state,
91
- write_mask: ColorWrites :: ALL ,
92
- } ) ] ,
93
- ..default ( )
94
- } ) ,
95
- multisample : MultisampleState {
96
- count : key. samples ,
97
- ..default ( )
91
+ fn specialize (
92
+ & self ,
93
+ key : Self :: Key ,
94
+ descriptor : & mut <RenderPipeline as Specializable >:: Descriptor ,
95
+ ) -> Result < Canonical < Self :: Key > , BevyError > {
96
+ descriptor. multisample . count = key. samples ;
97
+
98
+ descriptor. fragment_mut ( ) ?. set_target (
99
+ 0 ,
100
+ ColorTargetState {
101
+ format : key. texture_format ,
102
+ blend : key. blend_state ,
103
+ write_mask : ColorWrites :: ALL ,
98
104
} ,
99
- ..default ( )
100
- }
105
+ ) ;
106
+
107
+ Ok ( key)
101
108
}
102
109
}
110
+
111
+ #[ derive( PartialEq , Eq , Hash , Clone , Copy , SpecializerKey ) ]
112
+ pub struct BlitKey {
113
+ pub texture_format : TextureFormat ,
114
+ pub blend_state : Option < BlendState > ,
115
+ pub samples : u32 ,
116
+ }
0 commit comments