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
;
113 shader
->SourceChecksum
= 0xa110c; /* alloc */
115 _mesa_init_shader(shader
);
122 * Allocate a new gl_linked_shader object.
123 * Called via ctx->Driver.NewShader()
125 struct gl_linked_shader
*
126 _mesa_new_linked_shader(gl_shader_stage stage
)
128 struct gl_linked_shader
*shader
;
129 shader
= rzalloc(NULL
, struct gl_linked_shader
);
131 shader
->Stage
= stage
;
138 * Delete a shader object.
141 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
143 free((void *)sh
->Source
);
150 * Delete a shader object.
153 _mesa_delete_linked_shader(struct gl_context
*ctx
,
154 struct gl_linked_shader
*sh
)
156 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
162 * Lookup a GLSL shader object.
165 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
168 struct gl_shader
*sh
= (struct gl_shader
*)
169 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
170 /* Note that both gl_shader and gl_shader_program objects are kept
171 * in the same hash table. Check the object's type to be sure it's
172 * what we're expecting.
174 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
184 * As above, but record an error if shader is not found.
187 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
190 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
194 struct gl_shader
*sh
= (struct gl_shader
*)
195 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
197 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
200 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
201 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
210 /**********************************************************************/
211 /*** Shader Program object functions ***/
212 /**********************************************************************/
216 _mesa_reference_shader_program_data(struct gl_context
*ctx
,
217 struct gl_shader_program_data
**ptr
,
218 struct gl_shader_program_data
*data
)
224 struct gl_shader_program_data
*oldData
= *ptr
;
226 assert(oldData
->RefCount
> 0);
228 if (p_atomic_dec_zero(&oldData
->RefCount
)) {
230 ralloc_free(oldData
);
237 p_atomic_inc(&data
->RefCount
);
243 * Set ptr to point to shProg.
244 * If ptr is pointing to another object, decrement its refcount (and delete
245 * if refcount hits zero).
246 * Then set ptr to point to shProg, incrementing its refcount.
249 _mesa_reference_shader_program_(struct gl_context
*ctx
,
250 struct gl_shader_program
**ptr
,
251 struct gl_shader_program
*shProg
)
254 if (*ptr
== shProg
) {
259 /* Unreference the old shader program */
260 GLboolean deleteFlag
= GL_FALSE
;
261 struct gl_shader_program
*old
= *ptr
;
263 assert(old
->RefCount
> 0);
265 deleteFlag
= (old
->RefCount
== 0);
269 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
270 _mesa_delete_shader_program(ctx
, old
);
283 static struct gl_shader_program_data
*
284 create_shader_program_data()
286 struct gl_shader_program_data
*data
;
287 data
= rzalloc(NULL
, struct gl_shader_program_data
);
295 init_shader_program(struct gl_shader_program
*prog
)
297 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
300 prog
->AttributeBindings
= string_to_uint_map_ctor();
301 prog
->FragDataBindings
= string_to_uint_map_ctor();
302 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
304 prog
->Geom
.UsesEndPrimitive
= false;
305 prog
->Geom
.UsesStreams
= false;
307 prog
->Comp
.LocalSizeVariable
= false;
309 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
311 exec_list_make_empty(&prog
->EmptyUniformLocations
);
313 prog
->data
->InfoLog
= ralloc_strdup(prog
->data
, "");
317 * Allocate a new gl_shader_program object, initialize it.
319 struct gl_shader_program
*
320 _mesa_new_shader_program(GLuint name
)
322 struct gl_shader_program
*shProg
;
323 shProg
= rzalloc(NULL
, struct gl_shader_program
);
326 shProg
->data
= create_shader_program_data();
331 init_shader_program(shProg
);
338 * Clear (free) the shader program state that gets produced by linking.
341 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
342 struct gl_shader_program
*shProg
)
344 for (gl_shader_stage sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
345 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
346 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
347 shProg
->_LinkedShaders
[sh
] = NULL
;
351 shProg
->data
->linked_stages
= 0;
353 if (shProg
->data
->UniformStorage
) {
354 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; ++i
)
355 _mesa_uniform_detach_all_driver_storage(&shProg
->data
->
357 ralloc_free(shProg
->data
->UniformStorage
);
358 shProg
->data
->NumUniformStorage
= 0;
359 shProg
->data
->UniformStorage
= NULL
;
362 if (shProg
->UniformRemapTable
) {
363 ralloc_free(shProg
->UniformRemapTable
);
364 shProg
->NumUniformRemapTable
= 0;
365 shProg
->UniformRemapTable
= NULL
;
368 if (shProg
->UniformHash
) {
369 string_to_uint_map_dtor(shProg
->UniformHash
);
370 shProg
->UniformHash
= NULL
;
373 assert(shProg
->data
->InfoLog
!= NULL
);
374 ralloc_free(shProg
->data
->InfoLog
);
375 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, "");
377 ralloc_free(shProg
->data
->UniformBlocks
);
378 shProg
->data
->UniformBlocks
= NULL
;
379 shProg
->data
->NumUniformBlocks
= 0;
381 ralloc_free(shProg
->data
->ShaderStorageBlocks
);
382 shProg
->data
->ShaderStorageBlocks
= NULL
;
383 shProg
->data
->NumShaderStorageBlocks
= 0;
385 ralloc_free(shProg
->data
->AtomicBuffers
);
386 shProg
->data
->AtomicBuffers
= NULL
;
387 shProg
->data
->NumAtomicBuffers
= 0;
389 if (shProg
->ProgramResourceList
) {
390 ralloc_free(shProg
->ProgramResourceList
);
391 shProg
->ProgramResourceList
= NULL
;
392 shProg
->NumProgramResourceList
= 0;
398 * Free all the data that hangs off a shader program object, but not the
402 _mesa_free_shader_program_data(struct gl_context
*ctx
,
403 struct gl_shader_program
*shProg
)
407 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
409 _mesa_clear_shader_program_data(ctx
, shProg
);
411 if (shProg
->AttributeBindings
) {
412 string_to_uint_map_dtor(shProg
->AttributeBindings
);
413 shProg
->AttributeBindings
= NULL
;
416 if (shProg
->FragDataBindings
) {
417 string_to_uint_map_dtor(shProg
->FragDataBindings
);
418 shProg
->FragDataBindings
= NULL
;
421 if (shProg
->FragDataIndexBindings
) {
422 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
423 shProg
->FragDataIndexBindings
= NULL
;
427 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
428 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
430 shProg
->NumShaders
= 0;
432 free(shProg
->Shaders
);
433 shProg
->Shaders
= NULL
;
435 /* Transform feedback varying vars */
436 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
437 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
439 free(shProg
->TransformFeedback
.VaryingNames
);
440 shProg
->TransformFeedback
.VaryingNames
= NULL
;
441 shProg
->TransformFeedback
.NumVarying
= 0;
444 shProg
->Label
= NULL
;
449 * Free/delete a shader program object.
452 _mesa_delete_shader_program(struct gl_context
*ctx
,
453 struct gl_shader_program
*shProg
)
455 _mesa_free_shader_program_data(ctx
, shProg
);
456 _mesa_reference_shader_program_data(ctx
, &shProg
->data
, NULL
);
462 * Lookup a GLSL program object.
464 struct gl_shader_program
*
465 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
467 struct gl_shader_program
*shProg
;
469 shProg
= (struct gl_shader_program
*)
470 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
471 /* Note that both gl_shader and gl_shader_program objects are kept
472 * in the same hash table. Check the object's type to be sure it's
473 * what we're expecting.
475 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
485 * As above, but record an error if program is not found.
487 struct gl_shader_program
*
488 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
492 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
496 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
497 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
499 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
502 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
503 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
512 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
514 driver
->NewShader
= _mesa_new_linked_shader
;
515 driver
->LinkShader
= _mesa_ir_link_shader
;