naga/
lib.rs

1/*!
2Naga can be used to translate source code written in one shading language to another.
3
4# Example
5
6The following example translates WGSL to GLSL.
7It requires the features `"wgsl-in"` and `"glsl-out"` to be enabled.
8
9*/
10// If we don't have the required front- and backends, don't try to build this example.
11#![cfg_attr(all(feature = "wgsl-in", feature = "glsl-out"), doc = "```")]
12#![cfg_attr(not(all(feature = "wgsl-in", feature = "glsl-out")), doc = "```ignore")]
13/*!
14let wgsl_source = "
15@fragment
16fn main_fs() -> @location(0) vec4<f32> {
17    return vec4<f32>(1.0, 1.0, 1.0, 1.0);
18}
19";
20
21// Parse the source into a Module.
22let module: naga::Module = naga::front::wgsl::parse_str(wgsl_source)?;
23
24// Validate the module.
25// Validation can be made less restrictive by changing the ValidationFlags.
26let module_info: naga::valid::ModuleInfo =
27    naga::valid::Validator::new(
28        naga::valid::ValidationFlags::all(),
29        naga::valid::Capabilities::all(),
30    )
31    .subgroup_stages(naga::valid::ShaderStages::all())
32    .subgroup_operations(naga::valid::SubgroupOperationSet::all())
33    .validate(&module)?;
34
35// Translate the module.
36use naga::back::glsl;
37let mut glsl_source = String::new();
38glsl::Writer::new(
39    &mut glsl_source,
40    &module,
41    &module_info,
42    &glsl::Options::default(),
43    &glsl::PipelineOptions {
44        entry_point: "main_fs".into(),
45        shader_stage: naga::ShaderStage::Fragment,
46        multiview: None,
47    },
48    naga::proc::BoundsCheckPolicies::default(),
49)?.write()?;
50
51assert_eq!(glsl_source, "\
52#version 310 es
53
54precision highp float;
55precision highp int;
56
57layout(location = 0) out vec4 _fs2p_location0;
58
59void main() {
60    _fs2p_location0 = vec4(1.0, 1.0, 1.0, 1.0);
61    return;
62}
63
64");
65
66# Ok::<(), Box<dyn core::error::Error>>(())
67```
68*/
69
70#![allow(
71    clippy::new_without_default,
72    clippy::unneeded_field_pattern,
73    clippy::match_like_matches_macro,
74    clippy::collapsible_if,
75    clippy::derive_partial_eq_without_eq,
76    clippy::needless_borrowed_reference,
77    clippy::single_match,
78    clippy::enum_variant_names
79)]
80#![warn(
81    trivial_casts,
82    trivial_numeric_casts,
83    unused_extern_crates,
84    unused_qualifications,
85    clippy::pattern_type_mismatch,
86    clippy::missing_const_for_fn,
87    clippy::rest_pat_in_fully_bound_structs,
88    clippy::match_wildcard_for_single_variants
89)]
90#![deny(clippy::exit)]
91#![cfg_attr(
92    not(test),
93    warn(
94        clippy::dbg_macro,
95        clippy::panic,
96        clippy::print_stderr,
97        clippy::print_stdout,
98        clippy::todo
99    )
100)]
101#![no_std]
102
103#[cfg(std)]
104extern crate std;
105
106extern crate alloc;
107
108mod arena;
109pub mod back;
110pub mod common;
111pub mod compact;
112pub mod diagnostic_filter;
113pub mod error;
114pub mod front;
115pub mod ir;
116pub mod keywords;
117mod non_max_u32;
118mod path_like;
119pub mod proc;
120mod racy_lock;
121mod span;
122pub mod valid;
123
124use alloc::string::String;
125
126pub use crate::arena::{Arena, Handle, Range, UniqueArena};
127pub use crate::span::{SourceLocation, Span, SpanContext, WithSpan};
128
129// TODO: Eliminate this re-export and migrate uses of `crate::Foo` to `use crate::ir; ir::Foo`.
130pub use ir::*;
131
132/// Width of a boolean type, in bytes.
133pub const BOOL_WIDTH: Bytes = 1;
134
135/// Width of abstract types, in bytes.
136pub const ABSTRACT_WIDTH: Bytes = 8;
137
138/// Hash map that is faster but not resilient to DoS attacks.
139/// (Similar to rustc_hash::FxHashMap but using hashbrown::HashMap instead of alloc::collections::HashMap.)
140/// To construct a new instance: `FastHashMap::default()`
141pub type FastHashMap<K, T> =
142    hashbrown::HashMap<K, T, core::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
143
144/// Hash set that is faster but not resilient to DoS attacks.
145/// (Similar to rustc_hash::FxHashSet but using hashbrown::HashSet instead of alloc::collections::HashMap.)
146pub type FastHashSet<K> =
147    hashbrown::HashSet<K, core::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
148
149/// Insertion-order-preserving hash set (`IndexSet<K>`), but with the same
150/// hasher as `FastHashSet<K>` (faster but not resilient to DoS attacks).
151pub type FastIndexSet<K> =
152    indexmap::IndexSet<K, core::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
153
154/// Insertion-order-preserving hash map (`IndexMap<K, V>`), but with the same
155/// hasher as `FastHashMap<K, V>` (faster but not resilient to DoS attacks).
156pub type FastIndexMap<K, V> =
157    indexmap::IndexMap<K, V, core::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
158
159/// Map of expressions that have associated variable names
160pub(crate) type NamedExpressions = FastIndexMap<Handle<Expression>, String>;