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