Skip to content

Commit cbe6510

Browse files
committed
preprocess: the step argument is optional according to slang-shaders spec
1 parent d6f8950 commit cbe6510

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

librashader-preprocess/src/pragma.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use librashader_common::ImageFormat;
33
use nom::bytes::complete::{is_not, tag, take_while};
44

55
use librashader_common::map::ShortString;
6-
use nom::character::complete::multispace1;
6+
use nom::character::complete::{multispace0, multispace1};
7+
use nom::combinator::opt;
78
use nom::number::complete::float;
89
use nom::sequence::delimited;
910
use nom::IResult;
@@ -36,8 +37,16 @@ fn parse_parameter_string(input: &str) -> Result<ShaderParameter, PreprocessErro
3637
let (input, minimum) = float(input)?;
3738
let (input, _) = multispace1(input)?;
3839
let (input, maximum) = float(input)?;
39-
let (input, _) = multispace1(input)?;
40-
let (input, step) = float(input)?;
40+
41+
// Step is actually optional and defaults to 0.02
42+
// This behaviour can be seen in shaders like
43+
// crt/crt-slangtest-cubic.slangp
44+
// which doesn't have a step argument
45+
// #pragma parameter OUT_GAMMA "Monitor Output Gamma" 2.2 1.8 2.4
46+
47+
// https://github.com/libretro/slang-shaders/blob/0e2939787076e4a8a83be89175557fde23abe837/crt/shaders/crt-slangtest/parameters.inc#L1
48+
let (input, _) = multispace0(input)?;
49+
let (input, step) = opt(float)(input)?;
4150
Ok((
4251
input,
4352
ShaderParameter {
@@ -46,7 +55,7 @@ fn parse_parameter_string(input: &str) -> Result<ShaderParameter, PreprocessErro
4655
initial,
4756
minimum,
4857
maximum,
49-
step,
58+
step: step.unwrap_or(0.02),
5059
},
5160
))
5261
}
@@ -145,4 +154,22 @@ mod test {
145154
step: 25.0
146155
}, parse_parameter_string(r#"#pragma parameter HSM_CORE_RES_SAMPLING_MULT_SCANLINE_DIR " Scanline Dir Multiplier" 100 25 1600 25"#).unwrap())
147156
}
157+
158+
#[test]
159+
fn parses_parameter_pragma_with_no_step() {
160+
assert_eq!(
161+
ShaderParameter {
162+
id: "OUT_GAMMA".into(),
163+
description: "Monitor Output Gamma".to_string(),
164+
initial: 2.2,
165+
minimum: 1.8,
166+
maximum: 2.4,
167+
step: 0.02
168+
},
169+
parse_parameter_string(
170+
r#"#pragma parameter OUT_GAMMA "Monitor Output Gamma" 2.2 1.8 2.4"#
171+
)
172+
.unwrap()
173+
)
174+
}
148175
}

0 commit comments

Comments
 (0)