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 /* This case should be impossible. The implication is that a call like
245 * glGetUniformLocation(prog, "foo[8]") was successful but "foo" is not an
248 if (*array_index
!= 0 && shProg
->UniformStorage
[*loc
].array_elements
== 0) {
249 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
257 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
260 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
261 GLsizei bufSize
, enum glsl_base_type returnType
,
264 struct gl_shader_program
*shProg
=
265 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
266 struct gl_uniform_storage
*uni
;
267 unsigned loc
, offset
;
269 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
270 &loc
, &offset
, "glGetUniform", true))
273 uni
= &shProg
->UniformStorage
[loc
];
276 unsigned elements
= (uni
->type
->is_sampler())
277 ? 1 : uni
->type
->components();
279 /* Calculate the source base address *BEFORE* modifying elements to
280 * account for the size of the user's buffer.
282 const union gl_constant_value
*const src
=
283 &uni
->storage
[offset
* elements
];
285 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
286 returnType
== GLSL_TYPE_UINT
);
287 /* The three (currently) supported types all have the same size,
288 * which is of course the same as their union. That'll change
289 * with glGetUniformdv()...
291 unsigned bytes
= sizeof(src
[0]) * elements
;
292 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
293 _mesa_error( ctx
, GL_INVALID_OPERATION
,
294 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
295 " but %u bytes are required)", bufSize
, bytes
);
299 /* If the return type and the uniform's native type are "compatible,"
300 * just memcpy the data. If the types are not compatible, perform a
301 * slower convert-and-copy process.
303 if (returnType
== uni
->type
->base_type
304 || ((returnType
== GLSL_TYPE_INT
305 || returnType
== GLSL_TYPE_UINT
306 || returnType
== GLSL_TYPE_SAMPLER
)
308 (uni
->type
->base_type
== GLSL_TYPE_INT
309 || uni
->type
->base_type
== GLSL_TYPE_UINT
310 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
311 memcpy(paramsOut
, src
, bytes
);
313 union gl_constant_value
*const dst
=
314 (union gl_constant_value
*) paramsOut
;
316 /* This code could be optimized by putting the loop inside the switch
317 * statements. However, this is not expected to be
318 * performance-critical code.
320 for (unsigned i
= 0; i
< elements
; i
++) {
321 switch (returnType
) {
322 case GLSL_TYPE_FLOAT
:
323 switch (uni
->type
->base_type
) {
325 dst
[i
].f
= (float) src
[i
].u
;
328 case GLSL_TYPE_SAMPLER
:
329 dst
[i
].f
= (float) src
[i
].i
;
332 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
335 assert(!"Should not get here.");
342 switch (uni
->type
->base_type
) {
343 case GLSL_TYPE_FLOAT
:
344 /* While the GL 3.2 core spec doesn't explicitly
345 * state how conversion of float uniforms to integer
346 * values works, in section 6.2 "State Tables" on
349 * "Unless otherwise specified, when floating
350 * point state is returned as integer values or
351 * integer state is returned as floating-point
352 * values it is converted in the fashion
353 * described in section 6.1.2"
355 * That section, on page 248, says:
357 * "If GetIntegerv or GetInteger64v are called,
358 * a floating-point value is rounded to the
359 * nearest integer..."
361 dst
[i
].i
= IROUND(src
[i
].f
);
364 dst
[i
].i
= src
[i
].i
? 1 : 0;
367 assert(!"Should not get here.");
373 assert(!"Should not get here.");
382 log_uniform(const void *values
, enum glsl_base_type basicType
,
383 unsigned rows
, unsigned cols
, unsigned count
,
385 const struct gl_shader_program
*shProg
,
387 const struct gl_uniform_storage
*uni
)
390 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
391 const unsigned elems
= rows
* cols
* count
;
392 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
394 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
395 "transpose = %s) to: ",
396 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
397 transpose
? "true" : "false");
398 for (unsigned i
= 0; i
< elems
; i
++) {
399 if (i
!= 0 && ((i
% rows
) == 0))
404 printf("%u ", v
[i
].u
);
407 printf("%d ", v
[i
].i
);
409 case GLSL_TYPE_FLOAT
:
410 printf("%g ", v
[i
].f
);
413 assert(!"Should not get here.");
423 log_program_parameters(const struct gl_shader_program
*shProg
)
425 static const char *stages
[] = {
426 "vertex", "fragment", "geometry"
429 assert(Elements(stages
) == MESA_SHADER_TYPES
);
431 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
432 if (shProg
->_LinkedShaders
[i
] == NULL
)
435 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
437 printf("Program %d %s shader parameters:\n",
438 shProg
->Name
, stages
[i
]);
439 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
440 printf("%s: %p %f %f %f %f\n",
441 prog
->Parameters
->Parameters
[j
].Name
,
442 prog
->Parameters
->ParameterValues
[j
],
443 prog
->Parameters
->ParameterValues
[j
][0].f
,
444 prog
->Parameters
->ParameterValues
[j
][1].f
,
445 prog
->Parameters
->ParameterValues
[j
][2].f
,
446 prog
->Parameters
->ParameterValues
[j
][3].f
);
454 * Propagate some values from uniform backing storage to driver storage
456 * Values propagated from uniform backing storage to driver storage
457 * have all format / type conversions previously requested by the
458 * driver applied. This function is most often called by the
459 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
462 * \param uni Uniform whose data is to be propagated to driver storage
463 * \param array_index If \c uni is an array, this is the element of
464 * the array to be propagated.
465 * \param count Number of array elements to propagate.
468 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
469 unsigned array_index
,
474 /* vector_elements and matrix_columns can be 0 for samplers.
476 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
477 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
479 /* Store the data in the driver's requested type in the driver's storage
482 unsigned src_vector_byte_stride
= components
* 4;
484 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
485 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
486 uint8_t *dst
= (uint8_t *) store
->data
;
487 const unsigned extra_stride
=
488 store
->element_stride
- (vectors
* store
->vector_stride
);
490 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
493 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
495 __func__
, dst
, array_index
, components
,
496 vectors
, count
, store
->vector_stride
, extra_stride
);
499 dst
+= array_index
* store
->element_stride
;
501 switch (store
->format
) {
503 case uniform_bool_int_0_1
: {
507 for (j
= 0; j
< count
; j
++) {
508 for (v
= 0; v
< vectors
; v
++) {
509 memcpy(dst
, src
, src_vector_byte_stride
);
510 src
+= src_vector_byte_stride
;
511 dst
+= store
->vector_stride
;
519 case uniform_int_float
:
520 case uniform_bool_float
: {
521 const int *isrc
= (const int *) src
;
526 for (j
= 0; j
< count
; j
++) {
527 for (v
= 0; v
< vectors
; v
++) {
528 for (c
= 0; c
< components
; c
++) {
529 ((float *) dst
)[c
] = (float) *isrc
;
533 dst
+= store
->vector_stride
;
541 case uniform_bool_int_0_not0
: {
542 const int *isrc
= (const int *) src
;
547 for (j
= 0; j
< count
; j
++) {
548 for (v
= 0; v
< vectors
; v
++) {
549 for (c
= 0; c
< components
; c
++) {
550 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
554 dst
+= store
->vector_stride
;
563 assert(!"Should not get here.");
570 * Called via glUniform*() functions.
573 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
574 GLint location
, GLsizei count
,
575 const GLvoid
*values
, GLenum type
)
577 unsigned loc
, offset
;
579 unsigned src_components
;
580 enum glsl_base_type basicType
;
581 struct gl_uniform_storage
*uni
;
583 ASSERT_OUTSIDE_BEGIN_END(ctx
);
585 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
586 &loc
, &offset
, "glUniform", false))
589 uni
= &shProg
->UniformStorage
[loc
];
591 /* Verify that the types are compatible.
595 basicType
= GLSL_TYPE_FLOAT
;
599 basicType
= GLSL_TYPE_FLOAT
;
603 basicType
= GLSL_TYPE_FLOAT
;
607 basicType
= GLSL_TYPE_FLOAT
;
610 case GL_UNSIGNED_INT
:
611 basicType
= GLSL_TYPE_UINT
;
614 case GL_UNSIGNED_INT_VEC2
:
615 basicType
= GLSL_TYPE_UINT
;
618 case GL_UNSIGNED_INT_VEC3
:
619 basicType
= GLSL_TYPE_UINT
;
622 case GL_UNSIGNED_INT_VEC4
:
623 basicType
= GLSL_TYPE_UINT
;
627 basicType
= GLSL_TYPE_INT
;
631 basicType
= GLSL_TYPE_INT
;
635 basicType
= GLSL_TYPE_INT
;
639 basicType
= GLSL_TYPE_INT
;
647 case GL_FLOAT_MAT2x3
:
648 case GL_FLOAT_MAT2x4
:
649 case GL_FLOAT_MAT3x2
:
651 case GL_FLOAT_MAT3x4
:
652 case GL_FLOAT_MAT4x2
:
653 case GL_FLOAT_MAT4x3
:
656 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
660 if (uni
->type
->is_sampler()) {
663 components
= uni
->type
->vector_elements
;
667 switch (uni
->type
->base_type
) {
671 case GLSL_TYPE_SAMPLER
:
672 match
= (basicType
== GLSL_TYPE_INT
);
675 match
= (basicType
== uni
->type
->base_type
);
679 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
680 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
684 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
685 log_uniform(values
, basicType
, components
, 1, count
,
686 false, shProg
, location
, uni
);
689 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
691 * "Setting a sampler's value to i selects texture image unit number
692 * i. The values of i range from zero to the implementation- dependent
693 * maximum supported number of texture image units."
695 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
698 * "Error Description Offending command
701 * INVALID_VALUE Numeric argument out of range Yes"
703 * Based on that, when an invalid sampler is specified, we generate a
704 * GL_INVALID_VALUE error and ignore the command.
706 if (uni
->type
->is_sampler()) {
709 for (i
= 0; i
< count
; i
++) {
710 const unsigned texUnit
= ((unsigned *) values
)[i
];
712 /* check that the sampler (tex unit index) is legal */
713 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
714 _mesa_error(ctx
, GL_INVALID_VALUE
,
715 "glUniform1i(invalid sampler/tex unit index for "
723 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
725 * "When loading N elements starting at an arbitrary position k in a
726 * uniform declared as an array, elements k through k + N - 1 in the
727 * array will be replaced with the new values. Values for any array
728 * element that exceeds the highest array element index used, as
729 * reported by GetActiveUniform, will be ignored by the GL."
731 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
732 * will have already generated an error.
734 if (uni
->array_elements
!= 0) {
735 if (offset
>= uni
->array_elements
)
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
|| ctx
->API
== API_OPENGLES2
) {
869 _mesa_error(ctx
, GL_INVALID_VALUE
,
870 "glUniformMatrix(matrix transpose is not GL_FALSE)");
875 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
876 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
877 bool(transpose
), shProg
, location
, uni
);
880 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
882 * "When loading N elements starting at an arbitrary position k in a
883 * uniform declared as an array, elements k through k + N - 1 in the
884 * array will be replaced with the new values. Values for any array
885 * element that exceeds the highest array element index used, as
886 * reported by GetActiveUniform, will be ignored by the GL."
888 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
889 * will have already generated an error.
891 if (uni
->array_elements
!= 0) {
892 if (offset
>= uni
->array_elements
)
895 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
898 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
900 /* Store the data in the "actual type" backing storage for the uniform.
902 elements
= components
* vectors
;
905 memcpy(&uni
->storage
[elements
* offset
], values
,
906 sizeof(uni
->storage
[0]) * elements
* count
);
908 /* Copy and transpose the matrix.
910 const float *src
= values
;
911 float *dst
= &uni
->storage
[elements
* offset
].f
;
913 for (int i
= 0; i
< count
; i
++) {
914 for (unsigned r
= 0; r
< rows
; r
++) {
915 for (unsigned c
= 0; c
< cols
; c
++) {
916 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
925 uni
->initialized
= true;
927 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
931 * Called via glGetUniformLocation().
933 * Returns the uniform index into UniformStorage (also the
934 * glGetActiveUniformsiv uniform index), and stores the referenced
935 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
936 * return values can be encoded into a uniform location for
937 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
940 _mesa_get_uniform_location(struct gl_context
*ctx
,
941 struct gl_shader_program
*shProg
,
943 unsigned *out_offset
)
945 const size_t len
= strlen(name
);
950 /* If the name ends with a ']', assume that it refers to some element of an
951 * array. Malformed array references will fail the hash table look up
952 * below, so it doesn't matter that they are not caught here. This code
953 * only wants to catch the "leaf" array references so that arrays of
954 * structures containing arrays will be handled correctly.
956 if (name
[len
-1] == ']') {
959 /* Walk backwards over the string looking for a non-digit character.
960 * This had better be the opening bracket for an array index.
962 * Initially, i specifies the location of the ']'. Since the string may
963 * contain only the ']' charcater, walk backwards very carefully.
965 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
968 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
970 * "The first element of a uniform array is identified using the
971 * name of the uniform array appended with "[0]". Except if the last
972 * part of the string name indicates a uniform array, then the
973 * location of the first element of that array can be retrieved by
974 * either using the name of the uniform array, or the name of the
975 * uniform array appended with "[0]"."
977 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
979 * "name must be a null terminated string, without white space."
981 * Return an error if there is no opening '[' to match the closing ']'.
982 * An error will also be returned if there is intervening white space
983 * (or other non-digit characters) before the opening '['.
985 if ((i
== 0) || name
[i
-1] != '[')
986 return GL_INVALID_INDEX
;
988 /* Return an error if there are no digits between the opening '[' to
989 * match the closing ']'.
992 return GL_INVALID_INDEX
;
994 /* Make a new string that is a copy of the old string up to (but not
995 * including) the '[' character.
997 name_copy
= (char *) malloc(i
);
998 memcpy(name_copy
, name
, i
- 1);
999 name_copy
[i
-1] = '\0';
1001 offset
= strtol(&name
[i
], NULL
, 10);
1004 return GL_INVALID_INDEX
;
1007 array_lookup
= true;
1009 name_copy
= (char *) name
;
1011 array_lookup
= false;
1014 unsigned location
= 0;
1015 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
1018 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
1020 /* Free the temporary buffer *before* possibly returning an error.
1022 if (name_copy
!= name
)
1026 return GL_INVALID_INDEX
;
1028 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
1029 * to (correctly) fail if 'a' is not an array.
1031 if (array_lookup
&& shProg
->UniformStorage
[location
].array_elements
== 0) {
1032 return GL_INVALID_INDEX
;
1035 *out_offset
= offset
;
1040 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1041 char *errMsg
, size_t errMsgLength
)
1043 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1045 memset(unit_types
, 0, sizeof(unit_types
));
1047 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1048 const struct gl_uniform_storage
*const storage
=
1049 &shProg
->UniformStorage
[i
];
1050 const glsl_type
*const t
= (storage
->type
->is_array())
1051 ? storage
->type
->fields
.array
: storage
->type
;
1053 if (!t
->is_sampler())
1056 const unsigned count
= MAX2(1, storage
->type
->array_size());
1057 for (unsigned j
= 0; j
< count
; j
++) {
1058 const unsigned unit
= storage
->storage
[j
].i
;
1060 /* The types of the samplers associated with a particular texture
1061 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1062 * OpenGL 3.3 core spec says:
1064 * "It is not allowed to have variables of different sampler
1065 * types pointing to the same texture image unit within a program
1068 if (unit_types
[unit
] == NULL
) {
1069 unit_types
[unit
] = t
;
1070 } else if (unit_types
[unit
] != t
) {
1071 _mesa_snprintf(errMsg
, errMsgLength
,
1072 "Texture unit %d is accessed both as %s and %s",
1073 unit
, unit_types
[unit
]->name
, t
->name
);