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/shaderobj.h"
38 #include "main/uniforms.h"
39 #include "program/program.h"
40 #include "program/prog_parameter.h"
41 #include "program/hash_table.h"
44 /**********************************************************************/
45 /*** Shader object functions ***/
46 /**********************************************************************/
50 * Set ptr to point to sh.
51 * If ptr is pointing to another shader, decrement its refcount (and delete
52 * if refcount hits zero).
53 * Then set ptr to point to sh, incrementing its refcount.
56 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
65 /* Unreference the old shader */
66 GLboolean deleteFlag
= GL_FALSE
;
67 struct gl_shader
*old
= *ptr
;
69 ASSERT(old
->RefCount
> 0);
71 /*printf("SHADER DECR %p (%d) to %d\n",
72 (void*) old, old->Name, old->RefCount);*/
73 deleteFlag
= (old
->RefCount
== 0);
77 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
78 ctx
->Driver
.DeleteShader(ctx
, old
);
88 /*printf("SHADER INCR %p (%d) to %d\n",
89 (void*) sh, sh->Name, sh->RefCount);*/
95 _mesa_init_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
101 * Allocate a new gl_shader object, initialize it.
102 * Called via ctx->Driver.NewShader()
105 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
)
107 struct gl_shader
*shader
;
108 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
109 type
== GL_GEOMETRY_SHADER_ARB
);
110 shader
= rzalloc(NULL
, struct gl_shader
);
114 _mesa_init_shader(ctx
, shader
);
121 * Delete a shader object.
122 * Called via ctx->Driver.DeleteShader().
125 _mesa_delete_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
127 free((void *)sh
->Source
);
129 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
135 * Lookup a GLSL shader object.
138 _mesa_lookup_shader(struct gl_context
*ctx
, GLuint name
)
141 struct gl_shader
*sh
= (struct gl_shader
*)
142 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
143 /* Note that both gl_shader and gl_shader_program objects are kept
144 * in the same hash table. Check the object's type to be sure it's
145 * what we're expecting.
147 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
157 * As above, but record an error if shader is not found.
160 _mesa_lookup_shader_err(struct gl_context
*ctx
, GLuint name
, const char *caller
)
163 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
167 struct gl_shader
*sh
= (struct gl_shader
*)
168 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
170 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
173 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
183 /**********************************************************************/
184 /*** Shader Program object functions ***/
185 /**********************************************************************/
189 * Set ptr to point to shProg.
190 * If ptr is pointing to another object, decrement its refcount (and delete
191 * if refcount hits zero).
192 * Then set ptr to point to shProg, incrementing its refcount.
195 _mesa_reference_shader_program(struct gl_context
*ctx
,
196 struct gl_shader_program
**ptr
,
197 struct gl_shader_program
*shProg
)
200 if (*ptr
== shProg
) {
205 /* Unreference the old shader program */
206 GLboolean deleteFlag
= GL_FALSE
;
207 struct gl_shader_program
*old
= *ptr
;
209 ASSERT(old
->RefCount
> 0);
212 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
213 (void *) old
, old
->Name
, old
->RefCount
);
215 deleteFlag
= (old
->RefCount
== 0);
219 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
220 ctx
->Driver
.DeleteShaderProgram(ctx
, old
);
230 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
231 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
238 _mesa_init_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
240 prog
->Type
= GL_SHADER_PROGRAM_MESA
;
243 prog
->AttributeBindings
= string_to_uint_map_ctor();
244 prog
->FragDataBindings
= string_to_uint_map_ctor();
245 prog
->FragDataIndexBindings
= string_to_uint_map_ctor();
247 prog
->Geom
.VerticesOut
= 0;
248 prog
->Geom
.InputType
= GL_TRIANGLES
;
249 prog
->Geom
.OutputType
= GL_TRIANGLE_STRIP
;
251 prog
->TransformFeedback
.BufferMode
= GL_INTERLEAVED_ATTRIBS
;
253 prog
->InfoLog
= ralloc_strdup(prog
, "");
257 * Allocate a new gl_shader_program object, initialize it.
258 * Called via ctx->Driver.NewShaderProgram()
260 static struct gl_shader_program
*
261 _mesa_new_shader_program(struct gl_context
*ctx
, GLuint name
)
263 struct gl_shader_program
*shProg
;
264 shProg
= rzalloc(NULL
, struct gl_shader_program
);
267 _mesa_init_shader_program(ctx
, shProg
);
274 * Clear (free) the shader program state that gets produced by linking.
277 _mesa_clear_shader_program_data(struct gl_context
*ctx
,
278 struct gl_shader_program
*shProg
)
280 if (shProg
->UniformStorage
) {
282 for (i
= 0; i
< shProg
->NumUserUniformStorage
; ++i
)
283 _mesa_uniform_detach_all_driver_storage(&shProg
->UniformStorage
[i
]);
284 ralloc_free(shProg
->UniformStorage
);
285 shProg
->NumUserUniformStorage
= 0;
286 shProg
->UniformStorage
= NULL
;
287 shProg
->UniformLocationBaseScale
= 0;
290 if (shProg
->UniformHash
) {
291 string_to_uint_map_dtor(shProg
->UniformHash
);
292 shProg
->UniformHash
= NULL
;
295 assert(shProg
->InfoLog
!= NULL
);
296 ralloc_free(shProg
->InfoLog
);
297 shProg
->InfoLog
= ralloc_strdup(shProg
, "");
302 * Free all the data that hangs off a shader program object, but not the
306 _mesa_free_shader_program_data(struct gl_context
*ctx
,
307 struct gl_shader_program
*shProg
)
312 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
314 _mesa_clear_shader_program_data(ctx
, shProg
);
316 if (shProg
->AttributeBindings
) {
317 string_to_uint_map_dtor(shProg
->AttributeBindings
);
318 shProg
->AttributeBindings
= NULL
;
321 if (shProg
->FragDataBindings
) {
322 string_to_uint_map_dtor(shProg
->FragDataBindings
);
323 shProg
->FragDataBindings
= NULL
;
326 if (shProg
->FragDataIndexBindings
) {
327 string_to_uint_map_dtor(shProg
->FragDataIndexBindings
);
328 shProg
->FragDataIndexBindings
= NULL
;
332 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
333 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
335 shProg
->NumShaders
= 0;
337 free(shProg
->Shaders
);
338 shProg
->Shaders
= NULL
;
340 /* Transform feedback varying vars */
341 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
342 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
344 free(shProg
->TransformFeedback
.VaryingNames
);
345 shProg
->TransformFeedback
.VaryingNames
= NULL
;
346 shProg
->TransformFeedback
.NumVarying
= 0;
349 for (sh
= 0; sh
< MESA_SHADER_TYPES
; sh
++) {
350 if (shProg
->_LinkedShaders
[sh
] != NULL
) {
351 ctx
->Driver
.DeleteShader(ctx
, shProg
->_LinkedShaders
[sh
]);
352 shProg
->_LinkedShaders
[sh
] = NULL
;
361 * Free/delete a shader program object.
362 * Called via ctx->Driver.DeleteShaderProgram().
365 _mesa_delete_shader_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
367 _mesa_free_shader_program_data(ctx
, shProg
);
374 * Lookup a GLSL program object.
376 struct gl_shader_program
*
377 _mesa_lookup_shader_program(struct gl_context
*ctx
, GLuint name
)
379 struct gl_shader_program
*shProg
;
381 shProg
= (struct gl_shader_program
*)
382 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
383 /* Note that both gl_shader and gl_shader_program objects are kept
384 * in the same hash table. Check the object's type to be sure it's
385 * what we're expecting.
387 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
397 * As above, but record an error if program is not found.
399 struct gl_shader_program
*
400 _mesa_lookup_shader_program_err(struct gl_context
*ctx
, GLuint name
,
404 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
408 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
409 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
411 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
414 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
415 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
424 _mesa_init_shader_object_functions(struct dd_function_table
*driver
)
426 driver
->NewShader
= _mesa_new_shader
;
427 driver
->DeleteShader
= _mesa_delete_shader
;
428 driver
->NewShaderProgram
= _mesa_new_shader_program
;
429 driver
->DeleteShaderProgram
= _mesa_delete_shader_program
;
430 driver
->LinkShader
= _mesa_ir_link_shader
;