Crate naga
Available on crate feature
unstable-wgpu-26
only.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;
}
");
Modules§
- back
- Backend functions that export shader
Module
s into binary and text formats. - common
- Code common to the front and backends for specific languages.
- compact
- diagnostic_
filter DiagnosticFilter
s and supporting functionality.- error
- front
- Frontend parsers that consume binary and text shaders and load them into
Module
s. - 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.
- Barrier
- Memory barrier flags.
- Block
- A code block is a vector of statements, with maybe a vector of spans.
- Constant
- Constant value.
- DocComments
- Doc comments preceding items.
- Entry
Point - The main function for a pipeline stage.
- Function
- A function defined in the module.
- Function
Argument - A function argument.
- Function
Result - A function result.
- Global
Variable - Variable defined at module level.
- Handle
- A strongly typed reference to an arena item.
- Local
Variable - Variable defined at function level.
- Module
- Shader module.
- Override
- Pipeline-overridable constant.
- Range
- A strongly typed range of handles.
- RayFlag
- Ray flags used when casting rays. Matching vulkan constants can be found in https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/KHR/ray_common/ray_flags_section.txt
- Resource
Binding - Pipeline binding information for global resources.
- Scalar
- Characteristics of a scalar type.
- Source
Location - A human-readable representation for a span, tailored for text source.
- Span
- A source code span, used for error reporting.
- Special
Types - Set of special types that can be optionally generated by the frontends.
- Storage
Access - Flags describing an image.
- Struct
Member - Member of a user-defined structure.
- Switch
Case - A case for a switch statement.
- Type
- A data type declared in the module.
- Unique
Arena - An arena whose elements are guaranteed to be unique.
- With
Span - Wrapper class for
Error
, augmenting it with a list ofSpanContext
s.
Enums§
- Address
Space - Addressing space of variables.
- Array
Size - Size of an array.
- Atomic
Function - Function on an atomic value.
- Binary
Operator - Operation that can be applied on two values.
- Binding
- Describes how an input/output variable is to be bound.
- BuiltIn
- Built-in inputs and outputs.
- Collective
Operation - Conservative
Depth - Enables adjusting depth without disabling early Z.
- Derivative
Axis - Axis on which to compute a derivative.
- Derivative
Control - Hint at which precision to compute a derivative.
- Direction
- Early
Depth Test - Explicitly allows early depth/stencil tests.
- Expression
- An expression that can be evaluated to obtain a value.
- Gather
Mode - The specific behavior of a
SubgroupGather
statement. - Image
Class - Sub-class of the image type.
- Image
Dimension - The number of dimensions an image has.
- Image
Query - Type of an image query.
- Interpolation
- The interpolation qualifier of a binding or struct field.
- Literal
- Math
Function - Built-in shader function for math.
- Predeclared
Type - Return types predeclared for the frexp, modf, and atomicCompareExchangeWeak built-in functions.
- RayQuery
Function - An operation that a
RayQuery
statement applies to itsquery
operand. - RayQuery
Intersection - Type of a ray query intersection. Matching vulkan constants can be found in https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/KHR/SPV_KHR_ray_query.asciidoc but the actual values are different for candidate intersections.
- Relational
Function - Built-in shader function for testing relation between values.
- Sample
Level - Sampling modifier to control the level of detail.
- Sampling
- The sampling qualifiers of a binding or struct field.
- Scalar
Kind - Primitive type for a scalar.
- Shader
Stage - Stage of the programmable pipeline.
- Statement
- Instructions which make up an executable block.
- Storage
Format - Image storage format.
- Subgroup
Operation - Switch
Value - The value of the switch case.
- Swizzle
Component - Component selection for a vector swizzle.
- Type
Inner - Enum with additional information, depending on the kind of type.
- Unary
Operator - Operation that can be applied on a single value.
- Vector
Size - Number of components in a vector.
Constants§
- ABSTRACT_
WIDTH - Width of abstract types, in bytes.
- BOOL_
WIDTH - Width of a boolean type, in bytes.
Type Aliases§
- Bytes
- Number of bytes per scalar.
- Fast
Hash Map - 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()
- Fast
Hash Set - 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.)
- Fast
Index Map - Insertion-order-preserving hash map (
IndexMap<K, V>
), but with the same hasher asFastHashMap<K, V>
(faster but not resilient to DoS attacks). - Fast
Index Set - Insertion-order-preserving hash set (
IndexSet<K>
), but with the same hasher asFastHashSet<K>
(faster but not resilient to DoS attacks). - Span
Context - A source code span together with “context”, a user-readable description of what part of the error it refers to.