mesa: skip FLUSH_VERTICES() if no samplers were changed
[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 rmul = glsl_base_type_is_64bit(returnType) ? 2 : 1;
326 int dmul = (uni->type->is_64bit()) ? 2 : 1;
327
328 if ((uni->type->is_sampler() || uni->type->is_image()) &&
329 !uni->is_bindless) {
330 /* Non-bindless samplers/images are represented using unsigned integer
331 * 32-bit, while bindless handles are 64-bit.
332 */
333 dmul = 1;
334 }
335
336 /* Calculate the source base address *BEFORE* modifying elements to
337 * account for the size of the user's buffer.
338 */
339 const union gl_constant_value *const src =
340 &uni->storage[offset * elements * dmul];
341
342 assert(returnType == GLSL_TYPE_FLOAT || returnType == GLSL_TYPE_INT ||
343 returnType == GLSL_TYPE_UINT || returnType == GLSL_TYPE_DOUBLE ||
344 returnType == GLSL_TYPE_UINT64 || returnType == GLSL_TYPE_INT64);
345
346 /* doubles have a different size than the other 3 types */
347 unsigned bytes = sizeof(src[0]) * elements * rmul;
348 if (bufSize < 0 || bytes > (unsigned) bufSize) {
349 _mesa_error(ctx, GL_INVALID_OPERATION,
350 "glGetnUniform*vARB(out of bounds: bufSize is %d,"
351 " but %u bytes are required)", bufSize, bytes);
352 return;
353 }
354
355 /* If the return type and the uniform's native type are "compatible,"
356 * just memcpy the data. If the types are not compatible, perform a
357 * slower convert-and-copy process.
358 */
359 if (returnType == uni->type->base_type ||
360 ((returnType == GLSL_TYPE_INT || returnType == GLSL_TYPE_UINT) &&
361 (uni->type->is_sampler() || uni->type->is_image()))) {
362 memcpy(paramsOut, src, bytes);
363 } else {
364 union gl_constant_value *const dst =
365 (union gl_constant_value *) paramsOut;
366 /* This code could be optimized by putting the loop inside the switch
367 * statements. However, this is not expected to be
368 * performance-critical code.
369 */
370 for (unsigned i = 0; i < elements; i++) {
371 int sidx = i * dmul;
372 int didx = i * rmul;
373
374 switch (returnType) {
375 case GLSL_TYPE_FLOAT:
376 switch (uni->type->base_type) {
377 case GLSL_TYPE_UINT:
378 dst[didx].f = (float) src[sidx].u;
379 break;
380 case GLSL_TYPE_INT:
381 case GLSL_TYPE_SAMPLER:
382 case GLSL_TYPE_IMAGE:
383 dst[didx].f = (float) src[sidx].i;
384 break;
385 case GLSL_TYPE_BOOL:
386 dst[didx].f = src[sidx].i ? 1.0f : 0.0f;
387 break;
388 case GLSL_TYPE_DOUBLE: {
389 double tmp;
390 memcpy(&tmp, &src[sidx].f, sizeof(tmp));
391 dst[didx].f = tmp;
392 break;
393 }
394 case GLSL_TYPE_UINT64: {
395 uint64_t tmp;
396 memcpy(&tmp, &src[sidx].u, sizeof(tmp));
397 dst[didx].f = tmp;
398 break;
399 }
400 case GLSL_TYPE_INT64: {
401 uint64_t tmp;
402 memcpy(&tmp, &src[sidx].i, sizeof(tmp));
403 dst[didx].f = tmp;
404 break;
405 }
406 default:
407 assert(!"Should not get here.");
408 break;
409 }
410 break;
411
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
458 case GLSL_TYPE_INT:
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 = (int64_t) roundf(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_UINT:
484 dst[didx].i = MIN2(src[sidx].i, INT_MAX);
485 break;
486 case GLSL_TYPE_DOUBLE: {
487 double tmp;
488 memcpy(&tmp, &src[sidx].f, sizeof(tmp));
489 dst[didx].i = (int64_t) round(tmp);
490 break;
491 }
492 case GLSL_TYPE_UINT64: {
493 uint64_t tmp;
494 memcpy(&tmp, &src[sidx].u, sizeof(tmp));
495 dst[didx].i = tmp;
496 break;
497 }
498 case GLSL_TYPE_INT64: {
499 int64_t tmp;
500 memcpy(&tmp, &src[sidx].i, sizeof(tmp));
501 dst[didx].i = tmp;
502 break;
503 }
504 default:
505 assert(!"Should not get here.");
506 break;
507 }
508 break;
509
510 case GLSL_TYPE_UINT:
511 switch (uni->type->base_type) {
512 case GLSL_TYPE_FLOAT:
513 /* The spec isn't terribly clear how to handle negative
514 * values with an unsigned return type.
515 *
516 * GL 4.5 section 2.2.2 ("Data Conversions for State
517 * Query Commands") says:
518 *
519 * "If a value is so large in magnitude that it cannot be
520 * represented by the returned data type, then the nearest
521 * value representable using the requested type is
522 * returned."
523 */
524 dst[didx].u = src[sidx].f < 0.0f ?
525 0u : (uint32_t) roundf(src[sidx].f);
526 break;
527 case GLSL_TYPE_BOOL:
528 dst[didx].i = src[sidx].i ? 1 : 0;
529 break;
530 case GLSL_TYPE_INT:
531 dst[didx].i = MAX2(src[sidx].i, 0);
532 break;
533 case GLSL_TYPE_DOUBLE: {
534 double tmp;
535 memcpy(&tmp, &src[sidx].f, sizeof(tmp));
536 dst[didx].u = tmp < 0.0 ? 0u : (uint32_t) round(tmp);
537 break;
538 }
539 case GLSL_TYPE_UINT64: {
540 uint64_t tmp;
541 memcpy(&tmp, &src[sidx].u, sizeof(tmp));
542 dst[didx].i = MIN2(tmp, INT_MAX);
543 break;
544 }
545 case GLSL_TYPE_INT64: {
546 int64_t tmp;
547 memcpy(&tmp, &src[sidx].i, sizeof(tmp));
548 dst[didx].i = MAX2(tmp, 0);
549 break;
550 }
551 default:
552 unreachable("invalid uniform type");
553 }
554 break;
555
556 case GLSL_TYPE_INT64:
557 switch (uni->type->base_type) {
558 case GLSL_TYPE_UINT: {
559 uint64_t tmp = src[sidx].u;
560 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
561 break;
562 }
563 case GLSL_TYPE_INT:
564 case GLSL_TYPE_SAMPLER:
565 case GLSL_TYPE_IMAGE: {
566 int64_t tmp = src[sidx].i;
567 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
568 break;
569 }
570 case GLSL_TYPE_BOOL: {
571 int64_t tmp = src[sidx].i ? 1.0f : 0.0f;
572 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
573 break;
574 }
575 case GLSL_TYPE_UINT64: {
576 uint64_t u64;
577 memcpy(&u64, &src[sidx].u, sizeof(u64));
578 int64_t tmp = MIN2(u64, INT_MAX);
579 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
580 break;
581 }
582 case GLSL_TYPE_FLOAT: {
583 int64_t tmp = (int64_t) roundf(src[sidx].f);
584 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
585 break;
586 }
587 case GLSL_TYPE_DOUBLE: {
588 double d;
589 memcpy(&d, &src[sidx].f, sizeof(d));
590 int64_t tmp = (int64_t) round(d);
591 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
592 break;
593 }
594 default:
595 assert(!"Should not get here.");
596 break;
597 }
598 break;
599
600 case GLSL_TYPE_UINT64:
601 switch (uni->type->base_type) {
602 case GLSL_TYPE_UINT: {
603 uint64_t tmp = src[sidx].u;
604 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
605 break;
606 }
607 case GLSL_TYPE_INT:
608 case GLSL_TYPE_SAMPLER:
609 case GLSL_TYPE_IMAGE: {
610 int64_t tmp = MAX2(src[sidx].i, 0);
611 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
612 break;
613 }
614 case GLSL_TYPE_BOOL: {
615 int64_t tmp = src[sidx].i ? 1.0f : 0.0f;
616 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
617 break;
618 }
619 case GLSL_TYPE_INT64: {
620 uint64_t i64;
621 memcpy(&i64, &src[sidx].i, sizeof(i64));
622 uint64_t tmp = MAX2(i64, 0);
623 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
624 break;
625 }
626 case GLSL_TYPE_FLOAT: {
627 uint64_t tmp = src[sidx].f < 0.0f ?
628 0ull : (uint64_t) roundf(src[sidx].f);
629 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
630 break;
631 }
632 case GLSL_TYPE_DOUBLE: {
633 double d;
634 memcpy(&d, &src[sidx].f, sizeof(d));
635 uint64_t tmp = (d < 0.0) ? 0ull : (uint64_t) round(d);
636 memcpy(&dst[didx].u, &tmp, sizeof(tmp));
637 break;
638 }
639 default:
640 assert(!"Should not get here.");
641 break;
642 }
643 break;
644
645 default:
646 assert(!"Should not get here.");
647 break;
648 }
649 }
650 }
651 }
652 }
653
654 static void
655 log_uniform(const void *values, enum glsl_base_type basicType,
656 unsigned rows, unsigned cols, unsigned count,
657 bool transpose,
658 const struct gl_shader_program *shProg,
659 GLint location,
660 const struct gl_uniform_storage *uni)
661 {
662
663 const union gl_constant_value *v = (const union gl_constant_value *) values;
664 const unsigned elems = rows * cols * count;
665 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
666
667 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
668 "transpose = %s) to: ",
669 shProg->Name, extra, uni->name, location, uni->type->name,
670 transpose ? "true" : "false");
671 for (unsigned i = 0; i < elems; i++) {
672 if (i != 0 && ((i % rows) == 0))
673 printf(", ");
674
675 switch (basicType) {
676 case GLSL_TYPE_UINT:
677 printf("%u ", v[i].u);
678 break;
679 case GLSL_TYPE_INT:
680 printf("%d ", v[i].i);
681 break;
682 case GLSL_TYPE_UINT64: {
683 uint64_t tmp;
684 memcpy(&tmp, &v[i * 2].u, sizeof(tmp));
685 printf("%" PRIu64 " ", tmp);
686 break;
687 }
688 case GLSL_TYPE_INT64: {
689 int64_t tmp;
690 memcpy(&tmp, &v[i * 2].u, sizeof(tmp));
691 printf("%" PRId64 " ", tmp);
692 break;
693 }
694 case GLSL_TYPE_FLOAT:
695 printf("%g ", v[i].f);
696 break;
697 case GLSL_TYPE_DOUBLE: {
698 double tmp;
699 memcpy(&tmp, &v[i * 2].f, sizeof(tmp));
700 printf("%g ", tmp);
701 break;
702 }
703 default:
704 assert(!"Should not get here.");
705 break;
706 }
707 }
708 printf("\n");
709 fflush(stdout);
710 }
711
712 #if 0
713 static void
714 log_program_parameters(const struct gl_shader_program *shProg)
715 {
716 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
717 if (shProg->_LinkedShaders[i] == NULL)
718 continue;
719
720 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
721
722 printf("Program %d %s shader parameters:\n",
723 shProg->Name, _mesa_shader_stage_to_string(i));
724 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
725 printf("%s: %p %f %f %f %f\n",
726 prog->Parameters->Parameters[j].Name,
727 prog->Parameters->ParameterValues[j],
728 prog->Parameters->ParameterValues[j][0].f,
729 prog->Parameters->ParameterValues[j][1].f,
730 prog->Parameters->ParameterValues[j][2].f,
731 prog->Parameters->ParameterValues[j][3].f);
732 }
733 }
734 fflush(stdout);
735 }
736 #endif
737
738 /**
739 * Propagate some values from uniform backing storage to driver storage
740 *
741 * Values propagated from uniform backing storage to driver storage
742 * have all format / type conversions previously requested by the
743 * driver applied. This function is most often called by the
744 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
745 * etc.
746 *
747 * \param uni Uniform whose data is to be propagated to driver storage
748 * \param array_index If \c uni is an array, this is the element of
749 * the array to be propagated.
750 * \param count Number of array elements to propagate.
751 */
752 extern "C" void
753 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
754 unsigned array_index,
755 unsigned count)
756 {
757 unsigned i;
758
759 const unsigned components = uni->type->vector_elements;
760 const unsigned vectors = uni->type->matrix_columns;
761 const int dmul = uni->type->is_64bit() ? 2 : 1;
762
763 /* Store the data in the driver's requested type in the driver's storage
764 * areas.
765 */
766 unsigned src_vector_byte_stride = components * 4 * dmul;
767
768 for (i = 0; i < uni->num_driver_storage; i++) {
769 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
770 uint8_t *dst = (uint8_t *) store->data;
771 const unsigned extra_stride =
772 store->element_stride - (vectors * store->vector_stride);
773 const uint8_t *src =
774 (uint8_t *) (&uni->storage[array_index * (dmul * components * vectors)].i);
775
776 #if 0
777 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
778 "extra_stride=%u\n",
779 __func__, dst, array_index, components,
780 vectors, count, store->vector_stride, extra_stride);
781 #endif
782
783 dst += array_index * store->element_stride;
784
785 switch (store->format) {
786 case uniform_native: {
787 unsigned j;
788 unsigned v;
789
790 if (src_vector_byte_stride == store->vector_stride) {
791 if (extra_stride) {
792 for (j = 0; j < count; j++) {
793 memcpy(dst, src, src_vector_byte_stride * vectors);
794 src += src_vector_byte_stride * vectors;
795 dst += store->vector_stride * vectors;
796
797 dst += extra_stride;
798 }
799 } else {
800 /* Unigine Heaven benchmark gets here */
801 memcpy(dst, src, src_vector_byte_stride * vectors * count);
802 src += src_vector_byte_stride * vectors * count;
803 dst += store->vector_stride * vectors * count;
804 }
805 } else {
806 for (j = 0; j < count; j++) {
807 for (v = 0; v < vectors; v++) {
808 memcpy(dst, src, src_vector_byte_stride);
809 src += src_vector_byte_stride;
810 dst += store->vector_stride;
811 }
812
813 dst += extra_stride;
814 }
815 }
816 break;
817 }
818
819 case uniform_int_float: {
820 const int *isrc = (const int *) src;
821 unsigned j;
822 unsigned v;
823 unsigned c;
824
825 for (j = 0; j < count; j++) {
826 for (v = 0; v < vectors; v++) {
827 for (c = 0; c < components; c++) {
828 ((float *) dst)[c] = (float) *isrc;
829 isrc++;
830 }
831
832 dst += store->vector_stride;
833 }
834
835 dst += extra_stride;
836 }
837 break;
838 }
839
840 default:
841 assert(!"Should not get here.");
842 break;
843 }
844 }
845 }
846
847
848 /**
849 * Return printable string for a given GLSL_TYPE_x
850 */
851 static const char *
852 glsl_type_name(enum glsl_base_type type)
853 {
854 switch (type) {
855 case GLSL_TYPE_UINT:
856 return "uint";
857 case GLSL_TYPE_INT:
858 return "int";
859 case GLSL_TYPE_FLOAT:
860 return "float";
861 case GLSL_TYPE_DOUBLE:
862 return "double";
863 case GLSL_TYPE_UINT64:
864 return "uint64";
865 case GLSL_TYPE_INT64:
866 return "int64";
867 case GLSL_TYPE_BOOL:
868 return "bool";
869 case GLSL_TYPE_SAMPLER:
870 return "sampler";
871 case GLSL_TYPE_IMAGE:
872 return "image";
873 case GLSL_TYPE_ATOMIC_UINT:
874 return "atomic_uint";
875 case GLSL_TYPE_STRUCT:
876 return "struct";
877 case GLSL_TYPE_INTERFACE:
878 return "interface";
879 case GLSL_TYPE_ARRAY:
880 return "array";
881 case GLSL_TYPE_VOID:
882 return "void";
883 case GLSL_TYPE_ERROR:
884 return "error";
885 default:
886 return "other";
887 }
888 }
889
890
891 static struct gl_uniform_storage *
892 validate_uniform(GLint location, GLsizei count, const GLvoid *values,
893 unsigned *offset, struct gl_context *ctx,
894 struct gl_shader_program *shProg,
895 enum glsl_base_type basicType, unsigned src_components)
896 {
897 struct gl_uniform_storage *const uni =
898 validate_uniform_parameters(location, count, offset,
899 ctx, shProg, "glUniform");
900 if (uni == NULL)
901 return NULL;
902
903 if (uni->type->is_matrix()) {
904 /* Can't set matrix uniforms (like mat4) with glUniform */
905 _mesa_error(ctx, GL_INVALID_OPERATION,
906 "glUniform%u(uniform \"%s\"@%d is matrix)",
907 src_components, uni->name, location);
908 return NULL;
909 }
910
911 /* Verify that the types are compatible. */
912 const unsigned components = uni->type->vector_elements;
913
914 if (components != src_components) {
915 /* glUniformN() must match float/vecN type */
916 _mesa_error(ctx, GL_INVALID_OPERATION,
917 "glUniform%u(\"%s\"@%u has %u components, not %u)",
918 src_components, uni->name, location,
919 components, src_components);
920 return NULL;
921 }
922
923 bool match;
924 switch (uni->type->base_type) {
925 case GLSL_TYPE_BOOL:
926 match = (basicType != GLSL_TYPE_DOUBLE);
927 break;
928 case GLSL_TYPE_SAMPLER:
929 match = (basicType == GLSL_TYPE_INT);
930 break;
931 case GLSL_TYPE_IMAGE:
932 match = (basicType == GLSL_TYPE_INT && _mesa_is_desktop_gl(ctx));
933 break;
934 default:
935 match = (basicType == uni->type->base_type);
936 break;
937 }
938
939 if (!match) {
940 _mesa_error(ctx, GL_INVALID_OPERATION,
941 "glUniform%u(\"%s\"@%d is %s, not %s)",
942 src_components, uni->name, location,
943 glsl_type_name(uni->type->base_type),
944 glsl_type_name(basicType));
945 return NULL;
946 }
947
948 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
949 log_uniform(values, basicType, components, 1, count,
950 false, shProg, location, uni);
951 }
952
953 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
954 *
955 * "Setting a sampler's value to i selects texture image unit number
956 * i. The values of i range from zero to the implementation- dependent
957 * maximum supported number of texture image units."
958 *
959 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
960 * the PDF) says:
961 *
962 * "Error Description Offending command
963 * ignored?
964 * ...
965 * INVALID_VALUE Numeric argument out of range Yes"
966 *
967 * Based on that, when an invalid sampler is specified, we generate a
968 * GL_INVALID_VALUE error and ignore the command.
969 */
970 if (uni->type->is_sampler()) {
971 for (int i = 0; i < count; i++) {
972 const unsigned texUnit = ((unsigned *) values)[i];
973
974 /* check that the sampler (tex unit index) is legal */
975 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
976 _mesa_error(ctx, GL_INVALID_VALUE,
977 "glUniform1i(invalid sampler/tex unit index for "
978 "uniform %d)", location);
979 return NULL;
980 }
981 }
982 /* We need to reset the validate flag on changes to samplers in case
983 * two different sampler types are set to the same texture unit.
984 */
985 ctx->_Shader->Validated = GL_FALSE;
986 }
987
988 if (uni->type->is_image()) {
989 for (int i = 0; i < count; i++) {
990 const int unit = ((GLint *) values)[i];
991
992 /* check that the image unit is legal */
993 if (unit < 0 || unit >= (int)ctx->Const.MaxImageUnits) {
994 _mesa_error(ctx, GL_INVALID_VALUE,
995 "glUniform1i(invalid image unit index for uniform %d)",
996 location);
997 return NULL;
998 }
999 }
1000 }
1001
1002 return uni;
1003 }
1004
1005 void
1006 _mesa_flush_vertices_for_uniforms(struct gl_context *ctx,
1007 const struct gl_uniform_storage *uni)
1008 {
1009 /* Opaque uniforms have no storage unless they are bindless */
1010 if (!uni->is_bindless && uni->type->contains_opaque()) {
1011 FLUSH_VERTICES(ctx, 0);
1012 return;
1013 }
1014
1015 uint64_t new_driver_state = 0;
1016 unsigned mask = uni->active_shader_mask;
1017
1018 while (mask) {
1019 unsigned index = u_bit_scan(&mask);
1020
1021 assert(index < MESA_SHADER_STAGES);
1022 new_driver_state |= ctx->DriverFlags.NewShaderConstants[index];
1023 }
1024
1025 FLUSH_VERTICES(ctx, new_driver_state ? 0 : _NEW_PROGRAM_CONSTANTS);
1026 ctx->NewDriverState |= new_driver_state;
1027 }
1028
1029 /**
1030 * Called via glUniform*() functions.
1031 */
1032 extern "C" void
1033 _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
1034 struct gl_context *ctx, struct gl_shader_program *shProg,
1035 enum glsl_base_type basicType, unsigned src_components)
1036 {
1037 unsigned offset;
1038 int size_mul = glsl_base_type_is_64bit(basicType) ? 2 : 1;
1039
1040 struct gl_uniform_storage *uni;
1041 if (_mesa_is_no_error_enabled(ctx)) {
1042 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1043 *
1044 * "If the value of location is -1, the Uniform* commands will
1045 * silently ignore the data passed in, and the current uniform values
1046 * will not be changed.
1047 */
1048 if (location == -1)
1049 return;
1050
1051 uni = shProg->UniformRemapTable[location];
1052
1053 /* The array index specified by the uniform location is just the
1054 * uniform location minus the base location of of the uniform.
1055 */
1056 assert(uni->array_elements > 0 || location == (int)uni->remap_location);
1057 offset = location - uni->remap_location;
1058 } else {
1059 uni = validate_uniform(location, count, values, &offset, ctx, shProg,
1060 basicType, src_components);
1061 if (!uni)
1062 return;
1063 }
1064
1065 const unsigned components = uni->type->vector_elements;
1066
1067 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1068 *
1069 * "When loading N elements starting at an arbitrary position k in a
1070 * uniform declared as an array, elements k through k + N - 1 in the
1071 * array will be replaced with the new values. Values for any array
1072 * element that exceeds the highest array element index used, as
1073 * reported by GetActiveUniform, will be ignored by the GL."
1074 *
1075 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1076 * will have already generated an error.
1077 */
1078 if (uni->array_elements != 0) {
1079 count = MIN2(count, (int) (uni->array_elements - offset));
1080 }
1081
1082 /* We check samplers for changes and flush if needed in the sampler
1083 * handling code further down, so just skip them here.
1084 */
1085 if (!uni->type->is_sampler()) {
1086 _mesa_flush_vertices_for_uniforms(ctx, uni);
1087 }
1088
1089 /* Store the data in the "actual type" backing storage for the uniform.
1090 */
1091 if (!uni->type->is_boolean() && !uni->is_bindless) {
1092 memcpy(&uni->storage[size_mul * components * offset], values,
1093 sizeof(uni->storage[0]) * components * count * size_mul);
1094 } else if (uni->is_bindless) {
1095 const union gl_constant_value *src =
1096 (const union gl_constant_value *) values;
1097 GLuint64 *dst = (GLuint64 *)&uni->storage[components * offset].i;
1098 const unsigned elems = components * count;
1099
1100 for (unsigned i = 0; i < elems; i++) {
1101 dst[i] = src[i].i;
1102 }
1103 } else {
1104 const union gl_constant_value *src =
1105 (const union gl_constant_value *) values;
1106 union gl_constant_value *dst = &uni->storage[components * offset];
1107 const unsigned elems = components * count;
1108
1109 for (unsigned i = 0; i < elems; i++) {
1110 if (basicType == GLSL_TYPE_FLOAT) {
1111 dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
1112 } else {
1113 dst[i].i = src[i].i != 0 ? ctx->Const.UniformBooleanTrue : 0;
1114 }
1115 }
1116 }
1117
1118 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1119
1120 /* If the uniform is a sampler, do the extra magic necessary to propagate
1121 * the changes through.
1122 */
1123 if (uni->type->is_sampler()) {
1124 bool flushed = false;
1125
1126 shProg->SamplersValidated = GL_TRUE;
1127
1128 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1129 struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
1130
1131 /* If the shader stage doesn't use the sampler uniform, skip this. */
1132 if (!uni->opaque[i].active)
1133 continue;
1134
1135 bool changed = false;
1136 for (int j = 0; j < count; j++) {
1137 unsigned unit = uni->opaque[i].index + offset + j;
1138 unsigned value = ((unsigned *)values)[j];
1139
1140 if (uni->is_bindless) {
1141 struct gl_bindless_sampler *sampler =
1142 &sh->Program->sh.BindlessSamplers[unit];
1143
1144 /* Mark this bindless sampler as bound to a texture unit.
1145 */
1146 if (sampler->unit != value || !sampler->bound) {
1147 sampler->unit = value;
1148 changed = true;
1149 }
1150 sampler->bound = true;
1151 sh->Program->sh.HasBoundBindlessSampler = true;
1152 } else {
1153 if (sh->Program->SamplerUnits[unit] != value) {
1154 sh->Program->SamplerUnits[unit] = value;
1155 changed = true;
1156 }
1157 }
1158 }
1159
1160 if (changed) {
1161 if (!flushed) {
1162 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT | _NEW_PROGRAM);
1163 flushed = true;
1164 }
1165
1166 struct gl_program *const prog = sh->Program;
1167 _mesa_update_shader_textures_used(shProg, prog);
1168 if (ctx->Driver.SamplerUniformChange)
1169 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
1170 }
1171 }
1172 }
1173
1174 /* If the uniform is an image, update the mapping from image
1175 * uniforms to image units present in the shader data structure.
1176 */
1177 if (uni->type->is_image()) {
1178 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1179 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
1180
1181 /* If the shader stage doesn't use the image uniform, skip this. */
1182 if (!uni->opaque[i].active)
1183 continue;
1184
1185 for (int j = 0; j < count; j++) {
1186 unsigned unit = uni->opaque[i].index + offset + j;
1187 unsigned value = ((unsigned *)values)[j];
1188
1189 if (uni->is_bindless) {
1190 struct gl_bindless_image *image =
1191 &sh->Program->sh.BindlessImages[unit];
1192
1193 /* Mark this bindless image as bound to an image unit.
1194 */
1195 image->unit = value;
1196 image->bound = true;
1197 sh->Program->sh.HasBoundBindlessImage = true;
1198 } else {
1199 sh->Program->sh.ImageUnits[unit] = value;
1200 }
1201 }
1202 }
1203
1204 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
1205 }
1206 }
1207
1208 /**
1209 * Called by glUniformMatrix*() functions.
1210 * Note: cols=2, rows=4 ==> array[2] of vec4
1211 */
1212 extern "C" void
1213 _mesa_uniform_matrix(GLint location, GLsizei count,
1214 GLboolean transpose, const void *values,
1215 struct gl_context *ctx, struct gl_shader_program *shProg,
1216 GLuint cols, GLuint rows, enum glsl_base_type basicType)
1217 {
1218 unsigned offset;
1219 struct gl_uniform_storage *const uni =
1220 validate_uniform_parameters(location, count, &offset,
1221 ctx, shProg, "glUniformMatrix");
1222 if (uni == NULL)
1223 return;
1224
1225 if (!uni->type->is_matrix()) {
1226 _mesa_error(ctx, GL_INVALID_OPERATION,
1227 "glUniformMatrix(non-matrix uniform)");
1228 return;
1229 }
1230
1231 assert(basicType == GLSL_TYPE_FLOAT || basicType == GLSL_TYPE_DOUBLE);
1232 const unsigned size_mul = basicType == GLSL_TYPE_DOUBLE ? 2 : 1;
1233
1234 assert(!uni->type->is_sampler());
1235 const unsigned vectors = uni->type->matrix_columns;
1236 const unsigned components = uni->type->vector_elements;
1237
1238 /* Verify that the types are compatible. This is greatly simplified for
1239 * matrices because they can only have a float base type.
1240 */
1241 if (vectors != cols || components != rows) {
1242 _mesa_error(ctx, GL_INVALID_OPERATION,
1243 "glUniformMatrix(matrix size mismatch)");
1244 return;
1245 }
1246
1247 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1248 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1249 */
1250 if (transpose) {
1251 if (ctx->API == API_OPENGLES2 && ctx->Version < 30) {
1252 _mesa_error(ctx, GL_INVALID_VALUE,
1253 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1254 return;
1255 }
1256 }
1257
1258 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1259 * says:
1260 *
1261 * "If any of the following conditions occur, an INVALID_OPERATION
1262 * error is generated by the Uniform* commands, and no uniform values
1263 * are changed:
1264 *
1265 * ...
1266 *
1267 * - if the uniform declared in the shader is not of type boolean and
1268 * the type indicated in the name of the Uniform* command used does
1269 * not match the type of the uniform"
1270 *
1271 * There are no Boolean matrix types, so we do not need to allow
1272 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1273 */
1274 if (uni->type->base_type != basicType) {
1275 _mesa_error(ctx, GL_INVALID_OPERATION,
1276 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1277 cols, rows, uni->name, location,
1278 glsl_type_name(uni->type->base_type),
1279 glsl_type_name(basicType));
1280 return;
1281 }
1282
1283 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
1284 log_uniform(values, uni->type->base_type, components, vectors, count,
1285 bool(transpose), shProg, location, uni);
1286 }
1287
1288 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1289 *
1290 * "When loading N elements starting at an arbitrary position k in a
1291 * uniform declared as an array, elements k through k + N - 1 in the
1292 * array will be replaced with the new values. Values for any array
1293 * element that exceeds the highest array element index used, as
1294 * reported by GetActiveUniform, will be ignored by the GL."
1295 *
1296 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1297 * will have already generated an error.
1298 */
1299 if (uni->array_elements != 0) {
1300 count = MIN2(count, (int) (uni->array_elements - offset));
1301 }
1302
1303 _mesa_flush_vertices_for_uniforms(ctx, uni);
1304
1305 /* Store the data in the "actual type" backing storage for the uniform.
1306 */
1307 const unsigned elements = components * vectors;
1308
1309 if (!transpose) {
1310 memcpy(&uni->storage[size_mul * elements * offset], values,
1311 sizeof(uni->storage[0]) * elements * count * size_mul);
1312 } else if (basicType == GLSL_TYPE_FLOAT) {
1313 /* Copy and transpose the matrix.
1314 */
1315 const float *src = (const float *)values;
1316 float *dst = &uni->storage[elements * offset].f;
1317
1318 for (int i = 0; i < count; i++) {
1319 for (unsigned r = 0; r < rows; r++) {
1320 for (unsigned c = 0; c < cols; c++) {
1321 dst[(c * components) + r] = src[c + (r * vectors)];
1322 }
1323 }
1324
1325 dst += elements;
1326 src += elements;
1327 }
1328 } else {
1329 assert(basicType == GLSL_TYPE_DOUBLE);
1330 const double *src = (const double *)values;
1331 double *dst = (double *)&uni->storage[elements * offset].f;
1332
1333 for (int i = 0; i < count; i++) {
1334 for (unsigned r = 0; r < rows; r++) {
1335 for (unsigned c = 0; c < cols; c++) {
1336 dst[(c * components) + r] = src[c + (r * vectors)];
1337 }
1338 }
1339
1340 dst += elements;
1341 src += elements;
1342 }
1343 }
1344
1345 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1346 }
1347
1348 static void
1349 update_bound_bindless_sampler_flag(struct gl_program *prog)
1350 {
1351 unsigned i;
1352
1353 if (likely(!prog->sh.HasBoundBindlessSampler))
1354 return;
1355
1356 for (i = 0; i < prog->sh.NumBindlessSamplers; i++) {
1357 struct gl_bindless_sampler *sampler = &prog->sh.BindlessSamplers[i];
1358
1359 if (sampler->bound)
1360 return;
1361 }
1362 prog->sh.HasBoundBindlessSampler = false;
1363 }
1364
1365 static void
1366 update_bound_bindless_image_flag(struct gl_program *prog)
1367 {
1368 unsigned i;
1369
1370 if (likely(!prog->sh.HasBoundBindlessImage))
1371 return;
1372
1373 for (i = 0; i < prog->sh.NumBindlessImages; i++) {
1374 struct gl_bindless_image *image = &prog->sh.BindlessImages[i];
1375
1376 if (image->bound)
1377 return;
1378 }
1379 prog->sh.HasBoundBindlessImage = false;
1380 }
1381
1382 /**
1383 * Called via glUniformHandleui64*ARB() functions.
1384 */
1385 extern "C" void
1386 _mesa_uniform_handle(GLint location, GLsizei count, const GLvoid *values,
1387 struct gl_context *ctx, struct gl_shader_program *shProg)
1388 {
1389 unsigned offset;
1390 struct gl_uniform_storage *uni;
1391
1392 if (_mesa_is_no_error_enabled(ctx)) {
1393 /* From Section 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1394 *
1395 * "If the value of location is -1, the Uniform* commands will
1396 * silently ignore the data passed in, and the current uniform values
1397 * will not be changed.
1398 */
1399 if (location == -1)
1400 return;
1401
1402 uni = shProg->UniformRemapTable[location];
1403
1404 /* The array index specified by the uniform location is just the
1405 * uniform location minus the base location of of the uniform.
1406 */
1407 assert(uni->array_elements > 0 || location == (int)uni->remap_location);
1408 offset = location - uni->remap_location;
1409 } else {
1410 uni = validate_uniform_parameters(location, count, &offset,
1411 ctx, shProg, "glUniformHandleui64*ARB");
1412 if (!uni)
1413 return;
1414
1415 if (!uni->is_bindless) {
1416 /* From section "Errors" of the ARB_bindless_texture spec:
1417 *
1418 * "The error INVALID_OPERATION is generated by
1419 * UniformHandleui64{v}ARB if the sampler or image uniform being
1420 * updated has the "bound_sampler" or "bound_image" layout qualifier."
1421 *
1422 * From section 4.4.6 of the ARB_bindless_texture spec:
1423 *
1424 * "In the absence of these qualifiers, sampler and image uniforms are
1425 * considered "bound". Additionally, if GL_ARB_bindless_texture is
1426 * not enabled, these uniforms are considered "bound"."
1427 */
1428 _mesa_error(ctx, GL_INVALID_OPERATION,
1429 "glUniformHandleui64*ARB(non-bindless sampler/image uniform)");
1430 return;
1431 }
1432 }
1433
1434 const unsigned components = uni->type->vector_elements;
1435 const int size_mul = 2;
1436
1437 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
1438 log_uniform(values, GLSL_TYPE_UINT64, components, 1, count,
1439 false, shProg, location, uni);
1440 }
1441
1442 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1443 *
1444 * "When loading N elements starting at an arbitrary position k in a
1445 * uniform declared as an array, elements k through k + N - 1 in the
1446 * array will be replaced with the new values. Values for any array
1447 * element that exceeds the highest array element index used, as
1448 * reported by GetActiveUniform, will be ignored by the GL."
1449 *
1450 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1451 * will have already generated an error.
1452 */
1453 if (uni->array_elements != 0) {
1454 count = MIN2(count, (int) (uni->array_elements - offset));
1455 }
1456
1457 _mesa_flush_vertices_for_uniforms(ctx, uni);
1458
1459 /* Store the data in the "actual type" backing storage for the uniform.
1460 */
1461 memcpy(&uni->storage[size_mul * components * offset], values,
1462 sizeof(uni->storage[0]) * components * count * size_mul);
1463
1464 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1465
1466 if (uni->type->is_sampler()) {
1467 /* Mark this bindless sampler as not bound to a texture unit because
1468 * it refers to a texture handle.
1469 */
1470 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1471 struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
1472
1473 /* If the shader stage doesn't use the sampler uniform, skip this. */
1474 if (!uni->opaque[i].active)
1475 continue;
1476
1477 for (int j = 0; j < count; j++) {
1478 unsigned unit = uni->opaque[i].index + offset + j;
1479 struct gl_bindless_sampler *sampler =
1480 &sh->Program->sh.BindlessSamplers[unit];
1481
1482 sampler->bound = false;
1483 }
1484
1485 update_bound_bindless_sampler_flag(sh->Program);
1486 }
1487 }
1488
1489 if (uni->type->is_image()) {
1490 /* Mark this bindless image as not bound to an image unit because it
1491 * refers to a texture handle.
1492 */
1493 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1494 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
1495
1496 /* If the shader stage doesn't use the sampler uniform, skip this. */
1497 if (!uni->opaque[i].active)
1498 continue;
1499
1500 for (int j = 0; j < count; j++) {
1501 unsigned unit = uni->opaque[i].index + offset + j;
1502 struct gl_bindless_image *image =
1503 &sh->Program->sh.BindlessImages[unit];
1504
1505 image->bound = false;
1506 }
1507
1508 update_bound_bindless_image_flag(sh->Program);
1509 }
1510 }
1511 }
1512
1513 extern "C" bool
1514 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1515 char *errMsg, size_t errMsgLength)
1516 {
1517 /* Shader does not have samplers. */
1518 if (shProg->data->NumUniformStorage == 0)
1519 return true;
1520
1521 if (!shProg->SamplersValidated) {
1522 _mesa_snprintf(errMsg, errMsgLength,
1523 "active samplers with a different type "
1524 "refer to the same texture image unit");
1525 return false;
1526 }
1527 return true;
1528 }
1529
1530 extern "C" bool
1531 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object *pipeline)
1532 {
1533 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1534 * OpenGL 4.1 spec says:
1535 *
1536 * "[INVALID_OPERATION] is generated by any command that transfers
1537 * vertices to the GL if:
1538 *
1539 * ...
1540 *
1541 * - Any two active samplers in the current program object are of
1542 * different types, but refer to the same texture image unit.
1543 *
1544 * - The number of active samplers in the program exceeds the
1545 * maximum number of texture image units allowed."
1546 */
1547
1548 GLbitfield mask;
1549 GLbitfield TexturesUsed[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1550 unsigned active_samplers = 0;
1551 const struct gl_program **prog =
1552 (const struct gl_program **) pipeline->CurrentProgram;
1553
1554
1555 memset(TexturesUsed, 0, sizeof(TexturesUsed));
1556
1557 for (unsigned idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1558 if (!prog[idx])
1559 continue;
1560
1561 mask = prog[idx]->SamplersUsed;
1562 while (mask) {
1563 const int s = u_bit_scan(&mask);
1564 GLuint unit = prog[idx]->SamplerUnits[s];
1565 GLuint tgt = prog[idx]->sh.SamplerTargets[s];
1566
1567 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1568 * great job of eliminating unused uniforms currently so for now
1569 * don't throw an error if two sampler types both point to 0.
1570 */
1571 if (unit == 0)
1572 continue;
1573
1574 if (TexturesUsed[unit] & ~(1 << tgt)) {
1575 pipeline->InfoLog =
1576 ralloc_asprintf(pipeline,
1577 "Program %d: "
1578 "Texture unit %d is accessed with 2 different types",
1579 prog[idx]->Id, unit);
1580 return false;
1581 }
1582
1583 TexturesUsed[unit] |= (1 << tgt);
1584 }
1585
1586 active_samplers += prog[idx]->info.num_textures;
1587 }
1588
1589 if (active_samplers > MAX_COMBINED_TEXTURE_IMAGE_UNITS) {
1590 pipeline->InfoLog =
1591 ralloc_asprintf(pipeline,
1592 "the number of active samplers %d exceed the "
1593 "maximum %d",
1594 active_samplers, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1595 return false;
1596 }
1597
1598 return true;
1599 }