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