Skip to content

Commit 0f6abc9

Browse files
authored
[naga-cli] add --defines options for the glsl parser (#5859)
1 parent 6405dcf commit 0f6abc9

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TODO(wumpf): This is still work in progress. Should write a bit more about it. A
4747

4848
`wgpu::ComputePass` recording methods (e.g. `wgpu::ComputePass:set_render_pipeline`) no longer impose a lifetime constraint passed in resources.
4949

50-
Furthermore, you can now opt out of `wgpu::ComputePass`'s lifetime dependency on its parent `wgpu::CommandEncoder` using `wgpu::ComputePass::forget_lifetime`:
50+
Furthermore, you can now opt out of `wgpu::ComputePass`'s lifetime dependency on its parent `wgpu::CommandEncoder` using `wgpu::ComputePass::forget_lifetime`:
5151
```rust
5252
fn independent_cpass<'enc>(encoder: &'enc mut wgpu::CommandEncoder) -> wgpu::ComputePass<'static> {
5353
let cpass: wgpu::ComputePass<'enc> = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
@@ -126,6 +126,7 @@ By @atlv24 in [#5383](https://github.com/gfx-rs/wgpu/pull/5383)
126126

127127
#### Naga
128128

129+
- Added -D, --defines option to naga CLI to define preprocessor macros by @theomonnom in [#5859](https://github.com/gfx-rs/wgpu/pull/5859)
129130
- Added type upgrades to SPIR-V atomic support. Added related infrastructure. Tracking issue is [here](https://github.com/gfx-rs/wgpu/issues/4489). By @schell in [#5775](https://github.com/gfx-rs/wgpu/pull/5775).
130131
- Implement `WGSL`'s `unpack4xI8`,`unpack4xU8`,`pack4xI8` and `pack4xU8`. By @VlaDexa in [#5424](https://github.com/gfx-rs/wgpu/pull/5424)
131132
- Began work adding support for atomics to the SPIR-V frontend. Tracking issue is [here](https://github.com/gfx-rs/wgpu/issues/4489). By @schell in [#5702](https://github.com/gfx-rs/wgpu/pull/5702).

naga-cli/src/bin/naga.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ struct Args {
132132
/// In bulk validation mode, these are all input files to be validated.
133133
#[argh(positional)]
134134
files: Vec<String>,
135+
136+
/// defines to be passed to the parser (only glsl is supported)
137+
#[argh(option, short = 'D')]
138+
defines: Vec<Defines>,
135139
}
136140

137141
/// Newtype so we can implement [`FromStr`] for `BoundsCheckPolicy`.
@@ -285,6 +289,27 @@ impl FromStr for Overrides {
285289
}
286290
}
287291

292+
#[derive(Clone, Debug)]
293+
struct Defines {
294+
pairs: Vec<(String, String)>,
295+
}
296+
297+
impl FromStr for Defines {
298+
type Err = String;
299+
300+
fn from_str(s: &str) -> Result<Self, Self::Err> {
301+
let mut pairs = vec![];
302+
for pair in s.split(',') {
303+
let (name, value) = match pair.split_once('=') {
304+
Some((name, value)) => (name, value),
305+
None => (pair, ""), // Default to an empty string if no '=' is found
306+
};
307+
pairs.push((name.trim().to_string(), value.trim().to_string()));
308+
}
309+
Ok(Defines { pairs })
310+
}
311+
}
312+
288313
#[derive(Default)]
289314
struct Parameters<'a> {
290315
validation_flags: naga::valid::ValidationFlags,
@@ -300,6 +325,7 @@ struct Parameters<'a> {
300325
hlsl: naga::back::hlsl::Options,
301326
input_kind: Option<InputKind>,
302327
shader_stage: Option<ShaderStage>,
328+
defines: FastHashMap<String, String>,
303329
}
304330

305331
trait PrettyResult {
@@ -393,6 +419,14 @@ fn run() -> anyhow::Result<()> {
393419
.flat_map(|o| &o.pairs)
394420
.cloned()
395421
.collect();
422+
423+
params.defines = args
424+
.defines
425+
.iter()
426+
.flat_map(|o| &o.pairs)
427+
.cloned()
428+
.collect();
429+
396430
params.spv_in = naga::front::spv::Options {
397431
adjust_coordinate_space: !args.keep_coordinate_space,
398432
strict_capabilities: false,
@@ -611,7 +645,7 @@ fn parse_input(input_path: &Path, input: Vec<u8>, params: &Parameters) -> anyhow
611645
.parse(
612646
&naga::front::glsl::Options {
613647
stage: shader_stage.0,
614-
defines: Default::default(),
648+
defines: params.defines.clone(),
615649
},
616650
&input,
617651
)
@@ -859,7 +893,7 @@ use codespan_reporting::{
859893
termcolor::{ColorChoice, StandardStream},
860894
},
861895
};
862-
use naga::WithSpan;
896+
use naga::{FastHashMap, WithSpan};
863897

864898
pub fn emit_annotated_error<E: Error>(ann_err: &WithSpan<E>, filename: &str, source: &str) {
865899
let files = SimpleFile::new(filename, source);

0 commit comments

Comments
 (0)