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/hash_table.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);
76 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
77 ctx
->Driver
.DeleteShader(ctx
, old
);
87 /*printf("SHADER INCR %p (%d) to %d\n",
88 (void*) sh, sh->Name, sh->RefCount);*/
94 _mesa_init_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
100 * Allocate a new gl_shader object, initialize it.
101 * Called via ctx->Driver.NewShader()
104 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
)
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
);
113 _mesa_init_shader(ctx
, shader
);
120 * Delete a shader object.
121 * Called via ctx->Driver.DeleteShader().
124 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
127 free((void *) sh
->Source
);
128 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
134 * Lookup a GLSL shader object.
137 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint 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.
146 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
156 * As above, but record an error if shader is not found.
159 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
162 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
166 struct gl_shader
*sh
= (struct gl_shader
*)
167 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
169 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
172 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
173 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
182 /**********************************************************************/
183 /*** Shader Program object functions ***/
184 /**********************************************************************/
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.
194 _mesa_reference_shader_program(struct gl_context
*ctx
,
195 struct gl_shader_program
**ptr
,
196 struct gl_shader_program
*shProg
)
199 if (*ptr
== shProg
) {
204 /* Unreference the old shader program */
205 GLboolean deleteFlag
= GL_FALSE
;
206 struct gl_shader_program
*old
= *ptr
;
208 ASSERT(old
->RefCount
> 0);
211 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
212 (void *) old
, old
->Name
, old
->RefCount
);
214 deleteFlag
= (old
->RefCount
== 0);
218 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
219 ctx
->Driver
.DeleteShaderProgram(ctx
, old
);
229 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
230 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
237 _mesa_init_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
239 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
242 prog
->AttributeBindings
= string_to_uint_map_ctor();
243 prog
->FragDataBindings
= string_to_uint_map_ctor();
245 #if FEATURE_ARB_geometry_shader4
246 prog
->Geom
.VerticesOut
= 0;
247 prog
->Geom
.InputType
= GL_TRIANGLES
;
248 prog
->Geom
.OutputType
= GL_TRIANGLE_STRIP
;
251 prog
->InfoLog
= ralloc_strdup(prog
, "");
255 * Allocate a new gl_shader_program object, initialize it.
256 * Called via ctx->Driver.NewShaderProgram()
258 static struct gl_shader_program
*
259 _mesa_new_shader_program(struct gl_context
*ctx
, GLuint name
)
261 struct gl_shader_program
*shProg
;
262 shProg
= rzalloc(NULL
, struct gl_shader_program
);
265 _mesa_init_shader_program(ctx
, shProg
);
272 * Clear (free) the shader program state that gets produced by linking.
275 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
276 struct gl_shader_program
*shProg
)
278 if (shProg
->UniformStorage
) {
279 ralloc_free(shProg
->UniformStorage
);
280 shProg
->NumUserUniformStorage
= 0;
281 shProg
->UniformStorage
= NULL
;
284 if (shProg
->UniformHash
) {
285 string_to_uint_map_dtor(shProg
->UniformHash
);
286 shProg
->UniformHash
= NULL
;
289 assert(shProg
->InfoLog
!= NULL
);
290 ralloc_free(shProg
->InfoLog
);
291 shProg
->InfoLog
= ralloc_strdup(shProg
, "");
296 * Free all the data that hangs off a shader program object, but not the
300 _mesa_free_shader_program_data(struct gl_context
*ctx
,
301 struct gl_shader_program
*shProg
)
306 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
308 _mesa_clear_shader_program_data(ctx
, shProg
);
310 if (shProg
->AttributeBindings
) {
311 string_to_uint_map_dtor(shProg
->AttributeBindings
);
312 shProg
->AttributeBindings
= NULL
;
315 if (shProg
->FragDataBindings
) {
316 string_to_uint_map_dtor(shProg
->FragDataBindings
);
317 shProg
->FragDataBindings
= NULL
;
321 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
322 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
324 shProg
->NumShaders
= 0;
326 if (shProg
->Shaders
) {
327 free(shProg
->Shaders
);
328 shProg
->Shaders
= NULL
;
331 /* Transform feedback varying vars */
332 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
333 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
335 free(shProg
->TransformFeedback
.VaryingNames
);
336 shProg
->TransformFeedback
.VaryingNames
= NULL
;
337 shProg
->TransformFeedback
.NumVarying
= 0;
340 for (sh
= 0; sh
< MESA_SHADER_TYPES
; sh
++) {
341 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
342 ctx
->Driver
.DeleteShader(ctx
, shProg
->_LinkedShaders
[sh
]);
343 shProg
->_LinkedShaders
[sh
] = NULL
;
350 * Free/delete a shader program object.
351 * Called via ctx->Driver.DeleteShaderProgram().
354 _mesa_delete_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
356 _mesa_free_shader_program_data(ctx
, shProg
);
363 * Lookup a GLSL program object.
365 struct gl_shader_program
*
366 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
368 struct gl_shader_program
*shProg
;
370 shProg
= (struct gl_shader_program
*)
371 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
372 /* Note that both gl_shader and gl_shader_program objects are kept
373 * in the same hash table. Check the object's type to be sure it's
374 * what we're expecting.
376 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
386 * As above, but record an error if program is not found.
388 struct gl_shader_program
*
389 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
393 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
397 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
398 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
400 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
403 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
413 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
415 driver
->NewShader
= _mesa_new_shader
;
416 driver
->DeleteShader
= _mesa_delete_shader
;
417 driver
->NewShaderProgram
= _mesa_new_shader_program
;
418 driver
->DeleteShaderProgram
= _mesa_delete_shader_program
;
419 driver
->LinkShader
= _mesa_ir_link_shader
;