2008-06-02 06:58:57 -04:00
|
|
|
/*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Cogl
|
2008-06-02 06:58:57 -04:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
2008-06-02 06:58:57 -04:00
|
|
|
*
|
2010-08-02 16:40:55 -04:00
|
|
|
* Copyright (C) 2007,2008,2009,2010 Intel Corporation.
|
2008-06-02 06:58:57 -04:00
|
|
|
*
|
|
|
|
* 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
|
2010-03-01 07:56:10 -05:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
2008-06-02 06:58:57 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2008-10-30 13:25:00 -04:00
|
|
|
#include "cogl-shader-private.h"
|
2010-07-23 12:46:41 -04:00
|
|
|
#include "cogl-shader-boilerplate.h"
|
2008-06-02 06:58:57 -04:00
|
|
|
#include "cogl-internal.h"
|
2010-11-04 18:25:52 -04:00
|
|
|
#include "cogl-context-private.h"
|
2008-06-02 06:58:57 -04:00
|
|
|
#include "cogl-handle.h"
|
|
|
|
|
2010-08-02 16:40:55 -04:00
|
|
|
#include <glib.h>
|
|
|
|
|
2010-08-04 12:53:51 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-06-02 06:58:57 -04:00
|
|
|
static void _cogl_shader_free (CoglShader *shader);
|
|
|
|
|
2009-04-01 12:16:44 -04:00
|
|
|
COGL_HANDLE_DEFINE (Shader, shader);
|
2010-07-09 12:59:16 -04:00
|
|
|
COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING (shader);
|
2008-06-02 06:58:57 -04:00
|
|
|
|
2011-07-07 15:44:56 -04:00
|
|
|
#ifndef GL_FRAGMENT_SHADER
|
|
|
|
#define GL_FRAGMENT_SHADER 0x8B30
|
|
|
|
#endif
|
|
|
|
#ifndef GL_VERTEX_SHADER
|
|
|
|
#define GL_VERTEX_SHADER 0x8B31
|
|
|
|
#endif
|
|
|
|
|
2008-06-02 06:58:57 -04:00
|
|
|
static void
|
|
|
|
_cogl_shader_free (CoglShader *shader)
|
|
|
|
{
|
|
|
|
/* Frees shader resources but its handle is not
|
|
|
|
released! Do that separately before this! */
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-08-04 12:53:51 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_COGL_GL
|
|
|
|
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
if (shader->gl_handle)
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glDeletePrograms (1, &shader->gl_handle));
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
2010-08-04 12:53:51 -04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (shader->gl_handle)
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glDeleteShader (shader->gl_handle));
|
2010-07-22 11:58:23 -04:00
|
|
|
|
|
|
|
g_slice_free (CoglShader, shader);
|
2008-06-02 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglHandle
|
2009-05-12 09:15:18 -04:00
|
|
|
cogl_create_shader (CoglShaderType type)
|
2008-06-02 06:58:57 -04:00
|
|
|
{
|
|
|
|
CoglShader *shader;
|
2009-05-12 09:15:18 -04:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE);
|
2010-08-02 16:40:55 -04:00
|
|
|
|
2010-08-04 12:53:51 -04:00
|
|
|
switch (type)
|
2009-05-12 09:15:18 -04:00
|
|
|
{
|
2010-08-04 12:53:51 -04:00
|
|
|
case COGL_SHADER_TYPE_VERTEX:
|
|
|
|
case COGL_SHADER_TYPE_FRAGMENT:
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-12 09:15:18 -04:00
|
|
|
g_warning ("Unexpected shader type (0x%08lX) given to "
|
|
|
|
"cogl_create_shader", (unsigned long) type);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-06-02 06:58:57 -04:00
|
|
|
|
|
|
|
shader = g_slice_new (CoglShader);
|
2010-08-04 12:53:51 -04:00
|
|
|
shader->language = COGL_SHADER_LANGUAGE_GLSL;
|
|
|
|
shader->gl_handle = 0;
|
2010-12-02 16:08:30 -05:00
|
|
|
#ifdef HAVE_COGL_GLES2
|
|
|
|
shader->n_tex_coord_attribs = 0;
|
|
|
|
#endif
|
2010-07-14 14:39:24 -04:00
|
|
|
shader->type = type;
|
2008-06-02 06:58:57 -04:00
|
|
|
|
|
|
|
return _cogl_shader_handle_new (shader);
|
|
|
|
}
|
|
|
|
|
2010-12-02 16:08:30 -05:00
|
|
|
static void
|
|
|
|
delete_shader (CoglShader *shader)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
#ifdef HAVE_COGL_GL
|
|
|
|
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
|
|
|
|
{
|
|
|
|
if (shader->gl_handle)
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glDeletePrograms (1, &shader->gl_handle));
|
2010-12-02 16:08:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (shader->gl_handle)
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glDeleteShader (shader->gl_handle));
|
2010-12-02 16:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
shader->gl_handle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_shader_source (CoglHandle handle,
|
|
|
|
const char *source)
|
|
|
|
{
|
|
|
|
CoglShader *shader;
|
|
|
|
CoglShaderLanguage language;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
if (!cogl_is_shader (handle))
|
|
|
|
return;
|
|
|
|
|
2012-03-06 13:21:28 -05:00
|
|
|
shader = handle;
|
2010-12-02 16:08:30 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_COGL_GL
|
|
|
|
if (strncmp (source, "!!ARBfp1.0", 10) == 0)
|
|
|
|
language = COGL_SHADER_LANGUAGE_ARBFP;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
language = COGL_SHADER_LANGUAGE_GLSL;
|
|
|
|
|
|
|
|
/* Delete the old object if the language is changing... */
|
|
|
|
if (G_UNLIKELY (language != shader->language) &&
|
|
|
|
shader->gl_handle)
|
|
|
|
delete_shader (shader);
|
|
|
|
|
|
|
|
shader->source = g_strdup (source);
|
|
|
|
|
|
|
|
shader->language = language;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_shader_compile (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglShader *shader = handle;
|
2011-07-07 15:44:56 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-12-02 16:08:30 -05:00
|
|
|
|
|
|
|
if (!cogl_is_shader (handle))
|
|
|
|
return;
|
|
|
|
|
2011-07-07 15:44:56 -04:00
|
|
|
if (ctx->driver == COGL_DRIVER_GL)
|
|
|
|
_cogl_shader_compile_real (shader, 0 /* ignored */);
|
2010-12-02 16:08:30 -05:00
|
|
|
|
|
|
|
/* XXX: For GLES2 we don't actually compile anything until the
|
|
|
|
* shader gets used so we have an opportunity to add some
|
|
|
|
* boilerplate to the shader.
|
|
|
|
*
|
|
|
|
* At the end of the day this is obviously a badly designed API
|
|
|
|
* given that we are having to lie to the user. It was a mistake to
|
|
|
|
* so thinly wrap the OpenGL shader API and the current plan is to
|
|
|
|
* replace it with a pipeline snippets API. */
|
|
|
|
}
|
|
|
|
|
2010-11-22 07:15:35 -05:00
|
|
|
void
|
|
|
|
_cogl_shader_set_source_with_boilerplate (GLuint shader_gl_handle,
|
|
|
|
GLenum shader_gl_type,
|
2010-12-02 16:08:30 -05:00
|
|
|
int n_tex_coord_attribs,
|
2010-11-22 07:15:35 -05:00
|
|
|
GLsizei count_in,
|
|
|
|
const char **strings_in,
|
|
|
|
const GLint *lengths_in)
|
|
|
|
{
|
2011-07-07 15:44:56 -04:00
|
|
|
const char *vertex_boilerplate;
|
|
|
|
const char *fragment_boilerplate;
|
2010-11-22 07:15:35 -05:00
|
|
|
|
2010-12-07 08:44:45 -05:00
|
|
|
const char **strings = g_alloca (sizeof (char *) * (count_in + 3));
|
|
|
|
GLint *lengths = g_alloca (sizeof (GLint) * (count_in + 3));
|
2010-11-22 07:15:35 -05:00
|
|
|
int count = 0;
|
2010-12-02 17:08:51 -05:00
|
|
|
char *tex_coord_declarations = NULL;
|
2010-11-22 07:15:35 -05:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-11-22 07:15:35 -05:00
|
|
|
|
2011-07-07 15:44:56 -04:00
|
|
|
if (ctx->driver == COGL_DRIVER_GLES2)
|
|
|
|
{
|
|
|
|
vertex_boilerplate = _COGL_VERTEX_SHADER_BOILERPLATE_GLES2;
|
|
|
|
fragment_boilerplate = _COGL_FRAGMENT_SHADER_BOILERPLATE_GLES2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vertex_boilerplate = _COGL_VERTEX_SHADER_BOILERPLATE_GL;
|
|
|
|
fragment_boilerplate = _COGL_FRAGMENT_SHADER_BOILERPLATE_GL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->driver == COGL_DRIVER_GLES2 &&
|
2011-10-12 17:31:12 -04:00
|
|
|
cogl_has_feature (ctx, COGL_FEATURE_ID_TEXTURE_3D))
|
2010-12-07 08:44:45 -05:00
|
|
|
{
|
|
|
|
static const char texture_3d_extension[] =
|
|
|
|
"#extension GL_OES_texture_3D : enable\n";
|
|
|
|
strings[count] = texture_3d_extension;
|
|
|
|
lengths[count++] = sizeof (texture_3d_extension) - 1;
|
|
|
|
}
|
|
|
|
|
2010-11-22 07:15:35 -05:00
|
|
|
if (shader_gl_type == GL_VERTEX_SHADER)
|
|
|
|
{
|
|
|
|
strings[count] = vertex_boilerplate;
|
2011-07-07 15:44:56 -04:00
|
|
|
lengths[count++] = strlen (vertex_boilerplate);
|
2010-11-22 07:15:35 -05:00
|
|
|
}
|
|
|
|
else if (shader_gl_type == GL_FRAGMENT_SHADER)
|
|
|
|
{
|
|
|
|
strings[count] = fragment_boilerplate;
|
2011-07-07 15:44:56 -04:00
|
|
|
lengths[count++] = strlen (fragment_boilerplate);
|
2010-11-22 07:15:35 -05:00
|
|
|
}
|
|
|
|
|
2011-07-07 15:44:56 -04:00
|
|
|
if (ctx->driver == COGL_DRIVER_GLES2 &&
|
|
|
|
n_tex_coord_attribs)
|
2010-12-02 16:08:30 -05:00
|
|
|
{
|
2010-12-02 17:08:51 -05:00
|
|
|
GString *declarations = g_string_new (NULL);
|
|
|
|
|
|
|
|
g_string_append_printf (declarations,
|
|
|
|
"varying vec4 _cogl_tex_coord[%d];\n",
|
|
|
|
n_tex_coord_attribs);
|
|
|
|
|
|
|
|
if (shader_gl_type == GL_VERTEX_SHADER)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_string_append_printf (declarations,
|
|
|
|
"uniform mat4 cogl_texture_matrix[%d];\n",
|
|
|
|
n_tex_coord_attribs);
|
|
|
|
|
|
|
|
for (i = 0; i < n_tex_coord_attribs; i++)
|
|
|
|
g_string_append_printf (declarations,
|
|
|
|
"attribute vec4 cogl_tex_coord%d_in;\n",
|
|
|
|
i);
|
|
|
|
}
|
|
|
|
|
|
|
|
tex_coord_declarations = g_string_free (declarations, FALSE);
|
|
|
|
strings[count] = tex_coord_declarations;
|
2010-12-02 16:08:30 -05:00
|
|
|
lengths[count++] = -1; /* null terminated */
|
|
|
|
}
|
|
|
|
|
2010-11-22 07:15:35 -05:00
|
|
|
memcpy (strings + count, strings_in, sizeof (char *) * count_in);
|
|
|
|
if (lengths_in)
|
|
|
|
memcpy (lengths + count, lengths_in, sizeof (GLint) * count_in);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count_in; i++)
|
|
|
|
lengths[count + i] = -1; /* null terminated */
|
|
|
|
}
|
|
|
|
count += count_in;
|
|
|
|
|
2011-01-24 09:28:00 -05:00
|
|
|
if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_SHOW_SOURCE)))
|
2010-12-02 17:19:44 -05:00
|
|
|
{
|
|
|
|
GString *buf = g_string_new (NULL);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_string_append_printf (buf,
|
|
|
|
"%s shader:\n",
|
|
|
|
shader_gl_type == GL_VERTEX_SHADER ?
|
|
|
|
"vertex" : "fragment");
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
if (lengths[i] != -1)
|
|
|
|
g_string_append_len (buf, strings[i], lengths[i]);
|
|
|
|
else
|
|
|
|
g_string_append (buf, strings[i]);
|
|
|
|
|
|
|
|
g_message ("%s", buf->str);
|
|
|
|
|
|
|
|
g_string_free (buf, TRUE);
|
|
|
|
}
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glShaderSource (shader_gl_handle, count,
|
|
|
|
(const char **) strings, lengths) );
|
2010-12-02 16:08:30 -05:00
|
|
|
|
2010-12-02 17:08:51 -05:00
|
|
|
g_free (tex_coord_declarations);
|
2010-11-22 07:15:35 -05:00
|
|
|
}
|
|
|
|
|
2010-07-23 12:46:41 -04:00
|
|
|
void
|
2010-12-02 16:08:30 -05:00
|
|
|
_cogl_shader_compile_real (CoglHandle handle,
|
|
|
|
int n_tex_coord_attribs)
|
2010-07-23 12:46:41 -04:00
|
|
|
{
|
2010-12-02 16:08:30 -05:00
|
|
|
CoglShader *shader = handle;
|
2010-07-23 12:46:41 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-08-04 12:53:51 -04:00
|
|
|
|
2010-12-02 07:27:29 -05:00
|
|
|
#ifdef HAVE_COGL_GL
|
2010-12-02 16:08:30 -05:00
|
|
|
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
#ifdef COGL_GL_DEBUG
|
|
|
|
GLenum gl_error;
|
|
|
|
#endif
|
|
|
|
|
2010-12-02 16:08:30 -05:00
|
|
|
if (shader->gl_handle)
|
|
|
|
return;
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glGenPrograms (1, &shader->gl_handle));
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glBindProgram (GL_FRAGMENT_PROGRAM_ARB, shader->gl_handle));
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2011-01-24 09:28:00 -05:00
|
|
|
if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_SHOW_SOURCE)))
|
2010-12-02 17:19:44 -05:00
|
|
|
g_message ("user ARBfp program:\n%s", shader->source);
|
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
#ifdef COGL_GL_DEBUG
|
2011-07-06 16:51:00 -04:00
|
|
|
while ((gl_error = ctx->glGetError ()) != GL_NO_ERROR)
|
2010-10-15 13:00:29 -04:00
|
|
|
;
|
|
|
|
#endif
|
2011-07-06 16:51:00 -04:00
|
|
|
ctx->glProgramString (GL_FRAGMENT_PROGRAM_ARB,
|
|
|
|
GL_PROGRAM_FORMAT_ASCII_ARB,
|
|
|
|
strlen (shader->source),
|
|
|
|
shader->source);
|
2010-10-15 13:00:29 -04:00
|
|
|
#ifdef COGL_GL_DEBUG
|
2011-07-06 16:51:00 -04:00
|
|
|
gl_error = ctx->glGetError ();
|
2010-10-15 13:00:29 -04:00
|
|
|
if (gl_error != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
g_warning ("%s: GL error (%d): Failed to compile ARBfp:\n%s\n%s",
|
|
|
|
G_STRLOC,
|
|
|
|
gl_error,
|
2010-12-02 16:08:30 -05:00
|
|
|
shader->source,
|
2011-07-06 16:51:00 -04:00
|
|
|
ctx->glGetString (GL_PROGRAM_ERROR_STRING_ARB));
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2010-08-04 12:53:51 -04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2010-07-23 12:46:41 -04:00
|
|
|
GLenum gl_type;
|
|
|
|
|
2010-12-02 16:08:30 -05:00
|
|
|
if (shader->gl_handle
|
|
|
|
#ifdef HAVE_COGL_GLES2
|
2011-07-07 15:44:56 -04:00
|
|
|
&&
|
|
|
|
(ctx->driver != COGL_DRIVER_GLES2 ||
|
2011-10-19 07:47:22 -04:00
|
|
|
shader->n_tex_coord_attribs == n_tex_coord_attribs)
|
2010-12-02 16:08:30 -05:00
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (shader->gl_handle)
|
|
|
|
delete_shader (shader);
|
|
|
|
|
2010-07-23 12:46:41 -04:00
|
|
|
switch (shader->type)
|
2010-08-04 12:53:51 -04:00
|
|
|
{
|
2010-07-23 12:46:41 -04:00
|
|
|
case COGL_SHADER_TYPE_VERTEX:
|
|
|
|
gl_type = GL_VERTEX_SHADER;
|
|
|
|
break;
|
|
|
|
case COGL_SHADER_TYPE_FRAGMENT:
|
|
|
|
gl_type = GL_FRAGMENT_SHADER;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2010-08-04 12:53:51 -04:00
|
|
|
}
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
shader->gl_handle = ctx->glCreateShader (gl_type);
|
2008-06-02 06:58:57 -04:00
|
|
|
|
2010-11-22 07:15:35 -05:00
|
|
|
_cogl_shader_set_source_with_boilerplate (shader->gl_handle,
|
|
|
|
gl_type,
|
2010-12-02 16:08:30 -05:00
|
|
|
n_tex_coord_attribs,
|
|
|
|
1,
|
|
|
|
(const char **) &shader->source,
|
|
|
|
NULL);
|
2010-07-23 12:46:41 -04:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glCompileShader (shader->gl_handle));
|
2010-12-02 07:27:29 -05:00
|
|
|
|
2010-12-02 16:08:30 -05:00
|
|
|
#ifdef HAVE_COGL_GLES2
|
|
|
|
shader->n_tex_coord_attribs = n_tex_coord_attribs;
|
|
|
|
#endif
|
2010-12-02 07:27:29 -05:00
|
|
|
|
2010-12-02 16:08:30 -05:00
|
|
|
#ifdef COGL_GL_DEBUG
|
|
|
|
if (!cogl_shader_is_compiled (handle))
|
|
|
|
{
|
|
|
|
char *log = cogl_shader_get_info_log (handle);
|
|
|
|
g_warning ("Failed to compile GLSL program:\nsrc:\n%s\nerror:\n%s\n",
|
|
|
|
shader->source,
|
|
|
|
log);
|
2012-03-16 12:27:55 -04:00
|
|
|
g_free (log);
|
2010-12-02 16:08:30 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-06-02 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
char *
|
2009-06-01 11:31:32 -04:00
|
|
|
cogl_shader_get_info_log (CoglHandle handle)
|
2008-06-02 06:58:57 -04:00
|
|
|
{
|
|
|
|
CoglShader *shader;
|
2010-08-02 16:40:55 -04:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NULL);
|
2008-06-02 06:58:57 -04:00
|
|
|
|
|
|
|
if (!cogl_is_shader (handle))
|
2009-06-01 11:31:32 -04:00
|
|
|
return NULL;
|
2008-06-02 06:58:57 -04:00
|
|
|
|
2012-03-06 13:21:28 -05:00
|
|
|
shader = handle;
|
2008-06-02 06:58:57 -04:00
|
|
|
|
2010-08-12 05:43:22 -04:00
|
|
|
#ifdef HAVE_COGL_GL
|
2010-08-04 12:53:51 -04:00
|
|
|
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
|
|
|
|
{
|
|
|
|
/* ARBfp exposes a program error string, but since cogl_program
|
|
|
|
* doesn't have any API to query an error log it is not currently
|
|
|
|
* exposed. */
|
2010-08-12 05:43:22 -04:00
|
|
|
return g_strdup ("");
|
2010-08-04 12:53:51 -04:00
|
|
|
}
|
|
|
|
else
|
2010-08-12 05:43:22 -04:00
|
|
|
#endif
|
2010-08-04 12:53:51 -04:00
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
int len = 0;
|
2010-12-02 16:08:30 -05:00
|
|
|
|
|
|
|
/* We don't normally compile the shader when the user calls
|
|
|
|
* cogl_shader_compile() because we want to be able to add
|
|
|
|
* boilerplate code that depends on how it ends up finally being
|
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* Here we force an early compile if the user is interested in
|
|
|
|
* log information to increase the chance that the log will be
|
|
|
|
* useful! We have to guess the number of texture coordinate
|
2011-10-19 07:47:22 -04:00
|
|
|
* attributes that may be used since that affects the
|
|
|
|
* boilerplate. We use four so that the shader will still
|
|
|
|
* compile if the user is using more than one
|
|
|
|
* layer. Unfortunately this is likely to end up causing it to
|
|
|
|
* be compiled again when we know the actual number of layers */
|
2010-12-02 16:08:30 -05:00
|
|
|
if (!shader->gl_handle)
|
|
|
|
_cogl_shader_compile_real (handle, 4);
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
ctx->glGetShaderInfoLog (shader->gl_handle, 511, &len, buffer);
|
2010-08-04 12:53:51 -04:00
|
|
|
buffer[len] = '\0';
|
|
|
|
return g_strdup (buffer);
|
|
|
|
}
|
2008-06-02 06:58:57 -04:00
|
|
|
}
|
|
|
|
|
2009-05-12 09:15:18 -04:00
|
|
|
CoglShaderType
|
|
|
|
cogl_shader_get_type (CoglHandle handle)
|
2008-06-02 06:58:57 -04:00
|
|
|
{
|
|
|
|
CoglShader *shader;
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, COGL_SHADER_TYPE_VERTEX);
|
2010-08-02 16:40:55 -04:00
|
|
|
|
2008-06-02 06:58:57 -04:00
|
|
|
if (!cogl_is_shader (handle))
|
2009-05-12 09:15:18 -04:00
|
|
|
{
|
|
|
|
g_warning ("Non shader handle type passed to cogl_shader_get_type");
|
|
|
|
return COGL_SHADER_TYPE_VERTEX;
|
|
|
|
}
|
|
|
|
|
2012-03-06 13:21:28 -05:00
|
|
|
shader = handle;
|
2010-07-23 12:44:53 -04:00
|
|
|
return shader->type;
|
2009-05-12 09:15:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_shader_is_compiled (CoglHandle handle)
|
|
|
|
{
|
2011-07-07 15:44:56 -04:00
|
|
|
#if defined (HAVE_COGL_GL) || defined (HAVE_COGL_GLES2)
|
2009-05-12 09:15:18 -04:00
|
|
|
GLint status;
|
|
|
|
CoglShader *shader;
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
2010-08-02 16:40:55 -04:00
|
|
|
|
2009-05-12 09:15:18 -04:00
|
|
|
if (!cogl_is_shader (handle))
|
|
|
|
return FALSE;
|
2008-06-02 06:58:57 -04:00
|
|
|
|
2012-03-06 13:21:28 -05:00
|
|
|
shader = handle;
|
2008-06-02 06:58:57 -04:00
|
|
|
|
2010-08-04 12:53:51 -04:00
|
|
|
#ifdef HAVE_COGL_GL
|
|
|
|
if (shader->language == COGL_SHADER_LANGUAGE_ARBFP)
|
2009-05-12 09:15:18 -04:00
|
|
|
return TRUE;
|
|
|
|
else
|
2010-08-04 12:53:51 -04:00
|
|
|
#endif
|
|
|
|
{
|
2010-12-02 16:08:30 -05:00
|
|
|
/* FIXME: We currently have an arbitrary limit of 4 texture
|
|
|
|
* coordinate attributes since our API means we have to add
|
|
|
|
* some boilerplate to the users GLSL program (for GLES2)
|
|
|
|
* before we actually know how many attributes are in use.
|
|
|
|
*
|
|
|
|
* 4 will probably be enough (or at least that limitation should
|
|
|
|
* be enough until we can replace this API with the pipeline
|
|
|
|
* snippets API) but if it isn't then the shader won't compile,
|
|
|
|
* through no fault of the user.
|
|
|
|
*
|
|
|
|
* To some extent this is just a symptom of bad API design; it
|
|
|
|
* was a mistake for Cogl to so thinly wrap the OpenGL shader
|
|
|
|
* API. Eventually we plan for this whole API will be deprecated
|
|
|
|
* by the pipeline snippets framework.
|
|
|
|
*/
|
|
|
|
if (!shader->gl_handle)
|
|
|
|
_cogl_shader_compile_real (handle, 4);
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE (ctx, glGetShaderiv (shader->gl_handle, GL_COMPILE_STATUS, &status));
|
2010-08-04 12:53:51 -04:00
|
|
|
if (status == GL_TRUE)
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-07 15:44:56 -04:00
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2011-01-17 07:21:33 -05:00
|
|
|
}
|