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

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/driver/gl/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
#include "cogl-program.h"
 
30
#include "cogl-shader-private.h"
 
31
#include "cogl-internal.h"
 
32
#include "cogl-handle.h"
 
33
#include "cogl-context.h"
 
34
#include "cogl-journal-private.h"
 
35
 
 
36
#include <glib.h>
 
37
 
 
38
#define glCreateProgramObject        ctx->drv.pf_glCreateProgramObject
 
39
#define glAttachObject               ctx->drv.pf_glAttachObject
 
40
#define glUseProgramObject           ctx->drv.pf_glUseProgramObject
 
41
#define glLinkProgram                ctx->drv.pf_glLinkProgram
 
42
#define glGetUniformLocation         ctx->drv.pf_glGetUniformLocation
 
43
#define glUniform1f                  ctx->drv.pf_glUniform1f
 
44
#define glUniform2f                  ctx->drv.pf_glUniform2f
 
45
#define glUniform3f                  ctx->drv.pf_glUniform3f
 
46
#define glUniform4f                  ctx->drv.pf_glUniform4f
 
47
#define glUniform1fv                 ctx->drv.pf_glUniform1fv
 
48
#define glUniform2fv                 ctx->drv.pf_glUniform2fv
 
49
#define glUniform3fv                 ctx->drv.pf_glUniform3fv
 
50
#define glUniform4fv                 ctx->drv.pf_glUniform4fv
 
51
#define glUniform1i                  ctx->drv.pf_glUniform1i
 
52
#define glUniform2i                  ctx->drv.pf_glUniform2i
 
53
#define glUniform3i                  ctx->drv.pf_glUniform3i
 
54
#define glUniform4i                  ctx->drv.pf_glUniform4i
 
55
#define glUniform1iv                 ctx->drv.pf_glUniform1iv
 
56
#define glUniform2iv                 ctx->drv.pf_glUniform2iv
 
57
#define glUniform3iv                 ctx->drv.pf_glUniform3iv
 
58
#define glUniform4iv                 ctx->drv.pf_glUniform4iv
 
59
#define glUniformMatrix2fv           ctx->drv.pf_glUniformMatrix2fv
 
60
#define glUniformMatrix3fv           ctx->drv.pf_glUniformMatrix3fv
 
61
#define glUniformMatrix4fv           ctx->drv.pf_glUniformMatrix4fv
 
62
#define glDeleteObject               ctx->drv.pf_glDeleteObject
 
63
 
 
64
static void _cogl_program_free (CoglProgram *program);
 
65
 
 
66
COGL_HANDLE_DEFINE (Program, program);
 
67
 
 
68
static void
 
69
_cogl_program_free (CoglProgram *program)
 
70
{
 
71
  /* Frees program resources but its handle is not
 
72
     released! Do that separately before this! */
 
73
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
74
  glDeleteObject (program->gl_handle);
 
75
}
 
76
 
 
77
CoglHandle
 
78
cogl_create_program (void)
 
79
{
 
80
  CoglProgram *program;
 
81
  _COGL_GET_CONTEXT (ctx, 0);
 
82
 
 
83
  program = g_slice_new (CoglProgram);
 
84
  program->gl_handle = glCreateProgramObject ();
 
85
 
 
86
  return _cogl_program_handle_new (program);
 
87
}
 
88
 
 
89
void
 
90
cogl_program_attach_shader (CoglHandle program_handle,
 
91
                            CoglHandle shader_handle)
 
92
{
 
93
  CoglProgram *program;
 
94
  CoglShader *shader;
 
95
 
 
96
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
97
 
 
98
  if (!cogl_is_program (program_handle) || !cogl_is_shader (shader_handle))
 
99
    return;
 
100
 
 
101
  program = _cogl_program_pointer_from_handle (program_handle);
 
102
  shader = _cogl_shader_pointer_from_handle (shader_handle);
 
103
 
 
104
  glAttachObject (program->gl_handle, shader->gl_handle);
 
105
}
 
106
 
 
107
void
 
108
cogl_program_link (CoglHandle handle)
 
109
{
 
110
  CoglProgram *program;
 
111
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
112
 
 
113
  if (!cogl_is_program (handle))
 
114
    return;
 
115
 
 
116
  program = _cogl_program_pointer_from_handle (handle);
 
117
 
 
118
  glLinkProgram (program->gl_handle);
 
119
}
 
