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