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"
31 #include "main/shaderapi.h"
32 #include "main/shaderobj.h"
33 #include "main/uniforms.h"
34 #include "compiler/glsl/ir.h"
35 #include "compiler/glsl/ir_uniform.h"
36 #include "compiler/glsl/glsl_parser_extras.h"
37 #include "compiler/glsl/program.h"
38 #include "util/bitscan.h"
41 extern "C" void GLAPIENTRY
42 _mesa_GetActiveUniform(GLuint program
, GLuint index
,
43 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
44 GLenum
*type
, GLcharARB
*nameOut
)
46 GET_CURRENT_CONTEXT(ctx
);
47 struct gl_shader_program
*shProg
;
48 struct gl_program_resource
*res
;
51 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(maxLength < 0)");
55 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
59 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
63 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
68 _mesa_get_program_resource_name(shProg
, GL_UNIFORM
, index
, maxLength
,
69 length
, nameOut
, "glGetActiveUniform");
71 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
72 res
, index
, GL_TYPE
, (GLint
*) type
,
73 "glGetActiveUniform");
75 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
76 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
77 "glGetActiveUniform");
81 resource_prop_from_uniform_prop(GLenum uni_prop
)
88 case GL_UNIFORM_NAME_LENGTH
:
89 return GL_NAME_LENGTH
;
90 case GL_UNIFORM_BLOCK_INDEX
:
91 return GL_BLOCK_INDEX
;
92 case GL_UNIFORM_OFFSET
:
94 case GL_UNIFORM_ARRAY_STRIDE
:
95 return GL_ARRAY_STRIDE
;
96 case GL_UNIFORM_MATRIX_STRIDE
:
97 return GL_MATRIX_STRIDE
;
98 case GL_UNIFORM_IS_ROW_MAJOR
:
99 return GL_IS_ROW_MAJOR
;
100 case GL_UNIFORM_ATOMIC_COUNTER_BUFFER_INDEX
:
101 return GL_ATOMIC_COUNTER_BUFFER_INDEX
;
107 extern "C" void GLAPIENTRY
108 _mesa_GetActiveUniformsiv(GLuint program
,
109 GLsizei uniformCount
,
110 const GLuint
*uniformIndices
,
114 GET_CURRENT_CONTEXT(ctx
);
115 struct gl_shader_program
*shProg
;
116 struct gl_program_resource
*res
;
119 if (uniformCount
< 0) {
120 _mesa_error(ctx
, GL_INVALID_VALUE
,
121 "glGetActiveUniformsiv(uniformCount < 0)");
125 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
129 res_prop
= resource_prop_from_uniform_prop(pname
);
131 /* We need to first verify that each entry exists as active uniform. If
132 * not, generate error and do not cause any other side effects.
134 * In the case of and error condition, Page 16 (section 2.3.1 Errors)
135 * of the OpenGL 4.5 spec says:
137 * "If the generating command modifies values through a pointer argu-
138 * ment, no change is made to these values."
140 for (int i
= 0; i
< uniformCount
; i
++) {
141 if (!_mesa_program_resource_find_index(shProg
, GL_UNIFORM
,
142 uniformIndices
[i
])) {
143 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
148 for (int i
= 0; i
< uniformCount
; i
++) {
149 res
= _mesa_program_resource_find_index(shProg
, GL_UNIFORM
,
151 if (!_mesa_program_resource_prop(shProg
, res
, uniformIndices
[i
],
152 res_prop
, ¶ms
[i
],
153 "glGetActiveUniformsiv"))
158 static struct gl_uniform_storage
*
159 validate_uniform_parameters(struct gl_context
*ctx
,
160 struct gl_shader_program
*shProg
,
161 GLint location
, GLsizei count
,
162 unsigned *array_index
,
165 if (shProg
== NULL
) {
166 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
170 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
172 * "If a negative number is provided where an argument of type sizei or
173 * sizeiptr is specified, the error INVALID_VALUE is generated."
176 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
180 /* Check that the given location is in bounds of uniform remap table.
181 * Unlinked programs will have NumUniformRemapTable == 0, so we can take
182 * the shProg->data->LinkStatus check out of the main path.
184 if (unlikely(location
>= (GLint
) shProg
->NumUniformRemapTable
)) {
185 if (!shProg
->data
->LinkStatus
)
186 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)",
189 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
195 if (location
== -1) {
196 if (!shProg
->data
->LinkStatus
)
197 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)",
203 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
205 * "If any of the following conditions occur, an INVALID_OPERATION
206 * error is generated by the Uniform* commands, and no uniform values
211 * - if no variable with a location of location exists in the
212 * program object currently in use and location is not -1,
213 * - if count is greater than one, and the uniform declared in the
214 * shader is not an array variable,
216 if (location
< -1 || !shProg
->UniformRemapTable
[location
]) {
217 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
222 /* If the driver storage pointer in remap table is -1, we ignore silently.
224 * GL_ARB_explicit_uniform_location spec says:
225 * "What happens if Uniform* is called with an explicitly defined
226 * uniform location, but that uniform is deemed inactive by the
229 * RESOLVED: The call is ignored for inactive uniform variables and
230 * no error is generated."
233 if (shProg
->UniformRemapTable
[location
] ==
234 INACTIVE_UNIFORM_EXPLICIT_LOCATION
)
237 struct gl_uniform_storage
*const uni
= shProg
->UniformRemapTable
[location
];
239 /* Even though no location is assigned to a built-in uniform and this
240 * function should already have returned NULL, this test makes it explicit
241 * that we are not allowing to update the value of a built-in.
246 if (uni
->array_elements
== 0) {
248 _mesa_error(ctx
, GL_INVALID_OPERATION
,
249 "%s(count = %u for non-array \"%s\"@%d)",
250 caller
, count
, uni
->name
, location
);
254 assert((location
- uni
->remap_location
) == 0);
257 /* The array index specified by the uniform location is just the uniform
258 * location minus the base location of of the uniform.
260 *array_index
= location
- uni
->remap_location
;
262 /* If the uniform is an array, check that array_index is in bounds.
263 * array_index is unsigned so no need to check for less than zero.
265 if (*array_index
>= uni
->array_elements
) {
266 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
275 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
278 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
279 GLsizei bufSize
, enum glsl_base_type returnType
,
282 struct gl_shader_program
*shProg
=
283 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
286 struct gl_uniform_storage
*const uni
=
287 validate_uniform_parameters(ctx
, shProg
, location
, 1,
288 &offset
, "glGetUniform");
290 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
293 * "The error INVALID_OPERATION is generated if program has not been
294 * linked successfully, or if location is not a valid location for
297 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
300 * "If the value of location is -1, the Uniform* commands will
301 * silently ignore the data passed in, and the current uniform
302 * values will not be changed."
304 * Allowing -1 for the location parameter of glUniform allows
305 * applications to avoid error paths in the case that, for example, some
306 * uniform variable is removed by the compiler / linker after
307 * optimization. In this case, the new value of the uniform is dropped
308 * on the floor. For the case of glGetUniform, there is nothing
309 * sensible to do for a location of -1.
311 * If the location was -1, validate_unfirom_parameters will return NULL
312 * without raising an error. Raise the error here.
314 if (location
== -1) {
315 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniform(location=%d)",
323 unsigned elements
= (uni
->type
->is_sampler())
324 ? 1 : uni
->type
->components();
325 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
326 const int rmul
= glsl_base_type_is_64bit(returnType
) ? 2 : 1;
328 /* Calculate the source base address *BEFORE* modifying elements to
329 * account for the size of the user's buffer.
331 const union gl_constant_value
*const src
=
332 &uni
->storage
[offset
* elements
* dmul
];
334 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
335 returnType
== GLSL_TYPE_UINT
|| returnType
== GLSL_TYPE_DOUBLE
);
337 /* doubles have a different size than the other 3 types */
338 unsigned bytes
= sizeof(src
[0]) * elements
* rmul
;
339 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
340 _mesa_error( ctx
, GL_INVALID_OPERATION
,
341 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
342 " but %u bytes are required)", bufSize
, bytes
);
346 /* If the return type and the uniform's native type are "compatible,"
347 * just memcpy the data. If the types are not compatible, perform a
348 * slower convert-and-copy process.
350 if (returnType
== uni
->type
->base_type
351 || ((returnType
== GLSL_TYPE_INT
352 || returnType
== GLSL_TYPE_UINT
)
354 (uni
->type
->base_type
== GLSL_TYPE_INT
355 || uni
->type
->base_type
== GLSL_TYPE_UINT
356 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
357 || uni
->type
->base_type
== GLSL_TYPE_IMAGE
))) {
358 memcpy(paramsOut
, src
, bytes
);
360 union gl_constant_value
*const dst
=
361 (union gl_constant_value
*) paramsOut
;
362 /* This code could be optimized by putting the loop inside the switch
363 * statements. However, this is not expected to be
364 * performance-critical code.
366 for (unsigned i
= 0; i
< elements
; i
++) {
370 switch (returnType
) {
371 case GLSL_TYPE_FLOAT
:
372 switch (uni
->type
->base_type
) {
374 dst
[didx
].f
= (float) src
[sidx
].u
;
377 case GLSL_TYPE_SAMPLER
:
378 case GLSL_TYPE_IMAGE
:
379 dst
[didx
].f
= (float) src
[sidx
].i
;
382 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
384 case GLSL_TYPE_DOUBLE
: {
386 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
391 assert(!"Should not get here.");
395 case GLSL_TYPE_DOUBLE
:
396 switch (uni
->type
->base_type
) {
397 case GLSL_TYPE_UINT
: {
398 double tmp
= src
[sidx
].u
;
399 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
403 case GLSL_TYPE_SAMPLER
:
404 case GLSL_TYPE_IMAGE
: {
405 double tmp
= src
[sidx
].i
;
406 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
409 case GLSL_TYPE_BOOL
: {
410 double tmp
= src
[sidx
].i
? 1.0 : 0.0;
411 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
414 case GLSL_TYPE_FLOAT
: {
415 double tmp
= src
[sidx
].f
;
416 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
420 assert(!"Should not get here.");
426 switch (uni
->type
->base_type
) {
427 case GLSL_TYPE_FLOAT
:
428 /* While the GL 3.2 core spec doesn't explicitly
429 * state how conversion of float uniforms to integer
430 * values works, in section 6.2 "State Tables" on
433 * "Unless otherwise specified, when floating
434 * point state is returned as integer values or
435 * integer state is returned as floating-point
436 * values it is converted in the fashion
437 * described in section 6.1.2"
439 * That section, on page 248, says:
441 * "If GetIntegerv or GetInteger64v are called,
442 * a floating-point value is rounded to the
443 * nearest integer..."
445 dst
[didx
].i
= IROUND(src
[sidx
].f
);
448 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
450 case GLSL_TYPE_DOUBLE
: {
452 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
453 dst
[didx
].i
= IROUNDD(tmp
);
457 assert(!"Should not get here.");
463 assert(!"Should not get here.");
472 log_uniform(const void *values
, enum glsl_base_type basicType
,
473 unsigned rows
, unsigned cols
, unsigned count
,
475 const struct gl_shader_program
*shProg
,
477 const struct gl_uniform_storage
*uni
)
480 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
481 const unsigned elems
= rows
* cols
* count
;
482 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
484 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
485 "transpose = %s) to: ",
486 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
487 transpose
? "true" : "false");
488 for (unsigned i
= 0; i
< elems
; i
++) {
489 if (i
!= 0 && ((i
% rows
) == 0))
494 printf("%u ", v
[i
].u
);
497 printf("%d ", v
[i
].i
);
499 case GLSL_TYPE_FLOAT
:
500 printf("%g ", v
[i
].f
);
502 case GLSL_TYPE_DOUBLE
: {
504 memcpy(&tmp
, &v
[i
* 2].f
, sizeof(tmp
));
509 assert(!"Should not get here.");
519 log_program_parameters(const struct gl_shader_program
*shProg
)
521 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
522 if (shProg
->_LinkedShaders
[i
] == NULL
)
525 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
527 printf("Program %d %s shader parameters:\n",
528 shProg
->Name
, _mesa_shader_stage_to_string(i
));
529 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
530 printf("%s: %p %f %f %f %f\n",
531 prog
->Parameters
->Parameters
[j
].Name
,
532 prog
->Parameters
->ParameterValues
[j
],
533 prog
->Parameters
->ParameterValues
[j
][0].f
,
534 prog
->Parameters
->ParameterValues
[j
][1].f
,
535 prog
->Parameters
->ParameterValues
[j
][2].f
,
536 prog
->Parameters
->ParameterValues
[j
][3].f
);
544 * Propagate some values from uniform backing storage to driver storage
546 * Values propagated from uniform backing storage to driver storage
547 * have all format / type conversions previously requested by the
548 * driver applied. This function is most often called by the
549 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
552 * \param uni Uniform whose data is to be propagated to driver storage
553 * \param array_index If \c uni is an array, this is the element of
554 * the array to be propagated.
555 * \param count Number of array elements to propagate.
558 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
559 unsigned array_index
,
564 /* vector_elements and matrix_columns can be 0 for samplers.
566 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
567 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
568 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
570 /* Store the data in the driver's requested type in the driver's storage
573 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
575 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
576 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
577 uint8_t *dst
= (uint8_t *) store
->data
;
578 const unsigned extra_stride
=
579 store
->element_stride
- (vectors
* store
->vector_stride
);
581 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
584 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
586 __func__
, dst
, array_index
, components
,
587 vectors
, count
, store
->vector_stride
, extra_stride
);
590 dst
+= array_index
* store
->element_stride
;
592 switch (store
->format
) {
593 case uniform_native
: {
597 if (src_vector_byte_stride
== store
->vector_stride
) {
599 for (j
= 0; j
< count
; j
++) {
600 memcpy(dst
, src
, src_vector_byte_stride
* vectors
);
601 src
+= src_vector_byte_stride
* vectors
;
602 dst
+= store
->vector_stride
* vectors
;
607 /* Unigine Heaven benchmark gets here */
608 memcpy(dst
, src
, src_vector_byte_stride
* vectors
* count
);
609 src
+= src_vector_byte_stride
* vectors
* count
;
610 dst
+= store
->vector_stride
* vectors
* count
;
613 for (j
= 0; j
< count
; j
++) {
614 for (v
= 0; v
< vectors
; v
++) {
615 memcpy(dst
, src
, src_vector_byte_stride
);
616 src
+= src_vector_byte_stride
;
617 dst
+= store
->vector_stride
;
626 case uniform_int_float
: {
627 const int *isrc
= (const int *) src
;
632 for (j
= 0; j
< count
; j
++) {
633 for (v
= 0; v
< vectors
; v
++) {
634 for (c
= 0; c
< components
; c
++) {
635 ((float *) dst
)[c
] = (float) *isrc
;
639 dst
+= store
->vector_stride
;
648 assert(!"Should not get here.");
656 * Return printable string for a given GLSL_TYPE_x
659 glsl_type_name(enum glsl_base_type type
)
666 case GLSL_TYPE_FLOAT
:
668 case GLSL_TYPE_DOUBLE
:
672 case GLSL_TYPE_SAMPLER
:
674 case GLSL_TYPE_IMAGE
:
676 case GLSL_TYPE_ATOMIC_UINT
:
677 return "atomic_uint";
678 case GLSL_TYPE_STRUCT
:
680 case GLSL_TYPE_INTERFACE
:
682 case GLSL_TYPE_ARRAY
:
686 case GLSL_TYPE_ERROR
:
695 * Called via glUniform*() functions.
698 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
699 GLint location
, GLsizei count
,
700 const GLvoid
*values
,
701 enum glsl_base_type basicType
,
702 unsigned src_components
)
705 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
707 struct gl_uniform_storage
*const uni
=
708 validate_uniform_parameters(ctx
, shProg
, location
, count
,
709 &offset
, "glUniform");
713 if (uni
->type
->is_matrix()) {
714 /* Can't set matrix uniforms (like mat4) with glUniform */
715 _mesa_error(ctx
, GL_INVALID_OPERATION
,
716 "glUniform%u(uniform \"%s\"@%d is matrix)",
717 src_components
, uni
->name
, location
);
721 /* Verify that the types are compatible.
723 const unsigned components
= uni
->type
->is_sampler()
724 ? 1 : uni
->type
->vector_elements
;
726 if (components
!= src_components
) {
727 /* glUniformN() must match float/vecN type */
728 _mesa_error(ctx
, GL_INVALID_OPERATION
,
729 "glUniform%u(\"%s\"@%u has %u components, not %u)",
730 src_components
, uni
->name
, location
,
731 components
, src_components
);
736 switch (uni
->type
->base_type
) {
738 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
740 case GLSL_TYPE_SAMPLER
:
741 match
= (basicType
== GLSL_TYPE_INT
);
743 case GLSL_TYPE_IMAGE
:
744 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
747 match
= (basicType
== uni
->type
->base_type
);
752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
753 "glUniform%u(\"%s\"@%d is %s, not %s)",
754 src_components
, uni
->name
, location
,
755 glsl_type_name(uni
->type
->base_type
),
756 glsl_type_name(basicType
));
760 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
761 log_uniform(values
, basicType
, components
, 1, count
,
762 false, shProg
, location
, uni
);
765 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
767 * "Setting a sampler's value to i selects texture image unit number
768 * i. The values of i range from zero to the implementation- dependent
769 * maximum supported number of texture image units."
771 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
774 * "Error Description Offending command
777 * INVALID_VALUE Numeric argument out of range Yes"
779 * Based on that, when an invalid sampler is specified, we generate a
780 * GL_INVALID_VALUE error and ignore the command.
782 if (uni
->type
->is_sampler()) {
783 for (int i
= 0; i
< count
; i
++) {
784 const unsigned texUnit
= ((unsigned *) values
)[i
];
786 /* check that the sampler (tex unit index) is legal */
787 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
788 _mesa_error(ctx
, GL_INVALID_VALUE
,
789 "glUniform1i(invalid sampler/tex unit index for "
795 /* We need to reset the validate flag on changes to samplers in case
796 * two different sampler types are set to the same texture unit.
798 ctx
->_Shader
->Validated
= GL_FALSE
;
801 if (uni
->type
->is_image()) {
802 for (int i
= 0; i
< count
; i
++) {
803 const int unit
= ((GLint
*) values
)[i
];
805 /* check that the image unit is legal */
806 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
807 _mesa_error(ctx
, GL_INVALID_VALUE
,
808 "glUniform1i(invalid image unit index for uniform %d)",
815 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
817 * "When loading N elements starting at an arbitrary position k in a
818 * uniform declared as an array, elements k through k + N - 1 in the
819 * array will be replaced with the new values. Values for any array
820 * element that exceeds the highest array element index used, as
821 * reported by GetActiveUniform, will be ignored by the GL."
823 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
824 * will have already generated an error.
826 if (uni
->array_elements
!= 0) {
827 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
830 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
832 /* Store the data in the "actual type" backing storage for the uniform.
834 if (!uni
->type
->is_boolean()) {
835 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
836 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
838 const union gl_constant_value
*src
=
839 (const union gl_constant_value
*) values
;
840 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
841 const unsigned elems
= components
* count
;
843 for (unsigned i
= 0; i
< elems
; i
++) {
844 if (basicType
== GLSL_TYPE_FLOAT
) {
845 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
847 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
852 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
854 /* If the uniform is a sampler, do the extra magic necessary to propagate
855 * the changes through.
857 if (uni
->type
->is_sampler()) {
858 bool flushed
= false;
859 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
860 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
862 /* If the shader stage doesn't use the sampler uniform, skip this. */
863 if (!uni
->opaque
[i
].active
)
866 bool changed
= false;
867 for (int j
= 0; j
< count
; j
++) {
868 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
869 if (sh
->Program
->SamplerUnits
[unit
] != ((unsigned *) values
)[j
]) {
870 sh
->Program
->SamplerUnits
[unit
] = ((unsigned *) values
)[j
];
877 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
881 struct gl_program
*const prog
= sh
->Program
;
882 _mesa_update_shader_textures_used(shProg
, prog
);
883 if (ctx
->Driver
.SamplerUniformChange
)
884 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
889 /* If the uniform is an image, update the mapping from image
890 * uniforms to image units present in the shader data structure.
892 if (uni
->type
->is_image()) {
893 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
894 if (uni
->opaque
[i
].active
) {
895 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
897 for (int j
= 0; j
< count
; j
++)
898 sh
->Program
->sh
.ImageUnits
[uni
->opaque
[i
].index
+ offset
+ j
] =
899 ((GLint
*) values
)[j
];
903 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
908 * Called by glUniformMatrix*() functions.
909 * Note: cols=2, rows=4 ==> array[2] of vec4
912 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
913 GLuint cols
, GLuint rows
,
914 GLint location
, GLsizei count
,
916 const GLvoid
*values
, enum glsl_base_type basicType
)
923 struct gl_uniform_storage
*const uni
=
924 validate_uniform_parameters(ctx
, shProg
, location
, count
,
925 &offset
, "glUniformMatrix");
929 if (!uni
->type
->is_matrix()) {
930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
931 "glUniformMatrix(non-matrix uniform)");
935 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
936 size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
938 assert(!uni
->type
->is_sampler());
939 vectors
= uni
->type
->matrix_columns
;
940 components
= uni
->type
->vector_elements
;
942 /* Verify that the types are compatible. This is greatly simplified for
943 * matrices because they can only have a float base type.
945 if (vectors
!= cols
|| components
!= rows
) {
946 _mesa_error(ctx
, GL_INVALID_OPERATION
,
947 "glUniformMatrix(matrix size mismatch)");
951 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
952 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
955 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
956 _mesa_error(ctx
, GL_INVALID_VALUE
,
957 "glUniformMatrix(matrix transpose is not GL_FALSE)");
962 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
965 * "If any of the following conditions occur, an INVALID_OPERATION
966 * error is generated by the Uniform* commands, and no uniform values
971 * - if the uniform declared in the shader is not of type boolean and
972 * the type indicated in the name of the Uniform* command used does
973 * not match the type of the uniform"
975 * There are no Boolean matrix types, so we do not need to allow
976 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
978 if (uni
->type
->base_type
!= basicType
) {
979 _mesa_error(ctx
, GL_INVALID_OPERATION
,
980 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
981 cols
, rows
, uni
->name
, location
,
982 glsl_type_name(uni
->type
->base_type
),
983 glsl_type_name(basicType
));
987 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
988 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
989 bool(transpose
), shProg
, location
, uni
);
992 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
994 * "When loading N elements starting at an arbitrary position k in a
995 * uniform declared as an array, elements k through k + N - 1 in the
996 * array will be replaced with the new values. Values for any array
997 * element that exceeds the highest array element index used, as
998 * reported by GetActiveUniform, will be ignored by the GL."
1000 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1001 * will have already generated an error.
1003 if (uni
->array_elements
!= 0) {
1004 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1007 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1009 /* Store the data in the "actual type" backing storage for the uniform.
1011 elements
= components
* vectors
;
1014 memcpy(&uni
->storage
[size_mul
* elements
* offset
], values
,
1015 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
1016 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1017 /* Copy and transpose the matrix.
1019 const float *src
= (const float *)values
;
1020 float *dst
= &uni
->storage
[elements
* offset
].f
;
1022 for (int i
= 0; i
< count
; i
++) {
1023 for (unsigned r
= 0; r
< rows
; r
++) {
1024 for (unsigned c
= 0; c
< cols
; c
++) {
1025 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1033 assert(basicType
== GLSL_TYPE_DOUBLE
);
1034 const double *src
= (const double *)values
;
1035 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
1037 for (int i
= 0; i
< count
; i
++) {
1038 for (unsigned r
= 0; r
< rows
; r
++) {
1039 for (unsigned c
= 0; c
< cols
; c
++) {
1040 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1049 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1054 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1055 char *errMsg
, size_t errMsgLength
)
1057 /* Shader does not have samplers. */
1058 if (shProg
->data
->NumUniformStorage
== 0)
1061 if (!shProg
->SamplersValidated
) {
1062 _mesa_snprintf(errMsg
, errMsgLength
,
1063 "active samplers with a different type "
1064 "refer to the same texture image unit");
1071 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1073 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1074 * OpenGL 4.1 spec says:
1076 * "[INVALID_OPERATION] is generated by any command that transfers
1077 * vertices to the GL if:
1081 * - Any two active samplers in the current program object are of
1082 * different types, but refer to the same texture image unit.
1084 * - The number of active samplers in the program exceeds the
1085 * maximum number of texture image units allowed."
1089 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1090 struct gl_linked_shader
*shader
;
1091 unsigned active_samplers
= 0;
1092 const struct gl_shader_program
**shProg
=
1093 (const struct gl_shader_program
**) pipeline
->CurrentProgram
;
1096 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1098 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1102 shader
= shProg
[idx
]->_LinkedShaders
[idx
];
1103 if (!shader
|| !shader
->Program
)
1106 mask
= shader
->Program
->SamplersUsed
;
1108 const int s
= u_bit_scan(&mask
);
1109 GLuint unit
= shader
->Program
->SamplerUnits
[s
];
1110 GLuint tgt
= shader
->Program
->sh
.SamplerTargets
[s
];
1112 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1113 * great job of eliminating unused uniforms currently so for now
1114 * don't throw an error if two sampler types both point to 0.
1119 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1121 ralloc_asprintf(pipeline
,
1123 "Texture unit %d is accessed with 2 different types",
1124 shProg
[idx
]->Name
, unit
);
1128 TexturesUsed
[unit
] |= (1 << tgt
);
1131 active_samplers
+= shader
->Program
->info
.num_textures
;
1134 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1136 ralloc_asprintf(pipeline
,
1137 "the number of active samplers %d exceed the "
1139 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);