# Plugins

---

# Shader Nodes

_Framewright / Plugins_

> How a shader node turns a manifest and a WGSL function body into a node, what the function receives for each pixel, where Framewright finds plugins, how it reloads them, and how it reports their errors.

A shader node is a node you write yourself as a short GPU program, without
rebuilding Framewright. It lives in a folder that holds two files: a
manifest that names the node and lists its inputs and parameters, and a
function body in the WebGPU Shading Language (WGSL) that works out one pixel.
Framewright's own Exposure, Saturation, Invert, Vignette, and Mix nodes are
shader nodes, built the same way.

If you have written a DCTL for DaVinci Resolve or a BlinkScript kernel for
Nuke, a shader node will be familiar: a small function, run for every pixel
on the GPU, whose parameters appear as controls.

## How a shader node runs

Framewright runs the function once for every pixel of every frame. The
function sees one pixel of each input, all at the same position, and returns
the pixel the node outputs. It can't read neighboring pixels, so a shader
node can change color and combine images but can't blur or distort them.

Framewright joins neighboring per-pixel steps into one GPU pass. A chain of
shader nodes, together with the color conversions around it, reads and
writes each pixel once, so a shader node costs about as much as the
arithmetic it does.

The manifest and the shader's text are part of the key of every cached frame
the node contributes to. Editing either file changes those keys, so
Framewright never shows a frame made with an older version of the shader.

## What the function sees

The shader file holds the inside of a function. Before its first line,
Framewright declares names for the pixel, the inputs, and the parameters
that the [manifest](/framewright/plugins/shader-node-manifest.md) lists:

| Name | WGSL type | Holds |
|---|---|---|
| `c` | `vec4<f32>` | The pixel from the first input |
| Each other input's id | `vec4<f32>` | The pixel from that input |
| Each parameter's id | Set by the parameter's type | The parameter's value at this frame |
| `pos` | `vec2<f32>` | The pixel's center, in canvas units |
| `frame` | `vec4<f32>` | The image's edges: left, top, right, bottom |

The function returns a `vec4<f32>`: the pixel the node outputs. To get a
position that runs from 0 to 1 across the image, divide `pos - frame.xy` by
`frame.zw - frame.xy`, as the built-in Vignette does.

Each parameter type arrives in the shader as a WGSL type:

| Type in the manifest | WGSL type | Value |
|---|---|---|
| `float` | `f32` | The number |
| `int` | `i32` | The whole number |
| `bool` | `bool` | `true` or `false` |
| `choice` | `i32` | The position of the option picked, counting from 0 |
| `vec2` | `vec2<f32>` | The two numbers |
| `color` | `vec4<f32>` | Red, green, blue, and alpha, in the working color space |

## Color and alpha

Every input pixel arrives in Framewright's working color space, ACEScg, from
the Academy Color Encoding System (ACES). ACEScg holds scene-linear light:
doubling a value doubles the light, and values above 1.0 are ordinary
highlights rather than errors. Framewright converts each input to ACEScg
before the node, and converts the node's output on to wherever it goes next.

Pixels are premultiplied: their red, green, and blue are already multiplied
by their alpha. Scaling red, green, and blue together works directly on a
premultiplied pixel and leaves its edges clean. A curve, such as a gamma,
needs the unpremultiplied color, so divide by alpha first and multiply by
it again after.

A color parameter is the exception. It arrives unpremultiplied, converted to
the working color space, with its alpha in the fourth component.

## Where Framewright finds plugins

A plugin folder is any folder that holds a `plugin.toml`. Framewright reads
plugins in this order:

1. Its built-in shader nodes.
2. Each plugin folder in `~/.local/share/framewright/plugins`. If the
   `XDG_DATA_HOME` environment variable is set, Framewright uses
   `$XDG_DATA_HOME/framewright/plugins` instead.
3. Each plugin folder in each folder that `FW_PLUGIN_PATH` lists, separated
   by colons. A folder in the list can also be a plugin folder itself.

Every node type needs its own id. If a plugin uses an id that Framewright
has already loaded, Framewright skips the plugin and reports it, so a plugin
can't replace a built-in node.

## Changes while Framewright runs

Framewright watches its plugin folders. When you save a file in one, it
reads every plugin again within about a second and renders the viewer
again. Frames that don't involve a changed plugin stay in the cache.

## Errors

Framewright reports plugin problems in the bar at the bottom of the window,
and on the standard error stream when it was started from a terminal:

- **A manifest that can't be read.** Framewright skips the plugin and
  reports what is wrong with the manifest.
- **A shader with an error.** Framewright still loads the node, so projects
  that use it keep it. The report gives the line and column in the shader
  file, and rendering the node shows the same error in the viewer. Saving a
  fixed shader clears the report.
- **A plugin that isn't installed.** A project that uses its node keeps the
  node and its settings, saves them unchanged, and reports the node as
  unavailable when rendering. After you install the plugin, open the project
  again to use the node.

