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
||
357 returnType
== GLSL_TYPE_UINT
) &&
358 (uni
->type
->base_type
== GLSL_TYPE_INT
||
359 uni
->type
->base_type
== GLSL_TYPE_UINT
||
360 uni
->type
->is_sampler() ||
361 uni
->type
->is_image())) ||
362 ((returnType
== GLSL_TYPE_UINT64
||
363 returnType
== GLSL_TYPE_INT64
) &&
364 (uni
->type
->base_type
== GLSL_TYPE_UINT64
||
365 uni
->type
->base_type
== GLSL_TYPE_INT64
))) {
366 memcpy(paramsOut
, src
, bytes
);
368 union gl_constant_value
*const dst
=
369 (union gl_constant_value
*) paramsOut
;
370 /* This code could be optimized by putting the loop inside the switch
371 * statements. However, this is not expected to be
372 * performance-critical code.
374 for (unsigned i
= 0; i
< elements
; i
++) {
378 switch (returnType
) {
379 case GLSL_TYPE_FLOAT
:
380 switch (uni
->type
->base_type
) {
382 dst
[didx
].f
= (float) src
[sidx
].u
;
385 case GLSL_TYPE_SAMPLER
:
386 case GLSL_TYPE_IMAGE
:
387 dst
[didx
].f
= (float) src
[sidx
].i
;
390 dst
[didx
].f
= src
[sidx
].i
? 1.0f
: 0.0f
;
392 case GLSL_TYPE_DOUBLE
: {
394 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
398 case GLSL_TYPE_UINT64
: {
400 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
404 case GLSL_TYPE_INT64
: {
406 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
411 assert(!"Should not get here.");
416 case GLSL_TYPE_DOUBLE
:
417 switch (uni
->type
->base_type
) {
418 case GLSL_TYPE_UINT
: {
419 double tmp
= src
[sidx
].u
;
420 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
424 case GLSL_TYPE_SAMPLER
:
425 case GLSL_TYPE_IMAGE
: {
426 double tmp
= src
[sidx
].i
;
427 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
430 case GLSL_TYPE_BOOL
: {
431 double tmp
= src
[sidx
].i
? 1.0 : 0.0;
432 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
435 case GLSL_TYPE_FLOAT
: {
436 double tmp
= src
[sidx
].f
;
437 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
440 case GLSL_TYPE_UINT64
: {
443 memcpy(&tmpu
, &src
[sidx
].u
, sizeof(tmpu
));
445 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
448 case GLSL_TYPE_INT64
: {
451 memcpy(&tmpi
, &src
[sidx
].i
, sizeof(tmpi
));
453 memcpy(&dst
[didx
].f
, &tmp
, sizeof(tmp
));
457 assert(!"Should not get here.");
464 switch (uni
->type
->base_type
) {
465 case GLSL_TYPE_FLOAT
:
466 /* While the GL 3.2 core spec doesn't explicitly
467 * state how conversion of float uniforms to integer
468 * values works, in section 6.2 "State Tables" on
471 * "Unless otherwise specified, when floating
472 * point state is returned as integer values or
473 * integer state is returned as floating-point
474 * values it is converted in the fashion
475 * described in section 6.1.2"
477 * That section, on page 248, says:
479 * "If GetIntegerv or GetInteger64v are called,
480 * a floating-point value is rounded to the
481 * nearest integer..."
483 dst
[didx
].i
= IROUND(src
[sidx
].f
);
486 dst
[didx
].i
= src
[sidx
].i
? 1 : 0;
488 case GLSL_TYPE_DOUBLE
: {
490 memcpy(&tmp
, &src
[sidx
].f
, sizeof(tmp
));
491 dst
[didx
].i
= IROUNDD(tmp
);
494 case GLSL_TYPE_UINT64
: {
496 memcpy(&tmp
, &src
[sidx
].u
, sizeof(tmp
));
500 case GLSL_TYPE_INT64
: {
502 memcpy(&tmp
, &src
[sidx
].i
, sizeof(tmp
));
507 assert(!"Should not get here.");
512 case GLSL_TYPE_INT64
:
513 case GLSL_TYPE_UINT64
:
514 switch (uni
->type
->base_type
) {
515 case GLSL_TYPE_UINT
: {
516 uint64_t tmp
= src
[sidx
].u
;
517 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
521 case GLSL_TYPE_SAMPLER
:
522 case GLSL_TYPE_IMAGE
: {
523 int64_t tmp
= src
[sidx
].i
;
524 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
527 case GLSL_TYPE_BOOL
: {
528 int64_t tmp
= src
[sidx
].i
? 1.0f
: 0.0f
;
529 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
532 case GLSL_TYPE_FLOAT
: {
533 int64_t tmp
= src
[sidx
].f
;
534 memcpy(&dst
[didx
].u
, &tmp
, sizeof(tmp
));
538 assert(!"Should not get here.");
544 assert(!"Should not get here.");
553 log_uniform(const void *values
, enum glsl_base_type basicType
,
554 unsigned rows
, unsigned cols
, unsigned count
,
556 const struct gl_shader_program
*shProg
,
558 const struct gl_uniform_storage
*uni
)
561 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
562 const unsigned elems
= rows
* cols
* count
;
563 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
565 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
566 "transpose = %s) to: ",
567 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
568 transpose
? "true" : "false");
569 for (unsigned i
= 0; i
< elems
; i
++) {
570 if (i
!= 0 && ((i
% rows
) == 0))
575 printf("%u ", v
[i
].u
);
578 printf("%d ", v
[i
].i
);
580 case GLSL_TYPE_UINT64
: {
582 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
583 printf("%" PRIu64
" ", tmp
);
586 case GLSL_TYPE_INT64
: {
588 memcpy(&tmp
, &v
[i
* 2].u
, sizeof(tmp
));
589 printf("%" PRId64
" ", tmp
);
592 case GLSL_TYPE_FLOAT
:
593 printf("%g ", v
[i
].f
);
595 case GLSL_TYPE_DOUBLE
: {
597 memcpy(&tmp
, &v
[i
* 2].f
, sizeof(tmp
));
602 assert(!"Should not get here.");
612 log_program_parameters(const struct gl_shader_program
*shProg
)
614 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
615 if (shProg
->_LinkedShaders
[i
] == NULL
)
618 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
620 printf("Program %d %s shader parameters:\n",
621 shProg
->Name
, _mesa_shader_stage_to_string(i
));
622 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
623 printf("%s: %p %f %f %f %f\n",
624 prog
->Parameters
->Parameters
[j
].Name
,
625 prog
->Parameters
->ParameterValues
[j
],
626 prog
->Parameters
->ParameterValues
[j
][0].f
,
627 prog
->Parameters
->ParameterValues
[j
][1].f
,
628 prog
->Parameters
->ParameterValues
[j
][2].f
,
629 prog
->Parameters
->ParameterValues
[j
][3].f
);
637 * Propagate some values from uniform backing storage to driver storage
639 * Values propagated from uniform backing storage to driver storage
640 * have all format / type conversions previously requested by the
641 * driver applied. This function is most often called by the
642 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
645 * \param uni Uniform whose data is to be propagated to driver storage
646 * \param array_index If \c uni is an array, this is the element of
647 * the array to be propagated.
648 * \param count Number of array elements to propagate.
651 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
652 unsigned array_index
,
657 const unsigned components
= uni
->type
->vector_elements
;
658 const unsigned vectors
= uni
->type
->matrix_columns
;
659 const int dmul
= uni
->type
->is_64bit() ? 2 : 1;
661 /* Store the data in the driver's requested type in the driver's storage
664 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
666 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
667 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
668 uint8_t *dst
= (uint8_t *) store
->data
;
669 const unsigned extra_stride
=
670 store
->element_stride
- (vectors
* store
->vector_stride
);
672 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
675 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
677 __func__
, dst
, array_index
, components
,
678 vectors
, count
, store
->vector_stride
, extra_stride
);
681 dst
+= array_index
* store
->element_stride
;
683 switch (store
->format
) {
684 case uniform_native
: {
688 if (src_vector_byte_stride
== store
->vector_stride
) {
690 for (j
= 0; j
< count
; j
++) {
691 memcpy(dst
, src
, src_vector_byte_stride
* vectors
);
692 src
+= src_vector_byte_stride
* vectors
;
693 dst
+= store
->vector_stride
* vectors
;
698 /* Unigine Heaven benchmark gets here */
699 memcpy(dst
, src
, src_vector_byte_stride
* vectors
* count
);
700 src
+= src_vector_byte_stride
* vectors
* count
;
701 dst
+= store
->vector_stride
* vectors
* count
;
704 for (j
= 0; j
< count
; j
++) {
705 for (v
= 0; v
< vectors
; v
++) {
706 memcpy(dst
, src
, src_vector_byte_stride
);
707 src
+= src_vector_byte_stride
;
708 dst
+= store
->vector_stride
;
717 case uniform_int_float
: {
718 const int *isrc
= (const int *) src
;
723 for (j
= 0; j
< count
; j
++) {
724 for (v
= 0; v
< vectors
; v
++) {
725 for (c
= 0; c
< components
; c
++) {
726 ((float *) dst
)[c
] = (float) *isrc
;
730 dst
+= store
->vector_stride
;
739 assert(!"Should not get here.");
747 * Return printable string for a given GLSL_TYPE_x
750 glsl_type_name(enum glsl_base_type type
)
757 case GLSL_TYPE_FLOAT
:
759 case GLSL_TYPE_DOUBLE
:
761 case GLSL_TYPE_UINT64
:
763 case GLSL_TYPE_INT64
:
767 case GLSL_TYPE_SAMPLER
:
769 case GLSL_TYPE_IMAGE
:
771 case GLSL_TYPE_ATOMIC_UINT
:
772 return "atomic_uint";
773 case GLSL_TYPE_STRUCT
:
775 case GLSL_TYPE_INTERFACE
:
777 case GLSL_TYPE_ARRAY
:
781 case GLSL_TYPE_ERROR
:
789 static struct gl_uniform_storage
*
790 validate_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
791 unsigned *offset
, struct gl_context
*ctx
,
792 struct gl_shader_program
*shProg
,
793 enum glsl_base_type basicType
, unsigned src_components
)
795 struct gl_uniform_storage
*const uni
=
796 validate_uniform_parameters(location
, count
, offset
,
797 ctx
, shProg
, "glUniform");
801 if (uni
->type
->is_matrix()) {
802 /* Can't set matrix uniforms (like mat4) with glUniform */
803 _mesa_error(ctx
, GL_INVALID_OPERATION
,
804 "glUniform%u(uniform \"%s\"@%d is matrix)",
805 src_components
, uni
->name
, location
);
809 /* Verify that the types are compatible. */
810 const unsigned components
= uni
->type
->vector_elements
;
812 if (components
!= src_components
) {
813 /* glUniformN() must match float/vecN type */
814 _mesa_error(ctx
, GL_INVALID_OPERATION
,
815 "glUniform%u(\"%s\"@%u has %u components, not %u)",
816 src_components
, uni
->name
, location
,
817 components
, src_components
);
822 switch (uni
->type
->base_type
) {
824 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
826 case GLSL_TYPE_SAMPLER
:
827 match
= (basicType
== GLSL_TYPE_INT
);
829 case GLSL_TYPE_IMAGE
:
830 match
= (basicType
== GLSL_TYPE_INT
&& _mesa_is_desktop_gl(ctx
));
833 match
= (basicType
== uni
->type
->base_type
);
838 _mesa_error(ctx
, GL_INVALID_OPERATION
,
839 "glUniform%u(\"%s\"@%d is %s, not %s)",
840 src_components
, uni
->name
, location
,
841 glsl_type_name(uni
->type
->base_type
),
842 glsl_type_name(basicType
));
846 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
847 log_uniform(values
, basicType
, components
, 1, count
,
848 false, shProg
, location
, uni
);
851 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
853 * "Setting a sampler's value to i selects texture image unit number
854 * i. The values of i range from zero to the implementation- dependent
855 * maximum supported number of texture image units."
857 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
860 * "Error Description Offending command
863 * INVALID_VALUE Numeric argument out of range Yes"
865 * Based on that, when an invalid sampler is specified, we generate a
866 * GL_INVALID_VALUE error and ignore the command.
868 if (uni
->type
->is_sampler()) {
869 for (int i
= 0; i
< count
; i
++) {
870 const unsigned texUnit
= ((unsigned *) values
)[i
];
872 /* check that the sampler (tex unit index) is legal */
873 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
874 _mesa_error(ctx
, GL_INVALID_VALUE
,
875 "glUniform1i(invalid sampler/tex unit index for "
876 "uniform %d)", location
);
880 /* We need to reset the validate flag on changes to samplers in case
881 * two different sampler types are set to the same texture unit.
883 ctx
->_Shader
->Validated
= GL_FALSE
;
886 if (uni
->type
->is_image()) {
887 for (int i
= 0; i
< count
; i
++) {
888 const int unit
= ((GLint
*) values
)[i
];
890 /* check that the image unit is legal */
891 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
892 _mesa_error(ctx
, GL_INVALID_VALUE
,
893 "glUniform1i(invalid image unit index for uniform %d)",
905 * Called via glUniform*() functions.
908 _mesa_uniform(GLint location
, GLsizei count
, const GLvoid
*values
,
909 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
910 enum glsl_base_type basicType
, unsigned src_components
)
913 int size_mul
= glsl_base_type_is_64bit(basicType
) ? 2 : 1;
915 struct gl_uniform_storage
*uni
;
916 if (_mesa_is_no_error_enabled(ctx
)) {
917 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
919 * "If the value of location is -1, the Uniform* commands will
920 * silently ignore the data passed in, and the current uniform values
921 * will not be changed.
926 uni
= shProg
->UniformRemapTable
[location
];
928 /* The array index specified by the uniform location is just the
929 * uniform location minus the base location of of the uniform.
931 assert(uni
->array_elements
> 0 || location
== (int)uni
->remap_location
);
932 offset
= location
- uni
->remap_location
;
934 uni
= validate_uniform(location
, count
, values
, &offset
, ctx
, shProg
,
935 basicType
, src_components
);
940 const unsigned components
= uni
->type
->vector_elements
;
942 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
944 * "When loading N elements starting at an arbitrary position k in a
945 * uniform declared as an array, elements k through k + N - 1 in the
946 * array will be replaced with the new values. Values for any array
947 * element that exceeds the highest array element index used, as
948 * reported by GetActiveUniform, will be ignored by the GL."
950 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
951 * will have already generated an error.
953 if (uni
->array_elements
!= 0) {
954 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
957 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
959 /* Store the data in the "actual type" backing storage for the uniform.
961 if (!uni
->type
->is_boolean()) {
962 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
963 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
965 const union gl_constant_value
*src
=
966 (const union gl_constant_value
*) values
;
967 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
968 const unsigned elems
= components
* count
;
970 for (unsigned i
= 0; i
< elems
; i
++) {
971 if (basicType
== GLSL_TYPE_FLOAT
) {
972 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
974 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
979 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
981 /* If the uniform is a sampler, do the extra magic necessary to propagate
982 * the changes through.
984 if (uni
->type
->is_sampler()) {
985 bool flushed
= false;
987 shProg
->SamplersValidated
= GL_TRUE
;
989 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
990 struct gl_linked_shader
*const sh
= shProg
->_LinkedShaders
[i
];
992 /* If the shader stage doesn't use the sampler uniform, skip this. */
993 if (!uni
->opaque
[i
].active
)
996 bool changed
= false;
997 for (int j
= 0; j
< count
; j
++) {
998 unsigned unit
= uni
->opaque
[i
].index
+ offset
+ j
;
999 if (sh
->Program
->SamplerUnits
[unit
] != ((unsigned *) values
)[j
]) {
1000 sh
->Program
->SamplerUnits
[unit
] = ((unsigned *) values
)[j
];
1007 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
);
1011 struct gl_program
*const prog
= sh
->Program
;
1012 _mesa_update_shader_textures_used(shProg
, prog
);
1013 if (ctx
->Driver
.SamplerUniformChange
)
1014 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
1019 /* If the uniform is an image, update the mapping from image
1020 * uniforms to image units present in the shader data structure.
1022 if (uni
->type
->is_image()) {
1023 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1024 struct gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
1026 /* If the shader stage doesn't use the image uniform, skip this. */
1027 if (!uni
->opaque
[i
].active
)
1030 for (int j
= 0; j
< count
; j
++)
1031 sh
->Program
->sh
.ImageUnits
[uni
->opaque
[i
].index
+ offset
+ j
] =
1032 ((GLint
*) values
)[j
];
1035 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
1040 * Called by glUniformMatrix*() functions.
1041 * Note: cols=2, rows=4 ==> array[2] of vec4
1044 _mesa_uniform_matrix(GLint location
, GLsizei count
,
1045 GLboolean transpose
, const void *values
,
1046 struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1047 GLuint cols
, GLuint rows
, enum glsl_base_type basicType
)
1050 struct gl_uniform_storage
*const uni
=
1051 validate_uniform_parameters(location
, count
, &offset
,
1052 ctx
, shProg
, "glUniformMatrix");
1056 if (!uni
->type
->is_matrix()) {
1057 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1058 "glUniformMatrix(non-matrix uniform)");
1062 assert(basicType
== GLSL_TYPE_FLOAT
|| basicType
== GLSL_TYPE_DOUBLE
);
1063 const unsigned size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
1065 assert(!uni
->type
->is_sampler());
1066 const unsigned vectors
= uni
->type
->matrix_columns
;
1067 const unsigned components
= uni
->type
->vector_elements
;
1069 /* Verify that the types are compatible. This is greatly simplified for
1070 * matrices because they can only have a float base type.
1072 if (vectors
!= cols
|| components
!= rows
) {
1073 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1074 "glUniformMatrix(matrix size mismatch)");
1078 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1079 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1082 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
1083 _mesa_error(ctx
, GL_INVALID_VALUE
,
1084 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1089 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1092 * "If any of the following conditions occur, an INVALID_OPERATION
1093 * error is generated by the Uniform* commands, and no uniform values
1098 * - if the uniform declared in the shader is not of type boolean and
1099 * the type indicated in the name of the Uniform* command used does
1100 * not match the type of the uniform"
1102 * There are no Boolean matrix types, so we do not need to allow
1103 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1105 if (uni
->type
->base_type
!= basicType
) {
1106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1107 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1108 cols
, rows
, uni
->name
, location
,
1109 glsl_type_name(uni
->type
->base_type
),
1110 glsl_type_name(basicType
));
1114 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
1115 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
1116 bool(transpose
), shProg
, location
, uni
);
1119 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1121 * "When loading N elements starting at an arbitrary position k in a
1122 * uniform declared as an array, elements k through k + N - 1 in the
1123 * array will be replaced with the new values. Values for any array
1124 * element that exceeds the highest array element index used, as
1125 * reported by GetActiveUniform, will be ignored by the GL."
1127 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1128 * will have already generated an error.
1130 if (uni
->array_elements
!= 0) {
1131 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
1134 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1136 /* Store the data in the "actual type" backing storage for the uniform.
1138 const unsigned elements
= components
* vectors
;
1141 memcpy(&uni
->storage
[size_mul
* elements
* offset
], values
,
1142 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
1143 } else if (basicType
== GLSL_TYPE_FLOAT
) {
1144 /* Copy and transpose the matrix.
1146 const float *src
= (const float *)values
;
1147 float *dst
= &uni
->storage
[elements
* offset
].f
;
1149 for (int i
= 0; i
< count
; i
++) {
1150 for (unsigned r
= 0; r
< rows
; r
++) {
1151 for (unsigned c
= 0; c
< cols
; c
++) {
1152 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1160 assert(basicType
== GLSL_TYPE_DOUBLE
);
1161 const double *src
= (const double *)values
;
1162 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
1164 for (int i
= 0; i
< count
; i
++) {
1165 for (unsigned r
= 0; r
< rows
; r
++) {
1166 for (unsigned c
= 0; c
< cols
; c
++) {
1167 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1176 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1181 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1182 char *errMsg
, size_t errMsgLength
)
1184 /* Shader does not have samplers. */
1185 if (shProg
->data
->NumUniformStorage
== 0)
1188 if (!shProg
->SamplersValidated
) {
1189 _mesa_snprintf(errMsg
, errMsgLength
,
1190 "active samplers with a different type "
1191 "refer to the same texture image unit");
1198 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1200 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1201 * OpenGL 4.1 spec says:
1203 * "[INVALID_OPERATION] is generated by any command that transfers
1204 * vertices to the GL if:
1208 * - Any two active samplers in the current program object are of
1209 * different types, but refer to the same texture image unit.
1211 * - The number of active samplers in the program exceeds the
1212 * maximum number of texture image units allowed."
1216 GLbitfield TexturesUsed
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1217 unsigned active_samplers
= 0;
1218 const struct gl_program
**prog
=
1219 (const struct gl_program
**) pipeline
->CurrentProgram
;
1222 memset(TexturesUsed
, 0, sizeof(TexturesUsed
));
1224 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1228 mask
= prog
[idx
]->SamplersUsed
;
1230 const int s
= u_bit_scan(&mask
);
1231 GLuint unit
= prog
[idx
]->SamplerUnits
[s
];
1232 GLuint tgt
= prog
[idx
]->sh
.SamplerTargets
[s
];
1234 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1235 * great job of eliminating unused uniforms currently so for now
1236 * don't throw an error if two sampler types both point to 0.
1241 if (TexturesUsed
[unit
] & ~(1 << tgt
)) {
1243 ralloc_asprintf(pipeline
,
1245 "Texture unit %d is accessed with 2 different types",
1246 prog
[idx
]->Id
, unit
);
1250 TexturesUsed
[unit
] |= (1 << tgt
);
1253 active_samplers
+= prog
[idx
]->info
.num_textures
;
1256 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1258 ralloc_asprintf(pipeline
,
1259 "the number of active samplers %d exceed the "
1261 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);