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 */
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
*src
;
340 if (ctx
->Const
.PackedDriverUniformStorage
&&
341 (uni
->is_bindless
|| !uni
->type
->contains_opaque())) {
342 src
= (gl_constant_value
*) uni
->driver_storage
[0].data
+
343 (offset
* elements
* dmul
);
345 src
= &uni
->storage
[offset
* elements
* dmul
];
348 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
349 returnType
== GLSL_TYPE_UINT
|| returnType
== GLSL_TYPE_DOUBLE
||
350 returnType
== GLSL_TYPE_UINT64
|| returnType
== GLSL_TYPE_INT64
);
352 /* doubles have a different size than the other 3 types */
353 unsigned bytes
= sizeof(src
[0]) * elements
* rmul
;
354 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
355 _mesa_error(ctx
, GL_INVALID_OPERATION
,
356 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
357 " but %u bytes are required)", bufSize
, bytes
);
361 /* If the return type and the uniform's native type are "compatible,"
362 * just memcpy the data. If the types are not compatible, perform a
363 * slower convert-and-copy process.
365 if (returnType
== uni
->type
->base_type
||
366 ((returnType
== GLSL_TYPE_INT
|| returnType
== GLSL_TYPE_UINT
) &&
367 (uni
->type
->is_sampler() || uni
->type
->is_image())) ||
368 (returnType
== GLSL_TYPE_UINT64
&& uni
->is_bindless
)) {
369 memcpy(paramsOut
, src
, bytes
);
371 union gl_constant_value
*const dst
=
372 (union gl_constant_value
*) paramsOut
;
373 /* This code could be optimized by putting the loop inside the switch
374 * statements. However, this is not expected to be
375 * performance-critical code.
377 for (unsigned i
= 0; i
< elements
; i
++) {
381 switch (returnType
) {
382 case GLSL_TYPE_FLOAT
:
383 switch (uni
->type
->base_type
) {
385 dst
[didx
].f
= (float) src
[sidx
].u
;
388 case GLSL_TYPE_SAMPLER
:
389 case GLSL_TYPE_IMAGE
:
390 dst
[didx
].f
= (float) src
[sidx
].i
;
393 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
395 case GLSL_TYPE_DOUBLE
: {
397 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
401 case GLSL_TYPE_UINT64
: {
403 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
407 case GLSL_TYPE_INT64
: {
409 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
414 assert(!"Should not get here.");
419 case GLSL_TYPE_DOUBLE
:
420 switch (uni
->type
->base_type
) {
421 case GLSL_TYPE_UINT
: {
422 double tmp
= src
[sidx
].u
;
423 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
427 case GLSL_TYPE_SAMPLER
:
428 case GLSL_TYPE_IMAGE
: {
429 double tmp
= src
[sidx
].i
;
430 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
433 case GLSL_TYPE_BOOL
: {
434 double tmp
= src
[sidx
].i
? 1.0 : 0.0;
435 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
438 case GLSL_TYPE_FLOAT
: {
439 double tmp
= src
[sidx
].f
;
440 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
443 case GLSL_TYPE_UINT64
: {
446 memcpy(&tmpu
, &src
[sidx
].u
, sizeof(tmpu
));
448 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
451 case GLSL_TYPE_INT64
: {
454 memcpy(&tmpi
, &src
[sidx
].i
, sizeof(tmpi
));
456 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
460 assert(!"Should not get here.");
466 switch (uni
->type
->base_type
) {
467 case GLSL_TYPE_FLOAT
:
468 /* While the GL 3.2 core spec doesn't explicitly
469 * state how conversion of float uniforms to integer
470 * values works, in section 6.2 "State Tables" on
473 * "Unless otherwise specified, when floating
474 * point state is returned as integer values or
475 * integer state is returned as floating-point
476 * values it is converted in the fashion
477 * described in section 6.1.2"
479 * That section, on page 248, says:
481 * "If GetIntegerv or GetInteger64v are called,
482 * a floating-point value is rounded to the
483 * nearest integer..."
485 dst
[didx
].i
= (int64_t) roundf(src
[sidx
].f
);
488 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
491 dst
[didx
].i
= MIN2(src
[sidx
].i
, INT_MAX
);
493 case GLSL_TYPE_DOUBLE
: {
495 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
496 dst
[didx
].i
= (int64_t) round(tmp
);
499 case GLSL_TYPE_UINT64
: {
501 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
505 case GLSL_TYPE_INT64
: {
507 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
512 assert(!"Should not get here.");
518 switch (uni
->type
->base_type
) {
519 case GLSL_TYPE_FLOAT
:
520 /* The spec isn't terribly clear how to handle negative
521 * values with an unsigned return type.
523 * GL 4.5 section 2.2.2 ("Data Conversions for State
524 * Query Commands") says:
526 * "If a value is so large in magnitude that it cannot be
527 * represented by the returned data type, then the nearest
528 * value representable using the requested type is
531 dst
[didx
].u
= src
[sidx
].f
< 0.0f
?
532 0u : (uint32_t) roundf(src
[sidx
].f
);
535 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
538 dst
[didx
].i
= MAX2(src
[sidx
].i
, 0);
540 case GLSL_TYPE_DOUBLE
: {
542 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
543 dst
[didx
].u
= tmp
< 0.0 ? 0u : (uint32_t) round(tmp
);
546 case GLSL_TYPE_UINT64
: {
548 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
549 dst
[didx
].i
= MIN2(tmp
, INT_MAX
);
552 case GLSL_TYPE_INT64
: {
554 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
555 dst
[didx
].i
= MAX2(tmp
, 0);
559 unreachable("invalid uniform type");
563 case GLSL_TYPE_INT64
:
564 switch (uni
->type
->base_type
) {
565 case GLSL_TYPE_UINT
: {
566 uint64_t tmp
= src
[sidx
].u
;
567 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
571 case GLSL_TYPE_SAMPLER
:
572 case GLSL_TYPE_IMAGE
: {
573 int64_t tmp
= src
[sidx
].i
;
574 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
577 case GLSL_TYPE_BOOL
: {
578 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
579 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
582 case GLSL_TYPE_UINT64
: {
584 memcpy(&u64
, &src
[sidx
].u
, sizeof(u64
));
585 int64_t tmp
= MIN2(u64
, INT_MAX
);
586 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
589 case GLSL_TYPE_FLOAT
: {
590 int64_t tmp
= (int64_t) roundf(src
[sidx
].f
);
591 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
594 case GLSL_TYPE_DOUBLE
: {
596 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
597 int64_t tmp
= (int64_t) round(d
);
598 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
602 assert(!"Should not get here.");
607 case GLSL_TYPE_UINT64
:
608 switch (uni
->type
->base_type
) {
609 case GLSL_TYPE_UINT
: {
610 uint64_t tmp
= src
[sidx
].u
;
611 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
615 case GLSL_TYPE_SAMPLER
:
616 case GLSL_TYPE_IMAGE
: {
617 int64_t tmp
= MAX2(src
[sidx
].i
, 0);
618 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
621 case GLSL_TYPE_BOOL
: {
622 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
623 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
626 case GLSL_TYPE_INT64
: {
628 memcpy(&i64
, &src
[sidx
].i
, sizeof(i64
));
629 uint64_t tmp
= MAX2(i64
, 0);
630 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
633 case GLSL_TYPE_FLOAT
: {
634 uint64_t tmp
= src
[sidx
].f
< 0.0f
?
635 0ull : (uint64_t) roundf(src
[sidx
].f
);
636 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
639 case GLSL_TYPE_DOUBLE
: {
641 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
642 uint64_t tmp
= (d
< 0.0) ? 0ull : (uint64_t) round(d
);
643 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
647 assert(!"Should not get here.");
653 assert(!"Should not get here.");
662 log_uniform(const void *values
, enum glsl_base_type basicType
,
663 unsigned rows
, unsigned cols
, unsigned count
,
665 const struct gl_shader_program
*shProg
,
667 const struct gl_uniform_storage
*uni
)
670 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
671 const unsigned elems
= rows
* cols
* count
;
672 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
674 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
675 "transpose = %s) to: ",
676 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
677 transpose
? "true" : "false");
678 for (unsigned i
= 0; i
< elems
; i
++) {
679 if (i
!= 0 && ((i
% rows
) == 0))
684 printf("%u ", v
[i
].u
);
687 printf("%d ", v
[i
].i
);
689 case GLSL_TYPE_UINT64
: {
691 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
692 printf("%" PRIu64
" ", tmp
);
695 case GLSL_TYPE_INT64
: {
697 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
698 printf("%" PRId64
" ", tmp
);
701 case GLSL_TYPE_FLOAT
:
702 printf("%g ", v
[i
].f
);
704 case GLSL_TYPE_DOUBLE
: {
706 memcpy(&tmp
, &v
[i
* 2].f
, sizeof(tmp
));
711 assert(!"Should not get here.");
721 log_program_parameters(const struct gl_shader_program
*shProg
)
723 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
724 if (shProg
->_LinkedShaders
[i
] == NULL
)
727 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
729 printf("Program %d %s shader parameters:\n",
730 shProg
->Name
, _mesa_shader_stage_to_string(i
));
731 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
732 unsigned pvo
= prog
->Parameters
->ParameterValueOffset
[j
];
733 printf("%s: %u %p %f %f %f %f\n",
734 prog
->Parameters
->Parameters
[j
].Name
,
736 prog
->Parameters
->ParameterValues
+ pvo
,
737 prog
->Parameters
->ParameterValues
[pvo
].f
,
738 prog
->Parameters
->ParameterValues
[pvo
+ 1].f
,
739 prog
->Parameters
->ParameterValues
[pvo
+ 2].f
,
740 prog
->Parameters
->ParameterValues
[pvo
+ 3].f
);
748 * Propagate some values from uniform backing storage to driver storage
750 * Values propagated from uniform backing storage to driver storage
751 * have all format / type conversions previously requested by the
752 * driver applied. This function is most often called by the
753 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
756 * \param uni Uniform whose data is to be propagated to driver storage
757 * \param array_index If \c uni is an array, this is the element of
758 * the array to be propagated.
759 * \param count Number of array elements to propagate.
762 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
763 unsigned array_index
,
768 const unsigned components
= uni
->type
->vector_elements
;
769 const unsigned vectors
= uni
->type
->matrix_columns
;
770 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
772 /* Store the data in the driver's requested type in the driver's storage
775 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
777 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
778 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
779 uint8_t *dst
= (uint8_t *) store
->data
;
780 const unsigned extra_stride
=
781 store
->element_stride
- (vectors
* store
->vector_stride
);
783 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
786 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
788 __func__
, dst
, array_index
, components
,
789 vectors
, count
, store
->vector_stride
, extra_stride
);
792 dst
+= array_index
* store
->element_stride
;
794 switch (store
->format
) {
795 case uniform_native
: {
799 if (src_vector_byte_stride
== store
->vector_stride
) {
801 for (j
= 0; j
< count
; j
++) {
802 memcpy(dst
, src
, src_vector_byte_stride
* vectors
);
803 src
+= src_vector_byte_stride
* vectors
;
804 dst
+= store
->vector_stride
* vectors
;
809 /* Unigine Heaven benchmark gets here */
810 memcpy(dst
, src
, src_vector_byte_stride
* vectors
* count
);
811 src
+= src_vector_byte_stride
* vectors
* count
;
812 dst
+= store
->vector_stride
* vectors
* count
;
815 for (j
= 0; j
< count
; j
++) {
816 for (v
= 0; v
< vectors
; v
++) {
817 memcpy(dst
, src
, src_vector_byte_stride
);
818 src
+= src_vector_byte_stride
;
819 dst
+= store
->vector_stride
;
828 case uniform_int_float
: {
829 const int *isrc
= (const int *) src
;
834 for (j
= 0; j
< count
; j
++) {
835 for (v
= 0; v
< vectors
; v
++) {
836 for (c
= 0; c
< components
; c
++) {
837 ((float *) dst
)[c
] = (float) *isrc
;
841 dst
+= store
->vector_stride
;
850 assert(!"Should not get here.");
858 * Return printable string for a given GLSL_TYPE_x
861 glsl_type_name(enum glsl_base_type type
)
868 case GLSL_TYPE_FLOAT
:
870 case GLSL_TYPE_DOUBLE
:
872 case GLSL_TYPE_UINT64
:
874 case GLSL_TYPE_INT64
:
878 case GLSL_TYPE_SAMPLER
:
880 case GLSL_TYPE_IMAGE
:
882 case GLSL_TYPE_ATOMIC_UINT
:
883 return "atomic_uint";
884 case GLSL_TYPE_STRUCT
:
886 case GLSL_TYPE_INTERFACE
:
888 case GLSL_TYPE_ARRAY
:
892 case GLSL_TYPE_ERROR
:
900 static struct gl_uniform_storage
*
901 validate_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
902 unsigned *offset
, struct gl_context
*ctx
,
903 struct gl_shader_program
*shProg
,
904 enum glsl_base_type basicType
, unsigned src_components
)
906 struct gl_uniform_storage
*const uni
=
907 validate_uniform_parameters(location
, count
, offset
,
908 ctx
, shProg
, "glUniform");
912 if (uni
->type
->is_matrix()) {
913 /* Can't set matrix uniforms (like mat4) with glUniform */
914 _mesa_error(ctx
, GL_INVALID_OPERATION
,
915 "glUniform%u(uniform \"%s\"@%d is matrix)",
916 src_components
, uni
->name
, location
);
920 /* Verify that the types are compatible. */
921 const unsigned components
= uni
->type
->vector_elements
;
923 if (components
!= src_components
) {
924 /* glUniformN() must match float/vecN type */
925 _mesa_error(ctx
, GL_INVALID_OPERATION
,
926 "glUniform%u(\"%s\"@%u has %u components, not %u)",
927 src_components
, uni
->name
, location
,
928 components
, src_components
);
933 switch (uni
->type
->base_type
) {
935 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
937 case GLSL_TYPE_SAMPLER
:
938 match
= (basicType
== GLSL_TYPE_INT
);
940 case GLSL_TYPE_IMAGE
:
941 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
944 match
= (basicType
== uni
->type
->base_type
);
949 _mesa_error(ctx
, GL_INVALID_OPERATION
,
950 "glUniform%u(\"%s\"@%d is %s, not %s)",
951 src_components
, uni
->name
, location
,
952 glsl_type_name(uni
->type
->base_type
),
953 glsl_type_name(basicType
));
957 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
958 log_uniform(values
, basicType
, components
, 1, count
,
959 false, shProg
, location
, uni
);
962 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
964 * "Setting a sampler's value to i selects texture image unit number
965 * i. The values of i range from zero to the implementation- dependent
966 * maximum supported number of texture image units."
968 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
971 * "Error Description Offending command
974 * INVALID_VALUE Numeric argument out of range Yes"
976 * Based on that, when an invalid sampler is specified, we generate a
977 * GL_INVALID_VALUE error and ignore the command.
979 if (uni
->type
->is_sampler()) {
980 for (int i
= 0; i
< count
; i
++) {
981 const unsigned texUnit
= ((unsigned *) values
)[i
];
983 /* check that the sampler (tex unit index) is legal */
984 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
985 _mesa_error(ctx
, GL_INVALID_VALUE
,
986 "glUniform1i(invalid sampler/tex unit index for "
987 "uniform %d)", location
);
991 /* We need to reset the validate flag on changes to samplers in case
992 * two different sampler types are set to the same texture unit.
994 ctx
->_Shader
->Validated
= GL_FALSE
;
997 if (uni
->type
->is_image()) {
998 for (int i
= 0; i
< count
; i
++) {
999 const int unit
= ((GLint
*) values
)[i
];
1001 /* check that the image unit is legal */
1002 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
1003 _mesa_error(ctx
, GL_INVALID_VALUE
,
1004 "glUniform1i(invalid image unit index for uniform %d)",
1015 _mesa_flush_vertices_for_uniforms(struct gl_context
*ctx
,
1016 const struct gl_uniform_storage
*uni
)
1018 /* Opaque uniforms have no storage unless they are bindless */
1019 if (!uni
->is_bindless
&& uni
->type
->contains_opaque()) {
1020 FLUSH_VERTICES(ctx
, 0);
1024 uint64_t new_driver_state
= 0;
1025 unsigned mask
= uni
->active_shader_mask
;
1028 unsigned index
= u_bit_scan(&mask
);
1030 assert(index
< MESA_SHADER_STAGES
);
1031 new_driver_state
|= ctx
->DriverFlags
.NewShaderConstants
[index
];
1034 FLUSH_VERTICES(ctx
, new_driver_state
? 0 : _NEW_PROGRAM_CONSTANTS
);
1035 ctx
->NewDriverState
|= new_driver_state
;
1039 copy_uniforms_to_storage(gl_constant_value
*storage
,
1040 struct gl_uniform_storage
*uni
,
1041 struct gl_context
*ctx
, GLsizei count
,
1042 const GLvoid
*values
, const int size_mul
,
1043 const unsigned offset
, const unsigned components
,
1044 enum glsl_base_type basicType
)
1046 if (!uni
->type
->is_boolean() && !uni
->is_bindless
) {
1047 memcpy(storage
, values
,
1048 sizeof(storage
[0]) * components
* count
* size_mul
);
1049 } else if (uni
->is_bindless
) {
1050 const union gl_constant_value
*src
=
1051 (const union gl_constant_value
*) values
;
1052 GLuint64
*dst
= (GLuint64
*)&storage
->i
;
1053 const unsigned elems
= components
* count
;
1055 for (unsigned i
= 0; i
< elems
; i
++) {
1059 const union gl_constant_value
*src
=
1060 (const union gl_constant_value
*) values
;
1061 union gl_constant_value
*dst
= storage
;
1062 const unsigned elems
= components
* count
;
1064 for (unsigned i
= 0; i
< elems
; i
++) {
1065 if (basicType
== GLSL_TYPE_FLOAT
) {
1066 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
1068 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
1076 * Called via glUniform*() functions.
1079 _mesa_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
1080 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1081 enum glsl_base_type basicType
, unsigned src_components
)
1084 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
1086 struct gl_uniform_storage
*uni
;
1087 if (_mesa_is_no_error_enabled(ctx
)) {
1088 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1090 * "If the value of location is -1, the Uniform* commands will
1091 * silently ignore the data passed in, and the current uniform values
1092 * will not be changed.
1097 uni
= shProg
->UniformRemapTable
[location
];
1099 /* The array index specified by the uniform location is just the
1100 * uniform location minus the base location of of the uniform.
1102 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1103 offset
= location
- uni
->remap_location
;
1105 uni
= validate_uniform(location
, count
, values
, &offset
, ctx
, shProg
,
1106 basicType
, src_components
);
1111 const unsigned components
= uni
->type
->vector_elements
;
1113 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1115 * "When loading N elements starting at an arbitrary position k in a
1116 * uniform declared as an array, elements k through k + N - 1 in the
1117 * array will be replaced with the new values. Values for any array
1118 * element that exceeds the highest array element index used, as
1119 * reported by GetActiveUniform, will be ignored by the GL."
1121 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1122 * will have already generated an error.
1124 if (uni
->array_elements
!= 0) {
1125 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1128 /* We check samplers for changes and flush if needed in the sampler
1129 * handling code further down, so just skip them here.
1131 if (!uni
->type
->is_sampler()) {
1132 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1135 /* Store the data in the "actual type" backing storage for the uniform.
1137 gl_constant_value
*storage
;
1138 if (ctx
->Const
.PackedDriverUniformStorage
&&
1139 (uni
->is_bindless
|| !uni
->type
->contains_opaque())) {
1140 for (unsigned s
= 0; s
< uni
->num_driver_storage
; s
++) {
1141 storage
= (gl_constant_value
*)
1142 uni
->driver_storage
[s
].data
+ (size_mul
* offset
* components
);
1144 copy_uniforms_to_storage(storage
, uni
, ctx
, count
, values
, size_mul
,
1145 offset
, components
, basicType
);
1148 storage
= &uni
->storage
[size_mul
* components
* offset
];
1149 copy_uniforms_to_storage(storage
, uni
, ctx
, count
, values
, size_mul
,
1150 offset
, components
, basicType
);
1152 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1155 /* If the uniform is a sampler, do the extra magic necessary to propagate
1156 * the changes through.
1158 if (uni
->type
->is_sampler()) {
1159 bool flushed
= false;
1161 shProg
->SamplersValidated
= GL_TRUE
;
1163 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1164 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1166 /* If the shader stage doesn't use the sampler uniform, skip this. */
1167 if (!uni
->opaque
[i
].active
)
1170 bool changed
= false;
1171 for (int j
= 0; j
< count
; j
++) {
1172 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1173 unsigned value
= ((unsigned *)values
)[j
];
1175 if (uni
->is_bindless
) {
1176 struct gl_bindless_sampler
*sampler
=
1177 &sh
->Program
->sh
.BindlessSamplers
[unit
];
1179 /* Mark this bindless sampler as bound to a texture unit.
1181 if (sampler
->unit
!= value
|| !sampler
->bound
) {
1182 sampler
->unit
= value
;
1185 sampler
->bound
= true;
1186 sh
->Program
->sh
.HasBoundBindlessSampler
= true;
1188 if (sh
->Program
->SamplerUnits
[unit
] != value
) {
1189 sh
->Program
->SamplerUnits
[unit
] = value
;
1197 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
);
1201 struct gl_program
*const prog
= sh
->Program
;
1202 _mesa_update_shader_textures_used(shProg
, prog
);
1203 if (ctx
->Driver
.SamplerUniformChange
)
1204 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
1209 /* If the uniform is an image, update the mapping from image
1210 * uniforms to image units present in the shader data structure.
1212 if (uni
->type
->is_image()) {
1213 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1214 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1216 /* If the shader stage doesn't use the image uniform, skip this. */
1217 if (!uni
->opaque
[i
].active
)
1220 for (int j
= 0; j
< count
; j
++) {
1221 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1222 unsigned value
= ((unsigned *)values
)[j
];
1224 if (uni
->is_bindless
) {
1225 struct gl_bindless_image
*image
=
1226 &sh
->Program
->sh
.BindlessImages
[unit
];
1228 /* Mark this bindless image as bound to an image unit.
1230 image
->unit
= value
;
1231 image
->bound
= true;
1232 sh
->Program
->sh
.HasBoundBindlessImage
= true;
1234 sh
->Program
->sh
.ImageUnits
[unit
] = value
;
1239 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
1245 copy_uniform_matrix_to_storage(gl_constant_value
*storage
,
1246 GLsizei count
, const void *values
,
1247 const unsigned size_mul
, const unsigned offset
,
1248 const unsigned components
,
1249 const unsigned vectors
, bool transpose
,
1250 unsigned cols
, unsigned rows
,
1251 enum glsl_base_type basicType
)
1253 const unsigned elements
= components
* vectors
;
1256 memcpy(storage
, values
,
1257 sizeof(storage
[0]) * elements
* count
* size_mul
);
1258 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1259 /* Copy and transpose the matrix.
1261 const float *src
= (const float *)values
;
1262 float *dst
= &storage
->f
;
1264 for (int i
= 0; i
< count
; i
++) {
1265 for (unsigned r
= 0; r
< rows
; r
++) {
1266 for (unsigned c
= 0; c
< cols
; c
++) {
1267 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1275 assert(basicType
== GLSL_TYPE_DOUBLE
);
1276 const double *src
= (const double *)values
;
1277 double *dst
= (double *)&storage
->f
;
1279 for (int i
= 0; i
< count
; i
++) {
1280 for (unsigned r
= 0; r
< rows
; r
++) {
1281 for (unsigned c
= 0; c
< cols
; c
++) {
1282 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1294 * Called by glUniformMatrix*() functions.
1295 * Note: cols=2, rows=4 ==> array[2] of vec4
1298 _mesa_uniform_matrix(GLint location
, GLsizei count
,
1299 GLboolean transpose
, const void *values
,
1300 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1301 GLuint cols
, GLuint rows
, enum glsl_base_type basicType
)
1304 struct gl_uniform_storage
*const uni
=
1305 validate_uniform_parameters(location
, count
, &offset
,
1306 ctx
, shProg
, "glUniformMatrix");
1310 if (!uni
->type
->is_matrix()) {
1311 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1312 "glUniformMatrix(non-matrix uniform)");
1316 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
1317 const unsigned size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
1319 assert(!uni
->type
->is_sampler());
1320 const unsigned vectors
= uni
->type
->matrix_columns
;
1321 const unsigned components
= uni
->type
->vector_elements
;
1323 /* Verify that the types are compatible. This is greatly simplified for
1324 * matrices because they can only have a float base type.
1326 if (vectors
!= cols
|| components
!= rows
) {
1327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1328 "glUniformMatrix(matrix size mismatch)");
1332 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1333 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1336 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
1337 _mesa_error(ctx
, GL_INVALID_VALUE
,
1338 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1343 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1346 * "If any of the following conditions occur, an INVALID_OPERATION
1347 * error is generated by the Uniform* commands, and no uniform values
1352 * - if the uniform declared in the shader is not of type boolean and
1353 * the type indicated in the name of the Uniform* command used does
1354 * not match the type of the uniform"
1356 * There are no Boolean matrix types, so we do not need to allow
1357 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1359 if (uni
->type
->base_type
!= basicType
) {
1360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1361 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1362 cols
, rows
, uni
->name
, location
,
1363 glsl_type_name(uni
->type
->base_type
),
1364 glsl_type_name(basicType
));
1368 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1369 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
1370 bool(transpose
), shProg
, location
, uni
);
1373 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1375 * "When loading N elements starting at an arbitrary position k in a
1376 * uniform declared as an array, elements k through k + N - 1 in the
1377 * array will be replaced with the new values. Values for any array
1378 * element that exceeds the highest array element index used, as
1379 * reported by GetActiveUniform, will be ignored by the GL."
1381 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1382 * will have already generated an error.
1384 if (uni
->array_elements
!= 0) {
1385 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1388 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1390 /* Store the data in the "actual type" backing storage for the uniform.
1392 gl_constant_value
*storage
;
1393 const unsigned elements
= components
* vectors
;
1394 if (ctx
->Const
.PackedDriverUniformStorage
) {
1395 for (unsigned s
= 0; s
< uni
->num_driver_storage
; s
++) {
1396 storage
= (gl_constant_value
*)
1397 uni
->driver_storage
[s
].data
+ (size_mul
* offset
* elements
);
1399 copy_uniform_matrix_to_storage(storage
, count
, values
, size_mul
,
1400 offset
, components
, vectors
,
1401 transpose
, cols
, rows
, basicType
);
1404 storage
= &uni
->storage
[size_mul
* elements
* offset
];
1405 copy_uniform_matrix_to_storage(storage
, count
, values
, size_mul
, offset
,
1406 components
, vectors
, transpose
, cols
,
1409 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1414 update_bound_bindless_sampler_flag(struct gl_program
*prog
)
1418 if (likely(!prog
->sh
.HasBoundBindlessSampler
))
1421 for (i
= 0; i
< prog
->sh
.NumBindlessSamplers
; i
++) {
1422 struct gl_bindless_sampler
*sampler
= &prog
->sh
.BindlessSamplers
[i
];
1427 prog
->sh
.HasBoundBindlessSampler
= false;
1431 update_bound_bindless_image_flag(struct gl_program
*prog
)
1435 if (likely(!prog
->sh
.HasBoundBindlessImage
))
1438 for (i
= 0; i
< prog
->sh
.NumBindlessImages
; i
++) {
1439 struct gl_bindless_image
*image
= &prog
->sh
.BindlessImages
[i
];
1444 prog
->sh
.HasBoundBindlessImage
= false;
1448 * Called via glUniformHandleui64*ARB() functions.
1451 _mesa_uniform_handle(GLint location
, GLsizei count
, const GLvoid
*values
,
1452 struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1455 struct gl_uniform_storage
*uni
;
1457 if (_mesa_is_no_error_enabled(ctx
)) {
1458 /* From Section 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1460 * "If the value of location is -1, the Uniform* commands will
1461 * silently ignore the data passed in, and the current uniform values
1462 * will not be changed.
1467 uni
= shProg
->UniformRemapTable
[location
];
1469 /* The array index specified by the uniform location is just the
1470 * uniform location minus the base location of of the uniform.
1472 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1473 offset
= location
- uni
->remap_location
;
1475 uni
= validate_uniform_parameters(location
, count
, &offset
,
1476 ctx
, shProg
, "glUniformHandleui64*ARB");
1480 if (!uni
->is_bindless
) {
1481 /* From section "Errors" of the ARB_bindless_texture spec:
1483 * "The error INVALID_OPERATION is generated by
1484 * UniformHandleui64{v}ARB if the sampler or image uniform being
1485 * updated has the "bound_sampler" or "bound_image" layout qualifier."
1487 * From section 4.4.6 of the ARB_bindless_texture spec:
1489 * "In the absence of these qualifiers, sampler and image uniforms are
1490 * considered "bound". Additionally, if GL_ARB_bindless_texture is
1491 * not enabled, these uniforms are considered "bound"."
1493 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1494 "glUniformHandleui64*ARB(non-bindless sampler/image uniform)");
1499 const unsigned components
= uni
->type
->vector_elements
;
1500 const int size_mul
= 2;
1502 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1503 log_uniform(values
, GLSL_TYPE_UINT64
, components
, 1, count
,
1504 false, shProg
, location
, uni
);
1507 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1509 * "When loading N elements starting at an arbitrary position k in a
1510 * uniform declared as an array, elements k through k + N - 1 in the
1511 * array will be replaced with the new values. Values for any array
1512 * element that exceeds the highest array element index used, as
1513 * reported by GetActiveUniform, will be ignored by the GL."
1515 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1516 * will have already generated an error.
1518 if (uni
->array_elements
!= 0) {
1519 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1522 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
1524 /* Store the data in the "actual type" backing storage for the uniform.
1526 gl_constant_value
*storage
;
1527 if (ctx
->Const
.PackedDriverUniformStorage
) {
1528 for (unsigned s
= 0; s
< uni
->num_driver_storage
; s
++) {
1529 storage
= (gl_constant_value
*)
1530 uni
->driver_storage
[s
].data
+ (size_mul
* offset
* components
);
1531 memcpy(storage
, values
,
1532 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1535 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
1536 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1538 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1541 if (uni
->type
->is_sampler()) {
1542 /* Mark this bindless sampler as not bound to a texture unit because
1543 * it refers to a texture handle.
1545 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1546 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1548 /* If the shader stage doesn't use the sampler uniform, skip this. */
1549 if (!uni
->opaque
[i
].active
)
1552 for (int j
= 0; j
< count
; j
++) {
1553 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1554 struct gl_bindless_sampler
*sampler
=
1555 &sh
->Program
->sh
.BindlessSamplers
[unit
];
1557 sampler
->bound
= false;
1560 update_bound_bindless_sampler_flag(sh
->Program
);
1564 if (uni
->type
->is_image()) {
1565 /* Mark this bindless image as not bound to an image unit because it
1566 * refers to a texture handle.
1568 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1569 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1571 /* If the shader stage doesn't use the sampler uniform, skip this. */
1572 if (!uni
->opaque
[i
].active
)
1575 for (int j
= 0; j
< count
; j
++) {
1576 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1577 struct gl_bindless_image
*image
=
1578 &sh
->Program
->sh
.BindlessImages
[unit
];
1580 image
->bound
= false;
1583 update_bound_bindless_image_flag(sh
->Program
);
1589 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1590 char *errMsg
, size_t errMsgLength
)
1592 /* Shader does not have samplers. */
1593 if (shProg
->data
->NumUniformStorage
== 0)
1596 if (!shProg
->SamplersValidated
) {
1597 _mesa_snprintf(errMsg
, errMsgLength
,
1598 "active samplers with a different type "
1599 "refer to the same texture image unit");
1606 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1608 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1609 * OpenGL 4.1 spec says:
1611 * "[INVALID_OPERATION] is generated by any command that transfers
1612 * vertices to the GL if:
1616 * - Any two active samplers in the current program object are of
1617 * different types, but refer to the same texture image unit.
1619 * - The number of active samplers in the program exceeds the
1620 * maximum number of texture image units allowed."
1624 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1625 unsigned active_samplers
= 0;
1626 const struct gl_program
**prog
=
1627 (const struct gl_program
**) pipeline
->CurrentProgram
;
1630 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1632 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1636 mask
= prog
[idx
]->SamplersUsed
;
1638 const int s
= u_bit_scan(&mask
);
1639 GLuint unit
= prog
[idx
]->SamplerUnits
[s
];
1640 GLuint tgt
= prog
[idx
]->sh
.SamplerTargets
[s
];
1642 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1643 * great job of eliminating unused uniforms currently so for now
1644 * don't throw an error if two sampler types both point to 0.
1649 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1651 ralloc_asprintf(pipeline
,
1653 "Texture unit %d is accessed with 2 different types",
1654 prog
[idx
]->Id
, unit
);
1658 TexturesUsed
[unit
] |= (1 << tgt
);
1661 active_samplers
+= prog
[idx
]->info
.num_textures
;
1664 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1666 ralloc_asprintf(pipeline
,
1667 "the number of active samplers %d exceed the "
1669 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);