Skip to content

Commit 7bed9e8

Browse files
jimblandyteoxoy
authored andcommitted
[naga-cli] Add --override option.
1 parent ba19d8d commit 7bed9e8

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

naga-cli/src/bin/naga.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ struct Args {
105105
#[argh(switch)]
106106
version: bool,
107107

108+
/// override value, of the form "foo=N,bar=M", repeatable
109+
#[argh(option, long = "override")]
110+
overrides: Vec<Overrides>,
111+
108112
/// the input and output files.
109113
///
110114
/// First positional argument is the input file. If not specified, the
@@ -202,12 +206,34 @@ impl FromStr for MslVersionArg {
202206
}
203207
}
204208

209+
#[derive(Clone, Debug)]
210+
struct Overrides {
211+
pairs: Vec<(String, f64)>,
212+
}
213+
214+
impl FromStr for Overrides {
215+
type Err = String;
216+
217+
fn from_str(s: &str) -> Result<Self, Self::Err> {
218+
let mut pairs = vec![];
219+
for pair in s.split(',') {
220+
let Some((name, value)) = pair.split_once('=') else {
221+
return Err(format!("value needs a `=`: {pair:?}"));
222+
};
223+
let value = f64::from_str(value.trim()).map_err(|err| format!("{err}: {value:?}"))?;
224+
pairs.push((name.trim().to_string(), value));
225+
}
226+
Ok(Overrides { pairs })
227+
}
228+
}
229+
205230
#[derive(Default)]
206231
struct Parameters<'a> {
207232
validation_flags: naga::valid::ValidationFlags,
208233
bounds_check_policies: naga::proc::BoundsCheckPolicies,
209234
entry_point: Option<String>,
210235
keep_coordinate_space: bool,
236+
overrides: naga::back::PipelineConstants,
211237
spv_in: naga::front::spv::Options,
212238
spv_out: naga::back::spv::Options<'a>,
213239
dot: naga::back::dot::Options,
@@ -301,7 +327,12 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
301327
Some(arg) => arg.0,
302328
None => params.bounds_check_policies.index,
303329
};
304-
330+
params.overrides = args
331+
.overrides
332+
.iter()
333+
.flat_map(|o| &o.pairs)
334+
.cloned()
335+
.collect();
305336
params.spv_in = naga::front::spv::Options {
306337
adjust_coordinate_space: !args.keep_coordinate_space,
307338
strict_capabilities: false,
@@ -670,7 +701,9 @@ fn write_output(
670701
"Generating hlsl output requires validation to \
671702
succeed, and it failed in a previous step",
672703
))?,
673-
&hlsl::PipelineOptions::default(),
704+
&hlsl::PipelineOptions {
705+
constants: params.overrides.clone(),
706+
},
674707
)
675708
.unwrap_pretty();
676709
fs::write(output_path, buffer)?;

0 commit comments

Comments
 (0)