mesa: Use gl_shader_program::_LinkedShaders instead of FragmentProgram
[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/mfeatures.h"
36 #include "main/mtypes.h"
37 #include "main/shaderobj.h"
38 #include "program/program.h"
39 #include "program/prog_parameter.h"
40 #include "program/prog_uniform.h"
41 #include "program/hash_table.h"
42 #include "ralloc.h"
43
44 /**********************************************************************/
45 /*** Shader object functions ***/
46 /**********************************************************************/
47
48
49 /**
50 * Set ptr to point to sh.
51 * If ptr is pointing to another shader, decrement its refcount (and delete
52 * if refcount hits zero).
53 * Then set ptr to point to sh, incrementing its refcount.
54 */
55 void
56 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
57 struct gl_shader *sh)
58 {
59 assert(ptr);
60 if (*ptr == sh) {
61 /* no-op */
62 return;
63 }
64 if (*ptr) {
65 /* Unreference the old shader */
66 GLboolean deleteFlag = GL_FALSE;
67 struct gl_shader *old = *ptr;
68
69 ASSERT(old->RefCount > 0);
70 old->RefCount--;
71 /*printf("SHADER DECR %p (%d) to %d\n",
72 (void*) old, old->Name, old->RefCount);*/
73 deleteFlag = (old->RefCount == 0);
74
75 if (deleteFlag) {
76 if (old->Name != 0)
77 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
78 ctx->Driver.DeleteShader(ctx, old);
79 }
80
81 *ptr = NULL;
82 }
83 assert(!*ptr);
84
85 if (sh) {
86 /* reference new */
87 sh->RefCount++;
88 /*printf("SHADER INCR %p (%d) to %d\n",
89 (void*) sh, sh->Name, sh->RefCount);*/
90 *ptr = sh;
91 }
92 }
93
94 void
95 _mesa_init_shader(struct gl_context *ctx, struct gl_shader *shader)
96 {
97 shader->RefCount = 1;
98 }
99
100 /**
101 * Allocate a new gl_shader object, initialize it.
102 * Called via ctx->Driver.NewShader()
103 */
104 struct gl_shader *
105 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
106 {
107 struct gl_shader *shader;
108 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER ||
109 type == GL_GEOMETRY_SHADER_ARB);
110 shader = rzalloc(NULL, struct gl_shader);
111 if (shader) {
112 shader->Type = type;
113 shader->Name = name;
114 _mesa_init_shader(ctx, shader);
115 }
116 return shader;
117 }
118
119
120 /**
121 * Delete a shader object.
122 * Called via ctx->Driver.DeleteShader().
123 */
124 static void
125 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
126 {
127 if (sh->Source)
128 free((void *) sh->Source);
129 _mesa_reference_program(ctx, &sh->Program, NULL);
130 ralloc_free(sh);
131 }
132
133
134 /**
135 * Lookup a GLSL shader object.
136 */
137 struct gl_shader *
138 _mesa_lookup_shader(struct gl_context *ctx, GLuint name)
139 {
140 if (name) {
141 struct gl_shader *sh = (struct gl_shader *)
142 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
143 /* Note that both gl_shader and gl_shader_program objects are kept
144 * in the same hash table. Check the object's type to be sure it's
145 * what we're expecting.
146 */
147 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
148 return NULL;
149 }
150 return sh;
151 }
152 return NULL;
153 }
154
155
156 /**
157 * As above, but record an error if shader is not found.
158 */
159 struct gl_shader *
160 _mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
161 {
162 if (!name) {
163 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
164 return NULL;
165 }
166 else {
167 struct gl_shader *sh = (struct gl_shader *)
168 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
169 if (!sh) {
170 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
171 return NULL;
172 }
173 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
174 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
175 return NULL;
176 }
177 return sh;
178 }
179 }
180
181
182
183 /**********************************************************************/
184 /*** Shader Program object functions ***/
185 /**********************************************************************/
186
187
188 /**
189 * Set ptr to point to shProg.
190 * If ptr is pointing to another object, decrement its refcount (and delete
191 * if refcount hits zero).
192 * Then set ptr to point to shProg, incrementing its refcount.
193 */
194 void
195 _mesa_reference_shader_program(struct gl_context *ctx,
196 struct gl_shader_program **ptr,
197 struct gl_shader_program *shProg)
198 {
199 assert(ptr);
200 if (*ptr == shProg) {
201 /* no-op */
202 return;
203 }
204 if (*ptr) {
205 /* Unreference the old shader program */
206 GLboolean deleteFlag = GL_FALSE;
207 struct gl_shader_program *old = *ptr;
208
209 ASSERT(old->RefCount > 0);
210 old->RefCount--;
211 #if 0
212 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
213 (void *) old, old->Name, old->RefCount);
214 #endif
215 deleteFlag = (old->RefCount == 0);
216
217 if (deleteFlag) {
218 if (old->Name != 0)
219 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
220 ctx->Driver.DeleteShaderProgram(ctx, old);
221 }
222
223 *ptr = NULL;
224 }
225 assert(!*ptr);
226
227 if (shProg) {
228 shProg->RefCount++;
229 #if 0
230 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
231 (void *) shProg, shProg->Name, shProg->RefCount);
232 #endif
233 *ptr = shProg;
234 }
235 }
236
237 void
238 _mesa_init_shader_program(struct gl_context *ctx, struct gl_shader_program *prog)
239 {
240 prog->Type = GL_SHADER_PROGRAM_MESA;
241 prog->RefCount = 1;
242
243 prog->AttributeBindings = string_to_uint_map_ctor();
244
245 #if FEATURE_ARB_geometry_shader4
246 prog->Geom.VerticesOut = 0;
247 prog->Geom.InputType = GL_TRIANGLES;
248 prog->Geom.OutputType = GL_TRIANGLE_STRIP;
249 #endif
250
251 prog->InfoLog = ralloc_strdup(prog, "");
252 }
253
254 /**
255 * Allocate a new gl_shader_program object, initialize it.
256 * Called via ctx->Driver.NewShaderProgram()
257 */
258 static struct gl_shader_program *
259 _mesa_new_shader_program(struct gl_context *ctx, GLuint name)
260 {
261 struct gl_shader_program *shProg;
262 shProg = rzalloc(NULL, struct gl_shader_program);
263 if (shProg) {
264 shProg->Name = name;
265 _mesa_init_shader_program(ctx, shProg);
266 }
267 return shProg;
268 }
269
270
271 /**
272 * Clear (free) the shader program state that gets produced by linking.
273 */
274 void
275 _mesa_clear_shader_program_data(struct gl_context *ctx,
276 struct gl_shader_program *shProg)
277 {
278 if (shProg->Uniforms) {
279 _mesa_free_uniform_list(shProg->Uniforms);
280 shProg->Uniforms = NULL;
281 }
282
283 if (shProg->Varying) {
284 _mesa_free_parameter_list(shProg->Varying);
285 shProg->Varying = NULL;
286 }
287
288 assert(shProg->InfoLog != NULL);
289 ralloc_free(shProg->InfoLog);
290 shProg->InfoLog = ralloc_strdup(shProg, "");
291 }
292
293
294 /**
295 * Free all the data that hangs off a shader program object, but not the
296 * object itself.
297 */
298 void
299 _mesa_free_shader_program_data(struct gl_context *ctx,
300 struct gl_shader_program *shProg)
301 {
302 GLuint i;
303 gl_shader_type sh;
304
305 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
306
307 _mesa_clear_shader_program_data(ctx, shProg);
308
309 if (shProg->AttributeBindings) {
310 string_to_uint_map_dtor(shProg->AttributeBindings);
311 shProg->AttributeBindings = NULL;
312 }
313
314 /* detach shaders */
315 for (i = 0; i < shProg->NumShaders; i++) {
316 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
317 }
318 shProg->NumShaders = 0;
319
320 if (shProg->Shaders) {
321 free(shProg->Shaders);
322 shProg->Shaders = NULL;
323 }
324
325 /* Transform feedback varying vars */
326 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
327 free(shProg->TransformFeedback.VaryingNames[i]);
328 }
329 free(shProg->TransformFeedback.VaryingNames);
330 shProg->TransformFeedback.VaryingNames = NULL;
331 shProg->TransformFeedback.NumVarying = 0;
332
333
334 for (sh = 0; sh < MESA_SHADER_TYPES; sh++) {
335 if (shProg->_LinkedShaders[sh] != NULL) {
336 ctx->Driver.DeleteShader(ctx, shProg->_LinkedShaders[sh]);
337 shProg->_LinkedShaders[sh] = NULL;
338 }
339 }
340 }
341
342
343 /**
344 * Free/delete a shader program object.
345 * Called via ctx->Driver.DeleteShaderProgram().
346 */
347 static void
348 _mesa_delete_shader_program(struct gl_context *ctx, struct gl_shader_program *shProg)
349 {
350 _mesa_free_shader_program_data(ctx, shProg);
351
352 ralloc_free(shProg);
353 }
354
355
356 /**
357 * Lookup a GLSL program object.
358 */
359 struct gl_shader_program *
360 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
361 {
362 struct gl_shader_program *shProg;
363 if (name) {
364 shProg = (struct gl_shader_program *)
365 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
366 /* Note that both gl_shader and gl_shader_program objects are kept
367 * in the same hash table. Check the object's type to be sure it's
368 * what we're expecting.
369 */
370 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
371 return NULL;
372 }
373 return shProg;
374 }
375 return NULL;
376 }
377
378
379 /**
380 * As above, but record an error if program is not found.
381 */
382 struct gl_shader_program *
383 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
384 const char *caller)
385 {
386 if (!name) {
387 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
388 return NULL;
389 }
390 else {
391 struct gl_shader_program *shProg = (struct gl_shader_program *)
392 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
393 if (!shProg) {
394 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
395 return NULL;
396 }
397 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
398 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
399 return NULL;
400 }
401 return shProg;
402 }
403 }
404
405
406 void
407 _mesa_init_shader_object_functions(struct dd_function_table *driver)
408 {
409 driver->NewShader = _mesa_new_shader;
410 driver->DeleteShader = _mesa_delete_shader;
411 driver->NewShaderProgram = _mesa_new_shader_program;
412 driver->DeleteShaderProgram = _mesa_delete_shader_program;
413 driver->LinkShader = _mesa_ir_link_shader;
414 }