mesa: stop trying new filenames if the filename existing is not the issue
[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 <errno.h>
41 #include <stdbool.h>
42 #include <c99_alloca.h>
43 #include "main/glheader.h"
44 #include "main/context.h"
45 #include "main/enums.h"
46 #include "main/glspirv.h"
47 #include "main/hash.h"
48 #include "main/mtypes.h"
49 #include "main/pipelineobj.h"
50 #include "main/program_binary.h"
51 #include "main/shaderapi.h"
52 #include "main/shaderobj.h"
53 #include "main/state.h"
54 #include "main/transformfeedback.h"
55 #include "main/uniforms.h"
56 #include "compiler/glsl/glsl_parser_extras.h"
57 #include "compiler/glsl/ir.h"
58 #include "compiler/glsl/ir_uniform.h"
59 #include "compiler/glsl/program.h"
60 #include "program/program.h"
61 #include "program/prog_print.h"
62 #include "program/prog_parameter.h"
63 #include "util/ralloc.h"
64 #include "util/hash_table.h"
65 #include "util/mesa-sha1.h"
66 #include "util/crc32.h"
67 #include "util/os_file.h"
68
69 /**
70 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
71 */
72 GLbitfield
73 _mesa_get_shader_flags(void)
74 {
75 GLbitfield flags = 0x0;
76 const char *env = getenv("MESA_GLSL");
77
78 if (env) {
79 if (strstr(env, "dump_on_error"))
80 flags |= GLSL_DUMP_ON_ERROR;
81 else if (strstr(env, "dump"))
82 flags |= GLSL_DUMP;
83 if (strstr(env, "log"))
84 flags |= GLSL_LOG;
85 if (strstr(env, "cache_fb"))
86 flags |= GLSL_CACHE_FALLBACK;
87 if (strstr(env, "cache_info"))
88 flags |= GLSL_CACHE_INFO;
89 if (strstr(env, "nopvert"))
90 flags |= GLSL_NOP_VERT;
91 if (strstr(env, "nopfrag"))
92 flags |= GLSL_NOP_FRAG;
93 if (strstr(env, "uniform"))
94 flags |= GLSL_UNIFORMS;
95 if (strstr(env, "useprog"))
96 flags |= GLSL_USE_PROG;
97 if (strstr(env, "errors"))
98 flags |= GLSL_REPORT_ERRORS;
99 }
100
101 return flags;
102 }
103
104 /**
105 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
106 */
107 const char *
108 _mesa_get_shader_capture_path(void)
109 {
110 static bool read_env_var = false;
111 static const char *path = NULL;
112
113 if (!read_env_var) {
114 path = getenv("MESA_SHADER_CAPTURE_PATH");
115 read_env_var = true;
116 }
117
118 return path;
119 }
120
121 /**
122 * Initialize context's shader state.
123 */
124 void
125 _mesa_init_shader_state(struct gl_context *ctx)
126 {
127 /* Device drivers may override these to control what kind of instructions
128 * are generated by the GLSL compiler.
129 */
130 struct gl_shader_compiler_options options;
131 gl_shader_stage sh;
132 int i;
133
134 memset(&options, 0, sizeof(options));
135 options.MaxUnrollIterations = 32;
136 options.MaxIfDepth = UINT_MAX;
137
138 for (sh = 0; sh < MESA_SHADER_STAGES; ++sh)
139 memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
140
141 ctx->Shader.Flags = _mesa_get_shader_flags();
142
143 if (ctx->Shader.Flags != 0)
144 ctx->Const.GenerateTemporaryNames = true;
145
146 /* Extended for ARB_separate_shader_objects */
147 ctx->Shader.RefCount = 1;
148 ctx->TessCtrlProgram.patch_vertices = 3;
149 for (i = 0; i < 4; ++i)
150 ctx->TessCtrlProgram.patch_default_outer_level[i] = 1.0;
151 for (i = 0; i < 2; ++i)
152 ctx->TessCtrlProgram.patch_default_inner_level[i] = 1.0;
153 }
154
155
156 /**
157 * Free the per-context shader-related state.
158 */
159 void
160 _mesa_free_shader_state(struct gl_context *ctx)
161 {
162 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
163 _mesa_reference_program(ctx, &ctx->Shader.CurrentProgram[i], NULL);
164 _mesa_reference_shader_program(ctx,
165 &ctx->Shader.ReferencedPrograms[i],
166 NULL);
167 }
168 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, NULL);
169
170 /* Extended for ARB_separate_shader_objects */
171 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
172
173 assert(ctx->Shader.RefCount == 1);
174 }
175
176
177 /**
178 * Copy string from <src> to <dst>, up to maxLength characters, returning
179 * length of <dst> in <length>.
180 * \param src the strings source
181 * \param maxLength max chars to copy
182 * \param length returns number of chars copied
183 * \param dst the string destination
184 */
185 void
186 _mesa_copy_string(GLchar *dst, GLsizei maxLength,
187 GLsizei *length, const GLchar *src)
188 {
189 GLsizei len;
190 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
191 dst[len] = src[len];
192 if (maxLength > 0)
193 dst[len] = 0;
194 if (length)
195 *length = len;
196 }
197
198
199
200 /**
201 * Confirm that the a shader type is valid and supported by the implementation
202 *
203 * \param ctx Current GL context
204 * \param type Shader target
205 *
206 */
207 bool
208 _mesa_validate_shader_target(const struct gl_context *ctx, GLenum type)
209 {
210 /* Note: when building built-in GLSL functions, this function may be
211 * invoked with ctx == NULL. In that case, we can only validate that it's
212 * a shader target we recognize, not that it's supported in the current
213 * context. But that's fine--we don't need any further validation than
214 * that when building built-in GLSL functions.
215 */
216
217 switch (type) {
218 case GL_FRAGMENT_SHADER:
219 return ctx == NULL || ctx->Extensions.ARB_fragment_shader;
220 case GL_VERTEX_SHADER:
221 return ctx == NULL || ctx->Extensions.ARB_vertex_shader;
222 case GL_GEOMETRY_SHADER_ARB:
223 return ctx == NULL || _mesa_has_geometry_shaders(ctx);
224 case GL_TESS_CONTROL_SHADER:
225 case GL_TESS_EVALUATION_SHADER:
226 return ctx == NULL || _mesa_has_tessellation(ctx);
227 case GL_COMPUTE_SHADER:
228 return ctx == NULL || _mesa_has_compute_shaders(ctx);
229 default:
230 return false;
231 }
232 }
233
234
235 static GLboolean
236 is_program(struct gl_context *ctx, GLuint name)
237 {
238 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
239 return shProg ? GL_TRUE : GL_FALSE;
240 }
241
242
243 static GLboolean
244 is_shader(struct gl_context *ctx, GLuint name)
245 {
246 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
247 return shader ? GL_TRUE : GL_FALSE;
248 }
249
250
251 /**
252 * Attach shader to a shader program.
253 */
254 static void
255 attach_shader(struct gl_context *ctx, struct gl_shader_program *shProg,
256 struct gl_shader *sh)
257 {
258 GLuint n = shProg->NumShaders;
259
260 shProg->Shaders = realloc(shProg->Shaders,
261 (n + 1) * sizeof(struct gl_shader *));
262 if (!shProg->Shaders) {
263 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
264 return;
265 }
266
267 /* append */
268 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
269 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
270 shProg->NumShaders++;
271 }
272
273 static void
274 attach_shader_err(struct gl_context *ctx, GLuint program, GLuint shader,
275 const char *caller)
276 {
277 struct gl_shader_program *shProg;
278 struct gl_shader *sh;
279 GLuint i, n;
280
281 const bool same_type_disallowed = _mesa_is_gles(ctx);
282
283 shProg = _mesa_lookup_shader_program_err(ctx, program, caller);
284 if (!shProg)
285 return;
286
287 sh = _mesa_lookup_shader_err(ctx, shader, caller);
288 if (!sh) {
289 return;
290 }
291
292 n = shProg->NumShaders;
293 for (i = 0; i < n; i++) {
294 if (shProg->Shaders[i] == sh) {
295 /* The shader is already attched to this program. The
296 * GL_ARB_shader_objects spec says:
297 *
298 * "The error INVALID_OPERATION is generated by AttachObjectARB
299 * if <obj> is already attached to <containerObj>."
300 */
301 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
302 return;
303 } else if (same_type_disallowed &&
304 shProg->Shaders[i]->Stage == sh->Stage) {
305 /* Shader with the same type is already attached to this program,
306 * OpenGL ES 2.0 and 3.0 specs say:
307 *
308 * "Multiple shader objects of the same type may not be attached
309 * to a single program object. [...] The error INVALID_OPERATION
310 * is generated if [...] another shader object of the same type
311 * as shader is already attached to program."
312 */
313 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
314 return;
315 }
316 }
317
318 attach_shader(ctx, shProg, sh);
319 }
320
321 static void
322 attach_shader_no_error(struct gl_context *ctx, GLuint program, GLuint shader)
323 {
324 struct gl_shader_program *shProg;
325 struct gl_shader *sh;
326
327 shProg = _mesa_lookup_shader_program(ctx, program);
328 sh = _mesa_lookup_shader(ctx, shader);
329
330 attach_shader(ctx, shProg, sh);
331 }
332
333 static GLuint
334 create_shader(struct gl_context *ctx, GLenum type)
335 {
336 struct gl_shader *sh;
337 GLuint name;
338
339 _mesa_HashLockMutex(ctx->Shared->ShaderObjects);
340 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
341 sh = _mesa_new_shader(name, _mesa_shader_enum_to_shader_stage(type));
342 sh->Type = type;
343 _mesa_HashInsertLocked(ctx->Shared->ShaderObjects, name, sh);
344 _mesa_HashUnlockMutex(ctx->Shared->ShaderObjects);
345
346 return name;
347 }
348
349
350 static GLuint
351 create_shader_err(struct gl_context *ctx, GLenum type, const char *caller)
352 {
353 if (!_mesa_validate_shader_target(ctx, type)) {
354 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s)",
355 caller, _mesa_enum_to_string(type));
356 return 0;
357 }
358
359 return create_shader(ctx, type);
360 }
361
362
363 static GLuint
364 create_shader_program(struct gl_context *ctx)
365 {
366 GLuint name;
367 struct gl_shader_program *shProg;
368
369 _mesa_HashLockMutex(ctx->Shared->ShaderObjects);
370
371 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
372
373 shProg = _mesa_new_shader_program(name);
374
375 _mesa_HashInsertLocked(ctx->Shared->ShaderObjects, name, shProg);
376
377 assert(shProg->RefCount == 1);
378
379 _mesa_HashUnlockMutex(ctx->Shared->ShaderObjects);
380
381 return name;
382 }
383
384
385 /**
386 * Delete a shader program. Actually, just decrement the program's
387 * reference count and mark it as DeletePending.
388 * Used to implement glDeleteProgram() and glDeleteObjectARB().
389 */
390 static void
391 delete_shader_program(struct gl_context *ctx, GLuint name)
392 {
393 /*
394 * NOTE: deleting shaders/programs works a bit differently than
395 * texture objects (and buffer objects, etc). Shader/program
396 * handles/IDs exist in the hash table until the object is really
397 * deleted (refcount==0). With texture objects, the handle/ID is
398 * removed from the hash table in glDeleteTextures() while the tex
399 * object itself might linger until its refcount goes to zero.
400 */
401 struct gl_shader_program *shProg;
402
403 shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
404 if (!shProg)
405 return;
406
407 if (!shProg->DeletePending) {
408 shProg->DeletePending = GL_TRUE;
409
410 /* effectively, decr shProg's refcount */
411 _mesa_reference_shader_program(ctx, &shProg, NULL);
412 }
413 }
414
415
416 static void
417 delete_shader(struct gl_context *ctx, GLuint shader)
418 {
419 struct gl_shader *sh;
420
421 sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
422 if (!sh)
423 return;
424
425 if (!sh->DeletePending) {
426 sh->DeletePending = GL_TRUE;
427
428 /* effectively, decr sh's refcount */
429 _mesa_reference_shader(ctx, &sh, NULL);
430 }
431 }
432
433
434 static ALWAYS_INLINE void
435 detach_shader(struct gl_context *ctx, GLuint program, GLuint shader,
436 bool no_error)
437 {
438 struct gl_shader_program *shProg;
439 GLuint n;
440 GLuint i, j;
441
442 if (!no_error) {
443 shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
444 if (!shProg)
445 return;
446 } else {
447 shProg = _mesa_lookup_shader_program(ctx, program);
448 }
449
450 n = shProg->NumShaders;
451
452 for (i = 0; i < n; i++) {
453 if (shProg->Shaders[i]->Name == shader) {
454 /* found it */
455 struct gl_shader **newList;
456
457 /* release */
458 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
459
460 /* alloc new, smaller array */
461 newList = malloc((n - 1) * sizeof(struct gl_shader *));
462 if (!newList) {
463 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
464 return;
465 }
466 /* Copy old list entries to new list, skipping removed entry at [i] */
467 for (j = 0; j < i; j++) {
468 newList[j] = shProg->Shaders[j];
469 }
470 while (++i < n) {
471 newList[j++] = shProg->Shaders[i];
472 }
473
474 /* Free old list and install new one */
475 free(shProg->Shaders);
476 shProg->Shaders = newList;
477 shProg->NumShaders = n - 1;
478
479 #ifndef NDEBUG
480 /* sanity check - make sure the new list's entries are sensible */
481 for (j = 0; j < shProg->NumShaders; j++) {
482 assert(shProg->Shaders[j]->Stage == MESA_SHADER_VERTEX ||
483 shProg->Shaders[j]->Stage == MESA_SHADER_TESS_CTRL ||
484 shProg->Shaders[j]->Stage == MESA_SHADER_TESS_EVAL ||
485 shProg->Shaders[j]->Stage == MESA_SHADER_GEOMETRY ||
486 shProg->Shaders[j]->Stage == MESA_SHADER_FRAGMENT);
487 assert(shProg->Shaders[j]->RefCount > 0);
488 }
489 #endif
490
491 return;
492 }
493 }
494
495 /* not found */
496 if (!no_error) {
497 GLenum err;
498 if (is_shader(ctx, shader) || is_program(ctx, shader))
499 err = GL_INVALID_OPERATION;
500 else
501 err = GL_INVALID_VALUE;
502 _mesa_error(ctx, err, "glDetachShader(shader)");
503 return;
504 }
505 }
506
507
508 static void
509 detach_shader_error(struct gl_context *ctx, GLuint program, GLuint shader)
510 {
511 detach_shader(ctx, program, shader, false);
512 }
513
514
515 static void
516 detach_shader_no_error(struct gl_context *ctx, GLuint program, GLuint shader)
517 {
518 detach_shader(ctx, program, shader, true);
519 }
520
521
522 /**
523 * Return list of shaders attached to shader program.
524 * \param objOut returns GLuint ids
525 * \param handleOut returns GLhandleARB handles
526 */
527 static void
528 get_attached_shaders(struct gl_context *ctx, GLuint program, GLsizei maxCount,
529 GLsizei *countOut, GLuint *objOut, GLhandleARB *handleOut)
530 {
531 struct gl_shader_program *shProg;
532
533 if (maxCount < 0) {
534 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders(maxCount < 0)");
535 return;
536 }
537
538 shProg =
539 _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
540
541 if (shProg) {
542 GLuint i;
543 for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
544 if (objOut) {
545 objOut[i] = shProg->Shaders[i]->Name;
546 }
547
548 if (handleOut) {
549 handleOut[i] = (GLhandleARB) shProg->Shaders[i]->Name;
550 }
551 }
552 if (countOut) {
553 *countOut = i;
554 }
555 }
556 }
557
558 /**
559 * glGetHandleARB() - return ID/name of currently bound shader program.
560 */
561 static GLuint
562 get_handle(struct gl_context *ctx, GLenum pname)
563 {
564 if (pname == GL_PROGRAM_OBJECT_ARB) {
565 if (ctx->_Shader->ActiveProgram)
566 return ctx->_Shader->ActiveProgram->Name;
567 else
568 return 0;
569 }
570 else {
571 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
572 return 0;
573 }
574 }
575
576
577 /**
578 * Check if a geometry shader query is valid at this time. If not, report an
579 * error and return false.
580 *
581 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
582 *
583 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
584 * are queried for a program which has not been linked successfully, or
585 * which does not contain objects to form a geometry shader, then an
586 * INVALID_OPERATION error is generated."
587 */
588 static bool
589 check_gs_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
590 {
591 if (shProg->data->LinkStatus &&
592 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
593 return true;
594 }
595
596 _mesa_error(ctx, GL_INVALID_OPERATION,
597 "glGetProgramv(linked geometry shader required)");
598 return false;
599 }
600
601
602 /**
603 * Check if a tessellation control shader query is valid at this time.
604 * If not, report an error and return false.
605 *
606 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
607 *
608 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
609 * not been linked successfully, or which does not contain objects to
610 * form a tessellation control shader, then an INVALID_OPERATION error is
611 * generated."
612 */
613 static bool
614 check_tcs_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
615 {
616 if (shProg->data->LinkStatus &&
617 shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL] != NULL) {
618 return true;
619 }
620
621 _mesa_error(ctx, GL_INVALID_OPERATION,
622 "glGetProgramv(linked tessellation control shader required)");
623 return false;
624 }
625
626
627 /**
628 * Check if a tessellation evaluation shader query is valid at this time.
629 * If not, report an error and return false.
630 *
631 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
632 *
633 * "If any of the pname values in this paragraph are queried for a program
634 * which has not been linked successfully, or which does not contain
635 * objects to form a tessellation evaluation shader, then an
636 * INVALID_OPERATION error is generated."
637 *
638 */
639 static bool
640 check_tes_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
641 {
642 if (shProg->data->LinkStatus &&
643 shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL] != NULL) {
644 return true;
645 }
646
647 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramv(linked tessellation "
648 "evaluation shader required)");
649 return false;
650 }
651
652
653 /**
654 * glGetProgramiv() - get shader program state.
655 * Note that this is for GLSL shader programs, not ARB vertex/fragment
656 * programs (see glGetProgramivARB).
657 */
658 static void
659 get_programiv(struct gl_context *ctx, GLuint program, GLenum pname,
660 GLint *params)
661 {
662 struct gl_shader_program *shProg
663 = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramiv(program)");
664
665 /* Is transform feedback available in this context?
666 */
667 const bool has_xfb =
668 (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.EXT_transform_feedback)
669 || ctx->API == API_OPENGL_CORE
670 || _mesa_is_gles3(ctx);
671
672 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
673 * and GL 3.2) are available in this context
674 */
675 const bool has_gs = _mesa_has_geometry_shaders(ctx);
676 const bool has_tess = _mesa_has_tessellation(ctx);
677
678 /* Are uniform buffer objects available in this context?
679 */
680 const bool has_ubo =
681 (ctx->API == API_OPENGL_COMPAT &&
682 ctx->Extensions.ARB_uniform_buffer_object)
683 || ctx->API == API_OPENGL_CORE
684 || _mesa_is_gles3(ctx);
685
686 if (!shProg) {
687 return;
688 }
689
690 switch (pname) {
691 case GL_DELETE_STATUS:
692 *params = shProg->DeletePending;
693 return;
694 case GL_COMPLETION_STATUS_ARB:
695 if (ctx->Driver.GetShaderProgramCompletionStatus)
696 *params = ctx->Driver.GetShaderProgramCompletionStatus(ctx, shProg);
697 else
698 *params = GL_TRUE;
699 return;
700 case GL_LINK_STATUS:
701 *params = shProg->data->LinkStatus ? GL_TRUE : GL_FALSE;
702 return;
703 case GL_VALIDATE_STATUS:
704 *params = shProg->data->Validated;
705 return;
706 case GL_INFO_LOG_LENGTH:
707 *params = (shProg->data->InfoLog && shProg->data->InfoLog[0] != '\0') ?
708 strlen(shProg->data->InfoLog) + 1 : 0;
709 return;
710 case GL_ATTACHED_SHADERS:
711 *params = shProg->NumShaders;
712 return;
713 case GL_ACTIVE_ATTRIBUTES:
714 *params = _mesa_count_active_attribs(shProg);
715 return;
716 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
717 *params = _mesa_longest_attribute_name_length(shProg);
718 return;
719 case GL_ACTIVE_UNIFORMS: {
720 unsigned i;
721 const unsigned num_uniforms =
722 shProg->data->NumUniformStorage - shProg->data->NumHiddenUniforms;
723 for (*params = 0, i = 0; i < num_uniforms; i++) {
724 if (!shProg->data->UniformStorage[i].is_shader_storage)
725 (*params)++;
726 }
727 return;
728 }
729 case GL_ACTIVE_UNIFORM_MAX_LENGTH: {
730 unsigned i;
731 GLint max_len = 0;
732 const unsigned num_uniforms =
733 shProg->data->NumUniformStorage - shProg->data->NumHiddenUniforms;
734
735 for (i = 0; i < num_uniforms; i++) {
736 if (shProg->data->UniformStorage[i].is_shader_storage)
737 continue;
738
739 /* Add one for the terminating NUL character for a non-array, and
740 * 4 for the "[0]" and the NUL for an array.
741 */
742 const GLint len = strlen(shProg->data->UniformStorage[i].name) + 1 +
743 ((shProg->data->UniformStorage[i].array_elements != 0) ? 3 : 0);
744
745 if (len > max_len)
746 max_len = len;
747 }
748
749 *params = max_len;
750 return;
751 }
752 case GL_TRANSFORM_FEEDBACK_VARYINGS:
753 if (!has_xfb)
754 break;
755 *params = shProg->TransformFeedback.NumVarying;
756 return;
757 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH: {
758 unsigned i;
759 GLint max_len = 0;
760 if (!has_xfb)
761 break;
762
763 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
764 /* Add one for the terminating NUL character.
765 */
766 const GLint len =
767 strlen(shProg->TransformFeedback.VaryingNames[i]) + 1;
768
769 if (len > max_len)
770 max_len = len;
771 }
772
773 *params = max_len;
774 return;
775 }
776 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
777 if (!has_xfb)
778 break;
779 *params = shProg->TransformFeedback.BufferMode;
780 return;
781 case GL_GEOMETRY_VERTICES_OUT:
782 if (!has_gs)
783 break;
784 if (check_gs_query(ctx, shProg)) {
785 *params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
786 Program->info.gs.vertices_out;
787 }
788 return;
789 case GL_GEOMETRY_SHADER_INVOCATIONS:
790 if (!has_gs || !ctx->Extensions.ARB_gpu_shader5)
791 break;
792 if (check_gs_query(ctx, shProg)) {
793 *params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
794 Program->info.gs.invocations;
795 }
796 return;
797 case GL_GEOMETRY_INPUT_TYPE:
798 if (!has_gs)
799 break;
800 if (check_gs_query(ctx, shProg)) {
801 *params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
802 Program->info.gs.input_primitive;
803 }
804 return;
805 case GL_GEOMETRY_OUTPUT_TYPE:
806 if (!has_gs)
807 break;
808 if (check_gs_query(ctx, shProg)) {
809 *params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
810 Program->info.gs.output_primitive;
811 }
812 return;
813 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH: {
814 unsigned i;
815 GLint max_len = 0;
816
817 if (!has_ubo)
818 break;
819
820 for (i = 0; i < shProg->data->NumUniformBlocks; i++) {
821 /* Add one for the terminating NUL character.
822 */
823 const GLint len = strlen(shProg->data->UniformBlocks[i].Name) + 1;
824
825 if (len > max_len)
826 max_len = len;
827 }
828
829 *params = max_len;
830 return;
831 }
832 case GL_ACTIVE_UNIFORM_BLOCKS:
833 if (!has_ubo)
834 break;
835
836 *params = shProg->data->NumUniformBlocks;
837 return;
838 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
839 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
840 * only available with desktop OpenGL 3.0+ with the
841 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
842 *
843 * On desktop, we ignore the 3.0+ requirement because it is silly.
844 */
845 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
846 break;
847
848 *params = shProg->BinaryRetrievableHint;
849 return;
850 case GL_PROGRAM_BINARY_LENGTH:
851 if (ctx->Const.NumProgramBinaryFormats == 0 || !shProg->data->LinkStatus) {
852 *params = 0;
853 } else {
854 _mesa_get_program_binary_length(ctx, shProg, params);
855 }
856 return;
857 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
858 if (!ctx->Extensions.ARB_shader_atomic_counters)
859 break;
860
861 *params = shProg->data->NumAtomicBuffers;
862 return;
863 case GL_COMPUTE_WORK_GROUP_SIZE: {
864 int i;
865 if (!_mesa_has_compute_shaders(ctx))
866 break;
867 if (!shProg->data->LinkStatus) {
868 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(program not "
869 "linked)");
870 return;
871 }
872 if (shProg->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
873 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(no compute "
874 "shaders)");
875 return;
876 }
877 for (i = 0; i < 3; i++)
878 params[i] = shProg->_LinkedShaders[MESA_SHADER_COMPUTE]->
879 Program->info.cs.local_size[i];
880 return;
881 }
882 case GL_PROGRAM_SEPARABLE:
883 /* If the program has not been linked, return initial value 0. */
884 *params = (shProg->data->LinkStatus == LINKING_FAILURE) ? 0 : shProg->SeparateShader;
885 return;
886
887 /* ARB_tessellation_shader */
888 case GL_TESS_CONTROL_OUTPUT_VERTICES:
889 if (!has_tess)
890 break;
891 if (check_tcs_query(ctx, shProg)) {
892 *params = shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL]->
893 Program->info.tess.tcs_vertices_out;
894 }
895 return;
896 case GL_TESS_GEN_MODE:
897 if (!has_tess)
898 break;
899 if (check_tes_query(ctx, shProg)) {
900 *params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
901 Program->info.tess.primitive_mode;
902 }
903 return;
904 case GL_TESS_GEN_SPACING:
905 if (!has_tess)
906 break;
907 if (check_tes_query(ctx, shProg)) {
908 const struct gl_linked_shader *tes =
909 shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL];
910 switch (tes->Program->info.tess.spacing) {
911 case TESS_SPACING_EQUAL:
912 *params = GL_EQUAL;
913 break;
914 case TESS_SPACING_FRACTIONAL_ODD:
915 *params = GL_FRACTIONAL_ODD;
916 break;
917 case TESS_SPACING_FRACTIONAL_EVEN:
918 *params = GL_FRACTIONAL_EVEN;
919 break;
920 case TESS_SPACING_UNSPECIFIED:
921 *params = 0;
922 break;
923 }
924 }
925 return;
926 case GL_TESS_GEN_VERTEX_ORDER:
927 if (!has_tess)
928 break;
929 if (check_tes_query(ctx, shProg)) {
930 *params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
931 Program->info.tess.ccw ? GL_CCW : GL_CW;
932 }
933 return;
934 case GL_TESS_GEN_POINT_MODE:
935 if (!has_tess)
936 break;
937 if (check_tes_query(ctx, shProg)) {
938 *params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
939 Program->info.tess.point_mode ? GL_TRUE : GL_FALSE;
940 }
941 return;
942 default:
943 break;
944 }
945
946 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname=%s)",
947 _mesa_enum_to_string(pname));
948 }
949
950
951 /**
952 * glGetShaderiv() - get GLSL shader state
953 */
954 static void
955 get_shaderiv(struct gl_context *ctx, GLuint name, GLenum pname, GLint *params)
956 {
957 struct gl_shader *shader =
958 _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
959
960 if (!shader) {
961 return;
962 }
963
964 switch (pname) {
965 case GL_SHADER_TYPE:
966 *params = shader->Type;
967 break;
968 case GL_DELETE_STATUS:
969 *params = shader->DeletePending;
970 break;
971 case GL_COMPLETION_STATUS_ARB:
972 /* _mesa_glsl_compile_shader is not offloaded to other threads. */
973 *params = GL_TRUE;
974 return;
975 case GL_COMPILE_STATUS:
976 *params = shader->CompileStatus ? GL_TRUE : GL_FALSE;
977 break;
978 case GL_INFO_LOG_LENGTH:
979 *params = (shader->InfoLog && shader->InfoLog[0] != '\0') ?
980 strlen(shader->InfoLog) + 1 : 0;
981 break;
982 case GL_SHADER_SOURCE_LENGTH:
983 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
984 break;
985 case GL_SPIR_V_BINARY_ARB:
986 *params = (shader->spirv_data != NULL);
987 break;
988 default:
989 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
990 return;
991 }
992 }
993
994
995 static void
996 get_program_info_log(struct gl_context *ctx, GLuint program, GLsizei bufSize,
997 GLsizei *length, GLchar *infoLog)
998 {
999 struct gl_shader_program *shProg;
1000
1001 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1002 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1003 *
1004 * "If a negative number is provided where an argument of type sizei or
1005 * sizeiptr is specified, an INVALID_VALUE error is generated."
1006 */
1007 if (bufSize < 0) {
1008 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(bufSize < 0)");
1009 return;
1010 }
1011
1012 shProg = _mesa_lookup_shader_program_err(ctx, program,
1013 "glGetProgramInfoLog(program)");
1014 if (!shProg) {
1015 return;
1016 }
1017
1018 _mesa_copy_string(infoLog, bufSize, length, shProg->data->InfoLog);
1019 }
1020
1021
1022 static void
1023 get_shader_info_log(struct gl_context *ctx, GLuint shader, GLsizei bufSize,
1024 GLsizei *length, GLchar *infoLog)
1025 {
1026 struct gl_shader *sh;
1027
1028 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1029 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1030 *
1031 * "If a negative number is provided where an argument of type sizei or
1032 * sizeiptr is specified, an INVALID_VALUE error is generated."
1033 */
1034 if (bufSize < 0) {
1035 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(bufSize < 0)");
1036 return;
1037 }
1038
1039 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderInfoLog(shader)");
1040 if (!sh) {
1041 return;
1042 }
1043
1044 _mesa_copy_string(infoLog, bufSize, length, sh->InfoLog);
1045 }
1046
1047
1048 /**
1049 * Return shader source code.
1050 */
1051 static void
1052 get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
1053 GLsizei *length, GLchar *sourceOut)
1054 {
1055 struct gl_shader *sh;
1056
1057 if (maxLength < 0) {
1058 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(bufSize < 0)");
1059 return;
1060 }
1061
1062 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
1063 if (!sh) {
1064 return;
1065 }
1066 _mesa_copy_string(sourceOut, maxLength, length, sh->Source);
1067 }
1068
1069
1070 /**
1071 * Set/replace shader source code. A helper function used by
1072 * glShaderSource[ARB].
1073 */
1074 static void
1075 set_shader_source(struct gl_shader *sh, const GLchar *source)
1076 {
1077 assert(sh);
1078
1079 /* The GL_ARB_gl_spirv spec adds the following to the end of the description
1080 * of ShaderSource:
1081 *
1082 * "If <shader> was previously associated with a SPIR-V module (via the
1083 * ShaderBinary command), that association is broken. Upon successful
1084 * completion of this command the SPIR_V_BINARY_ARB state of <shader>
1085 * is set to FALSE."
1086 */
1087 _mesa_shader_spirv_data_reference(&sh->spirv_data, NULL);
1088
1089 if (sh->CompileStatus == COMPILE_SKIPPED && !sh->FallbackSource) {
1090 /* If shader was previously compiled back-up the source in case of cache
1091 * fallback.
1092 */
1093 sh->FallbackSource = sh->Source;
1094 sh->Source = source;
1095 } else {
1096 /* free old shader source string and install new one */
1097 free((void *)sh->Source);
1098 sh->Source = source;
1099 }
1100
1101 #ifdef DEBUG
1102 sh->SourceChecksum = util_hash_crc32(sh->Source, strlen(sh->Source));
1103 #endif
1104 }
1105
1106
1107 /**
1108 * Compile a shader.
1109 */
1110 void
1111 _mesa_compile_shader(struct gl_context *ctx, struct gl_shader *sh)
1112 {
1113 if (!sh)
1114 return;
1115
1116 /* The GL_ARB_gl_spirv spec says:
1117 *
1118 * "Add a new error for the CompileShader command:
1119 *
1120 * An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB
1121 * state of <shader> is TRUE."
1122 */
1123 if (sh->spirv_data) {
1124 _mesa_error(ctx, GL_INVALID_OPERATION, "glCompileShader(SPIR-V)");
1125 return;
1126 }
1127
1128 if (!sh->Source) {
1129 /* If the user called glCompileShader without first calling
1130 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1131 */
1132 sh->CompileStatus = COMPILE_FAILURE;
1133 } else {
1134 if (ctx->_Shader->Flags & GLSL_DUMP) {
1135 _mesa_log("GLSL source for %s shader %d:\n",
1136 _mesa_shader_stage_to_string(sh->Stage), sh->Name);
1137 _mesa_log("%s\n", sh->Source);
1138 }
1139
1140 /* this call will set the shader->CompileStatus field to indicate if
1141 * compilation was successful.
1142 */
1143 _mesa_glsl_compile_shader(ctx, sh, false, false, false);
1144
1145 if (ctx->_Shader->Flags & GLSL_LOG) {
1146 _mesa_write_shader_to_file(sh);
1147 }
1148
1149 if (ctx->_Shader->Flags & GLSL_DUMP) {
1150 if (sh->CompileStatus) {
1151 if (sh->ir) {
1152 _mesa_log("GLSL IR for shader %d:\n", sh->Name);
1153 _mesa_print_ir(_mesa_get_log_file(), sh->ir, NULL);
1154 } else {
1155 _mesa_log("No GLSL IR for shader %d (shader may be from "
1156 "cache)\n", sh->Name);
1157 }
1158 _mesa_log("\n\n");
1159 } else {
1160 _mesa_log("GLSL shader %d failed to compile.\n", sh->Name);
1161 }
1162 if (sh->InfoLog && sh->InfoLog[0] != 0) {
1163 _mesa_log("GLSL shader %d info log:\n", sh->Name);
1164 _mesa_log("%s\n", sh->InfoLog);
1165 }
1166 }
1167 }
1168
1169 if (!sh->CompileStatus) {
1170 if (ctx->_Shader->Flags & GLSL_DUMP_ON_ERROR) {
1171 _mesa_log("GLSL source for %s shader %d:\n",
1172 _mesa_shader_stage_to_string(sh->Stage), sh->Name);
1173 _mesa_log("%s\n", sh->Source);
1174 _mesa_log("Info Log:\n%s\n", sh->InfoLog);
1175 }
1176
1177 if (ctx->_Shader->Flags & GLSL_REPORT_ERRORS) {
1178 _mesa_debug(ctx, "Error compiling shader %u:\n%s\n",
1179 sh->Name, sh->InfoLog);
1180 }
1181 }
1182 }
1183
1184
1185 /**
1186 * Link a program's shaders.
1187 */
1188 static ALWAYS_INLINE void
1189 link_program(struct gl_context *ctx, struct gl_shader_program *shProg,
1190 bool no_error)
1191 {
1192 if (!shProg)
1193 return;
1194
1195 if (!no_error) {
1196 /* From the ARB_transform_feedback2 specification:
1197 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1198 * is the name of a program being used by one or more transform feedback
1199 * objects, even if the objects are not currently bound or are paused."
1200 */
1201 if (_mesa_transform_feedback_is_using_program(ctx, shProg)) {
1202 _mesa_error(ctx, GL_INVALID_OPERATION,
1203 "glLinkProgram(transform feedback is using the program)");
1204 return;
1205 }
1206 }
1207
1208 unsigned programs_in_use = 0;
1209 if (ctx->_Shader)
1210 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1211 if (ctx->_Shader->CurrentProgram[stage] &&
1212 ctx->_Shader->CurrentProgram[stage]->Id == shProg->Name) {
1213 programs_in_use |= 1 << stage;
1214 }
1215 }
1216
1217 FLUSH_VERTICES(ctx, 0);
1218 _mesa_glsl_link_shader(ctx, shProg);
1219
1220 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1221 *
1222 * "If LinkProgram or ProgramBinary successfully re-links a program
1223 * object that is active for any shader stage, then the newly generated
1224 * executable code will be installed as part of the current rendering
1225 * state for all shader stages where the program is active.
1226 * Additionally, the newly generated executable code is made part of
1227 * the state of any program pipeline for all stages where the program
1228 * is attached."
1229 */
1230 if (shProg->data->LinkStatus && programs_in_use) {
1231 while (programs_in_use) {
1232 const int stage = u_bit_scan(&programs_in_use);
1233
1234 struct gl_program *prog = NULL;
1235 if (shProg->_LinkedShaders[stage])
1236 prog = shProg->_LinkedShaders[stage]->Program;
1237
1238 _mesa_use_program(ctx, stage, shProg, prog, ctx->_Shader);
1239 }
1240 }
1241
1242 /* Capture .shader_test files. */
1243 const char *capture_path = _mesa_get_shader_capture_path();
1244 if (shProg->Name != 0 && shProg->Name != ~0 && capture_path != NULL) {
1245 /* Find an unused filename. */
1246 FILE *file = NULL;
1247 char *filename = NULL;
1248 for (unsigned i = 0;; i++) {
1249 if (i) {
1250 filename = ralloc_asprintf(NULL, "%s/%u-%u.shader_test",
1251 capture_path, shProg->Name, i);
1252 } else {
1253 filename = ralloc_asprintf(NULL, "%s/%u.shader_test",
1254 capture_path, shProg->Name);
1255 }
1256 file = os_file_create_unique(filename, 0644);
1257 if (file)
1258 break;
1259 /* If we are failing for another reason than "this filename already
1260 * exists", we are likely to fail again with another filename, so
1261 * let's just give up */
1262 if (errno != EEXIST)
1263 break;
1264 ralloc_free(filename);
1265 }
1266 if (file) {
1267 fprintf(file, "[require]\nGLSL%s >= %u.%02u\n",
1268 shProg->IsES ? " ES" : "",
1269 shProg->data->Version / 100, shProg->data->Version % 100);
1270 if (shProg->SeparateShader)
1271 fprintf(file, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1272 fprintf(file, "\n");
1273
1274 for (unsigned i = 0; i < shProg->NumShaders; i++) {
1275 fprintf(file, "[%s shader]\n%s\n",
1276 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
1277 shProg->Shaders[i]->Source);
1278 }
1279 fclose(file);
1280 } else {
1281 _mesa_warning(ctx, "Failed to open %s", filename);
1282 }
1283
1284 ralloc_free(filename);
1285 }
1286
1287 if (shProg->data->LinkStatus == LINKING_FAILURE &&
1288 (ctx->_Shader->Flags & GLSL_REPORT_ERRORS)) {
1289 _mesa_debug(ctx, "Error linking program %u:\n%s\n",
1290 shProg->Name, shProg->data->InfoLog);
1291 }
1292
1293 _mesa_update_vertex_processing_mode(ctx);
1294
1295 shProg->BinaryRetrievableHint = shProg->BinaryRetrievableHintPending;
1296
1297 /* debug code */
1298 if (0) {
1299 GLuint i;
1300
1301 printf("Link %u shaders in program %u: %s\n",
1302 shProg->NumShaders, shProg->Name,
1303 shProg->data->LinkStatus ? "Success" : "Failed");
1304
1305 for (i = 0; i < shProg->NumShaders; i++) {
1306 printf(" shader %u, stage %u\n",
1307 shProg->Shaders[i]->Name,
1308 shProg->Shaders[i]->Stage);
1309 }
1310 }
1311 }
1312
1313
1314 static void
1315 link_program_error(struct gl_context *ctx, struct gl_shader_program *shProg)
1316 {
1317 link_program(ctx, shProg, false);
1318 }
1319
1320
1321 static void
1322 link_program_no_error(struct gl_context *ctx, struct gl_shader_program *shProg)
1323 {
1324 link_program(ctx, shProg, true);
1325 }
1326
1327
1328 void
1329 _mesa_link_program(struct gl_context *ctx, struct gl_shader_program *shProg)
1330 {
1331 link_program_error(ctx, shProg);
1332 }
1333
1334
1335 /**
1336 * Print basic shader info (for debug).
1337 */
1338 static void
1339 print_shader_info(const struct gl_shader_program *shProg)
1340 {
1341 GLuint i;
1342
1343 printf("Mesa: glUseProgram(%u)\n", shProg->Name);
1344 for (i = 0; i < shProg->NumShaders; i++) {
1345 #ifdef DEBUG
1346 printf(" %s shader %u, checksum %u\n",
1347 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
1348 shProg->Shaders[i]->Name,
1349 shProg->Shaders[i]->SourceChecksum);
1350 #else
1351 printf(" %s shader %u\n",
1352 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
1353 shProg->Shaders[i]->Name);
1354 #endif
1355 }
1356 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX])
1357 printf(" vert prog %u\n",
1358 shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
1359 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT])
1360 printf(" frag prog %u\n",
1361 shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->Id);
1362 if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
1363 printf(" geom prog %u\n",
1364 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
1365 if (shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL])
1366 printf(" tesc prog %u\n",
1367 shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program->Id);
1368 if (shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL])
1369 printf(" tese prog %u\n",
1370 shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->Program->Id);
1371 }
1372
1373
1374 /**
1375 * Use the named shader program for subsequent glUniform calls
1376 */
1377 void
1378 _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
1379 const char *caller)
1380 {
1381 if ((shProg != NULL) && !shProg->data->LinkStatus) {
1382 _mesa_error(ctx, GL_INVALID_OPERATION,
1383 "%s(program %u not linked)", caller, shProg->Name);
1384 return;
1385 }
1386
1387 if (ctx->Shader.ActiveProgram != shProg) {
1388 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
1389 }
1390 }
1391
1392
1393 /**
1394 * Use the named shader program for subsequent rendering.
1395 */
1396 void
1397 _mesa_use_shader_program(struct gl_context *ctx,
1398 struct gl_shader_program *shProg)
1399 {
1400 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1401 struct gl_program *new_prog = NULL;
1402 if (shProg && shProg->_LinkedShaders[i])
1403 new_prog = shProg->_LinkedShaders[i]->Program;
1404 _mesa_use_program(ctx, i, shProg, new_prog, &ctx->Shader);
1405 }
1406 _mesa_active_program(ctx, shProg, "glUseProgram");
1407 }
1408
1409
1410 /**
1411 * Do validation of the given shader program.
1412 * \param errMsg returns error message if validation fails.
1413 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1414 */
1415 static GLboolean
1416 validate_shader_program(const struct gl_shader_program *shProg,
1417 char *errMsg)
1418 {
1419 if (!shProg->data->LinkStatus) {
1420 return GL_FALSE;
1421 }
1422
1423 /* From the GL spec, a program is invalid if any of these are true:
1424
1425 any two active samplers in the current program object are of
1426 different types, but refer to the same texture image unit,
1427
1428 any active sampler in the current program object refers to a texture
1429 image unit where fixed-function fragment processing accesses a
1430 texture target that does not match the sampler type, or
1431
1432 the sum of the number of active samplers in the program and the
1433 number of texture image units enabled for fixed-function fragment
1434 processing exceeds the combined limit on the total number of texture
1435 image units allowed.
1436 */
1437
1438 /*
1439 * Check: any two active samplers in the current program object are of
1440 * different types, but refer to the same texture image unit,
1441 */
1442 if (!_mesa_sampler_uniforms_are_valid(shProg, errMsg, 100))
1443 return GL_FALSE;
1444
1445 return GL_TRUE;
1446 }
1447
1448
1449 /**
1450 * Called via glValidateProgram()
1451 */
1452 static void
1453 validate_program(struct gl_context *ctx, GLuint program)
1454 {
1455 struct gl_shader_program *shProg;
1456 char errMsg[100] = "";
1457
1458 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1459 if (!shProg) {
1460 return;
1461 }
1462
1463 shProg->data->Validated = validate_shader_program(shProg, errMsg);
1464 if (!shProg->data->Validated) {
1465 /* update info log */
1466 if (shProg->data->InfoLog) {
1467 ralloc_free(shProg->data->InfoLog);
1468 }
1469 shProg->data->InfoLog = ralloc_strdup(shProg->data, errMsg);
1470 }
1471 }
1472
1473
1474 void GLAPIENTRY
1475 _mesa_AttachObjectARB_no_error(GLhandleARB program, GLhandleARB shader)
1476 {
1477 GET_CURRENT_CONTEXT(ctx);
1478 attach_shader_no_error(ctx, program, shader);
1479 }
1480
1481
1482 void GLAPIENTRY
1483 _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
1484 {
1485 GET_CURRENT_CONTEXT(ctx);
1486 attach_shader_err(ctx, program, shader, "glAttachObjectARB");
1487 }
1488
1489
1490 void GLAPIENTRY
1491 _mesa_AttachShader_no_error(GLuint program, GLuint shader)
1492 {
1493 GET_CURRENT_CONTEXT(ctx);
1494 attach_shader_no_error(ctx, program, shader);
1495 }
1496
1497
1498 void GLAPIENTRY
1499 _mesa_AttachShader(GLuint program, GLuint shader)
1500 {
1501 GET_CURRENT_CONTEXT(ctx);
1502 attach_shader_err(ctx, program, shader, "glAttachShader");
1503 }
1504
1505
1506 void GLAPIENTRY
1507 _mesa_CompileShader(GLuint shaderObj)
1508 {
1509 GET_CURRENT_CONTEXT(ctx);
1510 if (MESA_VERBOSE & VERBOSE_API)
1511 _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
1512 _mesa_compile_shader(ctx, _mesa_lookup_shader_err(ctx, shaderObj,
1513 "glCompileShader"));
1514 }
1515
1516
1517 GLuint GLAPIENTRY
1518 _mesa_CreateShader_no_error(GLenum type)
1519 {
1520 GET_CURRENT_CONTEXT(ctx);
1521 return create_shader(ctx, type);
1522 }
1523
1524
1525 GLuint GLAPIENTRY
1526 _mesa_CreateShader(GLenum type)
1527 {
1528 GET_CURRENT_CONTEXT(ctx);
1529
1530 if (MESA_VERBOSE & VERBOSE_API)
1531 _mesa_debug(ctx, "glCreateShader %s\n", _mesa_enum_to_string(type));
1532
1533 return create_shader_err(ctx, type, "glCreateShader");
1534 }
1535
1536
1537 GLhandleARB GLAPIENTRY
1538 _mesa_CreateShaderObjectARB_no_error(GLenum type)
1539 {
1540 GET_CURRENT_CONTEXT(ctx);
1541 return create_shader(ctx, type);
1542 }
1543
1544
1545 GLhandleARB GLAPIENTRY
1546 _mesa_CreateShaderObjectARB(GLenum type)
1547 {
1548 GET_CURRENT_CONTEXT(ctx);
1549 return create_shader_err(ctx, type, "glCreateShaderObjectARB");
1550 }
1551
1552
1553 GLuint GLAPIENTRY
1554 _mesa_CreateProgram(void)
1555 {
1556 GET_CURRENT_CONTEXT(ctx);
1557 if (MESA_VERBOSE & VERBOSE_API)
1558 _mesa_debug(ctx, "glCreateProgram\n");
1559 return create_shader_program(ctx);
1560 }
1561
1562
1563 GLhandleARB GLAPIENTRY
1564 _mesa_CreateProgramObjectARB(void)
1565 {
1566 GET_CURRENT_CONTEXT(ctx);
1567 return create_shader_program(ctx);
1568 }
1569
1570
1571 void GLAPIENTRY
1572 _mesa_DeleteObjectARB(GLhandleARB obj)
1573 {
1574 if (MESA_VERBOSE & VERBOSE_API) {
1575 GET_CURRENT_CONTEXT(ctx);
1576 _mesa_debug(ctx, "glDeleteObjectARB(%lu)\n", (unsigned long)obj);
1577 }
1578
1579 if (obj) {
1580 GET_CURRENT_CONTEXT(ctx);
1581 FLUSH_VERTICES(ctx, 0);
1582 if (is_program(ctx, obj)) {
1583 delete_shader_program(ctx, obj);
1584 }
1585 else if (is_shader(ctx, obj)) {
1586 delete_shader(ctx, obj);
1587 }
1588 else {
1589 /* error? */
1590 }
1591 }
1592 }
1593
1594
1595 void GLAPIENTRY
1596 _mesa_DeleteProgram(GLuint name)
1597 {
1598 if (name) {
1599 GET_CURRENT_CONTEXT(ctx);
1600 FLUSH_VERTICES(ctx, 0);
1601 delete_shader_program(ctx, name);
1602 }
1603 }
1604
1605
1606 void GLAPIENTRY
1607 _mesa_DeleteShader(GLuint name)
1608 {
1609 if (name) {
1610 GET_CURRENT_CONTEXT(ctx);
1611 FLUSH_VERTICES(ctx, 0);
1612 delete_shader(ctx, name);
1613 }
1614 }
1615
1616
1617 void GLAPIENTRY
1618 _mesa_DetachObjectARB_no_error(GLhandleARB program, GLhandleARB shader)
1619 {
1620 GET_CURRENT_CONTEXT(ctx);
1621 detach_shader_no_error(ctx, program, shader);
1622 }
1623
1624
1625 void GLAPIENTRY
1626 _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
1627 {
1628 GET_CURRENT_CONTEXT(ctx);
1629 detach_shader_error(ctx, program, shader);
1630 }
1631
1632
1633 void GLAPIENTRY
1634 _mesa_DetachShader_no_error(GLuint program, GLuint shader)
1635 {
1636 GET_CURRENT_CONTEXT(ctx);
1637 detach_shader_no_error(ctx, program, shader);
1638 }
1639
1640
1641 void GLAPIENTRY
1642 _mesa_DetachShader(GLuint program, GLuint shader)
1643 {
1644 GET_CURRENT_CONTEXT(ctx);
1645 detach_shader_error(ctx, program, shader);
1646 }
1647
1648
1649 void GLAPIENTRY
1650 _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
1651 GLsizei * count, GLhandleARB * obj)
1652 {
1653 GET_CURRENT_CONTEXT(ctx);
1654 get_attached_shaders(ctx, (GLuint)container, maxCount, count, NULL, obj);
1655 }
1656
1657
1658 void GLAPIENTRY
1659 _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
1660 GLsizei *count, GLuint *obj)
1661 {
1662 GET_CURRENT_CONTEXT(ctx);
1663 get_attached_shaders(ctx, program, maxCount, count, obj, NULL);
1664 }
1665
1666
1667 void GLAPIENTRY
1668 _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
1669 GLcharARB * infoLog)
1670 {
1671 GET_CURRENT_CONTEXT(ctx);
1672 if (is_program(ctx, object)) {
1673 get_program_info_log(ctx, object, maxLength, length, infoLog);
1674 }
1675 else if (is_shader(ctx, object)) {
1676 get_shader_info_log(ctx, object, maxLength, length, infoLog);
1677 }
1678 else {
1679 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
1680 }
1681 }
1682
1683
1684 void GLAPIENTRY
1685 _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
1686 {
1687 GET_CURRENT_CONTEXT(ctx);
1688 /* Implement in terms of GetProgramiv, GetShaderiv */
1689 if (is_program(ctx, object)) {
1690 if (pname == GL_OBJECT_TYPE_ARB) {
1691 *params = GL_PROGRAM_OBJECT_ARB;
1692 }
1693 else {
1694 get_programiv(ctx, object, pname, params);
1695 }
1696 }
1697 else if (is_shader(ctx, object)) {
1698 if (pname == GL_OBJECT_TYPE_ARB) {
1699 *params = GL_SHADER_OBJECT_ARB;
1700 }
1701 else {
1702 get_shaderiv(ctx, object, pname, params);
1703 }
1704 }
1705 else {
1706 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
1707 }
1708 }
1709
1710
1711 void GLAPIENTRY
1712 _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
1713 GLfloat *params)
1714 {
1715 GLint iparams[1] = {0}; /* XXX is one element enough? */
1716 _mesa_GetObjectParameterivARB(object, pname, iparams);
1717 params[0] = (GLfloat) iparams[0];
1718 }
1719
1720
1721 void GLAPIENTRY
1722 _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
1723 {
1724 GET_CURRENT_CONTEXT(ctx);
1725 get_programiv(ctx, program, pname, params);
1726 }
1727
1728
1729 void GLAPIENTRY
1730 _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
1731 {
1732 GET_CURRENT_CONTEXT(ctx);
1733 get_shaderiv(ctx, shader, pname, params);
1734 }
1735
1736
1737 void GLAPIENTRY
1738 _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
1739 GLsizei *length, GLchar *infoLog)
1740 {
1741 GET_CURRENT_CONTEXT(ctx);
1742 get_program_info_log(ctx, program, bufSize, length, infoLog);
1743 }
1744
1745
1746 void GLAPIENTRY
1747 _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
1748 GLsizei *length, GLchar *infoLog)
1749 {
1750 GET_CURRENT_CONTEXT(ctx);
1751 get_shader_info_log(ctx, shader, bufSize, length, infoLog);
1752 }
1753
1754
1755 void GLAPIENTRY
1756 _mesa_GetShaderSource(GLuint shader, GLsizei maxLength,
1757 GLsizei *length, GLchar *sourceOut)
1758 {
1759 GET_CURRENT_CONTEXT(ctx);
1760 get_shader_source(ctx, shader, maxLength, length, sourceOut);
1761 }
1762
1763
1764 GLhandleARB GLAPIENTRY
1765 _mesa_GetHandleARB(GLenum pname)
1766 {
1767 GET_CURRENT_CONTEXT(ctx);
1768 return get_handle(ctx, pname);
1769 }
1770
1771
1772 GLboolean GLAPIENTRY
1773 _mesa_IsProgram(GLuint name)
1774 {
1775 GET_CURRENT_CONTEXT(ctx);
1776 return is_program(ctx, name);
1777 }
1778
1779
1780 GLboolean GLAPIENTRY
1781 _mesa_IsShader(GLuint name)
1782 {
1783 GET_CURRENT_CONTEXT(ctx);
1784 return is_shader(ctx, name);
1785 }
1786
1787
1788 void GLAPIENTRY
1789 _mesa_LinkProgram_no_error(GLuint programObj)
1790 {
1791 GET_CURRENT_CONTEXT(ctx);
1792
1793 struct gl_shader_program *shProg =
1794 _mesa_lookup_shader_program(ctx, programObj);
1795 link_program_no_error(ctx, shProg);
1796 }
1797
1798
1799 void GLAPIENTRY
1800 _mesa_LinkProgram(GLuint programObj)
1801 {
1802 GET_CURRENT_CONTEXT(ctx);
1803
1804 if (MESA_VERBOSE & VERBOSE_API)
1805 _mesa_debug(ctx, "glLinkProgram %u\n", programObj);
1806
1807 struct gl_shader_program *shProg =
1808 _mesa_lookup_shader_program_err(ctx, programObj, "glLinkProgram");
1809 link_program_error(ctx, shProg);
1810 }
1811
1812 #ifdef ENABLE_SHADER_CACHE
1813 /**
1814 * Generate a SHA-1 hash value string for given source string.
1815 */
1816 static void
1817 generate_sha1(const char *source, char sha_str[64])
1818 {
1819 unsigned char sha[20];
1820 _mesa_sha1_compute(source, strlen(source), sha);
1821 _mesa_sha1_format(sha_str, sha);
1822 }
1823
1824 /**
1825 * Construct a full path for shader replacement functionality using
1826 * following format:
1827 *
1828 * <path>/<stage prefix>_<CHECKSUM>.glsl
1829 * <path>/<stage prefix>_<CHECKSUM>.arb
1830 */
1831 static char *
1832 construct_name(const gl_shader_stage stage, const char *source,
1833 const char *path)
1834 {
1835 char sha[64];
1836 static const char *types[] = {
1837 "VS", "TC", "TE", "GS", "FS", "CS",
1838 };
1839
1840 const char *format = strncmp(source, "!!ARB", 5) ? "glsl" : "arb";
1841
1842 generate_sha1(source, sha);
1843 return ralloc_asprintf(NULL, "%s/%s_%s.%s", path, types[stage], sha, format);
1844 }
1845
1846 /**
1847 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1848 */
1849 void
1850 _mesa_dump_shader_source(const gl_shader_stage stage, const char *source)
1851 {
1852 static bool path_exists = true;
1853 char *dump_path;
1854 FILE *f;
1855
1856 if (!path_exists)
1857 return;
1858
1859 dump_path = getenv("MESA_SHADER_DUMP_PATH");
1860 if (!dump_path) {
1861 path_exists = false;
1862 return;
1863 }
1864
1865 char *name = construct_name(stage, source, dump_path);
1866
1867 f = fopen(name, "w");
1868 if (f) {
1869 fputs(source, f);
1870 fclose(f);
1871 } else {
1872 GET_CURRENT_CONTEXT(ctx);
1873 _mesa_warning(ctx, "could not open %s for dumping shader (%s)", name,
1874 strerror(errno));
1875 }
1876 ralloc_free(name);
1877 }
1878
1879 /**
1880 * Read shader source code from a file.
1881 * Useful for debugging to override an app's shader.
1882 */
1883 GLcharARB *
1884 _mesa_read_shader_source(const gl_shader_stage stage, const char *source)
1885 {
1886 char *read_path;
1887 static bool path_exists = true;
1888 int len, shader_size = 0;
1889 GLcharARB *buffer;
1890 FILE *f;
1891
1892 if (!path_exists)
1893 return NULL;
1894
1895 read_path = getenv("MESA_SHADER_READ_PATH");
1896 if (!read_path) {
1897 path_exists = false;
1898 return NULL;
1899 }
1900
1901 char *name = construct_name(stage, source, read_path);
1902 f = fopen(name, "r");
1903 ralloc_free(name);
1904 if (!f)
1905 return NULL;
1906
1907 /* allocate enough room for the entire shader */
1908 fseek(f, 0, SEEK_END);
1909 shader_size = ftell(f);
1910 rewind(f);
1911 assert(shader_size);
1912
1913 /* add one for terminating zero */
1914 shader_size++;
1915
1916 buffer = malloc(shader_size);
1917 assert(buffer);
1918
1919 len = fread(buffer, 1, shader_size, f);
1920 buffer[len] = 0;
1921
1922 fclose(f);
1923
1924 return buffer;
1925 }
1926
1927 #endif /* ENABLE_SHADER_CACHE */
1928
1929 /**
1930 * Called via glShaderSource() and glShaderSourceARB() API functions.
1931 * Basically, concatenate the source code strings into one long string
1932 * and pass it to _mesa_shader_source().
1933 */
1934 static ALWAYS_INLINE void
1935 shader_source(struct gl_context *ctx, GLuint shaderObj, GLsizei count,
1936 const GLchar *const *string, const GLint *length, bool no_error)
1937 {
1938 GLint *offsets;
1939 GLsizei i, totalLength;
1940 GLcharARB *source;
1941 struct gl_shader *sh;
1942
1943 if (!no_error) {
1944 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glShaderSourceARB");
1945 if (!sh)
1946 return;
1947
1948 if (string == NULL) {
1949 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
1950 return;
1951 }
1952 } else {
1953 sh = _mesa_lookup_shader(ctx, shaderObj);
1954 }
1955
1956 /*
1957 * This array holds offsets of where the appropriate string ends, thus the
1958 * last element will be set to the total length of the source code.
1959 */
1960 offsets = malloc(count * sizeof(GLint));
1961 if (offsets == NULL) {
1962 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1963 return;
1964 }
1965
1966 for (i = 0; i < count; i++) {
1967 if (!no_error && string[i] == NULL) {
1968 free((GLvoid *) offsets);
1969 _mesa_error(ctx, GL_INVALID_OPERATION,
1970 "glShaderSourceARB(null string)");
1971 return;
1972 }
1973 if (length == NULL || length[i] < 0)
1974 offsets[i] = strlen(string[i]);
1975 else
1976 offsets[i] = length[i];
1977 /* accumulate string lengths */
1978 if (i > 0)
1979 offsets[i] += offsets[i - 1];
1980 }
1981
1982 /* Total length of source string is sum off all strings plus two.
1983 * One extra byte for terminating zero, another extra byte to silence
1984 * valgrind warnings in the parser/grammer code.
1985 */
1986 totalLength = offsets[count - 1] + 2;
1987 source = malloc(totalLength * sizeof(GLcharARB));
1988 if (source == NULL) {
1989 free((GLvoid *) offsets);
1990 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1991 return;
1992 }
1993
1994 for (i = 0; i < count; i++) {
1995 GLint start = (i > 0) ? offsets[i - 1] : 0;
1996 memcpy(source + start, string[i],
1997 (offsets[i] - start) * sizeof(GLcharARB));
1998 }
1999 source[totalLength - 1] = '\0';
2000 source[totalLength - 2] = '\0';
2001
2002 #ifdef ENABLE_SHADER_CACHE
2003 GLcharARB *replacement;
2004
2005 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
2006 * if corresponding entry found from MESA_SHADER_READ_PATH.
2007 */
2008 _mesa_dump_shader_source(sh->Stage, source);
2009
2010 replacement = _mesa_read_shader_source(sh->Stage, source);
2011 if (replacement) {
2012 free(source);
2013 source = replacement;
2014 }
2015 #endif /* ENABLE_SHADER_CACHE */
2016
2017 set_shader_source(sh, source);
2018
2019 free(offsets);
2020 }
2021
2022
2023 void GLAPIENTRY
2024 _mesa_ShaderSource_no_error(GLuint shaderObj, GLsizei count,
2025 const GLchar *const *string, const GLint *length)
2026 {
2027 GET_CURRENT_CONTEXT(ctx);
2028 shader_source(ctx, shaderObj, count, string, length, true);
2029 }
2030
2031
2032 void GLAPIENTRY
2033 _mesa_ShaderSource(GLuint shaderObj, GLsizei count,
2034 const GLchar *const *string, const GLint *length)
2035 {
2036 GET_CURRENT_CONTEXT(ctx);
2037 shader_source(ctx, shaderObj, count, string, length, false);
2038 }
2039
2040
2041 static ALWAYS_INLINE void
2042 use_program(GLuint program, bool no_error)
2043 {
2044 GET_CURRENT_CONTEXT(ctx);
2045 struct gl_shader_program *shProg = NULL;
2046
2047 if (MESA_VERBOSE & VERBOSE_API)
2048 _mesa_debug(ctx, "glUseProgram %u\n", program);
2049
2050 if (no_error) {
2051 if (program) {
2052 shProg = _mesa_lookup_shader_program(ctx, program);
2053 }
2054 } else {
2055 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
2056 _mesa_error(ctx, GL_INVALID_OPERATION,
2057 "glUseProgram(transform feedback active)");
2058 return;
2059 }
2060
2061 if (program) {
2062 shProg =
2063 _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
2064 if (!shProg)
2065 return;
2066
2067 if (!shProg->data->LinkStatus) {
2068 _mesa_error(ctx, GL_INVALID_OPERATION,
2069 "glUseProgram(program %u not linked)", program);
2070 return;
2071 }
2072
2073 /* debug code */
2074 if (ctx->_Shader->Flags & GLSL_USE_PROG) {
2075 print_shader_info(shProg);
2076 }
2077 }
2078 }
2079
2080 /* The ARB_separate_shader_object spec says:
2081 *
2082 * "The executable code for an individual shader stage is taken from
2083 * the current program for that stage. If there is a current program
2084 * object established by UseProgram, that program is considered current
2085 * for all stages. Otherwise, if there is a bound program pipeline
2086 * object (section 2.14.PPO), the program bound to the appropriate
2087 * stage of the pipeline object is considered current."
2088 */
2089 if (shProg) {
2090 /* Attach shader state to the binding point */
2091 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
2092 /* Update the program */
2093 _mesa_use_shader_program(ctx, shProg);
2094 } else {
2095 /* Must be done first: detach the progam */
2096 _mesa_use_shader_program(ctx, shProg);
2097 /* Unattach shader_state binding point */
2098 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
2099 ctx->Pipeline.Default);
2100 /* If a pipeline was bound, rebind it */
2101 if (ctx->Pipeline.Current) {
2102 if (no_error)
2103 _mesa_BindProgramPipeline_no_error(ctx->Pipeline.Current->Name);
2104 else
2105 _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
2106 }
2107 }
2108
2109 _mesa_update_vertex_processing_mode(ctx);
2110 }
2111
2112
2113 void GLAPIENTRY
2114 _mesa_UseProgram_no_error(GLuint program)
2115 {
2116 use_program(program, true);
2117 }
2118
2119
2120 void GLAPIENTRY
2121 _mesa_UseProgram(GLuint program)
2122 {
2123 use_program(program, false);
2124 }
2125
2126
2127 void GLAPIENTRY
2128 _mesa_ValidateProgram(GLuint program)
2129 {
2130 GET_CURRENT_CONTEXT(ctx);
2131 validate_program(ctx, program);
2132 }
2133
2134
2135 /**
2136 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2137 */
2138 void GLAPIENTRY
2139 _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
2140 GLint* range, GLint* precision)
2141 {
2142 const struct gl_program_constants *limits;
2143 const struct gl_precision *p;
2144 GET_CURRENT_CONTEXT(ctx);
2145
2146 switch (shadertype) {
2147 case GL_VERTEX_SHADER:
2148 limits = &ctx->Const.Program[MESA_SHADER_VERTEX];
2149 break;
2150 case GL_FRAGMENT_SHADER:
2151 limits = &ctx->Const.Program[MESA_SHADER_FRAGMENT];
2152 break;
2153 default:
2154 _mesa_error(ctx, GL_INVALID_ENUM,
2155 "glGetShaderPrecisionFormat(shadertype)");
2156 return;
2157 }
2158
2159 switch (precisiontype) {
2160 case GL_LOW_FLOAT:
2161 p = &limits->LowFloat;
2162 break;
2163 case GL_MEDIUM_FLOAT:
2164 p = &limits->MediumFloat;
2165 break;
2166 case GL_HIGH_FLOAT:
2167 p = &limits->HighFloat;
2168 break;
2169 case GL_LOW_INT:
2170 p = &limits->LowInt;
2171 break;
2172 case GL_MEDIUM_INT:
2173 p = &limits->MediumInt;
2174 break;
2175 case GL_HIGH_INT:
2176 p = &limits->HighInt;
2177 break;
2178 default:
2179 _mesa_error(ctx, GL_INVALID_ENUM,
2180 "glGetShaderPrecisionFormat(precisiontype)");
2181 return;
2182 }
2183
2184 range[0] = p->RangeMin;
2185 range[1] = p->RangeMax;
2186 precision[0] = p->Precision;
2187 }
2188
2189
2190 /**
2191 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2192 */
2193 void GLAPIENTRY
2194 _mesa_ReleaseShaderCompiler(void)
2195 {
2196 _mesa_destroy_shader_compiler_caches();
2197 }
2198
2199
2200 /**
2201 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2202 */
2203 void GLAPIENTRY
2204 _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
2205 const void* binary, GLint length)
2206 {
2207 GET_CURRENT_CONTEXT(ctx);
2208 struct gl_shader **sh;
2209
2210 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2211 * page 88 of the OpenGL 4.5 specs state:
2212 *
2213 * "An INVALID_VALUE error is generated if count or length is negative.
2214 * An INVALID_ENUM error is generated if binaryformat is not a supported
2215 * format returned in SHADER_BINARY_FORMATS."
2216 */
2217 if (n < 0 || length < 0) {
2218 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderBinary(count or length < 0)");
2219 return;
2220 }
2221
2222 /* Get all shader objects at once so we can make the operation
2223 * all-or-nothing.
2224 */
2225 if (n > SIZE_MAX / sizeof(*sh)) {
2226 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderBinary(count)");
2227 return;
2228 }
2229
2230 sh = alloca(sizeof(*sh) * (size_t)n);
2231 if (!sh) {
2232 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderBinary");
2233 return;
2234 }
2235
2236 for (int i = 0; i < n; ++i) {
2237 sh[i] = _mesa_lookup_shader_err(ctx, shaders[i], "glShaderBinary");
2238 if (!sh[i])
2239 return;
2240 }
2241
2242 if (binaryformat == GL_SHADER_BINARY_FORMAT_SPIR_V_ARB) {
2243 if (!ctx->Extensions.ARB_gl_spirv) {
2244 _mesa_error(ctx, GL_INVALID_OPERATION, "glShaderBinary(SPIR-V)");
2245 } else if (n > 0) {
2246 _mesa_spirv_shader_binary(ctx, (unsigned) n, sh, binary,
2247 (size_t) length);
2248 }
2249
2250 return;
2251 }
2252
2253 _mesa_error(ctx, GL_INVALID_ENUM, "glShaderBinary(format)");
2254 }
2255
2256
2257 void GLAPIENTRY
2258 _mesa_GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei *length,
2259 GLenum *binaryFormat, GLvoid *binary)
2260 {
2261 struct gl_shader_program *shProg;
2262 GLsizei length_dummy;
2263 GET_CURRENT_CONTEXT(ctx);
2264
2265 if (bufSize < 0){
2266 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramBinary(bufSize < 0)");
2267 return;
2268 }
2269
2270 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramBinary");
2271 if (!shProg)
2272 return;
2273
2274 /* The ARB_get_program_binary spec says:
2275 *
2276 * "If <length> is NULL, then no length is returned."
2277 *
2278 * Ensure that length always points to valid storage to avoid multiple NULL
2279 * pointer checks below.
2280 */
2281 if (length == NULL)
2282 length = &length_dummy;
2283
2284
2285 /* The ARB_get_program_binary spec says:
2286 *
2287 * "When a program object's LINK_STATUS is FALSE, its program binary
2288 * length is zero, and a call to GetProgramBinary will generate an
2289 * INVALID_OPERATION error.
2290 */
2291 if (!shProg->data->LinkStatus) {
2292 _mesa_error(ctx, GL_INVALID_OPERATION,
2293 "glGetProgramBinary(program %u not linked)",
2294 shProg->Name);
2295 *length = 0;
2296 return;
2297 }
2298
2299 if (ctx->Const.NumProgramBinaryFormats == 0) {
2300 *length = 0;
2301 _mesa_error(ctx, GL_INVALID_OPERATION,
2302 "glGetProgramBinary(driver supports zero binary formats)");
2303 } else {
2304 _mesa_get_program_binary(ctx, shProg, bufSize, length, binaryFormat,
2305 binary);
2306 assert(*length == 0 || *binaryFormat == GL_PROGRAM_BINARY_FORMAT_MESA);
2307 }
2308 }
2309
2310 void GLAPIENTRY
2311 _mesa_ProgramBinary(GLuint program, GLenum binaryFormat,
2312 const GLvoid *binary, GLsizei length)
2313 {
2314 struct gl_shader_program *shProg;
2315 GET_CURRENT_CONTEXT(ctx);
2316
2317 shProg = _mesa_lookup_shader_program_err(ctx, program, "glProgramBinary");
2318 if (!shProg)
2319 return;
2320
2321 _mesa_clear_shader_program_data(ctx, shProg);
2322 shProg->data = _mesa_create_shader_program_data();
2323
2324 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2325 *
2326 * "If a negative number is provided where an argument of type sizei or
2327 * sizeiptr is specified, an INVALID_VALUE error is generated."
2328 */
2329 if (length < 0) {
2330 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramBinary(length < 0)");
2331 return;
2332 }
2333
2334 if (ctx->Const.NumProgramBinaryFormats == 0 ||
2335 binaryFormat != GL_PROGRAM_BINARY_FORMAT_MESA) {
2336 /* The ARB_get_program_binary spec says:
2337 *
2338 * "<binaryFormat> and <binary> must be those returned by a previous
2339 * call to GetProgramBinary, and <length> must be the length of the
2340 * program binary as returned by GetProgramBinary or GetProgramiv with
2341 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2342 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2343 * are not met."
2344 *
2345 * Since any value of binaryFormat passed "is not one of those specified as
2346 * allowable for [this] command, an INVALID_ENUM error is generated."
2347 */
2348 shProg->data->LinkStatus = LINKING_FAILURE;
2349 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramBinary");
2350 } else {
2351 _mesa_program_binary(ctx, shProg, binaryFormat, binary, length);
2352 }
2353 }
2354
2355
2356 static ALWAYS_INLINE void
2357 program_parameteri(struct gl_context *ctx, struct gl_shader_program *shProg,
2358 GLuint pname, GLint value, bool no_error)
2359 {
2360 switch (pname) {
2361 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
2362 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2363 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2364 * even be in the dispatch table, so we shouldn't need to expclicitly
2365 * check here.
2366 *
2367 * On desktop, we ignore the 3.0+ requirement because it is silly.
2368 */
2369
2370 /* The ARB_get_program_binary extension spec says:
2371 *
2372 * "An INVALID_VALUE error is generated if the <value> argument to
2373 * ProgramParameteri is not TRUE or FALSE."
2374 */
2375 if (!no_error && value != GL_TRUE && value != GL_FALSE) {
2376 goto invalid_value;
2377 }
2378
2379 /* No need to notify the driver. Any changes will actually take effect
2380 * the next time the shader is linked.
2381 *
2382 * The ARB_get_program_binary extension spec says:
2383 *
2384 * "To indicate that a program binary is likely to be retrieved,
2385 * ProgramParameteri should be called with <pname>
2386 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2387 * will not be in effect until the next time LinkProgram or
2388 * ProgramBinary has been called successfully."
2389 *
2390 * The resolution of issue 9 in the extension spec also says:
2391 *
2392 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2393 * to indicate to the GL implementation that this program will
2394 * likely be saved with GetProgramBinary at some point. This will
2395 * give the GL implementation the opportunity to track any state
2396 * changes made to the program before being saved such that when it
2397 * is loaded again a recompile can be avoided."
2398 */
2399 shProg->BinaryRetrievableHintPending = value;
2400 return;
2401
2402 case GL_PROGRAM_SEPARABLE:
2403 /* Spec imply that the behavior is the same as ARB_get_program_binary
2404 * Chapter 7.3 Program Objects
2405 */
2406 if (!no_error && value != GL_TRUE && value != GL_FALSE) {
2407 goto invalid_value;
2408 }
2409 shProg->SeparateShader = value;
2410 return;
2411
2412 default:
2413 if (!no_error) {
2414 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteri(pname=%s)",
2415 _mesa_enum_to_string(pname));
2416 }
2417 return;
2418 }
2419
2420 invalid_value:
2421 _mesa_error(ctx, GL_INVALID_VALUE,
2422 "glProgramParameteri(pname=%s, value=%d): "
2423 "value must be 0 or 1.",
2424 _mesa_enum_to_string(pname),
2425 value);
2426 }
2427
2428
2429 void GLAPIENTRY
2430 _mesa_ProgramParameteri_no_error(GLuint program, GLenum pname, GLint value)
2431 {
2432 GET_CURRENT_CONTEXT(ctx);
2433
2434 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, program);
2435 program_parameteri(ctx, shProg, pname, value, true);
2436 }
2437
2438
2439 void GLAPIENTRY
2440 _mesa_ProgramParameteri(GLuint program, GLenum pname, GLint value)
2441 {
2442 struct gl_shader_program *shProg;
2443 GET_CURRENT_CONTEXT(ctx);
2444
2445 shProg = _mesa_lookup_shader_program_err(ctx, program,
2446 "glProgramParameteri");
2447 if (!shProg)
2448 return;
2449
2450 program_parameteri(ctx, shProg, pname, value, false);
2451 }
2452
2453
2454 void
2455 _mesa_use_program(struct gl_context *ctx, gl_shader_stage stage,
2456 struct gl_shader_program *shProg, struct gl_program *prog,
2457 struct gl_pipeline_object *shTarget)
2458 {
2459 struct gl_program **target;
2460
2461 target = &shTarget->CurrentProgram[stage];
2462 if (prog) {
2463 _mesa_program_init_subroutine_defaults(ctx, prog);
2464 }
2465
2466 if (*target != prog) {
2467 /* Program is current, flush it */
2468 if (shTarget == ctx->_Shader) {
2469 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
2470 }
2471
2472 _mesa_reference_shader_program(ctx,
2473 &shTarget->ReferencedPrograms[stage],
2474 shProg);
2475 _mesa_reference_program(ctx, target, prog);
2476 if (stage == MESA_SHADER_VERTEX)
2477 _mesa_update_vertex_processing_mode(ctx);
2478 return;
2479 }
2480
2481 }
2482
2483
2484 /**
2485 * Copy program-specific data generated by linking from the gl_shader_program
2486 * object to the gl_program object referred to by the gl_linked_shader.
2487 *
2488 * This function expects _mesa_reference_program() to have been previously
2489 * called setting the gl_linked_shaders program reference.
2490 */
2491 void
2492 _mesa_copy_linked_program_data(const struct gl_shader_program *src,
2493 struct gl_linked_shader *dst_sh)
2494 {
2495 assert(dst_sh->Program);
2496
2497 struct gl_program *dst = dst_sh->Program;
2498
2499 dst->info.separate_shader = src->SeparateShader;
2500
2501 switch (dst_sh->Stage) {
2502 case MESA_SHADER_GEOMETRY: {
2503 dst->info.gs.vertices_in = src->Geom.VerticesIn;
2504 dst->info.gs.uses_end_primitive = src->Geom.UsesEndPrimitive;
2505 dst->info.gs.uses_streams = src->Geom.UsesStreams;
2506 break;
2507 }
2508 case MESA_SHADER_FRAGMENT: {
2509 dst->info.fs.depth_layout = src->FragDepthLayout;
2510 break;
2511 }
2512 case MESA_SHADER_COMPUTE: {
2513 dst->info.cs.shared_size = src->Comp.SharedSize;
2514 break;
2515 }
2516 default:
2517 break;
2518 }
2519 }
2520
2521 /**
2522 * ARB_separate_shader_objects: Compile & Link Program
2523 */
2524 GLuint GLAPIENTRY
2525 _mesa_CreateShaderProgramv(GLenum type, GLsizei count,
2526 const GLchar* const *strings)
2527 {
2528 GET_CURRENT_CONTEXT(ctx);
2529
2530 const GLuint shader = create_shader_err(ctx, type, "glCreateShaderProgramv");
2531 GLuint program = 0;
2532
2533 /*
2534 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2535 * GL_INVALID_VALUE should be generated if count < 0
2536 */
2537 if (count < 0) {
2538 _mesa_error(ctx, GL_INVALID_VALUE, "glCreateShaderProgram (count < 0)");
2539 return program;
2540 }
2541
2542 if (shader) {
2543 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
2544
2545 _mesa_ShaderSource(shader, count, strings, NULL);
2546 _mesa_compile_shader(ctx, sh);
2547
2548 program = create_shader_program(ctx);
2549 if (program) {
2550 struct gl_shader_program *shProg;
2551 GLint compiled = GL_FALSE;
2552
2553 shProg = _mesa_lookup_shader_program(ctx, program);
2554
2555 shProg->SeparateShader = GL_TRUE;
2556
2557 get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
2558 if (compiled) {
2559 attach_shader_err(ctx, program, shader, "glCreateShaderProgramv");
2560 _mesa_link_program(ctx, shProg);
2561 detach_shader_error(ctx, program, shader);
2562
2563 #if 0
2564 /* Possibly... */
2565 if (active-user-defined-varyings-in-linked-program) {
2566 append-error-to-info-log;
2567 shProg->data->LinkStatus = LINKING_FAILURE;
2568 }
2569 #endif
2570 }
2571 if (sh->InfoLog)
2572 ralloc_strcat(&shProg->data->InfoLog, sh->InfoLog);
2573 }
2574
2575 delete_shader(ctx, shader);
2576 }
2577
2578 return program;
2579 }
2580
2581
2582 /**
2583 * For GL_ARB_tessellation_shader
2584 */
2585 void GLAPIENTRY
2586 _mesa_PatchParameteri_no_error(GLenum pname, GLint value)
2587 {
2588 GET_CURRENT_CONTEXT(ctx);
2589 ctx->TessCtrlProgram.patch_vertices = value;
2590 }
2591
2592
2593 extern void GLAPIENTRY
2594 _mesa_PatchParameteri(GLenum pname, GLint value)
2595 {
2596 GET_CURRENT_CONTEXT(ctx);
2597
2598 if (!_mesa_has_tessellation(ctx)) {
2599 _mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameteri");
2600 return;
2601 }
2602
2603 if (pname != GL_PATCH_VERTICES) {
2604 _mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameteri");
2605 return;
2606 }
2607
2608 if (value <= 0 || value > ctx->Const.MaxPatchVertices) {
2609 _mesa_error(ctx, GL_INVALID_VALUE, "glPatchParameteri");
2610 return;
2611 }
2612
2613 ctx->TessCtrlProgram.patch_vertices = value;
2614 }
2615
2616
2617 extern void GLAPIENTRY
2618 _mesa_PatchParameterfv(GLenum pname, const GLfloat *values)
2619 {
2620 GET_CURRENT_CONTEXT(ctx);
2621
2622 if (!_mesa_has_tessellation(ctx)) {
2623 _mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameterfv");
2624 return;
2625 }
2626
2627 switch(pname) {
2628 case GL_PATCH_DEFAULT_OUTER_LEVEL:
2629 FLUSH_VERTICES(ctx, 0);
2630 memcpy(ctx->TessCtrlProgram.patch_default_outer_level, values,
2631 4 * sizeof(GLfloat));
2632 ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
2633 return;
2634 case GL_PATCH_DEFAULT_INNER_LEVEL:
2635 FLUSH_VERTICES(ctx, 0);
2636 memcpy(ctx->TessCtrlProgram.patch_default_inner_level, values,
2637 2 * sizeof(GLfloat));
2638 ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
2639 return;
2640 default:
2641 _mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameterfv");
2642 return;
2643 }
2644 }
2645
2646 /**
2647 * ARB_shader_subroutine
2648 */
2649 GLint GLAPIENTRY
2650 _mesa_GetSubroutineUniformLocation(GLuint program, GLenum shadertype,
2651 const GLchar *name)
2652 {
2653 GET_CURRENT_CONTEXT(ctx);
2654 const char *api_name = "glGetSubroutineUniformLocation";
2655 struct gl_shader_program *shProg;
2656 GLenum resource_type;
2657 gl_shader_stage stage;
2658
2659 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2660 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2661 return -1;
2662 }
2663
2664 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2665 if (!shProg)
2666 return -1;
2667
2668 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2669 if (!shProg->_LinkedShaders[stage]) {
2670 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2671 return -1;
2672 }
2673
2674 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
2675 return _mesa_program_resource_location(shProg, resource_type, name);
2676 }
2677
2678 GLuint GLAPIENTRY
2679 _mesa_GetSubroutineIndex(GLuint program, GLenum shadertype,
2680 const GLchar *name)
2681 {
2682 GET_CURRENT_CONTEXT(ctx);
2683 const char *api_name = "glGetSubroutineIndex";
2684 struct gl_shader_program *shProg;
2685 struct gl_program_resource *res;
2686 GLenum resource_type;
2687 gl_shader_stage stage;
2688
2689 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2690 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2691 return -1;
2692 }
2693
2694 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2695 if (!shProg)
2696 return -1;
2697
2698 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2699 if (!shProg->_LinkedShaders[stage]) {
2700 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2701 return -1;
2702 }
2703
2704 resource_type = _mesa_shader_stage_to_subroutine(stage);
2705 res = _mesa_program_resource_find_name(shProg, resource_type, name, NULL);
2706 if (!res) {
2707 return -1;
2708 }
2709
2710 return _mesa_program_resource_index(shProg, res);
2711 }
2712
2713
2714 GLvoid GLAPIENTRY
2715 _mesa_GetActiveSubroutineUniformiv(GLuint program, GLenum shadertype,
2716 GLuint index, GLenum pname, GLint *values)
2717 {
2718 GET_CURRENT_CONTEXT(ctx);
2719 const char *api_name = "glGetActiveSubroutineUniformiv";
2720 struct gl_shader_program *shProg;
2721 struct gl_linked_shader *sh;
2722 gl_shader_stage stage;
2723 struct gl_program_resource *res;
2724 const struct gl_uniform_storage *uni;
2725 GLenum resource_type;
2726 int count, i, j;
2727
2728 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2729 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2730 return;
2731 }
2732
2733 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2734 if (!shProg)
2735 return;
2736
2737 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2738 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
2739
2740 sh = shProg->_LinkedShaders[stage];
2741 if (!sh) {
2742 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2743 return;
2744 }
2745
2746 struct gl_program *p = shProg->_LinkedShaders[stage]->Program;
2747 if (index >= p->sh.NumSubroutineUniforms) {
2748 _mesa_error(ctx, GL_INVALID_VALUE, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name);
2749 return;
2750 }
2751
2752 switch (pname) {
2753 case GL_NUM_COMPATIBLE_SUBROUTINES: {
2754 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2755 if (res) {
2756 uni = res->Data;
2757 values[0] = uni->num_compatible_subroutines;
2758 }
2759 break;
2760 }
2761 case GL_COMPATIBLE_SUBROUTINES: {
2762 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2763 if (res) {
2764 uni = res->Data;
2765 count = 0;
2766 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
2767 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
2768 for (j = 0; j < fn->num_compat_types; j++) {
2769 if (fn->types[j] == uni->type) {
2770 values[count++] = i;
2771 break;
2772 }
2773 }
2774 }
2775 }
2776 break;
2777 }
2778 case GL_UNIFORM_SIZE:
2779 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2780 if (res) {
2781 uni = res->Data;
2782 values[0] = uni->array_elements ? uni->array_elements : 1;
2783 }
2784 break;
2785 case GL_UNIFORM_NAME_LENGTH:
2786 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2787 if (res) {
2788 values[0] = strlen(_mesa_program_resource_name(res)) + 1
2789 + ((_mesa_program_resource_array_size(res) != 0) ? 3 : 0);
2790 }
2791 break;
2792 default:
2793 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2794 return;
2795 }
2796 }
2797
2798
2799 GLvoid GLAPIENTRY
2800 _mesa_GetActiveSubroutineUniformName(GLuint program, GLenum shadertype,
2801 GLuint index, GLsizei bufsize,
2802 GLsizei *length, GLchar *name)
2803 {
2804 GET_CURRENT_CONTEXT(ctx);
2805 const char *api_name = "glGetActiveSubroutineUniformName";
2806 struct gl_shader_program *shProg;
2807 GLenum resource_type;
2808 gl_shader_stage stage;
2809
2810 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2811 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2812 return;
2813 }
2814
2815 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2816 if (!shProg)
2817 return;
2818
2819 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2820 if (!shProg->_LinkedShaders[stage]) {
2821 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2822 return;
2823 }
2824
2825 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
2826 /* get program resource name */
2827 _mesa_get_program_resource_name(shProg, resource_type,
2828 index, bufsize,
2829 length, name, api_name);
2830 }
2831
2832
2833 GLvoid GLAPIENTRY
2834 _mesa_GetActiveSubroutineName(GLuint program, GLenum shadertype,
2835 GLuint index, GLsizei bufsize,
2836 GLsizei *length, GLchar *name)
2837 {
2838 GET_CURRENT_CONTEXT(ctx);
2839 const char *api_name = "glGetActiveSubroutineName";
2840 struct gl_shader_program *shProg;
2841 GLenum resource_type;
2842 gl_shader_stage stage;
2843
2844 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2845 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2846 return;
2847 }
2848
2849 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2850 if (!shProg)
2851 return;
2852
2853 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2854 if (!shProg->_LinkedShaders[stage]) {
2855 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2856 return;
2857 }
2858 resource_type = _mesa_shader_stage_to_subroutine(stage);
2859 _mesa_get_program_resource_name(shProg, resource_type,
2860 index, bufsize,
2861 length, name, api_name);
2862 }
2863
2864 GLvoid GLAPIENTRY
2865 _mesa_UniformSubroutinesuiv(GLenum shadertype, GLsizei count,
2866 const GLuint *indices)
2867 {
2868 GET_CURRENT_CONTEXT(ctx);
2869 const char *api_name = "glUniformSubroutinesuiv";
2870 gl_shader_stage stage;
2871 int i;
2872
2873 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2874 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2875 return;
2876 }
2877
2878 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2879 struct gl_program *p = ctx->_Shader->CurrentProgram[stage];
2880 if (!p) {
2881 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2882 return;
2883 }
2884
2885 if (count != p->sh.NumSubroutineUniformRemapTable) {
2886 _mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
2887 return;
2888 }
2889
2890 i = 0;
2891 bool flushed = false;
2892 do {
2893 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[i];
2894 if (uni == NULL) {
2895 i++;
2896 continue;
2897 }
2898
2899 if (!flushed) {
2900 _mesa_flush_vertices_for_uniforms(ctx, uni);
2901 flushed = true;
2902 }
2903
2904 int uni_count = uni->array_elements ? uni->array_elements : 1;
2905 int j, k, f;
2906
2907 for (j = i; j < i + uni_count; j++) {
2908 struct gl_subroutine_function *subfn = NULL;
2909 if (indices[j] > p->sh.MaxSubroutineFunctionIndex) {
2910 _mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
2911 return;
2912 }
2913
2914 for (f = 0; f < p->sh.NumSubroutineFunctions; f++) {
2915 if (p->sh.SubroutineFunctions[f].index == indices[j])
2916 subfn = &p->sh.SubroutineFunctions[f];
2917 }
2918
2919 if (!subfn) {
2920 continue;
2921 }
2922
2923 for (k = 0; k < subfn->num_compat_types; k++) {
2924 if (subfn->types[k] == uni->type)
2925 break;
2926 }
2927 if (k == subfn->num_compat_types) {
2928 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2929 return;
2930 }
2931
2932 ctx->SubroutineIndex[p->info.stage].IndexPtr[j] = indices[j];
2933 }
2934 i += uni_count;
2935 } while(i < count);
2936 }
2937
2938
2939 GLvoid GLAPIENTRY
2940 _mesa_GetUniformSubroutineuiv(GLenum shadertype, GLint location,
2941 GLuint *params)
2942 {
2943 GET_CURRENT_CONTEXT(ctx);
2944 const char *api_name = "glGetUniformSubroutineuiv";
2945 gl_shader_stage stage;
2946
2947 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2948 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2949 return;
2950 }
2951
2952 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2953 struct gl_program *p = ctx->_Shader->CurrentProgram[stage];
2954 if (!p) {
2955 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2956 return;
2957 }
2958
2959 if (location >= p->sh.NumSubroutineUniformRemapTable) {
2960 _mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
2961 return;
2962 }
2963
2964 *params = ctx->SubroutineIndex[p->info.stage].IndexPtr[location];
2965 }
2966
2967
2968 GLvoid GLAPIENTRY
2969 _mesa_GetProgramStageiv(GLuint program, GLenum shadertype,
2970 GLenum pname, GLint *values)
2971 {
2972 GET_CURRENT_CONTEXT(ctx);
2973 const char *api_name = "glGetProgramStageiv";
2974 struct gl_shader_program *shProg;
2975 struct gl_linked_shader *sh;
2976 gl_shader_stage stage;
2977
2978 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2979 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2980 return;
2981 }
2982
2983 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2984 if (!shProg)
2985 return;
2986
2987 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2988 sh = shProg->_LinkedShaders[stage];
2989
2990 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2991 * INVALID_OPERATION in the case of not be linked.
2992 *
2993 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2994 * same info using other specs (ARB_program_interface_query), without the
2995 * need of the program to be linked, being the value for that case 0.
2996 *
2997 * But at the same time, some other methods require the program to be
2998 * linked for pname related to locations, so it would be inconsistent to
2999 * not do the same here. So we are:
3000 * * Return GL_INVALID_OPERATION if not linked only for locations.
3001 * * Setting a default value of 0, to be returned if not linked.
3002 */
3003 if (!sh) {
3004 values[0] = 0;
3005 if (pname == GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS) {
3006 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
3007 }
3008 return;
3009 }
3010
3011 struct gl_program *p = sh->Program;
3012 switch (pname) {
3013 case GL_ACTIVE_SUBROUTINES:
3014 values[0] = p->sh.NumSubroutineFunctions;
3015 break;
3016 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS:
3017 values[0] = p->sh.NumSubroutineUniformRemapTable;
3018 break;
3019 case GL_ACTIVE_SUBROUTINE_UNIFORMS:
3020 values[0] = p->sh.NumSubroutineUniforms;
3021 break;
3022 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH:
3023 {
3024 unsigned i;
3025 GLint max_len = 0;
3026 GLenum resource_type;
3027 struct gl_program_resource *res;
3028
3029 resource_type = _mesa_shader_stage_to_subroutine(stage);
3030 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
3031 res = _mesa_program_resource_find_index(shProg, resource_type, i);
3032 if (res) {
3033 const GLint len = strlen(_mesa_program_resource_name(res)) + 1;
3034 if (len > max_len)
3035 max_len = len;
3036 }
3037 }
3038 values[0] = max_len;
3039 break;
3040 }
3041 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH:
3042 {
3043 unsigned i;
3044 GLint max_len = 0;
3045 GLenum resource_type;
3046 struct gl_program_resource *res;
3047
3048 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
3049 for (i = 0; i < p->sh.NumSubroutineUniformRemapTable; i++) {
3050 res = _mesa_program_resource_find_index(shProg, resource_type, i);
3051 if (res) {
3052 const GLint len = strlen(_mesa_program_resource_name(res)) + 1
3053 + ((_mesa_program_resource_array_size(res) != 0) ? 3 : 0);
3054
3055 if (len > max_len)
3056 max_len = len;
3057 }
3058 }
3059 values[0] = max_len;
3060 break;
3061 }
3062 default:
3063 _mesa_error(ctx, GL_INVALID_ENUM, "%s", api_name);
3064 values[0] = -1;
3065 break;
3066 }
3067 }
3068
3069 static int
3070 find_compat_subroutine(struct gl_program *p, const struct glsl_type *type)
3071 {
3072 int i, j;
3073
3074 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
3075 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
3076 for (j = 0; j < fn->num_compat_types; j++) {
3077 if (fn->types[j] == type)
3078 return i;
3079 }
3080 }
3081 return 0;
3082 }
3083
3084 static void
3085 _mesa_shader_write_subroutine_index(struct gl_context *ctx,
3086 struct gl_program *p)
3087 {
3088 int i, j;
3089
3090 if (p->sh.NumSubroutineUniformRemapTable == 0)
3091 return;
3092
3093 i = 0;
3094 do {
3095 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[i];
3096 int uni_count;
3097 int val;
3098
3099 if (!uni) {
3100 i++;
3101 continue;
3102 }
3103
3104 uni_count = uni->array_elements ? uni->array_elements : 1;
3105 for (j = 0; j < uni_count; j++) {
3106 val = ctx->SubroutineIndex[p->info.stage].IndexPtr[i + j];
3107 memcpy(&uni->storage[j], &val, sizeof(int));
3108 }
3109
3110 _mesa_propagate_uniforms_to_driver_storage(uni, 0, uni_count);
3111 i += uni_count;
3112 } while(i < p->sh.NumSubroutineUniformRemapTable);
3113 }
3114
3115 void
3116 _mesa_shader_write_subroutine_indices(struct gl_context *ctx,
3117 gl_shader_stage stage)
3118 {
3119 if (ctx->_Shader->CurrentProgram[stage])
3120 _mesa_shader_write_subroutine_index(ctx,
3121 ctx->_Shader->CurrentProgram[stage]);
3122 }
3123
3124 void
3125 _mesa_program_init_subroutine_defaults(struct gl_context *ctx,
3126 struct gl_program *p)
3127 {
3128 assert(p);
3129
3130 struct gl_subroutine_index_binding *binding = &ctx->SubroutineIndex[p->info.stage];
3131 if (binding->NumIndex != p->sh.NumSubroutineUniformRemapTable) {
3132 binding->IndexPtr = realloc(binding->IndexPtr,
3133 p->sh.NumSubroutineUniformRemapTable * (sizeof(GLuint)));
3134 binding->NumIndex = p->sh.NumSubroutineUniformRemapTable;
3135 }
3136
3137 for (int i = 0; i < p->sh.NumSubroutineUniformRemapTable; i++) {
3138 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[i];
3139
3140 if (!uni)
3141 continue;
3142
3143 binding->IndexPtr[i] = find_compat_subroutine(p, uni->type);
3144 }
3145 }