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::large_stack_frames,
86 clippy::match_wildcard_for_single_variants,
87 clippy::missing_const_for_fn,
88 clippy::pattern_type_mismatch,
89 clippy::rest_pat_in_fully_bound_structs
90)]
91#![deny(clippy::exit)]
92#![cfg_attr(
93 not(test),
94 warn(
95 clippy::dbg_macro,
96 clippy::panic,
97 clippy::print_stderr,
98 clippy::print_stdout,
99 clippy::todo
100 )
101)]
102#![no_std]
103#![forbid(unsafe_code)]
104
105#[cfg(std)]
106extern crate std;
107
108extern crate alloc;
109
110extern crate naga_types as nt;
111
112mod arena;
113pub mod back;
114pub mod common;
115pub mod compact;
116pub mod diagnostic_filter;
117pub mod error;
118pub mod front;
119pub mod ir;
120pub mod keywords;
121mod non_max_u32;
122pub mod proc;
123mod racy_lock;
124mod span;
125pub mod valid;
126
127use alloc::string::String;
128
129pub use crate::arena::{Arena, Handle, Range, UniqueArena};
130pub use crate::span::{SourceLocation, Span, SpanContext, WithSpan};
131
132pub use nt::{FastHashMap, FastHashSet, FastIndexMap, FastIndexSet};
133
134// TODO: Eliminate this re-export and migrate uses of `crate::Foo` to `use crate::ir; ir::Foo`.
135pub use ir::*;
136
137/// Width of a boolean type, in bytes.
138pub const BOOL_WIDTH: Bytes = 1;
139
140/// Width of abstract types, in bytes.
141pub const ABSTRACT_WIDTH: Bytes = 8;
142
143/// Map of expressions that have associated variable names
144pub(crate) type NamedExpressions = FastIndexMap<Handle<Expression>, String>;