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 "program/hash_table.h"
39 #include "util/bitscan.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 struct gl_program_resource
*res
;
52 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(maxLength < 0)");
56 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
60 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
64 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
69 _mesa_get_program_resource_name(shProg
, GL_UNIFORM
, index
, maxLength
,
70 length
, nameOut
, "glGetActiveUniform");
72 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
73 res
, index
, GL_TYPE
, (GLint
*) type
,
74 "glGetActiveUniform");
76 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
77 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
78 "glGetActiveUniform");
82 resource_prop_from_uniform_prop(GLenum uni_prop
)
89 case GL_UNIFORM_NAME_LENGTH
:
90 return GL_NAME_LENGTH
;
91 case GL_UNIFORM_BLOCK_INDEX
:
92 return GL_BLOCK_INDEX
;
93 case GL_UNIFORM_OFFSET
:
95 case GL_UNIFORM_ARRAY_STRIDE
:
96 return GL_ARRAY_STRIDE
;
97 case GL_UNIFORM_MATRIX_STRIDE
:
98 return GL_MATRIX_STRIDE
;
99 case GL_UNIFORM_IS_ROW_MAJOR
:
100 return GL_IS_ROW_MAJOR
;
101 case GL_UNIFORM_ATOMIC_COUNTER_BUFFER_INDEX
:
102 return GL_ATOMIC_COUNTER_BUFFER_INDEX
;
108 extern "C" void GLAPIENTRY
109 _mesa_GetActiveUniformsiv(GLuint program
,
110 GLsizei uniformCount
,
111 const GLuint
*uniformIndices
,
115 GET_CURRENT_CONTEXT(ctx
);
116 struct gl_shader_program
*shProg
;
117 struct gl_program_resource
*res
;
120 if (uniformCount
< 0) {
121 _mesa_error(ctx
, GL_INVALID_VALUE
,
122 "glGetActiveUniformsiv(uniformCount < 0)");
126 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
130 res_prop
= resource_prop_from_uniform_prop(pname
);
132 /* We need to first verify that each entry exists as active uniform. If
133 * not, generate error and do not cause any other side effects.
135 * In the case of and error condition, Page 16 (section 2.3.1 Errors)
136 * of the OpenGL 4.5 spec says:
138 * "If the generating command modifies values through a pointer argu-
139 * ment, no change is made to these values."
141 for (int i
= 0; i
< uniformCount
; i
++) {
142 if (!_mesa_program_resource_find_index(shProg
, GL_UNIFORM
,
143 uniformIndices
[i
])) {
144 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
149 for (int i
= 0; i
< uniformCount
; i
++) {
150 res
= _mesa_program_resource_find_index(shProg
, GL_UNIFORM
,
152 if (!_mesa_program_resource_prop(shProg
, res
, uniformIndices
[i
],
153 res_prop
, ¶ms
[i
],
154 "glGetActiveUniformsiv"))
159 static struct gl_uniform_storage
*
160 validate_uniform_parameters(struct gl_context
*ctx
,
161 struct gl_shader_program
*shProg
,
162 GLint location
, GLsizei count
,
163 unsigned *array_index
,
166 if (shProg
== NULL
) {
167 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
171 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
173 * "If a negative number is provided where an argument of type sizei or
174 * sizeiptr is specified, the error INVALID_VALUE is generated."
177 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
181 /* Check that the given location is in bounds of uniform remap table.
182 * Unlinked programs will have NumUniformRemapTable == 0, so we can take
183 * the shProg->LinkStatus check out of the main path.
185 if (unlikely(location
>= (GLint
) shProg
->NumUniformRemapTable
)) {
186 if (!shProg
->LinkStatus
)
187 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)",
190 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
196 if (location
== -1) {
197 if (!shProg
->LinkStatus
)
198 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)",
204 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
206 * "If any of the following conditions occur, an INVALID_OPERATION
207 * error is generated by the Uniform* commands, and no uniform values
212 * - if no variable with a location of location exists in the
213 * program object currently in use and location is not -1,
214 * - if count is greater than one, and the uniform declared in the
215 * shader is not an array variable,
217 if (location
< -1 || !shProg
->UniformRemapTable
[location
]) {
218 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
223 /* If the driver storage pointer in remap table is -1, we ignore silently.
225 * GL_ARB_explicit_uniform_location spec says:
226 * "What happens if Uniform* is called with an explicitly defined
227 * uniform location, but that uniform is deemed inactive by the
230 * RESOLVED: The call is ignored for inactive uniform variables and
231 * no error is generated."
234 if (shProg
->UniformRemapTable
[location
] ==
235 INACTIVE_UNIFORM_EXPLICIT_LOCATION
)
238 struct gl_uniform_storage
*const uni
= shProg
->UniformRemapTable
[location
];
240 /* Even though no location is assigned to a built-in uniform and this
241 * function should already have returned NULL, this test makes it explicit
242 * that we are not allowing to update the value of a built-in.
247 if (uni
->array_elements
== 0) {
249 _mesa_error(ctx
, GL_INVALID_OPERATION
,
250 "%s(count = %u for non-array \"%s\"@%d)",
251 caller
, count
, uni
->name
, location
);
255 assert((location
- uni
->remap_location
) == 0);
258 /* The array index specified by the uniform location is just the uniform
259 * location minus the base location of of the uniform.
261 *array_index
= location
- uni
->remap_location
;
263 /* If the uniform is an array, check that array_index is in bounds.
264 * array_index is unsigned so no need to check for less than zero.
266 if (*array_index
>= uni
->array_elements
) {
267 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
276 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
279 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
280 GLsizei bufSize
, enum glsl_base_type returnType
,
283 struct gl_shader_program
*shProg
=
284 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
287 struct gl_uniform_storage
*const uni
=
288 validate_uniform_parameters(ctx
, shProg
, location
, 1,
289 &offset
, "glGetUniform");
291 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
294 * "The error INVALID_OPERATION is generated if program has not been
295 * linked successfully, or if location is not a valid location for
298 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
301 * "If the value of location is -1, the Uniform* commands will
302 * silently ignore the data passed in, and the current uniform
303 * values will not be changed."
305 * Allowing -1 for the location parameter of glUniform allows
306 * applications to avoid error paths in the case that, for example, some
307 * uniform variable is removed by the compiler / linker after
308 * optimization. In this case, the new value of the uniform is dropped
309 * on the floor. For the case of glGetUniform, there is nothing
310 * sensible to do for a location of -1.
312 * If the location was -1, validate_unfirom_parameters will return NULL
313 * without raising an error. Raise the error here.
315 if (location
== -1) {
316 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniform(location=%d)",
324 unsigned elements
= (uni
->type
->is_sampler())
325 ? 1 : uni
->type
->components();
326 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
327 const int rmul
= glsl_base_type_is_64bit(returnType
) ? 2 : 1;
329 /* Calculate the source base address *BEFORE* modifying elements to
330 * account for the size of the user's buffer.
332 const union gl_constant_value
*const src
=
333 &uni
->storage
[offset
* elements
* dmul
];
335 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
336 returnType
== GLSL_TYPE_UINT
|| returnType
== GLSL_TYPE_DOUBLE
);
338 /* doubles have a different size than the other 3 types */
339 unsigned bytes
= sizeof(src
[0]) * elements
* rmul
;
340 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
341 _mesa_error( ctx
, GL_INVALID_OPERATION
,
342 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
343 " but %u bytes are required)", bufSize
, bytes
);
347 /* If the return type and the uniform's native type are "compatible,"
348 * just memcpy the data. If the types are not compatible, perform a
349 * slower convert-and-copy process.
351 if (returnType
== uni
->type
->base_type
352 || ((returnType
== GLSL_TYPE_INT
353 || returnType
== GLSL_TYPE_UINT
)
355 (uni
->type
->base_type
== GLSL_TYPE_INT
356 || uni
->type
->base_type
== GLSL_TYPE_UINT
357 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
358 || uni
->type
->base_type
== GLSL_TYPE_IMAGE
))) {
359 memcpy(paramsOut
, src
, bytes
);
361 union gl_constant_value
*const dst
=
362 (union gl_constant_value
*) paramsOut
;
363 /* This code could be optimized by putting the loop inside the switch
364 * statements. However, this is not expected to be
365 * performance-critical code.
367 for (unsigned i
= 0; i
< elements
; i
++) {
371 switch (returnType
) {
372 case GLSL_TYPE_FLOAT
:
373 switch (uni
->type
->base_type
) {
375 dst
[didx
].f
= (float) src
[sidx
].u
;
378 case GLSL_TYPE_SAMPLER
:
379 case GLSL_TYPE_IMAGE
:
380 dst
[didx
].f
= (float) src
[sidx
].i
;
383 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
385 case GLSL_TYPE_DOUBLE
:
386 dst
[didx
].f
= *(double *)&src
[sidx
].f
;
389 assert(!"Should not get here.");
393 case GLSL_TYPE_DOUBLE
:
394 switch (uni
->type
->base_type
) {
396 *(double *)&dst
[didx
].f
= (double) src
[sidx
].u
;
399 case GLSL_TYPE_SAMPLER
:
400 case GLSL_TYPE_IMAGE
:
401 *(double *)&dst
[didx
].f
= (double) src
[sidx
].i
;
404 *(double *)&dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
406 case GLSL_TYPE_FLOAT
:
407 *(double *)&dst
[didx
].f
= (double) src
[sidx
].f
;
410 assert(!"Should not get here.");
416 switch (uni
->type
->base_type
) {
417 case GLSL_TYPE_FLOAT
:
418 /* While the GL 3.2 core spec doesn't explicitly
419 * state how conversion of float uniforms to integer
420 * values works, in section 6.2 "State Tables" on
423 * "Unless otherwise specified, when floating
424 * point state is returned as integer values or
425 * integer state is returned as floating-point
426 * values it is converted in the fashion
427 * described in section 6.1.2"
429 * That section, on page 248, says:
431 * "If GetIntegerv or GetInteger64v are called,
432 * a floating-point value is rounded to the
433 * nearest integer..."
435 dst
[didx
].i
= IROUND(src
[sidx
].f
);
438 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
440 case GLSL_TYPE_DOUBLE
:
441 dst
[didx
].i
= IROUNDD(*(double *)&src
[sidx
].f
);
444 assert(!"Should not get here.");
450 assert(!"Should not get here.");
459 log_uniform(const void *values
, enum glsl_base_type basicType
,
460 unsigned rows
, unsigned cols
, unsigned count
,
462 const struct gl_shader_program
*shProg
,
464 const struct gl_uniform_storage
*uni
)
467 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
468 const unsigned elems
= rows
* cols
* count
;
469 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
471 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
472 "transpose = %s) to: ",
473 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
474 transpose
? "true" : "false");
475 for (unsigned i
= 0; i
< elems
; i
++) {
476 if (i
!= 0 && ((i
% rows
) == 0))
481 printf("%u ", v
[i
].u
);
484 printf("%d ", v
[i
].i
);
486 case GLSL_TYPE_FLOAT
:
487 printf("%g ", v
[i
].f
);
489 case GLSL_TYPE_DOUBLE
:
490 printf("%g ", *(double* )&v
[i
* 2].f
);
493 assert(!"Should not get here.");
503 log_program_parameters(const struct gl_shader_program
*shProg
)
505 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
506 if (shProg
->_LinkedShaders
[i
] == NULL
)
509 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
511 printf("Program %d %s shader parameters:\n",
512 shProg
->Name
, _mesa_shader_stage_to_string(i
));
513 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
514 printf("%s: %p %f %f %f %f\n",
515 prog
->Parameters
->Parameters
[j
].Name
,
516 prog
->Parameters
->ParameterValues
[j
],
517 prog
->Parameters
->ParameterValues
[j
][0].f
,
518 prog
->Parameters
->ParameterValues
[j
][1].f
,
519 prog
->Parameters
->ParameterValues
[j
][2].f
,
520 prog
->Parameters
->ParameterValues
[j
][3].f
);
528 * Propagate some values from uniform backing storage to driver storage
530 * Values propagated from uniform backing storage to driver storage
531 * have all format / type conversions previously requested by the
532 * driver applied. This function is most often called by the
533 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
536 * \param uni Uniform whose data is to be propagated to driver storage
537 * \param array_index If \c uni is an array, this is the element of
538 * the array to be propagated.
539 * \param count Number of array elements to propagate.
542 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
543 unsigned array_index
,
548 /* vector_elements and matrix_columns can be 0 for samplers.
550 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
551 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
552 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
554 /* Store the data in the driver's requested type in the driver's storage
557 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
559 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
560 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
561 uint8_t *dst
= (uint8_t *) store
->data
;
562 const unsigned extra_stride
=
563 store
->element_stride
- (vectors
* store
->vector_stride
);
565 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
568 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
570 __func__
, dst
, array_index
, components
,
571 vectors
, count
, store
->vector_stride
, extra_stride
);
574 dst
+= array_index
* store
->element_stride
;
576 switch (store
->format
) {
577 case uniform_native
: {
581 for (j
= 0; j
< count
; j
++) {
582 for (v
= 0; v
< vectors
; v
++) {
583 memcpy(dst
, src
, src_vector_byte_stride
);
584 src
+= src_vector_byte_stride
;
585 dst
+= store
->vector_stride
;
593 case uniform_int_float
: {
594 const int *isrc
= (const int *) src
;
599 for (j
= 0; j
< count
; j
++) {
600 for (v
= 0; v
< vectors
; v
++) {
601 for (c
= 0; c
< components
; c
++) {
602 ((float *) dst
)[c
] = (float) *isrc
;
606 dst
+= store
->vector_stride
;
615 assert(!"Should not get here.");
623 * Return printable string for a given GLSL_TYPE_x
626 glsl_type_name(enum glsl_base_type type
)
633 case GLSL_TYPE_FLOAT
:
635 case GLSL_TYPE_DOUBLE
:
639 case GLSL_TYPE_SAMPLER
:
641 case GLSL_TYPE_IMAGE
:
643 case GLSL_TYPE_ATOMIC_UINT
:
644 return "atomic_uint";
645 case GLSL_TYPE_STRUCT
:
647 case GLSL_TYPE_INTERFACE
:
649 case GLSL_TYPE_ARRAY
:
653 case GLSL_TYPE_ERROR
:
662 * Called via glUniform*() functions.
665 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
666 GLint location
, GLsizei count
,
667 const GLvoid
*values
,
668 enum glsl_base_type basicType
,
669 unsigned src_components
)
672 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
674 struct gl_uniform_storage
*const uni
=
675 validate_uniform_parameters(ctx
, shProg
, location
, count
,
676 &offset
, "glUniform");
680 if (uni
->type
->is_matrix()) {
681 /* Can't set matrix uniforms (like mat4) with glUniform */
682 _mesa_error(ctx
, GL_INVALID_OPERATION
,
683 "glUniform%u(uniform \"%s\"@%d is matrix)",
684 src_components
, uni
->name
, location
);
688 /* Verify that the types are compatible.
690 const unsigned components
= uni
->type
->is_sampler()
691 ? 1 : uni
->type
->vector_elements
;
693 if (components
!= src_components
) {
694 /* glUniformN() must match float/vecN type */
695 _mesa_error(ctx
, GL_INVALID_OPERATION
,
696 "glUniform%u(\"%s\"@%u has %u components, not %u)",
697 src_components
, uni
->name
, location
,
698 components
, src_components
);
703 switch (uni
->type
->base_type
) {
705 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
707 case GLSL_TYPE_SAMPLER
:
708 match
= (basicType
== GLSL_TYPE_INT
);
710 case GLSL_TYPE_IMAGE
:
711 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
714 match
= (basicType
== uni
->type
->base_type
);
719 _mesa_error(ctx
, GL_INVALID_OPERATION
,
720 "glUniform%u(\"%s\"@%d is %s, not %s)",
721 src_components
, uni
->name
, location
,
722 glsl_type_name(uni
->type
->base_type
),
723 glsl_type_name(basicType
));
727 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
728 log_uniform(values
, basicType
, components
, 1, count
,
729 false, shProg
, location
, uni
);
732 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
734 * "Setting a sampler's value to i selects texture image unit number
735 * i. The values of i range from zero to the implementation- dependent
736 * maximum supported number of texture image units."
738 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
741 * "Error Description Offending command
744 * INVALID_VALUE Numeric argument out of range Yes"
746 * Based on that, when an invalid sampler is specified, we generate a
747 * GL_INVALID_VALUE error and ignore the command.
749 if (uni
->type
->is_sampler()) {
750 for (int i
= 0; i
< count
; i
++) {
751 const unsigned texUnit
= ((unsigned *) values
)[i
];
753 /* check that the sampler (tex unit index) is legal */
754 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
755 _mesa_error(ctx
, GL_INVALID_VALUE
,
756 "glUniform1i(invalid sampler/tex unit index for "
762 /* We need to reset the validate flag on changes to samplers in case
763 * two different sampler types are set to the same texture unit.
765 ctx
->_Shader
->Validated
= GL_FALSE
;
768 if (uni
->type
->is_image()) {
769 for (int i
= 0; i
< count
; i
++) {
770 const int unit
= ((GLint
*) values
)[i
];
772 /* check that the image unit is legal */
773 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
774 _mesa_error(ctx
, GL_INVALID_VALUE
,
775 "glUniform1i(invalid image unit index for uniform %d)",
782 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
784 * "When loading N elements starting at an arbitrary position k in a
785 * uniform declared as an array, elements k through k + N - 1 in the
786 * array will be replaced with the new values. Values for any array
787 * element that exceeds the highest array element index used, as
788 * reported by GetActiveUniform, will be ignored by the GL."
790 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
791 * will have already generated an error.
793 if (uni
->array_elements
!= 0) {
794 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
797 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
799 /* Store the data in the "actual type" backing storage for the uniform.
801 if (!uni
->type
->is_boolean()) {
802 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
803 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
805 const union gl_constant_value
*src
=
806 (const union gl_constant_value
*) values
;
807 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
808 const unsigned elems
= components
* count
;
810 for (unsigned i
= 0; i
< elems
; i
++) {
811 if (basicType
== GLSL_TYPE_FLOAT
) {
812 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
814 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
819 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
821 /* If the uniform is a sampler, do the extra magic necessary to propagate
822 * the changes through.
824 if (uni
->type
->is_sampler()) {
825 bool flushed
= false;
826 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
827 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
829 /* If the shader stage doesn't use the sampler uniform, skip this.
831 if (sh
== NULL
|| !uni
->opaque
[i
].active
)
834 for (int j
= 0; j
< count
; j
++) {
835 sh
->SamplerUnits
[uni
->opaque
[i
].index
+ offset
+ j
] =
836 ((unsigned *) values
)[j
];
839 struct gl_program
*const prog
= sh
->Program
;
841 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
843 /* Determine if any of the samplers used by this shader stage have
846 bool changed
= false;
847 GLbitfield mask
= sh
->active_samplers
;
849 const int j
= u_bit_scan(&mask
);
850 if (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
]) {
858 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
862 memcpy(prog
->SamplerUnits
,
864 sizeof(sh
->SamplerUnits
));
866 _mesa_update_shader_textures_used(shProg
, prog
);
867 if (ctx
->Driver
.SamplerUniformChange
)
868 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
873 /* If the uniform is an image, update the mapping from image
874 * uniforms to image units present in the shader data structure.
876 if (uni
->type
->is_image()) {
877 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
878 if (uni
->opaque
[i
].active
) {
879 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
881 for (int j
= 0; j
< count
; j
++)
882 sh
->ImageUnits
[uni
->opaque
[i
].index
+ offset
+ j
] =
883 ((GLint
*) values
)[j
];
887 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
892 * Called by glUniformMatrix*() functions.
893 * Note: cols=2, rows=4 ==> array[2] of vec4
896 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
897 GLuint cols
, GLuint rows
,
898 GLint location
, GLsizei count
,
900 const GLvoid
*values
, enum glsl_base_type basicType
)
907 struct gl_uniform_storage
*const uni
=
908 validate_uniform_parameters(ctx
, shProg
, location
, count
,
909 &offset
, "glUniformMatrix");
913 if (!uni
->type
->is_matrix()) {
914 _mesa_error(ctx
, GL_INVALID_OPERATION
,
915 "glUniformMatrix(non-matrix uniform)");
919 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
920 size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
922 assert(!uni
->type
->is_sampler());
923 vectors
= uni
->type
->matrix_columns
;
924 components
= uni
->type
->vector_elements
;
926 /* Verify that the types are compatible. This is greatly simplified for
927 * matrices because they can only have a float base type.
929 if (vectors
!= cols
|| components
!= rows
) {
930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
931 "glUniformMatrix(matrix size mismatch)");
935 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
936 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
939 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
940 _mesa_error(ctx
, GL_INVALID_VALUE
,
941 "glUniformMatrix(matrix transpose is not GL_FALSE)");
946 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
949 * "If any of the following conditions occur, an INVALID_OPERATION
950 * error is generated by the Uniform* commands, and no uniform values
955 * - if the uniform declared in the shader is not of type boolean and
956 * the type indicated in the name of the Uniform* command used does
957 * not match the type of the uniform"
959 * There are no Boolean matrix types, so we do not need to allow
960 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
962 if (uni
->type
->base_type
!= basicType
) {
963 _mesa_error(ctx
, GL_INVALID_OPERATION
,
964 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
965 cols
, rows
, uni
->name
, location
,
966 glsl_type_name(uni
->type
->base_type
),
967 glsl_type_name(basicType
));
971 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
972 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
973 bool(transpose
), shProg
, location
, uni
);
976 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
978 * "When loading N elements starting at an arbitrary position k in a
979 * uniform declared as an array, elements k through k + N - 1 in the
980 * array will be replaced with the new values. Values for any array
981 * element that exceeds the highest array element index used, as
982 * reported by GetActiveUniform, will be ignored by the GL."
984 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
985 * will have already generated an error.
987 if (uni
->array_elements
!= 0) {
988 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
991 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
993 /* Store the data in the "actual type" backing storage for the uniform.
995 elements
= components
* vectors
;
998 memcpy(&uni
->storage
[size_mul
* elements
* offset
], values
,
999 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
1000 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1001 /* Copy and transpose the matrix.
1003 const float *src
= (const float *)values
;
1004 float *dst
= &uni
->storage
[elements
* offset
].f
;
1006 for (int i
= 0; i
< count
; i
++) {
1007 for (unsigned r
= 0; r
< rows
; r
++) {
1008 for (unsigned c
= 0; c
< cols
; c
++) {
1009 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1017 assert(basicType
== GLSL_TYPE_DOUBLE
);
1018 const double *src
= (const double *)values
;
1019 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
1021 for (int i
= 0; i
< count
; i
++) {
1022 for (unsigned r
= 0; r
< rows
; r
++) {
1023 for (unsigned c
= 0; c
< cols
; c
++) {
1024 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1033 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1038 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1039 char *errMsg
, size_t errMsgLength
)
1041 /* Shader does not have samplers. */
1042 if (shProg
->NumUniformStorage
== 0)
1045 if (!shProg
->SamplersValidated
) {
1046 _mesa_snprintf(errMsg
, errMsgLength
,
1047 "active samplers with a different type "
1048 "refer to the same texture image unit");
1055 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1057 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1058 * OpenGL 4.1 spec says:
1060 * "[INVALID_OPERATION] is generated by any command that transfers
1061 * vertices to the GL if:
1065 * - Any two active samplers in the current program object are of
1066 * different types, but refer to the same texture image unit.
1068 * - The number of active samplers in the program exceeds the
1069 * maximum number of texture image units allowed."
1073 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1074 struct gl_shader
*shader
;
1075 unsigned active_samplers
= 0;
1076 const struct gl_shader_program
**shProg
=
1077 (const struct gl_shader_program
**) pipeline
->CurrentProgram
;
1080 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1082 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1086 shader
= shProg
[idx
]->_LinkedShaders
[idx
];
1087 if (!shader
|| !shader
->Program
)
1090 mask
= shader
->Program
->SamplersUsed
;
1092 const int s
= u_bit_scan(&mask
);
1093 GLuint unit
= shader
->SamplerUnits
[s
];
1094 GLuint tgt
= shader
->SamplerTargets
[s
];
1096 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1097 * great job of eliminating unused uniforms currently so for now
1098 * don't throw an error if two sampler types both point to 0.
1103 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1105 ralloc_asprintf(pipeline
,
1107 "Texture unit %d is accessed with 2 different types",
1108 shProg
[idx
]->Name
, unit
);
1112 TexturesUsed
[unit
] |= (1 << tgt
);
1115 active_samplers
+= shader
->num_samplers
;
1118 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1120 ralloc_asprintf(pipeline
,
1121 "the number of active samplers %d exceed the "
1123 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);