Merge branch 'vbo_clean'
[mesa.git] / src / mesa / shader / shader_api.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file shader_api.c
28 * Implementation of GLSL-related API functions
29 * \author Brian Paul
30 */
31
32 /**
33 * XXX things to do:
34 * 1. Check that the right error code is generated for all _mesa_error() calls.
35 * 2. Insert FLUSH_VERTICES calls in various places
36 */
37
38
39 #include "main/glheader.h"
40 #include "main/context.h"
41 #include "main/hash.h"
42 #include "main/macros.h"
43 #include "shader/program.h"
44 #include "shader/prog_parameter.h"
45 #include "shader/prog_print.h"
46 #include "shader/prog_statevars.h"
47 #include "shader/prog_uniform.h"
48 #include "shader/shader_api.h"
49 #include "shader/slang/slang_compile.h"
50 #include "shader/slang/slang_link.h"
51 #include "glapi/dispatch.h"
52
53
54 /**
55 * Allocate a new gl_shader_program object, initialize it.
56 */
57 static struct gl_shader_program *
58 _mesa_new_shader_program(GLcontext *ctx, GLuint name)
59 {
60 struct gl_shader_program *shProg;
61 shProg = CALLOC_STRUCT(gl_shader_program);
62 if (shProg) {
63 shProg->Type = GL_SHADER_PROGRAM_MESA;
64 shProg->Name = name;
65 shProg->RefCount = 1;
66 shProg->Attributes = _mesa_new_parameter_list();
67 }
68 return shProg;
69 }
70
71
72 /**
73 * Clear (free) the shader program state that gets produced by linking.
74 */
75 void
76 _mesa_clear_shader_program_data(GLcontext *ctx,
77 struct gl_shader_program *shProg)
78 {
79 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
80 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
81
82 if (shProg->Uniforms) {
83 _mesa_free_uniform_list(shProg->Uniforms);
84 shProg->Uniforms = NULL;
85 }
86
87 if (shProg->Varying) {
88 _mesa_free_parameter_list(shProg->Varying);
89 shProg->Varying = NULL;
90 }
91 }
92
93
94 /**
95 * Free all the data that hangs off a shader program object, but not the
96 * object itself.
97 */
98 void
99 _mesa_free_shader_program_data(GLcontext *ctx,
100 struct gl_shader_program *shProg)
101 {
102 GLuint i;
103
104 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
105
106 _mesa_clear_shader_program_data(ctx, shProg);
107
108 if (shProg->Attributes) {
109 _mesa_free_parameter_list(shProg->Attributes);
110 shProg->Attributes = NULL;
111 }
112
113 /* detach shaders */
114 for (i = 0; i < shProg->NumShaders; i++) {
115 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
116 }
117 shProg->NumShaders = 0;
118
119 if (shProg->Shaders) {
120 _mesa_free(shProg->Shaders);
121 shProg->Shaders = NULL;
122 }
123
124 if (shProg->InfoLog) {
125 _mesa_free(shProg->InfoLog);
126 shProg->InfoLog = NULL;
127 }
128 }
129
130
131 /**
132 * Free/delete a shader program object.
133 */
134 void
135 _mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
136 {
137 _mesa_free_shader_program_data(ctx, shProg);
138
139 _mesa_free(shProg);
140 }
141
142
143 /**
144 * Set ptr to point to shProg.
145 * If ptr is pointing to another object, decrement its refcount (and delete
146 * if refcount hits zero).
147 * Then set ptr to point to shProg, incrementing its refcount.
148 */
149 /* XXX this could be static */
150 void
151 _mesa_reference_shader_program(GLcontext *ctx,
152 struct gl_shader_program **ptr,
153 struct gl_shader_program *shProg)
154 {
155 assert(ptr);
156 if (*ptr == shProg) {
157 /* no-op */
158 return;
159 }
160 if (*ptr) {
161 /* Unreference the old shader program */
162 GLboolean deleteFlag = GL_FALSE;
163 struct gl_shader_program *old = *ptr;
164
165 ASSERT(old->RefCount > 0);
166 old->RefCount--;
167 #if 0
168 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
169 (void *) old, old->Name, old->RefCount);
170 #endif
171 deleteFlag = (old->RefCount == 0);
172
173 if (deleteFlag) {
174 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
175 _mesa_free_shader_program(ctx, old);
176 }
177
178 *ptr = NULL;
179 }
180 assert(!*ptr);
181
182 if (shProg) {
183 shProg->RefCount++;
184 #if 0
185 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
186 (void *) shProg, shProg->Name, shProg->RefCount);
187 #endif
188 *ptr = shProg;
189 }
190 }
191
192
193 /**
194 * Lookup a GLSL program object.
195 */
196 struct gl_shader_program *
197 _mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
198 {
199 struct gl_shader_program *shProg;
200 if (name) {
201 shProg = (struct gl_shader_program *)
202 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
203 /* Note that both gl_shader and gl_shader_program objects are kept
204 * in the same hash table. Check the object's type to be sure it's
205 * what we're expecting.
206 */
207 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
208 return NULL;
209 }
210 return shProg;
211 }
212 return NULL;
213 }
214
215
216 /**
217 * As above, but record an error if program is not found.
218 */
219 static struct gl_shader_program *
220 _mesa_lookup_shader_program_err(GLcontext *ctx, GLuint name,
221 const char *caller)
222 {
223 if (!name) {
224 _mesa_error(ctx, GL_INVALID_VALUE, caller);
225 return NULL;
226 }
227 else {
228 struct gl_shader_program *shProg = (struct gl_shader_program *)
229 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
230 if (!shProg) {
231 _mesa_error(ctx, GL_INVALID_VALUE, caller);
232 return NULL;
233 }
234 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
235 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
236 return NULL;
237 }
238 return shProg;
239 }
240 }
241
242
243
244
245 /**
246 * Allocate a new gl_shader object, initialize it.
247 */
248 struct gl_shader *
249 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
250 {
251 struct gl_shader *shader;
252 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
253 shader = CALLOC_STRUCT(gl_shader);
254 if (shader) {
255 shader->Type = type;
256 shader->Name = name;
257 shader->RefCount = 1;
258 }
259 return shader;
260 }
261
262
263 void
264 _mesa_free_shader(GLcontext *ctx, struct gl_shader *sh)
265 {
266 if (sh->Source)
267 _mesa_free((void *) sh->Source);
268 if (sh->InfoLog)
269 _mesa_free(sh->InfoLog);
270 _mesa_reference_program(ctx, &sh->Program, NULL);
271 _mesa_free(sh);
272 }
273
274
275 /**
276 * Set ptr to point to sh.
277 * If ptr is pointing to another shader, decrement its refcount (and delete
278 * if refcount hits zero).
279 * Then set ptr to point to sh, incrementing its refcount.
280 */
281 /* XXX this could be static */
282 void
283 _mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
284 struct gl_shader *sh)
285 {
286 assert(ptr);
287 if (*ptr == sh) {
288 /* no-op */
289 return;
290 }
291 if (*ptr) {
292 /* Unreference the old shader */
293 GLboolean deleteFlag = GL_FALSE;
294 struct gl_shader *old = *ptr;
295
296 ASSERT(old->RefCount > 0);
297 old->RefCount--;
298 /*printf("SHADER DECR %p (%d) to %d\n",
299 (void*) old, old->Name, old->RefCount);*/
300 deleteFlag = (old->RefCount == 0);
301
302 if (deleteFlag) {
303 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
304 _mesa_free_shader(ctx, old);
305 }
306
307 *ptr = NULL;
308 }
309 assert(!*ptr);
310
311 if (sh) {
312 /* reference new */
313 sh->RefCount++;
314 /*printf("SHADER INCR %p (%d) to %d\n",
315 (void*) sh, sh->Name, sh->RefCount);*/
316 *ptr = sh;
317 }
318 }
319
320
321 /**
322 * Lookup a GLSL shader object.
323 */
324 struct gl_shader *
325 _mesa_lookup_shader(GLcontext *ctx, GLuint name)
326 {
327 if (name) {
328 struct gl_shader *sh = (struct gl_shader *)
329 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
330 /* Note that both gl_shader and gl_shader_program objects are kept
331 * in the same hash table. Check the object's type to be sure it's
332 * what we're expecting.
333 */
334 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
335 return NULL;
336 }
337 return sh;
338 }
339 return NULL;
340 }
341
342
343 /**
344 * As above, but record an error if shader is not found.
345 */
346 static struct gl_shader *
347 _mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
348 {
349 if (!name) {
350 _mesa_error(ctx, GL_INVALID_VALUE, caller);
351 return NULL;
352 }
353 else {
354 struct gl_shader *sh = (struct gl_shader *)
355 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
356 if (!sh) {
357 _mesa_error(ctx, GL_INVALID_VALUE, caller);
358 return NULL;
359 }
360 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
361 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
362 return NULL;
363 }
364 return sh;
365 }
366 }
367
368
369 /**
370 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
371 */
372 static GLbitfield
373 get_shader_flags(void)
374 {
375 GLbitfield flags = 0x0;
376 const char *env = _mesa_getenv("MESA_GLSL");
377
378 if (env) {
379 if (_mesa_strstr(env, "dump"))
380 flags |= GLSL_DUMP;
381 if (_mesa_strstr(env, "log"))
382 flags |= GLSL_LOG;
383 if (_mesa_strstr(env, "nopt"))
384 flags |= GLSL_NO_OPT;
385 else if (_mesa_strstr(env, "opt"))
386 flags |= GLSL_OPT;
387 if (_mesa_strstr(env, "uniform"))
388 flags |= GLSL_UNIFORMS;
389 }
390
391 return flags;
392 }
393
394
395 /**
396 * Initialize context's shader state.
397 */
398 void
399 _mesa_init_shader_state(GLcontext * ctx)
400 {
401 /* Device drivers may override these to control what kind of instructions
402 * are generated by the GLSL compiler.
403 */
404 ctx->Shader.EmitHighLevelInstructions = GL_TRUE;
405 ctx->Shader.EmitContReturn = GL_TRUE;
406 ctx->Shader.EmitCondCodes = GL_FALSE;
407 ctx->Shader.EmitComments = GL_FALSE;
408 ctx->Shader.Flags = get_shader_flags();
409
410 /* Default pragma settings */
411 ctx->Shader.DefaultPragmas.IgnoreOptimize = GL_FALSE;
412 ctx->Shader.DefaultPragmas.IgnoreDebug = GL_FALSE;
413 ctx->Shader.DefaultPragmas.Optimize = GL_TRUE;
414 ctx->Shader.DefaultPragmas.Debug = GL_FALSE;
415 }
416
417
418 /**
419 * Free the per-context shader-related state.
420 */
421 void
422 _mesa_free_shader_state(GLcontext *ctx)
423 {
424 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, NULL);
425 }
426
427
428 /**
429 * Copy string from <src> to <dst>, up to maxLength characters, returning
430 * length of <dst> in <length>.
431 * \param src the strings source
432 * \param maxLength max chars to copy
433 * \param length returns number of chars copied
434 * \param dst the string destination
435 */
436 static void
437 copy_string(GLchar *dst, GLsizei maxLength, GLsizei *length, const GLchar *src)
438 {
439 GLsizei len;
440 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
441 dst[len] = src[len];
442 if (maxLength > 0)
443 dst[len] = 0;
444 if (length)
445 *length = len;
446 }
447
448
449 static GLboolean
450 _mesa_is_program(GLcontext *ctx, GLuint name)
451 {
452 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
453 return shProg ? GL_TRUE : GL_FALSE;
454 }
455
456
457 static GLboolean
458 _mesa_is_shader(GLcontext *ctx, GLuint name)
459 {
460 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
461 return shader ? GL_TRUE : GL_FALSE;
462 }
463
464
465 /**
466 * Called via ctx->Driver.AttachShader()
467 */
468 static void
469 _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
470 {
471 struct gl_shader_program *shProg;
472 struct gl_shader *sh;
473 GLuint i, n;
474
475 shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
476 if (!shProg)
477 return;
478
479 sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
480 if (!sh) {
481 return;
482 }
483
484 n = shProg->NumShaders;
485 for (i = 0; i < n; i++) {
486 if (shProg->Shaders[i] == sh) {
487 /* The shader is already attched to this program. The
488 * GL_ARB_shader_objects spec says:
489 *
490 * "The error INVALID_OPERATION is generated by AttachObjectARB
491 * if <obj> is already attached to <containerObj>."
492 */
493 _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
494 return;
495 }
496 }
497
498 /* grow list */
499 shProg->Shaders = (struct gl_shader **)
500 _mesa_realloc(shProg->Shaders,
501 n * sizeof(struct gl_shader *),
502 (n + 1) * sizeof(struct gl_shader *));
503 if (!shProg->Shaders) {
504 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
505 return;
506 }
507
508 /* append */
509 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
510 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
511 shProg->NumShaders++;
512 }
513
514
515 static GLint
516 _mesa_get_attrib_location(GLcontext *ctx, GLuint program,
517 const GLchar *name)
518 {
519 struct gl_shader_program *shProg
520 = _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
521
522 if (!shProg) {
523 return -1;
524 }
525
526 if (!shProg->LinkStatus) {
527 _mesa_error(ctx, GL_INVALID_OPERATION,
528 "glGetAttribLocation(program not linked)");
529 return -1;
530 }
531
532 if (!name)
533 return -1;
534
535 if (shProg->VertexProgram) {
536 const struct gl_program_parameter_list *attribs =
537 shProg->VertexProgram->Base.Attributes;
538 if (attribs) {
539 GLint i = _mesa_lookup_parameter_index(attribs, -1, name);
540 if (i >= 0) {
541 return attribs->Parameters[i].StateIndexes[0];
542 }
543 }
544 }
545 return -1;
546 }
547
548
549 static void
550 _mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
551 const GLchar *name)
552 {
553 struct gl_shader_program *shProg;
554 const GLint size = -1; /* unknown size */
555 GLint i, oldIndex;
556 GLenum datatype = GL_FLOAT_VEC4;
557
558 shProg = _mesa_lookup_shader_program_err(ctx, program,
559 "glBindAttribLocation");
560 if (!shProg) {
561 return;
562 }
563
564 if (!name)
565 return;
566
567 if (strncmp(name, "gl_", 3) == 0) {
568 _mesa_error(ctx, GL_INVALID_OPERATION,
569 "glBindAttribLocation(illegal name)");
570 return;
571 }
572
573 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
574 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
575 return;
576 }
577
578 if (shProg->LinkStatus) {
579 /* get current index/location for the attribute */
580 oldIndex = _mesa_get_attrib_location(ctx, program, name);
581 }
582 else {
583 oldIndex = -1;
584 }
585
586 /* this will replace the current value if it's already in the list */
587 i = _mesa_add_attribute(shProg->Attributes, name, size, datatype, index);
588 if (i < 0) {
589 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindAttribLocation");
590 return;
591 }
592
593 /*
594 * Note that this attribute binding won't go into effect until
595 * glLinkProgram is called again.
596 */
597 }
598
599
600 static GLuint
601 _mesa_create_shader(GLcontext *ctx, GLenum type)
602 {
603 struct gl_shader *sh;
604 GLuint name;
605
606 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
607
608 switch (type) {
609 case GL_FRAGMENT_SHADER:
610 case GL_VERTEX_SHADER:
611 sh = _mesa_new_shader(ctx, name, type);
612 break;
613 default:
614 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
615 return 0;
616 }
617
618 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
619
620 return name;
621 }
622
623
624 static GLuint
625 _mesa_create_program(GLcontext *ctx)
626 {
627 GLuint name;
628 struct gl_shader_program *shProg;
629
630 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
631 shProg = _mesa_new_shader_program(ctx, name);
632
633 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
634
635 assert(shProg->RefCount == 1);
636
637 return name;
638 }
639
640
641 /**
642 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
643 * DeleteProgramARB.
644 */
645 static void
646 _mesa_delete_program2(GLcontext *ctx, GLuint name)
647 {
648 /*
649 * NOTE: deleting shaders/programs works a bit differently than
650 * texture objects (and buffer objects, etc). Shader/program
651 * handles/IDs exist in the hash table until the object is really
652 * deleted (refcount==0). With texture objects, the handle/ID is
653 * removed from the hash table in glDeleteTextures() while the tex
654 * object itself might linger until its refcount goes to zero.
655 */
656 struct gl_shader_program *shProg;
657
658 shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
659 if (!shProg)
660 return;
661
662 shProg->DeletePending = GL_TRUE;
663
664 /* effectively, decr shProg's refcount */
665 _mesa_reference_shader_program(ctx, &shProg, NULL);
666 }
667
668
669 static void
670 _mesa_delete_shader(GLcontext *ctx, GLuint shader)
671 {
672 struct gl_shader *sh;
673
674 sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
675 if (!sh)
676 return;
677
678 sh->DeletePending = GL_TRUE;
679
680 /* effectively, decr sh's refcount */
681 _mesa_reference_shader(ctx, &sh, NULL);
682 }
683
684
685 static void
686 _mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
687 {
688 struct gl_shader_program *shProg;
689 GLuint n;
690 GLuint i, j;
691
692 shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
693 if (!shProg)
694 return;
695
696 n = shProg->NumShaders;
697
698 for (i = 0; i < n; i++) {
699 if (shProg->Shaders[i]->Name == shader) {
700 /* found it */
701 struct gl_shader **newList;
702
703 /* release */
704 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
705
706 /* alloc new, smaller array */
707 newList = (struct gl_shader **)
708 _mesa_malloc((n - 1) * sizeof(struct gl_shader *));
709 if (!newList) {
710 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
711 return;
712 }
713 for (j = 0; j < i; j++) {
714 newList[j] = shProg->Shaders[j];
715 }
716 while (++i < n)
717 newList[j++] = shProg->Shaders[i];
718 _mesa_free(shProg->Shaders);
719
720 shProg->Shaders = newList;
721 shProg->NumShaders = n - 1;
722
723 #ifdef DEBUG
724 /* sanity check */
725 {
726 for (j = 0; j < shProg->NumShaders; j++) {
727 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
728 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
729 assert(shProg->Shaders[j]->RefCount > 0);
730 }
731 }
732 #endif
733
734 return;
735 }
736 }
737
738 /* not found */
739 {
740 GLenum err;
741 if (_mesa_is_shader(ctx, shader))
742 err = GL_INVALID_OPERATION;
743 else if (_mesa_is_program(ctx, shader))
744 err = GL_INVALID_OPERATION;
745 else
746 err = GL_INVALID_VALUE;
747 _mesa_error(ctx, err, "glDetachProgram(shader)");
748 return;
749 }
750 }
751
752
753 static GLint
754 sizeof_glsl_type(GLenum type)
755 {
756 switch (type) {
757 case GL_FLOAT:
758 case GL_INT:
759 case GL_BOOL:
760 case GL_SAMPLER_1D:
761 case GL_SAMPLER_2D:
762 case GL_SAMPLER_3D:
763 case GL_SAMPLER_CUBE:
764 case GL_SAMPLER_1D_SHADOW:
765 case GL_SAMPLER_2D_SHADOW:
766 case GL_SAMPLER_2D_RECT_ARB:
767 case GL_SAMPLER_2D_RECT_SHADOW_ARB:
768 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
769 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
770 case GL_SAMPLER_CUBE_SHADOW_EXT:
771 return 1;
772 case GL_FLOAT_VEC2:
773 case GL_INT_VEC2:
774 case GL_BOOL_VEC2:
775 return 2;
776 case GL_FLOAT_VEC3:
777 case GL_INT_VEC3:
778 case GL_BOOL_VEC3:
779 return 3;
780 case GL_FLOAT_VEC4:
781 case GL_INT_VEC4:
782 case GL_BOOL_VEC4:
783 return 4;
784 case GL_FLOAT_MAT2:
785 case GL_FLOAT_MAT2x3:
786 case GL_FLOAT_MAT2x4:
787 return 8; /* two float[4] vectors */
788 case GL_FLOAT_MAT3:
789 case GL_FLOAT_MAT3x2:
790 case GL_FLOAT_MAT3x4:
791 return 12; /* three float[4] vectors */
792 case GL_FLOAT_MAT4:
793 case GL_FLOAT_MAT4x2:
794 case GL_FLOAT_MAT4x3:
795 return 16; /* four float[4] vectors */
796 default:
797 _mesa_problem(NULL, "Invalid type in sizeof_glsl_type()");
798 return 1;
799 }
800 }
801
802
803 static GLboolean
804 is_boolean_type(GLenum type)
805 {
806 switch (type) {
807 case GL_BOOL:
808 case GL_BOOL_VEC2:
809 case GL_BOOL_VEC3:
810 case GL_BOOL_VEC4:
811 return GL_TRUE;
812 default:
813 return GL_FALSE;
814 }
815 }
816
817
818 static GLboolean
819 is_integer_type(GLenum type)
820 {
821 switch (type) {
822 case GL_INT:
823 case GL_INT_VEC2:
824 case GL_INT_VEC3:
825 case GL_INT_VEC4:
826 return GL_TRUE;
827 default:
828 return GL_FALSE;
829 }
830 }
831
832
833 static GLboolean
834 is_sampler_type(GLenum type)
835 {
836 switch (type) {
837 case GL_SAMPLER_1D:
838 case GL_SAMPLER_2D:
839 case GL_SAMPLER_3D:
840 case GL_SAMPLER_CUBE:
841 case GL_SAMPLER_1D_SHADOW:
842 case GL_SAMPLER_2D_SHADOW:
843 case GL_SAMPLER_2D_RECT_ARB:
844 case GL_SAMPLER_2D_RECT_SHADOW_ARB:
845 case GL_SAMPLER_1D_ARRAY_EXT:
846 case GL_SAMPLER_2D_ARRAY_EXT:
847 return GL_TRUE;
848 default:
849 return GL_FALSE;
850 }
851 }
852
853
854 static void
855 _mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index,
856 GLsizei maxLength, GLsizei *length, GLint *size,
857 GLenum *type, GLchar *nameOut)
858 {
859 const struct gl_program_parameter_list *attribs = NULL;
860 struct gl_shader_program *shProg;
861
862 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
863 if (!shProg)
864 return;
865
866 if (shProg->VertexProgram)
867 attribs = shProg->VertexProgram->Base.Attributes;
868
869 if (!attribs || index >= attribs->NumParameters) {
870 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
871 return;
872 }
873
874 copy_string(nameOut, maxLength, length, attribs->Parameters[index].Name);
875
876 if (size)
877 *size = attribs->Parameters[index].Size
878 / sizeof_glsl_type(attribs->Parameters[index].DataType);
879
880 if (type)
881 *type = attribs->Parameters[index].DataType;
882 }
883
884
885 static struct gl_program_parameter *
886 get_uniform_parameter(const struct gl_shader_program *shProg, GLuint index)
887 {
888 const struct gl_program *prog = NULL;
889 GLint progPos;
890
891 progPos = shProg->Uniforms->Uniforms[index].VertPos;
892 if (progPos >= 0) {
893 prog = &shProg->VertexProgram->Base;
894 }
895 else {
896 progPos = shProg->Uniforms->Uniforms[index].FragPos;
897 if (progPos >= 0) {
898 prog = &shProg->FragmentProgram->Base;
899 }
900 }
901
902 if (!prog || progPos < 0)
903 return NULL; /* should never happen */
904
905 return &prog->Parameters->Parameters[progPos];
906 }
907
908
909 /**
910 * Called via ctx->Driver.GetActiveUniform().
911 */
912 static void
913 _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
914 GLsizei maxLength, GLsizei *length, GLint *size,
915 GLenum *type, GLchar *nameOut)
916 {
917 const struct gl_shader_program *shProg;
918 const struct gl_program *prog = NULL;
919 const struct gl_program_parameter *param;
920 GLint progPos;
921
922 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
923 if (!shProg)
924 return;
925
926 if (!shProg->Uniforms || index >= shProg->Uniforms->NumUniforms) {
927 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
928 return;
929 }
930
931 progPos = shProg->Uniforms->Uniforms[index].VertPos;
932 if (progPos >= 0) {
933 prog = &shProg->VertexProgram->Base;
934 }
935 else {
936 progPos = shProg->Uniforms->Uniforms[index].FragPos;
937 if (progPos >= 0) {
938 prog = &shProg->FragmentProgram->Base;
939 }
940 }
941
942 if (!prog || progPos < 0)
943 return; /* should never happen */
944
945 ASSERT(progPos < prog->Parameters->NumParameters);
946 param = &prog->Parameters->Parameters[progPos];
947
948 if (nameOut) {
949 copy_string(nameOut, maxLength, length, param->Name);
950 }
951
952 if (size) {
953 GLint typeSize = sizeof_glsl_type(param->DataType);
954 if (param->Size > typeSize) {
955 /* This is an array.
956 * Array elements are placed on vector[4] boundaries so they're
957 * a multiple of four floats. We round typeSize up to next multiple
958 * of four to get the right size below.
959 */
960 typeSize = (typeSize + 3) & ~3;
961 }
962 /* Note that the returned size is in units of the <type>, not bytes */
963 *size = param->Size / typeSize;
964 }
965
966 if (type) {
967 *type = param->DataType;
968 }
969 }
970
971
972 /**
973 * Called via ctx->Driver.GetAttachedShaders().
974 */
975 static void
976 _mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
977 GLsizei *count, GLuint *obj)
978 {
979 struct gl_shader_program *shProg =
980 _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
981 if (shProg) {
982 GLuint i;
983 for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
984 obj[i] = shProg->Shaders[i]->Name;
985 }
986 if (count)
987 *count = i;
988 }
989 }
990
991
992 static GLuint
993 _mesa_get_handle(GLcontext *ctx, GLenum pname)
994 {
995 GLint handle = 0;
996
997 if (pname == GL_PROGRAM_OBJECT_ARB) {
998 CALL_GetIntegerv(ctx->Exec, (GL_CURRENT_PROGRAM, &handle));
999 } else {
1000 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
1001 }
1002
1003 return handle;
1004 }
1005
1006
1007 static void
1008 _mesa_get_programiv(GLcontext *ctx, GLuint program,
1009 GLenum pname, GLint *params)
1010 {
1011 const struct gl_program_parameter_list *attribs;
1012 struct gl_shader_program *shProg
1013 = _mesa_lookup_shader_program(ctx, program);
1014
1015 if (!shProg) {
1016 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
1017 return;
1018 }
1019
1020 if (shProg->VertexProgram)
1021 attribs = shProg->VertexProgram->Base.Attributes;
1022 else
1023 attribs = NULL;
1024
1025 switch (pname) {
1026 case GL_DELETE_STATUS:
1027 *params = shProg->DeletePending;
1028 break;
1029 case GL_LINK_STATUS:
1030 *params = shProg->LinkStatus;
1031 break;
1032 case GL_VALIDATE_STATUS:
1033 *params = shProg->Validated;
1034 break;
1035 case GL_INFO_LOG_LENGTH:
1036 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
1037 break;
1038 case GL_ATTACHED_SHADERS:
1039 *params = shProg->NumShaders;
1040 break;
1041 case GL_ACTIVE_ATTRIBUTES:
1042 *params = attribs ? attribs->NumParameters : 0;
1043 break;
1044 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
1045 *params = _mesa_longest_parameter_name(attribs, PROGRAM_INPUT) + 1;
1046 break;
1047 case GL_ACTIVE_UNIFORMS:
1048 *params = shProg->Uniforms ? shProg->Uniforms->NumUniforms : 0;
1049 break;
1050 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
1051 *params = _mesa_longest_uniform_name(shProg->Uniforms);
1052 if (*params > 0)
1053 (*params)++; /* add one for terminating zero */
1054 break;
1055 case GL_PROGRAM_BINARY_LENGTH_OES:
1056 *params = 0;
1057 break;
1058 default:
1059 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
1060 return;
1061 }
1062 }
1063
1064
1065 static void
1066 _mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
1067 {
1068 struct gl_shader *shader = _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
1069
1070 if (!shader) {
1071 return;
1072 }
1073
1074 switch (pname) {
1075 case GL_SHADER_TYPE:
1076 *params = shader->Type;
1077 break;
1078 case GL_DELETE_STATUS:
1079 *params = shader->DeletePending;
1080 break;
1081 case GL_COMPILE_STATUS:
1082 *params = shader->CompileStatus;
1083 break;
1084 case GL_INFO_LOG_LENGTH:
1085 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
1086 break;
1087 case GL_SHADER_SOURCE_LENGTH:
1088 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
1089 break;
1090 default:
1091 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
1092 return;
1093 }
1094 }
1095
1096
1097 static void
1098 _mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize,
1099 GLsizei *length, GLchar *infoLog)
1100 {
1101 struct gl_shader_program *shProg
1102 = _mesa_lookup_shader_program(ctx, program);
1103 if (!shProg) {
1104 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
1105 return;
1106 }
1107 copy_string(infoLog, bufSize, length, shProg->InfoLog);
1108 }
1109
1110
1111 static void
1112 _mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize,
1113 GLsizei *length, GLchar *infoLog)
1114 {
1115 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
1116 if (!sh) {
1117 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
1118 return;
1119 }
1120 copy_string(infoLog, bufSize, length, sh->InfoLog);
1121 }
1122
1123
1124 /**
1125 * Called via ctx->Driver.GetShaderSource().
1126 */
1127 static void
1128 _mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength,
1129 GLsizei *length, GLchar *sourceOut)
1130 {
1131 struct gl_shader *sh;
1132 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
1133 if (!sh) {
1134 return;
1135 }
1136 copy_string(sourceOut, maxLength, length, sh->Source);
1137 }
1138
1139
1140 static void
1141 get_matrix_dims(GLenum type, GLint *rows, GLint *cols)
1142 {
1143 switch (type) {
1144 case GL_FLOAT_MAT2:
1145 *rows = *cols = 2;
1146 break;
1147 case GL_FLOAT_MAT2x3:
1148 *rows = 3;
1149 *cols = 2;
1150 break;
1151 case GL_FLOAT_MAT2x4:
1152 *rows = 4;
1153 *cols = 2;
1154 break;
1155 case GL_FLOAT_MAT3:
1156 *rows = 3;
1157 *cols = 3;
1158 break;
1159 case GL_FLOAT_MAT3x2:
1160 *rows = 2;
1161 *cols = 3;
1162 break;
1163 case GL_FLOAT_MAT3x4:
1164 *rows = 4;
1165 *cols = 3;
1166 break;
1167 case GL_FLOAT_MAT4:
1168 *rows = 4;
1169 *cols = 4;
1170 break;
1171 case GL_FLOAT_MAT4x2:
1172 *rows = 2;
1173 *cols = 4;
1174 break;
1175 case GL_FLOAT_MAT4x3:
1176 *rows = 3;
1177 *cols = 4;
1178 break;
1179 default:
1180 *rows = *cols = 0;
1181 }
1182 }
1183
1184
1185 /**
1186 * Determine the number of rows and columns occupied by a uniform
1187 * according to its datatype. For non-matrix types (such as GL_FLOAT_VEC4),
1188 * the number of rows = 1 and cols = number of elements in the vector.
1189 */
1190 static void
1191 get_uniform_rows_cols(const struct gl_program_parameter *p,
1192 GLint *rows, GLint *cols)
1193 {
1194 get_matrix_dims(p->DataType, rows, cols);
1195 if (*rows == 0 && *cols == 0) {
1196 /* not a matrix type, probably a float or vector */
1197 if (p->Size <= 4) {
1198 *rows = 1;
1199 *cols = p->Size;
1200 }
1201 else {
1202 *rows = p->Size / 4 + 1;
1203 if (p->Size % 4 == 0)
1204 *cols = 4;
1205 else
1206 *cols = p->Size % 4;
1207 }
1208 }
1209 }
1210
1211
1212 /**
1213 * Helper for get_uniform[fi]v() functions.
1214 * Given a shader program name and uniform location, return a pointer
1215 * to the shader program and return the program parameter position.
1216 */
1217 static void
1218 lookup_uniform_parameter(GLcontext *ctx, GLuint program, GLint location,
1219 struct gl_program **progOut, GLint *paramPosOut)
1220 {
1221 struct gl_shader_program *shProg
1222 = _mesa_lookup_shader_program_err(ctx, program, "glGetUniform[if]v");
1223 struct gl_program *prog = NULL;
1224 GLint progPos = -1;
1225
1226 /* if shProg is NULL, we'll have already recorded an error */
1227
1228 if (shProg) {
1229 if (!shProg->Uniforms ||
1230 location < 0 ||
1231 location >= (GLint) shProg->Uniforms->NumUniforms) {
1232 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(location)");
1233 }
1234 else {
1235 /* OK, find the gl_program and program parameter location */
1236 progPos = shProg->Uniforms->Uniforms[location].VertPos;
1237 if (progPos >= 0) {
1238 prog = &shProg->VertexProgram->Base;
1239 }
1240 else {
1241 progPos = shProg->Uniforms->Uniforms[location].FragPos;
1242 if (progPos >= 0) {
1243 prog = &shProg->FragmentProgram->Base;
1244 }
1245 }
1246 }
1247 }
1248
1249 *progOut = prog;
1250 *paramPosOut = progPos;
1251 }
1252
1253
1254 /**
1255 * Called via ctx->Driver.GetUniformfv().
1256 */
1257 static void
1258 _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
1259 GLfloat *params)
1260 {
1261 struct gl_program *prog;
1262 GLint paramPos;
1263
1264 lookup_uniform_parameter(ctx, program, location, &prog, &paramPos);
1265
1266 if (prog) {
1267 const struct gl_program_parameter *p =
1268 &prog->Parameters->Parameters[paramPos];
1269 GLint rows, cols, i, j, k;
1270
1271 get_uniform_rows_cols(p, &rows, &cols);
1272
1273 k = 0;
1274 for (i = 0; i < rows; i++) {
1275 for (j = 0; j < cols; j++ ) {
1276 params[k++] = prog->Parameters->ParameterValues[paramPos+i][j];
1277 }
1278 }
1279 }
1280 }
1281
1282
1283 /**
1284 * Called via ctx->Driver.GetUniformiv().
1285 * \sa _mesa_get_uniformfv, only difference is a cast.
1286 */
1287 static void
1288 _mesa_get_uniformiv(GLcontext *ctx, GLuint program, GLint location,
1289 GLint *params)
1290 {
1291 struct gl_program *prog;
1292 GLint paramPos;
1293
1294 lookup_uniform_parameter(ctx, program, location, &prog, &paramPos);
1295
1296 if (prog) {
1297 const struct gl_program_parameter *p =
1298 &prog->Parameters->Parameters[paramPos];
1299 GLint rows, cols, i, j, k;
1300
1301 get_uniform_rows_cols(p, &rows, &cols);
1302
1303 k = 0;
1304 for (i = 0; i < rows; i++) {
1305 for (j = 0; j < cols; j++ ) {
1306 params[k++] = (GLint) prog->Parameters->ParameterValues[paramPos+i][j];
1307 }
1308 }
1309 }
1310 }
1311
1312
1313 /**
1314 * The value returned by GetUniformLocation actually encodes two things:
1315 * 1. the index into the prog->Uniforms[] array for the uniform
1316 * 2. an offset in the prog->ParameterValues[] array for specifying array
1317 * elements or structure fields.
1318 * This function merges those two values.
1319 */
1320 static void
1321 merge_location_offset(GLint *location, GLint offset)
1322 {
1323 *location = *location | (offset << 16);
1324 }
1325
1326
1327 /**
1328 * Seperate the uniform location and parameter offset. See above.
1329 */
1330 static void
1331 split_location_offset(GLint *location, GLint *offset)
1332 {
1333 *offset = (*location >> 16);
1334 *location = *location & 0xffff;
1335 }
1336
1337
1338 /**
1339 * Called via ctx->Driver.GetUniformLocation().
1340 *
1341 * The return value will encode two values, the uniform location and an
1342 * offset (used for arrays, structs).
1343 */
1344 static GLint
1345 _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
1346 {
1347 GLint offset = 0, location = -1;
1348
1349 struct gl_shader_program *shProg =
1350 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformLocation");
1351
1352 if (!shProg)
1353 return -1;
1354
1355 if (shProg->LinkStatus == GL_FALSE) {
1356 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
1357 return -1;
1358 }
1359
1360 /* XXX we should return -1 if the uniform was declared, but not
1361 * actually used.
1362 */
1363
1364 /* XXX we need to be able to parse uniform names for structs and arrays
1365 * such as:
1366 * mymatrix[1]
1367 * mystruct.field1
1368 */
1369
1370 {
1371 /* handle 1-dimension arrays here... */
1372 char *c = strchr(name, '[');
1373 if (c) {
1374 /* truncate name at [ */
1375 const GLint len = c - name;
1376 GLchar *newName = _mesa_malloc(len + 1);
1377 if (!newName)
1378 return -1; /* out of mem */
1379 _mesa_memcpy(newName, name, len);
1380 newName[len] = 0;
1381
1382 location = _mesa_lookup_uniform(shProg->Uniforms, newName);
1383 if (location >= 0) {
1384 const GLint element = _mesa_atoi(c + 1);
1385 if (element > 0) {
1386 /* get type of the uniform array element */
1387 struct gl_program_parameter *p;
1388 p = get_uniform_parameter(shProg, location);
1389 if (p) {
1390 GLint rows, cols;
1391 get_matrix_dims(p->DataType, &rows, &cols);
1392 if (rows < 1)
1393 rows = 1;
1394 offset = element * rows;
1395 }
1396 }
1397 }
1398
1399 _mesa_free(newName);
1400 }
1401 }
1402
1403 if (location < 0) {
1404 location = _mesa_lookup_uniform(shProg->Uniforms, name);
1405 }
1406
1407 if (location >= 0) {
1408 merge_location_offset(&location, offset);
1409 }
1410
1411 return location;
1412 }
1413
1414
1415
1416 /**
1417 * Called via ctx->Driver.ShaderSource()
1418 */
1419 static void
1420 _mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source)
1421 {
1422 struct gl_shader *sh;
1423
1424 sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
1425 if (!sh)
1426 return;
1427
1428 /* free old shader source string and install new one */
1429 if (sh->Source) {
1430 _mesa_free((void *) sh->Source);
1431 }
1432 sh->Source = source;
1433 sh->CompileStatus = GL_FALSE;
1434 #ifdef DEBUG
1435 sh->SourceChecksum = _mesa_str_checksum(sh->Source);
1436 #endif
1437 }
1438
1439
1440 /**
1441 * Called via ctx->Driver.CompileShader()
1442 */
1443 static void
1444 _mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
1445 {
1446 struct gl_shader *sh;
1447
1448 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
1449 if (!sh)
1450 return;
1451
1452 /* set default pragma state for shader */
1453 sh->Pragmas = ctx->Shader.DefaultPragmas;
1454
1455 /* this call will set the sh->CompileStatus field to indicate if
1456 * compilation was successful.
1457 */
1458 (void) _slang_compile(ctx, sh);
1459 }
1460
1461
1462 /**
1463 * Called via ctx->Driver.LinkProgram()
1464 */
1465 static void
1466 _mesa_link_program(GLcontext *ctx, GLuint program)
1467 {
1468 struct gl_shader_program *shProg;
1469
1470 shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
1471 if (!shProg)
1472 return;
1473
1474 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1475
1476 _slang_link(ctx, program, shProg);
1477 }
1478
1479
1480 /**
1481 * Called via ctx->Driver.UseProgram()
1482 */
1483 void
1484 _mesa_use_program(GLcontext *ctx, GLuint program)
1485 {
1486 struct gl_shader_program *shProg;
1487
1488 if (ctx->Shader.CurrentProgram &&
1489 ctx->Shader.CurrentProgram->Name == program) {
1490 /* no-op */
1491 return;
1492 }
1493
1494 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
1495
1496 if (program) {
1497 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
1498 if (!shProg) {
1499 return;
1500 }
1501 if (!shProg->LinkStatus) {
1502 _mesa_error(ctx, GL_INVALID_OPERATION,
1503 "glUseProgram(program %u not linked)", program);
1504 return;
1505 }
1506
1507 /* debug code */
1508 if (0) {
1509 GLuint i;
1510 _mesa_printf("Use Shader %u\n", shProg->Name);
1511 for (i = 0; i < shProg->NumShaders; i++) {
1512 _mesa_printf(" shader %u, type 0x%x, checksum %u\n",
1513 shProg->Shaders[i]->Name,
1514 shProg->Shaders[i]->Type,
1515 shProg->Shaders[i]->SourceChecksum);
1516 }
1517 if (shProg->VertexProgram)
1518 printf(" vert prog %u\n", shProg->VertexProgram->Base.Id);
1519 if (shProg->FragmentProgram)
1520 printf(" frag prog %u\n", shProg->FragmentProgram->Base.Id);
1521 }
1522 }
1523 else {
1524 shProg = NULL;
1525 }
1526
1527 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);
1528 }
1529
1530
1531
1532 /**
1533 * Update the vertex/fragment program's TexturesUsed array.
1534 *
1535 * This needs to be called after glUniform(set sampler var) is called.
1536 * A call to glUniform(samplerVar, value) causes a sampler to point to a
1537 * particular texture unit. We know the sampler's texture target
1538 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
1539 * set by glUniform() calls.
1540 *
1541 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
1542 * information to update the prog->TexturesUsed[] values.
1543 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
1544 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
1545 * We'll use that info for state validation before rendering.
1546 */
1547 void
1548 _mesa_update_shader_textures_used(struct gl_program *prog)
1549 {
1550 GLuint s;
1551
1552 memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed));
1553
1554 for (s = 0; s < MAX_SAMPLERS; s++) {
1555 if (prog->SamplersUsed & (1 << s)) {
1556 GLuint u = prog->SamplerUnits[s];
1557 GLuint t = prog->SamplerTargets[s];
1558 assert(u < MAX_TEXTURE_IMAGE_UNITS);
1559 prog->TexturesUsed[u] |= (1 << t);
1560 }
1561 }
1562 }
1563
1564
1565 /**
1566 * Check if the type given by userType is allowed to set a uniform of the
1567 * target type. Generally, equivalence is required, but setting Boolean
1568 * uniforms can be done with glUniformiv or glUniformfv.
1569 */
1570 static GLboolean
1571 compatible_types(GLenum userType, GLenum targetType)
1572 {
1573 if (userType == targetType)
1574 return GL_TRUE;
1575
1576 if (targetType == GL_BOOL && (userType == GL_FLOAT || userType == GL_INT))
1577 return GL_TRUE;
1578
1579 if (targetType == GL_BOOL_VEC2 && (userType == GL_FLOAT_VEC2 ||
1580 userType == GL_INT_VEC2))
1581 return GL_TRUE;
1582
1583 if (targetType == GL_BOOL_VEC3 && (userType == GL_FLOAT_VEC3 ||
1584 userType == GL_INT_VEC3))
1585 return GL_TRUE;
1586
1587 if (targetType == GL_BOOL_VEC4 && (userType == GL_FLOAT_VEC4 ||
1588 userType == GL_INT_VEC4))
1589 return GL_TRUE;
1590
1591 if (is_sampler_type(targetType) && userType == GL_INT)
1592 return GL_TRUE;
1593
1594 return GL_FALSE;
1595 }
1596
1597
1598 /**
1599 * Set the value of a program's uniform variable.
1600 * \param program the program whose uniform to update
1601 * \param index the index of the program parameter for the uniform
1602 * \param offset additional parameter slot offset (for arrays)
1603 * \param type the incoming datatype of 'values'
1604 * \param count the number of uniforms to set
1605 * \param elems number of elements per uniform (1, 2, 3 or 4)
1606 * \param values the new values, of datatype 'type'
1607 */
1608 static void
1609 set_program_uniform(GLcontext *ctx, struct gl_program *program,
1610 GLint index, GLint offset,
1611 GLenum type, GLsizei count, GLint elems,
1612 const void *values)
1613 {
1614 struct gl_program_parameter *param =
1615 &program->Parameters->Parameters[index];
1616 const GLboolean isUniformBool = is_boolean_type(param->DataType);
1617 const GLboolean areIntValues = is_integer_type(type);
1618
1619 assert(offset >= 0);
1620 assert(elems >= 1);
1621 assert(elems <= 4);
1622
1623 if (!compatible_types(type, param->DataType)) {
1624 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
1625 return;
1626 }
1627
1628 if (index + offset > (GLint) program->Parameters->Size) {
1629 /* out of bounds! */
1630 return;
1631 }
1632
1633 if (param->Type == PROGRAM_SAMPLER) {
1634 /* This controls which texture unit which is used by a sampler */
1635 GLuint texUnit, sampler;
1636 GLint i;
1637
1638 /* data type for setting samplers must be int */
1639 if (type != GL_INT) {
1640 _mesa_error(ctx, GL_INVALID_OPERATION,
1641 "glUniform(only glUniform1i can be used "
1642 "to set sampler uniforms)");
1643 return;
1644 }
1645
1646 /* XXX arrays of samplers haven't been tested much, but it's not a
1647 * common thing...
1648 */
1649 for (i = 0; i < count; i++) {
1650 sampler = (GLuint) program->Parameters->ParameterValues[index + i][0];
1651 texUnit = ((GLuint *) values)[i];
1652
1653 /* check that the sampler (tex unit index) is legal */
1654 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
1655 _mesa_error(ctx, GL_INVALID_VALUE,
1656 "glUniform1(invalid sampler/tex unit index)");
1657 return;
1658 }
1659
1660 /* This maps a sampler to a texture unit: */
1661 if (sampler < MAX_SAMPLERS) {
1662 program->SamplerUnits[sampler] = texUnit;
1663 }
1664 }
1665
1666 _mesa_update_shader_textures_used(program);
1667
1668 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1669 }
1670 else {
1671 /* ordinary uniform variable */
1672 GLsizei k, i;
1673 const GLint slots = (param->Size + 3) / 4;
1674 const GLint typeSize = sizeof_glsl_type(param->DataType);
1675
1676 if (param->Size > typeSize) {
1677 /* an array */
1678 /* we'll ignore extra data below */
1679 }
1680 else {
1681 /* non-array: count must be one */
1682 if (count != 1) {
1683 _mesa_error(ctx, GL_INVALID_OPERATION,
1684 "glUniform(uniform is not an array)");
1685 return;
1686 }
1687 }
1688
1689 /* loop over number of array elements */
1690 for (k = 0; k < count; k++) {
1691 GLfloat *uniformVal;
1692
1693 if (offset + k >= slots) {
1694 /* Extra array data is ignored */
1695 break;
1696 }
1697
1698 /* uniformVal (the destination) is always float[4] */
1699 uniformVal = program->Parameters->ParameterValues[index + offset + k];
1700
1701 if (areIntValues) {
1702 /* convert user's ints to floats */
1703 const GLint *iValues = ((const GLint *) values) + k * elems;
1704 for (i = 0; i < elems; i++) {
1705 uniformVal[i] = (GLfloat) iValues[i];
1706 }
1707 }
1708 else {
1709 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
1710 for (i = 0; i < elems; i++) {
1711 uniformVal[i] = fValues[i];
1712 }
1713 }
1714
1715 /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
1716 if (isUniformBool) {
1717 for (i = 0; i < elems; i++) {
1718 uniformVal[i] = uniformVal[i] ? 1.0f : 0.0f;
1719 }
1720 }
1721 }
1722 }
1723 }
1724
1725
1726 /**
1727 * Called via ctx->Driver.Uniform().
1728 */
1729 static void
1730 _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
1731 const GLvoid *values, GLenum type)
1732 {
1733 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1734 struct gl_uniform *uniform;
1735 GLint elems, offset;
1736 GLenum basicType;
1737
1738 if (!shProg || !shProg->LinkStatus) {
1739 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
1740 return;
1741 }
1742
1743 if (location == -1)
1744 return; /* The standard specifies this as a no-op */
1745
1746 if (location < -1) {
1747 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(location)");
1748 return;
1749 }
1750
1751 split_location_offset(&location, &offset);
1752
1753 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
1754 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
1755 return;
1756 }
1757
1758 if (count < 0) {
1759 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
1760 return;
1761 }
1762
1763 switch (type) {
1764 case GL_FLOAT:
1765 basicType = GL_FLOAT;
1766 elems = 1;
1767 break;
1768 case GL_INT:
1769 basicType = GL_INT;
1770 elems = 1;
1771 break;
1772 case GL_FLOAT_VEC2:
1773 basicType = GL_FLOAT;
1774 elems = 2;
1775 break;
1776 case GL_INT_VEC2:
1777 basicType = GL_INT;
1778 elems = 2;
1779 break;
1780 case GL_FLOAT_VEC3:
1781 basicType = GL_FLOAT;
1782 elems = 3;
1783 break;
1784 case GL_INT_VEC3:
1785 basicType = GL_INT;
1786 elems = 3;
1787 break;
1788 case GL_FLOAT_VEC4:
1789 basicType = GL_FLOAT;
1790 elems = 4;
1791 break;
1792 case GL_INT_VEC4:
1793 basicType = GL_INT;
1794 elems = 4;
1795 break;
1796 default:
1797 _mesa_problem(ctx, "Invalid type in _mesa_uniform");
1798 return;
1799 }
1800
1801 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1802
1803 uniform = &shProg->Uniforms->Uniforms[location];
1804
1805 if (ctx->Shader.Flags & GLSL_UNIFORMS) {
1806 GLint i;
1807 _mesa_printf("Mesa: set program %u uniform %s (loc %d) to: ",
1808 shProg->Name, uniform->Name, location);
1809 if (basicType == GL_INT) {
1810 const GLint *v = (const GLint *) values;
1811 for (i = 0; i < count * elems; i++) {
1812 _mesa_printf("%d ", v[i]);
1813 }
1814 }
1815 else {
1816 const GLfloat *v = (const GLfloat *) values;
1817 for (i = 0; i < count * elems; i++) {
1818 _mesa_printf("%g ", v[i]);
1819 }
1820 }
1821 _mesa_printf("\n");
1822 }
1823
1824 /* A uniform var may be used by both a vertex shader and a fragment
1825 * shader. We may need to update one or both shader's uniform here:
1826 */
1827 if (shProg->VertexProgram) {
1828 /* convert uniform location to program parameter index */
1829 GLint index = uniform->VertPos;
1830 if (index >= 0) {
1831 set_program_uniform(ctx, &shProg->VertexProgram->Base,
1832 index, offset, type, count, elems, values);
1833 }
1834 }
1835
1836 if (shProg->FragmentProgram) {
1837 /* convert uniform location to program parameter index */
1838 GLint index = uniform->FragPos;
1839 if (index >= 0) {
1840 set_program_uniform(ctx, &shProg->FragmentProgram->Base,
1841 index, offset, type, count, elems, values);
1842 }
1843 }
1844
1845 uniform->Initialized = GL_TRUE;
1846 }
1847
1848
1849 /**
1850 * Set a matrix-valued program parameter.
1851 */
1852 static void
1853 set_program_uniform_matrix(GLcontext *ctx, struct gl_program *program,
1854 GLuint index, GLuint offset,
1855 GLuint count, GLuint rows, GLuint cols,
1856 GLboolean transpose, const GLfloat *values)
1857 {
1858 GLuint mat, row, col;
1859 GLuint dst = index + offset, src = 0;
1860 GLint nr, nc;
1861
1862 /* check that the number of rows, columns is correct */
1863 get_matrix_dims(program->Parameters->Parameters[index].DataType, &nr, &nc);
1864 if (rows != nr || cols != nc) {
1865 _mesa_error(ctx, GL_INVALID_OPERATION,
1866 "glUniformMatrix(matrix size mismatch)");
1867 return;
1868 }
1869
1870 if (index + offset > program->Parameters->Size) {
1871 /* out of bounds! */
1872 return;
1873 }
1874
1875 /*
1876 * Note: the _columns_ of a matrix are stored in program registers, not
1877 * the rows. So, the loops below look a little funny.
1878 * XXX could optimize this a bit...
1879 */
1880
1881 /* loop over matrices */
1882 for (mat = 0; mat < count; mat++) {
1883
1884 /* each matrix: */
1885 for (col = 0; col < cols; col++) {
1886 GLfloat *v = program->Parameters->ParameterValues[dst];
1887 for (row = 0; row < rows; row++) {
1888 if (transpose) {
1889 v[row] = values[src + row * cols + col];
1890 }
1891 else {
1892 v[row] = values[src + col * rows + row];
1893 }
1894 }
1895 dst++;
1896 }
1897
1898 src += rows * cols; /* next matrix */
1899 }
1900 }
1901
1902
1903 /**
1904 * Called by ctx->Driver.UniformMatrix().
1905 * Note: cols=2, rows=4 ==> array[2] of vec4
1906 */
1907 static void
1908 _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
1909 GLint location, GLsizei count,
1910 GLboolean transpose, const GLfloat *values)
1911 {
1912 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1913 struct gl_uniform *uniform;
1914 GLint offset;
1915
1916 if (!shProg || !shProg->LinkStatus) {
1917 _mesa_error(ctx, GL_INVALID_OPERATION,
1918 "glUniformMatrix(program not linked)");
1919 return;
1920 }
1921
1922 if (location == -1)
1923 return; /* The standard specifies this as a no-op */
1924
1925 if (location < -1) {
1926 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniformMatrix(location)");
1927 return;
1928 }
1929
1930 split_location_offset(&location, &offset);
1931
1932 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
1933 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
1934 return;
1935 }
1936 if (values == NULL) {
1937 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
1938 return;
1939 }
1940
1941 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
1942
1943 uniform = &shProg->Uniforms->Uniforms[location];
1944
1945 if (shProg->VertexProgram) {
1946 /* convert uniform location to program parameter index */
1947 GLint index = uniform->VertPos;
1948 if (index >= 0) {
1949 set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
1950 index, offset,
1951 count, rows, cols, transpose, values);
1952 }
1953 }
1954
1955 if (shProg->FragmentProgram) {
1956 /* convert uniform location to program parameter index */
1957 GLint index = uniform->FragPos;
1958 if (index >= 0) {
1959 set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
1960 index, offset,
1961 count, rows, cols, transpose, values);
1962 }
1963 }
1964
1965 uniform->Initialized = GL_TRUE;
1966 }
1967
1968
1969 static void
1970 _mesa_validate_program(GLcontext *ctx, GLuint program)
1971 {
1972 struct gl_shader_program *shProg;
1973
1974 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1975 if (!shProg) {
1976 return;
1977 }
1978
1979 if (!shProg->LinkStatus) {
1980 shProg->Validated = GL_FALSE;
1981 return;
1982 }
1983
1984 /* From the GL spec, a program is invalid if any of these are true:
1985
1986 any two active samplers in the current program object are of
1987 different types, but refer to the same texture image unit,
1988
1989 any active sampler in the current program object refers to a texture
1990 image unit where fixed-function fragment processing accesses a
1991 texture target that does not match the sampler type, or
1992
1993 the sum of the number of active samplers in the program and the
1994 number of texture image units enabled for fixed-function fragment
1995 processing exceeds the combined limit on the total number of texture
1996 image units allowed.
1997 */
1998
1999 shProg->Validated = GL_TRUE;
2000 }
2001
2002
2003 /**
2004 * Plug in Mesa's GLSL functions into the device driver function table.
2005 */
2006 void
2007 _mesa_init_glsl_driver_functions(struct dd_function_table *driver)
2008 {
2009 driver->AttachShader = _mesa_attach_shader;
2010 driver->BindAttribLocation = _mesa_bind_attrib_location;
2011 driver->CompileShader = _mesa_compile_shader;
2012 driver->CreateProgram = _mesa_create_program;
2013 driver->CreateShader = _mesa_create_shader;
2014 driver->DeleteProgram2 = _mesa_delete_program2;
2015 driver->DeleteShader = _mesa_delete_shader;
2016 driver->DetachShader = _mesa_detach_shader;
2017 driver->GetActiveAttrib = _mesa_get_active_attrib;
2018 driver->GetActiveUniform = _mesa_get_active_uniform;
2019 driver->GetAttachedShaders = _mesa_get_attached_shaders;
2020 driver->GetAttribLocation = _mesa_get_attrib_location;
2021 driver->GetHandle = _mesa_get_handle;
2022 driver->GetProgramiv = _mesa_get_programiv;
2023 driver->GetProgramInfoLog = _mesa_get_program_info_log;
2024 driver->GetShaderiv = _mesa_get_shaderiv;
2025 driver->GetShaderInfoLog = _mesa_get_shader_info_log;
2026 driver->GetShaderSource = _mesa_get_shader_source;
2027 driver->GetUniformfv = _mesa_get_uniformfv;
2028 driver->GetUniformiv = _mesa_get_uniformiv;
2029 driver->GetUniformLocation = _mesa_get_uniform_location;
2030 driver->IsProgram = _mesa_is_program;
2031 driver->IsShader = _mesa_is_shader;
2032 driver->LinkProgram = _mesa_link_program;
2033 driver->ShaderSource = _mesa_shader_source;
2034 driver->Uniform = _mesa_uniform;
2035 driver->UniformMatrix = _mesa_uniform_matrix;
2036 driver->UseProgram = _mesa_use_program;
2037 driver->ValidateProgram = _mesa_validate_program;
2038 }