~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/driver/gles/cogl-program.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

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) 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
 
 
30
#include "cogl-internal.h"
 
31
#include "cogl-context.h"
 
32
#include "cogl-handle.h"
 
33
 
 
34
#ifdef HAVE_COGL_GLES2
 
35
 
 
36
#include <string.h>
 
37
 
 
38
#include "cogl-shader-private.h"
 
39
#include "cogl-program.h"
 
40
 
 
41
static void _cogl_program_free (CoglProgram *program);
 
42
 
 
43
COGL_HANDLE_DEFINE (Program, program);
 
44
 
 
45
static void
 
46
_cogl_program_free (CoglProgram *program)
 
47
{
 
48
  int i;
 
49
 
 
50
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
51
 
 
52
  /* Unref all of the attached shaders */
 
53
  g_slist_foreach (program->attached_shaders, (GFunc) cogl_handle_unref, NULL);
 
54
  /* Destroy the list */
 
55
  g_slist_free (program->attached_shaders);
 
56
 
 
57
  _cogl_gles2_clear_cache_for_program ((CoglHandle) program);
 
58
 
 
59
  if (ctx->drv.gles2.settings.user_program == (CoglHandle) program)
 
60
    {
 
61
      ctx->drv.gles2.settings.user_program = COGL_INVALID_HANDLE;
 
62
      ctx->drv.gles2.settings_dirty = TRUE;
 
63
    }
 
64
 
 
65
  for (i = 0; i < COGL_GLES2_NUM_CUSTOM_UNIFORMS; i++)
 
66
    if (program->custom_uniform_names[i])
 
67
      g_free (program->custom_uniform_names[i]);
 
68
}
 
69
 
 
70
CoglHandle
 
71
cogl_create_program (void)
 
72
{
 
73
  CoglProgram *program;
 
74
 
 
75
  program = g_slice_new (CoglProgram);
 
76
  program->attached_shaders = NULL;
 
77
  memset (program->custom_uniform_names, 0,
 
78
          COGL_GLES2_NUM_CUSTOM_UNIFORMS * sizeof (char *));
 
79
 
 
80
  return _cogl_program_handle_new (program);
 
81
}
 
82
 
 
83
void
 
84
cogl_program_attach_shader (CoglHandle program_handle,
 
85
                            CoglHandle shader_handle)
 
86
{
 
87
  CoglProgram *program;
 
88
 
 
89
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
90
 
 
91
  if (!cogl_is_program (program_handle) || !cogl_is_shader (shader_handle))
 
92
    return;
 
93
 
 
94
  program = _cogl_program_pointer_from_handle (program_handle);
 
95
  program->attached_shaders
 
96
    = g_slist_prepend (program->attached_shaders,
 
97
                       cogl_handle_ref (shader_handle));
 
98
 
 
99
  /* Whenever the shader changes we will need to relink the program
 
100
     with the fixed functionality shaders so we should forget the
 
101
     cached programs */
 
102
  _cogl_gles2_clear_cache_for_program (program);
 
103
}
 
104
 
 
105
void
 
106
cogl_program_link (CoglHandle handle)
 
107
{
 
108
  /* There's no point in linking the program here because it will have
 
109
     to be relinked with a different fixed functionality shader
 
110
     whenever the settings change */
 
111
}
 
112
 
 
113
void
 
114
cogl_program_use (CoglHandle handle)
 
115
{
 
116
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
117
 
 
118
  if (handle != COGL_INVALID_HANDLE && !cogl_is_program (handle))
 
119
    return;
 
120
 
 
121
  ctx->drv.gles2.settings.user_program = handle;
 
122
  ctx->drv.gles2.settings_dirty = TRUE;
 
123
}
 
124
 
 
125
int
 
126
cogl_program_get_uniform_location (CoglHandle   handle,
 
127
                                   const char *uniform_name)
 
128
{
 
129
  int i;
 
130
  CoglProgram *program;
 
131
 
 
132
  if (!cogl_is_program (handle))
 
133
    return -1;
 
134
 
 
135
  program = _cogl_program_pointer_from_handle (handle);
 
136
 
 
137
  /* We can't just ask the GL program object for the uniform location
 
138
     directly because it will change every time the program is linked
 
139
     with a new fixed functionality shader. Instead we make our own
 
140
     mapping of uniform numbers and cache the names */
 
141
  for (i = 0; program->custom_uniform_names[i]
 
142
         && i < COGL_GLES2_NUM_CUSTOM_UNIFORMS; i++)
 
143
    if (!strcmp (program->custom_uniform_names[i], uniform_name))
 
144
      return i;
 
145
 
 
146
  if (i < COGL_GLES2_NUM_CUSTOM_UNIFORMS)
 
147
    {
 
148
      program->custom_uniform_names[i] = g_strdup (uniform_name);
 
149
      return i;
 
150
    }
 
151
  else
 
152
    /* We've run out of space for new uniform names so just pretend it
 
153
       isn't there */
 
154
    return -1;
 
155
}
 
156
 
 
157
void
 
158
cogl_program_uniform_1f (int uniform_no,
 
159
                         float  value)
 
160
{
 
161
  cogl_program_uniform_float (uniform_no, 1, 1, &value);
 
162
}
 
163
 
 
164
void
 
165
cogl_program_uniform_1i (int uniform_no,
 
166
                         int    value)
 
167
{
 
168
  cogl_program_uniform_int (uniform_no, 1, 1, &value);
 
169
}
 
