Defining a Shader
Each shader has a YAML prototype stored inResources/Prototypes/Shaders. All shaders have fields:
type: must beshaderid: unique prototype ID (a string) that identifies this shaderkind: eithercanvasorshader(see below)
canvas Shaders
This is a shader with preset options, akin to Godot’s CanvasItemMaterial. This shader has two required properties:
blend_mode: The way the object is drawn over the scene. These are the same as the equivalent for Godot. Barring the naming being slightly different. Possible values aremix,add,subtract,multiplyandpremultiplied_alphalight_mode: The way the object interacts with light. Again equivalent to Godot’s, with the values beingnormal,unshadedandlight_only.
unshaded shader, which is used to draw the lit portion of computer displays and indicators by masking lighting operations, is defined by:
/Shaders/Internal/default-sprite.swsl), with the lighting, blending, and stencil options applied.
source Shaders
These are custom shaders written in the Space Wizard Shader Language (SWSL). source shaders have one required property:
path: The path, with respect to theResources/directory, of the SWSL shader source file.
.swsl files in Resources/Textures/Shaders.
SWSL
Space Wizard Shader Language (SWSL) is based on the Godot Shading language. Most of the compatibility differences between Godot and SWSL are handled automatically, except for the following differences:-
You MUST ensure that all numeric types (e.g.
float,vec3) have ahighporlowpprecision qualifier. See this article on why. - Avoid using any variable names that are reserved words in common shader specifications. Your computer may ignore them but they will break on other machines. Check the keywords section (3.8) here
Available Variables (Fragment Shaders)
| Name | Type | Description |
|---|---|---|
FRAGCOORD | highp vec4 | The coordinates within the fragment. |
COLOR | lowp vec4 | The resulting pixel color. (Think of it as the return value of the fragment shader.) |
lightMap | sampler2D | The lighting map of the current fragment (applied automatically by base-default.frag) |
modulate | highp vec4 | The draw color (applied automatically by base-default.frag) |
SCREEN_PIXEL_SIZE | highp vec2 | The size of one pixel, in local units. |
TIME | highp float | The number of seconds since game startup. |
Stencil Test Parameters
Shader support stencil operations. This is an advanced rendering feature that can be useful for some things if you know what you’re doing. The feature closely mimics the stencil parameters as exposed by OpenGL (and as far as I can tell, Vulkan too). See The OpenGL wiki if you need a reference. Stencil parameters are defined in a separatestencil object. As an example:
kind.
The options are:
ref: The reference to compare / write as passed to the second parameter ofglStencilFunc.- Default is 0.
op: The operation to apply to the stencil buffer if the stencil test passes. You can only set the op on pass (glStencilOp’s third parameter).- Options are
Keep,Zero,Replace,IncrementClamp,IncrementWrap,DecrementClamp,DecrementWrap,Invert - Default is
Keep.
- Options are
func: The comparison function to use to see if the test passes. (glStencilFunc’s first parameter).- Options are
Always,Never,Less,LessOrEqual,Greater,GreaterOrEqual,NotEqual,Equal. - Default is
Always.
- Options are
readMask: Mask to use when reading from the stencil buffer (glStencilFunc’s third parameter).- Default is all 1’s
writeMask: Mask to use when writing to the stencil buffer (glStencilMask’s parameter).- Default is all 1’s
Overlays
An overlay applies a shader to an entire screen or viewport (as opposed to individual sprites). Drug effects, blindness, and singularity space bending are examples of overlays (written in C#) that load shader prototypes (defined in YAML) with may load custom shader effects (written in SWSL). Overlays extendOverlay, for example:
PostInit() in Content.Client/Entry/EntryPoint.cs.
Testing and Debugging
You can use the
/rldshader command to reload the .swsl shaders without restarting the game. This means you can often use colour outputs to interactively debug shaders.error.glsl file with the post-processed shader that failed to load. Usually the shader compilation output log will also be shown and identify the problem, but you can also feed the .glsl file to a tool like
glslang.
External Tools
- renderdoc - An excellent tool for debugging rendering and shaders.