C

Effect Reference

This page explains how to write effects.

Overview

The effects specification allows writing custom effects, which are applied to layers. The effects can have multiple passes to calculate the final result. Each shader in a pass must implement the void frag() and void vert() functions for fragment and vertex shader respectively. The passes are executed in the order they are specified in the effect file. The studio shader library can also be accessed with #include directive to add functionality.

Effect Structure

<Effect>
    <MetaData>
        <Property name="FloatParam" type="Float" formalName="Float Param" description="" default="1.0"/>
        <Property name="DepthTextureParam" type="Texture" filter="nearest" clamp="clamp"/>
        <Property name="TextureParam" type="Texture" filter="linear" clamp="clamp"/>
        <Property name="Image2DParam" type="Image2D" format="rgba32f" binding="1"/>
        <Property name="BufferParam" type="Buffer" format="uvec4" usage="storage" binding="1" align="std140"/>
    </MetaData>
    <Shaders>
        <Shared></Shared>
        <VertexShaderShared></VertexShaderShared>
        <FragmentShaderShared></FragmentShaderShared>
        <Shader name="INPUTS">
            <VertexShader></VertexShader>
            <FragmentShader></FragmentShader>
        </Shader>
        <Shader name="INDIRECT">
            <VertexShader></VertexShader>
            <FragmentShader></FragmentShader>
        </Shader>
        <Shader name="MAIN">

            <VertexShader>
void vert()
{
}
            </VertexShader>
            <FragmentShader>
void frag()
{
}
            </FragmentShader>
        </Shader>
    </Shaders>
    <Passes>
        <Buffer name="buffer" type="fp16" format="rg" filter="nearest" wrap="clamp" size="1.0" lifetime="frame"/>
        <Buffer name="buffer2" type="fp16" format="rg" filter="nearest" wrap="clamp" size="1.0" lifetime="frame"/>
        <Image name="image" type="fp16" format="rgba" filter="nearest" wrap="clamp" size="1.0" lifetime="scene" access="readwrite"/>
        <DataBuffer name="drawIndirect" type="indirect" wrapName="BufferParam" wrapType="storage" format="uint" size="4" lifetime="frame"/>
        <Buffer name="stencilBuffer" format='depth24stencil8' lifetime="frame" />

        <Pass shader="INPUTS" input="[source]" output="buffer">
            <DepthInput param="DepthTextureParam"/>
            <ImageInput value="image" param="Image2DParam" usage="image"/>
            <DataBufferInput param="BufferParam" usage="storage"/>
            <DepthStencil buffer='stencilBuffer' flags='clear-stencil' stencil-fail='replace' depth-fail='replace' depth-pass='replace' stencil-function='always' reference='1'/>
        </Pass>

        <Pass shader="INDIRECT" input="buffer" output="buffer2">
            <BufferInput param="buffer" param="TextureParam" />
            <DataBufferInput param="drawIndirect" usage="indirect"/>
        </Pass>
        <Pass shader="MAIN" input="[source]" output="[dest]">
            <Blending source="SrcAlpha" dest="One"/>
            <SetParam name="FloatParam" value="0.0"/>
        </Pass>
    </Passes>
</Effect>

The effect file structure follows the same pattern as custom materials, first is the metadata with parameters in <Metadata> element, then effect shaders in <Shaders> element and render passes in <Passes> element.

Parameters

Effect metadata parameters are the same as for custom materials, but also have a couple of effect-specific parameters. Parameters with Image2D type and <Image> element in allow using image-variables in shader and parameters with Buffer type and <DataBuffer> element allow using shader storage buffer objects in shader. They also enable indirect drawing in render pass using the <DataBufferInput> element. Parameters with Texture type and <Buffer> element allow using sampler-variables in shader.

Image2D

<Property name="Image2DParam" type="Image2D" format="rgba32f" binding="1"/>

Image2D type is used to declare image property. Its value can not be set outside the effect, but instead using the <ImageInput> element. format-attribute specifies the format of the image. See GLSL Specification for accepted values. binding-attribute specifies the binding point of the image.

Buffer

<Property name="BufferParam" type="Buffer" format="uvec4" usage="storage" binding="1" align="std140"/>

Buffer type is used to declare buffer property and allows using shader storage buffer objects in shader. Its value can not be set outside the effect, but instead using the <DataBufferInput> element. format-attribute specifies the format of the buffer. See GLSL Specification for accepted values. usage-attribute specifies the usage of the buffer. For now, only storage is supported. binding-attribute specifies the binding point of the image. align-attribute specifies the buffer alignment. See GLSL Specification for accepted values. The shader generator appends _data[] to the name, which must be taken into account when using it in shader.

