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