f0ab0f079966aecf60854198a70316179727e4ba
[mesa.git] / src / mesa / main / uniform_query.cpp
1 /*
2 * Mesa 3-D graphics library
3 *
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
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26 #include <stdlib.h>
27
28 #include "main/core.h"
29 #include "main/context.h"
30 #include "ir.h"
31 #include "ir_uniform.h"
32 #include "program/hash_table.h"
33 #include "../glsl/program.h"
34 #include "../glsl/ir_uniform.h"
35 #include "main/shaderapi.h"
36 #include "main/shaderobj.h"
37 #include "uniforms.h"
38
39
40 extern "C" void GLAPIENTRY
41 _mesa_GetActiveUniform(GLhandleARB program, GLuint index,
42 GLsizei maxLength, GLsizei *length, GLint *size,
43 GLenum *type, GLcharARB *nameOut)
44 {
45 GET_CURRENT_CONTEXT(ctx);
46 struct gl_shader_program *shProg =
47 _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
48
49 ASSERT_OUTSIDE_BEGIN_END(ctx);
50
51 if (!shProg)
52 return;
53
54 if (index >= shProg->NumUserUniformStorage) {
55 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
56 return;
57 }
58
59 const struct gl_uniform_storage *const uni = &shProg->UniformStorage[index];
60
61 if (nameOut) {
62 _mesa_get_uniform_name(uni, maxLength, length, nameOut);
63 }
64
65 if (size) {
66 /* array_elements is zero for non-arrays, but the API requires that 1 be
67 * returned.
68 */
69 *size = MAX2(1, uni->array_elements);
70 }
71
72 if (type) {
73 *type = uni->type->gl_type;
74 }
75 }
76
77 extern "C" void GLAPIENTRY
78 _mesa_GetActiveUniformsiv(GLuint program,
79 GLsizei uniformCount,
80 const GLuint *uniformIndices,
81 GLenum pname,
82 GLint *params)
83 {
84 GET_CURRENT_CONTEXT(ctx);
85 struct gl_shader_program *shProg;
86 GLsizei i;
87
88 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
89 if (!shProg)
90 return;
91
92 if (uniformCount < 0) {
93 _mesa_error(ctx, GL_INVALID_VALUE,
94 "glGetUniformIndices(uniformCount < 0)");
95 return;
96 }
97
98 for (i = 0; i < uniformCount; i++) {
99 GLuint index = uniformIndices[i];
100
101 if (index >= shProg->NumUserUniformStorage) {
102 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniformsiv(index)");
103 return;
104 }
105 }
106
107 for (i = 0; i < uniformCount; i++) {
108 GLuint index = uniformIndices[i];
109 const struct gl_uniform_storage *uni = &shProg->UniformStorage[index];
110
111 switch (pname) {
112 case GL_UNIFORM_TYPE:
113 params[i] = uni->type->gl_type;
114 break;
115
116 case GL_UNIFORM_SIZE:
117 /* array_elements is zero for non-arrays, but the API requires that 1 be
118 * returned.
119 */
120 params[i] = MAX2(1, uni->array_elements);
121 break;
122
123 case GL_UNIFORM_NAME_LENGTH:
124 params[i] = strlen(uni->name) + 1;
125
126 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
127 * spec says:
128 *
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
131 * with "[0]"."
132 */
133 if (uni->array_elements != 0)
134 params[i] += 3;
135 break;
136
137 case GL_UNIFORM_BLOCK_INDEX:
138 params[i] = uni->block_index;
139 break;
140
141 case GL_UNIFORM_OFFSET:
142 params[i] = uni->offset;
143 break;
144
145 case GL_UNIFORM_ARRAY_STRIDE:
146 params[i] = uni->array_stride;
147 break;
148
149 case GL_UNIFORM_MATRIX_STRIDE:
150 params[i] = uni->matrix_stride;
151 break;
152
153 case GL_UNIFORM_IS_ROW_MAJOR:
154 params[i] = uni->row_major;
155 break;
156
157 default:
158 _mesa_error(ctx, GL_INVALID_ENUM, "glGetActiveUniformsiv(pname)");
159 return;
160 }
161 }
162 }
163
164 static bool
165 validate_uniform_parameters(struct gl_context *ctx,
166 struct gl_shader_program *shProg,
167 GLint location, GLsizei count,
168 unsigned *loc,
169 unsigned *array_index,
170 const char *caller,
171 bool negative_one_is_not_valid)
172 {
173 if (!shProg || !shProg->LinkStatus) {
174 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(program not linked)", caller);
175 return false;
176 }
177
178 if (location == -1) {
179 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
180 * spec says:
181 *
182 * "The error INVALID_OPERATION is generated if program has not been
183 * linked successfully, or if location is not a valid location for
184 * program."
185 *
186 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
187 * says:
188 *
189 * "If the value of location is -1, the Uniform* commands will
190 * silently ignore the data passed in, and the current uniform
191 * values will not be changed."
192 *
193 * Allowing -1 for the location parameter of glUniform allows
194 * applications to avoid error paths in the case that, for example, some
195 * uniform variable is removed by the compiler / linker after
196 * optimization. In this case, the new value of the uniform is dropped
197 * on the floor. For the case of glGetUniform, there is nothing
198 * sensible to do for a location of -1.
199 *
200 * The negative_one_is_not_valid flag selects between the two behaviors.
201 */
202 if (negative_one_is_not_valid) {
203 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
204 caller, location);
205 }
206
207 return false;
208 }
209
210 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
211 *
212 * "If a negative number is provided where an argument of type sizei or
213 * sizeiptr is specified, the error INVALID_VALUE is generated."
214 */
215 if (count < 0) {
216 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count < 0)", caller);
217 return false;
218 }
219
220 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
221 *
222 * "If any of the following conditions occur, an INVALID_OPERATION
223 * error is generated by the Uniform* commands, and no uniform values
224 * are changed:
225 *
226 * ...
227 *
228 * - if no variable with a location of location exists in the
229 * program object currently in use and location is not -1,
230 * - if count is greater than one, and the uniform declared in the
231 * shader is not an array variable,
232 */
233 if (location < -1) {
234 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
235 caller, location);
236 return false;
237 }
238
239 _mesa_uniform_split_location_offset(location, loc, array_index);
240
241 if (*loc >= shProg->NumUserUniformStorage) {
242 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
243 caller, location);
244 return false;
245 }
246
247 if (shProg->UniformStorage[*loc].array_elements == 0 && count > 1) {
248 _mesa_error(ctx, GL_INVALID_OPERATION,
249 "%s(count > 1 for non-array, location=%d)",
250 caller, location);
251 return false;
252 }
253
254 /* If the uniform is an array, check that array_index is in bounds.
255 * If not an array, check that array_index is zero.
256 * array_index is unsigned so no need to check for less than zero.
257 */
258 unsigned limit = shProg->UniformStorage[*loc].array_elements;
259 if (limit == 0)
260 limit = 1;
261 if (*array_index >= limit) {
262 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
263 caller, location);
264 return false;
265 }
266 return true;
267 }
268
269 /**
270 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
271 */
272 extern "C" void
273 _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
274 GLsizei bufSize, enum glsl_base_type returnType,
275 GLvoid *paramsOut)
276 {
277 struct gl_shader_program *shProg =
278 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformfv");
279 struct gl_uniform_storage *uni;
280 unsigned loc, offset;
281
282 if (!validate_uniform_parameters(ctx, shProg, location, 1,
283 &loc, &offset, "glGetUniform", true))
284 return;
285
286 uni = &shProg->UniformStorage[loc];
287
288 {
289 unsigned elements = (uni->type->is_sampler())
290 ? 1 : uni->type->components();
291
292 /* Calculate the source base address *BEFORE* modifying elements to
293 * account for the size of the user's buffer.
294 */
295 const union gl_constant_value *const src =
296 &uni->storage[offset * elements];
297
298 assert(returnType == GLSL_TYPE_FLOAT || returnType == GLSL_TYPE_INT ||
299 returnType == GLSL_TYPE_UINT);
300 /* The three (currently) supported types all have the same size,
301 * which is of course the same as their union. That'll change
302 * with glGetUniformdv()...
303 */
304 unsigned bytes = sizeof(src[0]) * elements;
305 if (bufSize < 0 || bytes > (unsigned) bufSize) {
306 _mesa_error( ctx, GL_INVALID_OPERATION,
307 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
308 " but %u bytes are required)", bufSize, bytes );
309 return;
310 }
311
312 /* If the return type and the uniform's native type are "compatible,"
313 * just memcpy the data. If the types are not compatible, perform a
314 * slower convert-and-copy process.
315 */
316 if (returnType == uni->type->base_type
317 || ((returnType == GLSL_TYPE_INT
318 || returnType == GLSL_TYPE_UINT
319 || returnType == GLSL_TYPE_SAMPLER)
320 &&
321 (uni->type->base_type == GLSL_TYPE_INT
322 || uni->type->base_type == GLSL_TYPE_UINT
323 || uni->type->base_type == GLSL_TYPE_SAMPLER))) {
324 memcpy(paramsOut, src, bytes);
325 } else {
326 union gl_constant_value *const dst =
327 (union gl_constant_value *) paramsOut;
328
329 /* This code could be optimized by putting the loop inside the switch
330 * statements. However, this is not expected to be
331 * performance-critical code.
332 */
333 for (unsigned i = 0; i < elements; i++) {
334 switch (returnType) {
335 case GLSL_TYPE_FLOAT:
336 switch (uni->type->base_type) {
337 case GLSL_TYPE_UINT:
338 dst[i].f = (float) src[i].u;
339 break;
340 case GLSL_TYPE_INT:
341 case GLSL_TYPE_SAMPLER:
342 dst[i].f = (float) src[i].i;
343 break;
344 case GLSL_TYPE_BOOL:
345 dst[i].f = src[i].i ? 1.0f : 0.0f;
346 break;
347 default:
348 assert(!"Should not get here.");
349 break;
350 }
351 break;
352
353 case GLSL_TYPE_INT:
354 case GLSL_TYPE_UINT:
355 switch (uni->type->base_type) {
356 case GLSL_TYPE_FLOAT:
357 /* While the GL 3.2 core spec doesn't explicitly
358 * state how conversion of float uniforms to integer
359 * values works, in section 6.2 "State Tables" on
360 * page 267 it says:
361 *
362 * "Unless otherwise specified, when floating
363 * point state is returned as integer values or
364 * integer state is returned as floating-point
365 * values it is converted in the fashion
366 * described in section 6.1.2"
367 *
368 * That section, on page 248, says:
369 *
370 * "If GetIntegerv or GetInteger64v are called,
371 * a floating-point value is rounded to the
372 * nearest integer..."
373 */
374 dst[i].i = IROUND(src[i].f);
375 break;
376 case GLSL_TYPE_BOOL:
377 dst[i].i = src[i].i ? 1 : 0;
378 break;
379 default:
380 assert(!"Should not get here.");
381 break;
382 }
383 break;
384
385 default:
386 assert(!"Should not get here.");
387 break;
388 }
389 }
390 }
391 }
392 }
393
394 static void
395 log_uniform(const void *values, enum glsl_base_type basicType,
396 unsigned rows, unsigned cols, unsigned count,
397 bool transpose,
398 const struct gl_shader_program *shProg,
399 GLint location,
400 const struct gl_uniform_storage *uni)
401 {
402
403 const union gl_constant_value *v = (const union gl_constant_value *) values;
404 const unsigned elems = rows * cols * count;
405 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
406
407 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
408 "transpose = %s) to: ",
409 shProg->Name, extra, uni->name, location, uni->type->name,
410 transpose ? "true" : "false");
411 for (unsigned i = 0; i < elems; i++) {
412 if (i != 0 && ((i % rows) == 0))
413 printf(", ");
414
415 switch (basicType) {
416 case GLSL_TYPE_UINT:
417 printf("%u ", v[i].u);
418 break;
419 case GLSL_TYPE_INT:
420 printf("%d ", v[i].i);
421 break;
422 case GLSL_TYPE_FLOAT:
423 printf("%g ", v[i].f);
424 break;
425 default:
426 assert(!"Should not get here.");
427 break;
428 }
429 }
430 printf("\n");
431 fflush(stdout);
432 }
433
434 #if 0
435 static void
436 log_program_parameters(const struct gl_shader_program *shProg)
437 {
438 static const char *stages[] = {
439 "vertex", "fragment", "geometry"
440 };
441
442 assert(Elements(stages) == MESA_SHADER_TYPES);
443
444 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
445 if (shProg->_LinkedShaders[i] == NULL)
446 continue;
447
448 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
449
450 printf("Program %d %s shader parameters:\n",
451 shProg->Name, stages[i]);
452 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
453 printf("%s: %p %f %f %f %f\n",
454 prog->Parameters->Parameters[j].Name,
455 prog->Parameters->ParameterValues[j],
456 prog->Parameters->ParameterValues[j][0].f,
457 prog->Parameters->ParameterValues[j][1].f,
458 prog->Parameters->ParameterValues[j][2].f,
459 prog->Parameters->ParameterValues[j][3].f);
460 }
461 }
462 fflush(stdout);
463 }
464 #endif
465
466 /**
467 * Propagate some values from uniform backing storage to driver storage
468 *
469 * Values propagated from uniform backing storage to driver storage
470 * have all format / type conversions previously requested by the
471 * driver applied. This function is most often called by the
472 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
473 * etc.
474 *
475 * \param uni Uniform whose data is to be propagated to driver storage
476 * \param array_index If \c uni is an array, this is the element of
477 * the array to be propagated.
478 * \param count Number of array elements to propagate.
479 */
480 extern "C" void
481 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
482 unsigned array_index,
483 unsigned count)
484 {
485 unsigned i;
486
487 /* vector_elements and matrix_columns can be 0 for samplers.
488 */
489 const unsigned components = MAX2(1, uni->type->vector_elements);
490 const unsigned vectors = MAX2(1, uni->type->matrix_columns);
491
492 /* Store the data in the driver's requested type in the driver's storage
493 * areas.
494 */
495 unsigned src_vector_byte_stride = components * 4;
496
497 for (i = 0; i < uni->num_driver_storage; i++) {
498 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
499 uint8_t *dst = (uint8_t *) store->data;
500 const unsigned extra_stride =
501 store->element_stride - (vectors * store->vector_stride);
502 const uint8_t *src =
503 (uint8_t *) (&uni->storage[array_index * (components * vectors)].i);
504
505 #if 0
506 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
507 "extra_stride=%u\n",
508 __func__, dst, array_index, components,
509 vectors, count, store->vector_stride, extra_stride);
510 #endif
511
512 dst += array_index * store->element_stride;
513
514 switch (store->format) {
515 case uniform_native:
516 case uniform_bool_int_0_1: {
517 unsigned j;
518 unsigned v;
519
520 for (j = 0; j < count; j++) {
521 for (v = 0; v < vectors; v++) {
522 memcpy(dst, src, src_vector_byte_stride);
523 src += src_vector_byte_stride;
524 dst += store->vector_stride;
525 }
526
527 dst += extra_stride;
528 }
529 break;
530 }
531
532 case uniform_int_float:
533 case uniform_bool_float: {
534 const int *isrc = (const int *) src;
535 unsigned j;
536 unsigned v;
537 unsigned c;
538
539 for (j = 0; j < count; j++) {
540 for (v = 0; v < vectors; v++) {
541 for (c = 0; c < components; c++) {
542 ((float *) dst)[c] = (float) *isrc;
543 isrc++;
544 }
545
546 dst += store->vector_stride;
547 }
548
549 dst += extra_stride;
550 }
551 break;
552 }
553
554 case uniform_bool_int_0_not0: {
555 const int *isrc = (const int *) src;
556 unsigned j;
557 unsigned v;
558 unsigned c;
559
560 for (j = 0; j < count; j++) {
561 for (v = 0; v < vectors; v++) {
562 for (c = 0; c < components; c++) {
563 ((int *) dst)[c] = *isrc == 0 ? 0 : ~0;
564 isrc++;
565 }
566
567 dst += store->vector_stride;
568 }
569
570 dst += extra_stride;
571 }
572 break;
573 }
574
575 default:
576 assert(!"Should not get here.");
577 break;
578 }
579 }
580 }
581
582 /**
583 * Called via glUniform*() functions.
584 */
585 extern "C" void
586 _mesa_uniform(struct gl_context *ctx, struct gl_shader_program *shProg,
587 GLint location, GLsizei count,
588 const GLvoid *values, GLenum type)
589 {
590 unsigned loc, offset;
591 unsigned components;
592 unsigned src_components;
593 enum glsl_base_type basicType;
594 struct gl_uniform_storage *uni;
595
596 ASSERT_OUTSIDE_BEGIN_END(ctx);
597
598 if (!validate_uniform_parameters(ctx, shProg, location, count,
599 &loc, &offset, "glUniform", false))
600 return;
601
602 uni = &shProg->UniformStorage[loc];
603
604 /* Verify that the types are compatible.
605 */
606 switch (type) {
607 case GL_FLOAT:
608 basicType = GLSL_TYPE_FLOAT;
609 src_components = 1;
610 break;
611 case GL_FLOAT_VEC2:
612 basicType = GLSL_TYPE_FLOAT;
613 src_components = 2;
614 break;
615 case GL_FLOAT_VEC3:
616 basicType = GLSL_TYPE_FLOAT;
617 src_components = 3;
618 break;
619 case GL_FLOAT_VEC4:
620 basicType = GLSL_TYPE_FLOAT;
621 src_components = 4;
622 break;
623 case GL_UNSIGNED_INT:
624 basicType = GLSL_TYPE_UINT;
625 src_components = 1;
626 break;
627 case GL_UNSIGNED_INT_VEC2:
628 basicType = GLSL_TYPE_UINT;
629 src_components = 2;
630 break;
631 case GL_UNSIGNED_INT_VEC3:
632 basicType = GLSL_TYPE_UINT;
633 src_components = 3;
634 break;
635 case GL_UNSIGNED_INT_VEC4:
636 basicType = GLSL_TYPE_UINT;
637 src_components = 4;
638 break;
639 case GL_INT:
640 basicType = GLSL_TYPE_INT;
641 src_components = 1;
642 break;
643 case GL_INT_VEC2:
644 basicType = GLSL_TYPE_INT;
645 src_components = 2;
646 break;
647 case GL_INT_VEC3:
648 basicType = GLSL_TYPE_INT;
649 src_components = 3;
650 break;
651 case GL_INT_VEC4:
652 basicType = GLSL_TYPE_INT;
653 src_components = 4;
654 break;
655 case GL_BOOL:
656 case GL_BOOL_VEC2:
657 case GL_BOOL_VEC3:
658 case GL_BOOL_VEC4:
659 case GL_FLOAT_MAT2:
660 case GL_FLOAT_MAT2x3:
661 case GL_FLOAT_MAT2x4:
662 case GL_FLOAT_MAT3x2:
663 case GL_FLOAT_MAT3:
664 case GL_FLOAT_MAT3x4:
665 case GL_FLOAT_MAT4x2:
666 case GL_FLOAT_MAT4x3:
667 case GL_FLOAT_MAT4:
668 default:
669 _mesa_problem(NULL, "Invalid type in %s", __func__);
670 return;
671 }
672
673 if (uni->type->is_sampler()) {
674 components = 1;
675 } else {
676 components = uni->type->vector_elements;
677 }
678
679 bool match;
680 switch (uni->type->base_type) {
681 case GLSL_TYPE_BOOL:
682 match = true;
683 break;
684 case GLSL_TYPE_SAMPLER:
685 match = (basicType == GLSL_TYPE_INT);
686 break;
687 default:
688 match = (basicType == uni->type->base_type);
689 break;
690 }
691
692 if (uni->type->is_matrix() || components != src_components || !match) {
693 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
694 return;
695 }
696
697 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
698 log_uniform(values, basicType, components, 1, count,
699 false, shProg, location, uni);
700 }
701
702 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
703 *
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."
707 *
708 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
709 * the PDF) says:
710 *
711 * "Error Description Offending command
712 * ignored?
713 * ...
714 * INVALID_VALUE Numeric argument out of range Yes"
715 *
716 * Based on that, when an invalid sampler is specified, we generate a
717 * GL_INVALID_VALUE error and ignore the command.
718 */
719 if (uni->type->is_sampler()) {
720 int i;
721
722 for (i = 0; i < count; i++) {
723 const unsigned texUnit = ((unsigned *) values)[i];
724
725 /* check that the sampler (tex unit index) is legal */
726 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
727 _mesa_error(ctx, GL_INVALID_VALUE,
728 "glUniform1i(invalid sampler/tex unit index for "
729 "uniform %d)",
730 location);
731 return;
732 }
733 }
734 }
735
736 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
737 *
738 * "When loading N elements starting at an arbitrary position k in a
739 * uniform declared as an array, elements k through k + N - 1 in the
740 * array will be replaced with the new values. Values for any array
741 * element that exceeds the highest array element index used, as
742 * reported by GetActiveUniform, will be ignored by the GL."
743 *
744 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
745 * will have already generated an error.
746 */
747 if (uni->array_elements != 0) {
748 count = MIN2(count, (int) (uni->array_elements - offset));
749 }
750
751 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
752
753 /* Store the data in the "actual type" backing storage for the uniform.
754 */
755 if (!uni->type->is_boolean()) {
756 memcpy(&uni->storage[components * offset], values,
757 sizeof(uni->storage[0]) * components * count);
758 } else {
759 const union gl_constant_value *src =
760 (const union gl_constant_value *) values;
761 union gl_constant_value *dst = &uni->storage[components * offset];
762 const unsigned elems = components * count;
763 unsigned i;
764
765 for (i = 0; i < elems; i++) {
766 if (basicType == GLSL_TYPE_FLOAT) {
767 dst[i].i = src[i].f != 0.0f ? 1 : 0;
768 } else {
769 dst[i].i = src[i].i != 0 ? 1 : 0;
770 }
771 }
772 }
773
774 uni->initialized = true;
775
776 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
777
778 /* If the uniform is a sampler, do the extra magic necessary to propagate
779 * the changes through.
780 */
781 if (uni->type->is_sampler()) {
782 int i;
783
784 for (i = 0; i < count; i++) {
785 shProg->SamplerUnits[uni->sampler + offset + i] =
786 ((unsigned *) values)[i];
787 }
788
789 bool flushed = false;
790 for (i = 0; i < MESA_SHADER_TYPES; i++) {
791 struct gl_shader *const sh = shProg->_LinkedShaders[i];
792
793 /* If the shader stage doesn't use any samplers, don't bother
794 * checking if any samplers have changed.
795 */
796 if (sh == NULL || sh->active_samplers == 0)
797 continue;
798
799 struct gl_program *const prog = sh->Program;
800
801 assert(sizeof(prog->SamplerUnits) == sizeof(shProg->SamplerUnits));
802
803 /* Determine if any of the samplers used by this shader stage have
804 * been modified.
805 */
806 bool changed = false;
807 for (unsigned j = 0; j < Elements(prog->SamplerUnits); j++) {
808 if ((sh->active_samplers & (1U << j)) != 0
809 && (prog->SamplerUnits[j] != shProg->SamplerUnits[j])) {
810 changed = true;
811 break;
812 }
813 }
814
815 if (changed) {
816 if (!flushed) {
817 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
818 flushed = true;
819 }
820
821 memcpy(prog->SamplerUnits,
822 shProg->SamplerUnits,
823 sizeof(shProg->SamplerUnits));
824
825 _mesa_update_shader_textures_used(shProg, prog);
826 if (ctx->Driver.SamplerUniformChange)
827 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
828 }
829 }
830 }
831 }
832
833 /**
834 * Called by glUniformMatrix*() functions.
835 * Note: cols=2, rows=4 ==> array[2] of vec4
836 */
837 extern "C" void
838 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
839 GLuint cols, GLuint rows,
840 GLint location, GLsizei count,
841 GLboolean transpose, const GLfloat *values)
842 {
843 unsigned loc, offset;
844 unsigned vectors;
845 unsigned components;
846 unsigned elements;
847 struct gl_uniform_storage *uni;
848
849 ASSERT_OUTSIDE_BEGIN_END(ctx);
850
851 if (!validate_uniform_parameters(ctx, shProg, location, count,
852 &loc, &offset, "glUniformMatrix", false))
853 return;
854
855 uni = &shProg->UniformStorage[loc];
856 if (!uni->type->is_matrix()) {
857 _mesa_error(ctx, GL_INVALID_OPERATION,
858 "glUniformMatrix(non-matrix uniform)");
859 return;
860 }
861
862 assert(!uni->type->is_sampler());
863 vectors = uni->type->matrix_columns;
864 components = uni->type->vector_elements;
865
866 /* Verify that the types are compatible. This is greatly simplified for
867 * matrices because they can only have a float base type.
868 */
869 if (vectors != cols || components != rows) {
870 _mesa_error(ctx, GL_INVALID_OPERATION,
871 "glUniformMatrix(matrix size mismatch)");
872 return;
873 }
874
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)) {
879 if (transpose) {
880 _mesa_error(ctx, GL_INVALID_VALUE,
881 "glUniformMatrix(matrix transpose is not GL_FALSE)");
882 return;
883 }
884 }
885
886 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
887 log_uniform(values, GLSL_TYPE_FLOAT, components, vectors, count,
888 bool(transpose), shProg, location, uni);
889 }
890
891 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
892 *
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."
898 *
899 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
900 * will have already generated an error.
901 */
902 if (uni->array_elements != 0) {
903 count = MIN2(count, (int) (uni->array_elements - offset));
904 }
905
906 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
907
908 /* Store the data in the "actual type" backing storage for the uniform.
909 */
910 elements = components * vectors;
911
912 if (!transpose) {
913 memcpy(&uni->storage[elements * offset], values,
914 sizeof(uni->storage[0]) * elements * count);
915 } else {
916 /* Copy and transpose the matrix.
917 */
918 const float *src = values;
919 float *dst = &uni->storage[elements * offset].f;
920
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)];
925 }
926 }
927
928 dst += elements;
929 src += elements;
930 }
931 }
932
933 uni->initialized = true;
934
935 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
936 }
937
938 /**
939 * Called via glGetUniformLocation().
940 *
941 * Returns the uniform index into UniformStorage (also the
942 * glGetActiveUniformsiv uniform index), and stores the referenced
943 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
944 * return values can be encoded into a uniform location for
945 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
946 */
947 extern "C" unsigned
948 _mesa_get_uniform_location(struct gl_context *ctx,
949 struct gl_shader_program *shProg,
950 const GLchar *name,
951 unsigned *out_offset)
952 {
953 const size_t len = strlen(name);
954 long offset;
955 bool array_lookup;
956 char *name_copy;
957
958 /* If the name ends with a ']', assume that it refers to some element of an
959 * array. Malformed array references will fail the hash table look up
960 * below, so it doesn't matter that they are not caught here. This code
961 * only wants to catch the "leaf" array references so that arrays of
962 * structures containing arrays will be handled correctly.
963 */
964 if (name[len-1] == ']') {
965 unsigned i;
966
967 /* Walk backwards over the string looking for a non-digit character.
968 * This had better be the opening bracket for an array index.
969 *
970 * Initially, i specifies the location of the ']'. Since the string may
971 * contain only the ']' charcater, walk backwards very carefully.
972 */
973 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
974 /* empty */ ;
975
976 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
977 *
978 * "The first element of a uniform array is identified using the
979 * name of the uniform array appended with "[0]". Except if the last
980 * part of the string name indicates a uniform array, then the
981 * location of the first element of that array can be retrieved by
982 * either using the name of the uniform array, or the name of the
983 * uniform array appended with "[0]"."
984 *
985 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
986 *
987 * "name must be a null terminated string, without white space."
988 *
989 * Return an error if there is no opening '[' to match the closing ']'.
990 * An error will also be returned if there is intervening white space
991 * (or other non-digit characters) before the opening '['.
992 */
993 if ((i == 0) || name[i-1] != '[')
994 return GL_INVALID_INDEX;
995
996 /* Return an error if there are no digits between the opening '[' to
997 * match the closing ']'.
998 */
999 if (i == (len - 1))
1000 return GL_INVALID_INDEX;
1001
1002 /* Make a new string that is a copy of the old string up to (but not
1003 * including) the '[' character.
1004 */
1005 name_copy = (char *) malloc(i);
1006 memcpy(name_copy, name, i - 1);
1007 name_copy[i-1] = '\0';
1008
1009 offset = strtol(&name[i], NULL, 10);
1010 if (offset < 0) {
1011 free(name_copy);
1012 return GL_INVALID_INDEX;
1013 }
1014
1015 array_lookup = true;
1016 } else {
1017 name_copy = (char *) name;
1018 offset = 0;
1019 array_lookup = false;
1020 }
1021
1022 unsigned location = 0;
1023 const bool found = shProg->UniformHash->get(location, name_copy);
1024
1025 assert(!found
1026 || strcmp(name_copy, shProg->UniformStorage[location].name) == 0);
1027
1028 /* Free the temporary buffer *before* possibly returning an error.
1029 */
1030 if (name_copy != name)
1031 free(name_copy);
1032
1033 if (!found)
1034 return GL_INVALID_INDEX;
1035
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.
1040 */
1041 if (array_lookup
1042 && offset >= (long) shProg->UniformStorage[location].array_elements) {
1043 return GL_INVALID_INDEX;
1044 }
1045
1046 *out_offset = offset;
1047 return location;
1048 }
1049
1050 extern "C" bool
1051 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1052 char *errMsg, size_t errMsgLength)
1053 {
1054 const glsl_type *unit_types[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1055
1056 memset(unit_types, 0, sizeof(unit_types));
1057
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;
1063
1064 if (!t->is_sampler())
1065 continue;
1066
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;
1070
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:
1074 *
1075 * "It is not allowed to have variables of different sampler
1076 * types pointing to the same texture image unit within a program
1077 * object."
1078 */
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);
1085 return false;
1086 }
1087 }
1088 }
1089
1090 return true;
1091 }