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/mfeatures.h"
43 #include "main/mtypes.h"
44 #include "main/shaderapi.h"
45 #include "main/shaderobj.h"
46 #include "main/uniforms.h"
47 #include "program/prog_parameter.h"
48 #include "program/prog_statevars.h"
49 #include "program/prog_uniform.h"
50 #include "program/prog_instruction.h"
54 base_uniform_type(GLenum type
)
57 #if 0 /* not needed, for now */
70 case GL_UNSIGNED_INT_VEC2
:
71 case GL_UNSIGNED_INT_VEC3
:
72 case GL_UNSIGNED_INT_VEC4
:
73 return GL_UNSIGNED_INT
;
80 _mesa_problem(NULL
, "Invalid type in base_uniform_type()");
87 is_boolean_type(GLenum type
)
102 is_sampler_type(GLenum type
)
108 case GL_SAMPLER_CUBE
:
109 case GL_SAMPLER_1D_SHADOW
:
110 case GL_SAMPLER_2D_SHADOW
:
111 case GL_SAMPLER_2D_RECT_ARB
:
112 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
113 case GL_SAMPLER_1D_ARRAY_EXT
:
114 case GL_SAMPLER_2D_ARRAY_EXT
:
115 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
116 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
124 static struct gl_program_parameter
*
125 get_uniform_parameter(const struct gl_shader_program
*shProg
, GLuint index
)
127 const struct gl_program
*prog
= NULL
;
130 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
132 prog
= &shProg
->VertexProgram
->Base
;
135 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
137 prog
= &shProg
->FragmentProgram
->Base
;
139 progPos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
141 prog
= &shProg
->GeometryProgram
->Base
;
146 if (!prog
|| progPos
< 0)
147 return NULL
; /* should never happen */
149 return &prog
->Parameters
->Parameters
[progPos
];
154 * Called by glGetActiveUniform().
157 _mesa_get_active_uniform(struct gl_context
*ctx
, GLuint program
, GLuint index
,
158 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
159 GLenum
*type
, GLchar
*nameOut
)
161 const struct gl_shader_program
*shProg
;
162 const struct gl_program
*prog
= NULL
;
163 const struct gl_program_parameter
*param
;
166 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
170 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
171 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
175 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
177 prog
= &shProg
->VertexProgram
->Base
;
180 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
182 prog
= &shProg
->FragmentProgram
->Base
;
184 progPos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
186 prog
= &shProg
->GeometryProgram
->Base
;
191 if (!prog
|| progPos
< 0)
192 return; /* should never happen */
194 ASSERT(progPos
< prog
->Parameters
->NumParameters
);
195 param
= &prog
->Parameters
->Parameters
[progPos
];
198 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
202 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
203 if ((GLint
) param
->Size
> typeSize
) {
205 * Array elements are placed on vector[4] boundaries so they're
206 * a multiple of four floats. We round typeSize up to next multiple
207 * of four to get the right size below.
209 typeSize
= (typeSize
+ 3) & ~3;
211 /* Note that the returned size is in units of the <type>, not bytes */
212 *size
= param
->Size
/ typeSize
;
216 *type
= param
->DataType
;
222 get_vector_elements(GLenum type
)
228 case GL_UNSIGNED_INT
:
229 default: /* Catch all the various sampler types. */
235 case GL_UNSIGNED_INT_VEC2
:
241 case GL_UNSIGNED_INT_VEC3
:
247 case GL_UNSIGNED_INT_VEC4
:
253 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
259 case GL_FLOAT_MAT2x3
:
263 case GL_FLOAT_MAT2x4
:
271 case GL_FLOAT_MAT3x2
:
275 case GL_FLOAT_MAT3x4
:
283 case GL_FLOAT_MAT4x2
:
287 case GL_FLOAT_MAT4x3
:
298 * Determine the number of rows and columns occupied by a uniform
299 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
300 * the number of rows = 1 and cols = number of elements in the vector.
303 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
304 GLint
*rows
, GLint
*cols
)
306 get_matrix_dims(p
->DataType
, rows
, cols
);
307 if (*rows
== 0 && *cols
== 0) {
308 /* not a matrix type, probably a float or vector */
310 *cols
= get_vector_elements(p
->DataType
);
316 * Helper for get_uniform[fi]v() functions.
317 * Given a shader program name and uniform location, return a pointer
318 * to the shader program and return the program parameter position.
321 lookup_uniform_parameter(struct gl_context
*ctx
, GLuint program
, GLint location
,
322 struct gl_program
**progOut
, GLint
*paramPosOut
)
324 struct gl_shader_program
*shProg
325 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniform[if]v");
326 struct gl_program
*prog
= NULL
;
329 /* if shProg is NULL, we'll have already recorded an error */
332 if (!shProg
->Uniforms
||
334 location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
335 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
338 /* OK, find the gl_program and program parameter location */
339 progPos
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
341 prog
= &shProg
->VertexProgram
->Base
;
344 progPos
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
346 prog
= &shProg
->FragmentProgram
->Base
;
348 progPos
= shProg
->Uniforms
->Uniforms
[location
].GeomPos
;
350 prog
= &shProg
->GeometryProgram
->Base
;
358 *paramPosOut
= progPos
;
363 * GLGL uniform arrays and structs require special handling.
365 * The GL_ARB_shader_objects spec says that if you use
366 * glGetUniformLocation to get the location of an array, you CANNOT
367 * access other elements of the array by adding an offset to the
368 * returned location. For example, you must call
369 * glGetUniformLocation("foo[16]") if you want to set the 16th element
370 * of the array with glUniform().
372 * HOWEVER, some other OpenGL drivers allow accessing array elements
373 * by adding an offset to the returned array location. And some apps
374 * seem to depend on that behaviour.
376 * Mesa's gl_uniform_list doesn't directly support this since each
377 * entry in the list describes one uniform variable, not one uniform
378 * element. We could insert dummy entries in the list for each array
379 * element after [0] but that causes complications elsewhere.
381 * We solve this problem by encoding two values in the location that's
382 * returned by glGetUniformLocation():
383 * a) index into gl_uniform_list::Uniforms[] for the uniform
384 * b) an array/field offset (0 for simple types)
386 * These two values are encoded in the high and low halves of a GLint.
387 * By putting the uniform number in the high part and the offset in the
388 * low part, we can support the unofficial ability to index into arrays
389 * by adding offsets to the location value.
392 merge_location_offset(GLint
*location
, GLint offset
)
394 *location
= (*location
<< 16) | offset
;
399 * Separate the uniform location and parameter offset. See above.
402 split_location_offset(GLint
*location
, GLint
*offset
)
404 *offset
= *location
& 0xffff;
405 *location
= *location
>> 16;
411 * Called via glGetUniformfv().
414 _mesa_get_uniformfv(struct gl_context
*ctx
, GLuint program
, GLint location
,
417 struct gl_program
*prog
;
421 split_location_offset(&location
, &offset
);
423 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
426 const struct gl_program_parameter
*p
=
427 &prog
->Parameters
->Parameters
[paramPos
];
428 GLint rows
, cols
, i
, j
, k
;
430 get_uniform_rows_cols(p
, &rows
, &cols
);
433 for (i
= 0; i
< rows
; i
++) {
434 const int base
= paramPos
+ offset
+ i
;
436 for (j
= 0; j
< cols
; j
++ ) {
437 params
[k
++] = prog
->Parameters
->ParameterValues
[base
][j
];
445 * Called via glGetUniformiv().
446 * \sa _mesa_get_uniformfv, only difference is a cast.
449 _mesa_get_uniformiv(struct gl_context
*ctx
, GLuint program
, GLint location
,
452 struct gl_program
*prog
;
456 split_location_offset(&location
, &offset
);
458 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
461 const struct gl_program_parameter
*p
=
462 &prog
->Parameters
->Parameters
[paramPos
];
463 GLint rows
, cols
, i
, j
, k
;
465 get_uniform_rows_cols(p
, &rows
, &cols
);
468 for (i
= 0; i
< rows
; i
++) {
469 const int base
= paramPos
+ offset
+ i
;
471 for (j
= 0; j
< cols
; j
++ ) {
472 params
[k
++] = (GLint
) prog
->Parameters
->ParameterValues
[base
][j
];
480 * Called via glGetUniformuiv().
481 * New in GL_EXT_gpu_shader4, OpenGL 3.0
482 * \sa _mesa_get_uniformfv, only difference is a cast.
485 _mesa_get_uniformuiv(struct gl_context
*ctx
, GLuint program
, GLint location
,
488 struct gl_program
*prog
;
492 split_location_offset(&location
, &offset
);
494 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
497 const struct gl_program_parameter
*p
=
498 &prog
->Parameters
->Parameters
[paramPos
];
499 GLint rows
, cols
, i
, j
, k
;
501 get_uniform_rows_cols(p
, &rows
, &cols
);
504 for (i
= 0; i
< rows
; i
++) {
505 const int base
= paramPos
+ offset
+ i
;
507 for (j
= 0; j
< cols
; j
++ ) {
508 params
[k
++] = (GLuint
) prog
->Parameters
->ParameterValues
[base
][j
];
516 * Called via glGetUniformLocation().
518 * The return value will encode two values, the uniform location and an
519 * offset (used for arrays, structs).
522 _mesa_get_uniform_location(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
525 GLint offset
= 0, location
= -1;
527 if (shProg
->LinkStatus
== GL_FALSE
) {
528 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
532 /* XXX we should return -1 if the uniform was declared, but not
536 /* XXX we need to be able to parse uniform names for structs and arrays
543 /* handle 1-dimension arrays here... */
544 char *c
= strchr(name
, '[');
546 /* truncate name at [ */
547 const GLint len
= c
- name
;
548 GLchar
*newName
= malloc(len
+ 1);
550 return -1; /* out of mem */
551 memcpy(newName
, name
, len
);
554 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
556 const GLint element
= atoi(c
+ 1);
558 /* get type of the uniform array element */
559 struct gl_program_parameter
*p
;
560 p
= get_uniform_parameter(shProg
, location
);
563 get_matrix_dims(p
->DataType
, &rows
, &cols
);
566 offset
= element
* rows
;
576 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
580 merge_location_offset(&location
, offset
);
589 * Update the vertex/fragment program's TexturesUsed array.
591 * This needs to be called after glUniform(set sampler var) is called.
592 * A call to glUniform(samplerVar, value) causes a sampler to point to a
593 * particular texture unit. We know the sampler's texture target
594 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
595 * set by glUniform() calls.
597 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
598 * information to update the prog->TexturesUsed[] values.
599 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
600 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
601 * We'll use that info for state validation before rendering.
604 _mesa_update_shader_textures_used(struct gl_program
*prog
)
608 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
610 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
611 if (prog
->SamplersUsed
& (1 << s
)) {
612 GLuint unit
= prog
->SamplerUnits
[s
];
613 GLuint tgt
= prog
->SamplerTargets
[s
];
614 assert(unit
< MAX_TEXTURE_IMAGE_UNITS
);
615 assert(tgt
< NUM_TEXTURE_TARGETS
);
616 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
623 * Check if the type given by userType is allowed to set a uniform of the
624 * target type. Generally, equivalence is required, but setting Boolean
625 * uniforms can be done with glUniformiv or glUniformfv.
628 compatible_types(GLenum userType
, GLenum targetType
)
630 if (userType
== targetType
)
633 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
634 userType
== GL_UNSIGNED_INT
||
638 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
639 userType
== GL_UNSIGNED_INT_VEC2
||
640 userType
== GL_INT_VEC2
))
643 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
644 userType
== GL_UNSIGNED_INT_VEC3
||
645 userType
== GL_INT_VEC3
))
648 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
649 userType
== GL_UNSIGNED_INT_VEC4
||
650 userType
== GL_INT_VEC4
))
653 if (is_sampler_type(targetType
) && userType
== GL_INT
)
661 * Set the value of a program's uniform variable.
662 * \param program the program whose uniform to update
663 * \param index the index of the program parameter for the uniform
664 * \param offset additional parameter slot offset (for arrays)
665 * \param type the incoming datatype of 'values'
666 * \param count the number of uniforms to set
667 * \param elems number of elements per uniform (1, 2, 3 or 4)
668 * \param values the new values, of datatype 'type'
671 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
672 GLint index
, GLint offset
,
673 GLenum type
, GLsizei count
, GLint elems
,
676 const struct gl_program_parameter
*param
=
677 &program
->Parameters
->Parameters
[index
];
683 if (!compatible_types(type
, param
->DataType
)) {
684 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
688 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
693 if (param
->Type
== PROGRAM_SAMPLER
) {
694 /* This controls which texture unit which is used by a sampler */
695 GLboolean changed
= GL_FALSE
;
698 /* this should have been caught by the compatible_types() check */
699 ASSERT(type
== GL_INT
);
701 /* loop over number of samplers to change */
702 for (i
= 0; i
< count
; i
++) {
704 (GLuint
) program
->Parameters
->ParameterValues
[index
+ offset
+ i
][0];
705 GLuint texUnit
= ((GLuint
*) values
)[i
];
707 /* check that the sampler (tex unit index) is legal */
708 if (texUnit
>= ctx
->Const
.MaxTextureImageUnits
) {
709 _mesa_error(ctx
, GL_INVALID_VALUE
,
710 "glUniform1(invalid sampler/tex unit index for '%s')",
715 /* This maps a sampler to a texture unit: */
716 if (sampler
< MAX_SAMPLERS
) {
718 printf("Set program %p sampler %d '%s' to unit %u\n",
719 program
, sampler
, param
->Name
, texUnit
);
721 if (program
->SamplerUnits
[sampler
] != texUnit
) {
722 program
->SamplerUnits
[sampler
] = texUnit
;
729 /* When a sampler's value changes it usually requires rewriting
730 * a GPU program's TEX instructions since there may not be a
731 * sampler->texture lookup table. We signal this with the
732 * ProgramStringNotify() callback.
734 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
735 _mesa_update_shader_textures_used(program
);
736 /* Do we need to care about the return value here?
737 * This should not be the first time the driver was notified of
740 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
744 /* ordinary uniform variable */
745 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
746 const GLenum basicType
= base_uniform_type(type
);
747 const GLint slots
= (param
->Size
+ 3) / 4;
748 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
751 if ((GLint
) param
->Size
> typeSize
) {
753 /* we'll ignore extra data below */
756 /* non-array: count must be at most one; count == 0 is handled by the loop below */
758 _mesa_error(ctx
, GL_INVALID_OPERATION
,
759 "glUniform(uniform '%s' is not an array)",
765 /* loop over number of array elements */
766 for (k
= 0; k
< count
; k
++) {
769 if (offset
+ k
>= slots
) {
770 /* Extra array data is ignored */
774 /* uniformVal (the destination) is always float[4] */
775 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
777 if (basicType
== GL_INT
) {
778 /* convert user's ints to floats */
779 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
780 for (i
= 0; i
< elems
; i
++) {
781 uniformVal
[i
] = (GLfloat
) iValues
[i
];
784 else if (basicType
== GL_UNSIGNED_INT
) {
785 /* convert user's uints to floats */
786 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
787 for (i
= 0; i
< elems
; i
++) {
788 uniformVal
[i
] = (GLfloat
) iValues
[i
];
792 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
793 assert(basicType
== GL_FLOAT
);
794 for (i
= 0; i
< elems
; i
++) {
795 uniformVal
[i
] = fValues
[i
];
799 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
801 for (i
= 0; i
< elems
; i
++) {
802 uniformVal
[i
] = uniformVal
[i
] ? 1.0f
: 0.0f
;
811 * Called via glUniform*() functions.
814 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
815 GLint location
, GLsizei count
,
816 const GLvoid
*values
, GLenum type
)
818 struct gl_uniform
*uniform
;
821 if (!shProg
|| !shProg
->LinkStatus
) {
822 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
827 return; /* The standard specifies this as a no-op */
830 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
835 split_location_offset(&location
, &offset
);
837 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
838 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
843 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
847 elems
= _mesa_sizeof_glsl_type(type
);
849 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
851 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
853 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
854 const GLenum basicType
= base_uniform_type(type
);
856 printf("Mesa: set program %u uniform %s (loc %d) to: ",
857 shProg
->Name
, uniform
->Name
, location
);
858 if (basicType
== GL_INT
) {
859 const GLint
*v
= (const GLint
*) values
;
860 for (i
= 0; i
< count
* elems
; i
++) {
864 else if (basicType
== GL_UNSIGNED_INT
) {
865 const GLuint
*v
= (const GLuint
*) values
;
866 for (i
= 0; i
< count
* elems
; i
++) {
871 const GLfloat
*v
= (const GLfloat
*) values
;
872 assert(basicType
== GL_FLOAT
);
873 for (i
= 0; i
< count
* elems
; i
++) {
880 /* A uniform var may be used by both a vertex shader and a fragment
881 * shader. We may need to update one or both shader's uniform here:
883 if (shProg
->VertexProgram
) {
884 /* convert uniform location to program parameter index */
885 GLint index
= uniform
->VertPos
;
887 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
888 index
, offset
, type
, count
, elems
, values
);
892 if (shProg
->FragmentProgram
) {
893 /* convert uniform location to program parameter index */
894 GLint index
= uniform
->FragPos
;
896 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
897 index
, offset
, type
, count
, elems
, values
);
901 if (shProg
->GeometryProgram
) {
902 /* convert uniform location to program parameter index */
903 GLint index
= uniform
->GeomPos
;
905 set_program_uniform(ctx
, &shProg
->GeometryProgram
->Base
,
906 index
, offset
, type
, count
, elems
, values
);
910 uniform
->Initialized
= GL_TRUE
;
915 * Set a matrix-valued program parameter.
918 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
919 GLuint index
, GLuint offset
,
920 GLuint count
, GLuint rows
, GLuint cols
,
921 GLboolean transpose
, const GLfloat
*values
)
923 GLuint mat
, row
, col
;
925 const struct gl_program_parameter
* param
= &program
->Parameters
->Parameters
[index
];
926 const GLuint slots
= (param
->Size
+ 3) / 4;
927 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
930 /* check that the number of rows, columns is correct */
931 get_matrix_dims(param
->DataType
, &nr
, &nc
);
932 if (rows
!= nr
|| cols
!= nc
) {
933 _mesa_error(ctx
, GL_INVALID_OPERATION
,
934 "glUniformMatrix(matrix size mismatch)");
938 if ((GLint
) param
->Size
<= typeSize
) {
939 /* non-array: count must be at most one; count == 0 is handled by the loop below */
941 _mesa_error(ctx
, GL_INVALID_OPERATION
,
942 "glUniformMatrix(uniform is not an array)");
948 * Note: the _columns_ of a matrix are stored in program registers, not
949 * the rows. So, the loops below look a little funny.
950 * XXX could optimize this a bit...
953 /* loop over matrices */
954 for (mat
= 0; mat
< count
; mat
++) {
957 for (col
= 0; col
< cols
; col
++) {
959 if (offset
>= slots
) {
960 /* Ignore writes beyond the end of (the used part of) an array */
963 v
= program
->Parameters
->ParameterValues
[index
+ offset
];
964 for (row
= 0; row
< rows
; row
++) {
966 v
[row
] = values
[src
+ row
* cols
+ col
];
969 v
[row
] = values
[src
+ col
* rows
+ row
];
976 src
+= rows
* cols
; /* next matrix */
982 * Called by glUniformMatrix*() functions.
983 * Note: cols=2, rows=4 ==> array[2] of vec4
986 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
987 GLint cols
, GLint rows
,
988 GLint location
, GLsizei count
,
989 GLboolean transpose
, const GLfloat
*values
)
991 struct gl_uniform
*uniform
;
994 if (!shProg
|| !shProg
->LinkStatus
) {
995 _mesa_error(ctx
, GL_INVALID_OPERATION
,
996 "glUniformMatrix(program not linked)");
1001 return; /* The standard specifies this as a no-op */
1003 if (location
< -1) {
1004 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
1008 split_location_offset(&location
, &offset
);
1010 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1011 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1014 if (values
== NULL
) {
1015 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1019 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1021 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1023 if (shProg
->VertexProgram
) {
1024 /* convert uniform location to program parameter index */
1025 GLint index
= uniform
->VertPos
;
1027 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
1029 count
, rows
, cols
, transpose
, values
);
1033 if (shProg
->FragmentProgram
) {
1034 /* convert uniform location to program parameter index */
1035 GLint index
= uniform
->FragPos
;
1037 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
1039 count
, rows
, cols
, transpose
, values
);
1043 if (shProg
->GeometryProgram
) {
1044 /* convert uniform location to program parameter index */
1045 GLint index
= uniform
->GeomPos
;
1047 set_program_uniform_matrix(ctx
, &shProg
->GeometryProgram
->Base
,
1049 count
, rows
, cols
, transpose
, values
);
1053 uniform
->Initialized
= GL_TRUE
;
1058 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1060 GET_CURRENT_CONTEXT(ctx
);
1061 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1065 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1067 GET_CURRENT_CONTEXT(ctx
);
1071 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1075 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1077 GET_CURRENT_CONTEXT(ctx
);
1082 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1086 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1089 GET_CURRENT_CONTEXT(ctx
);
1095 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1099 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1101 GET_CURRENT_CONTEXT(ctx
);
1102 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1106 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1108 GET_CURRENT_CONTEXT(ctx
);
1112 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1116 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1118 GET_CURRENT_CONTEXT(ctx
);
1123 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1127 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1129 GET_CURRENT_CONTEXT(ctx
);
1135 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1139 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1141 GET_CURRENT_CONTEXT(ctx
);
1142 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1146 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1148 GET_CURRENT_CONTEXT(ctx
);
1149 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1153 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1155 GET_CURRENT_CONTEXT(ctx
);
1156 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1160 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1162 GET_CURRENT_CONTEXT(ctx
);
1163 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1167 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1169 GET_CURRENT_CONTEXT(ctx
);
1170 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1174 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1181 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1183 GET_CURRENT_CONTEXT(ctx
);
1184 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1188 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1195 /** OpenGL 3.0 GLuint-valued functions **/
1197 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1199 GET_CURRENT_CONTEXT(ctx
);
1200 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1204 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1206 GET_CURRENT_CONTEXT(ctx
);
1210 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1214 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1216 GET_CURRENT_CONTEXT(ctx
);
1221 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1225 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1227 GET_CURRENT_CONTEXT(ctx
);
1233 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1237 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1239 GET_CURRENT_CONTEXT(ctx
);
1240 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1244 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1246 GET_CURRENT_CONTEXT(ctx
);
1247 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1251 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1253 GET_CURRENT_CONTEXT(ctx
);
1254 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1258 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1260 GET_CURRENT_CONTEXT(ctx
);
1261 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1267 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1268 const GLfloat
* value
)
1270 GET_CURRENT_CONTEXT(ctx
);
1271 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1272 2, 2, location
, count
, transpose
, value
);
1276 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1277 const GLfloat
* value
)
1279 GET_CURRENT_CONTEXT(ctx
);
1280 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1281 3, 3, location
, count
, transpose
, value
);
1285 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1286 const GLfloat
* value
)
1288 GET_CURRENT_CONTEXT(ctx
);
1289 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1290 4, 4, location
, count
, transpose
, value
);
1295 * Non-square UniformMatrix are OpenGL 2.1
1298 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1299 const GLfloat
*value
)
1301 GET_CURRENT_CONTEXT(ctx
);
1302 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1303 2, 3, location
, count
, transpose
, value
);
1307 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1308 const GLfloat
*value
)
1310 GET_CURRENT_CONTEXT(ctx
);
1311 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1312 3, 2, location
, count
, transpose
, value
);
1316 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1317 const GLfloat
*value
)
1319 GET_CURRENT_CONTEXT(ctx
);
1320 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1321 2, 4, location
, count
, transpose
, value
);
1325 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1326 const GLfloat
*value
)
1328 GET_CURRENT_CONTEXT(ctx
);
1329 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1330 4, 2, location
, count
, transpose
, value
);
1334 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1335 const GLfloat
*value
)
1337 GET_CURRENT_CONTEXT(ctx
);
1338 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1339 3, 4, location
, count
, transpose
, value
);
1343 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1344 const GLfloat
*value
)
1346 GET_CURRENT_CONTEXT(ctx
);
1347 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1348 4, 3, location
, count
, transpose
, value
);
1353 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1355 GET_CURRENT_CONTEXT(ctx
);
1356 _mesa_get_uniformfv(ctx
, program
, location
, params
);
1361 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1363 GET_CURRENT_CONTEXT(ctx
);
1364 _mesa_get_uniformiv(ctx
, program
, location
, params
);
1370 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1372 GET_CURRENT_CONTEXT(ctx
);
1373 _mesa_get_uniformuiv(ctx
, program
, location
, params
);
1379 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1381 struct gl_shader_program
*shProg
;
1383 GET_CURRENT_CONTEXT(ctx
);
1385 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1386 "glGetUniformLocation");
1390 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1395 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1396 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1397 GLenum
* type
, GLcharARB
* name
)
1399 GET_CURRENT_CONTEXT(ctx
);
1400 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1406 * Plug in shader uniform-related functions into API dispatch table.
1409 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1412 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1413 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1414 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1415 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1416 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1417 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1418 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1419 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1420 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1421 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1422 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1423 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1424 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1425 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1426 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1427 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1428 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1429 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1430 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1432 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1433 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1434 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1435 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1438 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1439 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1440 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1441 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1442 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1443 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1446 /* XXX finish dispatch */
1447 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
1448 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
1449 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
1450 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
1451 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
1452 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
1453 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
1454 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
1455 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
1458 #endif /* FEATURE_GL */