mesa: fix setting uniform variables for bindless samplers/images
[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
1006 /**
1007 * Called via glUniform*() functions.
1008 */
1009 extern "C" void
1010 _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
1011 struct gl_context *ctx, struct gl_shader_program *shProg,
1012 enum glsl_base_type basicType, unsigned src_components)
1013 {
1014 unsigned offset;
1015 int size_mul = glsl_base_type_is_64bit(basicType) ? 2 : 1;
1016
1017 struct gl_uniform_storage *uni;
1018 if (_mesa_is_no_error_enabled(ctx)) {
1019 /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
1020 *
1021 * "If the value of location is -1, the Uniform* commands will
1022 * silently ignore the data passed in, and the current uniform values
1023 * will not be changed.
1024 */
1025 if (location == -1)
1026 return;
1027
1028 uni = shProg->UniformRemapTable[location];
1029
1030 /* The array index specified by the uniform location is just the
1031 * uniform location minus the base location of of the uniform.
1032 */
1033 assert(uni->array_elements > 0 || location == (int)uni->remap_location);
1034 offset = location - uni->remap_location;
1035 } else {
1036 uni = validate_uniform(location, count, values, &offset, ctx, shProg,
1037 basicType, src_components);
1038 if (!uni)
1039 return;
1040 }
1041
1042 const unsigned components = uni->type->vector_elements;
1043
1044 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1045 *
1046 * "When loading N elements starting at an arbitrary position k in a
1047 * uniform declared as an array, elements k through k + N - 1 in the
1048 * array will be replaced with the new values. Values for any array
1049 * element that exceeds the highest array element index used, as
1050 * reported by GetActiveUniform, will be ignored by the GL."
1051 *
1052 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1053 * will have already generated an error.
1054 */
1055 if (uni->array_elements != 0) {
1056 count = MIN2(count, (int) (uni->array_elements - offset));
1057 }
1058
1059 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1060
1061 /* Store the data in the "actual type" backing storage for the uniform.
1062 */
1063 if (!uni->type->is_boolean() && !uni->is_bindless) {
1064 memcpy(&uni->storage[size_mul * components * offset], values,
1065 sizeof(uni->storage[0]) * components * count * size_mul);
1066 } else if (uni->is_bindless) {
1067 const union gl_constant_value *src =
1068 (const union gl_constant_value *) values;
1069 GLuint64 *dst = (GLuint64 *)&uni->storage[components * offset].i;
1070 const unsigned elems = components * count;
1071
1072 for (unsigned i = 0; i < elems; i++) {
1073 dst[i] = src[i].i;
1074 }
1075 } else {
1076 const union gl_constant_value *src =
1077 (const union gl_constant_value *) values;
1078 union gl_constant_value *dst = &uni->storage[components * offset];
1079 const unsigned elems = components * count;
1080
1081 for (unsigned i = 0; i < elems; i++) {
1082 if (basicType == GLSL_TYPE_FLOAT) {
1083 dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
1084 } else {
1085 dst[i].i = src[i].i != 0 ? ctx->Const.UniformBooleanTrue : 0;
1086 }
1087 }
1088 }
1089
1090 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1091
1092 /* If the uniform is a sampler, do the extra magic necessary to propagate
1093 * the changes through.
1094 */
1095 if (uni->type->is_sampler()) {
1096 bool flushed = false;
1097
1098 shProg->SamplersValidated = GL_TRUE;
1099
1100 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1101 struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
1102
1103 /* If the shader stage doesn't use the sampler uniform, skip this. */
1104 if (!uni->opaque[i].active)
1105 continue;
1106
1107 bool changed = false;
1108 for (int j = 0; j < count; j++) {
1109 unsigned unit = uni->opaque[i].index + offset + j;
1110 unsigned value = ((unsigned *)values)[j];
1111
1112 if (uni->is_bindless) {
1113 struct gl_bindless_sampler *sampler =
1114 &sh->Program->sh.BindlessSamplers[unit];
1115
1116 /* Mark this bindless sampler as bound to a texture unit.
1117 */
1118 if (sampler->unit != value || !sampler->bound) {
1119 sampler->unit = value;
1120 changed = true;
1121 }
1122 sampler->bound = true;
1123 sh->Program->sh.HasBoundBindlessSampler = true;
1124 } else {
1125 if (sh->Program->SamplerUnits[unit] != value) {
1126 sh->Program->SamplerUnits[unit] = value;
1127 changed = true;
1128 }
1129 }
1130 }
1131
1132 if (changed) {
1133 if (!flushed) {
1134 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT | _NEW_PROGRAM);
1135 flushed = true;
1136 }
1137
1138 struct gl_program *const prog = sh->Program;
1139 _mesa_update_shader_textures_used(shProg, prog);
1140 if (ctx->Driver.SamplerUniformChange)
1141 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
1142 }
1143 }
1144 }
1145
1146 /* If the uniform is an image, update the mapping from image
1147 * uniforms to image units present in the shader data structure.
1148 */
1149 if (uni->type->is_image()) {
1150 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1151 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
1152
1153 /* If the shader stage doesn't use the image uniform, skip this. */
1154 if (!uni->opaque[i].active)
1155 continue;
1156
1157 for (int j = 0; j < count; j++) {
1158 unsigned unit = uni->opaque[i].index + offset + j;
1159 unsigned value = ((unsigned *)values)[j];
1160
1161 if (uni->is_bindless) {
1162 struct gl_bindless_image *image =
1163 &sh->Program->sh.BindlessImages[unit];
1164
1165 /* Mark this bindless image as bound to an image unit.
1166 */
1167 image->unit = value;
1168 image->bound = true;
1169 sh->Program->sh.HasBoundBindlessImage = true;
1170 } else {
1171 sh->Program->sh.ImageUnits[unit] = value;
1172 }
1173 }
1174 }
1175
1176 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
1177 }
1178 }
1179
1180 /**
1181 * Called by glUniformMatrix*() functions.
1182 * Note: cols=2, rows=4 ==> array[2] of vec4
1183 */
1184 extern "C" void
1185 _mesa_uniform_matrix(GLint location, GLsizei count,
1186 GLboolean transpose, const void *values,
1187 struct gl_context *ctx, struct gl_shader_program *shProg,
1188 GLuint cols, GLuint rows, enum glsl_base_type basicType)
1189 {
1190 unsigned offset;
1191 struct gl_uniform_storage *const uni =
1192 validate_uniform_parameters(location, count, &offset,
1193 ctx, shProg, "glUniformMatrix");
1194 if (uni == NULL)
1195 return;
1196
1197 if (!uni->type->is_matrix()) {
1198 _mesa_error(ctx, GL_INVALID_OPERATION,
1199 "glUniformMatrix(non-matrix uniform)");
1200 return;
1201 }
1202
1203 assert(basicType == GLSL_TYPE_FLOAT || basicType == GLSL_TYPE_DOUBLE);
1204 const unsigned size_mul = basicType == GLSL_TYPE_DOUBLE ? 2 : 1;
1205
1206 assert(!uni->type->is_sampler());
1207 const unsigned vectors = uni->type->matrix_columns;
1208 const unsigned components = uni->type->vector_elements;
1209
1210 /* Verify that the types are compatible. This is greatly simplified for
1211 * matrices because they can only have a float base type.
1212 */
1213 if (vectors != cols || components != rows) {
1214 _mesa_error(ctx, GL_INVALID_OPERATION,
1215 "glUniformMatrix(matrix size mismatch)");
1216 return;
1217 }
1218
1219 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
1220 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
1221 */
1222 if (transpose) {
1223 if (ctx->API == API_OPENGLES2 && ctx->Version < 30) {
1224 _mesa_error(ctx, GL_INVALID_VALUE,
1225 "glUniformMatrix(matrix transpose is not GL_FALSE)");
1226 return;
1227 }
1228 }
1229
1230 /* Section 2.11.7 (Uniform Variables) of the OpenGL 4.2 Core Profile spec
1231 * says:
1232 *
1233 * "If any of the following conditions occur, an INVALID_OPERATION
1234 * error is generated by the Uniform* commands, and no uniform values
1235 * are changed:
1236 *
1237 * ...
1238 *
1239 * - if the uniform declared in the shader is not of type boolean and
1240 * the type indicated in the name of the Uniform* command used does
1241 * not match the type of the uniform"
1242 *
1243 * There are no Boolean matrix types, so we do not need to allow
1244 * GLSL_TYPE_BOOL here (as _mesa_uniform does).
1245 */
1246 if (uni->type->base_type != basicType) {
1247 _mesa_error(ctx, GL_INVALID_OPERATION,
1248 "glUniformMatrix%ux%u(\"%s\"@%d is %s, not %s)",
1249 cols, rows, uni->name, location,
1250 glsl_type_name(uni->type->base_type),
1251 glsl_type_name(basicType));
1252 return;
1253 }
1254
1255 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
1256 log_uniform(values, uni->type->base_type, components, vectors, count,
1257 bool(transpose), shProg, location, uni);
1258 }
1259
1260 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1261 *
1262 * "When loading N elements starting at an arbitrary position k in a
1263 * uniform declared as an array, elements k through k + N - 1 in the
1264 * array will be replaced with the new values. Values for any array
1265 * element that exceeds the highest array element index used, as
1266 * reported by GetActiveUniform, will be ignored by the GL."
1267 *
1268 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1269 * will have already generated an error.
1270 */
1271 if (uni->array_elements != 0) {
1272 count = MIN2(count, (int) (uni->array_elements - offset));
1273 }
1274
1275 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1276
1277 /* Store the data in the "actual type" backing storage for the uniform.
1278 */
1279 const unsigned elements = components * vectors;
1280
1281 if (!transpose) {
1282 memcpy(&uni->storage[size_mul * elements * offset], values,
1283 sizeof(uni->storage[0]) * elements * count * size_mul);
1284 } else if (basicType == GLSL_TYPE_FLOAT) {
1285 /* Copy and transpose the matrix.
1286 */
1287 const float *src = (const float *)values;
1288 float *dst = &uni->storage[elements * offset].f;
1289
1290 for (int i = 0; i < count; i++) {
1291 for (unsigned r = 0; r < rows; r++) {
1292 for (unsigned c = 0; c < cols; c++) {
1293 dst[(c * components) + r] = src[c + (r * vectors)];
1294 }
1295 }
1296
1297 dst += elements;
1298 src += elements;
1299 }
1300 } else {
1301 assert(basicType == GLSL_TYPE_DOUBLE);
1302 const double *src = (const double *)values;
1303 double *dst = (double *)&uni->storage[elements * offset].f;
1304
1305 for (int i = 0; i < count; i++) {
1306 for (unsigned r = 0; r < rows; r++) {
1307 for (unsigned c = 0; c < cols; c++) {
1308 dst[(c * components) + r] = src[c + (r * vectors)];
1309 }
1310 }
1311
1312 dst += elements;
1313 src += elements;
1314 }
1315 }
1316
1317 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1318 }
1319
1320 static void
1321 update_bound_bindless_sampler_flag(struct gl_program *prog)
1322 {
1323 unsigned i;
1324
1325 if (likely(!prog->sh.HasBoundBindlessSampler))
1326 return;
1327
1328 for (i = 0; i < prog->sh.NumBindlessSamplers; i++) {
1329 struct gl_bindless_sampler *sampler = &prog->sh.BindlessSamplers[i];
1330
1331 if (sampler->bound)
1332 return;
1333 }
1334 prog->sh.HasBoundBindlessSampler = false;
1335 }
1336
1337 static void
1338 update_bound_bindless_image_flag(struct gl_program *prog)
1339 {
1340 unsigned i;
1341
1342 if (likely(!prog->sh.HasBoundBindlessImage))
1343 return;
1344
1345 for (i = 0; i < prog->sh.NumBindlessImages; i++) {
1346 struct gl_bindless_image *image = &prog->sh.BindlessImages[i];
1347
1348 if (image->bound)
1349 return;
1350 }
1351 prog->sh.HasBoundBindlessImage = false;
1352 }
1353
1354 /**
1355 * Called via glUniformHandleui64*ARB() functions.
1356 */
1357 extern "C" void
1358 _mesa_uniform_handle(GLint location, GLsizei count, const GLvoid *values,
1359 struct gl_context *ctx, struct gl_shader_program *shProg)
1360 {
1361 unsigned offset;
1362 struct gl_uniform_storage *const uni =
1363 validate_uniform_parameters(location, count, &offset,
1364 ctx, shProg, "glUniformHandleui64*ARB");
1365 if (uni == NULL)
1366 return;
1367
1368 if (!uni->is_bindless) {
1369 /* From section "Errors" of the ARB_bindless_texture spec:
1370 *
1371 * "The error INVALID_OPERATION is generated by
1372 * UniformHandleui64{v}ARB if the sampler or image uniform being
1373 * updated has the "bound_sampler" or "bound_image" layout qualifier."
1374 *
1375 * From section 4.4.6 of the ARB_bindless_texture spec:
1376 *
1377 * "In the absence of these qualifiers, sampler and image uniforms are
1378 * considered "bound". Additionally, if GL_ARB_bindless_texture is not
1379 * enabled, these uniforms are considered "bound"."
1380 */
1381 _mesa_error(ctx, GL_INVALID_OPERATION,
1382 "glUniformHandleui64*ARB(non-bindless sampler/image uniform)");
1383 return;
1384 }
1385
1386 const unsigned components = uni->type->vector_elements;
1387 const int size_mul = 2;
1388
1389 if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
1390 log_uniform(values, GLSL_TYPE_UINT64, components, 1, count,
1391 false, shProg, location, uni);
1392 }
1393
1394 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1395 *
1396 * "When loading N elements starting at an arbitrary position k in a
1397 * uniform declared as an array, elements k through k + N - 1 in the
1398 * array will be replaced with the new values. Values for any array
1399 * element that exceeds the highest array element index used, as
1400 * reported by GetActiveUniform, will be ignored by the GL."
1401 *
1402 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1403 * will have already generated an error.
1404 */
1405 if (uni->array_elements != 0) {
1406 count = MIN2(count, (int) (uni->array_elements - offset));
1407 }
1408
1409 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1410
1411 /* Store the data in the "actual type" backing storage for the uniform.
1412 */
1413 memcpy(&uni->storage[size_mul * components * offset], values,
1414 sizeof(uni->storage[0]) * components * count * size_mul);
1415
1416 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1417
1418 if (uni->type->is_sampler()) {
1419 /* Mark this bindless sampler as not bound to a texture unit because
1420 * it refers to a texture handle.
1421 */
1422 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1423 struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
1424
1425 /* If the shader stage doesn't use the sampler uniform, skip this. */
1426 if (!uni->opaque[i].active)
1427 continue;
1428
1429 for (int j = 0; j < count; j++) {
1430 unsigned unit = uni->opaque[i].index + offset + j;
1431 struct gl_bindless_sampler *sampler =
1432 &sh->Program->sh.BindlessSamplers[unit];
1433
1434 sampler->bound = false;
1435 }
1436
1437 update_bound_bindless_sampler_flag(sh->Program);
1438 }
1439 }
1440
1441 if (uni->type->is_image()) {
1442 /* Mark this bindless image as not bound to an image unit because it
1443 * refers to a texture handle.
1444 */
1445 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1446 struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
1447
1448 /* If the shader stage doesn't use the sampler uniform, skip this. */
1449 if (!uni->opaque[i].active)
1450 continue;
1451
1452 for (int j = 0; j < count; j++) {
1453 unsigned unit = uni->opaque[i].index + offset + j;
1454 struct gl_bindless_image *image =
1455 &sh->Program->sh.BindlessImages[unit];
1456
1457 image->bound = false;
1458 }
1459
1460 update_bound_bindless_image_flag(sh->Program);
1461 }
1462 }
1463 }
1464
1465 extern "C" bool
1466 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1467 char *errMsg, size_t errMsgLength)
1468 {
1469 /* Shader does not have samplers. */
1470 if (shProg->data->NumUniformStorage == 0)
1471 return true;
1472
1473 if (!shProg->SamplersValidated) {
1474 _mesa_snprintf(errMsg, errMsgLength,
1475 "active samplers with a different type "
1476 "refer to the same texture image unit");
1477 return false;
1478 }
1479 return true;
1480 }
1481
1482 extern "C" bool
1483 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object *pipeline)
1484 {
1485 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
1486 * OpenGL 4.1 spec says:
1487 *
1488 * "[INVALID_OPERATION] is generated by any command that transfers
1489 * vertices to the GL if:
1490 *
1491 * ...
1492 *
1493 * - Any two active samplers in the current program object are of
1494 * different types, but refer to the same texture image unit.
1495 *
1496 * - The number of active samplers in the program exceeds the
1497 * maximum number of texture image units allowed."
1498 */
1499
1500 GLbitfield mask;
1501 GLbitfield TexturesUsed[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1502 unsigned active_samplers = 0;
1503 const struct gl_program **prog =
1504 (const struct gl_program **) pipeline->CurrentProgram;
1505
1506
1507 memset(TexturesUsed, 0, sizeof(TexturesUsed));
1508
1509 for (unsigned idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1510 if (!prog[idx])
1511 continue;
1512
1513 mask = prog[idx]->SamplersUsed;
1514 while (mask) {
1515 const int s = u_bit_scan(&mask);
1516 GLuint unit = prog[idx]->SamplerUnits[s];
1517 GLuint tgt = prog[idx]->sh.SamplerTargets[s];
1518
1519 /* FIXME: Samplers are initialized to 0 and Mesa doesn't do a
1520 * great job of eliminating unused uniforms currently so for now
1521 * don't throw an error if two sampler types both point to 0.
1522 */
1523 if (unit == 0)
1524 continue;
1525
1526 if (TexturesUsed[unit] & ~(1 << tgt)) {
1527 pipeline->InfoLog =
1528 ralloc_asprintf(pipeline,
1529 "Program %d: "
1530 "Texture unit %d is accessed with 2 different types",
1531 prog[idx]->Id, unit);
1532 return false;
1533 }
1534
1535 TexturesUsed[unit] |= (1 << tgt);
1536 }
1537
1538 active_samplers += prog[idx]->info.num_textures;
1539 }
1540
1541 if (active_samplers > MAX_COMBINED_TEXTURE_IMAGE_UNITS) {
1542 pipeline->InfoLog =
1543 ralloc_asprintf(pipeline,
1544 "the number of active samplers %d exceed the "
1545 "maximum %d",
1546 active_samplers, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1547 return false;
1548 }
1549
1550 return true;
1551 }