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_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
141 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
147 * Lookup a GLSL shader object.
150 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
153 struct gl_shader
*sh
= (struct gl_shader
*)
154 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
155 /* Note that both gl_shader and gl_shader_program objects are kept
156 * in the same hash table. Check the object's type to be sure it's
157 * what we're expecting.
159 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
169 * As above, but record an error if shader is not found.
172 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
175 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
179 struct gl_shader
*sh
= (struct gl_shader
*)
180 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
182 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
185 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
186 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
195 /**********************************************************************/
196 /*** Shader Program object functions ***/
197 /**********************************************************************/
200 _mesa_reference_shader_program_data(struct gl_context
*ctx
,
201 struct gl_shader_program_data
**ptr
,
202 struct gl_shader_program_data
*data
)
208 struct gl_shader_program_data
*oldData
= *ptr
;
210 assert(oldData
->RefCount
> 0);
212 if (p_atomic_dec_zero(&oldData
->RefCount
)) {
214 assert(oldData
->NumUniformStorage
== 0 ||
215 oldData
->UniformStorage
);
217 for (unsigned i
= 0; i
< oldData
->NumUniformStorage
; ++i
)
218 _mesa_uniform_detach_all_driver_storage(&oldData
->UniformStorage
[i
]);
220 ralloc_free(oldData
);
227 p_atomic_inc(&data
->RefCount
);
233 * Set ptr to point to shProg.
234 * If ptr is pointing to another object, decrement its refcount (and delete
235 * if refcount hits zero).
236 * Then set ptr to point to shProg, incrementing its refcount.
239 _mesa_reference_shader_program_(struct gl_context
*ctx
,
240 struct gl_shader_program
**ptr
,
241 struct gl_shader_program
*shProg
)
244 if (*ptr
== shProg
) {
249 /* Unreference the old shader program */
250 struct gl_shader_program
*old
= *ptr
;
252 assert(old
->RefCount
> 0);
254 if (p_atomic_dec_zero(&old
->RefCount
)) {
256 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
257 _mesa_delete_shader_program(ctx
, old
);
265 p_atomic_inc(&shProg
->RefCount
);
270 struct gl_shader_program_data
*
271 _mesa_create_shader_program_data()
273 struct gl_shader_program_data
*data
;
274 data
= rzalloc(NULL
, struct gl_shader_program_data
);
277 data
->InfoLog
= ralloc_strdup(data
, "");
284 init_shader_program(struct gl_shader_program
*prog
)
286 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
289 prog
->AttributeBindings
= string_to_uint_map_ctor();
290 prog
->FragDataBindings
= string_to_uint_map_ctor();
291 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
293 prog
->Geom
.UsesEndPrimitive
= false;
294 prog
->Geom
.UsesStreams
= false;
296 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
298 exec_list_make_empty(&prog
->EmptyUniformLocations
);
302 * Allocate a new gl_shader_program object, initialize it.
304 struct gl_shader_program
*
305 _mesa_new_shader_program(GLuint name
)
307 struct gl_shader_program
*shProg
;
308 shProg
= rzalloc(NULL
, struct gl_shader_program
);
311 shProg
->data
= _mesa_create_shader_program_data();
316 init_shader_program(shProg
);
323 * Clear (free) the shader program state that gets produced by linking.
326 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
327 struct gl_shader_program
*shProg
)
329 for (gl_shader_stage sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
330 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
331 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
332 shProg
->_LinkedShaders
[sh
] = NULL
;
336 if (shProg
->UniformRemapTable
) {
337 ralloc_free(shProg
->UniformRemapTable
);
338 shProg
->NumUniformRemapTable
= 0;
339 shProg
->UniformRemapTable
= NULL
;
342 if (shProg
->UniformHash
) {
343 string_to_uint_map_dtor(shProg
->UniformHash
);
344 shProg
->UniformHash
= NULL
;
347 if (shProg
->data
&& shProg
->data
->ProgramResourceHash
) {
348 _mesa_hash_table_u64_destroy(shProg
->data
->ProgramResourceHash
, NULL
);
349 shProg
->data
->ProgramResourceHash
= NULL
;
352 _mesa_reference_shader_program_data(ctx
, &shProg
->data
, NULL
);
357 * Free all the data that hangs off a shader program object, but not the
361 _mesa_free_shader_program_data(struct gl_context
*ctx
,
362 struct gl_shader_program
*shProg
)
366 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
368 _mesa_clear_shader_program_data(ctx
, shProg
);
370 if (shProg
->AttributeBindings
) {
371 string_to_uint_map_dtor(shProg
->AttributeBindings
);
372 shProg
->AttributeBindings
= NULL
;
375 if (shProg
->FragDataBindings
) {
376 string_to_uint_map_dtor(shProg
->FragDataBindings
);
377 shProg
->FragDataBindings
= NULL
;
380 if (shProg
->FragDataIndexBindings
) {
381 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
382 shProg
->FragDataIndexBindings
= NULL
;
386 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
387 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
389 shProg
->NumShaders
= 0;
391 free(shProg
->Shaders
);
392 shProg
->Shaders
= NULL
;
394 /* Transform feedback varying vars */
395 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
396 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
398 free(shProg
->TransformFeedback
.VaryingNames
);
399 shProg
->TransformFeedback
.VaryingNames
= NULL
;
400 shProg
->TransformFeedback
.NumVarying
= 0;
403 shProg
->Label
= NULL
;
408 * Free/delete a shader program object.
411 _mesa_delete_shader_program(struct gl_context
*ctx
,
412 struct gl_shader_program
*shProg
)
414 _mesa_free_shader_program_data(ctx
, shProg
);
420 * Lookup a GLSL program object.
422 struct gl_shader_program
*
423 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
425 struct gl_shader_program
*shProg
;
427 shProg
= (struct gl_shader_program
*)
428 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
429 /* Note that both gl_shader and gl_shader_program objects are kept
430 * in the same hash table. Check the object's type to be sure it's
431 * what we're expecting.
433 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
443 * As above, but record an error if program is not found.
445 struct gl_shader_program
*
446 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
450 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
454 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
455 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
457 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
460 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
461 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
470 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
472 driver
->LinkShader
= _mesa_ir_link_shader
;