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");
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_copy_string(nameOut
, maxLength
, length
, uni
->name
);
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
;
76 validate_uniform_parameters(struct gl_context
*ctx
,
77 struct gl_shader_program
*shProg
,
78 GLint location
, GLsizei count
,
80 unsigned *array_index
,
82 bool negative_one_is_not_valid
)
84 if (!shProg
|| !shProg
->LinkStatus
) {
85 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
90 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
93 * "The error INVALID_OPERATION is generated if program has not been
94 * linked successfully, or if location is not a valid location for
97 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
100 * "If the value of location is -1, the Uniform* commands will
101 * silently ignore the data passed in, and the current uniform
102 * values will not be changed."
104 * Allowing -1 for the location parameter of glUniform allows
105 * applications to avoid error paths in the case that, for example, some
106 * uniform variable is removed by the compiler / linker after
107 * optimization. In this case, the new value of the uniform is dropped
108 * on the floor. For the case of glGetUniform, there is nothing
109 * sensible to do for a location of -1.
111 * The negative_one_is_not_valid flag selects between the two behaviors.
113 if (negative_one_is_not_valid
) {
114 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
121 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
123 * "If a negative number is provided where an argument of type sizei or
124 * sizeiptr is specified, the error INVALID_VALUE is generated."
127 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
131 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
133 * "If any of the following conditions occur, an INVALID_OPERATION
134 * error is generated by the Uniform* commands, and no uniform values
139 * - if no variable with a location of location exists in the
140 * program object currently in use and location is not -1,
141 * - if count is greater than one, and the uniform declared in the
142 * shader is not an array variable,
145 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
150 _mesa_uniform_split_location_offset(location
, loc
, array_index
);
152 if (*loc
>= shProg
->NumUserUniformStorage
) {
153 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
158 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
159 _mesa_error(ctx
, GL_INVALID_OPERATION
,
160 "%s(count > 1 for non-array, location=%d)",
165 /* This case should be impossible. The implication is that a call like
166 * glGetUniformLocation(prog, "foo[8]") was successful but "foo" is not an
169 if (*array_index
!= 0 && shProg
->UniformStorage
[*loc
].array_elements
== 0) {
170 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
178 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
181 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
182 GLsizei bufSize
, enum glsl_base_type returnType
,
185 struct gl_shader_program
*shProg
=
186 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
187 struct gl_uniform_storage
*uni
;
188 unsigned loc
, offset
;
190 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
191 &loc
, &offset
, "glGetUniform", true))
194 uni
= &shProg
->UniformStorage
[loc
];
197 unsigned elements
= (uni
->type
->is_sampler())
198 ? 1 : uni
->type
->components();
200 /* Calculate the source base address *BEFORE* modifying elements to
201 * account for the size of the user's buffer.
203 const union gl_constant_value
*const src
=
204 &uni
->storage
[offset
* elements
];
206 unsigned bytes
= sizeof(uni
->storage
[0]) * elements
;
207 if (bytes
> (unsigned) bufSize
) {
208 elements
= bufSize
/ sizeof(uni
->storage
[0]);
212 /* If the return type and the uniform's native type are "compatible,"
213 * just memcpy the data. If the types are not compatible, perform a
214 * slower convert-and-copy process.
216 if (returnType
== uni
->type
->base_type
217 || ((returnType
== GLSL_TYPE_INT
218 || returnType
== GLSL_TYPE_UINT
219 || returnType
== GLSL_TYPE_SAMPLER
)
221 (uni
->type
->base_type
== GLSL_TYPE_INT
222 || uni
->type
->base_type
== GLSL_TYPE_UINT
223 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
224 memcpy(paramsOut
, src
, bytes
);
226 union gl_constant_value
*const dst
=
227 (union gl_constant_value
*) paramsOut
;
229 /* This code could be optimized by putting the loop inside the switch
230 * statements. However, this is not expected to be
231 * performance-critical code.
233 for (unsigned i
= 0; i
< elements
; i
++) {
234 switch (returnType
) {
235 case GLSL_TYPE_FLOAT
:
236 switch (uni
->type
->base_type
) {
238 dst
[i
].f
= (float) src
[i
].u
;
241 case GLSL_TYPE_SAMPLER
:
242 dst
[i
].f
= (float) src
[i
].i
;
245 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
248 assert(!"Should not get here.");
255 switch (uni
->type
->base_type
) {
256 case GLSL_TYPE_FLOAT
:
257 /* While the GL 3.2 core spec doesn't explicitly
258 * state how conversion of float uniforms to integer
259 * values works, in section 6.2 "State Tables" on
262 * "Unless otherwise specified, when floating
263 * point state is returned as integer values or
264 * integer state is returned as floating-point
265 * values it is converted in the fashion
266 * described in section 6.1.2"
268 * That section, on page 248, says:
270 * "If GetIntegerv or GetInteger64v are called,
271 * a floating-point value is rounded to the
272 * nearest integer..."
274 dst
[i
].i
= IROUND(src
[i
].f
);
277 dst
[i
].i
= src
[i
].i
? 1 : 0;
280 assert(!"Should not get here.");
286 assert(!"Should not get here.");
295 log_uniform(const void *values
, enum glsl_base_type basicType
,
296 unsigned rows
, unsigned cols
, unsigned count
,
298 const struct gl_shader_program
*shProg
,
300 const struct gl_uniform_storage
*uni
)
303 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
304 const unsigned elems
= rows
* cols
* count
;
305 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
307 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
308 "transpose = %s) to: ",
309 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
310 transpose
? "true" : "false");
311 for (unsigned i
= 0; i
< elems
; i
++) {
312 if (i
!= 0 && ((i
% rows
) == 0))
317 printf("%u ", v
[i
].u
);
320 printf("%d ", v
[i
].i
);
322 case GLSL_TYPE_FLOAT
:
323 printf("%g ", v
[i
].f
);
326 assert(!"Should not get here.");
336 log_program_parameters(const struct gl_shader_program
*shProg
)
338 static const char *stages
[] = {
339 "vertex", "fragment", "geometry"
342 assert(Elements(stages
) == MESA_SHADER_TYPES
);
344 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
345 if (shProg
->_LinkedShaders
[i
] == NULL
)
348 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
350 printf("Program %d %s shader parameters:\n",
351 shProg
->Name
, stages
[i
]);
352 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
353 printf("%s: %p %f %f %f %f\n",
354 prog
->Parameters
->Parameters
[j
].Name
,
355 prog
->Parameters
->ParameterValues
[j
],
356 prog
->Parameters
->ParameterValues
[j
][0].f
,
357 prog
->Parameters
->ParameterValues
[j
][1].f
,
358 prog
->Parameters
->ParameterValues
[j
][2].f
,
359 prog
->Parameters
->ParameterValues
[j
][3].f
);
367 * Propagate some values from uniform backing storage to driver storage
369 * Values propagated from uniform backing storage to driver storage
370 * have all format / type conversions previously requested by the
371 * driver applied. This function is most often called by the
372 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
375 * \param uni Uniform whose data is to be propagated to driver storage
376 * \param array_index If \c uni is an array, this is the element of
377 * the array to be propagated.
378 * \param count Number of array elements to propagate.
381 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
382 unsigned array_index
,
387 /* vector_elements and matrix_columns can be 0 for samplers.
389 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
390 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
392 /* Store the data in the driver's requested type in the driver's storage
395 unsigned src_vector_byte_stride
= components
* 4;
397 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
398 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
399 uint8_t *dst
= (uint8_t *) store
->data
;
400 const unsigned extra_stride
=
401 store
->element_stride
- (vectors
* store
->vector_stride
);
403 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
406 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
408 __func__
, dst
, array_index
, components
,
409 vectors
, count
, store
->vector_stride
, extra_stride
);
412 dst
+= array_index
* store
->element_stride
;
414 switch (store
->format
) {
416 case uniform_bool_int_0_1
: {
420 for (j
= 0; j
< count
; j
++) {
421 for (v
= 0; v
< vectors
; v
++) {
422 memcpy(dst
, src
, src_vector_byte_stride
);
423 src
+= src_vector_byte_stride
;
424 dst
+= store
->vector_stride
;
432 case uniform_int_float
:
433 case uniform_bool_float
: {
434 const int *isrc
= (const int *) src
;
439 for (j
= 0; j
< count
; j
++) {
440 for (v
= 0; v
< vectors
; v
++) {
441 for (c
= 0; c
< components
; c
++) {
442 ((float *) dst
)[c
] = (float) *isrc
;
446 dst
+= store
->vector_stride
;
454 case uniform_bool_int_0_not0
: {
455 const int *isrc
= (const int *) src
;
460 for (j
= 0; j
< count
; j
++) {
461 for (v
= 0; v
< vectors
; v
++) {
462 for (c
= 0; c
< components
; c
++) {
463 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
467 dst
+= store
->vector_stride
;
476 assert(!"Should not get here.");
483 * Called via glUniform*() functions.
486 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
487 GLint location
, GLsizei count
,
488 const GLvoid
*values
, GLenum type
)
490 unsigned loc
, offset
;
492 unsigned src_components
;
493 enum glsl_base_type basicType
;
494 struct gl_uniform_storage
*uni
;
496 ASSERT_OUTSIDE_BEGIN_END(ctx
);
498 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
499 &loc
, &offset
, "glUniform", false))
502 uni
= &shProg
->UniformStorage
[loc
];
504 /* Verify that the types are compatible.
508 basicType
= GLSL_TYPE_FLOAT
;
512 basicType
= GLSL_TYPE_FLOAT
;
516 basicType
= GLSL_TYPE_FLOAT
;
520 basicType
= GLSL_TYPE_FLOAT
;
523 case GL_UNSIGNED_INT
:
524 basicType
= GLSL_TYPE_UINT
;
527 case GL_UNSIGNED_INT_VEC2
:
528 basicType
= GLSL_TYPE_UINT
;
531 case GL_UNSIGNED_INT_VEC3
:
532 basicType
= GLSL_TYPE_UINT
;
535 case GL_UNSIGNED_INT_VEC4
:
536 basicType
= GLSL_TYPE_UINT
;
540 basicType
= GLSL_TYPE_INT
;
544 basicType
= GLSL_TYPE_INT
;
548 basicType
= GLSL_TYPE_INT
;
552 basicType
= GLSL_TYPE_INT
;
560 case GL_FLOAT_MAT2x3
:
561 case GL_FLOAT_MAT2x4
:
562 case GL_FLOAT_MAT3x2
:
564 case GL_FLOAT_MAT3x4
:
565 case GL_FLOAT_MAT4x2
:
566 case GL_FLOAT_MAT4x3
:
569 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
573 if (uni
->type
->is_sampler()) {
576 components
= uni
->type
->vector_elements
;
580 switch (uni
->type
->base_type
) {
584 case GLSL_TYPE_SAMPLER
:
585 match
= (basicType
== GLSL_TYPE_INT
);
588 match
= (basicType
== uni
->type
->base_type
);
592 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
593 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
597 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
598 log_uniform(values
, basicType
, components
, 1, count
,
599 false, shProg
, location
, uni
);
602 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
604 * "Setting a sampler's value to i selects texture image unit number
605 * i. The values of i range from zero to the implementation- dependent
606 * maximum supported number of texture image units."
608 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
611 * "Error Description Offending command
614 * INVALID_VALUE Numeric argument out of range Yes"
616 * Based on that, when an invalid sampler is specified, we generate a
617 * GL_INVALID_VALUE error and ignore the command.
619 if (uni
->type
->is_sampler()) {
622 for (i
= 0; i
< count
; i
++) {
623 const unsigned texUnit
= ((unsigned *) values
)[i
];
625 /* check that the sampler (tex unit index) is legal */
626 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
627 _mesa_error(ctx
, GL_INVALID_VALUE
,
628 "glUniform1i(invalid sampler/tex unit index for "
636 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
638 * "When loading N elements starting at an arbitrary position k in a
639 * uniform declared as an array, elements k through k + N - 1 in the
640 * array will be replaced with the new values. Values for any array
641 * element that exceeds the highest array element index used, as
642 * reported by GetActiveUniform, will be ignored by the GL."
644 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
645 * will have already generated an error.
647 if (uni
->array_elements
!= 0) {
648 if (offset
>= uni
->array_elements
)
651 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
654 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
656 /* Store the data in the "actual type" backing storage for the uniform.
658 if (!uni
->type
->is_boolean()) {
659 memcpy(&uni
->storage
[components
* offset
], values
,
660 sizeof(uni
->storage
[0]) * components
* count
);
662 const union gl_constant_value
*src
=
663 (const union gl_constant_value
*) values
;
664 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
665 const unsigned elems
= components
* count
;
668 for (i
= 0; i
< elems
; i
++) {
669 if (basicType
== GLSL_TYPE_FLOAT
) {
670 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
672 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
677 uni
->initialized
= true;
679 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
681 /* If the uniform is a sampler, do the extra magic necessary to propagate
682 * the changes through.
684 if (uni
->type
->is_sampler()) {
687 for (i
= 0; i
< count
; i
++) {
688 shProg
->SamplerUnits
[uni
->sampler
+ offset
+ i
] =
689 ((unsigned *) values
)[i
];
692 bool flushed
= false;
693 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
694 struct gl_program
*prog
;
696 if (shProg
->_LinkedShaders
[i
] == NULL
)
699 prog
= shProg
->_LinkedShaders
[i
]->Program
;
701 /* If the shader stage doesn't use any samplers, don't bother
702 * checking if any samplers have changed.
704 if (prog
->SamplersUsed
== 0)
707 assert(sizeof(prog
->SamplerUnits
) == sizeof(shProg
->SamplerUnits
));
709 /* Determine if any of the samplers used by this shader stage have
712 bool changed
= false;
713 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
714 if ((prog
->SamplersUsed
& (1U << j
)) != 0
715 && (prog
->SamplerUnits
[j
] != shProg
->SamplerUnits
[j
])) {
723 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
727 memcpy(prog
->SamplerUnits
,
728 shProg
->SamplerUnits
,
729 sizeof(shProg
->SamplerUnits
));
731 _mesa_update_shader_textures_used(shProg
, prog
);
732 (void) ctx
->Driver
.ProgramStringNotify(ctx
, prog
->Target
, prog
);
739 * Called by glUniformMatrix*() functions.
740 * Note: cols=2, rows=4 ==> array[2] of vec4
743 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
744 GLuint cols
, GLuint rows
,
745 GLint location
, GLsizei count
,
746 GLboolean transpose
, const GLfloat
*values
)
748 unsigned loc
, offset
;
752 struct gl_uniform_storage
*uni
;
754 ASSERT_OUTSIDE_BEGIN_END(ctx
);
756 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
757 &loc
, &offset
, "glUniformMatrix", false))
760 uni
= &shProg
->UniformStorage
[loc
];
761 if (!uni
->type
->is_matrix()) {
762 _mesa_error(ctx
, GL_INVALID_OPERATION
,
763 "glUniformMatrix(non-matrix uniform)");
767 assert(!uni
->type
->is_sampler());
768 vectors
= uni
->type
->matrix_columns
;
769 components
= uni
->type
->vector_elements
;
771 /* Verify that the types are compatible. This is greatly simplified for
772 * matrices because they can only have a float base type.
774 if (vectors
!= cols
|| components
!= rows
) {
775 _mesa_error(ctx
, GL_INVALID_OPERATION
,
776 "glUniformMatrix(matrix size mismatch)");
780 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
781 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
782 bool(transpose
), shProg
, location
, uni
);
785 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
787 * "When loading N elements starting at an arbitrary position k in a
788 * uniform declared as an array, elements k through k + N - 1 in the
789 * array will be replaced with the new values. Values for any array
790 * element that exceeds the highest array element index used, as
791 * reported by GetActiveUniform, will be ignored by the GL."
793 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
794 * will have already generated an error.
796 if (uni
->array_elements
!= 0) {
797 if (offset
>= uni
->array_elements
)
800 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
803 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
805 /* Store the data in the "actual type" backing storage for the uniform.
807 elements
= components
* vectors
;
810 memcpy(&uni
->storage
[elements
* offset
], values
,
811 sizeof(uni
->storage
[0]) * elements
* count
);
813 /* Copy and transpose the matrix.
815 const float *src
= values
;
816 float *dst
= &uni
->storage
[elements
* offset
].f
;
818 for (int i
= 0; i
< count
; i
++) {
819 for (unsigned r
= 0; r
< rows
; r
++) {
820 for (unsigned c
= 0; c
< cols
; c
++) {
821 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
830 uni
->initialized
= true;
832 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
836 * Called via glGetUniformLocation().
838 * The return value will encode two values, the uniform location and an
839 * offset (used for arrays, structs).
842 _mesa_get_uniform_location(struct gl_context
*ctx
,
843 struct gl_shader_program
*shProg
,
846 const size_t len
= strlen(name
);
851 /* If the name ends with a ']', assume that it refers to some element of an
852 * array. Malformed array references will fail the hash table look up
853 * below, so it doesn't matter that they are not caught here. This code
854 * only wants to catch the "leaf" array references so that arrays of
855 * structures containing arrays will be handled correctly.
857 if (name
[len
-1] == ']') {
860 /* Walk backwards over the string looking for a non-digit character.
861 * This had better be the opening bracket for an array index.
863 * Initially, i specifies the location of the ']'. Since the string may
864 * contain only the ']' charcater, walk backwards very carefully.
866 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
869 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
871 * "The first element of a uniform array is identified using the
872 * name of the uniform array appended with "[0]". Except if the last
873 * part of the string name indicates a uniform array, then the
874 * location of the first element of that array can be retrieved by
875 * either using the name of the uniform array, or the name of the
876 * uniform array appended with "[0]"."
878 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
880 * "name must be a null terminated string, without white space."
882 * Return an error if there is no opening '[' to match the closing ']'.
883 * An error will also be returned if there is intervening white space
884 * (or other non-digit characters) before the opening '['.
886 if ((i
== 0) || name
[i
-1] != '[')
889 /* Return an error if there are no digits between the opening '[' to
890 * match the closing ']'.
895 /* Make a new string that is a copy of the old string up to (but not
896 * including) the '[' character.
898 name_copy
= (char *) malloc(i
);
899 memcpy(name_copy
, name
, i
- 1);
900 name_copy
[i
-1] = '\0';
902 offset
= strtol(&name
[i
], NULL
, 10);
908 name_copy
= (char *) name
;
910 array_lookup
= false;
913 unsigned location
= 0;
914 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
917 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
919 /* Free the temporary buffer *before* possibly returning an error.
921 if (name_copy
!= name
)
927 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
928 * to (correctly) fail if 'a' is not an array.
930 if (array_lookup
&& shProg
->UniformStorage
[location
].array_elements
== 0) {
934 return _mesa_uniform_merge_location_offset(location
, offset
);
938 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
939 char *errMsg
, size_t errMsgLength
)
941 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
943 memset(unit_types
, 0, sizeof(unit_types
));
945 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
946 const struct gl_uniform_storage
*const storage
=
947 &shProg
->UniformStorage
[i
];
948 const glsl_type
*const t
= (storage
->type
->is_array())
949 ? storage
->type
->fields
.array
: storage
->type
;
951 if (!t
->is_sampler())
954 const unsigned count
= MAX2(1, storage
->type
->array_size());
955 for (unsigned j
= 0; j
< count
; j
++) {
956 const unsigned unit
= storage
->storage
[j
].i
;
958 /* The types of the samplers associated with a particular texture
959 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
960 * OpenGL 3.3 core spec says:
962 * "It is not allowed to have variables of different sampler
963 * types pointing to the same texture image unit within a program
966 if (unit_types
[unit
] == NULL
) {
967 unit_types
[unit
] = t
;
968 } else if (unit_types
[unit
] != t
) {
969 _mesa_snprintf(errMsg
, errMsgLength
,
970 "Texture unit %d is accessed both as %s and %s",
971 unit
, unit_types
[unit
]->name
, t
->name
);