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