Note: Buffer type properties are only used with SSBO's together with <DataBuffer> element specifying the data buffer. Use Texture type properties together with <Buffer> element to specify texture buffers used with samplers.

Elements

<Shared>

This element can be used to insert block of shader code that is shared among all shaders. It gets inserted to all shaders in the effect.

<VertexShaderShared> and <FragmentShaderShared>

These elements can be used to insert block of shader code that is shared among all vertex and fragments shaders. The code inside <VertexShaderShared> element gets inserted to all vertex shaders and similarly code inside <FragmentShaderShared> element gets inserted to all fragment shaders.

<Shader>, <VertexShader> and <FragmentShader>

The <Shader> element specifies a shader with shader name specified with name attribute. The name is used inside <Pass> element to specify which shader to execute. <VertexShader> and <FragmentShader> elements are used to specify vertex shader code and fragment shader code respectively. The vertex shader must implement the void vert() function and fragment shader must implement the void frag() function, unless left empty.

The shader can use the varying, in and out specifiers to define custom variables to pass parameters between shader stages.

<Passes>

The <Passes> element is used to specify render passes for the effect. Each render pass must be specified inside <Pass> element. The <Passes> element can also contain <Buffer>, <Image> and <Databuffer> elements to specify different kinds of data storages for the effect.

<Buffer>

This element can be used to create a buffer to be used inside the effect. Different passes can read and write to these buffers while implementing different stages of the effect. The <BufferInput> element can be used to bind the buffer to shader variable so that it can be read in shader.

The element attributes are:

attributevaluesdescription
namestringUsed to specify unique name for the buffer.
type
  • ubyte - 8-bit unsigned integer
  • ushort - 16-bit unsigned integer
  • fp16 - 16-bit floating point number
  • fp32 - 32-bit floating point number
Used to specify the type of the buffer.
format
  • source - take format from the source
  • depth24stencil8 - combined depth24 stencil 8
  • alpha - alpha
  • lum - luminance
  • lum_alpha - luminance alpha
  • rg - 2-component RG
  • rgb - 3-component RGB
  • rgba - 4-component RGBA
Used to specify the format of the buffer.
filter
  • nearest - nearest neighbor
  • linear - linear interpolation
Used to specify the input filtering mode.
wrap
  • clamp - clamp to edges
  • repeat - repeat values
Used to specify the wrapping mode of the texture coordinates.
sizefloatUsed to specify the size of the buffer relative to the frame buffer. 1.0 corresponds to same size. 0.5 corresponds to the buffer width and height being half the size.
lifetime
  • scene - The buffer is allocated at the beginning of the scene
  • frame - The buffer is allocated at the beginning of the frame
Used to specify how the buffer is allocated and deleted. Buffers with frame lifetime can be reused by other effects.

<Image>

This element can be used to create a image to be used inside the effect. The <ImageInput> element can be used to bind the image to shader variable so that it can be accessed in shader.

The element attributes are:

attributevaluesdescription
namestringUsed to specify unique name for the image.
type
  • ubyte - 8-bit unsigned integer
  • ushort - 16-bit unsigned integer
  • fp16 - 16-bit floating point number
  • fp32 - 32-bit floating point number
Used to specify the type of the image.
format
  • source - take format from the source
  • depth24stencil8 - combined depth24 stencil 8
  • alpha - alpha
  • lum - luminance
  • lum_alpha - luminance alpha
  • rg - 2-component RG
  • rgb - 3-component RGB
  • rgba - 4-component RGBA
Used to specify the format of the image.
filter
  • nearest - nearest neighbor
  • linear - linear interpolation
Used to specify the input filtering mode.
wrap
  • clamp - clamp to edges
  • repeat - repeat values
Used to specify the wrapping mode of the texture coordinates.
sizefloatUsed to specify the size of the image relative to the frame buffer. 1.0 corresponds to same size. 0.5 corresponds to the image width and height being half the size.
lifetime
  • scene - The image is allocated at the beginning of the scene
  • frame - The image is allocated at the beginning of the frame
Used to specify how the image is allocated and deleted. Images with frame lifetime can be reused by other effects.
access
  • read - The image can be read in the shader
  • write - The image can be written in the shader
  • readwrite - The image can be both read and written in the shader
Used to specify how the image is accessed in the shader.

<DataBuffer>

This element can be used to create a data buffer. The <DataBufferInput> element can be used to bind the data buffer to shader variable so that it can be accessed in shader.

