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 "main/shaderapi.h"
37 #include "main/shaderobj.h"
41 extern "C" void GLAPIENTRY
42 _mesa_GetActiveUniform(GLhandleARB program, GLuint index,
43 GLsizei maxLength, GLsizei *length, GLint *size,
44 GLenum *type, GLcharARB *nameOut)
46 GET_CURRENT_CONTEXT(ctx);
47 struct gl_shader_program *shProg =
48 _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
53 if (index >= shProg->NumUserUniformStorage) {
54 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
58 const struct gl_uniform_storage *const uni = &shProg->UniformStorage[index];
61 _mesa_get_uniform_name(uni, maxLength, length, nameOut);
65 /* array_elements is zero for non-arrays, but the API requires that 1 be
68 *size = MAX2(1, uni->array_elements);
72 *type = uni->type->gl_type;
76 extern "C" void GLAPIENTRY
77 _mesa_GetActiveUniformsiv(GLuint program,
79 const GLuint *uniformIndices,
83 GET_CURRENT_CONTEXT(ctx);
84 struct gl_shader_program *shProg;
87 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
91 if (uniformCount < 0) {
92 _mesa_error(ctx, GL_INVALID_VALUE,
93 "glGetUniformIndices(uniformCount < 0)");
97 for (i = 0; i < uniformCount; i++) {
98 GLuint index = uniformIndices[i];
100 if (index >= shProg->NumUserUniformStorage) {
101 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniformsiv(index)");
106 for (i = 0; i < uniformCount; i++) {
107 GLuint index = uniformIndices[i];
108 const struct gl_uniform_storage *uni = &shProg->UniformStorage[index];
111 case GL_UNIFORM_TYPE:
112 params[i] = uni->type->gl_type;
115 case GL_UNIFORM_SIZE:
116 /* array_elements is zero for non-arrays, but the API requires that 1 be
119 params[i] = MAX2(1, uni->array_elements);
122 case GL_UNIFORM_NAME_LENGTH:
123 params[i] = strlen(uni->name) + 1;
125 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
128 * "If the active uniform is an array, the uniform name returned
129 * in name will always be the name of the uniform array appended
132 if (uni->array_elements != 0)
136 case GL_UNIFORM_BLOCK_INDEX:
137 params[i] = uni->block_index;
140 case GL_UNIFORM_OFFSET:
141 params[i] = uni->offset;
144 case GL_UNIFORM_ARRAY_STRIDE:
145 params[i] = uni->array_stride;
148 case GL_UNIFORM_MATRIX_STRIDE:
149 params[i] = uni->matrix_stride;
152 case GL_UNIFORM_IS_ROW_MAJOR:
153 params[i] = uni->row_major;
157 _mesa_error(ctx, GL_INVALID_ENUM, "glGetActiveUniformsiv(pname)");
164 validate_uniform_parameters(struct gl_context *ctx,
165 struct gl_shader_program *shProg,
166 GLint location, GLsizei count,
168 unsigned *array_index,
170 bool negative_one_is_not_valid)
172 if (!shProg || !shProg->LinkStatus) {
173 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(program not linked)", caller);
177 if (location == -1) {
178 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
181 * "The error INVALID_OPERATION is generated if program has not been
182 * linked successfully, or if location is not a valid location for
185 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
188 * "If the value of location is -1, the Uniform* commands will
189 * silently ignore the data passed in, and the current uniform
190 * values will not be changed."
192 * Allowing -1 for the location parameter of glUniform allows
193 * applications to avoid error paths in the case that, for example, some
194 * uniform variable is removed by the compiler / linker after
195 * optimization. In this case, the new value of the uniform is dropped
196 * on the floor. For the case of glGetUniform, there is nothing
197 * sensible to do for a location of -1.
199 * The negative_one_is_not_valid flag selects between the two behaviors.
201 if (negative_one_is_not_valid) {
202 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
209 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
211 * "If a negative number is provided where an argument of type sizei or
212 * sizeiptr is specified, the error INVALID_VALUE is generated."
215 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count < 0)", caller);
219 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
221 * "If any of the following conditions occur, an INVALID_OPERATION
222 * error is generated by the Uniform* commands, and no uniform values
227 * - if no variable with a location of location exists in the
228 * program object currently in use and location is not -1,
229 * - if count is greater than one, and the uniform declared in the
230 * shader is not an array variable,
233 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
238 _mesa_uniform_split_location_offset(shProg, location, loc, array_index);
240 if (*loc >= shProg->NumUserUniformStorage) {
241 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
246 if (shProg->UniformStorage[*loc].array_elements == 0 && count > 1) {
247 _mesa_error(ctx, GL_INVALID_OPERATION,
248 "%s(count > 1 for non-array, location=%d)",
253 /* If the uniform is an array, check that array_index is in bounds.
254 * If not an array, check that array_index is zero.
255 * array_index is unsigned so no need to check for less than zero.
257 unsigned limit = shProg->UniformStorage[*loc].array_elements;
260 if (*array_index >= limit) {
261 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
269 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
272 _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
273 GLsizei bufSize, enum glsl_base_type returnType,
276 struct gl_shader_program *shProg =
277 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformfv");
278 struct gl_uniform_storage *uni;
279 unsigned loc, offset;
281 if (!validate_uniform_parameters(ctx, shProg, location, 1,
282 &loc, &offset, "glGetUniform", true))
285 uni = &shProg->UniformStorage[loc];
288 unsigned elements = (uni->type->is_sampler())
289 ? 1 : uni->type->components();
291 /* Calculate the source base address *BEFORE* modifying elements to
292 * account for the size of the user's buffer.
294 const union gl_constant_value *const src =
295 &uni->storage[offset * elements];
297 assert(returnType == GLSL_TYPE_FLOAT || returnType == GLSL_TYPE_INT ||
298 returnType == GLSL_TYPE_UINT);
299 /* The three (currently) supported types all have the same size,
300 * which is of course the same as their union. That'll change
301 * with glGetUniformdv()...
303 unsigned bytes = sizeof(src[0]) * elements;
304 if (bufSize < 0 || bytes > (unsigned) bufSize) {
305 _mesa_error( ctx, GL_INVALID_OPERATION,
306 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
307 " but %u bytes are required)", bufSize, bytes );
311 /* If the return type and the uniform's native type are "compatible,"
312 * just memcpy the data. If the types are not compatible, perform a
313 * slower convert-and-copy process.
315 if (returnType == uni->type->base_type
316 || ((returnType == GLSL_TYPE_INT
317 || returnType == GLSL_TYPE_UINT
318 || returnType == GLSL_TYPE_SAMPLER)
320 (uni->type->base_type == GLSL_TYPE_INT
321 || uni->type->base_type == GLSL_TYPE_UINT
322 || uni->type->base_type == GLSL_TYPE_SAMPLER))) {
323 memcpy(paramsOut, src, bytes);
325 union gl_constant_value *const dst =
326 (union gl_constant_value *) paramsOut;
328 /* This code could be optimized by putting the loop inside the switch
329 * statements. However, this is not expected to be
330 * performance-critical code.
332 for (unsigned i = 0; i < elements; i++) {
333 switch (returnType) {
334 case GLSL_TYPE_FLOAT:
335 switch (uni->type->base_type) {
337 dst[i].f = (float) src[i].u;
340 case GLSL_TYPE_SAMPLER:
341 dst[i].f = (float) src[i].i;
344 dst[i].f = src[i].i ? 1.0f : 0.0f;
347 assert(!"Should not get here.");
354 switch (uni->type->base_type) {
355 case GLSL_TYPE_FLOAT:
356 /* While the GL 3.2 core spec doesn't explicitly
357 * state how conversion of float uniforms to integer
358 * values works, in section 6.2 "State Tables" on
361 * "Unless otherwise specified, when floating
362 * point state is returned as integer values or
363 * integer state is returned as floating-point
364 * values it is converted in the fashion
365 * described in section 6.1.2"
367 * That section, on page 248, says:
369 * "If GetIntegerv or GetInteger64v are called,
370 * a floating-point value is rounded to the
371 * nearest integer..."
373 dst[i].i = IROUND(src[i].f);
376 dst[i].i = src[i].i ? 1 : 0;
379 assert(!"Should not get here.");
385 assert(!"Should not get here.");
394 log_uniform(const void *values, enum glsl_base_type basicType,
395 unsigned rows, unsigned cols, unsigned count,
397 const struct gl_shader_program *shProg,
399 const struct gl_uniform_storage *uni)
402 const union gl_constant_value *v = (const union gl_constant_value *) values;
403 const unsigned elems = rows * cols * count;
404 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
406 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
407 "transpose = %s) to: ",
408 shProg->Name, extra, uni->name, location, uni->type->name,
409 transpose ? "true" : "false");
410 for (unsigned i = 0; i < elems; i++) {
411 if (i != 0 && ((i % rows) == 0))
416 printf("%u ", v[i].u);
419 printf("%d ", v[i].i);
421 case GLSL_TYPE_FLOAT:
422 printf("%g ", v[i].f);
425 assert(!"Should not get here.");
435 log_program_parameters(const struct gl_shader_program *shProg)
437 static const char *stages[] = {
438 "vertex", "fragment", "geometry"
441 assert(Elements(stages) == MESA_SHADER_TYPES);
443 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
444 if (shProg->_LinkedShaders[i] == NULL)
447 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
449 printf("Program %d %s shader parameters:\n",
450 shProg->Name, stages[i]);
451 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
452 printf("%s: %p %f %f %f %f\n",
453 prog->Parameters->Parameters[j].Name,
454 prog->Parameters->ParameterValues[j],
455 prog->Parameters->ParameterValues[j][0].f,
456 prog->Parameters->ParameterValues[j][1].f,
457 prog->Parameters->ParameterValues[j][2].f,
458 prog->Parameters->ParameterValues[j][3].f);
466 * Propagate some values from uniform backing storage to driver storage
468 * Values propagated from uniform backing storage to driver storage
469 * have all format / type conversions previously requested by the
470 * driver applied. This function is most often called by the
471 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
474 * \param uni Uniform whose data is to be propagated to driver storage
475 * \param array_index If \c uni is an array, this is the element of
476 * the array to be propagated.
477 * \param count Number of array elements to propagate.
480 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
481 unsigned array_index,
486 /* vector_elements and matrix_columns can be 0 for samplers.
488 const unsigned components = MAX2(1, uni->type->vector_elements);
489 const unsigned vectors = MAX2(1, uni->type->matrix_columns);
491 /* Store the data in the driver's requested type in the driver's storage
494 unsigned src_vector_byte_stride = components * 4;
496 for (i = 0; i < uni->num_driver_storage; i++) {
497 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
498 uint8_t *dst = (uint8_t *) store->data;
499 const unsigned extra_stride =
500 store->element_stride - (vectors * store->vector_stride);
502 (uint8_t *) (&uni->storage[array_index * (components * vectors)].i);
505 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
507 __func__, dst, array_index, components,
508 vectors, count, store->vector_stride, extra_stride);
511 dst += array_index * store->element_stride;
513 switch (store->format) {
515 case uniform_bool_int_0_1: {
519 for (j = 0; j < count; j++) {
520 for (v = 0; v < vectors; v++) {
521 memcpy(dst, src, src_vector_byte_stride);
522 src += src_vector_byte_stride;
523 dst += store->vector_stride;
531 case uniform_int_float:
532 case uniform_bool_float: {
533 const int *isrc = (const int *) src;
538 for (j = 0; j < count; j++) {
539 for (v = 0; v < vectors; v++) {
540 for (c = 0; c < components; c++) {
541 ((float *) dst)[c] = (float) *isrc;
545 dst += store->vector_stride;
553 case uniform_bool_int_0_not0: {
554 const int *isrc = (const int *) src;
559 for (j = 0; j < count; j++) {
560 for (v = 0; v < vectors; v++) {
561 for (c = 0; c < components; c++) {
562 ((int *) dst)[c] = *isrc == 0 ? 0 : ~0;
566 dst += store->vector_stride;
575 assert(!"Should not get here.");
582 * Called via glUniform*() functions.
585 _mesa_uniform(struct gl_context *ctx, struct gl_shader_program *shProg,
586 GLint location, GLsizei count,
587 const GLvoid *values, GLenum type)
589 unsigned loc, offset;
591 unsigned src_components;
592 enum glsl_base_type basicType;
593 struct gl_uniform_storage *uni;
595 if (!validate_uniform_parameters(ctx, shProg, location, count,
596 &loc, &offset, "glUniform", false))
599 uni = &shProg->UniformStorage[loc];
601 /* Verify that the types are compatible.
605 basicType = GLSL_TYPE_FLOAT;
609 basicType = GLSL_TYPE_FLOAT;
613 basicType = GLSL_TYPE_FLOAT;
617 basicType = GLSL_TYPE_FLOAT;
620 case GL_UNSIGNED_INT:
621 basicType = GLSL_TYPE_UINT;
624 case GL_UNSIGNED_INT_VEC2:
625 basicType = GLSL_TYPE_UINT;
628 case GL_UNSIGNED_INT_VEC3:
629 basicType = GLSL_TYPE_UINT;
632 case GL_UNSIGNED_INT_VEC4:
633 basicType = GLSL_TYPE_UINT;
637 basicType = GLSL_TYPE_INT;
641 basicType = GLSL_TYPE_INT;
645 basicType = GLSL_TYPE_INT;
649 basicType = GLSL_TYPE_INT;
657 case GL_FLOAT_MAT2x3:
658 case GL_FLOAT_MAT2x4:
659 case GL_FLOAT_MAT3x2:
661 case GL_FLOAT_MAT3x4:
662 case GL_FLOAT_MAT4x2:
663 case GL_FLOAT_MAT4x3:
666 _mesa_problem(NULL, "Invalid type in %s", __func__);
670 if (uni->type->is_sampler()) {
673 components = uni->type->vector_elements;
677 switch (uni->type->base_type) {
681 case GLSL_TYPE_SAMPLER:
682 match = (basicType == GLSL_TYPE_INT);
685 match = (basicType == uni->type->base_type);
689 if (uni->type->is_matrix() || components != src_components || !match) {
690 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
694 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
695 log_uniform(values, basicType, components, 1, count,
696 false, shProg, location, uni);
699 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
701 * "Setting a sampler's value to i selects texture image unit number
702 * i. The values of i range from zero to the implementation- dependent
703 * maximum supported number of texture image units."
705 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
708 * "Error Description Offending command
711 * INVALID_VALUE Numeric argument out of range Yes"
713 * Based on that, when an invalid sampler is specified, we generate a
714 * GL_INVALID_VALUE error and ignore the command.
716 if (uni->type->is_sampler()) {
719 for (i = 0; i < count; i++) {
720 const unsigned texUnit = ((unsigned *) values)[i];
722 /* check that the sampler (tex unit index) is legal */
723 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
724 _mesa_error(ctx, GL_INVALID_VALUE,
725 "glUniform1i(invalid sampler/tex unit index for "
733 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
735 * "When loading N elements starting at an arbitrary position k in a
736 * uniform declared as an array, elements k through k + N - 1 in the
737 * array will be replaced with the new values. Values for any array
738 * element that exceeds the highest array element index used, as
739 * reported by GetActiveUniform, will be ignored by the GL."
741 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
742 * will have already generated an error.
744 if (uni->array_elements != 0) {
745 count = MIN2(count, (int) (uni->array_elements - offset));
748 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
750 /* Store the data in the "actual type" backing storage for the uniform.
752 if (!uni->type->is_boolean()) {
753 memcpy(&uni->storage[components * offset], values,
754 sizeof(uni->storage[0]) * components * count);
756 const union gl_constant_value *src =
757 (const union gl_constant_value *) values;
758 union gl_constant_value *dst = &uni->storage[components * offset];
759 const unsigned elems = components * count;
762 for (i = 0; i < elems; i++) {
763 if (basicType == GLSL_TYPE_FLOAT) {
764 dst[i].i = src[i].f != 0.0f ? 1 : 0;
766 dst[i].i = src[i].i != 0 ? 1 : 0;
771 uni->initialized = true;
773 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
775 /* If the uniform is a sampler, do the extra magic necessary to propagate
776 * the changes through.
778 if (uni->type->is_sampler()) {
781 bool flushed = false;
782 for (i = 0; i < MESA_SHADER_TYPES; i++) {
783 struct gl_shader *const sh = shProg->_LinkedShaders[i];
786 /* If the shader stage doesn't use the sampler uniform, skip this.
788 if (sh == NULL || !uni->sampler[i].active)
791 for (j = 0; j < count; j++) {
792 sh->SamplerUnits[uni->sampler[i].index + offset + j] =
793 ((unsigned *) values)[j];
796 struct gl_program *const prog = sh->Program;
798 assert(sizeof(prog->SamplerUnits) == sizeof(sh->SamplerUnits));
800 /* Determine if any of the samplers used by this shader stage have
803 bool changed = false;
804 for (unsigned j = 0; j < Elements(prog->SamplerUnits); j++) {
805 if ((sh->active_samplers & (1U << j)) != 0
806 && (prog->SamplerUnits[j] != sh->SamplerUnits[j])) {
814 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
818 memcpy(prog->SamplerUnits,
820 sizeof(sh->SamplerUnits));
822 _mesa_update_shader_textures_used(shProg, prog);
823 if (ctx->Driver.SamplerUniformChange)
824 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
831 * Called by glUniformMatrix*() functions.
832 * Note: cols=2, rows=4 ==> array[2] of vec4
835 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
836 GLuint cols, GLuint rows,
837 GLint location, GLsizei count,
838 GLboolean transpose, const GLfloat *values)
840 unsigned loc, offset;
844 struct gl_uniform_storage *uni;
846 if (!validate_uniform_parameters(ctx, shProg, location, count,
847 &loc, &offset, "glUniformMatrix", false))
850 uni = &shProg->UniformStorage[loc];
851 if (!uni->type->is_matrix()) {
852 _mesa_error(ctx, GL_INVALID_OPERATION,
853 "glUniformMatrix(non-matrix uniform)");
857 assert(!uni->type->is_sampler());
858 vectors = uni->type->matrix_columns;
859 components = uni->type->vector_elements;
861 /* Verify that the types are compatible. This is greatly simplified for
862 * matrices because they can only have a float base type.
864 if (vectors != cols || components != rows) {
865 _mesa_error(ctx, GL_INVALID_OPERATION,
866 "glUniformMatrix(matrix size mismatch)");
870 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
871 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
872 if (ctx->API == API_OPENGLES
873 || (ctx->API == API_OPENGLES2 && ctx->Version < 30)) {
875 _mesa_error(ctx, GL_INVALID_VALUE,
876 "glUniformMatrix(matrix transpose is not GL_FALSE)");
881 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
882 log_uniform(values, GLSL_TYPE_FLOAT, components, vectors, count,
883 bool(transpose), shProg, location, uni);
886 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
888 * "When loading N elements starting at an arbitrary position k in a
889 * uniform declared as an array, elements k through k + N - 1 in the
890 * array will be replaced with the new values. Values for any array
891 * element that exceeds the highest array element index used, as
892 * reported by GetActiveUniform, will be ignored by the GL."
894 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
895 * will have already generated an error.
897 if (uni->array_elements != 0) {
898 count = MIN2(count, (int) (uni->array_elements - offset));
901 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
903 /* Store the data in the "actual type" backing storage for the uniform.
905 elements = components * vectors;
908 memcpy(&uni->storage[elements * offset], values,
909 sizeof(uni->storage[0]) * elements * count);
911 /* Copy and transpose the matrix.
913 const float *src = values;
914 float *dst = &uni->storage[elements * offset].f;
916 for (int i = 0; i < count; i++) {
917 for (unsigned r = 0; r < rows; r++) {
918 for (unsigned c = 0; c < cols; c++) {
919 dst[(c * components) + r] = src[c + (r * vectors)];
928 uni->initialized = true;
930 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
935 * Called via glGetUniformLocation().
937 * Returns the uniform index into UniformStorage (also the
938 * glGetActiveUniformsiv uniform index), and stores the referenced
939 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
940 * return values can be encoded into a uniform location for
941 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
944 _mesa_get_uniform_location(struct gl_context *ctx,
945 struct gl_shader_program *shProg,
947 unsigned *out_offset)
949 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
951 * "The first element of a uniform array is identified using the
952 * name of the uniform array appended with "[0]". Except if the last
953 * part of the string name indicates a uniform array, then the
954 * location of the first element of that array can be retrieved by
955 * either using the name of the uniform array, or the name of the
956 * uniform array appended with "[0]"."
958 * Note: since uniform names are not allowed to use whitespace, and array
959 * indices within uniform names are not allowed to use "+", "-", or leading
960 * zeros, it follows that each uniform has a unique name up to the possible
961 * ambiguity with "[0]" noted above. Therefore we don't need to worry
962 * about mal-formed inputs--they will properly fail when we try to look up
963 * the uniform name in shProg->UniformHash.
966 const GLchar *base_name_end;
967 long offset = parse_program_resource_name(name, &base_name_end);
968 bool array_lookup = offset >= 0;
972 name_copy = (char *) malloc(base_name_end - name + 1);
973 memcpy(name_copy, name, base_name_end - name);
974 name_copy[base_name_end - name] = '\0';
976 name_copy = (char *) name;
980 unsigned location = 0;
981 const bool found = shProg->UniformHash->get(location, name_copy);
984 || strcmp(name_copy, shProg->UniformStorage[location].name) == 0);
986 /* Free the temporary buffer *before* possibly returning an error.
988 if (name_copy != name)
992 return GL_INVALID_INDEX;
994 /* If the uniform is an array, fail if the index is out of bounds.
995 * (A negative index is caught above.) This also fails if the uniform
996 * is not an array, but the user is trying to index it, because
997 * array_elements is zero and offset >= 0.
1000 && offset >= (long) shProg->UniformStorage[location].array_elements) {
1001 return GL_INVALID_INDEX;
1004 *out_offset = offset;
1009 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1010 char *errMsg, size_t errMsgLength)
1012 const glsl_type *unit_types[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1014 memset(unit_types, 0, sizeof(unit_types));
1016 for (unsigned i = 0; i < shProg->NumUserUniformStorage; i++) {
1017 const struct gl_uniform_storage *const storage =
1018 &shProg->UniformStorage[i];
1019 const glsl_type *const t = (storage->type->is_array())
1020 ? storage->type->fields.array : storage->type;
1022 if (!t->is_sampler())
1025 const unsigned count = MAX2(1, storage->type->array_size());
1026 for (unsigned j = 0; j < count; j++) {
1027 const unsigned unit = storage->storage[j].i;
1029 /* The types of the samplers associated with a particular texture
1030 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1031 * OpenGL 3.3 core spec says:
1033 * "It is not allowed to have variables of different sampler
1034 * types pointing to the same texture image unit within a program
1037 if (unit_types[unit] == NULL) {
1038 unit_types[unit] = t;
1039 } else if (unit_types[unit] != t) {
1040 _mesa_snprintf(errMsg, errMsgLength,
1041 "Texture unit %d is accessed both as %s and %s",
1042 unit, unit_types[unit]->name, t->name);