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