debe3ae596340de6e44a3257827303496f635f82
[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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include <stdlib.h>
26 #include "main/core.h"
27 #include "main/context.h"
28 #include "ir.h"
29 #include "ir_uniform.h"
30 #include "program/hash_table.h"
31 #include "../glsl/program.h"
32 #include "../glsl/ir_uniform.h"
33
34 extern "C" {
35 #include "main/shaderapi.h"
36 #include "main/shaderobj.h"
37 #include "uniforms.h"
38 }
39
40 extern "C" void GLAPIENTRY
41 _mesa_GetActiveUniformARB(GLhandleARB program, GLuint index,
42 GLsizei maxLength, GLsizei *length, GLint *size,
43 GLenum *type, GLcharARB *nameOut)
44 {
45 GET_CURRENT_CONTEXT(ctx);
46 struct gl_shader_program *shProg =
47 _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
48
49 if (!shProg)
50 return;
51
52 if (index >= shProg->NumUserUniformStorage) {
53 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
54 return;
55 }
56
57 const struct gl_uniform_storage *const uni = &shProg->UniformStorage[index];
58
59 if (nameOut) {
60 _mesa_copy_string(nameOut, maxLength, length, uni->name);
61 }
62
63 if (size) {
64 /* array_elements is zero for non-arrays, but the API requires that 1 be
65 * returned.
66 */
67 *size = MAX2(1, uni->array_elements);
68 }
69
70 if (type) {
71 *type = uni->type->gl_type;
72 }
73 }
74
75 static GLenum
76 base_uniform_type(GLenum type)
77 {
78 switch (type) {
79 case GL_BOOL:
80 case GL_BOOL_VEC2:
81 case GL_BOOL_VEC3:
82 case GL_BOOL_VEC4:
83 return GL_BOOL;
84 case GL_FLOAT:
85 case GL_FLOAT_VEC2:
86 case GL_FLOAT_VEC3:
87 case GL_FLOAT_VEC4:
88 case GL_FLOAT_MAT2:
89 case GL_FLOAT_MAT2x3:
90 case GL_FLOAT_MAT2x4:
91 case GL_FLOAT_MAT3x2:
92 case GL_FLOAT_MAT3:
93 case GL_FLOAT_MAT3x4:
94 case GL_FLOAT_MAT4x2:
95 case GL_FLOAT_MAT4x3:
96 case GL_FLOAT_MAT4:
97 return GL_FLOAT;
98 case GL_UNSIGNED_INT:
99 case GL_UNSIGNED_INT_VEC2:
100 case GL_UNSIGNED_INT_VEC3:
101 case GL_UNSIGNED_INT_VEC4:
102 return GL_UNSIGNED_INT;
103 case GL_INT:
104 case GL_INT_VEC2:
105 case GL_INT_VEC3:
106 case GL_INT_VEC4:
107 return GL_INT;
108 default:
109 _mesa_problem(NULL, "Invalid type in base_uniform_type()");
110 return GL_FLOAT;
111 }
112 }
113
114 static GLboolean
115 is_boolean_type(GLenum type)
116 {
117 switch (type) {
118 case GL_BOOL:
119 case GL_BOOL_VEC2:
120 case GL_BOOL_VEC3:
121 case GL_BOOL_VEC4:
122 return GL_TRUE;
123 default:
124 return GL_FALSE;
125 }
126 }
127
128 static GLboolean
129 is_sampler_type(GLenum type)
130 {
131 switch (type) {
132 case GL_SAMPLER_1D:
133 case GL_INT_SAMPLER_1D:
134 case GL_UNSIGNED_INT_SAMPLER_1D:
135 case GL_SAMPLER_2D:
136 case GL_INT_SAMPLER_2D:
137 case GL_UNSIGNED_INT_SAMPLER_2D:
138 case GL_SAMPLER_3D:
139 case GL_INT_SAMPLER_3D:
140 case GL_UNSIGNED_INT_SAMPLER_3D:
141 case GL_SAMPLER_CUBE:
142 case GL_INT_SAMPLER_CUBE:
143 case GL_UNSIGNED_INT_SAMPLER_CUBE:
144 case GL_SAMPLER_1D_SHADOW:
145 case GL_SAMPLER_2D_SHADOW:
146 case GL_SAMPLER_CUBE_SHADOW:
147 case GL_SAMPLER_2D_RECT_ARB:
148 case GL_INT_SAMPLER_2D_RECT:
149 case GL_UNSIGNED_INT_SAMPLER_2D_RECT:
150 case GL_SAMPLER_2D_RECT_SHADOW_ARB:
151 case GL_SAMPLER_1D_ARRAY_EXT:
152 case GL_INT_SAMPLER_1D_ARRAY:
153 case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY:
154 case GL_SAMPLER_2D_ARRAY_EXT:
155 case GL_INT_SAMPLER_2D_ARRAY:
156 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
157 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
158 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
159 case GL_SAMPLER_CUBE_MAP_ARRAY:
160 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
161 case GL_SAMPLER_BUFFER:
162 case GL_INT_SAMPLER_BUFFER:
163 case GL_UNSIGNED_INT_SAMPLER_BUFFER:
164 case GL_SAMPLER_2D_MULTISAMPLE:
165 case GL_INT_SAMPLER_2D_MULTISAMPLE:
166 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
167 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
168 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
169 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
170 case GL_SAMPLER_EXTERNAL_OES:
171 return GL_TRUE;
172 default:
173 return GL_FALSE;
174 }
175 }
176
177 /**
178 * Given a uniform index, return the vertex/geometry/fragment program
179 * that has that parameter, plus the position of the parameter in the
180 * parameter/constant buffer.
181 * \param shProg the shader program
182 * \param index the uniform index in [0, NumUniforms-1]
183 * \param progOut returns containing program
184 * \param posOut returns position of the uniform in the param/const buffer
185 * \return GL_TRUE for success, GL_FALSE for invalid index
186 */
187 static GLboolean
188 find_uniform_parameter_pos(struct gl_shader_program *shProg, GLint index,
189 struct gl_program **progOut, GLint *posOut)
190 {
191 struct gl_program *prog = NULL;
192 GLint pos;
193
194 if (!shProg->Uniforms ||
195 index < 0 ||
196 index >= (GLint) shProg->Uniforms->NumUniforms) {
197 return GL_FALSE;
198 }
199
200 pos = shProg->Uniforms->Uniforms[index].VertPos;
201 if (pos >= 0) {
202 prog = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program;
203 }
204 else {
205 pos = shProg->Uniforms->Uniforms[index].FragPos;
206 if (pos >= 0) {
207 prog = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program;
208 }
209 else {
210 pos = shProg->Uniforms->Uniforms[index].GeomPos;
211 if (pos >= 0) {
212 prog = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program;
213 }
214 }
215 }
216
217 if (!prog || pos < 0)
218 return GL_FALSE; /* should really never happen */
219
220 *progOut = prog;
221 *posOut = pos;
222
223 return GL_TRUE;
224 }
225
226 /**
227 * Return pointer to a gl_program_parameter which corresponds to a uniform.
228 * \param shProg the shader program
229 * \param index the uniform index in [0, NumUniforms-1]
230 * \return gl_program_parameter point or NULL if index is invalid
231 */
232 const struct gl_program_parameter *
233 get_uniform_parameter(struct gl_shader_program *shProg, GLint index)
234 {
235 struct gl_program *prog;
236 GLint progPos;
237
238 if (find_uniform_parameter_pos(shProg, index, &prog, &progPos))
239 return &prog->Parameters->Parameters[progPos];
240 else
241 return NULL;
242 }
243
244 static unsigned
245 get_vector_elements(GLenum type)
246 {
247 switch (type) {
248 case GL_FLOAT:
249 case GL_INT:
250 case GL_BOOL:
251 case GL_UNSIGNED_INT:
252 default: /* Catch all the various sampler types. */
253 return 1;
254
255 case GL_FLOAT_VEC2:
256 case GL_INT_VEC2:
257 case GL_BOOL_VEC2:
258 case GL_UNSIGNED_INT_VEC2:
259 return 2;
260
261 case GL_FLOAT_VEC3:
262 case GL_INT_VEC3:
263 case GL_BOOL_VEC3:
264 case GL_UNSIGNED_INT_VEC3:
265 return 3;
266
267 case GL_FLOAT_VEC4:
268 case GL_INT_VEC4:
269 case GL_BOOL_VEC4:
270 case GL_UNSIGNED_INT_VEC4:
271 return 4;
272 }
273 }
274
275 static void
276 get_matrix_dims(GLenum type, GLint *rows, GLint *cols)
277 {
278 switch (type) {
279 case GL_FLOAT_MAT2:
280 *rows = *cols = 2;
281 break;
282 case GL_FLOAT_MAT2x3:
283 *rows = 3;
284 *cols = 2;
285 break;
286 case GL_FLOAT_MAT2x4:
287 *rows = 4;
288 *cols = 2;
289 break;
290 case GL_FLOAT_MAT3:
291 *rows = 3;
292 *cols = 3;
293 break;
294 case GL_FLOAT_MAT3x2:
295 *rows = 2;
296 *cols = 3;
297 break;
298 case GL_FLOAT_MAT3x4:
299 *rows = 4;
300 *cols = 3;
301 break;
302 case GL_FLOAT_MAT4:
303 *rows = 4;
304 *cols = 4;
305 break;
306 case GL_FLOAT_MAT4x2:
307 *rows = 2;
308 *cols = 4;
309 break;
310 case GL_FLOAT_MAT4x3:
311 *rows = 3;
312 *cols = 4;
313 break;
314 default:
315 *rows = *cols = 0;
316 }
317 }
318
319 /**
320 * Determine the number of rows and columns occupied by a uniform
321 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
322 * the number of rows = 1 and cols = number of elements in the vector.
323 */
324 static void
325 get_uniform_rows_cols(const struct gl_program_parameter *p,
326 GLint *rows, GLint *cols)
327 {
328 get_matrix_dims(p->DataType, rows, cols);
329 if (*rows == 0 && *cols == 0) {
330 /* not a matrix type, probably a float or vector */
331 *rows = 1;
332 *cols = get_vector_elements(p->DataType);
333 }
334 }
335
336 static bool
337 validate_uniform_parameters(struct gl_context *ctx,
338 struct gl_shader_program *shProg,
339 GLint location, GLsizei count,
340 unsigned *loc,
341 unsigned *array_index,
342 const char *caller,
343 bool negative_one_is_not_valid)
344 {
345 if (!shProg || !shProg->LinkStatus) {
346 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(program not linked)", caller);
347 return false;
348 }
349
350 if (location == -1) {
351 /* For glGetUniform, page 264 (page 278 of the PDF) of the OpenGL 2.1
352 * spec says:
353 *
354 * "The error INVALID_OPERATION is generated if program has not been
355 * linked successfully, or if location is not a valid location for
356 * program."
357 *
358 * For glUniform, page 82 (page 96 of the PDF) of the OpenGL 2.1 spec
359 * says:
360 *
361 * "If the value of location is -1, the Uniform* commands will
362 * silently ignore the data passed in, and the current uniform
363 * values will not be changed."
364 *
365 * Allowing -1 for the location parameter of glUniform allows
366 * applications to avoid error paths in the case that, for example, some
367 * uniform variable is removed by the compiler / linker after
368 * optimization. In this case, the new value of the uniform is dropped
369 * on the floor. For the case of glGetUniform, there is nothing
370 * sensible to do for a location of -1.
371 *
372 * The negative_one_is_not_valid flag selects between the two behaviors.
373 */
374 if (negative_one_is_not_valid) {
375 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
376 caller, location);
377 }
378
379 return false;
380 }
381
382 /* From page 12 (page 26 of the PDF) of the OpenGL 2.1 spec:
383 *
384 * "If a negative number is provided where an argument of type sizei or
385 * sizeiptr is specified, the error INVALID_VALUE is generated."
386 */
387 if (count < 0) {
388 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count < 0)", caller);
389 return false;
390 }
391
392 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
393 *
394 * "If any of the following conditions occur, an INVALID_OPERATION
395 * error is generated by the Uniform* commands, and no uniform values
396 * are changed:
397 *
398 * ...
399 *
400 * - if no variable with a location of location exists in the
401 * program object currently in use and location is not -1,
402 * - if count is greater than one, and the uniform declared in the
403 * shader is not an array variable,
404 */
405 if (location < -1) {
406 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
407 caller, location);
408 return false;
409 }
410
411 _mesa_uniform_split_location_offset(location, loc, array_index);
412
413 if (*loc >= shProg->NumUserUniformStorage) {
414 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
415 caller, location);
416 return false;
417 }
418
419 if (shProg->UniformStorage[*loc].array_elements == 0 && count > 1) {
420 _mesa_error(ctx, GL_INVALID_OPERATION,
421 "%s(count > 1 for non-array, location=%d)",
422 caller, location);
423 return false;
424 }
425
426 /* This case should be impossible. The implication is that a call like
427 * glGetUniformLocation(prog, "foo[8]") was successful but "foo" is not an
428 * array.
429 */
430 if (*array_index != 0 && shProg->UniformStorage[*loc].array_elements == 0) {
431 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(location=%d)",
432 caller, location);
433 return false;
434 }
435 return true;
436 }
437
438 /**
439 * Called via glGetUniform[fiui]v() to get the current value of a uniform.
440 */
441 extern "C" void
442 _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
443 GLsizei bufSize, enum glsl_base_type returnType,
444 GLvoid *paramsOut)
445 {
446 struct gl_shader_program *shProg =
447 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformfv");
448 struct gl_uniform_storage *uni;
449 unsigned loc, offset;
450
451 if (!validate_uniform_parameters(ctx, shProg, location, 1,
452 &loc, &offset, "glGetUniform", true))
453 return;
454
455 uni = &shProg->UniformStorage[loc];
456
457 {
458 unsigned elements = (uni->type->is_sampler())
459 ? 1 : uni->type->components();
460
461 /* Calculate the source base address *BEFORE* modifying elements to
462 * account for the size of the user's buffer.
463 */
464 const union gl_constant_value *const src =
465 &uni->storage[offset * elements];
466
467 unsigned bytes = sizeof(uni->storage[0]) * elements;
468 if (bytes > bufSize) {
469 elements = bufSize / sizeof(uni->storage[0]);
470 bytes = bufSize;
471 }
472
473 /* If the return type and the uniform's native type are "compatible,"
474 * just memcpy the data. If the types are not compatible, perform a
475 * slower convert-and-copy process.
476 */
477 if (returnType == uni->type->base_type
478 || ((returnType == GLSL_TYPE_INT
479 || returnType == GLSL_TYPE_UINT
480 || returnType == GLSL_TYPE_SAMPLER)
481 &&
482 (uni->type->base_type == GLSL_TYPE_INT
483 || uni->type->base_type == GLSL_TYPE_UINT
484 || uni->type->base_type == GLSL_TYPE_SAMPLER))) {
485 memcpy(paramsOut, src, bytes);
486 } else {
487 union gl_constant_value *const dst =
488 (union gl_constant_value *) paramsOut;
489
490 /* This code could be optimized by putting the loop inside the switch
491 * statements. However, this is not expected to be
492 * performance-critical code.
493 */
494 for (unsigned i = 0; i < elements; i++) {
495 switch (returnType) {
496 case GLSL_TYPE_FLOAT:
497 switch (uni->type->base_type) {
498 case GLSL_TYPE_UINT:
499 dst[i].f = (float) src[i].u;
500 break;
501 case GLSL_TYPE_INT:
502 case GLSL_TYPE_SAMPLER:
503 dst[i].f = (float) src[i].i;
504 break;
505 case GLSL_TYPE_BOOL:
506 dst[i].f = src[i].i ? 1.0f : 0.0f;
507 break;
508 default:
509 assert(!"Should not get here.");
510 break;
511 }
512 break;
513
514 case GLSL_TYPE_INT:
515 case GLSL_TYPE_UINT:
516 switch (uni->type->base_type) {
517 case GLSL_TYPE_FLOAT:
518 /* While the GL 3.2 core spec doesn't explicitly
519 * state how conversion of float uniforms to integer
520 * values works, in section 6.2 "State Tables" on
521 * page 267 it says:
522 *
523 * "Unless otherwise specified, when floating
524 * point state is returned as integer values or
525 * integer state is returned as floating-point
526 * values it is converted in the fashion
527 * described in section 6.1.2"
528 *
529 * That section, on page 248, says:
530 *
531 * "If GetIntegerv or GetInteger64v are called,
532 * a floating-point value is rounded to the
533 * nearest integer..."
534 */
535 dst[i].i = IROUND(src[i].f);
536 break;
537 case GLSL_TYPE_BOOL:
538 dst[i].i = src[i].i ? 1 : 0;
539 break;
540 default:
541 assert(!"Should not get here.");
542 break;
543 }
544 break;
545
546 default:
547 assert(!"Should not get here.");
548 break;
549 }
550 }
551 }
552 }
553 }
554
555 static void
556 log_uniform(const void *values, enum glsl_base_type basicType,
557 unsigned rows, unsigned cols, unsigned count,
558 bool transpose,
559 const struct gl_shader_program *shProg,
560 GLint location,
561 const struct gl_uniform_storage *uni)
562 {
563
564 const union gl_constant_value *v = (const union gl_constant_value *) values;
565 const unsigned elems = rows * cols * count;
566 const char *const extra = (cols == 1) ? "uniform" : "uniform matrix";
567
568 printf("Mesa: set program %u %s \"%s\" (loc %d, type \"%s\", "
569 "transpose = %s) to: ",
570 shProg->Name, extra, uni->name, location, uni->type->name,
571 transpose ? "true" : "false");
572 for (unsigned i = 0; i < elems; i++) {
573 if (i != 0 && ((i % rows) == 0))
574 printf(", ");
575
576 switch (basicType) {
577 case GLSL_TYPE_UINT:
578 printf("%u ", v[i].u);
579 break;
580 case GLSL_TYPE_INT:
581 printf("%d ", v[i].i);
582 break;
583 case GLSL_TYPE_FLOAT:
584 printf("%g ", v[i].f);
585 break;
586 default:
587 assert(!"Should not get here.");
588 break;
589 }
590 }
591 printf("\n");
592 fflush(stdout);
593 }
594
595 #if 0
596 static void
597 log_program_parameters(const struct gl_shader_program *shProg)
598 {
599 static const char *stages[] = {
600 "vertex", "fragment", "geometry"
601 };
602
603 assert(Elements(stages) == MESA_SHADER_TYPES);
604
605 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
606 if (shProg->_LinkedShaders[i] == NULL)
607 continue;
608
609 const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
610
611 printf("Program %d %s shader parameters:\n",
612 shProg->Name, stages[i]);
613 for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
614 printf("%s: %p %f %f %f %f\n",
615 prog->Parameters->Parameters[j].Name,
616 prog->Parameters->ParameterValues[j],
617 prog->Parameters->ParameterValues[j][0].f,
618 prog->Parameters->ParameterValues[j][1].f,
619 prog->Parameters->ParameterValues[j][2].f,
620 prog->Parameters->ParameterValues[j][3].f);
621 }
622 }
623 fflush(stdout);
624 }
625 #endif
626
627 /**
628 * Check if the type given by userType is allowed to set a uniform of the
629 * target type. Generally, equivalence is required, but setting Boolean
630 * uniforms can be done with glUniformiv or glUniformfv.
631 */
632 static GLboolean
633 compatible_types(GLenum userType, GLenum targetType)
634 {
635 if (userType == targetType)
636 return GL_TRUE;
637
638 if (targetType == GL_BOOL && (userType == GL_FLOAT ||
639 userType == GL_UNSIGNED_INT ||
640 userType == GL_INT))
641 return GL_TRUE;
642
643 if (targetType == GL_BOOL_VEC2 && (userType == GL_FLOAT_VEC2 ||
644 userType == GL_UNSIGNED_INT_VEC2 ||
645 userType == GL_INT_VEC2))
646 return GL_TRUE;
647
648 if (targetType == GL_BOOL_VEC3 && (userType == GL_FLOAT_VEC3 ||
649 userType == GL_UNSIGNED_INT_VEC3 ||
650 userType == GL_INT_VEC3))
651 return GL_TRUE;
652
653 if (targetType == GL_BOOL_VEC4 && (userType == GL_FLOAT_VEC4 ||
654 userType == GL_UNSIGNED_INT_VEC4 ||
655 userType == GL_INT_VEC4))
656 return GL_TRUE;
657
658 if (is_sampler_type(targetType) && userType == GL_INT)
659 return GL_TRUE;
660
661 return GL_FALSE;
662 }
663
664 /**
665 * Set the value of a program's uniform variable.
666 * \param program the program whose uniform to update
667 * \param index the index of the program parameter for the uniform
668 * \param offset additional parameter slot offset (for arrays)
669 * \param type the incoming datatype of 'values'
670 * \param count the number of uniforms to set
671 * \param elems number of elements per uniform (1, 2, 3 or 4)
672 * \param values the new values, of datatype 'type'
673 */
674 static void
675 set_program_uniform(struct gl_context *ctx, struct gl_program *program,
676 GLint index, GLint offset,
677 GLenum type, GLsizei count, GLint elems,
678 const void *values)
679 {
680 const struct gl_program_parameter *param =
681 &program->Parameters->Parameters[index];
682
683 assert(offset >= 0);
684 assert(elems >= 1);
685 assert(elems <= 4);
686
687 if (!compatible_types(type, param->DataType)) {
688 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
689 return;
690 }
691
692 if (index + offset > (GLint) program->Parameters->Size) {
693 /* out of bounds! */
694 return;
695 }
696
697 if (param->Type == PROGRAM_SAMPLER) {
698 /* This controls which texture unit which is used by a sampler */
699 GLboolean changed = GL_FALSE;
700 GLint i;
701
702 /* this should have been caught by the compatible_types() check */
703 ASSERT(type == GL_INT);
704
705 /* loop over number of samplers to change */
706 for (i = 0; i < count; i++) {
707 GLuint sampler = (GLuint)
708 program->Parameters->ParameterValues[index+offset + i][0].f;
709 GLuint texUnit = ((GLuint *) values)[i];
710
711 /* check that the sampler (tex unit index) is legal */
712 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
713 _mesa_error(ctx, GL_INVALID_VALUE,
714 "glUniform1(invalid sampler/tex unit index for '%s')",
715 param->Name);
716 return;
717 }
718
719 /* This maps a sampler to a texture unit: */
720 if (sampler < MAX_SAMPLERS) {
721 #if 0
722 printf("Set program %p sampler %d '%s' to unit %u\n",
723 program, sampler, param->Name, texUnit);
724 #endif
725 if (program->SamplerUnits[sampler] != texUnit) {
726 program->SamplerUnits[sampler] = texUnit;
727 changed = GL_TRUE;
728 }
729 }
730 }
731
732 if (changed) {
733 /* When a sampler's value changes it usually requires rewriting
734 * a GPU program's TEX instructions since there may not be a
735 * sampler->texture lookup table. We signal this with the
736 * ProgramStringNotify() callback.
737 */
738 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
739 _mesa_update_shader_textures_used(program);
740 /* Do we need to care about the return value here?
741 * This should not be the first time the driver was notified of
742 * this program.
743 */
744 (void) ctx->Driver.ProgramStringNotify(ctx, program->Target, program);
745 }
746 }
747 else {
748 /* ordinary uniform variable */
749 const GLboolean isUniformBool = is_boolean_type(param->DataType);
750 const GLenum basicType = base_uniform_type(type);
751 const GLint slots = (param->Size + 3) / 4;
752 const GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
753 GLsizei k, i;
754
755 if ((GLint) param->Size > typeSize) {
756 /* an array */
757 /* we'll ignore extra data below */
758 }
759 else {
760 /* non-array: count must be at most one; count == 0 is handled
761 * by the loop below
762 */
763 if (count > 1) {
764 _mesa_error(ctx, GL_INVALID_OPERATION,
765 "glUniform(uniform '%s' is not an array)",
766 param->Name);
767 return;
768 }
769 }
770
771 /* loop over number of array elements */
772 for (k = 0; k < count; k++) {
773 gl_constant_value *uniformVal;
774
775 if (offset + k >= slots) {
776 /* Extra array data is ignored */
777 break;
778 }
779
780 /* uniformVal (the destination) is always gl_constant_value[4] */
781 uniformVal = program->Parameters->ParameterValues[index + offset + k];
782
783 if (basicType == GL_INT) {
784 const GLint *iValues = ((const GLint *) values) + k * elems;
785 for (i = 0; i < elems; i++) {
786 if (!ctx->Const.NativeIntegers)
787 uniformVal[i].f = (GLfloat) iValues[i];
788 else
789 uniformVal[i].i = iValues[i];
790 }
791 }
792 else if (basicType == GL_UNSIGNED_INT) {
793 const GLuint *iValues = ((const GLuint *) values) + k * elems;
794 for (i = 0; i < elems; i++) {
795 if (!ctx->Const.NativeIntegers)
796 uniformVal[i].f = (GLfloat)(GLuint) iValues[i];
797 else
798 uniformVal[i].u = iValues[i];
799 }
800 }
801 else {
802 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
803 assert(basicType == GL_FLOAT);
804 for (i = 0; i < elems; i++) {
805 uniformVal[i].f = fValues[i];
806 }
807 }
808
809 /* if the uniform is bool-valued, convert to 1 or 0 */
810 if (isUniformBool) {
811 for (i = 0; i < elems; i++) {
812 if (basicType == GL_FLOAT)
813 uniformVal[i].b = uniformVal[i].f != 0.0f ? 1 : 0;
814 else
815 uniformVal[i].b = uniformVal[i].u ? 1 : 0;
816
817 if (ctx->Const.NativeIntegers)
818 uniformVal[i].u =
819 uniformVal[i].b ? ctx->Const.UniformBooleanTrue : 0;
820 else
821 uniformVal[i].f = uniformVal[i].b ? 1.0f : 0.0f;
822 }
823 }
824 }
825 }
826 }
827
828 /**
829 * Propagate some values from uniform backing storage to driver storage
830 *
831 * Values propagated from uniform backing storage to driver storage
832 * have all format / type conversions previously requested by the
833 * driver applied. This function is most often called by the
834 * implementations of \c glUniform1f, etc. and \c glUniformMatrix2f,
835 * etc.
836 *
837 * \param uni Uniform whose data is to be propagated to driver storage
838 * \param array_index If \c uni is an array, this is the element of
839 * the array to be propagated.
840 * \param count Number of array elements to propagate.
841 */
842 extern "C" void
843 _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
844 unsigned array_index,
845 unsigned count)
846 {
847 unsigned i;
848
849 /* vector_elements and matrix_columns can be 0 for samplers.
850 */
851 const unsigned components = MAX2(1, uni->type->vector_elements);
852 const unsigned vectors = MAX2(1, uni->type->matrix_columns);
853
854 /* Store the data in the driver's requested type in the driver's storage
855 * areas.
856 */
857 unsigned src_vector_byte_stride = components * 4;
858
859 for (i = 0; i < uni->num_driver_storage; i++) {
860 struct gl_uniform_driver_storage *const store = &uni->driver_storage[i];
861 uint8_t *dst = (uint8_t *) store->data;
862 const unsigned extra_stride =
863 store->element_stride - (vectors * store->vector_stride);
864 const uint8_t *src =
865 (uint8_t *) (&uni->storage[array_index * (components * vectors)].i);
866
867 #if 0
868 printf("%s: %p[%d] components=%u vectors=%u count=%u vector_stride=%u "
869 "extra_stride=%u\n",
870 __func__, dst, array_index, components,
871 vectors, count, store->vector_stride, extra_stride);
872 #endif
873
874 dst += array_index * store->element_stride;
875
876 switch (store->format) {
877 case uniform_native:
878 case uniform_bool_int_0_1: {
879 unsigned j;
880 unsigned v;
881
882 for (j = 0; j < count; j++) {
883 for (v = 0; v < vectors; v++) {
884 memcpy(dst, src, src_vector_byte_stride);
885 src += src_vector_byte_stride;
886 dst += store->vector_stride;
887 }
888
889 dst += extra_stride;
890 }
891 break;
892 }
893
894 case uniform_int_float:
895 case uniform_bool_float: {
896 const int *isrc = (const int *) src;
897 unsigned j;
898 unsigned v;
899 unsigned c;
900
901 for (j = 0; j < count; j++) {
902 for (v = 0; v < vectors; v++) {
903 for (c = 0; c < components; c++) {
904 ((float *) dst)[c] = (float) *isrc;
905 isrc++;
906 }
907
908 dst += store->vector_stride;
909 }
910
911 dst += extra_stride;
912 }
913 break;
914 }
915
916 case uniform_bool_int_0_not0: {
917 const int *isrc = (const int *) src;
918 unsigned j;
919 unsigned v;
920 unsigned c;
921
922 for (j = 0; j < count; j++) {
923 for (v = 0; v < vectors; v++) {
924 for (c = 0; c < components; c++) {
925 ((int *) dst)[c] = *isrc == 0 ? 0 : ~0;
926 isrc++;
927 }
928
929 dst += store->vector_stride;
930 }
931
932 dst += extra_stride;
933 }
934 break;
935 }
936
937 default:
938 assert(!"Should not get here.");
939 break;
940 }
941 }
942 }
943
944 /**
945 * Called via glUniform*() functions.
946 */
947 extern "C" void
948 _mesa_uniform(struct gl_context *ctx, struct gl_shader_program *shProg,
949 GLint location, GLsizei count,
950 const GLvoid *values, GLenum type)
951 {
952 unsigned loc, offset;
953 unsigned components;
954 unsigned src_components;
955 unsigned i;
956 enum glsl_base_type basicType;
957 struct gl_uniform_storage *uni;
958
959 ASSERT_OUTSIDE_BEGIN_END(ctx);
960
961 if (!validate_uniform_parameters(ctx, shProg, location, count,
962 &loc, &offset, "glUniform", false))
963 return;
964
965 uni = &shProg->UniformStorage[loc];
966
967 /* Verify that the types are compatible.
968 */
969 switch (type) {
970 case GL_FLOAT:
971 basicType = GLSL_TYPE_FLOAT;
972 src_components = 1;
973 break;
974 case GL_FLOAT_VEC2:
975 basicType = GLSL_TYPE_FLOAT;
976 src_components = 2;
977 break;
978 case GL_FLOAT_VEC3:
979 basicType = GLSL_TYPE_FLOAT;
980 src_components = 3;
981 break;
982 case GL_FLOAT_VEC4:
983 basicType = GLSL_TYPE_FLOAT;
984 src_components = 4;
985 break;
986 case GL_UNSIGNED_INT:
987 basicType = GLSL_TYPE_UINT;
988 src_components = 1;
989 break;
990 case GL_UNSIGNED_INT_VEC2:
991 basicType = GLSL_TYPE_UINT;
992 src_components = 2;
993 break;
994 case GL_UNSIGNED_INT_VEC3:
995 basicType = GLSL_TYPE_UINT;
996 src_components = 3;
997 break;
998 case GL_UNSIGNED_INT_VEC4:
999 basicType = GLSL_TYPE_UINT;
1000 src_components = 4;
1001 break;
1002 case GL_INT:
1003 basicType = GLSL_TYPE_INT;
1004 src_components = 1;
1005 break;
1006 case GL_INT_VEC2:
1007 basicType = GLSL_TYPE_INT;
1008 src_components = 2;
1009 break;
1010 case GL_INT_VEC3:
1011 basicType = GLSL_TYPE_INT;
1012 src_components = 3;
1013 break;
1014 case GL_INT_VEC4:
1015 basicType = GLSL_TYPE_INT;
1016 src_components = 4;
1017 break;
1018 case GL_BOOL:
1019 case GL_BOOL_VEC2:
1020 case GL_BOOL_VEC3:
1021 case GL_BOOL_VEC4:
1022 case GL_FLOAT_MAT2:
1023 case GL_FLOAT_MAT2x3:
1024 case GL_FLOAT_MAT2x4:
1025 case GL_FLOAT_MAT3x2:
1026 case GL_FLOAT_MAT3:
1027 case GL_FLOAT_MAT3x4:
1028 case GL_FLOAT_MAT4x2:
1029 case GL_FLOAT_MAT4x3:
1030 case GL_FLOAT_MAT4:
1031 default:
1032 _mesa_problem(NULL, "Invalid type in %s", __func__);
1033 return;
1034 }
1035
1036 if (uni->type->is_sampler()) {
1037 components = 1;
1038 } else {
1039 components = uni->type->vector_elements;
1040 }
1041
1042 bool match;
1043 switch (uni->type->base_type) {
1044 case GLSL_TYPE_BOOL:
1045 match = true;
1046 break;
1047 case GLSL_TYPE_SAMPLER:
1048 match = (basicType == GLSL_TYPE_INT);
1049 break;
1050 default:
1051 match = (basicType == uni->type->base_type);
1052 break;
1053 }
1054
1055 if (uni->type->is_matrix() || components != src_components || !match) {
1056 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
1057 return;
1058 }
1059
1060 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
1061 log_uniform(values, basicType, components, 1, count,
1062 false, shProg, location, uni);
1063 }
1064
1065 /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
1066 *
1067 * "Setting a sampler's value to i selects texture image unit number
1068 * i. The values of i range from zero to the implementation- dependent
1069 * maximum supported number of texture image units."
1070 *
1071 * In addition, table 2.3, "Summary of GL errors," on page 17 (page 33 of
1072 * the PDF) says:
1073 *
1074 * "Error Description Offending command
1075 * ignored?
1076 * ...
1077 * INVALID_VALUE Numeric argument out of range Yes"
1078 *
1079 * Based on that, when an invalid sampler is specified, we generate a
1080 * GL_INVALID_VALUE error and ignore the command.
1081 */
1082 if (uni->type->is_sampler()) {
1083 for (i = 0; i < count; i++) {
1084 const unsigned texUnit = ((unsigned *) values)[i];
1085
1086 /* check that the sampler (tex unit index) is legal */
1087 if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
1088 _mesa_error(ctx, GL_INVALID_VALUE,
1089 "glUniform1i(invalid sampler/tex unit index for "
1090 "uniform %d)",
1091 location);
1092 return;
1093 }
1094 }
1095 }
1096
1097 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1098 *
1099 * "When loading N elements starting at an arbitrary position k in a
1100 * uniform declared as an array, elements k through k + N - 1 in the
1101 * array will be replaced with the new values. Values for any array
1102 * element that exceeds the highest array element index used, as
1103 * reported by GetActiveUniform, will be ignored by the GL."
1104 *
1105 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1106 * will have already generated an error.
1107 */
1108 if (uni->array_elements != 0) {
1109 if (offset >= uni->array_elements)
1110 return;
1111
1112 count = MIN2(count, (uni->array_elements - offset));
1113 }
1114
1115 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1116
1117 /* Store the data in the "actual type" backing storage for the uniform.
1118 */
1119 if (!uni->type->is_boolean()) {
1120 memcpy(&uni->storage[components * offset], values,
1121 sizeof(uni->storage[0]) * components * count);
1122 } else {
1123 const union gl_constant_value *src =
1124 (const union gl_constant_value *) values;
1125 union gl_constant_value *dst = &uni->storage[components * offset];
1126 const unsigned elems = components * count;
1127
1128 for (i = 0; i < elems; i++) {
1129 if (basicType == GLSL_TYPE_FLOAT) {
1130 dst[i].i = src[i].f != 0.0f ? 1 : 0;
1131 } else {
1132 dst[i].i = src[i].i != 0 ? 1 : 0;
1133 }
1134 }
1135 }
1136
1137 uni->initialized = true;
1138
1139 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1140
1141 /* If the uniform is a sampler, do the extra magic necessary to propagate
1142 * the changes through.
1143 */
1144 if (uni->type->is_sampler()) {
1145 for (i = 0; i < count; i++) {
1146 shProg->SamplerUnits[uni->sampler + offset + i] =
1147 ((unsigned *) values)[i];
1148 }
1149
1150 bool flushed = false;
1151 for (i = 0; i < MESA_SHADER_TYPES; i++) {
1152 struct gl_program *prog;
1153
1154 if (shProg->_LinkedShaders[i] == NULL)
1155 continue;
1156
1157 prog = shProg->_LinkedShaders[i]->Program;
1158
1159 assert(sizeof(prog->SamplerUnits) == sizeof(shProg->SamplerUnits));
1160
1161 if (memcmp(prog->SamplerUnits,
1162 shProg->SamplerUnits,
1163 sizeof(shProg->SamplerUnits)) != 0) {
1164 if (!flushed) {
1165 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
1166 flushed = true;
1167 }
1168
1169 memcpy(prog->SamplerUnits,
1170 shProg->SamplerUnits,
1171 sizeof(shProg->SamplerUnits));
1172
1173 _mesa_update_shader_textures_used(prog);
1174 (void) ctx->Driver.ProgramStringNotify(ctx, prog->Target, prog);
1175 }
1176 }
1177 }
1178 }
1179
1180 /**
1181 * Set a matrix-valued program parameter.
1182 */
1183 static void
1184 set_program_uniform_matrix(struct gl_context *ctx, struct gl_program *program,
1185 GLuint index, GLuint offset,
1186 GLuint count, GLuint rows, GLuint cols,
1187 GLboolean transpose, const GLfloat *values)
1188 {
1189 GLuint mat, row, col;
1190 GLuint src = 0;
1191 const struct gl_program_parameter *param =
1192 &program->Parameters->Parameters[index];
1193 const GLuint slots = (param->Size + 3) / 4;
1194 const GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
1195 GLint nr, nc;
1196
1197 /* check that the number of rows, columns is correct */
1198 get_matrix_dims(param->DataType, &nr, &nc);
1199 if (rows != nr || cols != nc) {
1200 _mesa_error(ctx, GL_INVALID_OPERATION,
1201 "glUniformMatrix(matrix size mismatch)");
1202 return;
1203 }
1204
1205 if ((GLint) param->Size <= typeSize) {
1206 /* non-array: count must be at most one; count == 0 is handled
1207 * by the loop below
1208 */
1209 if (count > 1) {
1210 _mesa_error(ctx, GL_INVALID_OPERATION,
1211 "glUniformMatrix(uniform is not an array)");
1212 return;
1213 }
1214 }
1215
1216 /*
1217 * Note: the _columns_ of a matrix are stored in program registers, not
1218 * the rows. So, the loops below look a little funny.
1219 * XXX could optimize this a bit...
1220 */
1221
1222 /* loop over matrices */
1223 for (mat = 0; mat < count; mat++) {
1224
1225 /* each matrix: */
1226 for (col = 0; col < cols; col++) {
1227 GLfloat *v;
1228 if (offset >= slots) {
1229 /* Ignore writes beyond the end of (the used part of) an array */
1230 return;
1231 }
1232 v = (GLfloat *) program->Parameters->ParameterValues[index + offset];
1233 for (row = 0; row < rows; row++) {
1234 if (transpose) {
1235 v[row] = values[src + row * cols + col];
1236 }
1237 else {
1238 v[row] = values[src + col * rows + row];
1239 }
1240 }
1241
1242 offset++;
1243 }
1244
1245 src += rows * cols; /* next matrix */
1246 }
1247 }
1248
1249 /**
1250 * Called by glUniformMatrix*() functions.
1251 * Note: cols=2, rows=4 ==> array[2] of vec4
1252 */
1253 extern "C" void
1254 _mesa_uniform_matrix(struct gl_context *ctx, struct gl_shader_program *shProg,
1255 GLint cols, GLint rows,
1256 GLint location, GLsizei count,
1257 GLboolean transpose, const GLfloat *values)
1258 {
1259 unsigned loc, offset;
1260 unsigned vectors;
1261 unsigned components;
1262 unsigned elements;
1263 struct gl_uniform_storage *uni;
1264
1265 ASSERT_OUTSIDE_BEGIN_END(ctx);
1266
1267 if (!validate_uniform_parameters(ctx, shProg, location, count,
1268 &loc, &offset, "glUniformMatrix", false))
1269 return;
1270
1271 uni = &shProg->UniformStorage[loc];
1272 if (!uni->type->is_matrix()) {
1273 _mesa_error(ctx, GL_INVALID_OPERATION,
1274 "glUniformMatrix(non-matrix uniform)");
1275 return;
1276 }
1277
1278 assert(!uni->type->is_sampler());
1279 vectors = uni->type->matrix_columns;
1280 components = uni->type->vector_elements;
1281
1282 /* Verify that the types are compatible. This is greatly simplified for
1283 * matrices because they can only have a float base type.
1284 */
1285 if (vectors != cols || components != rows) {
1286 _mesa_error(ctx, GL_INVALID_OPERATION,
1287 "glUniformMatrix(matrix size mismatch)");
1288 return;
1289 }
1290
1291 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
1292 log_uniform(values, GLSL_TYPE_FLOAT, components, vectors, count,
1293 bool(transpose), shProg, location, uni);
1294 }
1295
1296 /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
1297 *
1298 * "When loading N elements starting at an arbitrary position k in a
1299 * uniform declared as an array, elements k through k + N - 1 in the
1300 * array will be replaced with the new values. Values for any array
1301 * element that exceeds the highest array element index used, as
1302 * reported by GetActiveUniform, will be ignored by the GL."
1303 *
1304 * Clamp 'count' to a valid value. Note that for non-arrays a count > 1
1305 * will have already generated an error.
1306 */
1307 if (uni->array_elements != 0) {
1308 if (offset >= uni->array_elements)
1309 return;
1310
1311 count = MIN2(count, (uni->array_elements - offset));
1312 }
1313
1314 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1315
1316 /* Store the data in the "actual type" backing storage for the uniform.
1317 */
1318 elements = components * vectors;
1319
1320 if (!transpose) {
1321 memcpy(&uni->storage[elements * offset], values,
1322 sizeof(uni->storage[0]) * elements * count);
1323 } else {
1324 /* Copy and transpose the matrix.
1325 */
1326 const float *src = values;
1327 float *dst = &uni->storage[elements * offset].f;
1328
1329 for (unsigned i = 0; i < count; i++) {
1330 for (unsigned r = 0; r < rows; r++) {
1331 for (unsigned c = 0; c < cols; c++) {
1332 dst[(c * components) + r] = src[c + (r * vectors)];
1333 }
1334 }
1335
1336 dst += elements;
1337 src += elements;
1338 }
1339 }
1340
1341 uni->initialized = true;
1342
1343 _mesa_propagate_uniforms_to_driver_storage(uni, offset, count);
1344 }
1345
1346 /**
1347 * Called via glGetUniformLocation().
1348 *
1349 * The return value will encode two values, the uniform location and an
1350 * offset (used for arrays, structs).
1351 */
1352 extern "C" GLint
1353 _mesa_get_uniform_location(struct gl_context *ctx,
1354 struct gl_shader_program *shProg,
1355 const GLchar *name)
1356 {
1357 const size_t len = strlen(name);
1358 long offset;
1359 bool array_lookup;
1360 char *name_copy;
1361
1362 /* If the name ends with a ']', assume that it refers to some element of an
1363 * array. Malformed array references will fail the hash table look up
1364 * below, so it doesn't matter that they are not caught here. This code
1365 * only wants to catch the "leaf" array references so that arrays of
1366 * structures containing arrays will be handled correctly.
1367 */
1368 if (name[len-1] == ']') {
1369 unsigned i;
1370
1371 /* Walk backwards over the string looking for a non-digit character.
1372 * This had better be the opening bracket for an array index.
1373 *
1374 * Initially, i specifies the location of the ']'. Since the string may
1375 * contain only the ']' charcater, walk backwards very carefully.
1376 */
1377 for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
1378 /* empty */ ;
1379
1380 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
1381 *
1382 * "The first element of a uniform array is identified using the
1383 * name of the uniform array appended with "[0]". Except if the last
1384 * part of the string name indicates a uniform array, then the
1385 * location of the first element of that array can be retrieved by
1386 * either using the name of the uniform array, or the name of the
1387 * uniform array appended with "[0]"."
1388 *
1389 * Page 79 (page 93 of the PDF) of the OpenGL 2.1 spec says:
1390 *
1391 * "name must be a null terminated string, without white space."
1392 *
1393 * Return an error if there is no opening '[' to match the closing ']'.
1394 * An error will also be returned if there is intervening white space
1395 * (or other non-digit characters) before the opening '['.
1396 */
1397 if ((i == 0) || name[i-1] != '[')
1398 return -1;
1399
1400 /* Return an error if there are no digits between the opening '[' to
1401 * match the closing ']'.
1402 */
1403 if (i == (len - 1))
1404 return -1;
1405
1406 /* Make a new string that is a copy of the old string up to (but not
1407 * including) the '[' character.
1408 */
1409 name_copy = (char *) malloc(i);
1410 memcpy(name_copy, name, i - 1);
1411 name_copy[i-1] = '\0';
1412
1413 offset = strtol(&name[i], NULL, 10);
1414 if (offset < 0)
1415 return -1;
1416
1417 array_lookup = true;
1418 } else {
1419 name_copy = (char *) name;
1420 offset = 0;
1421 array_lookup = false;
1422 }
1423
1424 unsigned location;
1425 const bool found = shProg->UniformHash->get(location, name_copy);
1426
1427 assert(!found
1428 || strcmp(name_copy, shProg->UniformStorage[location].name) == 0);
1429
1430 /* Free the temporary buffer *before* possibly returning an error.
1431 */
1432 if (name_copy != name)
1433 free(name_copy);
1434
1435 if (!found)
1436 return -1;
1437
1438 /* Since array_elements is 0 for non-arrays, this causes look-ups of 'a[0]'
1439 * to (correctly) fail if 'a' is not an array.
1440 */
1441 if (array_lookup && shProg->UniformStorage[location].array_elements == 0) {
1442 return -1;
1443 }
1444
1445 return _mesa_uniform_merge_location_offset(location, offset);
1446 }