| 1 |
/*! |
| 2 |
Naga can be used to translate source code written in one shading language to another. |
| 3 |
|
| 4 |
# Example |
| 5 |
|
| 6 |
The following example translates WGSL to GLSL. |
| 7 |
It 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 |
/*! |
| 14 |
let wgsl_source = " |
| 15 |
@fragment |
| 16 |
fn 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. |
| 22 |
let 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. |
| 26 |
let 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. |
| 36 |
use naga::back::glsl; |
| 37 |
let mut glsl_source = String::new(); |
| 38 |
glsl::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 |
|
| 51 |
assert_eq!(glsl_source, "\ |
| 52 |
#version 310 es |
| 53 |
|
| 54 |
precision highp float; |
| 55 |
precision highp int; |
| 56 |
|
| 57 |
layout(location = 0) out vec4 _fs2p_location0; |
| 58 |
|
| 59 |
void 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)] |
| 104 |
extern crate std; |
| 105 |
|
| 106 |
extern crate alloc; |
| 107 |
|
| 108 |
mod arena; |
| 109 |
pub mod back; |
| 110 |
pub mod common; |
| 111 |
#[cfg(feature = "compact")] |
| 112 |
pub mod compact; |
| 113 |
pub mod diagnostic_filter; |
| 114 |
pub mod error; |
| 115 |
pub mod front; |
| 116 |
pub mod ir; |
| 117 |
pub mod keywords; |
| 118 |
mod non_max_u32; |
| 119 |
pub mod proc; |
| 120 |
mod racy_lock; |
| 121 |
mod span; |
| 122 |
pub mod valid; |
| 123 |
|
| 124 |
use alloc::string::String; |
| 125 |
|
| 126 |
pub use crate::arena::{Arena, Handle, Range, UniqueArena}; |
| 127 |
pub 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`. |
| 130 |
pub use ir::*; |
| 131 |
|
| 132 |
/// Width of a boolean type, in bytes. |
| 133 |
pub const BOOL_WIDTH: Bytes = 1; |
| 134 |
|
| 135 |
/// Width of abstract types, in bytes. |
| 136 |
pub 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()` |
| 141 |
pub 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.) |
| 146 |
pub 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). |
| 151 |
pub 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). |
| 156 |
pub 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 |
| 160 |
pub(crate) type NamedExpressions = FastIndexMap<Handle<Expression>, String>; |