mesa: assorted glsl uniform/attribute fixes
[mesa.git] / src / mesa / shader / shader_api.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0
4 *
5 * Copyright (C) 2004-2007 Brian Paul 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 shader_api.c
27 * Implementation of GLSL-related API functions
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 "glheader.h"
39 #include "context.h"
40 #include "hash.h"
41 #include "macros.h"
42 #include "program.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_statevars.h"
46 #include "prog_uniform.h"
47 #include "shader/shader_api.h"
48 #include "shader/slang/slang_compile.h"
49 #include "shader/slang/slang_link.h"
50
51
52
53 /**
54 * Allocate a new gl_shader_program object, initialize it.
55 */
56 static struct gl_shader_program *
57 _mesa_new_shader_program(GLcontext *ctx, GLuint name)
58 {
59 struct gl_shader_program *shProg;
60 shProg = CALLOC_STRUCT(gl_shader_program);
61 if (shProg) {
62 shProg->Type = GL_SHADER_PROGRAM_MESA;
63 shProg->Name = name;
64 shProg->RefCount = 1;
65 shProg->Attributes = _mesa_new_parameter_list();
66 }
67 return shProg;
68 }
69
70
71 /**
72 * Clear (free) the shader program state that gets produced by linking.
73 */
74 void
75 _mesa_clear_shader_program_data(GLcontext *ctx,
76 struct gl_shader_program *shProg)
77 {
78 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
79 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
80
81 if (shProg->Uniforms) {
82 _mesa_free_uniform_list(shProg->Uniforms);
83 shProg->Uniforms = NULL;
84 }
85
86 if (shProg->Varying) {
87 _mesa_free_parameter_list(shProg->Varying);
88 shProg->Varying = NULL;
89 }
90 }
91
92
93 /**
94 * Free all the data that hangs off a shader program object, but not the
95 * object itself.
96 */
97 void
98 _mesa_free_shader_program_data(GLcontext *ctx,
99 struct gl_shader_program *shProg)
100 {
101 GLuint i;
102
103 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
104
105 _mesa_clear_shader_program_data(ctx, shProg);
106
107 if (shProg->Attributes) {
108 _mesa_free_parameter_list(shProg->Attributes);
109 shProg->Attributes = NULL;
110 }
111
112 /* detach shaders */
113 for (i = 0; i < shProg->NumShaders; i++) {
114 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
115 }
116 shProg->NumShaders = 0;
117
118 if (shProg->Shaders) {
119 _mesa_free(shProg->Shaders);
120 shProg->Shaders = NULL;
121 }
122
123 if (shProg->InfoLog) {
124 _mesa_free(shProg->InfoLog);
125 shProg->InfoLog = NULL;
126 }
127 }
128
129
130 /**
131 * Free/delete a shader program object.
132 */
133 void
134 _mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
135 {
136 _mesa_free_shader_program_data(ctx, shProg);
137
138 _mesa_free(shProg);
139 }
140
141
142 /**
143 * Set ptr to point to shProg.
144 * If ptr is pointing to another object, decrement its refcount (and delete
145 * if refcount hits zero).
146 * Then set ptr to point to shProg, incrementing its refcount.
147 */
148 /* XXX this could be static */
149 void
150 _mesa_reference_shader_program(GLcontext *ctx,
151 struct gl_shader_program **ptr,
152 struct gl_shader_program *shProg)
153 {
154 assert(ptr);
155 if (*ptr == shProg) {
156 /* no-op */
157 return;
158 }
159 if (*ptr) {
160 /* Unreference the old shader program */
161 GLboolean deleteFlag = GL_FALSE;
162 struct gl_shader_program *old = *ptr;
163
164 ASSERT(old->RefCount > 0);
165 old->RefCount--;
166 #if 0
167 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
168 (void *) old, old->Name, old->RefCount);
169 #endif
170 deleteFlag = (old->RefCount == 0);
171
172 if (deleteFlag) {
173 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
174 _mesa_free_shader_program(ctx, old);
175 }
176
177 *ptr = NULL;
178 }
179 assert(!*ptr);
180
181 if (shProg) {
182 shProg->RefCount++;
183 #if 0
184 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
185 (void *) shProg, shProg->Name, shProg->RefCount);
186 #endif
187 *ptr = shProg;
188 }
189 }
190
191
192 /**
193 * Lookup a GLSL program object.
194 */
195 struct gl_shader_program *
196 _mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
197 {
198 struct gl_shader_program *shProg;
199 if (name) {
200 shProg = (struct gl_shader_program *)
201 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
202 /* Note that both gl_shader and gl_shader_program objects are kept
203 * in the same hash table. Check the object's type to be sure it's
204 * what we're expecting.
205 */
206 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
207 return NULL;
208 }
209 return shProg;
210 }
211 return NULL;
212 }
213
214
215 /**
216 * As above, but record an error if program is not found.
217 */
218 static struct gl_shader_program *
219 _mesa_lookup_shader_program_err(GLcontext *ctx, GLuint name,
220 const char *caller)
221 {
222 if (!name) {
223 _mesa_error(ctx, GL_INVALID_VALUE, caller);
224 return NULL;
225 }
226 else {
227 struct gl_shader_program *shProg = (struct gl_shader_program *)
228 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
229 if (!shProg) {
230 _mesa_error(ctx, GL_INVALID_VALUE, caller);
231 return NULL;
232 }
233 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
234 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
235 return NULL;
236 }
237 return shProg;
238 }
239 }
240
241
242
243
244 /**
245 * Allocate a new gl_shader object, initialize it.
246 */
247 struct gl_shader *
248 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
249 {
250 struct gl_shader *shader;
251 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
252 shader = CALLOC_STRUCT(gl_shader);
253 if (shader) {
254 shader->Type = type;
255 shader->Name = name;
256 shader->RefCount = 1;
257 }
258 return shader;
259 }
260
261
262 void
263 _mesa_free_shader(GLcontext *ctx, struct gl_shader *sh)
264 {
265 GLuint i;
266 if (sh->Source)
267 _mesa_free((void *) sh->Source);
268 if (sh->InfoLog)
269 _mesa_free(sh->InfoLog);
270 for (i = 0; i < sh->NumPrograms; i++)
271 _mesa_reference_program(ctx, &sh->Programs[i], NULL);
272 if (sh->Programs)
273 _mesa_free(sh->Programs);
274 _mesa_free(sh);
275 }
276
277
278 /**
279 * Set ptr to point to sh.
280 * If ptr is pointing to another shader, decrement its refcount (and delete
281 * if refcount hits zero).
282 * Then set ptr to point to sh, incrementing its refcount.
283 */
284 /* XXX this could be static */
285 void
286 _mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
287 struct gl_shader *sh)
288 {
289 assert(ptr);
290 if (*ptr == sh) {
291 /* no-op */
292 return;
293 }
294 if (*ptr) {
295 /* Unreference the old shader */
296 GLboolean deleteFlag = GL_FALSE;
297 struct gl_shader *old = *ptr;
298
299 ASSERT(old->RefCount > 0);
300 old->RefCount--;
301 /*printf("SHADER DECR %p (%d) to %d\n",
302 (void*) old, old->Name, old->RefCount);*/
303 deleteFlag = (old->RefCount == 0);
304
305 if (deleteFlag) {
306 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
307 _mesa_free_shader(ctx, old);
308 }
309
310 *ptr = NULL;
311 }
312 assert(!*ptr);
313
314 if (sh) {
315 /* reference new */
316 sh->RefCount++;
317 /*printf("SHADER INCR %p (%d) to %d\n",
318 (void*) sh, sh->Name, sh->RefCount);*/
319 *ptr = sh;
320 }
321 }
322
323
324 /**
325 * Lookup a GLSL shader object.
326 */
327 struct gl_shader *
328 _mesa_lookup_shader(GLcontext *ctx, GLuint name)
329 {
330 if (name) {
331 struct gl_shader *sh = (struct gl_shader *)
332 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
333 /* Note that both gl_shader and gl_shader_program objects are kept
334 * in the same hash table. Check the object's type to be sure it's
335 * what we're expecting.
336 */
337 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
338 return NULL;
339 }
340 return sh;
341 }
342 return NULL;
343 }
344
345
346 /**
347 * As above, but record an error if shader is not found.
348 */
349 static struct gl_shader *
350 _mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
351 {
352 if (!name) {
353 _mesa_error(ctx, GL_INVALID_VALUE, caller);
354 return NULL;
355 }
356 else {
357 struct gl_shader *sh = (struct gl_shader *)
358 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
359 if (!sh) {
360 _mesa_error(ctx, GL_INVALID_VALUE, caller);
361 return NULL;
362 }
363 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
364 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
365 return NULL;
366 }
367 return sh;
368 }
369 }
370
371
372
373 /**
374 * Initialize context's shader state.
375 */
376 void
377 _mesa_init_shader_state(GLcontext * ctx)
378 {
379 /* Device drivers may override these to control what kind of instructions
380 * are generated by the GLSL compiler.
381 */
382 ctx->Shader.EmitHighLevelInstructions = GL_TRUE;
383 ctx->Shader.EmitCondCodes = GL_FALSE;/*GL_TRUE;*/ /* XXX probably want GL_FALSE... */
384 ctx->Shader.EmitComments = GL_FALSE;
385 }
386
387
388 /**
389 * Free the per-context shader-related state.
390 */
391 void
392 _mesa_free_shader_state(GLcontext *ctx)
393 {
394 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, NULL);
395 }
396
397
398 /**
399 * Copy string from <src> to <dst>, up to maxLength characters, returning
400 * length of <dst> in <length>.
401 * \param src the strings source
402 * \param maxLength max chars to copy
403 * \param length returns number of chars copied
404 * \param dst the string destination
405 */
406 static void
407 copy_string(GLchar *dst, GLsizei maxLength, GLsizei *length, const GLchar *src)
408 {
409 GLsizei len;
410 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
411 dst[len] = src[len];
412 if (maxLength > 0)
413 dst[len] = 0;
414 if (length)
415 *length = len;
416 }
417
418
419 static GLboolean
420 _mesa_is_program(GLcontext *ctx, GLuint name)
421 {
422 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
423 return shProg ? GL_TRUE : GL_FALSE;
424 }
425
426
427 static GLboolean
428 _mesa_is_shader(GLcontext *ctx, GLuint name)
429 {
430 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
431 return shader ? GL_TRUE : GL_FALSE;
432 }
433
434
435 /**
436 * Called via ctx->Driver.AttachShader()
437 */
438 static void
439 _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
440 {
441 struct gl_shader_program *shProg;
442 struct gl_shader *sh;
443 GLuint i, n;
444
445 shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
446 if (!shProg)
447 return;
448
449 sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
450 if (!sh) {
451 return;
452 }
453
454 n = shProg->NumShaders;
455 for (i = 0; i < n; i++) {
456 if (shProg->Shaders[i] == sh) {
457 /* already attached */
458 return;
459 }
460 }
461
462 /* grow list */
463 shProg->Shaders = (struct gl_shader **)
464 _mesa_realloc(shProg->Shaders,
465 n * sizeof(struct gl_shader *),
466 (n + 1) * sizeof(struct gl_shader *));
467 if (!shProg->Shaders) {
468 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
469 return;
470 }
471
472 /* append */
473 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
474 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
475 shProg->NumShaders++;
476 }
477
478
479 static GLint
480 _mesa_get_attrib_location(GLcontext *ctx, GLuint program,
481 const GLchar *name)
482 {
483 struct gl_shader_program *shProg
484 = _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
485
486 if (!shProg) {
487 return -1;
488 }
489
490 if (!shProg->LinkStatus) {
491 _mesa_error(ctx, GL_INVALID_OPERATION,
492 "glGetAttribLocation(program not linked)");
493 return -1;
494 }
495
496 if (!name)
497 return -1;
498
499 if (shProg->Attributes) {
500 GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
501 if (i >= 0) {
502 return shProg->Attributes->Parameters[i].StateIndexes[0];
503 }
504 }
505 return -1;
506 }
507
508
509 static void
510 _mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
511 const GLchar *name)
512 {
513 struct gl_shader_program *shProg;
514 const GLint size = -1; /* unknown size */
515 GLint i, oldIndex;
516 GLenum datatype;
517
518 shProg = _mesa_lookup_shader_program_err(ctx, program,
519 "glBindAttribLocation");
520 if (!shProg) {
521 return;
522 }
523
524 if (!name)
525 return;
526
527 if (strncmp(name, "gl_", 3) == 0) {
528 _mesa_error(ctx, GL_INVALID_OPERATION,
529 "glBindAttribLocation(illegal name)");
530 return;
531 }
532
533 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
534 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
535 return;
536 }
537
538 if (shProg->LinkStatus) {
539 /* get current index/location for the attribute */
540 oldIndex = _mesa_get_attrib_location(ctx, program, name);
541 assert(0);
542 }
543 else {
544 oldIndex = -1;
545 }
546
547 /* this will replace the current value if it's already in the list */
548 i = _mesa_add_attribute(shProg->Attributes, name, size, datatype, index);
549 if (i < 0) {
550 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindAttribLocation");
551 }
552
553 if (shProg->VertexProgram && oldIndex >= 0 && oldIndex != index) {
554 /* If the index changed, need to search/replace references to that attribute
555 * in the vertex program.
556 */
557 _slang_remap_attribute(&shProg->VertexProgram->Base, oldIndex, index);
558 }
559 }
560
561
562 static GLuint
563 _mesa_create_shader(GLcontext *ctx, GLenum type)
564 {
565 struct gl_shader *sh;
566 GLuint name;
567
568 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
569
570 switch (type) {
571 case GL_FRAGMENT_SHADER:
572 case GL_VERTEX_SHADER:
573 sh = _mesa_new_shader(ctx, name, type);
574 break;
575 default:
576 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
577 return 0;
578 }
579
580 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
581
582 return name;
583 }
584
585
586 static GLuint
587 _mesa_create_program(GLcontext *ctx)
588 {
589 GLuint name;
590 struct gl_shader_program *shProg;
591
592 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
593 shProg = _mesa_new_shader_program(ctx, name);
594
595 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
596
597 assert(shProg->RefCount == 1);
598
599 return name;
600 }
601
602
603 /**
604 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
605 * DeleteProgramARB.
606 */
607 static void
608 _mesa_delete_program2(GLcontext *ctx, GLuint name)
609 {
610 /*
611 * NOTE: deleting shaders/programs works a bit differently than
612 * texture objects (and buffer objects, etc). Shader/program
613 * handles/IDs exist in the hash table until the object is really
614 * deleted (refcount==0). With texture objects, the handle/ID is
615 * removed from the hash table in glDeleteTextures() while the tex
616 * object itself might linger until its refcount goes to zero.
617 */
618 struct gl_shader_program *shProg;
619
620 shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
621 if (!shProg)
622 return;
623
624 shProg->DeletePending = GL_TRUE;
625
626 /* effectively, decr shProg's refcount */
627 _mesa_reference_shader_program(ctx, &shProg, NULL);
628 }
629
630
631 static void
632 _mesa_delete_shader(GLcontext *ctx, GLuint shader)
633 {
634 struct gl_shader *sh;
635
636 sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
637 if (!sh)
638 return;
639
640 sh->DeletePending = GL_TRUE;
641
642 /* effectively, decr sh's refcount */
643 _mesa_reference_shader(ctx, &sh, NULL);
644 }
645
646
647 static void
648 _mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
649 {
650 struct gl_shader_program *shProg;
651 GLuint n;
652 GLuint i, j;
653
654 shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
655 if (!shProg)
656 return;
657
658 n = shProg->NumShaders;
659
660 for (i = 0; i < n; i++) {
661 if (shProg->Shaders[i]->Name == shader) {
662 /* found it */
663 struct gl_shader **newList;
664
665 /* release */
666 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
667
668 /* alloc new, smaller array */
669 newList = (struct gl_shader **)
670 _mesa_malloc((n - 1) * sizeof(struct gl_shader *));
671 if (!newList) {
672 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
673 return;
674 }
675 for (j = 0; j < i; j++) {
676 newList[j] = shProg->Shaders[j];
677 }
678 while (++i < n)
679 newList[j++] = shProg->Shaders[i];
680 _mesa_free(shProg->Shaders);
681
682 shProg->Shaders = newList;
683 shProg->NumShaders = n - 1;
684
685 #ifdef DEBUG
686 /* sanity check */
687 {
688 for (j = 0; j < shProg->NumShaders; j++) {
689 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
690 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
691 assert(shProg->Shaders[j]->RefCount > 0);
692 }
693 }
694 #endif
695
696 return;
697 }
698 }
699
700 /* not found */
701 {
702 GLenum err;
703 if (_mesa_is_shader(ctx, shader))
704 err = GL_INVALID_OPERATION;
705 else if (_mesa_is_program(ctx, shader))
706 err = GL_INVALID_OPERATION;
707 else
708 err = GL_INVALID_VALUE;
709 _mesa_error(ctx, err, "glDetachProgram(shader)");
710 return;
711 }
712 }
713
714
715 static GLint
716 sizeof_glsl_type(GLenum type)
717 {
718 switch (type) {
719 case GL_FLOAT:
720 case GL_INT:
721 case GL_BOOL:
722 return 1;
723 case GL_FLOAT_VEC2:
724 case GL_INT_VEC2:
725 case GL_BOOL_VEC2:
726 return 2;
727 case GL_FLOAT_VEC3:
728 case GL_INT_VEC3:
729 case GL_BOOL_VEC3:
730 return 3;
731 case GL_FLOAT_VEC4:
732 case GL_INT_VEC4:
733 case GL_BOOL_VEC4:
734 return 4;
735 case GL_FLOAT_MAT2:
736 case GL_FLOAT_MAT2x3:
737 case GL_FLOAT_MAT2x4:
738 return 8; /* two float[4] vectors */
739 case GL_FLOAT_MAT3:
740 case GL_FLOAT_MAT3x2:
741 case GL_FLOAT_MAT3x4:
742 return 12; /* three float[4] vectors */
743 case GL_FLOAT_MAT4:
744 case GL_FLOAT_MAT4x2:
745 case GL_FLOAT_MAT4x3:
746 return 16; /* four float[4] vectors */
747 default:
748 _mesa_problem(NULL, "Invalid type in sizeof_glsl_type()");
749 return 1;
750 }
751 }
752
753
754 static void
755 _mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index,
756 GLsizei maxLength, GLsizei *length, GLint *size,
757 GLenum *type, GLchar *nameOut)
758 {
759 struct gl_shader_program *shProg;
760
761 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
762 if (!shProg)
763 return;
764
765 if (!shProg->Attributes || index >= shProg->Attributes->NumParameters) {
766 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
767 return;
768 }
769
770 copy_string(nameOut, maxLength, length,
771 shProg->Attributes->Parameters[index].Name);
772 if (size)
773 *size = shProg->Attributes->Parameters[index].Size
774 / sizeof_glsl_type(shProg->Attributes->Parameters[index].DataType);
775 if (type)
776 *type = shProg->Attributes->Parameters[index].DataType;
777 }
778
779
780 /**
781 * Called via ctx->Driver.GetActiveUniform().
782 */
783 static void
784 _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
785 GLsizei maxLength, GLsizei *length, GLint *size,
786 GLenum *type, GLchar *nameOut)
787 {
788 const struct gl_shader_program *shProg;
789 const struct gl_program *prog;
790 GLint progPos;
791
792 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
793 if (!shProg)
794 return;
795
796 if (!shProg->Uniforms || index >= shProg->Uniforms->NumUniforms) {
797 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
798 return;
799 }
800
801 progPos = shProg->Uniforms->Uniforms[index].VertPos;
802 if (progPos >= 0) {
803 prog = &shProg->VertexProgram->Base;
804 }
805 else {
806 progPos = shProg->Uniforms->Uniforms[index].FragPos;
807 if (progPos >= 0) {
808 prog = &shProg->FragmentProgram->Base;
809 }
810 }
811
812 if (!prog || progPos < 0)
813 return; /* should never happen */
814
815 if (nameOut)
816 copy_string(nameOut, maxLength, length,
817 prog->Parameters->Parameters[progPos].Name);
818 if (size)
819 *size = prog->Parameters->Parameters[progPos].Size
820 / sizeof_glsl_type(prog->Parameters->Parameters[progPos].DataType);
821 if (type)
822 *type = prog->Parameters->Parameters[progPos].DataType;
823 }
824
825
826 /**
827 * Called via ctx->Driver.GetAttachedShaders().
828 */
829 static void
830 _mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
831 GLsizei *count, GLuint *obj)
832 {
833 struct gl_shader_program *shProg =
834 _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
835 if (shProg) {
836 GLuint i;
837 for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
838 obj[i] = shProg->Shaders[i]->Name;
839 }
840 if (count)
841 *count = i;
842 }
843 }
844
845
846 static GLuint
847 _mesa_get_handle(GLcontext *ctx, GLenum pname)
848 {
849 #if 0
850 GET_CURRENT_CONTEXT(ctx);
851
852 switch (pname) {
853 case GL_PROGRAM_OBJECT_ARB:
854 {
855 struct gl2_program_intf **pro = ctx->Shader.CurrentProgram;
856
857 if (pro != NULL)
858 return (**pro)._container._generic.
859 GetName((struct gl2_generic_intf **) (pro));
860 }
861 break;
862 default:
863 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
864 }
865 #endif
866 return 0;
867 }
868
869
870 static void
871 _mesa_get_programiv(GLcontext *ctx, GLuint program,
872 GLenum pname, GLint *params)
873 {
874 struct gl_shader_program *shProg
875 = _mesa_lookup_shader_program(ctx, program);
876
877 if (!shProg) {
878 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
879 return;
880 }
881
882 switch (pname) {
883 case GL_DELETE_STATUS:
884 *params = shProg->DeletePending;
885 break;
886 case GL_LINK_STATUS:
887 *params = shProg->LinkStatus;
888 break;
889 case GL_VALIDATE_STATUS:
890 *params = shProg->Validated;
891 break;
892 case GL_INFO_LOG_LENGTH:
893 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
894 break;
895 case GL_ATTACHED_SHADERS:
896 *params = shProg->NumShaders;
897 break;
898 case GL_ACTIVE_ATTRIBUTES:
899 *params = shProg->Attributes ? shProg->Attributes->NumParameters : 0;
900 break;
901 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
902 *params = _mesa_longest_parameter_name(shProg->Attributes,
903 PROGRAM_INPUT) + 1;
904 break;
905 case GL_ACTIVE_UNIFORMS:
906 *params = shProg->Uniforms ? shProg->Uniforms->NumUniforms : 0;
907 break;
908 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
909 *params = _mesa_longest_uniform_name(shProg->Uniforms);
910 if (*params > 0)
911 (*params)++; /* add one for terminating zero */
912 break;
913 default:
914 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
915 return;
916 }
917 }
918
919
920 static void
921 _mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
922 {
923 struct gl_shader *shader = _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
924
925 if (!shader) {
926 return;
927 }
928
929 switch (pname) {
930 case GL_SHADER_TYPE:
931 *params = shader->Type;
932 break;
933 case GL_DELETE_STATUS:
934 *params = shader->DeletePending;
935 break;
936 case GL_COMPILE_STATUS:
937 *params = shader->CompileStatus;
938 break;
939 case GL_INFO_LOG_LENGTH:
940 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
941 break;
942 case GL_SHADER_SOURCE_LENGTH:
943 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
944 break;
945 default:
946 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
947 return;
948 }
949 }
950
951
952 static void
953 _mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize,
954 GLsizei *length, GLchar *infoLog)
955 {
956 struct gl_shader_program *shProg
957 = _mesa_lookup_shader_program(ctx, program);
958 if (!shProg) {
959 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
960 return;
961 }
962 copy_string(infoLog, bufSize, length, shProg->InfoLog);
963 }
964
965
966 static void
967 _mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize,
968 GLsizei *length, GLchar *infoLog)
969 {
970 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
971 if (!sh) {
972 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
973 return;
974 }
975 copy_string(infoLog, bufSize, length, sh->InfoLog);
976 }
977
978
979 /**
980 * Called via ctx->Driver.GetShaderSource().
981 */
982 static void
983 _mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength,
984 GLsizei *length, GLchar *sourceOut)
985 {
986 struct gl_shader *sh;
987 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
988 if (!sh) {
989 return;
990 }
991 copy_string(sourceOut, maxLength, length, sh->Source);
992 }
993
994
995 #define MAX_UNIFORM_ELEMENTS 16
996
997 /**
998 * Helper for GetUniformfv(), GetUniformiv()
999 * Returns number of elements written to 'params' output.
1000 */
1001 static GLuint
1002 get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
1003 GLfloat *params)
1004 {
1005 struct gl_shader_program *shProg
1006 = _mesa_lookup_shader_program(ctx, program);
1007 if (shProg) {
1008 if (shProg->Uniforms &&
1009 location >= 0 && location < (GLint) shProg->Uniforms->NumUniforms) {
1010 GLint progPos;
1011 GLuint i;
1012 const struct gl_program *prog = NULL;
1013
1014 progPos = shProg->Uniforms->Uniforms[location].VertPos;
1015 if (progPos >= 0) {
1016 prog = &shProg->VertexProgram->Base;
1017 }
1018 else {
1019 progPos = shProg->Uniforms->Uniforms[location].FragPos;
1020 if (progPos >= 0) {
1021 prog = &shProg->FragmentProgram->Base;
1022 }
1023 }
1024
1025 ASSERT(prog);
1026 if (prog) {
1027 /* See uniformiv() below */
1028 assert(prog->Parameters->Parameters[progPos].Size <= MAX_UNIFORM_ELEMENTS);
1029
1030 for (i = 0; i < prog->Parameters->Parameters[progPos].Size; i++) {
1031 params[i] = prog->Parameters->ParameterValues[progPos][i];
1032 }
1033 return prog->Parameters->Parameters[progPos].Size;
1034 }
1035 }
1036 else {
1037 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(location)");
1038 }
1039 }
1040 else {
1041 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
1042 }
1043 return 0;
1044 }
1045
1046
1047 /**
1048 * Called via ctx->Driver.GetUniformfv().
1049 */
1050 static void
1051 _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
1052 GLfloat *params)
1053 {
1054 (void) get_uniformfv(ctx, program, location, params);
1055 }
1056
1057
1058 /**
1059 * Called via ctx->Driver.GetUniformiv().
1060 */
1061 static void
1062 _mesa_get_uniformiv(GLcontext *ctx, GLuint program, GLint location,
1063 GLint *params)
1064 {
1065 GLfloat fparams[MAX_UNIFORM_ELEMENTS];
1066 GLuint n = get_uniformfv(ctx, program, location, fparams);
1067 GLuint i;
1068 assert(n <= MAX_UNIFORM_ELEMENTS);
1069 for (i = 0; i < n; i++) {
1070 params[i] = (GLint) fparams[i];
1071 }
1072 }
1073
1074
1075 /**
1076 * Called via ctx->Driver.GetUniformLocation().
1077 */
1078 static GLint
1079 _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
1080 {
1081 struct gl_shader_program *shProg =
1082 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformLocation");
1083
1084 if (!shProg)
1085 return -1;
1086
1087 if (shProg->LinkStatus == GL_FALSE) {
1088 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
1089 return -1;
1090 }
1091
1092 /* XXX we should return -1 if the uniform was declared, but not
1093 * actually used.
1094 */
1095
1096 return _mesa_lookup_uniform(shProg->Uniforms, name);
1097 }
1098
1099
1100
1101 /**
1102 * Called via ctx->Driver.ShaderSource()
1103 */
1104 static void
1105 _mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source)
1106 {
1107 struct gl_shader *sh;
1108
1109 sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
1110 if (!sh)
1111 return;
1112
1113 /* free old shader source string and install new one */
1114 if (sh->Source) {
1115 _mesa_free((void *) sh->Source);
1116 }
1117 sh->Source = source;
1118 sh->CompileStatus = GL_FALSE;
1119 }
1120
1121
1122 /**
1123 * Called via ctx->Driver.CompileShader()
1124 */
1125 static void
1126 _mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
1127 {
1128 struct gl_shader *sh;
1129
1130 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
1131 if (!sh)
1132 return;
1133
1134 sh->CompileStatus = _slang_compile(ctx, sh);
1135 }
1136
1137
1138 /**
1139 * Called via ctx->Driver.LinkProgram()
1140 */
1141 static void
1142 _mesa_link_program(GLcontext *ctx, GLuint program)
1143 {
1144 struct gl_shader_program *shProg;
1145
1146 shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
1147 if (!shProg)
1148 return;
1149
1150 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1151
1152 _slang_link(ctx, program, shProg);
1153 }
1154
1155
1156 /**
1157 * Called via ctx->Driver.UseProgram()
1158 */
1159 void
1160 _mesa_use_program(GLcontext *ctx, GLuint program)
1161 {
1162 struct gl_shader_program *shProg;
1163
1164 if (ctx->Shader.CurrentProgram &&
1165 ctx->Shader.CurrentProgram->Name == program) {
1166 /* no-op */
1167 return;
1168 }
1169
1170 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1171
1172 if (program) {
1173 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
1174 if (!shProg) {
1175 return;
1176 }
1177 if (!shProg->LinkStatus) {
1178 _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgram");
1179 return;
1180 }
1181 }
1182 else {
1183 shProg = NULL;
1184 }
1185
1186 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);
1187 }
1188
1189
1190
1191 /**
1192 * Update the vertex and fragment program's TexturesUsed arrays.
1193 */
1194 static void
1195 update_textures_used(struct gl_program *prog)
1196 {
1197 GLuint s;
1198
1199 memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed));
1200
1201 for (s = 0; s < MAX_SAMPLERS; s++) {
1202 if (prog->SamplersUsed & (1 << s)) {
1203 GLuint u = prog->SamplerUnits[s];
1204 GLuint t = prog->SamplerTargets[s];
1205 assert(u < MAX_TEXTURE_IMAGE_UNITS);
1206 prog->TexturesUsed[u] |= (1 << t);
1207 }
1208 }
1209 }
1210
1211
1212 /**
1213 * Check if the type given by userType is allowed to set a uniform of the
1214 * target type. Generally, equivalence is required, but setting Boolean
1215 * uniforms can be done with glUniformiv or glUniformfv.
1216 */
1217 static GLboolean
1218 compatible_types(GLenum userType, GLenum targetType)
1219 {
1220 if (userType == targetType)
1221 return GL_TRUE;
1222
1223 if (targetType == GL_BOOL && (userType == GL_FLOAT || userType == GL_INT))
1224 return GL_TRUE;
1225
1226 if (targetType == GL_BOOL_VEC2 && (userType == GL_FLOAT_VEC2 ||
1227 userType == GL_INT_VEC2))
1228 return GL_TRUE;
1229
1230 if (targetType == GL_BOOL_VEC3 && (userType == GL_FLOAT_VEC3 ||
1231 userType == GL_INT_VEC3))
1232 return GL_TRUE;
1233
1234 if (targetType == GL_BOOL_VEC4 && (userType == GL_FLOAT_VEC4 ||
1235 userType == GL_INT_VEC4))
1236 return GL_TRUE;
1237
1238 return GL_FALSE;
1239 }
1240
1241
1242 /**
1243 * Set the value of a program's uniform variable.
1244 * \param program the program whose uniform to update
1245 * \param location the location/index of the uniform
1246 * \param type the datatype of the uniform
1247 * \param count the number of uniforms to set
1248 * \param elems number of elements per uniform
1249 * \param values the new values
1250 */
1251 static void
1252 set_program_uniform(GLcontext *ctx, struct gl_program *program, GLint location,
1253 GLenum type, GLsizei count, GLint elems, const void *values)
1254 {
1255 if (!compatible_types(type,
1256 program->Parameters->Parameters[location].DataType)) {
1257 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
1258 return;
1259 }
1260
1261 if (program->Parameters->Parameters[location].Type == PROGRAM_SAMPLER) {
1262 /* This controls which texture unit which is used by a sampler */
1263 GLuint texUnit, sampler;
1264
1265 /* data type for setting samplers must be int */
1266 if (type != GL_INT || count != 1) {
1267 _mesa_error(ctx, GL_INVALID_OPERATION,
1268 "glUniform(only glUniform1i can be used "
1269 "to set sampler uniforms)");
1270 return;
1271 }
1272
1273 sampler = (GLuint) program->Parameters->ParameterValues[location][0];
1274 texUnit = ((GLuint *) values)[0];
1275
1276 /* check that the sampler (tex unit index) is legal */
1277 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
1278 _mesa_error(ctx, GL_INVALID_VALUE,
1279 "glUniform1(invalid sampler/tex unit index)");
1280 return;
1281 }
1282
1283 /* This maps a sampler to a texture unit: */
1284 program->SamplerUnits[sampler] = texUnit;
1285 update_textures_used(program);
1286
1287 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1288 }
1289 else {
1290 /* ordinary uniform variable */
1291 GLsizei k, i;
1292
1293 if (count * elems > (GLint) program->Parameters->Parameters[location].Size) {
1294 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
1295 return;
1296 }
1297
1298 for (k = 0; k < count; k++) {
1299 GLfloat *uniformVal = program->Parameters->ParameterValues[location + k];
1300 if (type == GL_INT ||
1301 type == GL_INT_VEC2 ||
1302 type == GL_INT_VEC3 ||
1303 type == GL_INT_VEC4) {
1304 const GLint *iValues = ((const GLint *) values) + k * elems;
1305 for (i = 0; i < elems; i++) {
1306 uniformVal[i] = (GLfloat) iValues[i];
1307 }
1308 }
1309 else {
1310 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
1311 for (i = 0; i < elems; i++) {
1312 uniformVal[i] = fValues[i];
1313 }
1314 }
1315 }
1316 }
1317 }
1318
1319
1320 /**
1321 * Called via ctx->Driver.Uniform().
1322 */
1323 static void
1324 _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
1325 const GLvoid *values, GLenum type)
1326 {
1327 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1328 GLint elems;
1329
1330 if (!shProg || !shProg->LinkStatus) {
1331 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
1332 return;
1333 }
1334
1335 if (location == -1)
1336 return; /* The standard specifies this as a no-op */
1337
1338 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
1339 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
1340 return;
1341 }
1342
1343 if (count < 0) {
1344 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
1345 return;
1346 }
1347
1348 switch (type) {
1349 case GL_FLOAT:
1350 case GL_INT:
1351 elems = 1;
1352 break;
1353 case GL_FLOAT_VEC2:
1354 case GL_INT_VEC2:
1355 elems = 2;
1356 break;
1357 case GL_FLOAT_VEC3:
1358 case GL_INT_VEC3:
1359 elems = 3;
1360 break;
1361 case GL_FLOAT_VEC4:
1362 case GL_INT_VEC4:
1363 elems = 4;
1364 break;
1365 default:
1366 _mesa_problem(ctx, "Invalid type in _mesa_uniform");
1367 return;
1368 }
1369
1370 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1371
1372 /* A uniform var may be used by both a vertex shader and a fragment
1373 * shader. We may need to update one or both shader's uniform here:
1374 */
1375 if (shProg->VertexProgram) {
1376 GLint loc = shProg->Uniforms->Uniforms[location].VertPos;
1377 if (loc >= 0) {
1378 set_program_uniform(ctx, &shProg->VertexProgram->Base,
1379 loc, type, count, elems, values);
1380 }
1381 }
1382
1383 if (shProg->FragmentProgram) {
1384 GLint loc = shProg->Uniforms->Uniforms[location].FragPos;
1385 if (loc >= 0) {
1386 set_program_uniform(ctx, &shProg->FragmentProgram->Base,
1387 loc, type, count, elems, values);
1388 }
1389 }
1390 }
1391
1392
1393 static void
1394 get_matrix_dims(GLenum type, GLint *rows, GLint *cols)
1395 {
1396 switch (type) {
1397 case GL_FLOAT_MAT2:
1398 *rows = *cols = 2;
1399 break;
1400 case GL_FLOAT_MAT2x3:
1401 *rows = 3;
1402 *cols = 2;
1403 break;
1404 case GL_FLOAT_MAT2x4:
1405 *rows = 4;
1406 *cols = 2;
1407 break;
1408 case GL_FLOAT_MAT3:
1409 *rows = 3;
1410 *cols = 3;
1411 break;
1412 case GL_FLOAT_MAT3x2:
1413 *rows = 2;
1414 *cols = 3;
1415 break;
1416 case GL_FLOAT_MAT3x4:
1417 *rows = 4;
1418 *cols = 3;
1419 break;
1420 case GL_FLOAT_MAT4:
1421 *rows = 4;
1422 *cols = 4;
1423 break;
1424 case GL_FLOAT_MAT4x2:
1425 *rows = 2;
1426 *cols = 4;
1427 break;
1428 case GL_FLOAT_MAT4x3:
1429 *rows = 3;
1430 *cols = 4;
1431 break;
1432 default:
1433 *rows = *cols = 0;
1434 }
1435 }
1436
1437
1438 static void
1439 set_program_uniform_matrix(GLcontext *ctx, struct gl_program *program,
1440 GLuint location, GLuint count,
1441 GLuint rows, GLuint cols,
1442 GLboolean transpose, const GLfloat *values)
1443 {
1444 GLuint mat, row, col;
1445 GLuint dst = location, src = 0;
1446 GLint nr, nc;
1447
1448 /* check that the number of rows, columns is correct */
1449 get_matrix_dims(program->Parameters->Parameters[location].DataType, &nr, &nc);
1450 if (rows != nr || cols != nc) {
1451 _mesa_error(ctx, GL_INVALID_OPERATION,
1452 "glUniformMatrix(matrix size mismatch");
1453 return;
1454 }
1455
1456 /*
1457 * Note: the _columns_ of a matrix are stored in program registers, not
1458 * the rows. So, the loops below look a little funny.
1459 * XXX could optimize this a bit...
1460 */
1461
1462 /* loop over matrices */
1463 for (mat = 0; mat < count; mat++) {
1464
1465 /* each matrix: */
1466 for (col = 0; col < cols; col++) {
1467 GLfloat *v = program->Parameters->ParameterValues[dst];
1468 for (row = 0; row < rows; row++) {
1469 if (transpose) {
1470 v[row] = values[src + row * cols + col];
1471 }
1472 else {
1473 v[row] = values[src + col * rows + row];
1474 }
1475 }
1476 dst++;
1477 }
1478
1479 src += rows * cols; /* next matrix */
1480 }
1481 }
1482
1483
1484 /**
1485 * Called by ctx->Driver.UniformMatrix().
1486 * Note: cols=2, rows=4 ==> array[2] of vec4
1487 */
1488 static void
1489 _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
1490 GLenum matrixType, GLint location, GLsizei count,
1491 GLboolean transpose, const GLfloat *values)
1492 {
1493 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1494
1495 if (!shProg || !shProg->LinkStatus) {
1496 _mesa_error(ctx, GL_INVALID_OPERATION,
1497 "glUniformMatrix(program not linked)");
1498 return;
1499 }
1500
1501 if (location == -1)
1502 return; /* The standard specifies this as a no-op */
1503
1504 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
1505 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
1506 return;
1507 }
1508 if (values == NULL) {
1509 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
1510 return;
1511 }
1512
1513 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1514
1515 if (shProg->VertexProgram) {
1516 GLint loc = shProg->Uniforms->Uniforms[location].VertPos;
1517 if (loc >= 0) {
1518 set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
1519 loc, count, rows, cols, transpose, values);
1520 }
1521 }
1522
1523 if (shProg->FragmentProgram) {
1524 GLint loc = shProg->Uniforms->Uniforms[location].FragPos;
1525 if (loc >= 0) {
1526 set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
1527 loc, count, rows, cols, transpose, values);
1528 }
1529 }
1530 }
1531
1532
1533 static void
1534 _mesa_validate_program(GLcontext *ctx, GLuint program)
1535 {
1536 struct gl_shader_program *shProg;
1537 shProg = _mesa_lookup_shader_program(ctx, program);
1538 if (!shProg) {
1539 _mesa_error(ctx, GL_INVALID_VALUE, "glValidateProgram(program)");
1540 return;
1541 }
1542 /* XXX temporary */
1543 shProg->Validated = GL_TRUE;
1544
1545 /* From the GL spec:
1546 any two active samplers in the current program object are of
1547 different types, but refer to the same texture image unit,
1548
1549 any active sampler in the current program object refers to a texture
1550 image unit where fixed-function fragment processing accesses a
1551 texture target that does not match the sampler type, or
1552
1553 the sum of the number of active samplers in the program and the
1554 number of texture image units enabled for fixed-function fragment
1555 processing exceeds the combined limit on the total number of texture
1556 image units allowed.
1557 */
1558 }
1559
1560
1561 /**
1562 * Plug in Mesa's GLSL functions into the device driver function table.
1563 */
1564 void
1565 _mesa_init_glsl_driver_functions(struct dd_function_table *driver)
1566 {
1567 driver->AttachShader = _mesa_attach_shader;
1568 driver->BindAttribLocation = _mesa_bind_attrib_location;
1569 driver->CompileShader = _mesa_compile_shader;
1570 driver->CreateProgram = _mesa_create_program;
1571 driver->CreateShader = _mesa_create_shader;
1572 driver->DeleteProgram2 = _mesa_delete_program2;
1573 driver->DeleteShader = _mesa_delete_shader;
1574 driver->DetachShader = _mesa_detach_shader;
1575 driver->GetActiveAttrib = _mesa_get_active_attrib;
1576 driver->GetActiveUniform = _mesa_get_active_uniform;
1577 driver->GetAttachedShaders = _mesa_get_attached_shaders;
1578 driver->GetAttribLocation = _mesa_get_attrib_location;
1579 driver->GetHandle = _mesa_get_handle;
1580 driver->GetProgramiv = _mesa_get_programiv;
1581 driver->GetProgramInfoLog = _mesa_get_program_info_log;
1582 driver->GetShaderiv = _mesa_get_shaderiv;
1583 driver->GetShaderInfoLog = _mesa_get_shader_info_log;
1584 driver->GetShaderSource = _mesa_get_shader_source;
1585 driver->GetUniformfv = _mesa_get_uniformfv;
1586 driver->GetUniformiv = _mesa_get_uniformiv;
1587 driver->GetUniformLocation = _mesa_get_uniform_location;
1588 driver->IsProgram = _mesa_is_program;
1589 driver->IsShader = _mesa_is_shader;
1590 driver->LinkProgram = _mesa_link_program;
1591 driver->ShaderSource = _mesa_shader_source;
1592 driver->Uniform = _mesa_uniform;
1593 driver->UniformMatrix = _mesa_uniform_matrix;
1594 driver->UseProgram = _mesa_use_program;
1595 driver->ValidateProgram = _mesa_validate_program;
1596 }