170
 
 
171
static void
 
172
cogl_program_uniform_x (int uniform_no,
 
173
                        int size,
 
174
                        int count,
 
175
                        CoglBoxedType type,
 
176
                        gsize value_size,
 
177
                        gconstpointer value)
 
178
{
 
179
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
180
 
 
181
  if (uniform_no >= 0 && uniform_no < COGL_GLES2_NUM_CUSTOM_UNIFORMS
 
182
      && size >= 1 && size <= 4 && count >= 1)
 
183
    {
 
184
      CoglBoxedValue *bv = ctx->drv.gles2.custom_uniforms + uniform_no;
 
185
 
 
186
      if (count == 1)
 
187
        {
 
188
          if (bv->count > 1)
 
189
            g_free (bv->v.array);
 
190
 
 
191
          memcpy (bv->v.float_value, value, value_size);
 
192
        }
 
193
      else
 
194
        {
 
195
          if (bv->count > 1)
 
196
            {
 
197
              if (bv->count != count || bv->size != size || bv->type != type)
 
198
                {
 
199
                  g_free (bv->v.array);
 
200
                  bv->v.array = g_malloc (count * value_size);
 
201
                }
 
202
            }
 
203
          else
 
204
            bv->v.array = g_malloc (count * value_size);
 
205
 
 
206
          memcpy (bv->v.array, value, count * value_size);
 
207
        }
 
208
 
 
209
      bv->type = type;
 
210
      bv->size = size;
 
211
      bv->count = count;
 
212
 
 
213
      ctx->drv.gles2.dirty_custom_uniforms |= 1 << uniform_no;
 
214
    }
 
215
}
 
216
 
 
217
void
 
218
cogl_program_uniform_float (int  uniform_no,
 
219
                            int     size,
 
220
                            int     count,
 
221
                            const GLfloat *value)
 
222
{
 
223
  cogl_program_uniform_x (uniform_no, size, count, COGL_BOXED_FLOAT,
 
224
                          sizeof (float) * size, value);
 
225
}
 
226
 
 
227
void
 
228
cogl_program_uniform_int (int  uniform_no,
 
229
                          int   size,
 
230
                          int   count,
 
231
                          const GLint *value)
 
232
{
 
233
  cogl_program_uniform_x (uniform_no, size, count, COGL_BOXED_INT,
 
234
                          sizeof (int) * size, value);
 
235
}
 
236
 
 
237
void
 
238
cogl_program_uniform_matrix (int   uniform_no,
 
239
                             int      size,
 
240
                             int      count,
 
241
                             gboolean  transpose,
 
242
                             const GLfloat  *value)
 
243
{
 
244
  CoglBoxedValue *bv;
 
245
 
 
246
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
247
 
 
248
  bv = ctx->drv.gles2.custom_uniforms + uniform_no;
 
249
 
 
250
  cogl_program_uniform_x (uniform_no, size, count, COGL_BOXED_MATRIX,
 
251
                          sizeof (float) * size * size, value);
 
252
 
 
253
  bv->transpose = transpose;
 
254
}
 
255
 
 
256
#else /* HAVE_COGL_GLES2 */
 
257
 
 
258
/* No support on regular OpenGL 1.1 */
 
259
 
 
260
CoglHandle
 
261
cogl_create_program (void)
 
262
{
 
263
  return COGL_INVALID_HANDLE;
 
264
}
 
265
 
 
266
gboolean
 
267
cogl_is_program (CoglHandle handle)
 
268
{
 
269
  return FALSE;
 
270
}
 
271
 
 
272
CoglHandle
 
273
cogl_program_ref (CoglHandle handle)
 
274
{
 
275
  return COGL_INVALID_HANDLE;
 
276
}
 
277
 
 
278
void
 
279
cogl_program_unref (CoglHandle handle)
 
280
{
 
281
}
 
282
 
 
283
void
 
284
cogl_program_attach_shader (CoglHandle program_handle,
 
285
                            CoglHandle shader_handle)
 
286
{
 
287
}
 
288
 
 
289
void
 
290
cogl_program_link (CoglHandle program_handle)
 
291
{
 
292
}
 
293
 
 
294
void
 
295
cogl_program_use (CoglHandle program_handle)
 
296
{
 
297
}
 
298
 
 
299
int
 
300
cogl_program_get_uniform_location (CoglHandle   program_handle,
 
301
                                   const char *uniform_name)
 
302
{
 
303
  return 0;
 
304
}
 
305
 
 
306
void
 
307
cogl_program_uniform_1f (int uniform_no,
 
308
                         float  value)
 
309
{
 
310
}
 
311
 
 
312
void
 
313
cogl_program_uniform_float (int  uniform_no,
 
314
                            int     size,
 
315
                            int     count,
 
316
                            const GLfloat *value)
 
317
{
 
318
}
 
319
 
 
320
void
 
321
cogl_program_uniform_int (int  uniform_no,
 
322
                          int     size,
 
323
                          int     count,
 
324
                          const int *value)
 
325
{
 
326
}
 
327
 
 
328
void
 
329
cogl_program_uniform_matrix (int   uniform_no,
 
330
                             int      size,
 
331
                             int      count,
 
332
                             gboolean  transpose,
 
333
                             const GLfloat  *value)
 
334
{
 
335
}
 
336
 
 
337
 
 
338
#endif /* HAVE_COGL_GLES2 */