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 "util/ralloc.h"
43 #include "util/string_to_uint_map.h"
44 #include "util/u_atomic.h"
46 /**********************************************************************/
47 /*** Shader object functions ***/
48 /**********************************************************************/
52 * Set ptr to point to sh.
53 * If ptr is pointing to another shader, decrement its refcount (and delete
54 * if refcount hits zero).
55 * Then set ptr to point to sh, incrementing its refcount.
58 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
67 /* Unreference the old shader */
68 GLboolean deleteFlag
= GL_FALSE
;
69 struct gl_shader
*old
= *ptr
;
71 assert(old
->RefCount
> 0);
73 deleteFlag
= (old
->RefCount
== 0);
77 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
78 _mesa_delete_shader(ctx
, old
);
93 _mesa_init_shader(struct gl_shader
*shader
)
96 shader
->info
.Geom
.VerticesOut
= -1;
97 shader
->info
.Geom
.InputType
= GL_TRIANGLES
;
98 shader
->info
.Geom
.OutputType
= GL_TRIANGLE_STRIP
;
102 * Allocate a new gl_shader object, initialize it.
105 _mesa_new_shader(GLuint name
, gl_shader_stage stage
)
107 struct gl_shader
*shader
;
108 shader
= rzalloc(NULL
, struct gl_shader
);
110 shader
->Stage
= stage
;
112 _mesa_init_shader(shader
);
119 * Allocate a new gl_linked_shader object.
120 * Called via ctx->Driver.NewShader()
122 struct gl_linked_shader
*
123 _mesa_new_linked_shader(gl_shader_stage stage
)
125 struct gl_linked_shader
*shader
;
126 shader
= rzalloc(NULL
, struct gl_linked_shader
);
128 shader
->Stage
= stage
;
135 * Delete a shader object.
138 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
140 free((void *)sh
->Source
);
147 * Delete a shader object.
150 _mesa_delete_linked_shader(struct gl_context
*ctx
,
151 struct gl_linked_shader
*sh
)
153 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
159 * Lookup a GLSL shader object.
162 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
165 struct gl_shader
*sh
= (struct gl_shader
*)
166 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
167 /* Note that both gl_shader and gl_shader_program objects are kept
168 * in the same hash table. Check the object's type to be sure it's
169 * what we're expecting.
171 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
181 * As above, but record an error if shader is not found.
184 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
187 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
191 struct gl_shader
*sh
= (struct gl_shader
*)
192 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
194 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
197 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
198 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
207 /**********************************************************************/
208 /*** Shader Program object functions ***/
209 /**********************************************************************/
213 _mesa_reference_shader_program_data(struct gl_context
*ctx
,
214 struct gl_shader_program_data
**ptr
,
215 struct gl_shader_program_data
*data
)
221 struct gl_shader_program_data
*oldData
= *ptr
;
223 assert(oldData
->RefCount
> 0);
225 if (p_atomic_dec_zero(&oldData
->RefCount
)) {
227 ralloc_free(oldData
);
234 p_atomic_inc(&data
->RefCount
);
240 * Set ptr to point to shProg.
241 * If ptr is pointing to another object, decrement its refcount (and delete
242 * if refcount hits zero).
243 * Then set ptr to point to shProg, incrementing its refcount.
246 _mesa_reference_shader_program_(struct gl_context
*ctx
,
247 struct gl_shader_program
**ptr
,
248 struct gl_shader_program
*shProg
)
251 if (*ptr
== shProg
) {
256 /* Unreference the old shader program */
257 GLboolean deleteFlag
= GL_FALSE
;
258 struct gl_shader_program
*old
= *ptr
;
260 assert(old
->RefCount
> 0);
262 deleteFlag
= (old
->RefCount
== 0);
266 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
267 _mesa_delete_shader_program(ctx
, old
);
280 static struct gl_shader_program_data
*
281 create_shader_program_data()
283 struct gl_shader_program_data
*data
;
284 data
= rzalloc(NULL
, struct gl_shader_program_data
);
292 init_shader_program(struct gl_shader_program
*prog
)
294 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
297 prog
->AttributeBindings
= string_to_uint_map_ctor();
298 prog
->FragDataBindings
= string_to_uint_map_ctor();
299 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
301 prog
->Geom
.UsesEndPrimitive
= false;
302 prog
->Geom
.UsesStreams
= false;
304 prog
->Comp
.LocalSizeVariable
= false;
306 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
308 exec_list_make_empty(&prog
->EmptyUniformLocations
);
310 prog
->data
->InfoLog
= ralloc_strdup(prog
->data
, "");
314 * Allocate a new gl_shader_program object, initialize it.
316 struct gl_shader_program
*
317 _mesa_new_shader_program(GLuint name
)
319 struct gl_shader_program
*shProg
;
320 shProg
= rzalloc(NULL
, struct gl_shader_program
);
323 shProg
->data
= create_shader_program_data();
328 init_shader_program(shProg
);
335 * Clear (free) the shader program state that gets produced by linking.
338 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
339 struct gl_shader_program
*shProg
)
341 for (gl_shader_stage sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
342 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
343 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
344 shProg
->_LinkedShaders
[sh
] = NULL
;
348 if (shProg
->data
->UniformStorage
) {
349 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; ++i
)
350 _mesa_uniform_detach_all_driver_storage(&shProg
->data
->
352 ralloc_free(shProg
->data
->UniformStorage
);
353 shProg
->data
->NumUniformStorage
= 0;
354 shProg
->data
->UniformStorage
= NULL
;
357 if (shProg
->UniformRemapTable
) {
358 ralloc_free(shProg
->UniformRemapTable
);
359 shProg
->NumUniformRemapTable
= 0;
360 shProg
->UniformRemapTable
= NULL
;
363 if (shProg
->UniformHash
) {
364 string_to_uint_map_dtor(shProg
->UniformHash
);
365 shProg
->UniformHash
= NULL
;
368 assert(shProg
->data
->InfoLog
!= NULL
);
369 ralloc_free(shProg
->data
->InfoLog
);
370 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, "");
372 ralloc_free(shProg
->data
->UniformBlocks
);
373 shProg
->data
->UniformBlocks
= NULL
;
374 shProg
->data
->NumUniformBlocks
= 0;
376 ralloc_free(shProg
->data
->ShaderStorageBlocks
);
377 shProg
->data
->ShaderStorageBlocks
= NULL
;
378 shProg
->data
->NumShaderStorageBlocks
= 0;
380 ralloc_free(shProg
->data
->AtomicBuffers
);
381 shProg
->data
->AtomicBuffers
= NULL
;
382 shProg
->data
->NumAtomicBuffers
= 0;
384 if (shProg
->ProgramResourceList
) {
385 ralloc_free(shProg
->ProgramResourceList
);
386 shProg
->ProgramResourceList
= NULL
;
387 shProg
->NumProgramResourceList
= 0;
393 * Free all the data that hangs off a shader program object, but not the
397 _mesa_free_shader_program_data(struct gl_context
*ctx
,
398 struct gl_shader_program
*shProg
)
402 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
404 _mesa_clear_shader_program_data(ctx
, shProg
);
406 if (shProg
->AttributeBindings
) {
407 string_to_uint_map_dtor(shProg
->AttributeBindings
);
408 shProg
->AttributeBindings
= NULL
;
411 if (shProg
->FragDataBindings
) {
412 string_to_uint_map_dtor(shProg
->FragDataBindings
);
413 shProg
->FragDataBindings
= NULL
;
416 if (shProg
->FragDataIndexBindings
) {
417 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
418 shProg
->FragDataIndexBindings
= NULL
;
422 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
423 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
425 shProg
->NumShaders
= 0;
427 free(shProg
->Shaders
);
428 shProg
->Shaders
= NULL
;
430 /* Transform feedback varying vars */
431 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
432 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
434 free(shProg
->TransformFeedback
.VaryingNames
);
435 shProg
->TransformFeedback
.VaryingNames
= NULL
;
436 shProg
->TransformFeedback
.NumVarying
= 0;
439 shProg
->Label
= NULL
;
444 * Free/delete a shader program object.
447 _mesa_delete_shader_program(struct gl_context
*ctx
,
448 struct gl_shader_program
*shProg
)
450 _mesa_free_shader_program_data(ctx
, shProg
);
451 _mesa_reference_shader_program_data(ctx
, &shProg
->data
, NULL
);
457 * Lookup a GLSL program object.
459 struct gl_shader_program
*
460 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
462 struct gl_shader_program
*shProg
;
464 shProg
= (struct gl_shader_program
*)
465 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
466 /* Note that both gl_shader and gl_shader_program objects are kept
467 * in the same hash table. Check the object's type to be sure it's
468 * what we're expecting.
470 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
480 * As above, but record an error if program is not found.
482 struct gl_shader_program
*
483 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
487 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
491 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
492 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
494 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
497 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
498 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
507 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
509 driver
->NewShader
= _mesa_new_linked_shader
;
510 driver
->LinkShader
= _mesa_ir_link_shader
;