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
)
78 case GL_UNSIGNED_INT_VEC2
:
79 case GL_UNSIGNED_INT_VEC3
:
80 case GL_UNSIGNED_INT_VEC4
:
81 return GL_UNSIGNED_INT
;
88 _mesa_problem(NULL
, "Invalid type in base_uniform_type()");
95 is_boolean_type(GLenum type
)
110 is_sampler_type(GLenum type
)
114 case GL_INT_SAMPLER_1D
:
115 case GL_UNSIGNED_INT_SAMPLER_1D
:
117 case GL_INT_SAMPLER_2D
:
118 case GL_UNSIGNED_INT_SAMPLER_2D
:
120 case GL_INT_SAMPLER_3D
:
121 case GL_UNSIGNED_INT_SAMPLER_3D
:
122 case GL_SAMPLER_CUBE
:
123 case GL_INT_SAMPLER_CUBE
:
124 case GL_UNSIGNED_INT_SAMPLER_CUBE
:
125 case GL_SAMPLER_1D_SHADOW
:
126 case GL_SAMPLER_2D_SHADOW
:
127 case GL_SAMPLER_CUBE_SHADOW
:
128 case GL_SAMPLER_2D_RECT_ARB
:
129 case GL_INT_SAMPLER_2D_RECT
:
130 case GL_UNSIGNED_INT_SAMPLER_2D_RECT
:
131 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
132 case GL_SAMPLER_1D_ARRAY_EXT
:
133 case GL_INT_SAMPLER_1D_ARRAY
:
134 case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY
:
135 case GL_SAMPLER_2D_ARRAY_EXT
:
136 case GL_INT_SAMPLER_2D_ARRAY
:
137 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY
:
138 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
139 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
140 case GL_SAMPLER_CUBE_MAP_ARRAY
:
141 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW
:
142 case GL_SAMPLER_BUFFER
:
143 case GL_INT_SAMPLER_BUFFER
:
144 case GL_UNSIGNED_INT_SAMPLER_BUFFER
:
145 case GL_SAMPLER_2D_MULTISAMPLE
:
146 case GL_INT_SAMPLER_2D_MULTISAMPLE
:
147 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE
:
148 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY
:
149 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY
:
150 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY
:
159 * Given a uniform index, return the vertex/geometry/fragment program
160 * that has that parameter, plus the position of the parameter in the
161 * parameter/constant buffer.
162 * \param shProg the shader program
163 * \param index the uniform index in [0, NumUniforms-1]
164 * \param progOut returns containing program
165 * \param posOut returns position of the uniform in the param/const buffer
166 * \return GL_TRUE for success, GL_FALSE for invalid index
169 find_uniform_parameter_pos(struct gl_shader_program
*shProg
, GLint index
,
170 struct gl_program
**progOut
, GLint
*posOut
)
172 struct gl_program
*prog
= NULL
;
175 if (!shProg
->Uniforms
||
177 index
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
181 pos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
183 prog
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
186 pos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
188 prog
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
191 pos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
193 prog
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
;
198 if (!prog
|| pos
< 0)
199 return GL_FALSE
; /* should really never happen */
209 * Return pointer to a gl_program_parameter which corresponds to a uniform.
210 * \param shProg the shader program
211 * \param index the uniform index in [0, NumUniforms-1]
212 * \return gl_program_parameter point or NULL if index is invalid
214 static const struct gl_program_parameter
*
215 get_uniform_parameter(struct gl_shader_program
*shProg
, GLint index
)
217 struct gl_program
*prog
;
220 if (find_uniform_parameter_pos(shProg
, index
, &prog
, &progPos
))
221 return &prog
->Parameters
->Parameters
[progPos
];
228 * Called by glGetActiveUniform().
231 _mesa_get_active_uniform(struct gl_context
*ctx
, GLuint program
, GLuint index
,
232 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
233 GLenum
*type
, GLchar
*nameOut
)
235 struct gl_shader_program
*shProg
=
236 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
237 const struct gl_program_parameter
*param
;
242 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
243 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
247 param
= get_uniform_parameter(shProg
, index
);
252 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
256 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
257 if ((GLint
) param
->Size
> typeSize
) {
259 * Array elements are placed on vector[4] boundaries so they're
260 * a multiple of four floats. We round typeSize up to next multiple
261 * of four to get the right size below.
263 typeSize
= (typeSize
+ 3) & ~3;
265 /* Note that the returned size is in units of the <type>, not bytes */
266 *size
= param
->Size
/ typeSize
;
270 *type
= param
->DataType
;
276 get_vector_elements(GLenum type
)
282 case GL_UNSIGNED_INT
:
283 default: /* Catch all the various sampler types. */
289 case GL_UNSIGNED_INT_VEC2
:
295 case GL_UNSIGNED_INT_VEC3
:
301 case GL_UNSIGNED_INT_VEC4
:
307 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
313 case GL_FLOAT_MAT2x3
:
317 case GL_FLOAT_MAT2x4
:
325 case GL_FLOAT_MAT3x2
:
329 case GL_FLOAT_MAT3x4
:
337 case GL_FLOAT_MAT4x2
:
341 case GL_FLOAT_MAT4x3
:
352 * Determine the number of rows and columns occupied by a uniform
353 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
354 * the number of rows = 1 and cols = number of elements in the vector.
357 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
358 GLint
*rows
, GLint
*cols
)
360 get_matrix_dims(p
->DataType
, rows
, cols
);
361 if (*rows
== 0 && *cols
== 0) {
362 /* not a matrix type, probably a float or vector */
364 *cols
= get_vector_elements(p
->DataType
);
370 * GLSL uniform arrays and structs require special handling.
372 * The GL_ARB_shader_objects spec says that if you use
373 * glGetUniformLocation to get the location of an array, you CANNOT
374 * access other elements of the array by adding an offset to the
375 * returned location. For example, you must call
376 * glGetUniformLocation("foo[16]") if you want to set the 16th element
377 * of the array with glUniform().
379 * HOWEVER, some other OpenGL drivers allow accessing array elements
380 * by adding an offset to the returned array location. And some apps
381 * seem to depend on that behaviour.
383 * Mesa's gl_uniform_list doesn't directly support this since each
384 * entry in the list describes one uniform variable, not one uniform
385 * element. We could insert dummy entries in the list for each array
386 * element after [0] but that causes complications elsewhere.
388 * We solve this problem by encoding two values in the location that's
389 * returned by glGetUniformLocation():
390 * a) index into gl_uniform_list::Uniforms[] for the uniform
391 * b) an array/field offset (0 for simple types)
393 * These two values are encoded in the high and low halves of a GLint.
394 * By putting the uniform number in the high part and the offset in the
395 * low part, we can support the unofficial ability to index into arrays
396 * by adding offsets to the location value.
399 merge_location_offset(GLint
*location
, GLint offset
)
401 *location
= (*location
<< 16) | offset
;
406 * Separate the uniform location and parameter offset. See above.
409 split_location_offset(GLint
*location
, GLint
*offset
)
411 *offset
= *location
& 0xffff;
412 *location
= *location
>> 16;
418 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
421 get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
422 GLsizei bufSize
, GLenum returnType
, GLvoid
*paramsOut
)
424 struct gl_shader_program
*shProg
=
425 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
426 struct gl_program
*prog
;
427 GLint paramPos
, offset
;
432 split_location_offset(&location
, &offset
);
434 if (!find_uniform_parameter_pos(shProg
, location
, &prog
, ¶mPos
)) {
435 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
438 const struct gl_program_parameter
*p
=
439 &prog
->Parameters
->Parameters
[paramPos
];
440 gl_constant_value (*values
)[4];
441 GLint rows
, cols
, i
, j
, k
;
445 values
= prog
->Parameters
->ParameterValues
+ paramPos
+ offset
;
447 get_uniform_rows_cols(p
, &rows
, &cols
);
449 numBytes
= rows
* cols
* _mesa_sizeof_type(returnType
);
450 if (bufSize
< numBytes
) {
451 _mesa_error( ctx
, GL_INVALID_OPERATION
,
452 "glGetnUniformfvARB(out of bounds: bufSize is %d,"
453 " but %d bytes are required)", bufSize
, numBytes
);
457 if (ctx
->Const
.NativeIntegers
) {
458 storage_type
= base_uniform_type(p
->DataType
);
460 storage_type
= GL_FLOAT
;
464 for (i
= 0; i
< rows
; i
++) {
465 for (j
= 0; j
< cols
; j
++ ) {
466 void *out
= (char *)paramsOut
+ 4 * k
;
468 switch (returnType
) {
470 switch (storage_type
) {
472 *(float *)out
= values
[i
][j
].f
;
475 case GL_BOOL
: /* boolean is just an integer 1 or 0. */
476 *(float *)out
= values
[i
][j
].i
;
478 case GL_UNSIGNED_INT
:
479 *(float *)out
= values
[i
][j
].u
;
485 case GL_UNSIGNED_INT
:
486 switch (storage_type
) {
488 /* While the GL 3.2 core spec doesn't explicitly
489 * state how conversion of float uniforms to integer
490 * values works, in section 6.2 "State Tables" on
493 * "Unless otherwise specified, when floating
494 * point state is returned as integer values or
495 * integer state is returned as floating-point
496 * values it is converted in the fashion
497 * described in section 6.1.2"
499 * That section, on page 248, says:
501 * "If GetIntegerv or GetInteger64v are called,
502 * a floating-point value is rounded to the
503 * nearest integer..."
505 *(int *)out
= IROUND(values
[i
][j
].f
);
509 case GL_UNSIGNED_INT
:
511 /* type conversions for these to int/uint are just
514 *(int *)out
= values
[i
][j
].i
;
529 * Called via glGetUniformLocation().
531 * The return value will encode two values, the uniform location and an
532 * offset (used for arrays, structs).
535 _mesa_get_uniform_location(struct gl_context
*ctx
,
536 struct gl_shader_program
*shProg
,
539 GLint offset
= 0, location
= -1;
541 if (shProg
->LinkStatus
== GL_FALSE
) {
542 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
546 /* XXX we should return -1 if the uniform was declared, but not
550 /* XXX we need to be able to parse uniform names for structs and arrays
557 /* handle 1-dimension arrays here... */
558 char *c
= strchr(name
, '[');
560 /* truncate name at [ */
561 const GLint len
= c
- name
;
562 GLchar
*newName
= malloc(len
+ 1);
564 return -1; /* out of mem */
565 memcpy(newName
, name
, len
);
568 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
570 const GLint element
= atoi(c
+ 1);
572 /* get type of the uniform array element */
573 const struct gl_program_parameter
*p
=
574 get_uniform_parameter(shProg
, location
);
577 get_matrix_dims(p
->DataType
, &rows
, &cols
);
580 offset
= element
* rows
;
590 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
594 merge_location_offset(&location
, offset
);
603 * Update the vertex/fragment program's TexturesUsed array.
605 * This needs to be called after glUniform(set sampler var) is called.
606 * A call to glUniform(samplerVar, value) causes a sampler to point to a
607 * particular texture unit. We know the sampler's texture target
608 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
609 * set by glUniform() calls.
611 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
612 * information to update the prog->TexturesUsed[] values.
613 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
614 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
615 * We'll use that info for state validation before rendering.
618 _mesa_update_shader_textures_used(struct gl_program
*prog
)
622 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
624 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
625 if (prog
->SamplersUsed
& (1 << s
)) {
626 GLuint unit
= prog
->SamplerUnits
[s
];
627 GLuint tgt
= prog
->SamplerTargets
[s
];
628 assert(unit
< Elements(prog
->TexturesUsed
));
629 assert(tgt
< NUM_TEXTURE_TARGETS
);
630 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
637 * Check if the type given by userType is allowed to set a uniform of the
638 * target type. Generally, equivalence is required, but setting Boolean
639 * uniforms can be done with glUniformiv or glUniformfv.
642 compatible_types(GLenum userType
, GLenum targetType
)
644 if (userType
== targetType
)
647 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
648 userType
== GL_UNSIGNED_INT
||
652 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
653 userType
== GL_UNSIGNED_INT_VEC2
||
654 userType
== GL_INT_VEC2
))
657 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
658 userType
== GL_UNSIGNED_INT_VEC3
||
659 userType
== GL_INT_VEC3
))
662 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
663 userType
== GL_UNSIGNED_INT_VEC4
||
664 userType
== GL_INT_VEC4
))
667 if (is_sampler_type(targetType
) && userType
== GL_INT
)
675 * Set the value of a program's uniform variable.
676 * \param program the program whose uniform to update
677 * \param index the index of the program parameter for the uniform
678 * \param offset additional parameter slot offset (for arrays)
679 * \param type the incoming datatype of 'values'
680 * \param count the number of uniforms to set
681 * \param elems number of elements per uniform (1, 2, 3 or 4)
682 * \param values the new values, of datatype 'type'
685 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
686 GLint index
, GLint offset
,
687 GLenum type
, GLsizei count
, GLint elems
,
690 const struct gl_program_parameter
*param
=
691 &program
->Parameters
->Parameters
[index
];
697 if (!compatible_types(type
, param
->DataType
)) {
698 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
702 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
707 if (param
->Type
== PROGRAM_SAMPLER
) {
708 /* This controls which texture unit which is used by a sampler */
709 GLboolean changed
= GL_FALSE
;
712 /* this should have been caught by the compatible_types() check */
713 ASSERT(type
== GL_INT
);
715 /* loop over number of samplers to change */
716 for (i
= 0; i
< count
; i
++) {
717 GLuint sampler
= (GLuint
)
718 program
->Parameters
->ParameterValues
[index
+offset
+ i
][0].f
;
719 GLuint texUnit
= ((GLuint
*) values
)[i
];
721 /* check that the sampler (tex unit index) is legal */
722 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
723 _mesa_error(ctx
, GL_INVALID_VALUE
,
724 "glUniform1(invalid sampler/tex unit index for '%s')",
729 /* This maps a sampler to a texture unit: */
730 if (sampler
< MAX_SAMPLERS
) {
732 printf("Set program %p sampler %d '%s' to unit %u\n",
733 program
, sampler
, param
->Name
, texUnit
);
735 if (program
->SamplerUnits
[sampler
] != texUnit
) {
736 program
->SamplerUnits
[sampler
] = texUnit
;
743 /* When a sampler's value changes it usually requires rewriting
744 * a GPU program's TEX instructions since there may not be a
745 * sampler->texture lookup table. We signal this with the
746 * ProgramStringNotify() callback.
748 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
749 _mesa_update_shader_textures_used(program
);
750 /* Do we need to care about the return value here?
751 * This should not be the first time the driver was notified of
754 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
758 /* ordinary uniform variable */
759 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
760 const GLenum basicType
= base_uniform_type(type
);
761 const GLint slots
= (param
->Size
+ 3) / 4;
762 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
765 if ((GLint
) param
->Size
> typeSize
) {
767 /* we'll ignore extra data below */
770 /* non-array: count must be at most one; count == 0 is handled
774 _mesa_error(ctx
, GL_INVALID_OPERATION
,
775 "glUniform(uniform '%s' is not an array)",
781 /* loop over number of array elements */
782 for (k
= 0; k
< count
; k
++) {
783 gl_constant_value
*uniformVal
;
785 if (offset
+ k
>= slots
) {
786 /* Extra array data is ignored */
790 /* uniformVal (the destination) is always gl_constant_value[4] */
791 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
793 if (basicType
== GL_INT
) {
794 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
795 for (i
= 0; i
< elems
; i
++) {
796 if (!ctx
->Const
.NativeIntegers
)
797 uniformVal
[i
].f
= (GLfloat
) iValues
[i
];
799 uniformVal
[i
].i
= iValues
[i
];
802 else if (basicType
== GL_UNSIGNED_INT
) {
803 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
804 for (i
= 0; i
< elems
; i
++) {
805 if (!ctx
->Const
.NativeIntegers
)
806 uniformVal
[i
].f
= (GLfloat
)(GLuint
) iValues
[i
];
808 uniformVal
[i
].u
= iValues
[i
];
812 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
813 assert(basicType
== GL_FLOAT
);
814 for (i
= 0; i
< elems
; i
++) {
815 uniformVal
[i
].f
= fValues
[i
];
819 /* if the uniform is bool-valued, convert to 1 or 0 */
821 for (i
= 0; i
< elems
; i
++) {
822 if (basicType
== GL_FLOAT
)
823 uniformVal
[i
].b
= uniformVal
[i
].f
!= 0.0f
? 1 : 0;
825 uniformVal
[i
].b
= uniformVal
[i
].u
? 1 : 0;
827 if (ctx
->Const
.NativeIntegers
)
829 uniformVal
[i
].b
? ctx
->Const
.UniformBooleanTrue
: 0;
831 uniformVal
[i
].f
= uniformVal
[i
].b
? 1.0f
: 0.0f
;
840 * Called via glUniform*() functions.
843 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
844 GLint location
, GLsizei count
,
845 const GLvoid
*values
, GLenum type
)
847 struct gl_uniform
*uniform
;
850 ASSERT_OUTSIDE_BEGIN_END(ctx
);
852 if (!shProg
|| !shProg
->LinkStatus
) {
853 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
858 return; /* The standard specifies this as a no-op */
861 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
866 split_location_offset(&location
, &offset
);
868 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
869 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
874 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
878 elems
= _mesa_sizeof_glsl_type(type
);
880 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
882 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
884 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
885 const GLenum basicType
= base_uniform_type(type
);
887 printf("Mesa: set program %u uniform %s (loc %d) to: ",
888 shProg
->Name
, uniform
->Name
, location
);
889 if (basicType
== GL_INT
) {
890 const GLint
*v
= (const GLint
*) values
;
891 for (i
= 0; i
< count
* elems
; i
++) {
895 else if (basicType
== GL_UNSIGNED_INT
) {
896 const GLuint
*v
= (const GLuint
*) values
;
897 for (i
= 0; i
< count
* elems
; i
++) {
902 const GLfloat
*v
= (const GLfloat
*) values
;
903 assert(basicType
== GL_FLOAT
);
904 for (i
= 0; i
< count
* elems
; i
++) {
911 /* A uniform var may be used by both a vertex shader and a fragment
912 * shader. We may need to update one or both shader's uniform here:
914 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
915 /* convert uniform location to program parameter index */
916 GLint index
= uniform
->VertPos
;
918 set_program_uniform(ctx
,
919 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
,
920 index
, offset
, type
, count
, elems
, values
);
924 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
925 /* convert uniform location to program parameter index */
926 GLint index
= uniform
->FragPos
;
928 set_program_uniform(ctx
,
929 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
,
930 index
, offset
, type
, count
, elems
, values
);
934 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
935 /* convert uniform location to program parameter index */
936 GLint index
= uniform
->GeomPos
;
938 set_program_uniform(ctx
,
939 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
,
940 index
, offset
, type
, count
, elems
, values
);
944 uniform
->Initialized
= GL_TRUE
;
949 * Set a matrix-valued program parameter.
952 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
953 GLuint index
, GLuint offset
,
954 GLuint count
, GLuint rows
, GLuint cols
,
955 GLboolean transpose
, const GLfloat
*values
)
957 GLuint mat
, row
, col
;
959 const struct gl_program_parameter
*param
=
960 &program
->Parameters
->Parameters
[index
];
961 const GLuint slots
= (param
->Size
+ 3) / 4;
962 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
965 /* check that the number of rows, columns is correct */
966 get_matrix_dims(param
->DataType
, &nr
, &nc
);
967 if (rows
!= nr
|| cols
!= nc
) {
968 _mesa_error(ctx
, GL_INVALID_OPERATION
,
969 "glUniformMatrix(matrix size mismatch)");
973 if ((GLint
) param
->Size
<= typeSize
) {
974 /* non-array: count must be at most one; count == 0 is handled
978 _mesa_error(ctx
, GL_INVALID_OPERATION
,
979 "glUniformMatrix(uniform is not an array)");
985 * Note: the _columns_ of a matrix are stored in program registers, not
986 * the rows. So, the loops below look a little funny.
987 * XXX could optimize this a bit...
990 /* loop over matrices */
991 for (mat
= 0; mat
< count
; mat
++) {
994 for (col
= 0; col
< cols
; col
++) {
996 if (offset
>= slots
) {
997 /* Ignore writes beyond the end of (the used part of) an array */
1000 v
= (GLfloat
*) program
->Parameters
->ParameterValues
[index
+ offset
];
1001 for (row
= 0; row
< rows
; row
++) {
1003 v
[row
] = values
[src
+ row
* cols
+ col
];
1006 v
[row
] = values
[src
+ col
* rows
+ row
];
1013 src
+= rows
* cols
; /* next matrix */
1019 * Called by glUniformMatrix*() functions.
1020 * Note: cols=2, rows=4 ==> array[2] of vec4
1023 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1024 GLint cols
, GLint rows
,
1025 GLint location
, GLsizei count
,
1026 GLboolean transpose
, const GLfloat
*values
)
1028 struct gl_uniform
*uniform
;
1031 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1033 if (!shProg
|| !shProg
->LinkStatus
) {
1034 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1035 "glUniformMatrix(program not linked)");
1040 return; /* The standard specifies this as a no-op */
1042 if (location
< -1) {
1043 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
1047 split_location_offset(&location
, &offset
);
1049 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1050 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1053 if (values
== NULL
) {
1054 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1058 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1060 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1062 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
1063 /* convert uniform location to program parameter index */
1064 GLint index
= uniform
->VertPos
;
1066 set_program_uniform_matrix(ctx
,
1067 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
,
1069 count
, rows
, cols
, transpose
, values
);
1073 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
1074 /* convert uniform location to program parameter index */
1075 GLint index
= uniform
->FragPos
;
1077 set_program_uniform_matrix(ctx
,
1078 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
,
1080 count
, rows
, cols
, transpose
, values
);
1084 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
1085 /* convert uniform location to program parameter index */
1086 GLint index
= uniform
->GeomPos
;
1088 set_program_uniform_matrix(ctx
,
1089 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
,
1091 count
, rows
, cols
, transpose
, values
);
1095 uniform
->Initialized
= GL_TRUE
;
1100 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1102 GET_CURRENT_CONTEXT(ctx
);
1103 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1107 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1109 GET_CURRENT_CONTEXT(ctx
);
1113 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1117 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1119 GET_CURRENT_CONTEXT(ctx
);
1124 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1128 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1131 GET_CURRENT_CONTEXT(ctx
);
1137 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1141 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1143 GET_CURRENT_CONTEXT(ctx
);
1144 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1148 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1150 GET_CURRENT_CONTEXT(ctx
);
1154 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1158 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1160 GET_CURRENT_CONTEXT(ctx
);
1165 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1169 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1171 GET_CURRENT_CONTEXT(ctx
);
1177 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1181 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1183 GET_CURRENT_CONTEXT(ctx
);
1184 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1188 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1195 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1197 GET_CURRENT_CONTEXT(ctx
);
1198 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1202 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1204 GET_CURRENT_CONTEXT(ctx
);
1205 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1209 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1211 GET_CURRENT_CONTEXT(ctx
);
1212 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1216 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1218 GET_CURRENT_CONTEXT(ctx
);
1219 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1223 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1225 GET_CURRENT_CONTEXT(ctx
);
1226 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1230 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1232 GET_CURRENT_CONTEXT(ctx
);
1233 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1237 /** OpenGL 3.0 GLuint-valued functions **/
1239 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1241 GET_CURRENT_CONTEXT(ctx
);
1242 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1246 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1248 GET_CURRENT_CONTEXT(ctx
);
1252 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1256 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1258 GET_CURRENT_CONTEXT(ctx
);
1263 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1267 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1269 GET_CURRENT_CONTEXT(ctx
);
1275 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1279 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1281 GET_CURRENT_CONTEXT(ctx
);
1282 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1286 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1288 GET_CURRENT_CONTEXT(ctx
);
1289 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1293 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1300 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1302 GET_CURRENT_CONTEXT(ctx
);
1303 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1309 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1310 const GLfloat
* value
)
1312 GET_CURRENT_CONTEXT(ctx
);
1313 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1314 2, 2, location
, count
, transpose
, value
);
1318 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1319 const GLfloat
* value
)
1321 GET_CURRENT_CONTEXT(ctx
);
1322 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1323 3, 3, location
, count
, transpose
, value
);
1327 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1328 const GLfloat
* value
)
1330 GET_CURRENT_CONTEXT(ctx
);
1331 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1332 4, 4, location
, count
, transpose
, value
);
1337 * Non-square UniformMatrix are OpenGL 2.1
1340 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1341 const GLfloat
*value
)
1343 GET_CURRENT_CONTEXT(ctx
);
1344 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1345 2, 3, location
, count
, transpose
, value
);
1349 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1350 const GLfloat
*value
)
1352 GET_CURRENT_CONTEXT(ctx
);
1353 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1354 3, 2, location
, count
, transpose
, value
);
1358 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1359 const GLfloat
*value
)
1361 GET_CURRENT_CONTEXT(ctx
);
1362 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1363 2, 4, location
, count
, transpose
, value
);
1367 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1368 const GLfloat
*value
)
1370 GET_CURRENT_CONTEXT(ctx
);
1371 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1372 4, 2, location
, count
, transpose
, value
);
1376 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1377 const GLfloat
*value
)
1379 GET_CURRENT_CONTEXT(ctx
);
1380 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1381 3, 4, location
, count
, transpose
, value
);
1385 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1386 const GLfloat
*value
)
1388 GET_CURRENT_CONTEXT(ctx
);
1389 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1390 4, 3, location
, count
, transpose
, value
);
1395 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
1396 GLsizei bufSize
, GLfloat
*params
)
1398 GET_CURRENT_CONTEXT(ctx
);
1399 get_uniform(ctx
, program
, location
, bufSize
, GL_FLOAT
, params
);
1403 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1405 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
1410 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
1411 GLsizei bufSize
, GLint
*params
)
1413 GET_CURRENT_CONTEXT(ctx
);
1414 get_uniform(ctx
, program
, location
, bufSize
, GL_INT
, params
);
1418 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1420 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
1426 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
1427 GLsizei bufSize
, GLuint
*params
)
1429 GET_CURRENT_CONTEXT(ctx
);
1430 get_uniform(ctx
, program
, location
, bufSize
, GL_UNSIGNED_INT
, params
);
1434 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1436 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
1442 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
1443 GLsizei bufSize
, GLdouble
*params
)
1445 GET_CURRENT_CONTEXT(ctx
);
1453 get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
1455 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
1456 "(GL_ARB_gpu_shader_fp64 not implemented)");
1460 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
1462 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
1467 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1469 struct gl_shader_program
*shProg
;
1471 GET_CURRENT_CONTEXT(ctx
);
1473 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1474 "glGetUniformLocation");
1478 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1483 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1484 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1485 GLenum
* type
, GLcharARB
* name
)
1487 GET_CURRENT_CONTEXT(ctx
);
1488 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1494 * Plug in shader uniform-related functions into API dispatch table.
1497 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1500 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1501 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1502 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1503 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1504 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1505 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1506 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1507 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1508 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1509 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1510 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1511 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1512 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1513 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1514 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1515 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1516 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1517 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1518 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1520 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1521 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1522 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1523 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1526 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1527 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1528 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1529 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1530 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1531 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1534 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
1535 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
1536 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
1537 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
1538 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
1539 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
1540 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
1541 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
1542 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
1544 /* GL_ARB_robustness */
1545 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
1546 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
1547 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
1548 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
1550 #endif /* FEATURE_GL */