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