Merge branch 'mesa-2d-registers'
[mesa.git] / src / mesa / main / 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 "main/dispatch.h"
41 #include "main/shaderapi.h"
42 #include "main/shaderobj.h"
43 #include "main/uniforms.h"
44 #include "program/prog_parameter.h"
45 #include "program/prog_statevars.h"
46 #include "program/prog_uniform.h"
47
48
49
50 static GLenum
51 base_uniform_type(GLenum type)
52 {
53 switch (type) {
54 #if 0 /* not needed, for now */
55 case GL_BOOL:
56 case GL_BOOL_VEC2:
57 case GL_BOOL_VEC3:
58 case GL_BOOL_VEC4:
59 return GL_BOOL;
60 #endif
61 case GL_FLOAT:
62 case GL_FLOAT_VEC2:
63 case GL_FLOAT_VEC3:
64 case GL_FLOAT_VEC4:
65 return GL_FLOAT;
66 case GL_UNSIGNED_INT:
67 case GL_UNSIGNED_INT_VEC2:
68 case GL_UNSIGNED_INT_VEC3:
69 case GL_UNSIGNED_INT_VEC4:
70 return GL_UNSIGNED_INT;
71 case GL_INT:
72 case GL_INT_VEC2:
73 case GL_INT_VEC3:
74 case GL_INT_VEC4:
75 return GL_INT;
76 default:
77 _mesa_problem(NULL, "Invalid type in base_uniform_type()");
78 return GL_FLOAT;
79 }
80 }
81
82
83 static GLboolean
84 is_boolean_type(GLenum type)
85 {
86 switch (type) {
87 case GL_BOOL:
88 case GL_BOOL_VEC2:
89 case GL_BOOL_VEC3:
90 case GL_BOOL_VEC4:
91 return GL_TRUE;
92 default:
93 return GL_FALSE;
94 }
95 }
96
97
98 static GLboolean
99 is_sampler_type(GLenum type)
100 {
101 switch (type) {
102 case GL_SAMPLER_1D:
103 case GL_SAMPLER_2D:
104 case GL_SAMPLER_3D:
105 case GL_SAMPLER_CUBE:
106 case GL_SAMPLER_1D_SHADOW:
107 case GL_SAMPLER_2D_SHADOW:
108 case GL_SAMPLER_2D_RECT_ARB:
109 case GL_SAMPLER_2D_RECT_SHADOW_ARB:
110 case GL_SAMPLER_1D_ARRAY_EXT:
111 case GL_SAMPLER_2D_ARRAY_EXT:
112 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
113 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
114 return GL_TRUE;
115 default:
116 return GL_FALSE;
117 }
118 }
119
120
121 static struct gl_program_parameter *
122 get_uniform_parameter(const struct gl_shader_program *shProg, GLuint index)
123 {
124 const struct gl_program *prog = NULL;
125 GLint progPos;
126
127 progPos = shProg->Uniforms->Uniforms[index].VertPos;
128 if (progPos >= 0) {
129 prog = &shProg->VertexProgram->Base;
130 }
131 else {
132 progPos = shProg->Uniforms->Uniforms[index].FragPos;
133 if (progPos >= 0) {
134 prog = &shProg->FragmentProgram->Base;
135 } else {
136 progPos = shProg->Uniforms->Uniforms[index].GeomPos;
137 if (progPos >= 0) {
138 prog = &shProg->GeometryProgram->Base;
139 }
140 }
141 }
142
143 if (!prog || progPos < 0)
144 return NULL; /* should never happen */
145
146 return &prog->Parameters->Parameters[progPos];
147 }
148
149
150 /**
151 * Called by glGetActiveUniform().
152 */
153 static void
154 _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
155 GLsizei maxLength, GLsizei *length, GLint *size,
156 GLenum *type, GLchar *nameOut)
157 {
158 const struct gl_shader_program *shProg;
159 const struct gl_program *prog = NULL;
160 const struct gl_program_parameter *param;
161 GLint progPos;
162
163 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
164 if (!shProg)
165 return;
166
167 if (!shProg->Uniforms || index >= shProg->Uniforms->NumUniforms) {
168 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
169 return;
170 }
171
172 progPos = shProg->Uniforms->Uniforms[index].VertPos;
173 if (progPos >= 0) {
174 prog = &shProg->VertexProgram->Base;
175 }
176 else {
177 progPos = shProg->Uniforms->Uniforms[index].FragPos;
178 if (progPos >= 0) {
179 prog = &shProg->FragmentProgram->Base;
180 } else {
181 progPos = shProg->Uniforms->Uniforms[index].GeomPos;
182 if (progPos >= 0) {
183 prog = &shProg->GeometryProgram->Base;
184 }
185 }
186 }
187
188 if (!prog || progPos < 0)
189 return; /* should never happen */
190
191 ASSERT(progPos < prog->Parameters->NumParameters);
192 param = &prog->Parameters->Parameters[progPos];
193
194 if (nameOut) {
195 _mesa_copy_string(nameOut, maxLength, length, param->Name);
196 }
197
198 if (size) {
199 GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
200 if ((GLint) param->Size > typeSize) {
201 /* This is an array.
202 * Array elements are placed on vector[4] boundaries so they're
203 * a multiple of four floats. We round typeSize up to next multiple
204 * of four to get the right size below.
205 */
206 typeSize = (typeSize + 3) & ~3;
207 }
208 /* Note that the returned size is in units of the <type>, not bytes */
209 *size = param->Size / typeSize;
210 }
211
212 if (type) {
213 *type = param->DataType;
214 }
215 }
216
217
218
219 static void
220 get_matrix_dims(GLenum type, GLint *rows, GLint *cols)
221 {
222 switch (type) {
223 case GL_FLOAT_MAT2:
224 *rows = *cols = 2;
225 break;
226 case GL_FLOAT_MAT2x3:
227 *rows = 3;
228 *cols = 2;
229 break;
230 case GL_FLOAT_MAT2x4:
231 *rows = 4;
232 *cols = 2;
233 break;
234 case GL_FLOAT_MAT3:
235 *rows = 3;
236 *cols = 3;
237 break;
238 case GL_FLOAT_MAT3x2:
239 *rows = 2;
240 *cols = 3;
241 break;
242 case GL_FLOAT_MAT3x4:
243 *rows = 4;
244 *cols = 3;
245 break;
246 case GL_FLOAT_MAT4:
247 *rows = 4;
248 *cols = 4;
249 break;
250 case GL_FLOAT_MAT4x2:
251 *rows = 2;
252 *cols = 4;
253 break;
254 case GL_FLOAT_MAT4x3:
255 *rows = 3;
256 *cols = 4;
257 break;
258 default:
259 *rows = *cols = 0;
260 }
261 }
262
263
264 /**
265 * Determine the number of rows and columns occupied by a uniform
266 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
267 * the number of rows = 1 and cols = number of elements in the vector.
268 */
269 static void
270 get_uniform_rows_cols(const struct gl_program_parameter *p,
271 GLint *rows, GLint *cols)
272 {
273 get_matrix_dims(p->DataType, rows, cols);
274 if (*rows == 0 && *cols == 0) {
275 /* not a matrix type, probably a float or vector */
276 if (p->Size <= 4) {
277 *rows = 1;
278 *cols = p->Size;
279 }
280 else {
281 *rows = p->Size / 4 + 1;
282 if (p->Size % 4 == 0)
283 *cols = 4;
284 else
285 *cols = p->Size % 4;
286 }
287 }
288 }
289
290
291 /**
292 * Helper for get_uniform[fi]v() functions.
293 * Given a shader program name and uniform location, return a pointer
294 * to the shader program and return the program parameter position.
295 */
296 static void
297 lookup_uniform_parameter(GLcontext *ctx, GLuint program, GLint location,
298 struct gl_program **progOut, GLint *paramPosOut)
299 {
300 struct gl_shader_program *shProg
301 = _mesa_lookup_shader_program_err(ctx, program, "glGetUniform[if]v");
302 struct gl_program *prog = NULL;
303 GLint progPos = -1;
304
305 /* if shProg is NULL, we'll have already recorded an error */
306
307 if (shProg) {
308 if (!shProg->Uniforms ||
309 location < 0 ||
310 location >= (GLint) shProg->Uniforms->NumUniforms) {
311 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(location)");
312 }
313 else {
314 /* OK, find the gl_program and program parameter location */
315 progPos = shProg->Uniforms->Uniforms[location].VertPos;
316 if (progPos >= 0) {
317 prog = &shProg->VertexProgram->Base;
318 }
319 else {
320 progPos = shProg->Uniforms->Uniforms[location].FragPos;
321 if (progPos >= 0) {
322 prog = &shProg->FragmentProgram->Base;
323 } else {
324 progPos = shProg->Uniforms->Uniforms[location].GeomPos;
325 if (progPos >= 0) {
326 prog = &shProg->GeometryProgram->Base;
327 }
328 }
329 }
330 }
331 }
332
333 *progOut = prog;
334 *paramPosOut = progPos;
335 }
336
337
338 /**
339 * GLGL uniform arrays and structs require special handling.
340 *
341 * The GL_ARB_shader_objects spec says that if you use
342 * glGetUniformLocation to get the location of an array, you CANNOT
343 * access other elements of the array by adding an offset to the
344 * returned location. For example, you must call
345 * glGetUniformLocation("foo[16]") if you want to set the 16th element
346 * of the array with glUniform().
347 *
348 * HOWEVER, some other OpenGL drivers allow accessing array elements
349 * by adding an offset to the returned array location. And some apps
350 * seem to depend on that behaviour.
351 *
352 * Mesa's gl_uniform_list doesn't directly support this since each
353 * entry in the list describes one uniform variable, not one uniform
354 * element. We could insert dummy entries in the list for each array
355 * element after [0] but that causes complications elsewhere.
356 *
357 * We solve this problem by encoding two values in the location that's
358 * returned by glGetUniformLocation():
359 * a) index into gl_uniform_list::Uniforms[] for the uniform
360 * b) an array/field offset (0 for simple types)
361 *
362 * These two values are encoded in the high and low halves of a GLint.
363 * By putting the uniform number in the high part and the offset in the
364 * low part, we can support the unofficial ability to index into arrays
365 * by adding offsets to the location value.
366 */
367 static void
368 merge_location_offset(GLint *location, GLint offset)
369 {
370 *location = (*location << 16) | offset;
371 }
372
373
374 /**
375 * Separate the uniform location and parameter offset. See above.
376 */
377 static void
378 split_location_offset(GLint *location, GLint *offset)
379 {
380 *offset = *location & 0xffff;
381 *location = *location >> 16;
382 }
383
384
385
386 /**
387 * Called via glGetUniformfv().
388 */
389 static void
390 _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
391 GLfloat *params)
392 {
393 struct gl_program *prog;
394 GLint paramPos;
395 GLint offset;
396
397 split_location_offset(&location, &offset);
398
399 lookup_uniform_parameter(ctx, program, location, &prog, &paramPos);
400
401 if (prog) {
402 const struct gl_program_parameter *p =
403 &prog->Parameters->Parameters[paramPos];
404 GLint rows, cols, i, j, k;
405
406 get_uniform_rows_cols(p, &rows, &cols);
407
408 k = 0;
409 for (i = 0; i < rows; i++) {
410 for (j = 0; j < cols; j++ ) {
411 params[k++] = prog->Parameters->ParameterValues[paramPos+i][j];
412 }
413 }
414 }
415 }
416
417
418 /**
419 * Called via glGetUniformiv().
420 * \sa _mesa_get_uniformfv, only difference is a cast.
421 */
422 static void
423 _mesa_get_uniformiv(GLcontext *ctx, GLuint program, GLint location,
424 GLint *params)
425 {
426 struct gl_program *prog;
427 GLint paramPos;
428 GLint offset;
429
430 split_location_offset(&location, &offset);
431
432 lookup_uniform_parameter(ctx, program, location, &prog, &paramPos);
433
434 if (prog) {
435 const struct gl_program_parameter *p =
436 &prog->Parameters->Parameters[paramPos];
437 GLint rows, cols, i, j, k;
438
439 get_uniform_rows_cols(p, &rows, &cols);
440
441 k = 0;
442 for (i = 0; i < rows; i++) {
443 for (j = 0; j < cols; j++ ) {
444 params[k++] = (GLint) prog->Parameters->ParameterValues[paramPos+i][j];
445 }
446 }
447 }
448 }
449
450
451 /**
452 * Called via glGetUniformLocation().
453 *
454 * The return value will encode two values, the uniform location and an
455 * offset (used for arrays, structs).
456 */
457 static GLint
458 _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
459 {
460 GLint offset = 0, location = -1;
461
462 struct gl_shader_program *shProg =
463 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformLocation");
464
465 if (!shProg)
466 return -1;
467
468 if (shProg->LinkStatus == GL_FALSE) {
469 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
470 return -1;
471 }
472
473 /* XXX we should return -1 if the uniform was declared, but not
474 * actually used.
475 */
476
477 /* XXX we need to be able to parse uniform names for structs and arrays
478 * such as:
479 * mymatrix[1]
480 * mystruct.field1
481 */
482
483 {
484 /* handle 1-dimension arrays here... */
485 char *c = strchr(name, '[');
486 if (c) {
487 /* truncate name at [ */
488 const GLint len = c - name;
489 GLchar *newName = malloc(len + 1);
490 if (!newName)
491 return -1; /* out of mem */
492 memcpy(newName, name, len);
493 newName[len] = 0;
494
495 location = _mesa_lookup_uniform(shProg->Uniforms, newName);
496 if (location >= 0) {
497 const GLint element = atoi(c + 1);
498 if (element > 0) {
499 /* get type of the uniform array element */
500 struct gl_program_parameter *p;
501 p = get_uniform_parameter(shProg, location);
502 if (p) {
503 GLint rows, cols;
504 get_matrix_dims(p->DataType, &rows, &cols);
505 if (rows < 1)
506 rows = 1;
507 offset = element * rows;
508 }
509 }
510 }
511
512 free(newName);
513 }
514 }
515
516 if (location < 0) {
517 location = _mesa_lookup_uniform(shProg->Uniforms, name);
518 }
519
520 if (location >= 0) {
521 merge_location_offset(&location, offset);
522 }
523
524 return location;
525 }
526
527
528
529 /**
530 * Update the vertex/fragment program's TexturesUsed array.
531 *
532 * This needs to be called after glUniform(set sampler var) is called.
533 * A call to glUniform(samplerVar, value) causes a sampler to point to a
534 * particular texture unit. We know the sampler's texture target
535 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
536 * set by glUniform() calls.
537 *
538 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
539 * information to update the prog->TexturesUsed[] values.
540 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
541 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
542 * We'll use that info for state validation before rendering.
543 */
544 void
545 _mesa_update_shader_textures_used(struct gl_program *prog)
546 {
547 GLuint s;
548
549 memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed));
550
551 for (s = 0; s < MAX_SAMPLERS; s++) {
552 if (prog->SamplersUsed & (1 << s)) {
553 GLuint unit = prog->SamplerUnits[s];
554 GLuint tgt = prog->SamplerTargets[s];
555 assert(unit < MAX_TEXTURE_IMAGE_UNITS);
556 assert(tgt < NUM_TEXTURE_TARGETS);
557 prog->TexturesUsed[unit] |= (1 << tgt);
558 }
559 }
560 }
561
562
563 /**
564 * Check if the type given by userType is allowed to set a uniform of the
565 * target type. Generally, equivalence is required, but setting Boolean
566 * uniforms can be done with glUniformiv or glUniformfv.
567 */
568 static GLboolean
569 compatible_types(GLenum userType, GLenum targetType)
570 {
571 if (userType == targetType)
572 return GL_TRUE;
573
574 if (targetType == GL_BOOL && (userType == GL_FLOAT ||
575 userType == GL_UNSIGNED_INT ||
576 userType == GL_INT))
577 return GL_TRUE;
578
579 if (targetType == GL_BOOL_VEC2 && (userType == GL_FLOAT_VEC2 ||
580 userType == GL_UNSIGNED_INT_VEC2 ||
581 userType == GL_INT_VEC2))
582 return GL_TRUE;
583
584 if (targetType == GL_BOOL_VEC3 && (userType == GL_FLOAT_VEC3 ||
585 userType == GL_UNSIGNED_INT_VEC3 ||
586 userType == GL_INT_VEC3))
587 return GL_TRUE;
588
589 if (targetType == GL_BOOL_VEC4 && (userType == GL_FLOAT_VEC4 ||
590 userType == GL_UNSIGNED_INT_VEC4 ||
591 userType == GL_INT_VEC4))
592 return GL_TRUE;
593
594 if (is_sampler_type(targetType) && userType == GL_INT)
595 return GL_TRUE;
596
597 return GL_FALSE;
598 }
599
600
601 /**
602 * Set the value of a program's uniform variable.
603 * \param program the program whose uniform to update
604 * \param index the index of the program parameter for the uniform
605 * \param offset additional parameter slot offset (for arrays)
606 * \param type the incoming datatype of 'values'
607 * \param count the number of uniforms to set
608 * \param elems number of elements per uniform (1, 2, 3 or 4)
609 * \param values the new values, of datatype 'type'
610 */
611 static void
612 set_program_uniform(GLcontext *ctx, struct gl_program *program,
613 GLint index, GLint offset,
614 GLenum type, GLsizei count, GLint elems,
615 const void *values)
616 {
617 const struct gl_program_parameter *param =
618 &program->Parameters->Parameters[index];
619
620 assert(offset >= 0);
621 assert(elems >= 1);
622 assert(elems <= 4);
623
624 if (!compatible_types(type, param->DataType)) {
625 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
626 return;
627 }
628
629 if (index + offset > (GLint) program->Parameters->Size) {
630 /* out of bounds! */
631 return;
632 }
633
634 if (param->Type == PROGRAM_SAMPLER) {
635 /* This controls which texture unit which is used by a sampler */
636 GLboolean changed = GL_FALSE;
637 GLint i;
638
639 /* this should have been caught by the compatible_types() check */
640 ASSERT(type == GL_INT);
641
642 /* loop over number of samplers to change */
643 for (i = 0; i < count; i++) {
644 GLuint sampler =
645 (GLuint) program->Parameters->ParameterValues[index + offset + i][0];
646 GLuint texUnit = ((GLuint *) values)[i];
647
648 /* check that the sampler (tex unit index) is legal */
649 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
650 _mesa_error(ctx, GL_INVALID_VALUE,
651 "glUniform1(invalid sampler/tex unit index for '%s')",
652 param->Name);
653 return;
654 }
655
656 /* This maps a sampler to a texture unit: */
657 if (sampler < MAX_SAMPLERS) {
658 #if 0
659 printf("Set program %p sampler %d '%s' to unit %u\n",
660 program, sampler, param->Name, texUnit);
661 #endif
662 if (program->SamplerUnits[sampler] != texUnit) {
663 program->SamplerUnits[sampler] = texUnit;
664 changed = GL_TRUE;
665 }
666 }
667 }
668
669 if (changed) {
670 /* When a sampler's value changes it usually requires rewriting
671 * a GPU program's TEX instructions since there may not be a
672 * sampler->texture lookup table. We signal this with the
673 * ProgramStringNotify() callback.
674 */
675 FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
676 _mesa_update_shader_textures_used(program);
677 /* Do we need to care about the return value here?
678 * This should not be the first time the driver was notified of
679 * this program.
680 */
681 (void) ctx->Driver.ProgramStringNotify(ctx, program->Target, program);
682 }
683 }
684 else {
685 /* ordinary uniform variable */
686 const GLboolean isUniformBool = is_boolean_type(param->DataType);
687 const GLenum basicType = base_uniform_type(type);
688 const GLint slots = (param->Size + 3) / 4;
689 const GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
690 GLsizei k, i;
691
692 if ((GLint) param->Size > typeSize) {
693 /* an array */
694 /* we'll ignore extra data below */
695 }
696 else {
697 /* non-array: count must be at most one; count == 0 is handled by the loop below */
698 if (count > 1) {
699 _mesa_error(ctx, GL_INVALID_OPERATION,
700 "glUniform(uniform '%s' is not an array)",
701 param->Name);
702 return;
703 }
704 }
705
706 /* loop over number of array elements */
707 for (k = 0; k < count; k++) {
708 GLfloat *uniformVal;
709
710 if (offset + k >= slots) {
711 /* Extra array data is ignored */
712 break;
713 }
714
715 /* uniformVal (the destination) is always float[4] */
716 uniformVal = program->Parameters->ParameterValues[index + offset + k];
717
718 if (basicType == GL_INT) {
719 /* convert user's ints to floats */
720 const GLint *iValues = ((const GLint *) values) + k * elems;
721 for (i = 0; i < elems; i++) {
722 uniformVal[i] = (GLfloat) iValues[i];
723 }
724 }
725 else if (basicType == GL_UNSIGNED_INT) {
726 /* convert user's uints to floats */
727 const GLuint *iValues = ((const GLuint *) values) + k * elems;
728 for (i = 0; i < elems; i++) {
729 uniformVal[i] = (GLfloat) iValues[i];
730 }
731 }
732 else {
733 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
734 assert(basicType == GL_FLOAT);
735 for (i = 0; i < elems; i++) {
736 uniformVal[i] = fValues[i];
737 }
738 }
739
740 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
741 if (isUniformBool) {
742 for (i = 0; i < elems; i++) {
743 uniformVal[i] = uniformVal[i] ? 1.0f : 0.0f;
744 }
745 }
746 }
747 }
748 }
749
750
751 /**
752 * Called via glUniform*() functions.
753 */
754 static void
755 _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
756 const GLvoid *values, GLenum type)
757 {
758 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
759 struct gl_uniform *uniform;
760 GLint elems, offset;
761
762 if (!shProg || !shProg->LinkStatus) {
763 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
764 return;
765 }
766
767 if (location == -1)
768 return; /* The standard specifies this as a no-op */
769
770 if (location < -1) {
771 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(location=%d)",
772 location);
773 return;
774 }
775
776 split_location_offset(&location, &offset);
777
778 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
779 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location=%d)", location);
780 return;
781 }
782
783 if (count < 0) {
784 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
785 return;
786 }
787
788 elems = _mesa_sizeof_glsl_type(type);
789
790 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
791
792 uniform = &shProg->Uniforms->Uniforms[location];
793
794 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
795 const GLenum basicType = base_uniform_type(type);
796 GLint i;
797 printf("Mesa: set program %u uniform %s (loc %d) to: ",
798 shProg->Name, uniform->Name, location);
799 if (basicType == GL_INT) {
800 const GLint *v = (const GLint *) values;
801 for (i = 0; i < count * elems; i++) {
802 printf("%d ", v[i]);
803 }
804 }
805 else if (basicType == GL_UNSIGNED_INT) {
806 const GLuint *v = (const GLuint *) values;
807 for (i = 0; i < count * elems; i++) {
808 printf("%u ", v[i]);
809 }
810 }
811 else {
812 const GLfloat *v = (const GLfloat *) values;
813 assert(basicType == GL_FLOAT);
814 for (i = 0; i < count * elems; i++) {
815 printf("%g ", v[i]);
816 }
817 }
818 printf("\n");
819 }
820
821 /* A uniform var may be used by both a vertex shader and a fragment
822 * shader. We may need to update one or both shader's uniform here:
823 */
824 if (shProg->VertexProgram) {
825 /* convert uniform location to program parameter index */
826 GLint index = uniform->VertPos;
827 if (index >= 0) {
828 set_program_uniform(ctx, &shProg->VertexProgram->Base,
829 index, offset, type, count, elems, values);
830 }
831 }
832
833 if (shProg->FragmentProgram) {
834 /* convert uniform location to program parameter index */
835 GLint index = uniform->FragPos;
836 if (index >= 0) {
837 set_program_uniform(ctx, &shProg->FragmentProgram->Base,
838 index, offset, type, count, elems, values);
839 }
840 }
841
842 if (shProg->GeometryProgram) {
843 /* convert uniform location to program parameter index */
844 GLint index = uniform->GeomPos;
845 if (index >= 0) {
846 set_program_uniform(ctx, &shProg->GeometryProgram->Base,
847 index, offset, type, count, elems, values);
848 }
849 }
850
851 uniform->Initialized = GL_TRUE;
852 }
853
854
855 /**
856 * Set a matrix-valued program parameter.
857 */
858 static void
859 set_program_uniform_matrix(GLcontext *ctx, struct gl_program *program,
860 GLuint index, GLuint offset,
861 GLuint count, GLuint rows, GLuint cols,
862 GLboolean transpose, const GLfloat *values)
863 {
864 GLuint mat, row, col;
865 GLuint src = 0;
866 const struct gl_program_parameter * param = &program->Parameters->Parameters[index];
867 const GLuint slots = (param->Size + 3) / 4;
868 const GLint typeSize = _mesa_sizeof_glsl_type(param->DataType);
869 GLint nr, nc;
870
871 /* check that the number of rows, columns is correct */
872 get_matrix_dims(param->DataType, &nr, &nc);
873 if (rows != nr || cols != nc) {
874 _mesa_error(ctx, GL_INVALID_OPERATION,
875 "glUniformMatrix(matrix size mismatch)");
876 return;
877 }
878
879 if ((GLint) param->Size <= typeSize) {
880 /* non-array: count must be at most one; count == 0 is handled by the loop below */
881 if (count > 1) {
882 _mesa_error(ctx, GL_INVALID_OPERATION,
883 "glUniformMatrix(uniform is not an array)");
884 return;
885 }
886 }
887
888 /*
889 * Note: the _columns_ of a matrix are stored in program registers, not
890 * the rows. So, the loops below look a little funny.
891 * XXX could optimize this a bit...
892 */
893
894 /* loop over matrices */
895 for (mat = 0; mat < count; mat++) {
896
897 /* each matrix: */
898 for (col = 0; col < cols; col++) {
899 GLfloat *v;
900 if (offset >= slots) {
901 /* Ignore writes beyond the end of (the used part of) an array */
902 return;
903 }
904 v = program->Parameters->ParameterValues[index + offset];
905 for (row = 0; row < rows; row++) {
906 if (transpose) {
907 v[row] = values[src + row * cols + col];
908 }
909 else {
910 v[row] = values[src + col * rows + row];
911 }
912 }
913
914 offset++;
915 }
916
917 src += rows * cols; /* next matrix */
918 }
919 }
920
921
922 /**
923 * Called by glUniformMatrix*() functions.
924 * Note: cols=2, rows=4 ==> array[2] of vec4
925 */
926 static void
927 _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
928 GLint location, GLsizei count,
929 GLboolean transpose, const GLfloat *values)
930 {
931 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
932 struct gl_uniform *uniform;
933 GLint offset;
934
935 if (!shProg || !shProg->LinkStatus) {
936 _mesa_error(ctx, GL_INVALID_OPERATION,
937 "glUniformMatrix(program not linked)");
938 return;
939 }
940
941 if (location == -1)
942 return; /* The standard specifies this as a no-op */
943
944 if (location < -1) {
945 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniformMatrix(location)");
946 return;
947 }
948
949 split_location_offset(&location, &offset);
950
951 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
952 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
953 return;
954 }
955 if (values == NULL) {
956 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
957 return;
958 }
959
960 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
961
962 uniform = &shProg->Uniforms->Uniforms[location];
963
964 if (shProg->VertexProgram) {
965 /* convert uniform location to program parameter index */
966 GLint index = uniform->VertPos;
967 if (index >= 0) {
968 set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
969 index, offset,
970 count, rows, cols, transpose, values);
971 }
972 }
973
974 if (shProg->FragmentProgram) {
975 /* convert uniform location to program parameter index */
976 GLint index = uniform->FragPos;
977 if (index >= 0) {
978 set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
979 index, offset,
980 count, rows, cols, transpose, values);
981 }
982 }
983
984 if (shProg->GeometryProgram) {
985 /* convert uniform location to program parameter index */
986 GLint index = uniform->GeomPos;
987 if (index >= 0) {
988 set_program_uniform_matrix(ctx, &shProg->GeometryProgram->Base,
989 index, offset,
990 count, rows, cols, transpose, values);
991 }
992 }
993
994 uniform->Initialized = GL_TRUE;
995 }
996
997
998 void GLAPIENTRY
999 _mesa_Uniform1fARB(GLint location, GLfloat v0)
1000 {
1001 GET_CURRENT_CONTEXT(ctx);
1002 _mesa_uniform(ctx, location, 1, &v0, GL_FLOAT);
1003 }
1004
1005 void GLAPIENTRY
1006 _mesa_Uniform2fARB(GLint location, GLfloat v0, GLfloat v1)
1007 {
1008 GET_CURRENT_CONTEXT(ctx);
1009 GLfloat v[2];
1010 v[0] = v0;
1011 v[1] = v1;
1012 _mesa_uniform(ctx, location, 1, v, GL_FLOAT_VEC2);
1013 }
1014
1015 void GLAPIENTRY
1016 _mesa_Uniform3fARB(GLint location, GLfloat v0, GLfloat v1, GLfloat v2)
1017 {
1018 GET_CURRENT_CONTEXT(ctx);
1019 GLfloat v[3];
1020 v[0] = v0;
1021 v[1] = v1;
1022 v[2] = v2;
1023 _mesa_uniform(ctx, location, 1, v, GL_FLOAT_VEC3);
1024 }
1025
1026 void GLAPIENTRY
1027 _mesa_Uniform4fARB(GLint location, GLfloat v0, GLfloat v1, GLfloat v2,
1028 GLfloat v3)
1029 {
1030 GET_CURRENT_CONTEXT(ctx);
1031 GLfloat v[4];
1032 v[0] = v0;
1033 v[1] = v1;
1034 v[2] = v2;
1035 v[3] = v3;
1036 _mesa_uniform(ctx, location, 1, v, GL_FLOAT_VEC4);
1037 }
1038
1039 void GLAPIENTRY
1040 _mesa_Uniform1iARB(GLint location, GLint v0)
1041 {
1042 GET_CURRENT_CONTEXT(ctx);
1043 _mesa_uniform(ctx, location, 1, &v0, GL_INT);
1044 }
1045
1046 void GLAPIENTRY
1047 _mesa_Uniform2iARB(GLint location, GLint v0, GLint v1)
1048 {
1049 GET_CURRENT_CONTEXT(ctx);
1050 GLint v[2];
1051 v[0] = v0;
1052 v[1] = v1;
1053 _mesa_uniform(ctx, location, 1, v, GL_INT_VEC2);
1054 }
1055
1056 void GLAPIENTRY
1057 _mesa_Uniform3iARB(GLint location, GLint v0, GLint v1, GLint v2)
1058 {
1059 GET_CURRENT_CONTEXT(ctx);
1060 GLint v[3];
1061 v[0] = v0;
1062 v[1] = v1;
1063 v[2] = v2;
1064 _mesa_uniform(ctx, location, 1, v, GL_INT_VEC3);
1065 }
1066
1067 void GLAPIENTRY
1068 _mesa_Uniform4iARB(GLint location, GLint v0, GLint v1, GLint v2, GLint v3)
1069 {
1070 GET_CURRENT_CONTEXT(ctx);
1071 GLint v[4];
1072 v[0] = v0;
1073 v[1] = v1;
1074 v[2] = v2;
1075 v[3] = v3;
1076 _mesa_uniform(ctx, location, 1, v, GL_INT_VEC4);
1077 }
1078
1079 void GLAPIENTRY
1080 _mesa_Uniform1fvARB(GLint location, GLsizei count, const GLfloat * value)
1081 {
1082 GET_CURRENT_CONTEXT(ctx);
1083 _mesa_uniform(ctx, location, count, value, GL_FLOAT);
1084 }
1085
1086 void GLAPIENTRY
1087 _mesa_Uniform2fvARB(GLint location, GLsizei count, const GLfloat * value)
1088 {
1089 GET_CURRENT_CONTEXT(ctx);
1090 _mesa_uniform(ctx, location, count, value, GL_FLOAT_VEC2);
1091 }
1092
1093 void GLAPIENTRY
1094 _mesa_Uniform3fvARB(GLint location, GLsizei count, const GLfloat * value)
1095 {
1096 GET_CURRENT_CONTEXT(ctx);
1097 _mesa_uniform(ctx, location, count, value, GL_FLOAT_VEC3);
1098 }
1099
1100 void GLAPIENTRY
1101 _mesa_Uniform4fvARB(GLint location, GLsizei count, const GLfloat * value)
1102 {
1103 GET_CURRENT_CONTEXT(ctx);
1104 _mesa_uniform(ctx, location, count, value, GL_FLOAT_VEC4);
1105 }
1106
1107 void GLAPIENTRY
1108 _mesa_Uniform1ivARB(GLint location, GLsizei count, const GLint * value)
1109 {
1110 GET_CURRENT_CONTEXT(ctx);
1111 _mesa_uniform(ctx, location, count, value, GL_INT);
1112 }
1113
1114 void GLAPIENTRY
1115 _mesa_Uniform2ivARB(GLint location, GLsizei count, const GLint * value)
1116 {
1117 GET_CURRENT_CONTEXT(ctx);
1118 _mesa_uniform(ctx, location, count, value, GL_INT_VEC2);
1119 }
1120
1121 void GLAPIENTRY
1122 _mesa_Uniform3ivARB(GLint location, GLsizei count, const GLint * value)
1123 {
1124 GET_CURRENT_CONTEXT(ctx);
1125 _mesa_uniform(ctx, location, count, value, GL_INT_VEC3);
1126 }
1127
1128 void GLAPIENTRY
1129 _mesa_Uniform4ivARB(GLint location, GLsizei count, const GLint * value)
1130 {
1131 GET_CURRENT_CONTEXT(ctx);
1132 _mesa_uniform(ctx, location, count, value, GL_INT_VEC4);
1133 }
1134
1135
1136 /** OpenGL 3.0 GLuint-valued functions **/
1137 void GLAPIENTRY
1138 _mesa_Uniform1ui(GLint location, GLuint v0)
1139 {
1140 GET_CURRENT_CONTEXT(ctx);
1141 _mesa_uniform(ctx, location, 1, &v0, GL_UNSIGNED_INT);
1142 }
1143
1144 void GLAPIENTRY
1145 _mesa_Uniform2ui(GLint location, GLuint v0, GLuint v1)
1146 {
1147 GET_CURRENT_CONTEXT(ctx);
1148 GLuint v[2];
1149 v[0] = v0;
1150 v[1] = v1;
1151 _mesa_uniform(ctx, location, 1, v, GL_UNSIGNED_INT_VEC2);
1152 }
1153
1154 void GLAPIENTRY
1155 _mesa_Uniform3ui(GLint location, GLuint v0, GLuint v1, GLuint v2)
1156 {
1157 GET_CURRENT_CONTEXT(ctx);
1158 GLuint v[3];
1159 v[0] = v0;
1160 v[1] = v1;
1161 v[2] = v2;
1162 _mesa_uniform(ctx, location, 1, v, GL_UNSIGNED_INT_VEC3);
1163 }
1164
1165 void GLAPIENTRY
1166 _mesa_Uniform4ui(GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3)
1167 {
1168 GET_CURRENT_CONTEXT(ctx);
1169 GLuint v[4];
1170 v[0] = v0;
1171 v[1] = v1;
1172 v[2] = v2;
1173 v[3] = v3;
1174 _mesa_uniform(ctx, location, 1, v, GL_UNSIGNED_INT_VEC4);
1175 }
1176
1177 void GLAPIENTRY
1178 _mesa_Uniform1uiv(GLint location, GLsizei count, const GLuint *value)
1179 {
1180 GET_CURRENT_CONTEXT(ctx);
1181 _mesa_uniform(ctx, location, count, value, GL_UNSIGNED_INT);
1182 }
1183
1184 void GLAPIENTRY
1185 _mesa_Uniform2uiv(GLint location, GLsizei count, const GLuint *value)
1186 {
1187 GET_CURRENT_CONTEXT(ctx);
1188 _mesa_uniform(ctx, location, count, value, GL_UNSIGNED_INT_VEC2);
1189 }
1190
1191 void GLAPIENTRY
1192 _mesa_Uniform3uiv(GLint location, GLsizei count, const GLuint *value)
1193 {
1194 GET_CURRENT_CONTEXT(ctx);
1195 _mesa_uniform(ctx, location, count, value, GL_UNSIGNED_INT_VEC3);
1196 }
1197
1198 void GLAPIENTRY
1199 _mesa_Uniform4uiv(GLint location, GLsizei count, const GLuint *value)
1200 {
1201 GET_CURRENT_CONTEXT(ctx);
1202 _mesa_uniform(ctx, location, count, value, GL_UNSIGNED_INT_VEC4);
1203 }
1204
1205
1206
1207 void GLAPIENTRY
1208 _mesa_UniformMatrix2fvARB(GLint location, GLsizei count, GLboolean transpose,
1209 const GLfloat * value)
1210 {
1211 GET_CURRENT_CONTEXT(ctx);
1212 _mesa_uniform_matrix(ctx, 2, 2, location, count, transpose, value);
1213 }
1214
1215 void GLAPIENTRY
1216 _mesa_UniformMatrix3fvARB(GLint location, GLsizei count, GLboolean transpose,
1217 const GLfloat * value)
1218 {
1219 GET_CURRENT_CONTEXT(ctx);
1220 _mesa_uniform_matrix(ctx, 3, 3, location, count, transpose, value);
1221 }
1222
1223 void GLAPIENTRY
1224 _mesa_UniformMatrix4fvARB(GLint location, GLsizei count, GLboolean transpose,
1225 const GLfloat * value)
1226 {
1227 GET_CURRENT_CONTEXT(ctx);
1228 _mesa_uniform_matrix(ctx, 4, 4, location, count, transpose, value);
1229 }
1230
1231
1232 /**
1233 * Non-square UniformMatrix are OpenGL 2.1
1234 */
1235 void GLAPIENTRY
1236 _mesa_UniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose,
1237 const GLfloat *value)
1238 {
1239 GET_CURRENT_CONTEXT(ctx);
1240 _mesa_uniform_matrix(ctx, 2, 3, location, count, transpose, value);
1241 }
1242
1243 void GLAPIENTRY
1244 _mesa_UniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose,
1245 const GLfloat *value)
1246 {
1247 GET_CURRENT_CONTEXT(ctx);
1248 _mesa_uniform_matrix(ctx, 3, 2, location, count, transpose, value);
1249 }
1250
1251 void GLAPIENTRY
1252 _mesa_UniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose,
1253 const GLfloat *value)
1254 {
1255 GET_CURRENT_CONTEXT(ctx);
1256 _mesa_uniform_matrix(ctx, 2, 4, location, count, transpose, value);
1257 }
1258
1259 void GLAPIENTRY
1260 _mesa_UniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose,
1261 const GLfloat *value)
1262 {
1263 GET_CURRENT_CONTEXT(ctx);
1264 _mesa_uniform_matrix(ctx, 4, 2, location, count, transpose, value);
1265 }
1266
1267 void GLAPIENTRY
1268 _mesa_UniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose,
1269 const GLfloat *value)
1270 {
1271 GET_CURRENT_CONTEXT(ctx);
1272 _mesa_uniform_matrix(ctx, 3, 4, location, count, transpose, value);
1273 }
1274
1275 void GLAPIENTRY
1276 _mesa_UniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose,
1277 const GLfloat *value)
1278 {
1279 GET_CURRENT_CONTEXT(ctx);
1280 _mesa_uniform_matrix(ctx, 4, 3, location, count, transpose, value);
1281 }
1282
1283
1284 void GLAPIENTRY
1285 _mesa_GetUniformfvARB(GLhandleARB program, GLint location, GLfloat *params)
1286 {
1287 GET_CURRENT_CONTEXT(ctx);
1288 _mesa_get_uniformfv(ctx, program, location, params);
1289 }
1290
1291
1292 void GLAPIENTRY
1293 _mesa_GetUniformivARB(GLhandleARB program, GLint location, GLint *params)
1294 {
1295 GET_CURRENT_CONTEXT(ctx);
1296 _mesa_get_uniformiv(ctx, program, location, params);
1297 }
1298
1299
1300 GLint GLAPIENTRY
1301 _mesa_GetUniformLocationARB(GLhandleARB programObj, const GLcharARB *name)
1302 {
1303 GET_CURRENT_CONTEXT(ctx);
1304 return _mesa_get_uniform_location(ctx, programObj, name);
1305 }
1306
1307
1308 void GLAPIENTRY
1309 _mesa_GetActiveUniformARB(GLhandleARB program, GLuint index,
1310 GLsizei maxLength, GLsizei * length, GLint * size,
1311 GLenum * type, GLcharARB * name)
1312 {
1313 GET_CURRENT_CONTEXT(ctx);
1314 _mesa_get_active_uniform(ctx, program, index, maxLength, length, size,
1315 type, name);
1316 }
1317
1318
1319 /**
1320 * Plug in shader uniform-related functions into API dispatch table.
1321 */
1322 void
1323 _mesa_init_shader_uniform_dispatch(struct _glapi_table *exec)
1324 {
1325 #if FEATURE_GL
1326 SET_Uniform1fARB(exec, _mesa_Uniform1fARB);
1327 SET_Uniform2fARB(exec, _mesa_Uniform2fARB);
1328 SET_Uniform3fARB(exec, _mesa_Uniform3fARB);
1329 SET_Uniform4fARB(exec, _mesa_Uniform4fARB);
1330 SET_Uniform1iARB(exec, _mesa_Uniform1iARB);
1331 SET_Uniform2iARB(exec, _mesa_Uniform2iARB);
1332 SET_Uniform3iARB(exec, _mesa_Uniform3iARB);
1333 SET_Uniform4iARB(exec, _mesa_Uniform4iARB);
1334 SET_Uniform1fvARB(exec, _mesa_Uniform1fvARB);
1335 SET_Uniform2fvARB(exec, _mesa_Uniform2fvARB);
1336 SET_Uniform3fvARB(exec, _mesa_Uniform3fvARB);
1337 SET_Uniform4fvARB(exec, _mesa_Uniform4fvARB);
1338 SET_Uniform1ivARB(exec, _mesa_Uniform1ivARB);
1339 SET_Uniform2ivARB(exec, _mesa_Uniform2ivARB);
1340 SET_Uniform3ivARB(exec, _mesa_Uniform3ivARB);
1341 SET_Uniform4ivARB(exec, _mesa_Uniform4ivARB);
1342 SET_UniformMatrix2fvARB(exec, _mesa_UniformMatrix2fvARB);
1343 SET_UniformMatrix3fvARB(exec, _mesa_UniformMatrix3fvARB);
1344 SET_UniformMatrix4fvARB(exec, _mesa_UniformMatrix4fvARB);
1345
1346 SET_GetActiveUniformARB(exec, _mesa_GetActiveUniformARB);
1347 SET_GetUniformLocationARB(exec, _mesa_GetUniformLocationARB);
1348 SET_GetUniformfvARB(exec, _mesa_GetUniformfvARB);
1349 SET_GetUniformivARB(exec, _mesa_GetUniformivARB);
1350
1351 /* OpenGL 2.1 */
1352 SET_UniformMatrix2x3fv(exec, _mesa_UniformMatrix2x3fv);
1353 SET_UniformMatrix3x2fv(exec, _mesa_UniformMatrix3x2fv);
1354 SET_UniformMatrix2x4fv(exec, _mesa_UniformMatrix2x4fv);
1355 SET_UniformMatrix4x2fv(exec, _mesa_UniformMatrix4x2fv);
1356 SET_UniformMatrix3x4fv(exec, _mesa_UniformMatrix3x4fv);
1357 SET_UniformMatrix4x3fv(exec, _mesa_UniformMatrix4x3fv);
1358
1359 /* OpenGL 3.0 */
1360 /* XXX finish dispatch */
1361 (void) _mesa_Uniform1ui;
1362 (void) _mesa_Uniform2ui;
1363 (void) _mesa_Uniform3ui;
1364 (void) _mesa_Uniform4ui;
1365 (void) _mesa_Uniform1uiv;
1366 (void) _mesa_Uniform2uiv;
1367 (void) _mesa_Uniform3uiv;
1368 (void) _mesa_Uniform4uiv;
1369 #endif /* FEATURE_GL */
1370 }