2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Functions related to GLSL uniform variables.
34 * 1. Check that the right error code is generated for all _mesa_error() calls.
35 * 2. Insert FLUSH_VERTICES calls in various places
39 #include "main/glheader.h"
40 #include "main/context.h"
41 #include "main/dispatch.h"
42 #include "main/image.h"
43 #include "main/mfeatures.h"
44 #include "main/mtypes.h"
45 #include "main/shaderapi.h"
46 #include "main/shaderobj.h"
47 #include "main/uniforms.h"
48 #include "program/prog_parameter.h"
49 #include "program/prog_statevars.h"
50 #include "program/prog_uniform.h"
51 #include "program/prog_instruction.h"
55 base_uniform_type(GLenum type
)
58 #if 0 /* not needed, for now */
71 case GL_UNSIGNED_INT_VEC2
:
72 case GL_UNSIGNED_INT_VEC3
:
73 case GL_UNSIGNED_INT_VEC4
:
74 return GL_UNSIGNED_INT
;
81 _mesa_problem(NULL
, "Invalid type in base_uniform_type()");
88 is_boolean_type(GLenum type
)
103 is_sampler_type(GLenum type
)
109 case GL_SAMPLER_CUBE
:
110 case GL_SAMPLER_1D_SHADOW
:
111 case GL_SAMPLER_2D_SHADOW
:
112 case GL_SAMPLER_CUBE_SHADOW
:
113 case GL_SAMPLER_2D_RECT_ARB
:
114 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
115 case GL_SAMPLER_1D_ARRAY_EXT
:
116 case GL_SAMPLER_2D_ARRAY_EXT
:
117 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
118 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
119 case GL_SAMPLER_CUBE_MAP_ARRAY
:
120 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW
:
121 case GL_SAMPLER_BUFFER
:
130 * Given a uniform index, return the vertex/geometry/fragment program
131 * that has that parameter, plus the position of the parameter in the
132 * parameter/constant buffer.
133 * \param shProg the shader program
134 * \param index the uniform index in [0, NumUniforms-1]
135 * \param progOut returns containing program
136 * \param posOut returns position of the uniform in the param/const buffer
137 * \return GL_TRUE for success, GL_FALSE for invalid index
140 find_uniform_parameter_pos(struct gl_shader_program
*shProg
, GLint index
,
141 struct gl_program
**progOut
, GLint
*posOut
)
143 struct gl_program
*prog
= NULL
;
146 if (!shProg
->Uniforms
||
148 index
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
152 pos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
154 prog
= &shProg
->VertexProgram
->Base
;
157 pos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
159 prog
= &shProg
->FragmentProgram
->Base
;
162 pos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
164 prog
= &shProg
->GeometryProgram
->Base
;
169 if (!prog
|| pos
< 0)
170 return GL_FALSE
; /* should really never happen */
180 * Return pointer to a gl_program_parameter which corresponds to a uniform.
181 * \param shProg the shader program
182 * \param index the uniform index in [0, NumUniforms-1]
183 * \return gl_program_parameter point or NULL if index is invalid
185 static const struct gl_program_parameter
*
186 get_uniform_parameter(struct gl_shader_program
*shProg
, GLint index
)
188 struct gl_program
*prog
;
191 if (find_uniform_parameter_pos(shProg
, index
, &prog
, &progPos
))
192 return &prog
->Parameters
->Parameters
[progPos
];
199 * Called by glGetActiveUniform().
202 _mesa_get_active_uniform(struct gl_context
*ctx
, GLuint program
, GLuint index
,
203 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
204 GLenum
*type
, GLchar
*nameOut
)
206 struct gl_shader_program
*shProg
=
207 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
208 const struct gl_program_parameter
*param
;
213 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
214 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
218 param
= get_uniform_parameter(shProg
, index
);
223 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
227 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
228 if ((GLint
) param
->Size
> typeSize
) {
230 * Array elements are placed on vector[4] boundaries so they're
231 * a multiple of four floats. We round typeSize up to next multiple
232 * of four to get the right size below.
234 typeSize
= (typeSize
+ 3) & ~3;
236 /* Note that the returned size is in units of the <type>, not bytes */
237 *size
= param
->Size
/ typeSize
;
241 *type
= param
->DataType
;
247 get_vector_elements(GLenum type
)
253 case GL_UNSIGNED_INT
:
254 default: /* Catch all the various sampler types. */
260 case GL_UNSIGNED_INT_VEC2
:
266 case GL_UNSIGNED_INT_VEC3
:
272 case GL_UNSIGNED_INT_VEC4
:
278 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
284 case GL_FLOAT_MAT2x3
:
288 case GL_FLOAT_MAT2x4
:
296 case GL_FLOAT_MAT3x2
:
300 case GL_FLOAT_MAT3x4
:
308 case GL_FLOAT_MAT4x2
:
312 case GL_FLOAT_MAT4x3
:
323 * Determine the number of rows and columns occupied by a uniform
324 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
325 * the number of rows = 1 and cols = number of elements in the vector.
328 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
329 GLint
*rows
, GLint
*cols
)
331 get_matrix_dims(p
->DataType
, rows
, cols
);
332 if (*rows
== 0 && *cols
== 0) {
333 /* not a matrix type, probably a float or vector */
335 *cols
= get_vector_elements(p
->DataType
);
341 * GLSL uniform arrays and structs require special handling.
343 * The GL_ARB_shader_objects spec says that if you use
344 * glGetUniformLocation to get the location of an array, you CANNOT
345 * access other elements of the array by adding an offset to the
346 * returned location. For example, you must call
347 * glGetUniformLocation("foo[16]") if you want to set the 16th element
348 * of the array with glUniform().
350 * HOWEVER, some other OpenGL drivers allow accessing array elements
351 * by adding an offset to the returned array location. And some apps
352 * seem to depend on that behaviour.
354 * Mesa's gl_uniform_list doesn't directly support this since each
355 * entry in the list describes one uniform variable, not one uniform
356 * element. We could insert dummy entries in the list for each array
357 * element after [0] but that causes complications elsewhere.
359 * We solve this problem by encoding two values in the location that's
360 * returned by glGetUniformLocation():
361 * a) index into gl_uniform_list::Uniforms[] for the uniform
362 * b) an array/field offset (0 for simple types)
364 * These two values are encoded in the high and low halves of a GLint.
365 * By putting the uniform number in the high part and the offset in the
366 * low part, we can support the unofficial ability to index into arrays
367 * by adding offsets to the location value.
370 merge_location_offset(GLint
*location
, GLint offset
)
372 *location
= (*location
<< 16) | offset
;
377 * Separate the uniform location and parameter offset. See above.
380 split_location_offset(GLint
*location
, GLint
*offset
)
382 *offset
= *location
& 0xffff;
383 *location
= *location
>> 16;
389 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
392 get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
393 GLsizei bufSize
, GLenum returnType
, GLvoid
*paramsOut
)
395 struct gl_shader_program
*shProg
=
396 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
397 struct gl_program
*prog
;
398 GLint paramPos
, offset
;
403 split_location_offset(&location
, &offset
);
405 if (!find_uniform_parameter_pos(shProg
, location
, &prog
, ¶mPos
)) {
406 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
409 const struct gl_program_parameter
*p
=
410 &prog
->Parameters
->Parameters
[paramPos
];
411 GLint rows
, cols
, i
, j
, k
;
414 get_uniform_rows_cols(p
, &rows
, &cols
);
416 numBytes
= rows
* cols
* _mesa_sizeof_type(returnType
);
417 if (bufSize
< numBytes
) {
418 _mesa_error( ctx
, GL_INVALID_OPERATION
,
419 "glGetnUniformfvARB(out of bounds: bufSize is %d,"
420 " but %d bytes are required)", bufSize
, numBytes
);
424 switch (returnType
) {
427 GLfloat
*params
= (GLfloat
*) paramsOut
;
429 for (i
= 0; i
< rows
; i
++) {
430 const int base
= paramPos
+ offset
+ i
;
431 for (j
= 0; j
< cols
; j
++ ) {
432 params
[k
++] = prog
->Parameters
->ParameterValues
[base
][j
].f
;
439 GLfloat
*params
= (GLfloat
*) paramsOut
;
441 for (i
= 0; i
< rows
; i
++) {
442 const int base
= paramPos
+ offset
+ i
;
443 for (j
= 0; j
< cols
; j
++ ) {
444 params
[k
++] = (GLdouble
)
445 prog
->Parameters
->ParameterValues
[base
][j
].f
;
452 GLint
*params
= (GLint
*) paramsOut
;
454 for (i
= 0; i
< rows
; i
++) {
455 const int base
= paramPos
+ offset
+ i
;
456 for (j
= 0; j
< cols
; j
++ ) {
457 params
[k
++] = ctx
->Const
.GLSLVersion
<= 120 ?
458 (GLint
) prog
->Parameters
->ParameterValues
[base
][j
].f
:
459 prog
->Parameters
->ParameterValues
[base
][j
].i
;
464 case GL_UNSIGNED_INT
:
466 GLuint
*params
= (GLuint
*) paramsOut
;
468 for (i
= 0; i
< rows
; i
++) {
469 const int base
= paramPos
+ offset
+ i
;
470 for (j
= 0; j
< cols
; j
++ ) {
471 params
[k
++] = ctx
->Const
.GLSLVersion
<= 120 ?
472 (GLuint
) prog
->Parameters
->ParameterValues
[base
][j
].f
:
473 prog
->Parameters
->ParameterValues
[base
][j
].u
;
479 _mesa_problem(ctx
, "bad returnType in get_uniform()");
486 * Called via glGetUniformLocation().
488 * The return value will encode two values, the uniform location and an
489 * offset (used for arrays, structs).
492 _mesa_get_uniform_location(struct gl_context
*ctx
,
493 struct gl_shader_program
*shProg
,
496 GLint offset
= 0, location
= -1;
498 if (shProg
->LinkStatus
== GL_FALSE
) {
499 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
503 /* XXX we should return -1 if the uniform was declared, but not
507 /* XXX we need to be able to parse uniform names for structs and arrays
514 /* handle 1-dimension arrays here... */
515 char *c
= strchr(name
, '[');
517 /* truncate name at [ */
518 const GLint len
= c
- name
;
519 GLchar
*newName
= malloc(len
+ 1);
521 return -1; /* out of mem */
522 memcpy(newName
, name
, len
);
525 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
527 const GLint element
= atoi(c
+ 1);
529 /* get type of the uniform array element */
530 const struct gl_program_parameter
*p
=
531 get_uniform_parameter(shProg
, location
);
534 get_matrix_dims(p
->DataType
, &rows
, &cols
);
537 offset
= element
* rows
;
547 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
551 merge_location_offset(&location
, offset
);
560 * Update the vertex/fragment program's TexturesUsed array.
562 * This needs to be called after glUniform(set sampler var) is called.
563 * A call to glUniform(samplerVar, value) causes a sampler to point to a
564 * particular texture unit. We know the sampler's texture target
565 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
566 * set by glUniform() calls.
568 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
569 * information to update the prog->TexturesUsed[] values.
570 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
571 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
572 * We'll use that info for state validation before rendering.
575 _mesa_update_shader_textures_used(struct gl_program
*prog
)
579 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
581 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
582 if (prog
->SamplersUsed
& (1 << s
)) {
583 GLuint unit
= prog
->SamplerUnits
[s
];
584 GLuint tgt
= prog
->SamplerTargets
[s
];
585 assert(unit
< Elements(prog
->TexturesUsed
));
586 assert(tgt
< NUM_TEXTURE_TARGETS
);
587 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
594 * Check if the type given by userType is allowed to set a uniform of the
595 * target type. Generally, equivalence is required, but setting Boolean
596 * uniforms can be done with glUniformiv or glUniformfv.
599 compatible_types(GLenum userType
, GLenum targetType
)
601 if (userType
== targetType
)
604 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
605 userType
== GL_UNSIGNED_INT
||
609 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
610 userType
== GL_UNSIGNED_INT_VEC2
||
611 userType
== GL_INT_VEC2
))
614 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
615 userType
== GL_UNSIGNED_INT_VEC3
||
616 userType
== GL_INT_VEC3
))
619 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
620 userType
== GL_UNSIGNED_INT_VEC4
||
621 userType
== GL_INT_VEC4
))
624 if (is_sampler_type(targetType
) && userType
== GL_INT
)
632 * Set the value of a program's uniform variable.
633 * \param program the program whose uniform to update
634 * \param index the index of the program parameter for the uniform
635 * \param offset additional parameter slot offset (for arrays)
636 * \param type the incoming datatype of 'values'
637 * \param count the number of uniforms to set
638 * \param elems number of elements per uniform (1, 2, 3 or 4)
639 * \param values the new values, of datatype 'type'
642 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
643 GLint index
, GLint offset
,
644 GLenum type
, GLsizei count
, GLint elems
,
647 const struct gl_program_parameter
*param
=
648 &program
->Parameters
->Parameters
[index
];
654 if (!compatible_types(type
, param
->DataType
)) {
655 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
659 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
664 if (param
->Type
== PROGRAM_SAMPLER
) {
665 /* This controls which texture unit which is used by a sampler */
666 GLboolean changed
= GL_FALSE
;
669 /* this should have been caught by the compatible_types() check */
670 ASSERT(type
== GL_INT
);
672 /* loop over number of samplers to change */
673 for (i
= 0; i
< count
; i
++) {
674 GLuint sampler
= (GLuint
)
675 program
->Parameters
->ParameterValues
[index
+offset
+ i
][0].f
;
676 GLuint texUnit
= ((GLuint
*) values
)[i
];
678 /* check that the sampler (tex unit index) is legal */
679 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
680 _mesa_error(ctx
, GL_INVALID_VALUE
,
681 "glUniform1(invalid sampler/tex unit index for '%s')",
686 /* This maps a sampler to a texture unit: */
687 if (sampler
< MAX_SAMPLERS
) {
689 printf("Set program %p sampler %d '%s' to unit %u\n",
690 program
, sampler
, param
->Name
, texUnit
);
692 if (program
->SamplerUnits
[sampler
] != texUnit
) {
693 program
->SamplerUnits
[sampler
] = texUnit
;
700 /* When a sampler's value changes it usually requires rewriting
701 * a GPU program's TEX instructions since there may not be a
702 * sampler->texture lookup table. We signal this with the
703 * ProgramStringNotify() callback.
705 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
706 _mesa_update_shader_textures_used(program
);
707 /* Do we need to care about the return value here?
708 * This should not be the first time the driver was notified of
711 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
715 /* ordinary uniform variable */
716 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
717 const GLenum basicType
= base_uniform_type(type
);
718 const GLint slots
= (param
->Size
+ 3) / 4;
719 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
722 if ((GLint
) param
->Size
> typeSize
) {
724 /* we'll ignore extra data below */
727 /* non-array: count must be at most one; count == 0 is handled
731 _mesa_error(ctx
, GL_INVALID_OPERATION
,
732 "glUniform(uniform '%s' is not an array)",
738 /* loop over number of array elements */
739 for (k
= 0; k
< count
; k
++) {
740 gl_constant_value
*uniformVal
;
742 if (offset
+ k
>= slots
) {
743 /* Extra array data is ignored */
747 /* uniformVal (the destination) is always gl_constant_value[4] */
748 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
750 if (basicType
== GL_INT
) {
751 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
752 for (i
= 0; i
< elems
; i
++) {
753 if (ctx
->Const
.GLSLVersion
<= 120)
754 uniformVal
[i
].f
= (GLfloat
) iValues
[i
];
756 uniformVal
[i
].i
= iValues
[i
];
759 else if (basicType
== GL_UNSIGNED_INT
) {
760 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
761 for (i
= 0; i
< elems
; i
++) {
762 if (ctx
->Const
.GLSLVersion
<= 120)
763 uniformVal
[i
].f
= (GLfloat
)(GLuint
) iValues
[i
];
765 uniformVal
[i
].u
= iValues
[i
];
769 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
770 assert(basicType
== GL_FLOAT
);
771 for (i
= 0; i
< elems
; i
++) {
772 uniformVal
[i
].f
= fValues
[i
];
776 /* if the uniform is bool-valued, convert to 1 or 0 */
778 for (i
= 0; i
< elems
; i
++) {
779 if (basicType
== GL_FLOAT
)
780 uniformVal
[i
].b
= uniformVal
[i
].f
!= 0.0f
? 1 : 0;
782 uniformVal
[i
].b
= uniformVal
[i
].u
? 1 : 0;
784 if (ctx
->Const
.GLSLVersion
<= 120)
785 uniformVal
[i
].f
= uniformVal
[i
].b
? 1.0f
: 0.0f
;
794 * Called via glUniform*() functions.
797 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
798 GLint location
, GLsizei count
,
799 const GLvoid
*values
, GLenum type
)
801 struct gl_uniform
*uniform
;
804 ASSERT_OUTSIDE_BEGIN_END(ctx
);
806 if (!shProg
|| !shProg
->LinkStatus
) {
807 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
812 return; /* The standard specifies this as a no-op */
815 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
820 split_location_offset(&location
, &offset
);
822 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
823 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
828 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
832 elems
= _mesa_sizeof_glsl_type(type
);
834 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
836 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
838 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
839 const GLenum basicType
= base_uniform_type(type
);
841 printf("Mesa: set program %u uniform %s (loc %d) to: ",
842 shProg
->Name
, uniform
->Name
, location
);
843 if (basicType
== GL_INT
) {
844 const GLint
*v
= (const GLint
*) values
;
845 for (i
= 0; i
< count
* elems
; i
++) {
849 else if (basicType
== GL_UNSIGNED_INT
) {
850 const GLuint
*v
= (const GLuint
*) values
;
851 for (i
= 0; i
< count
* elems
; i
++) {
856 const GLfloat
*v
= (const GLfloat
*) values
;
857 assert(basicType
== GL_FLOAT
);
858 for (i
= 0; i
< count
* elems
; i
++) {
865 /* A uniform var may be used by both a vertex shader and a fragment
866 * shader. We may need to update one or both shader's uniform here:
868 if (shProg
->VertexProgram
) {
869 /* convert uniform location to program parameter index */
870 GLint index
= uniform
->VertPos
;
872 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
873 index
, offset
, type
, count
, elems
, values
);
877 if (shProg
->FragmentProgram
) {
878 /* convert uniform location to program parameter index */
879 GLint index
= uniform
->FragPos
;
881 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
882 index
, offset
, type
, count
, elems
, values
);
886 if (shProg
->GeometryProgram
) {
887 /* convert uniform location to program parameter index */
888 GLint index
= uniform
->GeomPos
;
890 set_program_uniform(ctx
, &shProg
->GeometryProgram
->Base
,
891 index
, offset
, type
, count
, elems
, values
);
895 uniform
->Initialized
= GL_TRUE
;
900 * Set a matrix-valued program parameter.
903 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
904 GLuint index
, GLuint offset
,
905 GLuint count
, GLuint rows
, GLuint cols
,
906 GLboolean transpose
, const GLfloat
*values
)
908 GLuint mat
, row
, col
;
910 const struct gl_program_parameter
*param
=
911 &program
->Parameters
->Parameters
[index
];
912 const GLuint slots
= (param
->Size
+ 3) / 4;
913 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
916 /* check that the number of rows, columns is correct */
917 get_matrix_dims(param
->DataType
, &nr
, &nc
);
918 if (rows
!= nr
|| cols
!= nc
) {
919 _mesa_error(ctx
, GL_INVALID_OPERATION
,
920 "glUniformMatrix(matrix size mismatch)");
924 if ((GLint
) param
->Size
<= typeSize
) {
925 /* non-array: count must be at most one; count == 0 is handled
929 _mesa_error(ctx
, GL_INVALID_OPERATION
,
930 "glUniformMatrix(uniform is not an array)");
936 * Note: the _columns_ of a matrix are stored in program registers, not
937 * the rows. So, the loops below look a little funny.
938 * XXX could optimize this a bit...
941 /* loop over matrices */
942 for (mat
= 0; mat
< count
; mat
++) {
945 for (col
= 0; col
< cols
; col
++) {
947 if (offset
>= slots
) {
948 /* Ignore writes beyond the end of (the used part of) an array */
951 v
= (GLfloat
*) program
->Parameters
->ParameterValues
[index
+ offset
];
952 for (row
= 0; row
< rows
; row
++) {
954 v
[row
] = values
[src
+ row
* cols
+ col
];
957 v
[row
] = values
[src
+ col
* rows
+ row
];
964 src
+= rows
* cols
; /* next matrix */
970 * Called by glUniformMatrix*() functions.
971 * Note: cols=2, rows=4 ==> array[2] of vec4
974 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
975 GLint cols
, GLint rows
,
976 GLint location
, GLsizei count
,
977 GLboolean transpose
, const GLfloat
*values
)
979 struct gl_uniform
*uniform
;
982 ASSERT_OUTSIDE_BEGIN_END(ctx
);
984 if (!shProg
|| !shProg
->LinkStatus
) {
985 _mesa_error(ctx
, GL_INVALID_OPERATION
,
986 "glUniformMatrix(program not linked)");
991 return; /* The standard specifies this as a no-op */
994 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
998 split_location_offset(&location
, &offset
);
1000 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1001 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1004 if (values
== NULL
) {
1005 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1009 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1011 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1013 if (shProg
->VertexProgram
) {
1014 /* convert uniform location to program parameter index */
1015 GLint index
= uniform
->VertPos
;
1017 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
1019 count
, rows
, cols
, transpose
, values
);
1023 if (shProg
->FragmentProgram
) {
1024 /* convert uniform location to program parameter index */
1025 GLint index
= uniform
->FragPos
;
1027 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
1029 count
, rows
, cols
, transpose
, values
);
1033 if (shProg
->GeometryProgram
) {
1034 /* convert uniform location to program parameter index */
1035 GLint index
= uniform
->GeomPos
;
1037 set_program_uniform_matrix(ctx
, &shProg
->GeometryProgram
->Base
,
1039 count
, rows
, cols
, transpose
, values
);
1043 uniform
->Initialized
= GL_TRUE
;
1048 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1050 GET_CURRENT_CONTEXT(ctx
);
1051 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1055 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1057 GET_CURRENT_CONTEXT(ctx
);
1061 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1065 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1067 GET_CURRENT_CONTEXT(ctx
);
1072 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1076 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1079 GET_CURRENT_CONTEXT(ctx
);
1085 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1089 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1091 GET_CURRENT_CONTEXT(ctx
);
1092 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1096 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1098 GET_CURRENT_CONTEXT(ctx
);
1102 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1106 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1108 GET_CURRENT_CONTEXT(ctx
);
1113 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1117 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1119 GET_CURRENT_CONTEXT(ctx
);
1125 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1129 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1131 GET_CURRENT_CONTEXT(ctx
);
1132 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1136 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1138 GET_CURRENT_CONTEXT(ctx
);
1139 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1143 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1145 GET_CURRENT_CONTEXT(ctx
);
1146 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1150 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1152 GET_CURRENT_CONTEXT(ctx
);
1153 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1157 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1159 GET_CURRENT_CONTEXT(ctx
);
1160 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1164 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1166 GET_CURRENT_CONTEXT(ctx
);
1167 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1171 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1173 GET_CURRENT_CONTEXT(ctx
);
1174 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1178 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1185 /** OpenGL 3.0 GLuint-valued functions **/
1187 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1189 GET_CURRENT_CONTEXT(ctx
);
1190 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1194 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1196 GET_CURRENT_CONTEXT(ctx
);
1200 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1204 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1206 GET_CURRENT_CONTEXT(ctx
);
1211 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1215 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1217 GET_CURRENT_CONTEXT(ctx
);
1223 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1227 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1229 GET_CURRENT_CONTEXT(ctx
);
1230 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1234 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1236 GET_CURRENT_CONTEXT(ctx
);
1237 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1241 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1243 GET_CURRENT_CONTEXT(ctx
);
1244 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1248 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1250 GET_CURRENT_CONTEXT(ctx
);
1251 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1257 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1258 const GLfloat
* value
)
1260 GET_CURRENT_CONTEXT(ctx
);
1261 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1262 2, 2, location
, count
, transpose
, value
);
1266 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1267 const GLfloat
* value
)
1269 GET_CURRENT_CONTEXT(ctx
);
1270 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1271 3, 3, location
, count
, transpose
, value
);
1275 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1276 const GLfloat
* value
)
1278 GET_CURRENT_CONTEXT(ctx
);
1279 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1280 4, 4, location
, count
, transpose
, value
);
1285 * Non-square UniformMatrix are OpenGL 2.1
1288 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1289 const GLfloat
*value
)
1291 GET_CURRENT_CONTEXT(ctx
);
1292 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1293 2, 3, location
, count
, transpose
, value
);
1297 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1298 const GLfloat
*value
)
1300 GET_CURRENT_CONTEXT(ctx
);
1301 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1302 3, 2, location
, count
, transpose
, value
);
1306 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1307 const GLfloat
*value
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1311 2, 4, location
, count
, transpose
, value
);
1315 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1316 const GLfloat
*value
)
1318 GET_CURRENT_CONTEXT(ctx
);
1319 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1320 4, 2, location
, count
, transpose
, value
);
1324 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1325 const GLfloat
*value
)
1327 GET_CURRENT_CONTEXT(ctx
);
1328 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1329 3, 4, location
, count
, transpose
, value
);
1333 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1334 const GLfloat
*value
)
1336 GET_CURRENT_CONTEXT(ctx
);
1337 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1338 4, 3, location
, count
, transpose
, value
);
1343 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
1344 GLsizei bufSize
, GLfloat
*params
)
1346 GET_CURRENT_CONTEXT(ctx
);
1347 get_uniform(ctx
, program
, location
, bufSize
, GL_FLOAT
, params
);
1351 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1353 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
1358 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
1359 GLsizei bufSize
, GLint
*params
)
1361 GET_CURRENT_CONTEXT(ctx
);
1362 get_uniform(ctx
, program
, location
, bufSize
, GL_INT
, params
);
1366 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1368 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
1374 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
1375 GLsizei bufSize
, GLuint
*params
)
1377 GET_CURRENT_CONTEXT(ctx
);
1378 get_uniform(ctx
, program
, location
, bufSize
, GL_UNSIGNED_INT
, params
);
1382 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1384 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
1390 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
1391 GLsizei bufSize
, GLdouble
*params
)
1393 GET_CURRENT_CONTEXT(ctx
);
1395 get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
1397 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
1398 "(GL_ARB_gpu_shader_fp64 not implemented)");
1402 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
1404 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
1409 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1411 struct gl_shader_program
*shProg
;
1413 GET_CURRENT_CONTEXT(ctx
);
1415 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1416 "glGetUniformLocation");
1420 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1425 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1426 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1427 GLenum
* type
, GLcharARB
* name
)
1429 GET_CURRENT_CONTEXT(ctx
);
1430 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1436 * Plug in shader uniform-related functions into API dispatch table.
1439 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1442 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1443 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1444 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1445 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1446 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1447 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1448 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1449 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1450 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1451 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1452 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1453 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1454 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1455 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1456 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1457 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1458 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1459 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1460 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1462 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1463 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1464 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1465 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1468 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1469 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1470 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1471 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1472 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1473 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1476 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
1477 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
1478 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
1479 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
1480 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
1481 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
1482 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
1483 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
1484 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
1486 /* GL_ARB_robustness */
1487 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
1488 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
1489 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
1490 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
1492 #endif /* FEATURE_GL */