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