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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
33 #include "main/glheader.h"
34 #include "main/context.h"
35 #include "main/hash.h"
36 #include "main/mtypes.h"
37 #include "main/shaderobj.h"
38 #include "main/uniforms.h"
39 #include "program/program.h"
40 #include "program/prog_parameter.h"
41 #include "program/hash_table.h"
44 /**********************************************************************/
45 /*** Shader object functions ***/
46 /**********************************************************************/
50 * Set ptr to point to sh.
51 * If ptr is pointing to another shader, decrement its refcount (and delete
52 * if refcount hits zero).
53 * Then set ptr to point to sh, incrementing its refcount.
56 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
65 /* Unreference the old shader */
66 GLboolean deleteFlag
= GL_FALSE
;
67 struct gl_shader
*old
= *ptr
;
69 ASSERT(old
->RefCount
> 0);
71 /*printf("SHADER DECR %p (%d) to %d\n",
72 (void*) old, old->Name, old->RefCount);*/
73 deleteFlag
= (old
->RefCount
== 0);
77 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
78 ctx
->Driver
.DeleteShader(ctx
, old
);
88 /*printf("SHADER INCR %p (%d) to %d\n",
89 (void*) sh, sh->Name, sh->RefCount);*/
95 _mesa_init_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
101 * Allocate a new gl_shader object, initialize it.
102 * Called via ctx->Driver.NewShader()
105 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
)
107 struct gl_shader
*shader
;
108 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
109 type
== GL_GEOMETRY_SHADER_ARB
);
110 shader
= rzalloc(NULL
, struct gl_shader
);
114 _mesa_init_shader(ctx
, shader
);
121 * Delete a shader object.
122 * Called via ctx->Driver.DeleteShader().
125 _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();
244 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
246 prog
->Geom
.VerticesOut
= 0;
247 prog
->Geom
.InputType
= GL_TRIANGLES
;
248 prog
->Geom
.OutputType
= GL_TRIANGLE_STRIP
;
250 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
252 prog
->InfoLog
= ralloc_strdup(prog
, "");
256 * Allocate a new gl_shader_program object, initialize it.
257 * Called via ctx->Driver.NewShaderProgram()
259 static struct gl_shader_program
*
260 _mesa_new_shader_program(struct gl_context
*ctx
, GLuint name
)
262 struct gl_shader_program
*shProg
;
263 shProg
= rzalloc(NULL
, struct gl_shader_program
);
266 _mesa_init_shader_program(ctx
, shProg
);
273 * Clear (free) the shader program state that gets produced by linking.
276 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
277 struct gl_shader_program
*shProg
)
279 if (shProg
->UniformStorage
) {
281 for (i
= 0; i
< shProg
->NumUserUniformStorage
; ++i
)
282 _mesa_uniform_detach_all_driver_storage(&shProg
->UniformStorage
[i
]);
283 ralloc_free(shProg
->UniformStorage
);
284 shProg
->NumUserUniformStorage
= 0;
285 shProg
->UniformStorage
= NULL
;
288 if (shProg
->UniformHash
) {
289 string_to_uint_map_dtor(shProg
->UniformHash
);
290 shProg
->UniformHash
= NULL
;
293 assert(shProg
->InfoLog
!= NULL
);
294 ralloc_free(shProg
->InfoLog
);
295 shProg
->InfoLog
= ralloc_strdup(shProg
, "");
300 * Free all the data that hangs off a shader program object, but not the
304 _mesa_free_shader_program_data(struct gl_context
*ctx
,
305 struct gl_shader_program
*shProg
)
310 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
312 _mesa_clear_shader_program_data(ctx
, shProg
);
314 if (shProg
->AttributeBindings
) {
315 string_to_uint_map_dtor(shProg
->AttributeBindings
);
316 shProg
->AttributeBindings
= NULL
;
319 if (shProg
->FragDataBindings
) {
320 string_to_uint_map_dtor(shProg
->FragDataBindings
);
321 shProg
->FragDataBindings
= NULL
;
324 if (shProg
->FragDataIndexBindings
) {
325 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
326 shProg
->FragDataIndexBindings
= NULL
;
330 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
331 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
333 shProg
->NumShaders
= 0;
335 free(shProg
->Shaders
);
336 shProg
->Shaders
= NULL
;
338 /* Transform feedback varying vars */
339 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
340 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
342 free(shProg
->TransformFeedback
.VaryingNames
);
343 shProg
->TransformFeedback
.VaryingNames
= NULL
;
344 shProg
->TransformFeedback
.NumVarying
= 0;
347 for (sh
= 0; sh
< MESA_SHADER_TYPES
; sh
++) {
348 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
349 ctx
->Driver
.DeleteShader(ctx
, shProg
->_LinkedShaders
[sh
]);
350 shProg
->_LinkedShaders
[sh
] = NULL
;
357 * Free/delete a shader program object.
358 * Called via ctx->Driver.DeleteShaderProgram().
361 _mesa_delete_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
363 _mesa_free_shader_program_data(ctx
, shProg
);
370 * Lookup a GLSL program object.
372 struct gl_shader_program
*
373 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
375 struct gl_shader_program
*shProg
;
377 shProg
= (struct gl_shader_program
*)
378 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
379 /* Note that both gl_shader and gl_shader_program objects are kept
380 * in the same hash table. Check the object's type to be sure it's
381 * what we're expecting.
383 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
393 * As above, but record an error if program is not found.
395 struct gl_shader_program
*
396 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
400 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
404 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
405 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
407 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
410 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
411 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
420 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
422 driver
->NewShader
= _mesa_new_shader
;
423 driver
->DeleteShader
= _mesa_delete_shader
;
424 driver
->NewShaderProgram
= _mesa_new_shader_program
;
425 driver
->DeleteShaderProgram
= _mesa_delete_shader_program
;
426 driver
->LinkShader
= _mesa_ir_link_shader
;