2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010, 2011 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
29 #include "main/core.h"
30 #include "main/context.h"
32 #include "ir_uniform.h"
33 #include "program/hash_table.h"
34 #include "../glsl/program.h"
35 #include "../glsl/ir_uniform.h"
36 #include "../glsl/glsl_parser_extras.h"
37 #include "main/shaderapi.h"
38 #include "main/shaderobj.h"
42 extern "C" void GLAPIENTRY
43 _mesa_GetActiveUniform(GLuint program
, GLuint index
,
44 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
45 GLenum
*type
, GLcharARB
*nameOut
)
47 GET_CURRENT_CONTEXT(ctx
);
48 struct gl_shader_program
*shProg
=
49 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
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_get_uniform_name(uni
, maxLength
, length
, nameOut
);
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 "glGetActiveUniformsiv(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;
126 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
129 * "If the active uniform is an array, the uniform name returned
130 * in name will always be the name of the uniform array appended
133 if (uni
->array_elements
!= 0)
137 case GL_UNIFORM_BLOCK_INDEX
:
138 params
[i
] = uni
->block_index
;
141 case GL_UNIFORM_OFFSET
:
142 params
[i
] = uni
->offset
;
145 case GL_UNIFORM_ARRAY_STRIDE
:
146 params
[i
] = uni
->array_stride
;
149 case GL_UNIFORM_MATRIX_STRIDE
:
150 params
[i
] = uni
->matrix_stride
;
153 case GL_UNIFORM_IS_ROW_MAJOR
:
154 params
[i
] = uni
->row_major
;
157 case GL_UNIFORM_ATOMIC_COUNTER_BUFFER_INDEX
:
158 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
160 params
[i
] = uni
->atomic_buffer_index
;
171 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetActiveUniformsiv(pname)");
174 static struct gl_uniform_storage
*
175 validate_uniform_parameters(struct gl_context
*ctx
,
176 struct gl_shader_program
*shProg
,
177 GLint location
, GLsizei count
,
178 unsigned *array_index
,
181 if (shProg
== NULL
) {
182 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
186 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
188 * "If a negative number is provided where an argument of type sizei or
189 * sizeiptr is specified, the error INVALID_VALUE is generated."
192 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
196 /* Check that the given location is in bounds of uniform remap table.
197 * Unlinked programs will have NumUniformRemapTable == 0, so we can take
198 * the shProg->LinkStatus check out of the main path.
200 if (unlikely(location
>= (GLint
) shProg
->NumUniformRemapTable
)) {
201 if (!shProg
->LinkStatus
)
202 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)",
205 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
211 if (location
== -1) {
212 if (!shProg
->LinkStatus
)
213 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)",
219 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
221 * "If any of the following conditions occur, an INVALID_OPERATION
222 * error is generated by the Uniform* commands, and no uniform values
227 * - if no variable with a location of location exists in the
228 * program object currently in use and location is not -1,
229 * - if count is greater than one, and the uniform declared in the
230 * shader is not an array variable,
232 if (location
< -1 || !shProg
->UniformRemapTable
[location
]) {
233 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
238 /* If the driver storage pointer in remap table is -1, we ignore silently.
240 * GL_ARB_explicit_uniform_location spec says:
241 * "What happens if Uniform* is called with an explicitly defined
242 * uniform location, but that uniform is deemed inactive by the
245 * RESOLVED: The call is ignored for inactive uniform variables and
246 * no error is generated."
249 if (shProg
->UniformRemapTable
[location
] ==
250 INACTIVE_UNIFORM_EXPLICIT_LOCATION
)
253 struct gl_uniform_storage
*const uni
= shProg
->UniformRemapTable
[location
];
255 if (uni
->array_elements
== 0 && count
> 1) {
256 _mesa_error(ctx
, GL_INVALID_OPERATION
,
257 "%s(count > 1 for non-array, location=%d)",
262 /* The array index specified by the uniform location is just the uniform
263 * location minus the base location of of the uniform.
265 *array_index
= location
- uni
->remap_location
;
267 /* If the uniform is an array, check that array_index is in bounds.
268 * If not an array, check that array_index is zero.
269 * array_index is unsigned so no need to check for less than zero.
271 const unsigned limit
= MAX2(uni
->array_elements
, 1);
272 if (*array_index
>= limit
) {
273 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
281 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
284 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
285 GLsizei bufSize
, enum glsl_base_type returnType
,
288 struct gl_shader_program
*shProg
=
289 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
292 struct gl_uniform_storage
*const uni
=
293 validate_uniform_parameters(ctx
, shProg
, location
, 1,
294 &offset
, "glGetUniform");
296 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
299 * "The error INVALID_OPERATION is generated if program has not been
300 * linked successfully, or if location is not a valid location for
303 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
306 * "If the value of location is -1, the Uniform* commands will
307 * silently ignore the data passed in, and the current uniform
308 * values will not be changed."
310 * Allowing -1 for the location parameter of glUniform allows
311 * applications to avoid error paths in the case that, for example, some
312 * uniform variable is removed by the compiler / linker after
313 * optimization. In this case, the new value of the uniform is dropped
314 * on the floor. For the case of glGetUniform, there is nothing
315 * sensible to do for a location of -1.
317 * If the location was -1, validate_unfirom_parameters will return NULL
318 * without raising an error. Raise the error here.
320 if (location
== -1) {
321 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniform(location=%d)",
329 unsigned elements
= (uni
->type
->is_sampler())
330 ? 1 : uni
->type
->components();
332 /* Calculate the source base address *BEFORE* modifying elements to
333 * account for the size of the user's buffer.
335 const union gl_constant_value
*const src
=
336 &uni
->storage
[offset
* elements
];
338 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
339 returnType
== GLSL_TYPE_UINT
);
340 /* The three (currently) supported types all have the same size,
341 * which is of course the same as their union. That'll change
342 * with glGetUniformdv()...
344 unsigned bytes
= sizeof(src
[0]) * elements
;
345 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
346 _mesa_error( ctx
, GL_INVALID_OPERATION
,
347 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
348 " but %u bytes are required)", bufSize
, bytes
);
352 /* If the return type and the uniform's native type are "compatible,"
353 * just memcpy the data. If the types are not compatible, perform a
354 * slower convert-and-copy process.
356 if (returnType
== uni
->type
->base_type
357 || ((returnType
== GLSL_TYPE_INT
358 || returnType
== GLSL_TYPE_UINT
)
360 (uni
->type
->base_type
== GLSL_TYPE_INT
361 || uni
->type
->base_type
== GLSL_TYPE_UINT
362 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
363 memcpy(paramsOut
, src
, bytes
);
365 union gl_constant_value
*const dst
=
366 (union gl_constant_value
*) paramsOut
;
368 /* This code could be optimized by putting the loop inside the switch
369 * statements. However, this is not expected to be
370 * performance-critical code.
372 for (unsigned i
= 0; i
< elements
; i
++) {
373 switch (returnType
) {
374 case GLSL_TYPE_FLOAT
:
375 switch (uni
->type
->base_type
) {
377 dst
[i
].f
= (float) src
[i
].u
;
380 case GLSL_TYPE_SAMPLER
:
381 dst
[i
].f
= (float) src
[i
].i
;
384 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
387 assert(!"Should not get here.");
394 switch (uni
->type
->base_type
) {
395 case GLSL_TYPE_FLOAT
:
396 /* While the GL 3.2 core spec doesn't explicitly
397 * state how conversion of float uniforms to integer
398 * values works, in section 6.2 "State Tables" on
401 * "Unless otherwise specified, when floating
402 * point state is returned as integer values or
403 * integer state is returned as floating-point
404 * values it is converted in the fashion
405 * described in section 6.1.2"
407 * That section, on page 248, says:
409 * "If GetIntegerv or GetInteger64v are called,
410 * a floating-point value is rounded to the
411 * nearest integer..."
413 dst
[i
].i
= IROUND(src
[i
].f
);
416 dst
[i
].i
= src
[i
].i
? 1 : 0;
419 assert(!"Should not get here.");
425 assert(!"Should not get here.");
434 log_uniform(const void *values
, enum glsl_base_type basicType
,
435 unsigned rows
, unsigned cols
, unsigned count
,
437 const struct gl_shader_program
*shProg
,
439 const struct gl_uniform_storage
*uni
)
442 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
443 const unsigned elems
= rows
* cols
* count
;
444 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
446 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
447 "transpose = %s) to: ",
448 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
449 transpose
? "true" : "false");
450 for (unsigned i
= 0; i
< elems
; i
++) {
451 if (i
!= 0 && ((i
% rows
) == 0))
456 printf("%u ", v
[i
].u
);
459 printf("%d ", v
[i
].i
);
461 case GLSL_TYPE_FLOAT
:
462 printf("%g ", v
[i
].f
);
465 assert(!"Should not get here.");
475 log_program_parameters(const struct gl_shader_program
*shProg
)
477 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
478 if (shProg
->_LinkedShaders
[i
] == NULL
)
481 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
483 printf("Program %d %s shader parameters:\n",
484 shProg
->Name
, _mesa_shader_stage_to_string(i
));
485 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
486 printf("%s: %p %f %f %f %f\n",
487 prog
->Parameters
->Parameters
[j
].Name
,
488 prog
->Parameters
->ParameterValues
[j
],
489 prog
->Parameters
->ParameterValues
[j
][0].f
,
490 prog
->Parameters
->ParameterValues
[j
][1].f
,
491 prog
->Parameters
->ParameterValues
[j
][2].f
,
492 prog
->Parameters
->ParameterValues
[j
][3].f
);
500 * Propagate some values from uniform backing storage to driver storage
502 * Values propagated from uniform backing storage to driver storage
503 * have all format / type conversions previously requested by the
504 * driver applied. This function is most often called by the
505 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
508 * \param uni Uniform whose data is to be propagated to driver storage
509 * \param array_index If \c uni is an array, this is the element of
510 * the array to be propagated.
511 * \param count Number of array elements to propagate.
514 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
515 unsigned array_index
,
520 /* vector_elements and matrix_columns can be 0 for samplers.
522 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
523 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
525 /* Store the data in the driver's requested type in the driver's storage
528 unsigned src_vector_byte_stride
= components
* 4;
530 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
531 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
532 uint8_t *dst
= (uint8_t *) store
->data
;
533 const unsigned extra_stride
=
534 store
->element_stride
- (vectors
* store
->vector_stride
);
536 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
539 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
541 __func__
, dst
, array_index
, components
,
542 vectors
, count
, store
->vector_stride
, extra_stride
);
545 dst
+= array_index
* store
->element_stride
;
547 switch (store
->format
) {
548 case uniform_native
: {
552 for (j
= 0; j
< count
; j
++) {
553 for (v
= 0; v
< vectors
; v
++) {
554 memcpy(dst
, src
, src_vector_byte_stride
);
555 src
+= src_vector_byte_stride
;
556 dst
+= store
->vector_stride
;
564 case uniform_int_float
: {
565 const int *isrc
= (const int *) src
;
570 for (j
= 0; j
< count
; j
++) {
571 for (v
= 0; v
< vectors
; v
++) {
572 for (c
= 0; c
< components
; c
++) {
573 ((float *) dst
)[c
] = (float) *isrc
;
577 dst
+= store
->vector_stride
;
586 assert(!"Should not get here.");
593 * Called via glUniform*() functions.
596 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
597 GLint location
, GLsizei count
,
598 const GLvoid
*values
,
599 enum glsl_base_type basicType
,
600 unsigned src_components
)
604 struct gl_uniform_storage
*const uni
=
605 validate_uniform_parameters(ctx
, shProg
, location
, count
,
606 &offset
, "glUniform");
610 /* Verify that the types are compatible.
612 const unsigned components
= uni
->type
->is_sampler()
613 ? 1 : uni
->type
->vector_elements
;
616 switch (uni
->type
->base_type
) {
620 case GLSL_TYPE_SAMPLER
:
621 case GLSL_TYPE_IMAGE
:
622 match
= (basicType
== GLSL_TYPE_INT
);
625 match
= (basicType
== uni
->type
->base_type
);
629 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
630 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
634 if (ctx
->_Shader
->Flags
& GLSL_UNIFORMS
) {
635 log_uniform(values
, basicType
, components
, 1, count
,
636 false, shProg
, location
, uni
);
639 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
641 * "Setting a sampler's value to i selects texture image unit number
642 * i. The values of i range from zero to the implementation- dependent
643 * maximum supported number of texture image units."
645 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
648 * "Error Description Offending command
651 * INVALID_VALUE Numeric argument out of range Yes"
653 * Based on that, when an invalid sampler is specified, we generate a
654 * GL_INVALID_VALUE error and ignore the command.
656 if (uni
->type
->is_sampler()) {
657 for (int i
= 0; i
< count
; i
++) {
658 const unsigned texUnit
= ((unsigned *) values
)[i
];
660 /* check that the sampler (tex unit index) is legal */
661 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
662 _mesa_error(ctx
, GL_INVALID_VALUE
,
663 "glUniform1i(invalid sampler/tex unit index for "
671 if (uni
->type
->is_image()) {
672 for (int i
= 0; i
< count
; i
++) {
673 const int unit
= ((GLint
*) values
)[i
];
675 /* check that the image unit is legal */
676 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
677 _mesa_error(ctx
, GL_INVALID_VALUE
,
678 "glUniform1i(invalid image unit index for uniform %d)",
685 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
687 * "When loading N elements starting at an arbitrary position k in a
688 * uniform declared as an array, elements k through k + N - 1 in the
689 * array will be replaced with the new values. Values for any array
690 * element that exceeds the highest array element index used, as
691 * reported by GetActiveUniform, will be ignored by the GL."
693 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
694 * will have already generated an error.
696 if (uni
->array_elements
!= 0) {
697 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
700 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
702 /* Store the data in the "actual type" backing storage for the uniform.
704 if (!uni
->type
->is_boolean()) {
705 memcpy(&uni
->storage
[components
* offset
], values
,
706 sizeof(uni
->storage
[0]) * components
* count
);
708 const union gl_constant_value
*src
=
709 (const union gl_constant_value
*) values
;
710 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
711 const unsigned elems
= components
* count
;
713 for (unsigned i
= 0; i
< elems
; i
++) {
714 if (basicType
== GLSL_TYPE_FLOAT
) {
715 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
717 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
722 uni
->initialized
= true;
724 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
726 /* If the uniform is a sampler, do the extra magic necessary to propagate
727 * the changes through.
729 if (uni
->type
->is_sampler()) {
730 bool flushed
= false;
731 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
732 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
734 /* If the shader stage doesn't use the sampler uniform, skip this.
736 if (sh
== NULL
|| !uni
->sampler
[i
].active
)
739 for (int j
= 0; j
< count
; j
++) {
740 sh
->SamplerUnits
[uni
->sampler
[i
].index
+ offset
+ j
] =
741 ((unsigned *) values
)[j
];
744 struct gl_program
*const prog
= sh
->Program
;
746 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
748 /* Determine if any of the samplers used by this shader stage have
751 bool changed
= false;
752 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
753 if ((sh
->active_samplers
& (1U << j
)) != 0
754 && (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
])) {
762 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
766 memcpy(prog
->SamplerUnits
,
768 sizeof(sh
->SamplerUnits
));
770 _mesa_update_shader_textures_used(shProg
, prog
);
771 if (ctx
->Driver
.SamplerUniformChange
)
772 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
777 /* If the uniform is an image, update the mapping from image
778 * uniforms to image units present in the shader data structure.
780 if (uni
->type
->is_image()) {
781 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
782 if (uni
->image
[i
].active
) {
783 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
785 for (int j
= 0; j
< count
; j
++)
786 sh
->ImageUnits
[uni
->image
[i
].index
+ offset
+ j
] =
787 ((GLint
*) values
)[j
];
791 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
796 * Called by glUniformMatrix*() functions.
797 * Note: cols=2, rows=4 ==> array[2] of vec4
800 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
801 GLuint cols
, GLuint rows
,
802 GLint location
, GLsizei count
,
803 GLboolean transpose
, const GLfloat
*values
)
810 struct gl_uniform_storage
*const uni
=
811 validate_uniform_parameters(ctx
, shProg
, location
, count
,
812 &offset
, "glUniformMatrix");
816 if (!uni
->type
->is_matrix()) {
817 _mesa_error(ctx
, GL_INVALID_OPERATION
,
818 "glUniformMatrix(non-matrix uniform)");
822 assert(!uni
->type
->is_sampler());
823 vectors
= uni
->type
->matrix_columns
;
824 components
= uni
->type
->vector_elements
;
826 /* Verify that the types are compatible. This is greatly simplified for
827 * matrices because they can only have a float base type.
829 if (vectors
!= cols
|| components
!= rows
) {
830 _mesa_error(ctx
, GL_INVALID_OPERATION
,
831 "glUniformMatrix(matrix size mismatch)");
835 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
836 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
837 if (ctx
->API
== API_OPENGLES
838 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)) {
840 _mesa_error(ctx
, GL_INVALID_VALUE
,
841 "glUniformMatrix(matrix transpose is not GL_FALSE)");
846 if (ctx
->_Shader
->Flags
& GLSL_UNIFORMS
) {
847 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
848 bool(transpose
), shProg
, location
, uni
);
851 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
853 * "When loading N elements starting at an arbitrary position k in a
854 * uniform declared as an array, elements k through k + N - 1 in the
855 * array will be replaced with the new values. Values for any array
856 * element that exceeds the highest array element index used, as
857 * reported by GetActiveUniform, will be ignored by the GL."
859 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
860 * will have already generated an error.
862 if (uni
->array_elements
!= 0) {
863 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
866 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
868 /* Store the data in the "actual type" backing storage for the uniform.
870 elements
= components
* vectors
;
873 memcpy(&uni
->storage
[elements
* offset
], values
,
874 sizeof(uni
->storage
[0]) * elements
* count
);
876 /* Copy and transpose the matrix.
878 const float *src
= values
;
879 float *dst
= &uni
->storage
[elements
* offset
].f
;
881 for (int i
= 0; i
< count
; i
++) {
882 for (unsigned r
= 0; r
< rows
; r
++) {
883 for (unsigned c
= 0; c
< cols
; c
++) {
884 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
893 uni
->initialized
= true;
895 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
900 * Called via glGetUniformLocation().
902 * Returns the uniform index into UniformStorage (also the
903 * glGetActiveUniformsiv uniform index), and stores the referenced
904 * array offset in *offset, or GL_INVALID_INDEX (-1).
907 _mesa_get_uniform_location(struct gl_shader_program
*shProg
,
909 unsigned *out_offset
)
911 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
913 * "The first element of a uniform array is identified using the
914 * name of the uniform array appended with "[0]". Except if the last
915 * part of the string name indicates a uniform array, then the
916 * location of the first element of that array can be retrieved by
917 * either using the name of the uniform array, or the name of the
918 * uniform array appended with "[0]"."
920 * Note: since uniform names are not allowed to use whitespace, and array
921 * indices within uniform names are not allowed to use "+", "-", or leading
922 * zeros, it follows that each uniform has a unique name up to the possible
923 * ambiguity with "[0]" noted above. Therefore we don't need to worry
924 * about mal-formed inputs--they will properly fail when we try to look up
925 * the uniform name in shProg->UniformHash.
928 const GLchar
*base_name_end
;
929 long offset
= parse_program_resource_name(name
, &base_name_end
);
930 bool array_lookup
= offset
>= 0;
934 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
935 memcpy(name_copy
, name
, base_name_end
- name
);
936 name_copy
[base_name_end
- name
] = '\0';
938 name_copy
= (char *) name
;
942 unsigned location
= 0;
943 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
946 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
948 /* Free the temporary buffer *before* possibly returning an error.
950 if (name_copy
!= name
)
954 return GL_INVALID_INDEX
;
956 /* If the uniform is an array, fail if the index is out of bounds.
957 * (A negative index is caught above.) This also fails if the uniform
958 * is not an array, but the user is trying to index it, because
959 * array_elements is zero and offset >= 0.
962 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
963 return GL_INVALID_INDEX
;
966 *out_offset
= offset
;
971 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
972 char *errMsg
, size_t errMsgLength
)
974 /* Shader does not have samplers. */
975 if (shProg
->NumUserUniformStorage
== 0)
978 if (!shProg
->SamplersValidated
) {
979 _mesa_snprintf(errMsg
, errMsgLength
,
980 "active samplers with a different type "
981 "refer to the same texture image unit");
988 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
990 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
991 * OpenGL 4.1 spec says:
993 * "[INVALID_OPERATION] is generated by any command that transfers
994 * vertices to the GL if:
998 * - Any two active samplers in the current program object are of
999 * different types, but refer to the same texture image unit.
1001 * - The number of active samplers in the program exceeds the
1002 * maximum number of texture image units allowed."
1004 unsigned active_samplers
= 0;
1005 const struct gl_shader_program
**shProg
=
1006 (const struct gl_shader_program
**) pipeline
->CurrentProgram
;
1008 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1009 memset(unit_types
, 0, sizeof(unit_types
));
1011 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1015 for (unsigned i
= 0; i
< shProg
[idx
]->NumUserUniformStorage
; i
++) {
1016 const struct gl_uniform_storage
*const storage
=
1017 &shProg
[idx
]->UniformStorage
[i
];
1018 const glsl_type
*const t
= (storage
->type
->is_array())
1019 ? storage
->type
->fields
.array
: storage
->type
;
1021 if (!t
->is_sampler())
1026 const unsigned count
= MAX2(1, storage
->type
->array_size());
1027 for (unsigned j
= 0; j
< count
; j
++) {
1028 const unsigned unit
= storage
->storage
[j
].i
;
1030 /* The types of the samplers associated with a particular texture
1031 * unit must be an exact match. Page 74 (page 89 of the PDF) of
1032 * the OpenGL 3.3 core spec says:
1034 * "It is not allowed to have variables of different sampler
1035 * types pointing to the same texture image unit within a
1038 if (unit_types
[unit
] == NULL
) {
1039 unit_types
[unit
] = t
;
1040 } else if (unit_types
[unit
] != t
) {
1042 ralloc_asprintf(pipeline
,
1043 "Texture unit %d is accessed both as %s "
1045 unit
, unit_types
[unit
]->name
, t
->name
);
1052 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1054 ralloc_asprintf(pipeline
,
1055 "the number of active samplers %d exceed the "
1057 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);