Merge cogl-program-{gl,gles}.c into one cogl-program.c

This merges the two implementations of CoglProgram for the GLES2 and
GL backends into one. The implementation is more like the GLES2
version which would track the uniform values and delay sending them to
GL. CoglProgram is now effectively just a GList of CoglShaders along
with an array of stored uniform values. CoglProgram never actually
creates a GL program, instead this is left up to the GLSL material
backend. This is necessary on GLES2 where we may need to relink the
user's program with different generated shaders depending on the other
emulated fixed function state. It will also be necessary in the future
GLSL backends for regular OpenGL. The GLSL and ARBfp material backends
are now the ones that create and link the GL program from the list of
shaders. The linked program is attached to the private material state
so that it can be reused if the CoglProgram is used again with the
same material. This does mean the program will get relinked if the
shader is used with multiple materials. This will be particularly bad
if the legacy cogl_program_use function is used because that
effectively always makes one-shot materials. This problem will
hopefully be alleviated if we make a hash table with a cache of
generated programs. The cogl program would then need to become part of
the hash lookup.

Each CoglProgram now has an age counter which is incremented every
time a shader is added. This is used by the material backends to
detect when we need to create a new GL program for the user program.

The internal _cogl_use_program function now takes a GL program handle
rather than a CoglProgram. It no longer needs any special differences
for GLES2. The GLES2 wrapper function now also uses this function to
bind its generated shaders.

The ARBfp shaders no longer store a copy of the program source but
instead just directly create a program object when cogl_shader_source
is called. This avoids having to reupload the source if the same
shader is used in multiple materials.

There are currently a few gross hacks to get the GLES2 backend to work
with this. The problem is that the GLSL material backend is now
generating a complete GL program but the GLES2 wrapper still needs to
add its fixed function emulation shaders if the program doesn't
provide either a vertex or fragment shader. There is a new function in
the GLES2 wrapper called _cogl_gles2_use_program which replaces the
previous cogl_program_use implementation. It extracts the GL shaders
from the GL program object and creates a new GL program containing all
of the shaders plus its fixed function emulation. This new program is
returned to the GLSL material backend so that it can still flush the
custom uniforms using it. The user_program is attached to the GLES2
settings struct as before but its stored using a GL program handle
rather than a CoglProgram pointer. This hack will go away once the
GLSL material backend replaces the GLES2 wrapper by generating the
code itself.

Under Mesa this currently generates some GL errors when glClear is
called in test-cogl-shader-glsl. I think this is due to a bug in Mesa
however. When the user program on the material is changed the GLSL
backend gets notified and deletes the GL program that it linked from
the user shaders. The program will still be bound in GL
however. Leaving a deleted shader bound exposes a bug in Mesa's
glClear implementation. More details are here:

https://bugs.freedesktop.org/show_bug.cgi?id=31194
This commit is contained in:
Neil Roberts 2010-10-15 18:00:29 +01:00
parent 7ab928a4e0
commit 63206a208b
18 changed files with 1313 additions and 1330 deletions

View File

@ -84,8 +84,6 @@ cogl_driver_sources += \
$(srcdir)/driver/gl/cogl-context-driver-gl.h \
$(srcdir)/driver/gl/cogl-feature-functions-gl.h \
$(srcdir)/driver/gl/cogl-gl.c \
$(srcdir)/driver/gl/cogl-program-gl.c \
$(srcdir)/driver/gl/cogl-program-gl.h \
$(srcdir)/driver/gl/cogl-texture-driver-gl.c \
$(NULL)
endif
@ -96,8 +94,6 @@ cogl_driver_sources += \
$(srcdir)/driver/gles/cogl-context-driver-gles.h \
$(srcdir)/driver/gles/cogl-feature-functions-gles.h \
$(srcdir)/driver/gles/cogl-gles.c \
$(srcdir)/driver/gles/cogl-program-gles.c \
$(srcdir)/driver/gles/cogl-program-gles.h \
$(srcdir)/driver/gles/cogl-texture-driver-gles.c \
$(NULL)
@ -224,6 +220,8 @@ cogl_sources_c = \
$(srcdir)/cogl-material-arbfp-private.h \
$(srcdir)/cogl-material-fixed.c \
$(srcdir)/cogl-material-fixed-private.h \
$(srcdir)/cogl-program.c \
$(srcdir)/cogl-program-private.h \
$(srcdir)/cogl-blend-string.c \
$(srcdir)/cogl-blend-string.h \
$(srcdir)/cogl-debug.c \

View File

@ -38,7 +38,6 @@ typedef enum
COGL_FRONT_WINDING_COUNTER_CLOCKWISE
} CoglFrontWinding;
#ifdef HAVE_COGL_GLES2
typedef enum {
COGL_BOXED_NONE,
COGL_BOXED_INT,
@ -58,10 +57,9 @@ typedef struct _CoglBoxedValue
float matrix[16];
float *float_array;
int *int_array;
gpointer array;
void *array;
} v;
} CoglBoxedValue;
#endif
#ifdef COGL_GL_DEBUG

View File

@ -44,14 +44,7 @@
#include "cogl-journal-private.h"
#include "cogl-color-private.h"
#include "cogl-profile.h"
#ifdef HAVE_COGL_GL
#include "cogl-program-gl.h"
#endif
#ifdef HAVE_COGL_GLES2
#include "cogl-program-gles.h"
#endif
#include "cogl-program-private.h"
#include <glib.h>
#include <glib/gprintf.h>
@ -97,6 +90,10 @@ typedef struct _ArbfpProgramState
UnitState *unit_state;
int next_constant_id;
/* Age of the program the last time the uniforms were flushed. This
is used to detect when we need to flush all of the uniforms */
unsigned int user_program_age;
/* We need to track the last material that an ARBfp program was used
* with so know if we need to update any program.local parameters. */
CoglMaterial *last_used_for_material;
@ -1064,14 +1061,18 @@ _cogl_material_backend_arbfp_end (CoglMaterial *material,
if (arbfp_program_state->user_program != COGL_INVALID_HANDLE)
{
CoglProgram *program = (CoglProgram *)arbfp_program_state->user_program;
gl_program = program->gl_handle;
/* An arbfp program should contain exactly one shader which we
can use directly */
CoglProgram *program = arbfp_program_state->user_program;
CoglShader *shader = program->attached_shaders->data;
gl_program = shader->gl_handle;
}
else
gl_program = arbfp_program_state->gl_program;
GE (glBindProgram (GL_FRAGMENT_PROGRAM_ARB, gl_program));
_cogl_use_program (COGL_INVALID_HANDLE, COGL_MATERIAL_PROGRAM_TYPE_ARBFP);
_cogl_use_program (0, COGL_MATERIAL_PROGRAM_TYPE_ARBFP);
if (arbfp_program_state->user_program == COGL_INVALID_HANDLE)
{
@ -1086,6 +1087,19 @@ _cogl_material_backend_arbfp_end (CoglMaterial *material,
update_constants_cb,
&state);
}
else
{
CoglProgram *program = arbfp_program_state->user_program;
gboolean program_changed;
/* If the shader has changed since it was last flushed then we
need to update all uniforms */
program_changed = program->age != arbfp_program_state->user_program_age;
_cogl_program_flush_uniforms (program, gl_program, program_changed);
arbfp_program_state->user_program_age = program->age;
}
/* We need to track what material used this arbfp program last since
* we will need to update program.local params when switching

View File

@ -43,14 +43,6 @@
#include "cogl-blend-string.h"
#include "cogl-profile.h"
#ifdef HAVE_COGL_GL
#include "cogl-program-gl.h"
#endif
#ifdef HAVE_COGL_GLES2
#include "cogl-program-gles.h"
#endif
#include <glib.h>
#include <glib/gprintf.h>
#include <string.h>
@ -83,7 +75,7 @@ _cogl_material_backend_fixed_start (CoglMaterial *material,
int n_layers,
unsigned long materials_difference)
{
_cogl_use_program (COGL_INVALID_HANDLE, COGL_MATERIAL_PROGRAM_TYPE_FIXED);
_cogl_use_program (0, COGL_MATERIAL_PROGRAM_TYPE_FIXED);
return TRUE;
}
@ -242,6 +234,13 @@ _cogl_material_backend_fixed_end (CoglMaterial *material,
GE (glDisable (GL_FOG));
}
#ifdef HAVE_COGL_GLES2
/* Let the GLES2 backend know that we're not using a user shader
anymore. This is a massive hack but it will go away once the GLSL
backend replaces the GLES2 wrapper */
_cogl_gles2_use_program (0);
#endif
return TRUE;
}

View File

