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 /* XXX: Remove the sampler/image check workarounds when bindless is fully
329 (uni
->type
->is_64bit() && !uni
->type
->is_sampler() && !uni
->type
->is_image()) ? 2 : 1;
330 const int rmul
= glsl_base_type_is_64bit(returnType
) ? 2 : 1;
332 /* Calculate the source base address *BEFORE* modifying elements to
333 * account for the size of the user's buffer.
335 const union gl_constant_value
*const src
=
336 &uni
->storage
[offset
* elements
* dmul
];
338 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
339 returnType
== GLSL_TYPE_UINT
|| returnType
== GLSL_TYPE_DOUBLE
||
340 returnType
== GLSL_TYPE_UINT64
|| returnType
== GLSL_TYPE_INT64
);
342 /* doubles have a different size than the other 3 types */
343 unsigned bytes
= sizeof(src
[0]) * elements
* rmul
;
344 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
345 _mesa_error(ctx
, GL_INVALID_OPERATION
,
346 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
347 " but %u bytes are required)", bufSize
, bytes
);
351 /* If the return type and the uniform's native type are "compatible,"
352 * just memcpy the data. If the types are not compatible, perform a
353 * slower convert-and-copy process.
355 if (returnType
== uni
->type
->base_type
||
356 ((returnType
== GLSL_TYPE_INT
|| returnType
== GLSL_TYPE_UINT
) &&
357 (uni
->type
->is_sampler() || uni
->type
->is_image()))) {
358 memcpy(paramsOut
, src
, bytes
);
360 union gl_constant_value
*const dst
=
361 (union gl_constant_value
*) paramsOut
;
362 /* This code could be optimized by putting the loop inside the switch
363 * statements. However, this is not expected to be
364 * performance-critical code.
366 for (unsigned i
= 0; i
< elements
; i
++) {
370 switch (returnType
) {
371 case GLSL_TYPE_FLOAT
:
372 switch (uni
->type
->base_type
) {
374 dst
[didx
].f
= (float) src
[sidx
].u
;
377 case GLSL_TYPE_SAMPLER
:
378 case GLSL_TYPE_IMAGE
:
379 dst
[didx
].f
= (float) src
[sidx
].i
;
382 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
384 case GLSL_TYPE_DOUBLE
: {
386 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
390 case GLSL_TYPE_UINT64
: {
392 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
396 case GLSL_TYPE_INT64
: {
398 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
403 assert(!"Should not get here.");
408 case GLSL_TYPE_DOUBLE
:
409 switch (uni
->type
->base_type
) {
410 case GLSL_TYPE_UINT
: {
411 double tmp
= src
[sidx
].u
;
412 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
416 case GLSL_TYPE_SAMPLER
:
417 case GLSL_TYPE_IMAGE
: {
418 double tmp
= src
[sidx
].i
;
419 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
422 case GLSL_TYPE_BOOL
: {
423 double tmp
= src
[sidx
].i
? 1.0 : 0.0;
424 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
427 case GLSL_TYPE_FLOAT
: {
428 double tmp
= src
[sidx
].f
;
429 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
432 case GLSL_TYPE_UINT64
: {
435 memcpy(&tmpu
, &src
[sidx
].u
, sizeof(tmpu
));
437 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
440 case GLSL_TYPE_INT64
: {
443 memcpy(&tmpi
, &src
[sidx
].i
, sizeof(tmpi
));
445 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
449 assert(!"Should not get here.");
455 switch (uni
->type
->base_type
) {
456 case GLSL_TYPE_FLOAT
:
457 /* While the GL 3.2 core spec doesn't explicitly
458 * state how conversion of float uniforms to integer
459 * values works, in section 6.2 "State Tables" on
462 * "Unless otherwise specified, when floating
463 * point state is returned as integer values or
464 * integer state is returned as floating-point
465 * values it is converted in the fashion
466 * described in section 6.1.2"
468 * That section, on page 248, says:
470 * "If GetIntegerv or GetInteger64v are called,
471 * a floating-point value is rounded to the
472 * nearest integer..."
474 dst
[didx
].i
= IROUND(src
[sidx
].f
);
477 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
480 dst
[didx
].i
= MIN2(src
[sidx
].i
, INT_MAX
);
482 case GLSL_TYPE_DOUBLE
: {
484 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
485 dst
[didx
].i
= IROUNDD(tmp
);
488 case GLSL_TYPE_UINT64
: {
490 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
494 case GLSL_TYPE_INT64
: {
496 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
501 assert(!"Should not get here.");
507 switch (uni
->type
->base_type
) {
508 case GLSL_TYPE_FLOAT
:
509 /* The spec isn't terribly clear how to handle negative
510 * values with an unsigned return type.
512 * GL 4.5 section 2.2.2 ("Data Conversions for State
513 * Query Commands") says:
515 * "If a value is so large in magnitude that it cannot be
516 * represented by the returned data type, then the nearest
517 * value representable using the requested type is
520 dst
[didx
].u
= src
[sidx
].f
< 0.0f
?
521 0u : (uint32_t) roundf(src
[sidx
].f
);
524 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
527 dst
[didx
].i
= MAX2(src
[sidx
].i
, 0);
529 case GLSL_TYPE_DOUBLE
: {
531 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
532 dst
[didx
].u
= tmp
< 0.0 ? 0u : (uint32_t) round(tmp
);
535 case GLSL_TYPE_UINT64
: {
537 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
538 dst
[didx
].i
= MIN2(tmp
, INT_MAX
);
541 case GLSL_TYPE_INT64
: {
543 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
544 dst
[didx
].i
= MAX2(tmp
, 0);
548 unreachable("invalid uniform type");
552 case GLSL_TYPE_INT64
:
553 switch (uni
->type
->base_type
) {
554 case GLSL_TYPE_UINT
: {
555 uint64_t tmp
= src
[sidx
].u
;
556 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
560 case GLSL_TYPE_SAMPLER
:
561 case GLSL_TYPE_IMAGE
: {
562 int64_t tmp
= src
[sidx
].i
;
563 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
566 case GLSL_TYPE_BOOL
: {
567 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
568 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
571 case GLSL_TYPE_UINT64
: {
573 memcpy(&u64
, &src
[sidx
].u
, sizeof(u64
));
574 int64_t tmp
= MIN2(u64
, INT_MAX
);
575 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
578 case GLSL_TYPE_FLOAT
: {
579 int64_t tmp
= (int64_t) roundf(src
[sidx
].f
);
580 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
583 case GLSL_TYPE_DOUBLE
: {
585 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
586 int64_t tmp
= (int64_t) round(d
);
587 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
591 assert(!"Should not get here.");
596 case GLSL_TYPE_UINT64
:
597 switch (uni
->type
->base_type
) {
598 case GLSL_TYPE_UINT
: {
599 uint64_t tmp
= src
[sidx
].u
;
600 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
604 case GLSL_TYPE_SAMPLER
:
605 case GLSL_TYPE_IMAGE
: {
606 int64_t tmp
= MAX2(src
[sidx
].i
, 0);
607 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
610 case GLSL_TYPE_BOOL
: {
611 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
612 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
615 case GLSL_TYPE_INT64
: {
617 memcpy(&i64
, &src
[sidx
].i
, sizeof(i64
));
618 uint64_t tmp
= MAX2(i64
, 0);
619 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
622 case GLSL_TYPE_FLOAT
: {
623 uint64_t tmp
= src
[sidx
].f
< 0.0f
?
624 0ull : (uint64_t) roundf(src
[sidx
].f
);
625 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
628 case GLSL_TYPE_DOUBLE
: {
630 memcpy(&d
, &src
[sidx
].f
, sizeof(d
));
631 uint64_t tmp
= (d
< 0.0) ? 0ull : (uint64_t) round(d
);
632 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
636 assert(!"Should not get here.");
642 assert(!"Should not get here.");
651 log_uniform(const void *values
, enum glsl_base_type basicType
,
652 unsigned rows
, unsigned cols
, unsigned count
,
654 const struct gl_shader_program
*shProg
,
656 const struct gl_uniform_storage
*uni
)
659 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
660 const unsigned elems
= rows
* cols
* count
;
661 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
663 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
664 "transpose = %s) to: ",
665 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
666 transpose
? "true" : "false");
667 for (unsigned i
= 0; i
< elems
; i
++) {
668 if (i
!= 0 && ((i
% rows
) == 0))
673 printf("%u ", v
[i
].u
);
676 printf("%d ", v
[i
].i
);
678 case GLSL_TYPE_UINT64
: {
680 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
681 printf("%" PRIu64
" ", tmp
);
684 case GLSL_TYPE_INT64
: {
686 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
687 printf("%" PRId64
" ", tmp
);
690 case GLSL_TYPE_FLOAT
:
691 printf("%g ", v
[i
].f
);
693 case GLSL_TYPE_DOUBLE
: {
695 memcpy(&tmp
, &v
[i
* 2].f
, sizeof(tmp
));
700 assert(!"Should not get here.");
710 log_program_parameters(const struct gl_shader_program
*shProg
)
712 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
713 if (shProg
->_LinkedShaders
[i
] == NULL
)
716 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
718 printf("Program %d %s shader parameters:\n",
719 shProg
->Name
, _mesa_shader_stage_to_string(i
));
720 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
721 printf("%s: %p %f %f %f %f\n",
722 prog
->Parameters
->Parameters
[j
].Name
,
723 prog
->Parameters
->ParameterValues
[j
],
724 prog
->Parameters
->ParameterValues
[j
][0].f
,
725 prog
->Parameters
->ParameterValues
[j
][1].f
,
726 prog
->Parameters
->ParameterValues
[j
][2].f
,
727 prog
->Parameters
->ParameterValues
[j
][3].f
);
735 * Propagate some values from uniform backing storage to driver storage
737 * Values propagated from uniform backing storage to driver storage
738 * have all format / type conversions previously requested by the
739 * driver applied. This function is most often called by the
740 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
743 * \param uni Uniform whose data is to be propagated to driver storage
744 * \param array_index If \c uni is an array, this is the element of
745 * the array to be propagated.
746 * \param count Number of array elements to propagate.
749 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
750 unsigned array_index
,
755 const unsigned components
= uni
->type
->vector_elements
;
756 const unsigned vectors
= uni
->type
->matrix_columns
;
757 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
759 /* Store the data in the driver's requested type in the driver's storage
762 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
764 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
765 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
766 uint8_t *dst
= (uint8_t *) store
->data
;
767 const unsigned extra_stride
=
768 store
->element_stride
- (vectors
* store
->vector_stride
);
770 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
773 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
775 __func__
, dst
, array_index
, components
,
776 vectors
, count
, store
->vector_stride
, extra_stride
);
779 dst
+= array_index
* store
->element_stride
;
781 switch (store
->format
) {
782 case uniform_native
: {
786 if (src_vector_byte_stride
== store
->vector_stride
) {
788 for (j
= 0; j
< count
; j
++) {
789 memcpy(dst
, src
, src_vector_byte_stride
* vectors
);
790 src
+= src_vector_byte_stride
* vectors
;
791 dst
+= store
->vector_stride
* vectors
;
796 /* Unigine Heaven benchmark gets here */
797 memcpy(dst
, src
, src_vector_byte_stride
* vectors
* count
);
798 src
+= src_vector_byte_stride
* vectors
* count
;
799 dst
+= store
->vector_stride
* vectors
* count
;
802 for (j
= 0; j
< count
; j
++) {
803 for (v
= 0; v
< vectors
; v
++) {
804 memcpy(dst
, src
, src_vector_byte_stride
);
805 src
+= src_vector_byte_stride
;
806 dst
+= store
->vector_stride
;
815 case uniform_int_float
: {
816 const int *isrc
= (const int *) src
;
821 for (j
= 0; j
< count
; j
++) {
822 for (v
= 0; v
< vectors
; v
++) {
823 for (c
= 0; c
< components
; c
++) {
824 ((float *) dst
)[c
] = (float) *isrc
;
828 dst
+= store
->vector_stride
;
837 assert(!"Should not get here.");
845 * Return printable string for a given GLSL_TYPE_x
848 glsl_type_name(enum glsl_base_type type
)
855 case GLSL_TYPE_FLOAT
:
857 case GLSL_TYPE_DOUBLE
:
859 case GLSL_TYPE_UINT64
:
861 case GLSL_TYPE_INT64
:
865 case GLSL_TYPE_SAMPLER
:
867 case GLSL_TYPE_IMAGE
:
869 case GLSL_TYPE_ATOMIC_UINT
:
870 return "atomic_uint";
871 case GLSL_TYPE_STRUCT
:
873 case GLSL_TYPE_INTERFACE
:
875 case GLSL_TYPE_ARRAY
:
879 case GLSL_TYPE_ERROR
:
887 static struct gl_uniform_storage
*
888 validate_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
889 unsigned *offset
, struct gl_context
*ctx
,
890 struct gl_shader_program
*shProg
,
891 enum glsl_base_type basicType
, unsigned src_components
)
893 struct gl_uniform_storage
*const uni
=
894 validate_uniform_parameters(location
, count
, offset
,
895 ctx
, shProg
, "glUniform");
899 if (uni
->type
->is_matrix()) {
900 /* Can't set matrix uniforms (like mat4) with glUniform */
901 _mesa_error(ctx
, GL_INVALID_OPERATION
,
902 "glUniform%u(uniform \"%s\"@%d is matrix)",
903 src_components
, uni
->name
, location
);
907 /* Verify that the types are compatible. */
908 const unsigned components
= uni
->type
->vector_elements
;
910 if (components
!= src_components
) {
911 /* glUniformN() must match float/vecN type */
912 _mesa_error(ctx
, GL_INVALID_OPERATION
,
913 "glUniform%u(\"%s\"@%u has %u components, not %u)",
914 src_components
, uni
->name
, location
,
915 components
, src_components
);
920 switch (uni
->type
->base_type
) {
922 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
924 case GLSL_TYPE_SAMPLER
:
925 match
= (basicType
== GLSL_TYPE_INT
);
927 case GLSL_TYPE_IMAGE
:
928 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
931 match
= (basicType
== uni
->type
->base_type
);
936 _mesa_error(ctx
, GL_INVALID_OPERATION
,
937 "glUniform%u(\"%s\"@%d is %s, not %s)",
938 src_components
, uni
->name
, location
,
939 glsl_type_name(uni
->type
->base_type
),
940 glsl_type_name(basicType
));
944 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
945 log_uniform(values
, basicType
, components
, 1, count
,
946 false, shProg
, location
, uni
);
949 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
951 * "Setting a sampler's value to i selects texture image unit number
952 * i. The values of i range from zero to the implementation- dependent
953 * maximum supported number of texture image units."
955 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
958 * "Error Description Offending command
961 * INVALID_VALUE Numeric argument out of range Yes"
963 * Based on that, when an invalid sampler is specified, we generate a
964 * GL_INVALID_VALUE error and ignore the command.
966 if (uni
->type
->is_sampler()) {
967 for (int i
= 0; i
< count
; i
++) {
968 const unsigned texUnit
= ((unsigned *) values
)[i
];
970 /* check that the sampler (tex unit index) is legal */
971 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
972 _mesa_error(ctx
, GL_INVALID_VALUE
,
973 "glUniform1i(invalid sampler/tex unit index for "
974 "uniform %d)", location
);
978 /* We need to reset the validate flag on changes to samplers in case
979 * two different sampler types are set to the same texture unit.
981 ctx
->_Shader
->Validated
= GL_FALSE
;
984 if (uni
->type
->is_image()) {
985 for (int i
= 0; i
< count
; i
++) {
986 const int unit
= ((GLint
*) values
)[i
];
988 /* check that the image unit is legal */
989 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
990 _mesa_error(ctx
, GL_INVALID_VALUE
,
991 "glUniform1i(invalid image unit index for uniform %d)",
1003 * Called via glUniform*() functions.
1006 _mesa_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
1007 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1008 enum glsl_base_type basicType
, unsigned src_components
)
1011 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
1013 struct gl_uniform_storage
*uni
;
1014 if (_mesa_is_no_error_enabled(ctx
)) {
1015 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1017 * "If the value of location is -1, the Uniform* commands will
1018 * silently ignore the data passed in, and the current uniform values
1019 * will not be changed.
1024 uni
= shProg
->UniformRemapTable
[location
];
1026 /* The array index specified by the uniform location is just the
1027 * uniform location minus the base location of of the uniform.
1029 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
1030 offset
= location
- uni
->remap_location
;
1032 uni
= validate_uniform(location
, count
, values
, &offset
, ctx
, shProg
,
1033 basicType
, src_components
);
1038 const unsigned components
= uni
->type
->vector_elements
;
1040 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1042 * "When loading N elements starting at an arbitrary position k in a
1043 * uniform declared as an array, elements k through k + N - 1 in the
1044 * array will be replaced with the new values. Values for any array
1045 * element that exceeds the highest array element index used, as
1046 * reported by GetActiveUniform, will be ignored by the GL."
1048 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1049 * will have already generated an error.
1051 if (uni
->array_elements
!= 0) {
1052 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1055 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1057 /* Store the data in the "actual type" backing storage for the uniform.
1059 if (!uni
->type
->is_boolean()) {
1060 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
1061 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
1063 const union gl_constant_value
*src
=
1064 (const union gl_constant_value
*) values
;
1065 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
1066 const unsigned elems
= components
* count
;
1068 for (unsigned i
= 0; i
< elems
; i
++) {
1069 if (basicType
== GLSL_TYPE_FLOAT
) {
1070 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
1072 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
1077 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1079 /* If the uniform is a sampler, do the extra magic necessary to propagate
1080 * the changes through.
1082 if (uni
->type
->is_sampler()) {
1083 bool flushed
= false;
1085 shProg
->SamplersValidated
= GL_TRUE
;
1087 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1088 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
1090 /* If the shader stage doesn't use the sampler uniform, skip this. */
1091 if (!uni
->opaque
[i
].active
)
1094 bool changed
= false;
1095 for (int j
= 0; j
< count
; j
++) {
1096 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
1097 if (sh
->Program
->SamplerUnits
[unit
] != ((unsigned *) values
)[j
]) {
1098 sh
->Program
->SamplerUnits
[unit
] = ((unsigned *) values
)[j
];
1105 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
);
1109 struct gl_program
*const prog
= sh
->Program
;
1110 _mesa_update_shader_textures_used(shProg
, prog
);
1111 if (ctx
->Driver
.SamplerUniformChange
)
1112 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
1117 /* If the uniform is an image, update the mapping from image
1118 * uniforms to image units present in the shader data structure.
1120 if (uni
->type
->is_image()) {
1121 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1122 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1124 /* If the shader stage doesn't use the image uniform, skip this. */
1125 if (!uni
->opaque
[i
].active
)
1128 for (int j
= 0; j
< count
; j
++)
1129 sh
->Program
->sh
.ImageUnits
[uni
->opaque
[i
].index
+ offset
+ j
] =
1130 ((GLint
*) values
)[j
];
1133 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
1138 * Called by glUniformMatrix*() functions.
1139 * Note: cols=2, rows=4 ==> array[2] of vec4
1142 _mesa_uniform_matrix(GLint location
, GLsizei count
,
1143 GLboolean transpose
, const void *values
,
1144 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1145 GLuint cols
, GLuint rows
, enum glsl_base_type basicType
)
1148 struct gl_uniform_storage
*const uni
=
1149 validate_uniform_parameters(location
, count
, &offset
,
1150 ctx
, shProg
, "glUniformMatrix");
1154 if (!uni
->type
->is_matrix()) {
1155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1156 "glUniformMatrix(non-matrix uniform)");
1160 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
1161 const unsigned size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
1163 assert(!uni
->type
->is_sampler());
1164 const unsigned vectors
= uni
->type
->matrix_columns
;
1165 const unsigned components
= uni
->type
->vector_elements
;
1167 /* Verify that the types are compatible. This is greatly simplified for
1168 * matrices because they can only have a float base type.
1170 if (vectors
!= cols
|| components
!= rows
) {
1171 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1172 "glUniformMatrix(matrix size mismatch)");
1176 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1177 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1180 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
1181 _mesa_error(ctx
, GL_INVALID_VALUE
,
1182 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1187 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1190 * "If any of the following conditions occur, an INVALID_OPERATION
1191 * error is generated by the Uniform* commands, and no uniform values
1196 * - if the uniform declared in the shader is not of type boolean and
1197 * the type indicated in the name of the Uniform* command used does
1198 * not match the type of the uniform"
1200 * There are no Boolean matrix types, so we do not need to allow
1201 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1203 if (uni
->type
->base_type
!= basicType
) {
1204 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1205 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1206 cols
, rows
, uni
->name
, location
,
1207 glsl_type_name(uni
->type
->base_type
),
1208 glsl_type_name(basicType
));
1212 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1213 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
1214 bool(transpose
), shProg
, location
, uni
);
1217 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1219 * "When loading N elements starting at an arbitrary position k in a
1220 * uniform declared as an array, elements k through k + N - 1 in the
1221 * array will be replaced with the new values. Values for any array
1222 * element that exceeds the highest array element index used, as
1223 * reported by GetActiveUniform, will be ignored by the GL."
1225 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1226 * will have already generated an error.
1228 if (uni
->array_elements
!= 0) {
1229 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1232 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1234 /* Store the data in the "actual type" backing storage for the uniform.
1236 const unsigned elements
= components
* vectors
;
1239 memcpy(&uni
->storage
[size_mul
* elements
* offset
], values
,
1240 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
1241 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1242 /* Copy and transpose the matrix.
1244 const float *src
= (const float *)values
;
1245 float *dst
= &uni
->storage
[elements
* offset
].f
;
1247 for (int i
= 0; i
< count
; i
++) {
1248 for (unsigned r
= 0; r
< rows
; r
++) {
1249 for (unsigned c
= 0; c
< cols
; c
++) {
1250 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1258 assert(basicType
== GLSL_TYPE_DOUBLE
);
1259 const double *src
= (const double *)values
;
1260 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
1262 for (int i
= 0; i
< count
; i
++) {
1263 for (unsigned r
= 0; r
< rows
; r
++) {
1264 for (unsigned c
= 0; c
< cols
; c
++) {
1265 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1274 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1279 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1280 char *errMsg
, size_t errMsgLength
)
1282 /* Shader does not have samplers. */
1283 if (shProg
->data
->NumUniformStorage
== 0)
1286 if (!shProg
->SamplersValidated
) {
1287 _mesa_snprintf(errMsg
, errMsgLength
,
1288 "active samplers with a different type "
1289 "refer to the same texture image unit");
1296 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1298 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1299 * OpenGL 4.1 spec says:
1301 * "[INVALID_OPERATION] is generated by any command that transfers
1302 * vertices to the GL if:
1306 * - Any two active samplers in the current program object are of
1307 * different types, but refer to the same texture image unit.
1309 * - The number of active samplers in the program exceeds the
1310 * maximum number of texture image units allowed."
1314 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1315 unsigned active_samplers
= 0;
1316 const struct gl_program
**prog
=
1317 (const struct gl_program
**) pipeline
->CurrentProgram
;
1320 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1322 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1326 mask
= prog
[idx
]->SamplersUsed
;
1328 const int s
= u_bit_scan(&mask
);
1329 GLuint unit
= prog
[idx
]->SamplerUnits
[s
];
1330 GLuint tgt
= prog
[idx
]->sh
.SamplerTargets
[s
];
1332 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1333 * great job of eliminating unused uniforms currently so for now
1334 * don't throw an error if two sampler types both point to 0.
1339 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1341 ralloc_asprintf(pipeline
,
1343 "Texture unit %d is accessed with 2 different types",
1344 prog
[idx
]->Id
, unit
);
1348 TexturesUsed
[unit
] |= (1 << tgt
);
1351 active_samplers
+= prog
[idx
]->info
.num_textures
;
1354 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1356 ralloc_asprintf(pipeline
,
1357 "the number of active samplers %d exceed the "
1359 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);