@@ -3,21 +3,27 @@ use crate::define_atomic_id;
3
3
use bevy_asset:: { AssetLoader , AssetPath , Handle , LoadContext , LoadedAsset } ;
4
4
use bevy_reflect:: TypeUuid ;
5
5
use bevy_utils:: { tracing:: error, BoxedFuture , HashMap } ;
6
- use naga:: { back:: wgsl:: WriterFlags , valid:: Capabilities , valid:: ModuleInfo , Module } ;
6
+ #[ cfg( feature = "shader_format_glsl" ) ]
7
+ use naga:: back:: wgsl:: WriterFlags ;
8
+ use naga:: { valid:: Capabilities , valid:: ModuleInfo , Module } ;
7
9
use once_cell:: sync:: Lazy ;
8
10
use regex:: Regex ;
9
11
use std:: { borrow:: Cow , marker:: Copy , ops:: Deref , path:: PathBuf , str:: FromStr } ;
10
12
use thiserror:: Error ;
11
- use wgpu:: { util:: make_spirv, Features , ShaderModuleDescriptor , ShaderSource } ;
13
+ #[ cfg( feature = "shader_format_spirv" ) ]
14
+ use wgpu:: util:: make_spirv;
15
+ use wgpu:: { Features , ShaderModuleDescriptor , ShaderSource } ;
12
16
13
17
define_atomic_id ! ( ShaderId ) ;
14
18
15
19
#[ derive( Error , Debug ) ]
16
20
pub enum ShaderReflectError {
17
21
#[ error( transparent) ]
18
22
WgslParse ( #[ from] naga:: front:: wgsl:: ParseError ) ,
23
+ #[ cfg( feature = "shader_format_glsl" ) ]
19
24
#[ error( "GLSL Parse Error: {0:?}" ) ]
20
25
GlslParse ( Vec < naga:: front:: glsl:: Error > ) ,
26
+ #[ cfg( feature = "shader_format_spirv" ) ]
21
27
#[ error( transparent) ]
22
28
SpirVParse ( #[ from] naga:: front:: spv:: Error ) ,
23
29
#[ error( transparent) ]
@@ -120,19 +126,29 @@ impl ProcessedShader {
120
126
let module = match & self {
121
127
// TODO: process macros here
122
128
ProcessedShader :: Wgsl ( source) => naga:: front:: wgsl:: parse_str ( source) ?,
129
+ #[ cfg( feature = "shader_format_glsl" ) ]
123
130
ProcessedShader :: Glsl ( source, shader_stage) => {
124
131
let mut parser = naga:: front:: glsl:: Parser :: default ( ) ;
125
132
parser
126
133
. parse ( & naga:: front:: glsl:: Options :: from ( * shader_stage) , source)
127
134
. map_err ( ShaderReflectError :: GlslParse ) ?
128
135
}
136
+ #[ cfg( not( feature = "shader_format_glsl" ) ) ]
137
+ ProcessedShader :: Glsl ( _source, _shader_stage) => {
138
+ unimplemented ! ( "Enable feature \" shader_format_glsl\" to use GLSL shaders" )
139
+ }
140
+ #[ cfg( feature = "shader_format_spirv" ) ]
129
141
ProcessedShader :: SpirV ( source) => naga:: front:: spv:: parse_u8_slice (
130
142
source,
131
143
& naga:: front:: spv:: Options {
132
144
adjust_coordinate_space : false ,
133
145
..naga:: front:: spv:: Options :: default ( )
134
146
} ,
135
147
) ?,
148
+ #[ cfg( not( feature = "shader_format_spirv" ) ) ]
149
+ ProcessedShader :: SpirV ( _source) => {
150
+ unimplemented ! ( "Enable feature \" shader_format_spirv\" to use SPIR-V shaders" )
151
+ }
136
152
} ;
137
153
const CAPABILITIES : & [ ( Features , Capabilities ) ] = & [
138
154
( Features :: PUSH_CONSTANTS , Capabilities :: PUSH_CONSTANT ) ,
@@ -172,7 +188,7 @@ impl ProcessedShader {
172
188
173
189
pub fn get_module_descriptor (
174
190
& self ,
175
- features : Features ,
191
+ _features : Features ,
176
192
) -> Result < ShaderModuleDescriptor , AsModuleDescriptorError > {
177
193
Ok ( ShaderModuleDescriptor {
178
194
label : None ,
@@ -182,18 +198,28 @@ impl ProcessedShader {
182
198
// Parse and validate the shader early, so that (e.g. while hot reloading) we can
183
199
// display nicely formatted error messages instead of relying on just displaying the error string
184
200
// returned by wgpu upon creating the shader module.
185
- let _ = self . reflect ( features ) ?;
201
+ let _ = self . reflect ( _features ) ?;
186
202
187
203
ShaderSource :: Wgsl ( source. clone ( ) )
188
204
}
205
+ #[ cfg( feature = "shader_format_glsl" ) ]
189
206
ProcessedShader :: Glsl ( _source, _stage) => {
190
- let reflection = self . reflect ( features ) ?;
207
+ let reflection = self . reflect ( _features ) ?;
191
208
// TODO: it probably makes more sense to convert this to spirv, but as of writing
192
209
// this comment, naga's spirv conversion is broken
193
210
let wgsl = reflection. get_wgsl ( ) ?;
194
211
ShaderSource :: Wgsl ( wgsl. into ( ) )
195
212
}
213
+ #[ cfg( not( feature = "shader_format_glsl" ) ) ]
214
+ ProcessedShader :: Glsl ( _source, _stage) => {
215
+ unimplemented ! ( "Enable feature \" shader_format_glsl\" to use GLSL shaders" )
216
+ }
217
+ #[ cfg( feature = "shader_format_spirv" ) ]
196
218
ProcessedShader :: SpirV ( source) => make_spirv ( source) ,
219
+ #[ cfg( not( feature = "shader_format_spirv" ) ) ]
220
+ ProcessedShader :: SpirV ( _source) => {
221
+ unimplemented ! ( )
222
+ }
197
223
} ,
198
224
} )
199
225
}
@@ -203,8 +229,10 @@ impl ProcessedShader {
203
229
pub enum AsModuleDescriptorError {
204
230
#[ error( transparent) ]
205
231
ShaderReflectError ( #[ from] ShaderReflectError ) ,
232
+ #[ cfg( feature = "shader_format_glsl" ) ]
206
233
#[ error( transparent) ]
207
234
WgslConversion ( #[ from] naga:: back:: wgsl:: Error ) ,
235
+ #[ cfg( feature = "shader_format_spirv" ) ]
208
236
#[ error( transparent) ]
209
237
SpirVConversion ( #[ from] naga:: back:: spv:: Error ) ,
210
238
}
@@ -215,6 +243,7 @@ pub struct ShaderReflection {
215
243
}
216
244
217
245
impl ShaderReflection {
246
+ #[ cfg( feature = "shader_format_spirv" ) ]
218
247
pub fn get_spirv ( & self ) -> Result < Vec < u32 > , naga:: back:: spv:: Error > {
219
248
naga:: back:: spv:: write_vec (
220
249
& self . module ,
@@ -227,6 +256,7 @@ impl ShaderReflection {
227
256
)
228
257
}
229
258
259
+ #[ cfg( feature = "shader_format_glsl" ) ]
230
260
pub fn get_wgsl ( & self ) -> Result < String , naga:: back:: wgsl:: Error > {
231
261
naga:: back:: wgsl:: write_string ( & self . module , & self . module_info , WriterFlags :: EXPLICIT_TYPES )
232
262
}
0 commit comments