Convert Figma logo to code with AI

gfx-rs logonaga

Universal shader translation in Rust

1,525
194
1,525
17

Top Related Projects

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.

1,816

A collection of tools, libraries, and tests for Vulkan shader compilation.

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.

4,753

Where the GPU for the Web work happens!

2,977

Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.

3,403

A conformant OpenGL ES implementation for Windows, Mac, Linux, iOS and Android.

Quick Overview

Naga is a shader translation library written in Rust. It aims to provide a unified intermediate representation (IR) for various shading languages, enabling cross-compilation between different graphics APIs and platforms. Naga supports parsing, analyzing, and generating shaders for multiple graphics backends.

Pros

  • Cross-platform shader translation between different graphics APIs (e.g., GLSL, HLSL, MSL)
  • Written in Rust, providing memory safety and performance benefits
  • Modular design allowing for easy integration into other projects
  • Supports modern shader features and extensions

Cons

  • Still in active development, may have occasional breaking changes
  • Limited documentation for advanced use cases
  • Learning curve for understanding the IR and translation process
  • May not support all niche shader language features or extensions

Code Examples

  1. Parsing a GLSL shader:
use naga::front::glsl::Parser;

let source = r#"
    #version 450
    layout(location = 0) in vec3 v_position;
    layout(location = 0) out vec4 f_color;
    void main() {
        f_color = vec4(v_position, 1.0);
    }
"#;

let mut parser = Parser::default();
let module = parser.parse(&naga::ShaderStage::Fragment, source).unwrap();
  1. Generating SPIR-V from a Naga module:
use naga::back::spv;

let spv_options = spv::Options::default();
let pipeline_options = naga::back::PipelineOptions::default();

let spv = spv::write_vec(&module, &spv_options, &pipeline_options).unwrap();
  1. Validating a Naga module:
use naga::valid::Validator;

let mut validator = Validator::new(naga::valid::ValidationFlags::all(), Default::default());
let info = validator.validate(&module).unwrap();

Getting Started

To use Naga in your Rust project, add it to your Cargo.toml:

[dependencies]
naga = "0.11"

Then, in your Rust code:

use naga::{front::glsl, back::spv, valid::Validator};

fn main() {
    let source = r#"
        #version 450
        void main() {
            gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
        }
    "#;

    let mut parser = glsl::Parser::default();
    let module = parser.parse(&naga::ShaderStage::Vertex, source).unwrap();

    let mut validator = Validator::new(naga::valid::ValidationFlags::all(), Default::default());
    validator.validate(&module).unwrap();

    let spv = spv::write_vec(&module, &spv::Options::default(), &naga::back::PipelineOptions::default()).unwrap();
    println!("Generated SPIR-V: {:?}", spv);
}

Competitor Comparisons

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.

Pros of SPIRV-Cross

  • More mature and widely adopted in the industry
  • Supports a broader range of shader languages and targets
  • Extensive documentation and community support

Cons of SPIRV-Cross

  • Written in C++, which may be less accessible for some developers
  • Larger codebase and potentially more complex to maintain
  • May have higher memory usage and slower compilation times

Code Comparison

Naga (Rust):

let module = naga::front::wgsl::parse_str(&source)?;
let info = naga::valid::Validator::new().validate(&module)?;
let output = naga::back::glsl::write_string(&module, &info, &options)?;

SPIRV-Cross (C++):

spirv_cross::CompilerGLSL glsl(spirv_data);
glsl.compile();
std::string output = glsl.dump();

Summary

Naga is a newer, Rust-based shader translation library, while SPIRV-Cross is a more established C++ solution. Naga offers potential benefits in terms of memory safety and performance, but SPIRV-Cross has broader language support and industry adoption. The choice between them depends on project requirements, language preferences, and specific use cases.

1,816

A collection of tools, libraries, and tests for Vulkan shader compilation.

Pros of shaderc

  • Mature and widely adopted, with extensive industry support
  • Comprehensive support for GLSL and HLSL input languages
  • Robust optimization capabilities for generated SPIR-V

Cons of shaderc

  • C++ based, which may be less accessible for some developers
  • Larger binary size and potentially slower compilation times
  • Less flexible for custom shader language support

Code Comparison

shaderc (C++):

shaderc::Compiler compiler;
shaderc::CompileOptions options;
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(
    shader_source, shaderc_glsl_vertex_shader, "shader.vert", options);

naga (Rust):

let module = naga::front::glsl::parse_str(
    shader_source,
    &naga::front::glsl::Options::default(),
    naga::ShaderStage::Vertex,
    None,
)?;

Key Differences

  • Language: shaderc is written in C++, while naga is written in Rust
  • Focus: shaderc primarily targets GLSL/HLSL to SPIR-V, while naga supports multiple shader languages and targets
  • Ecosystem: shaderc is part of the larger Vulkan ecosystem, while naga is more closely tied to the gfx-rs project
  • Flexibility: naga offers more extensibility for custom shader languages and intermediate representations

Both projects aim to provide shader compilation and transformation capabilities, but they cater to different use cases and developer preferences. shaderc is more established and widely used in industry, while naga offers a more modern and flexible approach with its Rust implementation.

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.

