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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/core.h"
27 #include "main/context.h"
29 #include "ir_uniform.h"
30 #include "program/hash_table.h"
31 #include "../glsl/program.h"
32 #include "../glsl/ir_uniform.h"
35 #include "main/shaderapi.h"
36 #include "main/shaderobj.h"
40 extern "C" void GLAPIENTRY
41 _mesa_GetActiveUniformARB(GLhandleARB program
, GLuint index
,
42 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
43 GLenum
*type
, GLcharARB
*nameOut
)
45 GET_CURRENT_CONTEXT(ctx
);
46 struct gl_shader_program
*shProg
=
47 _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
52 if (index
>= shProg
->NumUserUniformStorage
) {
53 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
57 const struct gl_uniform_storage
*const uni
= &shProg
->UniformStorage
[index
];
60 _mesa_copy_string(nameOut
, maxLength
, length
, uni
->name
);
64 /* array_elements is zero for non-arrays, but the API requires that 1 be
67 *size
= MAX2(1, uni
->array_elements
);
71 *type
= uni
->type
->gl_type
;
76 base_uniform_type(GLenum type
)
99 case GL_UNSIGNED_INT_VEC2
:
100 case GL_UNSIGNED_INT_VEC3
:
101 case GL_UNSIGNED_INT_VEC4
:
102 return GL_UNSIGNED_INT
;
109 _mesa_problem(NULL
, "Invalid type in base_uniform_type()");
115 is_boolean_type(GLenum type
)
129 is_sampler_type(GLenum type
)
133 case GL_INT_SAMPLER_1D
:
134 case GL_UNSIGNED_INT_SAMPLER_1D
:
136 case GL_INT_SAMPLER_2D
:
137 case GL_UNSIGNED_INT_SAMPLER_2D
:
139 case GL_INT_SAMPLER_3D
:
140 case GL_UNSIGNED_INT_SAMPLER_3D
:
141 case GL_SAMPLER_CUBE
:
142 case GL_INT_SAMPLER_CUBE
:
143 case GL_UNSIGNED_INT_SAMPLER_CUBE
:
144 case GL_SAMPLER_1D_SHADOW
:
145 case GL_SAMPLER_2D_SHADOW
:
146 case GL_SAMPLER_CUBE_SHADOW
:
147 case GL_SAMPLER_2D_RECT_ARB
:
148 case GL_INT_SAMPLER_2D_RECT
:
149 case GL_UNSIGNED_INT_SAMPLER_2D_RECT
:
150 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
151 case GL_SAMPLER_1D_ARRAY_EXT
:
152 case GL_INT_SAMPLER_1D_ARRAY
:
153 case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY
:
154 case GL_SAMPLER_2D_ARRAY_EXT
:
155 case GL_INT_SAMPLER_2D_ARRAY
:
156 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY
:
157 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
158 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
159 case GL_SAMPLER_CUBE_MAP_ARRAY
:
160 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW
:
161 case GL_SAMPLER_BUFFER
:
162 case GL_INT_SAMPLER_BUFFER
:
163 case GL_UNSIGNED_INT_SAMPLER_BUFFER
:
164 case GL_SAMPLER_2D_MULTISAMPLE
:
165 case GL_INT_SAMPLER_2D_MULTISAMPLE
:
166 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE
:
167 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY
:
168 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY
:
169 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY
:
170 case GL_SAMPLER_EXTERNAL_OES
:
178 * Given a uniform index, return the vertex/geometry/fragment program
179 * that has that parameter, plus the position of the parameter in the
180 * parameter/constant buffer.
181 * \param shProg the shader program
182 * \param index the uniform index in [0, NumUniforms-1]
183 * \param progOut returns containing program
184 * \param posOut returns position of the uniform in the param/const buffer
185 * \return GL_TRUE for success, GL_FALSE for invalid index
188 find_uniform_parameter_pos(struct gl_shader_program
*shProg
, GLint index
,
189 struct gl_program
**progOut
, GLint
*posOut
)
191 struct gl_program
*prog
= NULL
;
194 if (!shProg
->Uniforms
||
196 index
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
200 pos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
202 prog
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
205 pos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
207 prog
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
210 pos
= shProg
->Uniforms
->Uniforms
[index
].GeomPos
;
212 prog
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
;
217 if (!prog
|| pos
< 0)
218 return GL_FALSE
; /* should really never happen */
227 * Return pointer to a gl_program_parameter which corresponds to a uniform.
228 * \param shProg the shader program
229 * \param index the uniform index in [0, NumUniforms-1]
230 * \return gl_program_parameter point or NULL if index is invalid
232 const struct gl_program_parameter
*
233 get_uniform_parameter(struct gl_shader_program
*shProg
, GLint index
)
235 struct gl_program
*prog
;
238 if (find_uniform_parameter_pos(shProg
, index
, &prog
, &progPos
))
239 return &prog
->Parameters
->Parameters
[progPos
];
245 get_vector_elements(GLenum type
)
251 case GL_UNSIGNED_INT
:
252 default: /* Catch all the various sampler types. */
258 case GL_UNSIGNED_INT_VEC2
:
264 case GL_UNSIGNED_INT_VEC3
:
270 case GL_UNSIGNED_INT_VEC4
:
276 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
282 case GL_FLOAT_MAT2x3
:
286 case GL_FLOAT_MAT2x4
:
294 case GL_FLOAT_MAT3x2
:
298 case GL_FLOAT_MAT3x4
:
306 case GL_FLOAT_MAT4x2
:
310 case GL_FLOAT_MAT4x3
:
320 * Determine the number of rows and columns occupied by a uniform
321 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
322 * the number of rows = 1 and cols = number of elements in the vector.
325 get_uniform_rows_cols(const struct gl_program_parameter
*p
,
326 GLint
*rows
, GLint
*cols
)
328 get_matrix_dims(p
->DataType
, rows
, cols
);
329 if (*rows
== 0 && *cols
== 0) {
330 /* not a matrix type, probably a float or vector */
332 *cols
= get_vector_elements(p
->DataType
);
337 validate_uniform_parameters(struct gl_context
*ctx
,
338 struct gl_shader_program
*shProg
,
339 GLint location
, GLsizei count
,
341 unsigned *array_index
,
343 bool negative_one_is_not_valid
)
345 if (!shProg
|| !shProg
->LinkStatus
) {
346 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(program not linked)", caller
);
350 if (location
== -1) {
351 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
354 * "The error INVALID_OPERATION is generated if program has not been
355 * linked successfully, or if location is not a valid location for
358 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
361 * "If the value of location is -1, the Uniform* commands will
362 * silently ignore the data passed in, and the current uniform
363 * values will not be changed."
365 * Allowing -1 for the location parameter of glUniform allows
366 * applications to avoid error paths in the case that, for example, some
367 * uniform variable is removed by the compiler / linker after
368 * optimization. In this case, the new value of the uniform is dropped
369 * on the floor. For the case of glGetUniform, there is nothing
370 * sensible to do for a location of -1.
372 * The negative_one_is_not_valid flag selects between the two behaviors.
374 if (negative_one_is_not_valid
) {
375 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
382 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
384 * "If a negative number is provided where an argument of type sizei or
385 * sizeiptr is specified, the error INVALID_VALUE is generated."
388 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count < 0)", caller
);
392 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
394 * "If any of the following conditions occur, an INVALID_OPERATION
395 * error is generated by the Uniform* commands, and no uniform values
400 * - if no variable with a location of location exists in the
401 * program object currently in use and location is not -1,
402 * - if count is greater than one, and the uniform declared in the
403 * shader is not an array variable,
406 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
411 _mesa_uniform_split_location_offset(location
, loc
, array_index
);
413 if (*loc
>= shProg
->NumUserUniformStorage
) {
414 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
419 if (shProg
->UniformStorage
[*loc
].array_elements
== 0 && count
> 1) {
420 _mesa_error(ctx
, GL_INVALID_OPERATION
,
421 "%s(count > 1 for non-array, location=%d)",
426 /* This case should be impossible. The implication is that a call like
427 * glGetUniformLocation(prog, "foo[8]") was successful but "foo" is not an
430 if (*array_index
!= 0 && shProg
->UniformStorage
[*loc
].array_elements
== 0) {
431 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(location=%d)",
439 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
442 _mesa_get_uniform(struct gl_context
*ctx
, GLuint program
, GLint location
,
443 GLsizei bufSize
, enum glsl_base_type returnType
,
446 struct gl_shader_program
*shProg
=
447 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformfv");
448 struct gl_uniform_storage
*uni
;
449 unsigned loc
, offset
;
451 if (!validate_uniform_parameters(ctx
, shProg
, location
, 1,
452 &loc
, &offset
, "glGetUniform", true))
455 uni
= &shProg
->UniformStorage
[loc
];
458 unsigned elements
= (uni
->type
->is_sampler())
459 ? 1 : uni
->type
->components();
461 /* Calculate the source base address *BEFORE* modifying elements to
462 * account for the size of the user's buffer.
464 const union gl_constant_value
*const src
=
465 &uni
->storage
[offset
* elements
];
467 unsigned bytes
= sizeof(uni
->storage
[0]) * elements
;
468 if (bytes
> bufSize
) {
469 elements
= bufSize
/ sizeof(uni
->storage
[0]);
473 /* If the return type and the uniform's native type are "compatible,"
474 * just memcpy the data. If the types are not compatible, perform a
475 * slower convert-and-copy process.
477 if (returnType
== uni
->type
->base_type
478 || ((returnType
== GLSL_TYPE_INT
479 || returnType
== GLSL_TYPE_UINT
480 || returnType
== GLSL_TYPE_SAMPLER
)
482 (uni
->type
->base_type
== GLSL_TYPE_INT
483 || uni
->type
->base_type
== GLSL_TYPE_UINT
484 || uni
->type
->base_type
== GLSL_TYPE_SAMPLER
))) {
485 memcpy(paramsOut
, src
, bytes
);
487 union gl_constant_value
*const dst
=
488 (union gl_constant_value
*) paramsOut
;
490 /* This code could be optimized by putting the loop inside the switch
491 * statements. However, this is not expected to be
492 * performance-critical code.
494 for (unsigned i
= 0; i
< elements
; i
++) {
495 switch (returnType
) {
496 case GLSL_TYPE_FLOAT
:
497 switch (uni
->type
->base_type
) {
499 dst
[i
].f
= (float) src
[i
].u
;
502 case GLSL_TYPE_SAMPLER
:
503 dst
[i
].f
= (float) src
[i
].i
;
506 dst
[i
].f
= src
[i
].i
? 1.0f
: 0.0f
;
509 assert(!"Should not get here.");
516 switch (uni
->type
->base_type
) {
517 case GLSL_TYPE_FLOAT
:
518 /* While the GL 3.2 core spec doesn't explicitly
519 * state how conversion of float uniforms to integer
520 * values works, in section 6.2 "State Tables" on
523 * "Unless otherwise specified, when floating
524 * point state is returned as integer values or
525 * integer state is returned as floating-point
526 * values it is converted in the fashion
527 * described in section 6.1.2"
529 * That section, on page 248, says:
531 * "If GetIntegerv or GetInteger64v are called,
532 * a floating-point value is rounded to the
533 * nearest integer..."
535 dst
[i
].i
= IROUND(src
[i
].f
);
538 dst
[i
].i
= src
[i
].i
? 1 : 0;
541 assert(!"Should not get here.");
547 assert(!"Should not get here.");
556 log_uniform(const void *values
, enum glsl_base_type basicType
,
557 unsigned rows
, unsigned cols
, unsigned count
,
559 const struct gl_shader_program
*shProg
,
561 const struct gl_uniform_storage
*uni
)
564 const union gl_constant_value
*v
= (const union gl_constant_value
*) values
;
565 const unsigned elems
= rows
* cols
* count
;
566 const char *const extra
= (cols
== 1) ? "uniform" : "uniform matrix";
568 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
569 "transpose = %s) to: ",
570 shProg
->Name
, extra
, uni
->name
, location
, uni
->type
->name
,
571 transpose
? "true" : "false");
572 for (unsigned i
= 0; i
< elems
; i
++) {
573 if (i
!= 0 && ((i
% rows
) == 0))
578 printf("%u ", v
[i
].u
);
581 printf("%d ", v
[i
].i
);
583 case GLSL_TYPE_FLOAT
:
584 printf("%g ", v
[i
].f
);
587 assert(!"Should not get here.");
597 log_program_parameters(const struct gl_shader_program
*shProg
)
599 static const char *stages
[] = {
600 "vertex", "fragment", "geometry"
603 assert(Elements(stages
) == MESA_SHADER_TYPES
);
605 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
606 if (shProg
->_LinkedShaders
[i
] == NULL
)
609 const struct gl_program
*const prog
= shProg
->_LinkedShaders
[i
]->Program
;
611 printf("Program %d %s shader parameters:\n",
612 shProg
->Name
, stages
[i
]);
613 for (unsigned j
= 0; j
< prog
->Parameters
->NumParameters
; j
++) {
614 printf("%s: %p %f %f %f %f\n",
615 prog
->Parameters
->Parameters
[j
].Name
,
616 prog
->Parameters
->ParameterValues
[j
],
617 prog
->Parameters
->ParameterValues
[j
][0].f
,
618 prog
->Parameters
->ParameterValues
[j
][1].f
,
619 prog
->Parameters
->ParameterValues
[j
][2].f
,
620 prog
->Parameters
->ParameterValues
[j
][3].f
);
628 * Check if the type given by userType is allowed to set a uniform of the
629 * target type. Generally, equivalence is required, but setting Boolean
630 * uniforms can be done with glUniformiv or glUniformfv.
633 compatible_types(GLenum userType
, GLenum targetType
)
635 if (userType
== targetType
)
638 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
||
639 userType
== GL_UNSIGNED_INT
||
643 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
644 userType
== GL_UNSIGNED_INT_VEC2
||
645 userType
== GL_INT_VEC2
))
648 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
649 userType
== GL_UNSIGNED_INT_VEC3
||
650 userType
== GL_INT_VEC3
))
653 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
654 userType
== GL_UNSIGNED_INT_VEC4
||
655 userType
== GL_INT_VEC4
))
658 if (is_sampler_type(targetType
) && userType
== GL_INT
)
665 * Set the value of a program's uniform variable.
666 * \param program the program whose uniform to update
667 * \param index the index of the program parameter for the uniform
668 * \param offset additional parameter slot offset (for arrays)
669 * \param type the incoming datatype of 'values'
670 * \param count the number of uniforms to set
671 * \param elems number of elements per uniform (1, 2, 3 or 4)
672 * \param values the new values, of datatype 'type'
675 set_program_uniform(struct gl_context
*ctx
, struct gl_program
*program
,
676 GLint index
, GLint offset
,
677 GLenum type
, GLsizei count
, GLint elems
,
680 const struct gl_program_parameter
*param
=
681 &program
->Parameters
->Parameters
[index
];
687 if (!compatible_types(type
, param
->DataType
)) {
688 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
692 if (index
+ offset
> (GLint
) program
->Parameters
->Size
) {
697 if (param
->Type
== PROGRAM_SAMPLER
) {
698 /* This controls which texture unit which is used by a sampler */
699 GLboolean changed
= GL_FALSE
;
702 /* this should have been caught by the compatible_types() check */
703 ASSERT(type
== GL_INT
);
705 /* loop over number of samplers to change */
706 for (i
= 0; i
< count
; i
++) {
707 GLuint sampler
= (GLuint
)
708 program
->Parameters
->ParameterValues
[index
+offset
+ i
][0].f
;
709 GLuint texUnit
= ((GLuint
*) values
)[i
];
711 /* check that the sampler (tex unit index) is legal */
712 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
713 _mesa_error(ctx
, GL_INVALID_VALUE
,
714 "glUniform1(invalid sampler/tex unit index for '%s')",
719 /* This maps a sampler to a texture unit: */
720 if (sampler
< MAX_SAMPLERS
) {
722 printf("Set program %p sampler %d '%s' to unit %u\n",
723 program
, sampler
, param
->Name
, texUnit
);
725 if (program
->SamplerUnits
[sampler
] != texUnit
) {
726 program
->SamplerUnits
[sampler
] = texUnit
;
733 /* When a sampler's value changes it usually requires rewriting
734 * a GPU program's TEX instructions since there may not be a
735 * sampler->texture lookup table. We signal this with the
736 * ProgramStringNotify() callback.
738 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
739 _mesa_update_shader_textures_used(program
);
740 /* Do we need to care about the return value here?
741 * This should not be the first time the driver was notified of
744 (void) ctx
->Driver
.ProgramStringNotify(ctx
, program
->Target
, program
);
748 /* ordinary uniform variable */
749 const GLboolean isUniformBool
= is_boolean_type(param
->DataType
);
750 const GLenum basicType
= base_uniform_type(type
);
751 const GLint slots
= (param
->Size
+ 3) / 4;
752 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
755 if ((GLint
) param
->Size
> typeSize
) {
757 /* we'll ignore extra data below */
760 /* non-array: count must be at most one; count == 0 is handled
764 _mesa_error(ctx
, GL_INVALID_OPERATION
,
765 "glUniform(uniform '%s' is not an array)",
771 /* loop over number of array elements */
772 for (k
= 0; k
< count
; k
++) {
773 gl_constant_value
*uniformVal
;
775 if (offset
+ k
>= slots
) {
776 /* Extra array data is ignored */
780 /* uniformVal (the destination) is always gl_constant_value[4] */
781 uniformVal
= program
->Parameters
->ParameterValues
[index
+ offset
+ k
];
783 if (basicType
== GL_INT
) {
784 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
785 for (i
= 0; i
< elems
; i
++) {
786 if (!ctx
->Const
.NativeIntegers
)
787 uniformVal
[i
].f
= (GLfloat
) iValues
[i
];
789 uniformVal
[i
].i
= iValues
[i
];
792 else if (basicType
== GL_UNSIGNED_INT
) {
793 const GLuint
*iValues
= ((const GLuint
*) values
) + k
* elems
;
794 for (i
= 0; i
< elems
; i
++) {
795 if (!ctx
->Const
.NativeIntegers
)
796 uniformVal
[i
].f
= (GLfloat
)(GLuint
) iValues
[i
];
798 uniformVal
[i
].u
= iValues
[i
];
802 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
803 assert(basicType
== GL_FLOAT
);
804 for (i
= 0; i
< elems
; i
++) {
805 uniformVal
[i
].f
= fValues
[i
];
809 /* if the uniform is bool-valued, convert to 1 or 0 */
811 for (i
= 0; i
< elems
; i
++) {
812 if (basicType
== GL_FLOAT
)
813 uniformVal
[i
].b
= uniformVal
[i
].f
!= 0.0f
? 1 : 0;
815 uniformVal
[i
].b
= uniformVal
[i
].u
? 1 : 0;
817 if (ctx
->Const
.NativeIntegers
)
819 uniformVal
[i
].b
? ctx
->Const
.UniformBooleanTrue
: 0;
821 uniformVal
[i
].f
= uniformVal
[i
].b
? 1.0f
: 0.0f
;
829 * Propagate some values from uniform backing storage to driver storage
831 * Values propagated from uniform backing storage to driver storage
832 * have all format / type conversions previously requested by the
833 * driver applied. This function is most often called by the
834 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
837 * \param uni Uniform whose data is to be propagated to driver storage
838 * \param array_index If \c uni is an array, this is the element of
839 * the array to be propagated.
840 * \param count Number of array elements to propagate.
843 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage
*uni
,
844 unsigned array_index
,
849 /* vector_elements and matrix_columns can be 0 for samplers.
851 const unsigned components
= MAX2(1, uni
->type
->vector_elements
);
852 const unsigned vectors
= MAX2(1, uni
->type
->matrix_columns
);
854 /* Store the data in the driver's requested type in the driver's storage
857 unsigned src_vector_byte_stride
= components
* 4;
859 for (i
= 0; i
< uni
->num_driver_storage
; i
++) {
860 struct gl_uniform_driver_storage
*const store
= &uni
->driver_storage
[i
];
861 uint8_t *dst
= (uint8_t *) store
->data
;
862 const unsigned extra_stride
=
863 store
->element_stride
- (vectors
* store
->vector_stride
);
865 (uint8_t *) (&uni
->storage
[array_index
* (components
* vectors
)].i
);
868 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
870 __func__
, dst
, array_index
, components
,
871 vectors
, count
, store
->vector_stride
, extra_stride
);
874 dst
+= array_index
* store
->element_stride
;
876 switch (store
->format
) {
878 case uniform_bool_int_0_1
: {
882 for (j
= 0; j
< count
; j
++) {
883 for (v
= 0; v
< vectors
; v
++) {
884 memcpy(dst
, src
, src_vector_byte_stride
);
885 src
+= src_vector_byte_stride
;
886 dst
+= store
->vector_stride
;
894 case uniform_int_float
:
895 case uniform_bool_float
: {
896 const int *isrc
= (const int *) src
;
901 for (j
= 0; j
< count
; j
++) {
902 for (v
= 0; v
< vectors
; v
++) {
903 for (c
= 0; c
< components
; c
++) {
904 ((float *) dst
)[c
] = (float) *isrc
;
908 dst
+= store
->vector_stride
;
916 case uniform_bool_int_0_not0
: {
917 const int *isrc
= (const int *) src
;
922 for (j
= 0; j
< count
; j
++) {
923 for (v
= 0; v
< vectors
; v
++) {
924 for (c
= 0; c
< components
; c
++) {
925 ((int *) dst
)[c
] = *isrc
== 0 ? 0 : ~0;
929 dst
+= store
->vector_stride
;
938 assert(!"Should not get here.");
945 * Called via glUniform*() functions.
948 _mesa_uniform(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
949 GLint location
, GLsizei count
,
950 const GLvoid
*values
, GLenum type
)
952 unsigned loc
, offset
;
954 unsigned src_components
;
956 enum glsl_base_type basicType
;
957 struct gl_uniform_storage
*uni
;
959 ASSERT_OUTSIDE_BEGIN_END(ctx
);
961 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
962 &loc
, &offset
, "glUniform", false))
965 uni
= &shProg
->UniformStorage
[loc
];
967 /* Verify that the types are compatible.
971 basicType
= GLSL_TYPE_FLOAT
;
975 basicType
= GLSL_TYPE_FLOAT
;
979 basicType
= GLSL_TYPE_FLOAT
;
983 basicType
= GLSL_TYPE_FLOAT
;
986 case GL_UNSIGNED_INT
:
987 basicType
= GLSL_TYPE_UINT
;
990 case GL_UNSIGNED_INT_VEC2
:
991 basicType
= GLSL_TYPE_UINT
;
994 case GL_UNSIGNED_INT_VEC3
:
995 basicType
= GLSL_TYPE_UINT
;
998 case GL_UNSIGNED_INT_VEC4
:
999 basicType
= GLSL_TYPE_UINT
;
1003 basicType
= GLSL_TYPE_INT
;
1007 basicType
= GLSL_TYPE_INT
;
1011 basicType
= GLSL_TYPE_INT
;
1015 basicType
= GLSL_TYPE_INT
;
1023 case GL_FLOAT_MAT2x3
:
1024 case GL_FLOAT_MAT2x4
:
1025 case GL_FLOAT_MAT3x2
:
1027 case GL_FLOAT_MAT3x4
:
1028 case GL_FLOAT_MAT4x2
:
1029 case GL_FLOAT_MAT4x3
:
1032 _mesa_problem(NULL
, "Invalid type in %s", __func__
);
1036 if (uni
->type
->is_sampler()) {
1039 components
= uni
->type
->vector_elements
;
1043 switch (uni
->type
->base_type
) {
1044 case GLSL_TYPE_BOOL
:
1047 case GLSL_TYPE_SAMPLER
:
1048 match
= (basicType
== GLSL_TYPE_INT
);
1051 match
= (basicType
== uni
->type
->base_type
);
1055 if (uni
->type
->is_matrix() || components
!= src_components
|| !match
) {
1056 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
1060 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
1061 log_uniform(values
, basicType
, components
, 1, count
,
1062 false, shProg
, location
, uni
);
1065 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
1067 * "Setting a sampler's value to i selects texture image unit number
1068 * i. The values of i range from zero to the implementation- dependent
1069 * maximum supported number of texture image units."
1071 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
1074 * "Error Description Offending command
1077 * INVALID_VALUE Numeric argument out of range Yes"
1079 * Based on that, when an invalid sampler is specified, we generate a
1080 * GL_INVALID_VALUE error and ignore the command.
1082 if (uni
->type
->is_sampler()) {
1083 for (i
= 0; i
< count
; i
++) {
1084 const unsigned texUnit
= ((unsigned *) values
)[i
];
1086 /* check that the sampler (tex unit index) is legal */
1087 if (texUnit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
1088 _mesa_error(ctx
, GL_INVALID_VALUE
,
1089 "glUniform1i(invalid sampler/tex unit index for "
1097 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1099 * "When loading N elements starting at an arbitrary position k in a
1100 * uniform declared as an array, elements k through k + N - 1 in the
1101 * array will be replaced with the new values. Values for any array
1102 * element that exceeds the highest array element index used, as
1103 * reported by GetActiveUniform, will be ignored by the GL."
1105 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1106 * will have already generated an error.
1108 if (uni
->array_elements
!= 0) {
1109 if (offset
>= uni
->array_elements
)
1112 count
= MIN2(count
, (uni
->array_elements
- offset
));
1115 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1117 /* Store the data in the "actual type" backing storage for the uniform.
1119 if (!uni
->type
->is_boolean()) {
1120 memcpy(&uni
->storage
[components
* offset
], values
,
1121 sizeof(uni
->storage
[0]) * components
* count
);
1123 const union gl_constant_value
*src
=
1124 (const union gl_constant_value
*) values
;
1125 union gl_constant_value
*dst
= &uni
->storage
[components
* offset
];
1126 const unsigned elems
= components
* count
;
1128 for (i
= 0; i
< elems
; i
++) {
1129 if (basicType
== GLSL_TYPE_FLOAT
) {
1130 dst
[i
].i
= src
[i
].f
!= 0.0f
? 1 : 0;
1132 dst
[i
].i
= src
[i
].i
!= 0 ? 1 : 0;
1137 uni
->initialized
= true;
1139 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1141 /* If the uniform is a sampler, do the extra magic necessary to propagate
1142 * the changes through.
1144 if (uni
->type
->is_sampler()) {
1145 for (i
= 0; i
< count
; i
++) {
1146 shProg
->SamplerUnits
[uni
->sampler
+ offset
+ i
] =
1147 ((unsigned *) values
)[i
];
1150 bool flushed
= false;
1151 for (i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
1152 struct gl_program
*prog
;
1154 if (shProg
->_LinkedShaders
[i
] == NULL
)
1157 prog
= shProg
->_LinkedShaders
[i
]->Program
;
1159 assert(sizeof(prog
->SamplerUnits
) == sizeof(shProg
->SamplerUnits
));
1161 if (memcmp(prog
->SamplerUnits
,
1162 shProg
->SamplerUnits
,
1163 sizeof(shProg
->SamplerUnits
)) != 0) {
1165 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
| _NEW_PROGRAM
);
1169 memcpy(prog
->SamplerUnits
,
1170 shProg
->SamplerUnits
,
1171 sizeof(shProg
->SamplerUnits
));
1173 _mesa_update_shader_textures_used(prog
);
1174 (void) ctx
->Driver
.ProgramStringNotify(ctx
, prog
->Target
, prog
);
1181 * Set a matrix-valued program parameter.
1184 set_program_uniform_matrix(struct gl_context
*ctx
, struct gl_program
*program
,
1185 GLuint index
, GLuint offset
,
1186 GLuint count
, GLuint rows
, GLuint cols
,
1187 GLboolean transpose
, const GLfloat
*values
)
1189 GLuint mat
, row
, col
;
1191 const struct gl_program_parameter
*param
=
1192 &program
->Parameters
->Parameters
[index
];
1193 const GLuint slots
= (param
->Size
+ 3) / 4;
1194 const GLint typeSize
= _mesa_sizeof_glsl_type(param
->DataType
);
1197 /* check that the number of rows, columns is correct */
1198 get_matrix_dims(param
->DataType
, &nr
, &nc
);
1199 if (rows
!= nr
|| cols
!= nc
) {
1200 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1201 "glUniformMatrix(matrix size mismatch)");
1205 if ((GLint
) param
->Size
<= typeSize
) {
1206 /* non-array: count must be at most one; count == 0 is handled
1210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1211 "glUniformMatrix(uniform is not an array)");
1217 * Note: the _columns_ of a matrix are stored in program registers, not
1218 * the rows. So, the loops below look a little funny.
1219 * XXX could optimize this a bit...
1222 /* loop over matrices */
1223 for (mat
= 0; mat
< count
; mat
++) {
1226 for (col
= 0; col
< cols
; col
++) {
1228 if (offset
>= slots
) {
1229 /* Ignore writes beyond the end of (the used part of) an array */
1232 v
= (GLfloat
*) program
->Parameters
->ParameterValues
[index
+ offset
];
1233 for (row
= 0; row
< rows
; row
++) {
1235 v
[row
] = values
[src
+ row
* cols
+ col
];
1238 v
[row
] = values
[src
+ col
* rows
+ row
];
1245 src
+= rows
* cols
; /* next matrix */
1250 * Called by glUniformMatrix*() functions.
1251 * Note: cols=2, rows=4 ==> array[2] of vec4
1254 _mesa_uniform_matrix(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1255 GLint cols
, GLint rows
,
1256 GLint location
, GLsizei count
,
1257 GLboolean transpose
, const GLfloat
*values
)
1259 unsigned loc
, offset
;
1261 unsigned components
;
1263 struct gl_uniform_storage
*uni
;
1265 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1267 if (!validate_uniform_parameters(ctx
, shProg
, location
, count
,
1268 &loc
, &offset
, "glUniformMatrix", false))
1271 uni
= &shProg
->UniformStorage
[loc
];
1272 if (!uni
->type
->is_matrix()) {
1273 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1274 "glUniformMatrix(non-matrix uniform)");
1278 assert(!uni
->type
->is_sampler());
1279 vectors
= uni
->type
->matrix_columns
;
1280 components
= uni
->type
->vector_elements
;
1282 /* Verify that the types are compatible. This is greatly simplified for
1283 * matrices because they can only have a float base type.
1285 if (vectors
!= cols
|| components
!= rows
) {
1286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1287 "glUniformMatrix(matrix size mismatch)");
1291 if (ctx
->Shader
.Flags
& GLSL_UNIFORMS
) {
1292 log_uniform(values
, GLSL_TYPE_FLOAT
, components
, vectors
, count
,
1293 bool(transpose
), shProg
, location
, uni
);
1296 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1298 * "When loading N elements starting at an arbitrary position k in a
1299 * uniform declared as an array, elements k through k + N - 1 in the
1300 * array will be replaced with the new values. Values for any array
1301 * element that exceeds the highest array element index used, as
1302 * reported by GetActiveUniform, will be ignored by the GL."
1304 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1305 * will have already generated an error.
1307 if (uni
->array_elements
!= 0) {
1308 if (offset
>= uni
->array_elements
)
1311 count
= MIN2(count
, (uni
->array_elements
- offset
));
1314 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
1316 /* Store the data in the "actual type" backing storage for the uniform.
1318 elements
= components
* vectors
;
1321 memcpy(&uni
->storage
[elements
* offset
], values
,
1322 sizeof(uni
->storage
[0]) * elements
* count
);
1324 /* Copy and transpose the matrix.
1326 const float *src
= values
;
1327 float *dst
= &uni
->storage
[elements
* offset
].f
;
1329 for (unsigned i
= 0; i
< count
; i
++) {
1330 for (unsigned r
= 0; r
< rows
; r
++) {
1331 for (unsigned c
= 0; c
< cols
; c
++) {
1332 dst
[(c
* components
) + r
] = src
[c
+ (r
* vectors
)];
1341 uni
->initialized
= true;
1343 _mesa_propagate_uniforms_to_driver_storage(uni
, offset
, count
);
1347 * Called via glGetUniformLocation().
1349 * The return value will encode two values, the uniform location and an
1350 * offset (used for arrays, structs).
1353 _mesa_get_uniform_location(struct gl_context
*ctx
,
1354 struct gl_shader_program
*shProg
,
1357 const size_t len
= strlen(name
);
1362 /* If the name ends with a ']', assume that it refers to some element of an
1363 * array. Malformed array references will fail the hash table look up
1364 * below, so it doesn't matter that they are not caught here. This code
1365 * only wants to catch the "leaf" array references so that arrays of
1366 * structures containing arrays will be handled correctly.
1368 if (name
[len
-1] == ']') {
1371 /* Walk backwards over the string looking for a non-digit character.
1372 * This had better be the opening bracket for an array index.
1374 * Initially, i specifies the location of the ']'. Since the string may
1375 * contain only the ']' charcater, walk backwards very carefully.
1377 for (i
= len
- 1; (i
> 0) && isdigit(name
[i
-1]); --i
)
1380 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
1382 * "The first element of a uniform array is identified using the
1383 * name of the uniform array appended with "[0]". Except if the last
1384 * part of the string name indicates a uniform array, then the
1385 * location of the first element of that array can be retrieved by
1386 * either using the name of the uniform array, or the name of the
1387 * uniform array appended with "[0]"."
1389 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
1391 * "name must be a null terminated string, without white space."
1393 * Return an error if there is no opening '[' to match the closing ']'.
1394 * An error will also be returned if there is intervening white space
1395 * (or other non-digit characters) before the opening '['.
1397 if ((i
== 0) || name
[i
-1] != '[')
1400 /* Return an error if there are no digits between the opening '[' to
1401 * match the closing ']'.
1406 /* Make a new string that is a copy of the old string up to (but not
1407 * including) the '[' character.
1409 name_copy
= (char *) malloc(i
);
1410 memcpy(name_copy
, name
, i
- 1);
1411 name_copy
[i
-1] = '\0';
1413 offset
= strtol(&name
[i
], NULL
, 10);
1417 array_lookup
= true;
1419 name_copy
= (char *) name
;
1421 array_lookup
= false;
1425 const bool found
= shProg
->UniformHash
->get(location
, name_copy
);
1428 || strcmp(name_copy
, shProg
->UniformStorage
[location
].name
) == 0);
1430 /* Free the temporary buffer *before* possibly returning an error.
1432 if (name_copy
!= name
)
1438 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
1439 * to (correctly) fail if 'a' is not an array.
1441 if (array_lookup
&& shProg
->UniformStorage
[location
].array_elements
== 0) {
1445 return _mesa_uniform_merge_location_offset(location
, offset
);