Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / mesa / main / shaderobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file shaderobj.c
27 * \author Brian Paul
28 *
29 */
30
31
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/hash.h"
35 #include "main/shaderobj.h"
36 #include "program/program.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_uniform.h"
39 #include "talloc.h"
40
41 /**********************************************************************/
42 /*** Shader object functions ***/
43 /**********************************************************************/
44
45
46 /**
47 * Set ptr to point to sh.
48 * If ptr is pointing to another shader, decrement its refcount (and delete
49 * if refcount hits zero).
50 * Then set ptr to point to sh, incrementing its refcount.
51 */
52 void
53 _mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
54 struct gl_shader *sh)
55 {
56 assert(ptr);
57 if (*ptr == sh) {
58 /* no-op */
59 return;
60 }
61 if (*ptr) {
62 /* Unreference the old shader */
63 GLboolean deleteFlag = GL_FALSE;
64 struct gl_shader *old = *ptr;
65
66 ASSERT(old->RefCount > 0);
67 old->RefCount--;
68 /*printf("SHADER DECR %p (%d) to %d\n",
69 (void*) old, old->Name, old->RefCount);*/
70 deleteFlag = (old->RefCount == 0);
71
72 if (deleteFlag) {
73 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
74 ctx->Driver.DeleteShader(ctx, old);
75 }
76
77 *ptr = NULL;
78 }
79 assert(!*ptr);
80
81 if (sh) {
82 /* reference new */
83 sh->RefCount++;
84 /*printf("SHADER INCR %p (%d) to %d\n",
85 (void*) sh, sh->Name, sh->RefCount);*/
86 *ptr = sh;
87 }
88 }
89
90
91 /**
92 * Allocate a new gl_shader object, initialize it.
93 * Called via ctx->Driver.NewShader()
94 */
95 struct gl_shader *
96 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
97 {
98 struct gl_shader *shader;
99 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER ||
100 type == GL_GEOMETRY_SHADER_ARB);
101 shader = talloc_zero(NULL, struct gl_shader);
102 if (shader) {
103 shader->Type = type;
104 shader->Name = name;
105 shader->RefCount = 1;
106 }
107 return shader;
108 }
109
110
111 /**
112 * Delete a shader object.
113 * Called via ctx->Driver.DeleteShader().
114 */
115 static void
116 __mesa_delete_shader(GLcontext *ctx, struct gl_shader *sh)
117 {
118 if (sh->Source)
119 free((void *) sh->Source);
120 _mesa_reference_program(ctx, &sh->Program, NULL);
121 talloc_free(sh);
122 }
123
124
125 /**
126 * Lookup a GLSL shader object.
127 */
128 struct gl_shader *
129 _mesa_lookup_shader(GLcontext *ctx, GLuint name)
130 {
131 if (name) {
132 struct gl_shader *sh = (struct gl_shader *)
133 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
134 /* Note that both gl_shader and gl_shader_program objects are kept
135 * in the same hash table. Check the object's type to be sure it's
136 * what we're expecting.
137 */
138 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
139 return NULL;
140 }
141 return sh;
142 }
143 return NULL;
144 }
145
146
147 /**
148 * As above, but record an error if shader is not found.
149 */
150 struct gl_shader *
151 _mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
152 {
153 if (!name) {
154 _mesa_error(ctx, GL_INVALID_VALUE, caller);
155 return NULL;
156 }
157 else {
158 struct gl_shader *sh = (struct gl_shader *)
159 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
160 if (!sh) {
161 _mesa_error(ctx, GL_INVALID_VALUE, caller);
162 return NULL;
163 }
164 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
165 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
166 return NULL;
167 }
168 return sh;
169 }
170 }
171
172
173
174 /**********************************************************************/
175 /*** Shader Program object functions ***/
176 /**********************************************************************/
177
178
179 /**
180 * Set ptr to point to shProg.
181 * If ptr is pointing to another object, decrement its refcount (and delete
182 * if refcount hits zero).
183 * Then set ptr to point to shProg, incrementing its refcount.
184 */
185 void
186 _mesa_reference_shader_program(GLcontext *ctx,
187 struct gl_shader_program **ptr,
188 struct gl_shader_program *shProg)
189 {
190 assert(ptr);
191 if (*ptr == shProg) {
192 /* no-op */
193 return;
194 }
195 if (*ptr) {
196 /* Unreference the old shader program */
197 GLboolean deleteFlag = GL_FALSE;
198 struct gl_shader_program *old = *ptr;
199
200 ASSERT(old->RefCount > 0);
201 old->RefCount--;
202 #if 0
203 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
204 (void *) old, old->Name, old->RefCount);
205 #endif
206 deleteFlag = (old->RefCount == 0);
207
208 if (deleteFlag) {
209 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
210 ctx->Driver.DeleteShaderProgram(ctx, old);
211 }
212
213 *ptr = NULL;
214 }
215 assert(!*ptr);
216
217 if (shProg) {
218 shProg->RefCount++;
219 #if 0
220 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
221 (void *) shProg, shProg->Name, shProg->RefCount);
222 #endif
223 *ptr = shProg;
224 }
225 }
226
227
228 /**
229 * Allocate a new gl_shader_program object, initialize it.
230 * Called via ctx->Driver.NewShaderProgram()
231 */
232 static struct gl_shader_program *
233 _mesa_new_shader_program(GLcontext *ctx, GLuint name)
234 {
235 struct gl_shader_program *shProg;
236 shProg = talloc_zero(NULL, struct gl_shader_program);
237 if (shProg) {
238 shProg->Type = GL_SHADER_PROGRAM_MESA;
239 shProg->Name = name;
240 shProg->RefCount = 1;
241 shProg->Attributes = _mesa_new_parameter_list();
242 #if FEATURE_ARB_geometry_shader4
243 shProg->Geom.VerticesOut = 0;
244 shProg->Geom.InputType = GL_TRIANGLES;
245 shProg->Geom.OutputType = GL_TRIANGLE_STRIP;
246 #endif
247 }
248 return shProg;
249 }
250
251
252 /**
253 * Clear (free) the shader program state that gets produced by linking.
254 */
255 void
256 _mesa_clear_shader_program_data(GLcontext *ctx,
257 struct gl_shader_program *shProg)
258 {
259 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
260 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
261 _mesa_reference_geomprog(ctx, &shProg->GeometryProgram, NULL);
262
263 if (shProg->Uniforms) {
264 _mesa_free_uniform_list(shProg->Uniforms);
265 shProg->Uniforms = NULL;
266 }
267
268 if (shProg->Varying) {
269 _mesa_free_parameter_list(shProg->Varying);
270 shProg->Varying = NULL;
271 }
272 }
273
274
275 /**
276 * Free all the data that hangs off a shader program object, but not the
277 * object itself.
278 */
279 void
280 _mesa_free_shader_program_data(GLcontext *ctx,
281 struct gl_shader_program *shProg)
282 {
283 GLuint i;
284
285 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
286
287 _mesa_clear_shader_program_data(ctx, shProg);
288
289 if (shProg->Attributes) {
290 _mesa_free_parameter_list(shProg->Attributes);
291 shProg->Attributes = NULL;
292 }
293
294 /* detach shaders */
295 for (i = 0; i < shProg->NumShaders; i++) {
296 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
297 }
298 shProg->NumShaders = 0;
299
300 if (shProg->Shaders) {
301 free(shProg->Shaders);
302 shProg->Shaders = NULL;
303 }
304
305 if (shProg->InfoLog) {
306 talloc_free(shProg->InfoLog);
307 shProg->InfoLog = NULL;
308 }
309
310 /* Transform feedback varying vars */
311 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
312 free(shProg->TransformFeedback.VaryingNames[i]);
313 }
314 free(shProg->TransformFeedback.VaryingNames);
315 shProg->TransformFeedback.VaryingNames = NULL;
316 shProg->TransformFeedback.NumVarying = 0;
317 }
318
319
320 /**
321 * Free/delete a shader program object.
322 * Called via ctx->Driver.DeleteShaderProgram().
323 */
324 static void
325 __mesa_delete_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
326 {
327 _mesa_free_shader_program_data(ctx, shProg);
328
329 talloc_free(shProg);
330 }
331
332
333 /**
334 * Lookup a GLSL program object.
335 */
336 struct gl_shader_program *
337 _mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
338 {
339 struct gl_shader_program *shProg;
340 if (name) {
341 shProg = (struct gl_shader_program *)
342 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
343 /* Note that both gl_shader and gl_shader_program objects are kept
344 * in the same hash table. Check the object's type to be sure it's
345 * what we're expecting.
346 */
347 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
348 return NULL;
349 }
350 return shProg;
351 }
352 return NULL;
353 }
354
355
356 /**
357 * As above, but record an error if program is not found.
358 */
359 struct gl_shader_program *
360 _mesa_lookup_shader_program_err(GLcontext *ctx, GLuint name,
361 const char *caller)
362 {
363 if (!name) {
364 _mesa_error(ctx, GL_INVALID_VALUE, caller);
365 return NULL;
366 }
367 else {
368 struct gl_shader_program *shProg = (struct gl_shader_program *)
369 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
370 if (!shProg) {
371 _mesa_error(ctx, GL_INVALID_VALUE, caller);
372 return NULL;
373 }
374 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
375 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
376 return NULL;
377 }
378 return shProg;
379 }
380 }
381
382
383 void
384 _mesa_init_shader_object_functions(struct dd_function_table *driver)
385 {
386 driver->NewShader = _mesa_new_shader;
387 driver->DeleteShader = __mesa_delete_shader;
388 driver->NewShaderProgram = _mesa_new_shader_program;
389 driver->DeleteShaderProgram = __mesa_delete_shader_program;
390 }