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