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