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/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"
43 /**********************************************************************/
44 /*** Shader object functions ***/
45 /**********************************************************************/
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.
55 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
64 /* Unreference the old shader */
65 GLboolean deleteFlag
= GL_FALSE
;
66 struct gl_shader
*old
= *ptr
;
68 ASSERT(old
->RefCount
> 0);
70 /*printf("SHADER DECR %p (%d) to %d\n",
71 (void*) old, old->Name, old->RefCount);*/
72 deleteFlag
= (old
->RefCount
== 0);
75 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
76 ctx
->Driver
.DeleteShader(ctx
, old
);
86 /*printf("SHADER INCR %p (%d) to %d\n",
87 (void*) sh, sh->Name, sh->RefCount);*/
93 _mesa_init_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
99 * Allocate a new gl_shader object, initialize it.
100 * Called via ctx->Driver.NewShader()
103 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
)
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
);
112 _mesa_init_shader(ctx
, shader
);
119 * Delete a shader object.
120 * Called via ctx->Driver.DeleteShader().
123 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
126 free((void *) sh
->Source
);
127 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
133 * Lookup a GLSL shader object.
136 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint 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.
145 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
155 * As above, but record an error if shader is not found.
158 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
161 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
165 struct gl_shader
*sh
= (struct gl_shader
*)
166 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
168 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
171 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
172 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
181 /**********************************************************************/
182 /*** Shader Program object functions ***/
183 /**********************************************************************/
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.
193 _mesa_reference_shader_program(struct gl_context
*ctx
,
194 struct gl_shader_program
**ptr
,
195 struct gl_shader_program
*shProg
)
198 if (*ptr
== shProg
) {
203 /* Unreference the old shader program */
204 GLboolean deleteFlag
= GL_FALSE
;
205 struct gl_shader_program
*old
= *ptr
;
207 ASSERT(old
->RefCount
> 0);
210 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
211 (void *) old
, old
->Name
, old
->RefCount
);
213 deleteFlag
= (old
->RefCount
== 0);
216 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
217 ctx
->Driver
.DeleteShaderProgram(ctx
, old
);
227 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
228 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
235 _mesa_init_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
237 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
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
;
248 * Allocate a new gl_shader_program object, initialize it.
249 * Called via ctx->Driver.NewShaderProgram()
251 static struct gl_shader_program
*
252 _mesa_new_shader_program(struct gl_context
*ctx
, GLuint name
)
254 struct gl_shader_program
*shProg
;
255 shProg
= talloc_zero(NULL
, struct gl_shader_program
);
258 _mesa_init_shader_program(ctx
, shProg
);
265 * Clear (free) the shader program state that gets produced by linking.
268 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
269 struct gl_shader_program
*shProg
)
271 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
272 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
273 _mesa_reference_geomprog(ctx
, &shProg
->GeometryProgram
, NULL
);
275 if (shProg
->Uniforms
) {
276 _mesa_free_uniform_list(shProg
->Uniforms
);
277 shProg
->Uniforms
= NULL
;
280 if (shProg
->Varying
) {
281 _mesa_free_parameter_list(shProg
->Varying
);
282 shProg
->Varying
= NULL
;
288 * Free all the data that hangs off a shader program object, but not the
292 _mesa_free_shader_program_data(struct gl_context
*ctx
,
293 struct gl_shader_program
*shProg
)
298 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
300 _mesa_clear_shader_program_data(ctx
, shProg
);
302 if (shProg
->Attributes
) {
303 _mesa_free_parameter_list(shProg
->Attributes
);
304 shProg
->Attributes
= NULL
;
308 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
309 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
311 shProg
->NumShaders
= 0;
313 if (shProg
->Shaders
) {
314 free(shProg
->Shaders
);
315 shProg
->Shaders
= NULL
;
318 if (shProg
->InfoLog
) {
319 talloc_free(shProg
->InfoLog
);
320 shProg
->InfoLog
= NULL
;
323 /* Transform feedback varying vars */
324 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
325 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
327 free(shProg
->TransformFeedback
.VaryingNames
);
328 shProg
->TransformFeedback
.VaryingNames
= NULL
;
329 shProg
->TransformFeedback
.NumVarying
= 0;
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
;
342 * Free/delete a shader program object.
343 * Called via ctx->Driver.DeleteShaderProgram().
346 _mesa_delete_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
348 _mesa_free_shader_program_data(ctx
, shProg
);
355 * Lookup a GLSL program object.
357 struct gl_shader_program
*
358 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
360 struct gl_shader_program
*shProg
;
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.
368 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
378 * As above, but record an error if program is not found.
380 struct gl_shader_program
*
381 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
385 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
389 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
390 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
392 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
395 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
396 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
405 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
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
;