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