@ -39,14 +39,19 @@
#include "cogl-context.h"
#include "cogl-handle.h"
#include "cogl-shader-private.h"
#include "cogl-program-private.h"
#ifdef HAVE_COGL_GL
#include "cogl-program-gl.h"
#endif
#ifndef HAVE_COGL_GLES2
#ifdef HAVE_COGL_GLES2
#include "cogl-program-gles.h"
#endif
#define glCreateProgram ctx->drv.pf_glCreateProgram
#define glAttachShader ctx->drv.pf_glAttachShader
#define glUseProgram ctx->drv.pf_glUseProgram
#define glLinkProgram ctx->drv.pf_glLinkProgram
#define glDeleteProgram ctx->drv.pf_glDeleteProgram
#define glGetProgramInfoLog ctx->drv.pf_glGetProgramInfoLog
#define glGetProgramiv ctx->drv.pf_glGetProgramiv
#endif /* HAVE_COGL_GLES2 */
#include <glib.h>
@ -58,6 +63,35 @@
#include "../gles/cogl-gles2-wrapper.h"
#endif
typedef struct _GlslProgramState
{
int ref_count;
/* Age of the user program that was current when the gl_program was
linked. This is used to detect when we need to relink a new
program */
unsigned int user_program_age;
GLuint gl_program;
/* This is set to TRUE if the program has changed since we last
flushed the uniforms */
gboolean gl_program_changed;
#ifdef HAVE_COGL_GLES2
/* The GLES2 generated program that was generated from the user
program. This is used to detect when the GLES2 backend generates
a different program which would mean we need to flush all of the
custom uniforms. This is a massive hack but it can go away once
this GLSL backend starts generating its own shaders */
GLuint gles2_program;
#endif
} GlslProgramState;
typedef struct _CoglMaterialBackendGlslPrivate
{
GlslProgramState *glsl_program_state;
} CoglMaterialBackendGlslPrivate;
const CoglMaterialBackend _cogl_material_glsl_backend;
static int
@ -66,24 +100,261 @@ _cogl_material_backend_glsl_get_max_texture_units (void)
return _cogl_get_max_texture_image_units ();
}
static GlslProgramState *
glsl_program_state_new (int n_layers)
{
GlslProgramState *state = g_slice_new0 (GlslProgramState);
state->ref_count = 1;
return state;
}
static GlslProgramState *
glsl_program_state_ref (GlslProgramState *state)
{
state->ref_count++;
return state;
}
static void
delete_program (GLuint program)
{
#ifdef HAVE_COGL_GLES2
/* This hack can go away once this GLSL backend replaces the GLES2
wrapper */
_cogl_gles2_clear_cache_for_program (program);
#else
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
#endif
GE (glDeleteProgram (program));
}
void
glsl_program_state_unref (GlslProgramState *state)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (state->ref_count > 0);
state->ref_count--;
if (state->ref_count == 0)
{
if (state->gl_program)
{
delete_program (state->gl_program);
state->gl_program = 0;
}
g_slice_free (GlslProgramState, state);
}
}
/* This tries to find the oldest ancestor whos state would generate
* the same glsl program as the current material. This is a simple
* mechanism for reducing the number of glsl programs we have to
* generate.
*/
static CoglMaterial *
find_glsl_authority (CoglMaterial *material, CoglHandle user_program)
{
/* Find the first material that modifies the user shader */
return _cogl_material_get_authority (material,
COGL_MATERIAL_STATE_USER_SHADER);
}
static CoglMaterialBackendGlslPrivate *
get_glsl_priv (CoglMaterial *material)
{
if (!(material->backend_priv_set_mask & COGL_MATERIAL_BACKEND_GLSL_MASK))
return NULL;
return material->backend_privs[COGL_MATERIAL_BACKEND_GLSL];
}
static void
set_glsl_priv (CoglMaterial *material, CoglMaterialBackendGlslPrivate *priv)
{
if (priv)
{
material->backend_privs[COGL_MATERIAL_BACKEND_GLSL] = priv;
material->backend_priv_set_mask |= COGL_MATERIAL_BACKEND_GLSL_MASK;
}
else
material->backend_priv_set_mask &= ~COGL_MATERIAL_BACKEND_GLSL_MASK;
}
static GlslProgramState *
get_glsl_program_state (CoglMaterial *material)
{
CoglMaterialBackendGlslPrivate *priv = get_glsl_priv (material);
if (!priv)
return NULL;
return priv->glsl_program_state;
}
static void
dirty_glsl_program_state (CoglMaterial *material)
{
CoglMaterialBackendGlslPrivate *priv;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
priv = get_glsl_priv (material);
if (!priv)
return;
if (priv->glsl_program_state)
{
glsl_program_state_unref (priv->glsl_program_state);
priv->glsl_program_state = NULL;
}
}
static void
link_program (GLint gl_program)
{
/* On GLES2 we'll let the backend link the program. This hack can go
away once this backend replaces the GLES2 wrapper */
#ifndef HAVE_COGL_GLES2
GLint link_status;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
GE( glLinkProgram (gl_program) );
GE( glGetProgramiv (gl_program, GL_LINK_STATUS, &link_status) );
if (!link_status)
{
GLint log_length;
GLsizei out_log_length;
char *log;
GE( glGetProgramiv (gl_program, GL_INFO_LOG_LENGTH, &log_length) );
log = g_malloc (log_length);
GE( glGetProgramInfoLog (gl_program, log_length,
&out_log_length, log) );
g_warning ("Failed to link GLSL program:\n%.*s\n",
log_length, log);
g_free (log);
}
#endif /* HAVE_COGL_GLES2 */
}
static gboolean
_cogl_material_backend_glsl_start (CoglMaterial *material,
int n_layers,
unsigned long materials_difference)
{
CoglHandle program;
CoglMaterialBackendGlslPrivate *priv;
CoglMaterial *authority;
CoglMaterialBackendGlslPrivate *authority_priv;
CoglProgram *user_program;
GLuint gl_program;
GSList *l;
_COGL_GET_CONTEXT (ctx, FALSE);
if (!cogl_features_available (COGL_FEATURE_SHADERS_GLSL))
return FALSE;
program = cogl_material_get_user_program (material);
if (program == COGL_INVALID_HANDLE ||
_cogl_program_get_language (program) != COGL_SHADER_LANGUAGE_GLSL)
user_program = cogl_material_get_user_program (material);
if (user_program == COGL_INVALID_HANDLE ||
_cogl_program_get_language (user_program) != COGL_SHADER_LANGUAGE_GLSL)
return FALSE; /* XXX: change me when we support code generation here */
_cogl_use_program (program, COGL_MATERIAL_PROGRAM_TYPE_GLSL);
/* Now lookup our glsl backend private state (allocating if
* necessary) */
priv = get_glsl_priv (material);
if (!priv)
{
priv = g_slice_new0 (CoglMaterialBackendGlslPrivate);
set_glsl_priv (material, priv);
}
/* If we already have a valid GLSL program then we don't need to
relink a new one */
if (priv->glsl_program_state)
{
/* However if the program has changed since the last link then we do
need to relink */
if (priv->glsl_program_state->user_program_age == user_program->age)
return TRUE;
/* Destroy the existing program. We can't just dirty the whole
glsl state because otherwise if we are not the authority on
the user program then we'll just find the same state again */
delete_program (priv->glsl_program_state->gl_program);
priv->glsl_program_state->gl_program = 0;
}
else
{
/* If we don't have an associated glsl program yet then find the
* glsl-authority (the oldest ancestor whose state will result in
* the same program being generated as for this material).
*
* We always make sure to associate new programs with the
* glsl-authority to maximize the chance that other materials can
* share it.
*/
authority = find_glsl_authority (material, user_program);
authority_priv = get_glsl_priv (authority);
if (!authority_priv)
{
authority_priv = g_slice_new0 (CoglMaterialBackendGlslPrivate);
set_glsl_priv (authority, authority_priv);
}
/* If we don't have an existing program associated with the
* glsl-authority then start generating code for a new program...
*/
if (!authority_priv->glsl_program_state)
{
GlslProgramState *glsl_program_state =
glsl_program_state_new (n_layers);
authority_priv->glsl_program_state = glsl_program_state;
/* If the material isn't actually its own glsl-authority
* then take a reference to the program state associated
* with the glsl-authority... */
if (authority != material)
priv->glsl_program_state =
glsl_program_state_ref (authority_priv->glsl_program_state);
}
}
/* If we make it here then we have a glsl_program_state struct
without a gl_program either because this is the first time we've
encountered it or because the user program has changed since it
was last linked */
priv->glsl_program_state->gl_program_changed = TRUE;
GE_RET( gl_program, glCreateProgram () );
/* Add all of the shaders from the user program */
for (l = user_program->attached_shaders; l; l = l->next)
{
CoglShader *shader = l->data;
g_assert (shader->language == COGL_SHADER_LANGUAGE_GLSL);
GE( glAttachShader (gl_program, shader->gl_handle) );
}
priv->glsl_program_state->gl_program = gl_program;
priv->glsl_program_state->user_program_age = user_program->age;
link_program (gl_program);
return TRUE;
}
@ -105,9 +376,64 @@ gboolean
_cogl_material_backend_glsl_end (CoglMaterial *material,
unsigned long materials_difference)
{
GlslProgramState *glsl_program_state = get_glsl_program_state (material);
GLuint gl_program;
gboolean gl_program_changed;
gl_program = glsl_program_state->gl_program;
gl_program_changed = glsl_program_state->gl_program_changed;
#ifdef HAVE_COGL_GLES2
/* This function is a massive hack to get the GLES2 backend to
work. It should only be neccessary until we move the GLSL
generation into this file instead of the GLES2 driver backend */
gl_program = _cogl_gles2_use_program (gl_program);
/* We need to detect when the GLES2 backend gives us a different
program from last time */
if (gl_program != glsl_program_state->gles2_program)
{
glsl_program_state->gles2_program = gl_program;
gl_program_changed = TRUE;
}
#else
_cogl_use_program (gl_program, COGL_MATERIAL_PROGRAM_TYPE_GLSL);
#endif
_cogl_program_flush_uniforms (cogl_material_get_user_program (material),
gl_program, gl_program_changed);
glsl_program_state->gl_program_changed = FALSE;
return TRUE;
}
static void
_cogl_material_backend_glsl_pre_change_notify (CoglMaterial *material,
CoglMaterialState change,
const CoglColor *new_color)
{
static const unsigned long glsl_op_changes =
COGL_MATERIAL_STATE_USER_SHADER;
if (!(change & glsl_op_changes))
return;
dirty_glsl_program_state (material);
}
static void
_cogl_material_backend_glsl_free_priv (CoglMaterial *material)
{
CoglMaterialBackendGlslPrivate *priv = get_glsl_priv (material);
if (priv)
{
if (priv->glsl_program_state)
glsl_program_state_unref (priv->glsl_program_state);
g_slice_free (CoglMaterialBackendGlslPrivate, priv);
set_glsl_priv (material, NULL);
}
}
const CoglMaterialBackend _cogl_material_glsl_backend =
{
_cogl_material_backend_glsl_get_max_texture_units,
@ -115,10 +441,11 @@ const CoglMaterialBackend _cogl_material_glsl_backend =
_cogl_material_backend_glsl_add_layer,
_cogl_material_backend_glsl_passthrough,
_cogl_material_backend_glsl_end,
NULL, /* material_state_change_notify */
_cogl_material_backend_glsl_pre_change_notify,
NULL, /* material_set_parent_notify */
NULL, /* layer_state_change_notify */
NULL, /* free_priv */
NULL, /* layer_pre_change_notify */
_cogl_material_backend_glsl_free_priv,
NULL /* free_layer_priv */
};
#endif /* COGL_MATERIAL_BACKEND_GLSL */

