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