Refined the ClutterShader API based on a patch from Emmanuele Bassi.

* clutter/clutter-actor.c: (clutter_actor_paint),
(clutter_actor_apply_shader): queue a redraw of actor when shader is
set.
* clutter/clutter-actor.h: indentation.
* clutter/clutter-shader.[ch]: made the fragment and vertex shader
sources properties. Report error through GError when binding shaders.
* tests/test-shader.c: modified to use newer API.
This commit is contained in:
Øyvind Kolås 2007-12-11 14:49:41 +00:00
parent 6e61c0a999
commit 5220f68e17
6 changed files with 703 additions and 362 deletions

View File

@ -1,3 +1,15 @@
2007-12-11 Øyvind Kolås <pippin@o-hand.com>
Refined the ClutterShader API based on a patch from Emmanuele Bassi.
* clutter/clutter-actor.c: (clutter_actor_paint),
(clutter_actor_apply_shader): queue a redraw of actor when shader is
set.
* clutter/clutter-actor.h: indentation.
* clutter/clutter-shader.[ch]: made the fragment and vertex shader
sources properties. Report error through GError when binding shaders.
* tests/test-shader.c: modified to use newer API.
2007-12-10 Emmanuele Bassi <ebassi@openedhand.com> 2007-12-10 Emmanuele Bassi <ebassi@openedhand.com>
* clutter/clutter-model.c: * clutter/clutter-model.c:

View File

