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