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()
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 type
== GL_GEOMETRY_SHADER_ARB
);
101 shader
= talloc_zero(NULL
, struct gl_shader
);
105 shader
->RefCount
= 1;
112 * Delete a shader object.
113 * Called via ctx->Driver.DeleteShader().
116 __mesa_delete_shader(GLcontext
*ctx
, struct gl_shader
*sh
)
119 free((void *) sh
->Source
);
120 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
126 * Lookup a GLSL shader object.
129 _mesa_lookup_shader(GLcontext
*ctx
, GLuint name
)
132 struct gl_shader
*sh
= (struct gl_shader
*)
133 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
134 /* Note that both gl_shader and gl_shader_program objects are kept
135 * in the same hash table. Check the object's type to be sure it's
136 * what we're expecting.
138 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
148 * As above, but record an error if shader is not found.
151 _mesa_lookup_shader_err(GLcontext
*ctx
, GLuint name
, const char *caller
)
154 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
158 struct gl_shader
*sh
= (struct gl_shader
*)
159 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
161 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
164 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
165 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
174 /**********************************************************************/
175 /*** Shader Program object functions ***/
176 /**********************************************************************/
180 * Set ptr to point to shProg.
181 * If ptr is pointing to another object, decrement its refcount (and delete
182 * if refcount hits zero).
183 * Then set ptr to point to shProg, incrementing its refcount.
186 _mesa_reference_shader_program(GLcontext
*ctx
,
187 struct gl_shader_program
**ptr
,
188 struct gl_shader_program
*shProg
)
191 if (*ptr
== shProg
) {
196 /* Unreference the old shader program */
197 GLboolean deleteFlag
= GL_FALSE
;
198 struct gl_shader_program
*old
= *ptr
;
200 ASSERT(old
->RefCount
> 0);
203 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
204 (void *) old
, old
->Name
, old
->RefCount
);
206 deleteFlag
= (old
->RefCount
== 0);
209 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
210 ctx
->Driver
.DeleteShaderProgram(ctx
, old
);
220 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
221 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
229 * Allocate a new gl_shader_program object, initialize it.
230 * Called via ctx->Driver.NewShaderProgram()
232 static struct gl_shader_program
*
233 _mesa_new_shader_program(GLcontext
*ctx
, GLuint name
)
235 struct gl_shader_program
*shProg
;
236 shProg
= talloc_zero(NULL
, struct gl_shader_program
);
238 shProg
->Type
= GL_SHADER_PROGRAM_MESA
;
240 shProg
->RefCount
= 1;
241 shProg
->Attributes
= _mesa_new_parameter_list();
242 #if FEATURE_ARB_geometry_shader4
243 shProg
->Geom
.VerticesOut
= 0;
244 shProg
->Geom
.InputType
= GL_TRIANGLES
;
245 shProg
->Geom
.OutputType
= GL_TRIANGLE_STRIP
;
253 * Clear (free) the shader program state that gets produced by linking.
256 _mesa_clear_shader_program_data(GLcontext
*ctx
,
257 struct gl_shader_program
*shProg
)
259 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
260 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
261 _mesa_reference_geomprog(ctx
, &shProg
->GeometryProgram
, NULL
);
263 if (shProg
->Uniforms
) {
264 _mesa_free_uniform_list(shProg
->Uniforms
);
265 shProg
->Uniforms
= NULL
;
268 if (shProg
->Varying
) {
269 _mesa_free_parameter_list(shProg
->Varying
);
270 shProg
->Varying
= NULL
;
276 * Free all the data that hangs off a shader program object, but not the
280 _mesa_free_shader_program_data(GLcontext
*ctx
,
281 struct gl_shader_program
*shProg
)
285 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
287 _mesa_clear_shader_program_data(ctx
, shProg
);
289 if (shProg
->Attributes
) {
290 _mesa_free_parameter_list(shProg
->Attributes
);
291 shProg
->Attributes
= NULL
;
295 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
296 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
298 shProg
->NumShaders
= 0;
300 if (shProg
->Shaders
) {
301 free(shProg
->Shaders
);
302 shProg
->Shaders
= NULL
;
305 if (shProg
->InfoLog
) {
306 talloc_free(shProg
->InfoLog
);
307 shProg
->InfoLog
= NULL
;
310 /* Transform feedback varying vars */
311 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
312 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
314 free(shProg
->TransformFeedback
.VaryingNames
);
315 shProg
->TransformFeedback
.VaryingNames
= NULL
;
316 shProg
->TransformFeedback
.NumVarying
= 0;
321 * Free/delete a shader program object.
322 * Called via ctx->Driver.DeleteShaderProgram().
325 __mesa_delete_shader_program(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
327 _mesa_free_shader_program_data(ctx
, shProg
);
334 * Lookup a GLSL program object.
336 struct gl_shader_program
*
337 _mesa_lookup_shader_program(GLcontext
*ctx
, GLuint name
)
339 struct gl_shader_program
*shProg
;
341 shProg
= (struct gl_shader_program
*)
342 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
343 /* Note that both gl_shader and gl_shader_program objects are kept
344 * in the same hash table. Check the object's type to be sure it's
345 * what we're expecting.
347 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
357 * As above, but record an error if program is not found.
359 struct gl_shader_program
*
360 _mesa_lookup_shader_program_err(GLcontext
*ctx
, GLuint name
,
364 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
368 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
369 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
371 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
374 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
375 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
384 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
386 driver
->NewShader
= _mesa_new_shader
;
387 driver
->DeleteShader
= __mesa_delete_shader
;
388 driver
->NewShaderProgram
= _mesa_new_shader_program
;
389 driver
->DeleteShaderProgram
= __mesa_delete_shader_program
;
390 driver
->CompileShader
= _mesa_ir_compile_shader
;
391 driver
->LinkShader
= _mesa_ir_link_shader
;