Pros of DirectXShaderCompiler

  • More mature and widely adopted in the industry
  • Extensive support for DirectX shader models and HLSL
  • Backed by Microsoft, ensuring long-term support and updates

Cons of DirectXShaderCompiler

  • Larger codebase and potentially more complex to contribute to
  • Primarily focused on DirectX and Windows platforms
  • Slower compilation times for large shader codebases

Code Comparison

Naga (WGSL):

[[stage(vertex)]]
fn main([[builtin(vertex_index)]] vert_idx: u32) -> [[builtin(position)]] vec4<f32> {
    let pos = array<vec2<f32>, 3>(
        vec2<f32>(-0.5, -0.5),
        vec2<f32>( 0.5, -0.5),
        vec2<f32>( 0.0,  0.5)
    );
    return vec4<f32>(pos[vert_idx], 0.0, 1.0);
}

DirectXShaderCompiler (HLSL):

struct VSOutput {
    float4 pos : SV_POSITION;
};

VSOutput main(uint vert_idx : SV_VertexID) {
    const float2 pos[3] = {
        float2(-0.5, -0.5),
        float2( 0.5, -0.5),
        float2( 0.0,  0.5)
    };
    VSOutput output;
    output.pos = float4(pos[vert_idx], 0.0, 1.0);
    return output;
}
4,753

Where the GPU for the Web work happens!

Pros of gpuweb

  • Broader scope, focusing on the entire WebGPU API specification
  • More comprehensive documentation and examples for developers
  • Wider community involvement and support from major browser vendors

Cons of gpuweb

  • Less focused on specific shader language translation
  • May have a slower development cycle due to its broader scope
  • Potentially more complex for developers looking for a specific shader solution

Code Comparison

naga:

use naga::front::wgsl::parse_str;
let module = parse_str(shader_source)?;

gpuweb:

const shaderModule = device.createShaderModule({
  code: shaderSource
});

Summary

naga is a focused shader translation library, while gpuweb is a comprehensive WebGPU API specification. naga offers more specialized shader language support, while gpuweb provides a broader framework for GPU programming on the web. The choice between them depends on the specific needs of the project, with naga being more suitable for shader-specific tasks and gpuweb for full WebGPU implementations.

2,977

Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.

Pros of glslang

  • Industry standard reference compiler for GLSL and ESSL
  • Supports a wide range of shader stages and extensions
  • Backed by Khronos Group, ensuring compatibility with OpenGL and Vulkan

Cons of glslang

  • Primarily focused on GLSL/ESSL, limited support for other shading languages
  • Can be complex to integrate and use in custom toolchains
  • Slower compilation times compared to more specialized tools

Code Comparison

glslang:

#include <glslang/Public/ShaderLang.h>

// Initialize glslang
glslang::InitializeProcess();

// Compile GLSL to SPIR-V
glslang::TShader shader(EShLangVertex);
shader.setStrings(&shaderSource, 1);
shader.setEnvInput(glslang::EShSourceGlsl, EShLangVertex, glslang::EShClientVulkan, 100);
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_0);

naga:

use naga::front::glsl::Parser;

let mut parser = Parser::default();
let module = parser.parse(
    naga::ShaderStage::Vertex,
    &source,
    &naga::front::glsl::Options::default(),
)?;

Summary

While glslang is the industry standard for GLSL compilation, naga offers a more flexible and language-agnostic approach to shader translation. glslang excels in GLSL support and Khronos ecosystem compatibility, while naga provides a more modern, Rust-based solution with support for multiple shading languages and backends.

3,403

A conformant OpenGL ES implementation for Windows, Mac, Linux, iOS and Android.

Pros of ANGLE

  • More mature and widely adopted, used in major browsers like Chrome
  • Extensive support for DirectX and OpenGL ES translation
  • Robust testing infrastructure and performance optimizations

Cons of ANGLE

  • Primarily focused on graphics APIs, less versatile for general shader translation
  • Larger codebase and potentially steeper learning curve
  • More complex build process due to its broad platform support

Code Comparison

ANGLE (GLSL to HLSL translation):

void main()
{
    gl_Position = vec4(aPosition, 1.0);
    vColor = aColor;
}

Naga (GLSL to WGSL translation):

fn vertex_main(
    @location(0) position: vec3<f32>,
    @location(1) color: vec4<f32>
) -> VertexOutput {
    var out: VertexOutput;
    out.position = vec4<f32>(position, 1.0);
    out.color = color;
    return out;
}

Summary

ANGLE is a more established project with a focus on graphics API translation, particularly for web browsers. Naga, on the other hand, is a newer, more versatile shader translation library written in Rust. While ANGLE excels in DirectX and OpenGL ES support, Naga offers broader shader language support and integration with the gfx-rs ecosystem. The choice between them depends on specific project requirements and target platforms.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Naga

Matrix Crates.io Docs.rs

Developement has moved inside the wgpu respository! Despite it being moved, it is still being developed as a stand alone library.

This respository is now archived.