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.
28 #include <inttypes.h> /* for PRIx64 macro */
30 #include "main/core.h"
31 #include "main/context.h"
32 #include "main/shaderapi.h"
33 #include "main/shaderobj.h"
34 #include "main/uniforms.h"
35 #include "compiler/glsl/ir.h"
36 #include "compiler/glsl/ir_uniform.h"
37 #include "compiler/glsl/glsl_parser_extras.h"
38 #include "compiler/glsl/program.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(GLint location
, GLsizei count
,
161 unsigned *array_index
,
162 struct gl_context
*ctx
,
163 struct gl_shader_program
*shProg
,
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->data->LinkStatus check out of the main path.
185 if (unlikely(location
>= (GLint
) shProg
->NumUniformRemapTable
)) {
186 if (!shProg
->data
->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
->data
->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(location
, 1, &offset
,
289 ctx
, shProg
, "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
->components();
325 const int rmul
= glsl_base_type_is_64bit(returnType
) ? 2 : 1;
326 int dmul
= (uni
->type
->is_64bit()) ? 2 : 1;
328 if ((uni
->type
->is_sampler() || uni
->type
->is_image()) &&
330 /* Non-bindless samplers/images are represented using unsigned integer
331 * 32-bit, while bindless handles are 64-bit.
336 /* Calculate the source base address *BEFORE* modifying elements to
337 * account for the size of the user's buffer.
339 const union gl_constant_value
*const src
=
340 &uni
->storage
[offset
* elements
* dmul
];
342 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
343 returnType
== GLSL_TYPE_UINT
|| returnType
== GLSL_TYPE_DOUBLE
||
344 returnType
== GLSL_TYPE_UINT64
|| returnType
== GLSL_TYPE_INT64
);
346 /* doubles have a different size than the other 3 types */
347 unsigned bytes
= sizeof(src
[0]) * elements
* rmul
;
348 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
349 _mesa_error(ctx
, GL_INVALID_OPERATION
,
350 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
351 " but %u bytes are required)", bufSize
, bytes
);
355 /* If the return type and the uniform's native type are "compatible,"
356 * just memcpy the data. If the types are not compatible, perform a
357 * slower convert-and-copy process.
359 if (returnType
== uni
->type
->base_type
||
360 ((returnType
== GLSL_TYPE_INT
|| returnType
== GLSL_TYPE_UINT
) &&
361 (uni
->type
->is_sampler() || uni
->type
->is_image()))) {
362 memcpy(paramsOut
, src
, bytes
);
364 union gl_constant_value
*const dst
=
365 (union gl_constant_value
*) paramsOut
;
366 /* This code could be optimized by putting the loop inside the switch
367 * statements. However, this is not expected to be
368 * performance-critical code.
370 for (unsigned i
= 0; i
< elements
; i
++) {
374 switch (returnType
) {
375 case GLSL_TYPE_FLOAT
:
376 switch (uni
->type
->base_type
) {
378 dst
[didx
].f
= (float) src
[sidx
].u
;
381 case GLSL_TYPE_SAMPLER
:
382 case GLSL_TYPE_IMAGE
:
383 dst
[didx
].f
= (float) src
[sidx
].i
;
386 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
388 case GLSL_TYPE_DOUBLE
: {
390 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
394 case GLSL_TYPE_UINT64
: {
396 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
400 case GLSL_TYPE_INT64
: {
402 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
407 assert(!"Should not get here.");
412 case GLSL_TYPE_DOUBLE
:
413 switch (uni
->type
->base_type
) {
414 case GLSL_TYPE_UINT
: {
415 double tmp
= src
[sidx
].u
;
416 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
420 case GLSL_TYPE_SAMPLER
:
421 case GLSL_TYPE_IMAGE
: {
422 double tmp
= src
[sidx
].i
;
423 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
426 case GLSL_TYPE_BOOL
: {
427 double tmp
= src
[sidx
].i
? 1.0 : 0.0;
428 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
431 case GLSL_TYPE_FLOAT
: {
432 double tmp
= src
[sidx
].f
;
433 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
436 case GLSL_TYPE_UINT64
: {
439 memcpy(&tmpu
, &src
[sidx
].u
, sizeof(tmpu
));
441 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
444 case GLSL_TYPE_INT64
: {
447 memcpy(&tmpi
, &src
[sidx
].i
, sizeof(tmpi
));
449 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
453 assert(!"Should not get here.");
459 switch (uni
->type
->base_type
) {
460 case GLSL_TYPE_FLOAT
:
461 /* While the GL 3.2 core spec doesn't explicitly
462 * state how conversion of float uniforms to integer
463 * values works, in section 6.2 "State Tables" on
466 * "Unless otherwise specified, when floating
467 * point state is returned as integer values or
468 * integer state is returned as floating-point
469 * values it is converted in the fashion
470 * described in section 6.1.2"
472 * That section, on page 248, says:
474 * "If GetIntegerv or GetInteger64v are called,
475 * a floating-point value is rounded to the
476 * nearest integer..."
478 dst
[didx
].i
= (int64_t) roundf(src
[sidx
].f
);
481 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
484 dst
[didx
].i
= MIN2(src
[sidx
].i
, INT_MAX
);
486 case GLSL_TYPE_DOUBLE
: {
488 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
489 dst
[didx
].i
= (int64_t) round(tmp
);
492 case GLSL_TYPE_UINT64
: {
494 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
498 case GLSL_TYPE_INT64
: {
500 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
505 assert(!"Should not get here.");
511 switch (uni
->type
->base_type
) {
512 case GLSL_TYPE_FLOAT
:
513 /* The spec isn't terribly clear how to handle negative
514 * values with an unsigned return type.
516 * GL 4.5 section 2.2.2 ("Data Conversions for State
517 * Query Commands") says:
519 * "If a value is so large in magnitude that it cannot be
520 * represented by the returned data type, then the nearest
521 * value representable using the requested type is
524 dst
[didx
].u
= src
[sidx
].f
< 0.0f
?
525 0u : (uint32_t) roundf(src
[sidx
].f
);
528 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
531 dst
[didx
].i
= MAX2(src
[sidx
].i
, 0);
533 case GLSL_TYPE_DOUBLE
: {
535 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
536 dst
[didx
].u
= tmp
< 0.0 ? 0u : (uint32_t) round(tmp
);
539 case GLSL_TYPE_UINT64
: {
541 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
542 dst
[didx
].i
= MIN2(tmp
, INT_MAX
);
545 case GLSL_TYPE_INT64
: {
547 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
548 dst
[didx
].i
= MAX2(tmp
, 0);
552 unreachable("invalid uniform type");
556 case GLSL_TYPE_INT64
:
557 switch (uni
->type
->base_type
) {
558 case GLSL_TYPE_UINT
: {
559 uint64_t tmp
= src
[sidx
].u
;
560 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
564 case GLSL_TYPE_SAMPLER
:
565 case GLSL_TYPE_IMAGE
: {
566 int64_t tmp
= src
[sidx
].i
;
567 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
570 case GLSL_TYPE_BOOL
: {
571 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
572 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
575 case GLSL_TYPE_UINT64
: {
577 memcpy(&u64
, &src
[sidx
].u
, sizeof(u64
));
578 int64_t tmp
= MIN2(u64
, INT_MAX
);
579 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
582 case GLSL_TYPE_FLOAT
: {
583 int64_t tmp
= (int64_t) roundf(src
[sidx
].f
);
584 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
587 case GLSL_TYPE_DOUBLE
: {
589 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
590 int64_t tmp
= (int64_t) round(d
);
591 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
595 assert(!"Should not get here.");
600 case GLSL_TYPE_UINT64
:
601 switch (uni
->type
->base_type
) {
602 case GLSL_TYPE_UINT
: {
603 uint64_t tmp
= src
[sidx
].u
;
604 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
608 case GLSL_TYPE_SAMPLER
:
609 case GLSL_TYPE_IMAGE
: {
610 int64_t tmp
= MAX2(src
[sidx
].i
, 0);
611 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
614 case GLSL_TYPE_BOOL
: {
615 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
616 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
619 case GLSL_TYPE_INT64
: {
621 memcpy(&i64
, &src
[sidx
].i
, sizeof(i64
));
622 uint64_t tmp
= MAX2(i64
, 0);
623 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
626 case GLSL_TYPE_FLOAT
: {
627 uint64_t tmp
= src
[sidx
].f
< 0.0f
?
628 0ull : (uint64_t) roundf(src
[sidx
].f
);
629 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
632 case GLSL_TYPE_DOUBLE
: {
634 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
635 uint64_t tmp
= (d
< 0.0) ? 0ull : (uint64_t) round(d
);
636 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
640 assert(!"Should not get here.");
646 assert(!"Should not get here.");
655 log_uniform(const void *values
, enum glsl_base_type basicType
,
656 unsigned rows
, unsigned cols
, unsigned count
,
658 const struct gl_shader_program
*shProg
,
660 const struct gl_uniform_storage
*uni
)
663 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
664 const unsigned elems
= rows
* cols
* count
;
665 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
667 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
668 "transpose = %s) to: ",
669 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
670 transpose
? "true" : "false");
671 for (unsigned i
= 0; i
< elems
; i
++) {
672 if (i
!= 0 && ((i
% rows
) == 0))
677 printf("%u ", v
[i
].u
);
680 printf("%d ", v
[i
].i
);
682 case GLSL_TYPE_UINT64
: {
684 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
685 printf("%" PRIu64
" ", tmp
);
688 case GLSL_TYPE_INT64
: {
690 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
691 printf("%" PRId64
" ", tmp
);
694 case GLSL_TYPE_FLOAT
:
695 printf("%g ", v
[i
].f
);
697 case GLSL_TYPE_DOUBLE
: {
699 memcpy(&tmp
, &v
[i
* 2].f
, sizeof(tmp
));
704 assert(!"Should not get here.");
714 log_program_parameters(const struct gl_shader_program
*shProg
)
716 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
717 if (shProg
->_LinkedShaders
[i
] == NULL
)
720 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
722 printf("Program %d %s shader parameters:\n",
723 shProg
->Name
, _mesa_shader_stage_to_string(i
));
724 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
725 printf("%s: %p %f %f %f %f\n",
726 prog
->Parameters
->Parameters
[j
].Name
,
727 prog
->Parameters
->ParameterValues
[j
],
728 prog
->Parameters
->ParameterValues
[j
][0].f
,
729 prog
->Parameters
->ParameterValues
[j
][1].f
,
730 prog
->Parameters
->ParameterValues
[j
][2].f
,
731 prog
->Parameters
->ParameterValues
[j
][3].f
);
739 * Propagate some values from uniform backing storage to driver storage
741 * Values propagated from uniform backing storage to driver storage
742 * have all format / type conversions previously requested by the
743 * driver applied. This function is most often called by the
744 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
747 * \param uni Uniform whose data is to be propagated to driver storage
748 * \param array_index If \c uni is an array, this is the element of
749 * the array to be propagated.
750 * \param count Number of array elements to propagate.
753 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
754 unsigned array_index
,
759 const unsigned components
= uni
->type
->vector_elements
;
760 const unsigned vectors
= uni
->type
->matrix_columns
;
761 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
763 /* Store the data in the driver's requested type in the driver's storage
766 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
768 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
769 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
770 uint8_t *dst
= (uint8_t *) store
->data
;
771 const unsigned extra_stride
=
772 store
->element_stride
- (vectors
* store
->vector_stride
);
774 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
777 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
779 __func__
, dst
, array_index
, components
,
780 vectors
, count
, store
->vector_stride
, extra_stride
);
783 dst
+= array_index
* store
->element_stride
;
785 switch (store
->format
) {
786 case uniform_native
: {
790 if (src_vector_byte_stride
== store
->vector_stride
) {
792 for (j
= 0; j
< count
; j
++) {
793 memcpy(dst
, src
, src_vector_byte_stride
* vectors
);
794 src
+= src_vector_byte_stride
* vectors
;
795 dst
+= store
->vector_stride
* vectors
;
800 /* Unigine Heaven benchmark gets here */
801 memcpy(dst
, src
, src_vector_byte_stride
* vectors
* count
);
802 src
+= src_vector_byte_stride
* vectors
* count
;
803 dst
+= store
->vector_stride
* vectors
* count
;
806 for (j
= 0; j
< count
; j
++) {
807 for (v
= 0; v
< vectors
; v
++) {
808 memcpy(dst
, src
, src_vector_byte_stride
);
809 src
+= src_vector_byte_stride
;
810 dst
+= store
->vector_stride
;
819 case uniform_int_float
: {
820 const int *isrc
= (const int *) src
;
825 for (j
= 0; j
< count
; j
++) {
826 for (v
= 0; v
< vectors
; v
++) {
827 for (c
= 0; c
< components
; c
++) {
828 ((float *) dst
)[c
] = (float) *isrc
;
832 dst
+= store
->vector_stride
;
841 assert(!"Should not get here.");
849 * Return printable string for a given GLSL_TYPE_x
852 glsl_type_name(enum glsl_base_type type
)
859 case GLSL_TYPE_FLOAT
:
861 case GLSL_TYPE_DOUBLE
:
863 case GLSL_TYPE_UINT64
:
865 case GLSL_TYPE_INT64
:
869 case GLSL_TYPE_SAMPLER
:
871 case GLSL_TYPE_IMAGE
:
873 case GLSL_TYPE_ATOMIC_UINT
:
874 return "atomic_uint";
875 case GLSL_TYPE_STRUCT
:
877 case GLSL_TYPE_INTERFACE
:
879 case GLSL_TYPE_ARRAY
:
883 case GLSL_TYPE_ERROR
:
891 static struct gl_uniform_storage
*
892 validate_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
893 unsigned *offset
, struct gl_context
*ctx
,
894 struct gl_shader_program
*shProg
,
895 enum glsl_base_type basicType
, unsigned src_components
)
897 struct gl_uniform_storage
*const uni
=
898 validate_uniform_parameters(location
, count
, offset
,
899 ctx
, shProg
, "glUniform");
903 if (uni
->type
->is_matrix()) {
904 /* Can't set matrix uniforms (like mat4) with glUniform */
905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
906 "glUniform%u(uniform \"%s\"@%d is matrix)",
907 src_components
, uni
->name
, location
);
911 /* Verify that the types are compatible. */
912 const unsigned components
= uni
->type
->vector_elements
;
914 if (components
!= src_components
) {
915 /* glUniformN() must match float/vecN type */
916 _mesa_error(ctx
, GL_INVALID_OPERATION
,
917 "glUniform%u(\"%s\"@%u has %u components, not %u)",
918 src_components
, uni
->name
, location
,
919 components
, src_components
);
924 switch (uni
->type
->base_type
) {
926 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
928 case GLSL_TYPE_SAMPLER
:
929 match
= (basicType
== GLSL_TYPE_INT
);
931 case GLSL_TYPE_IMAGE
:
932 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
935 match
= (basicType
== uni
->type
->base_type
);
940 _mesa_error(ctx
, GL_INVALID_OPERATION
,
941 "glUniform%u(\"%s\"@%d is %s, not %s)",
942 src_components
, uni
->name
, location
,
943 glsl_type_name(uni
->type
->base_type
),
944 glsl_type_name(basicType
));
948 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
949 log_uniform(values
, basicType
, components
, 1, count
,
950 false, shProg
, location
, uni
);
953 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
955 * "Setting a sampler's value to i selects texture image unit number
956 * i. The values of i range from zero to the implementation- dependent
957 * maximum supported number of texture image units."
959 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
962 * "Error Description Offending command
965 * INVALID_VALUE Numeric argument out of range Yes"
967 * Based on that, when an invalid sampler is specified, we generate a
968 * GL_INVALID_VALUE error and ignore the command.
970 if (uni
->type
->is_sampler()) {
971 for (int i
= 0; i
< count
; i
++) {
972 const unsigned texUnit
= ((unsigned *) values
)[i
];
974 /* check that the sampler (tex unit index) is legal */
975 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
976 _mesa_error(ctx
, GL_INVALID_VALUE
,
977 "glUniform1i(invalid sampler/tex unit index for "
978 "uniform %d)", location
);
982 /* We need to reset the validate flag on changes to samplers in case
983 * two different sampler types are set to the same texture unit.
985 ctx
->_Shader
->Validated
= GL_FALSE
;
988 if (uni
->type
->is_image()) {
989 for (int i
= 0; i
< count
; i
++) {
990 const int unit
= ((GLint
*) values
)[i
];
992 /* check that the image unit is legal */
993 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
994 _mesa_error(ctx
, GL_INVALID_VALUE
,
995 "glUniform1i(invalid image unit index for uniform %d)",
1006 _mesa_flush_vertices_for_uniforms(struct gl_context
*ctx
,
1007 const struct gl_uniform_storage
*uni
)
1009 /* Opaque uniforms have no storage unless they are bindless */
1010 if (!uni
->is_bindless
&& uni
->type
->contains_opaque()) {
1011 FLUSH_VERTICES(ctx
, 0);
1015 uint64_t new_driver_state
= 0;
1016 unsigned mask
= uni
->active_shader_mask
;
1019 unsigned index
= u_bit_scan(&mask
);
1021 assert(index
< MESA_SHADER_STAGES
);
1022 new_driver_state
|= ctx
->DriverFlags
.NewShaderConstants
[index
];
1025 FLUSH_VERTICES(ctx
, new_driver_state
? 0 : _NEW_PROGRAM_CONSTANTS
);
1026 ctx
->NewDriverState
|= new_driver_state
;
1030 * Called via glUniform*() functions.
1033 _mesa_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
1034 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1035 enum glsl_base_type basicType
, unsigned src_components
)
1038 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
1040 struct gl_uniform_storage
*uni
;
1041 if (_mesa_is_no_error_enabled(ctx
)) {
1042 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1044 * "If the value of location is -1, the Uniform* commands will
1045 * silently ignore the data passed in, and the current uniform values
1046 * will not be changed.
1051 uni
= shProg
->UniformRemapTable
[location
];
1053 /* The array index specified by the uniform location is just the
1054 * uniform location minus the base location of of the uniform.
1056 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1057 offset
= location
- uni
->remap_location
;
1059 uni
= validate_uniform(location
, count
, values
, &offset
, ctx
, shProg
,
1060 basicType
, src_components
);
1065 const unsigned components
= uni
->type
->vector_elements
;
1067 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1069 * "When loading N elements starting at an arbitrary position k in a
1070 * uniform declared as an array, elements k through k + N - 1 in the
1071 * array will be replaced with the new values. Values for any array
1072 * element that exceeds the highest array element index used, as
1073 * reported by GetActiveUniform, will be ignored by the GL."
1075 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1076 * will have already generated an error.
1078 if (uni
->array_elements
!= 0) {
1079 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1082 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1084 /* Store the data in the "actual type" backing storage for the uniform.
1086 if (!uni
->type
->is_boolean() && !uni
->is_bindless
) {
1087 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
1088 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1089 } else if (uni
->is_bindless
) {
1090 const union gl_constant_value
*src
=
1091 (const union gl_constant_value
*) values
;
1092 GLuint64
*dst
= (GLuint64
*)&uni
->storage
[components
* offset
].i
;
1093 const unsigned elems
= components
* count
;
1095 for (unsigned i
= 0; i
< elems
; i
++) {
1099 const union gl_constant_value
*src
=
1100 (const union gl_constant_value
*) values
;
1101 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
1102 const unsigned elems
= components
* count
;
1104 for (unsigned i
= 0; i
< elems
; i
++) {
1105 if (basicType
== GLSL_TYPE_FLOAT
) {
1106 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
1108 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
1113 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1115 /* If the uniform is a sampler, do the extra magic necessary to propagate
1116 * the changes through.
1118 if (uni
->type
->is_sampler()) {
1119 bool flushed
= false;
1121 shProg
->SamplersValidated
= GL_TRUE
;
1123 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1124 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1126 /* If the shader stage doesn't use the sampler uniform, skip this. */
1127 if (!uni
->opaque
[i
].active
)
1130 bool changed
= false;
1131 for (int j
= 0; j
< count
; j
++) {
1132 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1133 unsigned value
= ((unsigned *)values
)[j
];
1135 if (uni
->is_bindless
) {
1136 struct gl_bindless_sampler
*sampler
=
1137 &sh
->Program
->sh
.BindlessSamplers
[unit
];
1139 /* Mark this bindless sampler as bound to a texture unit.
1141 if (sampler
->unit
!= value
|| !sampler
->bound
) {
1142 sampler
->unit
= value
;
1145 sampler
->bound
= true;
1146 sh
->Program
->sh
.HasBoundBindlessSampler
= true;
1148 if (sh
->Program
->SamplerUnits
[unit
] != value
) {
1149 sh
->Program
->SamplerUnits
[unit
] = value
;
1157 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
);
1161 struct gl_program
*const prog
= sh
->Program
;
1162 _mesa_update_shader_textures_used(shProg
, prog
);
1163 if (ctx
->Driver
.SamplerUniformChange
)
1164 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
1169 /* If the uniform is an image, update the mapping from image
1170 * uniforms to image units present in the shader data structure.
1172 if (uni
->type
->is_image()) {
1173 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1174 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1176 /* If the shader stage doesn't use the image uniform, skip this. */
1177 if (!uni
->opaque
[i
].active
)
1180 for (int j
= 0; j
< count
; j
++) {
1181 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1182 unsigned value
= ((unsigned *)values
)[j
];
1184 if (uni
->is_bindless
) {
1185 struct gl_bindless_image
*image
=
1186 &sh
->Program
->sh
.BindlessImages
[unit
];
1188 /* Mark this bindless image as bound to an image unit.
1190 image
->unit
= value
;
1191 image
->bound
= true;
1192 sh
->Program
->sh
.HasBoundBindlessImage
= true;
1194 sh
->Program
->sh
.ImageUnits
[unit
] = value
;
1199 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
1204 * Called by glUniformMatrix*() functions.
1205 * Note: cols=2, rows=4 ==> array[2] of vec4
1208 _mesa_uniform_matrix(GLint location
, GLsizei count
,
1209 GLboolean transpose
, const void *values
,
1210 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1211 GLuint cols
, GLuint rows
, enum glsl_base_type basicType
)
1214 struct gl_uniform_storage
*const uni
=
1215 validate_uniform_parameters(location
, count
, &offset
,
1216 ctx
, shProg
, "glUniformMatrix");
1220 if (!uni
->type
->is_matrix()) {
1221 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1222 "glUniformMatrix(non-matrix uniform)");
1226 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
1227 const unsigned size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
1229 assert(!uni
->type
->is_sampler());
1230 const unsigned vectors
= uni
->type
->matrix_columns
;
1231 const unsigned components
= uni
->type
->vector_elements
;
1233 /* Verify that the types are compatible. This is greatly simplified for
1234 * matrices because they can only have a float base type.
1236 if (vectors
!= cols
|| components
!= rows
) {
1237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1238 "glUniformMatrix(matrix size mismatch)");
1242 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1243 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1246 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
1247 _mesa_error(ctx
, GL_INVALID_VALUE
,
1248 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1253 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1256 * "If any of the following conditions occur, an INVALID_OPERATION
1257 * error is generated by the Uniform* commands, and no uniform values
1262 * - if the uniform declared in the shader is not of type boolean and
1263 * the type indicated in the name of the Uniform* command used does
1264 * not match the type of the uniform"
1266 * There are no Boolean matrix types, so we do not need to allow
1267 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1269 if (uni
->type
->base_type
!= basicType
) {
1270 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1271 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1272 cols
, rows
, uni
->name
, location
,
1273 glsl_type_name(uni
->type
->base_type
),
1274 glsl_type_name(basicType
));
1278 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1279 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
1280 bool(transpose
), shProg
, location
, uni
);
1283 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1285 * "When loading N elements starting at an arbitrary position k in a
1286 * uniform declared as an array, elements k through k + N - 1 in the
1287 * array will be replaced with the new values. Values for any array
1288 * element that exceeds the highest array element index used, as
1289 * reported by GetActiveUniform, will be ignored by the GL."
1291 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1292 * will have already generated an error.
1294 if (uni
->array_elements
!= 0) {
1295 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1298 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1300 /* Store the data in the "actual type" backing storage for the uniform.
1302 const unsigned elements
= components
* vectors
;
1305 memcpy(&uni
->storage
[size_mul
* elements
* offset
], values
,
1306 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
1307 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1308 /* Copy and transpose the matrix.
1310 const float *src
= (const float *)values
;
1311 float *dst
= &uni
->storage
[elements
* offset
].f
;
1313 for (int i
= 0; i
< count
; i
++) {
1314 for (unsigned r
= 0; r
< rows
; r
++) {
1315 for (unsigned c
= 0; c
< cols
; c
++) {
1316 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1324 assert(basicType
== GLSL_TYPE_DOUBLE
);
1325 const double *src
= (const double *)values
;
1326 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
1328 for (int i
= 0; i
< count
; i
++) {
1329 for (unsigned r
= 0; r
< rows
; r
++) {
1330 for (unsigned c
= 0; c
< cols
; c
++) {
1331 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1340 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1344 update_bound_bindless_sampler_flag(struct gl_program
*prog
)
1348 if (likely(!prog
->sh
.HasBoundBindlessSampler
))
1351 for (i
= 0; i
< prog
->sh
.NumBindlessSamplers
; i
++) {
1352 struct gl_bindless_sampler
*sampler
= &prog
->sh
.BindlessSamplers
[i
];
1357 prog
->sh
.HasBoundBindlessSampler
= false;
1361 update_bound_bindless_image_flag(struct gl_program
*prog
)
1365 if (likely(!prog
->sh
.HasBoundBindlessImage
))
1368 for (i
= 0; i
< prog
->sh
.NumBindlessImages
; i
++) {
1369 struct gl_bindless_image
*image
= &prog
->sh
.BindlessImages
[i
];
1374 prog
->sh
.HasBoundBindlessImage
= false;
1378 * Called via glUniformHandleui64*ARB() functions.
1381 _mesa_uniform_handle(GLint location
, GLsizei count
, const GLvoid
*values
,
1382 struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1385 struct gl_uniform_storage
*uni
;
1387 if (_mesa_is_no_error_enabled(ctx
)) {
1388 /* From Section 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1390 * "If the value of location is -1, the Uniform* commands will
1391 * silently ignore the data passed in, and the current uniform values
1392 * will not be changed.
1397 uni
= shProg
->UniformRemapTable
[location
];
1399 /* The array index specified by the uniform location is just the
1400 * uniform location minus the base location of of the uniform.
1402 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1403 offset
= location
- uni
->remap_location
;
1405 uni
= validate_uniform_parameters(location
, count
, &offset
,
1406 ctx
, shProg
, "glUniformHandleui64*ARB");
1410 if (!uni
->is_bindless
) {
1411 /* From section "Errors" of the ARB_bindless_texture spec:
1413 * "The error INVALID_OPERATION is generated by
1414 * UniformHandleui64{v}ARB if the sampler or image uniform being
1415 * updated has the "bound_sampler" or "bound_image" layout qualifier."
1417 * From section 4.4.6 of the ARB_bindless_texture spec:
1419 * "In the absence of these qualifiers, sampler and image uniforms are
1420 * considered "bound". Additionally, if GL_ARB_bindless_texture is
1421 * not enabled, these uniforms are considered "bound"."
1423 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1424 "glUniformHandleui64*ARB(non-bindless sampler/image uniform)");
1429 const unsigned components
= uni
->type
->vector_elements
;
1430 const int size_mul
= 2;
1432 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1433 log_uniform(values
, GLSL_TYPE_UINT64
, components
, 1, count
,
1434 false, shProg
, location
, uni
);
1437 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1439 * "When loading N elements starting at an arbitrary position k in a
1440 * uniform declared as an array, elements k through k + N - 1 in the
1441 * array will be replaced with the new values. Values for any array
1442 * element that exceeds the highest array element index used, as
1443 * reported by GetActiveUniform, will be ignored by the GL."
1445 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1446 * will have already generated an error.
1448 if (uni
->array_elements
!= 0) {
1449 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1452 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1454 /* Store the data in the "actual type" backing storage for the uniform.
1456 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
1457 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1459 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1461 if (uni
->type
->is_sampler()) {
1462 /* Mark this bindless sampler as not bound to a texture unit because
1463 * it refers to a texture handle.
1465 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1466 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1468 /* If the shader stage doesn't use the sampler uniform, skip this. */
1469 if (!uni
->opaque
[i
].active
)
1472 for (int j
= 0; j
< count
; j
++) {
1473 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1474 struct gl_bindless_sampler
*sampler
=
1475 &sh
->Program
->sh
.BindlessSamplers
[unit
];
1477 sampler
->bound
= false;
1480 update_bound_bindless_sampler_flag(sh
->Program
);
1484 if (uni
->type
->is_image()) {
1485 /* Mark this bindless image as not bound to an image unit because it
1486 * refers to a texture handle.
1488 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1489 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1491 /* If the shader stage doesn't use the sampler uniform, skip this. */
1492 if (!uni
->opaque
[i
].active
)
1495 for (int j
= 0; j
< count
; j
++) {
1496 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1497 struct gl_bindless_image
*image
=
1498 &sh
->Program
->sh
.BindlessImages
[unit
];
1500 image
->bound
= false;
1503 update_bound_bindless_image_flag(sh
->Program
);
1509 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1510 char *errMsg
, size_t errMsgLength
)
1512 /* Shader does not have samplers. */
1513 if (shProg
->data
->NumUniformStorage
== 0)
1516 if (!shProg
->SamplersValidated
) {
1517 _mesa_snprintf(errMsg
, errMsgLength
,
1518 "active samplers with a different type "
1519 "refer to the same texture image unit");
1526 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1528 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1529 * OpenGL 4.1 spec says:
1531 * "[INVALID_OPERATION] is generated by any command that transfers
1532 * vertices to the GL if:
1536 * - Any two active samplers in the current program object are of
1537 * different types, but refer to the same texture image unit.
1539 * - The number of active samplers in the program exceeds the
1540 * maximum number of texture image units allowed."
1544 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1545 unsigned active_samplers
= 0;
1546 const struct gl_program
**prog
=
1547 (const struct gl_program
**) pipeline
->CurrentProgram
;
1550 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1552 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1556 mask
= prog
[idx
]->SamplersUsed
;
1558 const int s
= u_bit_scan(&mask
);
1559 GLuint unit
= prog
[idx
]->SamplerUnits
[s
];
1560 GLuint tgt
= prog
[idx
]->sh
.SamplerTargets
[s
];
1562 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1563 * great job of eliminating unused uniforms currently so for now
1564 * don't throw an error if two sampler types both point to 0.
1569 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1571 ralloc_asprintf(pipeline
,
1573 "Texture unit %d is accessed with 2 different types",
1574 prog
[idx
]->Id
, unit
);
1578 TexturesUsed
[unit
] |= (1 << tgt
);
1581 active_samplers
+= prog
[idx
]->info
.num_textures
;
1584 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1586 ralloc_asprintf(pipeline
,
1587 "the number of active samplers %d exceed the "
1589 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);