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