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