c70fda1e205fba51ef7b623f4e7a6efe39d0329d
[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->_LinkedShaders[MESA_SHADER_VERTEX])
812 printf(" vert prog %u\n",
813 shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
814 if (shProg->FragmentProgram)
815 printf(" frag prog %u\n", shProg->FragmentProgram->Base.Id);
816 if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
817 printf(" geom prog %u\n",
818 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
819 }
820
821
822 /**
823 * Use the named shader program for subsequent glUniform calls
824 */
825 void
826 _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
827 const char *caller)
828 {
829 if ((shProg != NULL) && !shProg->LinkStatus) {
830 _mesa_error(ctx, GL_INVALID_OPERATION,
831 "%s(program %u not linked)", caller, shProg->Name);
832 return;
833 }
834
835 if (ctx->Shader.ActiveProgram != shProg) {
836 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
837 }
838 }
839
840 /**
841 */
842 static bool
843 use_shader_program(struct gl_context *ctx, GLenum type,
844 struct gl_shader_program *shProg)
845 {
846 struct gl_shader_program **target;
847
848 switch (type) {
849 #if FEATURE_ARB_vertex_shader
850 case GL_VERTEX_SHADER:
851 target = &ctx->Shader.CurrentVertexProgram;
852 if ((shProg == NULL)
853 || (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)) {
854 shProg = NULL;
855 }
856 break;
857 #endif
858 #if FEATURE_ARB_geometry_shader4
859 case GL_GEOMETRY_SHADER_ARB:
860 target = &ctx->Shader.CurrentGeometryProgram;
861 if ((shProg == NULL)
862 || (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY] == NULL)) {
863 shProg = NULL;
864 }
865 break;
866 #endif
867 #if FEATURE_ARB_fragment_shader
868 case GL_FRAGMENT_SHADER:
869 target = &ctx->Shader.CurrentFragmentProgram;
870 if ((shProg == NULL)
871 || (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)) {
872 shProg = NULL;
873 }
874 break;
875 #endif
876 default:
877 return false;
878 }
879
880 if (*target != shProg) {
881 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
882 _mesa_reference_shader_program(ctx, target, shProg);
883 return true;
884 }
885
886 return false;
887 }
888
889 /**
890 * Use the named shader program for subsequent rendering.
891 */
892 void
893 _mesa_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
894 {
895 use_shader_program(ctx, GL_VERTEX_SHADER, shProg);
896 use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB, shProg);
897 use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg);
898 _mesa_active_program(ctx, shProg, "glUseProgram");
899
900 if (ctx->Driver.UseProgram)
901 ctx->Driver.UseProgram(ctx, shProg);
902 }
903
904
905 /**
906 * Validate a program's samplers.
907 * Specifically, check that there aren't two samplers of different types
908 * pointing to the same texture unit.
909 * \return GL_TRUE if valid, GL_FALSE if invalid
910 */
911 static GLboolean
912 validate_samplers(const struct gl_program *prog, char *errMsg)
913 {
914 static const char *targetName[] = {
915 "TEXTURE_BUFFER",
916 "TEXTURE_2D_ARRAY",
917 "TEXTURE_1D_ARRAY",
918 "TEXTURE_CUBE",
919 "TEXTURE_3D",
920 "TEXTURE_RECT",
921 "TEXTURE_2D",
922 "TEXTURE_1D",
923 };
924 GLint targetUsed[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
925 GLbitfield samplersUsed = prog->SamplersUsed;
926 GLuint i;
927
928 assert(Elements(targetName) == NUM_TEXTURE_TARGETS);
929
930 if (samplersUsed == 0x0)
931 return GL_TRUE;
932
933 for (i = 0; i < Elements(targetUsed); i++)
934 targetUsed[i] = -1;
935
936 /* walk over bits which are set in 'samplers' */
937 while (samplersUsed) {
938 GLuint unit;
939 gl_texture_index target;
940 GLint sampler = _mesa_ffs(samplersUsed) - 1;
941 assert(sampler >= 0);
942 assert(sampler < Elements(prog->SamplerUnits));
943 unit = prog->SamplerUnits[sampler];
944 target = prog->SamplerTargets[sampler];
945 if (targetUsed[unit] != -1 && targetUsed[unit] != (int) target) {
946 _mesa_snprintf(errMsg, 100,
947 "Texture unit %d is accessed both as %s and %s",
948 unit, targetName[targetUsed[unit]], targetName[target]);
949 return GL_FALSE;
950 }
951 targetUsed[unit] = target;
952 samplersUsed ^= (1 << sampler);
953 }
954
955 return GL_TRUE;
956 }
957
958
959 /**
960 * Do validation of the given shader program.
961 * \param errMsg returns error message if validation fails.
962 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
963 */
964 static GLboolean
965 validate_shader_program(const struct gl_shader_program *shProg,
966 char *errMsg)
967 {
968 const struct gl_shader *vs = shProg->_LinkedShaders[MESA_SHADER_VERTEX];
969 const struct gl_shader *gs = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY];
970 const struct gl_fragment_program *fp = shProg->FragmentProgram;
971
972 if (!shProg->LinkStatus) {
973 return GL_FALSE;
974 }
975
976 /* From the GL spec, a program is invalid if any of these are true:
977
978 any two active samplers in the current program object are of
979 different types, but refer to the same texture image unit,
980
981 any active sampler in the current program object refers to a texture
982 image unit where fixed-function fragment processing accesses a
983 texture target that does not match the sampler type, or
984
985 the sum of the number of active samplers in the program and the
986 number of texture image units enabled for fixed-function fragment
987 processing exceeds the combined limit on the total number of texture
988 image units allowed.
989 */
990
991
992 /*
993 * Check: any two active samplers in the current program object are of
994 * different types, but refer to the same texture image unit,
995 */
996 if (vs && !validate_samplers(vs->Program, errMsg)) {
997 return GL_FALSE;
998 }
999 if (gs && !validate_samplers(gs->Program, errMsg)) {
1000 return GL_FALSE;
1001 }
1002 if (fp && !validate_samplers(&fp->Base, errMsg)) {
1003 return GL_FALSE;
1004 }
1005
1006 return GL_TRUE;
1007 }
1008
1009
1010 /**
1011 * Called via glValidateProgram()
1012 */
1013 static void
1014 validate_program(struct gl_context *ctx, GLuint program)
1015 {
1016 struct gl_shader_program *shProg;
1017 char errMsg[100] = "";
1018
1019 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1020 if (!shProg) {
1021 return;
1022 }
1023
1024 shProg->Validated = validate_shader_program(shProg, errMsg);
1025 if (!shProg->Validated) {
1026 /* update info log */
1027 if (shProg->InfoLog) {
1028 ralloc_free(shProg->InfoLog);
1029 }
1030 shProg->InfoLog = ralloc_strdup(shProg, errMsg);
1031 }
1032 }
1033
1034
1035
1036 void GLAPIENTRY
1037 _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
1038 {
1039 GET_CURRENT_CONTEXT(ctx);
1040 attach_shader(ctx, program, shader);
1041 }
1042
1043
1044 void GLAPIENTRY
1045 _mesa_AttachShader(GLuint program, GLuint shader)
1046 {
1047 GET_CURRENT_CONTEXT(ctx);
1048 attach_shader(ctx, program, shader);
1049 }
1050
1051
1052 /* GL_EXT_gpu_shader4, GL3 */
1053 void GLAPIENTRY
1054 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
1055 const GLchar *name)
1056 {
1057 GET_CURRENT_CONTEXT(ctx);
1058 bind_frag_data_location(ctx, program, colorNumber, name);
1059 }
1060
1061
1062 void GLAPIENTRY
1063 _mesa_CompileShaderARB(GLhandleARB shaderObj)
1064 {
1065 GET_CURRENT_CONTEXT(ctx);
1066 if (MESA_VERBOSE & VERBOSE_API)
1067 _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
1068 compile_shader(ctx, shaderObj);
1069 }
1070
1071
1072 GLuint GLAPIENTRY
1073 _mesa_CreateShader(GLenum type)
1074 {
1075 GET_CURRENT_CONTEXT(ctx);
1076 if (MESA_VERBOSE & VERBOSE_API)
1077 _mesa_debug(ctx, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type));
1078 return create_shader(ctx, type);
1079 }
1080
1081
1082 GLhandleARB GLAPIENTRY
1083 _mesa_CreateShaderObjectARB(GLenum type)
1084 {
1085 GET_CURRENT_CONTEXT(ctx);
1086 return create_shader(ctx, type);
1087 }
1088
1089
1090 GLuint GLAPIENTRY
1091 _mesa_CreateProgram(void)
1092 {
1093 GET_CURRENT_CONTEXT(ctx);
1094 if (MESA_VERBOSE & VERBOSE_API)
1095 _mesa_debug(ctx, "glCreateProgram\n");
1096 return create_shader_program(ctx);
1097 }
1098
1099
1100 GLhandleARB GLAPIENTRY
1101 _mesa_CreateProgramObjectARB(void)
1102 {
1103 GET_CURRENT_CONTEXT(ctx);
1104 return create_shader_program(ctx);
1105 }
1106
1107
1108 void GLAPIENTRY
1109 _mesa_DeleteObjectARB(GLhandleARB obj)
1110 {
1111 if (MESA_VERBOSE & VERBOSE_API) {
1112 GET_CURRENT_CONTEXT(ctx);
1113 _mesa_debug(ctx, "glDeleteObjectARB(%u)\n", obj);
1114 }
1115
1116 if (obj) {
1117 GET_CURRENT_CONTEXT(ctx);
1118 FLUSH_VERTICES(ctx, 0);
1119 if (is_program(ctx, obj)) {
1120 delete_shader_program(ctx, obj);
1121 }
1122 else if (is_shader(ctx, obj)) {
1123 delete_shader(ctx, obj);
1124 }
1125 else {
1126 /* error? */
1127 }
1128 }
1129 }
1130
1131
1132 void GLAPIENTRY
1133 _mesa_DeleteProgram(GLuint name)
1134 {
1135 if (name) {
1136 GET_CURRENT_CONTEXT(ctx);
1137 FLUSH_VERTICES(ctx, 0);
1138 delete_shader_program(ctx, name);
1139 }
1140 }
1141
1142
1143 void GLAPIENTRY
1144 _mesa_DeleteShader(GLuint name)
1145 {
1146 if (name) {
1147 GET_CURRENT_CONTEXT(ctx);
1148 FLUSH_VERTICES(ctx, 0);
1149 delete_shader(ctx, name);
1150 }
1151 }
1152
1153
1154 void GLAPIENTRY
1155 _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
1156 {
1157 GET_CURRENT_CONTEXT(ctx);
1158 detach_shader(ctx, program, shader);
1159 }
1160
1161
1162 void GLAPIENTRY
1163 _mesa_DetachShader(GLuint program, GLuint shader)
1164 {
1165 GET_CURRENT_CONTEXT(ctx);
1166 detach_shader(ctx, program, shader);
1167 }
1168
1169
1170 void GLAPIENTRY
1171 _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
1172 GLsizei * count, GLhandleARB * obj)
1173 {
1174 GET_CURRENT_CONTEXT(ctx);
1175 get_attached_shaders(ctx, container, maxCount, count, obj);
1176 }
1177
1178
1179 void GLAPIENTRY
1180 _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
1181 GLsizei *count, GLuint *obj)
1182 {
1183 GET_CURRENT_CONTEXT(ctx);
1184 get_attached_shaders(ctx, program, maxCount, count, obj);
1185 }
1186
1187
1188 /* GL_EXT_gpu_shader4, GL3 */
1189 GLint GLAPIENTRY
1190 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
1191 {
1192 GET_CURRENT_CONTEXT(ctx);
1193 return get_frag_data_location(ctx, program, name);
1194 }
1195
1196
1197
1198 void GLAPIENTRY
1199 _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
1200 GLcharARB * infoLog)
1201 {
1202 GET_CURRENT_CONTEXT(ctx);
1203 if (is_program(ctx, object)) {
1204 get_program_info_log(ctx, object, maxLength, length, infoLog);
1205 }
1206 else if (is_shader(ctx, object)) {
1207 get_shader_info_log(ctx, object, maxLength, length, infoLog);
1208 }
1209 else {
1210 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
1211 }
1212 }
1213
1214
1215 void GLAPIENTRY
1216 _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
1217 {
1218 GET_CURRENT_CONTEXT(ctx);
1219 /* Implement in terms of GetProgramiv, GetShaderiv */
1220 if (is_program(ctx, object)) {
1221 if (pname == GL_OBJECT_TYPE_ARB) {
1222 *params = GL_PROGRAM_OBJECT_ARB;
1223 }
1224 else {
1225 get_programiv(ctx, object, pname, params);
1226 }
1227 }
1228 else if (is_shader(ctx, object)) {
1229 if (pname == GL_OBJECT_TYPE_ARB) {
1230 *params = GL_SHADER_OBJECT_ARB;
1231 }
1232 else {
1233 get_shaderiv(ctx, object, pname, params);
1234 }
1235 }
1236 else {
1237 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
1238 }
1239 }
1240
1241
1242 void GLAPIENTRY
1243 _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
1244 GLfloat *params)
1245 {
1246 GLint iparams[1]; /* XXX is one element enough? */
1247 _mesa_GetObjectParameterivARB(object, pname, iparams);
1248 params[0] = (GLfloat) iparams[0];
1249 }
1250
1251
1252 void GLAPIENTRY
1253 _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
1254 {
1255 GET_CURRENT_CONTEXT(ctx);
1256 get_programiv(ctx, program, pname, params);
1257 }
1258
1259
1260 void GLAPIENTRY
1261 _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
1262 {
1263 GET_CURRENT_CONTEXT(ctx);
1264 get_shaderiv(ctx, shader, pname, params);
1265 }
1266
1267
1268 void GLAPIENTRY
1269 _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
1270 GLsizei *length, GLchar *infoLog)
1271 {
1272 GET_CURRENT_CONTEXT(ctx);
1273 get_program_info_log(ctx, program, bufSize, length, infoLog);
1274 }
1275
1276
1277 void GLAPIENTRY
1278 _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
1279 GLsizei *length, GLchar *infoLog)
1280 {
1281 GET_CURRENT_CONTEXT(ctx);
1282 get_shader_info_log(ctx, shader, bufSize, length, infoLog);
1283 }
1284
1285
1286 void GLAPIENTRY
1287 _mesa_GetShaderSourceARB(GLhandleARB shader, GLsizei maxLength,
1288 GLsizei *length, GLcharARB *sourceOut)
1289 {
1290 GET_CURRENT_CONTEXT(ctx);
1291 get_shader_source(ctx, shader, maxLength, length, sourceOut);
1292 }
1293
1294
1295 GLhandleARB GLAPIENTRY
1296 _mesa_GetHandleARB(GLenum pname)
1297 {
1298 GET_CURRENT_CONTEXT(ctx);
1299 return get_handle(ctx, pname);
1300 }
1301
1302
1303 GLboolean GLAPIENTRY
1304 _mesa_IsProgram(GLuint name)
1305 {
1306 GET_CURRENT_CONTEXT(ctx);
1307 return is_program(ctx, name);
1308 }
1309
1310
1311 GLboolean GLAPIENTRY
1312 _mesa_IsShader(GLuint name)
1313 {
1314 GET_CURRENT_CONTEXT(ctx);
1315 return is_shader(ctx, name);
1316 }
1317
1318
1319 void GLAPIENTRY
1320 _mesa_LinkProgramARB(GLhandleARB programObj)
1321 {
1322 GET_CURRENT_CONTEXT(ctx);
1323 link_program(ctx, programObj);
1324 }
1325
1326
1327
1328 /**
1329 * Read shader source code from a file.
1330 * Useful for debugging to override an app's shader.
1331 */
1332 static GLcharARB *
1333 read_shader(const char *fname)
1334 {
1335 const int max = 50*1000;
1336 FILE *f = fopen(fname, "r");
1337 GLcharARB *buffer, *shader;
1338 int len;
1339
1340 if (!f) {
1341 return NULL;
1342 }
1343
1344 buffer = (char *) malloc(max);
1345 len = fread(buffer, 1, max, f);
1346 buffer[len] = 0;
1347
1348 fclose(f);
1349
1350 shader = _mesa_strdup(buffer);
1351 free(buffer);
1352
1353 return shader;
1354 }
1355
1356
1357 /**
1358 * Called via glShaderSource() and glShaderSourceARB() API functions.
1359 * Basically, concatenate the source code strings into one long string
1360 * and pass it to _mesa_shader_source().
1361 */
1362 void GLAPIENTRY
1363 _mesa_ShaderSourceARB(GLhandleARB shaderObj, GLsizei count,
1364 const GLcharARB ** string, const GLint * length)
1365 {
1366 GET_CURRENT_CONTEXT(ctx);
1367 GLint *offsets;
1368 GLsizei i, totalLength;
1369 GLcharARB *source;
1370 GLuint checksum;
1371
1372 if (!shaderObj || string == NULL) {
1373 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
1374 return;
1375 }
1376
1377 /*
1378 * This array holds offsets of where the appropriate string ends, thus the
1379 * last element will be set to the total length of the source code.
1380 */
1381 offsets = (GLint *) malloc(count * sizeof(GLint));
1382 if (offsets == NULL) {
1383 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1384 return;
1385 }
1386
1387 for (i = 0; i < count; i++) {
1388 if (string[i] == NULL) {
1389 free((GLvoid *) offsets);
1390 _mesa_error(ctx, GL_INVALID_OPERATION,
1391 "glShaderSourceARB(null string)");
1392 return;
1393 }
1394 if (length == NULL || length[i] < 0)
1395 offsets[i] = strlen(string[i]);
1396 else
1397 offsets[i] = length[i];
1398 /* accumulate string lengths */
1399 if (i > 0)
1400 offsets[i] += offsets[i - 1];
1401 }
1402
1403 /* Total length of source string is sum off all strings plus two.
1404 * One extra byte for terminating zero, another extra byte to silence
1405 * valgrind warnings in the parser/grammer code.
1406 */
1407 totalLength = offsets[count - 1] + 2;
1408 source = (GLcharARB *) malloc(totalLength * sizeof(GLcharARB));
1409 if (source == NULL) {
1410 free((GLvoid *) offsets);
1411 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1412 return;
1413 }
1414
1415 for (i = 0; i < count; i++) {
1416 GLint start = (i > 0) ? offsets[i - 1] : 0;
1417 memcpy(source + start, string[i],
1418 (offsets[i] - start) * sizeof(GLcharARB));
1419 }
1420 source[totalLength - 1] = '\0';
1421 source[totalLength - 2] = '\0';
1422
1423 if (SHADER_SUBST) {
1424 /* Compute the shader's source code checksum then try to open a file
1425 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1426 * original shader source code. For debugging.
1427 */
1428 char filename[100];
1429 GLcharARB *newSource;
1430
1431 checksum = _mesa_str_checksum(source);
1432
1433 _mesa_snprintf(filename, sizeof(filename), "newshader_%d", checksum);
1434
1435 newSource = read_shader(filename);
1436 if (newSource) {
1437 fprintf(stderr, "Mesa: Replacing shader %u chksum=%d with %s\n",
1438 shaderObj, checksum, filename);
1439 free(source);
1440 source = newSource;
1441 }
1442 }
1443
1444 shader_source(ctx, shaderObj, source);
1445
1446 if (SHADER_SUBST) {
1447 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
1448 if (sh)
1449 sh->SourceChecksum = checksum; /* save original checksum */
1450 }
1451
1452 free(offsets);
1453 }
1454
1455
1456 void GLAPIENTRY
1457 _mesa_UseProgramObjectARB(GLhandleARB program)
1458 {
1459 GET_CURRENT_CONTEXT(ctx);
1460 struct gl_shader_program *shProg;
1461 struct gl_transform_feedback_object *obj =
1462 ctx->TransformFeedback.CurrentObject;
1463
1464 ASSERT_OUTSIDE_BEGIN_END(ctx);
1465
1466 if (obj->Active) {
1467 _mesa_error(ctx, GL_INVALID_OPERATION,
1468 "glUseProgram(transform feedback active)");
1469 return;
1470 }
1471
1472 if (program) {
1473 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
1474 if (!shProg) {
1475 return;
1476 }
1477 if (!shProg->LinkStatus) {
1478 _mesa_error(ctx, GL_INVALID_OPERATION,
1479 "glUseProgram(program %u not linked)", program);
1480 return;
1481 }
1482
1483 /* debug code */
1484 if (ctx->Shader.Flags & GLSL_USE_PROG) {
1485 print_shader_info(shProg);
1486 }
1487 }
1488 else {
1489 shProg = NULL;
1490 }
1491
1492 _mesa_use_program(ctx, shProg);
1493 }
1494
1495
1496 void GLAPIENTRY
1497 _mesa_ValidateProgramARB(GLhandleARB program)
1498 {
1499 GET_CURRENT_CONTEXT(ctx);
1500 validate_program(ctx, program);
1501 }
1502
1503 #ifdef FEATURE_ES2
1504
1505 void GLAPIENTRY
1506 _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
1507 GLint* range, GLint* precision)
1508 {
1509 const struct gl_program_constants *limits;
1510 const struct gl_precision *p;
1511 GET_CURRENT_CONTEXT(ctx);
1512
1513 switch (shadertype) {
1514 case GL_VERTEX_SHADER:
1515 limits = &ctx->Const.VertexProgram;
1516 break;
1517 case GL_FRAGMENT_SHADER:
1518 limits = &ctx->Const.FragmentProgram;
1519 break;
1520 default:
1521 _mesa_error(ctx, GL_INVALID_ENUM,
1522 "glGetShaderPrecisionFormat(shadertype)");
1523 return;
1524 }
1525
1526 switch (precisiontype) {
1527 case GL_LOW_FLOAT:
1528 p = &limits->LowFloat;
1529 break;
1530 case GL_MEDIUM_FLOAT:
1531 p = &limits->MediumFloat;
1532 break;
1533 case GL_HIGH_FLOAT:
1534 p = &limits->HighFloat;
1535 break;
1536 case GL_LOW_INT:
1537 p = &limits->LowInt;
1538 break;
1539 case GL_MEDIUM_INT:
1540 p = &limits->MediumInt;
1541 break;
1542 case GL_HIGH_INT:
1543 p = &limits->HighInt;
1544 break;
1545 default:
1546 _mesa_error(ctx, GL_INVALID_ENUM,
1547 "glGetShaderPrecisionFormat(precisiontype)");
1548 return;
1549 }
1550
1551 range[0] = p->RangeMin;
1552 range[1] = p->RangeMax;
1553 precision[0] = p->Precision;
1554 }
1555
1556
1557 void GLAPIENTRY
1558 _mesa_ReleaseShaderCompiler(void)
1559 {
1560 _mesa_destroy_shader_compiler_caches();
1561 }
1562
1563
1564 void GLAPIENTRY
1565 _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
1566 const void* binary, GLint length)
1567 {
1568 GET_CURRENT_CONTEXT(ctx);
1569 (void) n;
1570 (void) shaders;
1571 (void) binaryformat;
1572 (void) binary;
1573 (void) length;
1574 _mesa_error(ctx, GL_INVALID_OPERATION, __FUNCTION__);
1575 }
1576
1577 #endif /* FEATURE_ES2 */
1578
1579
1580 #if FEATURE_ARB_geometry_shader4
1581
1582 void GLAPIENTRY
1583 _mesa_ProgramParameteriARB(GLuint program, GLenum pname, GLint value)
1584 {
1585 struct gl_shader_program *shProg;
1586 GET_CURRENT_CONTEXT(ctx);
1587
1588 ASSERT_OUTSIDE_BEGIN_END(ctx);
1589
1590 shProg = _mesa_lookup_shader_program_err(ctx, program,
1591 "glProgramParameteri");
1592 if (!shProg)
1593 return;
1594
1595 switch (pname) {
1596 case GL_GEOMETRY_VERTICES_OUT_ARB:
1597 if (value < 1 ||
1598 (unsigned) value > ctx->Const.MaxGeometryOutputVertices) {
1599 _mesa_error(ctx, GL_INVALID_VALUE,
1600 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1601 value);
1602 return;
1603 }
1604 shProg->Geom.VerticesOut = value;
1605 break;
1606 case GL_GEOMETRY_INPUT_TYPE_ARB:
1607 switch (value) {
1608 case GL_POINTS:
1609 case GL_LINES:
1610 case GL_LINES_ADJACENCY_ARB:
1611 case GL_TRIANGLES:
1612 case GL_TRIANGLES_ADJACENCY_ARB:
1613 shProg->Geom.InputType = value;
1614 break;
1615 default:
1616 _mesa_error(ctx, GL_INVALID_VALUE,
1617 "glProgramParameteri(geometry input type = %s",
1618 _mesa_lookup_enum_by_nr(value));
1619 return;
1620 }
1621 break;
1622 case GL_GEOMETRY_OUTPUT_TYPE_ARB:
1623 switch (value) {
1624 case GL_POINTS:
1625 case GL_LINE_STRIP:
1626 case GL_TRIANGLE_STRIP:
1627 shProg->Geom.OutputType = value;
1628 break;
1629 default:
1630 _mesa_error(ctx, GL_INVALID_VALUE,
1631 "glProgramParameteri(geometry output type = %s",
1632 _mesa_lookup_enum_by_nr(value));
1633 return;
1634 }
1635 break;
1636 default:
1637 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteriARB(pname=%s)",
1638 _mesa_lookup_enum_by_nr(pname));
1639 break;
1640 }
1641 }
1642
1643 #endif
1644
1645 void
1646 _mesa_use_shader_program(struct gl_context *ctx, GLenum type,
1647 struct gl_shader_program *shProg)
1648 {
1649 use_shader_program(ctx, type, shProg);
1650
1651 if (ctx->Driver.UseProgram)
1652 ctx->Driver.UseProgram(ctx, shProg);
1653 }
1654
1655 void GLAPIENTRY
1656 _mesa_UseShaderProgramEXT(GLenum type, GLuint program)
1657 {
1658 GET_CURRENT_CONTEXT(ctx);
1659 struct gl_shader_program *shProg = NULL;
1660
1661 ASSERT_OUTSIDE_BEGIN_END(ctx);
1662
1663 if (!validate_shader_target(ctx, type)) {
1664 _mesa_error(ctx, GL_INVALID_ENUM, "glUseShaderProgramEXT(type)");
1665 return;
1666 }
1667
1668 if (ctx->TransformFeedback.CurrentObject->Active) {
1669 _mesa_error(ctx, GL_INVALID_OPERATION,
1670 "glUseShaderProgramEXT(transform feedback is active)");
1671 return;
1672 }
1673
1674 if (program) {
1675 shProg = _mesa_lookup_shader_program_err(ctx, program,
1676 "glUseShaderProgramEXT");
1677 if (shProg == NULL)
1678 return;
1679
1680 if (!shProg->LinkStatus) {
1681 _mesa_error(ctx, GL_INVALID_OPERATION,
1682 "glUseShaderProgramEXT(program not linked)");
1683 return;
1684 }
1685 }
1686
1687 _mesa_use_shader_program(ctx, type, shProg);
1688 }
1689
1690 void GLAPIENTRY
1691 _mesa_ActiveProgramEXT(GLuint program)
1692 {
1693 GET_CURRENT_CONTEXT(ctx);
1694 struct gl_shader_program *shProg = (program != 0)
1695 ? _mesa_lookup_shader_program_err(ctx, program, "glActiveProgramEXT")
1696 : NULL;
1697
1698 _mesa_active_program(ctx, shProg, "glActiveProgramEXT");
1699 return;
1700 }
1701
1702 GLuint GLAPIENTRY
1703 _mesa_CreateShaderProgramEXT(GLenum type, const GLchar *string)
1704 {
1705 GET_CURRENT_CONTEXT(ctx);
1706 const GLuint shader = create_shader(ctx, type);
1707 GLuint program = 0;
1708
1709 if (shader) {
1710 shader_source(ctx, shader, _mesa_strdup(string));
1711 compile_shader(ctx, shader);
1712
1713 program = create_shader_program(ctx);
1714 if (program) {
1715 struct gl_shader_program *shProg;
1716 struct gl_shader *sh;
1717 GLint compiled = GL_FALSE;
1718
1719 shProg = _mesa_lookup_shader_program(ctx, program);
1720 sh = _mesa_lookup_shader(ctx, shader);
1721
1722 get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
1723 if (compiled) {
1724 attach_shader(ctx, program, shader);
1725 link_program(ctx, program);
1726 detach_shader(ctx, program, shader);
1727
1728 #if 0
1729 /* Possibly... */
1730 if (active-user-defined-varyings-in-linked-program) {
1731 append-error-to-info-log;
1732 shProg->LinkStatus = GL_FALSE;
1733 }
1734 #endif
1735 }
1736
1737 ralloc_strcat(&shProg->InfoLog, sh->InfoLog);
1738 }
1739
1740 delete_shader(ctx, shader);
1741 }
1742
1743 return program;
1744 }
1745
1746 /**
1747 * Plug in shader-related functions into API dispatch table.
1748 */
1749 void
1750 _mesa_init_shader_dispatch(struct _glapi_table *exec)
1751 {
1752 #if FEATURE_GL
1753 /* GL_ARB_vertex/fragment_shader */
1754 SET_DeleteObjectARB(exec, _mesa_DeleteObjectARB);
1755 SET_GetHandleARB(exec, _mesa_GetHandleARB);
1756 SET_DetachObjectARB(exec, _mesa_DetachObjectARB);
1757 SET_CreateShaderObjectARB(exec, _mesa_CreateShaderObjectARB);
1758 SET_ShaderSourceARB(exec, _mesa_ShaderSourceARB);
1759 SET_CompileShaderARB(exec, _mesa_CompileShaderARB);
1760 SET_CreateProgramObjectARB(exec, _mesa_CreateProgramObjectARB);
1761 SET_AttachObjectARB(exec, _mesa_AttachObjectARB);
1762 SET_LinkProgramARB(exec, _mesa_LinkProgramARB);
1763 SET_UseProgramObjectARB(exec, _mesa_UseProgramObjectARB);
1764 SET_ValidateProgramARB(exec, _mesa_ValidateProgramARB);
1765 SET_GetObjectParameterfvARB(exec, _mesa_GetObjectParameterfvARB);
1766 SET_GetObjectParameterivARB(exec, _mesa_GetObjectParameterivARB);
1767 SET_GetInfoLogARB(exec, _mesa_GetInfoLogARB);
1768 SET_GetAttachedObjectsARB(exec, _mesa_GetAttachedObjectsARB);
1769 SET_GetShaderSourceARB(exec, _mesa_GetShaderSourceARB);
1770
1771 /* OpenGL 2.0 */
1772 SET_AttachShader(exec, _mesa_AttachShader);
1773 SET_CreateProgram(exec, _mesa_CreateProgram);
1774 SET_CreateShader(exec, _mesa_CreateShader);
1775 SET_DeleteProgram(exec, _mesa_DeleteProgram);
1776 SET_DeleteShader(exec, _mesa_DeleteShader);
1777 SET_DetachShader(exec, _mesa_DetachShader);
1778 SET_GetAttachedShaders(exec, _mesa_GetAttachedShaders);
1779 SET_GetProgramiv(exec, _mesa_GetProgramiv);
1780 SET_GetProgramInfoLog(exec, _mesa_GetProgramInfoLog);
1781 SET_GetShaderiv(exec, _mesa_GetShaderiv);
1782 SET_GetShaderInfoLog(exec, _mesa_GetShaderInfoLog);
1783 SET_IsProgram(exec, _mesa_IsProgram);
1784 SET_IsShader(exec, _mesa_IsShader);
1785
1786 #if FEATURE_ARB_vertex_shader
1787 SET_BindAttribLocationARB(exec, _mesa_BindAttribLocationARB);
1788 SET_GetActiveAttribARB(exec, _mesa_GetActiveAttribARB);
1789 SET_GetAttribLocationARB(exec, _mesa_GetAttribLocationARB);
1790 #endif
1791
1792 #if FEATURE_ARB_geometry_shader4
1793 SET_ProgramParameteriARB(exec, _mesa_ProgramParameteriARB);
1794 #endif
1795
1796 SET_UseShaderProgramEXT(exec, _mesa_UseShaderProgramEXT);
1797 SET_ActiveProgramEXT(exec, _mesa_ActiveProgramEXT);
1798 SET_CreateShaderProgramEXT(exec, _mesa_CreateShaderProgramEXT);
1799
1800 /* GL_EXT_gpu_shader4 / GL 3.0 */
1801 SET_BindFragDataLocationEXT(exec, _mesa_BindFragDataLocation);
1802 SET_GetFragDataLocationEXT(exec, _mesa_GetFragDataLocation);
1803
1804 /* GL_ARB_ES2_compatibility */
1805 SET_ReleaseShaderCompiler(exec, _mesa_ReleaseShaderCompiler);
1806 SET_GetShaderPrecisionFormat(exec, _mesa_GetShaderPrecisionFormat);
1807
1808 #endif /* FEATURE_GL */
1809 }
1810