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, 2011 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
29 #include "main/core.h"
30 #include "main/context.h"
32 #include "ir_uniform.h"
33 #include "program/hash_table.h"
34 #include "../glsl/program.h"
35 #include "../glsl/ir_uniform.h"
36 #include "../glsl/glsl_parser_extras.h"
37 #include "main/shaderapi.h"
38 #include "main/shaderobj.h"
42 extern "C" void GLAPIENTRY
43 _mesa_GetActiveUniform(GLhandleARB program
, GLuint index
,
44 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
45 GLenum
*type
, GLcharARB
*nameOut
)
47 GET_CURRENT_CONTEXT(ctx
);
48 struct gl_shader_program
*shProg
=
49 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
54 if (index
>= shProg
->NumUserUniformStorage
) {
55 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
59 const struct gl_uniform_storage
*const uni
= &shProg
->UniformStorage
[index
];
62 _mesa_get_uniform_name(uni
, maxLength
, length
, nameOut
);
66 /* array_elements is zero for non-arrays, but the API requires that 1 be
69 *size
= MAX2(1, uni
->array_elements
);
73 *type
= uni
->type
->gl_type
;
77 extern "C" void GLAPIENTRY
78 _mesa_GetActiveUniformsiv(GLuint program
,
80 const GLuint
*uniformIndices
,
84 GET_CURRENT_CONTEXT(ctx
);
85 struct gl_shader_program
*shProg
;
88 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
92 if (uniformCount
< 0) {
93 _mesa_error(ctx
, GL_INVALID_VALUE
,
94 "glGetUniformIndices(uniformCount < 0)");
98 for (i
= 0; i
< uniformCount
; i
++) {
99 GLuint index
= uniformIndices
[i
];
101 if (index
>= shProg
->NumUserUniformStorage
) {
102 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
107 for (i
= 0; i
< uniformCount
; i
++) {
108 GLuint index
= uniformIndices
[i
];
109 const struct gl_uniform_storage
*uni
= &shProg
->UniformStorage
[index
];
112 case GL_UNIFORM_TYPE
:
113 params
[i
] = uni
->type
->gl_type
;
116 case GL_UNIFORM_SIZE
:
117 /* array_elements is zero for non-arrays, but the API requires that 1 be
120 params
[i
] = MAX2(1, uni
->array_elements
);
123 case GL_UNIFORM_NAME_LENGTH
:
124 params
[i
] = strlen(uni
->name
) + 1;
126 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
129 * "If the active uniform is an array, the uniform name returned
130 * in name will always be the name of the uniform array appended
133 if (uni
->array_elements
!= 0)
137 case GL_UNIFORM_BLOCK_INDEX
:
138 params
[i
] = uni
->block_index
;
141 case GL_UNIFORM_OFFSET
:
142 params
[i
] = uni
->offset
;
145 case GL_UNIFORM_ARRAY_STRIDE
:
146 params
[i
] = uni
->array_stride
;
149 case GL_UNIFORM_MATRIX_STRIDE
:
150 params
[i
] = uni
->matrix_stride
;
153 case GL_UNIFORM_IS_ROW_MAJOR
:
154 params
[i
] = uni
->row_major
;
158 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetActiveUniformsiv(pname)");
165 validate_uniform_parameters(struct gl_context
*ctx
,
166 struct gl_shader_program
*shProg
,
167 GLint location
, GLsizei count
,
169 unsigned *array_index
,
171 bool negative_one_is_not_valid
)
173 if (!shProg
|| !shProg
->LinkStatus
) {
174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
178 if (location
== -1) {
179 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
182 * "The error INVALID_OPERATION is generated if program has not been
183 * linked successfully, or if location is not a valid location for
186 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
189 * "If the value of location is -1, the Uniform* commands will
190 * silently ignore the data passed in, and the current uniform
191 * values will not be changed."
193 * Allowing -1 for the location parameter of glUniform allows
194 * applications to avoid error paths in the case that, for example, some
195 * uniform variable is removed by the compiler / linker after
196 * optimization. In this case, the new value of the uniform is dropped
197 * on the floor. For the case of glGetUniform, there is nothing
198 * sensible to do for a location of -1.
200 * The negative_one_is_not_valid flag selects between the two behaviors.
202 if (negative_one_is_not_valid
) {
203 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
210 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
212 * "If a negative number is provided where an argument of type sizei or
213 * sizeiptr is specified, the error INVALID_VALUE is generated."
216 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
220 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
222 * "If any of the following conditions occur, an INVALID_OPERATION
223 * error is generated by the Uniform* commands, and no uniform values
228 * - if no variable with a location of location exists in the
229 * program object currently in use and location is not -1,
230 * - if count is greater than one, and the uniform declared in the
231 * shader is not an array variable,
234 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
239 _mesa_uniform_split_location_offset(shProg
, location
, loc
, array_index
);
241 if (*loc
>= shProg
->NumUserUniformStorage
) {
242 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
247 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
248 _mesa_error(ctx
, GL_INVALID_OPERATION
,
249 "%s(count > 1 for non-array, location=%d)",
254 /* If the uniform is an array, check that array_index is in bounds.
255 * If not an array, check that array_index is zero.
256 * array_index is unsigned so no need to check for less than zero.
258 unsigned limit
= shProg
->UniformStorage
[*loc
].array_elements
;
261 if (*array_index
>= limit
) {
262 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
270 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
273 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
274 GLsizei bufSize
, enum glsl_base_type returnType
,
277 struct gl_shader_program
*shProg
=
278 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
279 struct gl_uniform_storage
*uni
;
280 unsigned loc
, offset
;
282 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
283 &loc
, &offset
, "glGetUniform", true))
286 uni
= &shProg
->UniformStorage
[loc
];
289 unsigned elements
= (uni
->type
->is_sampler())
290 ? 1 : uni
->type
->components();
292 /* Calculate the source base address *BEFORE* modifying elements to
293 * account for the size of the user's buffer.
295 const union gl_constant_value
*const src
=
296 &uni
->storage
[offset
* elements
];
298 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
299 returnType
== GLSL_TYPE_UINT
);
300 /* The three (currently) supported types all have the same size,
301 * which is of course the same as their union. That'll change
302 * with glGetUniformdv()...
304 unsigned bytes
= sizeof(src
[0]) * elements
;
305 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
306 _mesa_error( ctx
, GL_INVALID_OPERATION
,
307 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
308 " but %u bytes are required)", bufSize
, bytes
);
312 /* If the return type and the uniform's native type are "compatible,"
313 * just memcpy the data. If the types are not compatible, perform a
314 * slower convert-and-copy process.
316 if (returnType
== uni
->type
->base_type
317 || ((returnType
== GLSL_TYPE_INT
318 || returnType
== GLSL_TYPE_UINT
319 || returnType
== GLSL_TYPE_SAMPLER
)
321 (uni
->type
->base_type
== GLSL_TYPE_INT
322 || uni
->type
->base_type
== GLSL_TYPE_UINT
323 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
324 memcpy(paramsOut
, src
, bytes
);
326 union gl_constant_value
*const dst
=
327 (union gl_constant_value
*) paramsOut
;
329 /* This code could be optimized by putting the loop inside the switch
330 * statements. However, this is not expected to be
331 * performance-critical code.
333 for (unsigned i
= 0; i
< elements
; i
++) {
334 switch (returnType
) {
335 case GLSL_TYPE_FLOAT
:
336 switch (uni
->type
->base_type
) {
338 dst
[i
].f
= (float) src
[i
].u
;
341 case GLSL_TYPE_SAMPLER
:
342 dst
[i
].f
= (float) src
[i
].i
;
345 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
348 assert(!"Should not get here.");
355 switch (uni
->type
->base_type
) {
356 case GLSL_TYPE_FLOAT
:
357 /* While the GL 3.2 core spec doesn't explicitly
358 * state how conversion of float uniforms to integer
359 * values works, in section 6.2 "State Tables" on
362 * "Unless otherwise specified, when floating
363 * point state is returned as integer values or
364 * integer state is returned as floating-point
365 * values it is converted in the fashion
366 * described in section 6.1.2"
368 * That section, on page 248, says:
370 * "If GetIntegerv or GetInteger64v are called,
371 * a floating-point value is rounded to the
372 * nearest integer..."
374 dst
[i
].i
= IROUND(src
[i
].f
);
377 dst
[i
].i
= src
[i
].i
? 1 : 0;
380 assert(!"Should not get here.");
386 assert(!"Should not get here.");
395 log_uniform(const void *values
, enum glsl_base_type basicType
,
396 unsigned rows
, unsigned cols
, unsigned count
,
398 const struct gl_shader_program
*shProg
,
400 const struct gl_uniform_storage
*uni
)
403 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
404 const unsigned elems
= rows
* cols
* count
;
405 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
407 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
408 "transpose = %s) to: ",
409 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
410 transpose
? "true" : "false");
411 for (unsigned i
= 0; i
< elems
; i
++) {
412 if (i
!= 0 && ((i
% rows
) == 0))
417 printf("%u ", v
[i
].u
);
420 printf("%d ", v
[i
].i
);
422 case GLSL_TYPE_FLOAT
:
423 printf("%g ", v
[i
].f
);
426 assert(!"Should not get here.");
436 log_program_parameters(const struct gl_shader_program
*shProg
)
438 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
439 if (shProg
->_LinkedShaders
[i
] == NULL
)
442 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
444 printf("Program %d %s shader parameters:\n",
445 shProg
->Name
, _mesa_glsl_shader_target_name(prog
->Target
));
446 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
447 printf("%s: %p %f %f %f %f\n",
448 prog
->Parameters
->Parameters
[j
].Name
,
449 prog
->Parameters
->ParameterValues
[j
],
450 prog
->Parameters
->ParameterValues
[j
][0].f
,
451 prog
->Parameters
->ParameterValues
[j
][1].f
,
452 prog
->Parameters
->ParameterValues
[j
][2].f
,
453 prog
->Parameters
->ParameterValues
[j
][3].f
);
461 * Propagate some values from uniform backing storage to driver storage
463 * Values propagated from uniform backing storage to driver storage
464 * have all format / type conversions previously requested by the
465 * driver applied. This function is most often called by the
466 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
469 * \param uni Uniform whose data is to be propagated to driver storage
470 * \param array_index If \c uni is an array, this is the element of
471 * the array to be propagated.
472 * \param count Number of array elements to propagate.
475 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
476 unsigned array_index
,
481 /* vector_elements and matrix_columns can be 0 for samplers.
483 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
484 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
486 /* Store the data in the driver's requested type in the driver's storage
489 unsigned src_vector_byte_stride
= components
* 4;
491 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
492 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
493 uint8_t *dst
= (uint8_t *) store
->data
;
494 const unsigned extra_stride
=
495 store
->element_stride
- (vectors
* store
->vector_stride
);
497 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
500 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
502 __func__
, dst
, array_index
, components
,
503 vectors
, count
, store
->vector_stride
, extra_stride
);
506 dst
+= array_index
* store
->element_stride
;
508 switch (store
->format
) {
510 case uniform_bool_int_0_1
: {
514 for (j
= 0; j
< count
; j
++) {
515 for (v
= 0; v
< vectors
; v
++) {
516 memcpy(dst
, src
, src_vector_byte_stride
);
517 src
+= src_vector_byte_stride
;
518 dst
+= store
->vector_stride
;
526 case uniform_int_float
:
527 case uniform_bool_float
: {
528 const int *isrc
= (const int *) src
;
533 for (j
= 0; j
< count
; j
++) {
534 for (v
= 0; v
< vectors
; v
++) {
535 for (c
= 0; c
< components
; c
++) {
536 ((float *) dst
)[c
] = (float) *isrc
;
540 dst
+= store
->vector_stride
;
548 case uniform_bool_int_0_not0
: {
549 const int *isrc
= (const int *) src
;
554 for (j
= 0; j
< count
; j
++) {
555 for (v
= 0; v
< vectors
; v
++) {
556 for (c
= 0; c
< components
; c
++) {
557 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
561 dst
+= store
->vector_stride
;
570 assert(!"Should not get here.");
577 * Called via glUniform*() functions.
580 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
581 GLint location
, GLsizei count
,
582 const GLvoid
*values
, GLenum type
)
584 unsigned loc
, offset
;
586 unsigned src_components
;
587 enum glsl_base_type basicType
;
588 struct gl_uniform_storage
*uni
;
590 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
591 &loc
, &offset
, "glUniform", false))
594 uni
= &shProg
->UniformStorage
[loc
];
596 /* Verify that the types are compatible.
600 basicType
= GLSL_TYPE_FLOAT
;
604 basicType
= GLSL_TYPE_FLOAT
;
608 basicType
= GLSL_TYPE_FLOAT
;
612 basicType
= GLSL_TYPE_FLOAT
;
615 case GL_UNSIGNED_INT
:
616 basicType
= GLSL_TYPE_UINT
;
619 case GL_UNSIGNED_INT_VEC2
:
620 basicType
= GLSL_TYPE_UINT
;
623 case GL_UNSIGNED_INT_VEC3
:
624 basicType
= GLSL_TYPE_UINT
;
627 case GL_UNSIGNED_INT_VEC4
:
628 basicType
= GLSL_TYPE_UINT
;
632 basicType
= GLSL_TYPE_INT
;
636 basicType
= GLSL_TYPE_INT
;
640 basicType
= GLSL_TYPE_INT
;
644 basicType
= GLSL_TYPE_INT
;
652 case GL_FLOAT_MAT2x3
:
653 case GL_FLOAT_MAT2x4
:
654 case GL_FLOAT_MAT3x2
:
656 case GL_FLOAT_MAT3x4
:
657 case GL_FLOAT_MAT4x2
:
658 case GL_FLOAT_MAT4x3
:
661 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
665 if (uni
->type
->is_sampler()) {
668 components
= uni
->type
->vector_elements
;
672 switch (uni
->type
->base_type
) {
676 case GLSL_TYPE_SAMPLER
:
677 match
= (basicType
== GLSL_TYPE_INT
);
680 match
= (basicType
== uni
->type
->base_type
);
684 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
685 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
689 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
690 log_uniform(values
, basicType
, components
, 1, count
,
691 false, shProg
, location
, uni
);
694 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
696 * "Setting a sampler's value to i selects texture image unit number
697 * i. The values of i range from zero to the implementation- dependent
698 * maximum supported number of texture image units."
700 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
703 * "Error Description Offending command
706 * INVALID_VALUE Numeric argument out of range Yes"
708 * Based on that, when an invalid sampler is specified, we generate a
709 * GL_INVALID_VALUE error and ignore the command.
711 if (uni
->type
->is_sampler()) {
714 for (i
= 0; i
< count
; i
++) {
715 const unsigned texUnit
= ((unsigned *) values
)[i
];
717 /* check that the sampler (tex unit index) is legal */
718 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
719 _mesa_error(ctx
, GL_INVALID_VALUE
,
720 "glUniform1i(invalid sampler/tex unit index for "
728 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
730 * "When loading N elements starting at an arbitrary position k in a
731 * uniform declared as an array, elements k through k + N - 1 in the
732 * array will be replaced with the new values. Values for any array
733 * element that exceeds the highest array element index used, as
734 * reported by GetActiveUniform, will be ignored by the GL."
736 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
737 * will have already generated an error.
739 if (uni
->array_elements
!= 0) {
740 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
743 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
745 /* Store the data in the "actual type" backing storage for the uniform.
747 if (!uni
->type
->is_boolean()) {
748 memcpy(&uni
->storage
[components
* offset
], values
,
749 sizeof(uni
->storage
[0]) * components
* count
);
751 const union gl_constant_value
*src
=
752 (const union gl_constant_value
*) values
;
753 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
754 const unsigned elems
= components
* count
;
757 for (i
= 0; i
< elems
; i
++) {
758 if (basicType
== GLSL_TYPE_FLOAT
) {
759 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
761 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
766 uni
->initialized
= true;
768 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
770 /* If the uniform is a sampler, do the extra magic necessary to propagate
771 * the changes through.
773 if (uni
->type
->is_sampler()) {
776 bool flushed
= false;
777 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
778 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
781 /* If the shader stage doesn't use the sampler uniform, skip this.
783 if (sh
== NULL
|| !uni
->sampler
[i
].active
)
786 for (j
= 0; j
< count
; j
++) {
787 sh
->SamplerUnits
[uni
->sampler
[i
].index
+ offset
+ j
] =
788 ((unsigned *) values
)[j
];
791 struct gl_program
*const prog
= sh
->Program
;
793 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
795 /* Determine if any of the samplers used by this shader stage have
798 bool changed
= false;
799 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
800 if ((sh
->active_samplers
& (1U << j
)) != 0
801 && (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
])) {
809 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
813 memcpy(prog
->SamplerUnits
,
815 sizeof(sh
->SamplerUnits
));
817 _mesa_update_shader_textures_used(shProg
, prog
);
818 if (ctx
->Driver
.SamplerUniformChange
)
819 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
826 * Called by glUniformMatrix*() functions.
827 * Note: cols=2, rows=4 ==> array[2] of vec4
830 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
831 GLuint cols
, GLuint rows
,
832 GLint location
, GLsizei count
,
833 GLboolean transpose
, const GLfloat
*values
)
835 unsigned loc
, offset
;
839 struct gl_uniform_storage
*uni
;
841 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
842 &loc
, &offset
, "glUniformMatrix", false))
845 uni
= &shProg
->UniformStorage
[loc
];
846 if (!uni
->type
->is_matrix()) {
847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
848 "glUniformMatrix(non-matrix uniform)");
852 assert(!uni
->type
->is_sampler());
853 vectors
= uni
->type
->matrix_columns
;
854 components
= uni
->type
->vector_elements
;
856 /* Verify that the types are compatible. This is greatly simplified for
857 * matrices because they can only have a float base type.
859 if (vectors
!= cols
|| components
!= rows
) {
860 _mesa_error(ctx
, GL_INVALID_OPERATION
,
861 "glUniformMatrix(matrix size mismatch)");
865 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
866 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
867 if (ctx
->API
== API_OPENGLES
868 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)) {
870 _mesa_error(ctx
, GL_INVALID_VALUE
,
871 "glUniformMatrix(matrix transpose is not GL_FALSE)");
876 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
877 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
878 bool(transpose
), shProg
, location
, uni
);
881 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
883 * "When loading N elements starting at an arbitrary position k in a
884 * uniform declared as an array, elements k through k + N - 1 in the
885 * array will be replaced with the new values. Values for any array
886 * element that exceeds the highest array element index used, as
887 * reported by GetActiveUniform, will be ignored by the GL."
889 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
890 * will have already generated an error.
892 if (uni
->array_elements
!= 0) {
893 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
896 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
898 /* Store the data in the "actual type" backing storage for the uniform.
900 elements
= components
* vectors
;
903 memcpy(&uni
->storage
[elements
* offset
], values
,
904 sizeof(uni
->storage
[0]) * elements
* count
);
906 /* Copy and transpose the matrix.
908 const float *src
= values
;
909 float *dst
= &uni
->storage
[elements
* offset
].f
;
911 for (int i
= 0; i
< count
; i
++) {
912 for (unsigned r
= 0; r
< rows
; r
++) {
913 for (unsigned c
= 0; c
< cols
; c
++) {
914 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
923 uni
->initialized
= true;
925 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
930 * Called via glGetUniformLocation().
932 * Returns the uniform index into UniformStorage (also the
933 * glGetActiveUniformsiv uniform index), and stores the referenced
934 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
935 * return values can be encoded into a uniform location for
936 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
939 _mesa_get_uniform_location(struct gl_context
*ctx
,
940 struct gl_shader_program
*shProg
,
942 unsigned *out_offset
)
944 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
946 * "The first element of a uniform array is identified using the
947 * name of the uniform array appended with "[0]". Except if the last
948 * part of the string name indicates a uniform array, then the
949 * location of the first element of that array can be retrieved by
950 * either using the name of the uniform array, or the name of the
951 * uniform array appended with "[0]"."
953 * Note: since uniform names are not allowed to use whitespace, and array
954 * indices within uniform names are not allowed to use "+", "-", or leading
955 * zeros, it follows that each uniform has a unique name up to the possible
956 * ambiguity with "[0]" noted above. Therefore we don't need to worry
957 * about mal-formed inputs--they will properly fail when we try to look up
958 * the uniform name in shProg->UniformHash.
961 const GLchar
*base_name_end
;
962 long offset
= parse_program_resource_name(name
, &base_name_end
);
963 bool array_lookup
= offset
>= 0;
967 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
968 memcpy(name_copy
, name
, base_name_end
- name
);
969 name_copy
[base_name_end
- name
] = '\0';
971 name_copy
= (char *) name
;
975 unsigned location
= 0;
976 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
979 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
981 /* Free the temporary buffer *before* possibly returning an error.
983 if (name_copy
!= name
)
987 return GL_INVALID_INDEX
;
989 /* If the uniform is an array, fail if the index is out of bounds.
990 * (A negative index is caught above.) This also fails if the uniform
991 * is not an array, but the user is trying to index it, because
992 * array_elements is zero and offset >= 0.
995 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
996 return GL_INVALID_INDEX
;
999 *out_offset
= offset
;
1004 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1005 char *errMsg
, size_t errMsgLength
)
1007 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1009 memset(unit_types
, 0, sizeof(unit_types
));
1011 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1012 const struct gl_uniform_storage
*const storage
=
1013 &shProg
->UniformStorage
[i
];
1014 const glsl_type
*const t
= (storage
->type
->is_array())
1015 ? storage
->type
->fields
.array
: storage
->type
;
1017 if (!t
->is_sampler())
1020 const unsigned count
= MAX2(1, storage
->type
->array_size());
1021 for (unsigned j
= 0; j
< count
; j
++) {
1022 const unsigned unit
= storage
->storage
[j
].i
;
1024 /* The types of the samplers associated with a particular texture
1025 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1026 * OpenGL 3.3 core spec says:
1028 * "It is not allowed to have variables of different sampler
1029 * types pointing to the same texture image unit within a program
1032 if (unit_types
[unit
] == NULL
) {
1033 unit_types
[unit
] = t
;
1034 } else if (unit_types
[unit
] != t
) {
1035 _mesa_snprintf(errMsg
, errMsgLength
,
1036 "Texture unit %d is accessed both as %s and %s",
1037 unit
, unit_types
[unit
]->name
, t
->name
);