2 * Mesa 3-D graphics library
5 * Copyright (C) 2004-2007 Brian Paul 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Implementation of GLSL-related API functions
33 * 1. Check that the right error code is generated for all _mesa_error() calls.
34 * 2. Insert FLUSH_VERTICES calls in various places
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_statevars.h"
46 #include "shader/shader_api.h"
47 #include "shader/slang/slang_compile.h"
48 #include "shader/slang/slang_link.h"
53 * Allocate a new gl_shader_program object, initialize it.
55 struct gl_shader_program
*
56 _mesa_new_shader_program(GLcontext
*ctx
, GLuint name
)
58 struct gl_shader_program
*shProg
;
59 shProg
= CALLOC_STRUCT(gl_shader_program
);
61 shProg
->Type
= GL_SHADER_PROGRAM_MESA
;
64 shProg
->Attributes
= _mesa_new_parameter_list();
71 * Clear (free) the shader program state that gets produced by linking.
74 _mesa_clear_shader_program_data(GLcontext
*ctx
,
75 struct gl_shader_program
*shProg
)
77 if (shProg
->VertexProgram
) {
78 if (shProg
->VertexProgram
->Base
.Parameters
== shProg
->Uniforms
) {
79 /* to prevent a double-free in the next call */
80 shProg
->VertexProgram
->Base
.Parameters
= NULL
;
82 _mesa_delete_program(ctx
, &shProg
->VertexProgram
->Base
);
83 shProg
->VertexProgram
= NULL
;
86 if (shProg
->FragmentProgram
) {
87 if (shProg
->FragmentProgram
->Base
.Parameters
== shProg
->Uniforms
) {
88 /* to prevent a double-free in the next call */
89 shProg
->FragmentProgram
->Base
.Parameters
= NULL
;
91 _mesa_delete_program(ctx
, &shProg
->FragmentProgram
->Base
);
92 shProg
->FragmentProgram
= NULL
;
95 if (shProg
->Uniforms
) {
96 _mesa_free_parameter_list(shProg
->Uniforms
);
97 shProg
->Uniforms
= NULL
;
100 if (shProg
->Varying
) {
101 _mesa_free_parameter_list(shProg
->Varying
);
102 shProg
->Varying
= NULL
;
108 * Free all the data that hangs off a shader program object, but not the
112 _mesa_free_shader_program_data(GLcontext
*ctx
,
113 struct gl_shader_program
*shProg
)
117 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
119 _mesa_clear_shader_program_data(ctx
, shProg
);
121 if (shProg
->Attributes
) {
122 _mesa_free_parameter_list(shProg
->Attributes
);
123 shProg
->Attributes
= NULL
;
127 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
128 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
130 if (shProg
->Shaders
) {
131 _mesa_free(shProg
->Shaders
);
132 shProg
->Shaders
= NULL
;
138 * Free/delete a shader program object.
141 _mesa_free_shader_program(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
143 _mesa_free_shader_program_data(ctx
, shProg
);
144 if (shProg
->Shaders
) {
145 _mesa_free(shProg
->Shaders
);
146 shProg
->Shaders
= NULL
;
153 * Set ptr to point to shProg.
154 * If ptr is pointing to another object, decrement its refcount (and delete
155 * if refcount hits zero).
156 * Then set ptr to point to shProg, incrementing its refcount.
158 /* XXX this could be static */
160 _mesa_reference_shader_program(GLcontext
*ctx
,
161 struct gl_shader_program
**ptr
,
162 struct gl_shader_program
*shProg
)
165 if (*ptr
== shProg
) {
170 /* Unreference the old shader program */
171 GLboolean deleteFlag
= GL_FALSE
;
172 struct gl_shader_program
*old
= *ptr
;
174 ASSERT(old
->RefCount
> 0);
176 /*printf("SHPROG DECR %p (%d) to %d\n",
177 (void*) old, old->Name, old->RefCount);*/
178 deleteFlag
= (old
->RefCount
== 0);
181 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
182 _mesa_free_shader_program(ctx
, old
);
191 /*printf("SHPROG INCR %p (%d) to %d\n",
192 (void*) shProg, shProg->Name, shProg->RefCount);*/
199 * Lookup a GLSL program object.
201 struct gl_shader_program
*
202 _mesa_lookup_shader_program(GLcontext
*ctx
, GLuint name
)
204 struct gl_shader_program
*shProg
;
206 shProg
= (struct gl_shader_program
*)
207 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
208 /* Note that both gl_shader and gl_shader_program objects are kept
209 * in the same hash table. Check the object's type to be sure it's
210 * what we're expecting.
212 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
222 * Allocate a new gl_shader object, initialize it.
225 _mesa_new_shader(GLcontext
*ctx
, GLuint name
, GLenum type
)
227 struct gl_shader
*shader
;
228 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
);
229 shader
= CALLOC_STRUCT(gl_shader
);
233 shader
->RefCount
= 1;
240 _mesa_free_shader(GLcontext
*ctx
, struct gl_shader
*sh
)
244 _mesa_free((void *) sh
->Source
);
246 _mesa_free(sh
->InfoLog
);
247 for (i
= 0; i
< sh
->NumPrograms
; i
++) {
248 assert(sh
->Programs
[i
]);
249 _mesa_delete_program(ctx
, sh
->Programs
[i
]);
252 _mesa_free(sh
->Programs
);
258 * Set ptr to point to sh.
259 * If ptr is pointing to another shader, decrement its refcount (and delete
260 * if refcount hits zero).
261 * Then set ptr to point to sh, incrementing its refcount.
263 /* XXX this could be static */
265 _mesa_reference_shader(GLcontext
*ctx
, struct gl_shader
**ptr
,
266 struct gl_shader
*sh
)
274 /* Unreference the old shader */
275 GLboolean deleteFlag
= GL_FALSE
;
276 struct gl_shader
*old
= *ptr
;
278 ASSERT(old
->RefCount
> 0);
280 /*printf("SHADER DECR %p (%d) to %d\n",
281 (void*) old, old->Name, old->RefCount);*/
282 deleteFlag
= (old
->RefCount
== 0);
285 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
286 _mesa_free_shader(ctx
, old
);
296 /*printf("SHADER INCR %p (%d) to %d\n",
297 (void*) sh, sh->Name, sh->RefCount);*/
304 * Lookup a GLSL shader object.
307 _mesa_lookup_shader(GLcontext
*ctx
, GLuint name
)
310 struct gl_shader
*sh
= (struct gl_shader
*)
311 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
312 /* Note that both gl_shader and gl_shader_program objects are kept
313 * in the same hash table. Check the object's type to be sure it's
314 * what we're expecting.
316 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
326 * Initialize context's shader state.
329 _mesa_init_shader_state(GLcontext
* ctx
)
331 /* Device drivers may override these to control what kind of instructions
332 * are generated by the GLSL compiler.
334 ctx
->Shader
.EmitHighLevelInstructions
= GL_TRUE
;
335 ctx
->Shader
.EmitCondCodes
= GL_TRUE
; /* XXX probably want GL_FALSE... */
336 ctx
->Shader
.EmitComments
= GL_FALSE
;
341 * Free the per-context shader-related state.
344 _mesa_free_shader_state(GLcontext
*ctx
)
346 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
351 * Copy string from <src> to <dst>, up to maxLength characters, returning
352 * length of <dst> in <length>.
353 * \param src the strings source
354 * \param maxLength max chars to copy
355 * \param length returns number of chars copied
356 * \param dst the string destination
359 copy_string(GLchar
*dst
, GLsizei maxLength
, GLsizei
*length
, const GLchar
*src
)
362 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
372 * Called via ctx->Driver.AttachShader()
375 _mesa_attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
377 struct gl_shader_program
*shProg
378 = _mesa_lookup_shader_program(ctx
, program
);
379 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
380 const GLuint n
= shProg
->NumShaders
;
383 if (!shProg
|| !sh
) {
384 _mesa_error(ctx
, GL_INVALID_VALUE
,
385 "glAttachShader(bad program or shader name)");
389 for (i
= 0; i
< n
; i
++) {
390 if (shProg
->Shaders
[i
] == sh
) {
391 /* already attached */
397 shProg
->Shaders
= (struct gl_shader
**)
398 _mesa_realloc(shProg
->Shaders
,
399 n
* sizeof(struct gl_shader
*),
400 (n
+ 1) * sizeof(struct gl_shader
*));
401 if (!shProg
->Shaders
) {
402 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
407 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
408 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
409 shProg
->NumShaders
++;
414 _mesa_bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
417 struct gl_shader_program
*shProg
418 = _mesa_lookup_shader_program(ctx
, program
);
419 const GLint size
= -1; /* unknown size */
423 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(program)");
430 if (strncmp(name
, "gl_", 3) == 0) {
431 _mesa_error(ctx
, GL_INVALID_OPERATION
,
432 "glBindAttribLocation(illegal name)");
436 if (shProg
->LinkStatus
) {
437 /* get current index/location for the attribute */
438 oldIndex
= _mesa_get_attrib_location(ctx
, program
, name
);
444 /* this will replace the current value if it's already in the list */
445 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, index
);
447 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
450 if (shProg
->VertexProgram
&& oldIndex
>= 0 && oldIndex
!= index
) {
451 /* If the index changed, need to search/replace references to that attribute
452 * in the vertex program.
454 _slang_remap_attribute(&shProg
->VertexProgram
->Base
, oldIndex
, index
);
460 _mesa_create_shader(GLcontext
*ctx
, GLenum type
)
462 struct gl_shader
*sh
;
465 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
468 case GL_FRAGMENT_SHADER
:
469 case GL_VERTEX_SHADER
:
470 sh
= _mesa_new_shader(ctx
, name
, type
);
473 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
477 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
484 _mesa_create_program(GLcontext
*ctx
)
487 struct gl_shader_program
*shProg
;
489 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
490 shProg
= _mesa_new_shader_program(ctx
, name
);
492 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
494 assert(shProg
->RefCount
== 1);
501 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
505 _mesa_delete_program2(GLcontext
*ctx
, GLuint name
)
508 * NOTE: deleting shaders/programs works a bit differently than
509 * texture objects (and buffer objects, etc). Shader/program
510 * handles/IDs exist in the hash table until the object is really
511 * deleted (refcount==0). With texture objects, the handle/ID is
512 * removed from the hash table in glDeleteTextures() while the tex
513 * object itself might linger until its refcount goes to zero.
515 struct gl_shader_program
*shProg
;
517 shProg
= _mesa_lookup_shader_program(ctx
, name
);
519 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteProgram(name)");
523 shProg
->DeletePending
= GL_TRUE
;
525 /* effectively, decr shProg's refcount */
526 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
531 _mesa_delete_shader(GLcontext
*ctx
, GLuint shader
)
533 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
538 sh
->DeletePending
= GL_TRUE
;
540 /* effectively, decr sh's refcount */
541 _mesa_reference_shader(ctx
, &sh
, NULL
);
546 _mesa_detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
548 struct gl_shader_program
*shProg
549 = _mesa_lookup_shader_program(ctx
, program
);
550 const GLuint n
= shProg
->NumShaders
;
554 _mesa_error(ctx
, GL_INVALID_VALUE
,
555 "glDetachShader(bad program or shader name)");
559 for (i
= 0; i
< n
; i
++) {
560 if (shProg
->Shaders
[i
]->Name
== shader
) {
562 struct gl_shader
**newList
;
565 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
567 /* alloc new, smaller array */
568 newList
= (struct gl_shader
**)
569 _mesa_malloc((n
- 1) * sizeof(struct gl_shader
*));
571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
574 for (j
= 0; j
< i
; j
++) {
575 newList
[j
] = shProg
->Shaders
[j
];
578 newList
[j
++] = shProg
->Shaders
[i
];
579 _mesa_free(shProg
->Shaders
);
581 shProg
->Shaders
= newList
;
582 shProg
->NumShaders
= n
- 1;
587 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
588 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
589 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
590 assert(shProg
->Shaders
[j
]->RefCount
> 0);
600 _mesa_error(ctx
, GL_INVALID_VALUE
,
601 "glDetachShader(shader not found)");
606 _mesa_get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
607 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
608 GLenum
*type
, GLchar
*nameOut
)
610 static const GLenum vec_types
[] = {
611 GL_FLOAT
, GL_FLOAT_VEC2
, GL_FLOAT_VEC3
, GL_FLOAT_VEC4
613 struct gl_shader_program
*shProg
614 = _mesa_lookup_shader_program(ctx
, program
);
618 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib");
622 if (!shProg
->Attributes
|| index
>= shProg
->Attributes
->NumParameters
) {
623 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
627 copy_string(nameOut
, maxLength
, length
,
628 shProg
->Attributes
->Parameters
[index
].Name
);
629 sz
= shProg
->Attributes
->Parameters
[index
].Size
;
633 *type
= vec_types
[sz
]; /* XXX this is a temporary hack */
638 * Called via ctx->Driver.GetActiveUniform().
641 _mesa_get_active_uniform(GLcontext
*ctx
, GLuint program
, GLuint index
,
642 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
643 GLenum
*type
, GLchar
*nameOut
)
645 struct gl_shader_program
*shProg
646 = _mesa_lookup_shader_program(ctx
, program
);
650 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform");
654 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumParameters
) {
655 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
660 for (j
= 0; j
< shProg
->Uniforms
->NumParameters
; j
++) {
661 if (shProg
->Uniforms
->Parameters
[j
].Type
== PROGRAM_UNIFORM
||
662 shProg
->Uniforms
->Parameters
[j
].Type
== PROGRAM_SAMPLER
) {
665 copy_string(nameOut
, maxLength
, length
,
666 shProg
->Uniforms
->Parameters
[j
].Name
);
668 *size
= shProg
->Uniforms
->Parameters
[j
].Size
;
670 *type
= shProg
->Uniforms
->Parameters
[j
].DataType
;
677 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
682 * Called via ctx->Driver.GetAttachedShaders().
685 _mesa_get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
686 GLsizei
*count
, GLuint
*obj
)
688 struct gl_shader_program
*shProg
689 = _mesa_lookup_shader_program(ctx
, program
);
692 for (i
= 0; i
< maxCount
&& i
< shProg
->NumShaders
; i
++) {
693 obj
[i
] = shProg
->Shaders
[i
]->Name
;
699 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders");
705 _mesa_get_attrib_location(GLcontext
*ctx
, GLuint program
,
708 struct gl_shader_program
*shProg
709 = _mesa_lookup_shader_program(ctx
, program
);
712 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttribLocation");
716 if (!shProg
->LinkStatus
) {
717 _mesa_error(ctx
, GL_INVALID_OPERATION
,
718 "glGetAttribLocation(program not linked)");
725 if (shProg
->Attributes
) {
726 GLint i
= _mesa_lookup_parameter_index(shProg
->Attributes
, -1, name
);
728 return shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
736 _mesa_get_handle(GLcontext
*ctx
, GLenum pname
)
739 GET_CURRENT_CONTEXT(ctx
);
742 case GL_PROGRAM_OBJECT_ARB
:
744 struct gl2_program_intf
**pro
= ctx
->Shader
.CurrentProgram
;
747 return (**pro
)._container
._generic
.
748 GetName((struct gl2_generic_intf
**) (pro
));
752 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
760 _mesa_get_programiv(GLcontext
*ctx
, GLuint program
,
761 GLenum pname
, GLint
*params
)
763 struct gl_shader_program
*shProg
764 = _mesa_lookup_shader_program(ctx
, program
);
767 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
772 case GL_DELETE_STATUS
:
773 *params
= shProg
->DeletePending
;
776 *params
= shProg
->LinkStatus
;
778 case GL_VALIDATE_STATUS
:
779 *params
= shProg
->Validated
;
781 case GL_INFO_LOG_LENGTH
:
782 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) : 0;
784 case GL_ATTACHED_SHADERS
:
785 *params
= shProg
->NumShaders
;
787 case GL_ACTIVE_ATTRIBUTES
:
788 *params
= shProg
->Attributes
? shProg
->Attributes
->NumParameters
: 0;
790 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
791 *params
= _mesa_longest_parameter_name(shProg
->Attributes
,
794 case GL_ACTIVE_UNIFORMS
:
796 = _mesa_num_parameters_of_type(shProg
->Uniforms
, PROGRAM_UNIFORM
)
797 + _mesa_num_parameters_of_type(shProg
->Uniforms
, PROGRAM_SAMPLER
);
799 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
801 _mesa_longest_parameter_name(shProg
->Uniforms
, PROGRAM_UNIFORM
),
802 _mesa_longest_parameter_name(shProg
->Uniforms
, PROGRAM_SAMPLER
));
804 (*params
)++; /* add one for terminating zero */
807 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
814 _mesa_get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
816 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
819 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderiv(shader)");
825 *params
= shader
->Type
;
827 case GL_DELETE_STATUS
:
828 *params
= shader
->DeletePending
;
830 case GL_COMPILE_STATUS
:
831 *params
= shader
->CompileStatus
;
833 case GL_INFO_LOG_LENGTH
:
834 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) : 0;
836 case GL_SHADER_SOURCE_LENGTH
:
837 *params
= shader
->Source
? strlen((char *) shader
->Source
) : 0;
840 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
847 _mesa_get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
848 GLsizei
*length
, GLchar
*infoLog
)
850 struct gl_shader_program
*shProg
851 = _mesa_lookup_shader_program(ctx
, program
);
853 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
856 copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
861 _mesa_get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
862 GLsizei
*length
, GLchar
*infoLog
)
864 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
866 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
869 copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
874 * Called via ctx->Driver.GetShaderSource().
877 _mesa_get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
878 GLsizei
*length
, GLchar
*sourceOut
)
880 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
882 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(shader)");
885 copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
890 * Called via ctx->Driver.GetUniformfv().
893 _mesa_get_uniformfv(GLcontext
*ctx
, GLuint program
, GLint location
,
896 struct gl_shader_program
*shProg
897 = _mesa_lookup_shader_program(ctx
, program
);
900 if (location
>= 0 && location
< shProg
->Uniforms
->NumParameters
) {
901 for (i
= 0; i
< shProg
->Uniforms
->Parameters
[location
].Size
; i
++) {
902 params
[i
] = shProg
->Uniforms
->ParameterValues
[location
][i
];
906 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetUniformfv(location)");
910 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetUniformfv(program)");
916 * Called via ctx->Driver.GetUniformLocation().
919 _mesa_get_uniform_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
921 struct gl_shader_program
*shProg
922 = _mesa_lookup_shader_program(ctx
, program
);
925 for (loc
= 0; loc
< shProg
->Uniforms
->NumParameters
; loc
++) {
926 const struct gl_program_parameter
*u
927 = shProg
->Uniforms
->Parameters
+ loc
;
928 /* XXX this is a temporary simplification / short-cut.
929 * We need to handle things like "e.c[0].b" as seen in the
930 * GLSL orange book, page 189.
932 if ((u
->Type
== PROGRAM_UNIFORM
||
933 u
->Type
== PROGRAM_SAMPLER
) && !strcmp(u
->Name
, name
)) {
944 _mesa_is_program(GLcontext
*ctx
, GLuint name
)
946 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
947 return shProg
? GL_TRUE
: GL_FALSE
;
952 _mesa_is_shader(GLcontext
*ctx
, GLuint name
)
954 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
955 return shader
? GL_TRUE
: GL_FALSE
;
961 * Called via ctx->Driver.ShaderSource()
964 _mesa_shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
966 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
968 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSource(shaderObj)");
972 /* free old shader source string and install new one */
974 _mesa_free((void *) sh
->Source
);
977 sh
->CompileStatus
= GL_FALSE
;
982 * Called via ctx->Driver.CompileShader()
985 _mesa_compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
987 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
990 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompileShader(shaderObj)");
994 sh
->CompileStatus
= _slang_compile(ctx
, sh
);
999 * Called via ctx->Driver.LinkProgram()
1002 _mesa_link_program(GLcontext
*ctx
, GLuint program
)
1004 struct gl_shader_program
*shProg
;
1006 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1008 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLinkProgram(program)");
1012 _slang_link(ctx
, program
, shProg
);
1017 * Called via ctx->Driver.UseProgram()
1020 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
1022 struct gl_shader_program
*shProg
;
1024 if (ctx
->Shader
.CurrentProgram
&&
1025 ctx
->Shader
.CurrentProgram
->Name
== program
) {
1030 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1033 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1035 _mesa_error(ctx
, GL_INVALID_VALUE
,
1036 "glUseProgramObjectARB(programObj)");
1044 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
1049 * Called via ctx->Driver.Uniform().
1052 _mesa_uniform(GLcontext
*ctx
, GLint location
, GLsizei count
,
1053 const GLvoid
*values
, GLenum type
)
1055 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1058 if (!shProg
|| !shProg
->LinkStatus
) {
1059 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
1063 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumParameters
) {
1064 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location)");
1068 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1071 * If we're setting a sampler, we must use glUniformi1()!
1073 if (shProg
->Uniforms
->Parameters
[location
].Type
== PROGRAM_SAMPLER
) {
1075 if (type
!= GL_INT
|| count
!= 1) {
1076 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1077 "glUniform(only glUniform1i can be used "
1078 "to set sampler uniforms)");
1081 /* check that the sampler (tex unit index) is legal */
1082 unit
= ((GLint
*) values
)[0];
1083 if (unit
>= ctx
->Const
.MaxTextureImageUnits
) {
1084 _mesa_error(ctx
, GL_INVALID_VALUE
,
1085 "glUniform1(invalid sampler/tex unit index)");
1091 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
1113 _mesa_problem(ctx
, "Invalid type in _mesa_uniform");
1117 if (count
* elems
> shProg
->Uniforms
->Parameters
[location
].Size
) {
1118 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(count too large)");
1122 for (k
= 0; k
< count
; k
++) {
1123 GLfloat
*uniformVal
= shProg
->Uniforms
->ParameterValues
[location
+ k
];
1124 if (type
== GL_INT
||
1125 type
== GL_INT_VEC2
||
1126 type
== GL_INT_VEC3
||
1127 type
== GL_INT_VEC4
) {
1128 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
1129 for (i
= 0; i
< elems
; i
++) {
1130 uniformVal
[i
] = (GLfloat
) iValues
[i
];
1134 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
1135 for (i
= 0; i
< elems
; i
++) {
1136 uniformVal
[i
] = fValues
[i
];
1141 if (shProg
->Uniforms
->Parameters
[location
].Type
== PROGRAM_SAMPLER
) {
1142 if (shProg
->VertexProgram
)
1143 _slang_resolve_samplers(shProg
, &shProg
->VertexProgram
->Base
);
1144 if (shProg
->FragmentProgram
)
1145 _slang_resolve_samplers(shProg
, &shProg
->FragmentProgram
->Base
);
1146 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1152 * Called by ctx->Driver.UniformMatrix().
1155 _mesa_uniform_matrix(GLcontext
*ctx
, GLint cols
, GLint rows
,
1156 GLenum matrixType
, GLint location
, GLsizei count
,
1157 GLboolean transpose
, const GLfloat
*values
)
1159 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1160 if (!shProg
|| !shProg
->LinkStatus
) {
1161 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1162 "glUniformMatrix(program not linked)");
1165 if (location
< 0 || location
>= shProg
->Uniforms
->NumParameters
) {
1166 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1169 if (values
== NULL
) {
1170 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1174 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1177 * Note: the _columns_ of a matrix are stored in program registers, not
1180 /* XXXX need to test 3x3 and 2x2 matrices... */
1183 for (col
= 0; col
< cols
; col
++) {
1184 GLfloat
*v
= shProg
->Uniforms
->ParameterValues
[location
+ col
];
1185 for (row
= 0; row
< rows
; row
++) {
1186 v
[row
] = values
[row
* cols
+ col
];
1192 for (col
= 0; col
< cols
; col
++) {
1193 GLfloat
*v
= shProg
->Uniforms
->ParameterValues
[location
+ col
];
1194 for (row
= 0; row
< rows
; row
++) {
1195 v
[row
] = values
[col
* rows
+ row
];
1203 _mesa_validate_program(GLcontext
*ctx
, GLuint program
)
1205 struct gl_shader_program
*shProg
;
1206 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1208 _mesa_error(ctx
, GL_INVALID_VALUE
, "glValidateProgram(program)");
1212 shProg
->Validated
= GL_TRUE
;
1214 /* From the GL spec:
1215 any two active samplers in the current program object are of
1216 different types, but refer to the same texture image unit,
1218 any active sampler in the current program object refers to a texture
1219 image unit where fixed-function fragment processing accesses a
1220 texture target that does not match the sampler type, or
1222 the sum of the number of active samplers in the program and the
1223 number of texture image units enabled for fixed-function fragment
1224 processing exceeds the combined limit on the total number of texture
1225 image units allowed.