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 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
54 if (index
>= shProg
->NumUserUniformStorage
) {
55 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
59 const struct gl_uniform_storage
*const uni
= &shProg
->UniformStorage
[index
];
62 _mesa_get_uniform_name(uni
, maxLength
, length
, nameOut
);
66 /* array_elements is zero for non-arrays, but the API requires that 1 be
69 *size
= MAX2(1, uni
->array_elements
);
73 *type
= uni
->type
->gl_type
;
77 extern "C" void GLAPIENTRY
78 _mesa_GetActiveUniformsiv(GLuint program
,
80 const GLuint
*uniformIndices
,
84 GET_CURRENT_CONTEXT(ctx
);
85 struct gl_shader_program
*shProg
;
88 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
92 if (uniformCount
< 0) {
93 _mesa_error(ctx
, GL_INVALID_VALUE
,
94 "glGetUniformIndices(uniformCount < 0)");
98 for (i
= 0; i
< uniformCount
; i
++) {
99 GLuint index
= uniformIndices
[i
];
101 if (index
>= shProg
->NumUserUniformStorage
) {
102 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniformsiv(index)");
107 for (i
= 0; i
< uniformCount
; i
++) {
108 GLuint index
= uniformIndices
[i
];
109 const struct gl_uniform_storage
*uni
= &shProg
->UniformStorage
[index
];
112 case GL_UNIFORM_TYPE
:
113 params
[i
] = uni
->type
->gl_type
;
116 case GL_UNIFORM_SIZE
:
117 /* array_elements is zero for non-arrays, but the API requires that 1 be
120 params
[i
] = MAX2(1, uni
->array_elements
);
123 case GL_UNIFORM_NAME_LENGTH
:
124 params
[i
] = strlen(uni
->name
) + 1;
126 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
129 * "If the active uniform is an array, the uniform name returned
130 * in name will always be the name of the uniform array appended
133 if (uni
->array_elements
!= 0)
137 case GL_UNIFORM_BLOCK_INDEX
:
138 params
[i
] = uni
->block_index
;
141 case GL_UNIFORM_OFFSET
:
142 params
[i
] = uni
->offset
;
145 case GL_UNIFORM_ARRAY_STRIDE
:
146 params
[i
] = uni
->array_stride
;
149 case GL_UNIFORM_MATRIX_STRIDE
:
150 params
[i
] = uni
->matrix_stride
;
153 case GL_UNIFORM_IS_ROW_MAJOR
:
154 params
[i
] = uni
->row_major
;
157 case GL_UNIFORM_ATOMIC_COUNTER_BUFFER_INDEX
:
158 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
160 params
[i
] = uni
->atomic_buffer_index
;
171 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetActiveUniformsiv(pname)");
175 validate_uniform_parameters(struct gl_context
*ctx
,
176 struct gl_shader_program
*shProg
,
177 GLint location
, GLsizei count
,
179 unsigned *array_index
,
181 bool negative_one_is_not_valid
)
183 if (!shProg
|| !shProg
->LinkStatus
) {
184 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
188 if (location
== -1) {
189 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
192 * "The error INVALID_OPERATION is generated if program has not been
193 * linked successfully, or if location is not a valid location for
196 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
199 * "If the value of location is -1, the Uniform* commands will
200 * silently ignore the data passed in, and the current uniform
201 * values will not be changed."
203 * Allowing -1 for the location parameter of glUniform allows
204 * applications to avoid error paths in the case that, for example, some
205 * uniform variable is removed by the compiler / linker after
206 * optimization. In this case, the new value of the uniform is dropped
207 * on the floor. For the case of glGetUniform, there is nothing
208 * sensible to do for a location of -1.
210 * The negative_one_is_not_valid flag selects between the two behaviors.
212 if (negative_one_is_not_valid
) {
213 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
220 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
222 * "If a negative number is provided where an argument of type sizei or
223 * sizeiptr is specified, the error INVALID_VALUE is generated."
226 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
230 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
232 * "If any of the following conditions occur, an INVALID_OPERATION
233 * error is generated by the Uniform* commands, and no uniform values
238 * - if no variable with a location of location exists in the
239 * program object currently in use and location is not -1,
240 * - if count is greater than one, and the uniform declared in the
241 * shader is not an array variable,
244 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
249 /* Check that the given location is in bounds of uniform remap table. */
250 if (location
>= (GLint
) shProg
->NumUniformRemapTable
) {
251 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
256 _mesa_uniform_split_location_offset(shProg
, location
, loc
, array_index
);
258 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
259 _mesa_error(ctx
, GL_INVALID_OPERATION
,
260 "%s(count > 1 for non-array, location=%d)",
265 /* If the uniform is an array, check that array_index is in bounds.
266 * If not an array, check that array_index is zero.
267 * array_index is unsigned so no need to check for less than zero.
269 unsigned limit
= shProg
->UniformStorage
[*loc
].array_elements
;
272 if (*array_index
>= limit
) {
273 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
281 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
284 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
285 GLsizei bufSize
, enum glsl_base_type returnType
,
288 struct gl_shader_program
*shProg
=
289 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
290 struct gl_uniform_storage
*uni
;
291 unsigned loc
, offset
;
293 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
294 &loc
, &offset
, "glGetUniform", true))
297 uni
= &shProg
->UniformStorage
[loc
];
300 unsigned elements
= (uni
->type
->is_sampler())
301 ? 1 : uni
->type
->components();
303 /* Calculate the source base address *BEFORE* modifying elements to
304 * account for the size of the user's buffer.
306 const union gl_constant_value
*const src
=
307 &uni
->storage
[offset
* elements
];
309 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
310 returnType
== GLSL_TYPE_UINT
);
311 /* The three (currently) supported types all have the same size,
312 * which is of course the same as their union. That'll change
313 * with glGetUniformdv()...
315 unsigned bytes
= sizeof(src
[0]) * elements
;
316 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
317 _mesa_error( ctx
, GL_INVALID_OPERATION
,
318 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
319 " but %u bytes are required)", bufSize
, bytes
);
323 /* If the return type and the uniform's native type are "compatible,"
324 * just memcpy the data. If the types are not compatible, perform a
325 * slower convert-and-copy process.
327 if (returnType
== uni
->type
->base_type
328 || ((returnType
== GLSL_TYPE_INT
329 || returnType
== GLSL_TYPE_UINT
330 || returnType
== GLSL_TYPE_SAMPLER
)
332 (uni
->type
->base_type
== GLSL_TYPE_INT
333 || uni
->type
->base_type
== GLSL_TYPE_UINT
334 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
335 memcpy(paramsOut
, src
, bytes
);
337 union gl_constant_value
*const dst
=
338 (union gl_constant_value
*) paramsOut
;
340 /* This code could be optimized by putting the loop inside the switch
341 * statements. However, this is not expected to be
342 * performance-critical code.
344 for (unsigned i
= 0; i
< elements
; i
++) {
345 switch (returnType
) {
346 case GLSL_TYPE_FLOAT
:
347 switch (uni
->type
->base_type
) {
349 dst
[i
].f
= (float) src
[i
].u
;
352 case GLSL_TYPE_SAMPLER
:
353 dst
[i
].f
= (float) src
[i
].i
;
356 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
359 assert(!"Should not get here.");
366 switch (uni
->type
->base_type
) {
367 case GLSL_TYPE_FLOAT
:
368 /* While the GL 3.2 core spec doesn't explicitly
369 * state how conversion of float uniforms to integer
370 * values works, in section 6.2 "State Tables" on
373 * "Unless otherwise specified, when floating
374 * point state is returned as integer values or
375 * integer state is returned as floating-point
376 * values it is converted in the fashion
377 * described in section 6.1.2"
379 * That section, on page 248, says:
381 * "If GetIntegerv or GetInteger64v are called,
382 * a floating-point value is rounded to the
383 * nearest integer..."
385 dst
[i
].i
= IROUND(src
[i
].f
);
388 dst
[i
].i
= src
[i
].i
? 1 : 0;
391 assert(!"Should not get here.");
397 assert(!"Should not get here.");
406 log_uniform(const void *values
, enum glsl_base_type basicType
,
407 unsigned rows
, unsigned cols
, unsigned count
,
409 const struct gl_shader_program
*shProg
,
411 const struct gl_uniform_storage
*uni
)
414 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
415 const unsigned elems
= rows
* cols
* count
;
416 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
418 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
419 "transpose = %s) to: ",
420 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
421 transpose
? "true" : "false");
422 for (unsigned i
= 0; i
< elems
; i
++) {
423 if (i
!= 0 && ((i
% rows
) == 0))
428 printf("%u ", v
[i
].u
);
431 printf("%d ", v
[i
].i
);
433 case GLSL_TYPE_FLOAT
:
434 printf("%g ", v
[i
].f
);
437 assert(!"Should not get here.");
447 log_program_parameters(const struct gl_shader_program
*shProg
)
449 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
450 if (shProg
->_LinkedShaders
[i
] == NULL
)
453 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
455 printf("Program %d %s shader parameters:\n",
456 shProg
->Name
, _mesa_shader_stage_to_string(i
));
457 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
458 printf("%s: %p %f %f %f %f\n",
459 prog
->Parameters
->Parameters
[j
].Name
,
460 prog
->Parameters
->ParameterValues
[j
],
461 prog
->Parameters
->ParameterValues
[j
][0].f
,
462 prog
->Parameters
->ParameterValues
[j
][1].f
,
463 prog
->Parameters
->ParameterValues
[j
][2].f
,
464 prog
->Parameters
->ParameterValues
[j
][3].f
);
472 * Propagate some values from uniform backing storage to driver storage
474 * Values propagated from uniform backing storage to driver storage
475 * have all format / type conversions previously requested by the
476 * driver applied. This function is most often called by the
477 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
480 * \param uni Uniform whose data is to be propagated to driver storage
481 * \param array_index If \c uni is an array, this is the element of
482 * the array to be propagated.
483 * \param count Number of array elements to propagate.
486 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
487 unsigned array_index
,
492 /* vector_elements and matrix_columns can be 0 for samplers.
494 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
495 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
497 /* Store the data in the driver's requested type in the driver's storage
500 unsigned src_vector_byte_stride
= components
* 4;
502 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
503 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
504 uint8_t *dst
= (uint8_t *) store
->data
;
505 const unsigned extra_stride
=
506 store
->element_stride
- (vectors
* store
->vector_stride
);
508 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
511 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
513 __func__
, dst
, array_index
, components
,
514 vectors
, count
, store
->vector_stride
, extra_stride
);
517 dst
+= array_index
* store
->element_stride
;
519 switch (store
->format
) {
521 case uniform_bool_int_0_1
: {
525 for (j
= 0; j
< count
; j
++) {
526 for (v
= 0; v
< vectors
; v
++) {
527 memcpy(dst
, src
, src_vector_byte_stride
);
528 src
+= src_vector_byte_stride
;
529 dst
+= store
->vector_stride
;
537 case uniform_int_float
:
538 case uniform_bool_float
: {
539 const int *isrc
= (const int *) src
;
544 for (j
= 0; j
< count
; j
++) {
545 for (v
= 0; v
< vectors
; v
++) {
546 for (c
= 0; c
< components
; c
++) {
547 ((float *) dst
)[c
] = (float) *isrc
;
551 dst
+= store
->vector_stride
;
559 case uniform_bool_int_0_not0
: {
560 const int *isrc
= (const int *) src
;
565 for (j
= 0; j
< count
; j
++) {
566 for (v
= 0; v
< vectors
; v
++) {
567 for (c
= 0; c
< components
; c
++) {
568 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
572 dst
+= store
->vector_stride
;
581 assert(!"Should not get here.");
588 * Called via glUniform*() functions.
591 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
592 GLint location
, GLsizei count
,
593 const GLvoid
*values
, GLenum type
)
595 unsigned loc
, offset
;
597 unsigned src_components
;
598 enum glsl_base_type basicType
;
599 struct gl_uniform_storage
*uni
;
601 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
602 &loc
, &offset
, "glUniform", false))
605 uni
= &shProg
->UniformStorage
[loc
];
607 /* Verify that the types are compatible.
611 basicType
= GLSL_TYPE_FLOAT
;
615 basicType
= GLSL_TYPE_FLOAT
;
619 basicType
= GLSL_TYPE_FLOAT
;
623 basicType
= GLSL_TYPE_FLOAT
;
626 case GL_UNSIGNED_INT
:
627 basicType
= GLSL_TYPE_UINT
;
630 case GL_UNSIGNED_INT_VEC2
:
631 basicType
= GLSL_TYPE_UINT
;
634 case GL_UNSIGNED_INT_VEC3
:
635 basicType
= GLSL_TYPE_UINT
;
638 case GL_UNSIGNED_INT_VEC4
:
639 basicType
= GLSL_TYPE_UINT
;
643 basicType
= GLSL_TYPE_INT
;
647 basicType
= GLSL_TYPE_INT
;
651 basicType
= GLSL_TYPE_INT
;
655 basicType
= GLSL_TYPE_INT
;
663 case GL_FLOAT_MAT2x3
:
664 case GL_FLOAT_MAT2x4
:
665 case GL_FLOAT_MAT3x2
:
667 case GL_FLOAT_MAT3x4
:
668 case GL_FLOAT_MAT4x2
:
669 case GL_FLOAT_MAT4x3
:
672 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
676 if (uni
->type
->is_sampler()) {
679 components
= uni
->type
->vector_elements
;
683 switch (uni
->type
->base_type
) {
687 case GLSL_TYPE_SAMPLER
:
688 case GLSL_TYPE_IMAGE
:
689 match
= (basicType
== GLSL_TYPE_INT
);
692 match
= (basicType
== uni
->type
->base_type
);
696 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
697 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
701 if (ctx
->_Shader
->Flags
& GLSL_UNIFORMS
) {
702 log_uniform(values
, basicType
, components
, 1, count
,
703 false, shProg
, location
, uni
);
706 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
708 * "Setting a sampler's value to i selects texture image unit number
709 * i. The values of i range from zero to the implementation- dependent
710 * maximum supported number of texture image units."
712 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
715 * "Error Description Offending command
718 * INVALID_VALUE Numeric argument out of range Yes"
720 * Based on that, when an invalid sampler is specified, we generate a
721 * GL_INVALID_VALUE error and ignore the command.
723 if (uni
->type
->is_sampler()) {
726 for (i
= 0; i
< count
; i
++) {
727 const unsigned texUnit
= ((unsigned *) values
)[i
];
729 /* check that the sampler (tex unit index) is legal */
730 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
731 _mesa_error(ctx
, GL_INVALID_VALUE
,
732 "glUniform1i(invalid sampler/tex unit index for "
740 if (uni
->type
->is_image()) {
743 for (i
= 0; i
< count
; i
++) {
744 const int unit
= ((GLint
*) values
)[i
];
746 /* check that the image unit is legal */
747 if (unit
< 0 || unit
>= (int)ctx
->Const
.MaxImageUnits
) {
748 _mesa_error(ctx
, GL_INVALID_VALUE
,
749 "glUniform1i(invalid image unit index for uniform %d)",
756 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
758 * "When loading N elements starting at an arbitrary position k in a
759 * uniform declared as an array, elements k through k + N - 1 in the
760 * array will be replaced with the new values. Values for any array
761 * element that exceeds the highest array element index used, as
762 * reported by GetActiveUniform, will be ignored by the GL."
764 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
765 * will have already generated an error.
767 if (uni
->array_elements
!= 0) {
768 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
771 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
773 /* Store the data in the "actual type" backing storage for the uniform.
775 if (!uni
->type
->is_boolean()) {
776 memcpy(&uni
->storage
[components
* offset
], values
,
777 sizeof(uni
->storage
[0]) * components
* count
);
779 const union gl_constant_value
*src
=
780 (const union gl_constant_value
*) values
;
781 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
782 const unsigned elems
= components
* count
;
785 for (i
= 0; i
< elems
; i
++) {
786 if (basicType
== GLSL_TYPE_FLOAT
) {
787 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
789 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
794 uni
->initialized
= true;
796 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
798 /* If the uniform is a sampler, do the extra magic necessary to propagate
799 * the changes through.
801 if (uni
->type
->is_sampler()) {
804 bool flushed
= false;
805 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
806 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
809 /* If the shader stage doesn't use the sampler uniform, skip this.
811 if (sh
== NULL
|| !uni
->sampler
[i
].active
)
814 for (j
= 0; j
< count
; j
++) {
815 sh
->SamplerUnits
[uni
->sampler
[i
].index
+ offset
+ j
] =
816 ((unsigned *) values
)[j
];
819 struct gl_program
*const prog
= sh
->Program
;
821 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
823 /* Determine if any of the samplers used by this shader stage have
826 bool changed
= false;
827 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
828 if ((sh
->active_samplers
& (1U << j
)) != 0
829 && (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
])) {
837 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
841 memcpy(prog
->SamplerUnits
,
843 sizeof(sh
->SamplerUnits
));
845 _mesa_update_shader_textures_used(shProg
, prog
);
846 if (ctx
->Driver
.SamplerUniformChange
)
847 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
852 /* If the uniform is an image, update the mapping from image
853 * uniforms to image units present in the shader data structure.
855 if (uni
->type
->is_image()) {
858 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
859 if (uni
->image
[i
].active
) {
860 struct gl_shader
*sh
= shProg
->_LinkedShaders
[i
];
862 for (j
= 0; j
< count
; j
++)
863 sh
->ImageUnits
[uni
->image
[i
].index
+ offset
+ j
] =
864 ((GLint
*) values
)[j
];
868 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
873 * Called by glUniformMatrix*() functions.
874 * Note: cols=2, rows=4 ==> array[2] of vec4
877 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
878 GLuint cols
, GLuint rows
,
879 GLint location
, GLsizei count
,
880 GLboolean transpose
, const GLfloat
*values
)
882 unsigned loc
, offset
;
886 struct gl_uniform_storage
*uni
;
888 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
889 &loc
, &offset
, "glUniformMatrix", false))
892 uni
= &shProg
->UniformStorage
[loc
];
893 if (!uni
->type
->is_matrix()) {
894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
895 "glUniformMatrix(non-matrix uniform)");
899 assert(!uni
->type
->is_sampler());
900 vectors
= uni
->type
->matrix_columns
;
901 components
= uni
->type
->vector_elements
;
903 /* Verify that the types are compatible. This is greatly simplified for
904 * matrices because they can only have a float base type.
906 if (vectors
!= cols
|| components
!= rows
) {
907 _mesa_error(ctx
, GL_INVALID_OPERATION
,
908 "glUniformMatrix(matrix size mismatch)");
912 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
913 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
914 if (ctx
->API
== API_OPENGLES
915 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)) {
917 _mesa_error(ctx
, GL_INVALID_VALUE
,
918 "glUniformMatrix(matrix transpose is not GL_FALSE)");
923 if (ctx
->_Shader
->Flags
& GLSL_UNIFORMS
) {
924 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
925 bool(transpose
), shProg
, location
, uni
);
928 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
930 * "When loading N elements starting at an arbitrary position k in a
931 * uniform declared as an array, elements k through k + N - 1 in the
932 * array will be replaced with the new values. Values for any array
933 * element that exceeds the highest array element index used, as
934 * reported by GetActiveUniform, will be ignored by the GL."
936 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
937 * will have already generated an error.
939 if (uni
->array_elements
!= 0) {
940 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
943 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
945 /* Store the data in the "actual type" backing storage for the uniform.
947 elements
= components
* vectors
;
950 memcpy(&uni
->storage
[elements
* offset
], values
,
951 sizeof(uni
->storage
[0]) * elements
* count
);
953 /* Copy and transpose the matrix.
955 const float *src
= values
;
956 float *dst
= &uni
->storage
[elements
* offset
].f
;
958 for (int i
= 0; i
< count
; i
++) {
959 for (unsigned r
= 0; r
< rows
; r
++) {
960 for (unsigned c
= 0; c
< cols
; c
++) {
961 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
970 uni
->initialized
= true;
972 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
977 * Called via glGetUniformLocation().
979 * Returns the uniform index into UniformStorage (also the
980 * glGetActiveUniformsiv uniform index), and stores the referenced
981 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
982 * return values can be encoded into a uniform location for
983 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
986 _mesa_get_uniform_location(struct gl_context
*ctx
,
987 struct gl_shader_program
*shProg
,
989 unsigned *out_offset
)
991 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
993 * "The first element of a uniform array is identified using the
994 * name of the uniform array appended with "[0]". Except if the last
995 * part of the string name indicates a uniform array, then the
996 * location of the first element of that array can be retrieved by
997 * either using the name of the uniform array, or the name of the
998 * uniform array appended with "[0]"."
1000 * Note: since uniform names are not allowed to use whitespace, and array
1001 * indices within uniform names are not allowed to use "+", "-", or leading
1002 * zeros, it follows that each uniform has a unique name up to the possible
1003 * ambiguity with "[0]" noted above. Therefore we don't need to worry
1004 * about mal-formed inputs--they will properly fail when we try to look up
1005 * the uniform name in shProg->UniformHash.
1008 const GLchar
*base_name_end
;
1009 long offset
= parse_program_resource_name(name
, &base_name_end
);
1010 bool array_lookup
= offset
>= 0;
1014 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
1015 memcpy(name_copy
, name
, base_name_end
- name
);
1016 name_copy
[base_name_end
- name
] = '\0';
1018 name_copy
= (char *) name
;
1022 unsigned location
= 0;
1023 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
1026 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
1028 /* Free the temporary buffer *before* possibly returning an error.
1030 if (name_copy
!= name
)
1034 return GL_INVALID_INDEX
;
1036 /* If the uniform is an array, fail if the index is out of bounds.
1037 * (A negative index is caught above.) This also fails if the uniform
1038 * is not an array, but the user is trying to index it, because
1039 * array_elements is zero and offset >= 0.
1042 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
1043 return GL_INVALID_INDEX
;
1046 *out_offset
= offset
;
1051 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1052 char *errMsg
, size_t errMsgLength
)
1054 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1056 memset(unit_types
, 0, sizeof(unit_types
));
1058 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1059 const struct gl_uniform_storage
*const storage
=
1060 &shProg
->UniformStorage
[i
];
1061 const glsl_type
*const t
= (storage
->type
->is_array())
1062 ? storage
->type
->fields
.array
: storage
->type
;
1064 if (!t
->is_sampler())
1067 const unsigned count
= MAX2(1, storage
->type
->array_size());
1068 for (unsigned j
= 0; j
< count
; j
++) {
1069 const unsigned unit
= storage
->storage
[j
].i
;
1071 /* The types of the samplers associated with a particular texture
1072 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1073 * OpenGL 3.3 core spec says:
1075 * "It is not allowed to have variables of different sampler
1076 * types pointing to the same texture image unit within a program
1079 if (unit_types
[unit
] == NULL
) {
1080 unit_types
[unit
] = t
;
1081 } else if (unit_types
[unit
] != t
) {
1082 _mesa_snprintf(errMsg
, errMsgLength
,
1083 "Texture unit %d is accessed both as %s and %s",
1084 unit
, unit_types
[unit
]->name
, t
->name
);
1094 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object
*pipeline
)
1096 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1097 * OpenGL 4.1 spec says:
1099 * "[INVALID_OPERATION] is generated by any command that transfers
1100 * vertices to the GL if:
1104 * - Any two active samplers in the current program object are of
1105 * different types, but refer to the same texture image unit.
1107 * - The number of active samplers in the program exceeds the
1108 * maximum number of texture image units allowed."
1110 unsigned active_samplers
= 0;
1111 const struct gl_shader_program
**shProg
=
1112 (const struct gl_shader_program
**) pipeline
->CurrentProgram
;
1114 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1115 memset(unit_types
, 0, sizeof(unit_types
));
1117 for (unsigned idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1121 for (unsigned i
= 0; i
< shProg
[idx
]->NumUserUniformStorage
; i
++) {
1122 const struct gl_uniform_storage
*const storage
=
1123 &shProg
[idx
]->UniformStorage
[i
];
1124 const glsl_type
*const t
= (storage
->type
->is_array())
1125 ? storage
->type
->fields
.array
: storage
->type
;
1127 if (!t
->is_sampler())
1132 const unsigned count
= MAX2(1, storage
->type
->array_size());
1133 for (unsigned j
= 0; j
< count
; j
++) {
1134 const unsigned unit
= storage
->storage
[j
].i
;
1136 /* The types of the samplers associated with a particular texture
1137 * unit must be an exact match. Page 74 (page 89 of the PDF) of
1138 * the OpenGL 3.3 core spec says:
1140 * "It is not allowed to have variables of different sampler
1141 * types pointing to the same texture image unit within a
1144 if (unit_types
[unit
] == NULL
) {
1145 unit_types
[unit
] = t
;
1146 } else if (unit_types
[unit
] != t
) {
1148 ralloc_asprintf(pipeline
,
1149 "Texture unit %d is accessed both as %s "
1151 unit
, unit_types
[unit
]->name
, t
->name
);
1158 if (active_samplers
> MAX_COMBINED_TEXTURE_IMAGE_UNITS
) {
1160 ralloc_asprintf(pipeline
,
1161 "the number of active samplers %d exceed the "
1163 active_samplers
, MAX_COMBINED_TEXTURE_IMAGE_UNITS
);