ir_to_mesa: Tell Mesa about our choices for vertex attribute locations.
[mesa.git] / src / mesa / shader / uniforms.c
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 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file uniforms.c
27 * Functions related to GLSL uniform variables.
28 * \author Brian Paul
29 */
30
31 /**
32 * XXX things to do:
33 * 1. Check that the right error code is generated for all _mesa_error() calls.
34 * 2. Insert FLUSH_VERTICES calls in various places
35 */
36
37
38 #include "main/glheader.h"
39 #include "main/context.h"
40 #include "shader/prog_parameter.h"
41 #include "shader/prog_statevars.h"
42 #include "shader/prog_uniform.h"
43 #include "shader/shader_api.h"
44 #include "uniforms.h"
45
46
47
48 static GLenum
49 base_uniform_type(GLenum type)
50 {
51 switch (type) {
52 #if 0 /* not needed, for now */
53 case GL_BOOL:
54 case GL_BOOL_VEC2:
55 case GL_BOOL_VEC3:
56 case GL_BOOL_VEC4:
57 return GL_BOOL;
58 #endif
59 case GL_FLOAT:
60 case GL_FLOAT_VEC2:
61 case GL_FLOAT_VEC3:
62 case GL_FLOAT_VEC4:
63 return GL_FLOAT;
64 case GL_UNSIGNED_INT:
65 case GL_UNSIGNED_INT_VEC2:
66 case GL_UNSIGNED_INT_VEC3:
67 case GL_UNSIGNED_INT_VEC4:
68 return GL_UNSIGNED_INT;
69 case GL_INT:
70 case GL_INT_VEC2:
71 case GL_INT_VEC3:
72 case GL_INT_VEC4:
73 return GL_INT;
74 default:
75 _mesa_problem(NULL, "Invalid type in base_uniform_type()");
76 return GL_FLOAT;
77 }
78 }
79
80
81 static GLboolean
82 is_boolean_type(GLenum type)
83 {
84 switch (type) {
85 case GL_BOOL:
86 case GL_BOOL_VEC2:
87 case GL_BOOL_VEC3:
88 case GL_BOOL_VEC4:
89 return GL_TRUE;
90 default:
91 return GL_FALSE;
92 }
93 }
94
95
96 static GLboolean
97 is_sampler_type(GLenum type)
98 {
99 switch (type) {
100 case GL_SAMPLER_1D:
101 case GL_SAMPLER_2D:
102 case GL_SAMPLER_3D:
103 case GL_SAMPLER_CUBE:
104 case GL_SAMPLER_1D_SHADOW:
105 case GL_SAMPLER_2D_SHADOW:
106 case GL_SAMPLER_2D_RECT_ARB:
107 case GL_SAMPLER_2D_RECT_SHADOW_ARB:
108 case GL_SAMPLER_1D_ARRAY_EXT:
109 case GL_SAMPLER_2D_ARRAY_EXT:
110 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
111 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
112 return GL_TRUE;
113 default:
114 return GL_FALSE;
115 }
116 }
117
118
119 static struct gl_program_parameter *
120 get_uniform_parameter(const struct gl_shader_program *shProg, GLuint index)
121 {
122 const struct gl_program *prog = NULL;
123 GLint progPos;
124
125 progPos = shProg->Uniforms->Uniforms[index].VertPos;
126 if (progPos >= 0) {
127 prog = &shProg->VertexProgram->Base;
128 }
129 else {
130 progPos = shProg->Uniforms->Uniforms[index].FragPos;
131 if (progPos >= 0) {
132 prog = &shProg->FragmentProgram->Base;
133 }
134 }
135
136 if (!prog || progPos < 0)
137 return NULL; /* should never happen */
138
139 return &prog->Parameters->Parameters[progPos];
140 }
141
142
143 /**
144 * Called via ctx->Driver.GetActiveUniform().
145 */
146 static void
147 _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
148 GLsizei maxLength, GLsizei *length, GLint *size,
149 GLenum *type, GLchar *nameOut)
150 {
151 const struct gl_shader_program *shProg;
152 const struct gl_program *prog = NULL;
153 const struct gl_program_parameter *param;
154 GLint progPos;
155
156 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
157 if (!shProg)
158 return;
159
160 if (!shProg->Uniforms || index >= shProg->Uniforms->NumUniforms) {
161 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
162 return;
163 }
164
165 progPos = shProg->Uniforms->Uniforms[index].VertPos;
166 if (progPos >= 0) {
167 prog = &shProg->VertexProgram->Base;
168 }
169 else {
170 progPos = shProg->Uniforms->Uniforms[index].FragPos;
171 if (progPos >= 0) {
172 prog = &shProg->FragmentProgram->Base;
173 }
174 }
175
176 if (!prog || progPos < 0)
177 return; /* should never happen */
178
179 ASSERT(progPos < prog->Parameters->NumParameters);
180 param = &prog->Parameters->Parameters[progPos];
181
182 if (nameOut) {
183 _mesa_copy_string(nameOut, maxLength, length, param->Name);
184 }
185
186 if (size) {
187 GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
188 if ((GLint) param->Size > typeSize) {
189 /* This is an array.
190 * Array elements are placed on vector[4] boundaries so they're
191 * a multiple of four floats. We round typeSize up to next multiple
192 * of four to get the right size below.
193 */
194 typeSize = (typeSize + 3) & ~3;
195 }
196 /* Note that the returned size is in units of the <type>, not bytes */
197 *size = param->Size / typeSize;
198 }
199
200 if (type) {
201 *type = param->DataType;
202 }
203 }
204
205
206
207 static void
208 get_matrix_dims(GLenum type, GLint *rows, GLint *cols)
209 {
210 switch (type) {
211 case GL_FLOAT_MAT2:
212 *rows = *cols = 2;
213 break;
214 case GL_FLOAT_MAT2x3:
215 *rows = 3;
216 *cols = 2;
217 break;
218 case GL_FLOAT_MAT2x4:
219 *rows = 4;
220 *cols = 2;
221 break;
222 case GL_FLOAT_MAT3:
223 *rows = 3;
224 *cols = 3;
225 break;
226 case GL_FLOAT_MAT3x2:
227 *rows = 2;
228 *cols = 3;
229 break;
230 case GL_FLOAT_MAT3x4:
231 *rows = 4;
232 *cols = 3;
233 break;
234 case GL_FLOAT_MAT4:
235 *rows = 4;
236 *cols = 4;
237 break;
238 case GL_FLOAT_MAT4x2:
239 *rows = 2;
240 *cols = 4;
241 break;
242 case GL_FLOAT_MAT4x3:
243 *rows = 3;
244 *cols = 4;
245 break;
246 default:
247 *rows = *cols = 0;
248 }
249 }
250
251
252 /**
253 * Determine the number of rows and columns occupied by a uniform
254 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
255 * the number of rows = 1 and cols = number of elements in the vector.
256 */
257 static void
258 get_uniform_rows_cols(const struct gl_program_parameter *p,
259 GLint *rows, GLint *cols)
260 {
261 get_matrix_dims(p->DataType, rows, cols);
262 if (*rows == 0 && *cols == 0) {
263 /* not a matrix type, probably a float or vector */
264 if (p->Size <= 4) {
265 *rows = 1;
266 *cols = p->Size;
267 }
268 else {
269 *rows = p->Size / 4 + 1;
270 if (p->Size % 4 == 0)
271 *cols = 4;
272 else
273 *cols = p->Size % 4;
274 }
275 }
276 }
277
278
279 /**
280 * Helper for get_uniform[fi]v() functions.
281 * Given a shader program name and uniform location, return a pointer
282 * to the shader program and return the program parameter position.
283 */
284 static void
285 lookup_uniform_parameter(GLcontext *ctx, GLuint program, GLint location,
286 struct gl_program **progOut, GLint *paramPosOut)
287 {
288 struct gl_shader_program *shProg
289 = _mesa_lookup_shader_program_err(ctx, program, "glGetUniform[if]v");
290 struct gl_program *prog = NULL;
291 GLint progPos = -1;
292
293 /* if shProg is NULL, we'll have already recorded an error */
294
295 if (shProg) {
296 if (!shProg->Uniforms ||
297 location < 0 ||
298 location >= (GLint) shProg->Uniforms->NumUniforms) {
299 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(location)");
300 }
301 else {
302 /* OK, find the gl_program and program parameter location */
303 progPos = shProg->Uniforms->Uniforms[location].VertPos;
304 if (progPos >= 0) {
305 prog = &shProg->VertexProgram->Base;
306 }
307 else {
308 progPos = shProg->Uniforms->Uniforms[location].FragPos;
309 if (progPos >= 0) {
310 prog = &shProg->FragmentProgram->Base;
311 }
312 }
313 }
314 }
315
316 *progOut = prog;
317 *paramPosOut = progPos;
318 }
319
320
321 /**
322 * GLGL uniform arrays and structs require special handling.
323 *
324 * The GL_ARB_shader_objects spec says that if you use
325 * glGetUniformLocation to get the location of an array, you CANNOT
326 * access other elements of the array by adding an offset to the
327 * returned location. For example, you must call
328 * glGetUniformLocation("foo[16]") if you want to set the 16th element
329 * of the array with glUniform().
330 *
331 * HOWEVER, some other OpenGL drivers allow accessing array elements
332 * by adding an offset to the returned array location. And some apps
333 * seem to depend on that behaviour.
334 *
335 * Mesa's gl_uniform_list doesn't directly support this since each
336 * entry in the list describes one uniform variable, not one uniform
337 * element. We could insert dummy entries in the list for each array
338 * element after [0] but that causes complications elsewhere.
339 *
340 * We solve this problem by encoding two values in the location that's
341 * returned by glGetUniformLocation():
342 * a) index into gl_uniform_list::Uniforms[] for the uniform
343 * b) an array/field offset (0 for simple types)
344 *
345 * These two values are encoded in the high and low halves of a GLint.
346 * By putting the uniform number in the high part and the offset in the
347 * low part, we can support the unofficial ability to index into arrays
348 * by adding offsets to the location value.
349 */
350 static void
351 merge_location_offset(GLint *location, GLint offset)
352 {
353 *location = (*location << 16) | offset;
354 }
355
356
357 /**
358 * Separate the uniform location and parameter offset. See above.
359 */
360 static void
361 split_location_offset(GLint *location, GLint *offset)
362 {
363 *offset = *location & 0xffff;
364 *location = *location >> 16;
365 }
366
367
368
369 /**
370 * Called via ctx->Driver.GetUniformfv().
371 */
372 static void
373 _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
374 GLfloat *params)
375 {
376 struct gl_program *prog;
377 GLint paramPos;
378 GLint offset;
379
380 split_location_offset(&location, &offset);
381
382 lookup_uniform_parameter(ctx, program, location, &prog, &paramPos);
383
384 if (prog) {
385 const struct gl_program_parameter *p =
386 &prog->Parameters->Parameters[paramPos];
387 GLint rows, cols, i, j, k;
388
389 get_uniform_rows_cols(p, &rows, &cols);
390
391 k = 0;
392 for (i = 0; i < rows; i++) {
393 for (j = 0; j < cols; j++ ) {
394 params[k++] = prog->Parameters->ParameterValues[paramPos+i][j];
395 }
396 }
397 }
398 }
399
400
401 /**
402 * Called via ctx->Driver.GetUniformiv().
403 * \sa _mesa_get_uniformfv, only difference is a cast.
404 */
405 static void
406 _mesa_get_uniformiv(GLcontext *ctx, GLuint program, GLint location,
407 GLint *params)
408 {
409 struct gl_program *prog;
410 GLint paramPos;
411 GLint offset;
412
413 split_location_offset(&location, &offset);
414
415 lookup_uniform_parameter(ctx, program, location, &prog, &paramPos);
416
417 if (prog) {
418 const struct gl_program_parameter *p =
419 &prog->Parameters->Parameters[paramPos];
420 GLint rows, cols, i, j, k;
421
422 get_uniform_rows_cols(p, &rows, &cols);
423
424 k = 0;
425 for (i = 0; i < rows; i++) {
426 for (j = 0; j < cols; j++ ) {
427 params[k++] = (GLint) prog->Parameters->ParameterValues[paramPos+i][j];
428 }
429 }
430 }
431 }
432
433
434 /**
435 * Called via ctx->Driver.GetUniformLocation().
436 *
437 * The return value will encode two values, the uniform location and an
438 * offset (used for arrays, structs).
439 */
440 static GLint
441 _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
442 {
443 GLint offset = 0, location = -1;
444
445 struct gl_shader_program *shProg =
446 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformLocation");
447
448 if (!shProg)
449 return -1;
450
451 if (shProg->LinkStatus == GL_FALSE) {
452 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
453 return -1;
454 }
455
456 /* XXX we should return -1 if the uniform was declared, but not
457 * actually used.
458 */
459
460 /* XXX we need to be able to parse uniform names for structs and arrays
461 * such as:
462 * mymatrix[1]
463 * mystruct.field1
464 */
465
466 {
467 /* handle 1-dimension arrays here... */
468 char *c = strchr(name, '[');
469 if (c) {
470 /* truncate name at [ */
471 const GLint len = c - name;
472 GLchar *newName = malloc(len + 1);
473 if (!newName)
474 return -1; /* out of mem */
475 memcpy(newName, name, len);
476 newName[len] = 0;
477
478 location = _mesa_lookup_uniform(shProg->Uniforms, newName);
479 if (location >= 0) {
480 const GLint element = atoi(c + 1);
481 if (element > 0) {
482 /* get type of the uniform array element */
483 struct gl_program_parameter *p;
484 p = get_uniform_parameter(shProg, location);
485 if (p) {
486 GLint rows, cols;
487 get_matrix_dims(p->DataType, &rows, &cols);
488 if (rows < 1)
489 rows = 1;
490 offset = element * rows;
491 }
492 }
493 }
494
495 free(newName);
496 }
497 }
498
499 if (location < 0) {
500 location = _mesa_lookup_uniform(shProg->Uniforms, name);
501 }
502
503 if (location >= 0) {
504 merge_location_offset(&location, offset);
505 }
506
507 return location;
508 }
509
510
511 /**
512 * Check if the type given by userType is allowed to set a uniform of the
513 * target type. Generally, equivalence is required, but setting Boolean
514 * uniforms can be done with glUniformiv or glUniformfv.
515 */
516 static GLboolean
517 compatible_types(GLenum userType, GLenum targetType)
518 {
519 if (userType == targetType)
520 return GL_TRUE;
521
522 if (targetType == GL_BOOL && (userType == GL_FLOAT ||
523 userType == GL_UNSIGNED_INT ||
524 userType == GL_INT))
525 return GL_TRUE;
526
527 if (targetType == GL_BOOL_VEC2 && (userType == GL_FLOAT_VEC2 ||
528 userType == GL_UNSIGNED_INT_VEC2 ||
529 userType == GL_INT_VEC2))
530 return GL_TRUE;
531
532 if (targetType == GL_BOOL_VEC3 && (userType == GL_FLOAT_VEC3 ||
533 userType == GL_UNSIGNED_INT_VEC3 ||
534 userType == GL_INT_VEC3))
535 return GL_TRUE;
536
537 if (targetType == GL_BOOL_VEC4 && (userType == GL_FLOAT_VEC4 ||
538 userType == GL_UNSIGNED_INT_VEC4 ||
539 userType == GL_INT_VEC4))
540 return GL_TRUE;
541
542 if (is_sampler_type(targetType) && userType == GL_INT)
543 return GL_TRUE;
544
545 return GL_FALSE;
546 }
547
548
549 /**
550 * Set the value of a program's uniform variable.
551 * \param program the program whose uniform to update
552 * \param index the index of the program parameter for the uniform
553 * \param offset additional parameter slot offset (for arrays)
554 * \param type the incoming datatype of 'values'
555 * \param count the number of uniforms to set
556 * \param elems number of elements per uniform (1, 2, 3 or 4)
557 * \param values the new values, of datatype 'type'
558 */
559 static void
560 set_program_uniform(GLcontext *ctx, struct gl_program *program,
561 GLint index, GLint offset,
562 GLenum type, GLsizei count, GLint elems,
563 const void *values)
564 {
565 const struct gl_program_parameter *param =
566 &program->Parameters->Parameters[index];
567
568 assert(offset >= 0);
569 assert(elems >= 1);
570 assert(elems <= 4);
571
572 if (!compatible_types(type, param->DataType)) {
573 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
574 return;
575 }
576
577 if (index + offset > (GLint) program->Parameters->Size) {
578 /* out of bounds! */
579 return;
580 }
581
582 if (param->Type == PROGRAM_SAMPLER) {
583 /* This controls which texture unit which is used by a sampler */
584 GLboolean changed = GL_FALSE;
585 GLint i;
586
587 /* this should have been caught by the compatible_types() check */
588 ASSERT(type == GL_INT);
589
590 /* loop over number of samplers to change */
591 for (i = 0; i < count; i++) {
592 GLuint sampler =
593 (GLuint) program->Parameters->ParameterValues[index + offset + i][0];
594 GLuint texUnit = ((GLuint *) values)[i];
595
596 /* check that the sampler (tex unit index) is legal */
597 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
598 _mesa_error(ctx, GL_INVALID_VALUE,
599 "glUniform1(invalid sampler/tex unit index for '%s')",
600 param->Name);
601 return;
602 }
603
604 /* This maps a sampler to a texture unit: */
605 if (sampler < MAX_SAMPLERS) {
606 #if 0
607 printf("Set program %p sampler %d '%s' to unit %u\n",
608 program, sampler, param->Name, texUnit);
609 #endif
610 if (program->SamplerUnits[sampler] != texUnit) {
611 program->SamplerUnits[sampler] = texUnit;
612 changed = GL_TRUE;
613 }
614 }
615 }
616
617 if (changed) {
618 /* When a sampler's value changes it usually requires rewriting
619 * a GPU program's TEX instructions since there may not be a
620 * sampler->texture lookup table. We signal this with the
621 * ProgramStringNotify() callback.
622 */
623 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
624 _mesa_update_shader_textures_used(program);
625 /* Do we need to care about the return value here?
626 * This should not be the first time the driver was notified of
627 * this program.
628 */
629 (void) ctx->Driver.ProgramStringNotify(ctx, program->Target, program);
630 }
631 }
632 else {
633 /* ordinary uniform variable */
634 const GLboolean isUniformBool = is_boolean_type(param->DataType);
635 const GLenum basicType = base_uniform_type(type);
636 const GLint slots = (param->Size + 3) / 4;
637 const GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
638 GLsizei k, i;
639
640 if ((GLint) param->Size > typeSize) {
641 /* an array */
642 /* we'll ignore extra data below */
643 }
644 else {
645 /* non-array: count must be at most one; count == 0 is handled by the loop below */
646 if (count > 1) {
647 _mesa_error(ctx, GL_INVALID_OPERATION,
648 "glUniform(uniform '%s' is not an array)",
649 param->Name);
650 return;
651 }
652 }
653
654 /* loop over number of array elements */
655 for (k = 0; k < count; k++) {
656 GLfloat *uniformVal;
657
658 if (offset + k >= slots) {
659 /* Extra array data is ignored */
660 break;
661 }
662
663 /* uniformVal (the destination) is always float[4] */
664 uniformVal = program->Parameters->ParameterValues[index + offset + k];
665
666 if (basicType == GL_INT) {
667 /* convert user's ints to floats */
668 const GLint *iValues = ((const GLint *) values) + k * elems;
669 for (i = 0; i < elems; i++) {
670 uniformVal[i] = (GLfloat) iValues[i];
671 }
672 }
673 else if (basicType == GL_UNSIGNED_INT) {
674 /* convert user's uints to floats */
675 const GLuint *iValues = ((const GLuint *) values) + k * elems;
676 for (i = 0; i < elems; i++) {
677 uniformVal[i] = (GLfloat) iValues[i];
678 }
679 }
680 else {
681 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
682 assert(basicType == GL_FLOAT);
683 for (i = 0; i < elems; i++) {
684 uniformVal[i] = fValues[i];
685 }
686 }
687
688 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
689 if (isUniformBool) {
690 for (i = 0; i < elems; i++) {
691 uniformVal[i] = uniformVal[i] ? 1.0f : 0.0f;
692 }
693 }
694 }
695 }
696 }
697
698
699 /**
700 * Called via ctx->Driver.Uniform().
701 */
702 static void
703 _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
704 const GLvoid *values, GLenum type)
705 {
706 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
707 struct gl_uniform *uniform;
708 GLint elems, offset;
709
710 if (!shProg || !shProg->LinkStatus) {
711 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
712 return;
713 }
714
715 if (location == -1)
716 return; /* The standard specifies this as a no-op */
717
718 if (location < -1) {
719 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(location=%d)",
720 location);
721 return;
722 }
723
724 split_location_offset(&location, &offset);
725
726 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
727 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location=%d)", location);
728 return;
729 }
730
731 if (count < 0) {
732 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
733 return;
734 }
735
736 elems = _mesa_sizeof_glsl_type(type);
737
738 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
739
740 uniform = &shProg->Uniforms->Uniforms[location];
741
742 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
743 const GLenum basicType = base_uniform_type(type);
744 GLint i;
745 printf("Mesa: set program %u uniform %s (loc %d) to: ",
746 shProg->Name, uniform->Name, location);
747 if (basicType == GL_INT) {
748 const GLint *v = (const GLint *) values;
749 for (i = 0; i < count * elems; i++) {
750 printf("%d ", v[i]);
751 }
752 }
753 else if (basicType == GL_UNSIGNED_INT) {
754 const GLuint *v = (const GLuint *) values;
755 for (i = 0; i < count * elems; i++) {
756 printf("%u ", v[i]);
757 }
758 }
759 else {
760 const GLfloat *v = (const GLfloat *) values;
761 assert(basicType == GL_FLOAT);
762 for (i = 0; i < count * elems; i++) {
763 printf("%g ", v[i]);
764 }
765 }
766 printf("\n");
767 }
768
769 /* A uniform var may be used by both a vertex shader and a fragment
770 * shader. We may need to update one or both shader's uniform here:
771 */
772 if (shProg->VertexProgram) {
773 /* convert uniform location to program parameter index */
774 GLint index = uniform->VertPos;
775 if (index >= 0) {
776 set_program_uniform(ctx, &shProg->VertexProgram->Base,
777 index, offset, type, count, elems, values);
778 }
779 }
780
781 if (shProg->FragmentProgram) {
782 /* convert uniform location to program parameter index */
783 GLint index = uniform->FragPos;
784 if (index >= 0) {
785 set_program_uniform(ctx, &shProg->FragmentProgram->Base,
786 index, offset, type, count, elems, values);
787 }
788 }
789
790 uniform->Initialized = GL_TRUE;
791 }
792
793
794 /**
795 * Set a matrix-valued program parameter.
796 */
797 static void
798 set_program_uniform_matrix(GLcontext *ctx, struct gl_program *program,
799 GLuint index, GLuint offset,
800 GLuint count, GLuint rows, GLuint cols,
801 GLboolean transpose, const GLfloat *values)
802 {
803 GLuint mat, row, col;
804 GLuint src = 0;
805 const struct gl_program_parameter * param = &program->Parameters->Parameters[index];
806 const GLuint slots = (param->Size + 3) / 4;
807 const GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
808 GLint nr, nc;
809
810 /* check that the number of rows, columns is correct */
811 get_matrix_dims(param->DataType, &nr, &nc);
812 if (rows != nr || cols != nc) {
813 _mesa_error(ctx, GL_INVALID_OPERATION,
814 "glUniformMatrix(matrix size mismatch)");
815 return;
816 }
817
818 if ((GLint) param->Size <= typeSize) {
819 /* non-array: count must be at most one; count == 0 is handled by the loop below */
820 if (count > 1) {
821 _mesa_error(ctx, GL_INVALID_OPERATION,
822 "glUniformMatrix(uniform is not an array)");
823 return;
824 }
825 }
826
827 /*
828 * Note: the _columns_ of a matrix are stored in program registers, not
829 * the rows. So, the loops below look a little funny.
830 * XXX could optimize this a bit...
831 */
832
833 /* loop over matrices */
834 for (mat = 0; mat < count; mat++) {
835
836 /* each matrix: */
837 for (col = 0; col < cols; col++) {
838 GLfloat *v;
839 if (offset >= slots) {
840 /* Ignore writes beyond the end of (the used part of) an array */
841 return;
842 }
843 v = program->Parameters->ParameterValues[index + offset];
844 for (row = 0; row < rows; row++) {
845 if (transpose) {
846 v[row] = values[src + row * cols + col];
847 }
848 else {
849 v[row] = values[src + col * rows + row];
850 }
851 }
852
853 offset++;
854 }
855
856 src += rows * cols; /* next matrix */
857 }
858 }
859
860
861 /**
862 * Called by ctx->Driver.UniformMatrix().
863 * Note: cols=2, rows=4 ==> array[2] of vec4
864 */
865 static void
866 _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
867 GLint location, GLsizei count,
868 GLboolean transpose, const GLfloat *values)
869 {
870 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
871 struct gl_uniform *uniform;
872 GLint offset;
873
874 if (!shProg || !shProg->LinkStatus) {
875 _mesa_error(ctx, GL_INVALID_OPERATION,
876 "glUniformMatrix(program not linked)");
877 return;
878 }
879
880 if (location == -1)
881 return; /* The standard specifies this as a no-op */
882
883 if (location < -1) {
884 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniformMatrix(location)");
885 return;
886 }
887
888 split_location_offset(&location, &offset);
889
890 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
891 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
892 return;
893 }
894 if (values == NULL) {
895 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
896 return;
897 }
898
899 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
900
901 uniform = &shProg->Uniforms->Uniforms[location];
902
903 if (shProg->VertexProgram) {
904 /* convert uniform location to program parameter index */
905 GLint index = uniform->VertPos;
906 if (index >= 0) {
907 set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
908 index, offset,
909 count, rows, cols, transpose, values);
910 }
911 }
912
913 if (shProg->FragmentProgram) {
914 /* convert uniform location to program parameter index */
915 GLint index = uniform->FragPos;
916 if (index >= 0) {
917 set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
918 index, offset,
919 count, rows, cols, transpose, values);
920 }
921 }
922
923 uniform->Initialized = GL_TRUE;
924 }
925
926
927
928 void
929 _mesa_init_uniform_functions(struct dd_function_table *driver)
930 {
931 driver->GetActiveUniform = _mesa_get_active_uniform;
932 driver->GetUniformfv = _mesa_get_uniformfv;
933 driver->GetUniformiv = _mesa_get_uniformiv;
934 driver->GetUniformLocation = _mesa_get_uniform_location;
935 driver->Uniform = _mesa_uniform;
936 driver->UniformMatrix = _mesa_uniform_matrix;
937 }