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