use ctx->Driver.DeleteProgram() in a few more places
[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 "shader/shader_api.h"
47 #include "shader/slang/slang_compile.h"
48 #include "shader/slang/slang_link.h"
49
50
51
52 /**
53 * Allocate a new gl_shader_program object, initialize it.
54 */
55 static struct gl_shader_program *
56 _mesa_new_shader_program(GLcontext *ctx, GLuint name)
57 {
58 struct gl_shader_program *shProg;
59 shProg = CALLOC_STRUCT(gl_shader_program);
60 if (shProg) {
61 shProg->Type = GL_SHADER_PROGRAM_MESA;
62 shProg->Name = name;
63 shProg->RefCount = 1;
64 shProg->Attributes = _mesa_new_parameter_list();
65 }
66 return shProg;
67 }
68
69
70 /**
71 * Clear (free) the shader program state that gets produced by linking.
72 */
73 void
74 _mesa_clear_shader_program_data(GLcontext *ctx,
75 struct gl_shader_program *shProg)
76 {
77 if (shProg->VertexProgram) {
78 if (shProg->VertexProgram->Base.Parameters == shProg->Uniforms) {
79 /* to prevent a double-free in the next call */
80 shProg->VertexProgram->Base.Parameters = NULL;
81 }
82 ctx->Driver.DeleteProgram(ctx, &shProg->VertexProgram->Base);
83 shProg->VertexProgram = NULL;
84 }
85
86 if (shProg->FragmentProgram) {
87 if (shProg->FragmentProgram->Base.Parameters == shProg->Uniforms) {
88 /* to prevent a double-free in the next call */
89 shProg->FragmentProgram->Base.Parameters = NULL;
90 }
91 ctx->Driver.DeleteProgram(ctx, &shProg->FragmentProgram->Base);
92 shProg->FragmentProgram = NULL;
93 }
94
95 if (shProg->Uniforms) {
96 _mesa_free_parameter_list(shProg->Uniforms);
97 shProg->Uniforms = NULL;
98 }
99
100 if (shProg->Varying) {
101 _mesa_free_parameter_list(shProg->Varying);
102 shProg->Varying = NULL;
103 }
104 }
105
106
107 /**
108 * Free all the data that hangs off a shader program object, but not the
109 * object itself.
110 */
111 void
112 _mesa_free_shader_program_data(GLcontext *ctx,
113 struct gl_shader_program *shProg)
114 {
115 GLuint i;
116
117 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
118
119 _mesa_clear_shader_program_data(ctx, shProg);
120
121 if (shProg->Attributes) {
122 _mesa_free_parameter_list(shProg->Attributes);
123 shProg->Attributes = NULL;
124 }
125
126 /* detach shaders */
127 for (i = 0; i < shProg->NumShaders; i++) {
128 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
129 }
130 if (shProg->Shaders) {
131 _mesa_free(shProg->Shaders);
132 shProg->Shaders = NULL;
133 }
134 }
135
136
137 /**
138 * Free/delete a shader program object.
139 */
140 void
141 _mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
142 {
143 _mesa_free_shader_program_data(ctx, shProg);
144 if (shProg->Shaders) {
145 _mesa_free(shProg->Shaders);
146 shProg->Shaders = NULL;
147 }
148 _mesa_free(shProg);
149 }
150
151
152 /**
153 * Set ptr to point to shProg.
154 * If ptr is pointing to another object, decrement its refcount (and delete
155 * if refcount hits zero).
156 * Then set ptr to point to shProg, incrementing its refcount.
157 */
158 /* XXX this could be static */
159 void
160 _mesa_reference_shader_program(GLcontext *ctx,
161 struct gl_shader_program **ptr,
162 struct gl_shader_program *shProg)
163 {
164 assert(ptr);
165 if (*ptr == shProg) {
166 /* no-op */
167 return;
168 }
169 if (*ptr) {
170 /* Unreference the old shader program */
171 GLboolean deleteFlag = GL_FALSE;
172 struct gl_shader_program *old = *ptr;
173
174 ASSERT(old->RefCount > 0);
175 old->RefCount--;
176 /*printf("SHPROG DECR %p (%d) to %d\n",
177 (void*) old, old->Name, old->RefCount);*/
178 deleteFlag = (old->RefCount == 0);
179
180 if (deleteFlag) {
181 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
182 _mesa_free_shader_program(ctx, old);
183 }
184
185 *ptr = NULL;
186 }
187 assert(!*ptr);
188
189 if (shProg) {
190 shProg->RefCount++;
191 /*printf("SHPROG INCR %p (%d) to %d\n",
192 (void*) shProg, shProg->Name, shProg->RefCount);*/
193 *ptr = shProg;
194 }
195 }
196
197
198 /**
199 * Lookup a GLSL program object.
200 */
201 struct gl_shader_program *
202 _mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
203 {
204 struct gl_shader_program *shProg;
205 if (name) {
206 shProg = (struct gl_shader_program *)
207 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
208 /* Note that both gl_shader and gl_shader_program objects are kept
209 * in the same hash table. Check the object's type to be sure it's
210 * what we're expecting.
211 */
212 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
213 return NULL;
214 }
215 return shProg;
216 }
217 return NULL;
218 }
219
220
221 /**
222 * Allocate a new gl_shader object, initialize it.
223 */
224 struct gl_shader *
225 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
226 {
227 struct gl_shader *shader;
228 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
229 shader = CALLOC_STRUCT(gl_shader);
230 if (shader) {
231 shader->Type = type;
232 shader->Name = name;
233 shader->RefCount = 1;
234 }
235 return shader;
236 }
237
238
239 void
240 _mesa_free_shader(GLcontext *ctx, struct gl_shader *sh)
241 {
242 GLuint i;
243 if (sh->Source)
244 _mesa_free((void *) sh->Source);
245 if (sh->InfoLog)
246 _mesa_free(sh->InfoLog);
247 for (i = 0; i < sh->NumPrograms; i++) {
248 assert(sh->Programs[i]);
249 ctx->Driver.DeleteProgram(ctx, sh->Programs[i]);
250 }
251 if (sh->Programs)
252 _mesa_free(sh->Programs);
253 _mesa_free(sh);
254 }
255
256
257 /**
258 * Set ptr to point to sh.
259 * If ptr is pointing to another shader, decrement its refcount (and delete
260 * if refcount hits zero).
261 * Then set ptr to point to sh, incrementing its refcount.
262 */
263 /* XXX this could be static */
264 void
265 _mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
266 struct gl_shader *sh)
267 {
268 assert(ptr);
269 if (*ptr == sh) {
270 /* no-op */
271 return;
272 }
273 if (*ptr) {
274 /* Unreference the old shader */
275 GLboolean deleteFlag = GL_FALSE;
276 struct gl_shader *old = *ptr;
277
278 ASSERT(old->RefCount > 0);
279 old->RefCount--;
280 /*printf("SHADER DECR %p (%d) to %d\n",
281 (void*) old, old->Name, old->RefCount);*/
282 deleteFlag = (old->RefCount == 0);
283
284 if (deleteFlag) {
285 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
286 _mesa_free_shader(ctx, old);
287 }
288
289 *ptr = NULL;
290 }
291 assert(!*ptr);
292
293 if (sh) {
294 /* reference new */
295 sh->RefCount++;
296 /*printf("SHADER INCR %p (%d) to %d\n",
297 (void*) sh, sh->Name, sh->RefCount);*/
298 *ptr = sh;
299 }
300 }
301
302
303 /**
304 * Lookup a GLSL shader object.
305 */
306 struct gl_shader *
307 _mesa_lookup_shader(GLcontext *ctx, GLuint name)
308 {
309 if (name) {
310 struct gl_shader *sh = (struct gl_shader *)
311 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
312 /* Note that both gl_shader and gl_shader_program objects are kept
313 * in the same hash table. Check the object's type to be sure it's
314 * what we're expecting.
315 */
316 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
317 return NULL;
318 }
319 return sh;
320 }
321 return NULL;
322 }
323
324
325 /**
326 * Initialize context's shader state.
327 */
328 void
329 _mesa_init_shader_state(GLcontext * ctx)
330 {
331 /* Device drivers may override these to control what kind of instructions
332 * are generated by the GLSL compiler.
333 */
334 ctx->Shader.EmitHighLevelInstructions = GL_TRUE;
335 ctx->Shader.EmitCondCodes = GL_FALSE;/*GL_TRUE;*/ /* XXX probably want GL_FALSE... */
336 ctx->Shader.EmitComments = GL_FALSE;
337 }
338
339
340 /**
341 * Free the per-context shader-related state.
342 */
343 void
344 _mesa_free_shader_state(GLcontext *ctx)
345 {
346 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, NULL);
347 }
348
349
350 /**
351 * Copy string from <src> to <dst>, up to maxLength characters, returning
352 * length of <dst> in <length>.
353 * \param src the strings source
354 * \param maxLength max chars to copy
355 * \param length returns number of chars copied
356 * \param dst the string destination
357 */
358 static void
359 copy_string(GLchar *dst, GLsizei maxLength, GLsizei *length, const GLchar *src)
360 {
361 GLsizei len;
362 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
363 dst[len] = src[len];
364 if (maxLength > 0)
365 dst[len] = 0;
366 if (length)
367 *length = len;
368 }
369
370
371 /**
372 * Called via ctx->Driver.AttachShader()
373 */
374 static void
375 _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
376 {
377 struct gl_shader_program *shProg
378 = _mesa_lookup_shader_program(ctx, program);
379 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
380 const GLuint n = shProg->NumShaders;
381 GLuint i;
382
383 if (!shProg || !sh) {
384 _mesa_error(ctx, GL_INVALID_VALUE,
385 "glAttachShader(bad program or shader name)");
386 return;
387 }
388
389 for (i = 0; i < n; i++) {
390 if (shProg->Shaders[i] == sh) {
391 /* already attached */
392 return;
393 }
394 }
395
396 /* grow list */
397 shProg->Shaders = (struct gl_shader **)
398 _mesa_realloc(shProg->Shaders,
399 n * sizeof(struct gl_shader *),
400 (n + 1) * sizeof(struct gl_shader *));
401 if (!shProg->Shaders) {
402 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
403 return;
404 }
405
406 /* append */
407 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
408 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
409 shProg->NumShaders++;
410 }
411
412
413 static GLint
414 _mesa_get_attrib_location(GLcontext *ctx, GLuint program,
415 const GLchar *name)
416 {
417 struct gl_shader_program *shProg
418 = _mesa_lookup_shader_program(ctx, program);
419
420 if (!shProg) {
421 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttribLocation");
422 return -1;
423 }
424
425 if (!shProg->LinkStatus) {
426 _mesa_error(ctx, GL_INVALID_OPERATION,
427 "glGetAttribLocation(program not linked)");
428 return -1;
429 }
430
431 if (!name)
432 return -1;
433
434 if (shProg->Attributes) {
435 GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
436 if (i >= 0) {
437 return shProg->Attributes->Parameters[i].StateIndexes[0];
438 }
439 }
440 return -1;
441 }
442
443
444 static void
445 _mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
446 const GLchar *name)
447 {
448 struct gl_shader_program *shProg
449 = _mesa_lookup_shader_program(ctx, program);
450 const GLint size = -1; /* unknown size */
451 GLint i, oldIndex;
452
453 if (!shProg) {
454 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(program)");
455 return;
456 }
457
458 if (!name)
459 return;
460
461 if (strncmp(name, "gl_", 3) == 0) {
462 _mesa_error(ctx, GL_INVALID_OPERATION,
463 "glBindAttribLocation(illegal name)");
464 return;
465 }
466
467 if (shProg->LinkStatus) {
468 /* get current index/location for the attribute */
469 oldIndex = _mesa_get_attrib_location(ctx, program, name);
470 }
471 else {
472 oldIndex = -1;
473 }
474
475 /* this will replace the current value if it's already in the list */
476 i = _mesa_add_attribute(shProg->Attributes, name, size, index);
477 if (i < 0) {
478 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindAttribLocation");
479 }
480
481 if (shProg->VertexProgram && oldIndex >= 0 && oldIndex != index) {
482 /* If the index changed, need to search/replace references to that attribute
483 * in the vertex program.
484 */
485 _slang_remap_attribute(&shProg->VertexProgram->Base, oldIndex, index);
486 }
487 }
488
489
490 static GLuint
491 _mesa_create_shader(GLcontext *ctx, GLenum type)
492 {
493 struct gl_shader *sh;
494 GLuint name;
495
496 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
497
498 switch (type) {
499 case GL_FRAGMENT_SHADER:
500 case GL_VERTEX_SHADER:
501 sh = _mesa_new_shader(ctx, name, type);
502 break;
503 default:
504 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
505 return 0;
506 }
507
508 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
509
510 return name;
511 }
512
513
514 static GLuint
515 _mesa_create_program(GLcontext *ctx)
516 {
517 GLuint name;
518 struct gl_shader_program *shProg;
519
520 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
521 shProg = _mesa_new_shader_program(ctx, name);
522
523 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
524
525 assert(shProg->RefCount == 1);
526
527 return name;
528 }
529
530
531 /**
532 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
533 * DeleteProgramARB.
534 */
535 static void
536 _mesa_delete_program2(GLcontext *ctx, GLuint name)
537 {
538 /*
539 * NOTE: deleting shaders/programs works a bit differently than
540 * texture objects (and buffer objects, etc). Shader/program
541 * handles/IDs exist in the hash table until the object is really
542 * deleted (refcount==0). With texture objects, the handle/ID is
543 * removed from the hash table in glDeleteTextures() while the tex
544 * object itself might linger until its refcount goes to zero.
545 */
546 struct gl_shader_program *shProg;
547
548 shProg = _mesa_lookup_shader_program(ctx, name);
549 if (!shProg) {
550 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgram(name)");
551 return;
552 }
553
554 shProg->DeletePending = GL_TRUE;
555
556 /* effectively, decr shProg's refcount */
557 _mesa_reference_shader_program(ctx, &shProg, NULL);
558 }
559
560
561 static void
562 _mesa_delete_shader(GLcontext *ctx, GLuint shader)
563 {
564 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
565 if (!sh) {
566 return;
567 }
568
569 sh->DeletePending = GL_TRUE;
570
571 /* effectively, decr sh's refcount */
572 _mesa_reference_shader(ctx, &sh, NULL);
573 }
574
575
576 static void
577 _mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
578 {
579 struct gl_shader_program *shProg
580 = _mesa_lookup_shader_program(ctx, program);
581 const GLuint n = shProg->NumShaders;
582 GLuint i, j;
583
584 if (!shProg) {
585 _mesa_error(ctx, GL_INVALID_VALUE,
586 "glDetachShader(bad program or shader name)");
587 return;
588 }
589
590 for (i = 0; i < n; i++) {
591 if (shProg->Shaders[i]->Name == shader) {
592 /* found it */
593 struct gl_shader **newList;
594
595 /* derefernce */
596 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
597
598 /* alloc new, smaller array */
599 newList = (struct gl_shader **)
600 _mesa_malloc((n - 1) * sizeof(struct gl_shader *));
601 if (!newList) {
602 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
603 return;
604 }
605 for (j = 0; j < i; j++) {
606 newList[j] = shProg->Shaders[j];
607 }
608 while (++i < n)
609 newList[j++] = shProg->Shaders[i];
610 _mesa_free(shProg->Shaders);
611
612 shProg->Shaders = newList;
613 shProg->NumShaders = n - 1;
614
615 #ifdef DEBUG
616 /* sanity check */
617 {
618 for (j = 0; j < shProg->NumShaders; j++) {
619 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
620 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
621 assert(shProg->Shaders[j]->RefCount > 0);
622 }
623 }
624 #endif
625
626 return;
627 }
628 }
629
630 /* not found */
631 _mesa_error(ctx, GL_INVALID_VALUE,
632 "glDetachShader(shader not found)");
633 }
634
635
636 static void
637 _mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index,
638 GLsizei maxLength, GLsizei *length, GLint *size,
639 GLenum *type, GLchar *nameOut)
640 {
641 static const GLenum vec_types[] = {
642 GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4
643 };
644 struct gl_shader_program *shProg
645 = _mesa_lookup_shader_program(ctx, program);
646 GLint sz;
647
648 if (!shProg) {
649 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib");
650 return;
651 }
652
653 if (!shProg->Attributes || index >= shProg->Attributes->NumParameters) {
654 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
655 return;
656 }
657
658 copy_string(nameOut, maxLength, length,
659 shProg->Attributes->Parameters[index].Name);
660 sz = shProg->Attributes->Parameters[index].Size;
661 if (size)
662 *size = sz;
663 if (type)
664 *type = vec_types[sz]; /* XXX this is a temporary hack */
665 }
666
667
668 /**
669 * Called via ctx->Driver.GetActiveUniform().
670 */
671 static void
672 _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
673 GLsizei maxLength, GLsizei *length, GLint *size,
674 GLenum *type, GLchar *nameOut)
675 {
676 struct gl_shader_program *shProg
677 = _mesa_lookup_shader_program(ctx, program);
678 GLuint ind, j;
679
680 if (!shProg) {
681 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform");
682 return;
683 }
684
685 if (!shProg->Uniforms || index >= shProg->Uniforms->NumParameters) {
686 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
687 return;
688 }
689
690 ind = 0;
691 for (j = 0; j < shProg->Uniforms->NumParameters; j++) {
692 if (shProg->Uniforms->Parameters[j].Type == PROGRAM_UNIFORM ||
693 shProg->Uniforms->Parameters[j].Type == PROGRAM_SAMPLER) {
694 if (ind == index) {
695 /* found it */
696 copy_string(nameOut, maxLength, length,
697 shProg->Uniforms->Parameters[j].Name);
698 if (size)
699 *size = shProg->Uniforms->Parameters[j].Size;
700 if (type)
701 *type = shProg->Uniforms->Parameters[j].DataType;
702 return;
703 }
704 ind++;
705 }
706 }
707
708 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
709 }
710
711
712 /**
713 * Called via ctx->Driver.GetAttachedShaders().
714 */
715 static void
716 _mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
717 GLsizei *count, GLuint *obj)
718 {
719 struct gl_shader_program *shProg
720 = _mesa_lookup_shader_program(ctx, program);
721 if (shProg) {
722 GLint i;
723 for (i = 0; i < maxCount && i < shProg->NumShaders; i++) {
724 obj[i] = shProg->Shaders[i]->Name;
725 }
726 if (count)
727 *count = i;
728 }
729 else {
730 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders");
731 }
732 }
733
734
735 static GLuint
736 _mesa_get_handle(GLcontext *ctx, GLenum pname)
737 {
738 #if 0
739 GET_CURRENT_CONTEXT(ctx);
740
741 switch (pname) {
742 case GL_PROGRAM_OBJECT_ARB:
743 {
744 struct gl2_program_intf **pro = ctx->Shader.CurrentProgram;
745
746 if (pro != NULL)
747 return (**pro)._container._generic.
748 GetName((struct gl2_generic_intf **) (pro));
749 }
750 break;
751 default:
752 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
753 }
754 #endif
755 return 0;
756 }
757
758
759 static void
760 _mesa_get_programiv(GLcontext *ctx, GLuint program,
761 GLenum pname, GLint *params)
762 {
763 struct gl_shader_program *shProg
764 = _mesa_lookup_shader_program(ctx, program);
765
766 if (!shProg) {
767 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
768 return;
769 }
770
771 switch (pname) {
772 case GL_DELETE_STATUS:
773 *params = shProg->DeletePending;
774 break;
775 case GL_LINK_STATUS:
776 *params = shProg->LinkStatus;
777 break;
778 case GL_VALIDATE_STATUS:
779 *params = shProg->Validated;
780 break;
781 case GL_INFO_LOG_LENGTH:
782 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
783 break;
784 case GL_ATTACHED_SHADERS:
785 *params = shProg->NumShaders;
786 break;
787 case GL_ACTIVE_ATTRIBUTES:
788 *params = shProg->Attributes ? shProg->Attributes->NumParameters : 0;
789 break;
790 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
791 *params = _mesa_longest_parameter_name(shProg->Attributes,
792 PROGRAM_INPUT) + 1;
793 break;
794 case GL_ACTIVE_UNIFORMS:
795 *params
796 = _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_UNIFORM)
797 + _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_SAMPLER);
798 break;
799 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
800 *params = MAX2(
801 _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_UNIFORM),
802 _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_SAMPLER));
803 if (*params > 0)
804 (*params)++; /* add one for terminating zero */
805 break;
806 default:
807 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
808 return;
809 }
810 }
811
812
813 static void
814 _mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
815 {
816 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
817
818 if (!shader) {
819 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderiv(shader)");
820 return;
821 }
822
823 switch (pname) {
824 case GL_SHADER_TYPE:
825 *params = shader->Type;
826 break;
827 case GL_DELETE_STATUS:
828 *params = shader->DeletePending;
829 break;
830 case GL_COMPILE_STATUS:
831 *params = shader->CompileStatus;
832 break;
833 case GL_INFO_LOG_LENGTH:
834 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
835 break;
836 case GL_SHADER_SOURCE_LENGTH:
837 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
838 break;
839 default:
840 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
841 return;
842 }
843 }
844
845
846 static void
847 _mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize,
848 GLsizei *length, GLchar *infoLog)
849 {
850 struct gl_shader_program *shProg
851 = _mesa_lookup_shader_program(ctx, program);
852 if (!shProg) {
853 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
854 return;
855 }
856 copy_string(infoLog, bufSize, length, shProg->InfoLog);
857 }
858
859
860 static void
861 _mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize,
862 GLsizei *length, GLchar *infoLog)
863 {
864 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
865 if (!sh) {
866 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
867 return;
868 }
869 copy_string(infoLog, bufSize, length, sh->InfoLog);
870 }
871
872
873 /**
874 * Called via ctx->Driver.GetShaderSource().
875 */
876 static void
877 _mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength,
878 GLsizei *length, GLchar *sourceOut)
879 {
880 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
881 if (!sh) {
882 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(shader)");
883 return;
884 }
885 copy_string(sourceOut, maxLength, length, sh->Source);
886 }
887
888
889 /**
890 * Called via ctx->Driver.GetUniformfv().
891 */
892 static void
893 _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
894 GLfloat *params)
895 {
896 struct gl_shader_program *shProg
897 = _mesa_lookup_shader_program(ctx, program);
898 if (shProg) {
899 GLint i;
900 if (location >= 0 && location < shProg->Uniforms->NumParameters) {
901 for (i = 0; i < shProg->Uniforms->Parameters[location].Size; i++) {
902 params[i] = shProg->Uniforms->ParameterValues[location][i];
903 }
904 }
905 else {
906 _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(location)");
907 }
908 }
909 else {
910 _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(program)");
911 }
912 }
913
914
915 /**
916 * Called via ctx->Driver.GetUniformLocation().
917 */
918 static GLint
919 _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
920 {
921 struct gl_shader_program *shProg
922 = _mesa_lookup_shader_program(ctx, program);
923 if (shProg) {
924 GLuint loc;
925 for (loc = 0; loc < shProg->Uniforms->NumParameters; loc++) {
926 const struct gl_program_parameter *u
927 = shProg->Uniforms->Parameters + loc;
928 /* XXX this is a temporary simplification / short-cut.
929 * We need to handle things like "e.c[0].b" as seen in the
930 * GLSL orange book, page 189.
931 */
932 if ((u->Type == PROGRAM_UNIFORM ||
933 u->Type == PROGRAM_SAMPLER) && !strcmp(u->Name, name)) {
934 return loc;
935 }
936 }
937 }
938 return -1;
939
940 }
941
942
943 static GLboolean
944 _mesa_is_program(GLcontext *ctx, GLuint name)
945 {
946 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
947 return shProg ? GL_TRUE : GL_FALSE;
948 }
949
950
951 static GLboolean
952 _mesa_is_shader(GLcontext *ctx, GLuint name)
953 {
954 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
955 return shader ? GL_TRUE : GL_FALSE;
956 }
957
958
959
960 /**
961 * Called via ctx->Driver.ShaderSource()
962 */
963 static void
964 _mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source)
965 {
966 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
967 if (!sh) {
968 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSource(shaderObj)");
969 return;
970 }
971
972 /* free old shader source string and install new one */
973 if (sh->Source) {
974 _mesa_free((void *) sh->Source);
975 }
976 sh->Source = source;
977 sh->CompileStatus = GL_FALSE;
978 }
979
980
981 /**
982 * Called via ctx->Driver.CompileShader()
983 */
984 static void
985 _mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
986 {
987 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
988
989 if (!sh) {
990 _mesa_error(ctx, GL_INVALID_VALUE, "glCompileShader(shaderObj)");
991 return;
992 }
993
994 sh->CompileStatus = _slang_compile(ctx, sh);
995 }
996
997
998 /**
999 * Called via ctx->Driver.LinkProgram()
1000 */
1001 static void
1002 _mesa_link_program(GLcontext *ctx, GLuint program)
1003 {
1004 struct gl_shader_program *shProg;
1005
1006 shProg = _mesa_lookup_shader_program(ctx, program);
1007 if (!shProg) {
1008 _mesa_error(ctx, GL_INVALID_VALUE, "glLinkProgram(program)");
1009 return;
1010 }
1011
1012 _slang_link(ctx, program, shProg);
1013 }
1014
1015
1016 /**
1017 * Called via ctx->Driver.UseProgram()
1018 */
1019 void
1020 _mesa_use_program(GLcontext *ctx, GLuint program)
1021 {
1022 struct gl_shader_program *shProg;
1023
1024 if (ctx->Shader.CurrentProgram &&
1025 ctx->Shader.CurrentProgram->Name == program) {
1026 /* no-op */
1027 return;
1028 }
1029
1030 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1031
1032 if (program) {
1033 shProg = _mesa_lookup_shader_program(ctx, program);
1034 if (!shProg) {
1035 _mesa_error(ctx, GL_INVALID_VALUE,
1036 "glUseProgramObjectARB(programObj)");
1037 return;
1038 }
1039 }
1040 else {
1041 shProg = NULL;
1042 }
1043
1044 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);
1045 }
1046
1047
1048
1049 /**
1050 * Update the vertex and fragment program's TexturesUsed arrays.
1051 */
1052 static void
1053 update_textures_used(struct gl_program *prog)
1054 {
1055 GLuint s;
1056
1057 memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed));
1058
1059 for (s = 0; s < MAX_SAMPLERS; s++) {
1060 if (prog->SamplersUsed & (1 << s)) {
1061 GLuint u = prog->SamplerUnits[s];
1062 GLuint t = prog->SamplerTargets[s];
1063 assert(u < MAX_TEXTURE_IMAGE_UNITS);
1064 prog->TexturesUsed[u] |= (1 << t);
1065 }
1066 }
1067 }
1068
1069
1070 /**
1071 * Called via ctx->Driver.Uniform().
1072 */
1073 static void
1074 _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
1075 const GLvoid *values, GLenum type)
1076 {
1077 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1078 GLint elems, i, k;
1079
1080 if (!shProg || !shProg->LinkStatus) {
1081 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
1082 return;
1083 }
1084
1085 if (location < 0 || location >= (GLint) shProg->Uniforms->NumParameters) {
1086 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
1087 return;
1088 }
1089
1090 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1091
1092 if (count < 0) {
1093 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
1094 return;
1095 }
1096
1097 switch (type) {
1098 case GL_FLOAT:
1099 case GL_INT:
1100 elems = 1;
1101 break;
1102 case GL_FLOAT_VEC2:
1103 case GL_INT_VEC2:
1104 elems = 2;
1105 break;
1106 case GL_FLOAT_VEC3:
1107 case GL_INT_VEC3:
1108 elems = 3;
1109 break;
1110 case GL_FLOAT_VEC4:
1111 case GL_INT_VEC4:
1112 elems = 4;
1113 break;
1114 default:
1115 _mesa_problem(ctx, "Invalid type in _mesa_uniform");
1116 return;
1117 }
1118
1119 if (count * elems > shProg->Uniforms->Parameters[location].Size) {
1120 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
1121 return;
1122 }
1123
1124 if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
1125 /* This controls which texture unit which is used by a sampler */
1126 GLuint texUnit, sampler;
1127
1128 /* data type for setting samplers must be int */
1129 if (type != GL_INT || count != 1) {
1130 _mesa_error(ctx, GL_INVALID_OPERATION,
1131 "glUniform(only glUniform1i can be used "
1132 "to set sampler uniforms)");
1133 return;
1134 }
1135
1136 sampler = (GLuint) shProg->Uniforms->ParameterValues[location][0];
1137 texUnit = ((GLuint *) values)[0];
1138
1139 /* check that the sampler (tex unit index) is legal */
1140 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
1141 _mesa_error(ctx, GL_INVALID_VALUE,
1142 "glUniform1(invalid sampler/tex unit index)");
1143 return;
1144 }
1145
1146 if (shProg->VertexProgram) {
1147 shProg->VertexProgram->Base.SamplerUnits[sampler] = texUnit;
1148 update_textures_used(&shProg->VertexProgram->Base);
1149 }
1150 if (shProg->FragmentProgram) {
1151 shProg->FragmentProgram->Base.SamplerUnits[sampler] = texUnit;
1152 update_textures_used(&shProg->FragmentProgram->Base);
1153 }
1154
1155
1156 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1157 }
1158 else {
1159 /* ordinary uniform variable */
1160 for (k = 0; k < count; k++) {
1161 GLfloat *uniformVal = shProg->Uniforms->ParameterValues[location + k];
1162 if (type == GL_INT ||
1163 type == GL_INT_VEC2 ||
1164 type == GL_INT_VEC3 ||
1165 type == GL_INT_VEC4) {
1166 const GLint *iValues = ((const GLint *) values) + k * elems;
1167 for (i = 0; i < elems; i++) {
1168 uniformVal[i] = (GLfloat) iValues[i];
1169 }
1170 }
1171 else {
1172 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
1173 for (i = 0; i < elems; i++) {
1174 uniformVal[i] = fValues[i];
1175 }
1176 }
1177 }
1178 }
1179 }
1180
1181
1182 /**
1183 * Called by ctx->Driver.UniformMatrix().
1184 */
1185 static void
1186 _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
1187 GLenum matrixType, GLint location, GLsizei count,
1188 GLboolean transpose, const GLfloat *values)
1189 {
1190 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1191 if (!shProg || !shProg->LinkStatus) {
1192 _mesa_error(ctx, GL_INVALID_OPERATION,
1193 "glUniformMatrix(program not linked)");
1194 return;
1195 }
1196 if (location < 0 || location >= shProg->Uniforms->NumParameters) {
1197 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
1198 return;
1199 }
1200 if (values == NULL) {
1201 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
1202 return;
1203 }
1204
1205 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1206
1207 /*
1208 * Note: the _columns_ of a matrix are stored in program registers, not
1209 * the rows.
1210 */
1211 /* XXXX need to test 3x3 and 2x2 matrices... */
1212 if (transpose) {
1213 GLuint row, col;
1214 for (col = 0; col < cols; col++) {
1215 GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
1216 for (row = 0; row < rows; row++) {
1217 v[row] = values[row * cols + col];
1218 }
1219 }
1220 }
1221 else {
1222 GLuint row, col;
1223 for (col = 0; col < cols; col++) {
1224 GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
1225 for (row = 0; row < rows; row++) {
1226 v[row] = values[col * rows + row];
1227 }
1228 }
1229 }
1230 }
1231
1232
1233 static void
1234 _mesa_validate_program(GLcontext *ctx, GLuint program)
1235 {
1236 struct gl_shader_program *shProg;
1237 shProg = _mesa_lookup_shader_program(ctx, program);
1238 if (!shProg) {
1239 _mesa_error(ctx, GL_INVALID_VALUE, "glValidateProgram(program)");
1240 return;
1241 }
1242 /* XXX temporary */
1243 shProg->Validated = GL_TRUE;
1244
1245 /* From the GL spec:
1246 any two active samplers in the current program object are of
1247 different types, but refer to the same texture image unit,
1248
1249 any active sampler in the current program object refers to a texture
1250 image unit where fixed-function fragment processing accesses a
1251 texture target that does not match the sampler type, or
1252
1253 the sum of the number of active samplers in the program and the
1254 number of texture image units enabled for fixed-function fragment
1255 processing exceeds the combined limit on the total number of texture
1256 image units allowed.
1257 */
1258 }
1259
1260
1261 /**
1262 * Plug in Mesa's GLSL functions into the device driver function table.
1263 */
1264 void
1265 _mesa_init_glsl_driver_functions(struct dd_function_table *driver)
1266 {
1267 driver->AttachShader = _mesa_attach_shader;
1268 driver->BindAttribLocation = _mesa_bind_attrib_location;
1269 driver->CompileShader = _mesa_compile_shader;
1270 driver->CreateProgram = _mesa_create_program;
1271 driver->CreateShader = _mesa_create_shader;
1272 driver->DeleteProgram2 = _mesa_delete_program2;
1273 driver->DeleteShader = _mesa_delete_shader;
1274 driver->DetachShader = _mesa_detach_shader;
1275 driver->GetActiveAttrib = _mesa_get_active_attrib;
1276 driver->GetActiveUniform = _mesa_get_active_uniform;
1277 driver->GetAttachedShaders = _mesa_get_attached_shaders;
1278 driver->GetAttribLocation = _mesa_get_attrib_location;
1279 driver->GetHandle = _mesa_get_handle;
1280 driver->GetProgramiv = _mesa_get_programiv;
1281 driver->GetProgramInfoLog = _mesa_get_program_info_log;
1282 driver->GetShaderiv = _mesa_get_shaderiv;
1283 driver->GetShaderInfoLog = _mesa_get_shader_info_log;
1284 driver->GetShaderSource = _mesa_get_shader_source;
1285 driver->GetUniformfv = _mesa_get_uniformfv;
1286 driver->GetUniformLocation = _mesa_get_uniform_location;
1287 driver->IsProgram = _mesa_is_program;
1288 driver->IsShader = _mesa_is_shader;
1289 driver->LinkProgram = _mesa_link_program;
1290 driver->ShaderSource = _mesa_shader_source;
1291 driver->Uniform = _mesa_uniform;
1292 driver->UniformMatrix = _mesa_uniform_matrix;
1293 driver->UseProgram = _mesa_use_program;
1294 driver->ValidateProgram = _mesa_validate_program;
1295 }