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/shaderapi.h"
38 #include "main/shaderobj.h"
39 #include "main/uniforms.h"
40 #include "program/program.h"
41 #include "program/prog_parameter.h"
42 #include "program/hash_table.h"
43 #include "util/ralloc.h"
45 /**********************************************************************/
46 /*** Shader object functions ***/
47 /**********************************************************************/
51 * Set ptr to point to sh.
52 * If ptr is pointing to another shader, decrement its refcount (and delete
53 * if refcount hits zero).
54 * Then set ptr to point to sh, incrementing its refcount.
57 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
66 /* Unreference the old shader */
67 GLboolean deleteFlag
= GL_FALSE
;
68 struct gl_shader
*old
= *ptr
;
70 ASSERT(old
->RefCount
> 0);
72 /*printf("SHADER DECR %p (%d) to %d\n",
73 (void*) old, old->Name, old->RefCount);*/
74 deleteFlag
= (old
->RefCount
== 0);
78 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
79 ctx
->Driver
.DeleteShader(ctx
, old
);
89 /*printf("SHADER INCR %p (%d) to %d\n",
90 (void*) sh, sh->Name, sh->RefCount);*/
96 _mesa_init_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
102 * Allocate a new gl_shader object, initialize it.
103 * Called via ctx->Driver.NewShader()
106 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
)
108 struct gl_shader
*shader
;
109 assert(_mesa_validate_shader_target(ctx
, type
));
110 shader
= rzalloc(NULL
, struct gl_shader
);
113 shader
->Stage
= _mesa_shader_enum_to_shader_stage(type
);
115 _mesa_init_shader(ctx
, shader
);
122 * Delete a shader object.
123 * Called via ctx->Driver.DeleteShader().
126 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
128 free((void *)sh
->Source
);
130 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
136 * Lookup a GLSL shader object.
139 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
142 struct gl_shader
*sh
= (struct gl_shader
*)
143 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
144 /* Note that both gl_shader and gl_shader_program objects are kept
145 * in the same hash table. Check the object's type to be sure it's
146 * what we're expecting.
148 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
158 * As above, but record an error if shader is not found.
161 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
164 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
168 struct gl_shader
*sh
= (struct gl_shader
*)
169 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
171 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
174 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
175 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
184 /**********************************************************************/
185 /*** Shader Program object functions ***/
186 /**********************************************************************/
190 * Set ptr to point to shProg.
191 * If ptr is pointing to another object, decrement its refcount (and delete
192 * if refcount hits zero).
193 * Then set ptr to point to shProg, incrementing its refcount.
196 _mesa_reference_shader_program_(struct gl_context
*ctx
,
197 struct gl_shader_program
**ptr
,
198 struct gl_shader_program
*shProg
)
201 if (*ptr
== shProg
) {
206 /* Unreference the old shader program */
207 GLboolean deleteFlag
= GL_FALSE
;
208 struct gl_shader_program
*old
= *ptr
;
210 ASSERT(old
->RefCount
> 0);
213 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
214 (void *) old
, old
->Name
, old
->RefCount
);
216 deleteFlag
= (old
->RefCount
== 0);
220 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
221 ctx
->Driver
.DeleteShaderProgram(ctx
, old
);
231 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
232 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
239 init_shader_program(struct gl_shader_program
*prog
)
241 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
244 prog
->AttributeBindings
= string_to_uint_map_ctor();
245 prog
->FragDataBindings
= string_to_uint_map_ctor();
246 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
248 prog
->Geom
.VerticesOut
= 0;
249 prog
->Geom
.InputType
= GL_TRIANGLES
;
250 prog
->Geom
.OutputType
= GL_TRIANGLE_STRIP
;
251 prog
->Geom
.UsesEndPrimitive
= false;
252 prog
->Geom
.UsesStreams
= false;
254 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
256 prog
->InfoLog
= ralloc_strdup(prog
, "");
260 * Allocate a new gl_shader_program object, initialize it.
261 * Called via ctx->Driver.NewShaderProgram()
263 static struct gl_shader_program
*
264 _mesa_new_shader_program(GLuint name
)
266 struct gl_shader_program
*shProg
;
267 shProg
= rzalloc(NULL
, struct gl_shader_program
);
270 init_shader_program(shProg
);
277 * Clear (free) the shader program state that gets produced by linking.
280 _mesa_clear_shader_program_data(struct gl_shader_program
*shProg
)
284 if (shProg
->UniformStorage
) {
285 for (i
= 0; i
< shProg
->NumUserUniformStorage
; ++i
)
286 _mesa_uniform_detach_all_driver_storage(&shProg
->UniformStorage
[i
]);
287 ralloc_free(shProg
->UniformStorage
);
288 shProg
->NumUserUniformStorage
= 0;
289 shProg
->UniformStorage
= NULL
;
292 if (shProg
->UniformRemapTable
) {
293 ralloc_free(shProg
->UniformRemapTable
);
294 shProg
->NumUniformRemapTable
= 0;
295 shProg
->UniformRemapTable
= NULL
;
298 if (shProg
->UniformHash
) {
299 string_to_uint_map_dtor(shProg
->UniformHash
);
300 shProg
->UniformHash
= NULL
;
303 assert(shProg
->InfoLog
!= NULL
);
304 ralloc_free(shProg
->InfoLog
);
305 shProg
->InfoLog
= ralloc_strdup(shProg
, "");
307 ralloc_free(shProg
->UniformBlocks
);
308 shProg
->UniformBlocks
= NULL
;
309 shProg
->NumUniformBlocks
= 0;
310 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
311 ralloc_free(shProg
->UniformBlockStageIndex
[i
]);
312 shProg
->UniformBlockStageIndex
[i
] = NULL
;
315 ralloc_free(shProg
->AtomicBuffers
);
316 shProg
->AtomicBuffers
= NULL
;
317 shProg
->NumAtomicBuffers
= 0;
322 * Free all the data that hangs off a shader program object, but not the
326 _mesa_free_shader_program_data(struct gl_context
*ctx
,
327 struct gl_shader_program
*shProg
)
332 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
334 _mesa_clear_shader_program_data(shProg
);
336 if (shProg
->AttributeBindings
) {
337 string_to_uint_map_dtor(shProg
->AttributeBindings
);
338 shProg
->AttributeBindings
= NULL
;
341 if (shProg
->FragDataBindings
) {
342 string_to_uint_map_dtor(shProg
->FragDataBindings
);
343 shProg
->FragDataBindings
= NULL
;
346 if (shProg
->FragDataIndexBindings
) {
347 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
348 shProg
->FragDataIndexBindings
= NULL
;
352 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
353 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
355 shProg
->NumShaders
= 0;
357 free(shProg
->Shaders
);
358 shProg
->Shaders
= NULL
;
360 /* Transform feedback varying vars */
361 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
362 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
364 free(shProg
->TransformFeedback
.VaryingNames
);
365 shProg
->TransformFeedback
.VaryingNames
= NULL
;
366 shProg
->TransformFeedback
.NumVarying
= 0;
369 for (sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
370 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
371 ctx
->Driver
.DeleteShader(ctx
, shProg
->_LinkedShaders
[sh
]);
372 shProg
->_LinkedShaders
[sh
] = NULL
;
377 shProg
->Label
= NULL
;
382 * Free/delete a shader program object.
383 * Called via ctx->Driver.DeleteShaderProgram().
386 _mesa_delete_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
388 _mesa_free_shader_program_data(ctx
, shProg
);
395 * Lookup a GLSL program object.
397 struct gl_shader_program
*
398 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
400 struct gl_shader_program
*shProg
;
402 shProg
= (struct gl_shader_program
*)
403 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
404 /* Note that both gl_shader and gl_shader_program objects are kept
405 * in the same hash table. Check the object's type to be sure it's
406 * what we're expecting.
408 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
418 * As above, but record an error if program is not found.
420 struct gl_shader_program
*
421 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
425 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
429 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
430 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
432 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
435 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
436 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
445 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
447 driver
->NewShader
= _mesa_new_shader
;
448 driver
->DeleteShader
= _mesa_delete_shader
;
449 driver
->NewShaderProgram
= _mesa_new_shader_program
;
450 driver
->DeleteShaderProgram
= _mesa_delete_shader_program
;
451 driver
->LinkShader
= _mesa_ir_link_shader
;