glsl: set vector_elements to 1 for samplers
[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 #include <inttypes.h> /* for PRIx64 macro */
29
30 #include "main/core.h"
31 #include "main/context.h"
32 #include "main/shaderapi.h"
33 #include "main/shaderobj.h"
34 #include "main/uniforms.h"
35 #include "compiler/glsl/ir.h"
36 #include "compiler/glsl/ir_uniform.h"
37 #include "compiler/glsl/glsl_parser_extras.h"
38 #include "compiler/glsl/program.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(GLint location, GLsizei count,
161 unsigned *array_index,
162 struct gl_context *ctx,
163 struct gl_shader_program *shProg,
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->data->LinkStatus check out of the main path.
184 */
185 if (unlikely(location >= (GLint) shProg->NumUniformRemapTable)) {
186 if (!shProg->data->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->data->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(location, 1, &offset,
289 ctx, shProg, "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->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->is_sampler()
358 || uni->type->is_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 const unsigned components = uni->type->vector_elements;
651 const unsigned vectors = uni->type->matrix_columns;
652 const int dmul = uni->type->is_64bit() ? 2 : 1;
653
654 /* Store the data in the driver's requested type in the driver's storage
655 * areas.
656 */
657 unsigned src_vector_byte_stride = components * 4 * dmul;
658
659 for (i = 0; i < uni->num_driver_storage; i++) {
660 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
661 uint8_t *dst = (uint8_t *) store->data;
662 const unsigned extra_stride =
663 store->element_stride - (vectors * store->vector_stride);
664 const uint8_t *src =
665 (uint8_t *) (&uni->storage[array_index * (dmul * components * vectors)].i);
666
667 #if 0
668 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
669 "extra_stride=%u\n",
670 __func__, dst, array_index, components,
671 vectors, count, store->vector_stride, extra_stride);
672 #endif
673
674 dst += array_index * store->element_stride;
675
676 switch (store->format) {
677 case uniform_native: {
678 unsigned j;
679 unsigned v;
680
681 if (src_vector_byte_stride == store->vector_stride) {
682 if (extra_stride) {
683 for (j = 0; j < count; j++) {
684 memcpy(dst, src, src_vector_byte_stride * vectors);
685 src += src_vector_byte_stride * vectors;
686 dst += store->vector_stride * vectors;
687
688 dst += extra_stride;
689 }
690 } else {
691 /* Unigine Heaven benchmark gets here */
692 memcpy(dst, src, src_vector_byte_stride * vectors * count);
693 src += src_vector_byte_stride * vectors * count;
694 dst += store->vector_stride * vectors * count;
695 }
696 } else {
697 for (j = 0; j < count; j++) {
698 for (v = 0; v < vectors; v++) {
699 memcpy(dst, src, src_vector_byte_stride);
700 src += src_vector_byte_stride;
701 dst += store->vector_stride;
702 }
703
704 dst += extra_stride;
705 }
706 }
707 break;
708 }
709
710 case uniform_int_float: {
711 const int *isrc = (const int *) src;
712 unsigned j;
713 unsigned v;
714 unsigned c;
715
716 for (j = 0; j < count; j++) {
717 for (v = 0; v < vectors; v++) {
718 for (c = 0; c < components; c++) {
719 ((float *) dst)[c] = (float) *isrc;
720 isrc++;
721 }
722
723 dst += store->vector_stride;
724 }
725
726 dst += extra_stride;
727 }
728 break;
729 }
730
731 default:
732 assert(!"Should not get here.");
733 break;
734 }
735 }
736 }
737
738
739 /**
740 * Return printable string for a given GLSL_TYPE_x
741 */
742 static const char *
743 glsl_type_name(enum glsl_base_type type)
744 {
745 switch (type) {
746 case GLSL_TYPE_UINT:
747 return "uint";
748 case GLSL_TYPE_INT:
749 return "int";
750 case GLSL_TYPE_FLOAT:
751 return "float";
752 case GLSL_TYPE_DOUBLE:
753 return "double";
754 case GLSL_TYPE_UINT64:
755 return "uint64";
756 case GLSL_TYPE_INT64:
757 return "int64";
758 case GLSL_TYPE_BOOL:
759 return "bool";
760 case GLSL_TYPE_SAMPLER:
761 return "sampler";
762 case GLSL_TYPE_IMAGE:
763 return "image";
764 case GLSL_TYPE_ATOMIC_UINT:
765 return "atomic_uint";
766 case GLSL_TYPE_STRUCT:
767 return "struct";
768 case GLSL_TYPE_INTERFACE:
769 return "interface";
770 case GLSL_TYPE_ARRAY:
771 return "array";
772 case GLSL_TYPE_VOID:
773 return "void";
774 case GLSL_TYPE_ERROR:
775 return "error";
776 default:
777 return "other";
778 }
779 }
780
781
782 static struct gl_uniform_storage *
783 validate_uniform(GLint location, GLsizei count, const GLvoid *values,
784 unsigned *offset, struct gl_context *ctx,
785 struct gl_shader_program *shProg,
786 enum glsl_base_type basicType, unsigned src_components)
787 {
788 struct gl_uniform_storage *const uni =
789 validate_uniform_parameters(location, count, offset,
790 ctx, shProg, "glUniform");
791 if (uni == NULL)
792 return NULL;
793
794 if (uni->type->is_matrix()) {
795 /* Can't set matrix uniforms (like mat4) with glUniform */
796 _mesa_error(ctx, GL_INVALID_OPERATION,
797 "glUniform%u(uniform \"%s\"@%d is matrix)",
798 src_components, uni->name, location);
799 return NULL;
800 }
801
802 /* Verify that the types are compatible. */
803 const unsigned components = uni->type->vector_elements;
804
805 if (components != src_components) {
806 /* glUniformN() must match float/vecN type */
807 _mesa_error(ctx, GL_INVALID_OPERATION,
808 "glUniform%u(\"%s\"@%u has %u components, not %u)",
809 src_components, uni->name, location,
810 components, src_components);
811 return NULL;
812 }
813
814 bool match;
815 switch (uni->type->base_type) {
816 case GLSL_TYPE_BOOL:
817 match = (basicType != GLSL_TYPE_DOUBLE);
818 break;
819 case GLSL_TYPE_SAMPLER:
820 match = (basicType == GLSL_TYPE_INT);
821 break;
822 case GLSL_TYPE_IMAGE:
823 match = (basicType == GLSL_TYPE_INT && _mesa_is_desktop_gl(ctx));
824 break;
825 default:
826 match = (basicType == uni->type->base_type);
827 break;
828 }
829
830 if (!match) {
831 _mesa_error(ctx, GL_INVALID_OPERATION,
832 "glUniform%u(\"%s\"@%d is %s, not %s)",
833 src_components, uni->name, location,
834 glsl_type_name(uni->type->base_type),
835 glsl_type_name(basicType));
836 return NULL;
837 }
838
839 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
840 log_uniform(values, basicType, components, 1, count,
841 false, shProg, location, uni);
842 }
843
844 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
845 *
846 * "Setting a sampler's value to i selects texture image unit number
847 * i. The values of i range from zero to the implementation- dependent
848 * maximum supported number of texture image units."
849 *
850 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
851 * the PDF) says:
852 *
853 * "Error Description Offending command
854 * ignored?
855 * ...
856 * INVALID_VALUE Numeric argument out of range Yes"
857 *
858 * Based on that, when an invalid sampler is specified, we generate a
859 * GL_INVALID_VALUE error and ignore the command.
860 */
861 if (uni->type->is_sampler()) {
862 for (int i = 0; i < count; i++) {
863 const unsigned texUnit = ((unsigned *) values)[i];
864
865 /* check that the sampler (tex unit index) is legal */
866 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
867 _mesa_error(ctx, GL_INVALID_VALUE,
868 "glUniform1i(invalid sampler/tex unit index for "
869 "uniform %d)", location);
870 return NULL;
871 }
872 }
873 /* We need to reset the validate flag on changes to samplers in case
874 * two different sampler types are set to the same texture unit.
875 */
876 ctx->_Shader->Validated = GL_FALSE;
877 }
878
879 if (uni->type->is_image()) {
880 for (int i = 0; i < count; i++) {
881 const int unit = ((GLint *) values)[i];
882
883 /* check that the image unit is legal */
884 if (unit < 0 || unit >= (int)ctx->Const.MaxImageUnits) {
885 _mesa_error(ctx, GL_INVALID_VALUE,
886 "glUniform1i(invalid image unit index for uniform %d)",
887 location);
888 return NULL;
889 }
890 }
891 }
892
893 return uni;
894 }
895
896
897 /**
898 * Called via glUniform*() functions.
899 */
900 extern "C" void
901 _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
902 struct gl_context *ctx, struct gl_shader_program *shProg,
903 enum glsl_base_type basicType, unsigned src_components)
904 {
905 unsigned offset;
906 int size_mul = glsl_base_type_is_64bit(basicType) ? 2 : 1;
907
908 struct gl_uniform_storage *uni;
909 if (_mesa_is_no_error_enabled(ctx)) {
910 uni = shProg->UniformRemapTable[location];
911
912 /* The array index specified by the uniform location is just the
913 * uniform location minus the base location of of the uniform.
914 */
915 assert(uni->array_elements > 0 || location == (int)uni->remap_location);
916 offset = location - uni->remap_location;
917 } else {
918 uni = validate_uniform(location, count, values, &offset, ctx, shProg,
919 basicType, src_components);
920 if (!uni)
921 return;
922 }
923
924 const unsigned components = uni->type->vector_elements;
925
926 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
927 *
928 * "When loading N elements starting at an arbitrary position k in a
929 * uniform declared as an array, elements k through k + N - 1 in the
930 * array will be replaced with the new values. Values for any array
931 * element that exceeds the highest array element index used, as
932 * reported by GetActiveUniform, will be ignored by the GL."
933 *
934 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
935 * will have already generated an error.
936 */
937 if (uni->array_elements != 0) {
938 count = MIN2(count, (int) (uni->array_elements - offset));
939 }
940
941 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
942
943 /* Store the data in the "actual type" backing storage for the uniform.
944 */
945 if (!uni->type->is_boolean()) {
946 memcpy(&uni->storage[size_mul * components * offset], values,
947 sizeof(uni->storage[0]) * components * count * size_mul);
948 } else {
949 const union gl_constant_value *src =
950 (const union gl_constant_value *) values;
951 union gl_constant_value *dst = &uni->storage[components * offset];
952 const unsigned elems = components * count;
953
954 for (unsigned i = 0; i < elems; i++) {
955 if (basicType == GLSL_TYPE_FLOAT) {
956 dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
957 } else {
958 dst[i].i = src[i].i != 0 ? ctx->Const.UniformBooleanTrue : 0;
959 }
960 }
961 }
962
963 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
964
965 /* If the uniform is a sampler, do the extra magic necessary to propagate
966 * the changes through.
967 */
968 if (uni->type->is_sampler()) {
969 bool flushed = false;
970 shProg->SamplersValidated = GL_TRUE;
971
972 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
973 struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
974
975 /* If the shader stage doesn't use the sampler uniform, skip this. */
976 if (!uni->opaque[i].active)
977 continue;
978
979 bool changed = false;
980 for (int j = 0; j < count; j++) {
981 unsigned unit = uni->opaque[i].index + offset + j;
982 if (sh->Program->SamplerUnits[unit] != ((unsigned *) values)[j]) {
983 sh->Program->SamplerUnits[unit] = ((unsigned *) values)[j];
984 changed = true;
985 }
986 }
987
988 if (changed) {
989 if (!flushed) {
990 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT | _NEW_PROGRAM);
991 flushed = true;
992 }
993
994 struct gl_program *const prog = sh->Program;
995 _mesa_update_shader_textures_used(shProg, prog);
996 if (ctx->Driver.SamplerUniformChange)
997 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
998 }
999 }
1000 }
1001
1002 /* If the uniform is an image, update the mapping from image
1003 * uniforms to image units present in the shader data structure.
1004 */
1005 if (uni->type->is_image()) {
1006 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1007 if (uni->opaque[i].active) {
1008 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
1009
1010 for (int j = 0; j < count; j++)
1011 sh->Program->sh.ImageUnits[uni->opaque[i].index + offset + j] =
1012 ((GLint *) values)[j];
1013 }
1014 }
1015
1016 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
1017 }
1018 }
1019
1020 /**
1021 * Called by glUniformMatrix*() functions.
1022 * Note: cols=2, rows=4 ==> array[2] of vec4
1023 */
1024 extern "C" void
1025 _mesa_uniform_matrix(GLint location, GLsizei count,
1026 GLboolean transpose, const void *values,
1027 struct gl_context *ctx, struct gl_shader_program *shProg,
1028 GLuint cols, GLuint rows, enum glsl_base_type basicType)
1029 {
1030 unsigned offset;
1031 struct gl_uniform_storage *const uni =
1032 validate_uniform_parameters(location, count, &offset,
1033 ctx, shProg, "glUniformMatrix");
1034 if (uni == NULL)
1035 return;
1036
1037 if (!uni->type->is_matrix()) {
1038 _mesa_error(ctx, GL_INVALID_OPERATION,
1039 "glUniformMatrix(non-matrix uniform)");
1040 return;
1041 }
1042
1043 assert(basicType == GLSL_TYPE_FLOAT || basicType == GLSL_TYPE_DOUBLE);
1044 const unsigned size_mul = basicType == GLSL_TYPE_DOUBLE ? 2 : 1;
1045
1046 assert(!uni->type->is_sampler());
1047 const unsigned vectors = uni->type->matrix_columns;
1048 const unsigned components = uni->type->vector_elements;
1049
1050 /* Verify that the types are compatible. This is greatly simplified for
1051 * matrices because they can only have a float base type.
1052 */
1053 if (vectors != cols || components != rows) {
1054 _mesa_error(ctx, GL_INVALID_OPERATION,
1055 "glUniformMatrix(matrix size mismatch)");
1056 return;
1057 }
1058
1059 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1060 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1061 */
1062 if (transpose) {
1063 if (ctx->API == API_OPENGLES2 && ctx->Version < 30) {
1064 _mesa_error(ctx, GL_INVALID_VALUE,
1065 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1066 return;
1067 }
1068 }
1069
1070 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1071 * says:
1072 *
1073 * "If any of the following conditions occur, an INVALID_OPERATION
1074 * error is generated by the Uniform* commands, and no uniform values
1075 * are changed:
1076 *
1077 * ...
1078 *
1079 * - if the uniform declared in the shader is not of type boolean and
1080 * the type indicated in the name of the Uniform* command used does
1081 * not match the type of the uniform"
1082 *
1083 * There are no Boolean matrix types, so we do not need to allow
1084 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1085 */
1086 if (uni->type->base_type != basicType) {
1087 _mesa_error(ctx, GL_INVALID_OPERATION,
1088 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1089 cols, rows, uni->name, location,
1090 glsl_type_name(uni->type->base_type),
1091 glsl_type_name(basicType));
1092 return;
1093 }
1094
1095 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
1096 log_uniform(values, uni->type->base_type, components, vectors, count,
1097 bool(transpose), shProg, location, uni);
1098 }
1099
1100 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1101 *
1102 * "When loading N elements starting at an arbitrary position k in a
1103 * uniform declared as an array, elements k through k + N - 1 in the
1104 * array will be replaced with the new values. Values for any array
1105 * element that exceeds the highest array element index used, as
1106 * reported by GetActiveUniform, will be ignored by the GL."
1107 *
1108 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1109 * will have already generated an error.
1110 */
1111 if (uni->array_elements != 0) {
1112 count = MIN2(count, (int) (uni->array_elements - offset));
1113 }
1114
1115 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1116
1117 /* Store the data in the "actual type" backing storage for the uniform.
1118 */
1119 const unsigned elements = components * vectors;
1120
1121 if (!transpose) {
1122 memcpy(&uni->storage[size_mul * elements * offset], values,
1123 sizeof(uni->storage[0]) * elements * count * size_mul);
1124 } else if (basicType == GLSL_TYPE_FLOAT) {
1125 /* Copy and transpose the matrix.
1126 */
1127 const float *src = (const float *)values;
1128 float *dst = &uni->storage[elements * offset].f;
1129
1130 for (int i = 0; i < count; i++) {
1131 for (unsigned r = 0; r < rows; r++) {
1132 for (unsigned c = 0; c < cols; c++) {
1133 dst[(c * components) + r] = src[c + (r * vectors)];
1134 }
1135 }
1136
1137 dst += elements;
1138 src += elements;
1139 }
1140 } else {
1141 assert(basicType == GLSL_TYPE_DOUBLE);
1142 const double *src = (const double *)values;
1143 double *dst = (double *)&uni->storage[elements * offset].f;
1144
1145 for (int i = 0; i < count; i++) {
1146 for (unsigned r = 0; r < rows; r++) {
1147 for (unsigned c = 0; c < cols; c++) {
1148 dst[(c * components) + r] = src[c + (r * vectors)];
1149 }
1150 }
1151
1152 dst += elements;
1153 src += elements;
1154 }
1155 }
1156
1157 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1158 }
1159
1160
1161 extern "C" bool
1162 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1163 char *errMsg, size_t errMsgLength)
1164 {
1165 /* Shader does not have samplers. */
1166 if (shProg->data->NumUniformStorage == 0)
1167 return true;
1168
1169 if (!shProg->SamplersValidated) {
1170 _mesa_snprintf(errMsg, errMsgLength,
1171 "active samplers with a different type "
1172 "refer to the same texture image unit");
1173 return false;
1174 }
1175 return true;
1176 }
1177
1178 extern "C" bool
1179 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object *pipeline)
1180 {
1181 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1182 * OpenGL 4.1 spec says:
1183 *
1184 * "[INVALID_OPERATION] is generated by any command that transfers
1185 * vertices to the GL if:
1186 *
1187 * ...
1188 *
1189 * - Any two active samplers in the current program object are of
1190 * different types, but refer to the same texture image unit.
1191 *
1192 * - The number of active samplers in the program exceeds the
1193 * maximum number of texture image units allowed."
1194 */
1195
1196 GLbitfield mask;
1197 GLbitfield TexturesUsed[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1198 unsigned active_samplers = 0;
1199 const struct gl_program **prog =
1200 (const struct gl_program **) pipeline->CurrentProgram;
1201
1202
1203 memset(TexturesUsed, 0, sizeof(TexturesUsed));
1204
1205 for (unsigned idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1206 if (!prog[idx])
1207 continue;
1208
1209 mask = prog[idx]->SamplersUsed;
1210 while (mask) {
1211 const int s = u_bit_scan(&mask);
1212 GLuint unit = prog[idx]->SamplerUnits[s];
1213 GLuint tgt = prog[idx]->sh.SamplerTargets[s];
1214
1215 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1216 * great job of eliminating unused uniforms currently so for now
1217 * don't throw an error if two sampler types both point to 0.
1218 */
1219 if (unit == 0)
1220 continue;
1221
1222 if (TexturesUsed[unit] & ~(1 << tgt)) {
1223 pipeline->InfoLog =
1224 ralloc_asprintf(pipeline,
1225 "Program %d: "
1226 "Texture unit %d is accessed with 2 different types",
1227 prog[idx]->Id, unit);
1228 return false;
1229 }
1230
1231 TexturesUsed[unit] |= (1 << tgt);
1232 }
1233
1234 active_samplers += prog[idx]->info.num_textures;
1235 }
1236
1237 if (active_samplers > MAX_COMBINED_TEXTURE_IMAGE_UNITS) {
1238 pipeline->InfoLog =
1239 ralloc_asprintf(pipeline,
1240 "the number of active samplers %d exceed the "
1241 "maximum %d",
1242 active_samplers, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1243 return false;
1244 }
1245
1246 return true;
1247 }