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 * 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 * Functions related to GLSL uniform variables.
33 * 1. Check that the right error code is generated for all _mesa_error() calls.
34 * 2. Insert FLUSH_VERTICES calls in various places
38 #include "main/glheader.h"
39 #include "main/context.h"
40 #include "main/dispatch.h"
41 #include "main/shaderapi.h"
42 #include "main/shaderobj.h"
43 #include "main/uniforms.h"
44 #include "program/prog_parameter.h"
45 #include "program/prog_statevars.h"
46 #include "program/prog_uniform.h"
51 base_uniform_type(GLenum type
)
54 #if 0 /* not needed, for now */
67 case GL_UNSIGNED_INT_VEC2
:
68 case GL_UNSIGNED_INT_VEC3
:
69 case GL_UNSIGNED_INT_VEC4
:
70 return GL_UNSIGNED_INT
;
77 _mesa_problem(NULL
, "Invalid type in base_uniform_type()");
84 is_boolean_type(GLenum type
)
99 is_sampler_type(GLenum type
)
105 case GL_SAMPLER_CUBE
:
106 case GL_SAMPLER_1D_SHADOW
:
107 case GL_SAMPLER_2D_SHADOW
:
108 case GL_SAMPLER_2D_RECT_ARB
:
109 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
110 case GL_SAMPLER_1D_ARRAY_EXT
:
111 case GL_SAMPLER_2D_ARRAY_EXT
:
112 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
113 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
121 static struct gl_program_parameter
*
122 get_uniform_parameter(const struct gl_shader_program
*shProg
, GLuint index
)
124 const struct gl_program
*prog
= NULL
;
127 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
129 prog
= &shProg
->VertexProgram
->Base
;
132 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
134 prog
= &shProg
->FragmentProgram
->Base
;
138 if (!prog
|| progPos
< 0)
139 return NULL
; /* should never happen */
141 return &prog
->Parameters
->Parameters
[progPos
];
146 * Called by glGetActiveUniform().
149 _mesa_get_active_uniform(GLcontext
*ctx
, GLuint program
, GLuint index
,
150 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
151 GLenum
*type
, GLchar
*nameOut
)
153 const struct gl_shader_program
*shProg
;
154 const struct gl_program
*prog
= NULL
;
155 const struct gl_program_parameter
*param
;
158 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
162 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
163 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
167 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
169 prog
= &shProg
->VertexProgram
->Base
;
172 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
174 prog
= &shProg
->FragmentProgram
->Base
;
176 progPos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
178 prog
= &shProg
->GeometryProgram
->Base
;
183 if (!prog
|| progPos
< 0)
184 return; /* should never happen */
186 ASSERT(progPos
< prog
->Parameters
->NumParameters
);
187 param
= &prog
->Parameters
->Parameters
[progPos
];
190 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
194 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
195 if ((GLint
) param
->Size
> typeSize
) {
197 * Array elements are placed on vector[4] boundaries so they're
198 * a multiple of four floats. We round typeSize up to next multiple
199 * of four to get the right size below.
201 typeSize
= (typeSize
+ 3) & ~3;
203 /* Note that the returned size is in units of the <type>, not bytes */
204 *size
= param
->Size
/ typeSize
;
208 *type
= param
->DataType
;
215 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
221 case GL_FLOAT_MAT2x3
:
225 case GL_FLOAT_MAT2x4
:
233 case GL_FLOAT_MAT3x2
:
237 case GL_FLOAT_MAT3x4
:
245 case GL_FLOAT_MAT4x2
:
249 case GL_FLOAT_MAT4x3
:
260 * Determine the number of rows and columns occupied by a uniform
261 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
262 * the number of rows = 1 and cols = number of elements in the vector.
265 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
266 GLint
*rows
, GLint
*cols
)
268 get_matrix_dims(p
->DataType
, rows
, cols
);
269 if (*rows
== 0 && *cols
== 0) {
270 /* not a matrix type, probably a float or vector */
276 *rows
= p
->Size
/ 4 + 1;
277 if (p
->Size
% 4 == 0)
287 * Helper for get_uniform[fi]v() functions.
288 * Given a shader program name and uniform location, return a pointer
289 * to the shader program and return the program parameter position.
292 lookup_uniform_parameter(GLcontext
*ctx
, GLuint program
, GLint location
,
293 struct gl_program
**progOut
, GLint
*paramPosOut
)
295 struct gl_shader_program
*shProg
296 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniform[if]v");
297 struct gl_program
*prog
= NULL
;
300 /* if shProg is NULL, we'll have already recorded an error */
303 if (!shProg
->Uniforms
||
305 location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
306 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
309 /* OK, find the gl_program and program parameter location */
310 progPos
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
312 prog
= &shProg
->VertexProgram
->Base
;
315 progPos
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
317 prog
= &shProg
->FragmentProgram
->Base
;
324 *paramPosOut
= progPos
;
329 * GLGL uniform arrays and structs require special handling.
331 * The GL_ARB_shader_objects spec says that if you use
332 * glGetUniformLocation to get the location of an array, you CANNOT
333 * access other elements of the array by adding an offset to the
334 * returned location. For example, you must call
335 * glGetUniformLocation("foo[16]") if you want to set the 16th element
336 * of the array with glUniform().
338 * HOWEVER, some other OpenGL drivers allow accessing array elements
339 * by adding an offset to the returned array location. And some apps
340 * seem to depend on that behaviour.
342 * Mesa's gl_uniform_list doesn't directly support this since each
343 * entry in the list describes one uniform variable, not one uniform
344 * element. We could insert dummy entries in the list for each array
345 * element after [0] but that causes complications elsewhere.
347 * We solve this problem by encoding two values in the location that's
348 * returned by glGetUniformLocation():
349 * a) index into gl_uniform_list::Uniforms[] for the uniform
350 * b) an array/field offset (0 for simple types)
352 * These two values are encoded in the high and low halves of a GLint.
353 * By putting the uniform number in the high part and the offset in the
354 * low part, we can support the unofficial ability to index into arrays
355 * by adding offsets to the location value.
358 merge_location_offset(GLint
*location
, GLint offset
)
360 *location
= (*location
<< 16) | offset
;
365 * Separate the uniform location and parameter offset. See above.
368 split_location_offset(GLint
*location
, GLint
*offset
)
370 *offset
= *location
& 0xffff;
371 *location
= *location
>> 16;
377 * Called via glGetUniformfv().
380 _mesa_get_uniformfv(GLcontext
*ctx
, GLuint program
, GLint location
,
383 struct gl_program
*prog
;
387 split_location_offset(&location
, &offset
);
389 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
392 const struct gl_program_parameter
*p
=
393 &prog
->Parameters
->Parameters
[paramPos
];
394 GLint rows
, cols
, i
, j
, k
;
396 get_uniform_rows_cols(p
, &rows
, &cols
);
399 for (i
= 0; i
< rows
; i
++) {
400 for (j
= 0; j
< cols
; j
++ ) {
401 params
[k
++] = prog
->Parameters
->ParameterValues
[paramPos
+i
][j
];
409 * Called via glGetUniformiv().
410 * \sa _mesa_get_uniformfv, only difference is a cast.
413 _mesa_get_uniformiv(GLcontext
*ctx
, GLuint program
, GLint location
,
416 struct gl_program
*prog
;
420 split_location_offset(&location
, &offset
);
422 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
425 const struct gl_program_parameter
*p
=
426 &prog
->Parameters
->Parameters
[paramPos
];
427 GLint rows
, cols
, i
, j
, k
;
429 get_uniform_rows_cols(p
, &rows
, &cols
);
432 for (i
= 0; i
< rows
; i
++) {
433 for (j
= 0; j
< cols
; j
++ ) {
434 params
[k
++] = (GLint
) prog
->Parameters
->ParameterValues
[paramPos
+i
][j
];
442 * Called via glGetUniformLocation().
444 * The return value will encode two values, the uniform location and an
445 * offset (used for arrays, structs).
448 _mesa_get_uniform_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
450 GLint offset
= 0, location
= -1;
452 struct gl_shader_program
*shProg
=
453 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformLocation");
458 if (shProg
->LinkStatus
== GL_FALSE
) {
459 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
463 /* XXX we should return -1 if the uniform was declared, but not
467 /* XXX we need to be able to parse uniform names for structs and arrays
474 /* handle 1-dimension arrays here... */
475 char *c
= strchr(name
, '[');
477 /* truncate name at [ */
478 const GLint len
= c
- name
;
479 GLchar
*newName
= malloc(len
+ 1);
481 return -1; /* out of mem */
482 memcpy(newName
, name
, len
);
485 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
487 const GLint element
= atoi(c
+ 1);
489 /* get type of the uniform array element */
490 struct gl_program_parameter
*p
;
491 p
= get_uniform_parameter(shProg
, location
);
494 get_matrix_dims(p
->DataType
, &rows
, &cols
);
497 offset
= element
* rows
;
507 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
511 merge_location_offset(&location
, offset
);
520 * Update the vertex/fragment program's TexturesUsed array.
522 * This needs to be called after glUniform(set sampler var) is called.
523 * A call to glUniform(samplerVar, value) causes a sampler to point to a
524 * particular texture unit. We know the sampler's texture target
525 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
526 * set by glUniform() calls.
528 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
529 * information to update the prog->TexturesUsed[] values.
530 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
531 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
532 * We'll use that info for state validation before rendering.
535 _mesa_update_shader_textures_used(struct gl_program
*prog
)
539 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
541 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
542 if (prog
->SamplersUsed
& (1 << s
)) {
543 GLuint unit
= prog
->SamplerUnits
[s
];
544 GLuint tgt
= prog
->SamplerTargets
[s
];
545 assert(unit
< MAX_TEXTURE_IMAGE_UNITS
);
546 assert(tgt
< NUM_TEXTURE_TARGETS
);
547 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
554 * Check if the type given by userType is allowed to set a uniform of the
555 * target type. Generally, equivalence is required, but setting Boolean
556 * uniforms can be done with glUniformiv or glUniformfv.
559 compatible_types(GLenum userType
, GLenum targetType
)
561 if (userType
== targetType
)
564 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
565 userType
== GL_UNSIGNED_INT
||
569 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
570 userType
== GL_UNSIGNED_INT_VEC2
||
571 userType
== GL_INT_VEC2
))
574 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
575 userType
== GL_UNSIGNED_INT_VEC3
||
576 userType
== GL_INT_VEC3
))
579 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
580 userType
== GL_UNSIGNED_INT_VEC4
||
581 userType
== GL_INT_VEC4
))
584 if (is_sampler_type(targetType
) && userType
== GL_INT
)
592 * Set the value of a program's uniform variable.
593 * \param program the program whose uniform to update
594 * \param index the index of the program parameter for the uniform
595 * \param offset additional parameter slot offset (for arrays)
596 * \param type the incoming datatype of 'values'
597 * \param count the number of uniforms to set
598 * \param elems number of elements per uniform (1, 2, 3 or 4)
599 * \param values the new values, of datatype 'type'
602 set_program_uniform(GLcontext
*ctx
, struct gl_program
*program
,
603 GLint index
, GLint offset
,
604 GLenum type
, GLsizei count
, GLint elems
,
607 const struct gl_program_parameter
*param
=
608 &program
->Parameters
->Parameters
[index
];
614 if (!compatible_types(type
, param
->DataType
)) {
615 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
619 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
624 if (param
->Type
== PROGRAM_SAMPLER
) {
625 /* This controls which texture unit which is used by a sampler */
626 GLboolean changed
= GL_FALSE
;
629 /* this should have been caught by the compatible_types() check */
630 ASSERT(type
== GL_INT
);
632 /* loop over number of samplers to change */
633 for (i
= 0; i
< count
; i
++) {
635 (GLuint
) program
->Parameters
->ParameterValues
[index
+ offset
+ i
][0];
636 GLuint texUnit
= ((GLuint
*) values
)[i
];
638 /* check that the sampler (tex unit index) is legal */
639 if (texUnit
>= ctx
->Const
.MaxTextureImageUnits
) {
640 _mesa_error(ctx
, GL_INVALID_VALUE
,
641 "glUniform1(invalid sampler/tex unit index for '%s')",
646 /* This maps a sampler to a texture unit: */
647 if (sampler
< MAX_SAMPLERS
) {
649 printf("Set program %p sampler %d '%s' to unit %u\n",
650 program
, sampler
, param
->Name
, texUnit
);
652 if (program
->SamplerUnits
[sampler
] != texUnit
) {
653 program
->SamplerUnits
[sampler
] = texUnit
;
660 /* When a sampler's value changes it usually requires rewriting
661 * a GPU program's TEX instructions since there may not be a
662 * sampler->texture lookup table. We signal this with the
663 * ProgramStringNotify() callback.
665 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
666 _mesa_update_shader_textures_used(program
);
667 /* Do we need to care about the return value here?
668 * This should not be the first time the driver was notified of
671 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
675 /* ordinary uniform variable */
676 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
677 const GLenum basicType
= base_uniform_type(type
);
678 const GLint slots
= (param
->Size
+ 3) / 4;
679 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
682 if ((GLint
) param
->Size
> typeSize
) {
684 /* we'll ignore extra data below */
687 /* non-array: count must be at most one; count == 0 is handled by the loop below */
689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
690 "glUniform(uniform '%s' is not an array)",
696 /* loop over number of array elements */
697 for (k
= 0; k
< count
; k
++) {
700 if (offset
+ k
>= slots
) {
701 /* Extra array data is ignored */
705 /* uniformVal (the destination) is always float[4] */
706 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
708 if (basicType
== GL_INT
) {
709 /* convert user's ints to floats */
710 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
711 for (i
= 0; i
< elems
; i
++) {
712 uniformVal
[i
] = (GLfloat
) iValues
[i
];
715 else if (basicType
== GL_UNSIGNED_INT
) {
716 /* convert user's uints to floats */
717 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
718 for (i
= 0; i
< elems
; i
++) {
719 uniformVal
[i
] = (GLfloat
) iValues
[i
];
723 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
724 assert(basicType
== GL_FLOAT
);
725 for (i
= 0; i
< elems
; i
++) {
726 uniformVal
[i
] = fValues
[i
];
730 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
732 for (i
= 0; i
< elems
; i
++) {
733 uniformVal
[i
] = uniformVal
[i
] ? 1.0f
: 0.0f
;
742 * Called via glUniform*() functions.
745 _mesa_uniform(GLcontext
*ctx
, GLint location
, GLsizei count
,
746 const GLvoid
*values
, GLenum type
)
748 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
749 struct gl_uniform
*uniform
;
752 if (!shProg
|| !shProg
->LinkStatus
) {
753 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
758 return; /* The standard specifies this as a no-op */
761 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
766 split_location_offset(&location
, &offset
);
768 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
769 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
774 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
778 elems
= _mesa_sizeof_glsl_type(type
);
780 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
782 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
784 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
785 const GLenum basicType
= base_uniform_type(type
);
787 printf("Mesa: set program %u uniform %s (loc %d) to: ",
788 shProg
->Name
, uniform
->Name
, location
);
789 if (basicType
== GL_INT
) {
790 const GLint
*v
= (const GLint
*) values
;
791 for (i
= 0; i
< count
* elems
; i
++) {
795 else if (basicType
== GL_UNSIGNED_INT
) {
796 const GLuint
*v
= (const GLuint
*) values
;
797 for (i
= 0; i
< count
* elems
; i
++) {
802 const GLfloat
*v
= (const GLfloat
*) values
;
803 assert(basicType
== GL_FLOAT
);
804 for (i
= 0; i
< count
* elems
; i
++) {
811 /* A uniform var may be used by both a vertex shader and a fragment
812 * shader. We may need to update one or both shader's uniform here:
814 if (shProg
->VertexProgram
) {
815 /* convert uniform location to program parameter index */
816 GLint index
= uniform
->VertPos
;
818 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
819 index
, offset
, type
, count
, elems
, values
);
823 if (shProg
->FragmentProgram
) {
824 /* convert uniform location to program parameter index */
825 GLint index
= uniform
->FragPos
;
827 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
828 index
, offset
, type
, count
, elems
, values
);
832 uniform
->Initialized
= GL_TRUE
;
837 * Set a matrix-valued program parameter.
840 set_program_uniform_matrix(GLcontext
*ctx
, struct gl_program
*program
,
841 GLuint index
, GLuint offset
,
842 GLuint count
, GLuint rows
, GLuint cols
,
843 GLboolean transpose
, const GLfloat
*values
)
845 GLuint mat
, row
, col
;
847 const struct gl_program_parameter
* param
= &program
->Parameters
->Parameters
[index
];
848 const GLuint slots
= (param
->Size
+ 3) / 4;
849 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
852 /* check that the number of rows, columns is correct */
853 get_matrix_dims(param
->DataType
, &nr
, &nc
);
854 if (rows
!= nr
|| cols
!= nc
) {
855 _mesa_error(ctx
, GL_INVALID_OPERATION
,
856 "glUniformMatrix(matrix size mismatch)");
860 if ((GLint
) param
->Size
<= typeSize
) {
861 /* non-array: count must be at most one; count == 0 is handled by the loop below */
863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
864 "glUniformMatrix(uniform is not an array)");
870 * Note: the _columns_ of a matrix are stored in program registers, not
871 * the rows. So, the loops below look a little funny.
872 * XXX could optimize this a bit...
875 /* loop over matrices */
876 for (mat
= 0; mat
< count
; mat
++) {
879 for (col
= 0; col
< cols
; col
++) {
881 if (offset
>= slots
) {
882 /* Ignore writes beyond the end of (the used part of) an array */
885 v
= program
->Parameters
->ParameterValues
[index
+ offset
];
886 for (row
= 0; row
< rows
; row
++) {
888 v
[row
] = values
[src
+ row
* cols
+ col
];
891 v
[row
] = values
[src
+ col
* rows
+ row
];
898 src
+= rows
* cols
; /* next matrix */
904 * Called by glUniformMatrix*() functions.
905 * Note: cols=2, rows=4 ==> array[2] of vec4
908 _mesa_uniform_matrix(GLcontext
*ctx
, GLint cols
, GLint rows
,
909 GLint location
, GLsizei count
,
910 GLboolean transpose
, const GLfloat
*values
)
912 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
913 struct gl_uniform
*uniform
;
916 if (!shProg
|| !shProg
->LinkStatus
) {
917 _mesa_error(ctx
, GL_INVALID_OPERATION
,
918 "glUniformMatrix(program not linked)");
923 return; /* The standard specifies this as a no-op */
926 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
930 split_location_offset(&location
, &offset
);
932 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
933 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
936 if (values
== NULL
) {
937 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
941 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
943 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
945 if (shProg
->VertexProgram
) {
946 /* convert uniform location to program parameter index */
947 GLint index
= uniform
->VertPos
;
949 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
951 count
, rows
, cols
, transpose
, values
);
955 if (shProg
->FragmentProgram
) {
956 /* convert uniform location to program parameter index */
957 GLint index
= uniform
->FragPos
;
959 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
961 count
, rows
, cols
, transpose
, values
);
965 uniform
->Initialized
= GL_TRUE
;
970 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
972 GET_CURRENT_CONTEXT(ctx
);
973 _mesa_uniform(ctx
, location
, 1, &v0
, GL_FLOAT
);
977 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
979 GET_CURRENT_CONTEXT(ctx
);
983 _mesa_uniform(ctx
, location
, 1, v
, GL_FLOAT_VEC2
);
987 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
989 GET_CURRENT_CONTEXT(ctx
);
994 _mesa_uniform(ctx
, location
, 1, v
, GL_FLOAT_VEC3
);
998 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1001 GET_CURRENT_CONTEXT(ctx
);
1007 _mesa_uniform(ctx
, location
, 1, v
, GL_FLOAT_VEC4
);
1011 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1013 GET_CURRENT_CONTEXT(ctx
);
1014 _mesa_uniform(ctx
, location
, 1, &v0
, GL_INT
);
1018 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1020 GET_CURRENT_CONTEXT(ctx
);
1024 _mesa_uniform(ctx
, location
, 1, v
, GL_INT_VEC2
);
1028 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1030 GET_CURRENT_CONTEXT(ctx
);
1035 _mesa_uniform(ctx
, location
, 1, v
, GL_INT_VEC3
);
1039 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1041 GET_CURRENT_CONTEXT(ctx
);
1047 _mesa_uniform(ctx
, location
, 1, v
, GL_INT_VEC4
);
1051 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1053 GET_CURRENT_CONTEXT(ctx
);
1054 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT
);
1058 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1060 GET_CURRENT_CONTEXT(ctx
);
1061 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT_VEC2
);
1065 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1067 GET_CURRENT_CONTEXT(ctx
);
1068 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT_VEC3
);
1072 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1074 GET_CURRENT_CONTEXT(ctx
);
1075 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT_VEC4
);
1079 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1081 GET_CURRENT_CONTEXT(ctx
);
1082 _mesa_uniform(ctx
, location
, count
, value
, GL_INT
);
1086 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1088 GET_CURRENT_CONTEXT(ctx
);
1089 _mesa_uniform(ctx
, location
, count
, value
, GL_INT_VEC2
);
1093 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1095 GET_CURRENT_CONTEXT(ctx
);
1096 _mesa_uniform(ctx
, location
, count
, value
, GL_INT_VEC3
);
1100 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1102 GET_CURRENT_CONTEXT(ctx
);
1103 _mesa_uniform(ctx
, location
, count
, value
, GL_INT_VEC4
);
1107 /** OpenGL 3.0 GLuint-valued functions **/
1109 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1111 GET_CURRENT_CONTEXT(ctx
);
1112 _mesa_uniform(ctx
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1116 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1118 GET_CURRENT_CONTEXT(ctx
);
1122 _mesa_uniform(ctx
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1126 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1128 GET_CURRENT_CONTEXT(ctx
);
1133 _mesa_uniform(ctx
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1137 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1139 GET_CURRENT_CONTEXT(ctx
);
1145 _mesa_uniform(ctx
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1149 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1151 GET_CURRENT_CONTEXT(ctx
);
1152 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT
);
1156 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1158 GET_CURRENT_CONTEXT(ctx
);
1159 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1163 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1165 GET_CURRENT_CONTEXT(ctx
);
1166 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1170 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1172 GET_CURRENT_CONTEXT(ctx
);
1173 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1179 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1180 const GLfloat
* value
)
1182 GET_CURRENT_CONTEXT(ctx
);
1183 _mesa_uniform_matrix(ctx
, 2, 2, location
, count
, transpose
, value
);
1187 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1188 const GLfloat
* value
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 _mesa_uniform_matrix(ctx
, 3, 3, location
, count
, transpose
, value
);
1195 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1196 const GLfloat
* value
)
1198 GET_CURRENT_CONTEXT(ctx
);
1199 _mesa_uniform_matrix(ctx
, 4, 4, location
, count
, transpose
, value
);
1204 * Non-square UniformMatrix are OpenGL 2.1
1207 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1208 const GLfloat
*value
)
1210 GET_CURRENT_CONTEXT(ctx
);
1211 _mesa_uniform_matrix(ctx
, 2, 3, location
, count
, transpose
, value
);
1215 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1216 const GLfloat
*value
)
1218 GET_CURRENT_CONTEXT(ctx
);
1219 _mesa_uniform_matrix(ctx
, 3, 2, location
, count
, transpose
, value
);
1223 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1224 const GLfloat
*value
)
1226 GET_CURRENT_CONTEXT(ctx
);
1227 _mesa_uniform_matrix(ctx
, 2, 4, location
, count
, transpose
, value
);
1231 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1232 const GLfloat
*value
)
1234 GET_CURRENT_CONTEXT(ctx
);
1235 _mesa_uniform_matrix(ctx
, 4, 2, location
, count
, transpose
, value
);
1239 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1240 const GLfloat
*value
)
1242 GET_CURRENT_CONTEXT(ctx
);
1243 _mesa_uniform_matrix(ctx
, 3, 4, location
, count
, transpose
, value
);
1247 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1248 const GLfloat
*value
)
1250 GET_CURRENT_CONTEXT(ctx
);
1251 _mesa_uniform_matrix(ctx
, 4, 3, location
, count
, transpose
, value
);
1256 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1258 GET_CURRENT_CONTEXT(ctx
);
1259 _mesa_get_uniformfv(ctx
, program
, location
, params
);
1264 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1266 GET_CURRENT_CONTEXT(ctx
);
1267 _mesa_get_uniformiv(ctx
, program
, location
, params
);
1272 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1274 GET_CURRENT_CONTEXT(ctx
);
1275 return _mesa_get_uniform_location(ctx
, programObj
, name
);
1280 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1281 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1282 GLenum
* type
, GLcharARB
* name
)
1284 GET_CURRENT_CONTEXT(ctx
);
1285 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1291 * Plug in shader uniform-related functions into API dispatch table.
1294 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1296 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1297 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1298 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1299 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1300 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1301 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1302 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1303 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1304 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1305 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1306 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1307 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1308 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1309 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1310 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1311 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1312 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1313 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1314 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1316 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1317 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1318 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1319 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1322 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1323 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1324 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1325 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1326 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1327 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1330 /* XXX finish dispatch */
1331 (void) _mesa_Uniform1ui
;
1332 (void) _mesa_Uniform2ui
;
1333 (void) _mesa_Uniform3ui
;
1334 (void) _mesa_Uniform4ui
;
1335 (void) _mesa_Uniform1uiv
;
1336 (void) _mesa_Uniform2uiv
;
1337 (void) _mesa_Uniform3uiv
;
1338 (void) _mesa_Uniform4uiv
;