class Gsk::GLShader
- Gsk::GLShader
- GObject::Object
- Reference
- Object
Overview
A Gsk::GLShader
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.
Gsk::GLShader
is usually used with gtk_snapshot_push_gl_shader()
to produce a Gsk::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:
WARNING ⚠️ The following code is in glsl ⚠️
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 Gsk::GLShaderNode
, 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:
WARNING ⚠️ The following code is in glsl ⚠️
uniform float u_time;
uniform vec3 u_color;
uniform sampler2D u_texture1;
uniform sampler2D u_texture2;
GTK uses 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:
WARNING ⚠️ The following code is in glsl ⚠️
vec4 Gsk::Texture(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 Gsk::GLShader#compile
,
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
WARNING ⚠️ The following code is in glsl ⚠️
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 = Gsk::Texture(u_texture1, uv);
vec4 source2 = Gsk::Texture(u_texture2, uv);
fragColor = position * source1 + (1.0 - position) * source2;
}
Defined in:
lib/gi-crystal/src/auto/gsk-4.0/gl_shader.crConstructors
-
.new
Initialize a new
GLShader
. - .new(*, resource : String | Nil = nil, source : GLib::Bytes | Nil = nil)
-
.new_from_bytes(sourcecode : GLib::Bytes) : self
Creates a
Gsk::GLShader
that will render pixels using the specified code. -
.new_from_resource(resource_path : String) : self
Creates a
Gsk::GLShader
that will render pixels using the specified code.
Class Method Summary
-
.g_type : UInt64
Returns the type id (GType) registered in GLib type system.
Instance Method Summary
-
#==(other : self)
Returns
true
if this reference is the same as other. -
#arg_bool(args : GLib::Bytes, idx : Int32) : Bool
Gets the value of the uniform idx in the args block.
-
#arg_float(args : GLib::Bytes, idx : Int32) : Float32
Gets the value of the uniform idx in the args block.
-
#arg_int(args : GLib::Bytes, idx : Int32) : Int32
Gets the value of the uniform idx in the args block.
-
#arg_uint(args : GLib::Bytes, idx : Int32) : UInt32
Gets the value of the uniform idx in the args block.
-
#arg_vec2(args : GLib::Bytes, idx : Int32, out_value : Graphene::Vec2) : Nil
Gets the value of the uniform idx in the args block.
-
#arg_vec3(args : GLib::Bytes, idx : Int32, out_value : Graphene::Vec3) : Nil
Gets the value of the uniform idx in the args block.
-
#arg_vec4(args : GLib::Bytes, idx : Int32, out_value : Graphene::Vec4) : Nil
Gets the value of the uniform idx in the args block.
-
#args_size : UInt64
Get the size of the data block used to specify arguments for this shader.
-
#compile(renderer : Gsk::Renderer) : Bool
Tries to compile the shader for the given renderer.
-
#find_uniform_by_name(name : String) : Int32
Looks for a uniform by the name name, and returns the index of the uniform, or -1 if it was not found.
-
#hash(hasher)
See
Object#hash(hasher)
-
#n_textures : Int32
Returns the number of textures that the shader requires.
-
#n_uniforms : Int32
Get the number of declared uniforms for this shader.
-
#resource : String | Nil
Gets the resource path for the GLSL sourcecode being used to render this shader.
- #resource=(value : String) : String
-
#resource=(value : Nil) : Nil
Set
#resource
property to nil. -
#resource? : String | Nil
Same as
#resource
, but can return nil. -
#source : GLib::Bytes
Gets the GLSL sourcecode being used to render this shader.
- #source=(value : GLib::Bytes | Nil) : GLib::Bytes | Nil
-
#uniform_name(idx : Int32) : String
Get the name of the declared uniform for this shader at index idx.
-
#uniform_offset(idx : Int32) : Int32
Get the offset into the data block where data for this uniforms is stored.
-
#uniform_type(idx : Int32) : Gsk::GLUniformType
Get the type of the declared uniform for this shader at index idx.
Instance methods inherited from class GObject::Object
==(other : self)
==,
bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding
bind_property,
bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding
bind_property_full,
data(key : String) : Pointer(Void) | Nil
data,
finalize
finalize,
freeze_notify : Nil
freeze_notify,
getv(names : Enumerable(String), values : Enumerable(_)) : Nil
getv,
hash(hasher)
hash,
notify(property_name : String) : Nil
notify,
notify_by_pspec(pspec : GObject::ParamSpec) : Nil
notify_by_pspec,
notify_signal
notify_signal,
property(property_name : String, value : _) : Nil
property,
qdata(quark : UInt32) : Pointer(Void) | Nil
qdata,
ref_count : UInt32
ref_count,
run_dispose : Nil
run_dispose,
set_data(key : String, data : Pointer(Void) | Nil) : Nil
set_data,
set_property(property_name : String, value : _) : Nil
set_property,
steal_data(key : String) : Pointer(Void) | Nil
steal_data,
steal_qdata(quark : UInt32) : Pointer(Void) | Nil
steal_qdata,
thaw_notify : Nil
thaw_notify,
to_unsafe : Pointer(Void)
to_unsafe,
watch_closure(closure : GObject::Closure) : Nil
watch_closure
Constructor methods inherited from class GObject::Object
cast(obj : GObject::Object) : self
cast,
new(pointer : Pointer(Void), transfer : GICrystal::Transfer)new new, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv
Class methods inherited from class GObject::Object
cast?(obj : GObject::Object) : self | Nil
cast?,
compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64
compat_control,
g_type : UInt64
g_type,
interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec
interface_find_property,
interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec)
interface_list_properties
Macros inherited from class GObject::Object
previous_vfunc(*args)
previous_vfunc,
previous_vfunc!(*args)
previous_vfunc!,
signal(signature)
signal
Constructor Detail
Creates a Gsk::GLShader
that will render pixels using the specified code.
Creates a Gsk::GLShader
that will render pixels using the specified code.
Class Method Detail
Returns the type id (GType) registered in GLib type system.
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.
Gets the value of the uniform idx in the args block.
The uniform must be of bool type.
Gets the value of the uniform idx in the args block.
The uniform must be of float type.
Gets the value of the uniform idx in the args block.
The uniform must be of int type.
Gets the value of the uniform idx in the args block.
The uniform must be of uint type.
Gets the value of the uniform idx in the args block.
The uniform must be of vec2 type.
Gets the value of the uniform idx in the args block.
The uniform must be of vec3 type.
Gets the value of the uniform idx in the args block.
The uniform must be of vec4 type.
Get the size of the data block used to specify arguments for this shader.
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.
Looks for a uniform by the name name, and returns the index of the uniform, or -1 if it was not found.
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.
Gets the resource path for the GLSL sourcecode being used to render this shader.
Get the name of the declared uniform for this shader at index idx.
Get the offset into the data block where data for this uniforms is stored.
Get the type of the declared uniform for this shader at index idx.