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 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 get_vector_elements(GLenum type
)
234 case GL_UNSIGNED_INT
:
235 default: /* Catch all the various sampler types. */
241 case GL_UNSIGNED_INT_VEC2
:
247 case GL_UNSIGNED_INT_VEC3
:
253 case GL_UNSIGNED_INT_VEC4
:
259 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
265 case GL_FLOAT_MAT2x3
:
269 case GL_FLOAT_MAT2x4
:
277 case GL_FLOAT_MAT3x2
:
281 case GL_FLOAT_MAT3x4
:
289 case GL_FLOAT_MAT4x2
:
293 case GL_FLOAT_MAT4x3
:
304 * Determine the number of rows and columns occupied by a uniform
305 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
306 * the number of rows = 1 and cols = number of elements in the vector.
309 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
310 GLint
*rows
, GLint
*cols
)
312 get_matrix_dims(p
->DataType
, rows
, cols
);
313 if (*rows
== 0 && *cols
== 0) {
314 /* not a matrix type, probably a float or vector */
316 *cols
= get_vector_elements(p
->DataType
);
322 * GLSL uniform arrays and structs require special handling.
324 * The GL_ARB_shader_objects spec says that if you use
325 * glGetUniformLocation to get the location of an array, you CANNOT
326 * access other elements of the array by adding an offset to the
327 * returned location. For example, you must call
328 * glGetUniformLocation("foo[16]") if you want to set the 16th element
329 * of the array with glUniform().
331 * HOWEVER, some other OpenGL drivers allow accessing array elements
332 * by adding an offset to the returned array location. And some apps
333 * seem to depend on that behaviour.
335 * Mesa's gl_uniform_list doesn't directly support this since each
336 * entry in the list describes one uniform variable, not one uniform
337 * element. We could insert dummy entries in the list for each array
338 * element after [0] but that causes complications elsewhere.
340 * We solve this problem by encoding two values in the location that's
341 * returned by glGetUniformLocation():
342 * a) index into gl_uniform_list::Uniforms[] for the uniform
343 * b) an array/field offset (0 for simple types)
345 * These two values are encoded in the high and low halves of a GLint.
346 * By putting the uniform number in the high part and the offset in the
347 * low part, we can support the unofficial ability to index into arrays
348 * by adding offsets to the location value.
351 merge_location_offset(GLint
*location
, GLint offset
)
353 *location
= (*location
<< 16) | offset
;
358 * Separate the uniform location and parameter offset. See above.
361 split_location_offset(GLint
*location
, GLint
*offset
)
363 *offset
= *location
& 0xffff;
364 *location
= *location
>> 16;
370 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
373 get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
374 GLsizei bufSize
, GLenum returnType
, GLvoid
*paramsOut
)
376 struct gl_shader_program
*shProg
=
377 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
378 struct gl_program
*prog
;
379 GLint paramPos
, offset
;
384 split_location_offset(&location
, &offset
);
386 if (!find_uniform_parameter_pos(shProg
, location
, &prog
, ¶mPos
)) {
387 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
390 const struct gl_program_parameter
*p
=
391 &prog
->Parameters
->Parameters
[paramPos
];
392 gl_constant_value (*values
)[4];
393 GLint rows
, cols
, i
, j
, k
;
397 values
= prog
->Parameters
->ParameterValues
+ paramPos
+ offset
;
399 get_uniform_rows_cols(p
, &rows
, &cols
);
401 numBytes
= rows
* cols
* _mesa_sizeof_type(returnType
);
402 if (bufSize
< numBytes
) {
403 _mesa_error( ctx
, GL_INVALID_OPERATION
,
404 "glGetnUniformfvARB(out of bounds: bufSize is %d,"
405 " but %d bytes are required)", bufSize
, numBytes
);
409 if (ctx
->Const
.NativeIntegers
) {
410 storage_type
= base_uniform_type(p
->DataType
);
412 storage_type
= GL_FLOAT
;
416 for (i
= 0; i
< rows
; i
++) {
417 for (j
= 0; j
< cols
; j
++ ) {
418 void *out
= (char *)paramsOut
+ 4 * k
;
420 switch (returnType
) {
422 switch (storage_type
) {
424 *(float *)out
= values
[i
][j
].f
;
427 case GL_BOOL
: /* boolean is just an integer 1 or 0. */
428 *(float *)out
= values
[i
][j
].i
;
430 case GL_UNSIGNED_INT
:
431 *(float *)out
= values
[i
][j
].u
;
437 case GL_UNSIGNED_INT
:
438 switch (storage_type
) {
440 /* While the GL 3.2 core spec doesn't explicitly
441 * state how conversion of float uniforms to integer
442 * values works, in section 6.2 "State Tables" on
445 * "Unless otherwise specified, when floating
446 * point state is returned as integer values or
447 * integer state is returned as floating-point
448 * values it is converted in the fashion
449 * described in section 6.1.2"
451 * That section, on page 248, says:
453 * "If GetIntegerv or GetInteger64v are called,
454 * a floating-point value is rounded to the
455 * nearest integer..."
457 *(int *)out
= IROUND(values
[i
][j
].f
);
461 case GL_UNSIGNED_INT
:
463 /* type conversions for these to int/uint are just
466 *(int *)out
= values
[i
][j
].i
;
481 * Called via glGetUniformLocation().
483 * The return value will encode two values, the uniform location and an
484 * offset (used for arrays, structs).
487 _mesa_get_uniform_location(struct gl_context
*ctx
,
488 struct gl_shader_program
*shProg
,
491 GLint offset
= 0, location
= -1;
493 if (shProg
->LinkStatus
== GL_FALSE
) {
494 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
498 /* XXX we should return -1 if the uniform was declared, but not
502 /* XXX we need to be able to parse uniform names for structs and arrays
509 /* handle 1-dimension arrays here... */
510 char *c
= strchr(name
, '[');
512 /* truncate name at [ */
513 const GLint len
= c
- name
;
514 GLchar
*newName
= malloc(len
+ 1);
516 return -1; /* out of mem */
517 memcpy(newName
, name
, len
);
520 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
522 const GLint element
= atoi(c
+ 1);
524 /* get type of the uniform array element */
525 const struct gl_program_parameter
*p
=
526 get_uniform_parameter(shProg
, location
);
529 get_matrix_dims(p
->DataType
, &rows
, &cols
);
532 offset
= element
* rows
;
542 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
546 merge_location_offset(&location
, offset
);
555 * Update the vertex/fragment program's TexturesUsed array.
557 * This needs to be called after glUniform(set sampler var) is called.
558 * A call to glUniform(samplerVar, value) causes a sampler to point to a
559 * particular texture unit. We know the sampler's texture target
560 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
561 * set by glUniform() calls.
563 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
564 * information to update the prog->TexturesUsed[] values.
565 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
566 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
567 * We'll use that info for state validation before rendering.
570 _mesa_update_shader_textures_used(struct gl_program
*prog
)
574 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
576 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
577 if (prog
->SamplersUsed
& (1 << s
)) {
578 GLuint unit
= prog
->SamplerUnits
[s
];
579 GLuint tgt
= prog
->SamplerTargets
[s
];
580 assert(unit
< Elements(prog
->TexturesUsed
));
581 assert(tgt
< NUM_TEXTURE_TARGETS
);
582 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
589 * Check if the type given by userType is allowed to set a uniform of the
590 * target type. Generally, equivalence is required, but setting Boolean
591 * uniforms can be done with glUniformiv or glUniformfv.
594 compatible_types(GLenum userType
, GLenum targetType
)
596 if (userType
== targetType
)
599 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
600 userType
== GL_UNSIGNED_INT
||
604 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
605 userType
== GL_UNSIGNED_INT_VEC2
||
606 userType
== GL_INT_VEC2
))
609 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
610 userType
== GL_UNSIGNED_INT_VEC3
||
611 userType
== GL_INT_VEC3
))
614 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
615 userType
== GL_UNSIGNED_INT_VEC4
||
616 userType
== GL_INT_VEC4
))
619 if (is_sampler_type(targetType
) && userType
== GL_INT
)
627 * Set the value of a program's uniform variable.
628 * \param program the program whose uniform to update
629 * \param index the index of the program parameter for the uniform
630 * \param offset additional parameter slot offset (for arrays)
631 * \param type the incoming datatype of 'values'
632 * \param count the number of uniforms to set
633 * \param elems number of elements per uniform (1, 2, 3 or 4)
634 * \param values the new values, of datatype 'type'
637 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
638 GLint index
, GLint offset
,
639 GLenum type
, GLsizei count
, GLint elems
,
642 const struct gl_program_parameter
*param
=
643 &program
->Parameters
->Parameters
[index
];
649 if (!compatible_types(type
, param
->DataType
)) {
650 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
654 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
659 if (param
->Type
== PROGRAM_SAMPLER
) {
660 /* This controls which texture unit which is used by a sampler */
661 GLboolean changed
= GL_FALSE
;
664 /* this should have been caught by the compatible_types() check */
665 ASSERT(type
== GL_INT
);
667 /* loop over number of samplers to change */
668 for (i
= 0; i
< count
; i
++) {
669 GLuint sampler
= (GLuint
)
670 program
->Parameters
->ParameterValues
[index
+offset
+ i
][0].f
;
671 GLuint texUnit
= ((GLuint
*) values
)[i
];
673 /* check that the sampler (tex unit index) is legal */
674 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
675 _mesa_error(ctx
, GL_INVALID_VALUE
,
676 "glUniform1(invalid sampler/tex unit index for '%s')",
681 /* This maps a sampler to a texture unit: */
682 if (sampler
< MAX_SAMPLERS
) {
684 printf("Set program %p sampler %d '%s' to unit %u\n",
685 program
, sampler
, param
->Name
, texUnit
);
687 if (program
->SamplerUnits
[sampler
] != texUnit
) {
688 program
->SamplerUnits
[sampler
] = texUnit
;
695 /* When a sampler's value changes it usually requires rewriting
696 * a GPU program's TEX instructions since there may not be a
697 * sampler->texture lookup table. We signal this with the
698 * ProgramStringNotify() callback.
700 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
701 _mesa_update_shader_textures_used(program
);
702 /* Do we need to care about the return value here?
703 * This should not be the first time the driver was notified of
706 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
710 /* ordinary uniform variable */
711 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
712 const GLenum basicType
= base_uniform_type(type
);
713 const GLint slots
= (param
->Size
+ 3) / 4;
714 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
717 if ((GLint
) param
->Size
> typeSize
) {
719 /* we'll ignore extra data below */
722 /* non-array: count must be at most one; count == 0 is handled
726 _mesa_error(ctx
, GL_INVALID_OPERATION
,
727 "glUniform(uniform '%s' is not an array)",
733 /* loop over number of array elements */
734 for (k
= 0; k
< count
; k
++) {
735 gl_constant_value
*uniformVal
;
737 if (offset
+ k
>= slots
) {
738 /* Extra array data is ignored */
742 /* uniformVal (the destination) is always gl_constant_value[4] */
743 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
745 if (basicType
== GL_INT
) {
746 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
747 for (i
= 0; i
< elems
; i
++) {
748 if (!ctx
->Const
.NativeIntegers
)
749 uniformVal
[i
].f
= (GLfloat
) iValues
[i
];
751 uniformVal
[i
].i
= iValues
[i
];
754 else if (basicType
== GL_UNSIGNED_INT
) {
755 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
756 for (i
= 0; i
< elems
; i
++) {
757 if (!ctx
->Const
.NativeIntegers
)
758 uniformVal
[i
].f
= (GLfloat
)(GLuint
) iValues
[i
];
760 uniformVal
[i
].u
= iValues
[i
];
764 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
765 assert(basicType
== GL_FLOAT
);
766 for (i
= 0; i
< elems
; i
++) {
767 uniformVal
[i
].f
= fValues
[i
];
771 /* if the uniform is bool-valued, convert to 1 or 0 */
773 for (i
= 0; i
< elems
; i
++) {
774 if (basicType
== GL_FLOAT
)
775 uniformVal
[i
].b
= uniformVal
[i
].f
!= 0.0f
? 1 : 0;
777 uniformVal
[i
].b
= uniformVal
[i
].u
? 1 : 0;
779 if (ctx
->Const
.NativeIntegers
)
781 uniformVal
[i
].b
? ctx
->Const
.UniformBooleanTrue
: 0;
783 uniformVal
[i
].f
= uniformVal
[i
].b
? 1.0f
: 0.0f
;
792 * Called via glUniform*() functions.
795 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
796 GLint location
, GLsizei count
,
797 const GLvoid
*values
, GLenum type
)
799 struct gl_uniform
*uniform
;
802 ASSERT_OUTSIDE_BEGIN_END(ctx
);
804 if (!shProg
|| !shProg
->LinkStatus
) {
805 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
810 return; /* The standard specifies this as a no-op */
813 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
818 split_location_offset(&location
, &offset
);
820 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
821 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
826 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
830 elems
= _mesa_sizeof_glsl_type(type
);
832 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
834 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
836 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
837 const GLenum basicType
= base_uniform_type(type
);
839 printf("Mesa: set program %u uniform %s (loc %d) to: ",
840 shProg
->Name
, uniform
->Name
, location
);
841 if (basicType
== GL_INT
) {
842 const GLint
*v
= (const GLint
*) values
;
843 for (i
= 0; i
< count
* elems
; i
++) {
847 else if (basicType
== GL_UNSIGNED_INT
) {
848 const GLuint
*v
= (const GLuint
*) values
;
849 for (i
= 0; i
< count
* elems
; i
++) {
854 const GLfloat
*v
= (const GLfloat
*) values
;
855 assert(basicType
== GL_FLOAT
);
856 for (i
= 0; i
< count
* elems
; i
++) {
863 /* A uniform var may be used by both a vertex shader and a fragment
864 * shader. We may need to update one or both shader's uniform here:
866 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
867 /* convert uniform location to program parameter index */
868 GLint index
= uniform
->VertPos
;
870 set_program_uniform(ctx
,
871 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
,
872 index
, offset
, type
, count
, elems
, values
);
876 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
877 /* convert uniform location to program parameter index */
878 GLint index
= uniform
->FragPos
;
880 set_program_uniform(ctx
,
881 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
,
882 index
, offset
, type
, count
, elems
, values
);
886 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
887 /* convert uniform location to program parameter index */
888 GLint index
= uniform
->GeomPos
;
890 set_program_uniform(ctx
,
891 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
,
892 index
, offset
, type
, count
, elems
, values
);
896 uniform
->Initialized
= GL_TRUE
;
901 * Set a matrix-valued program parameter.
904 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
905 GLuint index
, GLuint offset
,
906 GLuint count
, GLuint rows
, GLuint cols
,
907 GLboolean transpose
, const GLfloat
*values
)
909 GLuint mat
, row
, col
;
911 const struct gl_program_parameter
*param
=
912 &program
->Parameters
->Parameters
[index
];
913 const GLuint slots
= (param
->Size
+ 3) / 4;
914 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
917 /* check that the number of rows, columns is correct */
918 get_matrix_dims(param
->DataType
, &nr
, &nc
);
919 if (rows
!= nr
|| cols
!= nc
) {
920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
921 "glUniformMatrix(matrix size mismatch)");
925 if ((GLint
) param
->Size
<= typeSize
) {
926 /* non-array: count must be at most one; count == 0 is handled
930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
931 "glUniformMatrix(uniform is not an array)");
937 * Note: the _columns_ of a matrix are stored in program registers, not
938 * the rows. So, the loops below look a little funny.
939 * XXX could optimize this a bit...
942 /* loop over matrices */
943 for (mat
= 0; mat
< count
; mat
++) {
946 for (col
= 0; col
< cols
; col
++) {
948 if (offset
>= slots
) {
949 /* Ignore writes beyond the end of (the used part of) an array */
952 v
= (GLfloat
*) program
->Parameters
->ParameterValues
[index
+ offset
];
953 for (row
= 0; row
< rows
; row
++) {
955 v
[row
] = values
[src
+ row
* cols
+ col
];
958 v
[row
] = values
[src
+ col
* rows
+ row
];
965 src
+= rows
* cols
; /* next matrix */
971 * Called by glUniformMatrix*() functions.
972 * Note: cols=2, rows=4 ==> array[2] of vec4
975 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
976 GLint cols
, GLint rows
,
977 GLint location
, GLsizei count
,
978 GLboolean transpose
, const GLfloat
*values
)
980 struct gl_uniform
*uniform
;
983 ASSERT_OUTSIDE_BEGIN_END(ctx
);
985 if (!shProg
|| !shProg
->LinkStatus
) {
986 _mesa_error(ctx
, GL_INVALID_OPERATION
,
987 "glUniformMatrix(program not linked)");
992 return; /* The standard specifies this as a no-op */
995 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
999 split_location_offset(&location
, &offset
);
1001 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1002 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1005 if (values
== NULL
) {
1006 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1010 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1012 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1014 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
1015 /* convert uniform location to program parameter index */
1016 GLint index
= uniform
->VertPos
;
1018 set_program_uniform_matrix(ctx
,
1019 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
,
1021 count
, rows
, cols
, transpose
, values
);
1025 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
1026 /* convert uniform location to program parameter index */
1027 GLint index
= uniform
->FragPos
;
1029 set_program_uniform_matrix(ctx
,
1030 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
,
1032 count
, rows
, cols
, transpose
, values
);
1036 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
1037 /* convert uniform location to program parameter index */
1038 GLint index
= uniform
->GeomPos
;
1040 set_program_uniform_matrix(ctx
,
1041 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
,
1043 count
, rows
, cols
, transpose
, values
);
1047 uniform
->Initialized
= GL_TRUE
;
1052 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1054 GET_CURRENT_CONTEXT(ctx
);
1055 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1059 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1061 GET_CURRENT_CONTEXT(ctx
);
1065 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1069 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1071 GET_CURRENT_CONTEXT(ctx
);
1076 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1080 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1083 GET_CURRENT_CONTEXT(ctx
);
1089 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1093 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1095 GET_CURRENT_CONTEXT(ctx
);
1096 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1100 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1102 GET_CURRENT_CONTEXT(ctx
);
1106 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1110 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1112 GET_CURRENT_CONTEXT(ctx
);
1117 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1121 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1123 GET_CURRENT_CONTEXT(ctx
);
1129 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1133 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1135 GET_CURRENT_CONTEXT(ctx
);
1136 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1140 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1142 GET_CURRENT_CONTEXT(ctx
);
1143 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1147 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1149 GET_CURRENT_CONTEXT(ctx
);
1150 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1154 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1156 GET_CURRENT_CONTEXT(ctx
);
1157 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1161 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1168 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1170 GET_CURRENT_CONTEXT(ctx
);
1171 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1175 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1177 GET_CURRENT_CONTEXT(ctx
);
1178 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1182 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1184 GET_CURRENT_CONTEXT(ctx
);
1185 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1189 /** OpenGL 3.0 GLuint-valued functions **/
1191 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1193 GET_CURRENT_CONTEXT(ctx
);
1194 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1198 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1200 GET_CURRENT_CONTEXT(ctx
);
1204 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1208 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1210 GET_CURRENT_CONTEXT(ctx
);
1215 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1219 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1221 GET_CURRENT_CONTEXT(ctx
);
1227 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1231 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1233 GET_CURRENT_CONTEXT(ctx
);
1234 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1238 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1240 GET_CURRENT_CONTEXT(ctx
);
1241 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1245 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1247 GET_CURRENT_CONTEXT(ctx
);
1248 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1252 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1254 GET_CURRENT_CONTEXT(ctx
);
1255 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1261 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1262 const GLfloat
* value
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1266 2, 2, location
, count
, transpose
, value
);
1270 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1271 const GLfloat
* value
)
1273 GET_CURRENT_CONTEXT(ctx
);
1274 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1275 3, 3, location
, count
, transpose
, value
);
1279 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1280 const GLfloat
* value
)
1282 GET_CURRENT_CONTEXT(ctx
);
1283 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1284 4, 4, location
, count
, transpose
, value
);
1289 * Non-square UniformMatrix are OpenGL 2.1
1292 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1293 const GLfloat
*value
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1297 2, 3, location
, count
, transpose
, value
);
1301 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1302 const GLfloat
*value
)
1304 GET_CURRENT_CONTEXT(ctx
);
1305 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1306 3, 2, location
, count
, transpose
, value
);
1310 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1311 const GLfloat
*value
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1315 2, 4, location
, count
, transpose
, value
);
1319 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1320 const GLfloat
*value
)
1322 GET_CURRENT_CONTEXT(ctx
);
1323 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1324 4, 2, location
, count
, transpose
, value
);
1328 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1329 const GLfloat
*value
)
1331 GET_CURRENT_CONTEXT(ctx
);
1332 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1333 3, 4, location
, count
, transpose
, value
);
1337 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1338 const GLfloat
*value
)
1340 GET_CURRENT_CONTEXT(ctx
);
1341 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1342 4, 3, location
, count
, transpose
, value
);
1347 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
1348 GLsizei bufSize
, GLfloat
*params
)
1350 GET_CURRENT_CONTEXT(ctx
);
1351 get_uniform(ctx
, program
, location
, bufSize
, GL_FLOAT
, params
);
1355 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1357 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
1362 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
1363 GLsizei bufSize
, GLint
*params
)
1365 GET_CURRENT_CONTEXT(ctx
);
1366 get_uniform(ctx
, program
, location
, bufSize
, GL_INT
, params
);
1370 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1372 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
1378 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
1379 GLsizei bufSize
, GLuint
*params
)
1381 GET_CURRENT_CONTEXT(ctx
);
1382 get_uniform(ctx
, program
, location
, bufSize
, GL_UNSIGNED_INT
, params
);
1386 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1388 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
1394 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
1395 GLsizei bufSize
, GLdouble
*params
)
1397 GET_CURRENT_CONTEXT(ctx
);
1405 get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
1407 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
1408 "(GL_ARB_gpu_shader_fp64 not implemented)");
1412 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
1414 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
1419 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1421 struct gl_shader_program
*shProg
;
1423 GET_CURRENT_CONTEXT(ctx
);
1425 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1426 "glGetUniformLocation");
1430 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1435 * Plug in shader uniform-related functions into API dispatch table.
1438 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1441 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1442 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1443 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1444 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1445 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1446 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1447 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1448 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1449 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1450 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1451 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1452 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1453 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1454 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1455 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1456 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1457 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1458 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1459 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1461 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1462 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1463 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1464 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1467 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1468 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1469 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1470 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1471 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1472 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1475 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
1476 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
1477 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
1478 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
1479 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
1480 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
1481 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
1482 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
1483 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
1485 /* GL_ARB_robustness */
1486 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
1487 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
1488 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
1489 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
1491 #endif /* FEATURE_GL */