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