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