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