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