mesa/sso: Add extension entry points for GL_ARB_separate_shader_objects
[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_shader_stage_to_string(i));
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 case GLSL_TYPE_IMAGE:
688 match = (basicType == GLSL_TYPE_INT);
689 break;
690 default:
691 match = (basicType == uni->type->base_type);
692 break;
693 }
694
695 if (uni->type->is_matrix() || components != src_components || !match) {
696 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
697 return;
698 }
699
700 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
701 log_uniform(values, basicType, components, 1, count,
702 false, shProg, location, uni);
703 }
704
705 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
706 *
707 * "Setting a sampler's value to i selects texture image unit number
708 * i. The values of i range from zero to the implementation- dependent
709 * maximum supported number of texture image units."
710 *
711 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
712 * the PDF) says:
713 *
714 * "Error Description Offending command
715 * ignored?
716 * ...
717 * INVALID_VALUE Numeric argument out of range Yes"
718 *
719 * Based on that, when an invalid sampler is specified, we generate a
720 * GL_INVALID_VALUE error and ignore the command.
721 */
722 if (uni->type->is_sampler()) {
723 int i;
724
725 for (i = 0; i < count; i++) {
726 const unsigned texUnit = ((unsigned *) values)[i];
727
728 /* check that the sampler (tex unit index) is legal */
729 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
730 _mesa_error(ctx, GL_INVALID_VALUE,
731 "glUniform1i(invalid sampler/tex unit index for "
732 "uniform %d)",
733 location);
734 return;
735 }
736 }
737 }
738
739 if (uni->type->is_image()) {
740 int i;
741
742 for (i = 0; i < count; i++) {
743 const int unit = ((GLint *) values)[i];
744
745 /* check that the image unit is legal */
746 if (unit < 0 || unit >= (int)ctx->Const.MaxImageUnits) {
747 _mesa_error(ctx, GL_INVALID_VALUE,
748 "glUniform1i(invalid image unit index for uniform %d)",
749 location);
750 return;
751 }
752 }
753 }
754
755 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
756 *
757 * "When loading N elements starting at an arbitrary position k in a
758 * uniform declared as an array, elements k through k + N - 1 in the
759 * array will be replaced with the new values. Values for any array
760 * element that exceeds the highest array element index used, as
761 * reported by GetActiveUniform, will be ignored by the GL."
762 *
763 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
764 * will have already generated an error.
765 */
766 if (uni->array_elements != 0) {
767 count = MIN2(count, (int) (uni->array_elements - offset));
768 }
769
770 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
771
772 /* Store the data in the "actual type" backing storage for the uniform.
773 */
774 if (!uni->type->is_boolean()) {
775 memcpy(&uni->storage[components * offset], values,
776 sizeof(uni->storage[0]) * components * count);
777 } else {
778 const union gl_constant_value *src =
779 (const union gl_constant_value *) values;
780 union gl_constant_value *dst = &uni->storage[components * offset];
781 const unsigned elems = components * count;
782 unsigned i;
783
784 for (i = 0; i < elems; i++) {
785 if (basicType == GLSL_TYPE_FLOAT) {
786 dst[i].i = src[i].f != 0.0f ? 1 : 0;
787 } else {
788 dst[i].i = src[i].i != 0 ? 1 : 0;
789 }
790 }
791 }
792
793 uni->initialized = true;
794
795 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
796
797 /* If the uniform is a sampler, do the extra magic necessary to propagate
798 * the changes through.
799 */
800 if (uni->type->is_sampler()) {
801 int i;
802
803 bool flushed = false;
804 for (i = 0; i < MESA_SHADER_STAGES; i++) {
805 struct gl_shader *const sh = shProg->_LinkedShaders[i];
806 int j;
807
808 /* If the shader stage doesn't use the sampler uniform, skip this.
809 */
810 if (sh == NULL || !uni->sampler[i].active)
811 continue;
812
813 for (j = 0; j < count; j++) {
814 sh->SamplerUnits[uni->sampler[i].index + offset + j] =
815 ((unsigned *) values)[j];
816 }
817
818 struct gl_program *const prog = sh->Program;
819
820 assert(sizeof(prog->SamplerUnits) == sizeof(sh->SamplerUnits));
821
822 /* Determine if any of the samplers used by this shader stage have
823 * been modified.
824 */
825 bool changed = false;
826 for (unsigned j = 0; j < Elements(prog->SamplerUnits); j++) {
827 if ((sh->active_samplers & (1U << j)) != 0
828 && (prog->SamplerUnits[j] != sh->SamplerUnits[j])) {
829 changed = true;
830 break;
831 }
832 }
833
834 if (changed) {
835 if (!flushed) {
836 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
837 flushed = true;
838 }
839
840 memcpy(prog->SamplerUnits,
841 sh->SamplerUnits,
842 sizeof(sh->SamplerUnits));
843
844 _mesa_update_shader_textures_used(shProg, prog);
845 if (ctx->Driver.SamplerUniformChange)
846 ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
847 }
848 }
849 }
850
851 /* If the uniform is an image, update the mapping from image
852 * uniforms to image units present in the shader data structure.
853 */
854 if (uni->type->is_image()) {
855 int i, j;
856
857 for (i = 0; i < MESA_SHADER_STAGES; i++) {
858 if (uni->image[i].active) {
859 struct gl_shader *sh = shProg->_LinkedShaders[i];
860
861 for (j = 0; j < count; j++)
862 sh->ImageUnits[uni->image[i].index + offset + j] =
863 ((GLint *) values)[j];
864 }
865 }
866
867 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
868 }
869 }
870
871 /**
872 * Called by glUniformMatrix*() functions.
873 * Note: cols=2, rows=4 ==> array[2] of vec4
874 */
875 extern "C" void
876 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
877 GLuint cols, GLuint rows,
878 GLint location, GLsizei count,
879 GLboolean transpose, const GLfloat *values)
880 {
881 unsigned loc, offset;
882 unsigned vectors;
883 unsigned components;
884 unsigned elements;
885 struct gl_uniform_storage *uni;
886
887 if (!validate_uniform_parameters(ctx, shProg, location, count,
888 &loc, &offset, "glUniformMatrix", false))
889 return;
890
891 uni = &shProg->UniformStorage[loc];
892 if (!uni->type->is_matrix()) {
893 _mesa_error(ctx, GL_INVALID_OPERATION,
894 "glUniformMatrix(non-matrix uniform)");
895 return;
896 }
897
898 assert(!uni->type->is_sampler());
899 vectors = uni->type->matrix_columns;
900 components = uni->type->vector_elements;
901
902 /* Verify that the types are compatible. This is greatly simplified for
903 * matrices because they can only have a float base type.
904 */
905 if (vectors != cols || components != rows) {
906 _mesa_error(ctx, GL_INVALID_OPERATION,
907 "glUniformMatrix(matrix size mismatch)");
908 return;
909 }
910
911 /* GL_INVALID_VALUE is generated if `transpose' is not GL_FALSE.
912 * http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml */
913 if (ctx->API == API_OPENGLES
914 || (ctx->API == API_OPENGLES2 && ctx->Version < 30)) {
915 if (transpose) {
916 _mesa_error(ctx, GL_INVALID_VALUE,
917 "glUniformMatrix(matrix transpose is not GL_FALSE)");
918 return;
919 }
920 }
921
922 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
923 log_uniform(values, GLSL_TYPE_FLOAT, components, vectors, count,
924 bool(transpose), shProg, location, uni);
925 }
926
927 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
928 *
929 * "When loading N elements starting at an arbitrary position k in a
930 * uniform declared as an array, elements k through k + N - 1 in the
931 * array will be replaced with the new values. Values for any array
932 * element that exceeds the highest array element index used, as
933 * reported by GetActiveUniform, will be ignored by the GL."
934 *
935 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
936 * will have already generated an error.
937 */
938 if (uni->array_elements != 0) {
939 count = MIN2(count, (int) (uni->array_elements - offset));
940 }
941
942 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
943
944 /* Store the data in the "actual type" backing storage for the uniform.
945 */
946 elements = components * vectors;
947
948 if (!transpose) {
949 memcpy(&uni->storage[elements * offset], values,
950 sizeof(uni->storage[0]) * elements * count);
951 } else {
952 /* Copy and transpose the matrix.
953 */
954 const float *src = values;
955 float *dst = &uni->storage[elements * offset].f;
956
957 for (int i = 0; i < count; i++) {
958 for (unsigned r = 0; r < rows; r++) {
959 for (unsigned c = 0; c < cols; c++) {
960 dst[(c * components) + r] = src[c + (r * vectors)];
961 }
962 }
963
964 dst += elements;
965 src += elements;
966 }
967 }
968
969 uni->initialized = true;
970
971 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
972 }
973
974
975 /**
976 * Called via glGetUniformLocation().
977 *
978 * Returns the uniform index into UniformStorage (also the
979 * glGetActiveUniformsiv uniform index), and stores the referenced
980 * array offset in *offset, or GL_INVALID_INDEX (-1). Those two
981 * return values can be encoded into a uniform location for
982 * glUniform* using _mesa_uniform_merge_location_offset(index, offset).
983 */
984 extern "C" unsigned
985 _mesa_get_uniform_location(struct gl_context *ctx,
986 struct gl_shader_program *shProg,
987 const GLchar *name,
988 unsigned *out_offset)
989 {
990 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
991 *
992 * "The first element of a uniform array is identified using the
993 * name of the uniform array appended with "[0]". Except if the last
994 * part of the string name indicates a uniform array, then the
995 * location of the first element of that array can be retrieved by
996 * either using the name of the uniform array, or the name of the
997 * uniform array appended with "[0]"."
998 *
999 * Note: since uniform names are not allowed to use whitespace, and array
1000 * indices within uniform names are not allowed to use "+", "-", or leading
1001 * zeros, it follows that each uniform has a unique name up to the possible
1002 * ambiguity with "[0]" noted above. Therefore we don't need to worry
1003 * about mal-formed inputs--they will properly fail when we try to look up
1004 * the uniform name in shProg->UniformHash.
1005 */
1006
1007 const GLchar *base_name_end;
1008 long offset = parse_program_resource_name(name, &base_name_end);
1009 bool array_lookup = offset >= 0;
1010 char *name_copy;
1011
1012 if (array_lookup) {
1013 name_copy = (char *) malloc(base_name_end - name + 1);
1014 memcpy(name_copy, name, base_name_end - name);
1015 name_copy[base_name_end - name] = '\0';
1016 } else {
1017 name_copy = (char *) name;
1018 offset = 0;
1019 }
1020
1021 unsigned location = 0;
1022 const bool found = shProg->UniformHash->get(location, name_copy);
1023
1024 assert(!found
1025 || strcmp(name_copy, shProg->UniformStorage[location].name) == 0);
1026
1027 /* Free the temporary buffer *before* possibly returning an error.
1028 */
1029 if (name_copy != name)
1030 free(name_copy);
1031
1032 if (!found)
1033 return GL_INVALID_INDEX;
1034
1035 /* If the uniform is an array, fail if the index is out of bounds.
1036 * (A negative index is caught above.) This also fails if the uniform
1037 * is not an array, but the user is trying to index it, because
1038 * array_elements is zero and offset >= 0.
1039 */
1040 if (array_lookup
1041 && offset >= (long) shProg->UniformStorage[location].array_elements) {
1042 return GL_INVALID_INDEX;
1043 }
1044
1045 *out_offset = offset;
1046 return location;
1047 }
1048
1049 extern "C" bool
1050 _mesa_sampler_uniforms_are_valid(const struct gl_shader_program *shProg,
1051 char *errMsg, size_t errMsgLength)
1052 {
1053 const glsl_type *unit_types[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
1054
1055 memset(unit_types, 0, sizeof(unit_types));
1056
1057 for (unsigned i = 0; i < shProg->NumUserUniformStorage; i++) {
1058 const struct gl_uniform_storage *const storage =
1059 &shProg->UniformStorage[i];
1060 const glsl_type *const t = (storage->type->is_array())
1061 ? storage->type->fields.array : storage->type;
1062
1063 if (!t->is_sampler())
1064 continue;
1065
1066 const unsigned count = MAX2(1, storage->type->array_size());
1067 for (unsigned j = 0; j < count; j++) {
1068 const unsigned unit = storage->storage[j].i;
1069
1070 /* The types of the samplers associated with a particular texture
1071 * unit must be an exact match. Page 74 (page 89 of the PDF) of the
1072 * OpenGL 3.3 core spec says:
1073 *
1074 * "It is not allowed to have variables of different sampler
1075 * types pointing to the same texture image unit within a program
1076 * object."
1077 */
1078 if (unit_types[unit] == NULL) {
1079 unit_types[unit] = t;
1080 } else if (unit_types[unit] != t) {
1081 _mesa_snprintf(errMsg, errMsgLength,
1082 "Texture unit %d is accessed both as %s and %s",
1083 unit, unit_types[unit]->name, t->name);
1084 return false;
1085 }
1086 }
1087 }
1088
1089 return true;
1090 }