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