mesa: Directly include mfeatures.h in files that perform feature tests.
[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 "program/program.h"
39 #include "program/prog_parameter.h"
40 #include "program/prog_uniform.h"
41 #include "talloc.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 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
76 ctx->Driver.DeleteShader(ctx, old);
77 }
78
79 *ptr = NULL;
80 }
81 assert(!*ptr);
82
83 if (sh) {
84 /* reference new */
85 sh->RefCount++;
86 /*printf("SHADER INCR %p (%d) to %d\n",
87 (void*) sh, sh->Name, sh->RefCount);*/
88 *ptr = sh;
89 }
90 }
91
92 void
93 _mesa_init_shader(struct gl_context *ctx, struct gl_shader *shader)
94 {
95 shader->RefCount = 1;
96 }
97
98 /**
99 * Allocate a new gl_shader object, initialize it.
100 * Called via ctx->Driver.NewShader()
101 */
102 struct gl_shader *
103 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
104 {
105 struct gl_shader *shader;
106 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER ||
107 type == GL_GEOMETRY_SHADER_ARB);
108 shader = talloc_zero(NULL, struct gl_shader);
109 if (shader) {
110 shader->Type = type;
111 shader->Name = name;
112 _mesa_init_shader(ctx, shader);
113 }
114 return shader;
115 }
116
117
118 /**
119 * Delete a shader object.
120 * Called via ctx->Driver.DeleteShader().
121 */
122 static void
123 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
124 {
125 if (sh->Source)
126 free((void *) sh->Source);
127 _mesa_reference_program(ctx, &sh->Program, NULL);
128 talloc_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 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
217 ctx->Driver.DeleteShaderProgram(ctx, old);
218 }
219
220 *ptr = NULL;
221 }
222 assert(!*ptr);
223
224 if (shProg) {
225 shProg->RefCount++;
226 #if 0
227 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
228 (void *) shProg, shProg->Name, shProg->RefCount);
229 #endif
230 *ptr = shProg;
231 }
232 }
233
234 void
235 _mesa_init_shader_program(struct gl_context *ctx, struct gl_shader_program *prog)
236 {
237 prog->Type = GL_SHADER_PROGRAM_MESA;
238 prog->RefCount = 1;
239 prog->Attributes = _mesa_new_parameter_list();
240 #if FEATURE_ARB_geometry_shader4
241 prog->Geom.VerticesOut = 0;
242 prog->Geom.InputType = GL_TRIANGLES;
243 prog->Geom.OutputType = GL_TRIANGLE_STRIP;
244 #endif
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(struct gl_context *ctx, GLuint name)
253 {
254 struct gl_shader_program *shProg;
255 shProg = talloc_zero(NULL, struct gl_shader_program);
256 if (shProg) {
257 shProg->Name = name;
258 _mesa_init_shader_program(ctx, 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_context *ctx,
269 struct gl_shader_program *shProg)
270 {
271 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
272 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
273 _mesa_reference_geomprog(ctx, &shProg->GeometryProgram, NULL);
274
275 if (shProg->Uniforms) {
276 _mesa_free_uniform_list(shProg->Uniforms);
277 shProg->Uniforms = NULL;
278 }
279
280 if (shProg->Varying) {
281 _mesa_free_parameter_list(shProg->Varying);
282 shProg->Varying = NULL;
283 }
284 }
285
286
287 /**
288 * Free all the data that hangs off a shader program object, but not the
289 * object itself.
290 */
291 void
292 _mesa_free_shader_program_data(struct gl_context *ctx,
293 struct gl_shader_program *shProg)
294 {
295 GLuint i;
296 gl_shader_type sh;
297
298 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
299
300 _mesa_clear_shader_program_data(ctx, shProg);
301
302 if (shProg->Attributes) {
303 _mesa_free_parameter_list(shProg->Attributes);
304 shProg->Attributes = NULL;
305 }
306
307 /* detach shaders */
308 for (i = 0; i < shProg->NumShaders; i++) {
309 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
310 }
311 shProg->NumShaders = 0;
312
313 if (shProg->Shaders) {
314 free(shProg->Shaders);
315 shProg->Shaders = NULL;
316 }
317
318 if (shProg->InfoLog) {
319 talloc_free(shProg->InfoLog);
320 shProg->InfoLog = NULL;
321 }
322
323 /* Transform feedback varying vars */
324 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
325 free(shProg->TransformFeedback.VaryingNames[i]);
326 }
327 free(shProg->TransformFeedback.VaryingNames);
328 shProg->TransformFeedback.VaryingNames = NULL;
329 shProg->TransformFeedback.NumVarying = 0;
330
331
332 for (sh = 0; sh < MESA_SHADER_TYPES; sh++) {
333 if (shProg->_LinkedShaders[sh] != NULL) {
334 ctx->Driver.DeleteShader(ctx, shProg->_LinkedShaders[sh]);
335 shProg->_LinkedShaders[sh] = NULL;
336 }
337 }
338 }
339
340
341 /**
342 * Free/delete a shader program object.
343 * Called via ctx->Driver.DeleteShaderProgram().
344 */
345 static void
346 _mesa_delete_shader_program(struct gl_context *ctx, struct gl_shader_program *shProg)
347 {
348 _mesa_free_shader_program_data(ctx, shProg);
349
350 talloc_free(shProg);
351 }
352
353
354 /**
355 * Lookup a GLSL program object.
356 */
357 struct gl_shader_program *
358 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
359 {
360 struct gl_shader_program *shProg;
361 if (name) {
362 shProg = (struct gl_shader_program *)
363 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
364 /* Note that both gl_shader and gl_shader_program objects are kept
365 * in the same hash table. Check the object's type to be sure it's
366 * what we're expecting.
367 */
368 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
369 return NULL;
370 }
371 return shProg;
372 }
373 return NULL;
374 }
375
376
377 /**
378 * As above, but record an error if program is not found.
379 */
380 struct gl_shader_program *
381 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
382 const char *caller)
383 {
384 if (!name) {
385 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
386 return NULL;
387 }
388 else {
389 struct gl_shader_program *shProg = (struct gl_shader_program *)
390 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
391 if (!shProg) {
392 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
393 return NULL;
394 }
395 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
396 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
397 return NULL;
398 }
399 return shProg;
400 }
401 }
402
403
404 void
405 _mesa_init_shader_object_functions(struct dd_function_table *driver)
406 {
407 driver->NewShader = _mesa_new_shader;
408 driver->DeleteShader = _mesa_delete_shader;
409 driver->NewShaderProgram = _mesa_new_shader_program;
410 driver->DeleteShaderProgram = _mesa_delete_shader_program;
411 driver->CompileShader = _mesa_ir_compile_shader;
412 driver->LinkShader = _mesa_ir_link_shader;
413 }