Skip to content

Source Filter Transition Shader

Xaymar edited this page Nov 7, 2021 · 38 revisions

Shader Source/Filter/Transition Windows Linux MacOS Experimental

StreamFX ships with three different ways to use custom shaders in OBS Studio: Source Shader, Filter Shader and Transition Shader. Each has slightly different usage, but can mostly work with almost the same shader and allows you to easily use your own or someone elses shaders in OBS Studio. Shaders allow for quick iteration and enable some effects that were previously impossible.

Version Information
Status Version
Added 0.5
Unstable N/A
Stable N/A
Deprecated N/A
Removed N/A

Guides

Examples

(note: links are currently dead)

Settings

TODO

Writing Shaders

Syntax

The [] denote optional things, while the () denote required things. They are only used to illustrate the structure itself, and must be removed.

Generic Parameters

uniform (type) (name)<
  // 'true' to allow Users to see this parameters, 'false' to hide it. Enables invisible parameters which do special things with scripts.
  [bool visible = true;]
  // 'true' to completely disallow modification from outside. Useful for predefined parameters and values that are spread from other parameters.
  [bool automatic = false;]
  // Name used in the UI.
  [string name = "Name";]
  // Description used in the UI.
  [string description = "Description";]
  // DANGEROUS: Override the detected type with a specific one. Do not use unless you absolutely need it.
  [string type = "bool";] // Turn this into a boolean.
  [string type = "float";] // Turn this into a float.
  [string type = "int";] // Turn this into an integer.
  [string type = "string";] // Turn this into a string.
  [string type = "texture";] // Turn this into a texture.
  [string type = "sampler";] // Turn this into a sampler.
  // DANGEROUS: Override the detected size with a specified one. Do not use unless you absolutely need it.
  [int size = 1;]
>[ = (value)];

Value Parameters

A value parameter is one of the following types: float, float#, float[], float#x#, int, int#, int[], int#x#, uint, uint#, uint[], uint#x#, bool, bool#, bool[], bool#x#. Some types may require type and size overrides in order to be detected correctly.

uniform (type) (name)<
  // ... continued from Generic Parameters
  // Changes the field type to a different one.
  [string field_type = "input";]       // Standard input widget with manual entry and nothing fancy.
  [string field_type = "slider";]      // Horizontal slider with manual entry option.
  [string field_type = "enum";]        // Dropdown with enumeration entries, see "Enumeration Parameters".
  // Suffix for the values.
  [string suffix = " my awesome suffix";]
  // Minimum value that can be set.
  [(type) minimum = {...};] // Defaults to the smallest possible number.
  // Maximum value that can be set.
  [(type) maximum = {...};] // Defaults to the largest possible number.
  // The step of the value.
  [(type) step = {...};] // Defaults to 1.
  // The scale of the value, which is applied to everything. Can be used to greatly increase the precision of an property.
  [(type) scale = {...};] // Defaults to 1.
>[ = (value)];

Enumeration Parameters

A subset of value parameters where the selection is limited to a defined list of values, which can be defined in the shader. This can be useful to have users select from certain behavior instead of requiring them to enter numbers manually.

Entries in the list are limited to int, float and string, the three basic types of parameters. Vector types will simply act as an array of the basic type, and show a dropdown for each vector part. A float4 for example would allow the user to pick a value for r, g, b and a (or x, y, z and w).

uniform (type) (name)<
  // ... continued from Generic Parameters
  // Set the field type to enumeration.
  [string field_type = "enum";]        // Dropdown with enumeration entries.
  // Defines the #. enumeration entry of the list. # must be a continuously increasing number starting from 0.
  [(base type) enum_# = (value);] // The value of the enumeration entry.
  [string enum_#_name = "(name)";] // The name of the enumeration entry, optional. If not specified, defaults to "Unnamed Entry".
>[ = (value)];

Predefined Parameters

float4x4 ViewProj

Camera Projection Matrix, must be used in the Vertex Shader.

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

float4 Time

The current time since the creation of the shader, in seconds.

  • x: Time in seconds since the source was created. (float)
  • y: Time in the current second.
  • z: Time in seconds since the source was created. (rounded down to nearest integer)
  • w: Reserved

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

float4 ViewSize

  • x: Width
  • y: Height
  • z: 1. / Width
  • w: 1. / Height

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

texture2d InputA

Available only to Filters and Transitions, holds a 2D Texture of size ViewSize.xy with the content that is to be filtered or transitioned from.

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

texture2d InputB

Available only to Transitions, holds a 2D Texture of size ViewSize.xy with the content that is to be transitioned to.

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

float TransitionTime

Value from 0 to 1 that describes the current expected progress of the transition. The transition must have completed by the time this value reaches 1 or the transition is cut off.

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

int32 RandomSeed

The seed value used for randomization, user controlled. Changing this automatically regenerates all random values.

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

float4x4 Random

A matrix containing random values that are generated per-instance, per-activation and per-frame. Use it like this:

float4 perInst = float4(Random[0][0], Random[1][0], Random[2][0], Random[3][0]);
float4 perActivation = float4(Random[0][1], Random[1][1], Random[2][1], Random[3][1]);
float4 perFrame1 = float4(Random[0][2], Random[1][2], Random[2][2], Random[3][2]);
float4 perFrame2 = float4(Random[0][3], Random[1][3], Random[2][3], Random[3][3]);

This is an automatically set option, ensure that the user does not see it by setting the annotation 'bool automatic = true;'.

libOBS's Shader Language

The baseline for the shader language libOBS uses is HLSL, for which you can find documentation here. libOBS modifies this so it works across all of its supported graphics APIs, sacrificing functionality and versatility for compatibility. As a result, some effects that would be possible with ease in GLSL or HLSL directly do not work in libOBS's shader language. For the sake of all future documentation, the langauge will be referred to as OBS-SL, since it mixes and matches many different langauge constructs into one.

Data

Similar to HLSL, OBS-SL supports storage, transfer and modification of data.

Types

Name DirectX OpenGL
bool bool bool
float float float
float2 float2 vec2
float3 float3 vec3
float4 float4 vec4
int int int
int2 int2 ivec2
int3 int3 ivec3
int4 int4 ivec4
string string string
float3x3 float3x3 mat3x3
float3x4 float3x4 mat3x4
float4x4 float4x4 mat4x4
texture2d Texture2D sampler2D
texture3d Texture3D sampler3D
texture_cube TextureCube samplerCube
texture_rect N/A sampler2DRect
sampler_state SamplerState Metadata only

Arrays

At the time of writing this, Arrays are only supported when DirectX is being used by libOBS. The patch to make it available in OpenGL was retracted at the request of the current OBS Project team.

Storage Specifiers

Storage specifiers seem to only be used by the OpenGL backend.

Name DirectX OpenGL Notes
uniform uniform
const const
inout inout
out out

Keywords

A limited subset of keywords are supported, other keywords may work if they match across both langauges.

Name DirectX OpenGL
POSITION SV_Position gl_FragCoord
TARGET SV_Target N/A
VERTEXID SV_VertexID uint(gl_VertexID)

Vertex/Fragment Input/Output Semantics

A limited subset of semantics are supported, other keywords will not work.

Name DirectX OpenGL Notes
POSITION SV_Position Metadata only Provided as float32[4]
NORMAL NORMAL Metadata only Provided as float32[4]
COLOR COLOR Metadata only Provided as uint8_unorm[4]
TANGENT TANGENT Metadata only Provided as float32[4]
TEXCOORD TEXCOORD Metadata only Either float32, float32[2] or float32[4].
VERTEXID VERTEXID Metadata only
Clone this wiki locally