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