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