Debian Codegraph @

clutter/deprecated/clutter-shader.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Authored By: Matthew Allum <[email protected]>
7 * Øyvind Kolås <[email protected]>
8 * Emmanuele Bassi <[email protected]>
9 *
10 * Copyright (C) 2007, 2008 OpenedHand
11 * Copyright (C) 2009 Intel Corp
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
25 *
26 *
27 */
28
29 /**
30 * SECTION:clutter-shader
31 * @short_description: Programmable pipeline abstraction
32 *
33 * #ClutterShader is an object providing an abstraction over the
34 * OpenGL programmable pipeline. By using #ClutterShader<!-- -->s is
35 * possible to override the drawing pipeline by using small programs
36 * also known as "shaders".
37 *
38 * #ClutterShader is available since Clutter 0.6.
39 *
40 * #ClutterShader is deprecated since Clutter 1.8; use #ClutterShaderEffect
41 * in newly written code, instead.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <string.h>
49 #include <stdlib.h>
50
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54
55 #define CLUTTER_DISABLE_DEPRECATION_WARNINGS
56
57 #include <glib.h>
58 #include <glib/gi18n-lib.h>
59
60 /* This file depends on the cogl 1.x api which isn't exposed when
61 * COGL_ENABLE_EXPERIMENTAL_2_0_API is defined...
62 */
63 #undef COGL_ENABLE_EXPERIMENTAL_2_0_API
64 #include <cogl/cogl.h>
65
66 #include "clutter-shader.h"
67
68 #include "clutter-debug.h"
69 #include "clutter-private.h"
70
71 /* global list of shaders */
72 static GList *clutter_shaders_list = NULL;
73
74 struct _ClutterShaderPrivate
75 {
76 guint compiled : 1; /* Shader is bound to the GL context */
77 guint is_enabled : 1;
78 guint vertex_is_glsl : 1;
79 guint fragment_is_glsl : 1;
80
81 gchar *vertex_source; /* GLSL source for vertex shader */
82 gchar *fragment_source; /* GLSL source for fragment shader */
83
84 CoglHandle program;
85
86 CoglHandle vertex_shader;
87 CoglHandle fragment_shader;
88 };
89
90 enum
91 {
92 PROP_0,
93
94 PROP_VERTEX_SOURCE,
95 PROP_FRAGMENT_SOURCE,
96 PROP_COMPILED,
97 PROP_ENABLED,
98
99 PROP_LAST
100 };
101
102 static GParamSpec *obj_props[PROP_LAST];
103
104 G_DEFINE_TYPE_WITH_PRIVATE (ClutterShader, clutter_shader, G_TYPE_OBJECT)
105
106 static inline void
107 clutter_shader_release_internal (ClutterShader *shader)
108 {
109 ClutterShaderPrivate *priv = shader->priv;
110
111 if (!priv->compiled)
112 return;
113
114 g_assert (priv->program != COGL_INVALID_HANDLE);
115
116 if (priv->vertex_is_glsl && priv->vertex_shader != COGL_INVALID_HANDLE)
117 cogl_handle_unref (priv->vertex_shader);
118
119 if (priv->fragment_is_glsl && priv->fragment_shader != COGL_INVALID_HANDLE)
120 cogl_handle_unref (priv->fragment_shader);
121
122 if (priv->program != COGL_INVALID_HANDLE)
123 cogl_handle_unref (priv->program);
124
125 priv->vertex_shader = COGL_INVALID_HANDLE;
126 priv->fragment_shader = COGL_INVALID_HANDLE;
127 priv->program = COGL_INVALID_HANDLE;
128 priv->compiled = FALSE;
129 }
130
131 static void
132 clutter_shader_finalize (GObject *object)
133 {
134 ClutterShader *shader;
135 ClutterShaderPrivate *priv;
136
137 shader = CLUTTER_SHADER (object);
138 priv = shader->priv;
139
140 clutter_shaders_list = g_list_remove (clutter_shaders_list, object);
141
142 g_free (priv->fragment_source);
143 g_free (priv->vertex_source);
144
145 G_OBJECT_CLASS (clutter_shader_parent_class)->finalize (object);
146 }
147
148 static void
149 clutter_shader_dispose (GObject *object)
150 {
151 ClutterShader *shader = CLUTTER_SHADER (object);
152
153 clutter_shader_release_internal (shader);
154
155 G_OBJECT_CLASS (clutter_shader_parent_class)->finalize (object);
156 }
157
158 static void
159 clutter_shader_set_property (GObject *object,
160 guint prop_id,
161 const GValue *value,
162 GParamSpec *pspec)
163 {
164 ClutterShader *shader = CLUTTER_SHADER(object);
165
166 switch (prop_id)
167 {
168 case PROP_VERTEX_SOURCE:
169 clutter_shader_set_vertex_source (shader,
170 g_value_get_string (value), -1);
171 break;
172 case PROP_FRAGMENT_SOURCE:
173 clutter_shader_set_fragment_source (shader,
174 g_value_get_string (value), -1);
175 break;
176 case PROP_ENABLED:
177 clutter_shader_set_is_enabled (shader, g_value_get_boolean (value));
178 break;
179 default:
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 break;
182 }
183 }
184
185 static void
186 clutter_shader_get_property (GObject *object,
187 guint prop_id,
188 GValue *value,
189 GParamSpec *pspec)
190 {
191 ClutterShader *shader;
192 ClutterShaderPrivate *priv;
193
194 shader = CLUTTER_SHADER(object);
195 priv = shader->priv;
196
197 switch (prop_id)
198 {
199 case PROP_VERTEX_SOURCE:
200 g_value_set_string (value, priv->vertex_source);
201 break;
202 case PROP_FRAGMENT_SOURCE:
203 g_value_set_string (value, priv->fragment_source);
204 break;
205 case PROP_COMPILED:
206 g_value_set_boolean (value, priv->compiled);
207 break;
208 case PROP_ENABLED:
209 g_value_set_boolean (value, priv->is_enabled);
210 break;
211 default:
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 break;
214 }
215 }
216
217 static GObject *
218 clutter_shader_constructor (GType type,
219 guint n_params,
220 GObjectConstructParam *params)
221 {
222 GObjectClass *parent_class;
223 GObject *object;
224
225 parent_class = G_OBJECT_CLASS (clutter_shader_parent_class);
226 object = parent_class->constructor (type, n_params, params);
227
228 /* add this instance to the global list of shaders */
229 clutter_shaders_list = g_list_prepend (clutter_shaders_list, object);
230
231 return object;
232 }
233
234 static void
235 clutter_shader_class_init (ClutterShaderClass *klass)
236 {
237 GObjectClass *object_class = G_OBJECT_CLASS (klass);
238 GParamSpec *pspec = NULL;
239
240 object_class->finalize = clutter_shader_finalize;
241 object_class->dispose = clutter_shader_dispose;
242 object_class->set_property = clutter_shader_set_property;
243 object_class->get_property = clutter_shader_get_property;
244 object_class->constructor = clutter_shader_constructor;
245
246 /**
247 * ClutterShader:vertex-source:
248 *
249 * GLSL source code for the vertex shader part of the shader
250 * program, if any
251 *
252 * Since: 0.6
253 *
254 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
255 */
256 pspec = g_param_spec_string ("vertex-source",
257 P_("Vertex Source"),
258 P_("Source of vertex shader"),
259 NULL,
260 CLUTTER_PARAM_READWRITE);
261 obj_props[PROP_VERTEX_SOURCE] = pspec;
262 g_object_class_install_property (object_class, PROP_VERTEX_SOURCE, pspec);
263
264 /**
265 * ClutterShader:fragment-source:
266 *
267 * GLSL source code for the fragment shader part of the shader program.
268 *
269 * Since: 0.6
270 *
271 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
272 */
273 pspec = g_param_spec_string ("fragment-source",
274 P_("Fragment Source"),
275 P_("Source of fragment shader"),
276 NULL,
277 CLUTTER_PARAM_READWRITE);
278 obj_props[PROP_FRAGMENT_SOURCE] = pspec;
279 g_object_class_install_property (object_class, PROP_FRAGMENT_SOURCE, pspec);
280
281 /**
282 * ClutterShader:compiled:
283 *
284 * Whether the shader is compiled and linked, ready for use
285 * in the GL context.
286 *
287 * Since: 0.8
288 *
289 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
290 */
291 pspec = g_param_spec_boolean ("compiled",
292 P_("Compiled"),
293 P_("Whether the shader is compiled and linked"),
294 FALSE,
295 CLUTTER_PARAM_READABLE);
296 obj_props[PROP_COMPILED] = pspec;
297 g_object_class_install_property (object_class, PROP_COMPILED, pspec);
298
299 /**
300 * ClutterShader:enabled:
301 *
302 * Whether the shader is currently used in the GL rendering pipeline.
303 *
304 * Since: 0.6
305 *
306 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
307 */
308 pspec = g_param_spec_boolean ("enabled",
309 P_("Enabled"),
310 P_("Whether the shader is enabled"),
311 FALSE,
312 CLUTTER_PARAM_READWRITE);
313 obj_props[PROP_ENABLED] = pspec;
314 g_object_class_install_property (object_class, PROP_ENABLED, pspec);
315 }
316
317 static void
318 clutter_shader_init (ClutterShader *self)
319 {
320 ClutterShaderPrivate *priv;
321
322 priv = self->priv = clutter_shader_get_instance_private (self);
323
324 priv->compiled = FALSE;
325
326 priv->vertex_source = NULL;
327 priv->fragment_source = NULL;
328
329 priv->program = COGL_INVALID_HANDLE;
330 priv->vertex_shader = COGL_INVALID_HANDLE;
331 priv->fragment_shader = COGL_INVALID_HANDLE;
332 }
333
334 /**
335 * clutter_shader_new:
336 *
337 * Create a new #ClutterShader instance.
338 *
339 * Return value: a new #ClutterShader.
340 *
341 * Since: 0.6
342 *
343 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
344 */
345 ClutterShader *
346 clutter_shader_new (void)
347 {
348 return g_object_new (CLUTTER_TYPE_SHADER, NULL);
349 }
350
351 static inline void
352 clutter_shader_set_source (ClutterShader *shader,
353 ClutterShaderType shader_type,
354 const gchar *data,
355 gssize length)
356 {
357 ClutterShaderPrivate *priv = shader->priv;
358 gboolean is_glsl = FALSE;
359
360 if (length < 0)
361 length = strlen (data);
362
363 g_object_freeze_notify (G_OBJECT (shader));
364
365 /* release shader if bound when changing the source, the shader will
366 * automatically be rebound on the next use.
367 */
368 if (clutter_shader_is_compiled (shader))
369 clutter_shader_release (shader);
370
371 is_glsl = !g_str_has_prefix (data, "!!ARBfp");
372
373 CLUTTER_NOTE (SHADER,
374 "setting %s shader (GLSL:%s, len:%" G_GSSIZE_FORMAT ")",
375 shader_type == CLUTTER_VERTEX_SHADER ? "vertex" : "fragment",
376 is_glsl ? "yes" : "no",
377 length);
378
379 switch (shader_type)
380 {
381 case CLUTTER_FRAGMENT_SHADER:
382 g_free (priv->fragment_source);
383
384 priv->fragment_source = g_strndup (data, length);
385 priv->fragment_is_glsl = is_glsl;
386 g_object_notify_by_pspec (G_OBJECT (shader), obj_props[PROP_FRAGMENT_SOURCE]);
387 break;
388
389 case CLUTTER_VERTEX_SHADER:
390 g_free (priv->vertex_source);
391
392 priv->vertex_source = g_strndup (data, length);
393 priv->vertex_is_glsl = is_glsl;
394 g_object_notify_by_pspec (G_OBJECT (shader), obj_props[PROP_VERTEX_SOURCE]);
395 break;
396 }
397
398 g_object_thaw_notify (G_OBJECT (shader));
399 }
400
401 /**
402 * clutter_shader_set_fragment_source:
403 * @shader: a #ClutterShader
404 * @data: GLSL source code.
405 * @length: length of source buffer (currently ignored)
406 *
407 * Sets the GLSL source code to be used by a #ClutterShader for the fragment
408 * program.
409 *
410 * Since: 0.6
411 *
412 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
413 */
414 void
415 clutter_shader_set_fragment_source (ClutterShader *shader,
416 const gchar *data,
417 gssize length)
418 {
419 g_return_if_fail (CLUTTER_IS_SHADER (shader));
420 g_return_if_fail (data != NULL);
421
422 clutter_shader_set_source (shader, CLUTTER_FRAGMENT_SHADER, data, length);
423 }
424
425 /**
426 * clutter_shader_set_vertex_source:
427 * @shader: a #ClutterShader
428 * @data: GLSL source code.
429 * @length: length of source buffer (currently ignored)
430 *
431 * Sets the GLSL source code to be used by a #ClutterShader for the vertex
432 * program.
433 *
434 * Since: 0.6
435 *
436 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
437 */
438 void
439 clutter_shader_set_vertex_source (ClutterShader *shader,
440 const gchar *data,
441 gssize length)
442 {
443 g_return_if_fail (CLUTTER_IS_SHADER (shader));
444 g_return_if_fail (data != NULL);
445
446 clutter_shader_set_source (shader, CLUTTER_VERTEX_SHADER, data, length);
447 }
448
449 static const gchar *
450 clutter_shader_get_source (ClutterShader *shader,
451 ClutterShaderType shader_type)
452 {
453 switch (shader_type)
454 {
455 case CLUTTER_FRAGMENT_SHADER:
456 return shader->priv->fragment_source;
457
458 case CLUTTER_VERTEX_SHADER:
459 return shader->priv->vertex_source;
460 }
461
462 return NULL;
463 }
464
465 static CoglHandle
466 clutter_shader_get_cogl_shader (ClutterShader *shader,
467 ClutterShaderType shader_type)
468 {
469 switch (shader_type)
470 {
471 case CLUTTER_FRAGMENT_SHADER:
472 return shader->priv->fragment_shader;
473
474 case CLUTTER_VERTEX_SHADER:
475 return shader->priv->vertex_shader;
476 }
477
478 return COGL_INVALID_HANDLE;
479 }
480
481 static gboolean
482 clutter_shader_glsl_bind (ClutterShader *self,
483 ClutterShaderType shader_type,
484 GError **error)
485 {
486 ClutterShaderPrivate *priv = self->priv;
487 CoglHandle shader = COGL_INVALID_HANDLE;
488
489 switch (shader_type)
490 {
491 case CLUTTER_VERTEX_SHADER:
492 shader = cogl_create_shader (COGL_SHADER_TYPE_VERTEX);
493 cogl_shader_source (shader, priv->vertex_source);
494
495 priv->vertex_shader = shader;
496 break;
497
498 case CLUTTER_FRAGMENT_SHADER:
499 shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
500 cogl_shader_source (shader, priv->fragment_source);
501
502 priv->fragment_shader = shader;
503 break;
504 }
505
506 g_assert (shader != COGL_INVALID_HANDLE);
507
508 cogl_shader_compile (shader);
509 if (!cogl_shader_is_compiled (shader))
510 {
511 gchar *log_buf;
512
513 log_buf = cogl_shader_get_info_log (shader);
514
515 g_set_error (error, CLUTTER_SHADER_ERROR,
516 CLUTTER_SHADER_ERROR_COMPILE,
517 /* translators: the first %s is the type of the shader, either
518 * Vertex shader or Fragment shader; the second %s is the actual
519 * error as reported by COGL
520 */
521 _("%s compilation failed: %s"),
522 shader_type == CLUTTER_VERTEX_SHADER ? _("Vertex shader")
523 : _("Fragment shader"),
524 log_buf);
525
526 g_free (log_buf);
527
528 return FALSE;
529 }
530
531 cogl_program_attach_shader (priv->program, shader);
532
533 return TRUE;
534 }
535
536 static gboolean
537 bind_glsl_shader (ClutterShader *self,
538 GError **error)
539 {
540 ClutterShaderPrivate *priv = self->priv;
541 GError *bind_error = NULL;
542 gboolean res;
543
544 priv->program = cogl_create_program ();
545
546 if (priv->vertex_is_glsl && priv->vertex_source != COGL_INVALID_HANDLE)
547 {
548 res = clutter_shader_glsl_bind (self,
549 CLUTTER_VERTEX_SHADER,
550 &bind_error);
551
552 if (!res)
553 {
554 g_propagate_error (error, bind_error);
555 return FALSE;
556 }
557 }
558
559 if (priv->fragment_is_glsl && priv->fragment_source != COGL_INVALID_HANDLE)
560 {
561 res = clutter_shader_glsl_bind (self,
562 CLUTTER_FRAGMENT_SHADER,
563 &bind_error);
564
565 if (!res)
566 {
567 g_propagate_error (error, bind_error);
568 return FALSE;
569 }
570 }
571
572 cogl_program_link (priv->program);
573
574 return TRUE;
575 }
576
577 /**
578 * clutter_shader_compile:
579 * @shader: a #ClutterShader
580 * @error: return location for a #GError, or %NULL
581 *
582 * Compiles and links GLSL sources set for vertex and fragment shaders for
583 * a #ClutterShader. If the compilation fails and a #GError return location is
584 * provided the error will contain the errors from the compiler, if any.
585 *
586 * Return value: returns TRUE if the shader was succesfully compiled.
587 *
588 * Since: 0.8
589 *
590 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
591 */
592 gboolean
593 clutter_shader_compile (ClutterShader *shader,
594 GError **error)
595 {
596 ClutterShaderPrivate *priv;
597
598 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), FALSE);
599
600 priv = shader->priv;
601
602 if (priv->compiled)
603 return priv->compiled;
604
605 if ((priv->vertex_source != COGL_INVALID_HANDLE && !priv->vertex_is_glsl) ||
606 (priv->fragment_source != COGL_INVALID_HANDLE && !priv->fragment_is_glsl))
607 {
608 /* XXX: Could remove this check, since we only advertise support for GLSL
609 * shaders anyways. */
610 g_set_error (error, CLUTTER_SHADER_ERROR,
611 CLUTTER_SHADER_ERROR_NO_ASM,
612 "ASM shaders not supported");
613 priv->compiled = FALSE;
614 return priv->compiled;
615 }
616
617 if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
618 {
619 g_set_error (error, CLUTTER_SHADER_ERROR,
620 CLUTTER_SHADER_ERROR_NO_GLSL,
621 "GLSL shaders not supported");
622 priv->compiled = FALSE;
623 return priv->compiled;
624 }
625
626 priv->compiled = bind_glsl_shader (shader, error);
627 g_object_notify_by_pspec (G_OBJECT (shader), obj_props[PROP_COMPILED]);
628
629 return priv->compiled;
630 }
631
632 /**
633 * clutter_shader_release:
634 * @shader: a #ClutterShader
635 *
636 * Frees up any GL context resources held by the shader.
637 *
638 * Since: 0.6
639 *
640 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
641 */
642 void
643 clutter_shader_release (ClutterShader *shader)
644 {
645 g_return_if_fail (CLUTTER_IS_SHADER (shader));
646
647 clutter_shader_release_internal (shader);
648
649 g_object_notify_by_pspec (G_OBJECT (shader), obj_props[PROP_COMPILED]);
650 }
651
652 /**
653 * clutter_shader_is_compiled:
654 * @shader: a #ClutterShader
655 *
656 * Checks whether @shader is is currently compiled, linked and bound
657 * to the GL context.
658 *
659 * Return value: %TRUE if the shader is compiled, linked and ready for use.
660 *
661 * Since: 0.8
662 *
663 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
664 */
665 gboolean
666 clutter_shader_is_compiled (ClutterShader *shader)
667 {
668 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), FALSE);
669
670 return shader->priv->compiled;
671 }
672
673 /**
674 * clutter_shader_set_is_enabled:
675 * @shader: a #ClutterShader
676 * @enabled: The new state of the shader.
677 *
678 * Enables a shader. This function will attempt to compile and link
679 * the shader, if it isn't already.
680 *
681 * When @enabled is %FALSE the default state of the GL pipeline will be
682 * used instead.
683 *
684 * Since: 0.6
685 *
686 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
687 */
688 void
689 clutter_shader_set_is_enabled (ClutterShader *shader,
690 gboolean enabled)
691 {
692 ClutterShaderPrivate *priv;
693
694 g_return_if_fail (CLUTTER_IS_SHADER (shader));
695
696 priv = shader->priv;
697
698 if (priv->is_enabled != enabled)
699 {
700 GError *error = NULL;
701 gboolean res;
702
703 res = clutter_shader_compile (shader, &error);
704 if (!res)
705 {
706 g_warning ("Unable to bind the shader: %s",
707 error ? error->message : "unknown error");
708 if (error)
709 g_error_free (error);
710
711 return;
712 }
713
714 priv->is_enabled = enabled;
715
716 if (priv->is_enabled)
717 cogl_program_use (priv->program);
718 else
719 cogl_program_use (COGL_INVALID_HANDLE);
720
721 g_object_notify_by_pspec (G_OBJECT (shader), obj_props[PROP_ENABLED]);
722 }
723 }
724
725 /**
726 * clutter_shader_get_is_enabled:
727 * @shader: a #ClutterShader
728 *
729 * Checks whether @shader is enabled.
730 *
731 * Return value: %TRUE if the shader is enabled.
732 *
733 * Since: 0.6
734 *
735 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
736 */
737 gboolean
738 clutter_shader_get_is_enabled (ClutterShader *shader)
739 {
740 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), FALSE);
741
742 return shader->priv->is_enabled;
743 }
744
745 /**
746 * clutter_shader_set_uniform:
747 * @shader: a #ClutterShader.
748 * @name: name of uniform in GLSL shader program to set.
749 * @value: a #ClutterShaderFloat, #ClutterShaderInt or #ClutterShaderMatrix
750 * #GValue.
751 *
752 * Sets a user configurable variable in the GLSL shader programs attached to
753 * a #ClutterShader.
754 *
755 * Since: 1.0
756 *
757 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
758 */
759 void
760 clutter_shader_set_uniform (ClutterShader *shader,
761 const gchar *name,
762 const GValue *value)
763 {
764 ClutterShaderPrivate *priv;
765 int location = 0;
766 gsize size;
767
768 g_return_if_fail (CLUTTER_IS_SHADER (shader));
769 g_return_if_fail (name != NULL);
770 g_return_if_fail (value != NULL);
771 g_return_if_fail (CLUTTER_VALUE_HOLDS_SHADER_FLOAT (value) ||
772 CLUTTER_VALUE_HOLDS_SHADER_INT (value) ||
773 CLUTTER_VALUE_HOLDS_SHADER_MATRIX (value) ||
774 G_VALUE_HOLDS_FLOAT (value) ||
775 G_VALUE_HOLDS_INT (value));
776
777 priv = shader->priv;
778 g_return_if_fail (priv->program != COGL_INVALID_HANDLE);
779
780 location = cogl_program_get_uniform_location (priv->program, name);
781
782 if (CLUTTER_VALUE_HOLDS_SHADER_FLOAT (value))
783 {
784 const float *floats;
785
786 floats = clutter_value_get_shader_float (value, &size);
787 cogl_program_set_uniform_float (priv->program,
788 location, size, 1, floats);
789 }
790 else if (CLUTTER_VALUE_HOLDS_SHADER_INT (value))
791 {
792 const int *ints;
793
794 ints = clutter_value_get_shader_int (value, &size);
795 cogl_program_set_uniform_int (priv->program,
796 location, size, 1, ints);
797 }
798 else if (CLUTTER_VALUE_HOLDS_SHADER_MATRIX (value))
799 {
800 const float *matrix;
801
802 matrix = clutter_value_get_shader_matrix (value, &size);
803 cogl_program_set_uniform_matrix (priv->program,
804 location, size, 1, FALSE, matrix);
805 }
806 else if (G_VALUE_HOLDS_FLOAT (value))
807 {
808 float float_val = g_value_get_float (value);
809
810 cogl_program_set_uniform_float (priv->program,
811 location, 1, 1, &float_val);
812 }
813 else if (G_VALUE_HOLDS_INT (value))
814 {
815 int int_val = g_value_get_int (value);
816
817 cogl_program_set_uniform_int (priv->program,
818 location, 1, 1, &int_val);
819 }
820 else
821 g_assert_not_reached ();
822 }
823
824 /**
825 * clutter_shader_get_fragment_source:
826 * @shader: a #ClutterShader
827 *
828 * Query the current GLSL fragment source set on @shader.
829 *
830 * Return value: the source of the fragment shader for this
831 * ClutterShader object or %NULL. The returned string is owned by the
832 * shader object and should never be modified or freed
833 *
834 * Since: 0.6
835 *
836 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
837 */
838 const gchar *
839 clutter_shader_get_fragment_source (ClutterShader *shader)
840 {
841 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
842
843 return clutter_shader_get_source (shader, CLUTTER_FRAGMENT_SHADER);
844 }
845
846 /**
847 * clutter_shader_get_vertex_source:
848 * @shader: a #ClutterShader
849 *
850 * Query the current GLSL vertex source set on @shader.
851 *
852 * Return value: the source of the vertex shader for this
853 * ClutterShader object or %NULL. The returned string is owned by the
854 * shader object and should never be modified or freed
855 *
856 * Since: 0.6
857 *
858 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
859 */
860 const gchar *
861 clutter_shader_get_vertex_source (ClutterShader *shader)
862 {
863 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
864
865 return clutter_shader_get_source (shader, CLUTTER_VERTEX_SHADER);
866 }
867
868 /**
869 * clutter_shader_get_cogl_program:
870 * @shader: a #ClutterShader
871 *
872 * Retrieves the underlying #CoglHandle for the shader program.
873 *
874 * Return value: (transfer none): A #CoglHandle for the shader program,
875 * or %NULL. The handle is owned by the #ClutterShader and it should
876 * not be unreferenced
877 *
878 * Since: 1.0
879 *
880 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
881 */
882 CoglHandle
883 clutter_shader_get_cogl_program (ClutterShader *shader)
884 {
885 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
886
887 return shader->priv->program;
888 }
889
890 /**
891 * clutter_shader_get_cogl_fragment_shader:
892 * @shader: a #ClutterShader
893 *
894 * Retrieves the underlying #CoglHandle for the fragment shader.
895 *
896 * Return value: (transfer none): A #CoglHandle for the fragment
897 * shader, or %NULL. The handle is owned by the #ClutterShader
898 * and it should not be unreferenced
899 *
900 * Since: 1.0
901 *
902 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
903 */
904 CoglHandle
905 clutter_shader_get_cogl_fragment_shader (ClutterShader *shader)
906 {
907 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
908
909 return clutter_shader_get_cogl_shader (shader, CLUTTER_FRAGMENT_SHADER);
910 }
911
912 /**
913 * clutter_shader_get_cogl_vertex_shader:
914 * @shader: a #ClutterShader
915 *
916 * Retrieves the underlying #CoglHandle for the vertex shader.
917 *
918 * Return value: (transfer none): A #CoglHandle for the vertex
919 * shader, or %NULL. The handle is owned by the #ClutterShader
920 * and it should not be unreferenced
921 *
922 * Since: 1.0
923 *
924 * Deprecated: 1.8: Use #ClutterShaderEffect instead.
925 */
926 CoglHandle
927 clutter_shader_get_cogl_vertex_shader (ClutterShader *shader)
928 {
929 g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
930
931 return clutter_shader_get_cogl_shader (shader, CLUTTER_VERTEX_SHADER);
932 }
933
934 GQuark
935 clutter_shader_error_quark (void)
936 {
937 return g_quark_from_static_string ("clutter-shader-error");
938 }