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 "compiler/glsl/string_to_uint_map.h"
34 #include "main/glheader.h"
35 #include "main/context.h"
36 #include "main/glspirv.h"
37 #include "main/hash.h"
38 #include "main/mtypes.h"
39 #include "main/shaderapi.h"
40 #include "main/shaderobj.h"
41 #include "main/uniforms.h"
42 #include "program/program.h"
43 #include "program/prog_parameter.h"
44 #include "util/ralloc.h"
45 #include "util/u_atomic.h"
47 /**********************************************************************/
48 /*** Shader object functions ***/
49 /**********************************************************************/
53 * Set ptr to point to sh.
54 * If ptr is pointing to another shader, decrement its refcount (and delete
55 * if refcount hits zero).
56 * Then set ptr to point to sh, incrementing its refcount.
59 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
68 /* Unreference the old shader */
69 struct gl_shader
*old
= *ptr
;
71 assert(old
->RefCount
> 0);
73 if (p_atomic_dec_zero(&old
->RefCount
)) {
75 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
76 _mesa_delete_shader(ctx
, old
);
85 p_atomic_inc(&sh
->RefCount
);
91 _mesa_init_shader(struct gl_shader
*shader
)
94 shader
->info
.Geom
.VerticesOut
= -1;
95 shader
->info
.Geom
.InputType
= GL_TRIANGLES
;
96 shader
->info
.Geom
.OutputType
= GL_TRIANGLE_STRIP
;
100 * Allocate a new gl_shader object, initialize it.
103 _mesa_new_shader(GLuint name
, gl_shader_stage stage
)
105 struct gl_shader
*shader
;
106 shader
= rzalloc(NULL
, struct gl_shader
);
108 shader
->Stage
= stage
;
111 shader
->SourceChecksum
= 0xa110c; /* alloc */
113 _mesa_init_shader(shader
);
120 * Delete a shader object.
123 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
125 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
126 free((void *)sh
->Source
);
127 free((void *)sh
->FallbackSource
);
134 * Delete a shader object.
137 _mesa_delete_linked_shader(struct gl_context
*ctx
,
138 struct gl_linked_shader
*sh
)
140 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
146 * Lookup a GLSL shader object.
149 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
152 struct gl_shader
*sh
= (struct gl_shader
*)
153 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
154 /* Note that both gl_shader and gl_shader_program objects are kept
155 * in the same hash table. Check the object's type to be sure it's
156 * what we're expecting.
158 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
168 * As above, but record an error if shader is not found.
171 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
174 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
178 struct gl_shader
*sh
= (struct gl_shader
*)
179 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
181 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
184 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
185 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
194 /**********************************************************************/
195 /*** Shader Program object functions ***/
196 /**********************************************************************/
199 _mesa_reference_shader_program_data(struct gl_context
*ctx
,
200 struct gl_shader_program_data
**ptr
,
201 struct gl_shader_program_data
*data
)
207 struct gl_shader_program_data
*oldData
= *ptr
;
209 assert(oldData
->RefCount
> 0);
211 if (p_atomic_dec_zero(&oldData
->RefCount
)) {
213 assert(oldData
->NumUniformStorage
== 0 ||
214 oldData
->UniformStorage
);
216 for (unsigned i
= 0; i
< oldData
->NumUniformStorage
; ++i
)
217 _mesa_uniform_detach_all_driver_storage(&oldData
->UniformStorage
[i
]);
219 ralloc_free(oldData
);
226 p_atomic_inc(&data
->RefCount
);
232 * Set ptr to point to shProg.
233 * If ptr is pointing to another object, decrement its refcount (and delete
234 * if refcount hits zero).
235 * Then set ptr to point to shProg, incrementing its refcount.
238 _mesa_reference_shader_program_(struct gl_context
*ctx
,
239 struct gl_shader_program
**ptr
,
240 struct gl_shader_program
*shProg
)
243 if (*ptr
== shProg
) {
248 /* Unreference the old shader program */
249 struct gl_shader_program
*old
= *ptr
;
251 assert(old
->RefCount
> 0);
253 if (p_atomic_dec_zero(&old
->RefCount
)) {
255 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
256 _mesa_delete_shader_program(ctx
, old
);
264 p_atomic_inc(&shProg
->RefCount
);
269 struct gl_shader_program_data
*
270 _mesa_create_shader_program_data()
272 struct gl_shader_program_data
*data
;
273 data
= rzalloc(NULL
, struct gl_shader_program_data
);
276 data
->InfoLog
= ralloc_strdup(data
, "");
283 init_shader_program(struct gl_shader_program
*prog
)
285 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
288 prog
->AttributeBindings
= string_to_uint_map_ctor();
289 prog
->FragDataBindings
= string_to_uint_map_ctor();
290 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
292 prog
->Geom
.UsesEndPrimitive
= false;
293 prog
->Geom
.UsesStreams
= false;
295 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
297 exec_list_make_empty(&prog
->EmptyUniformLocations
);
301 * Allocate a new gl_shader_program object, initialize it.
303 struct gl_shader_program
*
304 _mesa_new_shader_program(GLuint name
)
306 struct gl_shader_program
*shProg
;
307 shProg
= rzalloc(NULL
, struct gl_shader_program
);
310 shProg
->data
= _mesa_create_shader_program_data();
315 init_shader_program(shProg
);
322 * Clear (free) the shader program state that gets produced by linking.
325 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
326 struct gl_shader_program
*shProg
)
328 for (gl_shader_stage sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
329 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
330 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
331 shProg
->_LinkedShaders
[sh
] = NULL
;
335 if (shProg
->UniformRemapTable
) {
336 ralloc_free(shProg
->UniformRemapTable
);
337 shProg
->NumUniformRemapTable
= 0;
338 shProg
->UniformRemapTable
= NULL
;
341 if (shProg
->UniformHash
) {
342 string_to_uint_map_dtor(shProg
->UniformHash
);
343 shProg
->UniformHash
= NULL
;
346 _mesa_reference_shader_program_data(ctx
, &shProg
->data
, NULL
);
351 * Free all the data that hangs off a shader program object, but not the
355 _mesa_free_shader_program_data(struct gl_context
*ctx
,
356 struct gl_shader_program
*shProg
)
360 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
362 _mesa_clear_shader_program_data(ctx
, shProg
);
364 if (shProg
->AttributeBindings
) {
365 string_to_uint_map_dtor(shProg
->AttributeBindings
);
366 shProg
->AttributeBindings
= NULL
;
369 if (shProg
->FragDataBindings
) {
370 string_to_uint_map_dtor(shProg
->FragDataBindings
);
371 shProg
->FragDataBindings
= NULL
;
374 if (shProg
->FragDataIndexBindings
) {
375 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
376 shProg
->FragDataIndexBindings
= NULL
;
380 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
381 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
383 shProg
->NumShaders
= 0;
385 free(shProg
->Shaders
);
386 shProg
->Shaders
= NULL
;
388 /* Transform feedback varying vars */
389 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
390 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
392 free(shProg
->TransformFeedback
.VaryingNames
);
393 shProg
->TransformFeedback
.VaryingNames
= NULL
;
394 shProg
->TransformFeedback
.NumVarying
= 0;
397 shProg
->Label
= NULL
;
402 * Free/delete a shader program object.
405 _mesa_delete_shader_program(struct gl_context
*ctx
,
406 struct gl_shader_program
*shProg
)
408 _mesa_free_shader_program_data(ctx
, shProg
);
414 * Lookup a GLSL program object.
416 struct gl_shader_program
*
417 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
419 struct gl_shader_program
*shProg
;
421 shProg
= (struct gl_shader_program
*)
422 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
423 /* Note that both gl_shader and gl_shader_program objects are kept
424 * in the same hash table. Check the object's type to be sure it's
425 * what we're expecting.
427 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
437 * As above, but record an error if program is not found.
439 struct gl_shader_program
*
440 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
444 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
448 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
449 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
451 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
454 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
455 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
464 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
466 driver
->LinkShader
= _mesa_ir_link_shader
;