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