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");
49 ASSERT_OUTSIDE_BEGIN_END(ctx
);
54 if (index
>= shProg
->NumUserUniformStorage
) {
55 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
59 const struct gl_uniform_storage
*const uni
= &shProg
->UniformStorage
[index
];
62 _mesa_copy_string(nameOut
, maxLength
, length
, uni
->name
);
66 /* array_elements is zero for non-arrays, but the API requires that 1 be
69 *size
= MAX2(1, uni
->array_elements
);
73 *type
= uni
->type
->gl_type
;
77 extern "C" void GLAPIENTRY
78 _mesa_GetActiveUniformsiv(GLuint program
,
80 const GLuint
*uniformIndices
,
84 GET_CURRENT_CONTEXT(ctx
);
85 struct gl_shader_program
*shProg
;
88 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
92 if (uniformCount
< 0) {
93 _mesa_error(ctx
, GL_INVALID_VALUE
,
94 "glGetUniformIndices(uniformCount < 0)");
98 for (i
= 0; i
< uniformCount
; i
++) {
99 GLuint index
= uniformIndices
[i
];
101 if (index
>= shProg
->NumUserUniformStorage
) {
102 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
107 for (i
= 0; i
< uniformCount
; i
++) {
108 GLuint index
= uniformIndices
[i
];
109 const struct gl_uniform_storage
*uni
= &shProg
->UniformStorage
[index
];
112 case GL_UNIFORM_TYPE
:
113 params
[i
] = uni
->type
->gl_type
;
116 case GL_UNIFORM_SIZE
:
117 /* array_elements is zero for non-arrays, but the API requires that 1 be
120 params
[i
] = MAX2(1, uni
->array_elements
);
123 case GL_UNIFORM_NAME_LENGTH
:
124 params
[i
] = strlen(uni
->name
) + 1;
127 case GL_UNIFORM_BLOCK_INDEX
:
128 params
[i
] = uni
->block_index
;
131 case GL_UNIFORM_OFFSET
:
132 params
[i
] = uni
->offset
;
135 case GL_UNIFORM_ARRAY_STRIDE
:
136 params
[i
] = uni
->array_stride
;
139 case GL_UNIFORM_MATRIX_STRIDE
:
140 params
[i
] = uni
->matrix_stride
;
143 case GL_UNIFORM_IS_ROW_MAJOR
:
144 params
[i
] = uni
->row_major
;
148 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetActiveUniformsiv(pname)");
155 validate_uniform_parameters(struct gl_context
*ctx
,
156 struct gl_shader_program
*shProg
,
157 GLint location
, GLsizei count
,
159 unsigned *array_index
,
161 bool negative_one_is_not_valid
)
163 if (!shProg
|| !shProg
->LinkStatus
) {
164 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
168 if (location
== -1) {
169 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
172 * "The error INVALID_OPERATION is generated if program has not been
173 * linked successfully, or if location is not a valid location for
176 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
179 * "If the value of location is -1, the Uniform* commands will
180 * silently ignore the data passed in, and the current uniform
181 * values will not be changed."
183 * Allowing -1 for the location parameter of glUniform allows
184 * applications to avoid error paths in the case that, for example, some
185 * uniform variable is removed by the compiler / linker after
186 * optimization. In this case, the new value of the uniform is dropped
187 * on the floor. For the case of glGetUniform, there is nothing
188 * sensible to do for a location of -1.
190 * The negative_one_is_not_valid flag selects between the two behaviors.
192 if (negative_one_is_not_valid
) {
193 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
200 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
202 * "If a negative number is provided where an argument of type sizei or
203 * sizeiptr is specified, the error INVALID_VALUE is generated."
206 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
210 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
212 * "If any of the following conditions occur, an INVALID_OPERATION
213 * error is generated by the Uniform* commands, and no uniform values
218 * - if no variable with a location of location exists in the
219 * program object currently in use and location is not -1,
220 * - if count is greater than one, and the uniform declared in the
221 * shader is not an array variable,
224 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
229 _mesa_uniform_split_location_offset(location
, loc
, array_index
);
231 if (*loc
>= shProg
->NumUserUniformStorage
) {
232 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
237 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
238 _mesa_error(ctx
, GL_INVALID_OPERATION
,
239 "%s(count > 1 for non-array, location=%d)",
244 /* If the uniform is an array, check that array_index is in bounds.
245 * If not an array, check that array_index is zero.
246 * array_index is unsigned so no need to check for less than zero.
248 unsigned limit
= shProg
->UniformStorage
[*loc
].array_elements
;
251 if (*array_index
>= limit
) {
252 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
260 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
263 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
264 GLsizei bufSize
, enum glsl_base_type returnType
,
267 struct gl_shader_program
*shProg
=
268 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
269 struct gl_uniform_storage
*uni
;
270 unsigned loc
, offset
;
272 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
273 &loc
, &offset
, "glGetUniform", true))
276 uni
= &shProg
->UniformStorage
[loc
];
279 unsigned elements
= (uni
->type
->is_sampler())
280 ? 1 : uni
->type
->components();
282 /* Calculate the source base address *BEFORE* modifying elements to
283 * account for the size of the user's buffer.
285 const union gl_constant_value
*const src
=
286 &uni
->storage
[offset
* elements
];
288 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
289 returnType
== GLSL_TYPE_UINT
);
290 /* The three (currently) supported types all have the same size,
291 * which is of course the same as their union. That'll change
292 * with glGetUniformdv()...
294 unsigned bytes
= sizeof(src
[0]) * elements
;
295 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
296 _mesa_error( ctx
, GL_INVALID_OPERATION
,
297 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
298 " but %u bytes are required)", bufSize
, bytes
);
302 /* If the return type and the uniform's native type are "compatible,"
303 * just memcpy the data. If the types are not compatible, perform a
304 * slower convert-and-copy process.
306 if (returnType
== uni
->type
->base_type
307 || ((returnType
== GLSL_TYPE_INT
308 || returnType
== GLSL_TYPE_UINT
309 || returnType
== GLSL_TYPE_SAMPLER
)
311 (uni
->type
->base_type
== GLSL_TYPE_INT
312 || uni
->type
->base_type
== GLSL_TYPE_UINT
313 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
314 memcpy(paramsOut
, src
, bytes
);
316 union gl_constant_value
*const dst
=
317 (union gl_constant_value
*) paramsOut
;
319 /* This code could be optimized by putting the loop inside the switch
320 * statements. However, this is not expected to be
321 * performance-critical code.
323 for (unsigned i
= 0; i
< elements
; i
++) {
324 switch (returnType
) {
325 case GLSL_TYPE_FLOAT
:
326 switch (uni
->type
->base_type
) {
328 dst
[i
].f
= (float) src
[i
].u
;
331 case GLSL_TYPE_SAMPLER
:
332 dst
[i
].f
= (float) src
[i
].i
;
335 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
338 assert(!"Should not get here.");
345 switch (uni
->type
->base_type
) {
346 case GLSL_TYPE_FLOAT
:
347 /* While the GL 3.2 core spec doesn't explicitly
348 * state how conversion of float uniforms to integer
349 * values works, in section 6.2 "State Tables" on
352 * "Unless otherwise specified, when floating
353 * point state is returned as integer values or
354 * integer state is returned as floating-point
355 * values it is converted in the fashion
356 * described in section 6.1.2"
358 * That section, on page 248, says:
360 * "If GetIntegerv or GetInteger64v are called,
361 * a floating-point value is rounded to the
362 * nearest integer..."
364 dst
[i
].i
= IROUND(src
[i
].f
);
367 dst
[i
].i
= src
[i
].i
? 1 : 0;
370 assert(!"Should not get here.");
376 assert(!"Should not get here.");
385 log_uniform(const void *values
, enum glsl_base_type basicType
,
386 unsigned rows
, unsigned cols
, unsigned count
,
388 const struct gl_shader_program
*shProg
,
390 const struct gl_uniform_storage
*uni
)
393 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
394 const unsigned elems
= rows
* cols
* count
;
395 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
397 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
398 "transpose = %s) to: ",
399 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
400 transpose
? "true" : "false");
401 for (unsigned i
= 0; i
< elems
; i
++) {
402 if (i
!= 0 && ((i
% rows
) == 0))
407 printf("%u ", v
[i
].u
);
410 printf("%d ", v
[i
].i
);
412 case GLSL_TYPE_FLOAT
:
413 printf("%g ", v
[i
].f
);
416 assert(!"Should not get here.");
426 log_program_parameters(const struct gl_shader_program
*shProg
)
428 static const char *stages
[] = {
429 "vertex", "fragment", "geometry"
432 assert(Elements(stages
) == MESA_SHADER_TYPES
);
434 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
435 if (shProg
->_LinkedShaders
[i
] == NULL
)
438 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
440 printf("Program %d %s shader parameters:\n",
441 shProg
->Name
, stages
[i
]);
442 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
443 printf("%s: %p %f %f %f %f\n",
444 prog
->Parameters
->Parameters
[j
].Name
,
445 prog
->Parameters
->ParameterValues
[j
],
446 prog
->Parameters
->ParameterValues
[j
][0].f
,
447 prog
->Parameters
->ParameterValues
[j
][1].f
,
448 prog
->Parameters
->ParameterValues
[j
][2].f
,
449 prog
->Parameters
->ParameterValues
[j
][3].f
);
457 * Propagate some values from uniform backing storage to driver storage
459 * Values propagated from uniform backing storage to driver storage
460 * have all format / type conversions previously requested by the
461 * driver applied. This function is most often called by the
462 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
465 * \param uni Uniform whose data is to be propagated to driver storage
466 * \param array_index If \c uni is an array, this is the element of
467 * the array to be propagated.
468 * \param count Number of array elements to propagate.
471 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
472 unsigned array_index
,
477 /* vector_elements and matrix_columns can be 0 for samplers.
479 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
480 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
482 /* Store the data in the driver's requested type in the driver's storage
485 unsigned src_vector_byte_stride
= components
* 4;
487 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
488 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
489 uint8_t *dst
= (uint8_t *) store
->data
;
490 const unsigned extra_stride
=
491 store
->element_stride
- (vectors
* store
->vector_stride
);
493 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
496 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
498 __func__
, dst
, array_index
, components
,
499 vectors
, count
, store
->vector_stride
, extra_stride
);
502 dst
+= array_index
* store
->element_stride
;
504 switch (store
->format
) {
506 case uniform_bool_int_0_1
: {
510 for (j
= 0; j
< count
; j
++) {
511 for (v
= 0; v
< vectors
; v
++) {
512 memcpy(dst
, src
, src_vector_byte_stride
);
513 src
+= src_vector_byte_stride
;
514 dst
+= store
->vector_stride
;
522 case uniform_int_float
:
523 case uniform_bool_float
: {
524 const int *isrc
= (const int *) src
;
529 for (j
= 0; j
< count
; j
++) {
530 for (v
= 0; v
< vectors
; v
++) {
531 for (c
= 0; c
< components
; c
++) {
532 ((float *) dst
)[c
] = (float) *isrc
;
536 dst
+= store
->vector_stride
;
544 case uniform_bool_int_0_not0
: {
545 const int *isrc
= (const int *) src
;
550 for (j
= 0; j
< count
; j
++) {
551 for (v
= 0; v
< vectors
; v
++) {
552 for (c
= 0; c
< components
; c
++) {
553 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
557 dst
+= store
->vector_stride
;
566 assert(!"Should not get here.");
573 * Called via glUniform*() functions.
576 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
577 GLint location
, GLsizei count
,
578 const GLvoid
*values
, GLenum type
)
580 unsigned loc
, offset
;
582 unsigned src_components
;
583 enum glsl_base_type basicType
;
584 struct gl_uniform_storage
*uni
;
586 ASSERT_OUTSIDE_BEGIN_END(ctx
);
588 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
589 &loc
, &offset
, "glUniform", false))
592 uni
= &shProg
->UniformStorage
[loc
];
594 /* Verify that the types are compatible.
598 basicType
= GLSL_TYPE_FLOAT
;
602 basicType
= GLSL_TYPE_FLOAT
;
606 basicType
= GLSL_TYPE_FLOAT
;
610 basicType
= GLSL_TYPE_FLOAT
;
613 case GL_UNSIGNED_INT
:
614 basicType
= GLSL_TYPE_UINT
;
617 case GL_UNSIGNED_INT_VEC2
:
618 basicType
= GLSL_TYPE_UINT
;
621 case GL_UNSIGNED_INT_VEC3
:
622 basicType
= GLSL_TYPE_UINT
;
625 case GL_UNSIGNED_INT_VEC4
:
626 basicType
= GLSL_TYPE_UINT
;
630 basicType
= GLSL_TYPE_INT
;
634 basicType
= GLSL_TYPE_INT
;
638 basicType
= GLSL_TYPE_INT
;
642 basicType
= GLSL_TYPE_INT
;
650 case GL_FLOAT_MAT2x3
:
651 case GL_FLOAT_MAT2x4
:
652 case GL_FLOAT_MAT3x2
:
654 case GL_FLOAT_MAT3x4
:
655 case GL_FLOAT_MAT4x2
:
656 case GL_FLOAT_MAT4x3
:
659 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
663 if (uni
->type
->is_sampler()) {
666 components
= uni
->type
->vector_elements
;
670 switch (uni
->type
->base_type
) {
674 case GLSL_TYPE_SAMPLER
:
675 match
= (basicType
== GLSL_TYPE_INT
);
678 match
= (basicType
== uni
->type
->base_type
);
682 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
683 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
687 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
688 log_uniform(values
, basicType
, components
, 1, count
,
689 false, shProg
, location
, uni
);
692 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
694 * "Setting a sampler's value to i selects texture image unit number
695 * i. The values of i range from zero to the implementation- dependent
696 * maximum supported number of texture image units."
698 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
701 * "Error Description Offending command
704 * INVALID_VALUE Numeric argument out of range Yes"
706 * Based on that, when an invalid sampler is specified, we generate a
707 * GL_INVALID_VALUE error and ignore the command.
709 if (uni
->type
->is_sampler()) {
712 for (i
= 0; i
< count
; i
++) {
713 const unsigned texUnit
= ((unsigned *) values
)[i
];
715 /* check that the sampler (tex unit index) is legal */
716 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
717 _mesa_error(ctx
, GL_INVALID_VALUE
,
718 "glUniform1i(invalid sampler/tex unit index for "
726 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
728 * "When loading N elements starting at an arbitrary position k in a
729 * uniform declared as an array, elements k through k + N - 1 in the
730 * array will be replaced with the new values. Values for any array
731 * element that exceeds the highest array element index used, as
732 * reported by GetActiveUniform, will be ignored by the GL."
734 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
735 * will have already generated an error.
737 if (uni
->array_elements
!= 0) {
738 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
741 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
743 /* Store the data in the "actual type" backing storage for the uniform.
745 if (!uni
->type
->is_boolean()) {
746 memcpy(&uni
->storage
[components
* offset
], values
,
747 sizeof(uni
->storage
[0]) * components
* count
);
749 const union gl_constant_value
*src
=
750 (const union gl_constant_value
*) values
;
751 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
752 const unsigned elems
= components
* count
;
755 for (i
= 0; i
< elems
; i
++) {
756 if (basicType
== GLSL_TYPE_FLOAT
) {
757 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
759 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
764 uni
->initialized
= true;
766 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
768 /* If the uniform is a sampler, do the extra magic necessary to propagate
769 * the changes through.
771 if (uni
->type
->is_sampler()) {
774 for (i
= 0; i
< count
; i
++) {
775 shProg
->SamplerUnits
[uni
->sampler
+ offset
+ i
] =
776 ((unsigned *) values
)[i
];
779 bool flushed
= false;
780 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
781 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
783 /* If the shader stage doesn't use any samplers, don't bother
784 * checking if any samplers have changed.
786 if (sh
== NULL
|| sh
->active_samplers
== 0)
789 struct gl_program
*const prog
= sh
->Program
;
791 assert(sizeof(prog
->SamplerUnits
) == sizeof(shProg
->SamplerUnits
));
793 /* Determine if any of the samplers used by this shader stage have
796 bool changed
= false;
797 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
798 if ((sh
->active_samplers
& (1U << j
)) != 0
799 && (prog
->SamplerUnits
[j
] != shProg
->SamplerUnits
[j
])) {
807 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
811 memcpy(prog
->SamplerUnits
,
812 shProg
->SamplerUnits
,
813 sizeof(shProg
->SamplerUnits
));
815 _mesa_update_shader_textures_used(shProg
, prog
);
816 if (ctx
->Driver
.SamplerUniformChange
)
817 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
824 * Called by glUniformMatrix*() functions.
825 * Note: cols=2, rows=4 ==> array[2] of vec4
828 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
829 GLuint cols
, GLuint rows
,
830 GLint location
, GLsizei count
,
831 GLboolean transpose
, const GLfloat
*values
)
833 unsigned loc
, offset
;
837 struct gl_uniform_storage
*uni
;
839 ASSERT_OUTSIDE_BEGIN_END(ctx
);
841 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
842 &loc
, &offset
, "glUniformMatrix", false))
845 uni
= &shProg
->UniformStorage
[loc
];
846 if (!uni
->type
->is_matrix()) {
847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
848 "glUniformMatrix(non-matrix uniform)");
852 assert(!uni
->type
->is_sampler());
853 vectors
= uni
->type
->matrix_columns
;
854 components
= uni
->type
->vector_elements
;
856 /* Verify that the types are compatible. This is greatly simplified for
857 * matrices because they can only have a float base type.
859 if (vectors
!= cols
|| components
!= rows
) {
860 _mesa_error(ctx
, GL_INVALID_OPERATION
,
861 "glUniformMatrix(matrix size mismatch)");
865 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
866 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
867 if (ctx
->API
== API_OPENGLES
868 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)) {
870 _mesa_error(ctx
, GL_INVALID_VALUE
,
871 "glUniformMatrix(matrix transpose is not GL_FALSE)");
876 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
877 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
878 bool(transpose
), shProg
, location
, uni
);
881 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
883 * "When loading N elements starting at an arbitrary position k in a
884 * uniform declared as an array, elements k through k + N - 1 in the
885 * array will be replaced with the new values. Values for any array
886 * element that exceeds the highest array element index used, as
887 * reported by GetActiveUniform, will be ignored by the GL."
889 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
890 * will have already generated an error.
892 if (uni
->array_elements
!= 0) {
893 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
896 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
898 /* Store the data in the "actual type" backing storage for the uniform.
900 elements
= components
* vectors
;
903 memcpy(&uni
->storage
[elements
* offset
], values
,
904 sizeof(uni
->storage
[0]) * elements
* count
);
906 /* Copy and transpose the matrix.
908 const float *src
= values
;
909 float *dst
= &uni
->storage
[elements
* offset
].f
;
911 for (int i
= 0; i
< count
; i
++) {
912 for (unsigned r
= 0; r
< rows
; r
++) {
913 for (unsigned c
= 0; c
< cols
; c
++) {
914 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
923 uni
->initialized
= true;
925 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
929 * Called via glGetUniformLocation().
931 * Returns the uniform index into UniformStorage (also the
932 * glGetActiveUniformsiv uniform index), and stores the referenced
933 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
934 * return values can be encoded into a uniform location for
935 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
938 _mesa_get_uniform_location(struct gl_context
*ctx
,
939 struct gl_shader_program
*shProg
,
941 unsigned *out_offset
)
943 const size_t len
= strlen(name
);
948 /* If the name ends with a ']', assume that it refers to some element of an
949 * array. Malformed array references will fail the hash table look up
950 * below, so it doesn't matter that they are not caught here. This code
951 * only wants to catch the "leaf" array references so that arrays of
952 * structures containing arrays will be handled correctly.
954 if (name
[len
-1] == ']') {
957 /* Walk backwards over the string looking for a non-digit character.
958 * This had better be the opening bracket for an array index.
960 * Initially, i specifies the location of the ']'. Since the string may
961 * contain only the ']' charcater, walk backwards very carefully.
963 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
966 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
968 * "The first element of a uniform array is identified using the
969 * name of the uniform array appended with "[0]". Except if the last
970 * part of the string name indicates a uniform array, then the
971 * location of the first element of that array can be retrieved by
972 * either using the name of the uniform array, or the name of the
973 * uniform array appended with "[0]"."
975 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
977 * "name must be a null terminated string, without white space."
979 * Return an error if there is no opening '[' to match the closing ']'.
980 * An error will also be returned if there is intervening white space
981 * (or other non-digit characters) before the opening '['.
983 if ((i
== 0) || name
[i
-1] != '[')
984 return GL_INVALID_INDEX
;
986 /* Return an error if there are no digits between the opening '[' to
987 * match the closing ']'.
990 return GL_INVALID_INDEX
;
992 /* Make a new string that is a copy of the old string up to (but not
993 * including) the '[' character.
995 name_copy
= (char *) malloc(i
);
996 memcpy(name_copy
, name
, i
- 1);
997 name_copy
[i
-1] = '\0';
999 offset
= strtol(&name
[i
], NULL
, 10);
1002 return GL_INVALID_INDEX
;
1005 array_lookup
= true;
1007 name_copy
= (char *) name
;
1009 array_lookup
= false;
1012 unsigned location
= 0;
1013 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
1016 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
1018 /* Free the temporary buffer *before* possibly returning an error.
1020 if (name_copy
!= name
)
1024 return GL_INVALID_INDEX
;
1026 /* If the uniform is an array, fail if the index is out of bounds.
1027 * (A negative index is caught above.) This also fails if the uniform
1028 * is not an array, but the user is trying to index it, because
1029 * array_elements is zero and offset >= 0.
1032 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
1033 return GL_INVALID_INDEX
;
1036 *out_offset
= offset
;
1041 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1042 char *errMsg
, size_t errMsgLength
)
1044 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1046 memset(unit_types
, 0, sizeof(unit_types
));
1048 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1049 const struct gl_uniform_storage
*const storage
=
1050 &shProg
->UniformStorage
[i
];
1051 const glsl_type
*const t
= (storage
->type
->is_array())
1052 ? storage
->type
->fields
.array
: storage
->type
;
1054 if (!t
->is_sampler())
1057 const unsigned count
= MAX2(1, storage
->type
->array_size());
1058 for (unsigned j
= 0; j
< count
; j
++) {
1059 const unsigned unit
= storage
->storage
[j
].i
;
1061 /* The types of the samplers associated with a particular texture
1062 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1063 * OpenGL 3.3 core spec says:
1065 * "It is not allowed to have variables of different sampler
1066 * types pointing to the same texture image unit within a program
1069 if (unit_types
[unit
] == NULL
) {
1070 unit_types
[unit
] = t
;
1071 } else if (unit_types
[unit
] != t
) {
1072 _mesa_snprintf(errMsg
, errMsgLength
,
1073 "Texture unit %d is accessed both as %s and %s",
1074 unit
, unit_types
[unit
]->name
, t
->name
);