Skip to content

Commit e4203c3

Browse files
committed
shader preprocessor - do not import if scope is not valid (#4012)
# Objective - fix #4011 - imports are not limited by the current `ifdef` they are in ## Solution - process imports only if the current scope is enabled
1 parent 5afda8d commit e4203c3

File tree

1 file changed

+98
-33
lines changed
  • crates/bevy_render/src/render_resource

1 file changed

+98
-33
lines changed

crates/bevy_render/src/render_resource/shader.rs

Lines changed: 98 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -408,40 +408,42 @@ impl ShaderProcessor {
408408
if scopes.is_empty() {
409409
return Err(ProcessShaderError::TooManyEndIfs);
410410
}
411-
} else if let Some(cap) = SHADER_IMPORT_PROCESSOR
412-
.import_asset_path_regex
413-
.captures(line)
414-
{
415-
let import = ShaderImport::AssetPath(cap.get(1).unwrap().as_str().to_string());
416-
self.apply_import(
417-
import_handles,
418-
shaders,
419-
&import,
420-
shader,
421-
shader_defs,
422-
&mut final_string,
423-
)?;
424-
} else if let Some(cap) = SHADER_IMPORT_PROCESSOR
425-
.import_custom_path_regex
426-
.captures(line)
427-
{
428-
let import = ShaderImport::Custom(cap.get(1).unwrap().as_str().to_string());
429-
self.apply_import(
430-
import_handles,
431-
shaders,
432-
&import,
433-
shader,
434-
shader_defs,
435-
&mut final_string,
436-
)?;
437-
} else if SHADER_IMPORT_PROCESSOR
438-
.define_import_path_regex
439-
.is_match(line)
440-
{
441-
// ignore import path lines
442411
} else if *scopes.last().unwrap() {
443-
final_string.push_str(line);
444-
final_string.push('\n');
412+
if let Some(cap) = SHADER_IMPORT_PROCESSOR
413+
.import_asset_path_regex
414+
.captures(line)
415+
{
416+
let import = ShaderImport::AssetPath(cap.get(1).unwrap().as_str().to_string());
417+
self.apply_import(
418+
import_handles,
419+
shaders,
420+
&import,
421+
shader,
422+
shader_defs,
423+
&mut final_string,
424+
)?;
425+
} else if let Some(cap) = SHADER_IMPORT_PROCESSOR
426+
.import_custom_path_regex
427+
.captures(line)
428+
{
429+
let import = ShaderImport::Custom(cap.get(1).unwrap().as_str().to_string());
430+
self.apply_import(
431+
import_handles,
432+
shaders,
433+
&import,
434+
shader,
435+
shader_defs,
436+
&mut final_string,
437+
)?;
438+
} else if SHADER_IMPORT_PROCESSOR
439+
.define_import_path_regex
440+
.is_match(line)
441+
{
442+
// ignore import path lines
443+
} else {
444+
final_string.push_str(line);
445+
final_string.push('\n');
446+
}
445447
}
446448
}
447449

@@ -1231,4 +1233,67 @@ fn in_main() { }
12311233
.unwrap();
12321234
assert_eq!(result.get_wgsl_source().unwrap(), EXPECTED);
12331235
}
1236+
1237+
#[test]
1238+
fn process_import_in_ifdef() {
1239+
#[rustfmt::skip]
1240+
const BAR: &str = r"
1241+
fn bar() { }
1242+
";
1243+
#[rustfmt::skip]
1244+
const BAZ: &str = r"
1245+
fn baz() { }
1246+
";
1247+
#[rustfmt::skip]
1248+
const INPUT: &str = r"
1249+
#ifdef FOO
1250+
#import BAR
1251+
#else
1252+
#import BAZ
1253+
#endif
1254+
";
1255+
#[rustfmt::skip]
1256+
const EXPECTED_FOO: &str = r"
1257+
1258+
fn bar() { }
1259+
";
1260+
#[rustfmt::skip]
1261+
const EXPECTED: &str = r"
1262+
1263+
fn baz() { }
1264+
";
1265+
let processor = ShaderProcessor::default();
1266+
let mut shaders = HashMap::default();
1267+
let mut import_handles = HashMap::default();
1268+
{
1269+
let bar_handle = Handle::<Shader>::default();
1270+
shaders.insert(bar_handle.clone_weak(), Shader::from_wgsl(BAR));
1271+
import_handles.insert(
1272+
ShaderImport::Custom("BAR".to_string()),
1273+
bar_handle.clone_weak(),
1274+
);
1275+
}
1276+
{
1277+
let baz_handle = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 1).typed();
1278+
shaders.insert(baz_handle.clone_weak(), Shader::from_wgsl(BAZ));
1279+
import_handles.insert(
1280+
ShaderImport::Custom("BAZ".to_string()),
1281+
baz_handle.clone_weak(),
1282+
);
1283+
}
1284+
let result = processor
1285+
.process(
1286+
&Shader::from_wgsl(INPUT),
1287+
&["FOO".to_string()],
1288+
&shaders,
1289+
&import_handles,
1290+
)
1291+
.unwrap();
1292+
assert_eq!(result.get_wgsl_source().unwrap(), EXPECTED_FOO);
1293+
1294+
let result = processor
1295+
.process(&Shader::from_wgsl(INPUT), &[], &shaders, &import_handles)
1296+
.unwrap();
1297+
assert_eq!(result.get_wgsl_source().unwrap(), EXPECTED);
1298+
}
12341299
}

0 commit comments

Comments
 (0)