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