mesa: fixup cast expression
[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 struct update_programs_in_pipeline_params
1253 {
1254 struct gl_context *ctx;
1255 struct gl_shader_program *shProg;
1256 };
1257
1258 static void
1259 update_programs_in_pipeline(GLuint key, void *data, void *userData)
1260 {
1261 struct update_programs_in_pipeline_params *params =
1262 (struct update_programs_in_pipeline_params *) userData;
1263 struct gl_pipeline_object *obj = (struct gl_pipeline_object *) data;
1264
1265 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1266 if (obj->CurrentProgram[stage] &&
1267 obj->CurrentProgram[stage]->Id == params->shProg->Name) {
1268 struct gl_program *prog = params->shProg->_LinkedShaders[stage]->Program;
1269 _mesa_use_program(params->ctx, stage, params->shProg, prog, obj);
1270 }
1271 }
1272 }
1273
1274
1275 /**
1276 * Link a program's shaders.
1277 */
1278 static ALWAYS_INLINE void
1279 link_program(struct gl_context *ctx, struct gl_shader_program *shProg,
1280 bool no_error)
1281 {
1282 if (!shProg)
1283 return;
1284
1285 if (!no_error) {
1286 /* From the ARB_transform_feedback2 specification:
1287 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1288 * is the name of a program being used by one or more transform feedback
1289 * objects, even if the objects are not currently bound or are paused."
1290 */
1291 if (_mesa_transform_feedback_is_using_program(ctx, shProg)) {
1292 _mesa_error(ctx, GL_INVALID_OPERATION,
1293 "glLinkProgram(transform feedback is using the program)");
1294 return;
1295 }
1296 }
1297
1298 unsigned programs_in_use = 0;
1299 if (ctx->_Shader)
1300 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1301 if (ctx->_Shader->CurrentProgram[stage] &&
1302 ctx->_Shader->CurrentProgram[stage]->Id == shProg->Name) {
1303 programs_in_use |= 1 << stage;
1304 }
1305 }
1306
1307 ensure_builtin_types(ctx);
1308
1309 FLUSH_VERTICES(ctx, 0);
1310 _mesa_glsl_link_shader(ctx, shProg);
1311
1312 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1313 *
1314 * "If LinkProgram or ProgramBinary successfully re-links a program
1315 * object that is active for any shader stage, then the newly generated
1316 * executable code will be installed as part of the current rendering
1317 * state for all shader stages where the program is active.
1318 * Additionally, the newly generated executable code is made part of
1319 * the state of any program pipeline for all stages where the program
1320 * is attached."
1321 */
1322 if (shProg->data->LinkStatus) {
1323 while (programs_in_use) {
1324 const int stage = u_bit_scan(&programs_in_use);
1325
1326 struct gl_program *prog = NULL;
1327 if (shProg->_LinkedShaders[stage])
1328 prog = shProg->_LinkedShaders[stage]->Program;
1329
1330 _mesa_use_program(ctx, stage, shProg, prog, ctx->_Shader);
1331 }
1332
1333 if (ctx->Pipeline.Objects) {
1334 struct update_programs_in_pipeline_params params = {
1335 .ctx = ctx,
1336 .shProg = shProg
1337 };
1338 _mesa_HashWalk(ctx->Pipeline.Objects, update_programs_in_pipeline,
1339 &params);
1340 }
1341 }
1342
1343 /* Capture .shader_test files. */
1344 const char *capture_path = _mesa_get_shader_capture_path();
1345 if (shProg->Name != 0 && shProg->Name != ~0 && capture_path != NULL) {
1346 /* Find an unused filename. */
1347 FILE *file = NULL;
1348 char *filename = NULL;
1349 for (unsigned i = 0;; i++) {
1350 if (i) {
1351 filename = ralloc_asprintf(NULL, "%s/%u-%u.shader_test",
1352 capture_path, shProg->Name, i);
1353 } else {
1354 filename = ralloc_asprintf(NULL, "%s/%u.shader_test",
1355 capture_path, shProg->Name);
1356 }
1357 file = os_file_create_unique(filename, 0644);
1358 if (file)
1359 break;
1360 /* If we are failing for another reason than "this filename already
1361 * exists", we are likely to fail again with another filename, so
1362 * let's just give up */
1363 if (errno != EEXIST)
1364 break;
1365 ralloc_free(filename);
1366 }
1367 if (file) {
1368 fprintf(file, "[require]\nGLSL%s >= %u.%02u\n",
1369 shProg->IsES ? " ES" : "",
1370 shProg->data->Version / 100, shProg->data->Version % 100);
1371 if (shProg->SeparateShader)
1372 fprintf(file, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1373 fprintf(file, "\n");
1374
1375 for (unsigned i = 0; i < shProg->NumShaders; i++) {
1376 fprintf(file, "[%s shader]\n%s\n",
1377 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
1378 shProg->Shaders[i]->Source);
1379 }
1380 fclose(file);
1381 } else {
1382 _mesa_warning(ctx, "Failed to open %s", filename);
1383 }
1384
1385 ralloc_free(filename);
1386 }
1387
1388 if (shProg->data->LinkStatus == LINKING_FAILURE &&
1389 (ctx->_Shader->Flags & GLSL_REPORT_ERRORS)) {
1390 _mesa_debug(ctx, "Error linking program %u:\n%s\n",
1391 shProg->Name, shProg->data->InfoLog);
1392 }
1393
1394 _mesa_update_vertex_processing_mode(ctx);
1395
1396 shProg->BinaryRetrievableHint = shProg->BinaryRetrievableHintPending;
1397
1398 /* debug code */
1399 if (0) {
1400 GLuint i;
1401
1402 printf("Link %u shaders in program %u: %s\n",
1403 shProg->NumShaders, shProg->Name,
1404 shProg->data->LinkStatus ? "Success" : "Failed");
1405
1406 for (i = 0; i < shProg->NumShaders; i++) {
1407 printf(" shader %u, stage %u\n",
1408 shProg->Shaders[i]->Name,
1409 shProg->Shaders[i]->Stage);
1410 }
1411 }
1412 }
1413
1414
1415 static void
1416 link_program_error(struct gl_context *ctx, struct gl_shader_program *shProg)
1417 {
1418 link_program(ctx, shProg, false);
1419 }
1420
1421
1422 static void
1423 link_program_no_error(struct gl_context *ctx, struct gl_shader_program *shProg)
1424 {
1425 link_program(ctx, shProg, true);
1426 }
1427
1428
1429 void
1430 _mesa_link_program(struct gl_context *ctx, struct gl_shader_program *shProg)
1431 {
1432 link_program_error(ctx, shProg);
1433 }
1434
1435
1436 /**
1437 * Print basic shader info (for debug).
1438 */
1439 static void
1440 print_shader_info(const struct gl_shader_program *shProg)
1441 {
1442 GLuint i;
1443
1444 printf("Mesa: glUseProgram(%u)\n", shProg->Name);
1445 for (i = 0; i < shProg->NumShaders; i++) {
1446 #ifdef DEBUG
1447 printf(" %s shader %u, checksum %u\n",
1448 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
1449 shProg->Shaders[i]->Name,
1450 shProg->Shaders[i]->SourceChecksum);
1451 #else
1452 printf(" %s shader %u\n",
1453 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
1454 shProg->Shaders[i]->Name);
1455 #endif
1456 }
1457 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX])
1458 printf(" vert prog %u\n",
1459 shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
1460 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT])
1461 printf(" frag prog %u\n",
1462 shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->Id);
1463 if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
1464 printf(" geom prog %u\n",
1465 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
1466 if (shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL])
1467 printf(" tesc prog %u\n",
1468 shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program->Id);
1469 if (shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL])
1470 printf(" tese prog %u\n",
1471 shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->Program->Id);
1472 }
1473
1474
1475 /**
1476 * Use the named shader program for subsequent glUniform calls
1477 */
1478 void
1479 _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
1480 const char *caller)
1481 {
1482 if ((shProg != NULL) && !shProg->data->LinkStatus) {
1483 _mesa_error(ctx, GL_INVALID_OPERATION,
1484 "%s(program %u not linked)", caller, shProg->Name);
1485 return;
1486 }
1487
1488 if (ctx->Shader.ActiveProgram != shProg) {
1489 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
1490 }
1491 }
1492
1493
1494 /**
1495 * Use the named shader program for subsequent rendering.
1496 */
1497 void
1498 _mesa_use_shader_program(struct gl_context *ctx,
1499 struct gl_shader_program *shProg)
1500 {
1501 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1502 struct gl_program *new_prog = NULL;
1503 if (shProg && shProg->_LinkedShaders[i])
1504 new_prog = shProg->_LinkedShaders[i]->Program;
1505 _mesa_use_program(ctx, i, shProg, new_prog, &ctx->Shader);
1506 }
1507 _mesa_active_program(ctx, shProg, "glUseProgram");
1508 }
1509
1510
1511 /**
1512 * Do validation of the given shader program.
1513 * \param errMsg returns error message if validation fails.
1514 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1515 */
1516 static GLboolean
1517 validate_shader_program(const struct gl_shader_program *shProg,
1518 char *errMsg)
1519 {
1520 if (!shProg->data->LinkStatus) {
1521 return GL_FALSE;
1522 }
1523
1524 /* From the GL spec, a program is invalid if any of these are true:
1525
1526 any two active samplers in the current program object are of
1527 different types, but refer to the same texture image unit,
1528
1529 any active sampler in the current program object refers to a texture
1530 image unit where fixed-function fragment processing accesses a
1531 texture target that does not match the sampler type, or
1532
1533 the sum of the number of active samplers in the program and the
1534 number of texture image units enabled for fixed-function fragment
1535 processing exceeds the combined limit on the total number of texture
1536 image units allowed.
1537 */
1538
1539 /*
1540 * Check: any two active samplers in the current program object are of
1541 * different types, but refer to the same texture image unit,
1542 */
1543 if (!_mesa_sampler_uniforms_are_valid(shProg, errMsg, 100))
1544 return GL_FALSE;
1545
1546 return GL_TRUE;
1547 }
1548
1549
1550 /**
1551 * Called via glValidateProgram()
1552 */
1553 static void
1554 validate_program(struct gl_context *ctx, GLuint program)
1555 {
1556 struct gl_shader_program *shProg;
1557 char errMsg[100] = "";
1558
1559 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1560 if (!shProg) {
1561 return;
1562 }
1563
1564 shProg->data->Validated = validate_shader_program(shProg, errMsg);
1565 if (!shProg->data->Validated) {
1566 /* update info log */
1567 if (shProg->data->InfoLog) {
1568 ralloc_free(shProg->data->InfoLog);
1569 }
1570 shProg->data->InfoLog = ralloc_strdup(shProg->data, errMsg);
1571 }
1572 }
1573
1574
1575 void GLAPIENTRY
1576 _mesa_AttachObjectARB_no_error(GLhandleARB program, GLhandleARB shader)
1577 {
1578 GET_CURRENT_CONTEXT(ctx);
1579 attach_shader_no_error(ctx, program, shader);
1580 }
1581
1582
1583 void GLAPIENTRY
1584 _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
1585 {
1586 GET_CURRENT_CONTEXT(ctx);
1587 attach_shader_err(ctx, program, shader, "glAttachObjectARB");
1588 }
1589
1590
1591 void GLAPIENTRY
1592 _mesa_AttachShader_no_error(GLuint program, GLuint shader)
1593 {
1594 GET_CURRENT_CONTEXT(ctx);
1595 attach_shader_no_error(ctx, program, shader);
1596 }
1597
1598
1599 void GLAPIENTRY
1600 _mesa_AttachShader(GLuint program, GLuint shader)
1601 {
1602 GET_CURRENT_CONTEXT(ctx);
1603 attach_shader_err(ctx, program, shader, "glAttachShader");
1604 }
1605
1606
1607 void GLAPIENTRY
1608 _mesa_CompileShader(GLuint shaderObj)
1609 {
1610 GET_CURRENT_CONTEXT(ctx);
1611 if (MESA_VERBOSE & VERBOSE_API)
1612 _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
1613 _mesa_compile_shader(ctx, _mesa_lookup_shader_err(ctx, shaderObj,
1614 "glCompileShader"));
1615 }
1616
1617
1618 GLuint GLAPIENTRY
1619 _mesa_CreateShader_no_error(GLenum type)
1620 {
1621 GET_CURRENT_CONTEXT(ctx);
1622 return create_shader(ctx, type);
1623 }
1624
1625
1626 GLuint GLAPIENTRY
1627 _mesa_CreateShader(GLenum type)
1628 {
1629 GET_CURRENT_CONTEXT(ctx);
1630
1631 if (MESA_VERBOSE & VERBOSE_API)
1632 _mesa_debug(ctx, "glCreateShader %s\n", _mesa_enum_to_string(type));
1633
1634 return create_shader_err(ctx, type, "glCreateShader");
1635 }
1636
1637
1638 GLhandleARB GLAPIENTRY
1639 _mesa_CreateShaderObjectARB_no_error(GLenum type)
1640 {
1641 GET_CURRENT_CONTEXT(ctx);
1642 return create_shader(ctx, type);
1643 }
1644
1645
1646 GLhandleARB GLAPIENTRY
1647 _mesa_CreateShaderObjectARB(GLenum type)
1648 {
1649 GET_CURRENT_CONTEXT(ctx);
1650 return create_shader_err(ctx, type, "glCreateShaderObjectARB");
1651 }
1652
1653
1654 GLuint GLAPIENTRY
1655 _mesa_CreateProgram(void)
1656 {
1657 GET_CURRENT_CONTEXT(ctx);
1658 if (MESA_VERBOSE & VERBOSE_API)
1659 _mesa_debug(ctx, "glCreateProgram\n");
1660 return create_shader_program(ctx);
1661 }
1662
1663
1664 GLhandleARB GLAPIENTRY
1665 _mesa_CreateProgramObjectARB(void)
1666 {
1667 GET_CURRENT_CONTEXT(ctx);
1668 return create_shader_program(ctx);
1669 }
1670
1671
1672 void GLAPIENTRY
1673 _mesa_DeleteObjectARB(GLhandleARB obj)
1674 {
1675 if (MESA_VERBOSE & VERBOSE_API) {
1676 GET_CURRENT_CONTEXT(ctx);
1677 _mesa_debug(ctx, "glDeleteObjectARB(%lu)\n", (unsigned long)obj);
1678 }
1679
1680 if (obj) {
1681 GET_CURRENT_CONTEXT(ctx);
1682 FLUSH_VERTICES(ctx, 0);
1683 if (is_program(ctx, obj)) {
1684 delete_shader_program(ctx, obj);
1685 }
1686 else if (is_shader(ctx, obj)) {
1687 delete_shader(ctx, obj);
1688 }
1689 else {
1690 /* error? */
1691 }
1692 }
1693 }
1694
1695
1696 void GLAPIENTRY
1697 _mesa_DeleteProgram(GLuint name)
1698 {
1699 if (name) {
1700 GET_CURRENT_CONTEXT(ctx);
1701 FLUSH_VERTICES(ctx, 0);
1702 delete_shader_program(ctx, name);
1703 }
1704 }
1705
1706
1707 void GLAPIENTRY
1708 _mesa_DeleteShader(GLuint name)
1709 {
1710 if (name) {
1711 GET_CURRENT_CONTEXT(ctx);
1712 FLUSH_VERTICES(ctx, 0);
1713 delete_shader(ctx, name);
1714 }
1715 }
1716
1717
1718 void GLAPIENTRY
1719 _mesa_DetachObjectARB_no_error(GLhandleARB program, GLhandleARB shader)
1720 {
1721 GET_CURRENT_CONTEXT(ctx);
1722 detach_shader_no_error(ctx, program, shader);
1723 }
1724
1725
1726 void GLAPIENTRY
1727 _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
1728 {
1729 GET_CURRENT_CONTEXT(ctx);
1730 detach_shader_error(ctx, program, shader);
1731 }
1732
1733
1734 void GLAPIENTRY
1735 _mesa_DetachShader_no_error(GLuint program, GLuint shader)
1736 {
1737 GET_CURRENT_CONTEXT(ctx);
1738 detach_shader_no_error(ctx, program, shader);
1739 }
1740
1741
1742 void GLAPIENTRY
1743 _mesa_DetachShader(GLuint program, GLuint shader)
1744 {
1745 GET_CURRENT_CONTEXT(ctx);
1746 detach_shader_error(ctx, program, shader);
1747 }
1748
1749
1750 void GLAPIENTRY
1751 _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
1752 GLsizei * count, GLhandleARB * obj)
1753 {
1754 GET_CURRENT_CONTEXT(ctx);
1755 get_attached_shaders(ctx, (GLuint)container, maxCount, count, NULL, obj);
1756 }
1757
1758
1759 void GLAPIENTRY
1760 _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
1761 GLsizei *count, GLuint *obj)
1762 {
1763 GET_CURRENT_CONTEXT(ctx);
1764 get_attached_shaders(ctx, program, maxCount, count, obj, NULL);
1765 }
1766
1767
1768 void GLAPIENTRY
1769 _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
1770 GLcharARB * infoLog)
1771 {
1772 GET_CURRENT_CONTEXT(ctx);
1773 if (is_program(ctx, object)) {
1774 get_program_info_log(ctx, object, maxLength, length, infoLog);
1775 }
1776 else if (is_shader(ctx, object)) {
1777 get_shader_info_log(ctx, object, maxLength, length, infoLog);
1778 }
1779 else {
1780 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
1781 }
1782 }
1783
1784
1785 void GLAPIENTRY
1786 _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
1787 {
1788 GET_CURRENT_CONTEXT(ctx);
1789 /* Implement in terms of GetProgramiv, GetShaderiv */
1790 if (is_program(ctx, object)) {
1791 if (pname == GL_OBJECT_TYPE_ARB) {
1792 *params = GL_PROGRAM_OBJECT_ARB;
1793 }
1794 else {
1795 get_programiv(ctx, object, pname, params);
1796 }
1797 }
1798 else if (is_shader(ctx, object)) {
1799 if (pname == GL_OBJECT_TYPE_ARB) {
1800 *params = GL_SHADER_OBJECT_ARB;
1801 }
1802 else {
1803 get_shaderiv(ctx, object, pname, params);
1804 }
1805 }
1806 else {
1807 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
1808 }
1809 }
1810
1811
1812 void GLAPIENTRY
1813 _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
1814 GLfloat *params)
1815 {
1816 GLint iparams[1] = {0}; /* XXX is one element enough? */
1817 _mesa_GetObjectParameterivARB(object, pname, iparams);
1818 params[0] = (GLfloat) iparams[0];
1819 }
1820
1821
1822 void GLAPIENTRY
1823 _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
1824 {
1825 GET_CURRENT_CONTEXT(ctx);
1826 get_programiv(ctx, program, pname, params);
1827 }
1828
1829
1830 void GLAPIENTRY
1831 _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
1832 {
1833 GET_CURRENT_CONTEXT(ctx);
1834 get_shaderiv(ctx, shader, pname, params);
1835 }
1836
1837
1838 void GLAPIENTRY
1839 _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
1840 GLsizei *length, GLchar *infoLog)
1841 {
1842 GET_CURRENT_CONTEXT(ctx);
1843 get_program_info_log(ctx, program, bufSize, length, infoLog);
1844 }
1845
1846
1847 void GLAPIENTRY
1848 _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
1849 GLsizei *length, GLchar *infoLog)
1850 {
1851 GET_CURRENT_CONTEXT(ctx);
1852 get_shader_info_log(ctx, shader, bufSize, length, infoLog);
1853 }
1854
1855
1856 void GLAPIENTRY
1857 _mesa_GetShaderSource(GLuint shader, GLsizei maxLength,
1858 GLsizei *length, GLchar *sourceOut)
1859 {
1860 GET_CURRENT_CONTEXT(ctx);
1861 get_shader_source(ctx, shader, maxLength, length, sourceOut);
1862 }
1863
1864
1865 GLhandleARB GLAPIENTRY
1866 _mesa_GetHandleARB(GLenum pname)
1867 {
1868 GET_CURRENT_CONTEXT(ctx);
1869 return get_handle(ctx, pname);
1870 }
1871
1872
1873 GLboolean GLAPIENTRY
1874 _mesa_IsProgram(GLuint name)
1875 {
1876 GET_CURRENT_CONTEXT(ctx);
1877 return is_program(ctx, name);
1878 }
1879
1880
1881 GLboolean GLAPIENTRY
1882 _mesa_IsShader(GLuint name)
1883 {
1884 GET_CURRENT_CONTEXT(ctx);
1885 return is_shader(ctx, name);
1886 }
1887
1888
1889 void GLAPIENTRY
1890 _mesa_LinkProgram_no_error(GLuint programObj)
1891 {
1892 GET_CURRENT_CONTEXT(ctx);
1893
1894 struct gl_shader_program *shProg =
1895 _mesa_lookup_shader_program(ctx, programObj);
1896 link_program_no_error(ctx, shProg);
1897 }
1898
1899
1900 void GLAPIENTRY
1901 _mesa_LinkProgram(GLuint programObj)
1902 {
1903 GET_CURRENT_CONTEXT(ctx);
1904
1905 if (MESA_VERBOSE & VERBOSE_API)
1906 _mesa_debug(ctx, "glLinkProgram %u\n", programObj);
1907
1908 struct gl_shader_program *shProg =
1909 _mesa_lookup_shader_program_err(ctx, programObj, "glLinkProgram");
1910 link_program_error(ctx, shProg);
1911 }
1912
1913 #ifdef ENABLE_SHADER_CACHE
1914 /**
1915 * Generate a SHA-1 hash value string for given source string.
1916 */
1917 static void
1918 generate_sha1(const char *source, char sha_str[64])
1919 {
1920 unsigned char sha[20];
1921 _mesa_sha1_compute(source, strlen(source), sha);
1922 _mesa_sha1_format(sha_str, sha);
1923 }
1924
1925 /**
1926 * Construct a full path for shader replacement functionality using
1927 * following format:
1928 *
1929 * <path>/<stage prefix>_<CHECKSUM>.glsl
1930 * <path>/<stage prefix>_<CHECKSUM>.arb
1931 */
1932 static char *
1933 construct_name(const gl_shader_stage stage, const char *source,
1934 const char *path)
1935 {
1936 char sha[64];
1937 static const char *types[] = {
1938 "VS", "TC", "TE", "GS", "FS", "CS",
1939 };
1940
1941 const char *format = strncmp(source, "!!ARB", 5) ? "glsl" : "arb";
1942
1943 generate_sha1(source, sha);
1944 return ralloc_asprintf(NULL, "%s/%s_%s.%s", path, types[stage], sha, format);
1945 }
1946
1947 /**
1948 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1949 */
1950 void
1951 _mesa_dump_shader_source(const gl_shader_stage stage, const char *source)
1952 {
1953 static bool path_exists = true;
1954 char *dump_path;
1955 FILE *f;
1956
1957 if (!path_exists)
1958 return;
1959
1960 dump_path = getenv("MESA_SHADER_DUMP_PATH");
1961 if (!dump_path) {
1962 path_exists = false;
1963 return;
1964 }
1965
1966 char *name = construct_name(stage, source, dump_path);
1967
1968 f = fopen(name, "w");
1969 if (f) {
1970 fputs(source, f);
1971 fclose(f);
1972 } else {
1973 GET_CURRENT_CONTEXT(ctx);
1974 _mesa_warning(ctx, "could not open %s for dumping shader (%s)", name,
1975 strerror(errno));
1976 }
1977 ralloc_free(name);
1978 }
1979
1980 /**
1981 * Read shader source code from a file.
1982 * Useful for debugging to override an app's shader.
1983 */
1984 GLcharARB *
1985 _mesa_read_shader_source(const gl_shader_stage stage, const char *source)
1986 {
1987 char *read_path;
1988 static bool path_exists = true;
1989 int len, shader_size = 0;
1990 GLcharARB *buffer;
1991 FILE *f;
1992
1993 if (!path_exists)
1994 return NULL;
1995
1996 read_path = getenv("MESA_SHADER_READ_PATH");
1997 if (!read_path) {
1998 path_exists = false;
1999 return NULL;
2000 }
2001
2002 char *name = construct_name(stage, source, read_path);
2003 f = fopen(name, "r");
2004 ralloc_free(name);
2005 if (!f)
2006 return NULL;
2007
2008 /* allocate enough room for the entire shader */
2009 fseek(f, 0, SEEK_END);
2010 shader_size = ftell(f);
2011 rewind(f);
2012 assert(shader_size);
2013
2014 /* add one for terminating zero */
2015 shader_size++;
2016
2017 buffer = malloc(shader_size);
2018 assert(buffer);
2019
2020 len = fread(buffer, 1, shader_size, f);
2021 buffer[len] = 0;
2022
2023 fclose(f);
2024
2025 return buffer;
2026 }
2027
2028 #endif /* ENABLE_SHADER_CACHE */
2029
2030 /**
2031 * Called via glShaderSource() and glShaderSourceARB() API functions.
2032 * Basically, concatenate the source code strings into one long string
2033 * and pass it to _mesa_shader_source().
2034 */
2035 static ALWAYS_INLINE void
2036 shader_source(struct gl_context *ctx, GLuint shaderObj, GLsizei count,
2037 const GLchar *const *string, const GLint *length, bool no_error)
2038 {
2039 GLint *offsets;
2040 GLsizei i, totalLength;
2041 GLcharARB *source;
2042 struct gl_shader *sh;
2043
2044 if (!no_error) {
2045 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glShaderSourceARB");
2046 if (!sh)
2047 return;
2048
2049 if (string == NULL) {
2050 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
2051 return;
2052 }
2053 } else {
2054 sh = _mesa_lookup_shader(ctx, shaderObj);
2055 }
2056
2057 /*
2058 * This array holds offsets of where the appropriate string ends, thus the
2059 * last element will be set to the total length of the source code.
2060 */
2061 offsets = malloc(count * sizeof(GLint));
2062 if (offsets == NULL) {
2063 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
2064 return;
2065 }
2066
2067 for (i = 0; i < count; i++) {
2068 if (!no_error && string[i] == NULL) {
2069 free((GLvoid *) offsets);
2070 _mesa_error(ctx, GL_INVALID_OPERATION,
2071 "glShaderSourceARB(null string)");
2072 return;
2073 }
2074 if (length == NULL || length[i] < 0)
2075 offsets[i] = strlen(string[i]);
2076 else
2077 offsets[i] = length[i];
2078 /* accumulate string lengths */
2079 if (i > 0)
2080 offsets[i] += offsets[i - 1];
2081 }
2082
2083 /* Total length of source string is sum off all strings plus two.
2084 * One extra byte for terminating zero, another extra byte to silence
2085 * valgrind warnings in the parser/grammer code.
2086 */
2087 totalLength = offsets[count - 1] + 2;
2088 source = malloc(totalLength * sizeof(GLcharARB));
2089 if (source == NULL) {
2090 free((GLvoid *) offsets);
2091 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
2092 return;
2093 }
2094
2095 for (i = 0; i < count; i++) {
2096 GLint start = (i > 0) ? offsets[i - 1] : 0;
2097 memcpy(source + start, string[i],
2098 (offsets[i] - start) * sizeof(GLcharARB));
2099 }
2100 source[totalLength - 1] = '\0';
2101 source[totalLength - 2] = '\0';
2102
2103 #ifdef ENABLE_SHADER_CACHE
2104 GLcharARB *replacement;
2105
2106 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
2107 * if corresponding entry found from MESA_SHADER_READ_PATH.
2108 */
2109 _mesa_dump_shader_source(sh->Stage, source);
2110
2111 replacement = _mesa_read_shader_source(sh->Stage, source);
2112 if (replacement) {
2113 free(source);
2114 source = replacement;
2115 }
2116 #endif /* ENABLE_SHADER_CACHE */
2117
2118 set_shader_source(sh, source);
2119
2120 free(offsets);
2121 }
2122
2123
2124 void GLAPIENTRY
2125 _mesa_ShaderSource_no_error(GLuint shaderObj, GLsizei count,
2126 const GLchar *const *string, const GLint *length)
2127 {
2128 GET_CURRENT_CONTEXT(ctx);
2129 shader_source(ctx, shaderObj, count, string, length, true);
2130 }
2131
2132
2133 void GLAPIENTRY
2134 _mesa_ShaderSource(GLuint shaderObj, GLsizei count,
2135 const GLchar *const *string, const GLint *length)
2136 {
2137 GET_CURRENT_CONTEXT(ctx);
2138 shader_source(ctx, shaderObj, count, string, length, false);
2139 }
2140
2141
2142 static ALWAYS_INLINE void
2143 use_program(GLuint program, bool no_error)
2144 {
2145 GET_CURRENT_CONTEXT(ctx);
2146 struct gl_shader_program *shProg = NULL;
2147
2148 if (MESA_VERBOSE & VERBOSE_API)
2149 _mesa_debug(ctx, "glUseProgram %u\n", program);
2150
2151 if (no_error) {
2152 if (program) {
2153 shProg = _mesa_lookup_shader_program(ctx, program);
2154 }
2155 } else {
2156 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
2157 _mesa_error(ctx, GL_INVALID_OPERATION,
2158 "glUseProgram(transform feedback active)");
2159 return;
2160 }
2161
2162 if (program) {
2163 shProg =
2164 _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
2165 if (!shProg)
2166 return;
2167
2168 if (!shProg->data->LinkStatus) {
2169 _mesa_error(ctx, GL_INVALID_OPERATION,
2170 "glUseProgram(program %u not linked)", program);
2171 return;
2172 }
2173
2174 /* debug code */
2175 if (ctx->_Shader->Flags & GLSL_USE_PROG) {
2176 print_shader_info(shProg);
2177 }
2178 }
2179 }
2180
2181 /* The ARB_separate_shader_object spec says:
2182 *
2183 * "The executable code for an individual shader stage is taken from
2184 * the current program for that stage. If there is a current program
2185 * object established by UseProgram, that program is considered current
2186 * for all stages. Otherwise, if there is a bound program pipeline
2187 * object (section 2.14.PPO), the program bound to the appropriate
2188 * stage of the pipeline object is considered current."
2189 */
2190 if (shProg) {
2191 /* Attach shader state to the binding point */
2192 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
2193 /* Update the program */
2194 _mesa_use_shader_program(ctx, shProg);
2195 } else {
2196 /* Must be done first: detach the progam */
2197 _mesa_use_shader_program(ctx, shProg);
2198 /* Unattach shader_state binding point */
2199 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
2200 ctx->Pipeline.Default);
2201 /* If a pipeline was bound, rebind it */
2202 if (ctx->Pipeline.Current) {
2203 if (no_error)
2204 _mesa_BindProgramPipeline_no_error(ctx->Pipeline.Current->Name);
2205 else
2206 _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
2207 }
2208 }
2209
2210 _mesa_update_vertex_processing_mode(ctx);
2211 }
2212
2213
2214 void GLAPIENTRY
2215 _mesa_UseProgram_no_error(GLuint program)
2216 {
2217 use_program(program, true);
2218 }
2219
2220
2221 void GLAPIENTRY
2222 _mesa_UseProgram(GLuint program)
2223 {
2224 use_program(program, false);
2225 }
2226
2227
2228 void GLAPIENTRY
2229 _mesa_ValidateProgram(GLuint program)
2230 {
2231 GET_CURRENT_CONTEXT(ctx);
2232 validate_program(ctx, program);
2233 }
2234
2235
2236 /**
2237 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2238 */
2239 void GLAPIENTRY
2240 _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
2241 GLint* range, GLint* precision)
2242 {
2243 const struct gl_program_constants *limits;
2244 const struct gl_precision *p;
2245 GET_CURRENT_CONTEXT(ctx);
2246
2247 switch (shadertype) {
2248 case GL_VERTEX_SHADER:
2249 limits = &ctx->Const.Program[MESA_SHADER_VERTEX];
2250 break;
2251 case GL_FRAGMENT_SHADER:
2252 limits = &ctx->Const.Program[MESA_SHADER_FRAGMENT];
2253 break;
2254 default:
2255 _mesa_error(ctx, GL_INVALID_ENUM,
2256 "glGetShaderPrecisionFormat(shadertype)");
2257 return;
2258 }
2259
2260 switch (precisiontype) {
2261 case GL_LOW_FLOAT:
2262 p = &limits->LowFloat;
2263 break;
2264 case GL_MEDIUM_FLOAT:
2265 p = &limits->MediumFloat;
2266 break;
2267 case GL_HIGH_FLOAT:
2268 p = &limits->HighFloat;
2269 break;
2270 case GL_LOW_INT:
2271 p = &limits->LowInt;
2272 break;
2273 case GL_MEDIUM_INT:
2274 p = &limits->MediumInt;
2275 break;
2276 case GL_HIGH_INT:
2277 p = &limits->HighInt;
2278 break;
2279 default:
2280 _mesa_error(ctx, GL_INVALID_ENUM,
2281 "glGetShaderPrecisionFormat(precisiontype)");
2282 return;
2283 }
2284
2285 range[0] = p->RangeMin;
2286 range[1] = p->RangeMax;
2287 precision[0] = p->Precision;
2288 }
2289
2290
2291 /**
2292 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2293 */
2294 void GLAPIENTRY
2295 _mesa_ReleaseShaderCompiler(void)
2296 {
2297 GET_CURRENT_CONTEXT(ctx);
2298
2299 if (ctx->shader_builtin_ref) {
2300 _mesa_glsl_builtin_functions_decref();
2301 ctx->shader_builtin_ref = false;
2302 }
2303 }
2304
2305
2306 /**
2307 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2308 */
2309 void GLAPIENTRY
2310 _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
2311 const void* binary, GLint length)
2312 {
2313 GET_CURRENT_CONTEXT(ctx);
2314 struct gl_shader **sh;
2315
2316 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2317 * page 88 of the OpenGL 4.5 specs state:
2318 *
2319 * "An INVALID_VALUE error is generated if count or length is negative.
2320 * An INVALID_ENUM error is generated if binaryformat is not a supported
2321 * format returned in SHADER_BINARY_FORMATS."
2322 */
2323 if (n < 0 || length < 0) {
2324 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderBinary(count or length < 0)");
2325 return;
2326 }
2327
2328 /* Get all shader objects at once so we can make the operation
2329 * all-or-nothing.
2330 */
2331 if (n > SIZE_MAX / sizeof(*sh)) {
2332 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderBinary(count)");
2333 return;
2334 }
2335
2336 sh = alloca(sizeof(*sh) * (size_t)n);
2337 if (!sh) {
2338 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderBinary");
2339 return;
2340 }
2341
2342 for (int i = 0; i < n; ++i) {
2343 sh[i] = _mesa_lookup_shader_err(ctx, shaders[i], "glShaderBinary");
2344 if (!sh[i])
2345 return;
2346 }
2347
2348 if (binaryformat == GL_SHADER_BINARY_FORMAT_SPIR_V_ARB) {
2349 if (!ctx->Extensions.ARB_gl_spirv) {
2350 _mesa_error(ctx, GL_INVALID_OPERATION, "glShaderBinary(SPIR-V)");
2351 } else if (n > 0) {
2352 _mesa_spirv_shader_binary(ctx, (unsigned) n, sh, binary,
2353 (size_t) length);
2354 }
2355
2356 return;
2357 }
2358
2359 _mesa_error(ctx, GL_INVALID_ENUM, "glShaderBinary(format)");
2360 }
2361
2362
2363 void GLAPIENTRY
2364 _mesa_GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei *length,
2365 GLenum *binaryFormat, GLvoid *binary)
2366 {
2367 struct gl_shader_program *shProg;
2368 GLsizei length_dummy;
2369 GET_CURRENT_CONTEXT(ctx);
2370
2371 if (bufSize < 0){
2372 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramBinary(bufSize < 0)");
2373 return;
2374 }
2375
2376 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramBinary");
2377 if (!shProg)
2378 return;
2379
2380 /* The ARB_get_program_binary spec says:
2381 *
2382 * "If <length> is NULL, then no length is returned."
2383 *
2384 * Ensure that length always points to valid storage to avoid multiple NULL
2385 * pointer checks below.
2386 */
2387 if (length == NULL)
2388 length = &length_dummy;
2389
2390
2391 /* The ARB_get_program_binary spec says:
2392 *
2393 * "When a program object's LINK_STATUS is FALSE, its program binary
2394 * length is zero, and a call to GetProgramBinary will generate an
2395 * INVALID_OPERATION error.
2396 */
2397 if (!shProg->data->LinkStatus) {
2398 _mesa_error(ctx, GL_INVALID_OPERATION,
2399 "glGetProgramBinary(program %u not linked)",
2400 shProg->Name);
2401 *length = 0;
2402 return;
2403 }
2404
2405 if (ctx->Const.NumProgramBinaryFormats == 0) {
2406 *length = 0;
2407 _mesa_error(ctx, GL_INVALID_OPERATION,
2408 "glGetProgramBinary(driver supports zero binary formats)");
2409 } else {
2410 _mesa_get_program_binary(ctx, shProg, bufSize, length, binaryFormat,
2411 binary);
2412 assert(*length == 0 || *binaryFormat == GL_PROGRAM_BINARY_FORMAT_MESA);
2413 }
2414 }
2415
2416 void GLAPIENTRY
2417 _mesa_ProgramBinary(GLuint program, GLenum binaryFormat,
2418 const GLvoid *binary, GLsizei length)
2419 {
2420 struct gl_shader_program *shProg;
2421 GET_CURRENT_CONTEXT(ctx);
2422
2423 shProg = _mesa_lookup_shader_program_err(ctx, program, "glProgramBinary");
2424 if (!shProg)
2425 return;
2426
2427 _mesa_clear_shader_program_data(ctx, shProg);
2428 shProg->data = _mesa_create_shader_program_data();
2429
2430 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2431 *
2432 * "If a negative number is provided where an argument of type sizei or
2433 * sizeiptr is specified, an INVALID_VALUE error is generated."
2434 */
2435 if (length < 0) {
2436 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramBinary(length < 0)");
2437 return;
2438 }
2439
2440 if (ctx->Const.NumProgramBinaryFormats == 0 ||
2441 binaryFormat != GL_PROGRAM_BINARY_FORMAT_MESA) {
2442 /* The ARB_get_program_binary spec says:
2443 *
2444 * "<binaryFormat> and <binary> must be those returned by a previous
2445 * call to GetProgramBinary, and <length> must be the length of the
2446 * program binary as returned by GetProgramBinary or GetProgramiv with
2447 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2448 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2449 * are not met."
2450 *
2451 * Since any value of binaryFormat passed "is not one of those specified as
2452 * allowable for [this] command, an INVALID_ENUM error is generated."
2453 */
2454 shProg->data->LinkStatus = LINKING_FAILURE;
2455 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramBinary");
2456 } else {
2457 _mesa_program_binary(ctx, shProg, binaryFormat, binary, length);
2458 }
2459 }
2460
2461
2462 static ALWAYS_INLINE void
2463 program_parameteri(struct gl_context *ctx, struct gl_shader_program *shProg,
2464 GLuint pname, GLint value, bool no_error)
2465 {
2466 switch (pname) {
2467 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
2468 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2469 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2470 * even be in the dispatch table, so we shouldn't need to expclicitly
2471 * check here.
2472 *
2473 * On desktop, we ignore the 3.0+ requirement because it is silly.
2474 */
2475
2476 /* The ARB_get_program_binary extension spec says:
2477 *
2478 * "An INVALID_VALUE error is generated if the <value> argument to
2479 * ProgramParameteri is not TRUE or FALSE."
2480 */
2481 if (!no_error && value != GL_TRUE && value != GL_FALSE) {
2482 goto invalid_value;
2483 }
2484
2485 /* No need to notify the driver. Any changes will actually take effect
2486 * the next time the shader is linked.
2487 *
2488 * The ARB_get_program_binary extension spec says:
2489 *
2490 * "To indicate that a program binary is likely to be retrieved,
2491 * ProgramParameteri should be called with <pname>
2492 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2493 * will not be in effect until the next time LinkProgram or
2494 * ProgramBinary has been called successfully."
2495 *
2496 * The resolution of issue 9 in the extension spec also says:
2497 *
2498 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2499 * to indicate to the GL implementation that this program will
2500 * likely be saved with GetProgramBinary at some point. This will
2501 * give the GL implementation the opportunity to track any state
2502 * changes made to the program before being saved such that when it
2503 * is loaded again a recompile can be avoided."
2504 */
2505 shProg->BinaryRetrievableHintPending = value;
2506 return;
2507
2508 case GL_PROGRAM_SEPARABLE:
2509 /* Spec imply that the behavior is the same as ARB_get_program_binary
2510 * Chapter 7.3 Program Objects
2511 */
2512 if (!no_error && value != GL_TRUE && value != GL_FALSE) {
2513 goto invalid_value;
2514 }
2515 shProg->SeparateShader = value;
2516 return;
2517
2518 default:
2519 if (!no_error) {
2520 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteri(pname=%s)",
2521 _mesa_enum_to_string(pname));
2522 }
2523 return;
2524 }
2525
2526 invalid_value:
2527 _mesa_error(ctx, GL_INVALID_VALUE,
2528 "glProgramParameteri(pname=%s, value=%d): "
2529 "value must be 0 or 1.",
2530 _mesa_enum_to_string(pname),
2531 value);
2532 }
2533
2534
2535 void GLAPIENTRY
2536 _mesa_ProgramParameteri_no_error(GLuint program, GLenum pname, GLint value)
2537 {
2538 GET_CURRENT_CONTEXT(ctx);
2539
2540 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, program);
2541 program_parameteri(ctx, shProg, pname, value, true);
2542 }
2543
2544
2545 void GLAPIENTRY
2546 _mesa_ProgramParameteri(GLuint program, GLenum pname, GLint value)
2547 {
2548 struct gl_shader_program *shProg;
2549 GET_CURRENT_CONTEXT(ctx);
2550
2551 shProg = _mesa_lookup_shader_program_err(ctx, program,
2552 "glProgramParameteri");
2553 if (!shProg)
2554 return;
2555
2556 program_parameteri(ctx, shProg, pname, value, false);
2557 }
2558
2559
2560 void
2561 _mesa_use_program(struct gl_context *ctx, gl_shader_stage stage,
2562 struct gl_shader_program *shProg, struct gl_program *prog,
2563 struct gl_pipeline_object *shTarget)
2564 {
2565 struct gl_program **target;
2566
2567 target = &shTarget->CurrentProgram[stage];
2568 if (prog) {
2569 _mesa_program_init_subroutine_defaults(ctx, prog);
2570 }
2571
2572 if (*target != prog) {
2573 /* Program is current, flush it */
2574 if (shTarget == ctx->_Shader) {
2575 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
2576 }
2577
2578 _mesa_reference_shader_program(ctx,
2579 &shTarget->ReferencedPrograms[stage],
2580 shProg);
2581 _mesa_reference_program(ctx, target, prog);
2582 _mesa_update_allow_draw_out_of_order(ctx);
2583 if (stage == MESA_SHADER_VERTEX)
2584 _mesa_update_vertex_processing_mode(ctx);
2585 return;
2586 }
2587
2588 }
2589
2590
2591 /**
2592 * Copy program-specific data generated by linking from the gl_shader_program
2593 * object to the gl_program object referred to by the gl_linked_shader.
2594 *
2595 * This function expects _mesa_reference_program() to have been previously
2596 * called setting the gl_linked_shaders program reference.
2597 */
2598 void
2599 _mesa_copy_linked_program_data(const struct gl_shader_program *src,
2600 struct gl_linked_shader *dst_sh)
2601 {
2602 assert(dst_sh->Program);
2603
2604 struct gl_program *dst = dst_sh->Program;
2605
2606 dst->info.separate_shader = src->SeparateShader;
2607
2608 switch (dst_sh->Stage) {
2609 case MESA_SHADER_GEOMETRY: {
2610 dst->info.gs.vertices_in = src->Geom.VerticesIn;
2611 dst->info.gs.uses_end_primitive = src->Geom.UsesEndPrimitive;
2612 dst->info.gs.uses_streams = src->Geom.UsesStreams;
2613 break;
2614 }
2615 case MESA_SHADER_FRAGMENT: {
2616 dst->info.fs.depth_layout = src->FragDepthLayout;
2617 break;
2618 }
2619 case MESA_SHADER_COMPUTE: {
2620 dst->info.cs.shared_size = src->Comp.SharedSize;
2621 break;
2622 }
2623 default:
2624 break;
2625 }
2626 }
2627
2628 /**
2629 * ARB_separate_shader_objects: Compile & Link Program
2630 */
2631 GLuint GLAPIENTRY
2632 _mesa_CreateShaderProgramv(GLenum type, GLsizei count,
2633 const GLchar* const *strings)
2634 {
2635 GET_CURRENT_CONTEXT(ctx);
2636
2637 const GLuint shader = create_shader_err(ctx, type, "glCreateShaderProgramv");
2638 GLuint program = 0;
2639
2640 /*
2641 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2642 * GL_INVALID_VALUE should be generated if count < 0
2643 */
2644 if (count < 0) {
2645 _mesa_error(ctx, GL_INVALID_VALUE, "glCreateShaderProgram (count < 0)");
2646 return program;
2647 }
2648
2649 if (shader) {
2650 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
2651
2652 _mesa_ShaderSource(shader, count, strings, NULL);
2653 _mesa_compile_shader(ctx, sh);
2654
2655 program = create_shader_program(ctx);
2656 if (program) {
2657 struct gl_shader_program *shProg;
2658 GLint compiled = GL_FALSE;
2659
2660 shProg = _mesa_lookup_shader_program(ctx, program);
2661
2662 shProg->SeparateShader = GL_TRUE;
2663
2664 get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
2665 if (compiled) {
2666 attach_shader_err(ctx, program, shader, "glCreateShaderProgramv");
2667 _mesa_link_program(ctx, shProg);
2668 detach_shader_error(ctx, program, shader);
2669
2670 #if 0
2671 /* Possibly... */
2672 if (active-user-defined-varyings-in-linked-program) {
2673 append-error-to-info-log;
2674 shProg->data->LinkStatus = LINKING_FAILURE;
2675 }
2676 #endif
2677 }
2678 if (sh->InfoLog)
2679 ralloc_strcat(&shProg->data->InfoLog, sh->InfoLog);
2680 }
2681
2682 delete_shader(ctx, shader);
2683 }
2684
2685 return program;
2686 }
2687
2688
2689 /**
2690 * For GL_ARB_tessellation_shader
2691 */
2692 void GLAPIENTRY
2693 _mesa_PatchParameteri_no_error(GLenum pname, GLint value)
2694 {
2695 GET_CURRENT_CONTEXT(ctx);
2696 FLUSH_VERTICES(ctx, 0);
2697 ctx->TessCtrlProgram.patch_vertices = value;
2698 }
2699
2700
2701 extern void GLAPIENTRY
2702 _mesa_PatchParameteri(GLenum pname, GLint value)
2703 {
2704 GET_CURRENT_CONTEXT(ctx);
2705
2706 if (!_mesa_has_tessellation(ctx)) {
2707 _mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameteri");
2708 return;
2709 }
2710
2711 if (pname != GL_PATCH_VERTICES) {
2712 _mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameteri");
2713 return;
2714 }
2715
2716 if (value <= 0 || value > ctx->Const.MaxPatchVertices) {
2717 _mesa_error(ctx, GL_INVALID_VALUE, "glPatchParameteri");
2718 return;
2719 }
2720
2721 FLUSH_VERTICES(ctx, 0);
2722 ctx->TessCtrlProgram.patch_vertices = value;
2723 }
2724
2725
2726 extern void GLAPIENTRY
2727 _mesa_PatchParameterfv(GLenum pname, const GLfloat *values)
2728 {
2729 GET_CURRENT_CONTEXT(ctx);
2730
2731 if (!_mesa_has_tessellation(ctx)) {
2732 _mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameterfv");
2733 return;
2734 }
2735
2736 switch(pname) {
2737 case GL_PATCH_DEFAULT_OUTER_LEVEL:
2738 FLUSH_VERTICES(ctx, 0);
2739 memcpy(ctx->TessCtrlProgram.patch_default_outer_level, values,
2740 4 * sizeof(GLfloat));
2741 ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
2742 return;
2743 case GL_PATCH_DEFAULT_INNER_LEVEL:
2744 FLUSH_VERTICES(ctx, 0);
2745 memcpy(ctx->TessCtrlProgram.patch_default_inner_level, values,
2746 2 * sizeof(GLfloat));
2747 ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
2748 return;
2749 default:
2750 _mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameterfv");
2751 return;
2752 }
2753 }
2754
2755 /**
2756 * ARB_shader_subroutine
2757 */
2758 GLint GLAPIENTRY
2759 _mesa_GetSubroutineUniformLocation(GLuint program, GLenum shadertype,
2760 const GLchar *name)
2761 {
2762 GET_CURRENT_CONTEXT(ctx);
2763 const char *api_name = "glGetSubroutineUniformLocation";
2764 struct gl_shader_program *shProg;
2765 GLenum resource_type;
2766 gl_shader_stage stage;
2767
2768 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2769 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2770 return -1;
2771 }
2772
2773 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2774 if (!shProg)
2775 return -1;
2776
2777 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2778 if (!shProg->_LinkedShaders[stage]) {
2779 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2780 return -1;
2781 }
2782
2783 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
2784 return _mesa_program_resource_location(shProg, resource_type, name);
2785 }
2786
2787 GLuint GLAPIENTRY
2788 _mesa_GetSubroutineIndex(GLuint program, GLenum shadertype,
2789 const GLchar *name)
2790 {
2791 GET_CURRENT_CONTEXT(ctx);
2792 const char *api_name = "glGetSubroutineIndex";
2793 struct gl_shader_program *shProg;
2794 struct gl_program_resource *res;
2795 GLenum resource_type;
2796 gl_shader_stage stage;
2797
2798 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2799 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2800 return -1;
2801 }
2802
2803 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2804 if (!shProg)
2805 return -1;
2806
2807 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2808 if (!shProg->_LinkedShaders[stage]) {
2809 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2810 return -1;
2811 }
2812
2813 resource_type = _mesa_shader_stage_to_subroutine(stage);
2814 res = _mesa_program_resource_find_name(shProg, resource_type, name, NULL);
2815 if (!res) {
2816 return -1;
2817 }
2818
2819 return _mesa_program_resource_index(shProg, res);
2820 }
2821
2822
2823 GLvoid GLAPIENTRY
2824 _mesa_GetActiveSubroutineUniformiv(GLuint program, GLenum shadertype,
2825 GLuint index, GLenum pname, GLint *values)
2826 {
2827 GET_CURRENT_CONTEXT(ctx);
2828 const char *api_name = "glGetActiveSubroutineUniformiv";
2829 struct gl_shader_program *shProg;
2830 struct gl_linked_shader *sh;
2831 gl_shader_stage stage;
2832 struct gl_program_resource *res;
2833 const struct gl_uniform_storage *uni;
2834 GLenum resource_type;
2835 int count, i, j;
2836
2837 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2838 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2839 return;
2840 }
2841
2842 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2843 if (!shProg)
2844 return;
2845
2846 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2847 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
2848
2849 sh = shProg->_LinkedShaders[stage];
2850 if (!sh) {
2851 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2852 return;
2853 }
2854
2855 struct gl_program *p = shProg->_LinkedShaders[stage]->Program;
2856 if (index >= p->sh.NumSubroutineUniforms) {
2857 _mesa_error(ctx, GL_INVALID_VALUE, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name);
2858 return;
2859 }
2860
2861 switch (pname) {
2862 case GL_NUM_COMPATIBLE_SUBROUTINES: {
2863 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2864 if (res) {
2865 uni = res->Data;
2866 values[0] = uni->num_compatible_subroutines;
2867 }
2868 break;
2869 }
2870 case GL_COMPATIBLE_SUBROUTINES: {
2871 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2872 if (res) {
2873 uni = res->Data;
2874 count = 0;
2875 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
2876 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
2877 for (j = 0; j < fn->num_compat_types; j++) {
2878 if (fn->types[j] == uni->type) {
2879 values[count++] = i;
2880 break;
2881 }
2882 }
2883 }
2884 }
2885 break;
2886 }
2887 case GL_UNIFORM_SIZE:
2888 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2889 if (res) {
2890 uni = res->Data;
2891 values[0] = uni->array_elements ? uni->array_elements : 1;
2892 }
2893 break;
2894 case GL_UNIFORM_NAME_LENGTH:
2895 res = _mesa_program_resource_find_index(shProg, resource_type, index);
2896 if (res) {
2897 values[0] = strlen(_mesa_program_resource_name(res)) + 1
2898 + ((_mesa_program_resource_array_size(res) != 0) ? 3 : 0);
2899 }
2900 break;
2901 default:
2902 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2903 return;
2904 }
2905 }
2906
2907
2908 GLvoid GLAPIENTRY
2909 _mesa_GetActiveSubroutineUniformName(GLuint program, GLenum shadertype,
2910 GLuint index, GLsizei bufsize,
2911 GLsizei *length, GLchar *name)
2912 {
2913 GET_CURRENT_CONTEXT(ctx);
2914 const char *api_name = "glGetActiveSubroutineUniformName";
2915 struct gl_shader_program *shProg;
2916 GLenum resource_type;
2917 gl_shader_stage stage;
2918
2919 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2920 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2921 return;
2922 }
2923
2924 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2925 if (!shProg)
2926 return;
2927
2928 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2929 if (!shProg->_LinkedShaders[stage]) {
2930 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2931 return;
2932 }
2933
2934 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
2935 /* get program resource name */
2936 _mesa_get_program_resource_name(shProg, resource_type,
2937 index, bufsize,
2938 length, name, api_name);
2939 }
2940
2941
2942 GLvoid GLAPIENTRY
2943 _mesa_GetActiveSubroutineName(GLuint program, GLenum shadertype,
2944 GLuint index, GLsizei bufsize,
2945 GLsizei *length, GLchar *name)
2946 {
2947 GET_CURRENT_CONTEXT(ctx);
2948 const char *api_name = "glGetActiveSubroutineName";
2949 struct gl_shader_program *shProg;
2950 GLenum resource_type;
2951 gl_shader_stage stage;
2952
2953 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2954 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2955 return;
2956 }
2957
2958 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
2959 if (!shProg)
2960 return;
2961
2962 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2963 if (!shProg->_LinkedShaders[stage]) {
2964 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2965 return;
2966 }
2967 resource_type = _mesa_shader_stage_to_subroutine(stage);
2968 _mesa_get_program_resource_name(shProg, resource_type,
2969 index, bufsize,
2970 length, name, api_name);
2971 }
2972
2973 GLvoid GLAPIENTRY
2974 _mesa_UniformSubroutinesuiv(GLenum shadertype, GLsizei count,
2975 const GLuint *indices)
2976 {
2977 GET_CURRENT_CONTEXT(ctx);
2978 const char *api_name = "glUniformSubroutinesuiv";
2979 gl_shader_stage stage;
2980 int i;
2981
2982 if (!_mesa_validate_shader_target(ctx, shadertype)) {
2983 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2984 return;
2985 }
2986
2987 stage = _mesa_shader_enum_to_shader_stage(shadertype);
2988 struct gl_program *p = ctx->_Shader->CurrentProgram[stage];
2989 if (!p) {
2990 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
2991 return;
2992 }
2993
2994 if (count != p->sh.NumSubroutineUniformRemapTable) {
2995 _mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
2996 return;
2997 }
2998
2999 i = 0;
3000 bool flushed = false;
3001 do {
3002 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[i];
3003 if (uni == NULL) {
3004 i++;
3005 continue;
3006 }
3007
3008 if (!flushed) {
3009 _mesa_flush_vertices_for_uniforms(ctx, uni);
3010 flushed = true;
3011 }
3012
3013 int uni_count = uni->array_elements ? uni->array_elements : 1;
3014 int j, k, f;
3015
3016 for (j = i; j < i + uni_count; j++) {
3017 struct gl_subroutine_function *subfn = NULL;
3018 if (indices[j] > p->sh.MaxSubroutineFunctionIndex) {
3019 _mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
3020 return;
3021 }
3022
3023 for (f = 0; f < p->sh.NumSubroutineFunctions; f++) {
3024 if (p->sh.SubroutineFunctions[f].index == indices[j])
3025 subfn = &p->sh.SubroutineFunctions[f];
3026 }
3027
3028 if (!subfn) {
3029 continue;
3030 }
3031
3032 for (k = 0; k < subfn->num_compat_types; k++) {
3033 if (subfn->types[k] == uni->type)
3034 break;
3035 }
3036 if (k == subfn->num_compat_types) {
3037 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
3038 return;
3039 }
3040
3041 ctx->SubroutineIndex[p->info.stage].IndexPtr[j] = indices[j];
3042 }
3043 i += uni_count;
3044 } while(i < count);
3045 }
3046
3047
3048 GLvoid GLAPIENTRY
3049 _mesa_GetUniformSubroutineuiv(GLenum shadertype, GLint location,
3050 GLuint *params)
3051 {
3052 GET_CURRENT_CONTEXT(ctx);
3053 const char *api_name = "glGetUniformSubroutineuiv";
3054 gl_shader_stage stage;
3055
3056 if (!_mesa_validate_shader_target(ctx, shadertype)) {
3057 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
3058 return;
3059 }
3060
3061 stage = _mesa_shader_enum_to_shader_stage(shadertype);
3062 struct gl_program *p = ctx->_Shader->CurrentProgram[stage];
3063 if (!p) {
3064 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
3065 return;
3066 }
3067
3068 if (location >= p->sh.NumSubroutineUniformRemapTable) {
3069 _mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
3070 return;
3071 }
3072
3073 *params = ctx->SubroutineIndex[p->info.stage].IndexPtr[location];
3074 }
3075
3076
3077 GLvoid GLAPIENTRY
3078 _mesa_GetProgramStageiv(GLuint program, GLenum shadertype,
3079 GLenum pname, GLint *values)
3080 {
3081 GET_CURRENT_CONTEXT(ctx);
3082 const char *api_name = "glGetProgramStageiv";
3083 struct gl_shader_program *shProg;
3084 struct gl_linked_shader *sh;
3085 gl_shader_stage stage;
3086
3087 if (!_mesa_validate_shader_target(ctx, shadertype)) {
3088 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
3089 return;
3090 }
3091
3092 shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
3093 if (!shProg)
3094 return;
3095
3096 stage = _mesa_shader_enum_to_shader_stage(shadertype);
3097 sh = shProg->_LinkedShaders[stage];
3098
3099 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
3100 * INVALID_OPERATION in the case of not be linked.
3101 *
3102 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
3103 * same info using other specs (ARB_program_interface_query), without the
3104 * need of the program to be linked, being the value for that case 0.
3105 *
3106 * But at the same time, some other methods require the program to be
3107 * linked for pname related to locations, so it would be inconsistent to
3108 * not do the same here. So we are:
3109 * * Return GL_INVALID_OPERATION if not linked only for locations.
3110 * * Setting a default value of 0, to be returned if not linked.
3111 */
3112 if (!sh) {
3113 values[0] = 0;
3114 if (pname == GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS) {
3115 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
3116 }
3117 return;
3118 }
3119
3120 struct gl_program *p = sh->Program;
3121 switch (pname) {
3122 case GL_ACTIVE_SUBROUTINES:
3123 values[0] = p->sh.NumSubroutineFunctions;
3124 break;
3125 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS:
3126 values[0] = p->sh.NumSubroutineUniformRemapTable;
3127 break;
3128 case GL_ACTIVE_SUBROUTINE_UNIFORMS:
3129 values[0] = p->sh.NumSubroutineUniforms;
3130 break;
3131 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH:
3132 {
3133 unsigned i;
3134 GLint max_len = 0;
3135 GLenum resource_type;
3136 struct gl_program_resource *res;
3137
3138 resource_type = _mesa_shader_stage_to_subroutine(stage);
3139 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
3140 res = _mesa_program_resource_find_index(shProg, resource_type, i);
3141 if (res) {
3142 const GLint len = strlen(_mesa_program_resource_name(res)) + 1;
3143 if (len > max_len)
3144 max_len = len;
3145 }
3146 }
3147 values[0] = max_len;
3148 break;
3149 }
3150 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH:
3151 {
3152 unsigned i;
3153 GLint max_len = 0;
3154 GLenum resource_type;
3155 struct gl_program_resource *res;
3156
3157 resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
3158 for (i = 0; i < p->sh.NumSubroutineUniformRemapTable; i++) {
3159 res = _mesa_program_resource_find_index(shProg, resource_type, i);
3160 if (res) {
3161 const GLint len = strlen(_mesa_program_resource_name(res)) + 1
3162 + ((_mesa_program_resource_array_size(res) != 0) ? 3 : 0);
3163
3164 if (len > max_len)
3165 max_len = len;
3166 }
3167 }
3168 values[0] = max_len;
3169 break;
3170 }
3171 default:
3172 _mesa_error(ctx, GL_INVALID_ENUM, "%s", api_name);
3173 values[0] = -1;
3174 break;
3175 }
3176 }
3177
3178 /* This is simple list entry that will be used to hold a list of string
3179 * tokens of a parsed shader include path.
3180 */
3181 struct sh_incl_path_entry
3182 {
3183 struct sh_incl_path_entry *next;
3184 struct sh_incl_path_entry *prev;
3185
3186 char *path;
3187 };
3188
3189 /* Nodes of the shader include tree */
3190 struct sh_incl_path_ht_entry
3191 {
3192 struct hash_table *path;
3193 char *shader_source;
3194 };
3195
3196 struct shader_includes {
3197 /* Array to hold include paths given to glCompileShaderIncludeARB() */
3198 struct sh_incl_path_entry **include_paths;
3199 size_t num_include_paths;
3200 size_t relative_path_cursor;
3201
3202 /* Root hash table holding the shader include tree */
3203 struct hash_table *shader_include_tree;
3204 };
3205
3206 void
3207 _mesa_init_shader_includes(struct gl_shared_state *shared)
3208 {
3209 shared->ShaderIncludes = calloc(1, sizeof(struct shader_includes));
3210 shared->ShaderIncludes->shader_include_tree =
3211 _mesa_hash_table_create(NULL, _mesa_hash_string,
3212 _mesa_key_string_equal);
3213 }
3214
3215 size_t
3216 _mesa_get_shader_include_cursor(struct gl_shared_state *shared)
3217 {
3218 return shared->ShaderIncludes->relative_path_cursor;
3219 }
3220
3221 void
3222 _mesa_set_shader_include_cursor(struct gl_shared_state *shared, size_t cursor)
3223 {
3224 shared->ShaderIncludes->relative_path_cursor = cursor;
3225 }
3226
3227 static void
3228 destroy_shader_include(struct hash_entry *entry)
3229 {
3230 struct sh_incl_path_ht_entry *sh_incl_ht_entry =
3231 (struct sh_incl_path_ht_entry *) entry->data;
3232
3233 _mesa_hash_table_destroy(sh_incl_ht_entry->path, destroy_shader_include);
3234 free(sh_incl_ht_entry->shader_source);
3235 free(sh_incl_ht_entry);
3236 }
3237
3238 void
3239 _mesa_destroy_shader_includes(struct gl_shared_state *shared)
3240 {
3241 _mesa_hash_table_destroy(shared->ShaderIncludes->shader_include_tree,
3242 destroy_shader_include);
3243 free(shared->ShaderIncludes);
3244 }
3245
3246 static bool
3247 valid_path_format(const char *str, bool relative_path)
3248 {
3249 int i = 0;
3250
3251 if (!str[i] || (!relative_path && str[i] != '/'))
3252 return false;
3253
3254 i++;
3255
3256 while (str[i]) {
3257 const char c = str[i++];
3258 if (('A' <= c && c <= 'Z') ||
3259 ('a' <= c && c <= 'z') ||
3260 ('0' <= c && c <= '9'))
3261 continue;
3262
3263 if (c == '/') {
3264 if (str[i - 2] == '/')
3265 return false;
3266
3267 continue;
3268 }
3269
3270 if (strchr("^. _+*%[](){}|&~=!:;,?-", c) == NULL)
3271 return false;
3272 }
3273
3274 if (str[i - 1] == '/')
3275 return false;
3276
3277 return true;
3278 }
3279
3280
3281 static bool
3282 validate_and_tokenise_sh_incl(struct gl_context *ctx,
3283 void *mem_ctx,
3284 struct sh_incl_path_entry **path_list,
3285 char *full_path, bool error_check)
3286 {
3287 bool relative_path = ctx->Shared->ShaderIncludes->num_include_paths;
3288
3289 if (!valid_path_format(full_path, relative_path)) {
3290 if (error_check) {
3291 _mesa_error(ctx, GL_INVALID_VALUE,
3292 "glNamedStringARB(invalid name %s)", full_path);
3293 }
3294 return false;
3295 }
3296
3297 char *save_ptr = NULL;
3298 char *path_str = strtok_r(full_path, "/", &save_ptr);
3299
3300 *path_list = rzalloc(mem_ctx, struct sh_incl_path_entry);
3301
3302 make_empty_list(*path_list);
3303
3304 while (path_str != NULL) {
3305 if (strlen(path_str) == 0) {
3306 if (error_check) {
3307 _mesa_error(ctx, GL_INVALID_VALUE,
3308 "glNamedStringARB(invalid name %s)", full_path);
3309 }
3310
3311 return false;
3312 }
3313
3314 if (strcmp(path_str, ".") == 0) {
3315 /* Do nothing */
3316 } else if (strcmp(path_str, "..") == 0) {
3317 struct sh_incl_path_entry *last = last_elem(*path_list);
3318 remove_from_list(last);
3319 } else {
3320 struct sh_incl_path_entry *path =
3321 rzalloc(mem_ctx, struct sh_incl_path_entry);
3322
3323 path->path = strdup(path_str);
3324 insert_at_tail(*path_list, path);
3325 }
3326
3327 path_str = strtok_r(NULL, "/", &save_ptr);
3328 }
3329
3330 return true;
3331 }
3332
3333 static struct sh_incl_path_ht_entry *
3334 lookup_shader_include(struct gl_context *ctx, char *path,
3335 bool error_check)
3336 {
3337 void *mem_ctx = ralloc_context(NULL);
3338 struct sh_incl_path_entry *path_list;
3339
3340 if (!validate_and_tokenise_sh_incl(ctx, mem_ctx, &path_list, path,
3341 error_check)) {
3342 ralloc_free(mem_ctx);
3343 return NULL;
3344 }
3345
3346 struct sh_incl_path_ht_entry *sh_incl_ht_entry = NULL;
3347 struct hash_table *path_ht =
3348 ctx->Shared->ShaderIncludes->shader_include_tree;
3349
3350 size_t count = ctx->Shared->ShaderIncludes->num_include_paths;
3351 bool relative_path = path[0] != '/';
3352
3353 size_t i = ctx->Shared->ShaderIncludes->relative_path_cursor;
3354 bool use_cursor = ctx->Shared->ShaderIncludes->relative_path_cursor;
3355
3356 do {
3357 struct sh_incl_path_entry *entry;
3358
3359 if (relative_path) {
3360 next_relative_path:
3361 {
3362 struct sh_incl_path_entry *rel_path_list =
3363 ctx->Shared->ShaderIncludes->include_paths[i];
3364 foreach(entry, rel_path_list) {
3365 struct hash_entry *ht_entry =
3366 _mesa_hash_table_search(path_ht, entry->path);
3367
3368 if (!ht_entry) {
3369 /* Reset search path and skip to the next include path */
3370 path_ht = ctx->Shared->ShaderIncludes->shader_include_tree;
3371 sh_incl_ht_entry = NULL;
3372 if (use_cursor) {
3373 i = 0;
3374 use_cursor = false;
3375
3376 goto next_relative_path;
3377 }
3378 i++;
3379 if (i < count)
3380 goto next_relative_path;
3381 else
3382 break;
3383 } else {
3384 sh_incl_ht_entry =
3385 (struct sh_incl_path_ht_entry *) ht_entry->data;
3386 }
3387
3388 path_ht = sh_incl_ht_entry->path;
3389 }
3390 }
3391 }
3392
3393 foreach(entry, path_list) {
3394 struct hash_entry *ht_entry =
3395 _mesa_hash_table_search(path_ht, entry->path);
3396
3397 if (!ht_entry) {
3398 /* Reset search path and skip to the next include path */
3399 path_ht = ctx->Shared->ShaderIncludes->shader_include_tree;
3400 sh_incl_ht_entry = NULL;
3401 if (use_cursor) {
3402 i = 0;
3403 use_cursor = false;
3404
3405 break;
3406 }
3407 i++;
3408 break;
3409 } else {
3410
3411 sh_incl_ht_entry =
3412 (struct sh_incl_path_ht_entry *) ht_entry->data;
3413 }
3414
3415 path_ht = sh_incl_ht_entry->path;
3416 }
3417
3418 if (i < count &&
3419 (sh_incl_ht_entry == NULL || !sh_incl_ht_entry->shader_source))
3420 continue;
3421
3422 /* If we get here then we have found a matching path or exahusted our
3423 * relative search paths.
3424 */
3425 ctx->Shared->ShaderIncludes->relative_path_cursor = i;
3426 break;
3427 } while (i < count);
3428
3429 ralloc_free(mem_ctx);
3430
3431 return sh_incl_ht_entry;
3432 }
3433
3434 const char *
3435 _mesa_lookup_shader_include(struct gl_context *ctx, char *path,
3436 bool error_check)
3437 {
3438 struct sh_incl_path_ht_entry *shader_include =
3439 lookup_shader_include(ctx, path, error_check);
3440
3441 return shader_include ? shader_include->shader_source : NULL;
3442 }
3443
3444 static char *
3445 copy_string(struct gl_context *ctx, const char *str, int str_len,
3446 const char *caller)
3447 {
3448 if (!str) {
3449 _mesa_error(ctx, GL_INVALID_VALUE, "%s(NULL string)", caller);
3450 return NULL;
3451 }
3452
3453 char *cp;
3454 if (str_len == -1)
3455 cp = strdup(str);
3456 else {
3457 cp = calloc(sizeof(char), str_len + 1);
3458 memcpy(cp, str, str_len);
3459 }
3460
3461 return cp;
3462 }
3463
3464 GLvoid GLAPIENTRY
3465 _mesa_NamedStringARB(GLenum type, GLint namelen, const GLchar *name,
3466 GLint stringlen, const GLchar *string)
3467 {
3468 GET_CURRENT_CONTEXT(ctx);
3469 const char *caller = "glNamedStringARB";
3470
3471 if (type != GL_SHADER_INCLUDE_ARB) {
3472 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid type)", caller);
3473 return;
3474 }
3475
3476 char *name_cp = copy_string(ctx, name, namelen, caller);
3477 char *string_cp = copy_string(ctx, string, stringlen, caller);
3478 if (!name_cp || !string_cp) {
3479 free(string_cp);
3480 free(name_cp);
3481 return;
3482 }
3483
3484 void *mem_ctx = ralloc_context(NULL);
3485 struct sh_incl_path_entry *path_list;
3486
3487 if (!validate_and_tokenise_sh_incl(ctx, mem_ctx, &path_list, name_cp,
3488 true)) {
3489 free(string_cp);
3490 free(name_cp);
3491 ralloc_free(mem_ctx);
3492 return;
3493 }
3494
3495 mtx_lock(&ctx->Shared->ShaderIncludeMutex);
3496
3497 struct hash_table *path_ht =
3498 ctx->Shared->ShaderIncludes->shader_include_tree;
3499
3500 struct sh_incl_path_entry *entry;
3501 foreach(entry, path_list) {
3502 struct hash_entry *ht_entry =
3503 _mesa_hash_table_search(path_ht, entry->path);
3504
3505 struct sh_incl_path_ht_entry *sh_incl_ht_entry;
3506 if (!ht_entry) {
3507 sh_incl_ht_entry = calloc(1, sizeof(struct sh_incl_path_ht_entry));
3508 sh_incl_ht_entry->path =
3509 _mesa_hash_table_create(NULL, _mesa_hash_string,
3510 _mesa_key_string_equal);
3511 _mesa_hash_table_insert(path_ht, entry->path, sh_incl_ht_entry);
3512 } else {
3513 sh_incl_ht_entry = (struct sh_incl_path_ht_entry *) ht_entry->data;
3514 }
3515
3516 path_ht = sh_incl_ht_entry->path;
3517
3518 if (last_elem(path_list) == entry) {
3519 free(sh_incl_ht_entry->shader_source);
3520 sh_incl_ht_entry->shader_source = string_cp;
3521 }
3522 }
3523
3524 mtx_unlock(&ctx->Shared->ShaderIncludeMutex);
3525
3526 free(name_cp);
3527 ralloc_free(mem_ctx);
3528 }
3529
3530 GLvoid GLAPIENTRY
3531 _mesa_DeleteNamedStringARB(GLint namelen, const GLchar *name)
3532 {
3533 GET_CURRENT_CONTEXT(ctx);
3534 const char *caller = "glDeleteNamedStringARB";
3535
3536 char *name_cp = copy_string(ctx, name, namelen, caller);
3537 if (!name_cp)
3538 return;
3539
3540 struct sh_incl_path_ht_entry *shader_include =
3541 lookup_shader_include(ctx, name_cp, true);
3542
3543 if (!shader_include) {
3544 _mesa_error(ctx, GL_INVALID_OPERATION,
3545 "%s(no string associated with path %s)", caller, name_cp);
3546 free(name_cp);
3547 return;
3548 }
3549
3550 mtx_lock(&ctx->Shared->ShaderIncludeMutex);
3551
3552 free(shader_include->shader_source);
3553 shader_include->shader_source = NULL;
3554
3555 mtx_unlock(&ctx->Shared->ShaderIncludeMutex);
3556
3557 free(name_cp);
3558 }
3559
3560 GLvoid GLAPIENTRY
3561 _mesa_CompileShaderIncludeARB(GLuint shader, GLsizei count,
3562 const GLchar* const *path, const GLint *length)
3563 {
3564 GET_CURRENT_CONTEXT(ctx);
3565 const char *caller = "glCompileShaderIncludeARB";
3566
3567 if (count > 0 && path == NULL) {
3568 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count > 0 && path == NULL)",
3569 caller);
3570 return;
3571 }
3572
3573 void *mem_ctx = ralloc_context(NULL);
3574
3575 mtx_lock(&ctx->Shared->ShaderIncludeMutex);
3576
3577 ctx->Shared->ShaderIncludes->include_paths =
3578 ralloc_array_size(mem_ctx, sizeof(struct sh_incl_path_entry *), count);
3579
3580 for (size_t i = 0; i < count; i++) {
3581 char *path_cp = copy_string(ctx, path[i], length ? length[i] : -1,
3582 caller);
3583 if (!path_cp) {
3584 goto exit;
3585 }
3586
3587 struct sh_incl_path_entry *path_list;
3588
3589 if (!validate_and_tokenise_sh_incl(ctx, mem_ctx, &path_list, path_cp,
3590 true)) {
3591 free(path_cp);
3592 goto exit;
3593 }
3594
3595 ctx->Shared->ShaderIncludes->include_paths[i] = path_list;
3596
3597 free(path_cp);
3598 }
3599
3600 /* We must set this *after* all calls to validate_and_tokenise_sh_incl()
3601 * are done as we use this to decide if we need to check the start of the
3602 * path for a '/'
3603 */
3604 ctx->Shared->ShaderIncludes->num_include_paths = count;
3605
3606 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
3607 if (!sh) {
3608 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(shader)", caller);
3609 goto exit;
3610 }
3611
3612 _mesa_compile_shader(ctx, sh);
3613
3614 exit:
3615 ctx->Shared->ShaderIncludes->num_include_paths = 0;
3616 ctx->Shared->ShaderIncludes->relative_path_cursor = 0;
3617 ctx->Shared->ShaderIncludes->include_paths = NULL;
3618
3619 mtx_unlock(&ctx->Shared->ShaderIncludeMutex);
3620
3621 ralloc_free(mem_ctx);
3622 }
3623
3624 GLboolean GLAPIENTRY
3625 _mesa_IsNamedStringARB(GLint namelen, const GLchar *name)
3626 {
3627 GET_CURRENT_CONTEXT(ctx);
3628
3629 if (!name)
3630 return false;
3631
3632 char *name_cp = copy_string(ctx, name, namelen, "");
3633
3634 const char *source = _mesa_lookup_shader_include(ctx, name_cp, false);
3635 free(name_cp);
3636
3637 if (!source)
3638 return false;
3639
3640 return true;
3641 }
3642
3643 GLvoid GLAPIENTRY
3644 _mesa_GetNamedStringARB(GLint namelen, const GLchar *name, GLsizei bufSize,
3645 GLint *stringlen, GLchar *string)
3646 {
3647 GET_CURRENT_CONTEXT(ctx);
3648 const char *caller = "glGetNamedStringARB";
3649
3650 char *name_cp = copy_string(ctx, name, namelen, caller);
3651 if (!name_cp)
3652 return;
3653
3654 const char *source = _mesa_lookup_shader_include(ctx, name_cp, true);
3655 if (!source) {
3656 _mesa_error(ctx, GL_INVALID_OPERATION,
3657 "%s(no string associated with path %s)", caller, name_cp);
3658 free(name_cp);
3659 return;
3660 }
3661
3662 size_t size = MIN2(strlen(source), bufSize - 1);
3663 memcpy(string, source, size);
3664 string[size] = '\0';
3665
3666 *stringlen = size;
3667
3668 free(name_cp);
3669 }
3670
3671 GLvoid GLAPIENTRY
3672 _mesa_GetNamedStringivARB(GLint namelen, const GLchar *name,
3673 GLenum pname, GLint *params)
3674 {
3675 GET_CURRENT_CONTEXT(ctx);
3676 const char *caller = "glGetNamedStringivARB";
3677
3678 char *name_cp = copy_string(ctx, name, namelen, caller);
3679 if (!name_cp)
3680 return;
3681
3682 const char *source = _mesa_lookup_shader_include(ctx, name_cp, true);
3683 if (!source) {
3684 _mesa_error(ctx, GL_INVALID_OPERATION,
3685 "%s(no string associated with path %s)", caller, name_cp);
3686 free(name_cp);
3687 return;
3688 }
3689
3690 switch (pname) {
3691 case GL_NAMED_STRING_LENGTH_ARB:
3692 *params = strlen(source) + 1;
3693 break;
3694 case GL_NAMED_STRING_TYPE_ARB:
3695 *params = GL_SHADER_INCLUDE_ARB;
3696 break;
3697 default:
3698 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname)", caller);
3699 break;
3700 }
3701
3702 free(name_cp);
3703 }
3704
3705 static int
3706 find_compat_subroutine(struct gl_program *p, const struct glsl_type *type)
3707 {
3708 int i, j;
3709
3710 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
3711 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
3712 for (j = 0; j < fn->num_compat_types; j++) {
3713 if (fn->types[j] == type)
3714 return i;
3715 }
3716 }
3717 return 0;
3718 }
3719
3720 static void
3721 _mesa_shader_write_subroutine_index(struct gl_context *ctx,
3722 struct gl_program *p)
3723 {
3724 int i, j;
3725
3726 if (p->sh.NumSubroutineUniformRemapTable == 0)
3727 return;
3728
3729 i = 0;
3730 do {
3731 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[i];
3732 int uni_count;
3733 int val;
3734
3735 if (!uni) {
3736 i++;
3737 continue;
3738 }
3739
3740 uni_count = uni->array_elements ? uni->array_elements : 1;
3741 for (j = 0; j < uni_count; j++) {
3742 val = ctx->SubroutineIndex[p->info.stage].IndexPtr[i + j];
3743 memcpy(&uni->storage[j], &val, sizeof(int));
3744 }
3745
3746 _mesa_propagate_uniforms_to_driver_storage(uni, 0, uni_count);
3747 i += uni_count;
3748 } while(i < p->sh.NumSubroutineUniformRemapTable);
3749 }
3750
3751 void
3752 _mesa_shader_write_subroutine_indices(struct gl_context *ctx,
3753 gl_shader_stage stage)
3754 {
3755 if (ctx->_Shader->CurrentProgram[stage])
3756 _mesa_shader_write_subroutine_index(ctx,
3757 ctx->_Shader->CurrentProgram[stage]);
3758 }
3759
3760 void
3761 _mesa_program_init_subroutine_defaults(struct gl_context *ctx,
3762 struct gl_program *p)
3763 {
3764 assert(p);
3765
3766 struct gl_subroutine_index_binding *binding = &ctx->SubroutineIndex[p->info.stage];
3767 if (binding->NumIndex != p->sh.NumSubroutineUniformRemapTable) {
3768 binding->IndexPtr = realloc(binding->IndexPtr,
3769 p->sh.NumSubroutineUniformRemapTable * (sizeof(GLuint)));
3770 binding->NumIndex = p->sh.NumSubroutineUniformRemapTable;
3771 }
3772
3773 for (int i = 0; i < p->sh.NumSubroutineUniformRemapTable; i++) {
3774 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[i];
3775
3776 if (!uni)
3777 continue;
3778
3779 binding->IndexPtr[i] = find_compat_subroutine(p, uni->type);
3780 }
3781 }