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