Crate naga

Source
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§

back
Backend functions that export shader Modules into binary and text formats.
common
Code common to the front and backends for specific languages.
compact
diagnostic_filter
DiagnosticFilters and supporting functionality.
error
front
Frontend parsers that consume binary and text shaders and load them into Modules.
ir
The Intermediate Representation shared by all frontends and backends.
keywords
Lists of reserved keywords for each shading language with a frontend or backend.
proc
Module processing functionality.
valid
Shader validator.

Structs§

Arena
An arena holding some kind of component (e.g., type, constant, instruction, etc.) that can be referenced.
Handle
A strongly typed reference to an arena item.
Range
A strongly typed range of handles.
SourceLocation
A human-readable representation for a span, tailored for text source.
Span
A source code span, used for error reporting.
UniqueArena
An arena whose elements are guaranteed to be unique.
WithSpan
Wrapper class for Error, augmenting it with a list of SpanContexts.

Constants§

ABSTRACT_WIDTH
Width of abstract types, in bytes.
BOOL_WIDTH
Width of a boolean type, in bytes.

Type Aliases§

FastHashMap
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()
FastHashSet
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.)
FastIndexMap
Insertion-order-preserving hash map (IndexMap<K, V>), but with the same hasher as FastHashMap<K, V> (faster but not resilient to DoS attacks).
FastIndexSet
Insertion-order-preserving hash set (IndexSet<K>), but with the same hasher as FastHashSet<K> (faster but not resilient to DoS attacks).
SpanContext
A source code span together with “context”, a user-readable description of what part of the error it refers to.