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