~ubuntu-branches/ubuntu/vivid/clutter-1.0/vivid-proposed

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/driver/gl/cogl-shader.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-07-18 17:21:49 UTC
  • mfrom: (1.2.1 upstream) (4.1.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20100718172149-j6s9u4chocaoykme
Tags: 1.2.12-1
* New upstream release.
* debian/libclutter-1.0-0.symbols,
  debian/rules:
  - Add a symbols file.
* debian/rules,
  debian/source/format:
  - Switch to source format 3.0 (quilt).
* debian/control.in:
  - Standards-Version is 3.9.0, no changes needed.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cogl
 
3
 *
 
4
 * An object oriented GL/GLES Abstraction/Utility Layer
 
5
 *
 
6
 * Copyright (C) 2007,2008,2009 Intel Corporation.
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
20
 *
 
21
 *
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include "config.h"
 
26
#endif
 
27
 
 
28
#include "cogl.h"
 
29
#include "cogl-shader-private.h"
 
30
#include "cogl-internal.h"
 
31
#include "cogl-context.h"
 
32
#include "cogl-handle.h"
 
33
 
 
34
#include <glib.h>
 
35
 
 
36
#define glCreateShaderObject         ctx->drv.pf_glCreateShaderObject
 
37
#define glGetObjectParameteriv       ctx->drv.pf_glGetObjectParameteriv
 
38
#define glGetInfoLog                 ctx->drv.pf_glGetInfoLog
 
39
#define glCompileShader              ctx->drv.pf_glCompileShader
 
40
#define glShaderSource               ctx->drv.pf_glShaderSource
 
41
#define glDeleteObject               ctx->drv.pf_glDeleteObject
 
42
 
 
43
static void _cogl_shader_free (CoglShader *shader);
 
44
 
 
45
COGL_HANDLE_DEFINE (Shader, shader);
 
46
 
 
47
static void
 
48
_cogl_shader_free (CoglShader *shader)
 
49
{
 
50
  /* Frees shader resources but its handle is not
 
51
     released! Do that separately before this! */
 
52
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
53
  glDeleteObject (shader->gl_handle);
 
54
}
 
55
 
 
56
CoglHandle
 
57
cogl_create_shader (CoglShaderType type)
 
58
{
 
59
  CoglShader *shader;
 
60
  GLenum gl_type;
 
61
 
 
62
  _COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE);
 
63
 
 
64
  if (type == COGL_SHADER_TYPE_VERTEX)
 
65
    gl_type = GL_VERTEX_SHADER;
 
66
  else if (type == COGL_SHADER_TYPE_FRAGMENT)
 
67
    gl_type = GL_FRAGMENT_SHADER;
 
68
  else
 
69
    {
 
70
      g_warning ("Unexpected shader type (0x%08lX) given to "
 
71
                 "cogl_create_shader", (unsigned long) type);
 
72
      return COGL_INVALID_HANDLE;
 
73
    }
 
74
 
 
75
  shader = g_slice_new (CoglShader);
 
76
  shader->gl_handle = glCreateShaderObject (gl_type);
 
77
 
 
78
  return _cogl_shader_handle_new (shader);
 
79
}
 
80
 
 
81
void
 
82
cogl_shader_source (CoglHandle   handle,
 
83
                    const char  *source)
 
84
{
 
85
  CoglShader *shader;
 
86
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
87
 
 
88
  if (!cogl_is_shader (handle))
 
89
    return;
 
90
 
 
91
  shader = _cogl_shader_pointer_from_handle (handle);
 
92
 
 
93
  glShaderSource (shader->gl_handle, 1, &source, NULL);
 
94
}
 
95
 
 
96
void
 
97
cogl_shader_compile (CoglHandle handle)
 
98
{
 
99
  CoglShader *shader;
 
100
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
101
 
 
102
  if (!cogl_is_shader (handle))
 
103
    return;
 
104
 
 
105
  shader = _cogl_shader_pointer_from_handle (handle);
 
106
 
 
107
  glCompileShader (shader->gl_handle);
 
108
}
 
109
 
 
110
char *
 
111
cogl_shader_get_info_log (CoglHandle handle)
 
112
{
 
113
  CoglShader *shader;
 
114
  char buffer[512];
 
115
  int len;
 
116
  _COGL_GET_CONTEXT (ctx, NULL);
 
117
 
 
118
  if (!cogl_is_shader (handle))
 
119
    return NULL;
 
120
 
 
121
  shader = _cogl_shader_pointer_from_handle (handle);
 
122
 
 
123
  glGetInfoLog (shader->gl_handle, 511, &len, buffer);
 
124
  buffer[len]='\0';
 
125
 
 
126
  return g_strdup (buffer);
 
127
}
 
128
 
 
129
CoglShaderType
 
130
cogl_shader_get_type (CoglHandle  handle)
 
131
{
 
132
  GLint type;
 
133
  CoglShader *shader;
 
134
 
 
135
  _COGL_GET_CONTEXT (ctx, COGL_SHADER_TYPE_VERTEX);
 
136
 
 
137
  if (!cogl_is_shader (handle))
 
138
    {
 
139
      g_warning ("Non shader handle type passed to cogl_shader_get_type");
 
140
      return COGL_SHADER_TYPE_VERTEX;
 
141
    }
 
142
 
 
143
  shader = _cogl_shader_pointer_from_handle (handle);
 
144
 
 
145
  GE (glGetObjectParameteriv (shader->gl_handle, GL_SHADER_TYPE, &type));
 
146
  if (type == GL_VERTEX_SHADER)
 
147
    return COGL_SHADER_TYPE_VERTEX;
 
148
  else if (type == GL_FRAGMENT_SHADER)
 
149
    return COGL_SHADER_TYPE_VERTEX;
 
150
  else
 
151
    {
 
152
      g_warning ("Unexpected shader type 0x%08lX", (unsigned long)type);
 
153
      return COGL_SHADER_TYPE_VERTEX;
 
154
    }
 
155
}
 
156
 
 
157
gboolean
 
158
cogl_shader_is_compiled (CoglHandle handle)
 
159
{
 
160
  GLint status;
 
161
  CoglShader *shader;
 
162
 
 
163
  _COGL_GET_CONTEXT (ctx, FALSE);
 
164
 
 
165
  if (!cogl_is_shader (handle))
 
166
    return FALSE;
 
167
 
 
168
  shader = _cogl_shader_pointer_from_handle (handle);
 
169
 
 
170
  GE (glGetObjectParameteriv (shader->gl_handle, GL_COMPILE_STATUS, &status));
 
171
  if (status == GL_TRUE)
 
172
    return TRUE;
 
173
  else
 
174
    return FALSE;
 
175
}
 
176