Application shaders
The renderer's effects are fixed (rect.bento, filter.bento, slug.bento...). An application can add its own: a fragment function written in Bento that fills a rounded rectangle, with parameters, up to four textures and a blend mode. Everything around it is the renderer's own: the transform stack, the corner radii, the clips, the alpha stack, layers, planes (3D transforms), the order of drawing. Added in 0.25.0; Bento since the toolkit's shaders moved to it (src/md/infrastructure/shaders.md).
// once
shader := render.create_app_shader(&r, {name = "foil", bento = FOIL_BENTO, spirv = FOIL_SPIRV, dxil = FOIL_DXIL, hash = FOIL_HASH})
normals := render.create_texture_data(&r, 512, 512, .RGBA8) // raw texels: no sRGB decoding, no premultiplication
render.update_texture_data(&r, normals, 0, 0, 512, 512, texels)
// every frame
draw.app_shader(&dc, shader, card, {{time, 0, 0, 0}, {light.x, light.y, 0.4, 1}}, {normals}, .Normal, radii = {16, 16, 16, 16})The shader
Your file is a module named app that exports one function, shade. It is compiled against the toolkit's module rect (src/render/rect.bento) and linked with the toolkit's entry point ps_app (src/render/app.bento):
module app;
import std;
import rect;
export fn shade(a: rect::AppIn, params: &storage [array; _]f32x4, t: rect::AppTextures) -> f32x4 {
let p = rect::param(params, a, 0); // the draw's k-th vec4
let n = [sampled; 2]f32 { t.tex0, t.samp_img }.sample(a.uv).xyz * 2.0 - 1.0;
return f32x4 { 0.5 + 0.5 * n.x, 0.5 + 0.5 * n.y, 0.5, 1.0 };
}It returns the color of one pixel in linear light with straight alpha. The toolkit multiplies it by the coverage of the (rounded, clipped) rectangle, by the tint and alpha of the draw, converts it to the blend space of the target and blends it. rect::AppIn:
| field | |
|---|---|
uv | position in the rectangle, (0, 0) top-left to (1, 1) bottom-right (slightly outside on the antialiased rim) |
pos, size | the same in local units (the units the rectangle was drawn in), and the rectangle's size |
px | screen pixels per local unit (what the transforms made of a unit) |
frag | the pixel in the render target |
space | blend space of the target (0 linear, 1 sRGB); the color you return is linear either way |
base | where the draw's parameters start in params (rect::param reads it) |
Available to your code: rect::param(params, a, k); the textures t.tex0 .. t.tex3 ([texture; 2]f32; an empty slot is a transparent 1 x 1 texel) and the samplers t.samp_img (trilinear, clamp), t.samp_wrap (trilinear, repeat) and t.samp (bilinear); everything rect exports (colour spaces, rect::srgb_encode...). shade is called in uniform control flow: derivatives and samples with an implicit level work before any branch, and Bento checks the rest when it compiles. Parameters are vec4 values, at most render.MAX_APP_PARAMS (64) per draw; a draw with fewer reads zeros for the rest only by accident, so declare what you read.
The precompiled blobs
Vulkan embeds SPIR-V and Direct3D 12 embeds DXIL, as the toolkit's own shaders do (no program compiles a shader): Shokado writes both from your module, on every system. Run, after editing it (and after a toolkit update that changes rect.bento or app.bento):
./build/shaders_tool app foil path/foil.bento path/shaders # writes foil.spv, foil.dxil and foil.sha256
./build/shaders_tool app foil path/foil.bento path/shaders -check # exit code 1 when staleand load the three files and your source with #load (the toolkit's own licence card does: src/ui/licence_card_kept.fm). create_app_shader compares the SHA-256 of the sources (render.app_module_hash: Shokado's version, rect.bento, app.bento and your module) with the one in foil.sha256: a blob built from other sources is refused with a message, never drawn. Shokado comes from the toolkit's flake (nix develop) or tools/trame fetch_shokado (tools/shaders fetches it when it is missing); a program that only embeds blobs does not need it. The vertex shader is the renderer's (vs_main).
Data textures
render.create_texture_data(r, w, h, format = .RGBA8, mips = true) makes a texture of raw 8-bit values: .RGBA8 (a normal map, a mask pack) or .R8 (a height map). Unlike create_texture_rgba nothing converts them: no sRGB decoding, no premultiplication, whatever the blend space, so texel * 2 - 1 is a vector. update_texture_data(r, t, x, y, w, h, pixels, stride = 0) uploads a block. With mips the full chain is built (box filter) before the next frame after an update, so a texture seen smaller than itself is filtered, not aliased. Destroy with destroy_texture. After a device loss the contents are gone (same handle): upload again when render.Renderer.device_generation changes, as for images.
draw.app_shader
app_shader :: proc(c: ^Context, shader: render.App_Shader, r: Rect, params: []Vec4, textures: []render.Texture = nil,
blend := render.App_Blend.Normal, radii: [4]f32 = {}, tint: Color = WHITE) -> boolFills r (current local space), in order with everything else: under the current transform (a rotated rectangle keeps exact rounded corners and a 1 px antialiased edge), clips, alpha stack, inside layers and planes. Blend modes (App_Blend, fixed-function blending of the premultiplied result): Normal (over), Add (dst + src: a glow adds its light), Screen, Multiply.
It returns false and draws nothing when the shader is 0 (it failed to build: the caller draws its fallback), when it is not render.app_shader_ready (destroyed; or the device could not remake it after a loss, or refused its pipeline: the handle stays reserved until destroy_app_shader, so no other shader takes it), when a picture is being recorded (picture_begin: an application shader is not recordable, its parameters change every frame; draw it around the replay), or when there are more than 64 parameters or 4 textures.
Kept layers. A plane or layer given a cache_key keeps its pixels, shader output included: an animated shader must make its time part of the key (or the layer is not kept), a still one costs nothing while the key holds. The pointer-tilt of a card that is only projected differently (draw_plane's matrix changes, the plane's content does not) leaves the kept layer alone.
Cost
One instance and one draw call per app_shader, with its descriptor set on Vulkan (an application shader is a batch of its own: it breaks batching with its neighbors, like an image of another texture). The pixel cost is yours: the rectangle's coverage, the clips and the tint add a few operations to shade.
Tests
tests/appshaders (Vulkan and Direct3D 12; ./build/run_tests appshaders): colors in either blend space, uv, raw data textures, two textures and empty slots, the four blend modes against their formulas in both spaces, alpha stack and tint, rounded corners and clips, a rotated and scaled rectangle (size and px), inside a layer and a plane, 8 draws with their own parameters, the refusals (shader 0, too many parameters or textures, recording, a stale blob, no blob), a device loss, and mip filtering of a data texture.