2 * Mesa 3-D graphics library
5 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Implementation of GLSL-related API functions
34 * 1. Check that the right error code is generated for all _mesa_error() calls.
35 * 2. Insert FLUSH_VERTICES calls in various places
39 #include "main/glheader.h"
40 #include "main/context.h"
41 #include "main/hash.h"
42 #include "main/macros.h"
43 #include "shader/program.h"
44 #include "shader/prog_parameter.h"
45 #include "shader/prog_print.h"
46 #include "shader/prog_statevars.h"
47 #include "shader/prog_uniform.h"
48 #include "shader/shader_api.h"
49 #include "shader/slang/slang_compile.h"
50 #include "shader/slang/slang_link.h"
51 #include "glapi/dispatch.h"
54 #ifndef GL_PROGRAM_BINARY_LENGTH_OES
55 #define GL_PROGRAM_BINARY_LENGTH_OES 0x8741
60 * Allocate a new gl_shader_program object, initialize it.
62 static struct gl_shader_program
*
63 _mesa_new_shader_program(GLcontext
*ctx
, GLuint name
)
65 struct gl_shader_program
*shProg
;
66 shProg
= CALLOC_STRUCT(gl_shader_program
);
68 shProg
->Type
= GL_SHADER_PROGRAM_MESA
;
71 shProg
->Attributes
= _mesa_new_parameter_list();
78 * Clear (free) the shader program state that gets produced by linking.
81 _mesa_clear_shader_program_data(GLcontext
*ctx
,
82 struct gl_shader_program
*shProg
)
84 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
85 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
87 if (shProg
->Uniforms
) {
88 _mesa_free_uniform_list(shProg
->Uniforms
);
89 shProg
->Uniforms
= NULL
;
92 if (shProg
->Varying
) {
93 _mesa_free_parameter_list(shProg
->Varying
);
94 shProg
->Varying
= NULL
;
100 * Free all the data that hangs off a shader program object, but not the
104 _mesa_free_shader_program_data(GLcontext
*ctx
,
105 struct gl_shader_program
*shProg
)
109 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
111 _mesa_clear_shader_program_data(ctx
, shProg
);
113 if (shProg
->Attributes
) {
114 _mesa_free_parameter_list(shProg
->Attributes
);
115 shProg
->Attributes
= NULL
;
119 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
120 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
122 shProg
->NumShaders
= 0;
124 if (shProg
->Shaders
) {
125 _mesa_free(shProg
->Shaders
);
126 shProg
->Shaders
= NULL
;
129 if (shProg
->InfoLog
) {
130 _mesa_free(shProg
->InfoLog
);
131 shProg
->InfoLog
= NULL
;
137 * Free/delete a shader program object.
140 _mesa_free_shader_program(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
142 _mesa_free_shader_program_data(ctx
, shProg
);
149 * Set ptr to point to shProg.
150 * If ptr is pointing to another object, decrement its refcount (and delete
151 * if refcount hits zero).
152 * Then set ptr to point to shProg, incrementing its refcount.
154 /* XXX this could be static */
156 _mesa_reference_shader_program(GLcontext
*ctx
,
157 struct gl_shader_program
**ptr
,
158 struct gl_shader_program
*shProg
)
161 if (*ptr
== shProg
) {
166 /* Unreference the old shader program */
167 GLboolean deleteFlag
= GL_FALSE
;
168 struct gl_shader_program
*old
= *ptr
;
170 ASSERT(old
->RefCount
> 0);
173 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
174 (void *) old
, old
->Name
, old
->RefCount
);
176 deleteFlag
= (old
->RefCount
== 0);
179 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
180 _mesa_free_shader_program(ctx
, old
);
190 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
191 (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 * As above, but record an error if program is not found.
224 static struct gl_shader_program
*
225 _mesa_lookup_shader_program_err(GLcontext
*ctx
, GLuint name
,
229 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
233 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
234 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
236 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
239 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
240 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
251 * Allocate a new gl_shader object, initialize it.
254 _mesa_new_shader(GLcontext
*ctx
, GLuint name
, GLenum type
)
256 struct gl_shader
*shader
;
257 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
);
258 shader
= CALLOC_STRUCT(gl_shader
);
262 shader
->RefCount
= 1;
269 _mesa_free_shader(GLcontext
*ctx
, struct gl_shader
*sh
)
272 _mesa_free((void *) sh
->Source
);
274 _mesa_free(sh
->InfoLog
);
275 _mesa_reference_program(ctx
, &sh
->Program
, NULL
);
281 * Set ptr to point to sh.
282 * If ptr is pointing to another shader, decrement its refcount (and delete
283 * if refcount hits zero).
284 * Then set ptr to point to sh, incrementing its refcount.
286 /* XXX this could be static */
288 _mesa_reference_shader(GLcontext
*ctx
, struct gl_shader
**ptr
,
289 struct gl_shader
*sh
)
297 /* Unreference the old shader */
298 GLboolean deleteFlag
= GL_FALSE
;
299 struct gl_shader
*old
= *ptr
;
301 ASSERT(old
->RefCount
> 0);
303 /*printf("SHADER DECR %p (%d) to %d\n",
304 (void*) old, old->Name, old->RefCount);*/
305 deleteFlag
= (old
->RefCount
== 0);
308 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
309 _mesa_free_shader(ctx
, old
);
319 /*printf("SHADER INCR %p (%d) to %d\n",
320 (void*) sh, sh->Name, sh->RefCount);*/
327 * Lookup a GLSL shader object.
330 _mesa_lookup_shader(GLcontext
*ctx
, GLuint name
)
333 struct gl_shader
*sh
= (struct gl_shader
*)
334 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
335 /* Note that both gl_shader and gl_shader_program objects are kept
336 * in the same hash table. Check the object's type to be sure it's
337 * what we're expecting.
339 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
349 * As above, but record an error if shader is not found.
351 static struct gl_shader
*
352 _mesa_lookup_shader_err(GLcontext
*ctx
, GLuint name
, const char *caller
)
355 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
359 struct gl_shader
*sh
= (struct gl_shader
*)
360 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
362 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
365 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
366 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
375 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
378 get_shader_flags(void)
380 GLbitfield flags
= 0x0;
381 const char *env
= _mesa_getenv("MESA_GLSL");
384 if (_mesa_strstr(env
, "dump"))
386 if (_mesa_strstr(env
, "log"))
388 if (_mesa_strstr(env
, "nopt"))
389 flags
|= GLSL_NO_OPT
;
390 else if (_mesa_strstr(env
, "opt"))
392 if (_mesa_strstr(env
, "uniform"))
393 flags
|= GLSL_UNIFORMS
;
401 * Initialize context's shader state.
404 _mesa_init_shader_state(GLcontext
* ctx
)
406 /* Device drivers may override these to control what kind of instructions
407 * are generated by the GLSL compiler.
409 ctx
->Shader
.EmitHighLevelInstructions
= GL_TRUE
;
410 ctx
->Shader
.EmitCondCodes
= GL_FALSE
;
411 ctx
->Shader
.EmitComments
= GL_FALSE
;
412 ctx
->Shader
.Flags
= get_shader_flags();
417 * Free the per-context shader-related state.
420 _mesa_free_shader_state(GLcontext
*ctx
)
422 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
427 * Copy string from <src> to <dst>, up to maxLength characters, returning
428 * length of <dst> in <length>.
429 * \param src the strings source
430 * \param maxLength max chars to copy
431 * \param length returns number of chars copied
432 * \param dst the string destination
435 copy_string(GLchar
*dst
, GLsizei maxLength
, GLsizei
*length
, const GLchar
*src
)
438 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
448 _mesa_is_program(GLcontext
*ctx
, GLuint name
)
450 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
451 return shProg
? GL_TRUE
: GL_FALSE
;
456 _mesa_is_shader(GLcontext
*ctx
, GLuint name
)
458 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
459 return shader
? GL_TRUE
: GL_FALSE
;
464 * Called via ctx->Driver.AttachShader()
467 _mesa_attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
469 struct gl_shader_program
*shProg
;
470 struct gl_shader
*sh
;
473 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
477 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
482 n
= shProg
->NumShaders
;
483 for (i
= 0; i
< n
; i
++) {
484 if (shProg
->Shaders
[i
] == sh
) {
485 /* The shader is already attched to this program. The
486 * GL_ARB_shader_objects spec says:
488 * "The error INVALID_OPERATION is generated by AttachObjectARB
489 * if <obj> is already attached to <containerObj>."
491 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
497 shProg
->Shaders
= (struct gl_shader
**)
498 _mesa_realloc(shProg
->Shaders
,
499 n
* sizeof(struct gl_shader
*),
500 (n
+ 1) * sizeof(struct gl_shader
*));
501 if (!shProg
->Shaders
) {
502 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
507 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
508 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
509 shProg
->NumShaders
++;
514 _mesa_get_attrib_location(GLcontext
*ctx
, GLuint program
,
517 struct gl_shader_program
*shProg
518 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
524 if (!shProg
->LinkStatus
) {
525 _mesa_error(ctx
, GL_INVALID_OPERATION
,
526 "glGetAttribLocation(program not linked)");
533 if (shProg
->VertexProgram
) {
534 const struct gl_program_parameter_list
*attribs
=
535 shProg
->VertexProgram
->Base
.Attributes
;
537 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
539 return attribs
->Parameters
[i
].StateIndexes
[0];
548 _mesa_bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
551 struct gl_shader_program
*shProg
;
552 const GLint size
= -1; /* unknown size */
554 GLenum datatype
= GL_FLOAT_VEC4
;
556 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
557 "glBindAttribLocation");
565 if (strncmp(name
, "gl_", 3) == 0) {
566 _mesa_error(ctx
, GL_INVALID_OPERATION
,
567 "glBindAttribLocation(illegal name)");
571 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
572 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
576 if (shProg
->LinkStatus
) {
577 /* get current index/location for the attribute */
578 oldIndex
= _mesa_get_attrib_location(ctx
, program
, name
);
584 /* this will replace the current value if it's already in the list */
585 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
587 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
592 * Note that this attribute binding won't go into effect until
593 * glLinkProgram is called again.
599 _mesa_create_shader(GLcontext
*ctx
, GLenum type
)
601 struct gl_shader
*sh
;
604 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
607 case GL_FRAGMENT_SHADER
:
608 case GL_VERTEX_SHADER
:
609 sh
= _mesa_new_shader(ctx
, name
, type
);
612 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
616 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
623 _mesa_create_program(GLcontext
*ctx
)
626 struct gl_shader_program
*shProg
;
628 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
629 shProg
= _mesa_new_shader_program(ctx
, name
);
631 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
633 assert(shProg
->RefCount
== 1);
640 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
644 _mesa_delete_program2(GLcontext
*ctx
, GLuint name
)
647 * NOTE: deleting shaders/programs works a bit differently than
648 * texture objects (and buffer objects, etc). Shader/program
649 * handles/IDs exist in the hash table until the object is really
650 * deleted (refcount==0). With texture objects, the handle/ID is
651 * removed from the hash table in glDeleteTextures() while the tex
652 * object itself might linger until its refcount goes to zero.
654 struct gl_shader_program
*shProg
;
656 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
660 shProg
->DeletePending
= GL_TRUE
;
662 /* effectively, decr shProg's refcount */
663 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
668 _mesa_delete_shader(GLcontext
*ctx
, GLuint shader
)
670 struct gl_shader
*sh
;
672 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
676 sh
->DeletePending
= GL_TRUE
;
678 /* effectively, decr sh's refcount */
679 _mesa_reference_shader(ctx
, &sh
, NULL
);
684 _mesa_detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
686 struct gl_shader_program
*shProg
;
690 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
694 n
= shProg
->NumShaders
;
696 for (i
= 0; i
< n
; i
++) {
697 if (shProg
->Shaders
[i
]->Name
== shader
) {
699 struct gl_shader
**newList
;
702 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
704 /* alloc new, smaller array */
705 newList
= (struct gl_shader
**)
706 _mesa_malloc((n
- 1) * sizeof(struct gl_shader
*));
708 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
711 for (j
= 0; j
< i
; j
++) {
712 newList
[j
] = shProg
->Shaders
[j
];
715 newList
[j
++] = shProg
->Shaders
[i
];
716 _mesa_free(shProg
->Shaders
);
718 shProg
->Shaders
= newList
;
719 shProg
->NumShaders
= n
- 1;
724 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
725 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
726 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
727 assert(shProg
->Shaders
[j
]->RefCount
> 0);
739 if (_mesa_is_shader(ctx
, shader
))
740 err
= GL_INVALID_OPERATION
;
741 else if (_mesa_is_program(ctx
, shader
))
742 err
= GL_INVALID_OPERATION
;
744 err
= GL_INVALID_VALUE
;
745 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
752 sizeof_glsl_type(GLenum type
)
761 case GL_SAMPLER_CUBE
:
762 case GL_SAMPLER_1D_SHADOW
:
763 case GL_SAMPLER_2D_SHADOW
:
764 case GL_SAMPLER_2D_RECT_ARB
:
765 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
766 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
767 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
768 case GL_SAMPLER_CUBE_SHADOW_EXT
:
783 case GL_FLOAT_MAT2x3
:
784 case GL_FLOAT_MAT2x4
:
785 return 8; /* two float[4] vectors */
787 case GL_FLOAT_MAT3x2
:
788 case GL_FLOAT_MAT3x4
:
789 return 12; /* three float[4] vectors */
791 case GL_FLOAT_MAT4x2
:
792 case GL_FLOAT_MAT4x3
:
793 return 16; /* four float[4] vectors */
795 _mesa_problem(NULL
, "Invalid type in sizeof_glsl_type()");
802 is_boolean_type(GLenum type
)
817 is_integer_type(GLenum type
)
832 is_sampler_type(GLenum type
)
838 case GL_SAMPLER_CUBE
:
839 case GL_SAMPLER_1D_SHADOW
:
840 case GL_SAMPLER_2D_SHADOW
:
841 case GL_SAMPLER_2D_RECT_ARB
:
842 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
843 case GL_SAMPLER_1D_ARRAY_EXT
:
844 case GL_SAMPLER_2D_ARRAY_EXT
:
853 _mesa_get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
854 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
855 GLenum
*type
, GLchar
*nameOut
)
857 const struct gl_program_parameter_list
*attribs
= NULL
;
858 struct gl_shader_program
*shProg
;
860 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
864 if (shProg
->VertexProgram
)
865 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
867 if (!attribs
|| index
>= attribs
->NumParameters
) {
868 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
872 copy_string(nameOut
, maxLength
, length
, attribs
->Parameters
[index
].Name
);
875 *size
= attribs
->Parameters
[index
].Size
876 / sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
879 *type
= attribs
->Parameters
[index
].DataType
;
883 static struct gl_program_parameter
*
884 get_uniform_parameter(const struct gl_shader_program
*shProg
, GLuint index
)
886 const struct gl_program
*prog
;
889 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
891 prog
= &shProg
->VertexProgram
->Base
;
894 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
896 prog
= &shProg
->FragmentProgram
->Base
;
900 if (!prog
|| progPos
< 0)
901 return NULL
; /* should never happen */
903 return &prog
->Parameters
->Parameters
[progPos
];
908 * Called via ctx->Driver.GetActiveUniform().
911 _mesa_get_active_uniform(GLcontext
*ctx
, GLuint program
, GLuint index
,
912 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
913 GLenum
*type
, GLchar
*nameOut
)
915 const struct gl_shader_program
*shProg
;
916 const struct gl_program
*prog
;
917 const struct gl_program_parameter
*param
;
920 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
924 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
925 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
929 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
931 prog
= &shProg
->VertexProgram
->Base
;
934 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
936 prog
= &shProg
->FragmentProgram
->Base
;
940 if (!prog
|| progPos
< 0)
941 return; /* should never happen */
943 ASSERT(progPos
< prog
->Parameters
->NumParameters
);
944 param
= &prog
->Parameters
->Parameters
[progPos
];
947 copy_string(nameOut
, maxLength
, length
, param
->Name
);
951 GLint typeSize
= sizeof_glsl_type(param
->DataType
);
952 if (param
->Size
> typeSize
) {
954 * Array elements are placed on vector[4] boundaries so they're
955 * a multiple of four floats. We round typeSize up to next multiple
956 * of four to get the right size below.
958 typeSize
= (typeSize
+ 3) & ~3;
960 /* Note that the returned size is in units of the <type>, not bytes */
961 *size
= param
->Size
/ typeSize
;
965 *type
= param
->DataType
;
971 * Called via ctx->Driver.GetAttachedShaders().
974 _mesa_get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
975 GLsizei
*count
, GLuint
*obj
)
977 struct gl_shader_program
*shProg
=
978 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
981 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
982 obj
[i
] = shProg
->Shaders
[i
]->Name
;
991 _mesa_get_handle(GLcontext
*ctx
, GLenum pname
)
995 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
996 CALL_GetIntegerv(ctx
->Exec
, (GL_CURRENT_PROGRAM
, &handle
));
998 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
1006 _mesa_get_programiv(GLcontext
*ctx
, GLuint program
,
1007 GLenum pname
, GLint
*params
)
1009 const struct gl_program_parameter_list
*attribs
;
1010 struct gl_shader_program
*shProg
1011 = _mesa_lookup_shader_program(ctx
, program
);
1014 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
1018 if (shProg
->VertexProgram
)
1019 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
1024 case GL_DELETE_STATUS
:
1025 *params
= shProg
->DeletePending
;
1027 case GL_LINK_STATUS
:
1028 *params
= shProg
->LinkStatus
;
1030 case GL_VALIDATE_STATUS
:
1031 *params
= shProg
->Validated
;
1033 case GL_INFO_LOG_LENGTH
:
1034 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
1036 case GL_ATTACHED_SHADERS
:
1037 *params
= shProg
->NumShaders
;
1039 case GL_ACTIVE_ATTRIBUTES
:
1040 *params
= attribs
? attribs
->NumParameters
: 0;
1042 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
1043 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
1045 case GL_ACTIVE_UNIFORMS
:
1046 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
1048 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
1049 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
1051 (*params
)++; /* add one for terminating zero */
1053 case GL_PROGRAM_BINARY_LENGTH_OES
:
1057 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
1064 _mesa_get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
1066 struct gl_shader
*shader
= _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
1073 case GL_SHADER_TYPE
:
1074 *params
= shader
->Type
;
1076 case GL_DELETE_STATUS
:
1077 *params
= shader
->DeletePending
;
1079 case GL_COMPILE_STATUS
:
1080 *params
= shader
->CompileStatus
;
1082 case GL_INFO_LOG_LENGTH
:
1083 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
1085 case GL_SHADER_SOURCE_LENGTH
:
1086 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
1089 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
1096 _mesa_get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
1097 GLsizei
*length
, GLchar
*infoLog
)
1099 struct gl_shader_program
*shProg
1100 = _mesa_lookup_shader_program(ctx
, program
);
1102 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
1105 copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
1110 _mesa_get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
1111 GLsizei
*length
, GLchar
*infoLog
)
1113 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
1115 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
1118 copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1123 * Called via ctx->Driver.GetShaderSource().
1126 _mesa_get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
1127 GLsizei
*length
, GLchar
*sourceOut
)
1129 struct gl_shader
*sh
;
1130 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1134 copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1139 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
1145 case GL_FLOAT_MAT2x3
:
1149 case GL_FLOAT_MAT2x4
:
1157 case GL_FLOAT_MAT3x2
:
1161 case GL_FLOAT_MAT3x4
:
1169 case GL_FLOAT_MAT4x2
:
1173 case GL_FLOAT_MAT4x3
:
1184 * Determine the number of rows and columns occupied by a uniform
1185 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
1186 * the number of rows = 1 and cols = number of elements in the vector.
1189 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
1190 GLint
*rows
, GLint
*cols
)
1192 get_matrix_dims(p
->DataType
, rows
, cols
);
1193 if (*rows
== 0 && *cols
== 0) {
1194 /* not a matrix type, probably a float or vector */
1200 *rows
= p
->Size
/ 4 + 1;
1201 if (p
->Size
% 4 == 0)
1204 *cols
= p
->Size
% 4;
1211 * Helper for get_uniform[fi]v() functions.
1212 * Given a shader program name and uniform location, return a pointer
1213 * to the shader program and return the program parameter position.
1216 lookup_uniform_parameter(GLcontext
*ctx
, GLuint program
, GLint location
,
1217 struct gl_program
**progOut
, GLint
*paramPosOut
)
1219 struct gl_shader_program
*shProg
1220 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniform[if]v");
1221 struct gl_program
*prog
= NULL
;
1224 /* if shProg is NULL, we'll have already recorded an error */
1227 if (!shProg
->Uniforms
||
1229 location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1230 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
1233 /* OK, find the gl_program and program parameter location */
1234 progPos
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
1236 prog
= &shProg
->VertexProgram
->Base
;
1239 progPos
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
1241 prog
= &shProg
->FragmentProgram
->Base
;
1248 *paramPosOut
= progPos
;
1253 * Called via ctx->Driver.GetUniformfv().
1256 _mesa_get_uniformfv(GLcontext
*ctx
, GLuint program
, GLint location
,
1259 struct gl_program
*prog
;
1262 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
1265 const struct gl_program_parameter
*p
=
1266 &prog
->Parameters
->Parameters
[paramPos
];
1267 GLint rows
, cols
, i
, j
, k
;
1269 get_uniform_rows_cols(p
, &rows
, &cols
);
1272 for (i
= 0; i
< rows
; i
++) {
1273 for (j
= 0; j
< cols
; j
++ ) {
1274 params
[k
++] = prog
->Parameters
->ParameterValues
[paramPos
+i
][j
];
1282 * Called via ctx->Driver.GetUniformiv().
1283 * \sa _mesa_get_uniformfv, only difference is a cast.
1286 _mesa_get_uniformiv(GLcontext
*ctx
, GLuint program
, GLint location
,
1289 struct gl_program
*prog
;
1292 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
1295 const struct gl_program_parameter
*p
=
1296 &prog
->Parameters
->Parameters
[paramPos
];
1297 GLint rows
, cols
, i
, j
, k
;
1299 get_uniform_rows_cols(p
, &rows
, &cols
);
1302 for (i
= 0; i
< rows
; i
++) {
1303 for (j
= 0; j
< cols
; j
++ ) {
1304 params
[k
++] = (GLint
) prog
->Parameters
->ParameterValues
[paramPos
+i
][j
];
1312 * The value returned by GetUniformLocation actually encodes two things:
1313 * 1. the index into the prog->Uniforms[] array for the uniform
1314 * 2. an offset in the prog->ParameterValues[] array for specifying array
1315 * elements or structure fields.
1316 * This function merges those two values.
1319 merge_location_offset(GLint
*location
, GLint offset
)
1321 *location
= *location
| (offset
<< 16);
1326 * Seperate the uniform location and parameter offset. See above.
1329 split_location_offset(GLint
*location
, GLint
*offset
)
1331 *offset
= (*location
>> 16);
1332 *location
= *location
& 0xffff;
1337 * Called via ctx->Driver.GetUniformLocation().
1339 * The return value will encode two values, the uniform location and an
1340 * offset (used for arrays, structs).
1343 _mesa_get_uniform_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
1345 GLint offset
= 0, location
= -1;
1347 struct gl_shader_program
*shProg
=
1348 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformLocation");
1353 if (shProg
->LinkStatus
== GL_FALSE
) {
1354 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
1358 /* XXX we should return -1 if the uniform was declared, but not
1362 /* XXX we need to be able to parse uniform names for structs and arrays
1369 /* handle 1-dimension arrays here... */
1370 char *c
= strchr(name
, '[');
1372 /* truncate name at [ */
1373 const GLint len
= c
- name
;
1374 GLchar
*newName
= _mesa_malloc(len
+ 1);
1376 return -1; /* out of mem */
1377 _mesa_memcpy(newName
, name
, len
);
1380 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
1381 if (location
>= 0) {
1382 const GLint element
= _mesa_atoi(c
+ 1);
1384 /* get type of the uniform array element */
1385 struct gl_program_parameter
*p
;
1386 p
= get_uniform_parameter(shProg
, location
);
1389 get_matrix_dims(p
->DataType
, &rows
, &cols
);
1392 offset
= element
* rows
;
1397 _mesa_free(newName
);
1402 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
1405 if (location
>= 0) {
1406 merge_location_offset(&location
, offset
);
1415 * Called via ctx->Driver.ShaderSource()
1418 _mesa_shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
1420 struct gl_shader
*sh
;
1422 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
1426 /* free old shader source string and install new one */
1428 _mesa_free((void *) sh
->Source
);
1430 sh
->Source
= source
;
1431 sh
->CompileStatus
= GL_FALSE
;
1436 * Called via ctx->Driver.CompileShader()
1439 _mesa_compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
1441 struct gl_shader
*sh
;
1443 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
1447 /* this call will set the sh->CompileStatus field to indicate if
1448 * compilation was successful.
1450 (void) _slang_compile(ctx
, sh
);
1455 * Called via ctx->Driver.LinkProgram()
1458 _mesa_link_program(GLcontext
*ctx
, GLuint program
)
1460 struct gl_shader_program
*shProg
;
1462 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
1466 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1468 _slang_link(ctx
, program
, shProg
);
1473 * Called via ctx->Driver.UseProgram()
1476 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
1478 struct gl_shader_program
*shProg
;
1480 if (ctx
->Shader
.CurrentProgram
&&
1481 ctx
->Shader
.CurrentProgram
->Name
== program
) {
1486 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1489 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1493 if (!shProg
->LinkStatus
) {
1494 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1495 "glUseProgram(program %u not linked)", program
);
1503 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
1509 * Update the vertex/fragment program's TexturesUsed array.
1511 * This needs to be called after glUniform(set sampler var) is called.
1512 * A call to glUniform(samplerVar, value) causes a sampler to point to a
1513 * particular texture unit. We know the sampler's texture target
1514 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
1515 * set by glUniform() calls.
1517 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
1518 * information to update the prog->TexturesUsed[] values.
1519 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
1520 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
1521 * We'll use that info for state validation before rendering.
1524 _mesa_update_shader_textures_used(struct gl_program
*prog
)
1528 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
1530 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
1531 if (prog
->SamplersUsed
& (1 << s
)) {
1532 GLuint u
= prog
->SamplerUnits
[s
];
1533 GLuint t
= prog
->SamplerTargets
[s
];
1534 assert(u
< MAX_TEXTURE_IMAGE_UNITS
);
1535 prog
->TexturesUsed
[u
] |= (1 << t
);
1542 * Check if the type given by userType is allowed to set a uniform of the
1543 * target type. Generally, equivalence is required, but setting Boolean
1544 * uniforms can be done with glUniformiv or glUniformfv.
1547 compatible_types(GLenum userType
, GLenum targetType
)
1549 if (userType
== targetType
)
1552 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
|| userType
== GL_INT
))
1555 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
1556 userType
== GL_INT_VEC2
))
1559 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
1560 userType
== GL_INT_VEC3
))
1563 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
1564 userType
== GL_INT_VEC4
))
1567 if (is_sampler_type(targetType
) && userType
== GL_INT
)
1575 * Set the value of a program's uniform variable.
1576 * \param program the program whose uniform to update
1577 * \param index the index of the program parameter for the uniform
1578 * \param offset additional parameter slot offset (for arrays)
1579 * \param type the incoming datatype of 'values'
1580 * \param count the number of uniforms to set
1581 * \param elems number of elements per uniform (1, 2, 3 or 4)
1582 * \param values the new values, of datatype 'type'
1585 set_program_uniform(GLcontext
*ctx
, struct gl_program
*program
,
1586 GLint index
, GLint offset
,
1587 GLenum type
, GLsizei count
, GLint elems
,
1590 struct gl_program_parameter
*param
=
1591 &program
->Parameters
->Parameters
[index
];
1592 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
1593 const GLboolean areIntValues
= is_integer_type(type
);
1595 assert(offset
>= 0);
1599 if (!compatible_types(type
, param
->DataType
)) {
1600 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
1604 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
1605 /* out of bounds! */
1609 if (param
->Type
== PROGRAM_SAMPLER
) {
1610 /* This controls which texture unit which is used by a sampler */
1611 GLuint texUnit
, sampler
;
1614 /* data type for setting samplers must be int */
1615 if (type
!= GL_INT
) {
1616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1617 "glUniform(only glUniform1i can be used "
1618 "to set sampler uniforms)");
1622 /* XXX arrays of samplers haven't been tested much, but it's not a
1625 for (i
= 0; i
< count
; i
++) {
1626 sampler
= (GLuint
) program
->Parameters
->ParameterValues
[index
+ i
][0];
1627 texUnit
= ((GLuint
*) values
)[i
];
1629 /* check that the sampler (tex unit index) is legal */
1630 if (texUnit
>= ctx
->Const
.MaxTextureImageUnits
) {
1631 _mesa_error(ctx
, GL_INVALID_VALUE
,
1632 "glUniform1(invalid sampler/tex unit index)");
1636 /* This maps a sampler to a texture unit: */
1637 if (sampler
< MAX_SAMPLERS
) {
1638 program
->SamplerUnits
[sampler
] = texUnit
;
1642 _mesa_update_shader_textures_used(program
);
1644 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1647 /* ordinary uniform variable */
1649 const GLint slots
= (param
->Size
+ 3) / 4;
1650 const GLint typeSize
= sizeof_glsl_type(param
->DataType
);
1652 if (param
->Size
> typeSize
) {
1654 /* we'll ignore extra data below */
1657 /* non-array: count must be one */
1659 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1660 "glUniform(uniform is not an array)");
1665 /* loop over number of array elements */
1666 for (k
= 0; k
< count
; k
++) {
1667 GLfloat
*uniformVal
;
1669 if (offset
+ k
>= slots
) {
1670 /* Extra array data is ignored */
1674 /* uniformVal (the destination) is always float[4] */
1675 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
1678 /* convert user's ints to floats */
1679 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
1680 for (i
= 0; i
< elems
; i
++) {
1681 uniformVal
[i
] = (GLfloat
) iValues
[i
];
1685 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
1686 for (i
= 0; i
< elems
; i
++) {
1687 uniformVal
[i
] = fValues
[i
];
1691 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
1692 if (isUniformBool
) {
1693 for (i
= 0; i
< elems
; i
++) {
1694 uniformVal
[i
] = uniformVal
[i
] ? 1.0f
: 0.0f
;
1703 * Called via ctx->Driver.Uniform().
1706 _mesa_uniform(GLcontext
*ctx
, GLint location
, GLsizei count
,
1707 const GLvoid
*values
, GLenum type
)
1709 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1710 struct gl_uniform
*uniform
;
1711 GLint elems
, offset
;
1714 if (!shProg
|| !shProg
->LinkStatus
) {
1715 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
1720 return; /* The standard specifies this as a no-op */
1722 if (location
< -1) {
1723 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location)");
1727 split_location_offset(&location
, &offset
);
1729 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1730 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location)");
1735 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
1741 basicType
= GL_FLOAT
;
1749 basicType
= GL_FLOAT
;
1757 basicType
= GL_FLOAT
;
1765 basicType
= GL_FLOAT
;
1773 _mesa_problem(ctx
, "Invalid type in _mesa_uniform");
1777 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1779 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1781 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
1783 _mesa_printf("Mesa: set program %u uniform %s (loc %d) to: ",
1784 shProg
->Name
, uniform
->Name
, location
);
1785 if (basicType
== GL_INT
) {
1786 const GLint
*v
= (const GLint
*) values
;
1787 for (i
= 0; i
< count
* elems
; i
++) {
1788 _mesa_printf("%d ", v
[i
]);
1792 const GLfloat
*v
= (const GLfloat
*) values
;
1793 for (i
= 0; i
< count
* elems
; i
++) {
1794 _mesa_printf("%g ", v
[i
]);
1800 /* A uniform var may be used by both a vertex shader and a fragment
1801 * shader. We may need to update one or both shader's uniform here:
1803 if (shProg
->VertexProgram
) {
1804 /* convert uniform location to program parameter index */
1805 GLint index
= uniform
->VertPos
;
1807 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
1808 index
, offset
, type
, count
, elems
, values
);
1812 if (shProg
->FragmentProgram
) {
1813 /* convert uniform location to program parameter index */
1814 GLint index
= uniform
->FragPos
;
1816 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
1817 index
, offset
, type
, count
, elems
, values
);
1821 uniform
->Initialized
= GL_TRUE
;
1826 * Set a matrix-valued program parameter.
1829 set_program_uniform_matrix(GLcontext
*ctx
, struct gl_program
*program
,
1830 GLuint index
, GLuint offset
,
1831 GLuint count
, GLuint rows
, GLuint cols
,
1832 GLboolean transpose
, const GLfloat
*values
)
1834 GLuint mat
, row
, col
;
1835 GLuint dst
= index
+ offset
, src
= 0;
1838 /* check that the number of rows, columns is correct */
1839 get_matrix_dims(program
->Parameters
->Parameters
[index
].DataType
, &nr
, &nc
);
1840 if (rows
!= nr
|| cols
!= nc
) {
1841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1842 "glUniformMatrix(matrix size mismatch)");
1846 if (index
+ offset
> program
->Parameters
->Size
) {
1847 /* out of bounds! */
1852 * Note: the _columns_ of a matrix are stored in program registers, not
1853 * the rows. So, the loops below look a little funny.
1854 * XXX could optimize this a bit...
1857 /* loop over matrices */
1858 for (mat
= 0; mat
< count
; mat
++) {
1861 for (col
= 0; col
< cols
; col
++) {
1862 GLfloat
*v
= program
->Parameters
->ParameterValues
[dst
];
1863 for (row
= 0; row
< rows
; row
++) {
1865 v
[row
] = values
[src
+ row
* cols
+ col
];
1868 v
[row
] = values
[src
+ col
* rows
+ row
];
1874 src
+= rows
* cols
; /* next matrix */
1880 * Called by ctx->Driver.UniformMatrix().
1881 * Note: cols=2, rows=4 ==> array[2] of vec4
1884 _mesa_uniform_matrix(GLcontext
*ctx
, GLint cols
, GLint rows
,
1885 GLenum matrixType
, GLint location
, GLsizei count
,
1886 GLboolean transpose
, const GLfloat
*values
)
1888 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1889 struct gl_uniform
*uniform
;
1892 if (!shProg
|| !shProg
->LinkStatus
) {
1893 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1894 "glUniformMatrix(program not linked)");
1899 return; /* The standard specifies this as a no-op */
1901 if (location
< -1) {
1902 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
1906 split_location_offset(&location
, &offset
);
1908 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1909 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1912 if (values
== NULL
) {
1913 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1917 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1919 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1921 if (shProg
->VertexProgram
) {
1922 /* convert uniform location to program parameter index */
1923 GLint index
= uniform
->VertPos
;
1925 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
1927 count
, rows
, cols
, transpose
, values
);
1931 if (shProg
->FragmentProgram
) {
1932 /* convert uniform location to program parameter index */
1933 GLint index
= uniform
->FragPos
;
1935 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
1937 count
, rows
, cols
, transpose
, values
);
1941 uniform
->Initialized
= GL_TRUE
;
1946 _mesa_validate_program(GLcontext
*ctx
, GLuint program
)
1948 struct gl_shader_program
*shProg
;
1950 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1955 if (!shProg
->LinkStatus
) {
1956 shProg
->Validated
= GL_FALSE
;
1960 /* From the GL spec, a program is invalid if any of these are true:
1962 any two active samplers in the current program object are of
1963 different types, but refer to the same texture image unit,
1965 any active sampler in the current program object refers to a texture
1966 image unit where fixed-function fragment processing accesses a
1967 texture target that does not match the sampler type, or
1969 the sum of the number of active samplers in the program and the
1970 number of texture image units enabled for fixed-function fragment
1971 processing exceeds the combined limit on the total number of texture
1972 image units allowed.
1975 shProg
->Validated
= GL_TRUE
;
1980 * Plug in Mesa's GLSL functions into the device driver function table.
1983 _mesa_init_glsl_driver_functions(struct dd_function_table
*driver
)
1985 driver
->AttachShader
= _mesa_attach_shader
;
1986 driver
->BindAttribLocation
= _mesa_bind_attrib_location
;
1987 driver
->CompileShader
= _mesa_compile_shader
;
1988 driver
->CreateProgram
= _mesa_create_program
;
1989 driver
->CreateShader
= _mesa_create_shader
;
1990 driver
->DeleteProgram2
= _mesa_delete_program2
;
1991 driver
->DeleteShader
= _mesa_delete_shader
;
1992 driver
->DetachShader
= _mesa_detach_shader
;
1993 driver
->GetActiveAttrib
= _mesa_get_active_attrib
;
1994 driver
->GetActiveUniform
= _mesa_get_active_uniform
;
1995 driver
->GetAttachedShaders
= _mesa_get_attached_shaders
;
1996 driver
->GetAttribLocation
= _mesa_get_attrib_location
;
1997 driver
->GetHandle
= _mesa_get_handle
;
1998 driver
->GetProgramiv
= _mesa_get_programiv
;
1999 driver
->GetProgramInfoLog
= _mesa_get_program_info_log
;
2000 driver
->GetShaderiv
= _mesa_get_shaderiv
;
2001 driver
->GetShaderInfoLog
= _mesa_get_shader_info_log
;
2002 driver
->GetShaderSource
= _mesa_get_shader_source
;
2003 driver
->GetUniformfv
= _mesa_get_uniformfv
;
2004 driver
->GetUniformiv
= _mesa_get_uniformiv
;
2005 driver
->GetUniformLocation
= _mesa_get_uniform_location
;
2006 driver
->IsProgram
= _mesa_is_program
;
2007 driver
->IsShader
= _mesa_is_shader
;
2008 driver
->LinkProgram
= _mesa_link_program
;
2009 driver
->ShaderSource
= _mesa_shader_source
;
2010 driver
->Uniform
= _mesa_uniform
;
2011 driver
->UniformMatrix
= _mesa_uniform_matrix
;
2012 driver
->UseProgram
= _mesa_use_program
;
2013 driver
->ValidateProgram
= _mesa_validate_program
;