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