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 * 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 #include "main/core.h"
29 #include "main/context.h"
31 #include "ir_uniform.h"
32 #include "program/hash_table.h"
33 #include "../glsl/program.h"
34 #include "../glsl/ir_uniform.h"
35 #include "main/shaderapi.h"
36 #include "main/shaderobj.h"
40 extern "C" void GLAPIENTRY
41 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
42 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
43 GLenum
*type
, GLcharARB
*nameOut
)
45 GET_CURRENT_CONTEXT(ctx
);
46 struct gl_shader_program
*shProg
=
47 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
49 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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_copy_string(nameOut
, maxLength
, length
, uni
->name
);
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
];
100 const struct gl_uniform_storage
*uni
= &shProg
->UniformStorage
[index
];
102 if (index
>= shProg
->NumUserUniformStorage
) {
103 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
108 case GL_UNIFORM_TYPE
:
109 params
[i
] = uni
->type
->gl_type
;
112 case GL_UNIFORM_SIZE
:
113 /* array_elements is zero for non-arrays, but the API requires that 1 be
116 params
[i
] = MAX2(1, uni
->array_elements
);
119 case GL_UNIFORM_NAME_LENGTH
:
120 params
[i
] = strlen(uni
->name
) + 1;
123 case GL_UNIFORM_BLOCK_INDEX
:
124 params
[i
] = uni
->block_index
;
127 case GL_UNIFORM_OFFSET
:
128 params
[i
] = uni
->offset
;
131 case GL_UNIFORM_ARRAY_STRIDE
:
132 params
[i
] = uni
->array_stride
;
135 case GL_UNIFORM_MATRIX_STRIDE
:
136 params
[i
] = uni
->matrix_stride
;
139 case GL_UNIFORM_IS_ROW_MAJOR
:
140 params
[i
] = uni
->row_major
;
144 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetActiveUniformsiv(pname)");
151 validate_uniform_parameters(struct gl_context
*ctx
,
152 struct gl_shader_program
*shProg
,
153 GLint location
, GLsizei count
,
155 unsigned *array_index
,
157 bool negative_one_is_not_valid
)
159 if (!shProg
|| !shProg
->LinkStatus
) {
160 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
164 if (location
== -1) {
165 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
168 * "The error INVALID_OPERATION is generated if program has not been
169 * linked successfully, or if location is not a valid location for
172 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
175 * "If the value of location is -1, the Uniform* commands will
176 * silently ignore the data passed in, and the current uniform
177 * values will not be changed."
179 * Allowing -1 for the location parameter of glUniform allows
180 * applications to avoid error paths in the case that, for example, some
181 * uniform variable is removed by the compiler / linker after
182 * optimization. In this case, the new value of the uniform is dropped
183 * on the floor. For the case of glGetUniform, there is nothing
184 * sensible to do for a location of -1.
186 * The negative_one_is_not_valid flag selects between the two behaviors.
188 if (negative_one_is_not_valid
) {
189 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
196 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
198 * "If a negative number is provided where an argument of type sizei or
199 * sizeiptr is specified, the error INVALID_VALUE is generated."
202 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
206 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
208 * "If any of the following conditions occur, an INVALID_OPERATION
209 * error is generated by the Uniform* commands, and no uniform values
214 * - if no variable with a location of location exists in the
215 * program object currently in use and location is not -1,
216 * - if count is greater than one, and the uniform declared in the
217 * shader is not an array variable,
220 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
225 _mesa_uniform_split_location_offset(location
, loc
, array_index
);
227 if (*loc
>= shProg
->NumUserUniformStorage
) {
228 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
233 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
235 "%s(count > 1 for non-array, location=%d)",
240 /* This case should be impossible. The implication is that a call like
241 * glGetUniformLocation(prog, "foo[8]") was successful but "foo" is not an
244 if (*array_index
!= 0 && shProg
->UniformStorage
[*loc
].array_elements
== 0) {
245 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
253 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
256 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
257 GLsizei bufSize
, enum glsl_base_type returnType
,
260 struct gl_shader_program
*shProg
=
261 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
262 struct gl_uniform_storage
*uni
;
263 unsigned loc
, offset
;
265 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
266 &loc
, &offset
, "glGetUniform", true))
269 uni
= &shProg
->UniformStorage
[loc
];
272 unsigned elements
= (uni
->type
->is_sampler())
273 ? 1 : uni
->type
->components();
275 /* Calculate the source base address *BEFORE* modifying elements to
276 * account for the size of the user's buffer.
278 const union gl_constant_value
*const src
=
279 &uni
->storage
[offset
* elements
];
281 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
282 returnType
== GLSL_TYPE_UINT
);
283 /* The three (currently) supported types all have the same size,
284 * which is of course the same as their union. That'll change
285 * with glGetUniformdv()...
287 unsigned bytes
= sizeof(src
[0]) * elements
;
288 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
289 _mesa_error( ctx
, GL_INVALID_OPERATION
,
290 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
291 " but %u bytes are required)", bufSize
, bytes
);
295 /* If the return type and the uniform's native type are "compatible,"
296 * just memcpy the data. If the types are not compatible, perform a
297 * slower convert-and-copy process.
299 if (returnType
== uni
->type
->base_type
300 || ((returnType
== GLSL_TYPE_INT
301 || returnType
== GLSL_TYPE_UINT
302 || returnType
== GLSL_TYPE_SAMPLER
)
304 (uni
->type
->base_type
== GLSL_TYPE_INT
305 || uni
->type
->base_type
== GLSL_TYPE_UINT
306 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
307 memcpy(paramsOut
, src
, bytes
);
309 union gl_constant_value
*const dst
=
310 (union gl_constant_value
*) paramsOut
;
312 /* This code could be optimized by putting the loop inside the switch
313 * statements. However, this is not expected to be
314 * performance-critical code.
316 for (unsigned i
= 0; i
< elements
; i
++) {
317 switch (returnType
) {
318 case GLSL_TYPE_FLOAT
:
319 switch (uni
->type
->base_type
) {
321 dst
[i
].f
= (float) src
[i
].u
;
324 case GLSL_TYPE_SAMPLER
:
325 dst
[i
].f
= (float) src
[i
].i
;
328 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
331 assert(!"Should not get here.");
338 switch (uni
->type
->base_type
) {
339 case GLSL_TYPE_FLOAT
:
340 /* While the GL 3.2 core spec doesn't explicitly
341 * state how conversion of float uniforms to integer
342 * values works, in section 6.2 "State Tables" on
345 * "Unless otherwise specified, when floating
346 * point state is returned as integer values or
347 * integer state is returned as floating-point
348 * values it is converted in the fashion
349 * described in section 6.1.2"
351 * That section, on page 248, says:
353 * "If GetIntegerv or GetInteger64v are called,
354 * a floating-point value is rounded to the
355 * nearest integer..."
357 dst
[i
].i
= IROUND(src
[i
].f
);
360 dst
[i
].i
= src
[i
].i
? 1 : 0;
363 assert(!"Should not get here.");
369 assert(!"Should not get here.");
378 log_uniform(const void *values
, enum glsl_base_type basicType
,
379 unsigned rows
, unsigned cols
, unsigned count
,
381 const struct gl_shader_program
*shProg
,
383 const struct gl_uniform_storage
*uni
)
386 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
387 const unsigned elems
= rows
* cols
* count
;
388 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
390 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
391 "transpose = %s) to: ",
392 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
393 transpose
? "true" : "false");
394 for (unsigned i
= 0; i
< elems
; i
++) {
395 if (i
!= 0 && ((i
% rows
) == 0))
400 printf("%u ", v
[i
].u
);
403 printf("%d ", v
[i
].i
);
405 case GLSL_TYPE_FLOAT
:
406 printf("%g ", v
[i
].f
);
409 assert(!"Should not get here.");
419 log_program_parameters(const struct gl_shader_program
*shProg
)
421 static const char *stages
[] = {
422 "vertex", "fragment", "geometry"
425 assert(Elements(stages
) == MESA_SHADER_TYPES
);
427 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
428 if (shProg
->_LinkedShaders
[i
] == NULL
)
431 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
433 printf("Program %d %s shader parameters:\n",
434 shProg
->Name
, stages
[i
]);
435 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
436 printf("%s: %p %f %f %f %f\n",
437 prog
->Parameters
->Parameters
[j
].Name
,
438 prog
->Parameters
->ParameterValues
[j
],
439 prog
->Parameters
->ParameterValues
[j
][0].f
,
440 prog
->Parameters
->ParameterValues
[j
][1].f
,
441 prog
->Parameters
->ParameterValues
[j
][2].f
,
442 prog
->Parameters
->ParameterValues
[j
][3].f
);
450 * Propagate some values from uniform backing storage to driver storage
452 * Values propagated from uniform backing storage to driver storage
453 * have all format / type conversions previously requested by the
454 * driver applied. This function is most often called by the
455 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
458 * \param uni Uniform whose data is to be propagated to driver storage
459 * \param array_index If \c uni is an array, this is the element of
460 * the array to be propagated.
461 * \param count Number of array elements to propagate.
464 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
465 unsigned array_index
,
470 /* vector_elements and matrix_columns can be 0 for samplers.
472 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
473 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
475 /* Store the data in the driver's requested type in the driver's storage
478 unsigned src_vector_byte_stride
= components
* 4;
480 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
481 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
482 uint8_t *dst
= (uint8_t *) store
->data
;
483 const unsigned extra_stride
=
484 store
->element_stride
- (vectors
* store
->vector_stride
);
486 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
489 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
491 __func__
, dst
, array_index
, components
,
492 vectors
, count
, store
->vector_stride
, extra_stride
);
495 dst
+= array_index
* store
->element_stride
;
497 switch (store
->format
) {
499 case uniform_bool_int_0_1
: {
503 for (j
= 0; j
< count
; j
++) {
504 for (v
= 0; v
< vectors
; v
++) {
505 memcpy(dst
, src
, src_vector_byte_stride
);
506 src
+= src_vector_byte_stride
;
507 dst
+= store
->vector_stride
;
515 case uniform_int_float
:
516 case uniform_bool_float
: {
517 const int *isrc
= (const int *) src
;
522 for (j
= 0; j
< count
; j
++) {
523 for (v
= 0; v
< vectors
; v
++) {
524 for (c
= 0; c
< components
; c
++) {
525 ((float *) dst
)[c
] = (float) *isrc
;
529 dst
+= store
->vector_stride
;
537 case uniform_bool_int_0_not0
: {
538 const int *isrc
= (const int *) src
;
543 for (j
= 0; j
< count
; j
++) {
544 for (v
= 0; v
< vectors
; v
++) {
545 for (c
= 0; c
< components
; c
++) {
546 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
550 dst
+= store
->vector_stride
;
559 assert(!"Should not get here.");
566 * Called via glUniform*() functions.
569 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
570 GLint location
, GLsizei count
,
571 const GLvoid
*values
, GLenum type
)
573 unsigned loc
, offset
;
575 unsigned src_components
;
576 enum glsl_base_type basicType
;
577 struct gl_uniform_storage
*uni
;
579 ASSERT_OUTSIDE_BEGIN_END(ctx
);
581 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
582 &loc
, &offset
, "glUniform", false))
585 uni
= &shProg
->UniformStorage
[loc
];
587 /* Verify that the types are compatible.
591 basicType
= GLSL_TYPE_FLOAT
;
595 basicType
= GLSL_TYPE_FLOAT
;
599 basicType
= GLSL_TYPE_FLOAT
;
603 basicType
= GLSL_TYPE_FLOAT
;
606 case GL_UNSIGNED_INT
:
607 basicType
= GLSL_TYPE_UINT
;
610 case GL_UNSIGNED_INT_VEC2
:
611 basicType
= GLSL_TYPE_UINT
;
614 case GL_UNSIGNED_INT_VEC3
:
615 basicType
= GLSL_TYPE_UINT
;
618 case GL_UNSIGNED_INT_VEC4
:
619 basicType
= GLSL_TYPE_UINT
;
623 basicType
= GLSL_TYPE_INT
;
627 basicType
= GLSL_TYPE_INT
;
631 basicType
= GLSL_TYPE_INT
;
635 basicType
= GLSL_TYPE_INT
;
643 case GL_FLOAT_MAT2x3
:
644 case GL_FLOAT_MAT2x4
:
645 case GL_FLOAT_MAT3x2
:
647 case GL_FLOAT_MAT3x4
:
648 case GL_FLOAT_MAT4x2
:
649 case GL_FLOAT_MAT4x3
:
652 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
656 if (uni
->type
->is_sampler()) {
659 components
= uni
->type
->vector_elements
;
663 switch (uni
->type
->base_type
) {
667 case GLSL_TYPE_SAMPLER
:
668 match
= (basicType
== GLSL_TYPE_INT
);
671 match
= (basicType
== uni
->type
->base_type
);
675 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
676 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
680 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
681 log_uniform(values
, basicType
, components
, 1, count
,
682 false, shProg
, location
, uni
);
685 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
687 * "Setting a sampler's value to i selects texture image unit number
688 * i. The values of i range from zero to the implementation- dependent
689 * maximum supported number of texture image units."
691 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
694 * "Error Description Offending command
697 * INVALID_VALUE Numeric argument out of range Yes"
699 * Based on that, when an invalid sampler is specified, we generate a
700 * GL_INVALID_VALUE error and ignore the command.
702 if (uni
->type
->is_sampler()) {
705 for (i
= 0; i
< count
; i
++) {
706 const unsigned texUnit
= ((unsigned *) values
)[i
];
708 /* check that the sampler (tex unit index) is legal */
709 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
710 _mesa_error(ctx
, GL_INVALID_VALUE
,
711 "glUniform1i(invalid sampler/tex unit index for "
719 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
721 * "When loading N elements starting at an arbitrary position k in a
722 * uniform declared as an array, elements k through k + N - 1 in the
723 * array will be replaced with the new values. Values for any array
724 * element that exceeds the highest array element index used, as
725 * reported by GetActiveUniform, will be ignored by the GL."
727 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
728 * will have already generated an error.
730 if (uni
->array_elements
!= 0) {
731 if (offset
>= uni
->array_elements
)
734 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
737 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
739 /* Store the data in the "actual type" backing storage for the uniform.
741 if (!uni
->type
->is_boolean()) {
742 memcpy(&uni
->storage
[components
* offset
], values
,
743 sizeof(uni
->storage
[0]) * components
* count
);
745 const union gl_constant_value
*src
=
746 (const union gl_constant_value
*) values
;
747 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
748 const unsigned elems
= components
* count
;
751 for (i
= 0; i
< elems
; i
++) {
752 if (basicType
== GLSL_TYPE_FLOAT
) {
753 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
755 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
760 uni
->initialized
= true;
762 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
764 /* If the uniform is a sampler, do the extra magic necessary to propagate
765 * the changes through.
767 if (uni
->type
->is_sampler()) {
770 for (i
= 0; i
< count
; i
++) {
771 shProg
->SamplerUnits
[uni
->sampler
+ offset
+ i
] =
772 ((unsigned *) values
)[i
];
775 bool flushed
= false;
776 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
777 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
779 /* If the shader stage doesn't use any samplers, don't bother
780 * checking if any samplers have changed.
782 if (sh
== NULL
|| sh
->active_samplers
== 0)
785 struct gl_program
*const prog
= sh
->Program
;
787 assert(sizeof(prog
->SamplerUnits
) == sizeof(shProg
->SamplerUnits
));
789 /* Determine if any of the samplers used by this shader stage have
792 bool changed
= false;
793 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
794 if ((sh
->active_samplers
& (1U << j
)) != 0
795 && (prog
->SamplerUnits
[j
] != shProg
->SamplerUnits
[j
])) {
803 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
807 memcpy(prog
->SamplerUnits
,
808 shProg
->SamplerUnits
,
809 sizeof(shProg
->SamplerUnits
));
811 _mesa_update_shader_textures_used(shProg
, prog
);
812 (void) ctx
->Driver
.ProgramStringNotify(ctx
, prog
->Target
, prog
);
819 * Called by glUniformMatrix*() functions.
820 * Note: cols=2, rows=4 ==> array[2] of vec4
823 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
824 GLuint cols
, GLuint rows
,
825 GLint location
, GLsizei count
,
826 GLboolean transpose
, const GLfloat
*values
)
828 unsigned loc
, offset
;
832 struct gl_uniform_storage
*uni
;
834 ASSERT_OUTSIDE_BEGIN_END(ctx
);
836 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
837 &loc
, &offset
, "glUniformMatrix", false))
840 uni
= &shProg
->UniformStorage
[loc
];
841 if (!uni
->type
->is_matrix()) {
842 _mesa_error(ctx
, GL_INVALID_OPERATION
,
843 "glUniformMatrix(non-matrix uniform)");
847 assert(!uni
->type
->is_sampler());
848 vectors
= uni
->type
->matrix_columns
;
849 components
= uni
->type
->vector_elements
;
851 /* Verify that the types are compatible. This is greatly simplified for
852 * matrices because they can only have a float base type.
854 if (vectors
!= cols
|| components
!= rows
) {
855 _mesa_error(ctx
, GL_INVALID_OPERATION
,
856 "glUniformMatrix(matrix size mismatch)");
860 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
861 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
862 if (ctx
->API
== API_OPENGLES
|| ctx
->API
== API_OPENGLES2
) {
864 _mesa_error(ctx
, GL_INVALID_VALUE
,
865 "glUniformMatrix(matrix transpose is not GL_FALSE)");
870 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
871 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
872 bool(transpose
), shProg
, location
, uni
);
875 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
877 * "When loading N elements starting at an arbitrary position k in a
878 * uniform declared as an array, elements k through k + N - 1 in the
879 * array will be replaced with the new values. Values for any array
880 * element that exceeds the highest array element index used, as
881 * reported by GetActiveUniform, will be ignored by the GL."
883 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
884 * will have already generated an error.
886 if (uni
->array_elements
!= 0) {
887 if (offset
>= uni
->array_elements
)
890 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
893 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
895 /* Store the data in the "actual type" backing storage for the uniform.
897 elements
= components
* vectors
;
900 memcpy(&uni
->storage
[elements
* offset
], values
,
901 sizeof(uni
->storage
[0]) * elements
* count
);
903 /* Copy and transpose the matrix.
905 const float *src
= values
;
906 float *dst
= &uni
->storage
[elements
* offset
].f
;
908 for (int i
= 0; i
< count
; i
++) {
909 for (unsigned r
= 0; r
< rows
; r
++) {
910 for (unsigned c
= 0; c
< cols
; c
++) {
911 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
920 uni
->initialized
= true;
922 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
926 * Called via glGetUniformLocation().
928 * Returns the uniform index into UniformStorage (also the
929 * glGetActiveUniformsiv uniform index), and stores the referenced
930 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
931 * return values can be encoded into a uniform location for
932 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
935 _mesa_get_uniform_location(struct gl_context
*ctx
,
936 struct gl_shader_program
*shProg
,
938 unsigned *out_offset
)
940 const size_t len
= strlen(name
);
945 /* If the name ends with a ']', assume that it refers to some element of an
946 * array. Malformed array references will fail the hash table look up
947 * below, so it doesn't matter that they are not caught here. This code
948 * only wants to catch the "leaf" array references so that arrays of
949 * structures containing arrays will be handled correctly.
951 if (name
[len
-1] == ']') {
954 /* Walk backwards over the string looking for a non-digit character.
955 * This had better be the opening bracket for an array index.
957 * Initially, i specifies the location of the ']'. Since the string may
958 * contain only the ']' charcater, walk backwards very carefully.
960 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
963 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
965 * "The first element of a uniform array is identified using the
966 * name of the uniform array appended with "[0]". Except if the last
967 * part of the string name indicates a uniform array, then the
968 * location of the first element of that array can be retrieved by
969 * either using the name of the uniform array, or the name of the
970 * uniform array appended with "[0]"."
972 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
974 * "name must be a null terminated string, without white space."
976 * Return an error if there is no opening '[' to match the closing ']'.
977 * An error will also be returned if there is intervening white space
978 * (or other non-digit characters) before the opening '['.
980 if ((i
== 0) || name
[i
-1] != '[')
981 return GL_INVALID_INDEX
;
983 /* Return an error if there are no digits between the opening '[' to
984 * match the closing ']'.
987 return GL_INVALID_INDEX
;
989 /* Make a new string that is a copy of the old string up to (but not
990 * including) the '[' character.
992 name_copy
= (char *) malloc(i
);
993 memcpy(name_copy
, name
, i
- 1);
994 name_copy
[i
-1] = '\0';
996 offset
= strtol(&name
[i
], NULL
, 10);
999 return GL_INVALID_INDEX
;
1002 array_lookup
= true;
1004 name_copy
= (char *) name
;
1006 array_lookup
= false;
1009 unsigned location
= 0;
1010 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
1013 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
1015 /* Free the temporary buffer *before* possibly returning an error.
1017 if (name_copy
!= name
)
1021 return GL_INVALID_INDEX
;
1023 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
1024 * to (correctly) fail if 'a' is not an array.
1026 if (array_lookup
&& shProg
->UniformStorage
[location
].array_elements
== 0) {
1027 return GL_INVALID_INDEX
;
1030 *out_offset
= offset
;
1035 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1036 char *errMsg
, size_t errMsgLength
)
1038 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1040 memset(unit_types
, 0, sizeof(unit_types
));
1042 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1043 const struct gl_uniform_storage
*const storage
=
1044 &shProg
->UniformStorage
[i
];
1045 const glsl_type
*const t
= (storage
->type
->is_array())
1046 ? storage
->type
->fields
.array
: storage
->type
;
1048 if (!t
->is_sampler())
1051 const unsigned count
= MAX2(1, storage
->type
->array_size());
1052 for (unsigned j
= 0; j
< count
; j
++) {
1053 const unsigned unit
= storage
->storage
[j
].i
;
1055 /* The types of the samplers associated with a particular texture
1056 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1057 * OpenGL 3.3 core spec says:
1059 * "It is not allowed to have variables of different sampler
1060 * types pointing to the same texture image unit within a program
1063 if (unit_types
[unit
] == NULL
) {
1064 unit_types
[unit
] = t
;
1065 } else if (unit_types
[unit
] != t
) {
1066 _mesa_snprintf(errMsg
, errMsgLength
,
1067 "Texture unit %d is accessed both as %s and %s",
1068 unit
, unit_types
[unit
]->name
, t
->name
);