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_GetActiveUniform(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");
52 if (index
>= shProg
->NumUserUniformStorage
) {
53 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
57 const struct gl_uniform_storage
*const uni
= &shProg
->UniformStorage
[index
];
60 _mesa_get_uniform_name(uni
, maxLength
, length
, nameOut
);
64 /* array_elements is zero for non-arrays, but the API requires that 1 be
67 *size
= MAX2(1, uni
->array_elements
);
71 *type
= uni
->type
->gl_type
;
75 extern "C" void GLAPIENTRY
76 _mesa_GetActiveUniformsiv(GLuint program
,
78 const GLuint
*uniformIndices
,
82 GET_CURRENT_CONTEXT(ctx
);
83 struct gl_shader_program
*shProg
;
86 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
90 if (uniformCount
< 0) {
91 _mesa_error(ctx
, GL_INVALID_VALUE
,
92 "glGetUniformIndices(uniformCount < 0)");
96 for (i
= 0; i
< uniformCount
; i
++) {
97 GLuint index
= uniformIndices
[i
];
99 if (index
>= shProg
->NumUserUniformStorage
) {
100 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
105 for (i
= 0; i
< uniformCount
; i
++) {
106 GLuint index
= uniformIndices
[i
];
107 const struct gl_uniform_storage
*uni
= &shProg
->UniformStorage
[index
];
110 case GL_UNIFORM_TYPE
:
111 params
[i
] = uni
->type
->gl_type
;
114 case GL_UNIFORM_SIZE
:
115 /* array_elements is zero for non-arrays, but the API requires that 1 be
118 params
[i
] = MAX2(1, uni
->array_elements
);
121 case GL_UNIFORM_NAME_LENGTH
:
122 params
[i
] = strlen(uni
->name
) + 1;
124 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
127 * "If the active uniform is an array, the uniform name returned
128 * in name will always be the name of the uniform array appended
131 if (uni
->array_elements
!= 0)
135 case GL_UNIFORM_BLOCK_INDEX
:
136 params
[i
] = uni
->block_index
;
139 case GL_UNIFORM_OFFSET
:
140 params
[i
] = uni
->offset
;
143 case GL_UNIFORM_ARRAY_STRIDE
:
144 params
[i
] = uni
->array_stride
;
147 case GL_UNIFORM_MATRIX_STRIDE
:
148 params
[i
] = uni
->matrix_stride
;
151 case GL_UNIFORM_IS_ROW_MAJOR
:
152 params
[i
] = uni
->row_major
;
156 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetActiveUniformsiv(pname)");
163 validate_uniform_parameters(struct gl_context
*ctx
,
164 struct gl_shader_program
*shProg
,
165 GLint location
, GLsizei count
,
167 unsigned *array_index
,
169 bool negative_one_is_not_valid
)
171 if (!shProg
|| !shProg
->LinkStatus
) {
172 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
176 if (location
== -1) {
177 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
180 * "The error INVALID_OPERATION is generated if program has not been
181 * linked successfully, or if location is not a valid location for
184 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
187 * "If the value of location is -1, the Uniform* commands will
188 * silently ignore the data passed in, and the current uniform
189 * values will not be changed."
191 * Allowing -1 for the location parameter of glUniform allows
192 * applications to avoid error paths in the case that, for example, some
193 * uniform variable is removed by the compiler / linker after
194 * optimization. In this case, the new value of the uniform is dropped
195 * on the floor. For the case of glGetUniform, there is nothing
196 * sensible to do for a location of -1.
198 * The negative_one_is_not_valid flag selects between the two behaviors.
200 if (negative_one_is_not_valid
) {
201 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
208 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
210 * "If a negative number is provided where an argument of type sizei or
211 * sizeiptr is specified, the error INVALID_VALUE is generated."
214 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
218 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
220 * "If any of the following conditions occur, an INVALID_OPERATION
221 * error is generated by the Uniform* commands, and no uniform values
226 * - if no variable with a location of location exists in the
227 * program object currently in use and location is not -1,
228 * - if count is greater than one, and the uniform declared in the
229 * shader is not an array variable,
232 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
237 _mesa_uniform_split_location_offset(location
, loc
, array_index
);
239 if (*loc
>= shProg
->NumUserUniformStorage
) {
240 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
245 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
246 _mesa_error(ctx
, GL_INVALID_OPERATION
,
247 "%s(count > 1 for non-array, location=%d)",
252 /* If the uniform is an array, check that array_index is in bounds.
253 * If not an array, check that array_index is zero.
254 * array_index is unsigned so no need to check for less than zero.
256 unsigned limit
= shProg
->UniformStorage
[*loc
].array_elements
;
259 if (*array_index
>= limit
) {
260 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
268 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
271 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
272 GLsizei bufSize
, enum glsl_base_type returnType
,
275 struct gl_shader_program
*shProg
=
276 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
277 struct gl_uniform_storage
*uni
;
278 unsigned loc
, offset
;
280 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
281 &loc
, &offset
, "glGetUniform", true))
284 uni
= &shProg
->UniformStorage
[loc
];
287 unsigned elements
= (uni
->type
->is_sampler())
288 ? 1 : uni
->type
->components();
290 /* Calculate the source base address *BEFORE* modifying elements to
291 * account for the size of the user's buffer.
293 const union gl_constant_value
*const src
=
294 &uni
->storage
[offset
* elements
];
296 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
297 returnType
== GLSL_TYPE_UINT
);
298 /* The three (currently) supported types all have the same size,
299 * which is of course the same as their union. That'll change
300 * with glGetUniformdv()...
302 unsigned bytes
= sizeof(src
[0]) * elements
;
303 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
304 _mesa_error( ctx
, GL_INVALID_OPERATION
,
305 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
306 " but %u bytes are required)", bufSize
, bytes
);
310 /* If the return type and the uniform's native type are "compatible,"
311 * just memcpy the data. If the types are not compatible, perform a
312 * slower convert-and-copy process.
314 if (returnType
== uni
->type
->base_type
315 || ((returnType
== GLSL_TYPE_INT
316 || returnType
== GLSL_TYPE_UINT
317 || returnType
== GLSL_TYPE_SAMPLER
)
319 (uni
->type
->base_type
== GLSL_TYPE_INT
320 || uni
->type
->base_type
== GLSL_TYPE_UINT
321 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
322 memcpy(paramsOut
, src
, bytes
);
324 union gl_constant_value
*const dst
=
325 (union gl_constant_value
*) paramsOut
;
327 /* This code could be optimized by putting the loop inside the switch
328 * statements. However, this is not expected to be
329 * performance-critical code.
331 for (unsigned i
= 0; i
< elements
; i
++) {
332 switch (returnType
) {
333 case GLSL_TYPE_FLOAT
:
334 switch (uni
->type
->base_type
) {
336 dst
[i
].f
= (float) src
[i
].u
;
339 case GLSL_TYPE_SAMPLER
:
340 dst
[i
].f
= (float) src
[i
].i
;
343 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
346 assert(!"Should not get here.");
353 switch (uni
->type
->base_type
) {
354 case GLSL_TYPE_FLOAT
:
355 /* While the GL 3.2 core spec doesn't explicitly
356 * state how conversion of float uniforms to integer
357 * values works, in section 6.2 "State Tables" on
360 * "Unless otherwise specified, when floating
361 * point state is returned as integer values or
362 * integer state is returned as floating-point
363 * values it is converted in the fashion
364 * described in section 6.1.2"
366 * That section, on page 248, says:
368 * "If GetIntegerv or GetInteger64v are called,
369 * a floating-point value is rounded to the
370 * nearest integer..."
372 dst
[i
].i
= IROUND(src
[i
].f
);
375 dst
[i
].i
= src
[i
].i
? 1 : 0;
378 assert(!"Should not get here.");
384 assert(!"Should not get here.");
393 log_uniform(const void *values
, enum glsl_base_type basicType
,
394 unsigned rows
, unsigned cols
, unsigned count
,
396 const struct gl_shader_program
*shProg
,
398 const struct gl_uniform_storage
*uni
)
401 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
402 const unsigned elems
= rows
* cols
* count
;
403 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
405 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
406 "transpose = %s) to: ",
407 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
408 transpose
? "true" : "false");
409 for (unsigned i
= 0; i
< elems
; i
++) {
410 if (i
!= 0 && ((i
% rows
) == 0))
415 printf("%u ", v
[i
].u
);
418 printf("%d ", v
[i
].i
);
420 case GLSL_TYPE_FLOAT
:
421 printf("%g ", v
[i
].f
);
424 assert(!"Should not get here.");
434 log_program_parameters(const struct gl_shader_program
*shProg
)
436 static const char *stages
[] = {
437 "vertex", "fragment", "geometry"
440 assert(Elements(stages
) == MESA_SHADER_TYPES
);
442 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
443 if (shProg
->_LinkedShaders
[i
] == NULL
)
446 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
448 printf("Program %d %s shader parameters:\n",
449 shProg
->Name
, stages
[i
]);
450 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
451 printf("%s: %p %f %f %f %f\n",
452 prog
->Parameters
->Parameters
[j
].Name
,
453 prog
->Parameters
->ParameterValues
[j
],
454 prog
->Parameters
->ParameterValues
[j
][0].f
,
455 prog
->Parameters
->ParameterValues
[j
][1].f
,
456 prog
->Parameters
->ParameterValues
[j
][2].f
,
457 prog
->Parameters
->ParameterValues
[j
][3].f
);
465 * Propagate some values from uniform backing storage to driver storage
467 * Values propagated from uniform backing storage to driver storage
468 * have all format / type conversions previously requested by the
469 * driver applied. This function is most often called by the
470 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
473 * \param uni Uniform whose data is to be propagated to driver storage
474 * \param array_index If \c uni is an array, this is the element of
475 * the array to be propagated.
476 * \param count Number of array elements to propagate.
479 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
480 unsigned array_index
,
485 /* vector_elements and matrix_columns can be 0 for samplers.
487 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
488 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
490 /* Store the data in the driver's requested type in the driver's storage
493 unsigned src_vector_byte_stride
= components
* 4;
495 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
496 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
497 uint8_t *dst
= (uint8_t *) store
->data
;
498 const unsigned extra_stride
=
499 store
->element_stride
- (vectors
* store
->vector_stride
);
501 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
504 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
506 __func__
, dst
, array_index
, components
,
507 vectors
, count
, store
->vector_stride
, extra_stride
);
510 dst
+= array_index
* store
->element_stride
;
512 switch (store
->format
) {
514 case uniform_bool_int_0_1
: {
518 for (j
= 0; j
< count
; j
++) {
519 for (v
= 0; v
< vectors
; v
++) {
520 memcpy(dst
, src
, src_vector_byte_stride
);
521 src
+= src_vector_byte_stride
;
522 dst
+= store
->vector_stride
;
530 case uniform_int_float
:
531 case uniform_bool_float
: {
532 const int *isrc
= (const int *) src
;
537 for (j
= 0; j
< count
; j
++) {
538 for (v
= 0; v
< vectors
; v
++) {
539 for (c
= 0; c
< components
; c
++) {
540 ((float *) dst
)[c
] = (float) *isrc
;
544 dst
+= store
->vector_stride
;
552 case uniform_bool_int_0_not0
: {
553 const int *isrc
= (const int *) src
;
558 for (j
= 0; j
< count
; j
++) {
559 for (v
= 0; v
< vectors
; v
++) {
560 for (c
= 0; c
< components
; c
++) {
561 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
565 dst
+= store
->vector_stride
;
574 assert(!"Should not get here.");
581 * Called via glUniform*() functions.
584 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
585 GLint location
, GLsizei count
,
586 const GLvoid
*values
, GLenum type
)
588 unsigned loc
, offset
;
590 unsigned src_components
;
591 enum glsl_base_type basicType
;
592 struct gl_uniform_storage
*uni
;
594 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
595 &loc
, &offset
, "glUniform", false))
598 uni
= &shProg
->UniformStorage
[loc
];
600 /* Verify that the types are compatible.
604 basicType
= GLSL_TYPE_FLOAT
;
608 basicType
= GLSL_TYPE_FLOAT
;
612 basicType
= GLSL_TYPE_FLOAT
;
616 basicType
= GLSL_TYPE_FLOAT
;
619 case GL_UNSIGNED_INT
:
620 basicType
= GLSL_TYPE_UINT
;
623 case GL_UNSIGNED_INT_VEC2
:
624 basicType
= GLSL_TYPE_UINT
;
627 case GL_UNSIGNED_INT_VEC3
:
628 basicType
= GLSL_TYPE_UINT
;
631 case GL_UNSIGNED_INT_VEC4
:
632 basicType
= GLSL_TYPE_UINT
;
636 basicType
= GLSL_TYPE_INT
;
640 basicType
= GLSL_TYPE_INT
;
644 basicType
= GLSL_TYPE_INT
;
648 basicType
= GLSL_TYPE_INT
;
656 case GL_FLOAT_MAT2x3
:
657 case GL_FLOAT_MAT2x4
:
658 case GL_FLOAT_MAT3x2
:
660 case GL_FLOAT_MAT3x4
:
661 case GL_FLOAT_MAT4x2
:
662 case GL_FLOAT_MAT4x3
:
665 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
669 if (uni
->type
->is_sampler()) {
672 components
= uni
->type
->vector_elements
;
676 switch (uni
->type
->base_type
) {
680 case GLSL_TYPE_SAMPLER
:
681 match
= (basicType
== GLSL_TYPE_INT
);
684 match
= (basicType
== uni
->type
->base_type
);
688 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
689 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
693 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
694 log_uniform(values
, basicType
, components
, 1, count
,
695 false, shProg
, location
, uni
);
698 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
700 * "Setting a sampler's value to i selects texture image unit number
701 * i. The values of i range from zero to the implementation- dependent
702 * maximum supported number of texture image units."
704 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
707 * "Error Description Offending command
710 * INVALID_VALUE Numeric argument out of range Yes"
712 * Based on that, when an invalid sampler is specified, we generate a
713 * GL_INVALID_VALUE error and ignore the command.
715 if (uni
->type
->is_sampler()) {
718 for (i
= 0; i
< count
; i
++) {
719 const unsigned texUnit
= ((unsigned *) values
)[i
];
721 /* check that the sampler (tex unit index) is legal */
722 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
723 _mesa_error(ctx
, GL_INVALID_VALUE
,
724 "glUniform1i(invalid sampler/tex unit index for "
732 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
734 * "When loading N elements starting at an arbitrary position k in a
735 * uniform declared as an array, elements k through k + N - 1 in the
736 * array will be replaced with the new values. Values for any array
737 * element that exceeds the highest array element index used, as
738 * reported by GetActiveUniform, will be ignored by the GL."
740 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
741 * will have already generated an error.
743 if (uni
->array_elements
!= 0) {
744 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
747 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
749 /* Store the data in the "actual type" backing storage for the uniform.
751 if (!uni
->type
->is_boolean()) {
752 memcpy(&uni
->storage
[components
* offset
], values
,
753 sizeof(uni
->storage
[0]) * components
* count
);
755 const union gl_constant_value
*src
=
756 (const union gl_constant_value
*) values
;
757 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
758 const unsigned elems
= components
* count
;
761 for (i
= 0; i
< elems
; i
++) {
762 if (basicType
== GLSL_TYPE_FLOAT
) {
763 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
765 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
770 uni
->initialized
= true;
772 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
774 /* If the uniform is a sampler, do the extra magic necessary to propagate
775 * the changes through.
777 if (uni
->type
->is_sampler()) {
780 for (i
= 0; i
< count
; i
++) {
781 shProg
->SamplerUnits
[uni
->sampler
+ offset
+ i
] =
782 ((unsigned *) values
)[i
];
785 bool flushed
= false;
786 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
787 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
789 /* If the shader stage doesn't use any samplers, don't bother
790 * checking if any samplers have changed.
792 if (sh
== NULL
|| sh
->active_samplers
== 0)
795 struct gl_program
*const prog
= sh
->Program
;
797 assert(sizeof(prog
->SamplerUnits
) == sizeof(shProg
->SamplerUnits
));
799 /* Determine if any of the samplers used by this shader stage have
802 bool changed
= false;
803 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
804 if ((sh
->active_samplers
& (1U << j
)) != 0
805 && (prog
->SamplerUnits
[j
] != shProg
->SamplerUnits
[j
])) {
813 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
817 memcpy(prog
->SamplerUnits
,
818 shProg
->SamplerUnits
,
819 sizeof(shProg
->SamplerUnits
));
821 _mesa_update_shader_textures_used(shProg
, prog
);
822 if (ctx
->Driver
.SamplerUniformChange
)
823 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
830 * Called by glUniformMatrix*() functions.
831 * Note: cols=2, rows=4 ==> array[2] of vec4
834 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
835 GLuint cols
, GLuint rows
,
836 GLint location
, GLsizei count
,
837 GLboolean transpose
, const GLfloat
*values
)
839 unsigned loc
, offset
;
843 struct gl_uniform_storage
*uni
;
845 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
846 &loc
, &offset
, "glUniformMatrix", false))
849 uni
= &shProg
->UniformStorage
[loc
];
850 if (!uni
->type
->is_matrix()) {
851 _mesa_error(ctx
, GL_INVALID_OPERATION
,
852 "glUniformMatrix(non-matrix uniform)");
856 assert(!uni
->type
->is_sampler());
857 vectors
= uni
->type
->matrix_columns
;
858 components
= uni
->type
->vector_elements
;
860 /* Verify that the types are compatible. This is greatly simplified for
861 * matrices because they can only have a float base type.
863 if (vectors
!= cols
|| components
!= rows
) {
864 _mesa_error(ctx
, GL_INVALID_OPERATION
,
865 "glUniformMatrix(matrix size mismatch)");
869 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
870 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
871 if (ctx
->API
== API_OPENGLES
872 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)) {
874 _mesa_error(ctx
, GL_INVALID_VALUE
,
875 "glUniformMatrix(matrix transpose is not GL_FALSE)");
880 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
881 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
882 bool(transpose
), shProg
, location
, uni
);
885 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
887 * "When loading N elements starting at an arbitrary position k in a
888 * uniform declared as an array, elements k through k + N - 1 in the
889 * array will be replaced with the new values. Values for any array
890 * element that exceeds the highest array element index used, as
891 * reported by GetActiveUniform, will be ignored by the GL."
893 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
894 * will have already generated an error.
896 if (uni
->array_elements
!= 0) {
897 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
900 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
902 /* Store the data in the "actual type" backing storage for the uniform.
904 elements
= components
* vectors
;
907 memcpy(&uni
->storage
[elements
* offset
], values
,
908 sizeof(uni
->storage
[0]) * elements
* count
);
910 /* Copy and transpose the matrix.
912 const float *src
= values
;
913 float *dst
= &uni
->storage
[elements
* offset
].f
;
915 for (int i
= 0; i
< count
; i
++) {
916 for (unsigned r
= 0; r
< rows
; r
++) {
917 for (unsigned c
= 0; c
< cols
; c
++) {
918 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
927 uni
->initialized
= true;
929 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
934 * Called via glGetUniformLocation().
936 * Returns the uniform index into UniformStorage (also the
937 * glGetActiveUniformsiv uniform index), and stores the referenced
938 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
939 * return values can be encoded into a uniform location for
940 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
943 _mesa_get_uniform_location(struct gl_context
*ctx
,
944 struct gl_shader_program
*shProg
,
946 unsigned *out_offset
)
948 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
950 * "The first element of a uniform array is identified using the
951 * name of the uniform array appended with "[0]". Except if the last
952 * part of the string name indicates a uniform array, then the
953 * location of the first element of that array can be retrieved by
954 * either using the name of the uniform array, or the name of the
955 * uniform array appended with "[0]"."
957 * Note: since uniform names are not allowed to use whitespace, and array
958 * indices within uniform names are not allowed to use "+", "-", or leading
959 * zeros, it follows that each uniform has a unique name up to the possible
960 * ambiguity with "[0]" noted above. Therefore we don't need to worry
961 * about mal-formed inputs--they will properly fail when we try to look up
962 * the uniform name in shProg->UniformHash.
965 const GLchar
*base_name_end
;
966 long offset
= parse_program_resource_name(name
, &base_name_end
);
967 bool array_lookup
= offset
>= 0;
971 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
972 memcpy(name_copy
, name
, base_name_end
- name
);
973 name_copy
[base_name_end
- name
] = '\0';
975 name_copy
= (char *) name
;
979 unsigned location
= 0;
980 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
983 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
985 /* Free the temporary buffer *before* possibly returning an error.
987 if (name_copy
!= name
)
991 return GL_INVALID_INDEX
;
993 /* If the uniform is an array, fail if the index is out of bounds.
994 * (A negative index is caught above.) This also fails if the uniform
995 * is not an array, but the user is trying to index it, because
996 * array_elements is zero and offset >= 0.
999 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
1000 return GL_INVALID_INDEX
;
1003 *out_offset
= offset
;
1008 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1009 char *errMsg
, size_t errMsgLength
)
1011 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1013 memset(unit_types
, 0, sizeof(unit_types
));
1015 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1016 const struct gl_uniform_storage
*const storage
=
1017 &shProg
->UniformStorage
[i
];
1018 const glsl_type
*const t
= (storage
->type
->is_array())
1019 ? storage
->type
->fields
.array
: storage
->type
;
1021 if (!t
->is_sampler())
1024 const unsigned count
= MAX2(1, storage
->type
->array_size());
1025 for (unsigned j
= 0; j
< count
; j
++) {
1026 const unsigned unit
= storage
->storage
[j
].i
;
1028 /* The types of the samplers associated with a particular texture
1029 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1030 * OpenGL 3.3 core spec says:
1032 * "It is not allowed to have variables of different sampler
1033 * types pointing to the same texture image unit within a program
1036 if (unit_types
[unit
] == NULL
) {
1037 unit_types
[unit
] = t
;
1038 } else if (unit_types
[unit
] != t
) {
1039 _mesa_snprintf(errMsg
, errMsgLength
,
1040 "Texture unit %d is accessed both as %s and %s",
1041 unit
, unit_types
[unit
]->name
, t
->name
);