mesa: Don't check for API_OPENGLES in _mesa_uniform_matrix
[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
29 #include "main/core.h"
30 #include "main/context.h"
31 #include "ir.h"
32 #include "ir_uniform.h"
33 #include "program/hash_table.h"
34 #include "../glsl/program.h"
35 #include "../glsl/ir_uniform.h"
36 #include "../glsl/glsl_parser_extras.h"
37 #include "main/shaderapi.h"
38 #include "main/shaderobj.h"
39 #include "uniforms.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 _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
50
51 if (!shProg)
52 return;
53
54 if (index >= shProg->NumUserUniformStorage) {
55 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
56 return;
57 }
58
59 const struct gl_uniform_storage *const uni = &shProg->UniformStorage[index];
60
61 if (nameOut) {
62 _mesa_get_uniform_name(uni, maxLength, length, nameOut);
63 }
64
65 if (size) {
66 /* array_elements is zero for non-arrays, but the API requires that 1 be
67 * returned.
68 */
69 *size = MAX2(1, uni->array_elements);
70 }
71
72 if (type) {
73 *type = uni->type->gl_type;
74 }
75 }
76
77 extern "C" void GLAPIENTRY
78 _mesa_GetActiveUniformsiv(GLuint program,
79 GLsizei uniformCount,
80 const GLuint *uniformIndices,
81 GLenum pname,
82 GLint *params)
83 {
84 GET_CURRENT_CONTEXT(ctx);
85 struct gl_shader_program *shProg;
86 GLsizei i;
87
88 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
89 if (!shProg)
90 return;
91
92 if (uniformCount < 0) {
93 _mesa_error(ctx, GL_INVALID_VALUE,
94 "glGetActiveUniformsiv(uniformCount < 0)");
95 return;
96 }
97
98 for (i = 0; i < uniformCount; i++) {
99 GLuint index = uniformIndices[i];
100
101 if (index >= shProg->NumUserUniformStorage) {
102 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniformsiv(index)");
103 return;
104 }
105 }
106
107 for (i = 0; i < uniformCount; i++) {
108 GLuint index = uniformIndices[i];
109 const struct gl_uniform_storage *uni = &shProg->UniformStorage[index];
110
111 switch (pname) {
112 case GL_UNIFORM_TYPE:
113 params[i] = uni->type->gl_type;
114 break;
115
116 case GL_UNIFORM_SIZE:
117 /* array_elements is zero for non-arrays, but the API requires that 1 be
118 * returned.
119 */
120 params[i] = MAX2(1, uni->array_elements);
121 break;
122
123 case GL_UNIFORM_NAME_LENGTH:
124 params[i] = strlen(uni->name) + 1;
125
126 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
127 * spec says:
128 *
129 * "If the active uniform is an array, the uniform name returned
130 * in name will always be the name of the uniform array appended
131 * with "[0]"."
132 */
133 if (uni->array_elements != 0)
134 params[i] += 3;
135 break;
136
137 case GL_UNIFORM_BLOCK_INDEX:
138 params[i] = uni->block_index;
139 break;
140
141 case GL_UNIFORM_OFFSET:
142 params[i] = uni->offset;
143 break;
144
145 case GL_UNIFORM_ARRAY_STRIDE:
146 params[i] = uni->array_stride;
147 break;
148
149 case GL_UNIFORM_MATRIX_STRIDE:
150 params[i] = uni->matrix_stride;
151 break;
152
153 case GL_UNIFORM_IS_ROW_MAJOR:
154 params[i] = uni->row_major;
155 break;
156
157 case GL_UNIFORM_ATOMIC_COUNTER_BUFFER_INDEX:
158 if (!ctx->Extensions.ARB_shader_atomic_counters)
159 goto invalid_enum;
160 params[i] = uni->atomic_buffer_index;
161 break;
162
163 default:
164 goto invalid_enum;
165 }
166 }
167
168 return;
169
170 invalid_enum:
171 _mesa_error(ctx, GL_INVALID_ENUM, "glGetActiveUniformsiv(pname)");
172 }
173
174 static struct gl_uniform_storage *
175 validate_uniform_parameters(struct gl_context *ctx,
176 struct gl_shader_program *shProg,
177 GLint location, GLsizei count,
178 unsigned *array_index,
179 const char *caller)
180 {
181 if (shProg == NULL) {
182 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(program not linked)", caller);
183 return NULL;
184 }
185
186 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
187 *
188 * "If a negative number is provided where an argument of type sizei or
189 * sizeiptr is specified, the error INVALID_VALUE is generated."
190 */
191 if (count < 0) {
192 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count < 0)", caller);
193 return NULL;
194 }
195
196 /* Check that the given location is in bounds of uniform remap table.
197 * Unlinked programs will have NumUniformRemapTable == 0, so we can take
198 * the shProg->LinkStatus check out of the main path.
199 */
200 if (unlikely(location >= (GLint) shProg->NumUniformRemapTable)) {
201 if (!shProg->LinkStatus)
202 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(program not linked)",
203 caller);
204 else
205 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
206 caller, location);
207
208 return NULL;
209 }
210
211 if (location == -1) {
212 if (!shProg->LinkStatus)
213 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(program not linked)",
214 caller);
215
216 return NULL;
217 }
218
219 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
220 *
221 * "If any of the following conditions occur, an INVALID_OPERATION
222 * error is generated by the Uniform* commands, and no uniform values
223 * are changed:
224 *
225 * ...
226 *
227 * - if no variable with a location of location exists in the
228 * program object currently in use and location is not -1,
229 * - if count is greater than one, and the uniform declared in the
230 * shader is not an array variable,
231 */
232 if (location < -1 || !shProg->UniformRemapTable[location]) {
233 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
234 caller, location);
235 return NULL;
236 }
237
238 /* If the driver storage pointer in remap table is -1, we ignore silently.
239 *
240 * GL_ARB_explicit_uniform_location spec says:
241 * "What happens if Uniform* is called with an explicitly defined
242 * uniform location, but that uniform is deemed inactive by the
243 * linker?
244 *
245 * RESOLVED: The call is ignored for inactive uniform variables and
246 * no error is generated."
247 *
248 */
249 if (shProg->UniformRemapTable[location] ==
250 INACTIVE_UNIFORM_EXPLICIT_LOCATION)
251 return NULL;
252
253 struct gl_uniform_storage *const uni = shProg->UniformRemapTable[location];
254
255 if (uni->array_elements == 0) {
256 if (count > 1) {
257 _mesa_error(ctx, GL_INVALID_OPERATION,
258 "%s(count > 1 for non-array, location=%d)",
259 caller, location);
260 return NULL;
261 }
262
263 assert((location - uni->remap_location) == 0);
264 *array_index = 0;
265 } else {
266 /* The array index specified by the uniform location is just the uniform
267 * location minus the base location of of the uniform.
268 */
269 *array_index = location - uni->remap_location;
270
271 /* If the uniform is an array, check that array_index is in bounds.
272 * array_index is unsigned so no need to check for less than zero.
273 */
274 if (*array_index >= uni->array_elements) {
275 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
276 caller, location);
277 return NULL;
278 }
279 }
280 return uni;
281 }
282
283 /**
284 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
285 */
286 extern "C" void
287 _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
288 GLsizei bufSize, enum glsl_base_type returnType,
289 GLvoid *paramsOut)
290 {
291 struct gl_shader_program *shProg =
292 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformfv");
293 unsigned offset;
294
295 struct gl_uniform_storage *const uni =
296 validate_uniform_parameters(ctx, shProg, location, 1,
297 &offset, "glGetUniform");
298 if (uni == NULL) {
299 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
300 * spec says:
301 *
302 * "The error INVALID_OPERATION is generated if program has not been
303 * linked successfully, or if location is not a valid location for
304 * program."
305 *
306 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
307 * says:
308 *
309 * "If the value of location is -1, the Uniform* commands will
310 * silently ignore the data passed in, and the current uniform
311 * values will not be changed."
312 *
313 * Allowing -1 for the location parameter of glUniform allows
314 * applications to avoid error paths in the case that, for example, some
315 * uniform variable is removed by the compiler / linker after
316 * optimization. In this case, the new value of the uniform is dropped
317 * on the floor. For the case of glGetUniform, there is nothing
318 * sensible to do for a location of -1.
319 *
320 * If the location was -1, validate_unfirom_parameters will return NULL
321 * without raising an error. Raise the error here.
322 */
323 if (location == -1) {
324 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniform(location=%d)",
325 location);
326 }
327
328 return;
329 }
330
331 {
332 unsigned elements = (uni->type->is_sampler())
333 ? 1 : uni->type->components();
334
335 /* Calculate the source base address *BEFORE* modifying elements to
336 * account for the size of the user's buffer.
337 */
338 const union gl_constant_value *const src =
339 &uni->storage[offset * elements];
340
341 assert(returnType == GLSL_TYPE_FLOAT || returnType == GLSL_TYPE_INT ||
342 returnType == GLSL_TYPE_UINT);
343 /* The three (currently) supported types all have the same size,
344 * which is of course the same as their union. That'll change
345 * with glGetUniformdv()...
346 */
347 unsigned bytes = sizeof(src[0]) * elements;
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
361 || returnType == GLSL_TYPE_UINT)
362 &&
363 (uni->type->base_type == GLSL_TYPE_INT
364 || uni->type->base_type == GLSL_TYPE_UINT
365 || uni->type->base_type == GLSL_TYPE_SAMPLER))) {
366 memcpy(paramsOut, src, bytes);
367 } else {
368 union gl_constant_value *const dst =
369 (union gl_constant_value *) paramsOut;
370
371 /* This code could be optimized by putting the loop inside the switch
372 * statements. However, this is not expected to be
373 * performance-critical code.
374 */
375 for (unsigned i = 0; i < elements; i++) {
376 switch (returnType) {
377 case GLSL_TYPE_FLOAT:
378 switch (uni->type->base_type) {
379 case GLSL_TYPE_UINT:
380 dst[i].f = (float) src[i].u;
381 break;
382 case GLSL_TYPE_INT:
383 case GLSL_TYPE_SAMPLER:
384 dst[i].f = (float) src[i].i;
385 break;
386 case GLSL_TYPE_BOOL:
387 dst[i].f = src[i].i ? 1.0f : 0.0f;
388 break;
389 default:
390 assert(!"Should not get here.");
391 break;
392 }
393 break;
394
395 case GLSL_TYPE_INT:
396 case GLSL_TYPE_UINT:
397 switch (uni->type->base_type) {
398 case GLSL_TYPE_FLOAT:
399 /* While the GL 3.2 core spec doesn't explicitly
400 * state how conversion of float uniforms to integer
401 * values works, in section 6.2 "State Tables" on
402 * page 267 it says:
403 *
404 * "Unless otherwise specified, when floating
405 * point state is returned as integer values or
406 * integer state is returned as floating-point
407 * values it is converted in the fashion
408 * described in section 6.1.2"
409 *
410 * That section, on page 248, says:
411 *
412 * "If GetIntegerv or GetInteger64v are called,
413 * a floating-point value is rounded to the
414 * nearest integer..."
415 */
416 dst[i].i = IROUND(src[i].f);
417 break;
418 case GLSL_TYPE_BOOL:
419 dst[i].i = src[i].i ? 1 : 0;
420 break;
421 default:
422 assert(!"Should not get here.");
423 break;
424 }
425 break;
426
427 default:
428 assert(!"Should not get here.");
429 break;
430 }
431 }
432 }
433 }
434 }
435
436 static void
437 log_uniform(const void *values, enum glsl_base_type basicType,
438 unsigned rows, unsigned cols, unsigned count,
439 bool transpose,
440 const struct gl_shader_program *shProg,
441 GLint location,
442 const struct gl_uniform_storage *uni)
443 {
444
445 const union gl_constant_value *v = (const union gl_constant_value *) values;
446 const unsigned elems = rows * cols * count;
447 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
448
449 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
450 "transpose = %s) to: ",
451 shProg->Name, extra, uni->name, location, uni->type->name,
452 transpose ? "true" : "false");
453 for (unsigned i = 0; i < elems; i++) {
454 if (i != 0 && ((i % rows) == 0))
455 printf(", ");
456
457 switch (basicType) {
458 case GLSL_TYPE_UINT:
459 printf("%u ", v[i].u);
460 break;
461 case GLSL_TYPE_INT:
462 printf("%d ", v[i].i);
463 break;
464 case GLSL_TYPE_FLOAT:
465 printf("%g ", v[i].f);
466 break;
467 default:
468 assert(!"Should not get here.");
469 break;
470 }
471 }
472 printf("\n");
473 fflush(stdout);
474 }
475
476 #if 0
477 static void
478 log_program_parameters(const struct gl_shader_program *shProg)
479 {
480 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
481 if (shProg->_LinkedShaders[i] == NULL)
482 continue;
483
484 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
485
486 printf("Program %d %s shader parameters:\n",
487 shProg->Name, _mesa_shader_stage_to_string(i));
488 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
489 printf("%s: %p %f %f %f %f\n",
490 prog->Parameters->Parameters[j].Name,
491 prog->Parameters->ParameterValues[j],
492 prog->Parameters->ParameterValues[j][0].f,
493 prog->Parameters->ParameterValues[j][1].f,
494 prog->Parameters->ParameterValues[j][2].f,
495 prog->Parameters->ParameterValues[j][3].f);
496 }
497 }
498 fflush(stdout);
499 }
500 #endif
501
502 /**
503 * Propagate some values from uniform backing storage to driver storage
504 *
505 * Values propagated from uniform backing storage to driver storage
506 * have all format / type conversions previously requested by the
507 * driver applied. This function is most often called by the
508 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
509 * etc.
510 *
511 * \param uni Uniform whose data is to be propagated to driver storage
512 * \param array_index If \c uni is an array, this is the element of
513 * the array to be propagated.
514 * \param count Number of array elements to propagate.
515 */
516 extern "C" void
517 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
518 unsigned array_index,
519 unsigned count)
520 {
521 unsigned i;
522
523 /* vector_elements and matrix_columns can be 0 for samplers.
524 */
525 const unsigned components = MAX2(1, uni->type->vector_elements);
526 const unsigned vectors = MAX2(1, uni->type->matrix_columns);
527
528 /* Store the data in the driver's requested type in the driver's storage
529 * areas.
530 */
531 unsigned src_vector_byte_stride = components * 4;
532
533 for (i = 0; i < uni->num_driver_storage; i++) {
534 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
535 uint8_t *dst = (uint8_t *) store->data;
536 const unsigned extra_stride =
537 store->element_stride - (vectors * store->vector_stride);
538 const uint8_t *src =
539 (uint8_t *) (&uni->storage[array_index * (components * vectors)].i);
540
541 #if 0
542 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
543 "extra_stride=%u\n",
544 __func__, dst, array_index, components,
545 vectors, count, store->vector_stride, extra_stride);
546 #endif
547
548 dst += array_index * store->element_stride;
549
550 switch (store->format) {
551 case uniform_native: {
552 unsigned j;
553 unsigned v;
554
555 for (j = 0; j < count; j++) {
556 for (v = 0; v < vectors; v++) {
557 memcpy(dst, src, src_vector_byte_stride);
558 src += src_vector_byte_stride;
559 dst += store->vector_stride;
560 }
561
562 dst += extra_stride;
563 }
564 break;
565 }
566
567 case uniform_int_float: {
568 const int *isrc = (const int *) src;
569 unsigned j;
570 unsigned v;
571 unsigned c;
572
573 for (j = 0; j < count; j++) {
574 for (v = 0; v < vectors; v++) {
575 for (c = 0; c < components; c++) {
576 ((float *) dst)[c] = (float) *isrc;
577 isrc++;
578 }
579
580 dst += store->vector_stride;
581 }
582
583 dst += extra_stride;
584 }
585 break;
586 }
587
588 default:
589 assert(!"Should not get here.");
590 break;
591 }
592 }
593 }
594
595 /**
596 * Called via glUniform*() functions.
597 */
598 extern "C" void
599 _mesa_uniform(struct gl_context *ctx, struct gl_shader_program *shProg,
600 GLint location, GLsizei count,
601 const GLvoid *values,
602 enum glsl_base_type basicType,
603 unsigned src_components)
604 {
605 unsigned offset;
606
607 struct gl_uniform_storage *const uni =
608 validate_uniform_parameters(ctx, shProg, location, count,
609 &offset, "glUniform");
610 if (uni == NULL)
611 return;
612
613 /* Verify that the types are compatible.
614 */
615 const unsigned components = uni->type->is_sampler()
616 ? 1 : uni->type->vector_elements;
617
618 bool match;
619 switch (uni->type->base_type) {
620 case GLSL_TYPE_BOOL:
621 match = true;
622 break;
623 case GLSL_TYPE_SAMPLER:
624 case GLSL_TYPE_IMAGE:
625 match = (basicType == GLSL_TYPE_INT);
626 break;
627 default:
628 match = (basicType == uni->type->base_type);
629 break;
630 }
631
632 if (uni->type->is_matrix() || components != src_components || !match) {
633 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
634 return;
635 }
636
637 if (ctx->_Shader->Flags & GLSL_UNIFORMS) {
638 log_uniform(values, basicType, components, 1, count,
639 false, shProg, location, uni);
640 }
641
642 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
643 *
644 * "Setting a sampler's value to i selects texture image unit number
645 * i. The values of i range from zero to the implementation- dependent
646 * maximum supported number of texture image units."
647 *
648 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
649 * the PDF) says:
650 *
651 * "Error Description Offending command
652 * ignored?
653 * ...
654 * INVALID_VALUE Numeric argument out of range Yes"
655 *
656 * Based on that, when an invalid sampler is specified, we generate a
657 * GL_INVALID_VALUE error and ignore the command.
658 */
659 if (uni->type->is_sampler()) {
660 for (int i = 0; i < count; i++) {
661 const unsigned texUnit = ((unsigned *) values)[i];
662
663 /* check that the sampler (tex unit index) is legal */
664 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
665 _mesa_error(ctx, GL_INVALID_VALUE,
666 "glUniform1i(invalid sampler/tex unit index for "
667 "uniform %d)",
668 location);
669 return;
670 }
671 }
672 }
673
674 if (uni->type->is_image()) {
675 for (int i = 0; i < count; i++) {
676 const int unit = ((GLint *) values)[i];
677
678 /* check that the image unit is legal */
679 if (unit < 0 || unit >= (int)ctx->Const.MaxImageUnits) {
680 _mesa_error(ctx, GL_INVALID_VALUE,
681 "glUniform1i(invalid image unit index for uniform %d)",
682 location);
683 return;
684 }
685 }
686 }
687
688 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
689 *
690 * "When loading N elements starting at an arbitrary position k in a
691 * uniform declared as an array, elements k through k + N - 1 in the
692 * array will be replaced with the new values. Values for any array
693 * element that exceeds the highest array element index used, as
694 * reported by GetActiveUniform, will be ignored by the GL."
695 *
696 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
697 * will have already generated an error.
698 */
699 if (uni->array_elements != 0) {
700 count = MIN2(count, (int) (uni->array_elements - offset));
701 }
702
703 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
704
705 /* Store the data in the "actual type" backing storage for the uniform.
706 */
707 if (!uni->type->is_boolean()) {
708 memcpy(&uni->storage[components * offset], values,
709 sizeof(uni->storage[0]) * components * count);
710 } else {
711 const union gl_constant_value *src =
712 (const union gl_constant_value *) values;
713 union gl_constant_value *dst = &uni->storage[components * offset];
714 const unsigned elems = components * count;
715
716 for (unsigned i = 0; i < elems; i++) {
717 if (basicType == GLSL_TYPE_FLOAT) {
718 dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
719 } else {
720 dst[i].i = src[i].i != 0 ? ctx->Const.UniformBooleanTrue : 0;
721 }
722 }
723 }
724
725 uni->initialized = true;
726
727 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
728
729 /* If the uniform is a sampler, do the extra magic necessary to propagate
730 * the changes through.
731 */
732 if (uni->type->is_sampler()) {
733 bool flushed = false;
734 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
735 struct gl_shader *const sh = shProg->_LinkedShaders[i];
736
737 /* If the shader stage doesn't use the sampler uniform, skip this.
738 */
739 if (sh == NULL || !uni->sampler[i].active)
740 continue;
741
742 for (int j = 0; j < count; j++) {
743 sh->SamplerUnits[uni->sampler[i].index + offset + j] =
744 ((unsigned *) values)[j];
745 }
746
747 struct gl_program *const prog = sh->Program;
748
749 assert(sizeof(prog->SamplerUnits) == sizeof(sh->SamplerUnits));
750
751 /* Determine if any of the samplers used by this shader stage have
752 * been modified.
753 */
754 bool changed = false;
755 for (unsigned j = 0; j < Elements(prog->SamplerUnits); j++) {
756 if ((sh->active_samplers & (1U << j)) != 0
757 && (prog->SamplerUnits[j] != sh->SamplerUnits[j])) {
758 changed = true;
759 break;
760 }
761 }
762
763 if (changed) {
764 if (!flushed) {
765 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
766 flushed = true;
767 }
768
769 memcpy(prog->SamplerUnits,
770 sh->SamplerUnits,
771 sizeof(sh->SamplerUnits));
772
773 _mesa_update_shader_textures_used(shProg, prog);
774 if (ctx->Driver.SamplerUniformChange)
775 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
776 }
777 }
778 }
779
780 /* If the uniform is an image, update the mapping from image
781 * uniforms to image units present in the shader data structure.
782 */
783 if (uni->type->is_image()) {
784 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
785 if (uni->image[i].active) {
786 struct gl_shader *sh = shProg->_LinkedShaders[i];
787
788 for (int j = 0; j < count; j++)
789 sh->ImageUnits[uni->image[i].index + offset + j] =
790 ((GLint *) values)[j];
791 }
792 }
793
794 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
795 }
796 }
797
798 /**
799 * Called by glUniformMatrix*() functions.
800 * Note: cols=2, rows=4 ==> array[2] of vec4
801 */
802 extern "C" void
803 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
804 GLuint cols, GLuint rows,
805 GLint location, GLsizei count,
806 GLboolean transpose, const GLfloat *values)
807 {
808 unsigned offset;
809 unsigned vectors;
810 unsigned components;
811 unsigned elements;
812
813 struct gl_uniform_storage *const uni =
814 validate_uniform_parameters(ctx, shProg, location, count,
815 &offset, "glUniformMatrix");
816 if (uni == NULL)
817 return;
818
819 if (!uni->type->is_matrix()) {
820 _mesa_error(ctx, GL_INVALID_OPERATION,
821 "glUniformMatrix(non-matrix uniform)");
822 return;
823 }
824
825 assert(!uni->type->is_sampler());
826 vectors = uni->type->matrix_columns;
827 components = uni->type->vector_elements;
828
829 /* Verify that the types are compatible. This is greatly simplified for
830 * matrices because they can only have a float base type.
831 */
832 if (vectors != cols || components != rows) {
833 _mesa_error(ctx, GL_INVALID_OPERATION,
834 "glUniformMatrix(matrix size mismatch)");
835 return;
836 }
837
838 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
839 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
840 */
841 if (transpose) {
842 if (ctx->API == API_OPENGLES2 && ctx->Version < 30) {
843 _mesa_error(ctx, GL_INVALID_VALUE,
844 "glUniformMatrix(matrix transpose is not GL_FALSE)");
845 return;
846 }
847 }
848
849 if (ctx->_Shader->Flags & GLSL_UNIFORMS) {
850 log_uniform(values, GLSL_TYPE_FLOAT, components, vectors, count,
851 bool(transpose), shProg, location, uni);
852 }
853
854 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
855 *
856 * "When loading N elements starting at an arbitrary position k in a
857 * uniform declared as an array, elements k through k + N - 1 in the
858 * array will be replaced with the new values. Values for any array
859 * element that exceeds the highest array element index used, as
860 * reported by GetActiveUniform, will be ignored by the GL."
861 *
862 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
863 * will have already generated an error.
864 */
865 if (uni->array_elements != 0) {
866 count = MIN2(count, (int) (uni->array_elements - offset));
867 }
868
869 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
870
871 /* Store the data in the "actual type" backing storage for the uniform.
872 */
873 elements = components * vectors;
874
875 if (!transpose) {
876 memcpy(&uni->storage[elements * offset], values,
877 sizeof(uni->storage[0]) * elements * count);
878 } else {
879 /* Copy and transpose the matrix.
880 */
881 const float *src = values;
882 float *dst = &uni->storage[elements * offset].f;
883
884 for (int i = 0; i < count; i++) {
885 for (unsigned r = 0; r < rows; r++) {
886 for (unsigned c = 0; c < cols; c++) {
887 dst[(c * components) + r] = src[c + (r * vectors)];
888 }
889 }
890
891 dst += elements;
892 src += elements;
893 }
894 }
895
896 uni->initialized = true;
897
898 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
899 }
900
901
902 /**
903 * Called via glGetUniformLocation().
904 *
905 * Returns the uniform index into UniformStorage (also the
906 * glGetActiveUniformsiv uniform index), and stores the referenced
907 * array offset in *offset, or GL_INVALID_INDEX (-1).
908 */
909 extern "C" unsigned
910 _mesa_get_uniform_location(struct gl_shader_program *shProg,
911 const GLchar *name,
912 unsigned *out_offset)
913 {
914 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
915 *
916 * "The first element of a uniform array is identified using the
917 * name of the uniform array appended with "[0]". Except if the last
918 * part of the string name indicates a uniform array, then the
919 * location of the first element of that array can be retrieved by
920 * either using the name of the uniform array, or the name of the
921 * uniform array appended with "[0]"."
922 *
923 * Note: since uniform names are not allowed to use whitespace, and array
924 * indices within uniform names are not allowed to use "+", "-", or leading
925 * zeros, it follows that each uniform has a unique name up to the possible
926 * ambiguity with "[0]" noted above. Therefore we don't need to worry
927 * about mal-formed inputs--they will properly fail when we try to look up
928 * the uniform name in shProg->UniformHash.
929 */
930
931 const GLchar *base_name_end;
932 long offset = parse_program_resource_name(name, &base_name_end);
933 bool array_lookup = offset >= 0;
934 char *name_copy;
935
936 if (array_lookup) {
937 name_copy = (char *) malloc(base_name_end - name + 1);
938 memcpy(name_copy, name, base_name_end - name);
939 name_copy[base_name_end - name] = '\0';
940 } else {
941 name_copy = (char *) name;
942 offset = 0;
943 }
944
945 unsigned location = 0;
946 const bool found = shProg->UniformHash->get(location, name_copy);
947
948 assert(!found
949 || strcmp(name_copy, shProg->UniformStorage[location].name) == 0);
950
951 /* Free the temporary buffer *before* possibly returning an error.
952 */
953 if (name_copy != name)
954 free(name_copy);
955
956 if (!found)
957 return GL_INVALID_INDEX;
958
959 /* If the uniform is an array, fail if the index is out of bounds.
960 * (A negative index is caught above.) This also fails if the uniform
961 * is not an array, but the user is trying to index it, because
962 * array_elements is zero and offset >= 0.
963 */
964 if (array_lookup
965 && offset >= (long) shProg->UniformStorage[location].array_elements) {
966 return GL_INVALID_INDEX;
967 }
968
969 *out_offset = offset;
970 return location;
971 }
972
973 extern "C" bool
974 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
975 char *errMsg, size_t errMsgLength)
976 {
977 /* Shader does not have samplers. */
978 if (shProg->NumUserUniformStorage == 0)
979 return true;
980
981 if (!shProg->SamplersValidated) {
982 _mesa_snprintf(errMsg, errMsgLength,
983 "active samplers with a different type "
984 "refer to the same texture image unit");
985 return false;
986 }
987 return true;
988 }
989
990 extern "C" bool
991 _mesa_sampler_uniforms_pipeline_are_valid(struct gl_pipeline_object *pipeline)
992 {
993 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
994 * OpenGL 4.1 spec says:
995 *
996 * "[INVALID_OPERATION] is generated by any command that transfers
997 * vertices to the GL if:
998 *
999 * ...
1000 *
1001 * - Any two active samplers in the current program object are of
1002 * different types, but refer to the same texture image unit.
1003 *
1004 * - The number of active samplers in the program exceeds the
1005 * maximum number of texture image units allowed."
1006 */
1007 unsigned active_samplers = 0;
1008 const struct gl_shader_program **shProg =
1009 (const struct gl_shader_program **) pipeline->CurrentProgram;
1010
1011 const glsl_type *unit_types[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1012 memset(unit_types, 0, sizeof(unit_types));
1013
1014 for (unsigned idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1015 if (!shProg[idx])
1016 continue;
1017
1018 for (unsigned i = 0; i < shProg[idx]->NumUserUniformStorage; i++) {
1019 const struct gl_uniform_storage *const storage =
1020 &shProg[idx]->UniformStorage[i];
1021 const glsl_type *const t = (storage->type->is_array())
1022 ? storage->type->fields.array : storage->type;
1023
1024 if (!t->is_sampler())
1025 continue;
1026
1027 active_samplers++;
1028
1029 const unsigned count = MAX2(1, storage->type->array_size());
1030 for (unsigned j = 0; j < count; j++) {
1031 const unsigned unit = storage->storage[j].i;
1032
1033 /* The types of the samplers associated with a particular texture
1034 * unit must be an exact match. Page 74 (page 89 of the PDF) of
1035 * the OpenGL 3.3 core spec says:
1036 *
1037 * "It is not allowed to have variables of different sampler
1038 * types pointing to the same texture image unit within a
1039 * program object."
1040 */
1041 if (unit_types[unit] == NULL) {
1042 unit_types[unit] = t;
1043 } else if (unit_types[unit] != t) {
1044 pipeline->InfoLog =
1045 ralloc_asprintf(pipeline,
1046 "Texture unit %d is accessed both as %s "
1047 "and %s",
1048 unit, unit_types[unit]->name, t->name);
1049 return false;
1050 }
1051 }
1052 }
1053 }
1054
1055 if (active_samplers > MAX_COMBINED_TEXTURE_IMAGE_UNITS) {
1056 pipeline->InfoLog =
1057 ralloc_asprintf(pipeline,
1058 "the number of active samplers %d exceed the "
1059 "maximum %d",
1060 active_samplers, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1061 return false;
1062 }
1063
1064 return true;
1065 }