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