Skip to content

Commit

Permalink
preprocess: the step argument is optional according to slang-shaders …
Browse files Browse the repository at this point in the history
…spec
  • Loading branch information
chyyran committed Sep 16, 2024
1 parent d6f8950 commit cbe6510
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions librashader-preprocess/src/pragma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use librashader_common::ImageFormat;
use nom::bytes::complete::{is_not, tag, take_while};

use librashader_common::map::ShortString;
use nom::character::complete::multispace1;
use nom::character::complete::{multispace0, multispace1};
use nom::combinator::opt;
use nom::number::complete::float;
use nom::sequence::delimited;
use nom::IResult;
Expand Down Expand Up @@ -36,8 +37,16 @@ fn parse_parameter_string(input: &str) -> Result<ShaderParameter, PreprocessErro
let (input, minimum) = float(input)?;
let (input, _) = multispace1(input)?;
let (input, maximum) = float(input)?;
let (input, _) = multispace1(input)?;
let (input, step) = float(input)?;

// Step is actually optional and defaults to 0.02
// This behaviour can be seen in shaders like
// crt/crt-slangtest-cubic.slangp
// which doesn't have a step argument
// #pragma parameter OUT_GAMMA "Monitor Output Gamma" 2.2 1.8 2.4

// https://github.com/libretro/slang-shaders/blob/0e2939787076e4a8a83be89175557fde23abe837/crt/shaders/crt-slangtest/parameters.inc#L1
let (input, _) = multispace0(input)?;
let (input, step) = opt(float)(input)?;
Ok((
input,
ShaderParameter {
Expand All @@ -46,7 +55,7 @@ fn parse_parameter_string(input: &str) -> Result<ShaderParameter, PreprocessErro
initial,
minimum,
maximum,
step,
step: step.unwrap_or(0.02),
},
))
}
Expand Down Expand Up @@ -145,4 +154,22 @@ mod test {
step: 25.0
}, parse_parameter_string(r#"#pragma parameter HSM_CORE_RES_SAMPLING_MULT_SCANLINE_DIR " Scanline Dir Multiplier" 100 25 1600 25"#).unwrap())
}

#[test]
fn parses_parameter_pragma_with_no_step() {
assert_eq!(
ShaderParameter {
id: "OUT_GAMMA".into(),
description: "Monitor Output Gamma".to_string(),
initial: 2.2,
minimum: 1.8,
maximum: 2.4,
step: 0.02
},
parse_parameter_string(
r#"#pragma parameter OUT_GAMMA "Monitor Output Gamma" 2.2 1.8 2.4"#
)
.unwrap()
)
}
}

0 comments on commit cbe6510

Please sign in to comment.