summaryrefslogtreecommitdiffstats
path: root/src/shell-glsl-effect.c
blob: 3051e0af9de75469e25dc73ba796637a38a322f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/**
 * SECTION:shell-glsl-effect
 * @short_description: An offscreen effect using GLSL
 *
 * A #ShellGLSLEffect is a #ClutterOffscreenEffect that allows
 * running custom GLSL to the vertex and fragment stages of the
 * graphic pipeline.
 */

#include "config.h"

#include <cogl/cogl.h>
#include "shell-glsl-effect.h"

typedef struct _ShellGLSLEffectPrivate ShellGLSLEffectPrivate;
struct _ShellGLSLEffectPrivate
{
  CoglPipeline  *pipeline;
};

G_DEFINE_TYPE_WITH_PRIVATE (ShellGLSLEffect, shell_glsl_effect, CLUTTER_TYPE_OFFSCREEN_EFFECT);

static CoglPipeline *
shell_glsl_effect_create_pipeline (ClutterOffscreenEffect *effect,
                                   CoglTexture            *texture)
{
  ShellGLSLEffect *self = SHELL_GLSL_EFFECT (effect);
  ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (self);

  cogl_pipeline_set_layer_texture (priv->pipeline, 0, texture);

  return cogl_object_ref (priv->pipeline);
}

/**
 * shell_glsl_effect_add_glsl_snippet:
 * @effect: a #ShellGLSLEffect
 * @hook: where to insert the code
 * @declarations: GLSL declarations
 * @code: GLSL code
 * @is_replace: whether Cogl code should be replaced by the custom shader
 *
 * Adds a GLSL snippet to the pipeline used for drawing the effect texture.
 * See #CoglSnippet for details.
 *
 * This is only valid inside the a call to the build_pipeline() virtual
 * function.
 */
void
shell_glsl_effect_add_glsl_snippet (ShellGLSLEffect  *effect,
                                    ShellSnippetHook  hook,
                                    const char       *declarations,
                                    const char       *code,
                                    gboolean          is_replace)
{
  ShellGLSLEffectClass *klass = SHELL_GLSL_EFFECT_GET_CLASS (effect);
  CoglSnippet *snippet;

  g_return_if_fail (klass->base_pipeline != NULL);

  if (is_replace)
    {
      snippet = cogl_snippet_new ((CoglSnippetHook)hook, declarations, NULL);
      cogl_snippet_set_replace (snippet, code);
    }
  else
    {
      snippet = cogl_snippet_new ((CoglSnippetHook)hook, declarations, code);
    }

  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
shell_glsl_effect_dispose (GObject *gobject)
{
  ShellGLSLEffect *self = SHELL_GLSL_EFFECT (gobject);
  ShellGLSLEffectPrivate *priv;

  priv = shell_glsl_effect_get_instance_private (self);

  g_clear_pointer (&priv->pipeline, cogl_object_unref);

  G_OBJECT_CLASS (shell_glsl_effect_parent_class)->dispose (gobject);
}

static void
shell_glsl_effect_init (ShellGLSLEffect *effect)
{
}

static void
shell_glsl_effect_constructed (GObject *object)
{
  ShellGLSLEffect *self;
  ShellGLSLEffectClass *klass;
  ShellGLSLEffectPrivate *priv;
  CoglContext *ctx =
    clutter_backend_get_cogl_context (clutter_get_default_backend ());

  G_OBJECT_CLASS (shell_glsl_effect_parent_class)->constructed (object);

  /* Note that, differently from ClutterBlurEffect, we are calling
     this inside constructed, not init, so klass points to the most-derived
     GTypeClass, not ShellGLSLEffectClass.
  */
  klass = SHELL_GLSL_EFFECT_GET_CLASS (object);
  self = SHELL_GLSL_EFFECT (object);
  priv = shell_glsl_effect_get_instance_private (self);

  if (G_UNLIKELY (klass->base_pipeline == NULL))
    {
      klass->base_pipeline = cogl_pipeline_new (ctx);
      cogl_pipeline_set_blend (klass->base_pipeline, "RGBA = ADD (SRC_COLOR * (SRC_COLOR[A]), DST_COLOR * (1-SRC_COLOR[A]))", NULL);

      if (klass->build_pipeline != NULL)
        klass->build_pipeline (self);
    }

  priv->pipeline = cogl_pipeline_copy (klass->base_pipeline);

  cogl_pipeline_set_layer_null_texture (klass->base_pipeline, 0);
}

static void
shell_glsl_effect_class_init (ShellGLSLEffectClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterOffscreenEffectClass *offscreen_class;

  offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
  offscreen_class->create_pipeline = shell_glsl_effect_create_pipeline;

  gobject_class->constructed = shell_glsl_effect_constructed;
  gobject_class->dispose = shell_glsl_effect_dispose;
}

/**
 * shell_glsl_effect_get_uniform_location:
 * @effect: a #ShellGLSLEffect
 * @name: the uniform name
 *
 * Returns: the location of the uniform named @name, that can be
 *          passed to shell_glsl_effect_set_uniform_float().
 */
int
shell_glsl_effect_get_uniform_location (ShellGLSLEffect *effect,
                                        const char      *name)
{
  ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (effect);
  return cogl_pipeline_get_uniform_location (priv->pipeline, name);
}

/**
 * shell_glsl_effect_set_uniform_float:
 * @effect: a #ShellGLSLEffect
 * @uniform: the uniform location (as returned by shell_glsl_effect_get_uniform_location())
 * @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
shell_glsl_effect_set_uniform_float (ShellGLSLEffect *effect,
                                     int              uniform,
                                     int              n_components,
                                     int              total_count,
                                     const float     *value)
{
  ShellGLSLEffectPrivate *priv = shell_glsl_effect_get_instance_private (effect);
  cogl_pipeline_set_uniform_float (priv->pipeline, uniform,
                                   n_components, total_count / n_components,
                                   value);
}

/**
 * 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);
}