## What a shader node can't do

A shader node:

- Reads only the pixel at its own position in each input
- Has one image output, and needs at least one image input
- Holds its whole program in the function body, without functions of its
  own

---

# Writing a Shader Node

_Framewright / Plugins_

> Write a Tint node as a shader plugin, from an empty folder to a node in the node library, then change its shader while Framewright runs and read the error Framewright reports for a broken one.

This page builds a Tint node as a shader plugin: a node that multiplies the
image by a color, with a control for how strongly. When you finish, Tint is
in Framewright's node library, and editing a text file changes what it does.
[Shader Nodes](/framewright/plugins/shader-nodes.md) explains what a shader node can
do and what its function receives.

## Before you start

You need:

- Framewright, with a graph or a clip to try the node on
- A text editor

Knowing some WGSL helps, but this page doesn't depend on it.

## Create the plugin

1. Create a folder for the plugin:

   ```sh
   mkdir -p ~/.local/share/framewright/plugins/tint
   ```

2. In that folder, create `plugin.toml` with this content:

   ```toml
   id = "example.tint"
   version = 1
   name = "Tint"
   category = "Filter"

   [[params]]
   id = "tint"
   name = "Tint"
   type = "color"
   default = [1.0, 0.6, 0.3]

   [[params]]
   id = "amount"
   name = "Amount"
   type = "float"
   unit = "percent"
   default = 50
   range = [0, 100]
   limits = [0, 100]
   ```

   The manifest names the node, puts it in the library's **Filter** group,
   and gives it two parameters. `range` sets what the slider covers, and
   `limits` sets the values Framewright accepts. The
   [manifest reference](/framewright/plugins/shader-node-manifest.md) lists every key. Every node type needs its
   own `id`, so start yours with a prefix of your own and a dot; the
   built-in nodes use `fw.`.

3. In the same folder, create `shader.wgsl` with this content:

   ```wgsl
   // Multiply the image by the tint color, then blend by the amount.
   let tinted = c.rgb * tint.rgb;
   return vec4<f32>(mix(c.rgb, tinted, amount / 100.0), c.a);
   ```

   `c` is the pixel from the node's input. Each parameter is available by
   its `id`: `tint` is a `vec4<f32>` and `amount` is an `f32`.

If Framewright is running, it loads the plugin within about a second of the
save. Otherwise it loads the plugin when it starts.

## Add the node

1. Open the graph you want to tint on the **Compose** page. For a clip on
   the timeline, select the clip and press <kbd>Enter</kbd>.
2. Move the pointer over an empty part of the node editor and press
   <kbd>Tab</kbd>.
3. Enter `tint` and press <kbd>Enter</kbd>. Tint appears where the
   library opened.
4. Drag from the output port of the node that feeds **Output** to the input
   port of **Tint**.
5. Drag from the output port of **Tint** to the input port of **Output**.
   The connection replaces the one that was there.

## Check the result

The viewer shows the image with a warm tint. Select **Tint** in the node
editor: the inspector shows an orange swatch for **Tint** and a slider for
**Amount** at `50.0%`. Drag **Amount** to `100.0%` and the image takes on
the full tint.

## Change the shader

Framewright reads a plugin again whenever one of its files changes, so you
can work on the shader with the viewer open:

1. In `shader.wgsl`, change `tint.rgb` to `tint.bgr`.
2. Save the file.

Within about a second, the viewer shows the image with a cool tint instead,
because the shader swaps the tint's red and blue.

## Read an error

When the shader has an error, Framewright still loads the node and reports
where the error is. To see one, change the second line to multiply by
`tint` rather than `tint.rgb`:

```wgsl
let tinted = c.rgb * tint;
```

When you save, the bar at the bottom of the window shows the error, and the
viewer shows it over the image:

```text
/home/<user-name>/.local/share/framewright/plugins/tint: its shader doesn't work: shader.wgsl:2:14: Operation Multiply can't work with vec3<f32> and vec4<f32>
```

Here *user-name* is your user name. `shader.wgsl:2:14` is the line and
column where the error starts. `c.rgb` has three components and `tint` has
four, so WGSL can't multiply them. Change `tint` back to `tint.rgb` and
save: the error clears and the tint returns.

---

# Shader Node Manifest

_Framewright / Plugins_

> Every key a shader plugin's plugin.toml can hold, covering the node's identity and shader file, its image inputs, each parameter type with its default, units, slider range and limits, and the names ids can't use.

A shader plugin's `plugin.toml` names the node, points at its shader, and
lists its image inputs and parameters. Framewright reads it when it loads
the plugin and refuses a manifest with a missing key, an unknown key, or a
value of the wrong type, reporting which one.

## The node

The top level of the manifest describes the node:

