Skip to content
This repository has been archived by the owner on Jun 19, 2021. It is now read-only.

Shader code conventions

Kristin Schmidt edited this page Jul 26, 2016 · 2 revisions

While we use rustfmt to format Rust code correctly, we have no such tool for shader (GLSL) code. This page defines how we want to write GLSL code.

We often use the same style as in Rust:

  • the braces { and } are used like in Rust, notably: the opening brace belongs at the end of the line starting the block
  • we indent using 4 spaces
  • lines must not be longer than 100 characters
  • spacing between operators like in Rust: nearly always use a space to separate operand from operator
  • variable- and function-names are written in snake_case

Apart from that we will use some kind of naming convention for in, out and uniform variables.

The in-variables for vertex shader are written with the correct layout qualifier and start with in_:

layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;

The same rule applies to out-variables in fragment shaders (starting with out_, of course).

Uniform- as well as in- and out-variables between shaders are passed via interface blocks. The instance name of the uniform block is called u, i for in blocks, o for out blocks. Here is a complete example:

test.vert:

layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;

uniform Uniforms {
    mat4 view_matrix;
    mat4 proj_matrix;
} u;

out VertexData {
    vec3 world_pos;
    vec3 color;
} o;

void main() {
    o.world_pos = u.proj_matrix * u.view_matrix * vec4(in_position, 1);
    gl_Position = o.worldPos;
    o.color = vec3(in_position.z);
}

test.frag:

in VertexData {
    vec3 world_pos;
    vec3 color;
} i;

out vec3 out_color;

void main() {
    out_color = i.color;
}
Clone this wiki locally