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
;
136 progPos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
138 prog
= &shProg
->GeometryProgram
->Base
;
143 if (!prog
|| progPos
< 0)
144 return NULL
; /* should never happen */
146 return &prog
->Parameters
->Parameters
[progPos
];
151 * Called by glGetActiveUniform().
154 _mesa_get_active_uniform(GLcontext
*ctx
, GLuint program
, GLuint index
,
155 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
156 GLenum
*type
, GLchar
*nameOut
)
158 const struct gl_shader_program
*shProg
;
159 const struct gl_program
*prog
= NULL
;
160 const struct gl_program_parameter
*param
;
163 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
167 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
168 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
172 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
174 prog
= &shProg
->VertexProgram
->Base
;
177 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
179 prog
= &shProg
->FragmentProgram
->Base
;
181 progPos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
183 prog
= &shProg
->GeometryProgram
->Base
;
188 if (!prog
|| progPos
< 0)
189 return; /* should never happen */
191 ASSERT(progPos
< prog
->Parameters
->NumParameters
);
192 param
= &prog
->Parameters
->Parameters
[progPos
];
195 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
199 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
200 if ((GLint
) param
->Size
> typeSize
) {
202 * Array elements are placed on vector[4] boundaries so they're
203 * a multiple of four floats. We round typeSize up to next multiple
204 * of four to get the right size below.
206 typeSize
= (typeSize
+ 3) & ~3;
208 /* Note that the returned size is in units of the <type>, not bytes */
209 *size
= param
->Size
/ typeSize
;
213 *type
= param
->DataType
;
220 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
226 case GL_FLOAT_MAT2x3
:
230 case GL_FLOAT_MAT2x4
:
238 case GL_FLOAT_MAT3x2
:
242 case GL_FLOAT_MAT3x4
:
250 case GL_FLOAT_MAT4x2
:
254 case GL_FLOAT_MAT4x3
:
265 * Determine the number of rows and columns occupied by a uniform
266 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
267 * the number of rows = 1 and cols = number of elements in the vector.
270 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
271 GLint
*rows
, GLint
*cols
)
273 get_matrix_dims(p
->DataType
, rows
, cols
);
274 if (*rows
== 0 && *cols
== 0) {
275 /* not a matrix type, probably a float or vector */
281 *rows
= p
->Size
/ 4 + 1;
282 if (p
->Size
% 4 == 0)
292 * Helper for get_uniform[fi]v() functions.
293 * Given a shader program name and uniform location, return a pointer
294 * to the shader program and return the program parameter position.
297 lookup_uniform_parameter(GLcontext
*ctx
, GLuint program
, GLint location
,
298 struct gl_program
**progOut
, GLint
*paramPosOut
)
300 struct gl_shader_program
*shProg
301 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniform[if]v");
302 struct gl_program
*prog
= NULL
;
305 /* if shProg is NULL, we'll have already recorded an error */
308 if (!shProg
->Uniforms
||
310 location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
311 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
314 /* OK, find the gl_program and program parameter location */
315 progPos
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
317 prog
= &shProg
->VertexProgram
->Base
;
320 progPos
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
322 prog
= &shProg
->FragmentProgram
->Base
;
324 progPos
= shProg
->Uniforms
->Uniforms
[location
].GeomPos
;
326 prog
= &shProg
->GeometryProgram
->Base
;
334 *paramPosOut
= progPos
;
339 * GLGL uniform arrays and structs require special handling.
341 * The GL_ARB_shader_objects spec says that if you use
342 * glGetUniformLocation to get the location of an array, you CANNOT
343 * access other elements of the array by adding an offset to the
344 * returned location. For example, you must call
345 * glGetUniformLocation("foo[16]") if you want to set the 16th element
346 * of the array with glUniform().
348 * HOWEVER, some other OpenGL drivers allow accessing array elements
349 * by adding an offset to the returned array location. And some apps
350 * seem to depend on that behaviour.
352 * Mesa's gl_uniform_list doesn't directly support this since each
353 * entry in the list describes one uniform variable, not one uniform
354 * element. We could insert dummy entries in the list for each array
355 * element after [0] but that causes complications elsewhere.
357 * We solve this problem by encoding two values in the location that's
358 * returned by glGetUniformLocation():
359 * a) index into gl_uniform_list::Uniforms[] for the uniform
360 * b) an array/field offset (0 for simple types)
362 * These two values are encoded in the high and low halves of a GLint.
363 * By putting the uniform number in the high part and the offset in the
364 * low part, we can support the unofficial ability to index into arrays
365 * by adding offsets to the location value.
368 merge_location_offset(GLint
*location
, GLint offset
)
370 *location
= (*location
<< 16) | offset
;
375 * Separate the uniform location and parameter offset. See above.
378 split_location_offset(GLint
*location
, GLint
*offset
)
380 *offset
= *location
& 0xffff;
381 *location
= *location
>> 16;
387 * Called via glGetUniformfv().
390 _mesa_get_uniformfv(GLcontext
*ctx
, GLuint program
, GLint location
,
393 struct gl_program
*prog
;
397 split_location_offset(&location
, &offset
);
399 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
402 const struct gl_program_parameter
*p
=
403 &prog
->Parameters
->Parameters
[paramPos
];
404 GLint rows
, cols
, i
, j
, k
;
406 get_uniform_rows_cols(p
, &rows
, &cols
);
409 for (i
= 0; i
< rows
; i
++) {
410 for (j
= 0; j
< cols
; j
++ ) {
411 params
[k
++] = prog
->Parameters
->ParameterValues
[paramPos
+i
][j
];
419 * Called via glGetUniformiv().
420 * \sa _mesa_get_uniformfv, only difference is a cast.
423 _mesa_get_uniformiv(GLcontext
*ctx
, GLuint program
, GLint location
,
426 struct gl_program
*prog
;
430 split_location_offset(&location
, &offset
);
432 lookup_uniform_parameter(ctx
, program
, location
, &prog
, ¶mPos
);
435 const struct gl_program_parameter
*p
=
436 &prog
->Parameters
->Parameters
[paramPos
];
437 GLint rows
, cols
, i
, j
, k
;
439 get_uniform_rows_cols(p
, &rows
, &cols
);
442 for (i
= 0; i
< rows
; i
++) {
443 for (j
= 0; j
< cols
; j
++ ) {
444 params
[k
++] = (GLint
) prog
->Parameters
->ParameterValues
[paramPos
+i
][j
];
452 * Called via glGetUniformLocation().
454 * The return value will encode two values, the uniform location and an
455 * offset (used for arrays, structs).
458 _mesa_get_uniform_location(GLcontext
*ctx
, struct gl_shader_program
*shProg
,
461 GLint offset
= 0, location
= -1;
463 if (shProg
->LinkStatus
== GL_FALSE
) {
464 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
468 /* XXX we should return -1 if the uniform was declared, but not
472 /* XXX we need to be able to parse uniform names for structs and arrays
479 /* handle 1-dimension arrays here... */
480 char *c
= strchr(name
, '[');
482 /* truncate name at [ */
483 const GLint len
= c
- name
;
484 GLchar
*newName
= malloc(len
+ 1);
486 return -1; /* out of mem */
487 memcpy(newName
, name
, len
);
490 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
492 const GLint element
= atoi(c
+ 1);
494 /* get type of the uniform array element */
495 struct gl_program_parameter
*p
;
496 p
= get_uniform_parameter(shProg
, location
);
499 get_matrix_dims(p
->DataType
, &rows
, &cols
);
502 offset
= element
* rows
;
512 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
516 merge_location_offset(&location
, offset
);
525 * Update the vertex/fragment program's TexturesUsed array.
527 * This needs to be called after glUniform(set sampler var) is called.
528 * A call to glUniform(samplerVar, value) causes a sampler to point to a
529 * particular texture unit. We know the sampler's texture target
530 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
531 * set by glUniform() calls.
533 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
534 * information to update the prog->TexturesUsed[] values.
535 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
536 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
537 * We'll use that info for state validation before rendering.
540 _mesa_update_shader_textures_used(struct gl_program
*prog
)
544 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
546 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
547 if (prog
->SamplersUsed
& (1 << s
)) {
548 GLuint unit
= prog
->SamplerUnits
[s
];
549 GLuint tgt
= prog
->SamplerTargets
[s
];
550 assert(unit
< MAX_TEXTURE_IMAGE_UNITS
);
551 assert(tgt
< NUM_TEXTURE_TARGETS
);
552 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
559 * Check if the type given by userType is allowed to set a uniform of the
560 * target type. Generally, equivalence is required, but setting Boolean
561 * uniforms can be done with glUniformiv or glUniformfv.
564 compatible_types(GLenum userType
, GLenum targetType
)
566 if (userType
== targetType
)
569 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
570 userType
== GL_UNSIGNED_INT
||
574 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
575 userType
== GL_UNSIGNED_INT_VEC2
||
576 userType
== GL_INT_VEC2
))
579 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
580 userType
== GL_UNSIGNED_INT_VEC3
||
581 userType
== GL_INT_VEC3
))
584 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
585 userType
== GL_UNSIGNED_INT_VEC4
||
586 userType
== GL_INT_VEC4
))
589 if (is_sampler_type(targetType
) && userType
== GL_INT
)
597 * Set the value of a program's uniform variable.
598 * \param program the program whose uniform to update
599 * \param index the index of the program parameter for the uniform
600 * \param offset additional parameter slot offset (for arrays)
601 * \param type the incoming datatype of 'values'
602 * \param count the number of uniforms to set
603 * \param elems number of elements per uniform (1, 2, 3 or 4)
604 * \param values the new values, of datatype 'type'
607 set_program_uniform(GLcontext
*ctx
, struct gl_program
*program
,
608 GLint index
, GLint offset
,
609 GLenum type
, GLsizei count
, GLint elems
,
612 const struct gl_program_parameter
*param
=
613 &program
->Parameters
->Parameters
[index
];
619 if (!compatible_types(type
, param
->DataType
)) {
620 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
624 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
629 if (param
->Type
== PROGRAM_SAMPLER
) {
630 /* This controls which texture unit which is used by a sampler */
631 GLboolean changed
= GL_FALSE
;
634 /* this should have been caught by the compatible_types() check */
635 ASSERT(type
== GL_INT
);
637 /* loop over number of samplers to change */
638 for (i
= 0; i
< count
; i
++) {
640 (GLuint
) program
->Parameters
->ParameterValues
[index
+ offset
+ i
][0];
641 GLuint texUnit
= ((GLuint
*) values
)[i
];
643 /* check that the sampler (tex unit index) is legal */
644 if (texUnit
>= ctx
->Const
.MaxTextureImageUnits
) {
645 _mesa_error(ctx
, GL_INVALID_VALUE
,
646 "glUniform1(invalid sampler/tex unit index for '%s')",
651 /* This maps a sampler to a texture unit: */
652 if (sampler
< MAX_SAMPLERS
) {
654 printf("Set program %p sampler %d '%s' to unit %u\n",
655 program
, sampler
, param
->Name
, texUnit
);
657 if (program
->SamplerUnits
[sampler
] != texUnit
) {
658 program
->SamplerUnits
[sampler
] = texUnit
;
665 /* When a sampler's value changes it usually requires rewriting
666 * a GPU program's TEX instructions since there may not be a
667 * sampler->texture lookup table. We signal this with the
668 * ProgramStringNotify() callback.
670 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
671 _mesa_update_shader_textures_used(program
);
672 /* Do we need to care about the return value here?
673 * This should not be the first time the driver was notified of
676 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
680 /* ordinary uniform variable */
681 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
682 const GLenum basicType
= base_uniform_type(type
);
683 const GLint slots
= (param
->Size
+ 3) / 4;
684 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
687 if ((GLint
) param
->Size
> typeSize
) {
689 /* we'll ignore extra data below */
692 /* non-array: count must be at most one; count == 0 is handled by the loop below */
694 _mesa_error(ctx
, GL_INVALID_OPERATION
,
695 "glUniform(uniform '%s' is not an array)",
701 /* loop over number of array elements */
702 for (k
= 0; k
< count
; k
++) {
705 if (offset
+ k
>= slots
) {
706 /* Extra array data is ignored */
710 /* uniformVal (the destination) is always float[4] */
711 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
713 if (basicType
== GL_INT
) {
714 /* convert user's ints to floats */
715 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
716 for (i
= 0; i
< elems
; i
++) {
717 uniformVal
[i
] = (GLfloat
) iValues
[i
];
720 else if (basicType
== GL_UNSIGNED_INT
) {
721 /* convert user's uints to floats */
722 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
723 for (i
= 0; i
< elems
; i
++) {
724 uniformVal
[i
] = (GLfloat
) iValues
[i
];
728 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
729 assert(basicType
== GL_FLOAT
);
730 for (i
= 0; i
< elems
; i
++) {
731 uniformVal
[i
] = fValues
[i
];
735 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
737 for (i
= 0; i
< elems
; i
++) {
738 uniformVal
[i
] = uniformVal
[i
] ? 1.0f
: 0.0f
;
747 * Called via glUniform*() functions.
750 _mesa_uniform(GLcontext
*ctx
, struct gl_shader_program
*shProg
,
751 GLint location
, GLsizei count
,
752 const GLvoid
*values
, GLenum type
)
754 struct gl_uniform
*uniform
;
757 if (!shProg
|| !shProg
->LinkStatus
) {
758 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
763 return; /* The standard specifies this as a no-op */
766 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
771 split_location_offset(&location
, &offset
);
773 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
774 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
779 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
783 elems
= _mesa_sizeof_glsl_type(type
);
785 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
787 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
789 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
790 const GLenum basicType
= base_uniform_type(type
);
792 printf("Mesa: set program %u uniform %s (loc %d) to: ",
793 shProg
->Name
, uniform
->Name
, location
);
794 if (basicType
== GL_INT
) {
795 const GLint
*v
= (const GLint
*) values
;
796 for (i
= 0; i
< count
* elems
; i
++) {
800 else if (basicType
== GL_UNSIGNED_INT
) {
801 const GLuint
*v
= (const GLuint
*) values
;
802 for (i
= 0; i
< count
* elems
; i
++) {
807 const GLfloat
*v
= (const GLfloat
*) values
;
808 assert(basicType
== GL_FLOAT
);
809 for (i
= 0; i
< count
* elems
; i
++) {
816 /* A uniform var may be used by both a vertex shader and a fragment
817 * shader. We may need to update one or both shader's uniform here:
819 if (shProg
->VertexProgram
) {
820 /* convert uniform location to program parameter index */
821 GLint index
= uniform
->VertPos
;
823 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
824 index
, offset
, type
, count
, elems
, values
);
828 if (shProg
->FragmentProgram
) {
829 /* convert uniform location to program parameter index */
830 GLint index
= uniform
->FragPos
;
832 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
833 index
, offset
, type
, count
, elems
, values
);
837 if (shProg
->GeometryProgram
) {
838 /* convert uniform location to program parameter index */
839 GLint index
= uniform
->GeomPos
;
841 set_program_uniform(ctx
, &shProg
->GeometryProgram
->Base
,
842 index
, offset
, type
, count
, elems
, values
);
846 uniform
->Initialized
= GL_TRUE
;
851 * Set a matrix-valued program parameter.
854 set_program_uniform_matrix(GLcontext
*ctx
, struct gl_program
*program
,
855 GLuint index
, GLuint offset
,
856 GLuint count
, GLuint rows
, GLuint cols
,
857 GLboolean transpose
, const GLfloat
*values
)
859 GLuint mat
, row
, col
;
861 const struct gl_program_parameter
* param
= &program
->Parameters
->Parameters
[index
];
862 const GLuint slots
= (param
->Size
+ 3) / 4;
863 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
866 /* check that the number of rows, columns is correct */
867 get_matrix_dims(param
->DataType
, &nr
, &nc
);
868 if (rows
!= nr
|| cols
!= nc
) {
869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
870 "glUniformMatrix(matrix size mismatch)");
874 if ((GLint
) param
->Size
<= typeSize
) {
875 /* non-array: count must be at most one; count == 0 is handled by the loop below */
877 _mesa_error(ctx
, GL_INVALID_OPERATION
,
878 "glUniformMatrix(uniform is not an array)");
884 * Note: the _columns_ of a matrix are stored in program registers, not
885 * the rows. So, the loops below look a little funny.
886 * XXX could optimize this a bit...
889 /* loop over matrices */
890 for (mat
= 0; mat
< count
; mat
++) {
893 for (col
= 0; col
< cols
; col
++) {
895 if (offset
>= slots
) {
896 /* Ignore writes beyond the end of (the used part of) an array */
899 v
= program
->Parameters
->ParameterValues
[index
+ offset
];
900 for (row
= 0; row
< rows
; row
++) {
902 v
[row
] = values
[src
+ row
* cols
+ col
];
905 v
[row
] = values
[src
+ col
* rows
+ row
];
912 src
+= rows
* cols
; /* next matrix */
918 * Called by glUniformMatrix*() functions.
919 * Note: cols=2, rows=4 ==> array[2] of vec4
922 _mesa_uniform_matrix(GLcontext
*ctx
, struct gl_shader_program
*shProg
,
923 GLint cols
, GLint rows
,
924 GLint location
, GLsizei count
,
925 GLboolean transpose
, const GLfloat
*values
)
927 struct gl_uniform
*uniform
;
930 if (!shProg
|| !shProg
->LinkStatus
) {
931 _mesa_error(ctx
, GL_INVALID_OPERATION
,
932 "glUniformMatrix(program not linked)");
937 return; /* The standard specifies this as a no-op */
940 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
944 split_location_offset(&location
, &offset
);
946 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
947 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
950 if (values
== NULL
) {
951 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
955 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
957 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
959 if (shProg
->VertexProgram
) {
960 /* convert uniform location to program parameter index */
961 GLint index
= uniform
->VertPos
;
963 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
965 count
, rows
, cols
, transpose
, values
);
969 if (shProg
->FragmentProgram
) {
970 /* convert uniform location to program parameter index */
971 GLint index
= uniform
->FragPos
;
973 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
975 count
, rows
, cols
, transpose
, values
);
979 if (shProg
->GeometryProgram
) {
980 /* convert uniform location to program parameter index */
981 GLint index
= uniform
->GeomPos
;
983 set_program_uniform_matrix(ctx
, &shProg
->GeometryProgram
->Base
,
985 count
, rows
, cols
, transpose
, values
);
989 uniform
->Initialized
= GL_TRUE
;
994 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
996 GET_CURRENT_CONTEXT(ctx
);
997 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, &v0
, GL_FLOAT
);
1001 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1003 GET_CURRENT_CONTEXT(ctx
);
1007 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1011 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1013 GET_CURRENT_CONTEXT(ctx
);
1018 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1022 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1025 GET_CURRENT_CONTEXT(ctx
);
1031 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1035 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1037 GET_CURRENT_CONTEXT(ctx
);
1038 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, &v0
, GL_INT
);
1042 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1044 GET_CURRENT_CONTEXT(ctx
);
1048 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_INT_VEC2
);
1052 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1054 GET_CURRENT_CONTEXT(ctx
);
1059 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_INT_VEC3
);
1063 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1065 GET_CURRENT_CONTEXT(ctx
);
1071 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_INT_VEC4
);
1075 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1077 GET_CURRENT_CONTEXT(ctx
);
1078 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_FLOAT
);
1082 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1084 GET_CURRENT_CONTEXT(ctx
);
1085 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1089 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1091 GET_CURRENT_CONTEXT(ctx
);
1092 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1096 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1098 GET_CURRENT_CONTEXT(ctx
);
1099 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1103 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1105 GET_CURRENT_CONTEXT(ctx
);
1106 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_INT
);
1110 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1112 GET_CURRENT_CONTEXT(ctx
);
1113 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_INT_VEC2
);
1117 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1119 GET_CURRENT_CONTEXT(ctx
);
1120 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_INT_VEC3
);
1124 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1126 GET_CURRENT_CONTEXT(ctx
);
1127 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_INT_VEC4
);
1131 /** OpenGL 3.0 GLuint-valued functions **/
1133 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1135 GET_CURRENT_CONTEXT(ctx
);
1136 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1140 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1142 GET_CURRENT_CONTEXT(ctx
);
1146 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1150 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1152 GET_CURRENT_CONTEXT(ctx
);
1157 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1161 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1163 GET_CURRENT_CONTEXT(ctx
);
1169 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1173 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1175 GET_CURRENT_CONTEXT(ctx
);
1176 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1180 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1182 GET_CURRENT_CONTEXT(ctx
);
1183 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1187 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1189 GET_CURRENT_CONTEXT(ctx
);
1190 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1194 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1196 GET_CURRENT_CONTEXT(ctx
);
1197 _mesa_uniform(ctx
, ctx
->Shader
.CurrentProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1203 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1204 const GLfloat
* value
)
1206 GET_CURRENT_CONTEXT(ctx
);
1207 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1208 2, 2, location
, count
, transpose
, value
);
1212 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1213 const GLfloat
* value
)
1215 GET_CURRENT_CONTEXT(ctx
);
1216 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1217 3, 3, location
, count
, transpose
, value
);
1221 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1222 const GLfloat
* value
)
1224 GET_CURRENT_CONTEXT(ctx
);
1225 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1226 4, 4, location
, count
, transpose
, value
);
1231 * Non-square UniformMatrix are OpenGL 2.1
1234 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1235 const GLfloat
*value
)
1237 GET_CURRENT_CONTEXT(ctx
);
1238 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1239 2, 3, location
, count
, transpose
, value
);
1243 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1244 const GLfloat
*value
)
1246 GET_CURRENT_CONTEXT(ctx
);
1247 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1248 3, 2, location
, count
, transpose
, value
);
1252 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1253 const GLfloat
*value
)
1255 GET_CURRENT_CONTEXT(ctx
);
1256 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1257 2, 4, location
, count
, transpose
, value
);
1261 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1262 const GLfloat
*value
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1266 4, 2, location
, count
, transpose
, value
);
1270 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1271 const GLfloat
*value
)
1273 GET_CURRENT_CONTEXT(ctx
);
1274 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1275 3, 4, location
, count
, transpose
, value
);
1279 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1280 const GLfloat
*value
)
1282 GET_CURRENT_CONTEXT(ctx
);
1283 _mesa_uniform_matrix(ctx
, ctx
->Shader
.CurrentProgram
,
1284 4, 3, location
, count
, transpose
, value
);
1289 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1291 GET_CURRENT_CONTEXT(ctx
);
1292 _mesa_get_uniformfv(ctx
, program
, location
, params
);
1297 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 _mesa_get_uniformiv(ctx
, program
, location
, params
);
1305 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1307 struct gl_shader_program
*shProg
;
1309 GET_CURRENT_CONTEXT(ctx
);
1311 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1312 "glGetUniformLocation");
1316 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1321 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1322 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1323 GLenum
* type
, GLcharARB
* name
)
1325 GET_CURRENT_CONTEXT(ctx
);
1326 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1332 * Plug in shader uniform-related functions into API dispatch table.
1335 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1338 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1339 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1340 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1341 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1342 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1343 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1344 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1345 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1346 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1347 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1348 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1349 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1350 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1351 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1352 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1353 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1354 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1355 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1356 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1358 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1359 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1360 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1361 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1364 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1365 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1366 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1367 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1368 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1369 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1372 /* XXX finish dispatch */
1373 (void) _mesa_Uniform1ui
;
1374 (void) _mesa_Uniform2ui
;
1375 (void) _mesa_Uniform3ui
;
1376 (void) _mesa_Uniform4ui
;
1377 (void) _mesa_Uniform1uiv
;
1378 (void) _mesa_Uniform2uiv
;
1379 (void) _mesa_Uniform3uiv
;
1380 (void) _mesa_Uniform4uiv
;
1381 #endif /* FEATURE_GL */