Skip to content
zazbone edited this page Sep 12, 2023 · 3 revisions

Introduction

NZSL is a strong typed shading language, inspired by C++ and Rust, with a syntax aimed at being easy to use yet very clear about what it does.

Here's a short example of a fragment/vertex shader which outputs vertex color, first in GLSL then the equivalent NZSL code.

GLSL vertex shader:

#version 450

layout(std140, binding = 0) uniform Matrices
{
	mat4 projection;
	mat4 view;
	mat4 model;
} matrices;

layout(location = 0) in vec3 vertPos;
layout(location = 1) in vec3 vertColor;
layout(location = 0) out vec3 fragColor;

void main()
{
	vec4 worldPos = matrices.model * vec4(vertPos, 1.0);
 
	gl_Position = matrices.projection * matrices.view * worldPos;
	fragColor = vertColor;
}

GLSL fragment shader:

#version 450

layout(location = 0) in vec3 vertColor;
layout(location = 0) out vec3 outputColor;

void main()
{
	outputColor = vertColor;
}

And here's the equivalent NZSL shader:

[nzsl_version("1.0")]
module;

[layout(std140)]
struct Matrices
{
	projection: mat4[f32],
	view: mat4[f32],
	model: mat4[f32]
}

external
{
	[binding(0)] matrices: uniform[Matrices]
}

struct VertIn
{
	[location(0)] pos: vec3[f32],
	[location(1)] color: vec4[f32]
}

struct VertOut
{
	[location(0)] color: vec4[f32],
	[builtin(position)] pos: vec4[f32]
}

[entry(vert)]
fn main(input: VertIn) -> VertOut
{
	let worldPos = matrices.model * vec4[f32](input.pos, 1.0);

	let output: VertOut;
	output.pos = matrices.projection * matrices.view * worldPos;
	output.color = input.color;
	return output;
}

struct FragOut
{
	[location(0)] color: vec4[f32]
}

[entry(frag)]
fn main(input: VertOut) -> FragOut
{
	let output: FragOut;
	output.color = input.color;
	return output;
}

So if you're familiar with HLSL or GLSL, it should feel familiar yet a bit different.

First of all, it's a bit more verbose than GLSL by choice (vec3[f32] instead of vec3 for example), but it also has type inference and allows you to define multiple shaders stages inside of the same file (it also has modules which are described on their own page).

The module statement

Struct declaration

Function declaration

Variable declaration

Aliases