gi-gsk-4.0.4: Gsk bindings
CopyrightWill Thompson and Iñaki García Etxebarria
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellSafe-Inferred
LanguageHaskell2010

GI.Gsk.Objects.GLShader

Description

A GskGLShader is a snippet of GLSL that is meant to run in the fragment shader of the rendering pipeline.

A fragment shader gets the coordinates being rendered as input and produces the pixel values for that particular pixel. Additionally, the shader can declare a set of other input arguments, called uniforms (as they are uniform over all the calls to your shader in each instance of use). A shader can also receive up to 4 textures that it can use as input when producing the pixel data.

GskGLShader is usually used with gtk_snapshot_push_gl_shader() to produce a GLShaderNode in the rendering hierarchy, and then its input textures are constructed by rendering the child nodes to textures before rendering the shader node itself. (You can pass texture nodes as children if you want to directly use a texture as input).

The actual shader code is GLSL code that gets combined with some other code into the fragment shader. Since the exact capabilities of the GPU driver differs between different OpenGL drivers and hardware, GTK adds some defines that you can use to ensure your GLSL code runs on as many drivers as it can.

If the OpenGL driver is GLES, then the shader language version is set to 100, and GSK_GLES will be defined in the shader.

Otherwise, if the OpenGL driver does not support the 3.2 core profile, then the shader will run with language version 110 for GL2 and 130 for GL3, and GSK_LEGACY will be defined in the shader.

If the OpenGL driver supports the 3.2 code profile, it will be used, the shader language version is set to 150, and GSK_GL3 will be defined in the shader.

The main function the shader must implement is:

glsl code

void mainImage(out vec4 fragColor,
               in vec2 fragCoord,
               in vec2 resolution,
               in vec2 uv)

Where the input fragCoord is the coordinate of the pixel we're currently rendering, relative to the boundary rectangle that was specified in the GskGLShaderNode, and resolution is the width and height of that rectangle. This is in the typical GTK coordinate system with the origin in the top left. uv contains the u and v coordinates that can be used to index a texture at the corresponding point. These coordinates are in the [0..1]x[0..1] region, with 0, 0 being in the lower left corder (which is typical for OpenGL).

The output fragColor should be a RGBA color (with premultiplied alpha) that will be used as the output for the specified pixel location. Note that this output will be automatically clipped to the clip region of the glshader node.

In addition to the function arguments the shader can define up to 4 uniforms for textures which must be called u_textureN (i.e. u_texture1 to u_texture4) as well as any custom uniforms you want of types int, uint, bool, float, vec2, vec3 or vec4.

All textures sources contain premultiplied alpha colors, but if some there are outer sources of colors there is a gsk_premultiply() helper to compute premultiplication when needed.

Note that GTK parses the uniform declarations, so each uniform has to be on a line by itself with no other code, like so:

glsl code

uniform float u_time;
uniform vec3 u_color;
uniform sampler2D u_texture1;
uniform sampler2D u_texture2;

GTK uses the the "gsk" namespace in the symbols it uses in the shader, so your code should not use any symbols with the prefix gsk or GSK. There are some helper functions declared that you can use:

glsl code

vec4 GskTexture(sampler2D sampler, vec2 texCoords);

This samples a texture (e.g. u_texture1) at the specified coordinates, and containes some helper ifdefs to ensure that it works on all OpenGL versions.

You can compile the shader yourself using gLShaderCompile, otherwise the GSK renderer will do it when it handling the glshader node. If errors occurs, the returned error will include the glsl sources, so you can see what GSK was passing to the compiler. You can also set GSK_DEBUG=shaders in the environment to see the sources and other relevant information about all shaders that GSK is handling.

An example shader

glsl code

uniform float position;
uniform sampler2D u_texture1;
uniform sampler2D u_texture2;

void mainImage(out vec4 fragColor,
               in vec2 fragCoord,
               in vec2 resolution,
               in vec2 uv) {
  vec4 source1 = GskTexture(u_texture1, uv);
  vec4 source2 = GskTexture(u_texture2, uv);

  fragColor = position * source1 + (1.0 - position) * source2;
}
Synopsis

Exported types

newtype GLShader Source #

Memory-managed wrapper type.

Constructors

GLShader (ManagedPtr GLShader) 

Instances

Instances details
Eq GLShader Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

GObject GLShader Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

ManagedPtrNewtype GLShader Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

TypedObject GLShader Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

Methods

glibType :: IO GType #

HasParentTypes GLShader Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

IsGValue (Maybe GLShader) Source #

Convert GLShader to and from GValue. See toGValue and fromGValue.

Instance details

Defined in GI.Gsk.Objects.GLShader

type ParentTypes GLShader Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

class (GObject o, IsDescendantOf GLShader o) => IsGLShader o Source #

Type class for types which can be safely cast to GLShader, for instance with toGLShader.

Instances

Instances details
(GObject o, IsDescendantOf GLShader o) => IsGLShader o Source # 
Instance details

Defined in GI.Gsk.Objects.GLShader