View File

@ -37,14 +37,6 @@
#include "cogl-context.h"
#include "cogl-texture-private.h"
#ifdef HAVE_COGL_GL
#include "cogl-program-gl.h"
#endif
#ifdef HAVE_COGL_GLES2
#include "cogl-program-gles.h"
#endif
#ifdef COGL_MATERIAL_BACKEND_GLSL
#include "cogl-material-glsl-private.h"
#endif
@ -287,124 +279,80 @@ _cogl_material_texture_storage_change_notify (CoglHandle texture)
}
void
_cogl_gl_use_program_wrapper (CoglHandle program_handle)
_cogl_use_program (GLuint gl_program, CoglMaterialProgramType type)
{
#ifdef COGL_MATERIAL_BACKEND_GLSL
#ifndef HAVE_COGL_GLES2
CoglProgram *program = (CoglProgram *)program_handle;
GLuint gl_program;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (program_handle != COGL_INVALID_HANDLE)
gl_program = program->gl_handle;
else
gl_program = 0;
/* If we're changing program type... */
if (type != ctx->current_use_program_type)
{
/* ... disable the old type */
switch (ctx->current_use_program_type)
{
case COGL_MATERIAL_PROGRAM_TYPE_GLSL:
GE( glUseProgram (0) );
ctx->current_gl_program = 0;
break;
if (ctx->current_gl_program == gl_program)
return;
case COGL_MATERIAL_PROGRAM_TYPE_ARBFP:
#ifdef HAVE_COGL_GL
GE( glDisable (GL_FRAGMENT_PROGRAM_ARB) );
#endif
break;
if (gl_program != 0)
case COGL_MATERIAL_PROGRAM_TYPE_FIXED:
/* don't need to to anything */
break;
}
/* ... and enable the new type */
switch (type)
{
case COGL_MATERIAL_PROGRAM_TYPE_ARBFP:
#ifdef HAVE_COGL_GL
GE( glEnable (GL_FRAGMENT_PROGRAM_ARB) );
#endif
break;
case COGL_MATERIAL_PROGRAM_TYPE_GLSL:
case COGL_MATERIAL_PROGRAM_TYPE_FIXED:
/* don't need to to anything */
break;
}
}
if (type == COGL_MATERIAL_PROGRAM_TYPE_GLSL)
{
#ifdef COGL_MATERIAL_BACKEND_GLSL
if (ctx->current_gl_program != gl_program)
{
GLenum gl_error;
while ((gl_error = glGetError ()) != GL_NO_ERROR)
;
glUseProgram (gl_program);
if (glGetError () != GL_NO_ERROR)
{
GE (glUseProgram (0));
ctx->current_gl_program = 0;
return;
}
}
else
GE (glUseProgram (0));
if (glGetError () == GL_NO_ERROR)
ctx->current_gl_program = gl_program;
#else /* HAVE_COGL_GLES2 */
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
ctx->drv.gles2.settings.user_program = program_handle;
ctx->drv.gles2.settings_dirty = TRUE;
#endif /* HAVE_COGL_GLES2 */
#endif
}
static void
disable_arbfp (void)
{
#ifdef COGL_MATERIAL_BACKEND_ARBFP
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (ctx->current_use_program_type == COGL_MATERIAL_PROGRAM_TYPE_ARBFP)
GE (glDisable (GL_FRAGMENT_PROGRAM_ARB));
#endif
}
void
_cogl_use_program (CoglHandle program_handle, CoglMaterialProgramType type)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
switch (type)
else
{
#ifdef COGL_MATERIAL_BACKEND_GLSL
case COGL_MATERIAL_PROGRAM_TYPE_GLSL:
{
_cogl_gl_use_program_wrapper (program_handle);
disable_arbfp ();
ctx->current_use_program_type = type;
break;
GE( glUseProgram (0) );
ctx->current_gl_program = 0;
}
#else
case COGL_MATERIAL_PROGRAM_TYPE_GLSL:
g_warning ("Unexpected use of GLSL backend!");
break;
#endif
#ifdef COGL_MATERIAL_BACKEND_ARBFP
case COGL_MATERIAL_PROGRAM_TYPE_ARBFP:
/* _cogl_gl_use_program_wrapper can be called by cogl-program.c
* so we can't bailout without making sure we glUseProgram (0)
* first. */
_cogl_gl_use_program_wrapper (0);
if (ctx->current_use_program_type == COGL_MATERIAL_PROGRAM_TYPE_ARBFP)
break;
GE (glEnable (GL_FRAGMENT_PROGRAM_ARB));
ctx->current_use_program_type = type;
break;
#else
case COGL_MATERIAL_PROGRAM_TYPE_ARBFP:
g_warning ("Unexpected use of GLSL backend!");
break;
#endif
#ifdef COGL_MATERIAL_BACKEND_FIXED
case COGL_MATERIAL_PROGRAM_TYPE_FIXED:
/* _cogl_gl_use_program_wrapper can be called by cogl-program.c
* so we can't bailout without making sure we glUseProgram (0)
* first. */
_cogl_gl_use_program_wrapper (0);
if (ctx->current_use_program_type == COGL_MATERIAL_PROGRAM_TYPE_FIXED)
break;
disable_arbfp ();
ctx->current_use_program_type = type;
#endif
}
#else
g_warning ("Unexpected use of GLSL backend!");
#endif /* COGL_MATERIAL_BACKEND_GLSL */
}
#ifndef COGL_MATERIAL_BACKEND_ARBFP
else if (type == COGL_MATERIAL_PROGRAM_TYPE_ARBFP)
g_warning ("Unexpected use of ARBFP backend!");
#endif /* COGL_MATERIAL_BACKEND_ARBFP */
ctx->current_use_program_type = type;
}
#if defined (COGL_MATERIAL_BACKEND_GLSL) || \

View File

@ -709,7 +709,7 @@ _cogl_get_max_texture_image_units (void);
void
_cogl_use_program (CoglHandle program_handle, CoglMaterialProgramType type);
_cogl_use_program (GLuint gl_program, CoglMaterialProgramType type);
unsigned int
_cogl_get_n_args_for_combine_func (GLint func);

View File

@ -0,0 +1,78 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef __COGL_PROGRAM_H
#define __COGL_PROGRAM_H
#include "cogl-handle.h"
#include "cogl-internal.h"
#include "cogl-shader-private.h"
typedef struct _CoglProgram CoglProgram;
struct _CoglProgram
{
CoglHandleObject _parent;
GSList *attached_shaders;
GArray *custom_uniforms;
/* An age counter that changes whenever the list of shaders is modified */
unsigned int age;
};
typedef struct _CoglProgramUniform CoglProgramUniform;
struct _CoglProgramUniform
{
char *name;
CoglBoxedValue value;
/* The cached GL location for this uniform. This is only valid
between calls to _cogl_program_dirty_all_uniforms */
GLint location;
/* Whether we have a location yet */
unsigned int location_valid : 1;
/* Whether the uniform value has changed since the last time the
uniforms were flushed */
unsigned int dirty : 1;
};
CoglProgram *_cogl_program_pointer_from_handle (CoglHandle handle);
/* Internal function to flush the custom uniforms for the given use
program. This assumes the target GL program is already bound. The
gl_program still needs to be passed so that CoglProgram can query
the uniform locations. gl_program_changed should be set to TRUE if
we are flushing the uniforms against a different GL program from
the last time it was flushed. This will cause it to requery all of
the locations and assume that all uniforms are dirty */
void
_cogl_program_flush_uniforms (CoglProgram *program,
GLuint gl_program,
gboolean gl_program_changed);
CoglShaderLanguage
_cogl_program_get_language (CoglHandle handle);
#endif /* __COGL_PROGRAM_H */

683
cogl/cogl-program.c Normal file
View File

