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
;
78 validate_uniform_parameters(struct gl_context
*ctx
,
79 struct gl_shader_program
*shProg
,
80 GLint location
, GLsizei count
,
82 unsigned *array_index
,
84 bool negative_one_is_not_valid
)
86 if (!shProg
|| !shProg
->LinkStatus
) {
87 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
92 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
95 * "The error INVALID_OPERATION is generated if program has not been
96 * linked successfully, or if location is not a valid location for
99 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
102 * "If the value of location is -1, the Uniform* commands will
103 * silently ignore the data passed in, and the current uniform
104 * values will not be changed."
106 * Allowing -1 for the location parameter of glUniform allows
107 * applications to avoid error paths in the case that, for example, some
108 * uniform variable is removed by the compiler / linker after
109 * optimization. In this case, the new value of the uniform is dropped
110 * on the floor. For the case of glGetUniform, there is nothing
111 * sensible to do for a location of -1.
113 * The negative_one_is_not_valid flag selects between the two behaviors.
115 if (negative_one_is_not_valid
) {
116 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
123 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
125 * "If a negative number is provided where an argument of type sizei or
126 * sizeiptr is specified, the error INVALID_VALUE is generated."
129 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
133 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
135 * "If any of the following conditions occur, an INVALID_OPERATION
136 * error is generated by the Uniform* commands, and no uniform values
141 * - if no variable with a location of location exists in the
142 * program object currently in use and location is not -1,
143 * - if count is greater than one, and the uniform declared in the
144 * shader is not an array variable,
147 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
152 _mesa_uniform_split_location_offset(location
, loc
, array_index
);
154 if (*loc
>= shProg
->NumUserUniformStorage
) {
155 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
160 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
161 _mesa_error(ctx
, GL_INVALID_OPERATION
,
162 "%s(count > 1 for non-array, location=%d)",
167 /* This case should be impossible. The implication is that a call like
168 * glGetUniformLocation(prog, "foo[8]") was successful but "foo" is not an
171 if (*array_index
!= 0 && shProg
->UniformStorage
[*loc
].array_elements
== 0) {
172 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
180 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
183 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
184 GLsizei bufSize
, enum glsl_base_type returnType
,
187 struct gl_shader_program
*shProg
=
188 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
189 struct gl_uniform_storage
*uni
;
190 unsigned loc
, offset
;
192 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
193 &loc
, &offset
, "glGetUniform", true))
196 uni
= &shProg
->UniformStorage
[loc
];
199 unsigned elements
= (uni
->type
->is_sampler())
200 ? 1 : uni
->type
->components();
202 /* Calculate the source base address *BEFORE* modifying elements to
203 * account for the size of the user's buffer.
205 const union gl_constant_value
*const src
=
206 &uni
->storage
[offset
* elements
];
208 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
209 returnType
== GLSL_TYPE_UINT
);
210 /* The three (currently) supported types all have the same size,
211 * which is of course the same as their union. That'll change
212 * with glGetUniformdv()...
214 unsigned bytes
= sizeof(src
[0]) * elements
;
215 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
216 _mesa_error( ctx
, GL_INVALID_OPERATION
,
217 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
218 " but %u bytes are required)", bufSize
, bytes
);
222 /* If the return type and the uniform's native type are "compatible,"
223 * just memcpy the data. If the types are not compatible, perform a
224 * slower convert-and-copy process.
226 if (returnType
== uni
->type
->base_type
227 || ((returnType
== GLSL_TYPE_INT
228 || returnType
== GLSL_TYPE_UINT
229 || returnType
== GLSL_TYPE_SAMPLER
)
231 (uni
->type
->base_type
== GLSL_TYPE_INT
232 || uni
->type
->base_type
== GLSL_TYPE_UINT
233 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
234 memcpy(paramsOut
, src
, bytes
);
236 union gl_constant_value
*const dst
=
237 (union gl_constant_value
*) paramsOut
;
239 /* This code could be optimized by putting the loop inside the switch
240 * statements. However, this is not expected to be
241 * performance-critical code.
243 for (unsigned i
= 0; i
< elements
; i
++) {
244 switch (returnType
) {
245 case GLSL_TYPE_FLOAT
:
246 switch (uni
->type
->base_type
) {
248 dst
[i
].f
= (float) src
[i
].u
;
251 case GLSL_TYPE_SAMPLER
:
252 dst
[i
].f
= (float) src
[i
].i
;
255 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
258 assert(!"Should not get here.");
265 switch (uni
->type
->base_type
) {
266 case GLSL_TYPE_FLOAT
:
267 /* While the GL 3.2 core spec doesn't explicitly
268 * state how conversion of float uniforms to integer
269 * values works, in section 6.2 "State Tables" on
272 * "Unless otherwise specified, when floating
273 * point state is returned as integer values or
274 * integer state is returned as floating-point
275 * values it is converted in the fashion
276 * described in section 6.1.2"
278 * That section, on page 248, says:
280 * "If GetIntegerv or GetInteger64v are called,
281 * a floating-point value is rounded to the
282 * nearest integer..."
284 dst
[i
].i
= IROUND(src
[i
].f
);
287 dst
[i
].i
= src
[i
].i
? 1 : 0;
290 assert(!"Should not get here.");
296 assert(!"Should not get here.");
305 log_uniform(const void *values
, enum glsl_base_type basicType
,
306 unsigned rows
, unsigned cols
, unsigned count
,
308 const struct gl_shader_program
*shProg
,
310 const struct gl_uniform_storage
*uni
)
313 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
314 const unsigned elems
= rows
* cols
* count
;
315 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
317 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
318 "transpose = %s) to: ",
319 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
320 transpose
? "true" : "false");
321 for (unsigned i
= 0; i
< elems
; i
++) {
322 if (i
!= 0 && ((i
% rows
) == 0))
327 printf("%u ", v
[i
].u
);
330 printf("%d ", v
[i
].i
);
332 case GLSL_TYPE_FLOAT
:
333 printf("%g ", v
[i
].f
);
336 assert(!"Should not get here.");
346 log_program_parameters(const struct gl_shader_program
*shProg
)
348 static const char *stages
[] = {
349 "vertex", "fragment", "geometry"
352 assert(Elements(stages
) == MESA_SHADER_TYPES
);
354 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
355 if (shProg
->_LinkedShaders
[i
] == NULL
)
358 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
360 printf("Program %d %s shader parameters:\n",
361 shProg
->Name
, stages
[i
]);
362 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
363 printf("%s: %p %f %f %f %f\n",
364 prog
->Parameters
->Parameters
[j
].Name
,
365 prog
->Parameters
->ParameterValues
[j
],
366 prog
->Parameters
->ParameterValues
[j
][0].f
,
367 prog
->Parameters
->ParameterValues
[j
][1].f
,
368 prog
->Parameters
->ParameterValues
[j
][2].f
,
369 prog
->Parameters
->ParameterValues
[j
][3].f
);
377 * Propagate some values from uniform backing storage to driver storage
379 * Values propagated from uniform backing storage to driver storage
380 * have all format / type conversions previously requested by the
381 * driver applied. This function is most often called by the
382 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
385 * \param uni Uniform whose data is to be propagated to driver storage
386 * \param array_index If \c uni is an array, this is the element of
387 * the array to be propagated.
388 * \param count Number of array elements to propagate.
391 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
392 unsigned array_index
,
397 /* vector_elements and matrix_columns can be 0 for samplers.
399 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
400 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
402 /* Store the data in the driver's requested type in the driver's storage
405 unsigned src_vector_byte_stride
= components
* 4;
407 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
408 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
409 uint8_t *dst
= (uint8_t *) store
->data
;
410 const unsigned extra_stride
=
411 store
->element_stride
- (vectors
* store
->vector_stride
);
413 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
416 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
418 __func__
, dst
, array_index
, components
,
419 vectors
, count
, store
->vector_stride
, extra_stride
);
422 dst
+= array_index
* store
->element_stride
;
424 switch (store
->format
) {
426 case uniform_bool_int_0_1
: {
430 for (j
= 0; j
< count
; j
++) {
431 for (v
= 0; v
< vectors
; v
++) {
432 memcpy(dst
, src
, src_vector_byte_stride
);
433 src
+= src_vector_byte_stride
;
434 dst
+= store
->vector_stride
;
442 case uniform_int_float
:
443 case uniform_bool_float
: {
444 const int *isrc
= (const int *) src
;
449 for (j
= 0; j
< count
; j
++) {
450 for (v
= 0; v
< vectors
; v
++) {
451 for (c
= 0; c
< components
; c
++) {
452 ((float *) dst
)[c
] = (float) *isrc
;
456 dst
+= store
->vector_stride
;
464 case uniform_bool_int_0_not0
: {
465 const int *isrc
= (const int *) src
;
470 for (j
= 0; j
< count
; j
++) {
471 for (v
= 0; v
< vectors
; v
++) {
472 for (c
= 0; c
< components
; c
++) {
473 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
477 dst
+= store
->vector_stride
;
486 assert(!"Should not get here.");
493 * Called via glUniform*() functions.
496 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
497 GLint location
, GLsizei count
,
498 const GLvoid
*values
, GLenum type
)
500 unsigned loc
, offset
;
502 unsigned src_components
;
503 enum glsl_base_type basicType
;
504 struct gl_uniform_storage
*uni
;
506 ASSERT_OUTSIDE_BEGIN_END(ctx
);
508 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
509 &loc
, &offset
, "glUniform", false))
512 uni
= &shProg
->UniformStorage
[loc
];
514 /* Verify that the types are compatible.
518 basicType
= GLSL_TYPE_FLOAT
;
522 basicType
= GLSL_TYPE_FLOAT
;
526 basicType
= GLSL_TYPE_FLOAT
;
530 basicType
= GLSL_TYPE_FLOAT
;
533 case GL_UNSIGNED_INT
:
534 basicType
= GLSL_TYPE_UINT
;
537 case GL_UNSIGNED_INT_VEC2
:
538 basicType
= GLSL_TYPE_UINT
;
541 case GL_UNSIGNED_INT_VEC3
:
542 basicType
= GLSL_TYPE_UINT
;
545 case GL_UNSIGNED_INT_VEC4
:
546 basicType
= GLSL_TYPE_UINT
;
550 basicType
= GLSL_TYPE_INT
;
554 basicType
= GLSL_TYPE_INT
;
558 basicType
= GLSL_TYPE_INT
;
562 basicType
= GLSL_TYPE_INT
;
570 case GL_FLOAT_MAT2x3
:
571 case GL_FLOAT_MAT2x4
:
572 case GL_FLOAT_MAT3x2
:
574 case GL_FLOAT_MAT3x4
:
575 case GL_FLOAT_MAT4x2
:
576 case GL_FLOAT_MAT4x3
:
579 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
583 if (uni
->type
->is_sampler()) {
586 components
= uni
->type
->vector_elements
;
590 switch (uni
->type
->base_type
) {
594 case GLSL_TYPE_SAMPLER
:
595 match
= (basicType
== GLSL_TYPE_INT
);
598 match
= (basicType
== uni
->type
->base_type
);
602 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
603 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
607 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
608 log_uniform(values
, basicType
, components
, 1, count
,
609 false, shProg
, location
, uni
);
612 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
614 * "Setting a sampler's value to i selects texture image unit number
615 * i. The values of i range from zero to the implementation- dependent
616 * maximum supported number of texture image units."
618 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
621 * "Error Description Offending command
624 * INVALID_VALUE Numeric argument out of range Yes"
626 * Based on that, when an invalid sampler is specified, we generate a
627 * GL_INVALID_VALUE error and ignore the command.
629 if (uni
->type
->is_sampler()) {
632 for (i
= 0; i
< count
; i
++) {
633 const unsigned texUnit
= ((unsigned *) values
)[i
];
635 /* check that the sampler (tex unit index) is legal */
636 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
637 _mesa_error(ctx
, GL_INVALID_VALUE
,
638 "glUniform1i(invalid sampler/tex unit index for "
646 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
648 * "When loading N elements starting at an arbitrary position k in a
649 * uniform declared as an array, elements k through k + N - 1 in the
650 * array will be replaced with the new values. Values for any array
651 * element that exceeds the highest array element index used, as
652 * reported by GetActiveUniform, will be ignored by the GL."
654 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
655 * will have already generated an error.
657 if (uni
->array_elements
!= 0) {
658 if (offset
>= uni
->array_elements
)
661 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
664 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
666 /* Store the data in the "actual type" backing storage for the uniform.
668 if (!uni
->type
->is_boolean()) {
669 memcpy(&uni
->storage
[components
* offset
], values
,
670 sizeof(uni
->storage
[0]) * components
* count
);
672 const union gl_constant_value
*src
=
673 (const union gl_constant_value
*) values
;
674 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
675 const unsigned elems
= components
* count
;
678 for (i
= 0; i
< elems
; i
++) {
679 if (basicType
== GLSL_TYPE_FLOAT
) {
680 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
682 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
687 uni
->initialized
= true;
689 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
691 /* If the uniform is a sampler, do the extra magic necessary to propagate
692 * the changes through.
694 if (uni
->type
->is_sampler()) {
697 for (i
= 0; i
< count
; i
++) {
698 shProg
->SamplerUnits
[uni
->sampler
+ offset
+ i
] =
699 ((unsigned *) values
)[i
];
702 bool flushed
= false;
703 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
704 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
706 /* If the shader stage doesn't use any samplers, don't bother
707 * checking if any samplers have changed.
709 if (sh
== NULL
|| sh
->active_samplers
== 0)
712 struct gl_program
*const prog
= sh
->Program
;
714 assert(sizeof(prog
->SamplerUnits
) == sizeof(shProg
->SamplerUnits
));
716 /* Determine if any of the samplers used by this shader stage have
719 bool changed
= false;
720 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
721 if ((sh
->active_samplers
& (1U << j
)) != 0
722 && (prog
->SamplerUnits
[j
] != shProg
->SamplerUnits
[j
])) {
730 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
734 memcpy(prog
->SamplerUnits
,
735 shProg
->SamplerUnits
,
736 sizeof(shProg
->SamplerUnits
));
738 _mesa_update_shader_textures_used(shProg
, prog
);
739 (void) ctx
->Driver
.ProgramStringNotify(ctx
, prog
->Target
, prog
);
746 * Called by glUniformMatrix*() functions.
747 * Note: cols=2, rows=4 ==> array[2] of vec4
750 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
751 GLuint cols
, GLuint rows
,
752 GLint location
, GLsizei count
,
753 GLboolean transpose
, const GLfloat
*values
)
755 unsigned loc
, offset
;
759 struct gl_uniform_storage
*uni
;
761 ASSERT_OUTSIDE_BEGIN_END(ctx
);
763 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
764 &loc
, &offset
, "glUniformMatrix", false))
767 uni
= &shProg
->UniformStorage
[loc
];
768 if (!uni
->type
->is_matrix()) {
769 _mesa_error(ctx
, GL_INVALID_OPERATION
,
770 "glUniformMatrix(non-matrix uniform)");
774 assert(!uni
->type
->is_sampler());
775 vectors
= uni
->type
->matrix_columns
;
776 components
= uni
->type
->vector_elements
;
778 /* Verify that the types are compatible. This is greatly simplified for
779 * matrices because they can only have a float base type.
781 if (vectors
!= cols
|| components
!= rows
) {
782 _mesa_error(ctx
, GL_INVALID_OPERATION
,
783 "glUniformMatrix(matrix size mismatch)");
787 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
788 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
789 if (ctx
->API
== API_OPENGLES
|| ctx
->API
== API_OPENGLES2
) {
791 _mesa_error(ctx
, GL_INVALID_VALUE
,
792 "glUniformMatrix(matrix transpose is not GL_FALSE)");
797 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
798 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
799 bool(transpose
), shProg
, location
, uni
);
802 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
804 * "When loading N elements starting at an arbitrary position k in a
805 * uniform declared as an array, elements k through k + N - 1 in the
806 * array will be replaced with the new values. Values for any array
807 * element that exceeds the highest array element index used, as
808 * reported by GetActiveUniform, will be ignored by the GL."
810 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
811 * will have already generated an error.
813 if (uni
->array_elements
!= 0) {
814 if (offset
>= uni
->array_elements
)
817 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
820 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
822 /* Store the data in the "actual type" backing storage for the uniform.
824 elements
= components
* vectors
;
827 memcpy(&uni
->storage
[elements
* offset
], values
,
828 sizeof(uni
->storage
[0]) * elements
* count
);
830 /* Copy and transpose the matrix.
832 const float *src
= values
;
833 float *dst
= &uni
->storage
[elements
* offset
].f
;
835 for (int i
= 0; i
< count
; i
++) {
836 for (unsigned r
= 0; r
< rows
; r
++) {
837 for (unsigned c
= 0; c
< cols
; c
++) {
838 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
847 uni
->initialized
= true;
849 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
853 * Called via glGetUniformLocation().
855 * Returns the uniform index into UniformStorage (also the
856 * glGetActiveUniformsiv uniform index), and stores the referenced
857 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
858 * return values can be encoded into a uniform location for
859 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
862 _mesa_get_uniform_location(struct gl_context
*ctx
,
863 struct gl_shader_program
*shProg
,
865 unsigned *out_offset
)
867 const size_t len
= strlen(name
);
872 /* If the name ends with a ']', assume that it refers to some element of an
873 * array. Malformed array references will fail the hash table look up
874 * below, so it doesn't matter that they are not caught here. This code
875 * only wants to catch the "leaf" array references so that arrays of
876 * structures containing arrays will be handled correctly.
878 if (name
[len
-1] == ']') {
881 /* Walk backwards over the string looking for a non-digit character.
882 * This had better be the opening bracket for an array index.
884 * Initially, i specifies the location of the ']'. Since the string may
885 * contain only the ']' charcater, walk backwards very carefully.
887 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
890 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
892 * "The first element of a uniform array is identified using the
893 * name of the uniform array appended with "[0]". Except if the last
894 * part of the string name indicates a uniform array, then the
895 * location of the first element of that array can be retrieved by
896 * either using the name of the uniform array, or the name of the
897 * uniform array appended with "[0]"."
899 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
901 * "name must be a null terminated string, without white space."
903 * Return an error if there is no opening '[' to match the closing ']'.
904 * An error will also be returned if there is intervening white space
905 * (or other non-digit characters) before the opening '['.
907 if ((i
== 0) || name
[i
-1] != '[')
908 return GL_INVALID_INDEX
;
910 /* Return an error if there are no digits between the opening '[' to
911 * match the closing ']'.
914 return GL_INVALID_INDEX
;
916 /* Make a new string that is a copy of the old string up to (but not
917 * including) the '[' character.
919 name_copy
= (char *) malloc(i
);
920 memcpy(name_copy
, name
, i
- 1);
921 name_copy
[i
-1] = '\0';
923 offset
= strtol(&name
[i
], NULL
, 10);
926 return GL_INVALID_INDEX
;
931 name_copy
= (char *) name
;
933 array_lookup
= false;
936 unsigned location
= 0;
937 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
940 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
942 /* Free the temporary buffer *before* possibly returning an error.
944 if (name_copy
!= name
)
948 return GL_INVALID_INDEX
;
950 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
951 * to (correctly) fail if 'a' is not an array.
953 if (array_lookup
&& shProg
->UniformStorage
[location
].array_elements
== 0) {
954 return GL_INVALID_INDEX
;
957 *out_offset
= offset
;
962 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
963 char *errMsg
, size_t errMsgLength
)
965 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
967 memset(unit_types
, 0, sizeof(unit_types
));
969 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
970 const struct gl_uniform_storage
*const storage
=
971 &shProg
->UniformStorage
[i
];
972 const glsl_type
*const t
= (storage
->type
->is_array())
973 ? storage
->type
->fields
.array
: storage
->type
;
975 if (!t
->is_sampler())
978 const unsigned count
= MAX2(1, storage
->type
->array_size());
979 for (unsigned j
= 0; j
< count
; j
++) {
980 const unsigned unit
= storage
->storage
[j
].i
;
982 /* The types of the samplers associated with a particular texture
983 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
984 * OpenGL 3.3 core spec says:
986 * "It is not allowed to have variables of different sampler
987 * types pointing to the same texture image unit within a program
990 if (unit_types
[unit
] == NULL
) {
991 unit_types
[unit
] = t
;
992 } else if (unit_types
[unit
] != t
) {
993 _mesa_snprintf(errMsg
, errMsgLength
,
994 "Texture unit %d is accessed both as %s and %s",
995 unit
, unit_types
[unit
]->name
, t
->name
);