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