mesa: Determine GL_ACTIVE_ATTRIBUTE_MAX_LENGTH by walking the GLSL IR.
[mesa.git] / src / mesa / main / shaderapi.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file shaderapi.c
27 * \author Brian Paul
28 *
29 * Implementation of GLSL-related API functions.
30 * The glUniform* functions are in uniforms.c
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/dispatch.h"
42 #include "main/enums.h"
43 #include "main/hash.h"
44 #include "main/mfeatures.h"
45 #include "main/mtypes.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "program/program.h"
49 #include "program/prog_parameter.h"
50 #include "program/prog_uniform.h"
51 #include "ralloc.h"
52 #include <stdbool.h>
53 #include "../glsl/glsl_parser_extras.h"
54
55 /** Define this to enable shader substitution (see below) */
56 #define SHADER_SUBST 0
57
58
59 /**
60 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
61 */
62 static GLbitfield
63 get_shader_flags(void)
64 {
65 GLbitfield flags = 0x0;
66 const char *env = _mesa_getenv("MESA_GLSL");
67
68 if (env) {
69 if (strstr(env, "dump"))
70 flags |= GLSL_DUMP;
71 if (strstr(env, "log"))
72 flags |= GLSL_LOG;
73 if (strstr(env, "nopvert"))
74 flags |= GLSL_NOP_VERT;
75 if (strstr(env, "nopfrag"))
76 flags |= GLSL_NOP_FRAG;
77 if (strstr(env, "nopt"))
78 flags |= GLSL_NO_OPT;
79 else if (strstr(env, "opt"))
80 flags |= GLSL_OPT;
81 if (strstr(env, "uniform"))
82 flags |= GLSL_UNIFORMS;
83 if (strstr(env, "useprog"))
84 flags |= GLSL_USE_PROG;
85 }
86
87 return flags;
88 }
89
90
91 /**
92 * Initialize context's shader state.
93 */
94 void
95 _mesa_init_shader_state(struct gl_context *ctx)
96 {
97 /* Device drivers may override these to control what kind of instructions
98 * are generated by the GLSL compiler.
99 */
100 struct gl_shader_compiler_options options;
101 gl_shader_type sh;
102
103 memset(&options, 0, sizeof(options));
104 options.MaxUnrollIterations = 32;
105
106 /* Default pragma settings */
107 options.DefaultPragmas.Optimize = GL_TRUE;
108
109 for (sh = 0; sh < MESA_SHADER_TYPES; ++sh)
110 memcpy(&ctx->ShaderCompilerOptions[sh], &options, sizeof(options));
111
112 ctx->Shader.Flags = get_shader_flags();
113 }
114
115
116 /**
117 * Free the per-context shader-related state.
118 */
119 void
120 _mesa_free_shader_state(struct gl_context *ctx)
121 {
122 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentVertexProgram, NULL);
123 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentGeometryProgram,
124 NULL);
125 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentFragmentProgram,
126 NULL);
127 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, NULL);
128 }
129
130
131 /**
132 * Return the size of the given GLSL datatype, in floats (components).
133 */
134 GLint
135 _mesa_sizeof_glsl_type(GLenum type)
136 {
137 switch (type) {
138 case GL_FLOAT:
139 case GL_INT:
140 case GL_UNSIGNED_INT:
141 case GL_BOOL:
142 case GL_SAMPLER_1D:
143 case GL_SAMPLER_2D:
144 case GL_SAMPLER_3D:
145 case GL_SAMPLER_CUBE:
146 case GL_SAMPLER_1D_SHADOW:
147 case GL_SAMPLER_2D_SHADOW:
148 case GL_SAMPLER_2D_RECT_ARB:
149 case GL_SAMPLER_2D_RECT_SHADOW_ARB:
150 case GL_SAMPLER_1D_ARRAY_EXT:
151 case GL_SAMPLER_2D_ARRAY_EXT:
152 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
153 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
154 case GL_SAMPLER_CUBE_SHADOW_EXT:
155 return 1;
156 case GL_FLOAT_VEC2:
157 case GL_INT_VEC2:
158 case GL_UNSIGNED_INT_VEC2:
159 case GL_BOOL_VEC2:
160 return 2;
161 case GL_FLOAT_VEC3:
162 case GL_INT_VEC3:
163 case GL_UNSIGNED_INT_VEC3:
164 case GL_BOOL_VEC3:
165 return 3;
166 case GL_FLOAT_VEC4:
167 case GL_INT_VEC4:
168 case GL_UNSIGNED_INT_VEC4:
169 case GL_BOOL_VEC4:
170 return 4;
171 case GL_FLOAT_MAT2:
172 case GL_FLOAT_MAT2x3:
173 case GL_FLOAT_MAT2x4:
174 return 8; /* two float[4] vectors */
175 case GL_FLOAT_MAT3:
176 case GL_FLOAT_MAT3x2:
177 case GL_FLOAT_MAT3x4:
178 return 12; /* three float[4] vectors */
179 case GL_FLOAT_MAT4:
180 case GL_FLOAT_MAT4x2:
181 case GL_FLOAT_MAT4x3:
182 return 16; /* four float[4] vectors */
183 default:
184 _mesa_problem(NULL, "Invalid type in _mesa_sizeof_glsl_type()");
185 return 1;
186 }
187 }
188
189
190 /**
191 * Copy string from <src> to <dst>, up to maxLength characters, returning
192 * length of <dst> in <length>.
193 * \param src the strings source
194 * \param maxLength max chars to copy
195 * \param length returns number of chars copied
196 * \param dst the string destination
197 */
198 void
199 _mesa_copy_string(GLchar *dst, GLsizei maxLength,
200 GLsizei *length, const GLchar *src)
201 {
202 GLsizei len;
203 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
204 dst[len] = src[len];
205 if (maxLength > 0)
206 dst[len] = 0;
207 if (length)
208 *length = len;
209 }
210
211
212
213 /**
214 * Confirm that the a shader type is valid and supported by the implementation
215 *
216 * \param ctx Current GL context
217 * \param type Shader target
218 *
219 */
220 static bool
221 validate_shader_target(const struct gl_context *ctx, GLenum type)
222 {
223 switch (type) {
224 #if FEATURE_ARB_fragment_shader
225 case GL_FRAGMENT_SHADER:
226 return ctx->Extensions.ARB_fragment_shader;
227 #endif
228 #if FEATURE_ARB_vertex_shader
229 case GL_VERTEX_SHADER:
230 return ctx->Extensions.ARB_vertex_shader;
231 #endif
232 #if FEATURE_ARB_geometry_shader4
233 case GL_GEOMETRY_SHADER_ARB:
234 return ctx->Extensions.ARB_geometry_shader4;
235 #endif
236 default:
237 return false;
238 }
239 }
240
241
242 /**
243 * Find the length of the longest transform feedback varying name
244 * which was specified with glTransformFeedbackVaryings().
245 */
246 static GLint
247 longest_feedback_varying_name(const struct gl_shader_program *shProg)
248 {
249 GLuint i;
250 GLint max = 0;
251 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
252 GLint len = strlen(shProg->TransformFeedback.VaryingNames[i]);
253 if (len > max)
254 max = len;
255 }
256 return max;
257 }
258
259
260
261 static GLboolean
262 is_program(struct gl_context *ctx, GLuint name)
263 {
264 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
265 return shProg ? GL_TRUE : GL_FALSE;
266 }
267
268
269 static GLboolean
270 is_shader(struct gl_context *ctx, GLuint name)
271 {
272 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
273 return shader ? GL_TRUE : GL_FALSE;
274 }
275
276
277 /**
278 * Attach shader to a shader program.
279 */
280 static void
281 attach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
282 {
283 struct gl_shader_program *shProg;
284 struct gl_shader *sh;
285 GLuint i, n;
286
287 shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
288 if (!shProg)
289 return;
290
291 sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
292 if (!sh) {
293 return;
294 }
295
296 n = shProg->NumShaders;
297 for (i = 0; i < n; i++) {
298 if (shProg->Shaders[i] == sh) {
299 /* The shader is already attched to this program. The
300 * GL_ARB_shader_objects spec says:
301 *
302 * "The error INVALID_OPERATION is generated by AttachObjectARB
303 * if <obj> is already attached to <containerObj>."
304 */
305 _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
306 return;
307 }
308 }
309
310 /* grow list */
311 shProg->Shaders = (struct gl_shader **)
312 _mesa_realloc(shProg->Shaders,
313 n * sizeof(struct gl_shader *),
314 (n + 1) * sizeof(struct gl_shader *));
315 if (!shProg->Shaders) {
316 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
317 return;
318 }
319
320 /* append */
321 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
322 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
323 shProg->NumShaders++;
324 }
325
326
327 static void
328 bind_frag_data_location(struct gl_context *ctx, GLuint program,
329 GLuint colorNumber, const GLchar *name)
330 {
331 _mesa_problem(ctx, "bind_frag_data_location() not implemented yet");
332 }
333
334
335 static GLuint
336 create_shader(struct gl_context *ctx, GLenum type)
337 {
338 struct gl_shader *sh;
339 GLuint name;
340
341 if (!validate_shader_target(ctx, type)) {
342 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
343 return 0;
344 }
345
346 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
347 sh = ctx->Driver.NewShader(ctx, name, type);
348 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
349
350 return name;
351 }
352
353
354 static GLuint
355 create_shader_program(struct gl_context *ctx)
356 {
357 GLuint name;
358 struct gl_shader_program *shProg;
359
360 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
361
362 shProg = ctx->Driver.NewShaderProgram(ctx, name);
363
364 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
365
366 assert(shProg->RefCount == 1);
367
368 return name;
369 }
370
371
372 /**
373 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
374 * DeleteProgramARB.
375 */
376 static void
377 delete_shader_program(struct gl_context *ctx, GLuint name)
378 {
379 /*
380 * NOTE: deleting shaders/programs works a bit differently than
381 * texture objects (and buffer objects, etc). Shader/program
382 * handles/IDs exist in the hash table until the object is really
383 * deleted (refcount==0). With texture objects, the handle/ID is
384 * removed from the hash table in glDeleteTextures() while the tex
385 * object itself might linger until its refcount goes to zero.
386 */
387 struct gl_shader_program *shProg;
388
389 shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
390 if (!shProg)
391 return;
392
393 shProg->DeletePending = GL_TRUE;
394
395 /* effectively, decr shProg's refcount */
396 _mesa_reference_shader_program(ctx, &shProg, NULL);
397 }
398
399
400 static void
401 delete_shader(struct gl_context *ctx, GLuint shader)
402 {
403 struct gl_shader *sh;
404
405 sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
406 if (!sh)
407 return;
408
409 sh->DeletePending = GL_TRUE;
410
411 /* effectively, decr sh's refcount */
412 _mesa_reference_shader(ctx, &sh, NULL);
413 }
414
415
416 static void
417 detach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
418 {
419 struct gl_shader_program *shProg;
420 GLuint n;
421 GLuint i, j;
422
423 shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
424 if (!shProg)
425 return;
426
427 n = shProg->NumShaders;
428
429 for (i = 0; i < n; i++) {
430 if (shProg->Shaders[i]->Name == shader) {
431 /* found it */
432 struct gl_shader **newList;
433
434 /* release */
435 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
436
437 /* alloc new, smaller array */
438 newList = (struct gl_shader **)
439 malloc((n - 1) * sizeof(struct gl_shader *));
440 if (!newList) {
441 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
442 return;
443 }
444 for (j = 0; j < i; j++) {
445 newList[j] = shProg->Shaders[j];
446 }
447 while (++i < n)
448 newList[j++] = shProg->Shaders[i];
449 free(shProg->Shaders);
450
451 shProg->Shaders = newList;
452 shProg->NumShaders = n - 1;
453
454 #ifdef DEBUG
455 /* sanity check */
456 {
457 for (j = 0; j < shProg->NumShaders; j++) {
458 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
459 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
460 assert(shProg->Shaders[j]->RefCount > 0);
461 }
462 }
463 #endif
464
465 return;
466 }
467 }
468
469 /* not found */
470 {
471 GLenum err;
472 if (is_shader(ctx, shader))
473 err = GL_INVALID_OPERATION;
474 else if (is_program(ctx, shader))
475 err = GL_INVALID_OPERATION;
476 else
477 err = GL_INVALID_VALUE;
478 _mesa_error(ctx, err, "glDetachProgram(shader)");
479 return;
480 }
481 }
482
483
484 /**
485 * Return list of shaders attached to shader program.
486 */
487 static void
488 get_attached_shaders(struct gl_context *ctx, GLuint program, GLsizei maxCount,
489 GLsizei *count, GLuint *obj)
490 {
491 struct gl_shader_program *shProg =
492 _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
493 if (shProg) {
494 GLuint i;
495 for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
496 obj[i] = shProg->Shaders[i]->Name;
497 }
498 if (count)
499 *count = i;
500 }
501 }
502
503
504 static GLint
505 get_frag_data_location(struct gl_context *ctx, GLuint program,
506 const GLchar *name)
507 {
508 _mesa_problem(ctx, "get_frag_data_location() not implemented yet");
509 return -1;
510 }
511
512
513
514 /**
515 * glGetHandleARB() - return ID/name of currently bound shader program.
516 */
517 static GLuint
518 get_handle(struct gl_context *ctx, GLenum pname)
519 {
520 if (pname == GL_PROGRAM_OBJECT_ARB) {
521 if (ctx->Shader.ActiveProgram)
522 return ctx->Shader.ActiveProgram->Name;
523 else
524 return 0;
525 }
526 else {
527 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
528 return 0;
529 }
530 }
531
532
533 /**
534 * glGetProgramiv() - get shader program state.
535 * Note that this is for GLSL shader programs, not ARB vertex/fragment
536 * programs (see glGetProgramivARB).
537 */
538 static void
539 get_programiv(struct gl_context *ctx, GLuint program, GLenum pname, GLint *params)
540 {
541 struct gl_shader_program *shProg
542 = _mesa_lookup_shader_program(ctx, program);
543
544 if (!shProg) {
545 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
546 return;
547 }
548
549 switch (pname) {
550 case GL_DELETE_STATUS:
551 *params = shProg->DeletePending;
552 break;
553 case GL_LINK_STATUS:
554 *params = shProg->LinkStatus;
555 break;
556 case GL_VALIDATE_STATUS:
557 *params = shProg->Validated;
558 break;
559 case GL_INFO_LOG_LENGTH:
560 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
561 break;
562 case GL_ATTACHED_SHADERS:
563 *params = shProg->NumShaders;
564 break;
565 case GL_ACTIVE_ATTRIBUTES:
566 *params = _mesa_count_active_attribs(shProg);
567 break;
568 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
569 *params = _mesa_longest_attribute_name_length(shProg);
570 break;
571 case GL_ACTIVE_UNIFORMS:
572 *params = shProg->Uniforms ? shProg->Uniforms->NumUniforms : 0;
573 break;
574 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
575 *params = _mesa_longest_uniform_name(shProg->Uniforms);
576 if (*params > 0)
577 (*params)++; /* add one for terminating zero */
578 break;
579 case GL_PROGRAM_BINARY_LENGTH_OES:
580 *params = 0;
581 break;
582 #if FEATURE_EXT_transform_feedback
583 case GL_TRANSFORM_FEEDBACK_VARYINGS:
584 *params = shProg->TransformFeedback.NumVarying;
585 break;
586 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
587 *params = longest_feedback_varying_name(shProg) + 1;
588 break;
589 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
590 *params = shProg->TransformFeedback.BufferMode;
591 break;
592 #endif
593 #if FEATURE_ARB_geometry_shader4
594 case GL_GEOMETRY_VERTICES_OUT_ARB:
595 *params = shProg->Geom.VerticesOut;
596 break;
597 case GL_GEOMETRY_INPUT_TYPE_ARB:
598 *params = shProg->Geom.InputType;
599 break;
600 case GL_GEOMETRY_OUTPUT_TYPE_ARB:
601 *params = shProg->Geom.OutputType;
602 break;
603 #endif
604 default:
605 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
606 return;
607 }
608 }
609
610
611 /**
612 * glGetShaderiv() - get GLSL shader state
613 */
614 static void
615 get_shaderiv(struct gl_context *ctx, GLuint name, GLenum pname, GLint *params)
616 {
617 struct gl_shader *shader =
618 _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
619
620 if (!shader) {
621 return;
622 }
623
624 switch (pname) {
625 case GL_SHADER_TYPE:
626 *params = shader->Type;
627 break;
628 case GL_DELETE_STATUS:
629 *params = shader->DeletePending;
630 break;
631 case GL_COMPILE_STATUS:
632 *params = shader->CompileStatus;
633 break;
634 case GL_INFO_LOG_LENGTH:
635 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
636 break;
637 case GL_SHADER_SOURCE_LENGTH:
638 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
639 break;
640 default:
641 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
642 return;
643 }
644 }
645
646
647 static void
648 get_program_info_log(struct gl_context *ctx, GLuint program, GLsizei bufSize,
649 GLsizei *length, GLchar *infoLog)
650 {
651 struct gl_shader_program *shProg
652 = _mesa_lookup_shader_program(ctx, program);
653 if (!shProg) {
654 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
655 return;
656 }
657 _mesa_copy_string(infoLog, bufSize, length, shProg->InfoLog);
658 }
659
660
661 static void
662 get_shader_info_log(struct gl_context *ctx, GLuint shader, GLsizei bufSize,
663 GLsizei *length, GLchar *infoLog)
664 {
665 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
666 if (!sh) {
667 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
668 return;
669 }
670 _mesa_copy_string(infoLog, bufSize, length, sh->InfoLog);
671 }
672
673
674 /**
675 * Return shader source code.
676 */
677 static void
678 get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
679 GLsizei *length, GLchar *sourceOut)
680 {
681 struct gl_shader *sh;
682 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
683 if (!sh) {
684 return;
685 }
686 _mesa_copy_string(sourceOut, maxLength, length, sh->Source);
687 }
688
689
690 /**
691 * Set/replace shader source code.
692 */
693 static void
694 shader_source(struct gl_context *ctx, GLuint shader, const GLchar *source)
695 {
696 struct gl_shader *sh;
697
698 sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
699 if (!sh)
700 return;
701
702 /* free old shader source string and install new one */
703 if (sh->Source) {
704 free((void *) sh->Source);
705 }
706 sh->Source = source;
707 sh->CompileStatus = GL_FALSE;
708 #ifdef DEBUG
709 sh->SourceChecksum = _mesa_str_checksum(sh->Source);
710 #endif
711 }
712
713
714 /**
715 * Compile a shader.
716 */
717 static void
718 compile_shader(struct gl_context *ctx, GLuint shaderObj)
719 {
720 struct gl_shader *sh;
721 struct gl_shader_compiler_options *options;
722
723 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
724 if (!sh)
725 return;
726
727 options = &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(sh->Type)];
728
729 /* set default pragma state for shader */
730 sh->Pragmas = options->DefaultPragmas;
731
732 /* this call will set the sh->CompileStatus field to indicate if
733 * compilation was successful.
734 */
735 _mesa_glsl_compile_shader(ctx, sh);
736 }
737
738
739 /**
740 * Link a program's shaders.
741 */
742 static void
743 link_program(struct gl_context *ctx, GLuint program)
744 {
745 struct gl_shader_program *shProg;
746 struct gl_transform_feedback_object *obj =
747 ctx->TransformFeedback.CurrentObject;
748
749 shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
750 if (!shProg)
751 return;
752
753 if (obj->Active
754 && (shProg == ctx->Shader.CurrentVertexProgram
755 || shProg == ctx->Shader.CurrentGeometryProgram
756 || shProg == ctx->Shader.CurrentFragmentProgram)) {
757 _mesa_error(ctx, GL_INVALID_OPERATION,
758 "glLinkProgram(transform feedback active");
759 return;
760 }
761
762 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
763
764 _mesa_glsl_link_shader(ctx, shProg);
765
766 /* debug code */
767 if (0) {
768 GLuint i;
769
770 printf("Link %u shaders in program %u: %s\n",
771 shProg->NumShaders, shProg->Name,
772 shProg->LinkStatus ? "Success" : "Failed");
773
774 for (i = 0; i < shProg->NumShaders; i++) {
775 printf(" shader %u, type 0x%x\n",
776 shProg->Shaders[i]->Name,
777 shProg->Shaders[i]->Type);
778 }
779 }
780 }
781
782
783 /**
784 * Print basic shader info (for debug).
785 */
786 static void
787 print_shader_info(const struct gl_shader_program *shProg)
788 {
789 GLuint i;
790
791 printf("Mesa: glUseProgram(%u)\n", shProg->Name);
792 for (i = 0; i < shProg->NumShaders; i++) {
793 const char *s;
794 switch (shProg->Shaders[i]->Type) {
795 case GL_VERTEX_SHADER:
796 s = "vertex";
797 break;
798 case GL_FRAGMENT_SHADER:
799 s = "fragment";
800 break;
801 case GL_GEOMETRY_SHADER:
802 s = "geometry";
803 break;
804 default:
805 s = "";
806 }
807 printf(" %s shader %u, checksum %u\n", s,
808 shProg->Shaders[i]->Name,
809 shProg->Shaders[i]->SourceChecksum);
810 }
811 if (shProg->VertexProgram)
812 printf(" vert prog %u\n", shProg->VertexProgram->Base.Id);
813 if (shProg->FragmentProgram)
814 printf(" frag prog %u\n", shProg->FragmentProgram->Base.Id);
815 if (shProg->GeometryProgram)
816 printf(" geom prog %u\n", shProg->GeometryProgram->Base.Id);
817 }
818
819
820 /**
821 * Use the named shader program for subsequent glUniform calls
822 */
823 void
824 _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
825 const char *caller)
826 {
827 if ((shProg != NULL) && !shProg->LinkStatus) {
828 _mesa_error(ctx, GL_INVALID_OPERATION,
829 "%s(program %u not linked)", caller, shProg->Name);
830 return;
831 }
832
833 if (ctx->Shader.ActiveProgram != shProg) {
834 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
835 }
836 }
837
838 /**
839 */
840 static bool
841 use_shader_program(struct gl_context *ctx, GLenum type,
842 struct gl_shader_program *shProg)
843 {
844 struct gl_shader_program **target;
845
846 switch (type) {
847 #if FEATURE_ARB_vertex_shader
848 case GL_VERTEX_SHADER:
849 target = &ctx->Shader.CurrentVertexProgram;
850 if ((shProg == NULL)
851 || (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)) {
852 shProg = NULL;
853 }
854 break;
855 #endif
856 #if FEATURE_ARB_geometry_shader4
857 case GL_GEOMETRY_SHADER_ARB:
858 target = &ctx->Shader.CurrentGeometryProgram;
859 if ((shProg == NULL)
860 || (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY] == NULL)) {
861 shProg = NULL;
862 }
863 break;
864 #endif
865 #if FEATURE_ARB_fragment_shader
866 case GL_FRAGMENT_SHADER:
867 target = &ctx->Shader.CurrentFragmentProgram;
868 if ((shProg == NULL)
869 || (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)) {
870 shProg = NULL;
871 }
872 break;
873 #endif
874 default:
875 return false;
876 }
877
878 if (*target != shProg) {
879 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
880 _mesa_reference_shader_program(ctx, target, shProg);
881 return true;
882 }
883
884 return false;
885 }
886
887 /**
888 * Use the named shader program for subsequent rendering.
889 */
890 void
891 _mesa_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
892 {
893 use_shader_program(ctx, GL_VERTEX_SHADER, shProg);
894 use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB, shProg);
895 use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg);
896 _mesa_active_program(ctx, shProg, "glUseProgram");
897
898 if (ctx->Driver.UseProgram)
899 ctx->Driver.UseProgram(ctx, shProg);
900 }
901
902
903 /**
904 * Validate a program's samplers.
905 * Specifically, check that there aren't two samplers of different types
906 * pointing to the same texture unit.
907 * \return GL_TRUE if valid, GL_FALSE if invalid
908 */
909 static GLboolean
910 validate_samplers(const struct gl_program *prog, char *errMsg)
911 {
912 static const char *targetName[] = {
913 "TEXTURE_BUFFER",
914 "TEXTURE_2D_ARRAY",
915 "TEXTURE_1D_ARRAY",
916 "TEXTURE_CUBE",
917 "TEXTURE_3D",
918 "TEXTURE_RECT",
919 "TEXTURE_2D",
920 "TEXTURE_1D",
921 };
922 GLint targetUsed[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
923 GLbitfield samplersUsed = prog->SamplersUsed;
924 GLuint i;
925
926 assert(Elements(targetName) == NUM_TEXTURE_TARGETS);
927
928 if (samplersUsed == 0x0)
929 return GL_TRUE;
930
931 for (i = 0; i < Elements(targetUsed); i++)
932 targetUsed[i] = -1;
933
934 /* walk over bits which are set in 'samplers' */
935 while (samplersUsed) {
936 GLuint unit;
937 gl_texture_index target;
938 GLint sampler = _mesa_ffs(samplersUsed) - 1;
939 assert(sampler >= 0);
940 assert(sampler < Elements(prog->SamplerUnits));
941 unit = prog->SamplerUnits[sampler];
942 target = prog->SamplerTargets[sampler];
943 if (targetUsed[unit] != -1 && targetUsed[unit] != (int) target) {
944 _mesa_snprintf(errMsg, 100,
945 "Texture unit %d is accessed both as %s and %s",
946 unit, targetName[targetUsed[unit]], targetName[target]);
947 return GL_FALSE;
948 }
949 targetUsed[unit] = target;
950 samplersUsed ^= (1 << sampler);
951 }
952
953 return GL_TRUE;
954 }
955
956
957 /**
958 * Do validation of the given shader program.
959 * \param errMsg returns error message if validation fails.
960 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
961 */
962 static GLboolean
963 validate_shader_program(const struct gl_shader_program *shProg,
964 char *errMsg)
965 {
966 const struct gl_vertex_program *vp = shProg->VertexProgram;
967 const struct gl_geometry_program *gp = shProg->GeometryProgram;
968 const struct gl_fragment_program *fp = shProg->FragmentProgram;
969
970 if (!shProg->LinkStatus) {
971 return GL_FALSE;
972 }
973
974 /* From the GL spec, a program is invalid if any of these are true:
975
976 any two active samplers in the current program object are of
977 different types, but refer to the same texture image unit,
978
979 any active sampler in the current program object refers to a texture
980 image unit where fixed-function fragment processing accesses a
981 texture target that does not match the sampler type, or
982
983 the sum of the number of active samplers in the program and the
984 number of texture image units enabled for fixed-function fragment
985 processing exceeds the combined limit on the total number of texture
986 image units allowed.
987 */
988
989
990 /*
991 * Check: any two active samplers in the current program object are of
992 * different types, but refer to the same texture image unit,
993 */
994 if (vp && !validate_samplers(&vp->Base, errMsg)) {
995 return GL_FALSE;
996 }
997 if (gp && !validate_samplers(&gp->Base, errMsg)) {
998 return GL_FALSE;
999 }
1000 if (fp && !validate_samplers(&fp->Base, errMsg)) {
1001 return GL_FALSE;
1002 }
1003
1004 return GL_TRUE;
1005 }
1006
1007
1008 /**
1009 * Called via glValidateProgram()
1010 */
1011 static void
1012 validate_program(struct gl_context *ctx, GLuint program)
1013 {
1014 struct gl_shader_program *shProg;
1015 char errMsg[100] = "";
1016
1017 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1018 if (!shProg) {
1019 return;
1020 }
1021
1022 shProg->Validated = validate_shader_program(shProg, errMsg);
1023 if (!shProg->Validated) {
1024 /* update info log */
1025 if (shProg->InfoLog) {
1026 ralloc_free(shProg->InfoLog);
1027 }
1028 shProg->InfoLog = ralloc_strdup(shProg, errMsg);
1029 }
1030 }
1031
1032
1033
1034 void GLAPIENTRY
1035 _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
1036 {
1037 GET_CURRENT_CONTEXT(ctx);
1038 attach_shader(ctx, program, shader);
1039 }
1040
1041
1042 void GLAPIENTRY
1043 _mesa_AttachShader(GLuint program, GLuint shader)
1044 {
1045 GET_CURRENT_CONTEXT(ctx);
1046 attach_shader(ctx, program, shader);
1047 }
1048
1049
1050 /* GL_EXT_gpu_shader4, GL3 */
1051 void GLAPIENTRY
1052 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
1053 const GLchar *name)
1054 {
1055 GET_CURRENT_CONTEXT(ctx);
1056 bind_frag_data_location(ctx, program, colorNumber, name);
1057 }
1058
1059
1060 void GLAPIENTRY
1061 _mesa_CompileShaderARB(GLhandleARB shaderObj)
1062 {
1063 GET_CURRENT_CONTEXT(ctx);
1064 if (MESA_VERBOSE & VERBOSE_API)
1065 _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
1066 compile_shader(ctx, shaderObj);
1067 }
1068
1069
1070 GLuint GLAPIENTRY
1071 _mesa_CreateShader(GLenum type)
1072 {
1073 GET_CURRENT_CONTEXT(ctx);
1074 if (MESA_VERBOSE & VERBOSE_API)
1075 _mesa_debug(ctx, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type));
1076 return create_shader(ctx, type);
1077 }
1078
1079
1080 GLhandleARB GLAPIENTRY
1081 _mesa_CreateShaderObjectARB(GLenum type)
1082 {
1083 GET_CURRENT_CONTEXT(ctx);
1084 return create_shader(ctx, type);
1085 }
1086
1087
1088 GLuint GLAPIENTRY
1089 _mesa_CreateProgram(void)
1090 {
1091 GET_CURRENT_CONTEXT(ctx);
1092 if (MESA_VERBOSE & VERBOSE_API)
1093 _mesa_debug(ctx, "glCreateProgram\n");
1094 return create_shader_program(ctx);
1095 }
1096
1097
1098 GLhandleARB GLAPIENTRY
1099 _mesa_CreateProgramObjectARB(void)
1100 {
1101 GET_CURRENT_CONTEXT(ctx);
1102 return create_shader_program(ctx);
1103 }
1104
1105
1106 void GLAPIENTRY
1107 _mesa_DeleteObjectARB(GLhandleARB obj)
1108 {
1109 if (MESA_VERBOSE & VERBOSE_API) {
1110 GET_CURRENT_CONTEXT(ctx);
1111 _mesa_debug(ctx, "glDeleteObjectARB(%u)\n", obj);
1112 }
1113
1114 if (obj) {
1115 GET_CURRENT_CONTEXT(ctx);
1116 FLUSH_VERTICES(ctx, 0);
1117 if (is_program(ctx, obj)) {
1118 delete_shader_program(ctx, obj);
1119 }
1120 else if (is_shader(ctx, obj)) {
1121 delete_shader(ctx, obj);
1122 }
1123 else {
1124 /* error? */
1125 }
1126 }
1127 }
1128
1129
1130 void GLAPIENTRY
1131 _mesa_DeleteProgram(GLuint name)
1132 {
1133 if (name) {
1134 GET_CURRENT_CONTEXT(ctx);
1135 FLUSH_VERTICES(ctx, 0);
1136 delete_shader_program(ctx, name);
1137 }
1138 }
1139
1140
1141 void GLAPIENTRY
1142 _mesa_DeleteShader(GLuint name)
1143 {
1144 if (name) {
1145 GET_CURRENT_CONTEXT(ctx);
1146 FLUSH_VERTICES(ctx, 0);
1147 delete_shader(ctx, name);
1148 }
1149 }
1150
1151
1152 void GLAPIENTRY
1153 _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
1154 {
1155 GET_CURRENT_CONTEXT(ctx);
1156 detach_shader(ctx, program, shader);
1157 }
1158
1159
1160 void GLAPIENTRY
1161 _mesa_DetachShader(GLuint program, GLuint shader)
1162 {
1163 GET_CURRENT_CONTEXT(ctx);
1164 detach_shader(ctx, program, shader);
1165 }
1166
1167
1168 void GLAPIENTRY
1169 _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
1170 GLsizei * count, GLhandleARB * obj)
1171 {
1172 GET_CURRENT_CONTEXT(ctx);
1173 get_attached_shaders(ctx, container, maxCount, count, obj);
1174 }
1175
1176
1177 void GLAPIENTRY
1178 _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
1179 GLsizei *count, GLuint *obj)
1180 {
1181 GET_CURRENT_CONTEXT(ctx);
1182 get_attached_shaders(ctx, program, maxCount, count, obj);
1183 }
1184
1185
1186 /* GL_EXT_gpu_shader4, GL3 */
1187 GLint GLAPIENTRY
1188 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
1189 {
1190 GET_CURRENT_CONTEXT(ctx);
1191 return get_frag_data_location(ctx, program, name);
1192 }
1193
1194
1195
1196 void GLAPIENTRY
1197 _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
1198 GLcharARB * infoLog)
1199 {
1200 GET_CURRENT_CONTEXT(ctx);
1201 if (is_program(ctx, object)) {
1202 get_program_info_log(ctx, object, maxLength, length, infoLog);
1203 }
1204 else if (is_shader(ctx, object)) {
1205 get_shader_info_log(ctx, object, maxLength, length, infoLog);
1206 }
1207 else {
1208 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
1209 }
1210 }
1211
1212
1213 void GLAPIENTRY
1214 _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
1215 {
1216 GET_CURRENT_CONTEXT(ctx);
1217 /* Implement in terms of GetProgramiv, GetShaderiv */
1218 if (is_program(ctx, object)) {
1219 if (pname == GL_OBJECT_TYPE_ARB) {
1220 *params = GL_PROGRAM_OBJECT_ARB;
1221 }
1222 else {
1223 get_programiv(ctx, object, pname, params);
1224 }
1225 }
1226 else if (is_shader(ctx, object)) {
1227 if (pname == GL_OBJECT_TYPE_ARB) {
1228 *params = GL_SHADER_OBJECT_ARB;
1229 }
1230 else {
1231 get_shaderiv(ctx, object, pname, params);
1232 }
1233 }
1234 else {
1235 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
1236 }
1237 }
1238
1239
1240 void GLAPIENTRY
1241 _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
1242 GLfloat *params)
1243 {
1244 GLint iparams[1]; /* XXX is one element enough? */
1245 _mesa_GetObjectParameterivARB(object, pname, iparams);
1246 params[0] = (GLfloat) iparams[0];
1247 }
1248
1249
1250 void GLAPIENTRY
1251 _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
1252 {
1253 GET_CURRENT_CONTEXT(ctx);
1254 get_programiv(ctx, program, pname, params);
1255 }
1256
1257
1258 void GLAPIENTRY
1259 _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
1260 {
1261 GET_CURRENT_CONTEXT(ctx);
1262 get_shaderiv(ctx, shader, pname, params);
1263 }
1264
1265
1266 void GLAPIENTRY
1267 _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
1268 GLsizei *length, GLchar *infoLog)
1269 {
1270 GET_CURRENT_CONTEXT(ctx);
1271 get_program_info_log(ctx, program, bufSize, length, infoLog);
1272 }
1273
1274
1275 void GLAPIENTRY
1276 _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
1277 GLsizei *length, GLchar *infoLog)
1278 {
1279 GET_CURRENT_CONTEXT(ctx);
1280 get_shader_info_log(ctx, shader, bufSize, length, infoLog);
1281 }
1282
1283
1284 void GLAPIENTRY
1285 _mesa_GetShaderSourceARB(GLhandleARB shader, GLsizei maxLength,
1286 GLsizei *length, GLcharARB *sourceOut)
1287 {
1288 GET_CURRENT_CONTEXT(ctx);
1289 get_shader_source(ctx, shader, maxLength, length, sourceOut);
1290 }
1291
1292
1293 GLhandleARB GLAPIENTRY
1294 _mesa_GetHandleARB(GLenum pname)
1295 {
1296 GET_CURRENT_CONTEXT(ctx);
1297 return get_handle(ctx, pname);
1298 }
1299
1300
1301 GLboolean GLAPIENTRY
1302 _mesa_IsProgram(GLuint name)
1303 {
1304 GET_CURRENT_CONTEXT(ctx);
1305 return is_program(ctx, name);
1306 }
1307
1308
1309 GLboolean GLAPIENTRY
1310 _mesa_IsShader(GLuint name)
1311 {
1312 GET_CURRENT_CONTEXT(ctx);
1313 return is_shader(ctx, name);
1314 }
1315
1316
1317 void GLAPIENTRY
1318 _mesa_LinkProgramARB(GLhandleARB programObj)
1319 {
1320 GET_CURRENT_CONTEXT(ctx);
1321 link_program(ctx, programObj);
1322 }
1323
1324
1325
1326 /**
1327 * Read shader source code from a file.
1328 * Useful for debugging to override an app's shader.
1329 */
1330 static GLcharARB *
1331 read_shader(const char *fname)
1332 {
1333 const int max = 50*1000;
1334 FILE *f = fopen(fname, "r");
1335 GLcharARB *buffer, *shader;
1336 int len;
1337
1338 if (!f) {
1339 return NULL;
1340 }
1341
1342 buffer = (char *) malloc(max);
1343 len = fread(buffer, 1, max, f);
1344 buffer[len] = 0;
1345
1346 fclose(f);
1347
1348 shader = _mesa_strdup(buffer);
1349 free(buffer);
1350
1351 return shader;
1352 }
1353
1354
1355 /**
1356 * Called via glShaderSource() and glShaderSourceARB() API functions.
1357 * Basically, concatenate the source code strings into one long string
1358 * and pass it to _mesa_shader_source().
1359 */
1360 void GLAPIENTRY
1361 _mesa_ShaderSourceARB(GLhandleARB shaderObj, GLsizei count,
1362 const GLcharARB ** string, const GLint * length)
1363 {
1364 GET_CURRENT_CONTEXT(ctx);
1365 GLint *offsets;
1366 GLsizei i, totalLength;
1367 GLcharARB *source;
1368 GLuint checksum;
1369
1370 if (!shaderObj || string == NULL) {
1371 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
1372 return;
1373 }
1374
1375 /*
1376 * This array holds offsets of where the appropriate string ends, thus the
1377 * last element will be set to the total length of the source code.
1378 */
1379 offsets = (GLint *) malloc(count * sizeof(GLint));
1380 if (offsets == NULL) {
1381 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1382 return;
1383 }
1384
1385 for (i = 0; i < count; i++) {
1386 if (string[i] == NULL) {
1387 free((GLvoid *) offsets);
1388 _mesa_error(ctx, GL_INVALID_OPERATION,
1389 "glShaderSourceARB(null string)");
1390 return;
1391 }
1392 if (length == NULL || length[i] < 0)
1393 offsets[i] = strlen(string[i]);
1394 else
1395 offsets[i] = length[i];
1396 /* accumulate string lengths */
1397 if (i > 0)
1398 offsets[i] += offsets[i - 1];
1399 }
1400
1401 /* Total length of source string is sum off all strings plus two.
1402 * One extra byte for terminating zero, another extra byte to silence
1403 * valgrind warnings in the parser/grammer code.
1404 */
1405 totalLength = offsets[count - 1] + 2;
1406 source = (GLcharARB *) malloc(totalLength * sizeof(GLcharARB));
1407 if (source == NULL) {
1408 free((GLvoid *) offsets);
1409 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1410 return;
1411 }
1412
1413 for (i = 0; i < count; i++) {
1414 GLint start = (i > 0) ? offsets[i - 1] : 0;
1415 memcpy(source + start, string[i],
1416 (offsets[i] - start) * sizeof(GLcharARB));
1417 }
1418 source[totalLength - 1] = '\0';
1419 source[totalLength - 2] = '\0';
1420
1421 if (SHADER_SUBST) {
1422 /* Compute the shader's source code checksum then try to open a file
1423 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1424 * original shader source code. For debugging.
1425 */
1426 char filename[100];
1427 GLcharARB *newSource;
1428
1429 checksum = _mesa_str_checksum(source);
1430
1431 _mesa_snprintf(filename, sizeof(filename), "newshader_%d", checksum);
1432
1433 newSource = read_shader(filename);
1434 if (newSource) {
1435 fprintf(stderr, "Mesa: Replacing shader %u chksum=%d with %s\n",
1436 shaderObj, checksum, filename);
1437 free(source);
1438 source = newSource;
1439 }
1440 }
1441
1442 shader_source(ctx, shaderObj, source);
1443
1444 if (SHADER_SUBST) {
1445 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
1446 if (sh)
1447 sh->SourceChecksum = checksum; /* save original checksum */
1448 }
1449
1450 free(offsets);
1451 }
1452
1453
1454 void GLAPIENTRY
1455 _mesa_UseProgramObjectARB(GLhandleARB program)
1456 {
1457 GET_CURRENT_CONTEXT(ctx);
1458 struct gl_shader_program *shProg;
1459 struct gl_transform_feedback_object *obj =
1460 ctx->TransformFeedback.CurrentObject;
1461
1462 ASSERT_OUTSIDE_BEGIN_END(ctx);
1463
1464 if (obj->Active) {
1465 _mesa_error(ctx, GL_INVALID_OPERATION,
1466 "glUseProgram(transform feedback active)");
1467 return;
1468 }
1469
1470 if (program) {
1471 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
1472 if (!shProg) {
1473 return;
1474 }
1475 if (!shProg->LinkStatus) {
1476 _mesa_error(ctx, GL_INVALID_OPERATION,
1477 "glUseProgram(program %u not linked)", program);
1478 return;
1479 }
1480
1481 /* debug code */
1482 if (ctx->Shader.Flags & GLSL_USE_PROG) {
1483 print_shader_info(shProg);
1484 }
1485 }
1486 else {
1487 shProg = NULL;
1488 }
1489
1490 _mesa_use_program(ctx, shProg);
1491 }
1492
1493
1494 void GLAPIENTRY
1495 _mesa_ValidateProgramARB(GLhandleARB program)
1496 {
1497 GET_CURRENT_CONTEXT(ctx);
1498 validate_program(ctx, program);
1499 }
1500
1501 #ifdef FEATURE_ES2
1502
1503 void GLAPIENTRY
1504 _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
1505 GLint* range, GLint* precision)
1506 {
1507 const struct gl_program_constants *limits;
1508 const struct gl_precision *p;
1509 GET_CURRENT_CONTEXT(ctx);
1510
1511 switch (shadertype) {
1512 case GL_VERTEX_SHADER:
1513 limits = &ctx->Const.VertexProgram;
1514 break;
1515 case GL_FRAGMENT_SHADER:
1516 limits = &ctx->Const.FragmentProgram;
1517 break;
1518 default:
1519 _mesa_error(ctx, GL_INVALID_ENUM,
1520 "glGetShaderPrecisionFormat(shadertype)");
1521 return;
1522 }
1523
1524 switch (precisiontype) {
1525 case GL_LOW_FLOAT:
1526 p = &limits->LowFloat;
1527 break;
1528 case GL_MEDIUM_FLOAT:
1529 p = &limits->MediumFloat;
1530 break;
1531 case GL_HIGH_FLOAT:
1532 p = &limits->HighFloat;
1533 break;
1534 case GL_LOW_INT:
1535 p = &limits->LowInt;
1536 break;
1537 case GL_MEDIUM_INT:
1538 p = &limits->MediumInt;
1539 break;
1540 case GL_HIGH_INT:
1541 p = &limits->HighInt;
1542 break;
1543 default:
1544 _mesa_error(ctx, GL_INVALID_ENUM,
1545 "glGetShaderPrecisionFormat(precisiontype)");
1546 return;
1547 }
1548
1549 range[0] = p->RangeMin;
1550 range[1] = p->RangeMax;
1551 precision[0] = p->Precision;
1552 }
1553
1554
1555 void GLAPIENTRY
1556 _mesa_ReleaseShaderCompiler(void)
1557 {
1558 _mesa_destroy_shader_compiler_caches();
1559 }
1560
1561
1562 void GLAPIENTRY
1563 _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
1564 const void* binary, GLint length)
1565 {
1566 GET_CURRENT_CONTEXT(ctx);
1567 (void) n;
1568 (void) shaders;
1569 (void) binaryformat;
1570 (void) binary;
1571 (void) length;
1572 _mesa_error(ctx, GL_INVALID_OPERATION, __FUNCTION__);
1573 }
1574
1575 #endif /* FEATURE_ES2 */
1576
1577
1578 #if FEATURE_ARB_geometry_shader4
1579
1580 void GLAPIENTRY
1581 _mesa_ProgramParameteriARB(GLuint program, GLenum pname, GLint value)
1582 {
1583 struct gl_shader_program *shProg;
1584 GET_CURRENT_CONTEXT(ctx);
1585
1586 ASSERT_OUTSIDE_BEGIN_END(ctx);
1587
1588 shProg = _mesa_lookup_shader_program_err(ctx, program,
1589 "glProgramParameteri");
1590 if (!shProg)
1591 return;
1592
1593 switch (pname) {
1594 case GL_GEOMETRY_VERTICES_OUT_ARB:
1595 if (value < 1 ||
1596 (unsigned) value > ctx->Const.MaxGeometryOutputVertices) {
1597 _mesa_error(ctx, GL_INVALID_VALUE,
1598 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1599 value);
1600 return;
1601 }
1602 shProg->Geom.VerticesOut = value;
1603 break;
1604 case GL_GEOMETRY_INPUT_TYPE_ARB:
1605 switch (value) {
1606 case GL_POINTS:
1607 case GL_LINES:
1608 case GL_LINES_ADJACENCY_ARB:
1609 case GL_TRIANGLES:
1610 case GL_TRIANGLES_ADJACENCY_ARB:
1611 shProg->Geom.InputType = value;
1612 break;
1613 default:
1614 _mesa_error(ctx, GL_INVALID_VALUE,
1615 "glProgramParameteri(geometry input type = %s",
1616 _mesa_lookup_enum_by_nr(value));
1617 return;
1618 }
1619 break;
1620 case GL_GEOMETRY_OUTPUT_TYPE_ARB:
1621 switch (value) {
1622 case GL_POINTS:
1623 case GL_LINE_STRIP:
1624 case GL_TRIANGLE_STRIP:
1625 shProg->Geom.OutputType = value;
1626 break;
1627 default:
1628 _mesa_error(ctx, GL_INVALID_VALUE,
1629 "glProgramParameteri(geometry output type = %s",
1630 _mesa_lookup_enum_by_nr(value));
1631 return;
1632 }
1633 break;
1634 default:
1635 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteriARB(pname=%s)",
1636 _mesa_lookup_enum_by_nr(pname));
1637 break;
1638 }
1639 }
1640
1641 #endif
1642
1643 void
1644 _mesa_use_shader_program(struct gl_context *ctx, GLenum type,
1645 struct gl_shader_program *shProg)
1646 {
1647 use_shader_program(ctx, type, shProg);
1648
1649 if (ctx->Driver.UseProgram)
1650 ctx->Driver.UseProgram(ctx, shProg);
1651 }
1652
1653 void GLAPIENTRY
1654 _mesa_UseShaderProgramEXT(GLenum type, GLuint program)
1655 {
1656 GET_CURRENT_CONTEXT(ctx);
1657 struct gl_shader_program *shProg = NULL;
1658
1659 ASSERT_OUTSIDE_BEGIN_END(ctx);
1660
1661 if (!validate_shader_target(ctx, type)) {
1662 _mesa_error(ctx, GL_INVALID_ENUM, "glUseShaderProgramEXT(type)");
1663 return;
1664 }
1665
1666 if (ctx->TransformFeedback.CurrentObject->Active) {
1667 _mesa_error(ctx, GL_INVALID_OPERATION,
1668 "glUseShaderProgramEXT(transform feedback is active)");
1669 return;
1670 }
1671
1672 if (program) {
1673 shProg = _mesa_lookup_shader_program_err(ctx, program,
1674 "glUseShaderProgramEXT");
1675 if (shProg == NULL)
1676 return;
1677
1678 if (!shProg->LinkStatus) {
1679 _mesa_error(ctx, GL_INVALID_OPERATION,
1680 "glUseShaderProgramEXT(program not linked)");
1681 return;
1682 }
1683 }
1684
1685 _mesa_use_shader_program(ctx, type, shProg);
1686 }
1687
1688 void GLAPIENTRY
1689 _mesa_ActiveProgramEXT(GLuint program)
1690 {
1691 GET_CURRENT_CONTEXT(ctx);
1692 struct gl_shader_program *shProg = (program != 0)
1693 ? _mesa_lookup_shader_program_err(ctx, program, "glActiveProgramEXT")
1694 : NULL;
1695
1696 _mesa_active_program(ctx, shProg, "glActiveProgramEXT");
1697 return;
1698 }
1699
1700 GLuint GLAPIENTRY
1701 _mesa_CreateShaderProgramEXT(GLenum type, const GLchar *string)
1702 {
1703 GET_CURRENT_CONTEXT(ctx);
1704 const GLuint shader = create_shader(ctx, type);
1705 GLuint program = 0;
1706
1707 if (shader) {
1708 shader_source(ctx, shader, _mesa_strdup(string));
1709 compile_shader(ctx, shader);
1710
1711 program = create_shader_program(ctx);
1712 if (program) {
1713 struct gl_shader_program *shProg;
1714 struct gl_shader *sh;
1715 GLint compiled = GL_FALSE;
1716
1717 shProg = _mesa_lookup_shader_program(ctx, program);
1718 sh = _mesa_lookup_shader(ctx, shader);
1719
1720 get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
1721 if (compiled) {
1722 attach_shader(ctx, program, shader);
1723 link_program(ctx, program);
1724 detach_shader(ctx, program, shader);
1725
1726 #if 0
1727 /* Possibly... */
1728 if (active-user-defined-varyings-in-linked-program) {
1729 append-error-to-info-log;
1730 shProg->LinkStatus = GL_FALSE;
1731 }
1732 #endif
1733 }
1734
1735 ralloc_strcat(&shProg->InfoLog, sh->InfoLog);
1736 }
1737
1738 delete_shader(ctx, shader);
1739 }
1740
1741 return program;
1742 }
1743
1744 /**
1745 * Plug in shader-related functions into API dispatch table.
1746 */
1747 void
1748 _mesa_init_shader_dispatch(struct _glapi_table *exec)
1749 {
1750 #if FEATURE_GL
1751 /* GL_ARB_vertex/fragment_shader */
1752 SET_DeleteObjectARB(exec, _mesa_DeleteObjectARB);
1753 SET_GetHandleARB(exec, _mesa_GetHandleARB);
1754 SET_DetachObjectARB(exec, _mesa_DetachObjectARB);
1755 SET_CreateShaderObjectARB(exec, _mesa_CreateShaderObjectARB);
1756 SET_ShaderSourceARB(exec, _mesa_ShaderSourceARB);
1757 SET_CompileShaderARB(exec, _mesa_CompileShaderARB);
1758 SET_CreateProgramObjectARB(exec, _mesa_CreateProgramObjectARB);
1759 SET_AttachObjectARB(exec, _mesa_AttachObjectARB);
1760 SET_LinkProgramARB(exec, _mesa_LinkProgramARB);
1761 SET_UseProgramObjectARB(exec, _mesa_UseProgramObjectARB);
1762 SET_ValidateProgramARB(exec, _mesa_ValidateProgramARB);
1763 SET_GetObjectParameterfvARB(exec, _mesa_GetObjectParameterfvARB);
1764 SET_GetObjectParameterivARB(exec, _mesa_GetObjectParameterivARB);
1765 SET_GetInfoLogARB(exec, _mesa_GetInfoLogARB);
1766 SET_GetAttachedObjectsARB(exec, _mesa_GetAttachedObjectsARB);
1767 SET_GetShaderSourceARB(exec, _mesa_GetShaderSourceARB);
1768
1769 /* OpenGL 2.0 */
1770 SET_AttachShader(exec, _mesa_AttachShader);
1771 SET_CreateProgram(exec, _mesa_CreateProgram);
1772 SET_CreateShader(exec, _mesa_CreateShader);
1773 SET_DeleteProgram(exec, _mesa_DeleteProgram);
1774 SET_DeleteShader(exec, _mesa_DeleteShader);
1775 SET_DetachShader(exec, _mesa_DetachShader);
1776 SET_GetAttachedShaders(exec, _mesa_GetAttachedShaders);
1777 SET_GetProgramiv(exec, _mesa_GetProgramiv);
1778 SET_GetProgramInfoLog(exec, _mesa_GetProgramInfoLog);
1779 SET_GetShaderiv(exec, _mesa_GetShaderiv);
1780 SET_GetShaderInfoLog(exec, _mesa_GetShaderInfoLog);
1781 SET_IsProgram(exec, _mesa_IsProgram);
1782 SET_IsShader(exec, _mesa_IsShader);
1783
1784 #if FEATURE_ARB_vertex_shader
1785 SET_BindAttribLocationARB(exec, _mesa_BindAttribLocationARB);
1786 SET_GetActiveAttribARB(exec, _mesa_GetActiveAttribARB);
1787 SET_GetAttribLocationARB(exec, _mesa_GetAttribLocationARB);
1788 #endif
1789
1790 #if FEATURE_ARB_geometry_shader4
1791 SET_ProgramParameteriARB(exec, _mesa_ProgramParameteriARB);
1792 #endif
1793
1794 SET_UseShaderProgramEXT(exec, _mesa_UseShaderProgramEXT);
1795 SET_ActiveProgramEXT(exec, _mesa_ActiveProgramEXT);
1796 SET_CreateShaderProgramEXT(exec, _mesa_CreateShaderProgramEXT);
1797
1798 /* GL_EXT_gpu_shader4 / GL 3.0 */
1799 SET_BindFragDataLocationEXT(exec, _mesa_BindFragDataLocation);
1800 SET_GetFragDataLocationEXT(exec, _mesa_GetFragDataLocation);
1801
1802 /* GL_ARB_ES2_compatibility */
1803 SET_ReleaseShaderCompiler(exec, _mesa_ReleaseShaderCompiler);
1804 SET_GetShaderPrecisionFormat(exec, _mesa_GetShaderPrecisionFormat);
1805
1806 #endif /* FEATURE_GL */
1807 }
1808