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