@ -0,0 +1,683 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009,2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl.h"
#include "cogl-internal.h"
#include "cogl-context.h"
#include "cogl-handle.h"
#ifndef HAVE_COGL_GLES
#include <string.h>
#include "cogl-shader-private.h"
#include "cogl-program-private.h"
static void _cogl_program_free (CoglProgram *program);
COGL_HANDLE_DEFINE (Program, program);
COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING (program);
/* A CoglProgram is effectively just a list of shaders that will be
used together and a set of values for the custom uniforms. No
actual GL program is created - instead this is the responsibility
of the GLSL material backend. The uniform values are collected in
an array and then flushed whenever the material backend requests
it. */
#ifndef HAVE_COGL_GLES2
#define glGetUniformLocation ctx->drv.pf_glGetUniformLocation
#define glUniform1f ctx->drv.pf_glUniform1f
#define glUniform2f ctx->drv.pf_glUniform2f
#define glUniform3f ctx->drv.pf_glUniform3f
#define glUniform4f ctx->drv.pf_glUniform4f
#define glUniform1fv ctx->drv.pf_glUniform1fv
#define glUniform2fv ctx->drv.pf_glUniform2fv
#define glUniform3fv ctx->drv.pf_glUniform3fv
#define glUniform4fv ctx->drv.pf_glUniform4fv
#define glUniform1i ctx->drv.pf_glUniform1i
#define glUniform2i ctx->drv.pf_glUniform2i
#define glUniform3i ctx->drv.pf_glUniform3i
#define glUniform4i ctx->drv.pf_glUniform4i
#define glUniform1iv ctx->drv.pf_glUniform1iv
#define glUniform2iv ctx->drv.pf_glUniform2iv
#define glUniform3iv ctx->drv.pf_glUniform3iv
#define glUniform4iv ctx->drv.pf_glUniform4iv
#define glUniformMatrix2fv ctx->drv.pf_glUniformMatrix2fv
#define glUniformMatrix3fv ctx->drv.pf_glUniformMatrix3fv
#define glUniformMatrix4fv ctx->drv.pf_glUniformMatrix4fv
#define glProgramLocalParameter4fv ctx->drv.pf_glProgramLocalParameter4fv
#endif /* HAVE_COGL_GLES2 */
static void
_cogl_program_free (CoglProgram *program)
{
int i;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* Unref all of the attached shaders */
g_slist_foreach (program->attached_shaders, (GFunc) cogl_handle_unref, NULL);
/* Destroy the list */
g_slist_free (program->attached_shaders);
for (i = 0; i < program->custom_uniforms->len; i++)
{
CoglProgramUniform *uniform =
&g_array_index (program->custom_uniforms, CoglProgramUniform, i);
g_free (uniform->name);
if (uniform->value.count > 1)
g_free (uniform->value.v.array);
}
g_array_free (program->custom_uniforms, TRUE);
g_slice_free (CoglProgram, program);
}
CoglHandle
cogl_create_program (void)
{
CoglProgram *program;
program = g_slice_new0 (CoglProgram);
program->custom_uniforms =
g_array_new (FALSE, FALSE, sizeof (CoglProgramUniform));
program->age = 0;
return _cogl_program_handle_new (program);
}
void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle)
{
CoglProgram *program;
CoglShader *shader;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!cogl_is_program (program_handle) || !cogl_is_shader (shader_handle))
return;
program = _cogl_program_pointer_from_handle (program_handle);
shader = _cogl_shader_pointer_from_handle (shader_handle);
/* Only one shader is allowed if the type is ARBfp */
#ifdef HAVE_COGL_GL
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
g_return_if_fail (program->attached_shaders == NULL);
else if (shader->language == COGL_SHADER_LANGUAGE_GLSL)
g_return_if_fail (_cogl_program_get_language (program) ==
COGL_SHADER_LANGUAGE_GLSL);
#endif
program->attached_shaders
= g_slist_prepend (program->attached_shaders,
cogl_handle_ref (shader_handle));
program->age++;
}
void
cogl_program_link (CoglHandle handle)
{
/* There's no point in linking the program here because it will have
to be relinked with a different fixed functionality shader
whenever the settings change */
}
void
cogl_program_use (CoglHandle handle)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (handle == COGL_INVALID_HANDLE ||
cogl_is_program (handle));
if (ctx->current_program == 0 && handle != 0)
ctx->legacy_state_set++;
else if (handle == 0 && ctx->current_program != 0)
ctx->legacy_state_set--;
if (handle != COGL_INVALID_HANDLE)
cogl_handle_ref (handle);
if (ctx->current_program != COGL_INVALID_HANDLE)
cogl_handle_unref (ctx->current_program);
ctx->current_program = handle;
}
int
cogl_program_get_uniform_location (CoglHandle handle,
const char *uniform_name)
{
int i;
CoglProgram *program;
CoglProgramUniform *uniform;
if (!cogl_is_program (handle))
return -1;
program = _cogl_program_pointer_from_handle (handle);
/* We can't just ask the GL program object for the uniform location
directly because it will change every time the program is linked
with a different shader. Instead we make our own mapping of
uniform numbers and cache the names */
for (i = 0; i < program->custom_uniforms->len; i++)
{
uniform = &g_array_index (program->custom_uniforms,
CoglProgramUniform, i);
if (!strcmp (uniform->name, uniform_name))
return i;
}
/* Create a new uniform with the given name */
g_array_set_size (program->custom_uniforms,
program->custom_uniforms->len + 1);
uniform = &g_array_index (program->custom_uniforms,
CoglProgramUniform,
program->custom_uniforms->len - 1);
uniform->name = g_strdup (uniform_name);
memset (&uniform->value, 0, sizeof (CoglBoxedValue));
uniform->dirty = TRUE;
uniform->location_valid = FALSE;
return program->custom_uniforms->len - 1;
}
static void
cogl_program_uniform_x (CoglHandle handle,
int uniform_no,
int size,
int count,
CoglBoxedType type,
gsize value_size,
gconstpointer value,
gboolean transpose)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
g_return_if_fail (program != NULL);
if (uniform_no >= 0 && uniform_no < program->custom_uniforms->len &&
size >= 1 && size <= 4 && count >= 1)
{
CoglProgramUniform *uniform =
&g_array_index (program->custom_uniforms,
CoglProgramUniform, uniform_no);
if (count == 1)
{
if (uniform->value.count > 1)
g_free (uniform->value.v.array);
memcpy (uniform->value.v.float_value, value, value_size);
}
else
{
if (uniform->value.count > 1)
{
if (uniform->value.count != count ||
uniform->value.size != size ||
uniform->value.type != type)
{
g_free (uniform->value.v.array);
uniform->value.v.array = g_malloc (count * value_size);
}
}
else
uniform->value.v.array = g_malloc (count * value_size);
memcpy (uniform->value.v.array, value, count * value_size);
}
uniform->value.type = type;
uniform->value.size = size;
uniform->value.count = count;
uniform->dirty = TRUE;
}
}
void
cogl_program_uniform_1f (int uniform_no,
float value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_uniform_x (ctx->current_program,
uniform_no, 1, 1, COGL_BOXED_FLOAT,
sizeof (float), &value, FALSE);
}
void
cogl_program_set_uniform_1f (CoglHandle handle,
int uniform_location,
float value)
{
cogl_program_uniform_x (handle,
uniform_location, 1, 1, COGL_BOXED_FLOAT,
sizeof (float), &value, FALSE);
}
void
cogl_program_uniform_1i (int uniform_no,
int value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_uniform_x (ctx->current_program,
uniform_no, 1, 1, COGL_BOXED_INT,
sizeof (int), &value, FALSE);
}
void
cogl_program_set_uniform_1i (CoglHandle handle,
int uniform_location,
int value)
{
cogl_program_uniform_x (handle,
uniform_location, 1, 1, COGL_BOXED_INT,
sizeof (int), &value, FALSE);
}
void
cogl_program_uniform_float (int uniform_no,
int size,
int count,
const GLfloat *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_uniform_x (ctx->current_program,
uniform_no, size, count, COGL_BOXED_FLOAT,
sizeof (float) * size, value, FALSE);
}
void
cogl_program_set_uniform_float (CoglHandle handle,
int uniform_location,
int n_components,
int count,
const float *value)
{
cogl_program_uniform_x (handle,
uniform_location, n_components, count,
COGL_BOXED_FLOAT,
sizeof (float) * n_components, value, FALSE);
}
void
cogl_program_uniform_int (int uniform_no,
int size,
int count,
const GLint *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_uniform_x (ctx->current_program,
uniform_no, size, count, COGL_BOXED_INT,
sizeof (int) * size, value, FALSE);
}
void
cogl_program_set_uniform_int (CoglHandle handle,
int uniform_location,
int n_components,
int count,
const int *value)
{
cogl_program_uniform_x (handle,
uniform_location, n_components, count,
COGL_BOXED_INT,
sizeof (int) * n_components, value, FALSE);
}
void
cogl_program_set_uniform_matrix (CoglHandle handle,
int uniform_location,
int dimensions,
int count,
gboolean transpose,
const float *value)
{
g_return_if_fail (cogl_is_program (handle));
cogl_program_uniform_x (handle,
uniform_location, dimensions, count,
COGL_BOXED_MATRIX,
sizeof (float) * dimensions * dimensions,
value,
transpose);
}
void
cogl_program_uniform_matrix (int uniform_no,
int size,
int count,
gboolean transpose,
const float *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_matrix (ctx->current_program,
uniform_no, size, count, transpose, value);
}
/* ARBfp local parameters can be referenced like:
*
* "program.local[5]"
* ^14char offset (after whitespace is stripped)
*/
static int
get_local_param_index (const char *uniform_name)
{
char *input = g_strdup (uniform_name);
int i;
char *p = input;
char *endptr;
int _index;
for (i = 0; input[i] != '\0'; i++)
if (input[i] != '_' && input[i] != '\t')
*p++ = input[i];
input[i] = '\0';
g_return_val_if_fail (strncmp ("program.local[", input, 14) == 0, -1);
_index = g_ascii_strtoull (input + 14, &endptr, 10);
g_return_val_if_fail (endptr != input + 14, -1);
g_return_val_if_fail (*endptr == ']', -1);
g_return_val_if_fail (_index >= 0, -1);
g_free (input);
return _index;
}
static void
_cogl_program_flush_uniform_glsl (GLint location,
CoglBoxedValue *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
switch (value->type)
{
case COGL_BOXED_NONE:
break;
case COGL_BOXED_INT:
{
int *ptr;
if (value->count == 1)
ptr = value->v.int_value;
else
ptr = value->v.int_array;
switch (value->size)
{
case 1: glUniform1iv (location, value->count, ptr); break;
case 2: glUniform2iv (location, value->count, ptr); break;
case 3: glUniform3iv (location, value->count, ptr); break;
case 4: glUniform4iv (location, value->count, ptr); break;
}
}
break;
case COGL_BOXED_FLOAT:
{
float *ptr;
if (value->count == 1)
ptr = value->v.float_value;
else
ptr = value->v.float_array;
switch (value->size)
{
case 1: glUniform1fv (location, value->count, ptr); break;
case 2: glUniform2fv (location, value->count, ptr); break;
case 3: glUniform3fv (location, value->count, ptr); break;
case 4: glUniform4fv (location, value->count, ptr); break;
}
}
break;
case COGL_BOXED_MATRIX:
{
float *ptr;
if (value->count == 1)
ptr = value->v.matrix;
else
ptr = value->v.float_array;
switch (value->size)
{
case 2:
glUniformMatrix2fv (location, value->count, value->transpose, ptr);
break;
case 3:
glUniformMatrix3fv (location, value->count, value->transpose, ptr);
break;
case 4:
glUniformMatrix4fv (location, value->count, value->transpose, ptr);
break;
}
}
break;
}
}
#ifdef HAVE_COGL_GL
static void
_cogl_program_flush_uniform_arbfp (GLint location,
CoglBoxedValue *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (value->type != COGL_BOXED_NONE)
{
g_return_if_fail (value->type == COGL_BOXED_FLOAT);
g_return_if_fail (value->size == 4);
g_return_if_fail (value->count == 1);
GE( glProgramLocalParameter4fv (GL_FRAGMENT_PROGRAM_ARB, location,
value->v.float_value) );
}
}
#endif /* HAVE_COGL_GL */
void
_cogl_program_flush_uniforms (CoglProgram *program,
GLuint gl_program,
gboolean gl_program_changed)
{
CoglProgramUniform *uniform;
int i;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
for (i = 0; i < program->custom_uniforms->len; i++)
{
uniform = &g_array_index (program->custom_uniforms,
CoglProgramUniform, i);
if (gl_program_changed || uniform->dirty)
{
if (gl_program_changed || !uniform->location_valid)
{
if (_cogl_program_get_language (program) ==
COGL_SHADER_LANGUAGE_GLSL)
uniform->location =
glGetUniformLocation (gl_program, uniform->name);
else
uniform->location =
get_local_param_index (uniform->name);
uniform->location_valid = TRUE;
}
/* If the uniform isn't really in the program then there's
no need to actually set it */
if (uniform->location != -1)
{
switch (_cogl_program_get_language (program))
{
case COGL_SHADER_LANGUAGE_GLSL:
_cogl_program_flush_uniform_glsl (uniform->location,
&uniform->value);
break;
#ifdef HAVE_COGL_GL
case COGL_SHADER_LANGUAGE_ARBFP:
_cogl_program_flush_uniform_arbfp (uniform->location,
&uniform->value);
break;
#endif
}
}
uniform->dirty = FALSE;
}
}
}
#else /* HAVE_COGL_GLES */
/* No support on regular OpenGL 1.1 */
CoglHandle
cogl_create_program (void)
{
return COGL_INVALID_HANDLE;
}
gboolean
cogl_is_program (CoglHandle handle)
{
return FALSE;
}
CoglHandle
cogl_program_ref (CoglHandle handle)
{
return COGL_INVALID_HANDLE;
}
void
cogl_program_unref (CoglHandle handle)
{
}
void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle)
{
}
void
cogl_program_link (CoglHandle program_handle)
{
}
void
cogl_program_use (CoglHandle program_handle)
{
}
int
cogl_program_get_uniform_location (CoglHandle program_handle,
const char *uniform_name)
{
return 0;
}
void
cogl_program_uniform_1f (int uniform_no,
float value)
{
}
void
cogl_program_uniform_1i (int uniform_no,
int value)
{
}
void
cogl_program_uniform_float (int uniform_no,
int size,
int count,
const GLfloat *value)
{
}
void
cogl_program_uniform_int (int uniform_no,
int size,
int count,
const int *value)
{
}
void
cogl_program_uniform_matrix (int uniform_no,
int size,
int count,
gboolean transpose,
const GLfloat *value)
{
}
#endif /* HAVE_COGL_GLES2 */
CoglShaderLanguage
_cogl_program_get_language (CoglHandle handle)
{
CoglProgram *program = handle;
/* Use the language of the first shader */
if (program->attached_shaders)
{
CoglShader *shader = program->attached_shaders->data;
return shader->language;
}
else
return COGL_SHADER_LANGUAGE_GLSL;
}

