mesa: use special checksums for unset checksums and fixed-func shaders
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file shaderobj.c
28 * \author Brian Paul
29 *
30 */
31
32
33 #include "main/glheader.h"
34 #include "main/context.h"
35 #include "main/hash.h"
36 #include "main/mtypes.h"
37 #include "main/shaderapi.h"
38 #include "main/shaderobj.h"
39 #include "main/uniforms.h"
40 #include "program/program.h"
41 #include "program/prog_parameter.h"
42 #include "util/ralloc.h"
43 #include "util/string_to_uint_map.h"
44 #include "util/u_atomic.h"
45
46 /**********************************************************************/
47 /*** Shader object functions ***/
48 /**********************************************************************/
49
50
51 /**
52 * Set ptr to point to sh.
53 * If ptr is pointing to another shader, decrement its refcount (and delete
54 * if refcount hits zero).
55 * Then set ptr to point to sh, incrementing its refcount.
56 */
57 void
58 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
59 struct gl_shader *sh)
60 {
61 assert(ptr);
62 if (*ptr == sh) {
63 /* no-op */
64 return;
65 }
66 if (*ptr) {
67 /* Unreference the old shader */
68 GLboolean deleteFlag = GL_FALSE;
69 struct gl_shader *old = *ptr;
70
71 assert(old->RefCount > 0);
72 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 _mesa_delete_shader(ctx, old);
79 }
80
81 *ptr = NULL;
82 }
83 assert(!*ptr);
84
85 if (sh) {
86 /* reference new */
87 sh->RefCount++;
88 *ptr = sh;
89 }
90 }
91
92 static void
93 _mesa_init_shader(struct gl_shader *shader)
94 {
95 shader->RefCount = 1;
96 shader->info.Geom.VerticesOut = -1;
97 shader->info.Geom.InputType = GL_TRIANGLES;
98 shader->info.Geom.OutputType = GL_TRIANGLE_STRIP;
99 }
100
101 /**
102 * Allocate a new gl_shader object, initialize it.
103 */
104 struct gl_shader *
105 _mesa_new_shader(GLuint name, gl_shader_stage stage)
106 {
107 struct gl_shader *shader;
108 shader = rzalloc(NULL, struct gl_shader);
109 if (shader) {
110 shader->Stage = stage;
111 shader->Name = name;
112 #ifdef DEBUG
113 shader->SourceChecksum = 0xa110c; /* alloc */
114 #endif
115 _mesa_init_shader(shader);
116 }
117 return shader;
118 }
119
120
121 /**
122 * Allocate a new gl_linked_shader object.
123 * Called via ctx->Driver.NewShader()
124 */
125 struct gl_linked_shader *
126 _mesa_new_linked_shader(gl_shader_stage stage)
127 {
128 struct gl_linked_shader *shader;
129 shader = rzalloc(NULL, struct gl_linked_shader);
130 if (shader) {
131 shader->Stage = stage;
132 }
133 return shader;
134 }
135
136
137 /**
138 * Delete a shader object.
139 */
140 void
141 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
142 {
143 free((void *)sh->Source);
144 free(sh->Label);
145 ralloc_free(sh);
146 }
147
148
149 /**
150 * Delete a shader object.
151 */
152 void
153 _mesa_delete_linked_shader(struct gl_context *ctx,
154 struct gl_linked_shader *sh)
155 {
156 _mesa_reference_program(ctx, &sh->Program, NULL);
157 ralloc_free(sh);
158 }
159
160
161 /**
162 * Lookup a GLSL shader object.
163 */
164 struct gl_shader *
165 _mesa_lookup_shader(struct gl_context *ctx, GLuint name)
166 {
167 if (name) {
168 struct gl_shader *sh = (struct gl_shader *)
169 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
170 /* Note that both gl_shader and gl_shader_program objects are kept
171 * in the same hash table. Check the object's type to be sure it's
172 * what we're expecting.
173 */
174 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
175 return NULL;
176 }
177 return sh;
178 }
179 return NULL;
180 }
181
182
183 /**
184 * As above, but record an error if shader is not found.
185 */
186 struct gl_shader *
187 _mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
188 {
189 if (!name) {
190 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
191 return NULL;
192 }
193 else {
194 struct gl_shader *sh = (struct gl_shader *)
195 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
196 if (!sh) {
197 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
198 return NULL;
199 }
200 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
201 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
202 return NULL;
203 }
204 return sh;
205 }
206 }
207
208
209
210 /**********************************************************************/
211 /*** Shader Program object functions ***/
212 /**********************************************************************/
213
214
215 void
216 _mesa_reference_shader_program_data(struct gl_context *ctx,
217 struct gl_shader_program_data **ptr,
218 struct gl_shader_program_data *data)
219 {
220 if (*ptr == data)
221 return;
222
223 if (*ptr) {
224 struct gl_shader_program_data *oldData = *ptr;
225
226 assert(oldData->RefCount > 0);
227
228 if (p_atomic_dec_zero(&oldData->RefCount)) {
229 assert(ctx);
230 ralloc_free(oldData);
231 }
232
233 *ptr = NULL;
234 }
235
236 if (data)
237 p_atomic_inc(&data->RefCount);
238
239 *ptr = data;
240 }
241
242 /**
243 * Set ptr to point to shProg.
244 * If ptr is pointing to another object, decrement its refcount (and delete
245 * if refcount hits zero).
246 * Then set ptr to point to shProg, incrementing its refcount.
247 */
248 void
249 _mesa_reference_shader_program_(struct gl_context *ctx,
250 struct gl_shader_program **ptr,
251 struct gl_shader_program *shProg)
252 {
253 assert(ptr);
254 if (*ptr == shProg) {
255 /* no-op */
256 return;
257 }
258 if (*ptr) {
259 /* Unreference the old shader program */
260 GLboolean deleteFlag = GL_FALSE;
261 struct gl_shader_program *old = *ptr;
262
263 assert(old->RefCount > 0);
264 old->RefCount--;
265 deleteFlag = (old->RefCount == 0);
266
267 if (deleteFlag) {
268 if (old->Name != 0)
269 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
270 _mesa_delete_shader_program(ctx, old);
271 }
272
273 *ptr = NULL;
274 }
275 assert(!*ptr);
276
277 if (shProg) {
278 shProg->RefCount++;
279 *ptr = shProg;
280 }
281 }
282
283 static struct gl_shader_program_data *
284 create_shader_program_data()
285 {
286 struct gl_shader_program_data *data;
287 data = rzalloc(NULL, struct gl_shader_program_data);
288 if (data)
289 data->RefCount = 1;
290
291 return data;
292 }
293
294 static void
295 init_shader_program(struct gl_shader_program *prog)
296 {
297 prog->Type = GL_SHADER_PROGRAM_MESA;
298 prog->RefCount = 1;
299
300 prog->AttributeBindings = string_to_uint_map_ctor();
301 prog->FragDataBindings = string_to_uint_map_ctor();
302 prog->FragDataIndexBindings = string_to_uint_map_ctor();
303
304 prog->Geom.UsesEndPrimitive = false;
305 prog->Geom.UsesStreams = false;
306
307 prog->Comp.LocalSizeVariable = false;
308
309 prog->TransformFeedback.BufferMode = GL_INTERLEAVED_ATTRIBS;
310
311 exec_list_make_empty(&prog->EmptyUniformLocations);
312
313 prog->data->InfoLog = ralloc_strdup(prog->data, "");
314 }
315
316 /**
317 * Allocate a new gl_shader_program object, initialize it.
318 */
319 struct gl_shader_program *
320 _mesa_new_shader_program(GLuint name)
321 {
322 struct gl_shader_program *shProg;
323 shProg = rzalloc(NULL, struct gl_shader_program);
324 if (shProg) {
325 shProg->Name = name;
326 shProg->data = create_shader_program_data();
327 if (!shProg->data) {
328 ralloc_free(shProg);
329 return NULL;
330 }
331 init_shader_program(shProg);
332 }
333 return shProg;
334 }
335
336
337 /**
338 * Clear (free) the shader program state that gets produced by linking.
339 */
340 void
341 _mesa_clear_shader_program_data(struct gl_context *ctx,
342 struct gl_shader_program *shProg)
343 {
344 for (gl_shader_stage sh = 0; sh < MESA_SHADER_STAGES; sh++) {
345 if (shProg->_LinkedShaders[sh] != NULL) {
346 _mesa_delete_linked_shader(ctx, shProg->_LinkedShaders[sh]);
347 shProg->_LinkedShaders[sh] = NULL;
348 }
349 }
350
351 if (shProg->data->UniformStorage) {
352 for (unsigned i = 0; i < shProg->data->NumUniformStorage; ++i)
353 _mesa_uniform_detach_all_driver_storage(&shProg->data->
354 UniformStorage[i]);
355 ralloc_free(shProg->data->UniformStorage);
356 shProg->data->NumUniformStorage = 0;
357 shProg->data->UniformStorage = NULL;
358 }
359
360 if (shProg->UniformRemapTable) {
361 ralloc_free(shProg->UniformRemapTable);
362 shProg->NumUniformRemapTable = 0;
363 shProg->UniformRemapTable = NULL;
364 }
365
366 if (shProg->UniformHash) {
367 string_to_uint_map_dtor(shProg->UniformHash);
368 shProg->UniformHash = NULL;
369 }
370
371 assert(shProg->data->InfoLog != NULL);
372 ralloc_free(shProg->data->InfoLog);
373 shProg->data->InfoLog = ralloc_strdup(shProg->data, "");
374
375 ralloc_free(shProg->data->UniformBlocks);
376 shProg->data->UniformBlocks = NULL;
377 shProg->data->NumUniformBlocks = 0;
378
379 ralloc_free(shProg->data->ShaderStorageBlocks);
380 shProg->data->ShaderStorageBlocks = NULL;
381 shProg->data->NumShaderStorageBlocks = 0;
382
383 ralloc_free(shProg->data->AtomicBuffers);
384 shProg->data->AtomicBuffers = NULL;
385 shProg->data->NumAtomicBuffers = 0;
386
387 if (shProg->ProgramResourceList) {
388 ralloc_free(shProg->ProgramResourceList);
389 shProg->ProgramResourceList = NULL;
390 shProg->NumProgramResourceList = 0;
391 }
392 }
393
394
395 /**
396 * Free all the data that hangs off a shader program object, but not the
397 * object itself.
398 */
399 void
400 _mesa_free_shader_program_data(struct gl_context *ctx,
401 struct gl_shader_program *shProg)
402 {
403 GLuint i;
404
405 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
406
407 _mesa_clear_shader_program_data(ctx, shProg);
408
409 if (shProg->AttributeBindings) {
410 string_to_uint_map_dtor(shProg->AttributeBindings);
411 shProg->AttributeBindings = NULL;
412 }
413
414 if (shProg->FragDataBindings) {
415 string_to_uint_map_dtor(shProg->FragDataBindings);
416 shProg->FragDataBindings = NULL;
417 }
418
419 if (shProg->FragDataIndexBindings) {
420 string_to_uint_map_dtor(shProg->FragDataIndexBindings);
421 shProg->FragDataIndexBindings = NULL;
422 }
423
424 /* detach shaders */
425 for (i = 0; i < shProg->NumShaders; i++) {
426 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
427 }
428 shProg->NumShaders = 0;
429
430 free(shProg->Shaders);
431 shProg->Shaders = NULL;
432
433 /* Transform feedback varying vars */
434 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
435 free(shProg->TransformFeedback.VaryingNames[i]);
436 }
437 free(shProg->TransformFeedback.VaryingNames);
438 shProg->TransformFeedback.VaryingNames = NULL;
439 shProg->TransformFeedback.NumVarying = 0;
440
441 free(shProg->Label);
442 shProg->Label = NULL;
443 }
444
445
446 /**
447 * Free/delete a shader program object.
448 */
449 void
450 _mesa_delete_shader_program(struct gl_context *ctx,
451 struct gl_shader_program *shProg)
452 {
453 _mesa_free_shader_program_data(ctx, shProg);
454 _mesa_reference_shader_program_data(ctx, &shProg->data, NULL);
455 ralloc_free(shProg);
456 }
457
458
459 /**
460 * Lookup a GLSL program object.
461 */
462 struct gl_shader_program *
463 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
464 {
465 struct gl_shader_program *shProg;
466 if (name) {
467 shProg = (struct gl_shader_program *)
468 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
469 /* Note that both gl_shader and gl_shader_program objects are kept
470 * in the same hash table. Check the object's type to be sure it's
471 * what we're expecting.
472 */
473 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
474 return NULL;
475 }
476 return shProg;
477 }
478 return NULL;
479 }
480
481
482 /**
483 * As above, but record an error if program is not found.
484 */
485 struct gl_shader_program *
486 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
487 const char *caller)
488 {
489 if (!name) {
490 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
491 return NULL;
492 }
493 else {
494 struct gl_shader_program *shProg = (struct gl_shader_program *)
495 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
496 if (!shProg) {
497 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
498 return NULL;
499 }
500 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
501 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
502 return NULL;
503 }
504 return shProg;
505 }
506 }
507
508
509 void
510 _mesa_init_shader_object_functions(struct dd_function_table *driver)
511 {
512 driver->NewShader = _mesa_new_linked_shader;
513 driver->LinkShader = _mesa_ir_link_shader;
514 }