mesa: initialize gl_geometry_program Invocations field
[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_INPUT_TYPE:
610 if (!has_core_gs)
611 break;
612 if (check_gs_query(ctx, shProg))
613 *params = shProg->Geom.InputType;
614 return;
615 case GL_GEOMETRY_OUTPUT_TYPE:
616 if (!has_core_gs)
617 break;
618 if (check_gs_query(ctx, shProg))
619 *params = shProg->Geom.OutputType;
620 return;
621 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH: {
622 unsigned i;
623 GLint max_len = 0;
624
625 if (!has_ubo)
626 break;
627
628 for (i = 0; i < shProg->NumUniformBlocks; i++) {
629 /* Add one for the terminating NUL character.
630 */
631 const GLint len = strlen(shProg->UniformBlocks[i].Name) + 1;
632
633 if (len > max_len)
634 max_len = len;
635 }
636
637 *params = max_len;
638 return;
639 }
640 case GL_ACTIVE_UNIFORM_BLOCKS:
641 if (!has_ubo)
642 break;
643
644 *params = shProg->NumUniformBlocks;
645 return;
646 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
647 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
648 * only available with desktop OpenGL 3.0+ with the
649 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
650 *
651 * On desktop, we ignore the 3.0+ requirement because it is silly.
652 */
653 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
654 break;
655
656 *params = shProg->BinaryRetreivableHint;
657 return;
658 case GL_PROGRAM_BINARY_LENGTH:
659 *params = 0;
660 return;
661 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
662 if (!ctx->Extensions.ARB_shader_atomic_counters)
663 break;
664
665 *params = shProg->NumAtomicBuffers;
666 return;
667 case GL_COMPUTE_WORK_GROUP_SIZE: {
668 int i;
669 if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_compute_shader)
670 break;
671 if (!shProg->LinkStatus) {
672 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(program not "
673 "linked)");
674 return;
675 }
676 if (shProg->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
677 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(no compute "
678 "shaders)");
679 return;
680 }
681 for (i = 0; i < 3; i++)
682 params[i] = shProg->Comp.LocalSize[i];
683 return;
684 }
685 default:
686 break;
687 }
688
689 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname=%s)",
690 _mesa_lookup_enum_by_nr(pname));
691 }
692
693
694 /**
695 * glGetShaderiv() - get GLSL shader state
696 */
697 static void
698 get_shaderiv(struct gl_context *ctx, GLuint name, GLenum pname, GLint *params)
699 {
700 struct gl_shader *shader =
701 _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
702
703 if (!shader) {
704 return;
705 }
706
707 switch (pname) {
708 case GL_SHADER_TYPE:
709 *params = shader->Type;
710 break;
711 case GL_DELETE_STATUS:
712 *params = shader->DeletePending;
713 break;
714 case GL_COMPILE_STATUS:
715 *params = shader->CompileStatus;
716 break;
717 case GL_INFO_LOG_LENGTH:
718 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
719 break;
720 case GL_SHADER_SOURCE_LENGTH:
721 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
722 break;
723 default:
724 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
725 return;
726 }
727 }
728
729
730 static void
731 get_program_info_log(struct gl_context *ctx, GLuint program, GLsizei bufSize,
732 GLsizei *length, GLchar *infoLog)
733 {
734 struct gl_shader_program *shProg
735 = _mesa_lookup_shader_program(ctx, program);
736 if (!shProg) {
737 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
738 return;
739 }
740 _mesa_copy_string(infoLog, bufSize, length, shProg->InfoLog);
741 }
742
743
744 static void
745 get_shader_info_log(struct gl_context *ctx, GLuint shader, GLsizei bufSize,
746 GLsizei *length, GLchar *infoLog)
747 {
748 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
749 if (!sh) {
750 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
751 return;
752 }
753 _mesa_copy_string(infoLog, bufSize, length, sh->InfoLog);
754 }
755
756
757 /**
758 * Return shader source code.
759 */
760 static void
761 get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
762 GLsizei *length, GLchar *sourceOut)
763 {
764 struct gl_shader *sh;
765 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
766 if (!sh) {
767 return;
768 }
769 _mesa_copy_string(sourceOut, maxLength, length, sh->Source);
770 }
771
772
773 /**
774 * Set/replace shader source code. A helper function used by
775 * glShaderSource[ARB] and glCreateShaderProgramEXT.
776 */
777 static void
778 shader_source(struct gl_context *ctx, GLuint shader, const GLchar *source)
779 {
780 struct gl_shader *sh;
781
782 sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
783 if (!sh)
784 return;
785
786 /* free old shader source string and install new one */
787 free((void *)sh->Source);
788 sh->Source = source;
789 sh->CompileStatus = GL_FALSE;
790 #ifdef DEBUG
791 sh->SourceChecksum = _mesa_str_checksum(sh->Source);
792 #endif
793 }
794
795
796 /**
797 * Compile a shader.
798 */
799 static void
800 compile_shader(struct gl_context *ctx, GLuint shaderObj)
801 {
802 struct gl_shader *sh;
803 struct gl_shader_compiler_options *options;
804
805 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
806 if (!sh)
807 return;
808
809 options = &ctx->ShaderCompilerOptions[sh->Stage];
810
811 /* set default pragma state for shader */
812 sh->Pragmas = options->DefaultPragmas;
813
814 if (!sh->Source) {
815 /* If the user called glCompileShader without first calling
816 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
817 */
818 sh->CompileStatus = GL_FALSE;
819 } else {
820 if (ctx->Shader.Flags & GLSL_DUMP) {
821 printf("GLSL source for %s shader %d:\n",
822 _mesa_shader_stage_to_string(sh->Stage), sh->Name);
823 printf("%s\n", sh->Source);
824 }
825
826 /* this call will set the shader->CompileStatus field to indicate if
827 * compilation was successful.
828 */
829 _mesa_glsl_compile_shader(ctx, sh, false, false);
830
831 if (ctx->Shader.Flags & GLSL_LOG) {
832 _mesa_write_shader_to_file(sh);
833 }
834
835 if (ctx->Shader.Flags & GLSL_DUMP) {
836 if (sh->CompileStatus) {
837 printf("GLSL IR for shader %d:\n", sh->Name);
838 _mesa_print_ir(sh->ir, NULL);
839 printf("\n\n");
840 } else {
841 printf("GLSL shader %d failed to compile.\n", sh->Name);
842 }
843 if (sh->InfoLog && sh->InfoLog[0] != 0) {
844 printf("GLSL shader %d info log:\n", sh->Name);
845 printf("%s\n", sh->InfoLog);
846 }
847 }
848
849 }
850
851 if (!sh->CompileStatus) {
852 if (ctx->Shader.Flags & GLSL_DUMP_ON_ERROR) {
853 fprintf(stderr, "GLSL source for %s shader %d:\n",
854 _mesa_shader_stage_to_string(sh->Stage), sh->Name);
855 fprintf(stderr, "%s\n", sh->Source);
856 fprintf(stderr, "Info Log:\n%s\n", sh->InfoLog);
857 fflush(stderr);
858 }
859
860 if (ctx->Shader.Flags & GLSL_REPORT_ERRORS) {
861 _mesa_debug(ctx, "Error compiling shader %u:\n%s\n",
862 sh->Name, sh->InfoLog);
863 }
864 }
865 }
866
867
868 /**
869 * Link a program's shaders.
870 */
871 static void
872 link_program(struct gl_context *ctx, GLuint program)
873 {
874 struct gl_shader_program *shProg;
875
876 shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
877 if (!shProg)
878 return;
879
880 /* From the ARB_transform_feedback2 specification:
881 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
882 * the name of a program being used by one or more transform feedback
883 * objects, even if the objects are not currently bound or are paused."
884 */
885 if (_mesa_transform_feedback_is_using_program(ctx, shProg)) {
886 _mesa_error(ctx, GL_INVALID_OPERATION,
887 "glLinkProgram(transform feedback is using the program)");
888 return;
889 }
890
891 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
892
893 _mesa_glsl_link_shader(ctx, shProg);
894
895 if (shProg->LinkStatus == GL_FALSE &&
896 (ctx->Shader.Flags & GLSL_REPORT_ERRORS)) {
897 _mesa_debug(ctx, "Error linking program %u:\n%s\n",
898 shProg->Name, shProg->InfoLog);
899 }
900
901 /* debug code */
902 if (0) {
903 GLuint i;
904
905 printf("Link %u shaders in program %u: %s\n",
906 shProg->NumShaders, shProg->Name,
907 shProg->LinkStatus ? "Success" : "Failed");
908
909 for (i = 0; i < shProg->NumShaders; i++) {
910 printf(" shader %u, type 0x%x\n",
911 shProg->Shaders[i]->Name,
912 shProg->Shaders[i]->Type);
913 }
914 }
915 }
916
917
918 /**
919 * Print basic shader info (for debug).
920 */
921 static void
922 print_shader_info(const struct gl_shader_program *shProg)
923 {
924 GLuint i;
925
926 printf("Mesa: glUseProgram(%u)\n", shProg->Name);
927 for (i = 0; i < shProg->NumShaders; i++) {
928 printf(" %s shader %u, checksum %u\n",
929 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
930 shProg->Shaders[i]->Name,
931 shProg->Shaders[i]->SourceChecksum);
932 }
933 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX])
934 printf(" vert prog %u\n",
935 shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
936 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT])
937 printf(" frag prog %u\n",
938 shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->Id);
939 if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
940 printf(" geom prog %u\n",
941 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
942 }
943
944
945 /**
946 * Use the named shader program for subsequent glUniform calls
947 */
948 void
949 _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
950 const char *caller)
951 {
952 if ((shProg != NULL) && !shProg->LinkStatus) {
953 _mesa_error(ctx, GL_INVALID_OPERATION,
954 "%s(program %u not linked)", caller, shProg->Name);
955 return;
956 }
957
958 if (ctx->Shader.ActiveProgram != shProg) {
959 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
960 }
961 }
962
963 /**
964 */
965 static void
966 use_shader_program(struct gl_context *ctx, GLenum type,
967 struct gl_shader_program *shProg)
968 {
969 struct gl_shader_program **target;
970 gl_shader_stage stage = _mesa_shader_enum_to_shader_stage(type);
971
972 target = &ctx->Shader.CurrentProgram[stage];
973 if ((shProg == NULL) || (shProg->_LinkedShaders[stage] == NULL))
974 shProg = NULL;
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_COMPUTE_SHADER:
991 /* Empty for now. */
992 break;
993 case GL_FRAGMENT_SHADER:
994 if (*target == ctx->Shader._CurrentFragmentProgram) {
995 _mesa_reference_shader_program(ctx,
996 &ctx->Shader._CurrentFragmentProgram,
997 NULL);
998 }
999 break;
1000 }
1001
1002 _mesa_reference_shader_program(ctx, target, shProg);
1003 return;
1004 }
1005 }
1006
1007 /**
1008 * Use the named shader program for subsequent rendering.
1009 */
1010 void
1011 _mesa_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
1012 {
1013 use_shader_program(ctx, GL_VERTEX_SHADER, shProg);
1014 use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB, shProg);
1015 use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg);
1016 use_shader_program(ctx, GL_COMPUTE_SHADER, shProg);
1017 _mesa_active_program(ctx, shProg, "glUseProgram");
1018
1019 if (ctx->Driver.UseProgram)
1020 ctx->Driver.UseProgram(ctx, shProg);
1021 }
1022
1023
1024 /**
1025 * Do validation of the given shader program.
1026 * \param errMsg returns error message if validation fails.
1027 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1028 */
1029 static GLboolean
1030 validate_shader_program(const struct gl_shader_program *shProg,
1031 char *errMsg)
1032 {
1033 if (!shProg->LinkStatus) {
1034 return GL_FALSE;
1035 }
1036
1037 /* From the GL spec, a program is invalid if any of these are true:
1038
1039 any two active samplers in the current program object are of
1040 different types, but refer to the same texture image unit,
1041
1042 any active sampler in the current program object refers to a texture
1043 image unit where fixed-function fragment processing accesses a
1044 texture target that does not match the sampler type, or
1045
1046 the sum of the number of active samplers in the program and the
1047 number of texture image units enabled for fixed-function fragment
1048 processing exceeds the combined limit on the total number of texture
1049 image units allowed.
1050 */
1051
1052
1053 /*
1054 * Check: any two active samplers in the current program object are of
1055 * different types, but refer to the same texture image unit,
1056 */
1057 if (!_mesa_sampler_uniforms_are_valid(shProg, errMsg, 100))
1058 return GL_FALSE;
1059
1060 return GL_TRUE;
1061 }
1062
1063
1064 /**
1065 * Called via glValidateProgram()
1066 */
1067 static void
1068 validate_program(struct gl_context *ctx, GLuint program)
1069 {
1070 struct gl_shader_program *shProg;
1071 char errMsg[100] = "";
1072
1073 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1074 if (!shProg) {
1075 return;
1076 }
1077
1078 shProg->Validated = validate_shader_program(shProg, errMsg);
1079 if (!shProg->Validated) {
1080 /* update info log */
1081 if (shProg->InfoLog) {
1082 ralloc_free(shProg->InfoLog);
1083 }
1084 shProg->InfoLog = ralloc_strdup(shProg, errMsg);
1085 }
1086 }
1087
1088
1089
1090 void GLAPIENTRY
1091 _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
1092 {
1093 GET_CURRENT_CONTEXT(ctx);
1094 attach_shader(ctx, program, shader);
1095 }
1096
1097
1098 void GLAPIENTRY
1099 _mesa_AttachShader(GLuint program, GLuint shader)
1100 {
1101 GET_CURRENT_CONTEXT(ctx);
1102 attach_shader(ctx, program, shader);
1103 }
1104
1105
1106 void GLAPIENTRY
1107 _mesa_CompileShader(GLhandleARB shaderObj)
1108 {
1109 GET_CURRENT_CONTEXT(ctx);
1110 if (MESA_VERBOSE & VERBOSE_API)
1111 _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
1112 compile_shader(ctx, shaderObj);
1113 }
1114
1115
1116 GLuint GLAPIENTRY
1117 _mesa_CreateShader(GLenum type)
1118 {
1119 GET_CURRENT_CONTEXT(ctx);
1120 if (MESA_VERBOSE & VERBOSE_API)
1121 _mesa_debug(ctx, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type));
1122 return create_shader(ctx, type);
1123 }
1124
1125
1126 GLhandleARB GLAPIENTRY
1127 _mesa_CreateShaderObjectARB(GLenum type)
1128 {
1129 GET_CURRENT_CONTEXT(ctx);
1130 return create_shader(ctx, type);
1131 }
1132
1133
1134 GLuint GLAPIENTRY
1135 _mesa_CreateProgram(void)
1136 {
1137 GET_CURRENT_CONTEXT(ctx);
1138 if (MESA_VERBOSE & VERBOSE_API)
1139 _mesa_debug(ctx, "glCreateProgram\n");
1140 return create_shader_program(ctx);
1141 }
1142
1143
1144 GLhandleARB GLAPIENTRY
1145 _mesa_CreateProgramObjectARB(void)
1146 {
1147 GET_CURRENT_CONTEXT(ctx);
1148 return create_shader_program(ctx);
1149 }
1150
1151
1152 void GLAPIENTRY
1153 _mesa_DeleteObjectARB(GLhandleARB obj)
1154 {
1155 if (MESA_VERBOSE & VERBOSE_API) {
1156 GET_CURRENT_CONTEXT(ctx);
1157 _mesa_debug(ctx, "glDeleteObjectARB(%u)\n", obj);
1158 }
1159
1160 if (obj) {
1161 GET_CURRENT_CONTEXT(ctx);
1162 FLUSH_VERTICES(ctx, 0);
1163 if (is_program(ctx, obj)) {
1164 delete_shader_program(ctx, obj);
1165 }
1166 else if (is_shader(ctx, obj)) {
1167 delete_shader(ctx, obj);
1168 }
1169 else {
1170 /* error? */
1171 }
1172 }
1173 }
1174
1175
1176 void GLAPIENTRY
1177 _mesa_DeleteProgram(GLuint name)
1178 {
1179 if (name) {
1180 GET_CURRENT_CONTEXT(ctx);
1181 FLUSH_VERTICES(ctx, 0);
1182 delete_shader_program(ctx, name);
1183 }
1184 }
1185
1186
1187 void GLAPIENTRY
1188 _mesa_DeleteShader(GLuint name)
1189 {
1190 if (name) {
1191 GET_CURRENT_CONTEXT(ctx);
1192 FLUSH_VERTICES(ctx, 0);
1193 delete_shader(ctx, name);
1194 }
1195 }
1196
1197
1198 void GLAPIENTRY
1199 _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
1200 {
1201 GET_CURRENT_CONTEXT(ctx);
1202 detach_shader(ctx, program, shader);
1203 }
1204
1205
1206 void GLAPIENTRY
1207 _mesa_DetachShader(GLuint program, GLuint shader)
1208 {
1209 GET_CURRENT_CONTEXT(ctx);
1210 detach_shader(ctx, program, shader);
1211 }
1212
1213
1214 void GLAPIENTRY
1215 _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
1216 GLsizei * count, GLhandleARB * obj)
1217 {
1218 GET_CURRENT_CONTEXT(ctx);
1219 get_attached_shaders(ctx, container, maxCount, count, obj);
1220 }
1221
1222
1223 void GLAPIENTRY
1224 _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
1225 GLsizei *count, GLuint *obj)
1226 {
1227 GET_CURRENT_CONTEXT(ctx);
1228 get_attached_shaders(ctx, program, maxCount, count, obj);
1229 }
1230
1231
1232 void GLAPIENTRY
1233 _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
1234 GLcharARB * infoLog)
1235 {
1236 GET_CURRENT_CONTEXT(ctx);
1237 if (is_program(ctx, object)) {
1238 get_program_info_log(ctx, object, maxLength, length, infoLog);
1239 }
1240 else if (is_shader(ctx, object)) {
1241 get_shader_info_log(ctx, object, maxLength, length, infoLog);
1242 }
1243 else {
1244 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
1245 }
1246 }
1247
1248
1249 void GLAPIENTRY
1250 _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
1251 {
1252 GET_CURRENT_CONTEXT(ctx);
1253 /* Implement in terms of GetProgramiv, GetShaderiv */
1254 if (is_program(ctx, object)) {
1255 if (pname == GL_OBJECT_TYPE_ARB) {
1256 *params = GL_PROGRAM_OBJECT_ARB;
1257 }
1258 else {
1259 get_programiv(ctx, object, pname, params);
1260 }
1261 }
1262 else if (is_shader(ctx, object)) {
1263 if (pname == GL_OBJECT_TYPE_ARB) {
1264 *params = GL_SHADER_OBJECT_ARB;
1265 }
1266 else {
1267 get_shaderiv(ctx, object, pname, params);
1268 }
1269 }
1270 else {
1271 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
1272 }
1273 }
1274
1275
1276 void GLAPIENTRY
1277 _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
1278 GLfloat *params)
1279 {
1280 GLint iparams[1]; /* XXX is one element enough? */
1281 _mesa_GetObjectParameterivARB(object, pname, iparams);
1282 params[0] = (GLfloat) iparams[0];
1283 }
1284
1285
1286 void GLAPIENTRY
1287 _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
1288 {
1289 GET_CURRENT_CONTEXT(ctx);
1290 get_programiv(ctx, program, pname, params);
1291 }
1292
1293
1294 void GLAPIENTRY
1295 _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
1296 {
1297 GET_CURRENT_CONTEXT(ctx);
1298 get_shaderiv(ctx, shader, pname, params);
1299 }
1300
1301
1302 void GLAPIENTRY
1303 _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
1304 GLsizei *length, GLchar *infoLog)
1305 {
1306 GET_CURRENT_CONTEXT(ctx);
1307 get_program_info_log(ctx, program, bufSize, length, infoLog);
1308 }
1309
1310
1311 void GLAPIENTRY
1312 _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
1313 GLsizei *length, GLchar *infoLog)
1314 {
1315 GET_CURRENT_CONTEXT(ctx);
1316 get_shader_info_log(ctx, shader, bufSize, length, infoLog);
1317 }
1318
1319
1320 void GLAPIENTRY
1321 _mesa_GetShaderSource(GLhandleARB shader, GLsizei maxLength,
1322 GLsizei *length, GLcharARB *sourceOut)
1323 {
1324 GET_CURRENT_CONTEXT(ctx);
1325 get_shader_source(ctx, shader, maxLength, length, sourceOut);
1326 }
1327
1328
1329 GLhandleARB GLAPIENTRY
1330 _mesa_GetHandleARB(GLenum pname)
1331 {
1332 GET_CURRENT_CONTEXT(ctx);
1333 return get_handle(ctx, pname);
1334 }
1335
1336
1337 GLboolean GLAPIENTRY
1338 _mesa_IsProgram(GLuint name)
1339 {
1340 GET_CURRENT_CONTEXT(ctx);
1341 return is_program(ctx, name);
1342 }
1343
1344
1345 GLboolean GLAPIENTRY
1346 _mesa_IsShader(GLuint name)
1347 {
1348 GET_CURRENT_CONTEXT(ctx);
1349 return is_shader(ctx, name);
1350 }
1351
1352
1353 void GLAPIENTRY
1354 _mesa_LinkProgram(GLhandleARB programObj)
1355 {
1356 GET_CURRENT_CONTEXT(ctx);
1357 link_program(ctx, programObj);
1358 }
1359
1360
1361
1362 /**
1363 * Read shader source code from a file.
1364 * Useful for debugging to override an app's shader.
1365 */
1366 static GLcharARB *
1367 read_shader(const char *fname)
1368 {
1369 const int max = 50*1000;
1370 FILE *f = fopen(fname, "r");
1371 GLcharARB *buffer, *shader;
1372 int len;
1373
1374 if (!f) {
1375 return NULL;
1376 }
1377
1378 buffer = malloc(max);
1379 len = fread(buffer, 1, max, f);
1380 buffer[len] = 0;
1381
1382 fclose(f);
1383
1384 shader = _mesa_strdup(buffer);
1385 free(buffer);
1386
1387 return shader;
1388 }
1389
1390
1391 /**
1392 * Called via glShaderSource() and glShaderSourceARB() API functions.
1393 * Basically, concatenate the source code strings into one long string
1394 * and pass it to _mesa_shader_source().
1395 */
1396 void GLAPIENTRY
1397 _mesa_ShaderSource(GLhandleARB shaderObj, GLsizei count,
1398 const GLcharARB * const * string, const GLint * length)
1399 {
1400 GET_CURRENT_CONTEXT(ctx);
1401 GLint *offsets;
1402 GLsizei i, totalLength;
1403 GLcharARB *source;
1404 GLuint checksum;
1405
1406 if (!shaderObj || string == NULL) {
1407 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
1408 return;
1409 }
1410
1411 /*
1412 * This array holds offsets of where the appropriate string ends, thus the
1413 * last element will be set to the total length of the source code.
1414 */
1415 offsets = malloc(count * sizeof(GLint));
1416 if (offsets == NULL) {
1417 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1418 return;
1419 }
1420
1421 for (i = 0; i < count; i++) {
1422 if (string[i] == NULL) {
1423 free((GLvoid *) offsets);
1424 _mesa_error(ctx, GL_INVALID_OPERATION,
1425 "glShaderSourceARB(null string)");
1426 return;
1427 }
1428 if (length == NULL || length[i] < 0)
1429 offsets[i] = strlen(string[i]);
1430 else
1431 offsets[i] = length[i];
1432 /* accumulate string lengths */
1433 if (i > 0)
1434 offsets[i] += offsets[i - 1];
1435 }
1436
1437 /* Total length of source string is sum off all strings plus two.
1438 * One extra byte for terminating zero, another extra byte to silence
1439 * valgrind warnings in the parser/grammer code.
1440 */
1441 totalLength = offsets[count - 1] + 2;
1442 source = malloc(totalLength * sizeof(GLcharARB));
1443 if (source == NULL) {
1444 free((GLvoid *) offsets);
1445 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1446 return;
1447 }
1448
1449 for (i = 0; i < count; i++) {
1450 GLint start = (i > 0) ? offsets[i - 1] : 0;
1451 memcpy(source + start, string[i],
1452 (offsets[i] - start) * sizeof(GLcharARB));
1453 }
1454 source[totalLength - 1] = '\0';
1455 source[totalLength - 2] = '\0';
1456
1457 if (SHADER_SUBST) {
1458 /* Compute the shader's source code checksum then try to open a file
1459 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1460 * original shader source code. For debugging.
1461 */
1462 char filename[100];
1463 GLcharARB *newSource;
1464
1465 checksum = _mesa_str_checksum(source);
1466
1467 _mesa_snprintf(filename, sizeof(filename), "newshader_%d", checksum);
1468
1469 newSource = read_shader(filename);
1470 if (newSource) {
1471 fprintf(stderr, "Mesa: Replacing shader %u chksum=%d with %s\n",
1472 shaderObj, checksum, filename);
1473 free(source);
1474 source = newSource;
1475 }
1476 }
1477
1478 shader_source(ctx, shaderObj, source);
1479
1480 if (SHADER_SUBST) {
1481 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
1482 if (sh)
1483 sh->SourceChecksum = checksum; /* save original checksum */
1484 }
1485
1486 free(offsets);
1487 }
1488
1489
1490 void GLAPIENTRY
1491 _mesa_UseProgram(GLhandleARB program)
1492 {
1493 GET_CURRENT_CONTEXT(ctx);
1494 struct gl_shader_program *shProg;
1495
1496 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
1497 _mesa_error(ctx, GL_INVALID_OPERATION,
1498 "glUseProgram(transform feedback active)");
1499 return;
1500 }
1501
1502 if (program) {
1503 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
1504 if (!shProg) {
1505 return;
1506 }
1507 if (!shProg->LinkStatus) {
1508 _mesa_error(ctx, GL_INVALID_OPERATION,
1509 "glUseProgram(program %u not linked)", program);
1510 return;
1511 }
1512
1513 /* debug code */
1514 if (ctx->Shader.Flags & GLSL_USE_PROG) {
1515 print_shader_info(shProg);
1516 }
1517 }
1518 else {
1519 shProg = NULL;
1520 }
1521
1522 _mesa_use_program(ctx, shProg);
1523 }
1524
1525
1526 void GLAPIENTRY
1527 _mesa_ValidateProgram(GLhandleARB program)
1528 {
1529 GET_CURRENT_CONTEXT(ctx);
1530 validate_program(ctx, program);
1531 }
1532
1533
1534 /**
1535 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1536 */
1537 void GLAPIENTRY
1538 _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
1539 GLint* range, GLint* precision)
1540 {
1541 const struct gl_program_constants *limits;
1542 const struct gl_precision *p;
1543 GET_CURRENT_CONTEXT(ctx);
1544
1545 switch (shadertype) {
1546 case GL_VERTEX_SHADER:
1547 limits = &ctx->Const.Program[MESA_SHADER_VERTEX];
1548 break;
1549 case GL_FRAGMENT_SHADER:
1550 limits = &ctx->Const.Program[MESA_SHADER_FRAGMENT];
1551 break;
1552 default:
1553 _mesa_error(ctx, GL_INVALID_ENUM,
1554 "glGetShaderPrecisionFormat(shadertype)");
1555 return;
1556 }
1557
1558 switch (precisiontype) {
1559 case GL_LOW_FLOAT:
1560 p = &limits->LowFloat;
1561 break;
1562 case GL_MEDIUM_FLOAT:
1563 p = &limits->MediumFloat;
1564 break;
1565 case GL_HIGH_FLOAT:
1566 p = &limits->HighFloat;
1567 break;
1568 case GL_LOW_INT:
1569 p = &limits->LowInt;
1570 break;
1571 case GL_MEDIUM_INT:
1572 p = &limits->MediumInt;
1573 break;
1574 case GL_HIGH_INT:
1575 p = &limits->HighInt;
1576 break;
1577 default:
1578 _mesa_error(ctx, GL_INVALID_ENUM,
1579 "glGetShaderPrecisionFormat(precisiontype)");
1580 return;
1581 }
1582
1583 range[0] = p->RangeMin;
1584 range[1] = p->RangeMax;
1585 precision[0] = p->Precision;
1586 }
1587
1588
1589 /**
1590 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1591 */
1592 void GLAPIENTRY
1593 _mesa_ReleaseShaderCompiler(void)
1594 {
1595 _mesa_destroy_shader_compiler_caches();
1596 }
1597
1598
1599 /**
1600 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1601 */
1602 void GLAPIENTRY
1603 _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
1604 const void* binary, GLint length)
1605 {
1606 GET_CURRENT_CONTEXT(ctx);
1607 (void) n;
1608 (void) shaders;
1609 (void) binaryformat;
1610 (void) binary;
1611 (void) length;
1612 _mesa_error(ctx, GL_INVALID_OPERATION, __FUNCTION__);
1613 }
1614
1615
1616 void GLAPIENTRY
1617 _mesa_GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei *length,
1618 GLenum *binaryFormat, GLvoid *binary)
1619 {
1620 struct gl_shader_program *shProg;
1621 GET_CURRENT_CONTEXT(ctx);
1622
1623 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramBinary");
1624 if (!shProg)
1625 return;
1626
1627 if (!shProg->LinkStatus) {
1628 _mesa_error(ctx, GL_INVALID_OPERATION,
1629 "glGetProgramBinary(program %u not linked)",
1630 shProg->Name);
1631 return;
1632 }
1633
1634 if (bufSize < 0){
1635 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramBinary(bufSize < 0)");
1636 return;
1637 }
1638
1639 /* The ARB_get_program_binary spec says:
1640 *
1641 * "If <length> is NULL, then no length is returned."
1642 */
1643 if (length != NULL)
1644 *length = 0;
1645
1646 (void) binaryFormat;
1647 (void) binary;
1648 }
1649
1650 void GLAPIENTRY
1651 _mesa_ProgramBinary(GLuint program, GLenum binaryFormat,
1652 const GLvoid *binary, GLsizei length)
1653 {
1654 struct gl_shader_program *shProg;
1655 GET_CURRENT_CONTEXT(ctx);
1656
1657 shProg = _mesa_lookup_shader_program_err(ctx, program, "glProgramBinary");
1658 if (!shProg)
1659 return;
1660
1661 (void) binaryFormat;
1662 (void) binary;
1663 (void) length;
1664 _mesa_error(ctx, GL_INVALID_OPERATION, __FUNCTION__);
1665 }
1666
1667
1668 void GLAPIENTRY
1669 _mesa_ProgramParameteri(GLuint program, GLenum pname, GLint value)
1670 {
1671 struct gl_shader_program *shProg;
1672 GET_CURRENT_CONTEXT(ctx);
1673
1674 shProg = _mesa_lookup_shader_program_err(ctx, program,
1675 "glProgramParameteri");
1676 if (!shProg)
1677 return;
1678
1679 switch (pname) {
1680 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
1681 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1682 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1683 * even be in the dispatch table, so we shouldn't need to expclicitly
1684 * check here.
1685 *
1686 * On desktop, we ignore the 3.0+ requirement because it is silly.
1687 */
1688
1689 /* The ARB_get_program_binary extension spec says:
1690 *
1691 * "An INVALID_VALUE error is generated if the <value> argument to
1692 * ProgramParameteri is not TRUE or FALSE."
1693 */
1694 if (value != GL_TRUE && value != GL_FALSE) {
1695 _mesa_error(ctx, GL_INVALID_VALUE,
1696 "glProgramParameteri(pname=%s, value=%d): "
1697 "value must be 0 or 1.",
1698 _mesa_lookup_enum_by_nr(pname),
1699 value);
1700 return;
1701 }
1702
1703 /* No need to notify the driver. Any changes will actually take effect
1704 * the next time the shader is linked.
1705 *
1706 * The ARB_get_program_binary extension spec says:
1707 *
1708 * "To indicate that a program binary is likely to be retrieved,
1709 * ProgramParameteri should be called with <pname>
1710 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1711 * will not be in effect until the next time LinkProgram or
1712 * ProgramBinary has been called successfully."
1713 *
1714 * The resloution of issue 9 in the extension spec also says:
1715 *
1716 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1717 * to indicate to the GL implementation that this program will
1718 * likely be saved with GetProgramBinary at some point. This will
1719 * give the GL implementation the opportunity to track any state
1720 * changes made to the program before being saved such that when it
1721 * is loaded again a recompile can be avoided."
1722 */
1723 shProg->BinaryRetreivableHint = value;
1724 return;
1725 default:
1726 break;
1727 }
1728
1729 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteri(pname=%s)",
1730 _mesa_lookup_enum_by_nr(pname));
1731 }
1732
1733 void
1734 _mesa_use_shader_program(struct gl_context *ctx, GLenum type,
1735 struct gl_shader_program *shProg)
1736 {
1737 use_shader_program(ctx, type, shProg);
1738
1739 if (ctx->Driver.UseProgram)
1740 ctx->Driver.UseProgram(ctx, shProg);
1741 }
1742
1743
1744 /**
1745 * For GL_EXT_separate_shader_objects
1746 */
1747 void GLAPIENTRY
1748 _mesa_UseShaderProgramEXT(GLenum type, GLuint program)
1749 {
1750 GET_CURRENT_CONTEXT(ctx);
1751 struct gl_shader_program *shProg = NULL;
1752
1753 if (!_mesa_validate_shader_target(ctx, type)) {
1754 _mesa_error(ctx, GL_INVALID_ENUM, "glUseShaderProgramEXT(type)");
1755 return;
1756 }
1757
1758 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
1759 _mesa_error(ctx, GL_INVALID_OPERATION,
1760 "glUseShaderProgramEXT(transform feedback is active)");
1761 return;
1762 }
1763
1764 if (program) {
1765 shProg = _mesa_lookup_shader_program_err(ctx, program,
1766 "glUseShaderProgramEXT");
1767 if (shProg == NULL)
1768 return;
1769
1770 if (!shProg->LinkStatus) {
1771 _mesa_error(ctx, GL_INVALID_OPERATION,
1772 "glUseShaderProgramEXT(program not linked)");
1773 return;
1774 }
1775 }
1776
1777 _mesa_use_shader_program(ctx, type, shProg);
1778 }
1779
1780
1781 /**
1782 * For GL_EXT_separate_shader_objects
1783 */
1784 void GLAPIENTRY
1785 _mesa_ActiveProgramEXT(GLuint program)
1786 {
1787 GET_CURRENT_CONTEXT(ctx);
1788 struct gl_shader_program *shProg = (program != 0)
1789 ? _mesa_lookup_shader_program_err(ctx, program, "glActiveProgramEXT")
1790 : NULL;
1791
1792 _mesa_active_program(ctx, shProg, "glActiveProgramEXT");
1793 return;
1794 }
1795
1796
1797 /**
1798 * For GL_EXT_separate_shader_objects
1799 */
1800 GLuint GLAPIENTRY
1801 _mesa_CreateShaderProgramEXT(GLenum type, const GLchar *string)
1802 {
1803 GET_CURRENT_CONTEXT(ctx);
1804 const GLuint shader = create_shader(ctx, type);
1805 GLuint program = 0;
1806
1807 if (shader) {
1808 shader_source(ctx, shader, _mesa_strdup(string));
1809 compile_shader(ctx, shader);
1810
1811 program = create_shader_program(ctx);
1812 if (program) {
1813 struct gl_shader_program *shProg;
1814 struct gl_shader *sh;
1815 GLint compiled = GL_FALSE;
1816
1817 shProg = _mesa_lookup_shader_program(ctx, program);
1818 sh = _mesa_lookup_shader(ctx, shader);
1819
1820 get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
1821 if (compiled) {
1822 attach_shader(ctx, program, shader);
1823 link_program(ctx, program);
1824 detach_shader(ctx, program, shader);
1825
1826 #if 0
1827 /* Possibly... */
1828 if (active-user-defined-varyings-in-linked-program) {
1829 append-error-to-info-log;
1830 shProg->LinkStatus = GL_FALSE;
1831 }
1832 #endif
1833 }
1834
1835 ralloc_strcat(&shProg->InfoLog, sh->InfoLog);
1836 }
1837
1838 delete_shader(ctx, shader);
1839 }
1840
1841 return program;
1842 }
1843
1844
1845 /**
1846 * Copy program-specific data generated by linking from the gl_shader_program
1847 * object to a specific gl_program object.
1848 */
1849 void
1850 _mesa_copy_linked_program_data(gl_shader_stage type,
1851 const struct gl_shader_program *src,
1852 struct gl_program *dst)
1853 {
1854 switch (type) {
1855 case MESA_SHADER_VERTEX:
1856 dst->UsesClipDistanceOut = src->Vert.UsesClipDistance;
1857 break;
1858 case MESA_SHADER_GEOMETRY: {
1859 struct gl_geometry_program *dst_gp = (struct gl_geometry_program *) dst;
1860 dst_gp->VerticesIn = src->Geom.VerticesIn;
1861 dst_gp->VerticesOut = src->Geom.VerticesOut;
1862 dst_gp->Invocations = src->Geom.Invocations;
1863 dst_gp->InputType = src->Geom.InputType;
1864 dst_gp->OutputType = src->Geom.OutputType;
1865 dst->UsesClipDistanceOut = src->Geom.UsesClipDistance;
1866 dst_gp->UsesEndPrimitive = src->Geom.UsesEndPrimitive;
1867 }
1868 break;
1869 case MESA_SHADER_COMPUTE: {
1870 struct gl_compute_program *dst_cp = (struct gl_compute_program *) dst;
1871 int i;
1872 for (i = 0; i < 3; i++)
1873 dst_cp->LocalSize[i] = src->Comp.LocalSize[i];
1874 }
1875 break;
1876 default:
1877 break;
1878 }
1879 }