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"
35 #include "glsl/ir_uniform.h"
36 #include "glsl/glsl_parser_extras.h"
37 #include "glsl/program.h"
38 #include "program/hash_table.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->LinkStatus check out of the main path.
184 if (unlikely(location
>= (GLint
) shProg
->NumUniformRemapTable
)) {
185 if (!shProg
->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
->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)",
321 if ((uni
->type
->base_type
== GLSL_TYPE_DOUBLE
&&
322 returnType
!= GLSL_TYPE_DOUBLE
) ||
323 (uni
->type
->base_type
!= GLSL_TYPE_DOUBLE
&&
324 returnType
== GLSL_TYPE_DOUBLE
)) {
325 _mesa_error( ctx
, GL_INVALID_OPERATION
,
326 "glGetnUniform*vARB(incompatible uniform types)");
331 unsigned elements
= (uni
->type
->is_sampler())
332 ? 1 : uni
->type
->components();
333 const int dmul
= uni
->type
->base_type
== GLSL_TYPE_DOUBLE
? 2 : 1;
335 /* Calculate the source base address *BEFORE* modifying elements to
336 * account for the size of the user's buffer.
338 const union gl_constant_value
*const src
=
339 &uni
->storage
[offset
* elements
* dmul
];
341 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
342 returnType
== GLSL_TYPE_UINT
|| returnType
== GLSL_TYPE_DOUBLE
);
344 /* doubles have a different size than the other 3 types */
345 unsigned bytes
= sizeof(src
[0]) * elements
* dmul
;
346 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
347 _mesa_error( ctx
, GL_INVALID_OPERATION
,
348 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
349 " but %u bytes are required)", bufSize
, bytes
);
353 /* If the return type and the uniform's native type are "compatible,"
354 * just memcpy the data. If the types are not compatible, perform a
355 * slower convert-and-copy process.
357 if (returnType
== uni
->type
->base_type
358 || ((returnType
== GLSL_TYPE_INT
359 || returnType
== GLSL_TYPE_UINT
)
361 (uni
->type
->base_type
== GLSL_TYPE_INT
362 || uni
->type
->base_type
== GLSL_TYPE_UINT
363 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
364 || uni
->type
->base_type
== GLSL_TYPE_IMAGE
))) {
365 memcpy(paramsOut
, src
, bytes
);
367 union gl_constant_value
*const dst
=
368 (union gl_constant_value
*) paramsOut
;
370 /* This code could be optimized by putting the loop inside the switch
371 * statements. However, this is not expected to be
372 * performance-critical code.
374 for (unsigned i
= 0; i
< elements
; i
++) {
375 switch (returnType
) {
376 case GLSL_TYPE_FLOAT
:
377 switch (uni
->type
->base_type
) {
379 dst
[i
].f
= (float) src
[i
].u
;
382 case GLSL_TYPE_SAMPLER
:
383 case GLSL_TYPE_IMAGE
:
384 dst
[i
].f
= (float) src
[i
].i
;
387 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
390 assert(!"Should not get here.");
397 switch (uni
->type
->base_type
) {
398 case GLSL_TYPE_FLOAT
:
399 /* While the GL 3.2 core spec doesn't explicitly
400 * state how conversion of float uniforms to integer
401 * values works, in section 6.2 "State Tables" on
404 * "Unless otherwise specified, when floating
405 * point state is returned as integer values or
406 * integer state is returned as floating-point
407 * values it is converted in the fashion
408 * described in section 6.1.2"
410 * That section, on page 248, says:
412 * "If GetIntegerv or GetInteger64v are called,
413 * a floating-point value is rounded to the
414 * nearest integer..."
416 dst
[i
].i
= IROUND(src
[i
].f
);
419 dst
[i
].i
= src
[i
].i
? 1 : 0;
422 assert(!"Should not get here.");
428 assert(!"Should not get here.");
437 log_uniform(const void *values
, enum glsl_base_type basicType
,
438 unsigned rows
, unsigned cols
, unsigned count
,
440 const struct gl_shader_program
*shProg
,
442 const struct gl_uniform_storage
*uni
)
445 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
446 const unsigned elems
= rows
* cols
* count
;
447 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
449 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
450 "transpose = %s) to: ",
451 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
452 transpose
? "true" : "false");
453 for (unsigned i
= 0; i
< elems
; i
++) {
454 if (i
!= 0 && ((i
% rows
) == 0))
459 printf("%u ", v
[i
].u
);
462 printf("%d ", v
[i
].i
);
464 case GLSL_TYPE_FLOAT
:
465 printf("%g ", v
[i
].f
);
467 case GLSL_TYPE_DOUBLE
:
468 printf("%g ", *(double* )&v
[i
* 2].f
);
471 assert(!"Should not get here.");
481 log_program_parameters(const struct gl_shader_program
*shProg
)
483 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
484 if (shProg
->_LinkedShaders
[i
] == NULL
)
487 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
489 printf("Program %d %s shader parameters:\n",
490 shProg
->Name
, _mesa_shader_stage_to_string(i
));
491 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
492 printf("%s: %p %f %f %f %f\n",
493 prog
->Parameters
->Parameters
[j
].Name
,
494 prog
->Parameters
->ParameterValues
[j
],
495 prog
->Parameters
->ParameterValues
[j
][0].f
,
496 prog
->Parameters
->ParameterValues
[j
][1].f
,
497 prog
->Parameters
->ParameterValues
[j
][2].f
,
498 prog
->Parameters
->ParameterValues
[j
][3].f
);
506 * Propagate some values from uniform backing storage to driver storage
508 * Values propagated from uniform backing storage to driver storage
509 * have all format / type conversions previously requested by the
510 * driver applied. This function is most often called by the
511 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
514 * \param uni Uniform whose data is to be propagated to driver storage
515 * \param array_index If \c uni is an array, this is the element of
516 * the array to be propagated.
517 * \param count Number of array elements to propagate.
520 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
521 unsigned array_index
,
526 /* vector_elements and matrix_columns can be 0 for samplers.
528 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
529 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
530 const int dmul
= uni
->type
->base_type
== GLSL_TYPE_DOUBLE
? 2 : 1;
532 /* Store the data in the driver's requested type in the driver's storage
535 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
537 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
538 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
539 uint8_t *dst
= (uint8_t *) store
->data
;
540 const unsigned extra_stride
=
541 store
->element_stride
- (vectors
* store
->vector_stride
);
543 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
546 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
548 __func__
, dst
, array_index
, components
,
549 vectors
, count
, store
->vector_stride
, extra_stride
);
552 dst
+= array_index
* store
->element_stride
;
554 switch (store
->format
) {
555 case uniform_native
: {
559 for (j
= 0; j
< count
; j
++) {
560 for (v
= 0; v
< vectors
; v
++) {
561 memcpy(dst
, src
, src_vector_byte_stride
);
562 src
+= src_vector_byte_stride
;
563 dst
+= store
->vector_stride
;
571 case uniform_int_float
: {
572 const int *isrc
= (const int *) src
;
577 for (j
= 0; j
< count
; j
++) {
578 for (v
= 0; v
< vectors
; v
++) {
579 for (c
= 0; c
< components
; c
++) {
580 ((float *) dst
)[c
] = (float) *isrc
;
584 dst
+= store
->vector_stride
;
593 assert(!"Should not get here.");
601 * Return printable string for a given GLSL_TYPE_x
604 glsl_type_name(enum glsl_base_type type
)
611 case GLSL_TYPE_FLOAT
:
613 case GLSL_TYPE_DOUBLE
:
617 case GLSL_TYPE_SAMPLER
:
619 case GLSL_TYPE_IMAGE
:
621 case GLSL_TYPE_ATOMIC_UINT
:
622 return "atomic_uint";
623 case GLSL_TYPE_STRUCT
:
625 case GLSL_TYPE_INTERFACE
:
627 case GLSL_TYPE_ARRAY
:
631 case GLSL_TYPE_ERROR
:
640 * Called via glUniform*() functions.
643 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
644 GLint location
, GLsizei count
,
645 const GLvoid
*values
,
646 enum glsl_base_type basicType
,
647 unsigned src_components
)
650 int size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
652 struct gl_uniform_storage
*const uni
=
653 validate_uniform_parameters(ctx
, shProg
, location
, count
,
654 &offset
, "glUniform");
658 if (uni
->type
->is_matrix()) {
659 /* Can't set matrix uniforms (like mat4) with glUniform */
660 _mesa_error(ctx
, GL_INVALID_OPERATION
,
661 "glUniform%u(uniform \"%s\"@%d is matrix)",
662 src_components
, uni
->name
, location
);
666 /* Verify that the types are compatible.
668 const unsigned components
= uni
->type
->is_sampler()
669 ? 1 : uni
->type
->vector_elements
;
671 if (components
!= src_components
) {
672 /* glUniformN() must match float/vecN type */
673 _mesa_error(ctx
, GL_INVALID_OPERATION
,
674 "glUniform%u(\"%s\"@%u has %u components, not %u)",
675 src_components
, uni
->name
, location
,
676 components
, src_components
);
681 switch (uni
->type
->base_type
) {
683 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
685 case GLSL_TYPE_SAMPLER
:
686 match
= (basicType
== GLSL_TYPE_INT
);
688 case GLSL_TYPE_IMAGE
:
689 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
692 match
= (basicType
== uni
->type
->base_type
);
697 _mesa_error(ctx
, GL_INVALID_OPERATION
,
698 "glUniform%u(\"%s\"@%d is %s, not %s)",
699 src_components
, uni
->name
, location
,
700 glsl_type_name(uni
->type
->base_type
),
701 glsl_type_name(basicType
));
705 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
706 log_uniform(values
, basicType
, components
, 1, count
,
707 false, shProg
, location
, uni
);
710 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
712 * "Setting a sampler's value to i selects texture image unit number
713 * i. The values of i range from zero to the implementation- dependent
714 * maximum supported number of texture image units."
716 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
719 * "Error Description Offending command
722 * INVALID_VALUE Numeric argument out of range Yes"
724 * Based on that, when an invalid sampler is specified, we generate a
725 * GL_INVALID_VALUE error and ignore the command.
727 if (uni
->type
->is_sampler()) {
728 for (int i
= 0; i
< count
; i
++) {
729 const unsigned texUnit
= ((unsigned *) values
)[i
];
731 /* check that the sampler (tex unit index) is legal */
732 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
733 _mesa_error(ctx
, GL_INVALID_VALUE
,
734 "glUniform1i(invalid sampler/tex unit index for "
742 if (uni
->type
->is_image()) {
743 for (int i
= 0; i
< count
; i
++) {
744 const int unit
= ((GLint
*) values
)[i
];
746 /* check that the image unit is legal */
747 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
748 _mesa_error(ctx
, GL_INVALID_VALUE
,
749 "glUniform1i(invalid image unit index for uniform %d)",
756 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
758 * "When loading N elements starting at an arbitrary position k in a
759 * uniform declared as an array, elements k through k + N - 1 in the
760 * array will be replaced with the new values. Values for any array
761 * element that exceeds the highest array element index used, as
762 * reported by GetActiveUniform, will be ignored by the GL."
764 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
765 * will have already generated an error.
767 if (uni
->array_elements
!= 0) {
768 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
771 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
773 /* Store the data in the "actual type" backing storage for the uniform.
775 if (!uni
->type
->is_boolean()) {
776 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
777 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
779 const union gl_constant_value
*src
=
780 (const union gl_constant_value
*) values
;
781 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
782 const unsigned elems
= components
* count
;
784 for (unsigned i
= 0; i
< elems
; i
++) {
785 if (basicType
== GLSL_TYPE_FLOAT
) {
786 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
788 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
793 uni
->initialized
= true;
795 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
797 /* If the uniform is a sampler, do the extra magic necessary to propagate
798 * the changes through.
800 if (uni
->type
->is_sampler()) {
801 bool flushed
= false;
802 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
803 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
805 /* If the shader stage doesn't use the sampler uniform, skip this.
807 if (sh
== NULL
|| !uni
->sampler
[i
].active
)
810 for (int j
= 0; j
< count
; j
++) {
811 sh
->SamplerUnits
[uni
->sampler
[i
].index
+ offset
+ j
] =
812 ((unsigned *) values
)[j
];
815 struct gl_program
*const prog
= sh
->Program
;
817 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
819 /* Determine if any of the samplers used by this shader stage have
822 bool changed
= false;
823 for (unsigned j
= 0; j
< ARRAY_SIZE(prog
->SamplerUnits
); j
++) {
824 if ((sh
->active_samplers
& (1U << j
)) != 0
825 && (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
])) {
833 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
837 memcpy(prog
->SamplerUnits
,
839 sizeof(sh
->SamplerUnits
));
841 _mesa_update_shader_textures_used(shProg
, prog
);
842 if (ctx
->Driver
.SamplerUniformChange
)
843 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
848 /* If the uniform is an image, update the mapping from image
849 * uniforms to image units present in the shader data structure.
851 if (uni
->type
->is_image()) {
852 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
853 if (uni
->image
[i
].active
) {
854 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
856 for (int j
= 0; j
< count
; j
++)
857 sh
->ImageUnits
[uni
->image
[i
].index
+ offset
+ j
] =
858 ((GLint
*) values
)[j
];
862 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
867 * Called by glUniformMatrix*() functions.
868 * Note: cols=2, rows=4 ==> array[2] of vec4
871 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
872 GLuint cols
, GLuint rows
,
873 GLint location
, GLsizei count
,
875 const GLvoid
*values
, enum glsl_base_type basicType
)
882 struct gl_uniform_storage
*const uni
=
883 validate_uniform_parameters(ctx
, shProg
, location
, count
,
884 &offset
, "glUniformMatrix");
888 if (!uni
->type
->is_matrix()) {
889 _mesa_error(ctx
, GL_INVALID_OPERATION
,
890 "glUniformMatrix(non-matrix uniform)");
894 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
895 size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
897 assert(!uni
->type
->is_sampler());
898 vectors
= uni
->type
->matrix_columns
;
899 components
= uni
->type
->vector_elements
;
901 /* Verify that the types are compatible. This is greatly simplified for
902 * matrices because they can only have a float base type.
904 if (vectors
!= cols
|| components
!= rows
) {
905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
906 "glUniformMatrix(matrix size mismatch)");
910 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
911 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
914 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
915 _mesa_error(ctx
, GL_INVALID_VALUE
,
916 "glUniformMatrix(matrix transpose is not GL_FALSE)");
921 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
924 * "If any of the following conditions occur, an INVALID_OPERATION
925 * error is generated by the Uniform* commands, and no uniform values
930 * - if the uniform declared in the shader is not of type boolean and
931 * the type indicated in the name of the Uniform* command used does
932 * not match the type of the uniform"
934 * There are no Boolean matrix types, so we do not need to allow
935 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
937 if (uni
->type
->base_type
!= basicType
) {
938 _mesa_error(ctx
, GL_INVALID_OPERATION
,
939 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
940 cols
, rows
, uni
->name
, location
,
941 glsl_type_name(uni
->type
->base_type
),
942 glsl_type_name(basicType
));
946 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
947 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
948 bool(transpose
), shProg
, location
, uni
);
951 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
953 * "When loading N elements starting at an arbitrary position k in a
954 * uniform declared as an array, elements k through k + N - 1 in the
955 * array will be replaced with the new values. Values for any array
956 * element that exceeds the highest array element index used, as
957 * reported by GetActiveUniform, will be ignored by the GL."
959 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
960 * will have already generated an error.
962 if (uni
->array_elements
!= 0) {
963 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
966 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
968 /* Store the data in the "actual type" backing storage for the uniform.
970 elements
= components
* vectors
;
973 memcpy(&uni
->storage
[elements
* offset
], values
,
974 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
975 } else if (basicType
== GLSL_TYPE_FLOAT
) {
976 /* Copy and transpose the matrix.
978 const float *src
= (const float *)values
;
979 float *dst
= &uni
->storage
[elements
* offset
].f
;
981 for (int i
= 0; i
< count
; i
++) {
982 for (unsigned r
= 0; r
< rows
; r
++) {
983 for (unsigned c
= 0; c
< cols
; c
++) {
984 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
992 assert(basicType
== GLSL_TYPE_DOUBLE
);
993 const double *src
= (const double *)values
;
994 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
996 for (int i
= 0; i
< count
; i
++) {
997 for (unsigned r
= 0; r
< rows
; r
++) {
998 for (unsigned c
= 0; c
< cols
; c
++) {
999 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1008 uni
->initialized
= true;
1010 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1015 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1016 char *errMsg
, size_t errMsgLength
)
1018 /* Shader does not have samplers. */
1019 if (shProg
->NumUniformStorage
== 0)
1022 if (!shProg
->SamplersValidated
) {
1023 _mesa_snprintf(errMsg
, errMsgLength
,
1024 "active samplers with a different type "
1025 "refer to the same texture image unit");
1032 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1034 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1035 * OpenGL 4.1 spec says:
1037 * "[INVALID_OPERATION] is generated by any command that transfers
1038 * vertices to the GL if:
1042 * - Any two active samplers in the current program object are of
1043 * different types, but refer to the same texture image unit.
1045 * - The number of active samplers in the program exceeds the
1046 * maximum number of texture image units allowed."
1048 unsigned active_samplers
= 0;
1049 const struct gl_shader_program
**shProg
=
1050 (const struct gl_shader_program
**) pipeline
->CurrentProgram
;
1052 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1053 memset(unit_types
, 0, sizeof(unit_types
));
1055 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1059 for (unsigned i
= 0; i
< shProg
[idx
]->NumUniformStorage
; i
++) {
1060 const struct gl_uniform_storage
*const storage
=
1061 &shProg
[idx
]->UniformStorage
[i
];
1063 if (!storage
->type
->is_sampler())
1068 const unsigned count
= MAX2(1, storage
->array_elements
);
1069 for (unsigned j
= 0; j
< count
; j
++) {
1070 const unsigned unit
= storage
->storage
[j
].i
;
1072 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1073 * great job of eliminating unused uniforms currently so for now
1074 * don't throw an error if two sampler types both point to 0.
1079 /* The types of the samplers associated with a particular texture
1080 * unit must be an exact match. Page 74 (page 89 of the PDF) of
1081 * the OpenGL 3.3 core spec says:
1083 * "It is not allowed to have variables of different sampler
1084 * types pointing to the same texture image unit within a
1087 if (unit_types
[unit
] == NULL
) {
1088 unit_types
[unit
] = storage
->type
;
1089 } else if (unit_types
[unit
] != storage
->type
) {
1091 ralloc_asprintf(pipeline
,
1092 "Texture unit %d is accessed both as %s "
1094 unit
, unit_types
[unit
]->name
,
1095 storage
->type
->name
);
1102 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1104 ralloc_asprintf(pipeline
,
1105 "the number of active samplers %d exceed the "
1107 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);