Expand description
Naga can be used to translate source code written in one shading language to another.
§Example
The following example translates WGSL to GLSL.
It requires the features "wgsl-in"
and "glsl-out"
to be enabled.
let wgsl_source = "
@fragment
fn main_fs() -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 1.0, 1.0, 1.0);
}
";
// Parse the source into a Module.
let module: naga::Module = naga::front::wgsl::parse_str(wgsl_source)?;
// Validate the module.
// Validation can be made less restrictive by changing the ValidationFlags.
let module_info: naga::valid::ModuleInfo =
naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
)
.subgroup_stages(naga::valid::ShaderStages::all())
.subgroup_operations(naga::valid::SubgroupOperationSet::all())
.validate(&module)?;
// Translate the module.
use naga::back::glsl;
let mut glsl_source = String::new();
glsl::Writer::new(
&mut glsl_source,
&module,
&module_info,
&glsl::Options::default(),
&glsl::PipelineOptions {
entry_point: "main_fs".into(),
shader_stage: naga::ShaderStage::Fragment,
multiview: None,
},
naga::proc::BoundsCheckPolicies::default(),
)?.write()?;
assert_eq!(glsl_source, "\
#version 310 es
precision highp float;
precision highp int;
layout(location = 0) out vec4 _fs2p_location0;
void main() {
_fs2p_location0 = vec4(1.0, 1.0, 1.0, 1.0);
return;
}
");
Re-exports§
pub use ir::*;
Modules§
- arena 🔒
- Backend functions that export shader
Module
s into binary and text formats. - Code common to the front and backends for specific languages.
DiagnosticFilter
s and supporting functionality.- Frontend parsers that consume binary and text shaders and load them into
Module
s. - The Intermediate Representation shared by all frontends and backends.
Module
processing functionality.- span 🔒
- Shader validator.
Structs§
- An arena holding some kind of component (e.g., type, constant, instruction, etc.) that can be referenced.
- A strongly typed reference to an arena item.
- A strongly typed range of handles.
- A human-readable representation for a span, tailored for text source.
- A source code span, used for error reporting.
- An arena whose elements are guaranteed to be unique.
- Wrapper class for
Error
, augmenting it with a list ofSpanContext
s.
Constants§
- Width of abstract types, in bytes.
- Width of a boolean type, in bytes.
Type Aliases§
- Hash map that is faster but not resilient to DoS attacks. (Similar to rustc_hash::FxHashMap but using hashbrown::HashMap instead of alloc::collections::HashMap.) To construct a new instance:
FastHashMap::default()
- Hash set that is faster but not resilient to DoS attacks. (Similar to rustc_hash::FxHashSet but using hashbrown::HashSet instead of alloc::collections::HashMap.)
- Insertion-order-preserving hash map (
IndexMap<K, V>
), but with the same hasher asFastHashMap<K, V>
(faster but not resilient to DoS attacks). - Insertion-order-preserving hash set (
IndexSet<K>
), but with the same hasher asFastHashSet<K>
(faster but not resilient to DoS attacks). - Map of expressions that have associated variable names
- A source code span together with “context”, a user-readable description of what part of the error it refers to.