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