mesa: create new gl_shader_program_data struct
[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 _mesa_init_shader(shader);
113 }
114 return shader;
115 }
116
117
118 /**
119 * Allocate a new gl_linked_shader object.
120 * Called via ctx->Driver.NewShader()
121 */
122 struct gl_linked_shader *
123 _mesa_new_linked_shader(gl_shader_stage stage)
124 {
125 struct gl_linked_shader *shader;
126 shader = rzalloc(NULL, struct gl_linked_shader);
127 if (shader) {
128 shader->Stage = stage;
129 }
130 return shader;
131 }
132
133
134 /**
135 * Delete a shader object.
136 */
137 void
138 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
139 {
140 free((void *)sh->Source);
141 free(sh->Label);
142 ralloc_free(sh);
143 }
144
145
146 /**
147 * Delete a shader object.
148 */
149 void
150 _mesa_delete_linked_shader(struct gl_context *ctx,
151 struct gl_linked_shader *sh)
152 {
153 _mesa_reference_program(ctx, &sh->Program, NULL);
154 ralloc_free(sh);
155 }
156
157
158 /**
159 * Lookup a GLSL shader object.
160 */
161 struct gl_shader *
162 _mesa_lookup_shader(struct gl_context *ctx, GLuint name)
163 {
164 if (name) {
165 struct gl_shader *sh = (struct gl_shader *)
166 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
167 /* Note that both gl_shader and gl_shader_program objects are kept
168 * in the same hash table. Check the object's type to be sure it's
169 * what we're expecting.
170 */
171 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
172 return NULL;
173 }
174 return sh;
175 }
176 return NULL;
177 }
178
179
180 /**
181 * As above, but record an error if shader is not found.
182 */
183 struct gl_shader *
184 _mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
185 {
186 if (!name) {
187 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
188 return NULL;
189 }
190 else {
191 struct gl_shader *sh = (struct gl_shader *)
192 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
193 if (!sh) {
194 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
195 return NULL;
196 }
197 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
198 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
199 return NULL;
200 }
201 return sh;
202 }
203 }
204
205
206
207 /**********************************************************************/
208 /*** Shader Program object functions ***/
209 /**********************************************************************/
210
211
212 void
213 _mesa_reference_shader_program_data(struct gl_context *ctx,
214 struct gl_shader_program_data **ptr,
215 struct gl_shader_program_data *data)
216 {
217 if (*ptr == data)
218 return;
219
220 if (*ptr) {
221 struct gl_shader_program_data *oldData = *ptr;
222
223 assert(oldData->RefCount > 0);
224
225 if (p_atomic_dec_zero(&oldData->RefCount)) {
226 assert(ctx);
227 ralloc_free(oldData);
228 }
229
230 *ptr = NULL;
231 }
232
233 if (data)
234 p_atomic_inc(&data->RefCount);
235
236 *ptr = data;
237 }
238
239 /**
240 * Set ptr to point to shProg.
241 * If ptr is pointing to another object, decrement its refcount (and delete
242 * if refcount hits zero).
243 * Then set ptr to point to shProg, incrementing its refcount.
244 */
245 void
246 _mesa_reference_shader_program_(struct gl_context *ctx,
247 struct gl_shader_program **ptr,
248 struct gl_shader_program *shProg)
249 {
250 assert(ptr);
251 if (*ptr == shProg) {
252 /* no-op */
253 return;
254 }
255 if (*ptr) {
256 /* Unreference the old shader program */
257 GLboolean deleteFlag = GL_FALSE;
258 struct gl_shader_program *old = *ptr;
259
260 assert(old->RefCount > 0);
261 old->RefCount--;
262 deleteFlag = (old->RefCount == 0);
263
264 if (deleteFlag) {
265 if (old->Name != 0)
266 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
267 _mesa_delete_shader_program(ctx, old);
268 }
269
270 *ptr = NULL;
271 }
272 assert(!*ptr);
273
274 if (shProg) {
275 shProg->RefCount++;
276 *ptr = shProg;
277 }
278 }
279
280 static struct gl_shader_program_data *
281 create_shader_program_data()
282 {
283 struct gl_shader_program_data *data;
284 data = rzalloc(NULL, struct gl_shader_program_data);
285 if (data)
286 data->RefCount = 1;
287
288 return data;
289 }
290
291 static void
292 init_shader_program(struct gl_shader_program *prog)
293 {
294 prog->Type = GL_SHADER_PROGRAM_MESA;
295 prog->RefCount = 1;
296
297 prog->AttributeBindings = string_to_uint_map_ctor();
298 prog->FragDataBindings = string_to_uint_map_ctor();
299 prog->FragDataIndexBindings = string_to_uint_map_ctor();
300
301 prog->Geom.UsesEndPrimitive = false;
302 prog->Geom.UsesStreams = false;
303
304 prog->Comp.LocalSizeVariable = false;
305
306 prog->TransformFeedback.BufferMode = GL_INTERLEAVED_ATTRIBS;
307
308 exec_list_make_empty(&prog->EmptyUniformLocations);
309
310 prog->InfoLog = ralloc_strdup(prog, "");
311 }
312
313 /**
314 * Allocate a new gl_shader_program object, initialize it.
315 */
316 struct gl_shader_program *
317 _mesa_new_shader_program(GLuint name)
318 {
319 struct gl_shader_program *shProg;
320 shProg = rzalloc(NULL, struct gl_shader_program);
321 if (shProg) {
322 shProg->Name = name;
323 init_shader_program(shProg);
324 }
325 return shProg;
326 }
327
328
329 /**
330 * Clear (free) the shader program state that gets produced by linking.
331 */
332 void
333 _mesa_clear_shader_program_data(struct gl_context *ctx,
334 struct gl_shader_program *shProg)
335 {
336 for (gl_shader_stage sh = 0; sh < MESA_SHADER_STAGES; sh++) {
337 if (shProg->_LinkedShaders[sh] != NULL) {
338 _mesa_delete_linked_shader(ctx, shProg->_LinkedShaders[sh]);
339 shProg->_LinkedShaders[sh] = NULL;
340 }
341 }
342
343 if (shProg->UniformStorage) {
344 for (unsigned i = 0; i < shProg->NumUniformStorage; ++i)
345 _mesa_uniform_detach_all_driver_storage(&shProg->UniformStorage[i]);
346 ralloc_free(shProg->UniformStorage);
347 shProg->NumUniformStorage = 0;
348 shProg->UniformStorage = NULL;
349 }
350
351 if (shProg->UniformRemapTable) {
352 ralloc_free(shProg->UniformRemapTable);
353 shProg->NumUniformRemapTable = 0;
354 shProg->UniformRemapTable = NULL;
355 }
356
357 if (shProg->UniformHash) {
358 string_to_uint_map_dtor(shProg->UniformHash);
359 shProg->UniformHash = NULL;
360 }
361
362 assert(shProg->InfoLog != NULL);
363 ralloc_free(shProg->InfoLog);
364 shProg->InfoLog = ralloc_strdup(shProg, "");
365
366 ralloc_free(shProg->UniformBlocks);
367 shProg->UniformBlocks = NULL;
368 shProg->NumUniformBlocks = 0;
369
370 ralloc_free(shProg->ShaderStorageBlocks);
371 shProg->ShaderStorageBlocks = NULL;
372 shProg->NumShaderStorageBlocks = 0;
373
374 ralloc_free(shProg->AtomicBuffers);
375 shProg->AtomicBuffers = NULL;
376 shProg->NumAtomicBuffers = 0;
377
378 if (shProg->ProgramResourceList) {
379 ralloc_free(shProg->ProgramResourceList);
380 shProg->ProgramResourceList = NULL;
381 shProg->NumProgramResourceList = 0;
382 }
383 }
384
385
386 /**
387 * Free all the data that hangs off a shader program object, but not the
388 * object itself.
389 */
390 void
391 _mesa_free_shader_program_data(struct gl_context *ctx,
392 struct gl_shader_program *shProg)
393 {
394 GLuint i;
395
396 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
397
398 _mesa_clear_shader_program_data(ctx, shProg);
399
400 if (shProg->AttributeBindings) {
401 string_to_uint_map_dtor(shProg->AttributeBindings);
402 shProg->AttributeBindings = NULL;
403 }
404
405 if (shProg->FragDataBindings) {
406 string_to_uint_map_dtor(shProg->FragDataBindings);
407 shProg->FragDataBindings = NULL;
408 }
409
410 if (shProg->FragDataIndexBindings) {
411 string_to_uint_map_dtor(shProg->FragDataIndexBindings);
412 shProg->FragDataIndexBindings = NULL;
413 }
414
415 /* detach shaders */
416 for (i = 0; i < shProg->NumShaders; i++) {
417 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
418 }
419 shProg->NumShaders = 0;
420
421 free(shProg->Shaders);
422 shProg->Shaders = NULL;
423
424 /* Transform feedback varying vars */
425 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
426 free(shProg->TransformFeedback.VaryingNames[i]);
427 }
428 free(shProg->TransformFeedback.VaryingNames);
429 shProg->TransformFeedback.VaryingNames = NULL;
430 shProg->TransformFeedback.NumVarying = 0;
431
432 free(shProg->Label);
433 shProg->Label = NULL;
434 }
435
436
437 /**
438 * Free/delete a shader program object.
439 */
440 void
441 _mesa_delete_shader_program(struct gl_context *ctx,
442 struct gl_shader_program *shProg)
443 {
444 _mesa_free_shader_program_data(ctx, shProg);
445
446 ralloc_free(shProg);
447 }
448
449
450 /**
451 * Lookup a GLSL program object.
452 */
453 struct gl_shader_program *
454 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
455 {
456 struct gl_shader_program *shProg;
457 if (name) {
458 shProg = (struct gl_shader_program *)
459 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
460 /* Note that both gl_shader and gl_shader_program objects are kept
461 * in the same hash table. Check the object's type to be sure it's
462 * what we're expecting.
463 */
464 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
465 return NULL;
466 }
467 return shProg;
468 }
469 return NULL;
470 }
471
472
473 /**
474 * As above, but record an error if program is not found.
475 */
476 struct gl_shader_program *
477 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
478 const char *caller)
479 {
480 if (!name) {
481 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
482 return NULL;
483 }
484 else {
485 struct gl_shader_program *shProg = (struct gl_shader_program *)
486 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
487 if (!shProg) {
488 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
489 return NULL;
490 }
491 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
492 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
493 return NULL;
494 }
495 return shProg;
496 }
497 }
498
499
500 void
501 _mesa_init_shader_object_functions(struct dd_function_table *driver)
502 {
503 driver->NewShader = _mesa_new_linked_shader;
504 driver->LinkShader = _mesa_ir_link_shader;
505 }