b6ccc031d054af6c4051fad60bc3d326a865ff0b
[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
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 static 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 shader = CALLOC_STRUCT(gl_shader);
101 if (shader) {
102 shader->Type = type;
103 shader->Name = name;
104 shader->RefCount = 1;
105 }
106 return shader;
107 }
108
109
110 /**
111 * Delete a shader object.
112 * Called via ctx->Driver.DeleteShader().
113 */
114 static void
115 __mesa_delete_shader(GLcontext *ctx, struct gl_shader *sh)
116 {
117 if (sh->Source)
118 free((void *) sh->Source);
119 if (sh->InfoLog)
120 free(sh->InfoLog);
121 _mesa_reference_program(ctx, &sh->Program, NULL);
122 free(sh);
123 }
124
125
126 /**
127 * Lookup a GLSL shader object.
128 */
129 struct gl_shader *
130 _mesa_lookup_shader(GLcontext *ctx, GLuint name)
131 {
132 if (name) {
133 struct gl_shader *sh = (struct gl_shader *)
134 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
135 /* Note that both gl_shader and gl_shader_program objects are kept
136 * in the same hash table. Check the object's type to be sure it's
137 * what we're expecting.
138 */
139 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
140 return NULL;
141 }
142 return sh;
143 }
144 return NULL;
145 }
146
147
148 /**
149 * As above, but record an error if shader is not found.
150 */
151 struct gl_shader *
152 _mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
153 {
154 if (!name) {
155 _mesa_error(ctx, GL_INVALID_VALUE, caller);
156 return NULL;
157 }
158 else {
159 struct gl_shader *sh = (struct gl_shader *)
160 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
161 if (!sh) {
162 _mesa_error(ctx, GL_INVALID_VALUE, caller);
163 return NULL;
164 }
165 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
166 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
167 return NULL;
168 }
169 return sh;
170 }
171 }
172
173
174
175 /**********************************************************************/
176 /*** Shader Program object functions ***/
177 /**********************************************************************/
178
179
180 /**
181 * Set ptr to point to shProg.
182 * If ptr is pointing to another object, decrement its refcount (and delete
183 * if refcount hits zero).
184 * Then set ptr to point to shProg, incrementing its refcount.
185 */
186 void
187 _mesa_reference_shader_program(GLcontext *ctx,
188 struct gl_shader_program **ptr,
189 struct gl_shader_program *shProg)
190 {
191 assert(ptr);
192 if (*ptr == shProg) {
193 /* no-op */
194 return;
195 }
196 if (*ptr) {
197 /* Unreference the old shader program */
198 GLboolean deleteFlag = GL_FALSE;
199 struct gl_shader_program *old = *ptr;
200
201 ASSERT(old->RefCount > 0);
202 old->RefCount--;
203 #if 0
204 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
205 (void *) old, old->Name, old->RefCount);
206 #endif
207 deleteFlag = (old->RefCount == 0);
208
209 if (deleteFlag) {
210 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
211 ctx->Driver.DeleteShaderProgram(ctx, old);
212 }
213
214 *ptr = NULL;
215 }
216 assert(!*ptr);
217
218 if (shProg) {
219 shProg->RefCount++;
220 #if 0
221 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
222 (void *) shProg, shProg->Name, shProg->RefCount);
223 #endif
224 *ptr = shProg;
225 }
226 }
227
228
229 /**
230 * Allocate a new gl_shader_program object, initialize it.
231 * Called via ctx->Driver.NewShaderProgram()
232 */
233 static struct gl_shader_program *
234 _mesa_new_shader_program(GLcontext *ctx, GLuint name)
235 {
236 struct gl_shader_program *shProg;
237 shProg = CALLOC_STRUCT(gl_shader_program);
238 if (shProg) {
239 shProg->Type = GL_SHADER_PROGRAM_MESA;
240 shProg->Name = name;
241 shProg->RefCount = 1;
242 shProg->Attributes = _mesa_new_parameter_list();
243 }
244 return shProg;
245 }
246
247
248 /**
249 * Clear (free) the shader program state that gets produced by linking.
250 */
251 void
252 _mesa_clear_shader_program_data(GLcontext *ctx,
253 struct gl_shader_program *shProg)
254 {
255 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
256 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
257
258 if (shProg->Uniforms) {
259 _mesa_free_uniform_list(shProg->Uniforms);
260 shProg->Uniforms = NULL;
261 }
262
263 if (shProg->Varying) {
264 _mesa_free_parameter_list(shProg->Varying);
265 shProg->Varying = NULL;
266 }
267 }
268
269
270 /**
271 * Free all the data that hangs off a shader program object, but not the
272 * object itself.
273 */
274 void
275 _mesa_free_shader_program_data(GLcontext *ctx,
276 struct gl_shader_program *shProg)
277 {
278 GLuint i;
279
280 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
281
282 _mesa_clear_shader_program_data(ctx, shProg);
283
284 if (shProg->Attributes) {
285 _mesa_free_parameter_list(shProg->Attributes);
286 shProg->Attributes = NULL;
287 }
288
289 /* detach shaders */
290 for (i = 0; i < shProg->NumShaders; i++) {
291 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
292 }
293 shProg->NumShaders = 0;
294
295 if (shProg->Shaders) {
296 free(shProg->Shaders);
297 shProg->Shaders = NULL;
298 }
299
300 if (shProg->InfoLog) {
301 free(shProg->InfoLog);
302 shProg->InfoLog = NULL;
303 }
304
305 /* Transform feedback varying vars */
306 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
307 free(shProg->TransformFeedback.VaryingNames[i]);
308 }
309 free(shProg->TransformFeedback.VaryingNames);
310 shProg->TransformFeedback.VaryingNames = NULL;
311 shProg->TransformFeedback.NumVarying = 0;
312 }
313
314
315 /**
316 * Free/delete a shader program object.
317 * Called via ctx->Driver.DeleteShaderProgram().
318 */
319 static void
320 __mesa_delete_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
321 {
322 _mesa_free_shader_program_data(ctx, shProg);
323
324 free(shProg);
325 }
326
327
328 /**
329 * Lookup a GLSL program object.
330 */
331 struct gl_shader_program *
332 _mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
333 {
334 struct gl_shader_program *shProg;
335 if (name) {
336 shProg = (struct gl_shader_program *)
337 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
338 /* Note that both gl_shader and gl_shader_program objects are kept
339 * in the same hash table. Check the object's type to be sure it's
340 * what we're expecting.
341 */
342 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
343 return NULL;
344 }
345 return shProg;
346 }
347 return NULL;
348 }
349
350
351 /**
352 * As above, but record an error if program is not found.
353 */
354 struct gl_shader_program *
355 _mesa_lookup_shader_program_err(GLcontext *ctx, GLuint name,
356 const char *caller)
357 {
358 if (!name) {
359 _mesa_error(ctx, GL_INVALID_VALUE, caller);
360 return NULL;
361 }
362 else {
363 struct gl_shader_program *shProg = (struct gl_shader_program *)
364 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
365 if (!shProg) {
366 _mesa_error(ctx, GL_INVALID_VALUE, caller);
367 return NULL;
368 }
369 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
370 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
371 return NULL;
372 }
373 return shProg;
374 }
375 }
376
377
378 void
379 _mesa_init_shader_object_functions(struct dd_function_table *driver)
380 {
381 driver->NewShader = _mesa_new_shader;
382 driver->DeleteShader = __mesa_delete_shader;
383 driver->NewShaderProgram = _mesa_new_shader_program;
384 driver->DeleteShaderProgram = __mesa_delete_shader_program;
385 }