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