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