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