The element attributes are:

attributevaluesdescription
namestringUsed to specify unique name for the data buffer.
type
  • storage - storage buffer
  • indirect - indirect draw buffer
Used to specify the type of the data buffer.
format
  • uint - 32-bit unsigned integer
  • int - 32-bit signed integer
  • uvec4 - 4-component 32-bit unsigned integer vector
Used to specify the format of the data buffer.
sizeunsigned integerUsed to specify the size of the data buffer. Specifies the number of format sized elements are in the data buffer.
lifetime
  • scene - The data buffer is allocated at the beginning of the scene
  • frame - The data buffer is allocated at the beginning of the frame
Used to specify how the data buffer is allocated and deleted.
wrapNamestringThe name of the property wrapping this data buffer. The name must match to one property with type Buffer in the meta data.
wrapType
  • storage - storage type
  • indirect - indirect draw type
The type of the wrapping.

<Pass>

This element specifies one render pass in effect. The passes are executed in the order they are specified in the effect file.

The element attributes are:

attributevaluesdescription
shaderstringThe name of the shader executed in this pass. Must match to a shader name in the <Shaders> element.
inputstringThe name of one of the <Buffer> elements or [source].
outputstringThe name of one of the <Buffer> elements or [dest].
format
  • alpha - alpha
  • lum - luminance
  • lum_alpha - luminance alpha
  • rg - 2-component RG
  • rgb - 3-component RGB
  • rgba - 4-component RGBA
If output is [dest], specifies the output format.

<BufferInput>

This element binds buffer to one of the shader sampler variables.

The element attributes are:

attributevaluesdescription
valuestringThe name of one of the <Buffer> elements or [source].
paramstringThe name of the shader variable.

<ImageInput>

This element binds an image to one of the shader image variables.

The element attributes are:

attributevaluesdescription
valuestringThe name of one of the <Image> elements or [source].
paramstringThe name of the shader variable.
usage
  • texture - use as a texture
  • image - use as an image
Specifies how the image is used in shader.
syncbooleanSpecifies if the effect execution should synchronize accesses to the image.

<DepthInput>

This element binds frame buffer depth as a texture to shader sampler variable.

The element attributes are:

attributevaluesdescription
paramstringThe name of the shader variable.

<DataBufferInput>

This element either binds data buffer property to shader storage buffer object or performs indirect draw on the shader pass. The shader generator appends _data[] to the name, which must be taken into account when using it in shader.

The element attributes are:

attributevaluesdescription
paramstringThe name of the data buffer.
usage
  • storage - bind data buffer as a shader variable
  • indirect - perform indirect draw
Specifies the usage type of the element.

<SetParam>

This element can be used to set render pass specific shader parameters. The parameter must match to a parameter specified in the <Metadata> element.

The element attributes are:

attributevaluesdescription
namestringThe name of the property.
valuefloat or integerSpecifies the value of the property.

<Blending>

This element can be used to change the blending of the a render pass.

The element attributes are:

attributevaluesdescription
dest
  • SrcAlpha
  • OneMinusSrcAlpha
  • One
Specifies the destination blending mode.
source
  • SrcAlpha
  • OneMinusSrcAlpha
  • One
Specifies the source blending mode.

<RenderState>

This element can be used to enable/disable a render state for a pass.

The element attributes are:

attributevaluesdescription
name
  • Stencil
Name of the render state
valuebooleanSpecifies whether to enable or disable the render state.

<DepthStencil>

This element can be used to specify depth-stencil buffer and change the depth-stencil test for a render pass.

The element attributes are:

attributevaluesdescription
bufferstringName of the buffer used as a depth-stencil buffer.
flags
  • clear-stencil - Clear stencil buffer
  • clear-depth - Clear depth buffer
Specifies flags for the element. These can be combined with logical or('|').
stencil-failOne of the stencil operation valuesSpecifies stencil fail operation.
depth-failOne of the stencil operation valuesSpecifies depth fail operation.
depth-passOne of the stencil operation valuesSpecifies depth pass operation.
stencil-functionOne of the boolean operator valuesSpecifies stencil function.
referenceintegerSpecifies stencil reference value.
maskintegerSpecifies stencil mask value.

The stencil operation values are:

  • keep
  • zero
  • replace
  • increment
  • increment-wrap
  • decrement
  • decrement-wrap
  • invert

The boolean operator values are:

  • never
  • less
  • less-than-or-equal
  • equal
  • not-equal
  • greater
  • greater-than-or-equal
  • always

Available under certain Qt licenses.
Find out more.