Class GLShader
- All Implemented Interfaces:
Proxy
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:
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:
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:
vec4 GskTexture(sampler2D sampler, vec2 texCoords);
This samples a texture (e.g. u_texture1) at the specified coordinates, and contains some helper ifdefs to ensure that it works on all OpenGL versions.
You can compile the shader yourself using compile(org.gnome.gsk.Renderer)
,
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
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;
}
Deprecation
This feature was deprecated in GTK 4.16 after the new rendering infrastructure
introduced in 4.14 did not support it.
The lack of Vulkan integration would have made it a very hard feature to support.
If you want to use OpenGL directly, you should look at GtkGLArea which uses a different approach and is still well supported.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
GLShader.Builder<B extends GLShader.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionGLShader
(MemorySegment address) Create a GLShader proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected GLShader
asParent()
Returns this instance as if it were its parent type.static GLShader.Builder
<? extends GLShader.Builder> builder()
AGLShader.Builder
object constructs aGLShader
with the specified properties.boolean
Deprecated.GTK's new Vulkan-focused rendering does not support this feature.int
findUniformByName
(String name) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.formatArgs
(Object... varargs) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.static GLShader
Deprecated.GTK's new Vulkan-focused rendering does not support this feature.static GLShader
fromResource
(String resourcePath) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.boolean
getArgBool
(Bytes args, int idx) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.float
getArgFloat
(Bytes args, int idx) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.int
Deprecated.GTK's new Vulkan-focused rendering does not support this feature.long
Deprecated.GTK's new Vulkan-focused rendering does not support this feature.int
getArgUint
(Bytes args, int idx) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.void
getArgVec2
(Bytes args, int idx, Vec2 outValue) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.void
getArgVec3
(Bytes args, int idx, Vec3 outValue) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.void
getArgVec4
(Bytes args, int idx, Vec4 outValue) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.int
Deprecated.GTK's new Vulkan-focused rendering does not support this feature.int
Deprecated.GTK's new Vulkan-focused rendering does not support this feature.Deprecated.GTK's new Vulkan-focused rendering does not support this feature.Deprecated.GTK's new Vulkan-focused rendering does not support this feature.static Type
getType()
Get the GType of the GLShader classgetUniformName
(int idx) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.int
getUniformOffset
(int idx) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.getUniformType
(int idx) Deprecated.GTK's new Vulkan-focused rendering does not support this feature.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
GLShader
Create a GLShader proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
fromBytes
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Creates aGskGLShader
that will render pixels using the specified code.- Parameters:
sourcecode
- GLSL sourcecode for the shader, as aGBytes
- Returns:
- A new
GskGLShader
-
fromResource
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Creates aGskGLShader
that will render pixels using the specified code.- Parameters:
resourcePath
- path to a resource that contains the GLSL sourcecode for the shader- Returns:
- A new
GskGLShader
-
compile
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Tries to compile the this GLShader for the givenrenderer
.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.
- Parameters:
renderer
- aGskRenderer
- Returns:
true
on success,false
if an error occurred- Throws:
GErrorException
- seeGError
-
findUniformByName
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Looks for a uniform by the namename
, and returns the index of the uniform, or -1 if it was not found.- Parameters:
name
- uniform name- Returns:
- The index of the uniform, or -1
-
formatArgs
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Formats the uniform data as needed for feeding the named uniforms values into the shader.The argument list is a list of pairs of names, and values for the types that match the declared uniforms (i.e. double/int/guint/gboolean for primitive values and
graphene_vecN_t *
for vecN uniforms).Any uniforms of the shader that are not included in the argument list are zero-initialized.
- Parameters:
varargs
- name-Value pairs for the uniforms of this GLShader, ending with anull
name- Returns:
- A newly allocated block of data which can be
passed to
GLShaderNode(org.gnome.gsk.GLShader, org.gnome.graphene.Rect, org.gnome.glib.Bytes, org.gnome.gsk.RenderNode[])
.
-
getArgBool
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of bool type.
- Parameters:
args
- uniform argumentsidx
- index of the uniform- Returns:
- The value
-
getArgFloat
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of float type.
- Parameters:
args
- uniform argumentsidx
- index of the uniform- Returns:
- The value
-
getArgInt
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of int type.
- Parameters:
args
- uniform argumentsidx
- index of the uniform- Returns:
- The value
-
getArgUint
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of uint type.
- Parameters:
args
- uniform argumentsidx
- index of the uniform- Returns:
- The value
-
getArgVec2
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of vec2 type.
- Parameters:
args
- uniform argumentsidx
- index of the uniformoutValue
- location to store the uniform value in
-
getArgVec3
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of vec3 type.
- Parameters:
args
- uniform argumentsidx
- index of the uniformoutValue
- location to store the uniform value in
-
getArgVec4
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the value of the uniformidx
in theargs
block.The uniform must be of vec4 type.
- Parameters:
args
- uniform argumentsidx
- index of the uniformoutValue
- location to store set the uniform value in
-
getArgsSize
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Get the size of the data block used to specify arguments for this shader.- Returns:
- The size of the data block
-
getNTextures
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.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.
- Returns:
- The number of texture inputs required by this GLShader
-
getNUniforms
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Get the number of declared uniforms for this shader.- Returns:
- The number of declared uniforms
-
getResource
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the resource path for the GLSL sourcecode being used to render this shader.- Returns:
- The resource path for the shader
-
getSource
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Gets the GLSL sourcecode being used to render this shader.- Returns:
- The source code for the shader
-
getUniformName
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Get the name of the declared uniform for this shader at indexidx
.- Parameters:
idx
- index of the uniform- Returns:
- The name of the declared uniform
-
getUniformOffset
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Get the offset into the data block where data for this uniforms is stored.- Parameters:
idx
- index of the uniform- Returns:
- The data offset
-
getUniformType
Deprecated.GTK's new Vulkan-focused rendering does not support this feature. Use GtkGLArea for OpenGL rendering.Get the type of the declared uniform for this shader at indexidx
.- Parameters:
idx
- index of the uniform- Returns:
- The type of the declared uniform
-
builder
AGLShader.Builder
object constructs aGLShader
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withGLShader.Builder.build()
.
-