Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[naga-cli] add --defines options for the glsl parser #5859

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TODO(wumpf): This is still work in progress. Should write a bit more about it. A

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

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

#### Naga

- Added -D, --defines option to naga CLI to define preprocessor macros by @theomonnom in [#5859](https://github.com/gfx-rs/wgpu/pull/5859)
- 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).
- Implement `WGSL`'s `unpack4xI8`,`unpack4xU8`,`pack4xI8` and `pack4xU8`. By @VlaDexa in [#5424](https://github.com/gfx-rs/wgpu/pull/5424)
- 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).
Expand Down
38 changes: 36 additions & 2 deletions naga-cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ struct Args {
/// In bulk validation mode, these are all input files to be validated.
#[argh(positional)]
files: Vec<String>,

/// defines to be passed to the parser (only glsl is supported)
#[argh(option, short = 'D')]
defines: Vec<Defines>,
}

/// Newtype so we can implement [`FromStr`] for `BoundsCheckPolicy`.
Expand Down Expand Up @@ -285,6 +289,27 @@ impl FromStr for Overrides {
}
}

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

impl FromStr for Defines {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut pairs = vec![];
for pair in s.split(',') {
let (name, value) = match pair.split_once('=') {
Some((name, value)) => (name, value),
None => (pair, ""), // Default to an empty string if no '=' is found
};
pairs.push((name.trim().to_string(), value.trim().to_string()));
}
Ok(Defines { pairs })
}
}

#[derive(Default)]
struct Parameters<'a> {
validation_flags: naga::valid::ValidationFlags,
Expand All @@ -300,6 +325,7 @@ struct Parameters<'a> {
hlsl: naga::back::hlsl::Options,
input_kind: Option<InputKind>,
shader_stage: Option<ShaderStage>,
defines: FastHashMap<String, String>,
}

trait PrettyResult {
Expand Down Expand Up @@ -393,6 +419,14 @@ fn run() -> anyhow::Result<()> {
.flat_map(|o| &o.pairs)
.cloned()
.collect();

params.defines = args
.defines
.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 @@ -611,7 +645,7 @@ fn parse_input(input_path: &Path, input: Vec<u8>, params: &Parameters) -> anyhow
.parse(
&naga::front::glsl::Options {
stage: shader_stage.0,
defines: Default::default(),
defines: params.defines.clone(),
},
&input,
)
Expand Down Expand Up @@ -859,7 +893,7 @@ use codespan_reporting::{
termcolor::{ColorChoice, StandardStream},
},
};
use naga::WithSpan;
use naga::{FastHashMap, WithSpan};

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