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