mesa: renumber remaining DD_ constants
[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_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, (int) (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_shader *const sh = shProg->_LinkedShaders[i];
695
696 /* If the shader stage doesn't use any samplers, don't bother
697 * checking if any samplers have changed.
698 */
699 if (sh == NULL || sh->active_samplers == 0)
700 continue;
701
702 struct gl_program *const prog = sh->Program;
703
704 assert(sizeof(prog->SamplerUnits) == sizeof(shProg->SamplerUnits));
705
706 /* Determine if any of the samplers used by this shader stage have
707 * been modified.
708 */
709 bool changed = false;
710 for (unsigned j = 0; j < Elements(prog->SamplerUnits); j++) {
711 if ((sh->active_samplers & (1U << j)) != 0
712 && (prog->SamplerUnits[j] != shProg->SamplerUnits[j])) {
713 changed = true;
714 break;
715 }
716 }
717
718 if (changed) {
719 if (!flushed) {
720 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
721 flushed = true;
722 }
723
724 memcpy(prog->SamplerUnits,
725 shProg->SamplerUnits,
726 sizeof(shProg->SamplerUnits));
727
728 _mesa_update_shader_textures_used(shProg, prog);
729 (void) ctx->Driver.ProgramStringNotify(ctx, prog->Target, prog);
730 }
731 }
732 }
733 }
734
735 /**
736 * Called by glUniformMatrix*() functions.
737 * Note: cols=2, rows=4 ==> array[2] of vec4
738 */
739 extern "C" void
740 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
741 GLuint cols, GLuint rows,
742 GLint location, GLsizei count,
743 GLboolean transpose, const GLfloat *values)
744 {
745 unsigned loc, offset;
746 unsigned vectors;
747 unsigned components;
748 unsigned elements;
749 struct gl_uniform_storage *uni;
750
751 ASSERT_OUTSIDE_BEGIN_END(ctx);
752
753 if (!validate_uniform_parameters(ctx, shProg, location, count,
754 &loc, &offset, "glUniformMatrix", false))
755 return;
756
757 uni = &shProg->UniformStorage[loc];
758 if (!uni->type->is_matrix()) {
759 _mesa_error(ctx, GL_INVALID_OPERATION,
760 "glUniformMatrix(non-matrix uniform)");
761 return;
762 }
763
764 assert(!uni->type->is_sampler());
765 vectors = uni->type->matrix_columns;
766 components = uni->type->vector_elements;
767
768 /* Verify that the types are compatible. This is greatly simplified for
769 * matrices because they can only have a float base type.
770 */
771 if (vectors != cols || components != rows) {
772 _mesa_error(ctx, GL_INVALID_OPERATION,
773 "glUniformMatrix(matrix size mismatch)");
774 return;
775 }
776
777 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
778 log_uniform(values, GLSL_TYPE_FLOAT, components, vectors, count,
779 bool(transpose), shProg, location, uni);
780 }
781
782 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
783 *
784 * "When loading N elements starting at an arbitrary position k in a
785 * uniform declared as an array, elements k through k + N - 1 in the
786 * array will be replaced with the new values. Values for any array
787 * element that exceeds the highest array element index used, as
788 * reported by GetActiveUniform, will be ignored by the GL."
789 *
790 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
791 * will have already generated an error.
792 */
793 if (uni->array_elements != 0) {
794 if (offset >= uni->array_elements)
795 return;
796
797 count = MIN2(count, (int) (uni->array_elements - offset));
798 }
799
800 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
801
802 /* Store the data in the "actual type" backing storage for the uniform.
803 */
804 elements = components * vectors;
805
806 if (!transpose) {
807 memcpy(&uni->storage[elements * offset], values,
808 sizeof(uni->storage[0]) * elements * count);
809 } else {
810 /* Copy and transpose the matrix.
811 */
812 const float *src = values;
813 float *dst = &uni->storage[elements * offset].f;
814
815 for (int i = 0; i < count; i++) {
816 for (unsigned r = 0; r < rows; r++) {
817 for (unsigned c = 0; c < cols; c++) {
818 dst[(c * components) + r] = src[c + (r * vectors)];
819 }
820 }
821
822 dst += elements;
823 src += elements;
824 }
825 }
826
827 uni->initialized = true;
828
829 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
830 }
831
832 /**
833 * Called via glGetUniformLocation().
834 *
835 * The return value will encode two values, the uniform location and an
836 * offset (used for arrays, structs).
837 */
838 extern "C" GLint
839 _mesa_get_uniform_location(struct gl_context *ctx,
840 struct gl_shader_program *shProg,
841 const GLchar *name)
842 {
843 const size_t len = strlen(name);
844 long offset;
845 bool array_lookup;
846 char *name_copy;
847
848 /* If the name ends with a ']', assume that it refers to some element of an
849 * array. Malformed array references will fail the hash table look up
850 * below, so it doesn't matter that they are not caught here. This code
851 * only wants to catch the "leaf" array references so that arrays of
852 * structures containing arrays will be handled correctly.
853 */
854 if (name[len-1] == ']') {
855 unsigned i;
856
857 /* Walk backwards over the string looking for a non-digit character.
858 * This had better be the opening bracket for an array index.
859 *
860 * Initially, i specifies the location of the ']'. Since the string may
861 * contain only the ']' charcater, walk backwards very carefully.
862 */
863 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
864 /* empty */ ;
865
866 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
867 *
868 * "The first element of a uniform array is identified using the
869 * name of the uniform array appended with "[0]". Except if the last
870 * part of the string name indicates a uniform array, then the
871 * location of the first element of that array can be retrieved by
872 * either using the name of the uniform array, or the name of the
873 * uniform array appended with "[0]"."
874 *
875 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
876 *
877 * "name must be a null terminated string, without white space."
878 *
879 * Return an error if there is no opening '[' to match the closing ']'.
880 * An error will also be returned if there is intervening white space
881 * (or other non-digit characters) before the opening '['.
882 */
883 if ((i == 0) || name[i-1] != '[')
884 return -1;
885
886 /* Return an error if there are no digits between the opening '[' to
887 * match the closing ']'.
888 */
889 if (i == (len - 1))
890 return -1;
891
892 /* Make a new string that is a copy of the old string up to (but not
893 * including) the '[' character.
894 */
895 name_copy = (char *) malloc(i);
896 memcpy(name_copy, name, i - 1);
897 name_copy[i-1] = '\0';
898
899 offset = strtol(&name[i], NULL, 10);
900 if (offset < 0)
901 return -1;
902
903 array_lookup = true;
904 } else {
905 name_copy = (char *) name;
906 offset = 0;
907 array_lookup = false;
908 }
909
910 unsigned location = 0;
911 const bool found = shProg->UniformHash->get(location, name_copy);
912
913 assert(!found
914 || strcmp(name_copy, shProg->UniformStorage[location].name) == 0);
915
916 /* Free the temporary buffer *before* possibly returning an error.
917 */
918 if (name_copy != name)
919 free(name_copy);
920
921 if (!found)
922 return -1;
923
924 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
925 * to (correctly) fail if 'a' is not an array.
926 */
927 if (array_lookup && shProg->UniformStorage[location].array_elements == 0) {
928 return -1;
929 }
930
931 return _mesa_uniform_merge_location_offset(location, offset);
932 }
933
934 extern "C" bool
935 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
936 char *errMsg, size_t errMsgLength)
937 {
938 const glsl_type *unit_types[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
939
940 memset(unit_types, 0, sizeof(unit_types));
941
942 for (unsigned i = 0; i < shProg->NumUserUniformStorage; i++) {
943 const struct gl_uniform_storage *const storage =
944 &shProg->UniformStorage[i];
945 const glsl_type *const t = (storage->type->is_array())
946 ? storage->type->fields.array : storage->type;
947
948 if (!t->is_sampler())
949 continue;
950
951 const unsigned count = MAX2(1, storage->type->array_size());
952 for (unsigned j = 0; j < count; j++) {
953 const unsigned unit = storage->storage[j].i;
954
955 /* The types of the samplers associated with a particular texture
956 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
957 * OpenGL 3.3 core spec says:
958 *
959 * "It is not allowed to have variables of different sampler
960 * types pointing to the same texture image unit within a program
961 * object."
962 */
963 if (unit_types[unit] == NULL) {
964 unit_types[unit] = t;
965 } else if (unit_types[unit] != t) {
966 _mesa_snprintf(errMsg, errMsgLength,
967 "Texture unit %d is accessed both as %s and %s",
968 unit, unit_types[unit]->name, t->name);
969 return false;
970 }
971 }
972 }
973
974 return true;
975 }