mesa: fix build error
[mesa.git] / src / mesa / main / uniform_query.cpp
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010, 2011 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * 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 dst[didx].f = *(double *)&src[sidx].f;
387 break;
388 default:
389 assert(!"Should not get here.");
390 break;
391 }
392 break;
393 case GLSL_TYPE_DOUBLE:
394 switch (uni->type->base_type) {
395 case GLSL_TYPE_UINT:
396 *(double *)&dst[didx].f = (double) src[sidx].u;
397 break;
398 case GLSL_TYPE_INT:
399 case GLSL_TYPE_SAMPLER:
400 case GLSL_TYPE_IMAGE:
401 *(double *)&dst[didx].f = (double) src[sidx].i;
402 break;
403 case GLSL_TYPE_BOOL:
404 *(double *)&dst[didx].f = src[sidx].i ? 1.0f : 0.0f;
405 break;
406 case GLSL_TYPE_FLOAT:
407 *(double *)&dst[didx].f = (double) src[sidx].f;
408 break;
409 default:
410 assert(!"Should not get here.");
411 break;
412 }
413 break;
414 case GLSL_TYPE_INT:
415 case GLSL_TYPE_UINT:
416 switch (uni->type->base_type) {
417 case GLSL_TYPE_FLOAT:
418 /* While the GL 3.2 core spec doesn't explicitly
419 * state how conversion of float uniforms to integer
420 * values works, in section 6.2 "State Tables" on
421 * page 267 it says:
422 *
423 * "Unless otherwise specified, when floating
424 * point state is returned as integer values or
425 * integer state is returned as floating-point
426 * values it is converted in the fashion
427 * described in section 6.1.2"
428 *
429 * That section, on page 248, says:
430 *
431 * "If GetIntegerv or GetInteger64v are called,
432 * a floating-point value is rounded to the
433 * nearest integer..."
434 */
435 dst[didx].i = IROUND(src[sidx].f);
436 break;
437 case GLSL_TYPE_BOOL:
438 dst[didx].i = src[sidx].i ? 1 : 0;
439 break;
440 case GLSL_TYPE_DOUBLE:
441 dst[didx].i = IROUNDD(*(double *)&src[sidx].f);
442 break;
443 default:
444 assert(!"Should not get here.");
445 break;
446 }
447 break;
448
449 default:
450 assert(!"Should not get here.");
451 break;
452 }
453 }
454 }
455 }
456 }
457
458 static void
459 log_uniform(const void *values, enum glsl_base_type basicType,
460 unsigned rows, unsigned cols, unsigned count,
461 bool transpose,
462 const struct gl_shader_program *shProg,
463 GLint location,
464 const struct gl_uniform_storage *uni)
465 {
466
467 const union gl_constant_value *v = (const union gl_constant_value *) values;
468 const unsigned elems = rows * cols * count;
469 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
470
471 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
472 "transpose = %s) to: ",
473 shProg->Name, extra, uni->name, location, uni->type->name,
474 transpose ? "true" : "false");
475 for (unsigned i = 0; i < elems; i++) {
476 if (i != 0 && ((i % rows) == 0))
477 printf(", ");
478
479 switch (basicType) {
480 case GLSL_TYPE_UINT:
481 printf("%u ", v[i].u);
482 break;
483 case GLSL_TYPE_INT:
484 printf("%d ", v[i].i);
485 break;
486 case GLSL_TYPE_FLOAT:
487 printf("%g ", v[i].f);
488 break;
489 case GLSL_TYPE_DOUBLE:
490 printf("%g ", *(double* )&v[i * 2].f);
491 break;
492 default:
493 assert(!"Should not get here.");
494 break;
495 }
496 }
497 printf("\n");
498 fflush(stdout);
499 }
500
501 #if 0
502 static void
503 log_program_parameters(const struct gl_shader_program *shProg)
504 {
505 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
506 if (shProg->_LinkedShaders[i] == NULL)
507 continue;
508
509 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
510
511 printf("Program %d %s shader parameters:\n",
512 shProg->Name, _mesa_shader_stage_to_string(i));
513 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
514 printf("%s: %p %f %f %f %f\n",
515 prog->Parameters->Parameters[j].Name,
516 prog->Parameters->ParameterValues[j],
517 prog->Parameters->ParameterValues[j][0].f,
518 prog->Parameters->ParameterValues[j][1].f,
519 prog->Parameters->ParameterValues[j][2].f,
520 prog->Parameters->ParameterValues[j][3].f);
521 }
522 }
523 fflush(stdout);
524 }
525 #endif
526
527 /**
528 * Propagate some values from uniform backing storage to driver storage
529 *
530 * Values propagated from uniform backing storage to driver storage
531 * have all format / type conversions previously requested by the
532 * driver applied. This function is most often called by the
533 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
534 * etc.
535 *
536 * \param uni Uniform whose data is to be propagated to driver storage
537 * \param array_index If \c uni is an array, this is the element of
538 * the array to be propagated.
539 * \param count Number of array elements to propagate.
540 */
541 extern "C" void
542 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
543 unsigned array_index,
544 unsigned count)
545 {
546 unsigned i;
547
548 /* vector_elements and matrix_columns can be 0 for samplers.
549 */
550 const unsigned components = MAX2(1, uni->type->vector_elements);
551 const unsigned vectors = MAX2(1, uni->type->matrix_columns);
552 const int dmul = uni->type->is_64bit() ? 2 : 1;
553
554 /* Store the data in the driver's requested type in the driver's storage
555 * areas.
556 */
557 unsigned src_vector_byte_stride = components * 4 * dmul;
558
559 for (i = 0; i < uni->num_driver_storage; i++) {
560 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
561 uint8_t *dst = (uint8_t *) store->data;
562 const unsigned extra_stride =
563 store->element_stride - (vectors * store->vector_stride);
564 const uint8_t *src =
565 (uint8_t *) (&uni->storage[array_index * (dmul * components * vectors)].i);
566
567 #if 0
568 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
569 "extra_stride=%u\n",
570 __func__, dst, array_index, components,
571 vectors, count, store->vector_stride, extra_stride);
572 #endif
573
574 dst += array_index * store->element_stride;
575
576 switch (store->format) {
577 case uniform_native: {
578 unsigned j;
579 unsigned v;
580
581 for (j = 0; j < count; j++) {
582 for (v = 0; v < vectors; v++) {
583 memcpy(dst, src, src_vector_byte_stride);
584 src += src_vector_byte_stride;
585 dst += store->vector_stride;
586 }
587
588 dst += extra_stride;
589 }
590 break;
591 }
592
593 case uniform_int_float: {
594 const int *isrc = (const int *) src;
595 unsigned j;
596 unsigned v;
597 unsigned c;
598
599 for (j = 0; j < count; j++) {
600 for (v = 0; v < vectors; v++) {
601 for (c = 0; c < components; c++) {
602 ((float *) dst)[c] = (float) *isrc;
603 isrc++;
604 }
605
606 dst += store->vector_stride;
607 }
608
609 dst += extra_stride;
610 }
611 break;
612 }
613
614 default:
615 assert(!"Should not get here.");
616 break;
617 }
618 }
619 }
620
621
622 /**
623 * Return printable string for a given GLSL_TYPE_x
624 */
625 static const char *
626 glsl_type_name(enum glsl_base_type type)
627 {
628 switch (type) {
629 case GLSL_TYPE_UINT:
630 return "uint";
631 case GLSL_TYPE_INT:
632 return "int";
633 case GLSL_TYPE_FLOAT:
634 return "float";
635 case GLSL_TYPE_DOUBLE:
636 return "double";
637 case GLSL_TYPE_BOOL:
638 return "bool";
639 case GLSL_TYPE_SAMPLER:
640 return "sampler";
641 case GLSL_TYPE_IMAGE:
642 return "image";
643 case GLSL_TYPE_ATOMIC_UINT:
644 return "atomic_uint";
645 case GLSL_TYPE_STRUCT:
646 return "struct";
647 case GLSL_TYPE_INTERFACE:
648 return "interface";
649 case GLSL_TYPE_ARRAY:
650 return "array";
651 case GLSL_TYPE_VOID:
652 return "void";
653 case GLSL_TYPE_ERROR:
654 return "error";
655 default:
656 return "other";
657 }
658 }
659
660
661 /**
662 * Called via glUniform*() functions.
663 */
664 extern "C" void
665 _mesa_uniform(struct gl_context *ctx, struct gl_shader_program *shProg,
666 GLint location, GLsizei count,
667 const GLvoid *values,
668 enum glsl_base_type basicType,
669 unsigned src_components)
670 {
671 unsigned offset;
672 int size_mul = glsl_base_type_is_64bit(basicType) ? 2 : 1;
673
674 struct gl_uniform_storage *const uni =
675 validate_uniform_parameters(ctx, shProg, location, count,
676 &offset, "glUniform");
677 if (uni == NULL)
678 return;
679
680 if (uni->type->is_matrix()) {
681 /* Can't set matrix uniforms (like mat4) with glUniform */
682 _mesa_error(ctx, GL_INVALID_OPERATION,
683 "glUniform%u(uniform \"%s\"@%d is matrix)",
684 src_components, uni->name, location);
685 return;
686 }
687
688 /* Verify that the types are compatible.
689 */
690 const unsigned components = uni->type->is_sampler()
691 ? 1 : uni->type->vector_elements;
692
693 if (components != src_components) {
694 /* glUniformN() must match float/vecN type */
695 _mesa_error(ctx, GL_INVALID_OPERATION,
696 "glUniform%u(\"%s\"@%u has %u components, not %u)",
697 src_components, uni->name, location,
698 components, src_components);
699 return;
700 }
701
702 bool match;
703 switch (uni->type->base_type) {
704 case GLSL_TYPE_BOOL:
705 match = (basicType != GLSL_TYPE_DOUBLE);
706 break;
707 case GLSL_TYPE_SAMPLER:
708 match = (basicType == GLSL_TYPE_INT);
709 break;
710 case GLSL_TYPE_IMAGE:
711 match = (basicType == GLSL_TYPE_INT && _mesa_is_desktop_gl(ctx));
712 break;
713 default:
714 match = (basicType == uni->type->base_type);
715 break;
716 }
717
718 if (!match) {
719 _mesa_error(ctx, GL_INVALID_OPERATION,
720 "glUniform%u(\"%s\"@%d is %s, not %s)",
721 src_components, uni->name, location,
722 glsl_type_name(uni->type->base_type),
723 glsl_type_name(basicType));
724 return;
725 }
726
727 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
728 log_uniform(values, basicType, components, 1, count,
729 false, shProg, location, uni);
730 }
731
732 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
733 *
734 * "Setting a sampler's value to i selects texture image unit number
735 * i. The values of i range from zero to the implementation- dependent
736 * maximum supported number of texture image units."
737 *
738 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
739 * the PDF) says:
740 *
741 * "Error Description Offending command
742 * ignored?
743 * ...
744 * INVALID_VALUE Numeric argument out of range Yes"
745 *
746 * Based on that, when an invalid sampler is specified, we generate a
747 * GL_INVALID_VALUE error and ignore the command.
748 */
749 if (uni->type->is_sampler()) {
750 for (int i = 0; i < count; i++) {
751 const unsigned texUnit = ((unsigned *) values)[i];
752
753 /* check that the sampler (tex unit index) is legal */
754 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
755 _mesa_error(ctx, GL_INVALID_VALUE,
756 "glUniform1i(invalid sampler/tex unit index for "
757 "uniform %d)",
758 location);
759 return;
760 }
761 }
762 /* We need to reset the validate flag on changes to samplers in case
763 * two different sampler types are set to the same texture unit.
764 */
765 ctx->_Shader->Validated = GL_FALSE;
766 }
767
768 if (uni->type->is_image()) {
769 for (int i = 0; i < count; i++) {
770 const int unit = ((GLint *) values)[i];
771
772 /* check that the image unit is legal */
773 if (unit < 0 || unit >= (int)ctx->Const.MaxImageUnits) {
774 _mesa_error(ctx, GL_INVALID_VALUE,
775 "glUniform1i(invalid image unit index for uniform %d)",
776 location);
777 return;
778 }
779 }
780 }
781
782 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
783 *
784 * "When loading N elements starting at an arbitrary position k in a
785 * uniform declared as an array, elements k through k + N - 1 in the
786 * array will be replaced with the new values. Values for any array
787 * element that exceeds the highest array element index used, as
788 * reported by GetActiveUniform, will be ignored by the GL."
789 *
790 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
791 * will have already generated an error.
792 */
793 if (uni->array_elements != 0) {
794 count = MIN2(count, (int) (uni->array_elements - offset));
795 }
796
797 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
798
799 /* Store the data in the "actual type" backing storage for the uniform.
800 */
801 if (!uni->type->is_boolean()) {
802 memcpy(&uni->storage[size_mul * components * offset], values,
803 sizeof(uni->storage[0]) * components * count * size_mul);
804 } else {
805 const union gl_constant_value *src =
806 (const union gl_constant_value *) values;
807 union gl_constant_value *dst = &uni->storage[components * offset];
808 const unsigned elems = components * count;
809
810 for (unsigned i = 0; i < elems; i++) {
811 if (basicType == GLSL_TYPE_FLOAT) {
812 dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
813 } else {
814 dst[i].i = src[i].i != 0 ? ctx->Const.UniformBooleanTrue : 0;
815 }
816 }
817 }
818
819 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
820
821 /* If the uniform is a sampler, do the extra magic necessary to propagate
822 * the changes through.
823 */
824 if (uni->type->is_sampler()) {
825 bool flushed = false;
826 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
827 struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
828
829 /* If the shader stage doesn't use the sampler uniform, skip this.
830 */
831 if (sh == NULL || !uni->opaque[i].active)
832 continue;
833
834 for (int j = 0; j < count; j++) {
835 sh->SamplerUnits[uni->opaque[i].index + offset + j] =
836 ((unsigned *) values)[j];
837 }
838
839 struct gl_program *const prog = sh->Program;
840
841 assert(sizeof(prog->SamplerUnits) == sizeof(sh->SamplerUnits));
842
843 /* Determine if any of the samplers used by this shader stage have
844 * been modified.
845 */
846 bool changed = false;
847 GLbitfield mask = sh->active_samplers;
848 while (mask) {
849 const int j = u_bit_scan(&mask);
850 if (prog->SamplerUnits[j] != sh->SamplerUnits[j]) {
851 changed = true;
852 break;
853 }
854 }
855
856 if (changed) {
857 if (!flushed) {
858 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
859 flushed = true;
860 }
861
862 memcpy(prog->SamplerUnits,
863 sh->SamplerUnits,
864 sizeof(sh->SamplerUnits));
865
866 _mesa_update_shader_textures_used(shProg, prog);
867 if (ctx->Driver.SamplerUniformChange)
868 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
869 }
870 }
871 }
872
873 /* If the uniform is an image, update the mapping from image
874 * uniforms to image units present in the shader data structure.
875 */
876 if (uni->type->is_image()) {
877 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
878 if (uni->opaque[i].active) {
879 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
880
881 for (int j = 0; j < count; j++)
882 sh->ImageUnits[uni->opaque[i].index + offset + j] =
883 ((GLint *) values)[j];
884 }
885 }
886
887 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
888 }
889 }
890
891 /**
892 * Called by glUniformMatrix*() functions.
893 * Note: cols=2, rows=4 ==> array[2] of vec4
894 */
895 extern "C" void
896 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
897 GLuint cols, GLuint rows,
898 GLint location, GLsizei count,
899 GLboolean transpose,
900 const GLvoid *values, enum glsl_base_type basicType)
901 {
902 unsigned offset;
903 unsigned vectors;
904 unsigned components;
905 unsigned elements;
906 int size_mul;
907 struct gl_uniform_storage *const uni =
908 validate_uniform_parameters(ctx, shProg, location, count,
909 &offset, "glUniformMatrix");
910 if (uni == NULL)
911 return;
912
913 if (!uni->type->is_matrix()) {
914 _mesa_error(ctx, GL_INVALID_OPERATION,
915 "glUniformMatrix(non-matrix uniform)");
916 return;
917 }
918
919 assert(basicType == GLSL_TYPE_FLOAT || basicType == GLSL_TYPE_DOUBLE);
920 size_mul = basicType == GLSL_TYPE_DOUBLE ? 2 : 1;
921
922 assert(!uni->type->is_sampler());
923 vectors = uni->type->matrix_columns;
924 components = uni->type->vector_elements;
925
926 /* Verify that the types are compatible. This is greatly simplified for
927 * matrices because they can only have a float base type.
928 */
929 if (vectors != cols || components != rows) {
930 _mesa_error(ctx, GL_INVALID_OPERATION,
931 "glUniformMatrix(matrix size mismatch)");
932 return;
933 }
934
935 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
936 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
937 */
938 if (transpose) {
939 if (ctx->API == API_OPENGLES2 && ctx->Version < 30) {
940 _mesa_error(ctx, GL_INVALID_VALUE,
941 "glUniformMatrix(matrix transpose is not GL_FALSE)");
942 return;
943 }
944 }
945
946 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
947 * says:
948 *
949 * "If any of the following conditions occur, an INVALID_OPERATION
950 * error is generated by the Uniform* commands, and no uniform values
951 * are changed:
952 *
953 * ...
954 *
955 * - if the uniform declared in the shader is not of type boolean and
956 * the type indicated in the name of the Uniform* command used does
957 * not match the type of the uniform"
958 *
959 * There are no Boolean matrix types, so we do not need to allow
960 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
961 */
962 if (uni->type->base_type != basicType) {
963 _mesa_error(ctx, GL_INVALID_OPERATION,
964 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
965 cols, rows, uni->name, location,
966 glsl_type_name(uni->type->base_type),
967 glsl_type_name(basicType));
968 return;
969 }
970
971 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
972 log_uniform(values, uni->type->base_type, components, vectors, count,
973 bool(transpose), shProg, location, uni);
974 }
975
976 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
977 *
978 * "When loading N elements starting at an arbitrary position k in a
979 * uniform declared as an array, elements k through k + N - 1 in the
980 * array will be replaced with the new values. Values for any array
981 * element that exceeds the highest array element index used, as
982 * reported by GetActiveUniform, will be ignored by the GL."
983 *
984 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
985 * will have already generated an error.
986 */
987 if (uni->array_elements != 0) {
988 count = MIN2(count, (int) (uni->array_elements - offset));
989 }
990
991 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
992
993 /* Store the data in the "actual type" backing storage for the uniform.
994 */
995 elements = components * vectors;
996
997 if (!transpose) {
998 memcpy(&uni->storage[size_mul * elements * offset], values,
999 sizeof(uni->storage[0]) * elements * count * size_mul);
1000 } else if (basicType == GLSL_TYPE_FLOAT) {
1001 /* Copy and transpose the matrix.
1002 */
1003 const float *src = (const float *)values;
1004 float *dst = &uni->storage[elements * offset].f;
1005
1006 for (int i = 0; i < count; i++) {
1007 for (unsigned r = 0; r < rows; r++) {
1008 for (unsigned c = 0; c < cols; c++) {
1009 dst[(c * components) + r] = src[c + (r * vectors)];
1010 }
1011 }
1012
1013 dst += elements;
1014 src += elements;
1015 }
1016 } else {
1017 assert(basicType == GLSL_TYPE_DOUBLE);
1018 const double *src = (const double *)values;
1019 double *dst = (double *)&uni->storage[elements * offset].f;
1020
1021 for (int i = 0; i < count; i++) {
1022 for (unsigned r = 0; r < rows; r++) {
1023 for (unsigned c = 0; c < cols; c++) {
1024 dst[(c * components) + r] = src[c + (r * vectors)];
1025 }
1026 }
1027
1028 dst += elements;
1029 src += elements;
1030 }
1031 }
1032
1033 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1034 }
1035
1036
1037 extern "C" bool
1038 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1039 char *errMsg, size_t errMsgLength)
1040 {
1041 /* Shader does not have samplers. */
1042 if (shProg->NumUniformStorage == 0)
1043 return true;
1044
1045 if (!shProg->SamplersValidated) {
1046 _mesa_snprintf(errMsg, errMsgLength,
1047 "active samplers with a different type "
1048 "refer to the same texture image unit");
1049 return false;
1050 }
1051 return true;
1052 }
1053
1054 extern "C" bool
1055 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object *pipeline)
1056 {
1057 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1058 * OpenGL 4.1 spec says:
1059 *
1060 * "[INVALID_OPERATION] is generated by any command that transfers
1061 * vertices to the GL if:
1062 *
1063 * ...
1064 *
1065 * - Any two active samplers in the current program object are of
1066 * different types, but refer to the same texture image unit.
1067 *
1068 * - The number of active samplers in the program exceeds the
1069 * maximum number of texture image units allowed."
1070 */
1071
1072 GLbitfield mask;
1073 GLbitfield TexturesUsed[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1074 struct gl_linked_shader *shader;
1075 unsigned active_samplers = 0;
1076 const struct gl_shader_program **shProg =
1077 (const struct gl_shader_program **) pipeline->CurrentProgram;
1078
1079
1080 memset(TexturesUsed, 0, sizeof(TexturesUsed));
1081
1082 for (unsigned idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1083 if (!shProg[idx])
1084 continue;
1085
1086 shader = shProg[idx]->_LinkedShaders[idx];
1087 if (!shader || !shader->Program)
1088 continue;
1089
1090 mask = shader->Program->SamplersUsed;
1091 while (mask) {
1092 const int s = u_bit_scan(&mask);
1093 GLuint unit = shader->SamplerUnits[s];
1094 GLuint tgt = shader->SamplerTargets[s];
1095
1096 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1097 * great job of eliminating unused uniforms currently so for now
1098 * don't throw an error if two sampler types both point to 0.
1099 */
1100 if (unit == 0)
1101 continue;
1102
1103 if (TexturesUsed[unit] & ~(1 << tgt)) {
1104 pipeline->InfoLog =
1105 ralloc_asprintf(pipeline,
1106 "Program %d: "
1107 "Texture unit %d is accessed with 2 different types",
1108 shProg[idx]->Name, unit);
1109 return false;
1110 }
1111
1112 TexturesUsed[unit] |= (1 << tgt);
1113 }
1114
1115 active_samplers += shader->num_samplers;
1116 }
1117
1118 if (active_samplers > MAX_COMBINED_TEXTURE_IMAGE_UNITS) {
1119 pipeline->InfoLog =
1120 ralloc_asprintf(pipeline,
1121 "the number of active samplers %d exceed the "
1122 "maximum %d",
1123 active_samplers, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1124 return false;
1125 }
1126
1127 return true;
1128 }