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
)
116 case GL_SAMPLER_CUBE
:
117 case GL_SAMPLER_1D_SHADOW
:
118 case GL_SAMPLER_2D_SHADOW
:
119 case GL_SAMPLER_CUBE_SHADOW
:
120 case GL_SAMPLER_2D_RECT_ARB
:
121 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
122 case GL_SAMPLER_1D_ARRAY_EXT
:
123 case GL_SAMPLER_2D_ARRAY_EXT
:
124 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
125 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
126 case GL_SAMPLER_CUBE_MAP_ARRAY
:
127 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW
:
128 case GL_SAMPLER_BUFFER
:
137 * Given a uniform index, return the vertex/geometry/fragment program
138 * that has that parameter, plus the position of the parameter in the
139 * parameter/constant buffer.
140 * \param shProg the shader program
141 * \param index the uniform index in [0, NumUniforms-1]
142 * \param progOut returns containing program
143 * \param posOut returns position of the uniform in the param/const buffer
144 * \return GL_TRUE for success, GL_FALSE for invalid index
147 find_uniform_parameter_pos(struct gl_shader_program
*shProg
, GLint index
,
148 struct gl_program
**progOut
, GLint
*posOut
)
150 struct gl_program
*prog
= NULL
;
153 if (!shProg
->Uniforms
||
155 index
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
159 pos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
161 prog
= &shProg
->VertexProgram
->Base
;
164 pos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
166 prog
= &shProg
->FragmentProgram
->Base
;
169 pos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
171 prog
= &shProg
->GeometryProgram
->Base
;
176 if (!prog
|| pos
< 0)
177 return GL_FALSE
; /* should really never happen */
187 * Return pointer to a gl_program_parameter which corresponds to a uniform.
188 * \param shProg the shader program
189 * \param index the uniform index in [0, NumUniforms-1]
190 * \return gl_program_parameter point or NULL if index is invalid
192 static const struct gl_program_parameter
*
193 get_uniform_parameter(struct gl_shader_program
*shProg
, GLint index
)
195 struct gl_program
*prog
;
198 if (find_uniform_parameter_pos(shProg
, index
, &prog
, &progPos
))
199 return &prog
->Parameters
->Parameters
[progPos
];
206 * Called by glGetActiveUniform().
209 _mesa_get_active_uniform(struct gl_context
*ctx
, GLuint program
, GLuint index
,
210 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
211 GLenum
*type
, GLchar
*nameOut
)
213 struct gl_shader_program
*shProg
=
214 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
215 const struct gl_program_parameter
*param
;
220 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
221 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
225 param
= get_uniform_parameter(shProg
, index
);
230 _mesa_copy_string(nameOut
, maxLength
, length
, param
->Name
);
234 GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
235 if ((GLint
) param
->Size
> typeSize
) {
237 * Array elements are placed on vector[4] boundaries so they're
238 * a multiple of four floats. We round typeSize up to next multiple
239 * of four to get the right size below.
241 typeSize
= (typeSize
+ 3) & ~3;
243 /* Note that the returned size is in units of the <type>, not bytes */
244 *size
= param
->Size
/ typeSize
;
248 *type
= param
->DataType
;
254 get_vector_elements(GLenum type
)
260 case GL_UNSIGNED_INT
:
261 default: /* Catch all the various sampler types. */
267 case GL_UNSIGNED_INT_VEC2
:
273 case GL_UNSIGNED_INT_VEC3
:
279 case GL_UNSIGNED_INT_VEC4
:
285 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
291 case GL_FLOAT_MAT2x3
:
295 case GL_FLOAT_MAT2x4
:
303 case GL_FLOAT_MAT3x2
:
307 case GL_FLOAT_MAT3x4
:
315 case GL_FLOAT_MAT4x2
:
319 case GL_FLOAT_MAT4x3
:
330 * Determine the number of rows and columns occupied by a uniform
331 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
332 * the number of rows = 1 and cols = number of elements in the vector.
335 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
336 GLint
*rows
, GLint
*cols
)
338 get_matrix_dims(p
->DataType
, rows
, cols
);
339 if (*rows
== 0 && *cols
== 0) {
340 /* not a matrix type, probably a float or vector */
342 *cols
= get_vector_elements(p
->DataType
);
348 * GLSL uniform arrays and structs require special handling.
350 * The GL_ARB_shader_objects spec says that if you use
351 * glGetUniformLocation to get the location of an array, you CANNOT
352 * access other elements of the array by adding an offset to the
353 * returned location. For example, you must call
354 * glGetUniformLocation("foo[16]") if you want to set the 16th element
355 * of the array with glUniform().
357 * HOWEVER, some other OpenGL drivers allow accessing array elements
358 * by adding an offset to the returned array location. And some apps
359 * seem to depend on that behaviour.
361 * Mesa's gl_uniform_list doesn't directly support this since each
362 * entry in the list describes one uniform variable, not one uniform
363 * element. We could insert dummy entries in the list for each array
364 * element after [0] but that causes complications elsewhere.
366 * We solve this problem by encoding two values in the location that's
367 * returned by glGetUniformLocation():
368 * a) index into gl_uniform_list::Uniforms[] for the uniform
369 * b) an array/field offset (0 for simple types)
371 * These two values are encoded in the high and low halves of a GLint.
372 * By putting the uniform number in the high part and the offset in the
373 * low part, we can support the unofficial ability to index into arrays
374 * by adding offsets to the location value.
377 merge_location_offset(GLint
*location
, GLint offset
)
379 *location
= (*location
<< 16) | offset
;
384 * Separate the uniform location and parameter offset. See above.
387 split_location_offset(GLint
*location
, GLint
*offset
)
389 *offset
= *location
& 0xffff;
390 *location
= *location
>> 16;
396 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
399 get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
400 GLsizei bufSize
, GLenum returnType
, GLvoid
*paramsOut
)
402 struct gl_shader_program
*shProg
=
403 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
404 struct gl_program
*prog
;
405 GLint paramPos
, offset
;
410 split_location_offset(&location
, &offset
);
412 if (!find_uniform_parameter_pos(shProg
, location
, &prog
, ¶mPos
)) {
413 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
416 const struct gl_program_parameter
*p
=
417 &prog
->Parameters
->Parameters
[paramPos
];
418 gl_constant_value (*values
)[4];
419 GLint rows
, cols
, i
, j
, k
;
423 values
= prog
->Parameters
->ParameterValues
+ paramPos
+ offset
;
425 get_uniform_rows_cols(p
, &rows
, &cols
);
427 numBytes
= rows
* cols
* _mesa_sizeof_type(returnType
);
428 if (bufSize
< numBytes
) {
429 _mesa_error( ctx
, GL_INVALID_OPERATION
,
430 "glGetnUniformfvARB(out of bounds: bufSize is %d,"
431 " but %d bytes are required)", bufSize
, numBytes
);
435 if (ctx
->Const
.NativeIntegers
) {
436 storage_type
= base_uniform_type(p
->DataType
);
438 storage_type
= GL_FLOAT
;
442 for (i
= 0; i
< rows
; i
++) {
443 for (j
= 0; j
< cols
; j
++ ) {
444 void *out
= (char *)paramsOut
+ 4 * k
;
446 switch (returnType
) {
448 switch (storage_type
) {
450 *(float *)out
= values
[i
][j
].f
;
453 case GL_BOOL
: /* boolean is just an integer 1 or 0. */
454 *(float *)out
= values
[i
][j
].i
;
456 case GL_UNSIGNED_INT
:
457 *(float *)out
= values
[i
][j
].u
;
463 case GL_UNSIGNED_INT
:
464 switch (storage_type
) {
466 /* While the GL 3.2 core spec doesn't explicitly
467 * state how conversion of float uniforms to integer
468 * values works, in section 6.2 "State Tables" on
471 * "Unless otherwise specified, when floating
472 * point state is returned as integer values or
473 * integer state is returned as floating-point
474 * values it is converted in the fashion
475 * described in section 6.1.2"
477 * That section, on page 248, says:
479 * "If GetIntegerv or GetInteger64v are called,
480 * a floating-point value is rounded to the
481 * nearest integer..."
483 *(int *)out
= IROUND(values
[i
][j
].f
);
487 case GL_UNSIGNED_INT
:
489 /* type conversions for these to int/uint are just
492 *(int *)out
= values
[i
][j
].i
;
507 * Called via glGetUniformLocation().
509 * The return value will encode two values, the uniform location and an
510 * offset (used for arrays, structs).
513 _mesa_get_uniform_location(struct gl_context
*ctx
,
514 struct gl_shader_program
*shProg
,
517 GLint offset
= 0, location
= -1;
519 if (shProg
->LinkStatus
== GL_FALSE
) {
520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
524 /* XXX we should return -1 if the uniform was declared, but not
528 /* XXX we need to be able to parse uniform names for structs and arrays
535 /* handle 1-dimension arrays here... */
536 char *c
= strchr(name
, '[');
538 /* truncate name at [ */
539 const GLint len
= c
- name
;
540 GLchar
*newName
= malloc(len
+ 1);
542 return -1; /* out of mem */
543 memcpy(newName
, name
, len
);
546 location
= _mesa_lookup_uniform(shProg
->Uniforms
, newName
);
548 const GLint element
= atoi(c
+ 1);
550 /* get type of the uniform array element */
551 const struct gl_program_parameter
*p
=
552 get_uniform_parameter(shProg
, location
);
555 get_matrix_dims(p
->DataType
, &rows
, &cols
);
558 offset
= element
* rows
;
568 location
= _mesa_lookup_uniform(shProg
->Uniforms
, name
);
572 merge_location_offset(&location
, offset
);
581 * Update the vertex/fragment program's TexturesUsed array.
583 * This needs to be called after glUniform(set sampler var) is called.
584 * A call to glUniform(samplerVar, value) causes a sampler to point to a
585 * particular texture unit. We know the sampler's texture target
586 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
587 * set by glUniform() calls.
589 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
590 * information to update the prog->TexturesUsed[] values.
591 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
592 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
593 * We'll use that info for state validation before rendering.
596 _mesa_update_shader_textures_used(struct gl_program
*prog
)
600 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
602 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
603 if (prog
->SamplersUsed
& (1 << s
)) {
604 GLuint unit
= prog
->SamplerUnits
[s
];
605 GLuint tgt
= prog
->SamplerTargets
[s
];
606 assert(unit
< Elements(prog
->TexturesUsed
));
607 assert(tgt
< NUM_TEXTURE_TARGETS
);
608 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
615 * Check if the type given by userType is allowed to set a uniform of the
616 * target type. Generally, equivalence is required, but setting Boolean
617 * uniforms can be done with glUniformiv or glUniformfv.
620 compatible_types(GLenum userType
, GLenum targetType
)
622 if (userType
== targetType
)
625 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
626 userType
== GL_UNSIGNED_INT
||
630 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
631 userType
== GL_UNSIGNED_INT_VEC2
||
632 userType
== GL_INT_VEC2
))
635 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
636 userType
== GL_UNSIGNED_INT_VEC3
||
637 userType
== GL_INT_VEC3
))
640 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
641 userType
== GL_UNSIGNED_INT_VEC4
||
642 userType
== GL_INT_VEC4
))
645 if (is_sampler_type(targetType
) && userType
== GL_INT
)
653 * Set the value of a program's uniform variable.
654 * \param program the program whose uniform to update
655 * \param index the index of the program parameter for the uniform
656 * \param offset additional parameter slot offset (for arrays)
657 * \param type the incoming datatype of 'values'
658 * \param count the number of uniforms to set
659 * \param elems number of elements per uniform (1, 2, 3 or 4)
660 * \param values the new values, of datatype 'type'
663 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
664 GLint index
, GLint offset
,
665 GLenum type
, GLsizei count
, GLint elems
,
668 const struct gl_program_parameter
*param
=
669 &program
->Parameters
->Parameters
[index
];
675 if (!compatible_types(type
, param
->DataType
)) {
676 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
680 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
685 if (param
->Type
== PROGRAM_SAMPLER
) {
686 /* This controls which texture unit which is used by a sampler */
687 GLboolean changed
= GL_FALSE
;
690 /* this should have been caught by the compatible_types() check */
691 ASSERT(type
== GL_INT
);
693 /* loop over number of samplers to change */
694 for (i
= 0; i
< count
; i
++) {
695 GLuint sampler
= (GLuint
)
696 program
->Parameters
->ParameterValues
[index
+offset
+ i
][0].f
;
697 GLuint texUnit
= ((GLuint
*) values
)[i
];
699 /* check that the sampler (tex unit index) is legal */
700 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
701 _mesa_error(ctx
, GL_INVALID_VALUE
,
702 "glUniform1(invalid sampler/tex unit index for '%s')",
707 /* This maps a sampler to a texture unit: */
708 if (sampler
< MAX_SAMPLERS
) {
710 printf("Set program %p sampler %d '%s' to unit %u\n",
711 program
, sampler
, param
->Name
, texUnit
);
713 if (program
->SamplerUnits
[sampler
] != texUnit
) {
714 program
->SamplerUnits
[sampler
] = texUnit
;
721 /* When a sampler's value changes it usually requires rewriting
722 * a GPU program's TEX instructions since there may not be a
723 * sampler->texture lookup table. We signal this with the
724 * ProgramStringNotify() callback.
726 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
727 _mesa_update_shader_textures_used(program
);
728 /* Do we need to care about the return value here?
729 * This should not be the first time the driver was notified of
732 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
736 /* ordinary uniform variable */
737 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
738 const GLenum basicType
= base_uniform_type(type
);
739 const GLint slots
= (param
->Size
+ 3) / 4;
740 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
743 if ((GLint
) param
->Size
> typeSize
) {
745 /* we'll ignore extra data below */
748 /* non-array: count must be at most one; count == 0 is handled
752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
753 "glUniform(uniform '%s' is not an array)",
759 /* loop over number of array elements */
760 for (k
= 0; k
< count
; k
++) {
761 gl_constant_value
*uniformVal
;
763 if (offset
+ k
>= slots
) {
764 /* Extra array data is ignored */
768 /* uniformVal (the destination) is always gl_constant_value[4] */
769 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
771 if (basicType
== GL_INT
) {
772 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
773 for (i
= 0; i
< elems
; i
++) {
774 if (!ctx
->Const
.NativeIntegers
)
775 uniformVal
[i
].f
= (GLfloat
) iValues
[i
];
777 uniformVal
[i
].i
= iValues
[i
];
780 else if (basicType
== GL_UNSIGNED_INT
) {
781 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
782 for (i
= 0; i
< elems
; i
++) {
783 if (!ctx
->Const
.NativeIntegers
)
784 uniformVal
[i
].f
= (GLfloat
)(GLuint
) iValues
[i
];
786 uniformVal
[i
].u
= iValues
[i
];
790 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
791 assert(basicType
== GL_FLOAT
);
792 for (i
= 0; i
< elems
; i
++) {
793 uniformVal
[i
].f
= fValues
[i
];
797 /* if the uniform is bool-valued, convert to 1 or 0 */
799 for (i
= 0; i
< elems
; i
++) {
800 if (basicType
== GL_FLOAT
)
801 uniformVal
[i
].b
= uniformVal
[i
].f
!= 0.0f
? 1 : 0;
803 uniformVal
[i
].b
= uniformVal
[i
].u
? 1 : 0;
805 if (!ctx
->Const
.NativeIntegers
)
806 uniformVal
[i
].f
= uniformVal
[i
].b
? 1.0f
: 0.0f
;
815 * Called via glUniform*() functions.
818 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
819 GLint location
, GLsizei count
,
820 const GLvoid
*values
, GLenum type
)
822 struct gl_uniform
*uniform
;
825 ASSERT_OUTSIDE_BEGIN_END(ctx
);
827 if (!shProg
|| !shProg
->LinkStatus
) {
828 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
833 return; /* The standard specifies this as a no-op */
836 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(location=%d)",
841 split_location_offset(&location
, &offset
);
843 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
844 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location=%d)", location
);
849 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
853 elems
= _mesa_sizeof_glsl_type(type
);
855 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
857 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
859 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
860 const GLenum basicType
= base_uniform_type(type
);
862 printf("Mesa: set program %u uniform %s (loc %d) to: ",
863 shProg
->Name
, uniform
->Name
, location
);
864 if (basicType
== GL_INT
) {
865 const GLint
*v
= (const GLint
*) values
;
866 for (i
= 0; i
< count
* elems
; i
++) {
870 else if (basicType
== GL_UNSIGNED_INT
) {
871 const GLuint
*v
= (const GLuint
*) values
;
872 for (i
= 0; i
< count
* elems
; i
++) {
877 const GLfloat
*v
= (const GLfloat
*) values
;
878 assert(basicType
== GL_FLOAT
);
879 for (i
= 0; i
< count
* elems
; i
++) {
886 /* A uniform var may be used by both a vertex shader and a fragment
887 * shader. We may need to update one or both shader's uniform here:
889 if (shProg
->VertexProgram
) {
890 /* convert uniform location to program parameter index */
891 GLint index
= uniform
->VertPos
;
893 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
894 index
, offset
, type
, count
, elems
, values
);
898 if (shProg
->FragmentProgram
) {
899 /* convert uniform location to program parameter index */
900 GLint index
= uniform
->FragPos
;
902 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
903 index
, offset
, type
, count
, elems
, values
);
907 if (shProg
->GeometryProgram
) {
908 /* convert uniform location to program parameter index */
909 GLint index
= uniform
->GeomPos
;
911 set_program_uniform(ctx
, &shProg
->GeometryProgram
->Base
,
912 index
, offset
, type
, count
, elems
, values
);
916 uniform
->Initialized
= GL_TRUE
;
921 * Set a matrix-valued program parameter.
924 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
925 GLuint index
, GLuint offset
,
926 GLuint count
, GLuint rows
, GLuint cols
,
927 GLboolean transpose
, const GLfloat
*values
)
929 GLuint mat
, row
, col
;
931 const struct gl_program_parameter
*param
=
932 &program
->Parameters
->Parameters
[index
];
933 const GLuint slots
= (param
->Size
+ 3) / 4;
934 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
937 /* check that the number of rows, columns is correct */
938 get_matrix_dims(param
->DataType
, &nr
, &nc
);
939 if (rows
!= nr
|| cols
!= nc
) {
940 _mesa_error(ctx
, GL_INVALID_OPERATION
,
941 "glUniformMatrix(matrix size mismatch)");
945 if ((GLint
) param
->Size
<= typeSize
) {
946 /* non-array: count must be at most one; count == 0 is handled
950 _mesa_error(ctx
, GL_INVALID_OPERATION
,
951 "glUniformMatrix(uniform is not an array)");
957 * Note: the _columns_ of a matrix are stored in program registers, not
958 * the rows. So, the loops below look a little funny.
959 * XXX could optimize this a bit...
962 /* loop over matrices */
963 for (mat
= 0; mat
< count
; mat
++) {
966 for (col
= 0; col
< cols
; col
++) {
968 if (offset
>= slots
) {
969 /* Ignore writes beyond the end of (the used part of) an array */
972 v
= (GLfloat
*) program
->Parameters
->ParameterValues
[index
+ offset
];
973 for (row
= 0; row
< rows
; row
++) {
975 v
[row
] = values
[src
+ row
* cols
+ col
];
978 v
[row
] = values
[src
+ col
* rows
+ row
];
985 src
+= rows
* cols
; /* next matrix */
991 * Called by glUniformMatrix*() functions.
992 * Note: cols=2, rows=4 ==> array[2] of vec4
995 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
996 GLint cols
, GLint rows
,
997 GLint location
, GLsizei count
,
998 GLboolean transpose
, const GLfloat
*values
)
1000 struct gl_uniform
*uniform
;
1003 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1005 if (!shProg
|| !shProg
->LinkStatus
) {
1006 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1007 "glUniformMatrix(program not linked)");
1012 return; /* The standard specifies this as a no-op */
1014 if (location
< -1) {
1015 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniformMatrix(location)");
1019 split_location_offset(&location
, &offset
);
1021 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1022 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1025 if (values
== NULL
) {
1026 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1030 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1032 uniform
= &shProg
->Uniforms
->Uniforms
[location
];
1034 if (shProg
->VertexProgram
) {
1035 /* convert uniform location to program parameter index */
1036 GLint index
= uniform
->VertPos
;
1038 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
1040 count
, rows
, cols
, transpose
, values
);
1044 if (shProg
->FragmentProgram
) {
1045 /* convert uniform location to program parameter index */
1046 GLint index
= uniform
->FragPos
;
1048 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
1050 count
, rows
, cols
, transpose
, values
);
1054 if (shProg
->GeometryProgram
) {
1055 /* convert uniform location to program parameter index */
1056 GLint index
= uniform
->GeomPos
;
1058 set_program_uniform_matrix(ctx
, &shProg
->GeometryProgram
->Base
,
1060 count
, rows
, cols
, transpose
, values
);
1064 uniform
->Initialized
= GL_TRUE
;
1069 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
1071 GET_CURRENT_CONTEXT(ctx
);
1072 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
1076 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
1078 GET_CURRENT_CONTEXT(ctx
);
1082 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
1086 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
1088 GET_CURRENT_CONTEXT(ctx
);
1093 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
1097 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
1100 GET_CURRENT_CONTEXT(ctx
);
1106 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
1110 _mesa_Uniform1iARB(GLint location
, GLint v0
)
1112 GET_CURRENT_CONTEXT(ctx
);
1113 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
1117 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
1119 GET_CURRENT_CONTEXT(ctx
);
1123 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
1127 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
1129 GET_CURRENT_CONTEXT(ctx
);
1134 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
1138 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
1140 GET_CURRENT_CONTEXT(ctx
);
1146 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
1150 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1152 GET_CURRENT_CONTEXT(ctx
);
1153 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
1157 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1159 GET_CURRENT_CONTEXT(ctx
);
1160 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
1164 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1166 GET_CURRENT_CONTEXT(ctx
);
1167 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
1171 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
1173 GET_CURRENT_CONTEXT(ctx
);
1174 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
1178 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
1185 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1187 GET_CURRENT_CONTEXT(ctx
);
1188 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
1192 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1194 GET_CURRENT_CONTEXT(ctx
);
1195 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
1199 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
1201 GET_CURRENT_CONTEXT(ctx
);
1202 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
1206 /** OpenGL 3.0 GLuint-valued functions **/
1208 _mesa_Uniform1ui(GLint location
, GLuint v0
)
1210 GET_CURRENT_CONTEXT(ctx
);
1211 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
1215 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
1217 GET_CURRENT_CONTEXT(ctx
);
1221 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
1225 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
1227 GET_CURRENT_CONTEXT(ctx
);
1232 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
1236 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
1238 GET_CURRENT_CONTEXT(ctx
);
1244 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
1248 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1250 GET_CURRENT_CONTEXT(ctx
);
1251 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
1255 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1257 GET_CURRENT_CONTEXT(ctx
);
1258 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
1262 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
1269 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
1271 GET_CURRENT_CONTEXT(ctx
);
1272 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
1278 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1279 const GLfloat
* value
)
1281 GET_CURRENT_CONTEXT(ctx
);
1282 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1283 2, 2, location
, count
, transpose
, value
);
1287 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1288 const GLfloat
* value
)
1290 GET_CURRENT_CONTEXT(ctx
);
1291 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1292 3, 3, location
, count
, transpose
, value
);
1296 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
1297 const GLfloat
* value
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1301 4, 4, location
, count
, transpose
, value
);
1306 * Non-square UniformMatrix are OpenGL 2.1
1309 _mesa_UniformMatrix2x3fv(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, 3, location
, count
, transpose
, value
);
1318 _mesa_UniformMatrix3x2fv(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, 2, location
, count
, transpose
, value
);
1327 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1328 const GLfloat
*value
)
1330 GET_CURRENT_CONTEXT(ctx
);
1331 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1332 2, 4, location
, count
, transpose
, value
);
1336 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
1337 const GLfloat
*value
)
1339 GET_CURRENT_CONTEXT(ctx
);
1340 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1341 4, 2, location
, count
, transpose
, value
);
1345 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
1346 const GLfloat
*value
)
1348 GET_CURRENT_CONTEXT(ctx
);
1349 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1350 3, 4, location
, count
, transpose
, value
);
1354 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
1355 const GLfloat
*value
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
1359 4, 3, location
, count
, transpose
, value
);
1364 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
1365 GLsizei bufSize
, GLfloat
*params
)
1367 GET_CURRENT_CONTEXT(ctx
);
1368 get_uniform(ctx
, program
, location
, bufSize
, GL_FLOAT
, params
);
1372 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
1374 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
1379 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
1380 GLsizei bufSize
, GLint
*params
)
1382 GET_CURRENT_CONTEXT(ctx
);
1383 get_uniform(ctx
, program
, location
, bufSize
, GL_INT
, params
);
1387 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
1389 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
1395 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
1396 GLsizei bufSize
, GLuint
*params
)
1398 GET_CURRENT_CONTEXT(ctx
);
1399 get_uniform(ctx
, program
, location
, bufSize
, GL_UNSIGNED_INT
, params
);
1403 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
1405 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
1411 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
1412 GLsizei bufSize
, GLdouble
*params
)
1414 GET_CURRENT_CONTEXT(ctx
);
1416 get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
1418 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
1419 "(GL_ARB_gpu_shader_fp64 not implemented)");
1423 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
1425 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
1430 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
1432 struct gl_shader_program
*shProg
;
1434 GET_CURRENT_CONTEXT(ctx
);
1436 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
1437 "glGetUniformLocation");
1441 return _mesa_get_uniform_location(ctx
, shProg
, name
);
1446 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
1447 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1448 GLenum
* type
, GLcharARB
* name
)
1450 GET_CURRENT_CONTEXT(ctx
);
1451 _mesa_get_active_uniform(ctx
, program
, index
, maxLength
, length
, size
,
1457 * Plug in shader uniform-related functions into API dispatch table.
1460 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
1463 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
1464 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
1465 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
1466 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
1467 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
1468 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
1469 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
1470 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
1471 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
1472 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
1473 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
1474 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
1475 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
1476 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
1477 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
1478 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
1479 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
1480 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
1481 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
1483 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
1484 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
1485 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
1486 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
1489 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
1490 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
1491 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
1492 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
1493 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
1494 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
1497 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
1498 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
1499 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
1500 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
1501 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
1502 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
1503 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
1504 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
1505 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
1507 /* GL_ARB_robustness */
1508 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
1509 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
1510 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
1511 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
1513 #endif /* FEATURE_GL */