This is a simple tool to convert WGSL shaders into other formats. You can use it from the command line or in JavaScript (Node.js or browser).
cargo run --features cli -- shader.wgsl --format glslcargo run --features cli -- shader.wgsl --format hlsl --output shader.hlslFormats you can use: wgsl, glsl, hlsl, metal, spirv, spirv-asm
Node.js:
const { compileShader, init } = require('./pkg-nodejs/wgsl_wasm_transpiler.js');
init();
const wgsl = '@fragment fn main() -> @location(0) vec4<f32> { return vec4<f32>(1.0, 0.0, 0.0, 1.0); }';
const glsl = compileShader(wgsl, 'glsl');
console.log(glsl);Browser:
import init, { compileShader } from './pkg/wgsl_wasm_transpiler.js';
await init();
const result = compileShader(wgslCode, 'glsl');For the CLI:
cargo build --release --features cliFor WebAssembly:
cargo install wasm-pack
./scripts/build-wasm.sh- WGSL
- GLSL
- HLSL
- Metal
- SPIR-V (binary or assembly)
SPIR-V can be output as:
spirv: binary file (.spv), or base64 string in JSspirv-asm: readable text (.spvasm)
examples/web-example.html— browser demoexamples/nodejs-example.js— Node.js demo
compileShader(wgsl_code: string, format: string) -> stringgetSupportedFormats() -> string[]init()
cargo test --features cli
cargo test --features wasm
./scripts/test-all.sh
python3 scripts/serve.py
# Then open http://localhost:{port}/examples/web-example.html