Skip to content

Load a Shader From a File

A shader longer than a few lines is easier to write in its own .glsl file, where your editor’s GLSL support works on it. In VSCode, install a GLSL extension such as Shader languages support.

use "std/visuals" as gfx;
let v = gfx.load_shader("plasma", "shaders/plasma.glsl");
v >> screen;
PLAY;

The path is relative to the .non file that runs the line. The file holds the same GLSL you would put in a string: #version 300 es, a precision, and an out vec4.

With a visuals window open, a compile error in the shader is marked in the .glsl file, on the line you wrote.

Nothing watches the file. After you save it, run the gfx.load_shader line again: the file is read again, and the visual named plasma takes the new shader, keeping its wiring and its bound uniforms.

A shader’s source is a string, so build it from parts. load_file reads a file as a string, with the same relative path:

use "std/visuals" as gfx;
let lib = load_file("shaders/lib.glsl"); // #version, precision and shared functions
let v = gfx.Visual("plasma", lib + load_file("shaders/plasma-main.glsl"));
v >> screen;

A shader built with gfx.Visual has no file of its own, so its compile errors are marked on the >> screen line, and the message gives the line number within the whole string.