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