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 "program/hash_table.h"
43 #include "util/ralloc.h"
45 /**********************************************************************/
46 /*** Shader object functions ***/
47 /**********************************************************************/
51 * Set ptr to point to sh.
52 * If ptr is pointing to another shader, decrement its refcount (and delete
53 * if refcount hits zero).
54 * Then set ptr to point to sh, incrementing its refcount.
57 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
66 /* Unreference the old shader */
67 GLboolean deleteFlag
= GL_FALSE
;
68 struct gl_shader
*old
= *ptr
;
70 assert(old
->RefCount
> 0);
72 deleteFlag
= (old
->RefCount
== 0);
76 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
77 _mesa_delete_shader(ctx
, old
);
92 _mesa_init_shader(struct gl_shader
*shader
)
95 shader
->Geom
.VerticesOut
= -1;
96 shader
->Geom
.InputType
= GL_TRIANGLES
;
97 shader
->Geom
.OutputType
= GL_TRIANGLE_STRIP
;
101 * Allocate a new gl_shader object, initialize it.
104 _mesa_new_shader(GLuint name
, gl_shader_stage stage
)
106 struct gl_shader
*shader
;
107 shader
= rzalloc(NULL
, struct gl_shader
);
109 shader
->Stage
= stage
;
111 _mesa_init_shader(shader
);
118 * Allocate a new gl_linked_shader object.
119 * Called via ctx->Driver.NewShader()
121 struct gl_linked_shader
*
122 _mesa_new_linked_shader(gl_shader_stage stage
)
124 struct gl_linked_shader
*shader
;
125 shader
= rzalloc(NULL
, struct gl_linked_shader
);
127 shader
->Stage
= stage
;
134 * Delete a shader object.
137 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
139 free((void *)sh
->Source
);
146 * Delete a shader object.
149 _mesa_delete_linked_shader(struct gl_context
*ctx
,
150 struct gl_linked_shader
*sh
)
152 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
158 * Lookup a GLSL shader object.
161 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
164 struct gl_shader
*sh
= (struct gl_shader
*)
165 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
166 /* Note that both gl_shader and gl_shader_program objects are kept
167 * in the same hash table. Check the object's type to be sure it's
168 * what we're expecting.
170 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
180 * As above, but record an error if shader is not found.
183 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
186 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
190 struct gl_shader
*sh
= (struct gl_shader
*)
191 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
193 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
196 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
197 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
206 /**********************************************************************/
207 /*** Shader Program object functions ***/
208 /**********************************************************************/
212 * Set ptr to point to shProg.
213 * If ptr is pointing to another object, decrement its refcount (and delete
214 * if refcount hits zero).
215 * Then set ptr to point to shProg, incrementing its refcount.
218 _mesa_reference_shader_program_(struct gl_context
*ctx
,
219 struct gl_shader_program
**ptr
,
220 struct gl_shader_program
*shProg
)
223 if (*ptr
== shProg
) {
228 /* Unreference the old shader program */
229 GLboolean deleteFlag
= GL_FALSE
;
230 struct gl_shader_program
*old
= *ptr
;
232 assert(old
->RefCount
> 0);
234 deleteFlag
= (old
->RefCount
== 0);
238 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
239 _mesa_delete_shader_program(ctx
, old
);
253 init_shader_program(struct gl_shader_program
*prog
)
255 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
258 prog
->AttributeBindings
= string_to_uint_map_ctor();
259 prog
->FragDataBindings
= string_to_uint_map_ctor();
260 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
262 prog
->Geom
.UsesEndPrimitive
= false;
263 prog
->Geom
.UsesStreams
= false;
265 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
267 exec_list_make_empty(&prog
->EmptyUniformLocations
);
269 prog
->InfoLog
= ralloc_strdup(prog
, "");
273 * Allocate a new gl_shader_program object, initialize it.
275 struct gl_shader_program
*
276 _mesa_new_shader_program(GLuint name
)
278 struct gl_shader_program
*shProg
;
279 shProg
= rzalloc(NULL
, struct gl_shader_program
);
282 init_shader_program(shProg
);
289 * Clear (free) the shader program state that gets produced by linking.
292 _mesa_clear_shader_program_data(struct gl_shader_program
*shProg
)
296 if (shProg
->UniformStorage
) {
297 for (i
= 0; i
< shProg
->NumUniformStorage
; ++i
)
298 _mesa_uniform_detach_all_driver_storage(&shProg
->UniformStorage
[i
]);
299 ralloc_free(shProg
->UniformStorage
);
300 shProg
->NumUniformStorage
= 0;
301 shProg
->UniformStorage
= NULL
;
304 if (shProg
->UniformRemapTable
) {
305 ralloc_free(shProg
->UniformRemapTable
);
306 shProg
->NumUniformRemapTable
= 0;
307 shProg
->UniformRemapTable
= NULL
;
310 if (shProg
->UniformHash
) {
311 string_to_uint_map_dtor(shProg
->UniformHash
);
312 shProg
->UniformHash
= NULL
;
315 assert(shProg
->InfoLog
!= NULL
);
316 ralloc_free(shProg
->InfoLog
);
317 shProg
->InfoLog
= ralloc_strdup(shProg
, "");
319 ralloc_free(shProg
->UniformBlocks
);
320 shProg
->UniformBlocks
= NULL
;
321 shProg
->NumUniformBlocks
= 0;
323 ralloc_free(shProg
->ShaderStorageBlocks
);
324 shProg
->ShaderStorageBlocks
= NULL
;
325 shProg
->NumShaderStorageBlocks
= 0;
327 ralloc_free(shProg
->AtomicBuffers
);
328 shProg
->AtomicBuffers
= NULL
;
329 shProg
->NumAtomicBuffers
= 0;
331 if (shProg
->ProgramResourceList
) {
332 ralloc_free(shProg
->ProgramResourceList
);
333 shProg
->ProgramResourceList
= NULL
;
334 shProg
->NumProgramResourceList
= 0;
340 * Free all the data that hangs off a shader program object, but not the
344 _mesa_free_shader_program_data(struct gl_context
*ctx
,
345 struct gl_shader_program
*shProg
)
350 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
352 _mesa_clear_shader_program_data(shProg
);
354 if (shProg
->AttributeBindings
) {
355 string_to_uint_map_dtor(shProg
->AttributeBindings
);
356 shProg
->AttributeBindings
= NULL
;
359 if (shProg
->FragDataBindings
) {
360 string_to_uint_map_dtor(shProg
->FragDataBindings
);
361 shProg
->FragDataBindings
= NULL
;
364 if (shProg
->FragDataIndexBindings
) {
365 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
366 shProg
->FragDataIndexBindings
= NULL
;
370 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
371 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
373 shProg
->NumShaders
= 0;
375 free(shProg
->Shaders
);
376 shProg
->Shaders
= NULL
;
378 /* Transform feedback varying vars */
379 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
380 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
382 free(shProg
->TransformFeedback
.VaryingNames
);
383 shProg
->TransformFeedback
.VaryingNames
= NULL
;
384 shProg
->TransformFeedback
.NumVarying
= 0;
387 for (sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
388 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
389 _mesa_delete_linked_shader(ctx
, shProg
->_LinkedShaders
[sh
]);
390 shProg
->_LinkedShaders
[sh
] = NULL
;
395 shProg
->Label
= NULL
;
400 * Free/delete a shader program object.
403 _mesa_delete_shader_program(struct gl_context
*ctx
,
404 struct gl_shader_program
*shProg
)
406 _mesa_free_shader_program_data(ctx
, shProg
);
413 * Lookup a GLSL program object.
415 struct gl_shader_program
*
416 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
418 struct gl_shader_program
*shProg
;
420 shProg
= (struct gl_shader_program
*)
421 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
422 /* Note that both gl_shader and gl_shader_program objects are kept
423 * in the same hash table. Check the object's type to be sure it's
424 * what we're expecting.
426 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
436 * As above, but record an error if program is not found.
438 struct gl_shader_program
*
439 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
443 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
447 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
448 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
450 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
453 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
454 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
463 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
465 driver
->NewShader
= _mesa_new_linked_shader
;
466 driver
->LinkShader
= _mesa_ir_link_shader
;