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
->InfoLog
= ralloc_strdup(prog
, "");
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 init_shader_program(shProg
);
330 * Clear (free) the shader program state that gets produced by linking.
333 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
334 struct gl_shader_program
*shProg
)
336 for (gl_shader_stage sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
337 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
338 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
339 shProg
->_LinkedShaders
[sh
] = NULL
;
343 if (shProg
->UniformStorage
) {
344 for (unsigned i
= 0; i
< shProg
->NumUniformStorage
; ++i
)
345 _mesa_uniform_detach_all_driver_storage(&shProg
->UniformStorage
[i
]);
346 ralloc_free(shProg
->UniformStorage
);
347 shProg
->NumUniformStorage
= 0;
348 shProg
->UniformStorage
= NULL
;
351 if (shProg
->UniformRemapTable
) {
352 ralloc_free(shProg
->UniformRemapTable
);
353 shProg
->NumUniformRemapTable
= 0;
354 shProg
->UniformRemapTable
= NULL
;
357 if (shProg
->UniformHash
) {
358 string_to_uint_map_dtor(shProg
->UniformHash
);
359 shProg
->UniformHash
= NULL
;
362 assert(shProg
->InfoLog
!= NULL
);
363 ralloc_free(shProg
->InfoLog
);
364 shProg
->InfoLog
= ralloc_strdup(shProg
, "");
366 ralloc_free(shProg
->UniformBlocks
);
367 shProg
->UniformBlocks
= NULL
;
368 shProg
->NumUniformBlocks
= 0;
370 ralloc_free(shProg
->ShaderStorageBlocks
);
371 shProg
->ShaderStorageBlocks
= NULL
;
372 shProg
->NumShaderStorageBlocks
= 0;
374 ralloc_free(shProg
->AtomicBuffers
);
375 shProg
->AtomicBuffers
= NULL
;
376 shProg
->NumAtomicBuffers
= 0;
378 if (shProg
->ProgramResourceList
) {
379 ralloc_free(shProg
->ProgramResourceList
);
380 shProg
->ProgramResourceList
= NULL
;
381 shProg
->NumProgramResourceList
= 0;
387 * Free all the data that hangs off a shader program object, but not the
391 _mesa_free_shader_program_data(struct gl_context
*ctx
,
392 struct gl_shader_program
*shProg
)
396 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
398 _mesa_clear_shader_program_data(ctx
, shProg
);
400 if (shProg
->AttributeBindings
) {
401 string_to_uint_map_dtor(shProg
->AttributeBindings
);
402 shProg
->AttributeBindings
= NULL
;
405 if (shProg
->FragDataBindings
) {
406 string_to_uint_map_dtor(shProg
->FragDataBindings
);
407 shProg
->FragDataBindings
= NULL
;
410 if (shProg
->FragDataIndexBindings
) {
411 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
412 shProg
->FragDataIndexBindings
= NULL
;
416 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
417 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
419 shProg
->NumShaders
= 0;
421 free(shProg
->Shaders
);
422 shProg
->Shaders
= NULL
;
424 /* Transform feedback varying vars */
425 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
426 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
428 free(shProg
->TransformFeedback
.VaryingNames
);
429 shProg
->TransformFeedback
.VaryingNames
= NULL
;
430 shProg
->TransformFeedback
.NumVarying
= 0;
433 shProg
->Label
= NULL
;
438 * Free/delete a shader program object.
441 _mesa_delete_shader_program(struct gl_context
*ctx
,
442 struct gl_shader_program
*shProg
)
444 _mesa_free_shader_program_data(ctx
, shProg
);
451 * Lookup a GLSL program object.
453 struct gl_shader_program
*
454 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
456 struct gl_shader_program
*shProg
;
458 shProg
= (struct gl_shader_program
*)
459 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
460 /* Note that both gl_shader and gl_shader_program objects are kept
461 * in the same hash table. Check the object's type to be sure it's
462 * what we're expecting.
464 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
474 * As above, but record an error if program is not found.
476 struct gl_shader_program
*
477 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
481 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
485 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
486 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
488 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
491 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
492 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
501 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
503 driver
->NewShader
= _mesa_new_linked_shader
;
504 driver
->LinkShader
= _mesa_ir_link_shader
;