120
 
 
121
void
 
122
cogl_program_use (CoglHandle handle)
 
123
{
 
124
  CoglProgram *program;
 
125
  GLhandleARB gl_handle;
 
126
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
127
 
 
128
  if (handle != COGL_INVALID_HANDLE && !cogl_is_program (handle))
 
129
    return;
 
130
 
 
131
  /* The Cogl journal doesn't currently cope with the use of
 
132
   * shaders so we have to flush all priitives whenever the
 
133
   * current shader changes... */
 
134
  _cogl_journal_flush ();
 
135
 
 
136
  if (handle == COGL_INVALID_HANDLE)
 
137
    gl_handle = 0;
 
138
  else
 
139
    {
 
140
      program = _cogl_program_pointer_from_handle (handle);
 
141
      gl_handle = program->gl_handle;
 
142
    }
 
143
 
 
144
  glUseProgramObject (gl_handle);
 
145
}
 
146
 
 
147
int
 
148
cogl_program_get_uniform_location (CoglHandle   handle,
 
149
                                   const char *uniform_name)
 
150
{
 
151
  CoglProgram *program;
 
152
  _COGL_GET_CONTEXT (ctx, 0);
 
153
 
 
154
  if (!cogl_is_program (handle))
 
155
    return 0;
 
156
 
 
157
  program = _cogl_program_pointer_from_handle (handle);
 
158
 
 
159
  return glGetUniformLocation (program->gl_handle, uniform_name);
 
160
}
 
161
 
 
162
void
 
163
cogl_program_uniform_1f (int uniform_no,
 
164
                         float  value)
 
165
{
 
166
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
167
  glUniform1f (uniform_no, value);
 
168
}
 
169
 
 
170
void
 
171
cogl_program_uniform_1i (int uniform_no,
 
172
                         int    value)
 
173
{
 
174
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
175
  glUniform1i (uniform_no, value);
 
176
}
 
177
 
 
178
void
 
179
cogl_program_uniform_float (int  uniform_no,
 
180
                            int     size,
 
181
                            int     count,
 
182
                            const GLfloat *value)
 
183
{
 
184
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
185
 
 
186
  switch (size)
 
187
    {
 
188
    case 1:
 
189
      glUniform1fv (uniform_no, count, value);
 
190
      break;
 
191
    case 2:
 
192
      glUniform2fv (uniform_no, count, value);
 
193
      break;
 
194
    case 3:
 
195
      glUniform3fv (uniform_no, count, value);
 
196
      break;
 
197
    case 4:
 
198
      glUniform4fv (uniform_no, count, value);
 
199
      break;
 
200
    default:
 
201
      g_warning ("%s called with invalid size parameter", G_STRFUNC);
 
202
    }
 
203
}
 
204
 
 
205
void
 
206
cogl_program_uniform_int (int  uniform_no,
 
207
                          int     size,
 
208
                          int     count,
 
209
                          const int *value)
 
210
{
 
211
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
212
 
 
213
  switch (size)
 
214
    {
 
215
    case 1:
 
216
      glUniform1iv (uniform_no, count, value);
 
217
      break;
 
218
    case 2:
 
219
      glUniform2iv (uniform_no, count, value);
 
220
      break;
 
221
    case 3:
 
222
      glUniform3iv (uniform_no, count, value);
 
223
      break;
 
224
    case 4:
 
225
      glUniform4iv (uniform_no, count, value);
 
226
      break;
 
227
    default:
 
228
      g_warning ("%s called with invalid size parameter", G_STRFUNC);
 
229
    }
 
230
}
 
231
 
 
232
void
 
233
cogl_program_uniform_matrix (int   uniform_no,
 
234
                             int      size,
 
235
                             int      count,
 
236
                             gboolean  transpose,
 
237
                             const GLfloat  *value)
 
238
{
 
239
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
240
 
 
241
  switch (size)
 
242
    {
 
243
    case 2 :
 
244
      glUniformMatrix2fv (uniform_no, count, transpose, value);
 
245
      break;
 
246
    case 3 :
 
247
      glUniformMatrix3fv (uniform_no, count, transpose, value);
 
248
      break;
 
249
    case 4 :
 
250
      glUniformMatrix4fv (uniform_no, count, transpose, value);
 
251
      break;
 
252
    default :
 
253
      g_warning ("%s called with invalid size parameter", G_STRFUNC);
 
254
    }
 
255
}