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);| Argument | Type | Description |
|---|---|---|
name | identifier | Output name, bound with shader:set_output |
value | vec4 | Color written to the output |
How It Renders
| Draw | Result | Cost |
|---|---|---|
| World | COLOR / ALBEDO to the screen. VSDK_OUT is ignored | Always drawn |
| Output | Last VSDK_OUT of that name that ran, to its rendertarget | One extra draw per bound output. Unbound outputs are free |
Canvas Item
| Topic | Behavior |
|---|---|
| Written color | COLOR |
| Drawn by | core.engine.draw_material, once per bound output |
| Current target | An 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.
| Topic | Behavior |
|---|---|
| Lighting | Unlit, no shadows or fog |
| Alpha | Surface coverage, or the output's .a if the shader writes ALPHA |
| Surfaces not using the output | Drawn opaque black, so they still hide what is behind them. Other models don't |
| Camera | Copies the active camera, so use a rendertarget the size of the screen |
| Rendertarget | Binding turns on its 3D rendering, up to 12 rendertargets at once |
| Skinned models | Skeleton, skin, blendshapes and vertex() displacement follow the original |
Limitations
What is not allowed, and what happens when it is broken.
| Case | Behavior |
|---|---|
VSDK_OUT outside fragment() | Error when the shader is created |
POST_PROCESS shaders | Not supported, error when the shader is created |
| Same name used more than once | Allowed, e.g. in different branches. The last one that ran is used |
discard before VSDK_OUT | The pixel is discarded in the output too |
Examples
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, "*", "*")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)