Skip to content

Commit

Permalink
[naga-cli] Add --override option.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Mar 20, 2024
1 parent 4d73a81 commit 5fc8899
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions naga-cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ struct Args {
#[argh(switch)]
version: bool,

/// override value, of the form "foo=N,bar=M", repeatable
#[argh(option, long = "override")]
overrides: Vec<Overrides>,

/// the input and output files.
///
/// First positional argument is the input file. If not specified, the
Expand Down Expand Up @@ -174,12 +178,35 @@ impl FromStr for GlslProfileArg {
}
}

#[derive(Clone, Debug)]
struct Overrides {
pairs: Vec<(String, f64)>,
}

impl FromStr for Overrides {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut pairs = vec![];
for pair in s.split(',') {
let Some((name, value)) = pair.split_once('=') else {
return Err(format!("value needs a `=`: {pair:?}"));
};
let value = f64::from_str(value.trim())
.map_err(|err| format!("{err}: {value:?}"))?;
pairs.push((name.trim().to_string(), value));
}
Ok(Overrides { pairs })
}
}

#[derive(Default)]
struct Parameters<'a> {
validation_flags: naga::valid::ValidationFlags,
bounds_check_policies: naga::proc::BoundsCheckPolicies,
entry_point: Option<String>,
keep_coordinate_space: bool,
overrides: naga::back::PipelineConstants,
spv_in: naga::front::spv::Options,
spv_out: naga::back::spv::Options<'a>,
dot: naga::back::dot::Options,
Expand Down Expand Up @@ -273,7 +300,11 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
Some(arg) => arg.0,
None => params.bounds_check_policies.index,
};

params.overrides = args.overrides
.iter()
.flat_map(|o| &o.pairs)
.cloned()
.collect();
params.spv_in = naga::front::spv::Options {
adjust_coordinate_space: !args.keep_coordinate_space,
strict_capabilities: false,
Expand Down Expand Up @@ -639,7 +670,9 @@ fn write_output(
"Generating hlsl output requires validation to \
succeed, and it failed in a previous step",
))?,
&hlsl::PipelineOptions::default(),
&hlsl::PipelineOptions {
constants: params.overrides.clone(),
}
)
.unwrap_pretty();
fs::write(output_path, buffer)?;
Expand Down

0 comments on commit 5fc8899

Please sign in to comment.