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.
6 * Copyright © 2010 Intel Corporation
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 * Functions related to GLSL uniform variables.
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/dispatch.h"
42 #include "main/image.h"
43 #include "main/mfeatures.h"
44 #include "main/mtypes.h"
45 #include "main/shaderapi.h"
46 #include "main/shaderobj.h"
47 #include "main/uniforms.h"
48 #include "program/prog_parameter.h"
49 #include "program/prog_statevars.h"
50 #include "program/prog_uniform.h"
51 #include "program/prog_instruction.h"
55 base_uniform_type(GLenum type
)
58 #if 0 /* not needed, for now */
71 case GL_UNSIGNED_INT_VEC2
:
72 case GL_UNSIGNED_INT_VEC3
:
73 case GL_UNSIGNED_INT_VEC4
:
74 return GL_UNSIGNED_INT
;
81 _mesa_problem(NULL
, "Invalid type in base_uniform_type()");
88 is_boolean_type(GLenum type
)
103 is_sampler_type(GLenum type
)
109 case GL_SAMPLER_CUBE
:
110 case GL_SAMPLER_1D_SHADOW
:
111 case GL_SAMPLER_2D_SHADOW
:
112 case GL_SAMPLER_CUBE_SHADOW
:
113 case GL_SAMPLER_2D_RECT_ARB
:
114 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
115 case GL_SAMPLER_1D_ARRAY_EXT
:
116 case GL_SAMPLER_2D_ARRAY_EXT
:
117 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
118 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
119 case GL_SAMPLER_CUBE_MAP_ARRAY
:
120 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW
:
121 case GL_SAMPLER_BUFFER
:
130 * Given a uniform index, return the vertex/geometry/fragment program
131 * that has that parameter, plus the position of the parameter in the
132 * parameter/constant buffer.
133 * \param shProg the shader program
134 * \param index the uniform index in [0, NumUniforms-1]
135 * \param progOut returns containing program
136 * \param posOut returns position of the uniform in the param/const buffer
137 * \return GL_TRUE for success, GL_FALSE for invalid index
140 find_uniform_parameter_pos(struct gl_shader_program
*shProg
, GLint index
,
141 struct gl_program
**progOut
, GLint
*posOut
)
143 struct gl_program
*prog
= NULL
;
146 if (!shProg
->Uniforms
||
148 index
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
152 pos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
154 prog
= &shProg
->VertexProgram
->Base
;
157 pos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
159 prog
= &shProg
->FragmentProgram
->Base
;
162 pos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
164 prog
= &shProg
->GeometryProgram
->Base
;
169 if (!prog
|| pos
< 0)
170 return GL_FALSE
; /* should really never happen */
180 * Return pointer to a gl_program_parameter which corresponds to a uniform.
181 * \param shProg the shader program
182 * \param index the uniform index in [0, NumUniforms-1]
183 * \return gl_program_parameter point or NULL if index is invalid
185 static const struct gl_program_parameter
*
186 get_uniform_parameter(struct gl_shader_program
*shProg
, GLint index
)
188 struct gl_program
*prog
;
191 if (find_uniform_parameter_pos(shProg
, index
, &prog
, &progPos
))
192 return &prog
->Parameters
->Parameters
[progPos
];
199 * Called by glGetActiveUniform().
202 _mesa_get_active_uniform(struct gl_context
*ctx
, GLuint program
, GLuint index
,
203 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
204 GLenum
*type
, GLchar
*nameOut
)
206 struct gl_shader_program
*shProg
=
207 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
208 const struct gl_program_parameter
*param
;
213 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
214 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
218 param
= get_uniform_parameter(shProg
, index
);
223 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
227 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
228 if ((GLint
) param
->Size
> typeSize
) {
230 * Array elements are placed on vector[4] boundaries so they're
231 * a multiple of four floats. We round typeSize up to next multiple
232 * of four to get the right size below.
234 typeSize
= (typeSize
+ 3) & ~3;
236 /* Note that the returned size is in units of the <type>, not bytes */
237 *size
= param
->Size
/ typeSize
;
241 *type
= param
->DataType
;
247 get_vector_elements(GLenum type
)
253 case GL_UNSIGNED_INT
:
254 default: /* Catch all the various sampler types. */
260 case GL_UNSIGNED_INT_VEC2
:
266 case GL_UNSIGNED_INT_VEC3
:
272 case GL_UNSIGNED_INT_VEC4
:
278 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
284 case GL_FLOAT_MAT2x3
:
288 case GL_FLOAT_MAT2x4
:
296 case GL_FLOAT_MAT3x2
:
300 case GL_FLOAT_MAT3x4
:
308 case GL_FLOAT_MAT4x2
:
312 case GL_FLOAT_MAT4x3
:
323 * Determine the number of rows and columns occupied by a uniform
324 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
325 * the number of rows = 1 and cols = number of elements in the vector.
328 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
329 GLint
*rows
, GLint
*cols
)
331 get_matrix_dims(p
->DataType
, rows
, cols
);
332 if (*rows
== 0 && *cols
== 0) {
333 /* not a matrix type, probably a float or vector */
335 *cols
= get_vector_elements(p
->DataType
);
341 * GLSL uniform arrays and structs require special handling.
343 * The GL_ARB_shader_objects spec says that if you use
344 * glGetUniformLocation to get the location of an array, you CANNOT
345 * access other elements of the array by adding an offset to the
346 * returned location. For example, you must call
347 * glGetUniformLocation("foo[16]") if you want to set the 16th element
348 * of the array with glUniform().
350 * HOWEVER, some other OpenGL drivers allow accessing array elements
351 * by adding an offset to the returned array location. And some apps
352 * seem to depend on that behaviour.
354 * Mesa's gl_uniform_list doesn't directly support this since each
355 * entry in the list describes one uniform variable, not one uniform
356 * element. We could insert dummy entries in the list for each array
357 * element after [0] but that causes complications elsewhere.
359 * We solve this problem by encoding two values in the location that's
360 * returned by glGetUniformLocation():
361 * a) index into gl_uniform_list::Uniforms[] for the uniform
362 * b) an array/field offset (0 for simple types)
364 * These two values are encoded in the high and low halves of a GLint.
365 * By putting the uniform number in the high part and the offset in the
366 * low part, we can support the unofficial ability to index into arrays
367 * by adding offsets to the location value.
370 merge_location_offset(GLint
*location
, GLint offset
)
372 *location
= (*location
<< 16) | offset
;
377 * Separate the uniform location and parameter offset. See above.
380 split_location_offset(GLint
*location
, GLint
*offset
)
382 *offset
= *location
& 0xffff;
383 *location
= *location
>> 16;
389 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
392 get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
393 GLsizei bufSize
, GLenum returnType
, GLvoid
*paramsOut
)
395 struct gl_shader_program
*shProg
=
396 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
397 struct gl_program
*prog
;
398 GLint paramPos
, offset
;
403 split_location_offset(&location
, &offset
);
405 if (!find_uniform_parameter_pos(shProg
, location
, &prog
, ¶mPos
)) {
406 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
409 const struct gl_program_parameter
*p
=
410 &prog
->Parameters
->Parameters
[paramPos
];
411 GLint rows
, cols
, i
, j
, k
;
414 get_uniform_rows_cols(p
, &rows
, &cols
);
416 numBytes
= rows
* cols
* _mesa_sizeof_type(returnType
);
417 if (bufSize
< numBytes
) {
418 _mesa_error( ctx
, GL_INVALID_OPERATION
,
419 "glGetnUniformfvARB(out of bounds: bufSize is %d,"
420 " but %d bytes are required)", bufSize
, numBytes
);
424 switch (returnType
) {
427 GLfloat
*params
= (GLfloat
*) paramsOut
;
429 for (i
= 0; i
< rows
; i
++) {
430 const int base
= paramPos
+ offset
+ i
;
431 for (j
= 0; j
< cols
; j
++ ) {
432 params
[k
++] = prog
->Parameters
->ParameterValues
[base
][j
];
439 GLfloat
*params
= (GLfloat
*) paramsOut
;
441 for (i
= 0; i
< rows
; i
++) {
442 const int base
= paramPos
+ offset
+ i
;
443 for (j
= 0; j
< cols
; j
++ ) {
444 params
[k
++] = (GLdouble
)
445 prog
->Parameters
->ParameterValues
[base
][j
];
452 GLint
*params
= (GLint
*) paramsOut
;
454 for (i
= 0; i
< rows
; i
++) {
455 const int base
= paramPos
+ offset
+ i
;
456 for (j
= 0; j
< cols
; j
++ ) {
457 params
[k
++] = (GLint
)
458 prog
->Parameters
->ParameterValues
[base
][j
];
463 case GL_UNSIGNED_INT
:
465 GLuint
*params
= (GLuint
*) paramsOut
;
467 for (i
= 0; i
< rows
; i
++) {
468 const int base
= paramPos
+ offset
+ i
;
469 for (j
= 0; j
< cols
; j
++ ) {
470 params
[k
++] = (GLuint
)
471 prog
->Parameters
->ParameterValues
[base
][j
];
477 _mesa_problem(ctx
, "bad returnType in get_uniform()");
484 * Called via glGetUniformLocation().
486 * The return value will encode two values, the uniform location and an
487 * offset (used for arrays, structs).
490 _mesa_get_uniform_location(struct gl_context
*ctx
,
491 struct gl_shader_program
*shProg
,
494 GLint offset
= 0, location
= -1;
496 if (shProg
->LinkStatus
== GL_FALSE
) {
497 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
501 /* XXX we should return -1 if the uniform was declared, but not
505 /* XXX we need to be able to parse uniform names for structs and arrays
512 /* handle 1-dimension arrays here... */
513 char *c
= strchr(name
, '[');
515 /* truncate name at [ */
516 const GLint len
= c
- name
;
517 GLchar
*newName
= malloc(len
+ 1);
519 return -1; /* out of mem */
520 memcpy(newName
, name
, len
);
523 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
525 const GLint element
= atoi(c
+ 1);
527 /* get type of the uniform array element */
528 const struct gl_program_parameter
*p
=
529 get_uniform_parameter(shProg
, location
);
532 get_matrix_dims(p
->DataType
, &rows
, &cols
);
535 offset
= element
* rows
;
545 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
549 merge_location_offset(&location
, offset
);
558 * Update the vertex/fragment program's TexturesUsed array.
560 * This needs to be called after glUniform(set sampler var) is called.
561 * A call to glUniform(samplerVar, value) causes a sampler to point to a
562 * particular texture unit. We know the sampler's texture target
563 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
564 * set by glUniform() calls.
566 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
567 * information to update the prog->TexturesUsed[] values.
568 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
569 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
570 * We'll use that info for state validation before rendering.
573 _mesa_update_shader_textures_used(struct gl_program
*prog
)
577 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
579 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
580 if (prog
->SamplersUsed
& (1 << s
)) {
581 GLuint unit
= prog
->SamplerUnits
[s
];
582 GLuint tgt
= prog
->SamplerTargets
[s
];
583 assert(unit
< Elements(prog
->TexturesUsed
));
584 assert(tgt
< NUM_TEXTURE_TARGETS
);
585 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
592 * Check if the type given by userType is allowed to set a uniform of the
593 * target type. Generally, equivalence is required, but setting Boolean
594 * uniforms can be done with glUniformiv or glUniformfv.
597 compatible_types(GLenum userType
, GLenum targetType
)
599 if (userType
== targetType
)
602 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
603 userType
== GL_UNSIGNED_INT
||
607 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
608 userType
== GL_UNSIGNED_INT_VEC2
||
609 userType
== GL_INT_VEC2
))
612 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
613 userType
== GL_UNSIGNED_INT_VEC3
||
614 userType
== GL_INT_VEC3
))
617 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
618 userType
== GL_UNSIGNED_INT_VEC4
||
619 userType
== GL_INT_VEC4
))
622 if (is_sampler_type(targetType
) && userType
== GL_INT
)
630 * Set the value of a program's uniform variable.
631 * \param program the program whose uniform to update
632 * \param index the index of the program parameter for the uniform
633 * \param offset additional parameter slot offset (for arrays)
634 * \param type the incoming datatype of 'values'
635 * \param count the number of uniforms to set
636 * \param elems number of elements per uniform (1, 2, 3 or 4)
637 * \param values the new values, of datatype 'type'
640 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
641 GLint index
, GLint offset
,
642 GLenum type
, GLsizei count
, GLint elems
,
645 const struct gl_program_parameter
*param
=
646 &program
->Parameters
->Parameters
[index
];
652 if (!compatible_types(type
, param
->DataType
)) {
653 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
657 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
662 if (param
->Type
== PROGRAM_SAMPLER
) {
663 /* This controls which texture unit which is used by a sampler */
664 GLboolean changed
= GL_FALSE
;
667 /* this should have been caught by the compatible_types() check */
668 ASSERT(type
== GL_INT
);
670 /* loop over number of samplers to change */
671 for (i
= 0; i
< count
; i
++) {
672 GLuint sampler
= (GLuint
)
673 program
->Parameters
->ParameterValues
[index
+ offset
+ i
][0];
674 GLuint texUnit
= ((GLuint
*) values
)[i
];
676 /* check that the sampler (tex unit index) is legal */
677 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
678 _mesa_error(ctx
, GL_INVALID_VALUE
,
679 "glUniform1(invalid sampler/tex unit index for '%s')",
684 /* This maps a sampler to a texture unit: */
685 if (sampler
< MAX_SAMPLERS
) {
687 printf("Set program %p sampler %d '%s' to unit %u\n",
688 program
, sampler
, param
->Name
, texUnit
);
690 if (program
->SamplerUnits
[sampler
] != texUnit
) {
691 program
->SamplerUnits
[sampler
] = texUnit
;
698 /* When a sampler's value changes it usually requires rewriting
699 * a GPU program's TEX instructions since there may not be a
700 * sampler->texture lookup table. We signal this with the
701 * ProgramStringNotify() callback.
703 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
704 _mesa_update_shader_textures_used(program
);
705 /* Do we need to care about the return value here?
706 * This should not be the first time the driver was notified of
709 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
713 /* ordinary uniform variable */
714 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
715 const GLenum basicType
= base_uniform_type(type
);
716 const GLint slots
= (param
->Size
+ 3) / 4;
717 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
720 if ((GLint
) param
->Size
> typeSize
) {
722 /* we'll ignore extra data below */
725 /* non-array: count must be at most one; count == 0 is handled
729 _mesa_error(ctx
, GL_INVALID_OPERATION
,
730 "glUniform(uniform '%s' is not an array)",
736 /* loop over number of array elements */
737 for (k
= 0; k
< count
; k
++) {
740 if (offset
+ k
>= slots
) {
741 /* Extra array data is ignored */
745 /* uniformVal (the destination) is always float[4] */
746 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
748 if (basicType
== GL_INT
) {
749 /* convert user's ints to floats */
750 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
751 for (i
= 0; i
< elems
; i
++) {
752 uniformVal
[i
] = (GLfloat
) iValues
[i
];
755 else if (basicType
== GL_UNSIGNED_INT
) {
756 /* convert user's uints to floats */
757 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
758 for (i
= 0; i
< elems
; i
++) {
759 uniformVal
[i
] = (GLfloat
) iValues
[i
];
763 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
764 assert(basicType
== GL_FLOAT
);
765 for (i
= 0; i
< elems
; i
++) {
766 uniformVal
[i
] = fValues
[i
];
770 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
772 for (i
= 0; i
< elems
; i
++) {
773 uniformVal
[i
] = uniformVal
[i
] ? 1.0f
: 0.0f
;
782 * Called via glUniform*() functions.
785 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
786 GLint location
, GLsizei count
,
787 const GLvoid
*values
, GLenum type
)
789 struct gl_uniform
*uniform
;
792 ASSERT_OUTSIDE_BEGIN_END(ctx
);
794 if (!shProg
|| !shProg
->LinkStatus
) {
795 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
800 return; /* The standard specifies this as a no-op */
803 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
808 split_location_offset(&location
, &offset
);
810 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
811 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
816 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
820 elems
= _mesa_sizeof_glsl_type(type
);
822 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
824 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
826 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
827 const GLenum basicType
= base_uniform_type(type
);
829 printf("Mesa: set program %u uniform %s (loc %d) to: ",
830 shProg
->Name
, uniform
->Name
, location
);
831 if (basicType
== GL_INT
) {
832 const GLint
*v
= (const GLint
*) values
;
833 for (i
= 0; i
< count
* elems
; i
++) {
837 else if (basicType
== GL_UNSIGNED_INT
) {
838 const GLuint
*v
= (const GLuint
*) values
;
839 for (i
= 0; i
< count
* elems
; i
++) {
844 const GLfloat
*v
= (const GLfloat
*) values
;
845 assert(basicType
== GL_FLOAT
);
846 for (i
= 0; i
< count
* elems
; i
++) {
853 /* A uniform var may be used by both a vertex shader and a fragment
854 * shader. We may need to update one or both shader's uniform here:
856 if (shProg
->VertexProgram
) {
857 /* convert uniform location to program parameter index */
858 GLint index
= uniform
->VertPos
;
860 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
861 index
, offset
, type
, count
, elems
, values
);
865 if (shProg
->FragmentProgram
) {
866 /* convert uniform location to program parameter index */
867 GLint index
= uniform
->FragPos
;
869 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
870 index
, offset
, type
, count
, elems
, values
);
874 if (shProg
->GeometryProgram
) {
875 /* convert uniform location to program parameter index */
876 GLint index
= uniform
->GeomPos
;
878 set_program_uniform(ctx
, &shProg
->GeometryProgram
->Base
,
879 index
, offset
, type
, count
, elems
, values
);
883 uniform
->Initialized
= GL_TRUE
;
888 * Set a matrix-valued program parameter.
891 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
892 GLuint index
, GLuint offset
,
893 GLuint count
, GLuint rows
, GLuint cols
,
894 GLboolean transpose
, const GLfloat
*values
)
896 GLuint mat
, row
, col
;
898 const struct gl_program_parameter
*param
=
899 &program
->Parameters
->Parameters
[index
];
900 const GLuint slots
= (param
->Size
+ 3) / 4;
901 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
904 /* check that the number of rows, columns is correct */
905 get_matrix_dims(param
->DataType
, &nr
, &nc
);
906 if (rows
!= nr
|| cols
!= nc
) {
907 _mesa_error(ctx
, GL_INVALID_OPERATION
,
908 "glUniformMatrix(matrix size mismatch)");
912 if ((GLint
) param
->Size
<= typeSize
) {
913 /* non-array: count must be at most one; count == 0 is handled
917 _mesa_error(ctx
, GL_INVALID_OPERATION
,
918 "glUniformMatrix(uniform is not an array)");
924 * Note: the _columns_ of a matrix are stored in program registers, not
925 * the rows. So, the loops below look a little funny.
926 * XXX could optimize this a bit...
929 /* loop over matrices */
930 for (mat
= 0; mat
< count
; mat
++) {
933 for (col
= 0; col
< cols
; col
++) {
935 if (offset
>= slots
) {
936 /* Ignore writes beyond the end of (the used part of) an array */
939 v
= program
->Parameters
->ParameterValues
[index
+ offset
];
940 for (row
= 0; row
< rows
; row
++) {
942 v
[row
] = values
[src
+ row
* cols
+ col
];
945 v
[row
] = values
[src
+ col
* rows
+ row
];
952 src
+= rows
* cols
; /* next matrix */
958 * Called by glUniformMatrix*() functions.
959 * Note: cols=2, rows=4 ==> array[2] of vec4
962 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
963 GLint cols
, GLint rows
,
964 GLint location
, GLsizei count
,
965 GLboolean transpose
, const GLfloat
*values
)
967 struct gl_uniform
*uniform
;
970 ASSERT_OUTSIDE_BEGIN_END(ctx
);
972 if (!shProg
|| !shProg
->LinkStatus
) {
973 _mesa_error(ctx
, GL_INVALID_OPERATION
,
974 "glUniformMatrix(program not linked)");
979 return; /* The standard specifies this as a no-op */
982 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
986 split_location_offset(&location
, &offset
);
988 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
989 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
992 if (values
== NULL
) {
993 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
997 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
999 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1001 if (shProg
->VertexProgram
) {
1002 /* convert uniform location to program parameter index */
1003 GLint index
= uniform
->VertPos
;
1005 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
1007 count
, rows
, cols
, transpose
, values
);
1011 if (shProg
->FragmentProgram
) {
1012 /* convert uniform location to program parameter index */
1013 GLint index
= uniform
->FragPos
;
1015 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
1017 count
, rows
, cols
, transpose
, values
);
1021 if (shProg
->GeometryProgram
) {
1022 /* convert uniform location to program parameter index */
1023 GLint index
= uniform
->GeomPos
;
1025 set_program_uniform_matrix(ctx
, &shProg
->GeometryProgram
->Base
,
1027 count
, rows
, cols
, transpose
, values
);
1031 uniform
->Initialized
= GL_TRUE
;
1036 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1038 GET_CURRENT_CONTEXT(ctx
);
1039 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1043 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1045 GET_CURRENT_CONTEXT(ctx
);
1049 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1053 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1055 GET_CURRENT_CONTEXT(ctx
);
1060 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1064 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1067 GET_CURRENT_CONTEXT(ctx
);
1073 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1077 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1079 GET_CURRENT_CONTEXT(ctx
);
1080 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1084 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1086 GET_CURRENT_CONTEXT(ctx
);
1090 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1094 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1096 GET_CURRENT_CONTEXT(ctx
);
1101 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1105 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1107 GET_CURRENT_CONTEXT(ctx
);
1113 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1117 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1119 GET_CURRENT_CONTEXT(ctx
);
1120 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1124 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1126 GET_CURRENT_CONTEXT(ctx
);
1127 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1131 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1133 GET_CURRENT_CONTEXT(ctx
);
1134 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1138 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1140 GET_CURRENT_CONTEXT(ctx
);
1141 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1145 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1147 GET_CURRENT_CONTEXT(ctx
);
1148 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1152 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1154 GET_CURRENT_CONTEXT(ctx
);
1155 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1159 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1161 GET_CURRENT_CONTEXT(ctx
);
1162 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1166 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1168 GET_CURRENT_CONTEXT(ctx
);
1169 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1173 /** OpenGL 3.0 GLuint-valued functions **/
1175 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1177 GET_CURRENT_CONTEXT(ctx
);
1178 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1182 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1184 GET_CURRENT_CONTEXT(ctx
);
1188 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1192 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1194 GET_CURRENT_CONTEXT(ctx
);
1199 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1203 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1205 GET_CURRENT_CONTEXT(ctx
);
1211 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1215 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1217 GET_CURRENT_CONTEXT(ctx
);
1218 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1222 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1224 GET_CURRENT_CONTEXT(ctx
);
1225 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1229 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1231 GET_CURRENT_CONTEXT(ctx
);
1232 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1236 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1238 GET_CURRENT_CONTEXT(ctx
);
1239 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1245 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1246 const GLfloat
* value
)
1248 GET_CURRENT_CONTEXT(ctx
);
1249 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1250 2, 2, location
, count
, transpose
, value
);
1254 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1255 const GLfloat
* value
)
1257 GET_CURRENT_CONTEXT(ctx
);
1258 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1259 3, 3, location
, count
, transpose
, value
);
1263 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1264 const GLfloat
* value
)
1266 GET_CURRENT_CONTEXT(ctx
);
1267 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1268 4, 4, location
, count
, transpose
, value
);
1273 * Non-square UniformMatrix are OpenGL 2.1
1276 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1277 const GLfloat
*value
)
1279 GET_CURRENT_CONTEXT(ctx
);
1280 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1281 2, 3, location
, count
, transpose
, value
);
1285 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1286 const GLfloat
*value
)
1288 GET_CURRENT_CONTEXT(ctx
);
1289 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1290 3, 2, location
, count
, transpose
, value
);
1294 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1295 const GLfloat
*value
)
1297 GET_CURRENT_CONTEXT(ctx
);
1298 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1299 2, 4, location
, count
, transpose
, value
);
1303 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1304 const GLfloat
*value
)
1306 GET_CURRENT_CONTEXT(ctx
);
1307 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1308 4, 2, location
, count
, transpose
, value
);
1312 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1313 const GLfloat
*value
)
1315 GET_CURRENT_CONTEXT(ctx
);
1316 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1317 3, 4, location
, count
, transpose
, value
);
1321 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1322 const GLfloat
*value
)
1324 GET_CURRENT_CONTEXT(ctx
);
1325 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1326 4, 3, location
, count
, transpose
, value
);
1331 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
1332 GLsizei bufSize
, GLfloat
*params
)
1334 GET_CURRENT_CONTEXT(ctx
);
1335 get_uniform(ctx
, program
, location
, bufSize
, GL_FLOAT
, params
);
1339 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1341 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
1346 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
1347 GLsizei bufSize
, GLint
*params
)
1349 GET_CURRENT_CONTEXT(ctx
);
1350 get_uniform(ctx
, program
, location
, bufSize
, GL_INT
, params
);
1354 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1356 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
1362 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
1363 GLsizei bufSize
, GLuint
*params
)
1365 GET_CURRENT_CONTEXT(ctx
);
1366 get_uniform(ctx
, program
, location
, bufSize
, GL_UNSIGNED_INT
, params
);
1370 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1372 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
1378 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
1379 GLsizei bufSize
, GLdouble
*params
)
1381 GET_CURRENT_CONTEXT(ctx
);
1383 get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
1385 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
1386 "(GL_ARB_gpu_shader_fp64 not implemented)");
1390 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
1392 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
1397 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1399 struct gl_shader_program
*shProg
;
1401 GET_CURRENT_CONTEXT(ctx
);
1403 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1404 "glGetUniformLocation");
1408 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1413 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1414 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1415 GLenum
* type
, GLcharARB
* name
)
1417 GET_CURRENT_CONTEXT(ctx
);
1418 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1424 * Plug in shader uniform-related functions into API dispatch table.
1427 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1430 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1431 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1432 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1433 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1434 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1435 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1436 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1437 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1438 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1439 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1440 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1441 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1442 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1443 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1444 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1445 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1446 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1447 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1448 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1450 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1451 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1452 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1453 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1456 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1457 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1458 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1459 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1460 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1461 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1464 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
1465 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
1466 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
1467 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
1468 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
1469 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
1470 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
1471 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
1472 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
1474 /* GL_ARB_robustness */
1475 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
1476 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
1477 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
1478 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
1480 #endif /* FEATURE_GL */