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