Shader

Output Cheatsheet

Write extra results to rendertargets from the same shader — emulated multiple render targets with VSDK_OUT

A shader normally writes one result. Declare extra outputs with VSDK_OUT(name, value) inside fragment() and bind each one to a rendertarget with shader:set_output — the world keeps rendering as usual while every output is written to its own rendertarget.

Godot shaders can only write one output, so Vital compiles the shader once for the world and once per declared output, and draws each bound output again with its own variant.


Syntax

Place it inside fragment(), once per output you want to write.

VSDK_OUT(name, value);
ArgumentTypeDescription
nameidentifierOutput name, bound with shader:set_output
valuevec4Color written to the output

How It Renders

DrawResultCost
WorldCOLOR / ALBEDO to the screen. VSDK_OUT is ignoredAlways drawn
OutputLast VSDK_OUT of that name that ran, to its rendertargetOne extra draw per bound output. Unbound outputs are free

Canvas Item

TopicBehavior
Written colorCOLOR
Drawn bycore.engine.draw_material, once per bound output
Current targetAn output bound to the rendertarget being drawn to is skipped

Spatial

How an output behaves in a spatial shader. Each surface using the shader is drawn again, hidden, only for the output's rendertarget.

TopicBehavior
LightingUnlit, no shadows or fog
AlphaSurface coverage, or the output's .a if the shader writes ALPHA
Surfaces not using the outputDrawn opaque black, so they still hide what is behind them. Other models don't
CameraCopies the active camera, so use a rendertarget the size of the screen
RendertargetBinding turns on its 3D rendering, up to 12 rendertargets at once
Skinned modelsSkeleton, skin, blendshapes and vertex() displacement follow the original

Limitations

What is not allowed, and what happens when it is broken.

CaseBehavior
VSDK_OUT outside fragment()Error when the shader is created
POST_PROCESS shadersNot supported, error when the shader is created
Same name used more than onceAllowed, e.g. in different branches. The last one that ran is used
discard before VSDK_OUTThe pixel is discarded in the output too

Examples

Spatial shader that renders green in the world and red into a rendertarget
local shader = core.shader.create_from_raw([[
    void fragment() {
        ALBEDO = vec3(0.0, 1.0, 0.0);               // world: green
        VSDK_OUT(mask, vec4(1.0, 0.0, 0.0, 1.0));   // "mask" output: red
    }
]], core.shader.shader_mode.SPATIAL)

local mask = core.rendertarget.create(core.display.get_resolution(), true)

shader:set_output("mask", mask)
shader:apply_to_material(my_model, "*", "*")
Canvas Item shader with two outputs
local shader = core.shader.create_from_raw([[
    void fragment() {
        COLOR = vec4(0.0, 1.0, 0.0, 1.0);
        VSDK_OUT(mask, vec4(1.0, 0.0, 0.0, 1.0));
        VSDK_OUT(data, vec4(0.5, 0.25, 0.0, 1.0));
    }
]], core.shader.shader_mode.CANVAS_ITEM)

shader:set_output("mask", core.rendertarget.create({512, 512}, true))
shader:set_output("data", core.rendertarget.create({512, 512}, true))

util.event.on("sandbox:draw", function()
    core.engine.draw_material({0, 0}, {512, 512}, shader)
end)

On this page