Skip to content

Commit

Permalink
Remove string length check in preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
callym committed Aug 1, 2024
1 parent 1dd607b commit 5c52ef9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/compose/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ErrSource {
..
}) = composer
.preprocessor
.preprocess(raw_source, defs, composer.validate)
.preprocess(raw_source, defs)
else {
return Default::default();
};
Expand Down
4 changes: 2 additions & 2 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ impl Composer {
imports,
} = self
.preprocessor
.preprocess(&module_set.sanitized_source, shader_defs, self.validate)
.preprocess(&module_set.sanitized_source, shader_defs)
.map_err(|inner| ComposerError {
inner,
source: ErrSource::Module {
Expand Down Expand Up @@ -1673,7 +1673,7 @@ impl Composer {
imports,
} = self
.preprocessor
.preprocess(&sanitized_source, &shader_defs, self.validate)
.preprocess(&sanitized_source, &shader_defs)
.map_err(|inner| ComposerError {
inner,
source: ErrSource::Constructing {
Expand Down
42 changes: 7 additions & 35 deletions src/compose/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,13 @@ impl Preprocessor {
&self,
shader_str: &str,
shader_defs: &HashMap<String, ShaderDefValue>,
validate_len: bool,
) -> Result<PreprocessOutput, ComposerErrorInner> {
let mut declared_imports = IndexMap::new();
let mut used_imports = IndexMap::new();
let mut scope = Scope::new();
let mut final_string = String::new();
let mut offset = 0;

#[cfg(debug_assertions)]
let len = shader_str.len();

// this code broadly stolen from bevy_render::ShaderProcessor
let mut lines = shader_str.lines();
let mut lines = lines.replace_comments().zip(shader_str.lines()).peekable();
Expand Down Expand Up @@ -371,14 +367,6 @@ impl Preprocessor {

scope.finish(offset)?;

#[cfg(debug_assertions)]
if validate_len {
let revised_len = final_string.len();
debug_assert_eq!(len, revised_len);
}
#[cfg(not(debug_assertions))]
let _ = validate_len;

Ok(PreprocessOutput {
preprocessed_source: final_string,
imports: used_imports.into_values().collect(),
Expand Down Expand Up @@ -576,7 +564,6 @@ fn vertex(
let result_missing = processor.preprocess(
WGSL,
&[("TEXTURE".to_owned(), ShaderDefValue::Bool(true))].into(),
true,
);

let expected: Result<Preprocessor, ComposerErrorInner> =
Expand Down Expand Up @@ -677,7 +664,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Int(3))].into(),
true,
)
.unwrap();
assert_eq!(result_eq.preprocessed_source, EXPECTED_EQ);
Expand All @@ -686,12 +672,11 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Int(7))].into(),
true,
)
.unwrap();
assert_eq!(result_neq.preprocessed_source, EXPECTED_NEQ);

let result_missing = processor.preprocess(WGSL, &Default::default(), true);
let result_missing = processor.preprocess(WGSL, &Default::default());

let expected_err: Result<
(Option<String>, String, Vec<ImportDefWithOffset>),
Expand All @@ -705,7 +690,6 @@ fn vertex(
let result_wrong_type = processor.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
);

let expected_err: Result<
Expand Down Expand Up @@ -814,7 +798,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(result_eq.preprocessed_source, EXPECTED_EQ);
Expand All @@ -823,7 +806,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(false))].into(),
true,
)
.unwrap();
assert_eq!(result_neq.preprocessed_source, EXPECTED_NEQ);
Expand Down Expand Up @@ -919,7 +901,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(result_eq.preprocessed_source, EXPECTED_EQ);
Expand All @@ -928,12 +909,11 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(false))].into(),
true,
)
.unwrap();
assert_eq!(result_neq.preprocessed_source, EXPECTED_NEQ);

let result_missing = processor.preprocess(WGSL, &[].into(), true);
let result_missing = processor.preprocess(WGSL, &[].into());
let expected_err: Result<
(Option<String>, String, Vec<ImportDefWithOffset>),
ComposerErrorInner,
Expand All @@ -946,7 +926,6 @@ fn vertex(
let result_wrong_type = processor.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Int(7))].into(),
true,
);

let expected_err: Result<
Expand Down Expand Up @@ -1031,7 +1010,6 @@ fn vertex(
("SECOND_VALUE".to_string(), ShaderDefValue::Int(3)),
]
.into(),
true,
)
.unwrap();
assert_eq!(result.preprocessed_source, EXPECTED_REPLACED);
Expand Down Expand Up @@ -1060,7 +1038,7 @@ defined
..
} = processor.get_preprocessor_metadata(&WGSL, true).unwrap();
println!("defines: {:?}", shader_defs);
let result = processor.preprocess(&WGSL, &shader_defs, true).unwrap();
let result = processor.preprocess(&WGSL, &shader_defs).unwrap();
assert_eq!(result.preprocessed_source, EXPECTED);
}

Expand Down Expand Up @@ -1103,7 +1081,7 @@ bool: false
..
} = processor.get_preprocessor_metadata(&WGSL, true).unwrap();
println!("defines: {:?}", shader_defs);
let result = processor.preprocess(&WGSL, &shader_defs, true).unwrap();
let result = processor.preprocess(&WGSL, &shader_defs).unwrap();
assert_eq!(result.preprocessed_source, EXPECTED);
}

Expand Down Expand Up @@ -1136,7 +1114,7 @@ fn vertex(
";
let processor = Preprocessor::default();
let result = processor
.preprocess(&WGSL_ELSE_IFDEF, &[].into(), true)
.preprocess(&WGSL_ELSE_IFDEF, &[].into())
.unwrap();
assert_eq!(
result
Expand Down Expand Up @@ -1214,7 +1192,7 @@ fn vertex(
";
let processor = Preprocessor::default();
let result = processor
.preprocess(&WGSL_ELSE_IFDEF_NO_ELSE_FALLBACK, &[].into(), true)
.preprocess(&WGSL_ELSE_IFDEF_NO_ELSE_FALLBACK, &[].into())
.unwrap();
assert_eq!(
result
Expand Down Expand Up @@ -1265,7 +1243,6 @@ fn vertex(
.preprocess(
&WGSL_ELSE_IFDEF,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1314,7 +1291,6 @@ fn vertex(
.preprocess(
&WGSL_ELSE_IFDEF,
&[("SECOND_TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1363,7 +1339,6 @@ fn vertex(
.preprocess(
&WGSL_ELSE_IFDEF,
&[("THIRD_TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1416,7 +1391,6 @@ fn vertex(
("THIRD_TEXTURE".to_string(), ShaderDefValue::Bool(true)),
]
.into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1471,7 +1445,6 @@ fn vertex(
.preprocess(
&WGSL_COMPLICATED_ELSE_IFDEF,
&[("IS_DEFINED".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1504,7 +1477,7 @@ fail 3

const EXPECTED: &str = r"ok";
let processor = Preprocessor::default();
let result = processor.preprocess(&INPUT, &[].into(), true).unwrap();
let result = processor.preprocess(&INPUT, &[].into()).unwrap();
assert_eq!(
result
.preprocessed_source
Expand Down Expand Up @@ -1539,7 +1512,6 @@ fail 3
.preprocess(
&INPUT,
&[("x".to_owned(), ShaderDefValue::Int(2))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down

0 comments on commit 5c52ef9

Please sign in to comment.