liveBook · Manning

Shader

As explained in Systems Programming with Zig

A shader is a specialized program that performs a stage of graphics processing, such as transforming vertices or shading fragments.

A shader is a GPU program that handles a programmable stage of the rasterization pipeline. Despite the name, shaders do more than lighting and shadows: they can transform geometry, generate patterns, and produce other visual effects. section 7.1.2

A draw call starts the pipeline with geometry to render. A vertex shader can move vertices or project a 3D scene onto the screen. The rasterizer turns shapes into fragments, and a fragment shader determines their color before the fragments are assembled into an image. section 7.1.2

A minimal shader pair

A vertex shader passes a two-dimensional position into gl_Position; a fragment shader gives every fragment the same magenta color. The sources are GLSL strings stored as Zig global constants. For small shaders, multiline strings keep the sources readable; @embedFile is an alternative. section 7.4.3

Listing 7.9 Shader sources
const vertex_shader_source =
    \\#version 330 core #1
    \\
    \\in vec2 pos; #2
    \\
    \\void main() {
    \\    gl_Position = vec4(pos, 0.0, 1.0); #3
    \\}
;
const fragment_shader_source =
    \\#version 330 core #1
    \\
    \\out vec4 fragColor; #4
    \\
    \\void main() {
    \\    fragColor = vec4(1.0, 0.0, 1.0, 1.0); #5
    \\}
;

Inspect the minimal vertex and fragment shader sources and their GLSL version declaration. section 7.4.3

OpenGL expects the application to compile shader sources into shader objects and link them into a program object. For each shader, create an object of the appropriate kind, supply the source, compile it, and check the compilation status. If compilation fails, retrieve and report the information log. section 7.4.3

Listing 7.10 Acting like a compiler
fn compileShader(
    kind: c_uint, #1
    source: [:0]const u8, #2
) error{ShaderCompilationFailed}!c_uint { #3
    const shader = c.glCreateShader(kind); #4
    errdefer c.glDeleteShader(shader); #4

    c.glShaderSource(shader, 1, @ptrCast(&source), null); #5
    c.glCompileShader(shader); #5

    var compile_success: c_int = undefined;
    c.glGetShaderiv(shader, c.GL_COMPILE_STATUS, &compile_success); #6
    if (compile_success == 0) { #6
        var info_buf: [512]u8 = undefined;
        var info_len: c.GLsizei = undefined;
        c.glGetShaderInfoLog(shader, info_buf.len, &info_len, &info_buf); #7
        const info = info_buf[0..@intCast(info_len)];

        std.debug.print("shader compilation failed: {s}\n", .{info}); #7

        return error.ShaderCompilationFailed;
    }

    return shader;
}

Follow a Zig helper that compiles a shader, reports errors, and deletes the shader object on failure. section 7.4.3

To build the program, compile both shaders, attach them to a program object, link it, and check for linking errors. Zig’s defer and errdefer make object lifetimes explicit: shader objects are deleted after use, and a failed program is deleted rather than left behind. section 7.4.3

How Systems Programming with Zig covers it

How deep it goes
Explains pipeline roles and OpenGL compilation, linking, and cleanup, while deferring a full GLSL guide.
Examples
Demonstrates a pass-through vertex shader and a constant-magenta fragment shader stored as Zig strings.
What you can do after
Apply it via OpenGL by compiling shader sources, checking errors, and linking them into a program.
How it connects
Connects shaders to rasterization, vertex processing, fragment shading, and OpenGL program objects.

Also explained in

Compare how 2 books explain shader →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.