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