2012-12-15 01:41:03 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/**
|
2019-07-26 19:09:29 +00:00
|
|
|
* SECTION:shell-glsl-effect
|
2019-07-26 20:08:52 +00:00
|
|
|
* @short_description: An offscreen effect using GLSL
|
2012-12-15 01:41:03 +00:00
|
|
|
*
|
2019-07-26 20:08:52 +00:00
|
|
|
* A #ShellGLSLEffect is a #ClutterOffscreenEffect that allows
|
|
|
|
* running custom GLSL to the vertex and fragment stages of the
|
|
|
|
* graphic pipeline.
|
2012-12-15 01:41:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <cogl/cogl.h>
|
2019-07-26 19:00:06 +00:00
|
|
|
#include "shell-glsl-effect.h"
|
2012-12-15 01:41:03 +00:00
|
|
|
|
2019-07-26 19:09:29 +00:00
|
|
|
typedef struct _ShellGLSLEffectPrivate ShellGLSLEffectPrivate;
|
|
|
|
struct _ShellGLSLEffectPrivate
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
|
|
|
CoglPipeline *pipeline;
|
|
|
|
};
|
|
|
|
|
2019-07-26 20:08:52 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (ShellGLSLEffect, shell_glsl_effect, CLUTTER_TYPE_OFFSCREEN_EFFECT);
|
2015-09-24 16:56:13 +00:00
|
|
|
|
2020-07-05 20:40:46 +00:00
|
|
|
static CoglPipeline *
|
|
|
|
shell_glsl_effect_create_pipeline (ClutterOffscreenEffect *effect,
|
|
|
|
CoglTexture *texture)
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
2019-07-26 20:08:52 +00:00
|
|
|
ShellGLSLEffect *self = SHELL_GLSL_EFFECT (effect);
|
2020-07-05 20:40:46 +00:00
|
|
|
ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (self);
|
2012-12-15 01:41:03 +00:00
|
|
|
|
2020-07-05 20:40:46 +00:00
|
|
|
cogl_pipeline_set_layer_texture (priv->pipeline, 0, texture);
|
2019-11-22 17:36:17 +00:00
|
|
|
|
2020-07-05 20:40:46 +00:00
|
|
|
return cogl_object_ref (priv->pipeline);
|
2012-12-15 01:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-26 19:09:29 +00:00
|
|
|
* shell_glsl_effect_add_glsl_snippet:
|
|
|
|
* @effect: a #ShellGLSLEffect
|
2012-12-15 01:41:03 +00:00
|
|
|
* @hook: where to insert the code
|
|
|
|
* @declarations: GLSL declarations
|
|
|
|
* @code: GLSL code
|
2020-08-19 09:26:11 +00:00
|
|
|
* @is_replace: whether Cogl code should be replaced by the custom shader
|
2012-12-15 01:41:03 +00:00
|
|
|
*
|
2019-07-26 20:08:52 +00:00
|
|
|
* Adds a GLSL snippet to the pipeline used for drawing the effect texture.
|
2012-12-15 01:41:03 +00:00
|
|
|
* See #CoglSnippet for details.
|
|
|
|
*
|
|
|
|
* This is only valid inside the a call to the build_pipeline() virtual
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
void
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_add_glsl_snippet (ShellGLSLEffect *effect,
|
|
|
|
ShellSnippetHook hook,
|
|
|
|
const char *declarations,
|
|
|
|
const char *code,
|
|
|
|
gboolean is_replace)
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
2019-07-26 19:09:29 +00:00
|
|
|
ShellGLSLEffectClass *klass = SHELL_GLSL_EFFECT_GET_CLASS (effect);
|
2012-12-15 01:41:03 +00:00
|
|
|
CoglSnippet *snippet;
|
|
|
|
|
|
|
|
g_return_if_fail (klass->base_pipeline != NULL);
|
|
|
|
|
|
|
|
if (is_replace)
|
|
|
|
{
|
2015-09-26 10:32:37 +00:00
|
|
|
snippet = cogl_snippet_new ((CoglSnippetHook)hook, declarations, NULL);
|
2012-12-15 01:41:03 +00:00
|
|
|
cogl_snippet_set_replace (snippet, code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-26 10:32:37 +00:00
|
|
|
snippet = cogl_snippet_new ((CoglSnippetHook)hook, declarations, code);
|
2012-12-15 01:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hook == SHELL_SNIPPET_HOOK_VERTEX ||
|
|
|
|
hook == SHELL_SNIPPET_HOOK_FRAGMENT)
|
|
|
|
cogl_pipeline_add_snippet (klass->base_pipeline, snippet);
|
|
|
|
else
|
|
|
|
cogl_pipeline_add_layer_snippet (klass->base_pipeline, 0, snippet);
|
|
|
|
|
|
|
|
cogl_object_unref (snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_dispose (GObject *gobject)
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
2019-07-26 19:09:29 +00:00
|
|
|
ShellGLSLEffect *self = SHELL_GLSL_EFFECT (gobject);
|
|
|
|
ShellGLSLEffectPrivate *priv;
|
2012-12-15 01:41:03 +00:00
|
|
|
|
2019-07-26 19:09:29 +00:00
|
|
|
priv = shell_glsl_effect_get_instance_private (self);
|
2012-12-15 01:41:03 +00:00
|
|
|
|
|
|
|
g_clear_pointer (&priv->pipeline, cogl_object_unref);
|
|
|
|
|
2019-07-26 19:09:29 +00:00
|
|
|
G_OBJECT_CLASS (shell_glsl_effect_parent_class)->dispose (gobject);
|
2012-12-15 01:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_init (ShellGLSLEffect *effect)
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_constructed (GObject *object)
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
2019-07-26 19:09:29 +00:00
|
|
|
ShellGLSLEffect *self;
|
|
|
|
ShellGLSLEffectClass *klass;
|
|
|
|
ShellGLSLEffectPrivate *priv;
|
2012-12-15 01:41:03 +00:00
|
|
|
CoglContext *ctx =
|
|
|
|
clutter_backend_get_cogl_context (clutter_get_default_backend ());
|
|
|
|
|
2019-07-26 19:09:29 +00:00
|
|
|
G_OBJECT_CLASS (shell_glsl_effect_parent_class)->constructed (object);
|
2012-12-15 01:41:03 +00:00
|
|
|
|
|
|
|
/* Note that, differently from ClutterBlurEffect, we are calling
|
|
|
|
this inside constructed, not init, so klass points to the most-derived
|
2019-07-26 19:09:29 +00:00
|
|
|
GTypeClass, not ShellGLSLEffectClass.
|
2012-12-15 01:41:03 +00:00
|
|
|
*/
|
2019-07-26 19:09:29 +00:00
|
|
|
klass = SHELL_GLSL_EFFECT_GET_CLASS (object);
|
|
|
|
self = SHELL_GLSL_EFFECT (object);
|
|
|
|
priv = shell_glsl_effect_get_instance_private (self);
|
2012-12-15 01:41:03 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (klass->base_pipeline == NULL))
|
|
|
|
{
|
|
|
|
klass->base_pipeline = cogl_pipeline_new (ctx);
|
2023-02-17 06:17:56 +00:00
|
|
|
cogl_pipeline_set_blend (klass->base_pipeline, "RGB = ADD (SRC_COLOR * (SRC_COLOR[A]), DST_COLOR * (1-SRC_COLOR[A]))", NULL);
|
2012-12-15 01:41:03 +00:00
|
|
|
|
|
|
|
if (klass->build_pipeline != NULL)
|
|
|
|
klass->build_pipeline (self);
|
|
|
|
}
|
|
|
|
|
2015-09-24 18:07:44 +00:00
|
|
|
priv->pipeline = cogl_pipeline_copy (klass->base_pipeline);
|
2012-12-15 01:41:03 +00:00
|
|
|
|
2019-07-26 20:08:52 +00:00
|
|
|
cogl_pipeline_set_layer_null_texture (klass->base_pipeline, 0);
|
2012-12-15 01:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_class_init (ShellGLSLEffectClass *klass)
|
2012-12-15 01:41:03 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2019-07-26 20:08:52 +00:00
|
|
|
ClutterOffscreenEffectClass *offscreen_class;
|
|
|
|
|
|
|
|
offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
|
2020-07-05 20:40:46 +00:00
|
|
|
offscreen_class->create_pipeline = shell_glsl_effect_create_pipeline;
|
2019-07-26 20:08:52 +00:00
|
|
|
|
2019-07-26 19:09:29 +00:00
|
|
|
gobject_class->constructed = shell_glsl_effect_constructed;
|
|
|
|
gobject_class->dispose = shell_glsl_effect_dispose;
|
2012-12-15 01:41:03 +00:00
|
|
|
}
|
2014-03-06 15:17:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-07-26 19:09:29 +00:00
|
|
|
* shell_glsl_effect_get_uniform_location:
|
|
|
|
* @effect: a #ShellGLSLEffect
|
2014-03-06 15:17:36 +00:00
|
|
|
* @name: the uniform name
|
|
|
|
*
|
|
|
|
* Returns: the location of the uniform named @name, that can be
|
2019-07-26 19:09:29 +00:00
|
|
|
* passed to shell_glsl_effect_set_uniform_float().
|
2014-03-06 15:17:36 +00:00
|
|
|
*/
|
|
|
|
int
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_get_uniform_location (ShellGLSLEffect *effect,
|
|
|
|
const char *name)
|
2014-03-06 15:17:36 +00:00
|
|
|
{
|
2019-07-26 19:09:29 +00:00
|
|
|
ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (effect);
|
2015-09-24 18:07:44 +00:00
|
|
|
return cogl_pipeline_get_uniform_location (priv->pipeline, name);
|
2014-03-06 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-26 19:09:29 +00:00
|
|
|
* shell_glsl_effect_set_uniform_float:
|
|
|
|
* @effect: a #ShellGLSLEffect
|
|
|
|
* @uniform: the uniform location (as returned by shell_glsl_effect_get_uniform_location())
|
2014-03-06 15:17:36 +00:00
|
|
|
* @n_components: the number of components in the uniform (eg. 3 for a vec3)
|
|
|
|
* @total_count: the total number of floats in @value
|
|
|
|
* @value: (array length=total_count): the array of floats to set @uniform
|
|
|
|
*/
|
|
|
|
void
|
2019-07-26 19:09:29 +00:00
|
|
|
shell_glsl_effect_set_uniform_float (ShellGLSLEffect *effect,
|
|
|
|
int uniform,
|
|
|
|
int n_components,
|
|
|
|
int total_count,
|
|
|
|
const float *value)
|
2014-03-06 15:17:36 +00:00
|
|
|
{
|
2019-07-26 19:09:29 +00:00
|
|
|
ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (effect);
|
2015-09-24 18:07:44 +00:00
|
|
|
cogl_pipeline_set_uniform_float (priv->pipeline, uniform,
|
2014-03-06 15:17:36 +00:00
|
|
|
n_components, total_count / n_components,
|
|
|
|
value);
|
|
|
|
}
|
2022-02-08 20:22:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_glsl_effect_set_uniform_matrix:
|
|
|
|
* @effect: a #ShellGLSLEffect
|
|
|
|
* @uniform: the uniform location (as returned by shell_glsl_effect_get_uniform_location())
|
|
|
|
* @transpose: Whether to transpose the matrix
|
|
|
|
* @dimensions: the number of components in the uniform (eg. 3 for a vec3)
|
|
|
|
* @total_count: the total number of floats in @value
|
|
|
|
* @value: (array length=total_count): the array of floats to set @uniform
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
shell_glsl_effect_set_uniform_matrix (ShellGLSLEffect *effect,
|
|
|
|
int uniform,
|
|
|
|
gboolean transpose,
|
|
|
|
int dimensions,
|
|
|
|
int total_count,
|
|
|
|
const float *value)
|
|
|
|
{
|
|
|
|
ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (effect);
|
|
|
|
cogl_pipeline_set_uniform_matrix (priv->pipeline, uniform,
|
|
|
|
dimensions,
|
|
|
|
total_count / (dimensions * dimensions),
|
|
|
|
transpose, value);
|
|
|
|
}
|