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