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 (returnType
== GLSL_TYPE_UINT64
&& uni
->is_bindless
)) {
363 memcpy(paramsOut
, src
, bytes
);
365 union gl_constant_value
*const dst
=
366 (union gl_constant_value
*) paramsOut
;
367 /* This code could be optimized by putting the loop inside the switch
368 * statements. However, this is not expected to be
369 * performance-critical code.
371 for (unsigned i
= 0; i
< elements
; i
++) {
375 switch (returnType
) {
376 case GLSL_TYPE_FLOAT
:
377 switch (uni
->type
->base_type
) {
379 dst
[didx
].f
= (float) src
[sidx
].u
;
382 case GLSL_TYPE_SAMPLER
:
383 case GLSL_TYPE_IMAGE
:
384 dst
[didx
].f
= (float) src
[sidx
].i
;
387 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
389 case GLSL_TYPE_DOUBLE
: {
391 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
395 case GLSL_TYPE_UINT64
: {
397 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
401 case GLSL_TYPE_INT64
: {
403 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
408 assert(!"Should not get here.");
413 case GLSL_TYPE_DOUBLE
:
414 switch (uni
->type
->base_type
) {
415 case GLSL_TYPE_UINT
: {
416 double tmp
= src
[sidx
].u
;
417 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
421 case GLSL_TYPE_SAMPLER
:
422 case GLSL_TYPE_IMAGE
: {
423 double tmp
= src
[sidx
].i
;
424 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
427 case GLSL_TYPE_BOOL
: {
428 double tmp
= src
[sidx
].i
? 1.0 : 0.0;
429 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
432 case GLSL_TYPE_FLOAT
: {
433 double tmp
= src
[sidx
].f
;
434 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
437 case GLSL_TYPE_UINT64
: {
440 memcpy(&tmpu
, &src
[sidx
].u
, sizeof(tmpu
));
442 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
445 case GLSL_TYPE_INT64
: {
448 memcpy(&tmpi
, &src
[sidx
].i
, sizeof(tmpi
));
450 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
454 assert(!"Should not get here.");
460 switch (uni
->type
->base_type
) {
461 case GLSL_TYPE_FLOAT
:
462 /* While the GL 3.2 core spec doesn't explicitly
463 * state how conversion of float uniforms to integer
464 * values works, in section 6.2 "State Tables" on
467 * "Unless otherwise specified, when floating
468 * point state is returned as integer values or
469 * integer state is returned as floating-point
470 * values it is converted in the fashion
471 * described in section 6.1.2"
473 * That section, on page 248, says:
475 * "If GetIntegerv or GetInteger64v are called,
476 * a floating-point value is rounded to the
477 * nearest integer..."
479 dst
[didx
].i
= (int64_t) roundf(src
[sidx
].f
);
482 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
485 dst
[didx
].i
= MIN2(src
[sidx
].i
, INT_MAX
);
487 case GLSL_TYPE_DOUBLE
: {
489 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
490 dst
[didx
].i
= (int64_t) round(tmp
);
493 case GLSL_TYPE_UINT64
: {
495 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
499 case GLSL_TYPE_INT64
: {
501 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
506 assert(!"Should not get here.");
512 switch (uni
->type
->base_type
) {
513 case GLSL_TYPE_FLOAT
:
514 /* The spec isn't terribly clear how to handle negative
515 * values with an unsigned return type.
517 * GL 4.5 section 2.2.2 ("Data Conversions for State
518 * Query Commands") says:
520 * "If a value is so large in magnitude that it cannot be
521 * represented by the returned data type, then the nearest
522 * value representable using the requested type is
525 dst
[didx
].u
= src
[sidx
].f
< 0.0f
?
526 0u : (uint32_t) roundf(src
[sidx
].f
);
529 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
532 dst
[didx
].i
= MAX2(src
[sidx
].i
, 0);
534 case GLSL_TYPE_DOUBLE
: {
536 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
537 dst
[didx
].u
= tmp
< 0.0 ? 0u : (uint32_t) round(tmp
);
540 case GLSL_TYPE_UINT64
: {
542 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
543 dst
[didx
].i
= MIN2(tmp
, INT_MAX
);
546 case GLSL_TYPE_INT64
: {
548 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
549 dst
[didx
].i
= MAX2(tmp
, 0);
553 unreachable("invalid uniform type");
557 case GLSL_TYPE_INT64
:
558 switch (uni
->type
->base_type
) {
559 case GLSL_TYPE_UINT
: {
560 uint64_t tmp
= src
[sidx
].u
;
561 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
565 case GLSL_TYPE_SAMPLER
:
566 case GLSL_TYPE_IMAGE
: {
567 int64_t tmp
= src
[sidx
].i
;
568 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
571 case GLSL_TYPE_BOOL
: {
572 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
573 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
576 case GLSL_TYPE_UINT64
: {
578 memcpy(&u64
, &src
[sidx
].u
, sizeof(u64
));
579 int64_t tmp
= MIN2(u64
, INT_MAX
);
580 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
583 case GLSL_TYPE_FLOAT
: {
584 int64_t tmp
= (int64_t) roundf(src
[sidx
].f
);
585 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
588 case GLSL_TYPE_DOUBLE
: {
590 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
591 int64_t tmp
= (int64_t) round(d
);
592 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
596 assert(!"Should not get here.");
601 case GLSL_TYPE_UINT64
:
602 switch (uni
->type
->base_type
) {
603 case GLSL_TYPE_UINT
: {
604 uint64_t tmp
= src
[sidx
].u
;
605 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
609 case GLSL_TYPE_SAMPLER
:
610 case GLSL_TYPE_IMAGE
: {
611 int64_t tmp
= MAX2(src
[sidx
].i
, 0);
612 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
615 case GLSL_TYPE_BOOL
: {
616 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
617 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
620 case GLSL_TYPE_INT64
: {
622 memcpy(&i64
, &src
[sidx
].i
, sizeof(i64
));
623 uint64_t tmp
= MAX2(i64
, 0);
624 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
627 case GLSL_TYPE_FLOAT
: {
628 uint64_t tmp
= src
[sidx
].f
< 0.0f
?
629 0ull : (uint64_t) roundf(src
[sidx
].f
);
630 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
633 case GLSL_TYPE_DOUBLE
: {
635 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
636 uint64_t tmp
= (d
< 0.0) ? 0ull : (uint64_t) round(d
);
637 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
641 assert(!"Should not get here.");
647 assert(!"Should not get here.");
656 log_uniform(const void *values
, enum glsl_base_type basicType
,
657 unsigned rows
, unsigned cols
, unsigned count
,
659 const struct gl_shader_program
*shProg
,
661 const struct gl_uniform_storage
*uni
)
664 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
665 const unsigned elems
= rows
* cols
* count
;
666 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
668 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
669 "transpose = %s) to: ",
670 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
671 transpose
? "true" : "false");
672 for (unsigned i
= 0; i
< elems
; i
++) {
673 if (i
!= 0 && ((i
% rows
) == 0))
678 printf("%u ", v
[i
].u
);
681 printf("%d ", v
[i
].i
);
683 case GLSL_TYPE_UINT64
: {
685 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
686 printf("%" PRIu64
" ", tmp
);
689 case GLSL_TYPE_INT64
: {
691 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
692 printf("%" PRId64
" ", tmp
);
695 case GLSL_TYPE_FLOAT
:
696 printf("%g ", v
[i
].f
);
698 case GLSL_TYPE_DOUBLE
: {
700 memcpy(&tmp
, &v
[i
* 2].f
, sizeof(tmp
));
705 assert(!"Should not get here.");
715 log_program_parameters(const struct gl_shader_program
*shProg
)
717 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
718 if (shProg
->_LinkedShaders
[i
] == NULL
)
721 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
723 printf("Program %d %s shader parameters:\n",
724 shProg
->Name
, _mesa_shader_stage_to_string(i
));
725 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
726 unsigned pvo
= prog
->Parameters
->ParameterValueOffset
[j
];
727 printf("%s: %u %p %f %f %f %f\n",
728 prog
->Parameters
->Parameters
[j
].Name
,
730 prog
->Parameters
->ParameterValues
+ pvo
,
731 prog
->Parameters
->ParameterValues
[pvo
].f
,
732 prog
->Parameters
->ParameterValues
[pvo
+ 1].f
,
733 prog
->Parameters
->ParameterValues
[pvo
+ 2].f
,
734 prog
->Parameters
->ParameterValues
[pvo
+ 3].f
);
742 * Propagate some values from uniform backing storage to driver storage
744 * Values propagated from uniform backing storage to driver storage
745 * have all format / type conversions previously requested by the
746 * driver applied. This function is most often called by the
747 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
750 * \param uni Uniform whose data is to be propagated to driver storage
751 * \param array_index If \c uni is an array, this is the element of
752 * the array to be propagated.
753 * \param count Number of array elements to propagate.
756 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
757 unsigned array_index
,
762 const unsigned components
= uni
->type
->vector_elements
;
763 const unsigned vectors
= uni
->type
->matrix_columns
;
764 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
766 /* Store the data in the driver's requested type in the driver's storage
769 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
771 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
772 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
773 uint8_t *dst
= (uint8_t *) store
->data
;
774 const unsigned extra_stride
=
775 store
->element_stride
- (vectors
* store
->vector_stride
);
777 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
780 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
782 __func__
, dst
, array_index
, components
,
783 vectors
, count
, store
->vector_stride
, extra_stride
);
786 dst
+= array_index
* store
->element_stride
;
788 switch (store
->format
) {
789 case uniform_native
: {
793 if (src_vector_byte_stride
== store
->vector_stride
) {
795 for (j
= 0; j
< count
; j
++) {
796 memcpy(dst
, src
, src_vector_byte_stride
* vectors
);
797 src
+= src_vector_byte_stride
* vectors
;
798 dst
+= store
->vector_stride
* vectors
;
803 /* Unigine Heaven benchmark gets here */
804 memcpy(dst
, src
, src_vector_byte_stride
* vectors
* count
);
805 src
+= src_vector_byte_stride
* vectors
* count
;
806 dst
+= store
->vector_stride
* vectors
* count
;
809 for (j
= 0; j
< count
; j
++) {
810 for (v
= 0; v
< vectors
; v
++) {
811 memcpy(dst
, src
, src_vector_byte_stride
);
812 src
+= src_vector_byte_stride
;
813 dst
+= store
->vector_stride
;
822 case uniform_int_float
: {
823 const int *isrc
= (const int *) src
;
828 for (j
= 0; j
< count
; j
++) {
829 for (v
= 0; v
< vectors
; v
++) {
830 for (c
= 0; c
< components
; c
++) {
831 ((float *) dst
)[c
] = (float) *isrc
;
835 dst
+= store
->vector_stride
;
844 assert(!"Should not get here.");
852 * Return printable string for a given GLSL_TYPE_x
855 glsl_type_name(enum glsl_base_type type
)
862 case GLSL_TYPE_FLOAT
:
864 case GLSL_TYPE_DOUBLE
:
866 case GLSL_TYPE_UINT64
:
868 case GLSL_TYPE_INT64
:
872 case GLSL_TYPE_SAMPLER
:
874 case GLSL_TYPE_IMAGE
:
876 case GLSL_TYPE_ATOMIC_UINT
:
877 return "atomic_uint";
878 case GLSL_TYPE_STRUCT
:
880 case GLSL_TYPE_INTERFACE
:
882 case GLSL_TYPE_ARRAY
:
886 case GLSL_TYPE_ERROR
:
894 static struct gl_uniform_storage
*
895 validate_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
896 unsigned *offset
, struct gl_context
*ctx
,
897 struct gl_shader_program
*shProg
,
898 enum glsl_base_type basicType
, unsigned src_components
)
900 struct gl_uniform_storage
*const uni
=
901 validate_uniform_parameters(location
, count
, offset
,
902 ctx
, shProg
, "glUniform");
906 if (uni
->type
->is_matrix()) {
907 /* Can't set matrix uniforms (like mat4) with glUniform */
908 _mesa_error(ctx
, GL_INVALID_OPERATION
,
909 "glUniform%u(uniform \"%s\"@%d is matrix)",
910 src_components
, uni
->name
, location
);
914 /* Verify that the types are compatible. */
915 const unsigned components
= uni
->type
->vector_elements
;
917 if (components
!= src_components
) {
918 /* glUniformN() must match float/vecN type */
919 _mesa_error(ctx
, GL_INVALID_OPERATION
,
920 "glUniform%u(\"%s\"@%u has %u components, not %u)",
921 src_components
, uni
->name
, location
,
922 components
, src_components
);
927 switch (uni
->type
->base_type
) {
929 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
931 case GLSL_TYPE_SAMPLER
:
932 match
= (basicType
== GLSL_TYPE_INT
);
934 case GLSL_TYPE_IMAGE
:
935 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
938 match
= (basicType
== uni
->type
->base_type
);
943 _mesa_error(ctx
, GL_INVALID_OPERATION
,
944 "glUniform%u(\"%s\"@%d is %s, not %s)",
945 src_components
, uni
->name
, location
,
946 glsl_type_name(uni
->type
->base_type
),
947 glsl_type_name(basicType
));
951 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
952 log_uniform(values
, basicType
, components
, 1, count
,
953 false, shProg
, location
, uni
);
956 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
958 * "Setting a sampler's value to i selects texture image unit number
959 * i. The values of i range from zero to the implementation- dependent
960 * maximum supported number of texture image units."
962 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
965 * "Error Description Offending command
968 * INVALID_VALUE Numeric argument out of range Yes"
970 * Based on that, when an invalid sampler is specified, we generate a
971 * GL_INVALID_VALUE error and ignore the command.
973 if (uni
->type
->is_sampler()) {
974 for (int i
= 0; i
< count
; i
++) {
975 const unsigned texUnit
= ((unsigned *) values
)[i
];
977 /* check that the sampler (tex unit index) is legal */
978 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
979 _mesa_error(ctx
, GL_INVALID_VALUE
,
980 "glUniform1i(invalid sampler/tex unit index for "
981 "uniform %d)", location
);
985 /* We need to reset the validate flag on changes to samplers in case
986 * two different sampler types are set to the same texture unit.
988 ctx
->_Shader
->Validated
= GL_FALSE
;
991 if (uni
->type
->is_image()) {
992 for (int i
= 0; i
< count
; i
++) {
993 const int unit
= ((GLint
*) values
)[i
];
995 /* check that the image unit is legal */
996 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
997 _mesa_error(ctx
, GL_INVALID_VALUE
,
998 "glUniform1i(invalid image unit index for uniform %d)",
1009 _mesa_flush_vertices_for_uniforms(struct gl_context
*ctx
,
1010 const struct gl_uniform_storage
*uni
)
1012 /* Opaque uniforms have no storage unless they are bindless */
1013 if (!uni
->is_bindless
&& uni
->type
->contains_opaque()) {
1014 FLUSH_VERTICES(ctx
, 0);
1018 uint64_t new_driver_state
= 0;
1019 unsigned mask
= uni
->active_shader_mask
;
1022 unsigned index
= u_bit_scan(&mask
);
1024 assert(index
< MESA_SHADER_STAGES
);
1025 new_driver_state
|= ctx
->DriverFlags
.NewShaderConstants
[index
];
1028 FLUSH_VERTICES(ctx
, new_driver_state
? 0 : _NEW_PROGRAM_CONSTANTS
);
1029 ctx
->NewDriverState
|= new_driver_state
;
1033 * Called via glUniform*() functions.
1036 _mesa_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
1037 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1038 enum glsl_base_type basicType
, unsigned src_components
)
1041 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
1043 struct gl_uniform_storage
*uni
;
1044 if (_mesa_is_no_error_enabled(ctx
)) {
1045 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1047 * "If the value of location is -1, the Uniform* commands will
1048 * silently ignore the data passed in, and the current uniform values
1049 * will not be changed.
1054 uni
= shProg
->UniformRemapTable
[location
];
1056 /* The array index specified by the uniform location is just the
1057 * uniform location minus the base location of of the uniform.
1059 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1060 offset
= location
- uni
->remap_location
;
1062 uni
= validate_uniform(location
, count
, values
, &offset
, ctx
, shProg
,
1063 basicType
, src_components
);
1068 const unsigned components
= uni
->type
->vector_elements
;
1070 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1072 * "When loading N elements starting at an arbitrary position k in a
1073 * uniform declared as an array, elements k through k + N - 1 in the
1074 * array will be replaced with the new values. Values for any array
1075 * element that exceeds the highest array element index used, as
1076 * reported by GetActiveUniform, will be ignored by the GL."
1078 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1079 * will have already generated an error.
1081 if (uni
->array_elements
!= 0) {
1082 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1085 /* We check samplers for changes and flush if needed in the sampler
1086 * handling code further down, so just skip them here.
1088 if (!uni
->type
->is_sampler()) {
1089 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1092 /* Store the data in the "actual type" backing storage for the uniform.
1094 if (!uni
->type
->is_boolean() && !uni
->is_bindless
) {
1095 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
1096 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1097 } else if (uni
->is_bindless
) {
1098 const union gl_constant_value
*src
=
1099 (const union gl_constant_value
*) values
;
1100 GLuint64
*dst
= (GLuint64
*)&uni
->storage
[components
* offset
].i
;
1101 const unsigned elems
= components
* count
;
1103 for (unsigned i
= 0; i
< elems
; i
++) {
1107 const union gl_constant_value
*src
=
1108 (const union gl_constant_value
*) values
;
1109 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
1110 const unsigned elems
= components
* count
;
1112 for (unsigned i
= 0; i
< elems
; i
++) {
1113 if (basicType
== GLSL_TYPE_FLOAT
) {
1114 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
1116 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
1121 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1123 /* If the uniform is a sampler, do the extra magic necessary to propagate
1124 * the changes through.
1126 if (uni
->type
->is_sampler()) {
1127 bool flushed
= false;
1129 shProg
->SamplersValidated
= GL_TRUE
;
1131 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1132 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1134 /* If the shader stage doesn't use the sampler uniform, skip this. */
1135 if (!uni
->opaque
[i
].active
)
1138 bool changed
= false;
1139 for (int j
= 0; j
< count
; j
++) {
1140 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1141 unsigned value
= ((unsigned *)values
)[j
];
1143 if (uni
->is_bindless
) {
1144 struct gl_bindless_sampler
*sampler
=
1145 &sh
->Program
->sh
.BindlessSamplers
[unit
];
1147 /* Mark this bindless sampler as bound to a texture unit.
1149 if (sampler
->unit
!= value
|| !sampler
->bound
) {
1150 sampler
->unit
= value
;
1153 sampler
->bound
= true;
1154 sh
->Program
->sh
.HasBoundBindlessSampler
= true;
1156 if (sh
->Program
->SamplerUnits
[unit
] != value
) {
1157 sh
->Program
->SamplerUnits
[unit
] = value
;
1165 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
);
1169 struct gl_program
*const prog
= sh
->Program
;
1170 _mesa_update_shader_textures_used(shProg
, prog
);
1171 if (ctx
->Driver
.SamplerUniformChange
)
1172 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
1177 /* If the uniform is an image, update the mapping from image
1178 * uniforms to image units present in the shader data structure.
1180 if (uni
->type
->is_image()) {
1181 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1182 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1184 /* If the shader stage doesn't use the image uniform, skip this. */
1185 if (!uni
->opaque
[i
].active
)
1188 for (int j
= 0; j
< count
; j
++) {
1189 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1190 unsigned value
= ((unsigned *)values
)[j
];
1192 if (uni
->is_bindless
) {
1193 struct gl_bindless_image
*image
=
1194 &sh
->Program
->sh
.BindlessImages
[unit
];
1196 /* Mark this bindless image as bound to an image unit.
1198 image
->unit
= value
;
1199 image
->bound
= true;
1200 sh
->Program
->sh
.HasBoundBindlessImage
= true;
1202 sh
->Program
->sh
.ImageUnits
[unit
] = value
;
1207 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
1212 * Called by glUniformMatrix*() functions.
1213 * Note: cols=2, rows=4 ==> array[2] of vec4
1216 _mesa_uniform_matrix(GLint location
, GLsizei count
,
1217 GLboolean transpose
, const void *values
,
1218 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1219 GLuint cols
, GLuint rows
, enum glsl_base_type basicType
)
1222 struct gl_uniform_storage
*const uni
=
1223 validate_uniform_parameters(location
, count
, &offset
,
1224 ctx
, shProg
, "glUniformMatrix");
1228 if (!uni
->type
->is_matrix()) {
1229 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1230 "glUniformMatrix(non-matrix uniform)");
1234 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
1235 const unsigned size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
1237 assert(!uni
->type
->is_sampler());
1238 const unsigned vectors
= uni
->type
->matrix_columns
;
1239 const unsigned components
= uni
->type
->vector_elements
;
1241 /* Verify that the types are compatible. This is greatly simplified for
1242 * matrices because they can only have a float base type.
1244 if (vectors
!= cols
|| components
!= rows
) {
1245 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1246 "glUniformMatrix(matrix size mismatch)");
1250 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1251 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1254 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
1255 _mesa_error(ctx
, GL_INVALID_VALUE
,
1256 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1261 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1264 * "If any of the following conditions occur, an INVALID_OPERATION
1265 * error is generated by the Uniform* commands, and no uniform values
1270 * - if the uniform declared in the shader is not of type boolean and
1271 * the type indicated in the name of the Uniform* command used does
1272 * not match the type of the uniform"
1274 * There are no Boolean matrix types, so we do not need to allow
1275 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1277 if (uni
->type
->base_type
!= basicType
) {
1278 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1279 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1280 cols
, rows
, uni
->name
, location
,
1281 glsl_type_name(uni
->type
->base_type
),
1282 glsl_type_name(basicType
));
1286 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1287 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
1288 bool(transpose
), shProg
, location
, uni
);
1291 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1293 * "When loading N elements starting at an arbitrary position k in a
1294 * uniform declared as an array, elements k through k + N - 1 in the
1295 * array will be replaced with the new values. Values for any array
1296 * element that exceeds the highest array element index used, as
1297 * reported by GetActiveUniform, will be ignored by the GL."
1299 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1300 * will have already generated an error.
1302 if (uni
->array_elements
!= 0) {
1303 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1306 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1308 /* Store the data in the "actual type" backing storage for the uniform.
1310 const unsigned elements
= components
* vectors
;
1313 memcpy(&uni
->storage
[size_mul
* elements
* offset
], values
,
1314 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
1315 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1316 /* Copy and transpose the matrix.
1318 const float *src
= (const float *)values
;
1319 float *dst
= &uni
->storage
[elements
* offset
].f
;
1321 for (int i
= 0; i
< count
; i
++) {
1322 for (unsigned r
= 0; r
< rows
; r
++) {
1323 for (unsigned c
= 0; c
< cols
; c
++) {
1324 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1332 assert(basicType
== GLSL_TYPE_DOUBLE
);
1333 const double *src
= (const double *)values
;
1334 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
1336 for (int i
= 0; i
< count
; i
++) {
1337 for (unsigned r
= 0; r
< rows
; r
++) {
1338 for (unsigned c
= 0; c
< cols
; c
++) {
1339 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1348 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1352 update_bound_bindless_sampler_flag(struct gl_program
*prog
)
1356 if (likely(!prog
->sh
.HasBoundBindlessSampler
))
1359 for (i
= 0; i
< prog
->sh
.NumBindlessSamplers
; i
++) {
1360 struct gl_bindless_sampler
*sampler
= &prog
->sh
.BindlessSamplers
[i
];
1365 prog
->sh
.HasBoundBindlessSampler
= false;
1369 update_bound_bindless_image_flag(struct gl_program
*prog
)
1373 if (likely(!prog
->sh
.HasBoundBindlessImage
))
1376 for (i
= 0; i
< prog
->sh
.NumBindlessImages
; i
++) {
1377 struct gl_bindless_image
*image
= &prog
->sh
.BindlessImages
[i
];
1382 prog
->sh
.HasBoundBindlessImage
= false;
1386 * Called via glUniformHandleui64*ARB() functions.
1389 _mesa_uniform_handle(GLint location
, GLsizei count
, const GLvoid
*values
,
1390 struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1393 struct gl_uniform_storage
*uni
;
1395 if (_mesa_is_no_error_enabled(ctx
)) {
1396 /* From Section 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1398 * "If the value of location is -1, the Uniform* commands will
1399 * silently ignore the data passed in, and the current uniform values
1400 * will not be changed.
1405 uni
= shProg
->UniformRemapTable
[location
];
1407 /* The array index specified by the uniform location is just the
1408 * uniform location minus the base location of of the uniform.
1410 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1411 offset
= location
- uni
->remap_location
;
1413 uni
= validate_uniform_parameters(location
, count
, &offset
,
1414 ctx
, shProg
, "glUniformHandleui64*ARB");
1418 if (!uni
->is_bindless
) {
1419 /* From section "Errors" of the ARB_bindless_texture spec:
1421 * "The error INVALID_OPERATION is generated by
1422 * UniformHandleui64{v}ARB if the sampler or image uniform being
1423 * updated has the "bound_sampler" or "bound_image" layout qualifier."
1425 * From section 4.4.6 of the ARB_bindless_texture spec:
1427 * "In the absence of these qualifiers, sampler and image uniforms are
1428 * considered "bound". Additionally, if GL_ARB_bindless_texture is
1429 * not enabled, these uniforms are considered "bound"."
1431 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1432 "glUniformHandleui64*ARB(non-bindless sampler/image uniform)");
1437 const unsigned components
= uni
->type
->vector_elements
;
1438 const int size_mul
= 2;
1440 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1441 log_uniform(values
, GLSL_TYPE_UINT64
, components
, 1, count
,
1442 false, shProg
, location
, uni
);
1445 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1447 * "When loading N elements starting at an arbitrary position k in a
1448 * uniform declared as an array, elements k through k + N - 1 in the
1449 * array will be replaced with the new values. Values for any array
1450 * element that exceeds the highest array element index used, as
1451 * reported by GetActiveUniform, will be ignored by the GL."
1453 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1454 * will have already generated an error.
1456 if (uni
->array_elements
!= 0) {
1457 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1460 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1462 /* Store the data in the "actual type" backing storage for the uniform.
1464 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
1465 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1467 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1469 if (uni
->type
->is_sampler()) {
1470 /* Mark this bindless sampler as not bound to a texture unit because
1471 * it refers to a texture handle.
1473 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1474 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1476 /* If the shader stage doesn't use the sampler uniform, skip this. */
1477 if (!uni
->opaque
[i
].active
)
1480 for (int j
= 0; j
< count
; j
++) {
1481 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1482 struct gl_bindless_sampler
*sampler
=
1483 &sh
->Program
->sh
.BindlessSamplers
[unit
];
1485 sampler
->bound
= false;
1488 update_bound_bindless_sampler_flag(sh
->Program
);
1492 if (uni
->type
->is_image()) {
1493 /* Mark this bindless image as not bound to an image unit because it
1494 * refers to a texture handle.
1496 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1497 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1499 /* If the shader stage doesn't use the sampler uniform, skip this. */
1500 if (!uni
->opaque
[i
].active
)
1503 for (int j
= 0; j
< count
; j
++) {
1504 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1505 struct gl_bindless_image
*image
=
1506 &sh
->Program
->sh
.BindlessImages
[unit
];
1508 image
->bound
= false;
1511 update_bound_bindless_image_flag(sh
->Program
);
1517 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1518 char *errMsg
, size_t errMsgLength
)
1520 /* Shader does not have samplers. */
1521 if (shProg
->data
->NumUniformStorage
== 0)
1524 if (!shProg
->SamplersValidated
) {
1525 _mesa_snprintf(errMsg
, errMsgLength
,
1526 "active samplers with a different type "
1527 "refer to the same texture image unit");
1534 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1536 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1537 * OpenGL 4.1 spec says:
1539 * "[INVALID_OPERATION] is generated by any command that transfers
1540 * vertices to the GL if:
1544 * - Any two active samplers in the current program object are of
1545 * different types, but refer to the same texture image unit.
1547 * - The number of active samplers in the program exceeds the
1548 * maximum number of texture image units allowed."
1552 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1553 unsigned active_samplers
= 0;
1554 const struct gl_program
**prog
=
1555 (const struct gl_program
**) pipeline
->CurrentProgram
;
1558 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1560 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1564 mask
= prog
[idx
]->SamplersUsed
;
1566 const int s
= u_bit_scan(&mask
);
1567 GLuint unit
= prog
[idx
]->SamplerUnits
[s
];
1568 GLuint tgt
= prog
[idx
]->sh
.SamplerTargets
[s
];
1570 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1571 * great job of eliminating unused uniforms currently so for now
1572 * don't throw an error if two sampler types both point to 0.
1577 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1579 ralloc_asprintf(pipeline
,
1581 "Texture unit %d is accessed with 2 different types",
1582 prog
[idx
]->Id
, unit
);
1586 TexturesUsed
[unit
] |= (1 << tgt
);
1589 active_samplers
+= prog
[idx
]->info
.num_textures
;
1592 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1594 ralloc_asprintf(pipeline
,
1595 "the number of active samplers %d exceed the "
1597 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);