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