st/mesa/glsl/nir/i965: make use of new gl_shader_program_data in gl_shader_program
[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->data->InfoLog = ralloc_strdup(prog->data, "");
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 shProg->data = create_shader_program_data();
324 if (!shProg->data) {
325 ralloc_free(shProg);
326 return NULL;
327 }
328 init_shader_program(shProg);
329 }
330 return shProg;
331 }
332
333
334 /**
335 * Clear (free) the shader program state that gets produced by linking.
336 */
337 void
338 _mesa_clear_shader_program_data(struct gl_context *ctx,
339 struct gl_shader_program *shProg)
340 {
341 for (gl_shader_stage sh = 0; sh < MESA_SHADER_STAGES; sh++) {
342 if (shProg->_LinkedShaders[sh] != NULL) {
343 _mesa_delete_linked_shader(ctx, shProg->_LinkedShaders[sh]);
344 shProg->_LinkedShaders[sh] = NULL;
345 }
346 }
347
348 if (shProg->data->UniformStorage) {
349 for (unsigned i = 0; i < shProg->data->NumUniformStorage; ++i)
350 _mesa_uniform_detach_all_driver_storage(&shProg->data->
351 UniformStorage[i]);
352 ralloc_free(shProg->data->UniformStorage);
353 shProg->data->NumUniformStorage = 0;
354 shProg->data->UniformStorage = NULL;
355 }
356
357 if (shProg->UniformRemapTable) {
358 ralloc_free(shProg->UniformRemapTable);
359 shProg->NumUniformRemapTable = 0;
360 shProg->UniformRemapTable = NULL;
361 }
362
363 if (shProg->UniformHash) {
364 string_to_uint_map_dtor(shProg->UniformHash);
365 shProg->UniformHash = NULL;
366 }
367
368 assert(shProg->data->InfoLog != NULL);
369 ralloc_free(shProg->data->InfoLog);
370 shProg->data->InfoLog = ralloc_strdup(shProg->data, "");
371
372 ralloc_free(shProg->data->UniformBlocks);
373 shProg->data->UniformBlocks = NULL;
374 shProg->data->NumUniformBlocks = 0;
375
376 ralloc_free(shProg->data->ShaderStorageBlocks);
377 shProg->data->ShaderStorageBlocks = NULL;
378 shProg->data->NumShaderStorageBlocks = 0;
379
380 ralloc_free(shProg->data->AtomicBuffers);
381 shProg->data->AtomicBuffers = NULL;
382 shProg->data->NumAtomicBuffers = 0;
383
384 if (shProg->ProgramResourceList) {
385 ralloc_free(shProg->ProgramResourceList);
386 shProg->ProgramResourceList = NULL;
387 shProg->NumProgramResourceList = 0;
388 }
389 }
390
391
392 /**
393 * Free all the data that hangs off a shader program object, but not the
394 * object itself.
395 */
396 void
397 _mesa_free_shader_program_data(struct gl_context *ctx,
398 struct gl_shader_program *shProg)
399 {
400 GLuint i;
401
402 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
403
404 _mesa_clear_shader_program_data(ctx, shProg);
405
406 if (shProg->AttributeBindings) {
407 string_to_uint_map_dtor(shProg->AttributeBindings);
408 shProg->AttributeBindings = NULL;
409 }
410
411 if (shProg->FragDataBindings) {
412 string_to_uint_map_dtor(shProg->FragDataBindings);
413 shProg->FragDataBindings = NULL;
414 }
415
416 if (shProg->FragDataIndexBindings) {
417 string_to_uint_map_dtor(shProg->FragDataIndexBindings);
418 shProg->FragDataIndexBindings = NULL;
419 }
420
421 /* detach shaders */
422 for (i = 0; i < shProg->NumShaders; i++) {
423 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
424 }
425 shProg->NumShaders = 0;
426
427 free(shProg->Shaders);
428 shProg->Shaders = NULL;
429
430 /* Transform feedback varying vars */
431 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
432 free(shProg->TransformFeedback.VaryingNames[i]);
433 }
434 free(shProg->TransformFeedback.VaryingNames);
435 shProg->TransformFeedback.VaryingNames = NULL;
436 shProg->TransformFeedback.NumVarying = 0;
437
438 free(shProg->Label);
439 shProg->Label = NULL;
440 }
441
442
443 /**
444 * Free/delete a shader program object.
445 */
446 void
447 _mesa_delete_shader_program(struct gl_context *ctx,
448 struct gl_shader_program *shProg)
449 {
450 _mesa_free_shader_program_data(ctx, shProg);
451 _mesa_reference_shader_program_data(ctx, &shProg->data, NULL);
452 ralloc_free(shProg);
453 }
454
455
456 /**
457 * Lookup a GLSL program object.
458 */
459 struct gl_shader_program *
460 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
461 {
462 struct gl_shader_program *shProg;
463 if (name) {
464 shProg = (struct gl_shader_program *)
465 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
466 /* Note that both gl_shader and gl_shader_program objects are kept
467 * in the same hash table. Check the object's type to be sure it's
468 * what we're expecting.
469 */
470 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
471 return NULL;
472 }
473 return shProg;
474 }
475 return NULL;
476 }
477
478
479 /**
480 * As above, but record an error if program is not found.
481 */
482 struct gl_shader_program *
483 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
484 const char *caller)
485 {
486 if (!name) {
487 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
488 return NULL;
489 }
490 else {
491 struct gl_shader_program *shProg = (struct gl_shader_program *)
492 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
493 if (!shProg) {
494 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
495 return NULL;
496 }
497 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
498 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
499 return NULL;
500 }
501 return shProg;
502 }
503 }
504
505
506 void
507 _mesa_init_shader_object_functions(struct dd_function_table *driver)
508 {
509 driver->NewShader = _mesa_new_linked_shader;
510 driver->LinkShader = _mesa_ir_link_shader;
511 }