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