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(GLhandleARB 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 _mesa_uniform_split_location_offset(shProg
, location
, loc
, array_index
);
251 if (*loc
>= shProg
->NumUserUniformStorage
) {
252 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
257 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
258 _mesa_error(ctx
, GL_INVALID_OPERATION
,
259 "%s(count > 1 for non-array, location=%d)",
264 /* If the uniform is an array, check that array_index is in bounds.
265 * If not an array, check that array_index is zero.
266 * array_index is unsigned so no need to check for less than zero.
268 unsigned limit
= shProg
->UniformStorage
[*loc
].array_elements
;
271 if (*array_index
>= limit
) {
272 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
280 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
283 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
284 GLsizei bufSize
, enum glsl_base_type returnType
,
287 struct gl_shader_program
*shProg
=
288 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
289 struct gl_uniform_storage
*uni
;
290 unsigned loc
, offset
;
292 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
293 &loc
, &offset
, "glGetUniform", true))
296 uni
= &shProg
->UniformStorage
[loc
];
299 unsigned elements
= (uni
->type
->is_sampler())
300 ? 1 : uni
->type
->components();
302 /* Calculate the source base address *BEFORE* modifying elements to
303 * account for the size of the user's buffer.
305 const union gl_constant_value
*const src
=
306 &uni
->storage
[offset
* elements
];
308 assert(returnType
== GLSL_TYPE_FLOAT
|| returnType
== GLSL_TYPE_INT
||
309 returnType
== GLSL_TYPE_UINT
);
310 /* The three (currently) supported types all have the same size,
311 * which is of course the same as their union. That'll change
312 * with glGetUniformdv()...
314 unsigned bytes
= sizeof(src
[0]) * elements
;
315 if (bufSize
< 0 || bytes
> (unsigned) bufSize
) {
316 _mesa_error( ctx
, GL_INVALID_OPERATION
,
317 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
318 " but %u bytes are required)", bufSize
, bytes
);
322 /* If the return type and the uniform's native type are "compatible,"
323 * just memcpy the data. If the types are not compatible, perform a
324 * slower convert-and-copy process.
326 if (returnType
== uni
->type
->base_type
327 || ((returnType
== GLSL_TYPE_INT
328 || returnType
== GLSL_TYPE_UINT
329 || returnType
== GLSL_TYPE_SAMPLER
)
331 (uni
->type
->base_type
== GLSL_TYPE_INT
332 || uni
->type
->base_type
== GLSL_TYPE_UINT
333 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
334 memcpy(paramsOut
, src
, bytes
);
336 union gl_constant_value
*const dst
=
337 (union gl_constant_value
*) paramsOut
;
339 /* This code could be optimized by putting the loop inside the switch
340 * statements. However, this is not expected to be
341 * performance-critical code.
343 for (unsigned i
= 0; i
< elements
; i
++) {
344 switch (returnType
) {
345 case GLSL_TYPE_FLOAT
:
346 switch (uni
->type
->base_type
) {
348 dst
[i
].f
= (float) src
[i
].u
;
351 case GLSL_TYPE_SAMPLER
:
352 dst
[i
].f
= (float) src
[i
].i
;
355 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
358 assert(!"Should not get here.");
365 switch (uni
->type
->base_type
) {
366 case GLSL_TYPE_FLOAT
:
367 /* While the GL 3.2 core spec doesn't explicitly
368 * state how conversion of float uniforms to integer
369 * values works, in section 6.2 "State Tables" on
372 * "Unless otherwise specified, when floating
373 * point state is returned as integer values or
374 * integer state is returned as floating-point
375 * values it is converted in the fashion
376 * described in section 6.1.2"
378 * That section, on page 248, says:
380 * "If GetIntegerv or GetInteger64v are called,
381 * a floating-point value is rounded to the
382 * nearest integer..."
384 dst
[i
].i
= IROUND(src
[i
].f
);
387 dst
[i
].i
= src
[i
].i
? 1 : 0;
390 assert(!"Should not get here.");
396 assert(!"Should not get here.");
405 log_uniform(const void *values
, enum glsl_base_type basicType
,
406 unsigned rows
, unsigned cols
, unsigned count
,
408 const struct gl_shader_program
*shProg
,
410 const struct gl_uniform_storage
*uni
)
413 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
414 const unsigned elems
= rows
* cols
* count
;
415 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
417 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
418 "transpose = %s) to: ",
419 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
420 transpose
? "true" : "false");
421 for (unsigned i
= 0; i
< elems
; i
++) {
422 if (i
!= 0 && ((i
% rows
) == 0))
427 printf("%u ", v
[i
].u
);
430 printf("%d ", v
[i
].i
);
432 case GLSL_TYPE_FLOAT
:
433 printf("%g ", v
[i
].f
);
436 assert(!"Should not get here.");
446 log_program_parameters(const struct gl_shader_program
*shProg
)
448 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
449 if (shProg
->_LinkedShaders
[i
] == NULL
)
452 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
454 printf("Program %d %s shader parameters:\n",
455 shProg
->Name
, _mesa_progshader_enum_to_string(prog
->Target
));
456 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
457 printf("%s: %p %f %f %f %f\n",
458 prog
->Parameters
->Parameters
[j
].Name
,
459 prog
->Parameters
->ParameterValues
[j
],
460 prog
->Parameters
->ParameterValues
[j
][0].f
,
461 prog
->Parameters
->ParameterValues
[j
][1].f
,
462 prog
->Parameters
->ParameterValues
[j
][2].f
,
463 prog
->Parameters
->ParameterValues
[j
][3].f
);
471 * Propagate some values from uniform backing storage to driver storage
473 * Values propagated from uniform backing storage to driver storage
474 * have all format / type conversions previously requested by the
475 * driver applied. This function is most often called by the
476 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
479 * \param uni Uniform whose data is to be propagated to driver storage
480 * \param array_index If \c uni is an array, this is the element of
481 * the array to be propagated.
482 * \param count Number of array elements to propagate.
485 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
486 unsigned array_index
,
491 /* vector_elements and matrix_columns can be 0 for samplers.
493 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
494 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
496 /* Store the data in the driver's requested type in the driver's storage
499 unsigned src_vector_byte_stride
= components
* 4;
501 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
502 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
503 uint8_t *dst
= (uint8_t *) store
->data
;
504 const unsigned extra_stride
=
505 store
->element_stride
- (vectors
* store
->vector_stride
);
507 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
510 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
512 __func__
, dst
, array_index
, components
,
513 vectors
, count
, store
->vector_stride
, extra_stride
);
516 dst
+= array_index
* store
->element_stride
;
518 switch (store
->format
) {
520 case uniform_bool_int_0_1
: {
524 for (j
= 0; j
< count
; j
++) {
525 for (v
= 0; v
< vectors
; v
++) {
526 memcpy(dst
, src
, src_vector_byte_stride
);
527 src
+= src_vector_byte_stride
;
528 dst
+= store
->vector_stride
;
536 case uniform_int_float
:
537 case uniform_bool_float
: {
538 const int *isrc
= (const int *) src
;
543 for (j
= 0; j
< count
; j
++) {
544 for (v
= 0; v
< vectors
; v
++) {
545 for (c
= 0; c
< components
; c
++) {
546 ((float *) dst
)[c
] = (float) *isrc
;
550 dst
+= store
->vector_stride
;
558 case uniform_bool_int_0_not0
: {
559 const int *isrc
= (const int *) src
;
564 for (j
= 0; j
< count
; j
++) {
565 for (v
= 0; v
< vectors
; v
++) {
566 for (c
= 0; c
< components
; c
++) {
567 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
571 dst
+= store
->vector_stride
;
580 assert(!"Should not get here.");
587 * Called via glUniform*() functions.
590 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
591 GLint location
, GLsizei count
,
592 const GLvoid
*values
, GLenum type
)
594 unsigned loc
, offset
;
596 unsigned src_components
;
597 enum glsl_base_type basicType
;
598 struct gl_uniform_storage
*uni
;
600 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
601 &loc
, &offset
, "glUniform", false))
604 uni
= &shProg
->UniformStorage
[loc
];
606 /* Verify that the types are compatible.
610 basicType
= GLSL_TYPE_FLOAT
;
614 basicType
= GLSL_TYPE_FLOAT
;
618 basicType
= GLSL_TYPE_FLOAT
;
622 basicType
= GLSL_TYPE_FLOAT
;
625 case GL_UNSIGNED_INT
:
626 basicType
= GLSL_TYPE_UINT
;
629 case GL_UNSIGNED_INT_VEC2
:
630 basicType
= GLSL_TYPE_UINT
;
633 case GL_UNSIGNED_INT_VEC3
:
634 basicType
= GLSL_TYPE_UINT
;
637 case GL_UNSIGNED_INT_VEC4
:
638 basicType
= GLSL_TYPE_UINT
;
642 basicType
= GLSL_TYPE_INT
;
646 basicType
= GLSL_TYPE_INT
;
650 basicType
= GLSL_TYPE_INT
;
654 basicType
= GLSL_TYPE_INT
;
662 case GL_FLOAT_MAT2x3
:
663 case GL_FLOAT_MAT2x4
:
664 case GL_FLOAT_MAT3x2
:
666 case GL_FLOAT_MAT3x4
:
667 case GL_FLOAT_MAT4x2
:
668 case GL_FLOAT_MAT4x3
:
671 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
675 if (uni
->type
->is_sampler()) {
678 components
= uni
->type
->vector_elements
;
682 switch (uni
->type
->base_type
) {
686 case GLSL_TYPE_SAMPLER
:
687 match
= (basicType
== GLSL_TYPE_INT
);
690 match
= (basicType
== uni
->type
->base_type
);
694 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
695 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
699 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
700 log_uniform(values
, basicType
, components
, 1, count
,
701 false, shProg
, location
, uni
);
704 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
706 * "Setting a sampler's value to i selects texture image unit number
707 * i. The values of i range from zero to the implementation- dependent
708 * maximum supported number of texture image units."
710 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
713 * "Error Description Offending command
716 * INVALID_VALUE Numeric argument out of range Yes"
718 * Based on that, when an invalid sampler is specified, we generate a
719 * GL_INVALID_VALUE error and ignore the command.
721 if (uni
->type
->is_sampler()) {
724 for (i
= 0; i
< count
; i
++) {
725 const unsigned texUnit
= ((unsigned *) values
)[i
];
727 /* check that the sampler (tex unit index) is legal */
728 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
729 _mesa_error(ctx
, GL_INVALID_VALUE
,
730 "glUniform1i(invalid sampler/tex unit index for "
738 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
740 * "When loading N elements starting at an arbitrary position k in a
741 * uniform declared as an array, elements k through k + N - 1 in the
742 * array will be replaced with the new values. Values for any array
743 * element that exceeds the highest array element index used, as
744 * reported by GetActiveUniform, will be ignored by the GL."
746 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
747 * will have already generated an error.
749 if (uni
->array_elements
!= 0) {
750 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
753 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
755 /* Store the data in the "actual type" backing storage for the uniform.
757 if (!uni
->type
->is_boolean()) {
758 memcpy(&uni
->storage
[components
* offset
], values
,
759 sizeof(uni
->storage
[0]) * components
* count
);
761 const union gl_constant_value
*src
=
762 (const union gl_constant_value
*) values
;
763 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
764 const unsigned elems
= components
* count
;
767 for (i
= 0; i
< elems
; i
++) {
768 if (basicType
== GLSL_TYPE_FLOAT
) {
769 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
771 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
776 uni
->initialized
= true;
778 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
780 /* If the uniform is a sampler, do the extra magic necessary to propagate
781 * the changes through.
783 if (uni
->type
->is_sampler()) {
786 bool flushed
= false;
787 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
788 struct gl_shader
*const sh
= shProg
->_LinkedShaders
[i
];
791 /* If the shader stage doesn't use the sampler uniform, skip this.
793 if (sh
== NULL
|| !uni
->sampler
[i
].active
)
796 for (j
= 0; j
< count
; j
++) {
797 sh
->SamplerUnits
[uni
->sampler
[i
].index
+ offset
+ j
] =
798 ((unsigned *) values
)[j
];
801 struct gl_program
*const prog
= sh
->Program
;
803 assert(sizeof(prog
->SamplerUnits
) == sizeof(sh
->SamplerUnits
));
805 /* Determine if any of the samplers used by this shader stage have
808 bool changed
= false;
809 for (unsigned j
= 0; j
< Elements(prog
->SamplerUnits
); j
++) {
810 if ((sh
->active_samplers
& (1U << j
)) != 0
811 && (prog
->SamplerUnits
[j
] != sh
->SamplerUnits
[j
])) {
819 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
823 memcpy(prog
->SamplerUnits
,
825 sizeof(sh
->SamplerUnits
));
827 _mesa_update_shader_textures_used(shProg
, prog
);
828 if (ctx
->Driver
.SamplerUniformChange
)
829 ctx
->Driver
.SamplerUniformChange(ctx
, prog
->Target
, prog
);
836 * Called by glUniformMatrix*() functions.
837 * Note: cols=2, rows=4 ==> array[2] of vec4
840 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
841 GLuint cols
, GLuint rows
,
842 GLint location
, GLsizei count
,
843 GLboolean transpose
, const GLfloat
*values
)
845 unsigned loc
, offset
;
849 struct gl_uniform_storage
*uni
;
851 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
852 &loc
, &offset
, "glUniformMatrix", false))
855 uni
= &shProg
->UniformStorage
[loc
];
856 if (!uni
->type
->is_matrix()) {
857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
858 "glUniformMatrix(non-matrix uniform)");
862 assert(!uni
->type
->is_sampler());
863 vectors
= uni
->type
->matrix_columns
;
864 components
= uni
->type
->vector_elements
;
866 /* Verify that the types are compatible. This is greatly simplified for
867 * matrices because they can only have a float base type.
869 if (vectors
!= cols
|| components
!= rows
) {
870 _mesa_error(ctx
, GL_INVALID_OPERATION
,
871 "glUniformMatrix(matrix size mismatch)");
875 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
876 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
877 if (ctx
->API
== API_OPENGLES
878 || (ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)) {
880 _mesa_error(ctx
, GL_INVALID_VALUE
,
881 "glUniformMatrix(matrix transpose is not GL_FALSE)");
886 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
887 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
888 bool(transpose
), shProg
, location
, uni
);
891 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
893 * "When loading N elements starting at an arbitrary position k in a
894 * uniform declared as an array, elements k through k + N - 1 in the
895 * array will be replaced with the new values. Values for any array
896 * element that exceeds the highest array element index used, as
897 * reported by GetActiveUniform, will be ignored by the GL."
899 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
900 * will have already generated an error.
902 if (uni
->array_elements
!= 0) {
903 count
= MIN2(count
, (int) (uni
->array_elements
- offset
));
906 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
908 /* Store the data in the "actual type" backing storage for the uniform.
910 elements
= components
* vectors
;
913 memcpy(&uni
->storage
[elements
* offset
], values
,
914 sizeof(uni
->storage
[0]) * elements
* count
);
916 /* Copy and transpose the matrix.
918 const float *src
= values
;
919 float *dst
= &uni
->storage
[elements
* offset
].f
;
921 for (int i
= 0; i
< count
; i
++) {
922 for (unsigned r
= 0; r
< rows
; r
++) {
923 for (unsigned c
= 0; c
< cols
; c
++) {
924 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
933 uni
->initialized
= true;
935 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
940 * Called via glGetUniformLocation().
942 * Returns the uniform index into UniformStorage (also the
943 * glGetActiveUniformsiv uniform index), and stores the referenced
944 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
945 * return values can be encoded into a uniform location for
946 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
949 _mesa_get_uniform_location(struct gl_context
*ctx
,
950 struct gl_shader_program
*shProg
,
952 unsigned *out_offset
)
954 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
956 * "The first element of a uniform array is identified using the
957 * name of the uniform array appended with "[0]". Except if the last
958 * part of the string name indicates a uniform array, then the
959 * location of the first element of that array can be retrieved by
960 * either using the name of the uniform array, or the name of the
961 * uniform array appended with "[0]"."
963 * Note: since uniform names are not allowed to use whitespace, and array
964 * indices within uniform names are not allowed to use "+", "-", or leading
965 * zeros, it follows that each uniform has a unique name up to the possible
966 * ambiguity with "[0]" noted above. Therefore we don't need to worry
967 * about mal-formed inputs--they will properly fail when we try to look up
968 * the uniform name in shProg->UniformHash.
971 const GLchar
*base_name_end
;
972 long offset
= parse_program_resource_name(name
, &base_name_end
);
973 bool array_lookup
= offset
>= 0;
977 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
978 memcpy(name_copy
, name
, base_name_end
- name
);
979 name_copy
[base_name_end
- name
] = '\0';
981 name_copy
= (char *) name
;
985 unsigned location
= 0;
986 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
989 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
991 /* Free the temporary buffer *before* possibly returning an error.
993 if (name_copy
!= name
)
997 return GL_INVALID_INDEX
;
999 /* If the uniform is an array, fail if the index is out of bounds.
1000 * (A negative index is caught above.) This also fails if the uniform
1001 * is not an array, but the user is trying to index it, because
1002 * array_elements is zero and offset >= 0.
1005 && offset
>= (long) shProg
->UniformStorage
[location
].array_elements
) {
1006 return GL_INVALID_INDEX
;
1009 *out_offset
= offset
;
1014 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program
*shProg
,
1015 char *errMsg
, size_t errMsgLength
)
1017 const glsl_type
*unit_types
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
1019 memset(unit_types
, 0, sizeof(unit_types
));
1021 for (unsigned i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
1022 const struct gl_uniform_storage
*const storage
=
1023 &shProg
->UniformStorage
[i
];
1024 const glsl_type
*const t
= (storage
->type
->is_array())
1025 ? storage
->type
->fields
.array
: storage
->type
;
1027 if (!t
->is_sampler())
1030 const unsigned count
= MAX2(1, storage
->type
->array_size());
1031 for (unsigned j
= 0; j
< count
; j
++) {
1032 const unsigned unit
= storage
->storage
[j
].i
;
1034 /* The types of the samplers associated with a particular texture
1035 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1036 * OpenGL 3.3 core spec says:
1038 * "It is not allowed to have variables of different sampler
1039 * types pointing to the same texture image unit within a program
1042 if (unit_types
[unit
] == NULL
) {
1043 unit_types
[unit
] = t
;
1044 } else if (unit_types
[unit
] != t
) {
1045 _mesa_snprintf(errMsg
, errMsgLength
,
1046 "Texture unit %d is accessed both as %s and %s",
1047 unit
, unit_types
[unit
]->name
, t
->name
);