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