toGLShader :: (MonadIO m, IsGLShader o) => o -> m GLShader Source #

Cast to GLShader, for types for which this is known to be safe. For general casts, use castTo.

Methods

compile

gLShaderCompile Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a, IsRenderer b) 
=> a

shader: a GskGLShader

-> b

renderer: a GskRenderer

-> m ()

(Can throw GError)

Tries to compile the shader for the given renderer.

If there is a problem, this function returns False and reports an error. You should use this function before relying on the shader for rendering and use a fallback with a simpler shader or without shaders if it fails.

Note that this will modify the rendering state (for example change the current GL context) and requires the renderer to be set up. This means that the widget has to be realized. Commonly you want to call this from the realize signal of a widget, or during widget snapshot.

findUniformByName

gLShaderFindUniformByName Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Text

name: uniform name

-> m Int32

Returns: The index of the uniform, or -1

Looks for a uniform by the name name, and returns the index of the uniform, or -1 if it was not found.

getArgBool

gLShaderGetArgBool Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> m Bool

Returns: The value

Gets the value of the uniform idx in the args block.

The uniform must be of bool type.

getArgFloat

gLShaderGetArgFloat Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> m Float

Returns: The value

Gets the value of the uniform idx in the args block.

The uniform must be of float type.

getArgInt

gLShaderGetArgInt Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> m Int32

Returns: The value

Gets the value of the uniform idx in the args block.

The uniform must be of int type.

getArgUint

gLShaderGetArgUint Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> m Word32

Returns: The value

Gets the value of the uniform idx in the args block.

The uniform must be of uint type.

getArgVec2

gLShaderGetArgVec2 Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> Vec2

outValue: location to store the uniform value in

-> m () 

Gets the value of the uniform idx in the args block.

The uniform must be of vec2 type.

getArgVec3

gLShaderGetArgVec3 Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> Vec3

outValue: location to store the uniform value in

-> m () 

Gets the value of the uniform idx in the args block.

The uniform must be of vec3 type.

getArgVec4

gLShaderGetArgVec4 Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Bytes

args: uniform arguments

-> Int32

idx: index of the uniform

-> Vec4

outValue: location to store set the uniform value in

-> m () 

Gets the value of the uniform idx in the args block.

The uniform must be of vec4 type.

getArgsSize

gLShaderGetArgsSize Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> m Word64

Returns: The size of the data block

Get the size of the data block used to specify arguments for this shader.

getNTextures

gLShaderGetNTextures Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> m Int32

Returns: The number of texture inputs required by shader

Returns the number of textures that the shader requires.

This can be used to check that the a passed shader works in your usecase. It is determined by looking at the highest u_textureN value that the shader defines.

getNUniforms

gLShaderGetNUniforms Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> m Int32

Returns: The number of declared uniforms

Get the number of declared uniforms for this shader.

getResource

gLShaderGetResource Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> m (Maybe Text)

Returns: The resource path for the shader

Gets the resource path for the GLSL sourcecode being used to render this shader.

getSource

gLShaderGetSource Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> m Bytes

Returns: The source code for the shader

Gets the GLSL sourcecode being used to render this shader.

getUniformName

gLShaderGetUniformName Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Int32

idx: index of the uniform

-> m Text

Returns: The name of the declared uniform

Get the name of the declared uniform for this shader at index idx.

getUniformOffset

gLShaderGetUniformOffset Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Int32

idx: index of the uniform

-> m Int32

Returns: The data offset

Get the offset into the data block where data for this uniforms is stored.

getUniformType

gLShaderGetUniformType Source #

Arguments

:: (HasCallStack, MonadIO m, IsGLShader a) 
=> a

shader: a GskGLShader

-> Int32

idx: index of the uniform

-> m GLUniformType

Returns: The type of the declared uniform

Get the type of the declared uniform for this shader at index idx.

newFromBytes

gLShaderNewFromBytes Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> Bytes

sourcecode: GLSL sourcecode for the shader, as a GBytes

-> m GLShader

Returns: A new GskGLShader

Creates a GskGLShader that will render pixels using the specified code.

newFromResource

gLShaderNewFromResource Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> Text

resourcePath: path to a resource that contains the GLSL sourcecode for the shader

-> m GLShader

Returns: A new GskGLShader

Creates a GskGLShader that will render pixels using the specified code.

Properties

resource

Resource containing the source code for the shader.

If the shader source is not coming from a resource, this will be Nothing.

constructGLShaderResource :: (IsGLShader o, MonadIO m) => Text -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “resource” property. This is rarely needed directly, but it is used by new.

getGLShaderResource :: (MonadIO m, IsGLShader o) => o -> m (Maybe Text) Source #

Get the value of the “resource” property. When overloading is enabled, this is equivalent to

get gLShader #resource

source

No description available in the introspection data.

constructGLShaderSource :: (IsGLShader o, MonadIO m) => Bytes -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “source” property. This is rarely needed directly, but it is used by new.

getGLShaderSource :: (MonadIO m, IsGLShader o) => o -> m Bytes Source #

Get the value of the “source” property. When overloading is enabled, this is equivalent to

get gLShader #source