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