@ -928,8 +928,10 @@ clutter_actor_paint (ClutterActor *self)
else else
{ {
clutter_actor_shader_pre_paint (self, FALSE); clutter_actor_shader_pre_paint (self, FALSE);
if (G_LIKELY (klass->paint)) if (G_LIKELY (klass->paint))
(klass->paint) (self); klass->paint (self);
clutter_actor_shader_post_paint (self); clutter_actor_shader_post_paint (self);
} }
@ -4766,6 +4768,8 @@ clutter_actor_apply_shader (ClutterActor *self,
if (shader) if (shader)
shader_data->shader = g_object_ref (shader); shader_data->shader = g_object_ref (shader);
clutter_actor_queue_redraw (self);
return TRUE; return TRUE;
} }
@ -4802,7 +4806,7 @@ clutter_actor_shader_pre_paint (ClutterActor *actor,
if (shader) if (shader)
{ {
clutter_shader_enable (shader); clutter_shader_set_is_enabled (shader, TRUE);
g_hash_table_foreach (shader_data->float1f_hash, set_each_param, shader); g_hash_table_foreach (shader_data->float1f_hash, set_each_param, shader);
@ -4832,7 +4836,7 @@ clutter_actor_shader_post_paint (ClutterActor *actor)
if (shader) if (shader)
{ {
clutter_shader_disable (shader); clutter_shader_set_is_enabled (shader, FALSE);
context->shaders = g_slist_remove (context->shaders, actor); context->shaders = g_slist_remove (context->shaders, actor);
if (context->shaders) if (context->shaders)

View File

@ -400,7 +400,6 @@ gboolean clutter_actor_should_pick_paint (ClutterActor *self);
gboolean clutter_actor_apply_shader (ClutterActor *self, gboolean clutter_actor_apply_shader (ClutterActor *self,
ClutterShader *shader); ClutterShader *shader);
void clutter_actor_set_shader_param (ClutterActor *self, void clutter_actor_set_shader_param (ClutterActor *self,
const gchar *param, const gchar *param,
gfloat value); gfloat value);

View File

@ -24,44 +24,46 @@
* Boston, MA 02111-1307, USA. * Boston, MA 02111-1307, USA.
*/ */
#include "clutter.h" #ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <cogl/cogl.h>
#include "clutter-debug.h"
#include "clutter-private.h" #include "clutter-private.h"
#include "clutter-shader.h" #include "clutter-shader.h"
#include <unistd.h> static GList *clutter_shaders_list = NULL;
#include <glib.h>
#include <cogl/cogl.h>
#include <string.h>
#include <stdlib.h>
#define CLUTTER_SHADER_GET_PRIVATE(obj) \ #define CLUTTER_SHADER_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), \ (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
CLUTTER_TYPE_SHADER, ClutterShaderPrivate)) CLUTTER_TYPE_SHADER, ClutterShaderPrivate))
static GList *shader_list; typedef enum {
CLUTTER_VERTEX_SHADER,
static void clutter_shader_class_init (ClutterShaderClass *klass); CLUTTER_FRAGMENT_SHADER
static void clutter_shader_init (ClutterShader *sp); } ClutterShaderType;
static void clutter_shader_finalize (GObject *object);
static GObject *clutter_shader_constructor (GType type,
guint n_params,
GObjectConstructParam *params);
static void clutter_shader_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void clutter_shader_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
struct _ClutterShaderPrivate struct _ClutterShaderPrivate
{ {
gboolean glsl; /* The shader is a GLSL shader */ guint bound : 1; /* The shader is bound to the GL context */
gboolean bound; /* The shader is bound to the GL context */ guint is_enabled : 1;
gchar *vertex_shader_source; /* source (or asm) for vertex shader */ guint vertex_is_glsl : 1;
gchar *fragment_shader_source;/* source (or asm) for fragment shader*/ guint fragment_is_glsl : 1;
gchar *vertex_source; /* source (or asm) for vertex shader */
gchar *fragment_source; /* source (or asm) for fragment shader */
COGLint program; COGLint program;
@ -72,146 +74,19 @@ struct _ClutterShaderPrivate
enum enum
{ {
PROP_0, PROP_0,
PROP_VERTEX_SOURCE, PROP_VERTEX_SOURCE,
PROP_FRAGMENT_SOURCE PROP_FRAGMENT_SOURCE,
PROP_BOUND,
PROP_ENABLED
}; };
G_DEFINE_TYPE (ClutterShader, clutter_shader, G_TYPE_OBJECT); G_DEFINE_TYPE (ClutterShader, clutter_shader, G_TYPE_OBJECT);
static void G_CONST_RETURN gchar *clutter_shader_get_source (ClutterShader *shader,
clutter_shader_class_init (ClutterShaderClass *klass) ClutterShaderType type);
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
clutter_shader_parent_class = g_type_class_peek_parent (klass);
object_class->finalize = clutter_shader_finalize;
object_class->set_property = clutter_shader_set_property;
object_class->get_property = clutter_shader_get_property;
object_class->constructor = clutter_shader_constructor;
g_type_class_add_private (klass, sizeof (ClutterShaderPrivate));
g_object_class_install_property (object_class,
PROP_VERTEX_SOURCE,
g_param_spec_string ("vertex-source",
"Vertex Source",
"Source of vertex shader",
NULL,
CLUTTER_PARAM_READWRITE|
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_FRAGMENT_SOURCE,
g_param_spec_string ("fragment-source",
"Fragment Source",
"Source of fragment shader",
NULL,
CLUTTER_PARAM_READWRITE|
G_PARAM_CONSTRUCT_ONLY));
}
static void
clutter_shader_init (ClutterShader *self)
{
ClutterShaderPrivate *priv;
priv = self->priv = CLUTTER_SHADER_GET_PRIVATE (self);
priv->glsl = FALSE;
priv->bound = FALSE;
priv->vertex_shader_source = NULL;
priv->fragment_shader_source = NULL;
priv->program = 0;
priv->vertex_shader = 0;
priv->fragment_shader = 0;
}
static gboolean bind_glsl_shader (ClutterShader *self)
{
ClutterShaderPrivate *priv;
priv = self->priv;
cogl_enable (CGL_FRAGMENT_SHADER);
cogl_enable (CGL_VERTEX_SHADER);
priv->glsl = TRUE;
priv->program = cogl_create_program ();
if (priv->vertex_shader_source)
{
priv->vertex_shader = cogl_create_shader (CGL_VERTEX_SHADER);
cogl_shader_source (priv->vertex_shader, priv->vertex_shader_source);
cogl_shader_compile (priv->vertex_shader);
cogl_program_attach_shader (priv->program, priv->vertex_shader);
}
if (priv->fragment_shader_source)
{
GLint compiled = CGL_FALSE;
priv->fragment_shader = cogl_create_shader (CGL_FRAGMENT_SHADER);
cogl_shader_source (priv->fragment_shader, priv->fragment_shader_source);
cogl_shader_compile (priv->fragment_shader);
cogl_shader_get_parameteriv (priv->fragment_shader,
CGL_OBJECT_COMPILE_STATUS,
&compiled);
if (compiled != CGL_TRUE)
{
gchar *buffer;
gint max_length = 512;
buffer = g_malloc (max_length);
cogl_shader_get_info_log (priv->fragment_shader, max_length, buffer);
g_print ("Shader compilation failed:\n%s", buffer);
g_free (buffer);
g_object_unref (self);
return FALSE;
}
cogl_program_attach_shader (priv->program, priv->fragment_shader);
}
cogl_program_link (priv->program);
return TRUE;
}
gboolean
clutter_shader_bind (ClutterShader *self)
{
ClutterShaderPrivate *priv;
priv = self->priv;
if (priv->bound)
return priv->bound;
if (priv->glsl)
{
priv->bound = bind_glsl_shader (self);
}
return priv->bound;
}
void
clutter_shader_release (ClutterShader *self)
{
ClutterShaderPrivate *priv;
priv = self->priv;
if (!priv->bound)
return;
g_assert (priv->program);
if (priv->glsl)
{
if (priv->vertex_shader)
cogl_shader_destroy (priv->vertex_shader);
if (priv->fragment_shader)
cogl_shader_destroy (priv->fragment_shader);
if (priv->program)
cogl_program_destroy (priv->program);
priv->vertex_shader = 0;
priv->fragment_shader = 0;
priv->program = 0;
}
priv->bound = FALSE;
}
static void static void
clutter_shader_finalize (GObject *object) clutter_shader_finalize (GObject *object)
@ -224,85 +99,32 @@ clutter_shader_finalize (GObject *object)
clutter_shader_release (shader); clutter_shader_release (shader);
shader_list = g_list_remove (shader_list, object); clutter_shaders_list = g_list_remove (clutter_shaders_list, object);
if (priv->fragment_shader_source) g_free (priv->fragment_source);
g_free (priv->fragment_shader_source); g_free (priv->vertex_source);
if (priv->vertex_shader_source)
g_free (priv->vertex_shader_source);
G_OBJECT_CLASS (clutter_shader_parent_class)->finalize (object); G_OBJECT_CLASS (clutter_shader_parent_class)->finalize (object);
} }
void
clutter_shader_enable (ClutterShader *self)
{
ClutterShaderPrivate *priv = self->priv;
clutter_shader_bind (self);
cogl_program_use (priv->program);
}
void
clutter_shader_disable (ClutterShader *self)
{
cogl_program_use (0);
}
void
clutter_shader_set_uniform_1f (ClutterShader *self,
const gchar *name,
gfloat value)
{
ClutterShaderPrivate *priv = self->priv;
GLint location = 0;
GLfloat foo = value;
location =cogl_program_get_uniform_location (priv->program, name);
cogl_program_uniform_1f (location, foo);
}
void
clutter_shader_release_all (void)
{
GList *iter;
for (iter = shader_list; iter; iter = g_list_next (iter))
{
clutter_shader_release (iter->data);
}
}
static void static void
clutter_shader_set_property (GObject *object, clutter_shader_set_property (GObject *object,
guint prop_id, guint prop_id,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
ClutterShader *shader; ClutterShader *shader = CLUTTER_SHADER(object);
ClutterShaderPrivate *priv;
shader = CLUTTER_SHADER(object);
priv = shader->priv;
switch (prop_id) switch (prop_id)
{ {
case PROP_VERTEX_SOURCE: case PROP_VERTEX_SOURCE:
if (priv->vertex_shader_source) clutter_shader_set_vertex_source (shader, g_value_get_string (value), -1);
{
g_free (priv->vertex_shader_source);
priv->vertex_shader_source = NULL;
}
priv->vertex_shader_source = g_value_dup_string (value);
break; break;
case PROP_FRAGMENT_SOURCE: case PROP_FRAGMENT_SOURCE:
if (priv->fragment_shader_source) clutter_shader_set_fragment_source (shader, g_value_get_string (value), -1);
{ break;
g_free (priv->fragment_shader_source); case PROP_ENABLED:
priv->fragment_shader_source = NULL; clutter_shader_set_is_enabled (shader, g_value_get_boolean (value));
}
priv->fragment_shader_source = g_value_dup_string (value);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -325,10 +147,16 @@ clutter_shader_get_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_VERTEX_SOURCE: case PROP_VERTEX_SOURCE:
g_value_set_string (value, priv->vertex_shader_source); g_value_set_string (value, priv->vertex_source);
break; break;
case PROP_FRAGMENT_SOURCE: case PROP_FRAGMENT_SOURCE:
g_value_set_string (value, priv->fragment_shader_source); g_value_set_string (value, priv->fragment_source);
break;
case PROP_BOUND:
g_value_set_boolean (value, priv->bound);
break;
case PROP_ENABLED:
g_value_set_boolean (value, priv->is_enabled);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -342,93 +170,528 @@ clutter_shader_constructor (GType type,
GObjectConstructParam *params) GObjectConstructParam *params)
{ {
GObject *object; GObject *object;
ClutterShader *shader;
ClutterShaderPrivate *priv;
object = G_OBJECT_CLASS (clutter_shader_parent_class)->constructor ( object = G_OBJECT_CLASS (clutter_shader_parent_class)->constructor (type, n_params, params);
type, n_params, params);
shader = CLUTTER_SHADER (object);
priv = shader->priv;
priv->glsl = !((priv->vertex_shader_source && /* add this instance to the global list of shaders */
g_str_has_prefix (priv->vertex_shader_source, "!!ARBvp")) || clutter_shaders_list = g_list_prepend (clutter_shaders_list, object);
(priv->fragment_shader_source &&
g_str_has_prefix (priv->fragment_shader_source, "!!ARBfp")));
if (!priv->glsl)
{
g_warning ("ASM shader support not available");
g_object_unref (object);
return NULL;
}
if (priv->glsl && !clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
{
g_warning ("GLSL shaders not supported\n");
g_object_unref (object);
return NULL;
}
shader_list = g_list_prepend (shader_list, object);
return object; return object;
} }
ClutterShader *
clutter_shader_new_from_strings (const gchar *vertex_shader_program, static void
const gchar *fragment_shader_program) clutter_shader_class_init (ClutterShaderClass *klass)
{ {
/* evil hack, since g_object_new would interpret a NULL passed as GObjectClass *object_class = G_OBJECT_CLASS (klass);
* a argument termination
object_class->finalize = clutter_shader_finalize;
object_class->set_property = clutter_shader_set_property;
object_class->get_property = clutter_shader_get_property;
object_class->constructor = clutter_shader_constructor;
g_type_class_add_private (klass, sizeof (ClutterShaderPrivate));
/**
* ClutterShader:vertex-source:
*
* FIXME
*
* Since: 0.6
*/ */
if (vertex_shader_program && g_object_class_install_property (object_class,
fragment_shader_program) PROP_VERTEX_SOURCE,
return g_object_new (CLUTTER_TYPE_SHADER, g_param_spec_string ("vertex-source",
"vertex-source", vertex_shader_program, "Vertex Source",
"fragment-source", fragment_shader_program, "Source of vertex shader",
NULL); NULL,
else if (fragment_shader_program) CLUTTER_PARAM_READWRITE));
return g_object_new (CLUTTER_TYPE_SHADER, /**
"fragment-source", fragment_shader_program, * ClutterShader:fragment-source:
NULL); *
else if (vertex_shader_program) * FIXME
return g_object_new (CLUTTER_TYPE_SHADER, *
"vertex-source", vertex_shader_program, * Since: 0.6
NULL); */
else { g_object_class_install_property (object_class,
g_warning ("neither fragment nor vertex shader provided"); PROP_FRAGMENT_SOURCE,
return NULL; g_param_spec_string ("fragment-source",
} "Fragment Source",
"Source of fragment shader",
NULL,
CLUTTER_PARAM_READWRITE));
/**
* ClutterShader:bound:
*
* FIXME
*
* Since: 0.6
*/
g_object_class_install_property (object_class,
PROP_BOUND,
g_param_spec_boolean ("bound",
"Bound",
"Whether the shader is bound",
FALSE,
CLUTTER_PARAM_READABLE));
/**
* ClutterShader:enabled:
*
* FIXME
*
* Since: 0.6
*/
g_object_class_install_property (object_class,
PROP_ENABLED,
g_param_spec_boolean ("enabled",
"Enabled",
"Whether the shader is enabled",
FALSE,
CLUTTER_PARAM_READWRITE));
} }
static void
clutter_shader_init (ClutterShader *self)
{
ClutterShaderPrivate *priv;
priv = self->priv = CLUTTER_SHADER_GET_PRIVATE (self);
priv->bound = FALSE;
priv->vertex_source = NULL;
priv->fragment_source = NULL;
priv->program = 0;
priv->vertex_shader = 0;
priv->fragment_shader = 0;
}
/**
* clutter_shader_new:
*
* FIXME
*
* Return value: FIXME
*
* Since: 0.6
*/
ClutterShader * ClutterShader *
clutter_shader_new_from_files (const gchar *vertex_file, clutter_shader_new (void)
const gchar *fragment_file)
{ {
ClutterShader *shader; return g_object_new (CLUTTER_TYPE_SHADER, NULL);
gchar *vertex_shader_program = NULL; }
gchar *fragment_shader_program = NULL;
g_assert (vertex_file != NULL ||
fragment_file != NULL);
if (vertex_file) /**
* clutter_shader_set_fragment_source:
* @shader: a #ClutterShader
* @data: FIXME
* @length: FIXME (currently ignored)
*
* FIXME
*
*
* Since: 0.6
*/
void
clutter_shader_set_fragment_source (ClutterShader *shader,
const gchar *data,
gssize length)
{ {
g_file_get_contents (vertex_file, &vertex_shader_program, ClutterShaderPrivate *priv;
NULL, NULL); gboolean is_glsl;
}
if (fragment_file)
{
g_file_get_contents (fragment_file, &fragment_shader_program,
NULL, NULL);
}
shader = clutter_shader_new_from_strings (vertex_shader_program,
fragment_shader_program);
if (vertex_shader_program) if (shader == NULL)
g_error ("quack!");
g_return_if_fail (CLUTTER_IS_SHADER (shader));
g_return_if_fail (data != NULL);
priv = shader->priv;
/* release shader if bound when changing the source, the shader will
* automatically be rebound on the next use.
*/
if (clutter_shader_is_bound (shader))
clutter_shader_release (shader);
is_glsl = !g_str_has_prefix (data, "!!ARBfp");
if (priv->fragment_source)
{ {
g_free (vertex_shader_program); g_free (priv->fragment_source);
} }
if (fragment_shader_program)
CLUTTER_NOTE (SHADER, "setting fragment shader (GLSL:%s, len:%d)",
is_glsl ? "yes" : "no",
length);
priv->fragment_source = g_strdup (data);
priv->fragment_is_glsl = is_glsl;
}
/**
* clutter_shader_set_vertex_source:
* @shader: a #ClutterShader
* @data: FIXME
* @length: FIXME (currently ignored)
*
* FIXME
*
* Since: 0.6
*/
void
clutter_shader_set_vertex_source (ClutterShader *shader,
const gchar *data,
gssize length)
{ {
g_free (fragment_shader_program); ClutterShaderPrivate *priv;
gboolean is_glsl;
if (shader == NULL)
g_error ("quack!");
g_return_if_fail (CLUTTER_IS_SHADER (shader));
g_return_if_fail (data != NULL);
priv = shader->priv;
/* release shader if bound when changing the source, the shader will
* automatically be rebound on the next use.
*/
if (clutter_shader_is_bound (shader))
clutter_shader_release (shader);
is_glsl = !g_str_has_prefix (data, "!!ARBvp");
if (priv->vertex_source)
{
g_free (priv->vertex_source);
} }
return shader;
CLUTTER_NOTE (SHADER, "setting vertex shader (GLSL:%s, len:%d)",
is_glsl ? "yes" : "no",
length);
priv->vertex_source = g_strdup (data);
priv->vertex_is_glsl = is_glsl;
}
static gboolean
bind_glsl_shader (ClutterShader *self,
GError **error)
{
ClutterShaderPrivate *priv;
priv = self->priv;
cogl_enable (CGL_FRAGMENT_SHADER);
cogl_enable (CGL_VERTEX_SHADER);
priv->program = cogl_create_program ();
if (priv->vertex_is_glsl && priv->vertex_source)
{
priv->vertex_shader = cogl_create_shader (CGL_VERTEX_SHADER);
cogl_shader_source (priv->vertex_shader, priv->vertex_source);
cogl_shader_compile (priv->vertex_shader);
cogl_program_attach_shader (priv->program, priv->vertex_shader);
}
if (priv->fragment_is_glsl && priv->fragment_source)
{
GLint compiled = CGL_FALSE;
priv->fragment_shader = cogl_create_shader (CGL_FRAGMENT_SHADER);
cogl_shader_source (priv->fragment_shader, priv->fragment_source);
cogl_shader_compile (priv->fragment_shader);
cogl_shader_get_parameteriv (priv->fragment_shader,
CGL_OBJECT_COMPILE_STATUS,
&compiled);
if (compiled != CGL_TRUE)
{
gchar error_buf[512];
cogl_shader_get_info_log (priv->fragment_shader, 512, error_buf);
g_set_error (error, CLUTTER_SHADER_ERROR,
CLUTTER_SHADER_ERROR_COMPILE,
"Shader compilation failed: %s",
error_buf);
return FALSE;
}
else
cogl_program_attach_shader (priv->program, priv->fragment_shader);
}
cogl_program_link (priv->program);
return TRUE;
}
/**
* clutter_shader_bind:
* @shader: a #ClutterShader
*
* FIXME
*
* Return value: FIXME
*
* Since: 0.6
*/
gboolean
clutter_shader_bind (ClutterShader *shader,
GError **error)
{
ClutterShaderPrivate *priv;
g_return_val_if_fail (CLUTTER_IS_SHADER (shader), FALSE);
priv = shader->priv;
if (priv->bound)
return priv->bound;
if ((priv->vertex_source && !priv->vertex_is_glsl) ||
(priv->fragment_source && !priv->fragment_is_glsl))
{
/* XXX: maybe this error message should be about only GLSL
shaders supportes as of now
*/
g_set_error (error, CLUTTER_SHADER_ERROR,
CLUTTER_SHADER_ERROR_NO_ASM,
"ASM shaders not supported");
priv->bound = FALSE;
return priv->bound;
}
if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
{
g_set_error (error, CLUTTER_SHADER_ERROR,
CLUTTER_SHADER_ERROR_NO_GLSL,
"GLSL shaders not supported");
priv->bound = FALSE;
return priv->bound;
}
priv->bound = bind_glsl_shader (shader, error);
if (priv->bound)
g_object_notify (G_OBJECT (shader), "bound");
return priv->bound;
}
/**
* clutter_shader_release:
* @shader: a #ClutterShader
*
* FIXME
*
* Since: 0.6
*/
void
clutter_shader_release (ClutterShader *shader)
{
ClutterShaderPrivate *priv;
g_return_if_fail (CLUTTER_IS_SHADER (shader));
priv = shader->priv;
if (!priv->bound)
return;
g_assert (priv->program);
if (priv->vertex_is_glsl && priv->vertex_shader)
cogl_shader_destroy (priv->vertex_shader);
if (priv->fragment_is_glsl && priv->fragment_shader)
cogl_shader_destroy (priv->fragment_shader);
if (priv->program)
cogl_program_destroy (priv->program);
priv->vertex_shader = 0;
priv->fragment_shader = 0;
priv->program = 0;
priv->bound = FALSE;
g_object_notify (G_OBJECT (shader), "bound");
}
/**
* clutter_shader_is_bound:
* @shader: a #ClutterShader
*
* FIXME
*
* Return value: FIXME
*
* Since: 0.6
*/
gboolean
clutter_shader_is_bound (ClutterShader *shader)
{
g_return_val_if_fail (CLUTTER_IS_SHADER (shader), FALSE);
return shader->priv->bound;
}
/**
* clutter_shader_set_is_enabled:
* @shader: a #ClutterShader
* @enabled: FIXME
*
* FIXME
*
* Since: 0.6
*/
void
clutter_shader_set_is_enabled (ClutterShader *shader,
gboolean enabled)
{
ClutterShaderPrivate *priv;
g_return_if_fail (CLUTTER_IS_SHADER (shader));
priv = shader->priv;
if (priv->is_enabled != enabled)
{
GError *error = NULL;
gboolean res;
res = clutter_shader_bind (shader, &error);
if (!res)
{
g_warning ("Unable to bind the shader: %s",
error ? error->message : "unknown error");
if (error)
g_error_free (error);
return;
}
priv->is_enabled = enabled;
if (priv->is_enabled)
cogl_program_use (priv->program);
else
cogl_program_use (0);
g_object_notify (G_OBJECT (shader), "enabled");
}
}
/**
* clutter_shader_get_is_enabled:
* @shader: a #ClutterShader
*
* FIXME
*
* Return value: FIXME
*
* Since: 0.6
*/
gboolean
clutter_shader_get_is_enabled (ClutterShader *shader)
{
g_return_val_if_fail (CLUTTER_IS_SHADER (shader), FALSE);
return shader->priv->is_enabled;
}
/**
* clutter_shader_set_uniform_1f:
* @shader: a #ClutterShader
* @name: FIXME
* @value: FIXME
*
* FIXME
*
* Since: 0.6
*/
void
clutter_shader_set_uniform_1f (ClutterShader *shader,
const gchar *name,
gfloat value)
{
ClutterShaderPrivate *priv;
GLint location = 0;
GLfloat foo = value;
g_return_if_fail (CLUTTER_IS_SHADER (shader));
priv = shader->priv;
location = cogl_program_get_uniform_location (priv->program, name);
cogl_program_uniform_1f (location, foo);
}
/**
* clutter_shader_release_all:
*
* FIXME
*
* Since: 0.6
*/
void
clutter_shader_release_all (void)
{
g_list_foreach (clutter_shaders_list,
(GFunc) clutter_shader_release,
NULL);
}
/**
* clutter_shader_get_fragment_source:
* @shader: a #ClutterShader
*
* FIXME
*
* Return value: the source of the fragment shader for this ClutterShader object
* or %NULL. The returned string is owned by the shader object and should never
* be modified or freed
*
* Since: 0.6
*/
G_CONST_RETURN gchar *
clutter_shader_get_fragment_source (ClutterShader *shader)
{
g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
return shader->priv->fragment_source;
}
/**
* clutter_shader_get_vertex_source:
* @shader: a #ClutterShader
*
* FIXME
*
* Return value: the source of the vertex shader for this ClutterShader object
* or %NULL. The returned string is owned by the shader object and should never
* be modified or freed
*
* Since: 0.6
*/
G_CONST_RETURN gchar *
clutter_shader_get_vertex_source (ClutterShader *shader)
{
g_return_val_if_fail (CLUTTER_IS_SHADER (shader), NULL);
return shader->priv->vertex_source;
}
GQuark
clutter_shader_error_quark (void)
{
return g_quark_from_static_string ("clutter-shader-error");
} }

View File

@ -25,10 +25,9 @@
*/ */
#ifndef CLUTTER_SHADER_H #ifndef __CLUTTER_SHADER_H__
#define CLUTTER_SHADER_H #define __CLUTTER_SHADER_H__
#include <glib.h>
#include <glib-object.h> #include <glib-object.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -40,39 +39,61 @@ G_BEGIN_DECLS
#define CLUTTER_IS_SHADER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), CLUTTER_TYPE_SHADER)) #define CLUTTER_IS_SHADER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), CLUTTER_TYPE_SHADER))
#define CLUTTER_SHADER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), CLUTTER_TYPE_SHADER, ClutterShaderClass)) #define CLUTTER_SHADER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), CLUTTER_TYPE_SHADER, ClutterShaderClass))
#define CLUTTER_SHADER_ERROR (clutter_shader_error_quark ())
typedef enum {
CLUTTER_SHADER_ERROR_NO_ASM,
CLUTTER_SHADER_ERROR_NO_GLSL,
CLUTTER_SHADER_ERROR_COMPILE
} ClutterShaderError;
typedef struct _ClutterShader ClutterShader; typedef struct _ClutterShader ClutterShader;
typedef struct _ClutterShaderPrivate ClutterShaderPrivate; typedef struct _ClutterShaderPrivate ClutterShaderPrivate;
typedef struct _ClutterShaderClass ClutterShaderClass; typedef struct _ClutterShaderClass ClutterShaderClass;
struct _ClutterShader struct _ClutterShader
{ {
/*< private >*/
GObject parent; GObject parent;
ClutterShaderPrivate *priv; ClutterShaderPrivate *priv;
}; };
struct _ClutterShaderClass struct _ClutterShaderClass
{ {
/*< private >*/
GObjectClass parent_class; GObjectClass parent_class;
}; };
GType clutter_shader_get_type (); GQuark clutter_shader_error_quark (void);
GType clutter_shader_get_type (void) G_GNUC_CONST;
ClutterShader * clutter_shader_new_from_files (const gchar *vertex_file, ClutterShader * clutter_shader_new (void);
const gchar *fragment_file);
ClutterShader * clutter_shader_new_from_strings (const gchar *vertex_file,
const gchar *fragment_file);
void clutter_shader_enable (ClutterShader *self);
void clutter_shader_disable (ClutterShader *self);
gboolean clutter_shader_bind (ClutterShader *self); void clutter_shader_set_is_enabled (ClutterShader *shader,
void clutter_shader_release (ClutterShader *self); gboolean enabled);
void clutter_shader_set_uniform_1f (ClutterShader *self, gboolean clutter_shader_get_is_enabled (ClutterShader *shader);
gboolean clutter_shader_bind (ClutterShader *shader,
GError **error);
void clutter_shader_release (ClutterShader *shader);
gboolean clutter_shader_is_bound (ClutterShader *shader);
void clutter_shader_set_vertex_source (ClutterShader *shader,
const gchar *data,
gssize length);
void clutter_shader_set_fragment_source (ClutterShader *shader,
const gchar *data,
gssize length);
G_CONST_RETURN gchar *clutter_shader_get_vertex_source (ClutterShader *shader);
G_CONST_RETURN gchar *clutter_shader_get_fragment_source (ClutterShader *shader);
void clutter_shader_set_uniform_1f (ClutterShader *shader,
const gchar *name, const gchar *name,
gfloat value); gfloat value);
/* should be private and internal */ /* should be private and internal */
void clutter_shader_release_all (void); void clutter_shader_release_all (void);
G_END_DECLS G_END_DECLS
#endif /* CLUTTER_SHADER_H */ #endif /* __CLUTTER_SHADER_H__ */

View File

@ -1,4 +1,4 @@
/* #define TEST_GROUP 1 */ /*#define TEST_GROUP */
#include <clutter/clutter.h> #include <clutter/clutter.h>
@ -128,6 +128,7 @@ static ShaderSource shaders[]=
}; };
static gint shader_no = 0; static gint shader_no = 0;
ClutterShader *shader;
static gboolean static gboolean
button_release_cb (ClutterActor *actor, button_release_cb (ClutterActor *actor,
@ -135,6 +136,7 @@ button_release_cb (ClutterActor *actor,
gpointer data) gpointer data)
{ {
gint new_no; gint new_no;
if (event->button.button == 1) if (event->button.button == 1)
{ {
new_no = shader_no - 1; new_no = shader_no - 1;
@ -147,14 +149,38 @@ button_release_cb (ClutterActor *actor,
if (new_no >= 0 && shaders[new_no].name) if (new_no >= 0 && shaders[new_no].name)
{ {
ClutterShader *shader; ClutterShader *shader;
GError *error;
shader_no = new_no; shader_no = new_no;
g_print ("setting shaders[%i] named '%s'\n", shader_no, shaders[shader_no].name); g_print ("setting shaders[%i] named '%s'\n",
shader = clutter_shader_new_from_strings (NULL, shaders[shader_no].source); shader_no,
clutter_actor_apply_shader (actor, shader); shaders[shader_no].name);
shader = clutter_shader_new ();
error = NULL;
g_object_set (G_OBJECT (shader), "fragment-source", shaders[shader_no].source, NULL);
/* try to bind the shader, provoking an error we catch if there is issues
* with the shader sources we've provided. At a later stage it should be possible to
* iterate through a set of alternate shader sources (glsl -> asm -> cg?) and the one
* that succesfully compiles is used.
*/
clutter_shader_bind (shader, &error);
if (error)
{
g_print ("unable to set shaders[%i] named '%s': %s",
shader_no, shaders[shader_no].name,
error->message);
g_error_free (error);
clutter_actor_apply_shader (actor, NULL);
}
else
{
clutter_actor_apply_shader (actor, shader);
clutter_actor_set_shader_param (actor, "radius", 3.0); clutter_actor_set_shader_param (actor, "radius", 3.0);
clutter_redraw(); }
} }
return FALSE; return FALSE;
} }
@ -171,21 +197,35 @@ main (gint argc,
ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff }; ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff };
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
GError *error; GError *error;
ClutterShader *shader;
error = NULL;
clutter_init (&argc, &argv); clutter_init (&argc, &argv);
g_print ("applying shaders[%i] named '%s'\n", shader_no, shaders[shader_no].name);
shader = clutter_shader_new_from_strings (NULL, shaders[shader_no].source);
stage = clutter_stage_get_default (); stage = clutter_stage_get_default ();
clutter_actor_set_size (stage, 512, 384); clutter_actor_set_size (stage, 512, 384);
pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL); g_print ("applying shaders[%i] named '%s'\n",
shader_no,
shaders[shader_no].name);
shader = clutter_shader_new ();
error = NULL;
clutter_shader_set_fragment_source (shader, shaders[shader_no].source, NULL);
clutter_shader_bind (shader, &error);
if (error)
{
g_print ("unable to load shaders[%d] named '%s': %s\n",
shader_no,
shaders[shader_no].name,
error->message);
g_error_free (error);
return EXIT_FAILURE;
}
pixbuf = gdk_pixbuf_new_from_file ("redhand.png", &error);
if (!pixbuf) if (!pixbuf)
g_error("pixbuf load failed"); g_error("pixbuf load failed: %s", error ? error->message : "Unknown");
clutter_stage_set_title (CLUTTER_STAGE (stage), "Shader Test"); clutter_stage_set_title (CLUTTER_STAGE (stage), "Shader Test");
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
@ -218,14 +258,16 @@ main (gint argc,
clutter_group_add (CLUTTER_GROUP (actor), child2); clutter_group_add (CLUTTER_GROUP (actor), child2);
clutter_group_add (CLUTTER_GROUP (actor), child3); clutter_group_add (CLUTTER_GROUP (actor), child3);
clutter_group_add (CLUTTER_GROUP (actor), child4); clutter_group_add (CLUTTER_GROUP (actor), child4);
clutter_actor_show_all (actor); clutter_actor_show_all (actor);
} }
#endif #endif
clutter_actor_apply_shader (actor, shader);
clutter_actor_set_position (actor, 100, 100); clutter_actor_set_position (actor, 100, 100);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
clutter_actor_apply_shader (actor, shader);
clutter_actor_set_shader_param (actor, "brightness", 0.4); clutter_actor_set_shader_param (actor, "brightness", 0.4);
clutter_actor_set_shader_param (actor, "contrast", -1.9); clutter_actor_set_shader_param (actor, "contrast", -1.9);
@ -242,5 +284,5 @@ main (gint argc,
clutter_main (); clutter_main ();
return 0; return EXIT_SUCCESS;
} }