2ca6544272630dda477182e5583a993de38a9365
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 struct gl_shader
*old
= *ptr
;
70 assert(old
->RefCount
> 0);
72 if (p_atomic_dec_zero(&old
->RefCount
)) {
74 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
75 _mesa_delete_shader(ctx
, old
);
84 p_atomic_inc(&sh
->RefCount
);
90 _mesa_init_shader(struct gl_shader
*shader
)
93 shader
->info
.Geom
.VerticesOut
= -1;
94 shader
->info
.Geom
.InputType
= GL_TRIANGLES
;
95 shader
->info
.Geom
.OutputType
= GL_TRIANGLE_STRIP
;
99 * Allocate a new gl_shader object, initialize it.
102 _mesa_new_shader(GLuint name
, gl_shader_stage stage
)
104 struct gl_shader
*shader
;
105 shader
= rzalloc(NULL
, struct gl_shader
);
107 shader
->Stage
= stage
;
110 shader
->SourceChecksum
= 0xa110c; /* alloc */
112 _mesa_init_shader(shader
);
119 * Delete a shader object.
122 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
124 free((void *)sh
->Source
);
125 free((void *)sh
->FallbackSource
);
132 * Delete a shader object.
135 _mesa_delete_linked_shader(struct gl_context
*ctx
,
136 struct gl_linked_shader
*sh
)
138 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
144 * Lookup a GLSL shader object.
147 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
150 struct gl_shader
*sh
= (struct gl_shader
*)
151 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
152 /* Note that both gl_shader and gl_shader_program objects are kept
153 * in the same hash table. Check the object's type to be sure it's
154 * what we're expecting.
156 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
166 * As above, but record an error if shader is not found.
169 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
172 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
176 struct gl_shader
*sh
= (struct gl_shader
*)
177 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
179 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
182 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
183 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
192 /**********************************************************************/
193 /*** Shader Program object functions ***/
194 /**********************************************************************/
198 _mesa_reference_shader_program_data(struct gl_context
*ctx
,
199 struct gl_shader_program_data
**ptr
,
200 struct gl_shader_program_data
*data
)
206 struct gl_shader_program_data
*oldData
= *ptr
;
208 assert(oldData
->RefCount
> 0);
210 if (p_atomic_dec_zero(&oldData
->RefCount
)) {
212 ralloc_free(oldData
);
219 p_atomic_inc(&data
->RefCount
);
225 * Set ptr to point to shProg.
226 * If ptr is pointing to another object, decrement its refcount (and delete
227 * if refcount hits zero).
228 * Then set ptr to point to shProg, incrementing its refcount.
231 _mesa_reference_shader_program_(struct gl_context
*ctx
,
232 struct gl_shader_program
**ptr
,
233 struct gl_shader_program
*shProg
)
236 if (*ptr
== shProg
) {
241 /* Unreference the old shader program */
242 struct gl_shader_program
*old
= *ptr
;
244 assert(old
->RefCount
> 0);
246 if (p_atomic_dec_zero(&old
->RefCount
)) {
248 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
249 _mesa_delete_shader_program(ctx
, old
);
257 p_atomic_inc(&shProg
->RefCount
);
262 static struct gl_shader_program_data
*
263 create_shader_program_data()
265 struct gl_shader_program_data
*data
;
266 data
= rzalloc(NULL
, struct gl_shader_program_data
);
274 init_shader_program(struct gl_shader_program
*prog
)
276 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
279 prog
->AttributeBindings
= string_to_uint_map_ctor();
280 prog
->FragDataBindings
= string_to_uint_map_ctor();
281 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
283 prog
->Geom
.UsesEndPrimitive
= false;
284 prog
->Geom
.UsesStreams
= false;
286 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
288 exec_list_make_empty(&prog
->EmptyUniformLocations
);
290 prog
->data
->InfoLog
= ralloc_strdup(prog
->data
, "");
294 * Allocate a new gl_shader_program object, initialize it.
296 struct gl_shader_program
*
297 _mesa_new_shader_program(GLuint name
)
299 struct gl_shader_program
*shProg
;
300 shProg
= rzalloc(NULL
, struct gl_shader_program
);
303 shProg
->data
= create_shader_program_data();
308 init_shader_program(shProg
);
315 * Clear (free) the shader program state that gets produced by linking.
318 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
319 struct gl_shader_program
*shProg
)
321 for (gl_shader_stage sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
322 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
323 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
324 shProg
->_LinkedShaders
[sh
] = NULL
;
328 shProg
->data
->linked_stages
= 0;
330 if (shProg
->data
->UniformStorage
) {
331 for (unsigned i
= 0; i
< shProg
->data
->NumUniformStorage
; ++i
)
332 _mesa_uniform_detach_all_driver_storage(&shProg
->data
->
334 ralloc_free(shProg
->data
->UniformStorage
);
335 shProg
->data
->NumUniformStorage
= 0;
336 shProg
->data
->UniformStorage
= NULL
;
339 if (shProg
->UniformRemapTable
) {
340 ralloc_free(shProg
->UniformRemapTable
);
341 shProg
->NumUniformRemapTable
= 0;
342 shProg
->UniformRemapTable
= NULL
;
345 if (shProg
->UniformHash
) {
346 string_to_uint_map_dtor(shProg
->UniformHash
);
347 shProg
->UniformHash
= NULL
;
350 assert(shProg
->data
->InfoLog
!= NULL
);
351 ralloc_free(shProg
->data
->InfoLog
);
352 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, "");
354 ralloc_free(shProg
->data
->UniformBlocks
);
355 shProg
->data
->UniformBlocks
= NULL
;
356 shProg
->data
->NumUniformBlocks
= 0;
358 ralloc_free(shProg
->data
->ShaderStorageBlocks
);
359 shProg
->data
->ShaderStorageBlocks
= NULL
;
360 shProg
->data
->NumShaderStorageBlocks
= 0;
362 ralloc_free(shProg
->data
->AtomicBuffers
);
363 shProg
->data
->AtomicBuffers
= NULL
;
364 shProg
->data
->NumAtomicBuffers
= 0;
366 if (shProg
->data
->ProgramResourceList
) {
367 ralloc_free(shProg
->data
->ProgramResourceList
);
368 shProg
->data
->ProgramResourceList
= NULL
;
369 shProg
->data
->NumProgramResourceList
= 0;
375 * Free all the data that hangs off a shader program object, but not the
379 _mesa_free_shader_program_data(struct gl_context
*ctx
,
380 struct gl_shader_program
*shProg
)
384 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
386 _mesa_clear_shader_program_data(ctx
, shProg
);
388 if (shProg
->AttributeBindings
) {
389 string_to_uint_map_dtor(shProg
->AttributeBindings
);
390 shProg
->AttributeBindings
= NULL
;
393 if (shProg
->FragDataBindings
) {
394 string_to_uint_map_dtor(shProg
->FragDataBindings
);
395 shProg
->FragDataBindings
= NULL
;
398 if (shProg
->FragDataIndexBindings
) {
399 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
400 shProg
->FragDataIndexBindings
= NULL
;
404 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
405 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
407 shProg
->NumShaders
= 0;
409 free(shProg
->Shaders
);
410 shProg
->Shaders
= NULL
;
412 /* Transform feedback varying vars */
413 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
414 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
416 free(shProg
->TransformFeedback
.VaryingNames
);
417 shProg
->TransformFeedback
.VaryingNames
= NULL
;
418 shProg
->TransformFeedback
.NumVarying
= 0;
421 shProg
->Label
= NULL
;
426 * Free/delete a shader program object.
429 _mesa_delete_shader_program(struct gl_context
*ctx
,
430 struct gl_shader_program
*shProg
)
432 _mesa_free_shader_program_data(ctx
, shProg
);
433 if (!shProg
->data
->cache_fallback
)
434 _mesa_reference_shader_program_data(ctx
, &shProg
->data
, NULL
);
440 * Lookup a GLSL program object.
442 struct gl_shader_program
*
443 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
445 struct gl_shader_program
*shProg
;
447 shProg
= (struct gl_shader_program
*)
448 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
449 /* Note that both gl_shader and gl_shader_program objects are kept
450 * in the same hash table. Check the object's type to be sure it's
451 * what we're expecting.
453 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
463 * As above, but record an error if program is not found.
465 struct gl_shader_program
*
466 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
470 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
474 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
475 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
477 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
480 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
481 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
490 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
492 driver
->LinkShader
= _mesa_ir_link_shader
;