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