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