| Key | Type | Required | Meaning |
|---|---|---|---|
| `id` | String | Yes | The node type's id, which projects save. It must contain a dot, as in `example.tint`. |
| `version` | Whole number | Yes | Starts at 1. |
| `name` | String | Yes | The name in the node library and on the node. |
| `category` | String | Yes | The library group the node appears in, such as `Filter`. |
| `shader` | String | No | The shader file, relative to the plugin folder. The default is `shader.wgsl`. |
| `varies_with_time` | Boolean | No | Whether the output changes over time even with constant parameters. The default is `false`. |
| `inputs` | Array of tables | No | The node's image inputs, written as `[[inputs]]` tables. |
| `params` | Array of tables | No | The node's parameters, written as `[[params]]` tables. |

Every node has one image output.

Keep `version` at 1. Framewright can't open a project saved with an older
version of a plugin node, because a shader plugin has no way to convert the
parameters such a project saved. A project saved with a newer version than
the one installed keeps the node unchanged, and reports it as unavailable
when rendering.

When `varies_with_time` is `true`, Framewright renders the node again at
every frame instead of reusing one result while its inputs and parameters
stay the same. The shader receives no time value, so set the key only when
something the shader reads varies over time in a way Framewright can't see.

## Inputs

Each `[[inputs]]` table adds an image input, in order:

| Key | Type | Meaning |
|---|---|---|
| `id` | String | The name the shader reads the input by, and the name projects save its connection under |
| `name` | String | The label on the node's port |

A node needs at least one input. Without an `[[inputs]]` table, it has one
called `image`, labeled `Image`. The shader reads the first input as `c`,
whatever its `id`, and every other input by its `id`.

## Parameters

Each `[[params]]` table adds a parameter. The inspector shows parameters in
the order the manifest lists them:

| Key | Type | Required | Meaning |
|---|---|---|---|
| `id` | String | Yes | The name the shader reads the value by, and the name projects save it under |
| `name` | String | Yes | The label in the inspector |
| `type` | String | Yes | One of the parameter types in the table of types |
| `default` | Depends on `type` | No | The value of a new node |
| `unit` | String | No | How the value is shown, for `float` and `vec2` |
| `range` | Two numbers | No | The span the slider of a `float` covers |
| `limits` | Two numbers | No | The values Framewright accepts for a `float`, `vec2`, or `color`, checked on each component |
| `section` | String | No | The inspector section the parameter appears in. Without one, it's in the section named after the node. |
| `animatable` | Boolean | No | Whether the value can change over time with keyframes |
| `options` | Array of tables | For `choice` | The choices, each with an `id` and a `name` |

A value outside `range` is still allowed, as long as it is within `limits`.
A `default` outside `limits` makes the manifest invalid. Parameters are
animatable by default, except choices.

### Parameter types

Each type takes its default in its own form:

| Type | `default` | Without a default | In the shader |
|---|---|---|---|
| `float` | A number | `0` | `f32` |
| `int` | A whole number | `0` | `i32` |
| `bool` | `true` or `false` | `false` | `bool` |
| `choice` | The `id` of one option | The first option | `i32`, the position of the option picked, counting from 0 |
| `vec2` | Two numbers, `[x, y]` | `[0, 0]` | `vec2<f32>` |
| `color` | Three or four numbers, `[r, g, b]` or `[r, g, b, a]`, in linear Rec.709 | `[0, 0, 0, 1]` | `vec4<f32>`, unpremultiplied, in the working color space |

A `choice` parameter declares its options like this:

```toml
[[params]]
id = "mode"
name = "Mode"
type = "choice"
default = "screen"
options = [
  { id = "multiply", name = "Multiply" },
  { id = "screen", name = "Screen" },
]
```

The shader reads `mode` as `0` for Multiply and `1` for Screen. Projects
save the option's `id`, so you can reorder or add options without changing
what saved projects mean.

### Units

The `unit` key takes one of these values:

| Value | Shown as |
|---|---|
| `none` | `0.500`, a plain number (the default) |
| `pixels` | `12.0 px` |
| `degrees` | `45.0°` |
| `seconds` | `1.500 s` |
| `decibels` | `-6.0 dB` |
| `stops` | `1.00 EV`, stops of exposure |
| `ratio` | `1.500×` |
| `percent` | `50.0%` |

## Ids

Input and parameter ids become names in the shader, so they follow these
rules:

- They use lowercase letters, digits, and underscores, and start with a
  letter.
- They don't repeat each other: no parameter shares an id with an input or
  with another parameter.
- They avoid the names Framewright declares around the shader: `c`, `pos`,
  `frame`, `base`, `prm`, `params`, `src`, `dst`, and `main`, any name made
  of `e` or `ex` followed by digits, and any name starting with `fw_` or
  `op_`.
- They avoid WGSL keywords and type names, such as `let`, `fn`, `f32`, and
  `vec4`.
