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.
29 #include "main/core.h"
30 #include "main/context.h"
32 #include "ir_uniform.h"
33 #include "program/hash_table.h"
34 #include "../glsl/program.h"
35 #include "../glsl/ir_uniform.h"
36 #include "../glsl/glsl_parser_extras.h"
37 #include "main/shaderapi.h"
38 #include "main/shaderobj.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(struct gl_context
*ctx
,
161 struct gl_shader_program
*shProg
,
162 GLint location
, GLsizei count
,
163 unsigned *array_index
,
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->LinkStatus check out of the main path.
185 if (unlikely(location
>= (GLint
) shProg
->NumUniformRemapTable
)) {
186 if (!shProg
->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
->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(ctx
, shProg
, location
, 1,
289 &offset
, "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
->is_sampler())
325 ? 1 : uni
->type
->components();
327 /* Calculate the source base address *BEFORE* modifying elements to
328 * account for the size of the user's buffer.
330 const union gl_constant_value
*const src
=
331 &uni
->storage
[offset
* elements
];
333 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
334 returnType
== GLSL_TYPE_UINT
);
335 /* The three (currently) supported types all have the same size,
336 * which is of course the same as their union. That'll change
337 * with glGetUniformdv()...
339 unsigned bytes
= sizeof(src
[0]) * elements
;
340 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
341 _mesa_error( ctx
, GL_INVALID_OPERATION
,
342 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
343 " but %u bytes are required)", bufSize
, bytes
);
347 /* If the return type and the uniform's native type are "compatible,"
348 * just memcpy the data. If the types are not compatible, perform a
349 * slower convert-and-copy process.
351 if (returnType
== uni
->type
->base_type
352 || ((returnType
== GLSL_TYPE_INT
353 || returnType
== GLSL_TYPE_UINT
)
355 (uni
->type
->base_type
== GLSL_TYPE_INT
356 || uni
->type
->base_type
== GLSL_TYPE_UINT
357 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
358 || uni
->type
->base_type
== GLSL_TYPE_IMAGE
))) {
359 memcpy(paramsOut
, src
, bytes
);
361 union gl_constant_value
*const dst
=
362 (union gl_constant_value
*) paramsOut
;
364 /* This code could be optimized by putting the loop inside the switch
365 * statements. However, this is not expected to be
366 * performance-critical code.
368 for (unsigned i
= 0; i
< elements
; i
++) {
369 switch (returnType
) {
370 case GLSL_TYPE_FLOAT
:
371 switch (uni
->type
->base_type
) {
373 dst
[i
].f
= (float) src
[i
].u
;
376 case GLSL_TYPE_SAMPLER
:
377 case GLSL_TYPE_IMAGE
:
378 dst
[i
].f
= (float) src
[i
].i
;
381 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
384 assert(!"Should not get here.");
391 switch (uni
->type
->base_type
) {
392 case GLSL_TYPE_FLOAT
:
393 /* While the GL 3.2 core spec doesn't explicitly
394 * state how conversion of float uniforms to integer
395 * values works, in section 6.2 "State Tables" on
398 * "Unless otherwise specified, when floating
399 * point state is returned as integer values or
400 * integer state is returned as floating-point
401 * values it is converted in the fashion
402 * described in section 6.1.2"
404 * That section, on page 248, says:
406 * "If GetIntegerv or GetInteger64v are called,
407 * a floating-point value is rounded to the
408 * nearest integer..."
410 dst
[i
].i
= IROUND(src
[i
].f
);
413 dst
[i
].i
= src
[i
].i
? 1 : 0;
416 assert(!"Should not get here.");
422 assert(!"Should not get here.");
431 log_uniform(const void *values
, enum glsl_base_type basicType
,
432 unsigned rows
, unsigned cols
, unsigned count
,
434 const struct gl_shader_program
*shProg
,
436 const struct gl_uniform_storage
*uni
)
439 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
440 const unsigned elems
= rows
* cols
* count
;
441 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
443 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
444 "transpose = %s) to: ",
445 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
446 transpose
? "true" : "false");
447 for (unsigned i
= 0; i
< elems
; i
++) {
448 if (i
!= 0 && ((i
% rows
) == 0))
453 printf("%u ", v
[i
].u
);
456 printf("%d ", v
[i
].i
);
458 case GLSL_TYPE_FLOAT
:
459 printf("%g ", v
[i
].f
);
461 case GLSL_TYPE_DOUBLE
:
462 printf("%g ", *(double* )&v
[i
* 2].f
);
465 assert(!"Should not get here.");
475 log_program_parameters(const struct gl_shader_program
*shProg
)
477 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
478 if (shProg
->_LinkedShaders
[i
] == NULL
)
481 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
483 printf("Program %d %s shader parameters:\n",
484 shProg
->Name
, _mesa_shader_stage_to_string(i
));
485 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
486 printf("%s: %p %f %f %f %f\n",
487 prog
->Parameters
->Parameters
[j
].Name
,
488 prog
->Parameters
->ParameterValues
[j
],
489 prog
->Parameters
->ParameterValues
[j
][0].f
,
490 prog
->Parameters
->ParameterValues
[j
][1].f
,
491 prog
->Parameters
->ParameterValues
[j
][2].f
,
492 prog
->Parameters
->ParameterValues
[j
][3].f
);
500 * Propagate some values from uniform backing storage to driver storage
502 * Values propagated from uniform backing storage to driver storage
503 * have all format / type conversions previously requested by the
504 * driver applied. This function is most often called by the
505 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
508 * \param uni Uniform whose data is to be propagated to driver storage
509 * \param array_index If \c uni is an array, this is the element of
510 * the array to be propagated.
511 * \param count Number of array elements to propagate.
514 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
515 unsigned array_index
,
520 /* vector_elements and matrix_columns can be 0 for samplers.
522 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
523 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
524 const int dmul
= uni
->type
->base_type
== GLSL_TYPE_DOUBLE
? 2 : 1;
526 /* Store the data in the driver's requested type in the driver's storage
529 unsigned src_vector_byte_stride
= components
* 4 * dmul
;
531 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
532 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
533 uint8_t *dst
= (uint8_t *) store
->data
;
534 const unsigned extra_stride
=
535 store
->element_stride
- (vectors
* store
->vector_stride
);
537 (uint8_t *) (&uni
->storage
[array_index
* (dmul
* components
* vectors
)].i
);
540 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
542 __func__
, dst
, array_index
, components
,
543 vectors
, count
, store
->vector_stride
, extra_stride
);
546 dst
+= array_index
* store
->element_stride
;
548 switch (store
->format
) {
549 case uniform_native
: {
553 for (j
= 0; j
< count
; j
++) {
554 for (v
= 0; v
< vectors
; v
++) {
555 memcpy(dst
, src
, src_vector_byte_stride
);
556 src
+= src_vector_byte_stride
;
557 dst
+= store
->vector_stride
;
565 case uniform_int_float
: {
566 const int *isrc
= (const int *) src
;
571 for (j
= 0; j
< count
; j
++) {
572 for (v
= 0; v
< vectors
; v
++) {
573 for (c
= 0; c
< components
; c
++) {
574 ((float *) dst
)[c
] = (float) *isrc
;
578 dst
+= store
->vector_stride
;
587 assert(!"Should not get here.");
595 * Return printable string for a given GLSL_TYPE_x
598 glsl_type_name(enum glsl_base_type type
)
605 case GLSL_TYPE_FLOAT
:
607 case GLSL_TYPE_DOUBLE
:
611 case GLSL_TYPE_SAMPLER
:
613 case GLSL_TYPE_IMAGE
:
615 case GLSL_TYPE_ATOMIC_UINT
:
616 return "atomic_uint";
617 case GLSL_TYPE_STRUCT
:
619 case GLSL_TYPE_INTERFACE
:
621 case GLSL_TYPE_ARRAY
:
625 case GLSL_TYPE_ERROR
:
634 * Called via glUniform*() functions.
637 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
638 GLint location
, GLsizei count
,
639 const GLvoid
*values
,
640 enum glsl_base_type basicType
,
641 unsigned src_components
)
644 int size_mul
= basicType
== GLSL_TYPE_DOUBLE
? 2 : 1;
646 struct gl_uniform_storage
*const uni
=
647 validate_uniform_parameters(ctx
, shProg
, location
, count
,
648 &offset
, "glUniform");
652 if (uni
->type
->is_matrix()) {
653 /* Can't set matrix uniforms (like mat4) with glUniform */
654 _mesa_error(ctx
, GL_INVALID_OPERATION
,
655 "glUniform%u(uniform \"%s\"@%d is matrix)",
656 src_components
, uni
->name
, location
);
660 /* Verify that the types are compatible.
662 const unsigned components
= uni
->type
->is_sampler()
663 ? 1 : uni
->type
->vector_elements
;
665 if (components
!= src_components
) {
666 /* glUniformN() must match float/vecN type */
667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
668 "glUniform%u(\"%s\"@%u has %u components, not %u)",
669 src_components
, uni
->name
, location
,
670 components
, src_components
);
675 switch (uni
->type
->base_type
) {
677 match
= (basicType
!= GLSL_TYPE_DOUBLE
);
679 case GLSL_TYPE_SAMPLER
:
680 case GLSL_TYPE_IMAGE
:
681 match
= (basicType
== GLSL_TYPE_INT
);
684 match
= (basicType
== uni
->type
->base_type
);
689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
690 "glUniform%u(\"%s\"@%d is %s, not %s)",
691 src_components
, uni
->name
, location
,
692 glsl_type_name(uni
->type
->base_type
),
693 glsl_type_name(basicType
));
697 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
698 log_uniform(values
, basicType
, components
, 1, count
,
699 false, shProg
, location
, uni
);
702 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
704 * "Setting a sampler's value to i selects texture image unit number
705 * i. The values of i range from zero to the implementation- dependent
706 * maximum supported number of texture image units."
708 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
711 * "Error Description Offending command
714 * INVALID_VALUE Numeric argument out of range Yes"
716 * Based on that, when an invalid sampler is specified, we generate a
717 * GL_INVALID_VALUE error and ignore the command.
719 if (uni
->type
->is_sampler()) {
720 for (int i
= 0; i
< count
; i
++) {
721 const unsigned texUnit
= ((unsigned *) values
)[i
];
723 /* check that the sampler (tex unit index) is legal */
724 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
725 _mesa_error(ctx
, GL_INVALID_VALUE
,
726 "glUniform1i(invalid sampler/tex unit index for "
734 if (uni
->type
->is_image()) {
735 for (int i
= 0; i
< count
; i
++) {
736 const int unit
= ((GLint
*) values
)[i
];
738 /* check that the image unit is legal */
739 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
740 _mesa_error(ctx
, GL_INVALID_VALUE
,
741 "glUniform1i(invalid image unit index for uniform %d)",
748 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
750 * "When loading N elements starting at an arbitrary position k in a
751 * uniform declared as an array, elements k through k + N - 1 in the
752 * array will be replaced with the new values. Values for any array
753 * element that exceeds the highest array element index used, as
754 * reported by GetActiveUniform, will be ignored by the GL."
756 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
757 * will have already generated an error.
759 if (uni
->array_elements
!= 0) {
760 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
763 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
765 /* Store the data in the "actual type" backing storage for the uniform.
767 if (!uni
->type
->is_boolean()) {
768 memcpy(&uni
->storage
[size_mul
* components
* offset
], values
,
769 sizeof(uni
->storage
[0]) * components
* count
* size_mul
);
771 const union gl_constant_value
*src
=
772 (const union gl_constant_value
*) values
;
773 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
774 const unsigned elems
= components
* count
;
776 for (unsigned i
= 0; i
< elems
; i
++) {
777 if (basicType
== GLSL_TYPE_FLOAT
) {
778 dst
[i
].i
= src
[i
].f
!= 0.0f
? ctx
->Const
.UniformBooleanTrue
: 0;
780 dst
[i
].i
= src
[i
].i
!= 0 ? ctx
->Const
.UniformBooleanTrue
: 0;
785 uni
->initialized
= true;
787 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
789 /* If the uniform is a sampler, do the extra magic necessary to propagate
790 * the changes through.
792 if (uni
->type
->is_sampler()) {
793 bool flushed
= false;
794 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
795 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
797 /* If the shader stage doesn't use the sampler uniform, skip this.
799 if (sh
== NULL
|| !uni
->sampler
[i
].active
)
802 for (int j
= 0; j
< count
; j
++) {
803 sh
->SamplerUnits
[uni
->sampler
[i
].index
+ offset
+ j
] =
804 ((unsigned *) values
)[j
];
807 struct gl_program
*const prog
= sh
->Program
;
809 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
811 /* Determine if any of the samplers used by this shader stage have
814 bool changed
= false;
815 for (unsigned j
= 0; j
< ARRAY_SIZE(prog
->SamplerUnits
); j
++) {
816 if ((sh
->active_samplers
& (1U << j
)) != 0
817 && (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
])) {
825 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
829 memcpy(prog
->SamplerUnits
,
831 sizeof(sh
->SamplerUnits
));
833 _mesa_update_shader_textures_used(shProg
, prog
);
834 if (ctx
->Driver
.SamplerUniformChange
)
835 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
840 /* If the uniform is an image, update the mapping from image
841 * uniforms to image units present in the shader data structure.
843 if (uni
->type
->is_image()) {
844 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
845 if (uni
->image
[i
].active
) {
846 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
848 for (int j
= 0; j
< count
; j
++)
849 sh
->ImageUnits
[uni
->image
[i
].index
+ offset
+ j
] =
850 ((GLint
*) values
)[j
];
854 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
859 * Called by glUniformMatrix*() functions.
860 * Note: cols=2, rows=4 ==> array[2] of vec4
863 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
864 GLuint cols
, GLuint rows
,
865 GLint location
, GLsizei count
,
867 const GLvoid
*values
, GLenum type
)
874 struct gl_uniform_storage
*const uni
=
875 validate_uniform_parameters(ctx
, shProg
, location
, count
,
876 &offset
, "glUniformMatrix");
880 if (!uni
->type
->is_matrix()) {
881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
882 "glUniformMatrix(non-matrix uniform)");
886 assert(type
== GL_FLOAT
|| type
== GL_DOUBLE
);
887 size_mul
= type
== GL_DOUBLE
? 2 : 1;
889 assert(!uni
->type
->is_sampler());
890 vectors
= uni
->type
->matrix_columns
;
891 components
= uni
->type
->vector_elements
;
893 /* Verify that the types are compatible. This is greatly simplified for
894 * matrices because they can only have a float base type.
896 if (vectors
!= cols
|| components
!= rows
) {
897 _mesa_error(ctx
, GL_INVALID_OPERATION
,
898 "glUniformMatrix(matrix size mismatch)");
902 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
903 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
906 if (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30) {
907 _mesa_error(ctx
, GL_INVALID_VALUE
,
908 "glUniformMatrix(matrix transpose is not GL_FALSE)");
913 if (unlikely(ctx
->_Shader
->Flags
& GLSL_UNIFORMS
)) {
914 log_uniform(values
, uni
->type
->base_type
, components
, vectors
, count
,
915 bool(transpose
), shProg
, location
, uni
);
918 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
920 * "When loading N elements starting at an arbitrary position k in a
921 * uniform declared as an array, elements k through k + N - 1 in the
922 * array will be replaced with the new values. Values for any array
923 * element that exceeds the highest array element index used, as
924 * reported by GetActiveUniform, will be ignored by the GL."
926 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
927 * will have already generated an error.
929 if (uni
->array_elements
!= 0) {
930 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
933 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
935 /* Store the data in the "actual type" backing storage for the uniform.
937 elements
= components
* vectors
;
940 memcpy(&uni
->storage
[elements
* offset
], values
,
941 sizeof(uni
->storage
[0]) * elements
* count
* size_mul
);
942 } else if (type
== GL_FLOAT
) {
943 /* Copy and transpose the matrix.
945 const float *src
= (const float *)values
;
946 float *dst
= &uni
->storage
[elements
* offset
].f
;
948 for (int i
= 0; i
< count
; i
++) {
949 for (unsigned r
= 0; r
< rows
; r
++) {
950 for (unsigned c
= 0; c
< cols
; c
++) {
951 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
959 assert(type
== GL_DOUBLE
);
960 const double *src
= (const double *)values
;
961 double *dst
= (double *)&uni
->storage
[elements
* offset
].f
;
963 for (int i
= 0; i
< count
; i
++) {
964 for (unsigned r
= 0; r
< rows
; r
++) {
965 for (unsigned c
= 0; c
< cols
; c
++) {
966 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
975 uni
->initialized
= true;
977 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
982 * Called via glGetUniformLocation().
984 * Returns the uniform index into UniformStorage (also the
985 * glGetActiveUniformsiv uniform index), and stores the referenced
986 * array offset in *offset, or GL_INVALID_INDEX (-1).
989 _mesa_get_uniform_location(struct gl_shader_program
*shProg
,
991 unsigned *out_offset
)
993 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
995 * "The first element of a uniform array is identified using the
996 * name of the uniform array appended with "[0]". Except if the last
997 * part of the string name indicates a uniform array, then the
998 * location of the first element of that array can be retrieved by
999 * either using the name of the uniform array, or the name of the
1000 * uniform array appended with "[0]"."
1002 * Note: since uniform names are not allowed to use whitespace, and array
1003 * indices within uniform names are not allowed to use "+", "-", or leading
1004 * zeros, it follows that each uniform has a unique name up to the possible
1005 * ambiguity with "[0]" noted above. Therefore we don't need to worry
1006 * about mal-formed inputs--they will properly fail when we try to look up
1007 * the uniform name in shProg->UniformHash.
1010 const GLchar
*base_name_end
;
1011 long offset
= parse_program_resource_name(name
, &base_name_end
);
1012 bool array_lookup
= offset
>= 0;
1016 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
1017 memcpy(name_copy
, name
, base_name_end
- name
);
1018 name_copy
[base_name_end
- name
] = '\0';
1020 name_copy
= (char *) name
;
1024 unsigned location
= 0;
1025 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
1028 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
1030 /* Free the temporary buffer *before* possibly returning an error.
1032 if (name_copy
!= name
)
1036 return GL_INVALID_INDEX
;
1038 /* If the uniform is built-in, fail. */
1039 if (shProg
->UniformStorage
[location
].builtin
)
1040 return GL_INVALID_INDEX
;
1042 /* If the uniform is an array, fail if the index is out of bounds.
1043 * (A negative index is caught above.) This also fails if the uniform
1044 * is not an array, but the user is trying to index it, because
1045 * array_elements is zero and offset >= 0.
1048 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
1049 return GL_INVALID_INDEX
;
1052 *out_offset
= offset
;
1057 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1058 char *errMsg
, size_t errMsgLength
)
1060 /* Shader does not have samplers. */
1061 if (shProg
->NumUniformStorage
== 0)
1064 if (!shProg
->SamplersValidated
) {
1065 _mesa_snprintf(errMsg
, errMsgLength
,
1066 "active samplers with a different type "
1067 "refer to the same texture image unit");
1074 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1076 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1077 * OpenGL 4.1 spec says:
1079 * "[INVALID_OPERATION] is generated by any command that transfers
1080 * vertices to the GL if:
1084 * - Any two active samplers in the current program object are of
1085 * different types, but refer to the same texture image unit.
1087 * - The number of active samplers in the program exceeds the
1088 * maximum number of texture image units allowed."
1090 unsigned active_samplers
= 0;
1091 const struct gl_shader_program
**shProg
=
1092 (const struct gl_shader_program
**) pipeline
->CurrentProgram
;
1094 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1095 memset(unit_types
, 0, sizeof(unit_types
));
1097 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1101 for (unsigned i
= 0; i
< shProg
[idx
]->NumUniformStorage
; i
++) {
1102 const struct gl_uniform_storage
*const storage
=
1103 &shProg
[idx
]->UniformStorage
[i
];
1105 if (!storage
->type
->is_sampler())
1110 const unsigned count
= MAX2(1, storage
->array_elements
);
1111 for (unsigned j
= 0; j
< count
; j
++) {
1112 const unsigned unit
= storage
->storage
[j
].i
;
1114 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1115 * great job of eliminating unused uniforms currently so for now
1116 * don't throw an error if two sampler types both point to 0.
1121 /* The types of the samplers associated with a particular texture
1122 * unit must be an exact match. Page 74 (page 89 of the PDF) of
1123 * the OpenGL 3.3 core spec says:
1125 * "It is not allowed to have variables of different sampler
1126 * types pointing to the same texture image unit within a
1129 if (unit_types
[unit
] == NULL
) {
1130 unit_types
[unit
] = storage
->type
;
1131 } else if (unit_types
[unit
] != storage
->type
) {
1133 ralloc_asprintf(pipeline
,
1134 "Texture unit %d is accessed both as %s "
1136 unit
, unit_types
[unit
]->name
,
1137 storage
->type
->name
);
1144 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1146 ralloc_asprintf(pipeline
,
1147 "the number of active samplers %d exceed the "
1149 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);