View File

@ -40,7 +40,6 @@ struct _CoglShader
{
CoglHandleObject _parent;
GLuint gl_handle;
char *arbfp_source;
CoglShaderType type;
CoglShaderLanguage language;
};

View File

@ -42,6 +42,10 @@
#define glCompileShader ctx->drv.pf_glCompileShader
#define glShaderSource ctx->drv.pf_glShaderSource
#define glDeleteShader ctx->drv.pf_glDeleteShader
#define glProgramString ctx->drv.pf_glProgramString
#define glBindProgram ctx->drv.pf_glBindProgram
#define glDeletePrograms ctx->drv.pf_glDeletePrograms
#define glGenPrograms ctx->drv.pf_glGenPrograms
#define GET_CONTEXT _COGL_GET_CONTEXT
#else
#define GET_CONTEXT(CTXVAR,RETVAL) G_STMT_START { } G_STMT_END
@ -63,7 +67,10 @@ _cogl_shader_free (CoglShader *shader)
#ifdef HAVE_COGL_GL
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
g_free (shader->arbfp_source);
{
if (shader->gl_handle)
GE (glDeletePrograms (1, &shader->gl_handle));
}
else
#endif
if (shader->gl_handle)
@ -123,11 +130,10 @@ cogl_shader_source (CoglHandle handle,
if (G_UNLIKELY (language != shader->language))
{
#ifdef HAVE_COGL_GL
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
{
g_free (shader->arbfp_source);
shader->arbfp_source = NULL;
if (shader->gl_handle)
GE (glDeletePrograms (1, &shader->gl_handle));
}
else
#endif
@ -139,7 +145,35 @@ cogl_shader_source (CoglHandle handle,
#ifdef HAVE_COGL_GL
if (language == COGL_SHADER_LANGUAGE_ARBFP)
shader->arbfp_source = g_strdup (source);
{
#ifdef COGL_GL_DEBUG
GLenum gl_error;
#endif
GE (glGenPrograms (1, &shader->gl_handle));
GE (glBindProgram (GL_FRAGMENT_PROGRAM_ARB, shader->gl_handle));
#ifdef COGL_GL_DEBUG
while ((gl_error = glGetError ()) != GL_NO_ERROR)
;
#endif
glProgramString (GL_FRAGMENT_PROGRAM_ARB,
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen (source),
source);
#ifdef COGL_GL_DEBUG
gl_error = glGetError ();
if (gl_error != GL_NO_ERROR)
{
g_warning ("%s: GL error (%d): Failed to compile ARBfp:\n%s\n%s",
G_STRLOC,
gl_error,
source,
glGetString (GL_PROGRAM_ERROR_STRING_ARB));
}
#endif
}
else
#endif
{

View File

@ -279,6 +279,17 @@ COGL_FEATURE_FUNCTION (void, glUniformMatrix4fv,
GLboolean transpose,
const GLfloat *value))
COGL_FEATURE_FUNCTION (void, glGetProgramiv,
(GLuint program,
GLenum pname,
GLint *params))
COGL_FEATURE_FUNCTION (void, glGetProgramInfoLog,
(GLuint program,
GLsizei bufSize,
GLsizei *length,
GLchar *infoLog))
COGL_FEATURE_END ()
COGL_FEATURE_BEGIN (vbos, 1, 5,

View File

@ -1,488 +0,0 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009,2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl.h"
#include "cogl-program-gl.h"
#include "cogl-shader-private.h"
#include "cogl-internal.h"
#include "cogl-handle.h"
#include "cogl-context.h"
#include "cogl-journal-private.h"
#include "cogl-material-opengl-private.h"
#include <glib.h>
#include <string.h>
#define glCreateProgram ctx->drv.pf_glCreateProgram
#define glAttachShader ctx->drv.pf_glAttachShader
#define glUseProgram ctx->drv.pf_glUseProgram
#define glLinkProgram ctx->drv.pf_glLinkProgram
#define glGetUniformLocation ctx->drv.pf_glGetUniformLocation
#define glUniform1f ctx->drv.pf_glUniform1f
#define glUniform2f ctx->drv.pf_glUniform2f
#define glUniform3f ctx->drv.pf_glUniform3f
#define glUniform4f ctx->drv.pf_glUniform4f
#define glUniform1fv ctx->drv.pf_glUniform1fv
#define glUniform2fv ctx->drv.pf_glUniform2fv
#define glUniform3fv ctx->drv.pf_glUniform3fv
#define glUniform4fv ctx->drv.pf_glUniform4fv
#define glUniform1i ctx->drv.pf_glUniform1i
#define glUniform2i ctx->drv.pf_glUniform2i
#define glUniform3i ctx->drv.pf_glUniform3i
#define glUniform4i ctx->drv.pf_glUniform4i
#define glUniform1iv ctx->drv.pf_glUniform1iv
#define glUniform2iv ctx->drv.pf_glUniform2iv
#define glUniform3iv ctx->drv.pf_glUniform3iv
#define glUniform4iv ctx->drv.pf_glUniform4iv
#define glUniformMatrix2fv ctx->drv.pf_glUniformMatrix2fv
#define glUniformMatrix3fv ctx->drv.pf_glUniformMatrix3fv
#define glUniformMatrix4fv ctx->drv.pf_glUniformMatrix4fv
#define glDeleteProgram ctx->drv.pf_glDeleteProgram
#define glProgramString ctx->drv.pf_glProgramString
#define glBindProgram ctx->drv.pf_glBindProgram
#define glDeletePrograms ctx->drv.pf_glDeletePrograms
#define glGenPrograms ctx->drv.pf_glGenPrograms
#define glProgramLocalParameter4fv ctx->drv.pf_glProgramLocalParameter4fv
static void _cogl_program_free (CoglProgram *program);
COGL_HANDLE_DEFINE (Program, program);
COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING (program);
static void
_cogl_program_free (CoglProgram *program)
{
/* Frees program resources but its handle is not
released! Do that separately before this! */
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (program->gl_handle)
{
if (program->language == COGL_SHADER_LANGUAGE_ARBFP)
GE (glDeletePrograms (1, &program->gl_handle));
else
GE (glDeleteProgram (program->gl_handle));
}
g_slice_free (CoglProgram, program);
}
CoglHandle
cogl_create_program (void)
{
CoglProgram *program;
_COGL_GET_CONTEXT (ctx, NULL);
program = g_slice_new0 (CoglProgram);
return _cogl_program_handle_new (program);
}
void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle)
{
CoglProgram *program;
CoglShader *shader;
CoglShaderLanguage language;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (program_handle));
g_return_if_fail (cogl_is_shader (shader_handle));
program = _cogl_program_pointer_from_handle (program_handle);
shader = _cogl_shader_pointer_from_handle (shader_handle);
language = shader->language;
/* We only allow attaching one ARBfp shader to a program */
if (language == COGL_SHADER_LANGUAGE_ARBFP)
g_return_if_fail (program->gl_handle == 0);
program->language = language;
if (language == COGL_SHADER_LANGUAGE_ARBFP)
{
#ifdef COGL_GL_DEBUG
GLenum gl_error;
#endif
GE (glGenPrograms (1, &program->gl_handle));
GE (glBindProgram (GL_FRAGMENT_PROGRAM_ARB, program->gl_handle));
#ifdef COGL_GL_DEBUG
while ((gl_error = glGetError ()) != GL_NO_ERROR)
;
#endif
glProgramString (GL_FRAGMENT_PROGRAM_ARB,
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen (shader->arbfp_source),
shader->arbfp_source);
#ifdef COGL_GL_DEBUG
gl_error = glGetError ();
if (gl_error != GL_NO_ERROR)
{
g_warning ("%s: GL error (%d): Failed to compile ARBfp:\n%s\n%s",
G_STRLOC,
gl_error,
shader->arbfp_source,
glGetString (GL_PROGRAM_ERROR_STRING_ARB));
}
#endif
}
else
{
if (!program->gl_handle)
program->gl_handle = glCreateProgram ();
GE (glAttachShader (program->gl_handle, shader->gl_handle));
}
/* NB: There is no separation between shader objects and program
* objects for ARBfp */
}
void
cogl_program_link (CoglHandle handle)
{
CoglProgram *program;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!cogl_is_program (handle))
return;
program = _cogl_program_pointer_from_handle (handle);
if (program->language == COGL_SHADER_LANGUAGE_GLSL &&
program->gl_handle)
GE (glLinkProgram (program->gl_handle));
program->is_linked = TRUE;
}
void
cogl_program_use (CoglHandle handle)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (handle == COGL_INVALID_HANDLE ||
cogl_is_program (handle));
if (handle != COGL_INVALID_HANDLE)
{
CoglProgram *program = handle;
g_return_if_fail (program->is_linked);
}
if (ctx->current_program == 0 && handle != 0)
ctx->legacy_state_set++;
else if (handle == 0 && ctx->current_program != 0)
ctx->legacy_state_set--;
if (handle != COGL_INVALID_HANDLE)
cogl_handle_ref (handle);
if (ctx->current_program != COGL_INVALID_HANDLE)
cogl_handle_unref (ctx->current_program);
ctx->current_program = handle;
}
/* ARBfp local parameters can be referenced like:
*
* "program.local[5]"
* ^14char offset (after whitespace is stripped)
*/
static int
get_local_param_index (const char *uniform_name)
{
char *input = g_strdup (uniform_name);
int i;
char *p = input;
char *endptr;
int _index;
for (i = 0; input[i] != '\0'; i++)
if (input[i] != '_' && input[i] != '\t')
*p++ = input[i];
input[i] = '\0';
g_return_val_if_fail (strncmp ("program.local[", input, 14) == 0, -1);
_index = g_ascii_strtoull (input + 14, &endptr, 10);
g_return_val_if_fail (endptr != input + 14, -1);
g_return_val_if_fail (*endptr == ']', -1);
g_return_val_if_fail (_index >= 0 &&
_index < COGL_PROGRAM_MAX_ARBFP_LOCAL_PARAMS, -1);
g_free (input);
return _index;
}
int
cogl_program_get_uniform_location (CoglHandle handle,
const char *uniform_name)
{
CoglProgram *program;
_COGL_GET_CONTEXT (ctx, 0);
if (!cogl_is_program (handle))
return 0;
program = _cogl_program_pointer_from_handle (handle);
if (program->language == COGL_SHADER_LANGUAGE_ARBFP)
return get_local_param_index (uniform_name);
else
return glGetUniformLocation (program->gl_handle, uniform_name);
}
void
cogl_program_set_uniform_1f (CoglHandle handle,
int uniform_location,
float value)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
g_return_if_fail (program->language != COGL_SHADER_LANGUAGE_ARBFP);
_cogl_gl_use_program_wrapper (program);
GE (glUniform1f (uniform_location, value));
}
void
cogl_program_uniform_1f (int uniform_location,
float value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_1f (ctx->current_program,
uniform_location, value);
}
void
cogl_program_set_uniform_1i (CoglHandle handle,
int uniform_location,
int value)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
g_return_if_fail (program->language != COGL_SHADER_LANGUAGE_ARBFP);
_cogl_gl_use_program_wrapper (program);
GE (glUniform1i (uniform_location, value));
}
void
cogl_program_uniform_1i (int uniform_location,
int value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_1i (ctx->current_program, uniform_location, value);
}
void
cogl_program_set_uniform_float (CoglHandle handle,
int uniform_location,
int n_components,
int count,
const float *value)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
if (program->language == COGL_SHADER_LANGUAGE_ARBFP)
{
unsigned int _index = uniform_location;
unsigned int index_end = _index + count;
int i;
int j;
g_return_if_fail (n_components == 4);
GE (glBindProgram (GL_FRAGMENT_PROGRAM_ARB, program->gl_handle));
for (i = _index; i < index_end; i++)
for (j = 0; j < 4; j++)
program->arbfp_local_params[i][j] = *(value++);
for (i = _index; i < index_end; i++)
GE (glProgramLocalParameter4fv (GL_FRAGMENT_PROGRAM_ARB,
i,
&program->arbfp_local_params[i][0]));
}
else
{
_cogl_gl_use_program_wrapper (program);
switch (n_components)
{
case 1:
GE (glUniform1fv (uniform_location, count, value));
break;
case 2:
GE (glUniform2fv (uniform_location, count, value));
break;
case 3:
GE (glUniform3fv (uniform_location, count, value));
break;
case 4:
GE (glUniform4fv (uniform_location, count, value));
break;
default:
g_warning ("%s called with invalid size parameter", G_STRFUNC);
}
}
}
void
cogl_program_uniform_float (int uniform_location,
int n_components,
int count,
const float *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_float (ctx->current_program,
uniform_location,
n_components, count, value);
}
void
cogl_program_set_uniform_int (CoglHandle handle,
int uniform_location,
int n_components,
int count,
const int *value)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
_cogl_gl_use_program_wrapper (program);
switch (n_components)
{
case 1:
glUniform1iv (uniform_location, count, value);
break;
case 2:
glUniform2iv (uniform_location, count, value);
break;
case 3:
glUniform3iv (uniform_location, count, value);
break;
case 4:
glUniform4iv (uniform_location, count, value);
break;
default:
g_warning ("%s called with invalid size parameter", G_STRFUNC);
}
}
void
cogl_program_uniform_int (int uniform_location,
int n_components,
int count,
const int *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_int (ctx->current_program,
uniform_location, n_components, count, value);
}
void
cogl_program_set_uniform_matrix (CoglHandle handle,
int uniform_location,
int n_components,
int count,
gboolean transpose,
const float*value)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
g_return_if_fail (program->language != COGL_SHADER_LANGUAGE_ARBFP);
_cogl_gl_use_program_wrapper (program);
switch (n_components)
{
case 2 :
GE (glUniformMatrix2fv (uniform_location, count, transpose, value));
break;
case 3 :
GE (glUniformMatrix3fv (uniform_location, count, transpose, value));
break;
case 4 :
GE (glUniformMatrix4fv (uniform_location, count, transpose, value));
break;
default :
g_warning ("%s called with invalid size parameter", G_STRFUNC);
}
}
void
cogl_program_uniform_matrix (int uniform_location,
int dimensions,
int count,
gboolean transpose,
const float *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_matrix (ctx->current_program,
uniform_location, dimensions,
count, transpose, value);
}
CoglShaderLanguage
_cogl_program_get_language (CoglHandle handle)
{
CoglProgram *program = handle;
return program->language;
}

