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
, GLuint program
, const GLchar
*name
)
460 GLint offset
= 0, location
= -1;
462 struct gl_shader_program
*shProg
=
463 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformLocation");
468 if (shProg
->LinkStatus
== GL_FALSE
) {
469 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
473 /* XXX we should return -1 if the uniform was declared, but not
477 /* XXX we need to be able to parse uniform names for structs and arrays
484 /* handle 1-dimension arrays here... */
485 char *c
= strchr(name
, '[');
487 /* truncate name at [ */
488 const GLint len
= c
- name
;
489 GLchar
*newName
= malloc(len
+ 1);
491 return -1; /* out of mem */
492 memcpy(newName
, name
, len
);
495 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
497 const GLint element
= atoi(c
+ 1);
499 /* get type of the uniform array element */
500 struct gl_program_parameter
*p
;
501 p
= get_uniform_parameter(shProg
, location
);
504 get_matrix_dims(p
->DataType
, &rows
, &cols
);
507 offset
= element
* rows
;
517 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
521 merge_location_offset(&location
, offset
);
530 * Update the vertex/fragment program's TexturesUsed array.
532 * This needs to be called after glUniform(set sampler var) is called.
533 * A call to glUniform(samplerVar, value) causes a sampler to point to a
534 * particular texture unit. We know the sampler's texture target
535 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
536 * set by glUniform() calls.
538 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
539 * information to update the prog->TexturesUsed[] values.
540 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
541 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
542 * We'll use that info for state validation before rendering.
545 _mesa_update_shader_textures_used(struct gl_program
*prog
)
549 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
551 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
552 if (prog
->SamplersUsed
& (1 << s
)) {
553 GLuint unit
= prog
->SamplerUnits
[s
];
554 GLuint tgt
= prog
->SamplerTargets
[s
];
555 assert(unit
< MAX_TEXTURE_IMAGE_UNITS
);
556 assert(tgt
< NUM_TEXTURE_TARGETS
);
557 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
564 * Check if the type given by userType is allowed to set a uniform of the
565 * target type. Generally, equivalence is required, but setting Boolean
566 * uniforms can be done with glUniformiv or glUniformfv.
569 compatible_types(GLenum userType
, GLenum targetType
)
571 if (userType
== targetType
)
574 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
575 userType
== GL_UNSIGNED_INT
||
579 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
580 userType
== GL_UNSIGNED_INT_VEC2
||
581 userType
== GL_INT_VEC2
))
584 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
585 userType
== GL_UNSIGNED_INT_VEC3
||
586 userType
== GL_INT_VEC3
))
589 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
590 userType
== GL_UNSIGNED_INT_VEC4
||
591 userType
== GL_INT_VEC4
))
594 if (is_sampler_type(targetType
) && userType
== GL_INT
)
602 * Set the value of a program's uniform variable.
603 * \param program the program whose uniform to update
604 * \param index the index of the program parameter for the uniform
605 * \param offset additional parameter slot offset (for arrays)
606 * \param type the incoming datatype of 'values'
607 * \param count the number of uniforms to set
608 * \param elems number of elements per uniform (1, 2, 3 or 4)
609 * \param values the new values, of datatype 'type'
612 set_program_uniform(GLcontext
*ctx
, struct gl_program
*program
,
613 GLint index
, GLint offset
,
614 GLenum type
, GLsizei count
, GLint elems
,
617 const struct gl_program_parameter
*param
=
618 &program
->Parameters
->Parameters
[index
];
624 if (!compatible_types(type
, param
->DataType
)) {
625 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
629 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
634 if (param
->Type
== PROGRAM_SAMPLER
) {
635 /* This controls which texture unit which is used by a sampler */
636 GLboolean changed
= GL_FALSE
;
639 /* this should have been caught by the compatible_types() check */
640 ASSERT(type
== GL_INT
);
642 /* loop over number of samplers to change */
643 for (i
= 0; i
< count
; i
++) {
645 (GLuint
) program
->Parameters
->ParameterValues
[index
+ offset
+ i
][0];
646 GLuint texUnit
= ((GLuint
*) values
)[i
];
648 /* check that the sampler (tex unit index) is legal */
649 if (texUnit
>= ctx
->Const
.MaxTextureImageUnits
) {
650 _mesa_error(ctx
, GL_INVALID_VALUE
,
651 "glUniform1(invalid sampler/tex unit index for '%s')",
656 /* This maps a sampler to a texture unit: */
657 if (sampler
< MAX_SAMPLERS
) {
659 printf("Set program %p sampler %d '%s' to unit %u\n",
660 program
, sampler
, param
->Name
, texUnit
);
662 if (program
->SamplerUnits
[sampler
] != texUnit
) {
663 program
->SamplerUnits
[sampler
] = texUnit
;
670 /* When a sampler's value changes it usually requires rewriting
671 * a GPU program's TEX instructions since there may not be a
672 * sampler->texture lookup table. We signal this with the
673 * ProgramStringNotify() callback.
675 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
676 _mesa_update_shader_textures_used(program
);
677 /* Do we need to care about the return value here?
678 * This should not be the first time the driver was notified of
681 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
685 /* ordinary uniform variable */
686 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
687 const GLenum basicType
= base_uniform_type(type
);
688 const GLint slots
= (param
->Size
+ 3) / 4;
689 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
692 if ((GLint
) param
->Size
> typeSize
) {
694 /* we'll ignore extra data below */
697 /* non-array: count must be at most one; count == 0 is handled by the loop below */
699 _mesa_error(ctx
, GL_INVALID_OPERATION
,
700 "glUniform(uniform '%s' is not an array)",
706 /* loop over number of array elements */
707 for (k
= 0; k
< count
; k
++) {
710 if (offset
+ k
>= slots
) {
711 /* Extra array data is ignored */
715 /* uniformVal (the destination) is always float[4] */
716 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
718 if (basicType
== GL_INT
) {
719 /* convert user's ints to floats */
720 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
721 for (i
= 0; i
< elems
; i
++) {
722 uniformVal
[i
] = (GLfloat
) iValues
[i
];
725 else if (basicType
== GL_UNSIGNED_INT
) {
726 /* convert user's uints to floats */
727 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
728 for (i
= 0; i
< elems
; i
++) {
729 uniformVal
[i
] = (GLfloat
) iValues
[i
];
733 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
734 assert(basicType
== GL_FLOAT
);
735 for (i
= 0; i
< elems
; i
++) {
736 uniformVal
[i
] = fValues
[i
];
740 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
742 for (i
= 0; i
< elems
; i
++) {
743 uniformVal
[i
] = uniformVal
[i
] ? 1.0f
: 0.0f
;
752 * Called via glUniform*() functions.
755 _mesa_uniform(GLcontext
*ctx
, GLint location
, GLsizei count
,
756 const GLvoid
*values
, GLenum type
)
758 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
759 struct gl_uniform
*uniform
;
762 if (!shProg
|| !shProg
->LinkStatus
) {
763 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
768 return; /* The standard specifies this as a no-op */
771 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
776 split_location_offset(&location
, &offset
);
778 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
779 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
784 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
788 elems
= _mesa_sizeof_glsl_type(type
);
790 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
792 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
794 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
795 const GLenum basicType
= base_uniform_type(type
);
797 printf("Mesa: set program %u uniform %s (loc %d) to: ",
798 shProg
->Name
, uniform
->Name
, location
);
799 if (basicType
== GL_INT
) {
800 const GLint
*v
= (const GLint
*) values
;
801 for (i
= 0; i
< count
* elems
; i
++) {
805 else if (basicType
== GL_UNSIGNED_INT
) {
806 const GLuint
*v
= (const GLuint
*) values
;
807 for (i
= 0; i
< count
* elems
; i
++) {
812 const GLfloat
*v
= (const GLfloat
*) values
;
813 assert(basicType
== GL_FLOAT
);
814 for (i
= 0; i
< count
* elems
; i
++) {
821 /* A uniform var may be used by both a vertex shader and a fragment
822 * shader. We may need to update one or both shader's uniform here:
824 if (shProg
->VertexProgram
) {
825 /* convert uniform location to program parameter index */
826 GLint index
= uniform
->VertPos
;
828 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
829 index
, offset
, type
, count
, elems
, values
);
833 if (shProg
->FragmentProgram
) {
834 /* convert uniform location to program parameter index */
835 GLint index
= uniform
->FragPos
;
837 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
838 index
, offset
, type
, count
, elems
, values
);
842 if (shProg
->GeometryProgram
) {
843 /* convert uniform location to program parameter index */
844 GLint index
= uniform
->GeomPos
;
846 set_program_uniform(ctx
, &shProg
->GeometryProgram
->Base
,
847 index
, offset
, type
, count
, elems
, values
);
851 uniform
->Initialized
= GL_TRUE
;
856 * Set a matrix-valued program parameter.
859 set_program_uniform_matrix(GLcontext
*ctx
, struct gl_program
*program
,
860 GLuint index
, GLuint offset
,
861 GLuint count
, GLuint rows
, GLuint cols
,
862 GLboolean transpose
, const GLfloat
*values
)
864 GLuint mat
, row
, col
;
866 const struct gl_program_parameter
* param
= &program
->Parameters
->Parameters
[index
];
867 const GLuint slots
= (param
->Size
+ 3) / 4;
868 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
871 /* check that the number of rows, columns is correct */
872 get_matrix_dims(param
->DataType
, &nr
, &nc
);
873 if (rows
!= nr
|| cols
!= nc
) {
874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
875 "glUniformMatrix(matrix size mismatch)");
879 if ((GLint
) param
->Size
<= typeSize
) {
880 /* non-array: count must be at most one; count == 0 is handled by the loop below */
882 _mesa_error(ctx
, GL_INVALID_OPERATION
,
883 "glUniformMatrix(uniform is not an array)");
889 * Note: the _columns_ of a matrix are stored in program registers, not
890 * the rows. So, the loops below look a little funny.
891 * XXX could optimize this a bit...
894 /* loop over matrices */
895 for (mat
= 0; mat
< count
; mat
++) {
898 for (col
= 0; col
< cols
; col
++) {
900 if (offset
>= slots
) {
901 /* Ignore writes beyond the end of (the used part of) an array */
904 v
= program
->Parameters
->ParameterValues
[index
+ offset
];
905 for (row
= 0; row
< rows
; row
++) {
907 v
[row
] = values
[src
+ row
* cols
+ col
];
910 v
[row
] = values
[src
+ col
* rows
+ row
];
917 src
+= rows
* cols
; /* next matrix */
923 * Called by glUniformMatrix*() functions.
924 * Note: cols=2, rows=4 ==> array[2] of vec4
927 _mesa_uniform_matrix(GLcontext
*ctx
, GLint cols
, GLint rows
,
928 GLint location
, GLsizei count
,
929 GLboolean transpose
, const GLfloat
*values
)
931 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
932 struct gl_uniform
*uniform
;
935 if (!shProg
|| !shProg
->LinkStatus
) {
936 _mesa_error(ctx
, GL_INVALID_OPERATION
,
937 "glUniformMatrix(program not linked)");
942 return; /* The standard specifies this as a no-op */
945 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
949 split_location_offset(&location
, &offset
);
951 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
952 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
955 if (values
== NULL
) {
956 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
960 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
962 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
964 if (shProg
->VertexProgram
) {
965 /* convert uniform location to program parameter index */
966 GLint index
= uniform
->VertPos
;
968 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
970 count
, rows
, cols
, transpose
, values
);
974 if (shProg
->FragmentProgram
) {
975 /* convert uniform location to program parameter index */
976 GLint index
= uniform
->FragPos
;
978 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
980 count
, rows
, cols
, transpose
, values
);
984 if (shProg
->GeometryProgram
) {
985 /* convert uniform location to program parameter index */
986 GLint index
= uniform
->GeomPos
;
988 set_program_uniform_matrix(ctx
, &shProg
->GeometryProgram
->Base
,
990 count
, rows
, cols
, transpose
, values
);
994 uniform
->Initialized
= GL_TRUE
;
999 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1001 GET_CURRENT_CONTEXT(ctx
);
1002 _mesa_uniform(ctx
, location
, 1, &v0
, GL_FLOAT
);
1006 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1008 GET_CURRENT_CONTEXT(ctx
);
1012 _mesa_uniform(ctx
, location
, 1, v
, GL_FLOAT_VEC2
);
1016 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1018 GET_CURRENT_CONTEXT(ctx
);
1023 _mesa_uniform(ctx
, location
, 1, v
, GL_FLOAT_VEC3
);
1027 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1030 GET_CURRENT_CONTEXT(ctx
);
1036 _mesa_uniform(ctx
, location
, 1, v
, GL_FLOAT_VEC4
);
1040 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1042 GET_CURRENT_CONTEXT(ctx
);
1043 _mesa_uniform(ctx
, location
, 1, &v0
, GL_INT
);
1047 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1049 GET_CURRENT_CONTEXT(ctx
);
1053 _mesa_uniform(ctx
, location
, 1, v
, GL_INT_VEC2
);
1057 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1059 GET_CURRENT_CONTEXT(ctx
);
1064 _mesa_uniform(ctx
, location
, 1, v
, GL_INT_VEC3
);
1068 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1070 GET_CURRENT_CONTEXT(ctx
);
1076 _mesa_uniform(ctx
, location
, 1, v
, GL_INT_VEC4
);
1080 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1082 GET_CURRENT_CONTEXT(ctx
);
1083 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT
);
1087 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1089 GET_CURRENT_CONTEXT(ctx
);
1090 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT_VEC2
);
1094 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1096 GET_CURRENT_CONTEXT(ctx
);
1097 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT_VEC3
);
1101 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1103 GET_CURRENT_CONTEXT(ctx
);
1104 _mesa_uniform(ctx
, location
, count
, value
, GL_FLOAT_VEC4
);
1108 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1110 GET_CURRENT_CONTEXT(ctx
);
1111 _mesa_uniform(ctx
, location
, count
, value
, GL_INT
);
1115 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1117 GET_CURRENT_CONTEXT(ctx
);
1118 _mesa_uniform(ctx
, location
, count
, value
, GL_INT_VEC2
);
1122 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1124 GET_CURRENT_CONTEXT(ctx
);
1125 _mesa_uniform(ctx
, location
, count
, value
, GL_INT_VEC3
);
1129 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1131 GET_CURRENT_CONTEXT(ctx
);
1132 _mesa_uniform(ctx
, location
, count
, value
, GL_INT_VEC4
);
1136 /** OpenGL 3.0 GLuint-valued functions **/
1138 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1140 GET_CURRENT_CONTEXT(ctx
);
1141 _mesa_uniform(ctx
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1145 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1147 GET_CURRENT_CONTEXT(ctx
);
1151 _mesa_uniform(ctx
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1155 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1157 GET_CURRENT_CONTEXT(ctx
);
1162 _mesa_uniform(ctx
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1166 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1168 GET_CURRENT_CONTEXT(ctx
);
1174 _mesa_uniform(ctx
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1178 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT
);
1185 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1187 GET_CURRENT_CONTEXT(ctx
);
1188 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1192 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1194 GET_CURRENT_CONTEXT(ctx
);
1195 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1199 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1201 GET_CURRENT_CONTEXT(ctx
);
1202 _mesa_uniform(ctx
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1208 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1209 const GLfloat
* value
)
1211 GET_CURRENT_CONTEXT(ctx
);
1212 _mesa_uniform_matrix(ctx
, 2, 2, location
, count
, transpose
, value
);
1216 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1217 const GLfloat
* value
)
1219 GET_CURRENT_CONTEXT(ctx
);
1220 _mesa_uniform_matrix(ctx
, 3, 3, location
, count
, transpose
, value
);
1224 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1225 const GLfloat
* value
)
1227 GET_CURRENT_CONTEXT(ctx
);
1228 _mesa_uniform_matrix(ctx
, 4, 4, location
, count
, transpose
, value
);
1233 * Non-square UniformMatrix are OpenGL 2.1
1236 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1237 const GLfloat
*value
)
1239 GET_CURRENT_CONTEXT(ctx
);
1240 _mesa_uniform_matrix(ctx
, 2, 3, location
, count
, transpose
, value
);
1244 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1245 const GLfloat
*value
)
1247 GET_CURRENT_CONTEXT(ctx
);
1248 _mesa_uniform_matrix(ctx
, 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
, 2, 4, location
, count
, transpose
, value
);
1260 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1261 const GLfloat
*value
)
1263 GET_CURRENT_CONTEXT(ctx
);
1264 _mesa_uniform_matrix(ctx
, 4, 2, location
, count
, transpose
, value
);
1268 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1269 const GLfloat
*value
)
1271 GET_CURRENT_CONTEXT(ctx
);
1272 _mesa_uniform_matrix(ctx
, 3, 4, location
, count
, transpose
, value
);
1276 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1277 const GLfloat
*value
)
1279 GET_CURRENT_CONTEXT(ctx
);
1280 _mesa_uniform_matrix(ctx
, 4, 3, location
, count
, transpose
, value
);
1285 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1287 GET_CURRENT_CONTEXT(ctx
);
1288 _mesa_get_uniformfv(ctx
, program
, location
, params
);
1293 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 _mesa_get_uniformiv(ctx
, program
, location
, params
);
1301 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1303 GET_CURRENT_CONTEXT(ctx
);
1304 return _mesa_get_uniform_location(ctx
, programObj
, name
);
1309 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1310 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1311 GLenum
* type
, GLcharARB
* name
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1320 * Plug in shader uniform-related functions into API dispatch table.
1323 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1326 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1327 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1328 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1329 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1330 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1331 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1332 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1333 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1334 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1335 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1336 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1337 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1338 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1339 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1340 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1341 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1342 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1343 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1344 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1346 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1347 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1348 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1349 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1352 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1353 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1354 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1355 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1356 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1357 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1360 /* XXX finish dispatch */
1361 (void) _mesa_Uniform1ui
;
1362 (void) _mesa_Uniform2ui
;
1363 (void) _mesa_Uniform3ui
;
1364 (void) _mesa_Uniform4ui
;
1365 (void) _mesa_Uniform1uiv
;
1366 (void) _mesa_Uniform2uiv
;
1367 (void) _mesa_Uniform3uiv
;
1368 (void) _mesa_Uniform4uiv
;
1369 #endif /* FEATURE_GL */