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