View File

@ -1,46 +0,0 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef __COGL_PROGRAM_H
#define __COGL_PROGRAM_H
#include "cogl-handle.h"
#include "cogl-shader-private.h"
typedef struct _CoglProgram CoglProgram;
/* The ARBfp spec says at least 24 indices are available */
#define COGL_PROGRAM_MAX_ARBFP_LOCAL_PARAMS 24
struct _CoglProgram
{
CoglHandleObject _parent;
CoglShaderLanguage language;
float arbfp_local_params[COGL_PROGRAM_MAX_ARBFP_LOCAL_PARAMS][4];
GLuint gl_handle;
gboolean is_linked;
};
CoglProgram *_cogl_program_pointer_from_handle (CoglHandle handle);
#endif /* __COGL_PROGRAM_H */

View File

@ -834,7 +834,8 @@ cogl_gles2_wrapper_get_program (const CoglGles2WrapperSettings *settings)
CoglGles2WrapperShader *vertex_shader, *fragment_shader;
GLint status;
gboolean custom_vertex_shader = FALSE, custom_fragment_shader = FALSE;
CoglProgram *user_program = NULL;
GLuint shaders[16];
GLsizei n_shaders = 0;
int i;
_COGL_GET_GLES2_WRAPPER (w, NULL);
@ -851,21 +852,23 @@ cogl_gles2_wrapper_get_program (const CoglGles2WrapperSettings *settings)
/* Otherwise create a new program */
/* Check whether the currently used custom program has vertex and
fragment shaders */
if (w->settings.user_program != COGL_INVALID_HANDLE)
if (settings->user_program)
{
user_program
= _cogl_program_pointer_from_handle (w->settings.user_program);
/* We work out whether the program contains a vertex and
fragment shader by looking at the list of attached shaders */
glGetAttachedShaders (settings->user_program,
G_N_ELEMENTS (shaders),
&n_shaders, shaders);
for (node = user_program->attached_shaders; node; node = node->next)
for (i = 0; i < n_shaders; i++)
{
CoglShader *shader
= _cogl_shader_pointer_from_handle ((CoglHandle) node->data);
GLint shader_type;
if (shader->type == COGL_SHADER_TYPE_VERTEX)
glGetShaderiv (shaders[i], GL_SHADER_TYPE, &shader_type);
if (shader_type == GL_VERTEX_SHADER)
custom_vertex_shader = TRUE;
else if (shader->type == COGL_SHADER_TYPE_FRAGMENT)
else if (shader_type == GL_FRAGMENT_SHADER)
custom_fragment_shader = TRUE;
}
}
@ -892,13 +895,9 @@ cogl_gles2_wrapper_get_program (const CoglGles2WrapperSettings *settings)
glAttachShader (program->program, vertex_shader->shader);
if (!custom_fragment_shader)
glAttachShader (program->program, fragment_shader->shader);
if (user_program)
for (node = user_program->attached_shaders; node; node = node->next)
{
CoglShader *shader
= _cogl_shader_pointer_from_handle ((CoglHandle) node->data);
glAttachShader (program->program, shader->gl_handle);
}
/* Attach all the shaders stolen from the user program */
for (i = 0; i < n_shaders; i++)
glAttachShader (program->program, shaders[i]);
cogl_gles2_wrapper_bind_attributes (program->program);
glLinkProgram (program->program);
@ -927,11 +926,6 @@ cogl_gles2_wrapper_get_program (const CoglGles2WrapperSettings *settings)
&program->uniforms,
&program->attributes);
/* We haven't tried to get a location for any of the custom uniforms
yet */
for (i = 0; i < COGL_PROGRAM_NUM_CUSTOM_UNIFORMS; i++)
program->custom_gl_uniforms[i] = COGL_PROGRAM_UNBOUND_CUSTOM_UNIFORM;
w->compiled_programs = g_slist_append (w->compiled_programs, program);
return program;
@ -1098,83 +1092,10 @@ _cogl_wrap_glNormalPointer (GLenum type, GLsizei stride, const GLvoid *pointer)
GL_FALSE, stride, pointer);
}
static void
cogl_gles2_do_set_uniform (GLint location, CoglBoxedValue *value)
{
switch (value->type)
{
case COGL_BOXED_NONE:
break;
case COGL_BOXED_INT:
{
int *ptr;
if (value->count == 1)
ptr = value->v.int_value;
else
ptr = value->v.int_array;
switch (value->size)
{
case 1: glUniform1iv (location, value->count, ptr); break;
case 2: glUniform2iv (location, value->count, ptr); break;
case 3: glUniform3iv (location, value->count, ptr); break;
case 4: glUniform4iv (location, value->count, ptr); break;
}
}
break;
case COGL_BOXED_FLOAT:
{
float *ptr;
if (value->count == 1)
ptr = value->v.float_value;
else
ptr = value->v.float_array;
switch (value->size)
{
case 1: glUniform1fv (location, value->count, ptr); break;
case 2: glUniform2fv (location, value->count, ptr); break;
case 3: glUniform3fv (location, value->count, ptr); break;
case 4: glUniform4fv (location, value->count, ptr); break;
}
}
break;
case COGL_BOXED_MATRIX:
{
float *ptr;
if (value->count == 1)
ptr = value->v.matrix;
else
ptr = value->v.float_array;
switch (value->size)
{
case 2:
glUniformMatrix2fv (location, value->count, value->transpose, ptr);
break;
case 3:
glUniformMatrix3fv (location, value->count, value->transpose, ptr);
break;
case 4:
glUniformMatrix4fv (location, value->count, value->transpose, ptr);
break;
}
}
break;
}
}
static void
_cogl_wrap_prepare_for_draw (void)
{
CoglGles2WrapperProgram *program;
guint32 dirty_custom_uniforms = 0;
_COGL_GET_GLES2_WRAPPER (w, NO_RETVAL);
@ -1191,22 +1112,21 @@ _cogl_wrap_prepare_for_draw (void)
/* Start using it if we aren't already */
if (w->current_program != program)
{
glUseProgram (program->program);
w->current_program = program;
/* All of the uniforms are probably now out of date */
w->dirty_uniforms = COGL_GLES2_DIRTY_ALL;
dirty_custom_uniforms = (1 << COGL_PROGRAM_NUM_CUSTOM_UNIFORMS) - 1;
}
w->settings_dirty = FALSE;
}
else
{
CoglProgram *user_program =
_cogl_program_pointer_from_handle (w->settings.user_program);
if (user_program)
dirty_custom_uniforms = user_program->dirty_custom_uniforms;
program = w->current_program;
}
/* We always have to reassert the program even if it hasn't changed
because the fixed-function material backend disables the program
again in the _start function. This should go away once the GLSL
code is generated in the GLSL material backend so it's probably
not worth worrying about now */
_cogl_use_program (program->program, COGL_MATERIAL_PROGRAM_TYPE_GLSL);
/* Make sure all of the uniforms are up to date */
if (w->dirty_uniforms)
@ -1285,32 +1205,6 @@ _cogl_wrap_prepare_for_draw (void)
w->dirty_uniforms = 0;
}
if (dirty_custom_uniforms)
{
int i;
if (w->settings.user_program != COGL_INVALID_HANDLE)
{
CoglProgram *user_program
= _cogl_program_pointer_from_handle (w->settings.user_program);
const char *uniform_name;
for (i = 0; i < COGL_PROGRAM_NUM_CUSTOM_UNIFORMS; i++)
if ((dirty_custom_uniforms & (1 << i))
&& (uniform_name = user_program->custom_uniform_names[i]))
{
if (program->custom_gl_uniforms[i]
== COGL_PROGRAM_UNBOUND_CUSTOM_UNIFORM)
program->custom_gl_uniforms[i]
= glGetUniformLocation (program->program, uniform_name);
if (program->custom_gl_uniforms[i] >= 0)
cogl_gles2_do_set_uniform (program->custom_gl_uniforms[i],
&user_program->custom_uniforms[i]);
}
user_program->dirty_custom_uniforms = 0;
}
}
if (w->dirty_attribute_pointers
& COGL_GLES2_DIRTY_TEX_COORD_VERTEX_ATTRIB)
{
@ -1765,21 +1659,47 @@ _cogl_wrap_glPointSize (GLfloat size)
w->dirty_uniforms |= COGL_GLES2_DIRTY_POINT_SIZE;
}
/* This function is a massive hack to get custom GLSL programs to
work. It's only necessary until we move the GLSL shader generation
into the CoglMaterial. The gl_program specifies the user program to
be used. The list of shaders will be extracted out of this and
compiled into a new program containing any fixed function shaders
that need to be generated. The new program will be returned. */
GLuint
_cogl_gles2_use_program (GLuint gl_program)
{
_COGL_GET_GLES2_WRAPPER (w, 0);
_COGL_GLES2_CHANGE_SETTING (w, user_program, gl_program);
/* We need to bind the program immediately so that the GLSL material
backend can update the custom uniforms */
_cogl_wrap_prepare_for_draw ();
return w->current_program->program;
}
void
_cogl_gles2_clear_cache_for_program (CoglHandle user_program)
_cogl_gles2_clear_cache_for_program (GLuint gl_program)
{
GSList *node, *next, *last = NULL;
CoglGles2WrapperProgram *program;
_COGL_GET_GLES2_WRAPPER (w, NO_RETVAL);
if (w->settings.user_program == gl_program)
{
w->settings.user_program = 0;
w->settings_dirty = TRUE;
}
/* Remove any cached programs that link against this custom program */
for (node = w->compiled_programs; node; node = next)
{
next = node->next;
program = (CoglGles2WrapperProgram *) node->data;
if (program->settings.user_program == user_program)
if (program->settings.user_program == gl_program)
{
glDeleteProgram (program->program);

View File

@ -26,7 +26,6 @@
#include "cogl.h" /* needed for gl header include */
#include "cogl-internal.h"
#include "cogl-program-gles.h"
G_BEGIN_DECLS
@ -143,7 +142,7 @@ struct _CoglGles2WrapperSettings
GLint fog_mode;
/* The current in-use user program */
CoglHandle user_program;
GLuint user_program;
unsigned int alpha_test_enabled:1;
unsigned int fog_enabled:1;
@ -218,8 +217,6 @@ struct _CoglGles2WrapperProgram
{
GLuint program;
CoglProgram *user_program;
/* The settings that were used to generate this combination */
CoglGles2WrapperSettings settings;
@ -229,7 +226,6 @@ struct _CoglGles2WrapperProgram
/* The uniforms for this program */
CoglGles2WrapperUniforms uniforms;
GLint custom_gl_uniforms[COGL_PROGRAM_NUM_CUSTOM_UNIFORMS];
};
struct _CoglGles2WrapperShader
@ -394,7 +390,8 @@ void _cogl_wrap_glPointSize (GLfloat point_size);
/* This function is only available on GLES 2 */
#define _cogl_wrap_glGenerateMipmap glGenerateMipmap
void _cogl_gles2_clear_cache_for_program (CoglHandle program);
GLuint _cogl_gles2_use_program (GLuint gl_program);
void _cogl_gles2_clear_cache_for_program (GLuint gl_program);
/* Remap the missing GL functions to use the wrappers */
#ifndef COGL_GLES2_WRAPPER_NO_REMAP

View File

@ -1,437 +0,0 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl.h"
#include "cogl-internal.h"
#include "cogl-context.h"
#include "cogl-handle.h"
#ifdef HAVE_COGL_GLES2
#include <string.h>
#include "cogl-shader-private.h"
#include "cogl-program-gles.h"
static void _cogl_program_free (CoglProgram *program);
COGL_HANDLE_DEFINE (Program, program);
COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING (program);
static void
_cogl_program_free (CoglProgram *program)
{
int i;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* Unref all of the attached shaders */
g_slist_foreach (program->attached_shaders, (GFunc) cogl_handle_unref, NULL);
/* Destroy the list */
g_slist_free (program->attached_shaders);
_cogl_gles2_clear_cache_for_program ((CoglHandle) program);
if (ctx->drv.gles2.settings.user_program == (CoglHandle) program)
{
ctx->drv.gles2.settings.user_program = COGL_INVALID_HANDLE;
ctx->drv.gles2.settings_dirty = TRUE;
}
for (i = 0; i < COGL_PROGRAM_NUM_CUSTOM_UNIFORMS; i++)
{
if (program->custom_uniform_names[i])
g_free (program->custom_uniform_names[i]);
if (program->custom_uniforms[i].count > 1)
g_free (program->custom_uniforms[i].v.array);
}
g_slice_free (CoglProgram, program);
}
CoglHandle
cogl_create_program (void)
{
CoglProgram *program;
program = g_slice_new0 (CoglProgram);
return _cogl_program_handle_new (program);
}
void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle)
{
CoglProgram *program;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!cogl_is_program (program_handle) || !cogl_is_shader (shader_handle))
return;
program = _cogl_program_pointer_from_handle (program_handle);
program->attached_shaders
= g_slist_prepend (program->attached_shaders,
cogl_handle_ref (shader_handle));
/* Whenever the shader changes we will need to relink the program
with the fixed functionality shaders so we should forget the
cached programs */
_cogl_gles2_clear_cache_for_program (program);
}
void
cogl_program_link (CoglHandle handle)
{
/* There's no point in linking the program here because it will have
to be relinked with a different fixed functionality shader
whenever the settings change */
}
void
cogl_program_use (CoglHandle handle)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (handle == COGL_INVALID_HANDLE ||
cogl_is_program (handle));
if (ctx->current_program == 0 && handle != 0)
ctx->legacy_state_set++;
else if (handle == 0 && ctx->current_program != 0)
ctx->legacy_state_set--;
if (handle != COGL_INVALID_HANDLE)
cogl_handle_ref (handle);
if (ctx->current_program != COGL_INVALID_HANDLE)
cogl_handle_unref (ctx->current_program);
ctx->current_program = handle;
}
int
cogl_program_get_uniform_location (CoglHandle handle,
const char *uniform_name)
{
int i;
CoglProgram *program;
if (!cogl_is_program (handle))
return -1;
program = _cogl_program_pointer_from_handle (handle);
/* We can't just ask the GL program object for the uniform location
directly because it will change every time the program is linked
with a new fixed functionality shader. Instead we make our own
mapping of uniform numbers and cache the names */
for (i = 0; program->custom_uniform_names[i]
&& i < COGL_PROGRAM_NUM_CUSTOM_UNIFORMS; i++)
if (!strcmp (program->custom_uniform_names[i], uniform_name))
return i;
if (i < COGL_PROGRAM_NUM_CUSTOM_UNIFORMS)
{
program->custom_uniform_names[i] = g_strdup (uniform_name);
return i;
}
else
/* We've run out of space for new uniform names so just pretend it
isn't there */
return -1;
}
static void
cogl_program_uniform_x (CoglHandle handle,
int uniform_no,
int size,
int count,
CoglBoxedType type,
gsize value_size,
gconstpointer value)
{
CoglProgram *program = handle;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
g_return_if_fail (program != NULL);
if (uniform_no >= 0 && uniform_no < COGL_PROGRAM_NUM_CUSTOM_UNIFORMS
&& size >= 1 && size <= 4 && count >= 1)
{
CoglBoxedValue *bv = program->custom_uniforms + uniform_no;
if (count == 1)
{
if (bv->count > 1)
g_free (bv->v.array);
memcpy (bv->v.float_value, value, value_size);
}
else
{
if (bv->count > 1)
{
if (bv->count != count || bv->size != size || bv->type != type)
{
g_free (bv->v.array);
bv->v.array = g_malloc (count * value_size);
}
}
else
bv->v.array = g_malloc (count * value_size);
memcpy (bv->v.array, value, count * value_size);
}
bv->type = type;
bv->size = size;
bv->count = count;
program->dirty_custom_uniforms |= 1 << uniform_no;
}
}
void
cogl_program_uniform_1f (int uniform_no,
float value)
{
cogl_program_uniform_float (uniform_no, 1, 1, &value);
}
void
cogl_program_set_uniform_1f (CoglHandle handle,
int uniform_location,
float value)
{
cogl_program_uniform_x (handle,
uniform_location, 1, 1, COGL_BOXED_FLOAT,
sizeof (float), &value);
}
void
cogl_program_uniform_1i (int uniform_no,
int value)
{
cogl_program_uniform_int (uniform_no, 1, 1, &value);
}
void
cogl_program_set_uniform_1i (CoglHandle handle,
int uniform_location,
int value)
{
cogl_program_uniform_x (handle,
uniform_location, 1, 1, COGL_BOXED_INT,
sizeof (int), &value);
}
void
cogl_program_uniform_float (int uniform_no,
int size,
int count,
const GLfloat *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_uniform_x (ctx->current_program,
uniform_no, size, count, COGL_BOXED_FLOAT,
sizeof (float) * size, value);
}
void
cogl_program_set_uniform_float (CoglHandle handle,
int uniform_location,
int n_components,
int count,
const float *value)
{
cogl_program_uniform_x (handle,
uniform_location, n_components, count,
COGL_BOXED_FLOAT,
sizeof (float) * n_components, value);
}
void
cogl_program_uniform_int (int uniform_no,
int size,
int count,
const GLint *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_uniform_x (ctx->current_program,
uniform_no, size, count, COGL_BOXED_INT,
sizeof (int) * size, value);
}
void
cogl_program_set_uniform_int (CoglHandle handle,
int uniform_location,
int n_components,
int count,
const int *value)
{
cogl_program_uniform_x (handle,
uniform_location, n_components, count,
COGL_BOXED_INT,
sizeof (int) * n_components, value);
}
void
cogl_program_set_uniform_matrix (CoglHandle handle,
int uniform_location,
int dimensions,
int count,
gboolean transpose,
const float *value)
{
CoglProgram *program = handle;
CoglBoxedValue *bv;
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
g_return_if_fail (cogl_is_program (handle));
bv = program->custom_uniforms + uniform_location;
cogl_program_uniform_x (ctx->current_program,
uniform_location, dimensions, count,
COGL_BOXED_MATRIX,
sizeof (float) * dimensions * dimensions , value);
bv->transpose = transpose;
}
void
cogl_program_uniform_matrix (int uniform_no,
int size,
int count,
gboolean transpose,
const float *value)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
cogl_program_set_uniform_matrix (ctx->current_program,
uniform_no, size, count, transpose, value);
}
#else /* HAVE_COGL_GLES2 */
/* No support on regular OpenGL 1.1 */
CoglHandle
cogl_create_program (void)
{
return COGL_INVALID_HANDLE;
}
gboolean
cogl_is_program (CoglHandle handle)
{
return FALSE;
}
CoglHandle
cogl_program_ref (CoglHandle handle)
{
return COGL_INVALID_HANDLE;
}
void
cogl_program_unref (CoglHandle handle)
{
}
void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle)
{
}
void
cogl_program_link (CoglHandle program_handle)
{
}
void
cogl_program_use (CoglHandle program_handle)
{
}
int
cogl_program_get_uniform_location (CoglHandle program_handle,
const char *uniform_name)
{
return 0;
}
void
cogl_program_uniform_1f (int uniform_no,
float value)
{
}
void
cogl_program_uniform_1i (int uniform_no,
int value)
{
}
void
cogl_program_uniform_float (int uniform_no,
int size,
int count,
const GLfloat *value)
{
}
void
cogl_program_uniform_int (int uniform_no,
int size,
int count,
const int *value)
{
}
void
cogl_program_uniform_matrix (int uniform_no,
int size,
int count,
gboolean transpose,
const GLfloat *value)
{
}
#endif /* HAVE_COGL_GLES2 */
CoglShaderLanguage
_cogl_program_get_language (CoglHandle handle)
{
return COGL_SHADER_LANGUAGE_GLSL;
}

View File

@ -1,52 +0,0 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2008,2009 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef __COGL_PROGRAM_H
#define __COGL_PROGRAM_H
#include "cogl-gles2-wrapper.h"
#include "cogl-handle.h"
#define COGL_PROGRAM_NUM_CUSTOM_UNIFORMS 16
#define COGL_PROGRAM_UNBOUND_CUSTOM_UNIFORM -2
typedef struct _CoglProgram CoglProgram;
struct _CoglProgram
{
CoglHandleObject _parent;
GSList *attached_shaders;
char *custom_uniform_names[COGL_PROGRAM_NUM_CUSTOM_UNIFORMS];
CoglBoxedValue custom_uniforms[COGL_PROGRAM_NUM_CUSTOM_UNIFORMS];
/* Uniforms that have changed since the last time this program was
* used. */
guint32 dirty_custom_uniforms;
};
CoglProgram *_cogl_program_pointer_from_handle (CoglHandle handle);
#endif /* __COGL_PROGRAM_H */