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