mesa: Pass the type to _mesa_uniform_matrix as a glsl_base_type
[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 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 if ((uni->type->base_type == GLSL_TYPE_DOUBLE &&
323 returnType != GLSL_TYPE_DOUBLE) ||
324 (uni->type->base_type != GLSL_TYPE_DOUBLE &&
325 returnType == GLSL_TYPE_DOUBLE)) {
326 _mesa_error( ctx, GL_INVALID_OPERATION,
327 "glGetnUniform*vARB(incompatible uniform types)");
328 return;
329 }
330
331 {
332 unsigned elements = (uni->type->is_sampler())
333 ? 1 : uni->type->components();
334 const int dmul = uni->type->base_type == GLSL_TYPE_DOUBLE ? 2 : 1;
335
336 /* Calculate the source base address *BEFORE* modifying elements to
337 * account for the size of the user's buffer.
338 */
339 const union gl_constant_value *const src =
340 &uni->storage[offset * elements * dmul];
341
342 assert(returnType == GLSL_TYPE_FLOAT || returnType == GLSL_TYPE_INT ||
343 returnType == GLSL_TYPE_UINT || returnType == GLSL_TYPE_DOUBLE);
344
345 /* doubles have a different size than the other 3 types */
346 unsigned bytes = sizeof(src[0]) * elements * dmul;
347 if (bufSize < 0 || bytes > (unsigned) bufSize) {
348 _mesa_error( ctx, GL_INVALID_OPERATION,
349 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
350 " but %u bytes are required)", bufSize, bytes );
351 return;
352 }
353
354 /* If the return type and the uniform's native type are "compatible,"
355 * just memcpy the data. If the types are not compatible, perform a
356 * slower convert-and-copy process.
357 */
358 if (returnType == uni->type->base_type
359 || ((returnType == GLSL_TYPE_INT
360 || returnType == GLSL_TYPE_UINT)
361 &&
362 (uni->type->base_type == GLSL_TYPE_INT
363 || uni->type->base_type == GLSL_TYPE_UINT
364 || uni->type->base_type == GLSL_TYPE_SAMPLER
365 || uni->type->base_type == GLSL_TYPE_IMAGE))) {
366 memcpy(paramsOut, src, bytes);
367 } else {
368 union gl_constant_value *const dst =
369 (union gl_constant_value *) paramsOut;
370
371 /* This code could be optimized by putting the loop inside the switch
372 * statements. However, this is not expected to be
373 * performance-critical code.
374 */
375 for (unsigned i = 0; i < elements; i++) {
376 switch (returnType) {
377 case GLSL_TYPE_FLOAT:
378 switch (uni->type->base_type) {
379 case GLSL_TYPE_UINT:
380 dst[i].f = (float) src[i].u;
381 break;
382 case GLSL_TYPE_INT:
383 case GLSL_TYPE_SAMPLER:
384 case GLSL_TYPE_IMAGE:
385 dst[i].f = (float) src[i].i;
386 break;
387 case GLSL_TYPE_BOOL:
388 dst[i].f = src[i].i ? 1.0f : 0.0f;
389 break;
390 default:
391 assert(!"Should not get here.");
392 break;
393 }
394 break;
395
396 case GLSL_TYPE_INT:
397 case GLSL_TYPE_UINT:
398 switch (uni->type->base_type) {
399 case GLSL_TYPE_FLOAT:
400 /* While the GL 3.2 core spec doesn't explicitly
401 * state how conversion of float uniforms to integer
402 * values works, in section 6.2 "State Tables" on
403 * page 267 it says:
404 *
405 * "Unless otherwise specified, when floating
406 * point state is returned as integer values or
407 * integer state is returned as floating-point
408 * values it is converted in the fashion
409 * described in section 6.1.2"
410 *
411 * That section, on page 248, says:
412 *
413 * "If GetIntegerv or GetInteger64v are called,
414 * a floating-point value is rounded to the
415 * nearest integer..."
416 */
417 dst[i].i = IROUND(src[i].f);
418 break;
419 case GLSL_TYPE_BOOL:
420 dst[i].i = src[i].i ? 1 : 0;
421 break;
422 default:
423 assert(!"Should not get here.");
424 break;
425 }
426 break;
427
428 default:
429 assert(!"Should not get here.");
430 break;
431 }
432 }
433 }
434 }
435 }
436
437 static void
438 log_uniform(const void *values, enum glsl_base_type basicType,
439 unsigned rows, unsigned cols, unsigned count,
440 bool transpose,
441 const struct gl_shader_program *shProg,
442 GLint location,
443 const struct gl_uniform_storage *uni)
444 {
445
446 const union gl_constant_value *v = (const union gl_constant_value *) values;
447 const unsigned elems = rows * cols * count;
448 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
449
450 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
451 "transpose = %s) to: ",
452 shProg->Name, extra, uni->name, location, uni->type->name,
453 transpose ? "true" : "false");
454 for (unsigned i = 0; i < elems; i++) {
455 if (i != 0 && ((i % rows) == 0))
456 printf(", ");
457
458 switch (basicType) {
459 case GLSL_TYPE_UINT:
460 printf("%u ", v[i].u);
461 break;
462 case GLSL_TYPE_INT:
463 printf("%d ", v[i].i);
464 break;
465 case GLSL_TYPE_FLOAT:
466 printf("%g ", v[i].f);
467 break;
468 case GLSL_TYPE_DOUBLE:
469 printf("%g ", *(double* )&v[i * 2].f);
470 break;
471 default:
472 assert(!"Should not get here.");
473 break;
474 }
475 }
476 printf("\n");
477 fflush(stdout);
478 }
479
480 #if 0
481 static void
482 log_program_parameters(const struct gl_shader_program *shProg)
483 {
484 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
485 if (shProg->_LinkedShaders[i] == NULL)
486 continue;
487
488 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
489
490 printf("Program %d %s shader parameters:\n",
491 shProg->Name, _mesa_shader_stage_to_string(i));
492 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
493 printf("%s: %p %f %f %f %f\n",
494 prog->Parameters->Parameters[j].Name,
495 prog->Parameters->ParameterValues[j],
496 prog->Parameters->ParameterValues[j][0].f,
497 prog->Parameters->ParameterValues[j][1].f,
498 prog->Parameters->ParameterValues[j][2].f,
499 prog->Parameters->ParameterValues[j][3].f);
500 }
501 }
502 fflush(stdout);
503 }
504 #endif
505
506 /**
507 * Propagate some values from uniform backing storage to driver storage
508 *
509 * Values propagated from uniform backing storage to driver storage
510 * have all format / type conversions previously requested by the
511 * driver applied. This function is most often called by the
512 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
513 * etc.
514 *
515 * \param uni Uniform whose data is to be propagated to driver storage
516 * \param array_index If \c uni is an array, this is the element of
517 * the array to be propagated.
518 * \param count Number of array elements to propagate.
519 */
520 extern "C" void
521 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
522 unsigned array_index,
523 unsigned count)
524 {
525 unsigned i;
526
527 /* vector_elements and matrix_columns can be 0 for samplers.
528 */
529 const unsigned components = MAX2(1, uni->type->vector_elements);
530 const unsigned vectors = MAX2(1, uni->type->matrix_columns);
531 const int dmul = uni->type->base_type == GLSL_TYPE_DOUBLE ? 2 : 1;
532
533 /* Store the data in the driver's requested type in the driver's storage
534 * areas.
535 */
536 unsigned src_vector_byte_stride = components * 4 * dmul;
537
538 for (i = 0; i < uni->num_driver_storage; i++) {
539 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
540 uint8_t *dst = (uint8_t *) store->data;
541 const unsigned extra_stride =
542 store->element_stride - (vectors * store->vector_stride);
543 const uint8_t *src =
544 (uint8_t *) (&uni->storage[array_index * (dmul * components * vectors)].i);
545
546 #if 0
547 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
548 "extra_stride=%u\n",
549 __func__, dst, array_index, components,
550 vectors, count, store->vector_stride, extra_stride);
551 #endif
552
553 dst += array_index * store->element_stride;
554
555 switch (store->format) {
556 case uniform_native: {
557 unsigned j;
558 unsigned v;
559
560 for (j = 0; j < count; j++) {
561 for (v = 0; v < vectors; v++) {
562 memcpy(dst, src, src_vector_byte_stride);
563 src += src_vector_byte_stride;
564 dst += store->vector_stride;
565 }
566
567 dst += extra_stride;
568 }
569 break;
570 }
571
572 case uniform_int_float: {
573 const int *isrc = (const int *) src;
574 unsigned j;
575 unsigned v;
576 unsigned c;
577
578 for (j = 0; j < count; j++) {
579 for (v = 0; v < vectors; v++) {
580 for (c = 0; c < components; c++) {
581 ((float *) dst)[c] = (float) *isrc;
582 isrc++;
583 }
584
585 dst += store->vector_stride;
586 }
587
588 dst += extra_stride;
589 }
590 break;
591 }
592
593 default:
594 assert(!"Should not get here.");
595 break;
596 }
597 }
598 }
599
600
601 /**
602 * Return printable string for a given GLSL_TYPE_x
603 */
604 static const char *
605 glsl_type_name(enum glsl_base_type type)
606 {
607 switch (type) {
608 case GLSL_TYPE_UINT:
609 return "uint";
610 case GLSL_TYPE_INT:
611 return "int";
612 case GLSL_TYPE_FLOAT:
613 return "float";
614 case GLSL_TYPE_DOUBLE:
615 return "double";
616 case GLSL_TYPE_BOOL:
617 return "bool";
618 case GLSL_TYPE_SAMPLER:
619 return "sampler";
620 case GLSL_TYPE_IMAGE:
621 return "image";
622 case GLSL_TYPE_ATOMIC_UINT:
623 return "atomic_uint";
624 case GLSL_TYPE_STRUCT:
625 return "struct";
626 case GLSL_TYPE_INTERFACE:
627 return "interface";
628 case GLSL_TYPE_ARRAY:
629 return "array";
630 case GLSL_TYPE_VOID:
631 return "void";
632 case GLSL_TYPE_ERROR:
633 return "error";
634 default:
635 return "other";
636 }
637 }
638
639
640 /**
641 * Called via glUniform*() functions.
642 */
643 extern "C" void
644 _mesa_uniform(struct gl_context *ctx, struct gl_shader_program *shProg,
645 GLint location, GLsizei count,
646 const GLvoid *values,
647 enum glsl_base_type basicType,
648 unsigned src_components)
649 {
650 unsigned offset;
651 int size_mul = basicType == GLSL_TYPE_DOUBLE ? 2 : 1;
652
653 struct gl_uniform_storage *const uni =
654 validate_uniform_parameters(ctx, shProg, location, count,
655 &offset, "glUniform");
656 if (uni == NULL)
657 return;
658
659 if (uni->type->is_matrix()) {
660 /* Can't set matrix uniforms (like mat4) with glUniform */
661 _mesa_error(ctx, GL_INVALID_OPERATION,
662 "glUniform%u(uniform \"%s\"@%d is matrix)",
663 src_components, uni->name, location);
664 return;
665 }
666
667 /* Verify that the types are compatible.
668 */
669 const unsigned components = uni->type->is_sampler()
670 ? 1 : uni->type->vector_elements;
671
672 if (components != src_components) {
673 /* glUniformN() must match float/vecN type */
674 _mesa_error(ctx, GL_INVALID_OPERATION,
675 "glUniform%u(\"%s\"@%u has %u components, not %u)",
676 src_components, uni->name, location,
677 components, src_components);
678 return;
679 }
680
681 bool match;
682 switch (uni->type->base_type) {
683 case GLSL_TYPE_BOOL:
684 match = (basicType != GLSL_TYPE_DOUBLE);
685 break;
686 case GLSL_TYPE_SAMPLER:
687 match = (basicType == GLSL_TYPE_INT);
688 break;
689 case GLSL_TYPE_IMAGE:
690 match = (basicType == GLSL_TYPE_INT && _mesa_is_desktop_gl(ctx));
691 break;
692 default:
693 match = (basicType == uni->type->base_type);
694 break;
695 }
696
697 if (!match) {
698 _mesa_error(ctx, GL_INVALID_OPERATION,
699 "glUniform%u(\"%s\"@%d is %s, not %s)",
700 src_components, uni->name, location,
701 glsl_type_name(uni->type->base_type),
702 glsl_type_name(basicType));
703 return;
704 }
705
706 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
707 log_uniform(values, basicType, components, 1, count,
708 false, shProg, location, uni);
709 }
710
711 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
712 *
713 * "Setting a sampler's value to i selects texture image unit number
714 * i. The values of i range from zero to the implementation- dependent
715 * maximum supported number of texture image units."
716 *
717 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
718 * the PDF) says:
719 *
720 * "Error Description Offending command
721 * ignored?
722 * ...
723 * INVALID_VALUE Numeric argument out of range Yes"
724 *
725 * Based on that, when an invalid sampler is specified, we generate a
726 * GL_INVALID_VALUE error and ignore the command.
727 */
728 if (uni->type->is_sampler()) {
729 for (int i = 0; i < count; i++) {
730 const unsigned texUnit = ((unsigned *) values)[i];
731
732 /* check that the sampler (tex unit index) is legal */
733 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
734 _mesa_error(ctx, GL_INVALID_VALUE,
735 "glUniform1i(invalid sampler/tex unit index for "
736 "uniform %d)",
737 location);
738 return;
739 }
740 }
741 }
742
743 if (uni->type->is_image()) {
744 for (int i = 0; i < count; i++) {
745 const int unit = ((GLint *) values)[i];
746
747 /* check that the image unit is legal */
748 if (unit < 0 || unit >= (int)ctx->Const.MaxImageUnits) {
749 _mesa_error(ctx, GL_INVALID_VALUE,
750 "glUniform1i(invalid image unit index for uniform %d)",
751 location);
752 return;
753 }
754 }
755 }
756
757 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
758 *
759 * "When loading N elements starting at an arbitrary position k in a
760 * uniform declared as an array, elements k through k + N - 1 in the
761 * array will be replaced with the new values. Values for any array
762 * element that exceeds the highest array element index used, as
763 * reported by GetActiveUniform, will be ignored by the GL."
764 *
765 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
766 * will have already generated an error.
767 */
768 if (uni->array_elements != 0) {
769 count = MIN2(count, (int) (uni->array_elements - offset));
770 }
771
772 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
773
774 /* Store the data in the "actual type" backing storage for the uniform.
775 */
776 if (!uni->type->is_boolean()) {
777 memcpy(&uni->storage[size_mul * components * offset], values,
778 sizeof(uni->storage[0]) * components * count * size_mul);
779 } else {
780 const union gl_constant_value *src =
781 (const union gl_constant_value *) values;
782 union gl_constant_value *dst = &uni->storage[components * offset];
783 const unsigned elems = components * count;
784
785 for (unsigned i = 0; i < elems; i++) {
786 if (basicType == GLSL_TYPE_FLOAT) {
787 dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
788 } else {
789 dst[i].i = src[i].i != 0 ? ctx->Const.UniformBooleanTrue : 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 bool flushed = false;
803 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
804 struct gl_shader *const sh = shProg->_LinkedShaders[i];
805
806 /* If the shader stage doesn't use the sampler uniform, skip this.
807 */
808 if (sh == NULL || !uni->sampler[i].active)
809 continue;
810
811 for (int j = 0; j < count; j++) {
812 sh->SamplerUnits[uni->sampler[i].index + offset + j] =
813 ((unsigned *) values)[j];
814 }
815
816 struct gl_program *const prog = sh->Program;
817
818 assert(sizeof(prog->SamplerUnits) == sizeof(sh->SamplerUnits));
819
820 /* Determine if any of the samplers used by this shader stage have
821 * been modified.
822 */
823 bool changed = false;
824 for (unsigned j = 0; j < ARRAY_SIZE(prog->SamplerUnits); j++) {
825 if ((sh->active_samplers & (1U << j)) != 0
826 && (prog->SamplerUnits[j] != sh->SamplerUnits[j])) {
827 changed = true;
828 break;
829 }
830 }
831
832 if (changed) {
833 if (!flushed) {
834 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
835 flushed = true;
836 }
837
838 memcpy(prog->SamplerUnits,
839 sh->SamplerUnits,
840 sizeof(sh->SamplerUnits));
841
842 _mesa_update_shader_textures_used(shProg, prog);
843 if (ctx->Driver.SamplerUniformChange)
844 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
845 }
846 }
847 }
848
849 /* If the uniform is an image, update the mapping from image
850 * uniforms to image units present in the shader data structure.
851 */
852 if (uni->type->is_image()) {
853 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
854 if (uni->image[i].active) {
855 struct gl_shader *sh = shProg->_LinkedShaders[i];
856
857 for (int j = 0; j < count; j++)
858 sh->ImageUnits[uni->image[i].index + offset + j] =
859 ((GLint *) values)[j];
860 }
861 }
862
863 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
864 }
865 }
866
867 /**
868 * Called by glUniformMatrix*() functions.
869 * Note: cols=2, rows=4 ==> array[2] of vec4
870 */
871 extern "C" void
872 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
873 GLuint cols, GLuint rows,
874 GLint location, GLsizei count,
875 GLboolean transpose,
876 const GLvoid *values, enum glsl_base_type basicType)
877 {
878 unsigned offset;
879 unsigned vectors;
880 unsigned components;
881 unsigned elements;
882 int size_mul;
883 struct gl_uniform_storage *const uni =
884 validate_uniform_parameters(ctx, shProg, location, count,
885 &offset, "glUniformMatrix");
886 if (uni == NULL)
887 return;
888
889 if (!uni->type->is_matrix()) {
890 _mesa_error(ctx, GL_INVALID_OPERATION,
891 "glUniformMatrix(non-matrix uniform)");
892 return;
893 }
894
895 assert(basicType == GLSL_TYPE_FLOAT || basicType == GLSL_TYPE_DOUBLE);
896 size_mul = basicType == GLSL_TYPE_DOUBLE ? 2 : 1;
897
898 assert(!uni->type->is_sampler());
899 vectors = uni->type->matrix_columns;
900 components = uni->type->vector_elements;
901
902 /* Verify that the types are compatible. This is greatly simplified for
903 * matrices because they can only have a float base type.
904 */
905 if (vectors != cols || components != rows) {
906 _mesa_error(ctx, GL_INVALID_OPERATION,
907 "glUniformMatrix(matrix size mismatch)");
908 return;
909 }
910
911 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
912 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
913 */
914 if (transpose) {
915 if (ctx->API == API_OPENGLES2 && ctx->Version < 30) {
916 _mesa_error(ctx, GL_INVALID_VALUE,
917 "glUniformMatrix(matrix transpose is not GL_FALSE)");
918 return;
919 }
920 }
921
922 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
923 log_uniform(values, uni->type->base_type, components, vectors, count,
924 bool(transpose), shProg, location, uni);
925 }
926
927 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
928 *
929 * "When loading N elements starting at an arbitrary position k in a
930 * uniform declared as an array, elements k through k + N - 1 in the
931 * array will be replaced with the new values. Values for any array
932 * element that exceeds the highest array element index used, as
933 * reported by GetActiveUniform, will be ignored by the GL."
934 *
935 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
936 * will have already generated an error.
937 */
938 if (uni->array_elements != 0) {
939 count = MIN2(count, (int) (uni->array_elements - offset));
940 }
941
942 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
943
944 /* Store the data in the "actual type" backing storage for the uniform.
945 */
946 elements = components * vectors;
947
948 if (!transpose) {
949 memcpy(&uni->storage[elements * offset], values,
950 sizeof(uni->storage[0]) * elements * count * size_mul);
951 } else if (basicType == GLSL_TYPE_FLOAT) {
952 /* Copy and transpose the matrix.
953 */
954 const float *src = (const float *)values;
955 float *dst = &uni->storage[elements * offset].f;
956
957 for (int i = 0; i < count; i++) {
958 for (unsigned r = 0; r < rows; r++) {
959 for (unsigned c = 0; c < cols; c++) {
960 dst[(c * components) + r] = src[c + (r * vectors)];
961 }
962 }
963
964 dst += elements;
965 src += elements;
966 }
967 } else {
968 assert(basicType == GLSL_TYPE_DOUBLE);
969 const double *src = (const double *)values;
970 double *dst = (double *)&uni->storage[elements * offset].f;
971
972 for (int i = 0; i < count; i++) {
973 for (unsigned r = 0; r < rows; r++) {
974 for (unsigned c = 0; c < cols; c++) {
975 dst[(c * components) + r] = src[c + (r * vectors)];
976 }
977 }
978
979 dst += elements;
980 src += elements;
981 }
982 }
983
984 uni->initialized = true;
985
986 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
987 }
988
989
990 extern "C" bool
991 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
992 char *errMsg, size_t errMsgLength)
993 {
994 /* Shader does not have samplers. */
995 if (shProg->NumUniformStorage == 0)
996 return true;
997
998 if (!shProg->SamplersValidated) {
999 _mesa_snprintf(errMsg, errMsgLength,
1000 "active samplers with a different type "
1001 "refer to the same texture image unit");
1002 return false;
1003 }
1004 return true;
1005 }
1006
1007 extern "C" bool
1008 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object *pipeline)
1009 {
1010 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1011 * OpenGL 4.1 spec says:
1012 *
1013 * "[INVALID_OPERATION] is generated by any command that transfers
1014 * vertices to the GL if:
1015 *
1016 * ...
1017 *
1018 * - Any two active samplers in the current program object are of
1019 * different types, but refer to the same texture image unit.
1020 *
1021 * - The number of active samplers in the program exceeds the
1022 * maximum number of texture image units allowed."
1023 */
1024 unsigned active_samplers = 0;
1025 const struct gl_shader_program **shProg =
1026 (const struct gl_shader_program **) pipeline->CurrentProgram;
1027
1028 const glsl_type *unit_types[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1029 memset(unit_types, 0, sizeof(unit_types));
1030
1031 for (unsigned idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1032 if (!shProg[idx])
1033 continue;
1034
1035 for (unsigned i = 0; i < shProg[idx]->NumUniformStorage; i++) {
1036 const struct gl_uniform_storage *const storage =
1037 &shProg[idx]->UniformStorage[i];
1038
1039 if (!storage->type->is_sampler())
1040 continue;
1041
1042 active_samplers++;
1043
1044 const unsigned count = MAX2(1, storage->array_elements);
1045 for (unsigned j = 0; j < count; j++) {
1046 const unsigned unit = storage->storage[j].i;
1047
1048 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1049 * great job of eliminating unused uniforms currently so for now
1050 * don't throw an error if two sampler types both point to 0.
1051 */
1052 if (unit == 0)
1053 continue;
1054
1055 /* The types of the samplers associated with a particular texture
1056 * unit must be an exact match. Page 74 (page 89 of the PDF) of
1057 * the OpenGL 3.3 core spec says:
1058 *
1059 * "It is not allowed to have variables of different sampler
1060 * types pointing to the same texture image unit within a
1061 * program object."
1062 */
1063 if (unit_types[unit] == NULL) {
1064 unit_types[unit] = storage->type;
1065 } else if (unit_types[unit] != storage->type) {
1066 pipeline->InfoLog =
1067 ralloc_asprintf(pipeline,
1068 "Texture unit %d is accessed both as %s "
1069 "and %s",
1070 unit, unit_types[unit]->name,
1071 storage->type->name);
1072 return false;
1073 }
1074 }
1075 }
1076 }
1077
1078 if (active_samplers > MAX_COMBINED_TEXTURE_IMAGE_UNITS) {
1079 pipeline->InfoLog =
1080 ralloc_asprintf(pipeline,
1081 "the number of active samplers %d exceed the "
1082 "maximum %d",
1083 active_samplers, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1084 return false;
1085 }
1086
1087 return true;
1088 }