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