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