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
:
151 case GL_SAMPLER_EXTERNAL_OES
:
160 * Given a uniform index, return the vertex/geometry/fragment program
161 * that has that parameter, plus the position of the parameter in the
162 * parameter/constant buffer.
163 * \param shProg the shader program
164 * \param index the uniform index in [0, NumUniforms-1]
165 * \param progOut returns containing program
166 * \param posOut returns position of the uniform in the param/const buffer
167 * \return GL_TRUE for success, GL_FALSE for invalid index
170 find_uniform_parameter_pos(struct gl_shader_program
*shProg
, GLint index
,
171 struct gl_program
**progOut
, GLint
*posOut
)
173 struct gl_program
*prog
= NULL
;
176 if (!shProg
->Uniforms
||
178 index
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
182 pos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
184 prog
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
187 pos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
189 prog
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
192 pos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
194 prog
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
;
199 if (!prog
|| pos
< 0)
200 return GL_FALSE
; /* should really never happen */
210 * Return pointer to a gl_program_parameter which corresponds to a uniform.
211 * \param shProg the shader program
212 * \param index the uniform index in [0, NumUniforms-1]
213 * \return gl_program_parameter point or NULL if index is invalid
215 const struct gl_program_parameter
*
216 get_uniform_parameter(struct gl_shader_program
*shProg
, GLint index
)
218 struct gl_program
*prog
;
221 if (find_uniform_parameter_pos(shProg
, index
, &prog
, &progPos
))
222 return &prog
->Parameters
->Parameters
[progPos
];
229 get_vector_elements(GLenum type
)
235 case GL_UNSIGNED_INT
:
236 default: /* Catch all the various sampler types. */
242 case GL_UNSIGNED_INT_VEC2
:
248 case GL_UNSIGNED_INT_VEC3
:
254 case GL_UNSIGNED_INT_VEC4
:
260 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
266 case GL_FLOAT_MAT2x3
:
270 case GL_FLOAT_MAT2x4
:
278 case GL_FLOAT_MAT3x2
:
282 case GL_FLOAT_MAT3x4
:
290 case GL_FLOAT_MAT4x2
:
294 case GL_FLOAT_MAT4x3
:
305 * Determine the number of rows and columns occupied by a uniform
306 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
307 * the number of rows = 1 and cols = number of elements in the vector.
310 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
311 GLint
*rows
, GLint
*cols
)
313 get_matrix_dims(p
->DataType
, rows
, cols
);
314 if (*rows
== 0 && *cols
== 0) {
315 /* not a matrix type, probably a float or vector */
317 *cols
= get_vector_elements(p
->DataType
);
323 * GLSL uniform arrays and structs require special handling.
325 * The GL_ARB_shader_objects spec says that if you use
326 * glGetUniformLocation to get the location of an array, you CANNOT
327 * access other elements of the array by adding an offset to the
328 * returned location. For example, you must call
329 * glGetUniformLocation("foo[16]") if you want to set the 16th element
330 * of the array with glUniform().
332 * HOWEVER, some other OpenGL drivers allow accessing array elements
333 * by adding an offset to the returned array location. And some apps
334 * seem to depend on that behaviour.
336 * Mesa's gl_uniform_list doesn't directly support this since each
337 * entry in the list describes one uniform variable, not one uniform
338 * element. We could insert dummy entries in the list for each array
339 * element after [0] but that causes complications elsewhere.
341 * We solve this problem by encoding two values in the location that's
342 * returned by glGetUniformLocation():
343 * a) index into gl_uniform_list::Uniforms[] for the uniform
344 * b) an array/field offset (0 for simple types)
346 * These two values are encoded in the high and low halves of a GLint.
347 * By putting the uniform number in the high part and the offset in the
348 * low part, we can support the unofficial ability to index into arrays
349 * by adding offsets to the location value.
352 merge_location_offset(GLint
*location
, GLint offset
)
354 *location
= (*location
<< 16) | offset
;
359 * Separate the uniform location and parameter offset. See above.
362 split_location_offset(GLint
*location
, GLint
*offset
)
364 *offset
= *location
& 0xffff;
365 *location
= *location
>> 16;
371 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
374 get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
375 GLsizei bufSize
, GLenum returnType
, GLvoid
*paramsOut
)
377 struct gl_shader_program
*shProg
=
378 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
379 struct gl_program
*prog
;
380 GLint paramPos
, offset
;
385 split_location_offset(&location
, &offset
);
387 if (!find_uniform_parameter_pos(shProg
, location
, &prog
, ¶mPos
)) {
388 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
391 const struct gl_program_parameter
*p
=
392 &prog
->Parameters
->Parameters
[paramPos
];
393 gl_constant_value (*values
)[4];
394 GLint rows
, cols
, i
, j
, k
;
398 values
= prog
->Parameters
->ParameterValues
+ paramPos
+ offset
;
400 get_uniform_rows_cols(p
, &rows
, &cols
);
402 numBytes
= rows
* cols
* _mesa_sizeof_type(returnType
);
403 if (bufSize
< numBytes
) {
404 _mesa_error( ctx
, GL_INVALID_OPERATION
,
405 "glGetnUniformfvARB(out of bounds: bufSize is %d,"
406 " but %d bytes are required)", bufSize
, numBytes
);
410 if (ctx
->Const
.NativeIntegers
) {
411 storage_type
= base_uniform_type(p
->DataType
);
413 storage_type
= GL_FLOAT
;
417 for (i
= 0; i
< rows
; i
++) {
418 for (j
= 0; j
< cols
; j
++ ) {
419 void *out
= (char *)paramsOut
+ 4 * k
;
421 switch (returnType
) {
423 switch (storage_type
) {
425 *(float *)out
= values
[i
][j
].f
;
428 case GL_BOOL
: /* boolean is just an integer 1 or 0. */
429 *(float *)out
= values
[i
][j
].i
;
431 case GL_UNSIGNED_INT
:
432 *(float *)out
= values
[i
][j
].u
;
438 case GL_UNSIGNED_INT
:
439 switch (storage_type
) {
441 /* While the GL 3.2 core spec doesn't explicitly
442 * state how conversion of float uniforms to integer
443 * values works, in section 6.2 "State Tables" on
446 * "Unless otherwise specified, when floating
447 * point state is returned as integer values or
448 * integer state is returned as floating-point
449 * values it is converted in the fashion
450 * described in section 6.1.2"
452 * That section, on page 248, says:
454 * "If GetIntegerv or GetInteger64v are called,
455 * a floating-point value is rounded to the
456 * nearest integer..."
458 *(int *)out
= IROUND(values
[i
][j
].f
);
462 case GL_UNSIGNED_INT
:
464 /* type conversions for these to int/uint are just
467 *(int *)out
= values
[i
][j
].i
;
482 * Called via glGetUniformLocation().
484 * The return value will encode two values, the uniform location and an
485 * offset (used for arrays, structs).
488 _mesa_get_uniform_location(struct gl_context
*ctx
,
489 struct gl_shader_program
*shProg
,
492 GLint offset
= 0, location
= -1;
494 if (shProg
->LinkStatus
== GL_FALSE
) {
495 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
499 /* XXX we should return -1 if the uniform was declared, but not
503 /* XXX we need to be able to parse uniform names for structs and arrays
510 /* handle 1-dimension arrays here... */
511 char *c
= strchr(name
, '[');
513 /* truncate name at [ */
514 const GLint len
= c
- name
;
515 GLchar
*newName
= malloc(len
+ 1);
517 return -1; /* out of mem */
518 memcpy(newName
, name
, len
);
521 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
523 const GLint element
= atoi(c
+ 1);
525 /* get type of the uniform array element */
526 const struct gl_program_parameter
*p
=
527 get_uniform_parameter(shProg
, location
);
530 get_matrix_dims(p
->DataType
, &rows
, &cols
);
533 offset
= element
* rows
;
543 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
547 merge_location_offset(&location
, offset
);
556 * Update the vertex/fragment program's TexturesUsed array.
558 * This needs to be called after glUniform(set sampler var) is called.
559 * A call to glUniform(samplerVar, value) causes a sampler to point to a
560 * particular texture unit. We know the sampler's texture target
561 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
562 * set by glUniform() calls.
564 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
565 * information to update the prog->TexturesUsed[] values.
566 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
567 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
568 * We'll use that info for state validation before rendering.
571 _mesa_update_shader_textures_used(struct gl_program
*prog
)
575 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
577 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
578 if (prog
->SamplersUsed
& (1 << s
)) {
579 GLuint unit
= prog
->SamplerUnits
[s
];
580 GLuint tgt
= prog
->SamplerTargets
[s
];
581 assert(unit
< Elements(prog
->TexturesUsed
));
582 assert(tgt
< NUM_TEXTURE_TARGETS
);
583 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
590 * Check if the type given by userType is allowed to set a uniform of the
591 * target type. Generally, equivalence is required, but setting Boolean
592 * uniforms can be done with glUniformiv or glUniformfv.
595 compatible_types(GLenum userType
, GLenum targetType
)
597 if (userType
== targetType
)
600 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
601 userType
== GL_UNSIGNED_INT
||
605 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
606 userType
== GL_UNSIGNED_INT_VEC2
||
607 userType
== GL_INT_VEC2
))
610 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
611 userType
== GL_UNSIGNED_INT_VEC3
||
612 userType
== GL_INT_VEC3
))
615 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
616 userType
== GL_UNSIGNED_INT_VEC4
||
617 userType
== GL_INT_VEC4
))
620 if (is_sampler_type(targetType
) && userType
== GL_INT
)
628 * Set the value of a program's uniform variable.
629 * \param program the program whose uniform to update
630 * \param index the index of the program parameter for the uniform
631 * \param offset additional parameter slot offset (for arrays)
632 * \param type the incoming datatype of 'values'
633 * \param count the number of uniforms to set
634 * \param elems number of elements per uniform (1, 2, 3 or 4)
635 * \param values the new values, of datatype 'type'
638 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
639 GLint index
, GLint offset
,
640 GLenum type
, GLsizei count
, GLint elems
,
643 const struct gl_program_parameter
*param
=
644 &program
->Parameters
->Parameters
[index
];
650 if (!compatible_types(type
, param
->DataType
)) {
651 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
655 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
660 if (param
->Type
== PROGRAM_SAMPLER
) {
661 /* This controls which texture unit which is used by a sampler */
662 GLboolean changed
= GL_FALSE
;
665 /* this should have been caught by the compatible_types() check */
666 ASSERT(type
== GL_INT
);
668 /* loop over number of samplers to change */
669 for (i
= 0; i
< count
; i
++) {
670 GLuint sampler
= (GLuint
)
671 program
->Parameters
->ParameterValues
[index
+offset
+ i
][0].f
;
672 GLuint texUnit
= ((GLuint
*) values
)[i
];
674 /* check that the sampler (tex unit index) is legal */
675 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
676 _mesa_error(ctx
, GL_INVALID_VALUE
,
677 "glUniform1(invalid sampler/tex unit index for '%s')",
682 /* This maps a sampler to a texture unit: */
683 if (sampler
< MAX_SAMPLERS
) {
685 printf("Set program %p sampler %d '%s' to unit %u\n",
686 program
, sampler
, param
->Name
, texUnit
);
688 if (program
->SamplerUnits
[sampler
] != texUnit
) {
689 program
->SamplerUnits
[sampler
] = texUnit
;
696 /* When a sampler's value changes it usually requires rewriting
697 * a GPU program's TEX instructions since there may not be a
698 * sampler->texture lookup table. We signal this with the
699 * ProgramStringNotify() callback.
701 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
702 _mesa_update_shader_textures_used(program
);
703 /* Do we need to care about the return value here?
704 * This should not be the first time the driver was notified of
707 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
711 /* ordinary uniform variable */
712 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
713 const GLenum basicType
= base_uniform_type(type
);
714 const GLint slots
= (param
->Size
+ 3) / 4;
715 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
718 if ((GLint
) param
->Size
> typeSize
) {
720 /* we'll ignore extra data below */
723 /* non-array: count must be at most one; count == 0 is handled
727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
728 "glUniform(uniform '%s' is not an array)",
734 /* loop over number of array elements */
735 for (k
= 0; k
< count
; k
++) {
736 gl_constant_value
*uniformVal
;
738 if (offset
+ k
>= slots
) {
739 /* Extra array data is ignored */
743 /* uniformVal (the destination) is always gl_constant_value[4] */
744 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
746 if (basicType
== GL_INT
) {
747 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
748 for (i
= 0; i
< elems
; i
++) {
749 if (!ctx
->Const
.NativeIntegers
)
750 uniformVal
[i
].f
= (GLfloat
) iValues
[i
];
752 uniformVal
[i
].i
= iValues
[i
];
755 else if (basicType
== GL_UNSIGNED_INT
) {
756 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
757 for (i
= 0; i
< elems
; i
++) {
758 if (!ctx
->Const
.NativeIntegers
)
759 uniformVal
[i
].f
= (GLfloat
)(GLuint
) iValues
[i
];
761 uniformVal
[i
].u
= iValues
[i
];
765 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
766 assert(basicType
== GL_FLOAT
);
767 for (i
= 0; i
< elems
; i
++) {
768 uniformVal
[i
].f
= fValues
[i
];
772 /* if the uniform is bool-valued, convert to 1 or 0 */
774 for (i
= 0; i
< elems
; i
++) {
775 if (basicType
== GL_FLOAT
)
776 uniformVal
[i
].b
= uniformVal
[i
].f
!= 0.0f
? 1 : 0;
778 uniformVal
[i
].b
= uniformVal
[i
].u
? 1 : 0;
780 if (ctx
->Const
.NativeIntegers
)
782 uniformVal
[i
].b
? ctx
->Const
.UniformBooleanTrue
: 0;
784 uniformVal
[i
].f
= uniformVal
[i
].b
? 1.0f
: 0.0f
;
793 * Called via glUniform*() functions.
796 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
797 GLint location
, GLsizei count
,
798 const GLvoid
*values
, GLenum type
)
800 struct gl_uniform
*uniform
;
803 ASSERT_OUTSIDE_BEGIN_END(ctx
);
805 if (!shProg
|| !shProg
->LinkStatus
) {
806 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
811 return; /* The standard specifies this as a no-op */
814 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
819 split_location_offset(&location
, &offset
);
821 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
822 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
827 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
831 elems
= _mesa_sizeof_glsl_type(type
);
833 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
835 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
837 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
838 const GLenum basicType
= base_uniform_type(type
);
840 printf("Mesa: set program %u uniform %s (loc %d) to: ",
841 shProg
->Name
, uniform
->Name
, location
);
842 if (basicType
== GL_INT
) {
843 const GLint
*v
= (const GLint
*) values
;
844 for (i
= 0; i
< count
* elems
; i
++) {
848 else if (basicType
== GL_UNSIGNED_INT
) {
849 const GLuint
*v
= (const GLuint
*) values
;
850 for (i
= 0; i
< count
* elems
; i
++) {
855 const GLfloat
*v
= (const GLfloat
*) values
;
856 assert(basicType
== GL_FLOAT
);
857 for (i
= 0; i
< count
* elems
; i
++) {
864 /* A uniform var may be used by both a vertex shader and a fragment
865 * shader. We may need to update one or both shader's uniform here:
867 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
868 /* convert uniform location to program parameter index */
869 GLint index
= uniform
->VertPos
;
871 set_program_uniform(ctx
,
872 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
,
873 index
, offset
, type
, count
, elems
, values
);
877 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
878 /* convert uniform location to program parameter index */
879 GLint index
= uniform
->FragPos
;
881 set_program_uniform(ctx
,
882 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
,
883 index
, offset
, type
, count
, elems
, values
);
887 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
888 /* convert uniform location to program parameter index */
889 GLint index
= uniform
->GeomPos
;
891 set_program_uniform(ctx
,
892 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
,
893 index
, offset
, type
, count
, elems
, values
);
897 uniform
->Initialized
= GL_TRUE
;
902 * Set a matrix-valued program parameter.
905 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
906 GLuint index
, GLuint offset
,
907 GLuint count
, GLuint rows
, GLuint cols
,
908 GLboolean transpose
, const GLfloat
*values
)
910 GLuint mat
, row
, col
;
912 const struct gl_program_parameter
*param
=
913 &program
->Parameters
->Parameters
[index
];
914 const GLuint slots
= (param
->Size
+ 3) / 4;
915 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
918 /* check that the number of rows, columns is correct */
919 get_matrix_dims(param
->DataType
, &nr
, &nc
);
920 if (rows
!= nr
|| cols
!= nc
) {
921 _mesa_error(ctx
, GL_INVALID_OPERATION
,
922 "glUniformMatrix(matrix size mismatch)");
926 if ((GLint
) param
->Size
<= typeSize
) {
927 /* non-array: count must be at most one; count == 0 is handled
931 _mesa_error(ctx
, GL_INVALID_OPERATION
,
932 "glUniformMatrix(uniform is not an array)");
938 * Note: the _columns_ of a matrix are stored in program registers, not
939 * the rows. So, the loops below look a little funny.
940 * XXX could optimize this a bit...
943 /* loop over matrices */
944 for (mat
= 0; mat
< count
; mat
++) {
947 for (col
= 0; col
< cols
; col
++) {
949 if (offset
>= slots
) {
950 /* Ignore writes beyond the end of (the used part of) an array */
953 v
= (GLfloat
*) program
->Parameters
->ParameterValues
[index
+ offset
];
954 for (row
= 0; row
< rows
; row
++) {
956 v
[row
] = values
[src
+ row
* cols
+ col
];
959 v
[row
] = values
[src
+ col
* rows
+ row
];
966 src
+= rows
* cols
; /* next matrix */
972 * Called by glUniformMatrix*() functions.
973 * Note: cols=2, rows=4 ==> array[2] of vec4
976 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
977 GLint cols
, GLint rows
,
978 GLint location
, GLsizei count
,
979 GLboolean transpose
, const GLfloat
*values
)
981 struct gl_uniform
*uniform
;
984 ASSERT_OUTSIDE_BEGIN_END(ctx
);
986 if (!shProg
|| !shProg
->LinkStatus
) {
987 _mesa_error(ctx
, GL_INVALID_OPERATION
,
988 "glUniformMatrix(program not linked)");
993 return; /* The standard specifies this as a no-op */
996 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
1000 split_location_offset(&location
, &offset
);
1002 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1003 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1006 if (values
== NULL
) {
1007 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1011 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1013 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1015 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
1016 /* convert uniform location to program parameter index */
1017 GLint index
= uniform
->VertPos
;
1019 set_program_uniform_matrix(ctx
,
1020 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
,
1022 count
, rows
, cols
, transpose
, values
);
1026 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
1027 /* convert uniform location to program parameter index */
1028 GLint index
= uniform
->FragPos
;
1030 set_program_uniform_matrix(ctx
,
1031 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
,
1033 count
, rows
, cols
, transpose
, values
);
1037 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
1038 /* convert uniform location to program parameter index */
1039 GLint index
= uniform
->GeomPos
;
1041 set_program_uniform_matrix(ctx
,
1042 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
,
1044 count
, rows
, cols
, transpose
, values
);
1048 uniform
->Initialized
= GL_TRUE
;
1053 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1055 GET_CURRENT_CONTEXT(ctx
);
1056 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1060 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1062 GET_CURRENT_CONTEXT(ctx
);
1066 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1070 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1072 GET_CURRENT_CONTEXT(ctx
);
1077 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1081 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1084 GET_CURRENT_CONTEXT(ctx
);
1090 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1094 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1096 GET_CURRENT_CONTEXT(ctx
);
1097 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1101 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1103 GET_CURRENT_CONTEXT(ctx
);
1107 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1111 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1113 GET_CURRENT_CONTEXT(ctx
);
1118 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1122 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1124 GET_CURRENT_CONTEXT(ctx
);
1130 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1134 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1136 GET_CURRENT_CONTEXT(ctx
);
1137 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1141 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1143 GET_CURRENT_CONTEXT(ctx
);
1144 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1148 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1150 GET_CURRENT_CONTEXT(ctx
);
1151 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1155 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1157 GET_CURRENT_CONTEXT(ctx
);
1158 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1162 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1164 GET_CURRENT_CONTEXT(ctx
);
1165 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1169 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1171 GET_CURRENT_CONTEXT(ctx
);
1172 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1176 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1178 GET_CURRENT_CONTEXT(ctx
);
1179 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1183 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1185 GET_CURRENT_CONTEXT(ctx
);
1186 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1190 /** OpenGL 3.0 GLuint-valued functions **/
1192 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1194 GET_CURRENT_CONTEXT(ctx
);
1195 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1199 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1201 GET_CURRENT_CONTEXT(ctx
);
1205 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1209 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1211 GET_CURRENT_CONTEXT(ctx
);
1216 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1220 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1222 GET_CURRENT_CONTEXT(ctx
);
1228 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1232 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1234 GET_CURRENT_CONTEXT(ctx
);
1235 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1239 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1241 GET_CURRENT_CONTEXT(ctx
);
1242 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1246 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1248 GET_CURRENT_CONTEXT(ctx
);
1249 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1253 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1255 GET_CURRENT_CONTEXT(ctx
);
1256 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1262 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1263 const GLfloat
* value
)
1265 GET_CURRENT_CONTEXT(ctx
);
1266 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1267 2, 2, location
, count
, transpose
, value
);
1271 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1272 const GLfloat
* value
)
1274 GET_CURRENT_CONTEXT(ctx
);
1275 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1276 3, 3, location
, count
, transpose
, value
);
1280 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1281 const GLfloat
* value
)
1283 GET_CURRENT_CONTEXT(ctx
);
1284 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1285 4, 4, location
, count
, transpose
, value
);
1290 * Non-square UniformMatrix are OpenGL 2.1
1293 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1294 const GLfloat
*value
)
1296 GET_CURRENT_CONTEXT(ctx
);
1297 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1298 2, 3, location
, count
, transpose
, value
);
1302 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1303 const GLfloat
*value
)
1305 GET_CURRENT_CONTEXT(ctx
);
1306 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1307 3, 2, location
, count
, transpose
, value
);
1311 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1312 const GLfloat
*value
)
1314 GET_CURRENT_CONTEXT(ctx
);
1315 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1316 2, 4, location
, count
, transpose
, value
);
1320 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1321 const GLfloat
*value
)
1323 GET_CURRENT_CONTEXT(ctx
);
1324 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1325 4, 2, location
, count
, transpose
, value
);
1329 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1330 const GLfloat
*value
)
1332 GET_CURRENT_CONTEXT(ctx
);
1333 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1334 3, 4, location
, count
, transpose
, value
);
1338 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1339 const GLfloat
*value
)
1341 GET_CURRENT_CONTEXT(ctx
);
1342 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1343 4, 3, location
, count
, transpose
, value
);
1348 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
1349 GLsizei bufSize
, GLfloat
*params
)
1351 GET_CURRENT_CONTEXT(ctx
);
1352 get_uniform(ctx
, program
, location
, bufSize
, GL_FLOAT
, params
);
1356 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1358 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
1363 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
1364 GLsizei bufSize
, GLint
*params
)
1366 GET_CURRENT_CONTEXT(ctx
);
1367 get_uniform(ctx
, program
, location
, bufSize
, GL_INT
, params
);
1371 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1373 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
1379 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
1380 GLsizei bufSize
, GLuint
*params
)
1382 GET_CURRENT_CONTEXT(ctx
);
1383 get_uniform(ctx
, program
, location
, bufSize
, GL_UNSIGNED_INT
, params
);
1387 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1389 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
1395 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
1396 GLsizei bufSize
, GLdouble
*params
)
1398 GET_CURRENT_CONTEXT(ctx
);
1406 get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
1408 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
1409 "(GL_ARB_gpu_shader_fp64 not implemented)");
1413 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
1415 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
1420 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1422 struct gl_shader_program
*shProg
;
1424 GET_CURRENT_CONTEXT(ctx
);
1426 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1427 "glGetUniformLocation");
1431 return _mesa_get_uniform_location(ctx
, shProg
, name
);
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 */