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