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