2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/hash.h"
35 #include "main/shaderobj.h"
36 #include "program/program.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_uniform.h"
41 /**********************************************************************/
42 /*** Shader object functions ***/
43 /**********************************************************************/
47 * Set ptr to point to sh.
48 * If ptr is pointing to another shader, decrement its refcount (and delete
49 * if refcount hits zero).
50 * Then set ptr to point to sh, incrementing its refcount.
53 _mesa_reference_shader(GLcontext
*ctx
, struct gl_shader
**ptr
,
62 /* Unreference the old shader */
63 GLboolean deleteFlag
= GL_FALSE
;
64 struct gl_shader
*old
= *ptr
;
66 ASSERT(old
->RefCount
> 0);
68 /*printf("SHADER DECR %p (%d) to %d\n",
69 (void*) old, old->Name, old->RefCount);*/
70 deleteFlag
= (old
->RefCount
== 0);
73 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
74 ctx
->Driver
.DeleteShader(ctx
, old
);
84 /*printf("SHADER INCR %p (%d) to %d\n",
85 (void*) sh, sh->Name, sh->RefCount);*/
92 * Allocate a new gl_shader object, initialize it.
93 * Called via ctx->Driver.NewShader()
95 static struct gl_shader
*
96 _mesa_new_shader(GLcontext
*ctx
, GLuint name
, GLenum type
)
98 struct gl_shader
*shader
;
99 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
);
100 shader
= CALLOC_STRUCT(gl_shader
);
104 shader
->RefCount
= 1;
111 * Delete a shader object.
112 * Called via ctx->Driver.DeleteShader().
115 __mesa_delete_shader(GLcontext
*ctx
, struct gl_shader
*sh
)
118 free((void *) sh
->Source
);
121 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
127 * Lookup a GLSL shader object.
130 _mesa_lookup_shader(GLcontext
*ctx
, GLuint name
)
133 struct gl_shader
*sh
= (struct gl_shader
*)
134 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
135 /* Note that both gl_shader and gl_shader_program objects are kept
136 * in the same hash table. Check the object's type to be sure it's
137 * what we're expecting.
139 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
149 * As above, but record an error if shader is not found.
152 _mesa_lookup_shader_err(GLcontext
*ctx
, GLuint name
, const char *caller
)
155 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
159 struct gl_shader
*sh
= (struct gl_shader
*)
160 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
162 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
165 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
166 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
175 /**********************************************************************/
176 /*** Shader Program object functions ***/
177 /**********************************************************************/
181 * Set ptr to point to shProg.
182 * If ptr is pointing to another object, decrement its refcount (and delete
183 * if refcount hits zero).
184 * Then set ptr to point to shProg, incrementing its refcount.
187 _mesa_reference_shader_program(GLcontext
*ctx
,
188 struct gl_shader_program
**ptr
,
189 struct gl_shader_program
*shProg
)
192 if (*ptr
== shProg
) {
197 /* Unreference the old shader program */
198 GLboolean deleteFlag
= GL_FALSE
;
199 struct gl_shader_program
*old
= *ptr
;
201 ASSERT(old
->RefCount
> 0);
204 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
205 (void *) old
, old
->Name
, old
->RefCount
);
207 deleteFlag
= (old
->RefCount
== 0);
210 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
211 ctx
->Driver
.DeleteShaderProgram(ctx
, old
);
221 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
222 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
230 * Allocate a new gl_shader_program object, initialize it.
231 * Called via ctx->Driver.NewShaderProgram()
233 static struct gl_shader_program
*
234 _mesa_new_shader_program(GLcontext
*ctx
, GLuint name
)
236 struct gl_shader_program
*shProg
;
237 shProg
= CALLOC_STRUCT(gl_shader_program
);
239 shProg
->Type
= GL_SHADER_PROGRAM_MESA
;
241 shProg
->RefCount
= 1;
242 shProg
->Attributes
= _mesa_new_parameter_list();
249 * Clear (free) the shader program state that gets produced by linking.
252 _mesa_clear_shader_program_data(GLcontext
*ctx
,
253 struct gl_shader_program
*shProg
)
255 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
256 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
258 if (shProg
->Uniforms
) {
259 _mesa_free_uniform_list(shProg
->Uniforms
);
260 shProg
->Uniforms
= NULL
;
263 if (shProg
->Varying
) {
264 _mesa_free_parameter_list(shProg
->Varying
);
265 shProg
->Varying
= NULL
;
271 * Free all the data that hangs off a shader program object, but not the
275 _mesa_free_shader_program_data(GLcontext
*ctx
,
276 struct gl_shader_program
*shProg
)
280 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
282 _mesa_clear_shader_program_data(ctx
, shProg
);
284 if (shProg
->Attributes
) {
285 _mesa_free_parameter_list(shProg
->Attributes
);
286 shProg
->Attributes
= NULL
;
290 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
291 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
293 shProg
->NumShaders
= 0;
295 if (shProg
->Shaders
) {
296 free(shProg
->Shaders
);
297 shProg
->Shaders
= NULL
;
300 if (shProg
->InfoLog
) {
301 free(shProg
->InfoLog
);
302 shProg
->InfoLog
= NULL
;
305 /* Transform feedback varying vars */
306 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
307 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
309 free(shProg
->TransformFeedback
.VaryingNames
);
310 shProg
->TransformFeedback
.VaryingNames
= NULL
;
311 shProg
->TransformFeedback
.NumVarying
= 0;
316 * Free/delete a shader program object.
317 * Called via ctx->Driver.DeleteShaderProgram().
320 __mesa_delete_shader_program(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
322 _mesa_free_shader_program_data(ctx
, shProg
);
329 * Lookup a GLSL program object.
331 struct gl_shader_program
*
332 _mesa_lookup_shader_program(GLcontext
*ctx
, GLuint name
)
334 struct gl_shader_program
*shProg
;
336 shProg
= (struct gl_shader_program
*)
337 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
338 /* Note that both gl_shader and gl_shader_program objects are kept
339 * in the same hash table. Check the object's type to be sure it's
340 * what we're expecting.
342 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
352 * As above, but record an error if program is not found.
354 struct gl_shader_program
*
355 _mesa_lookup_shader_program_err(GLcontext
*ctx
, GLuint name
,
359 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
363 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
364 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
366 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
369 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
370 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
379 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
381 driver
->NewShader
= _mesa_new_shader
;
382 driver
->DeleteShader
= __mesa_delete_shader
;
383 driver
->NewShaderProgram
= _mesa_new_shader_program
;
384 driver
->DeleteShaderProgram
= __mesa_delete_shader_program
;