2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
30 * Implementation of GLSL-related API functions.
31 * The glUniform* functions are in uniforms.c
37 #include <c99_alloca.h>
38 #include "main/glheader.h"
39 #include "main/context.h"
40 #include "main/enums.h"
41 #include "main/glspirv.h"
42 #include "main/hash.h"
43 #include "main/mtypes.h"
44 #include "main/pipelineobj.h"
45 #include "main/program_binary.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "main/state.h"
49 #include "main/transformfeedback.h"
50 #include "main/uniforms.h"
51 #include "compiler/glsl/builtin_functions.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 #include "util/os_file.h"
66 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
69 _mesa_get_shader_flags(void)
71 GLbitfield flags
= 0x0;
72 const char *env
= getenv("MESA_GLSL");
75 if (strstr(env
, "dump_on_error"))
76 flags
|= GLSL_DUMP_ON_ERROR
;
77 else if (strstr(env
, "dump"))
79 if (strstr(env
, "log"))
81 if (strstr(env
, "cache_fb"))
82 flags
|= GLSL_CACHE_FALLBACK
;
83 if (strstr(env
, "cache_info"))
84 flags
|= GLSL_CACHE_INFO
;
85 if (strstr(env
, "nopvert"))
86 flags
|= GLSL_NOP_VERT
;
87 if (strstr(env
, "nopfrag"))
88 flags
|= GLSL_NOP_FRAG
;
89 if (strstr(env
, "uniform"))
90 flags
|= GLSL_UNIFORMS
;
91 if (strstr(env
, "useprog"))
92 flags
|= GLSL_USE_PROG
;
93 if (strstr(env
, "errors"))
94 flags
|= GLSL_REPORT_ERRORS
;
101 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
104 _mesa_get_shader_capture_path(void)
106 static bool read_env_var
= false;
107 static const char *path
= NULL
;
110 path
= getenv("MESA_SHADER_CAPTURE_PATH");
118 * Initialize context's shader state.
121 _mesa_init_shader_state(struct gl_context
*ctx
)
123 /* Device drivers may override these to control what kind of instructions
124 * are generated by the GLSL compiler.
126 struct gl_shader_compiler_options options
;
130 memset(&options
, 0, sizeof(options
));
131 options
.MaxUnrollIterations
= 32;
132 options
.MaxIfDepth
= UINT_MAX
;
134 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
135 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
137 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
139 if (ctx
->Shader
.Flags
!= 0)
140 ctx
->Const
.GenerateTemporaryNames
= true;
142 /* Extended for ARB_separate_shader_objects */
143 ctx
->Shader
.RefCount
= 1;
144 ctx
->TessCtrlProgram
.patch_vertices
= 3;
145 for (i
= 0; i
< 4; ++i
)
146 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
147 for (i
= 0; i
< 2; ++i
)
148 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
153 * Free the per-context shader-related state.
156 _mesa_free_shader_state(struct gl_context
*ctx
)
158 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
159 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
160 _mesa_reference_shader_program(ctx
,
161 &ctx
->Shader
.ReferencedPrograms
[i
],
163 free(ctx
->SubroutineIndex
[i
].IndexPtr
);
164 ctx
->SubroutineIndex
[i
].IndexPtr
= NULL
;
166 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
168 /* Extended for ARB_separate_shader_objects */
169 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
171 assert(ctx
->Shader
.RefCount
== 1);
176 * Copy string from <src> to <dst>, up to maxLength characters, returning
177 * length of <dst> in <length>.
178 * \param src the strings source
179 * \param maxLength max chars to copy
180 * \param length returns number of chars copied
181 * \param dst the string destination
184 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
185 GLsizei
*length
, const GLchar
*src
)
188 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
199 * Confirm that the a shader type is valid and supported by the implementation
201 * \param ctx Current GL context
202 * \param type Shader target
206 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
208 /* Note: when building built-in GLSL functions, this function may be
209 * invoked with ctx == NULL. In that case, we can only validate that it's
210 * a shader target we recognize, not that it's supported in the current
211 * context. But that's fine--we don't need any further validation than
212 * that when building built-in GLSL functions.
216 case GL_FRAGMENT_SHADER
:
217 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
218 case GL_VERTEX_SHADER
:
219 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
220 case GL_GEOMETRY_SHADER_ARB
:
221 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
222 case GL_TESS_CONTROL_SHADER
:
223 case GL_TESS_EVALUATION_SHADER
:
224 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
225 case GL_COMPUTE_SHADER
:
226 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
234 is_program(struct gl_context
*ctx
, GLuint name
)
236 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
237 return shProg
? GL_TRUE
: GL_FALSE
;
242 is_shader(struct gl_context
*ctx
, GLuint name
)
244 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
245 return shader
? GL_TRUE
: GL_FALSE
;
250 * Attach shader to a shader program.
253 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
254 struct gl_shader
*sh
)
256 GLuint n
= shProg
->NumShaders
;
258 shProg
->Shaders
= realloc(shProg
->Shaders
,
259 (n
+ 1) * sizeof(struct gl_shader
*));
260 if (!shProg
->Shaders
) {
261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
266 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
267 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
268 shProg
->NumShaders
++;
272 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
275 struct gl_shader_program
*shProg
;
276 struct gl_shader
*sh
;
279 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
281 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
285 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
290 n
= shProg
->NumShaders
;
291 for (i
= 0; i
< n
; i
++) {
292 if (shProg
->Shaders
[i
] == sh
) {
293 /* The shader is already attched to this program. The
294 * GL_ARB_shader_objects spec says:
296 * "The error INVALID_OPERATION is generated by AttachObjectARB
297 * if <obj> is already attached to <containerObj>."
299 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
301 } else if (same_type_disallowed
&&
302 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
303 /* Shader with the same type is already attached to this program,
304 * OpenGL ES 2.0 and 3.0 specs say:
306 * "Multiple shader objects of the same type may not be attached
307 * to a single program object. [...] The error INVALID_OPERATION
308 * is generated if [...] another shader object of the same type
309 * as shader is already attached to program."
311 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
316 attach_shader(ctx
, shProg
, sh
);
320 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
322 struct gl_shader_program
*shProg
;
323 struct gl_shader
*sh
;
325 shProg
= _mesa_lookup_shader_program(ctx
, program
);
326 sh
= _mesa_lookup_shader(ctx
, shader
);
328 attach_shader(ctx
, shProg
, sh
);
332 create_shader(struct gl_context
*ctx
, GLenum type
)
334 struct gl_shader
*sh
;
337 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
338 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
339 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
341 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
342 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
349 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
351 if (!_mesa_validate_shader_target(ctx
, type
)) {
352 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
353 caller
, _mesa_enum_to_string(type
));
357 return create_shader(ctx
, type
);
362 create_shader_program(struct gl_context
*ctx
)
365 struct gl_shader_program
*shProg
;
367 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
369 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
371 shProg
= _mesa_new_shader_program(name
);
373 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
375 assert(shProg
->RefCount
== 1);
377 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
384 * Delete a shader program. Actually, just decrement the program's
385 * reference count and mark it as DeletePending.
386 * Used to implement glDeleteProgram() and glDeleteObjectARB().
389 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
392 * NOTE: deleting shaders/programs works a bit differently than
393 * texture objects (and buffer objects, etc). Shader/program
394 * handles/IDs exist in the hash table until the object is really
395 * deleted (refcount==0). With texture objects, the handle/ID is
396 * removed from the hash table in glDeleteTextures() while the tex
397 * object itself might linger until its refcount goes to zero.
399 struct gl_shader_program
*shProg
;
401 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
405 if (!shProg
->DeletePending
) {
406 shProg
->DeletePending
= GL_TRUE
;
408 /* effectively, decr shProg's refcount */
409 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
415 delete_shader(struct gl_context
*ctx
, GLuint shader
)
417 struct gl_shader
*sh
;
419 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
423 if (!sh
->DeletePending
) {
424 sh
->DeletePending
= GL_TRUE
;
426 /* effectively, decr sh's refcount */
427 _mesa_reference_shader(ctx
, &sh
, NULL
);
432 static ALWAYS_INLINE
void
433 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
436 struct gl_shader_program
*shProg
;
441 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
445 shProg
= _mesa_lookup_shader_program(ctx
, program
);
448 n
= shProg
->NumShaders
;
450 for (i
= 0; i
< n
; i
++) {
451 if (shProg
->Shaders
[i
]->Name
== shader
) {
453 struct gl_shader
**newList
;
456 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
458 /* alloc new, smaller array */
459 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
461 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
464 /* Copy old list entries to new list, skipping removed entry at [i] */
465 for (j
= 0; j
< i
; j
++) {
466 newList
[j
] = shProg
->Shaders
[j
];
469 newList
[j
++] = shProg
->Shaders
[i
];
472 /* Free old list and install new one */
473 free(shProg
->Shaders
);
474 shProg
->Shaders
= newList
;
475 shProg
->NumShaders
= n
- 1;
478 /* sanity check - make sure the new list's entries are sensible */
479 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
480 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
481 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
482 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
483 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
484 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
485 assert(shProg
->Shaders
[j
]->RefCount
> 0);
496 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
497 err
= GL_INVALID_OPERATION
;
499 err
= GL_INVALID_VALUE
;
500 _mesa_error(ctx
, err
, "glDetachShader(shader)");
507 detach_shader_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
509 detach_shader(ctx
, program
, shader
, false);
514 detach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
516 detach_shader(ctx
, program
, shader
, true);
521 * Return list of shaders attached to shader program.
522 * \param objOut returns GLuint ids
523 * \param handleOut returns GLhandleARB handles
526 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
527 GLsizei
*countOut
, GLuint
*objOut
, GLhandleARB
*handleOut
)
529 struct gl_shader_program
*shProg
;
532 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
537 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
541 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
543 objOut
[i
] = shProg
->Shaders
[i
]->Name
;
547 handleOut
[i
] = (GLhandleARB
) shProg
->Shaders
[i
]->Name
;
557 * glGetHandleARB() - return ID/name of currently bound shader program.
560 get_handle(struct gl_context
*ctx
, GLenum pname
)
562 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
563 if (ctx
->_Shader
->ActiveProgram
)
564 return ctx
->_Shader
->ActiveProgram
->Name
;
569 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
576 * Check if a geometry shader query is valid at this time. If not, report an
577 * error and return false.
579 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
581 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
582 * are queried for a program which has not been linked successfully, or
583 * which does not contain objects to form a geometry shader, then an
584 * INVALID_OPERATION error is generated."
587 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
589 if (shProg
->data
->LinkStatus
&&
590 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
595 "glGetProgramv(linked geometry shader required)");
601 * Check if a tessellation control shader query is valid at this time.
602 * If not, report an error and return false.
604 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
606 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
607 * not been linked successfully, or which does not contain objects to
608 * form a tessellation control shader, then an INVALID_OPERATION error is
612 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
614 if (shProg
->data
->LinkStatus
&&
615 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
619 _mesa_error(ctx
, GL_INVALID_OPERATION
,
620 "glGetProgramv(linked tessellation control shader required)");
626 * Check if a tessellation evaluation shader query is valid at this time.
627 * If not, report an error and return false.
629 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
631 * "If any of the pname values in this paragraph are queried for a program
632 * which has not been linked successfully, or which does not contain
633 * objects to form a tessellation evaluation shader, then an
634 * INVALID_OPERATION error is generated."
638 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
640 if (shProg
->data
->LinkStatus
&&
641 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
645 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
646 "evaluation shader required)");
651 * Return the length of a string, or 0 if the pointer passed in is NULL
653 static size_t strlen_or_zero(const char *s
)
655 return s
? strlen(s
) : 0;
659 * glGetProgramiv() - get shader program state.
660 * Note that this is for GLSL shader programs, not ARB vertex/fragment
661 * programs (see glGetProgramivARB).
664 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
667 struct gl_shader_program
*shProg
668 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
670 /* Is transform feedback available in this context?
673 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
674 || ctx
->API
== API_OPENGL_CORE
675 || _mesa_is_gles3(ctx
);
677 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
678 * and GL 3.2) are available in this context
680 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
681 const bool has_tess
= _mesa_has_tessellation(ctx
);
683 /* Are uniform buffer objects available in this context?
686 (ctx
->API
== API_OPENGL_COMPAT
&&
687 ctx
->Extensions
.ARB_uniform_buffer_object
)
688 || ctx
->API
== API_OPENGL_CORE
689 || _mesa_is_gles3(ctx
);
696 case GL_DELETE_STATUS
:
697 *params
= shProg
->DeletePending
;
699 case GL_COMPLETION_STATUS_ARB
:
700 if (ctx
->Driver
.GetShaderProgramCompletionStatus
)
701 *params
= ctx
->Driver
.GetShaderProgramCompletionStatus(ctx
, shProg
);
706 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
708 case GL_VALIDATE_STATUS
:
709 *params
= shProg
->data
->Validated
;
711 case GL_INFO_LOG_LENGTH
:
712 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
713 strlen(shProg
->data
->InfoLog
) + 1 : 0;
715 case GL_ATTACHED_SHADERS
:
716 *params
= shProg
->NumShaders
;
718 case GL_ACTIVE_ATTRIBUTES
:
719 *params
= _mesa_count_active_attribs(shProg
);
721 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
722 *params
= _mesa_longest_attribute_name_length(shProg
);
724 case GL_ACTIVE_UNIFORMS
: {
726 const unsigned num_uniforms
=
727 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
728 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
729 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
734 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
737 const unsigned num_uniforms
=
738 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
740 for (i
= 0; i
< num_uniforms
; i
++) {
741 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
744 /* From ARB_gl_spirv spec:
746 * "If pname is ACTIVE_UNIFORM_MAX_LENGTH, the length of the
747 * longest active uniform name, including a null terminator, is
748 * returned. If no active uniforms exist, zero is returned. If no
749 * name reflection information is available, one is returned."
751 * We are setting 0 here, as below it will add 1 for the NUL character.
753 const GLint base_len
=
754 strlen_or_zero(shProg
->data
->UniformStorage
[i
].name
);
756 /* Add one for the terminating NUL character for a non-array, and
757 * 4 for the "[0]" and the NUL for an array.
759 const GLint len
= base_len
+ 1 +
760 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
769 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
773 /* Check first if there are transform feedback varyings specified in the
774 * shader (ARB_enhanced_layouts). If there isn't any, return the number of
775 * varyings specified using the API.
777 if (shProg
->last_vert_prog
&&
778 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
> 0)
780 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
;
782 *params
= shProg
->TransformFeedback
.NumVarying
;
784 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
787 bool in_shader_varyings
;
793 /* Check first if there are transform feedback varyings specified in the
794 * shader (ARB_enhanced_layouts). If there isn't any, use the ones
795 * specified using the API.
797 in_shader_varyings
= shProg
->last_vert_prog
&&
798 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
> 0;
800 num_varying
= in_shader_varyings
?
801 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
:
802 shProg
->TransformFeedback
.NumVarying
;
804 for (i
= 0; i
< num_varying
; i
++) {
805 const char *name
= in_shader_varyings
?
806 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->Varyings
[i
].Name
807 : shProg
->TransformFeedback
.VaryingNames
[i
];
809 /* Add one for the terminating NUL character. We have to use
810 * strlen_or_zero, as for shaders constructed from SPIR-V binaries,
811 * it is possible that no name reflection information is available.
813 const GLint len
= strlen_or_zero(name
) + 1;
822 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
825 *params
= shProg
->TransformFeedback
.BufferMode
;
827 case GL_GEOMETRY_VERTICES_OUT
:
830 if (check_gs_query(ctx
, shProg
)) {
831 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
832 Program
->info
.gs
.vertices_out
;
835 case GL_GEOMETRY_SHADER_INVOCATIONS
:
837 (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_gpu_shader5
)) {
840 if (check_gs_query(ctx
, shProg
)) {
841 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
842 Program
->info
.gs
.invocations
;
845 case GL_GEOMETRY_INPUT_TYPE
:
848 if (check_gs_query(ctx
, shProg
)) {
849 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
850 Program
->info
.gs
.input_primitive
;
853 case GL_GEOMETRY_OUTPUT_TYPE
:
856 if (check_gs_query(ctx
, shProg
)) {
857 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
858 Program
->info
.gs
.output_primitive
;
861 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
868 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
869 /* Add one for the terminating NUL character. Name can be NULL, in
870 * that case, from ARB_gl_spirv:
871 * "If pname is ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, the length of
872 * the longest active uniform block name, including the null
873 * terminator, is returned. If no active uniform blocks exist,
874 * zero is returned. If no name reflection information is
875 * available, one is returned."
878 strlen_or_zero(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
887 case GL_ACTIVE_UNIFORM_BLOCKS
:
891 *params
= shProg
->data
->NumUniformBlocks
;
893 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
894 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
895 * only available with desktop OpenGL 3.0+ with the
896 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
898 * On desktop, we ignore the 3.0+ requirement because it is silly.
900 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
903 *params
= shProg
->BinaryRetrievableHint
;
905 case GL_PROGRAM_BINARY_LENGTH
:
906 if (ctx
->Const
.NumProgramBinaryFormats
== 0 || !shProg
->data
->LinkStatus
) {
909 _mesa_get_program_binary_length(ctx
, shProg
, params
);
912 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
913 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
916 *params
= shProg
->data
->NumAtomicBuffers
;
918 case GL_COMPUTE_WORK_GROUP_SIZE
: {
920 if (!_mesa_has_compute_shaders(ctx
))
922 if (!shProg
->data
->LinkStatus
) {
923 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
927 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
928 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
932 for (i
= 0; i
< 3; i
++)
933 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
934 Program
->info
.cs
.local_size
[i
];
937 case GL_PROGRAM_SEPARABLE
:
938 /* If the program has not been linked, return initial value 0. */
939 *params
= (shProg
->data
->LinkStatus
== LINKING_FAILURE
) ? 0 : shProg
->SeparateShader
;
942 /* ARB_tessellation_shader */
943 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
946 if (check_tcs_query(ctx
, shProg
)) {
947 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
948 Program
->info
.tess
.tcs_vertices_out
;
951 case GL_TESS_GEN_MODE
:
954 if (check_tes_query(ctx
, shProg
)) {
955 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
956 Program
->info
.tess
.primitive_mode
;
959 case GL_TESS_GEN_SPACING
:
962 if (check_tes_query(ctx
, shProg
)) {
963 const struct gl_linked_shader
*tes
=
964 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
965 switch (tes
->Program
->info
.tess
.spacing
) {
966 case TESS_SPACING_EQUAL
:
969 case TESS_SPACING_FRACTIONAL_ODD
:
970 *params
= GL_FRACTIONAL_ODD
;
972 case TESS_SPACING_FRACTIONAL_EVEN
:
973 *params
= GL_FRACTIONAL_EVEN
;
975 case TESS_SPACING_UNSPECIFIED
:
981 case GL_TESS_GEN_VERTEX_ORDER
:
984 if (check_tes_query(ctx
, shProg
)) {
985 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
986 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
989 case GL_TESS_GEN_POINT_MODE
:
992 if (check_tes_query(ctx
, shProg
)) {
993 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
994 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
1001 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
1002 _mesa_enum_to_string(pname
));
1007 * glGetShaderiv() - get GLSL shader state
1010 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
1012 struct gl_shader
*shader
=
1013 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
1020 case GL_SHADER_TYPE
:
1021 *params
= shader
->Type
;
1023 case GL_DELETE_STATUS
:
1024 *params
= shader
->DeletePending
;
1026 case GL_COMPLETION_STATUS_ARB
:
1027 /* _mesa_glsl_compile_shader is not offloaded to other threads. */
1030 case GL_COMPILE_STATUS
:
1031 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
1033 case GL_INFO_LOG_LENGTH
:
1034 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
1035 strlen(shader
->InfoLog
) + 1 : 0;
1037 case GL_SHADER_SOURCE_LENGTH
:
1038 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
1040 case GL_SPIR_V_BINARY_ARB
:
1041 *params
= (shader
->spirv_data
!= NULL
);
1044 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
1051 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
1052 GLsizei
*length
, GLchar
*infoLog
)
1054 struct gl_shader_program
*shProg
;
1056 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1057 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1059 * "If a negative number is provided where an argument of type sizei or
1060 * sizeiptr is specified, an INVALID_VALUE error is generated."
1063 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
1067 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1068 "glGetProgramInfoLog(program)");
1073 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
1078 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
1079 GLsizei
*length
, GLchar
*infoLog
)
1081 struct gl_shader
*sh
;
1083 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1084 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1086 * "If a negative number is provided where an argument of type sizei or
1087 * sizeiptr is specified, an INVALID_VALUE error is generated."
1090 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
1094 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
1099 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1104 * Return shader source code.
1107 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1108 GLsizei
*length
, GLchar
*sourceOut
)
1110 struct gl_shader
*sh
;
1112 if (maxLength
< 0) {
1113 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1117 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1121 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1126 * Set/replace shader source code. A helper function used by
1127 * glShaderSource[ARB].
1130 set_shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1134 /* The GL_ARB_gl_spirv spec adds the following to the end of the description
1137 * "If <shader> was previously associated with a SPIR-V module (via the
1138 * ShaderBinary command), that association is broken. Upon successful
1139 * completion of this command the SPIR_V_BINARY_ARB state of <shader>
1142 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
1144 if (sh
->CompileStatus
== COMPILE_SKIPPED
&& !sh
->FallbackSource
) {
1145 /* If shader was previously compiled back-up the source in case of cache
1148 sh
->FallbackSource
= sh
->Source
;
1149 sh
->Source
= source
;
1151 /* free old shader source string and install new one */
1152 free((void *)sh
->Source
);
1153 sh
->Source
= source
;
1157 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1162 ensure_builtin_types(struct gl_context
*ctx
)
1164 if (!ctx
->shader_builtin_ref
) {
1165 _mesa_glsl_builtin_functions_init_or_ref();
1166 ctx
->shader_builtin_ref
= true;
1174 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1179 /* The GL_ARB_gl_spirv spec says:
1181 * "Add a new error for the CompileShader command:
1183 * An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB
1184 * state of <shader> is TRUE."
1186 if (sh
->spirv_data
) {
1187 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCompileShader(SPIR-V)");
1192 /* If the user called glCompileShader without first calling
1193 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1195 sh
->CompileStatus
= COMPILE_FAILURE
;
1197 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1198 _mesa_log("GLSL source for %s shader %d:\n",
1199 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1200 _mesa_log("%s\n", sh
->Source
);
1203 ensure_builtin_types(ctx
);
1205 /* this call will set the shader->CompileStatus field to indicate if
1206 * compilation was successful.
1208 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1210 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1211 _mesa_write_shader_to_file(sh
);
1214 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1215 if (sh
->CompileStatus
) {
1217 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1218 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1220 _mesa_log("No GLSL IR for shader %d (shader may be from "
1221 "cache)\n", sh
->Name
);
1225 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1227 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1228 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1229 _mesa_log("%s\n", sh
->InfoLog
);
1234 if (!sh
->CompileStatus
) {
1235 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1236 _mesa_log("GLSL source for %s shader %d:\n",
1237 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1238 _mesa_log("%s\n", sh
->Source
);
1239 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1242 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1243 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1244 sh
->Name
, sh
->InfoLog
);
1251 * Link a program's shaders.
1253 static ALWAYS_INLINE
void
1254 link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1261 /* From the ARB_transform_feedback2 specification:
1262 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1263 * is the name of a program being used by one or more transform feedback
1264 * objects, even if the objects are not currently bound or are paused."
1266 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1267 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1268 "glLinkProgram(transform feedback is using the program)");
1273 unsigned programs_in_use
= 0;
1275 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1276 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1277 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1278 programs_in_use
|= 1 << stage
;
1282 ensure_builtin_types(ctx
);
1284 FLUSH_VERTICES(ctx
, 0);
1285 _mesa_glsl_link_shader(ctx
, shProg
);
1287 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1289 * "If LinkProgram or ProgramBinary successfully re-links a program
1290 * object that is active for any shader stage, then the newly generated
1291 * executable code will be installed as part of the current rendering
1292 * state for all shader stages where the program is active.
1293 * Additionally, the newly generated executable code is made part of
1294 * the state of any program pipeline for all stages where the program
1297 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1298 while (programs_in_use
) {
1299 const int stage
= u_bit_scan(&programs_in_use
);
1301 struct gl_program
*prog
= NULL
;
1302 if (shProg
->_LinkedShaders
[stage
])
1303 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1305 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1309 /* Capture .shader_test files. */
1310 const char *capture_path
= _mesa_get_shader_capture_path();
1311 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1312 /* Find an unused filename. */
1314 char *filename
= NULL
;
1315 for (unsigned i
= 0;; i
++) {
1317 filename
= ralloc_asprintf(NULL
, "%s/%u-%u.shader_test",
1318 capture_path
, shProg
->Name
, i
);
1320 filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1321 capture_path
, shProg
->Name
);
1323 file
= os_file_create_unique(filename
, 0644);
1326 /* If we are failing for another reason than "this filename already
1327 * exists", we are likely to fail again with another filename, so
1328 * let's just give up */
1329 if (errno
!= EEXIST
)
1331 ralloc_free(filename
);
1334 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1335 shProg
->IsES
? " ES" : "",
1336 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1337 if (shProg
->SeparateShader
)
1338 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1339 fprintf(file
, "\n");
1341 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1342 fprintf(file
, "[%s shader]\n%s\n",
1343 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1344 shProg
->Shaders
[i
]->Source
);
1348 _mesa_warning(ctx
, "Failed to open %s", filename
);
1351 ralloc_free(filename
);
1354 if (shProg
->data
->LinkStatus
== LINKING_FAILURE
&&
1355 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1356 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1357 shProg
->Name
, shProg
->data
->InfoLog
);
1360 _mesa_update_vertex_processing_mode(ctx
);
1362 shProg
->BinaryRetrievableHint
= shProg
->BinaryRetrievableHintPending
;
1368 printf("Link %u shaders in program %u: %s\n",
1369 shProg
->NumShaders
, shProg
->Name
,
1370 shProg
->data
->LinkStatus
? "Success" : "Failed");
1372 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1373 printf(" shader %u, stage %u\n",
1374 shProg
->Shaders
[i
]->Name
,
1375 shProg
->Shaders
[i
]->Stage
);
1382 link_program_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1384 link_program(ctx
, shProg
, false);
1389 link_program_no_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1391 link_program(ctx
, shProg
, true);
1396 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1398 link_program_error(ctx
, shProg
);
1403 * Print basic shader info (for debug).
1406 print_shader_info(const struct gl_shader_program
*shProg
)
1410 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1411 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1413 printf(" %s shader %u, checksum %u\n",
1414 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1415 shProg
->Shaders
[i
]->Name
,
1416 shProg
->Shaders
[i
]->SourceChecksum
);
1418 printf(" %s shader %u\n",
1419 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1420 shProg
->Shaders
[i
]->Name
);
1423 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1424 printf(" vert prog %u\n",
1425 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1426 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1427 printf(" frag prog %u\n",
1428 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1429 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1430 printf(" geom prog %u\n",
1431 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1432 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1433 printf(" tesc prog %u\n",
1434 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1435 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1436 printf(" tese prog %u\n",
1437 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1442 * Use the named shader program for subsequent glUniform calls
1445 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1448 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1450 "%s(program %u not linked)", caller
, shProg
->Name
);
1454 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1455 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1461 * Use the named shader program for subsequent rendering.
1464 _mesa_use_shader_program(struct gl_context
*ctx
,
1465 struct gl_shader_program
*shProg
)
1467 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1468 struct gl_program
*new_prog
= NULL
;
1469 if (shProg
&& shProg
->_LinkedShaders
[i
])
1470 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1471 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1473 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1478 * Do validation of the given shader program.
1479 * \param errMsg returns error message if validation fails.
1480 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1483 validate_shader_program(const struct gl_shader_program
*shProg
,
1486 if (!shProg
->data
->LinkStatus
) {
1490 /* From the GL spec, a program is invalid if any of these are true:
1492 any two active samplers in the current program object are of
1493 different types, but refer to the same texture image unit,
1495 any active sampler in the current program object refers to a texture
1496 image unit where fixed-function fragment processing accesses a
1497 texture target that does not match the sampler type, or
1499 the sum of the number of active samplers in the program and the
1500 number of texture image units enabled for fixed-function fragment
1501 processing exceeds the combined limit on the total number of texture
1502 image units allowed.
1506 * Check: any two active samplers in the current program object are of
1507 * different types, but refer to the same texture image unit,
1509 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1517 * Called via glValidateProgram()
1520 validate_program(struct gl_context
*ctx
, GLuint program
)
1522 struct gl_shader_program
*shProg
;
1523 char errMsg
[100] = "";
1525 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1530 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1531 if (!shProg
->data
->Validated
) {
1532 /* update info log */
1533 if (shProg
->data
->InfoLog
) {
1534 ralloc_free(shProg
->data
->InfoLog
);
1536 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1542 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1544 GET_CURRENT_CONTEXT(ctx
);
1545 attach_shader_no_error(ctx
, program
, shader
);
1550 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1552 GET_CURRENT_CONTEXT(ctx
);
1553 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1558 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1560 GET_CURRENT_CONTEXT(ctx
);
1561 attach_shader_no_error(ctx
, program
, shader
);
1566 _mesa_AttachShader(GLuint program
, GLuint shader
)
1568 GET_CURRENT_CONTEXT(ctx
);
1569 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1574 _mesa_CompileShader(GLuint shaderObj
)
1576 GET_CURRENT_CONTEXT(ctx
);
1577 if (MESA_VERBOSE
& VERBOSE_API
)
1578 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1579 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1580 "glCompileShader"));
1585 _mesa_CreateShader_no_error(GLenum type
)
1587 GET_CURRENT_CONTEXT(ctx
);
1588 return create_shader(ctx
, type
);
1593 _mesa_CreateShader(GLenum type
)
1595 GET_CURRENT_CONTEXT(ctx
);
1597 if (MESA_VERBOSE
& VERBOSE_API
)
1598 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1600 return create_shader_err(ctx
, type
, "glCreateShader");
1604 GLhandleARB GLAPIENTRY
1605 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1607 GET_CURRENT_CONTEXT(ctx
);
1608 return create_shader(ctx
, type
);
1612 GLhandleARB GLAPIENTRY
1613 _mesa_CreateShaderObjectARB(GLenum type
)
1615 GET_CURRENT_CONTEXT(ctx
);
1616 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1621 _mesa_CreateProgram(void)
1623 GET_CURRENT_CONTEXT(ctx
);
1624 if (MESA_VERBOSE
& VERBOSE_API
)
1625 _mesa_debug(ctx
, "glCreateProgram\n");
1626 return create_shader_program(ctx
);
1630 GLhandleARB GLAPIENTRY
1631 _mesa_CreateProgramObjectARB(void)
1633 GET_CURRENT_CONTEXT(ctx
);
1634 return create_shader_program(ctx
);
1639 _mesa_DeleteObjectARB(GLhandleARB obj
)
1641 if (MESA_VERBOSE
& VERBOSE_API
) {
1642 GET_CURRENT_CONTEXT(ctx
);
1643 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1647 GET_CURRENT_CONTEXT(ctx
);
1648 FLUSH_VERTICES(ctx
, 0);
1649 if (is_program(ctx
, obj
)) {
1650 delete_shader_program(ctx
, obj
);
1652 else if (is_shader(ctx
, obj
)) {
1653 delete_shader(ctx
, obj
);
1663 _mesa_DeleteProgram(GLuint name
)
1666 GET_CURRENT_CONTEXT(ctx
);
1667 FLUSH_VERTICES(ctx
, 0);
1668 delete_shader_program(ctx
, name
);
1674 _mesa_DeleteShader(GLuint name
)
1677 GET_CURRENT_CONTEXT(ctx
);
1678 FLUSH_VERTICES(ctx
, 0);
1679 delete_shader(ctx
, name
);
1685 _mesa_DetachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1687 GET_CURRENT_CONTEXT(ctx
);
1688 detach_shader_no_error(ctx
, program
, shader
);
1693 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1695 GET_CURRENT_CONTEXT(ctx
);
1696 detach_shader_error(ctx
, program
, shader
);
1701 _mesa_DetachShader_no_error(GLuint program
, GLuint shader
)
1703 GET_CURRENT_CONTEXT(ctx
);
1704 detach_shader_no_error(ctx
, program
, shader
);
1709 _mesa_DetachShader(GLuint program
, GLuint shader
)
1711 GET_CURRENT_CONTEXT(ctx
);
1712 detach_shader_error(ctx
, program
, shader
);
1717 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1718 GLsizei
* count
, GLhandleARB
* obj
)
1720 GET_CURRENT_CONTEXT(ctx
);
1721 get_attached_shaders(ctx
, (GLuint
)container
, maxCount
, count
, NULL
, obj
);
1726 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1727 GLsizei
*count
, GLuint
*obj
)
1729 GET_CURRENT_CONTEXT(ctx
);
1730 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
, NULL
);
1735 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1736 GLcharARB
* infoLog
)
1738 GET_CURRENT_CONTEXT(ctx
);
1739 if (is_program(ctx
, object
)) {
1740 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1742 else if (is_shader(ctx
, object
)) {
1743 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1746 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1752 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1754 GET_CURRENT_CONTEXT(ctx
);
1755 /* Implement in terms of GetProgramiv, GetShaderiv */
1756 if (is_program(ctx
, object
)) {
1757 if (pname
== GL_OBJECT_TYPE_ARB
) {
1758 *params
= GL_PROGRAM_OBJECT_ARB
;
1761 get_programiv(ctx
, object
, pname
, params
);
1764 else if (is_shader(ctx
, object
)) {
1765 if (pname
== GL_OBJECT_TYPE_ARB
) {
1766 *params
= GL_SHADER_OBJECT_ARB
;
1769 get_shaderiv(ctx
, object
, pname
, params
);
1773 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1779 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1782 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1783 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1784 params
[0] = (GLfloat
) iparams
[0];
1789 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1791 GET_CURRENT_CONTEXT(ctx
);
1792 get_programiv(ctx
, program
, pname
, params
);
1797 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1799 GET_CURRENT_CONTEXT(ctx
);
1800 get_shaderiv(ctx
, shader
, pname
, params
);
1805 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1806 GLsizei
*length
, GLchar
*infoLog
)
1808 GET_CURRENT_CONTEXT(ctx
);
1809 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1814 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1815 GLsizei
*length
, GLchar
*infoLog
)
1817 GET_CURRENT_CONTEXT(ctx
);
1818 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1823 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1824 GLsizei
*length
, GLchar
*sourceOut
)
1826 GET_CURRENT_CONTEXT(ctx
);
1827 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1831 GLhandleARB GLAPIENTRY
1832 _mesa_GetHandleARB(GLenum pname
)
1834 GET_CURRENT_CONTEXT(ctx
);
1835 return get_handle(ctx
, pname
);
1839 GLboolean GLAPIENTRY
1840 _mesa_IsProgram(GLuint name
)
1842 GET_CURRENT_CONTEXT(ctx
);
1843 return is_program(ctx
, name
);
1847 GLboolean GLAPIENTRY
1848 _mesa_IsShader(GLuint name
)
1850 GET_CURRENT_CONTEXT(ctx
);
1851 return is_shader(ctx
, name
);
1856 _mesa_LinkProgram_no_error(GLuint programObj
)
1858 GET_CURRENT_CONTEXT(ctx
);
1860 struct gl_shader_program
*shProg
=
1861 _mesa_lookup_shader_program(ctx
, programObj
);
1862 link_program_no_error(ctx
, shProg
);
1867 _mesa_LinkProgram(GLuint programObj
)
1869 GET_CURRENT_CONTEXT(ctx
);
1871 if (MESA_VERBOSE
& VERBOSE_API
)
1872 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1874 struct gl_shader_program
*shProg
=
1875 _mesa_lookup_shader_program_err(ctx
, programObj
, "glLinkProgram");
1876 link_program_error(ctx
, shProg
);
1879 #ifdef ENABLE_SHADER_CACHE
1881 * Generate a SHA-1 hash value string for given source string.
1884 generate_sha1(const char *source
, char sha_str
[64])
1886 unsigned char sha
[20];
1887 _mesa_sha1_compute(source
, strlen(source
), sha
);
1888 _mesa_sha1_format(sha_str
, sha
);
1892 * Construct a full path for shader replacement functionality using
1895 * <path>/<stage prefix>_<CHECKSUM>.glsl
1896 * <path>/<stage prefix>_<CHECKSUM>.arb
1899 construct_name(const gl_shader_stage stage
, const char *source
,
1903 static const char *types
[] = {
1904 "VS", "TC", "TE", "GS", "FS", "CS",
1907 const char *format
= strncmp(source
, "!!ARB", 5) ? "glsl" : "arb";
1909 generate_sha1(source
, sha
);
1910 return ralloc_asprintf(NULL
, "%s/%s_%s.%s", path
, types
[stage
], sha
, format
);
1914 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1917 _mesa_dump_shader_source(const gl_shader_stage stage
, const char *source
)
1919 static bool path_exists
= true;
1926 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1928 path_exists
= false;
1932 char *name
= construct_name(stage
, source
, dump_path
);
1934 f
= fopen(name
, "w");
1939 GET_CURRENT_CONTEXT(ctx
);
1940 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1947 * Read shader source code from a file.
1948 * Useful for debugging to override an app's shader.
1951 _mesa_read_shader_source(const gl_shader_stage stage
, const char *source
)
1954 static bool path_exists
= true;
1955 int len
, shader_size
= 0;
1962 read_path
= getenv("MESA_SHADER_READ_PATH");
1964 path_exists
= false;
1968 char *name
= construct_name(stage
, source
, read_path
);
1969 f
= fopen(name
, "r");
1974 /* allocate enough room for the entire shader */
1975 fseek(f
, 0, SEEK_END
);
1976 shader_size
= ftell(f
);
1978 assert(shader_size
);
1980 /* add one for terminating zero */
1983 buffer
= malloc(shader_size
);
1986 len
= fread(buffer
, 1, shader_size
, f
);
1994 #endif /* ENABLE_SHADER_CACHE */
1997 * Called via glShaderSource() and glShaderSourceARB() API functions.
1998 * Basically, concatenate the source code strings into one long string
1999 * and pass it to _mesa_shader_source().
2001 static ALWAYS_INLINE
void
2002 shader_source(struct gl_context
*ctx
, GLuint shaderObj
, GLsizei count
,
2003 const GLchar
*const *string
, const GLint
*length
, bool no_error
)
2006 GLsizei i
, totalLength
;
2008 struct gl_shader
*sh
;
2011 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
2015 if (string
== NULL
) {
2016 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
2020 sh
= _mesa_lookup_shader(ctx
, shaderObj
);
2024 * This array holds offsets of where the appropriate string ends, thus the
2025 * last element will be set to the total length of the source code.
2027 offsets
= malloc(count
* sizeof(GLint
));
2028 if (offsets
== NULL
) {
2029 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
2033 for (i
= 0; i
< count
; i
++) {
2034 if (!no_error
&& string
[i
] == NULL
) {
2035 free((GLvoid
*) offsets
);
2036 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2037 "glShaderSourceARB(null string)");
2040 if (length
== NULL
|| length
[i
] < 0)
2041 offsets
[i
] = strlen(string
[i
]);
2043 offsets
[i
] = length
[i
];
2044 /* accumulate string lengths */
2046 offsets
[i
] += offsets
[i
- 1];
2049 /* Total length of source string is sum off all strings plus two.
2050 * One extra byte for terminating zero, another extra byte to silence
2051 * valgrind warnings in the parser/grammer code.
2053 totalLength
= offsets
[count
- 1] + 2;
2054 source
= malloc(totalLength
* sizeof(GLcharARB
));
2055 if (source
== NULL
) {
2056 free((GLvoid
*) offsets
);
2057 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
2061 for (i
= 0; i
< count
; i
++) {
2062 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
2063 memcpy(source
+ start
, string
[i
],
2064 (offsets
[i
] - start
) * sizeof(GLcharARB
));
2066 source
[totalLength
- 1] = '\0';
2067 source
[totalLength
- 2] = '\0';
2069 #ifdef ENABLE_SHADER_CACHE
2070 GLcharARB
*replacement
;
2072 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
2073 * if corresponding entry found from MESA_SHADER_READ_PATH.
2075 _mesa_dump_shader_source(sh
->Stage
, source
);
2077 replacement
= _mesa_read_shader_source(sh
->Stage
, source
);
2080 source
= replacement
;
2082 #endif /* ENABLE_SHADER_CACHE */
2084 set_shader_source(sh
, source
);
2091 _mesa_ShaderSource_no_error(GLuint shaderObj
, GLsizei count
,
2092 const GLchar
*const *string
, const GLint
*length
)
2094 GET_CURRENT_CONTEXT(ctx
);
2095 shader_source(ctx
, shaderObj
, count
, string
, length
, true);
2100 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
2101 const GLchar
*const *string
, const GLint
*length
)
2103 GET_CURRENT_CONTEXT(ctx
);
2104 shader_source(ctx
, shaderObj
, count
, string
, length
, false);
2108 static ALWAYS_INLINE
void
2109 use_program(GLuint program
, bool no_error
)
2111 GET_CURRENT_CONTEXT(ctx
);
2112 struct gl_shader_program
*shProg
= NULL
;
2114 if (MESA_VERBOSE
& VERBOSE_API
)
2115 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
2119 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2122 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
2123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2124 "glUseProgram(transform feedback active)");
2130 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
2134 if (!shProg
->data
->LinkStatus
) {
2135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2136 "glUseProgram(program %u not linked)", program
);
2141 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
2142 print_shader_info(shProg
);
2147 /* The ARB_separate_shader_object spec says:
2149 * "The executable code for an individual shader stage is taken from
2150 * the current program for that stage. If there is a current program
2151 * object established by UseProgram, that program is considered current
2152 * for all stages. Otherwise, if there is a bound program pipeline
2153 * object (section 2.14.PPO), the program bound to the appropriate
2154 * stage of the pipeline object is considered current."
2157 /* Attach shader state to the binding point */
2158 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
2159 /* Update the program */
2160 _mesa_use_shader_program(ctx
, shProg
);
2162 /* Must be done first: detach the progam */
2163 _mesa_use_shader_program(ctx
, shProg
);
2164 /* Unattach shader_state binding point */
2165 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
2166 ctx
->Pipeline
.Default
);
2167 /* If a pipeline was bound, rebind it */
2168 if (ctx
->Pipeline
.Current
) {
2170 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
2172 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
2176 _mesa_update_vertex_processing_mode(ctx
);
2181 _mesa_UseProgram_no_error(GLuint program
)
2183 use_program(program
, true);
2188 _mesa_UseProgram(GLuint program
)
2190 use_program(program
, false);
2195 _mesa_ValidateProgram(GLuint program
)
2197 GET_CURRENT_CONTEXT(ctx
);
2198 validate_program(ctx
, program
);
2203 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2206 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
2207 GLint
* range
, GLint
* precision
)
2209 const struct gl_program_constants
*limits
;
2210 const struct gl_precision
*p
;
2211 GET_CURRENT_CONTEXT(ctx
);
2213 switch (shadertype
) {
2214 case GL_VERTEX_SHADER
:
2215 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
2217 case GL_FRAGMENT_SHADER
:
2218 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
2221 _mesa_error(ctx
, GL_INVALID_ENUM
,
2222 "glGetShaderPrecisionFormat(shadertype)");
2226 switch (precisiontype
) {
2228 p
= &limits
->LowFloat
;
2230 case GL_MEDIUM_FLOAT
:
2231 p
= &limits
->MediumFloat
;
2234 p
= &limits
->HighFloat
;
2237 p
= &limits
->LowInt
;
2240 p
= &limits
->MediumInt
;
2243 p
= &limits
->HighInt
;
2246 _mesa_error(ctx
, GL_INVALID_ENUM
,
2247 "glGetShaderPrecisionFormat(precisiontype)");
2251 range
[0] = p
->RangeMin
;
2252 range
[1] = p
->RangeMax
;
2253 precision
[0] = p
->Precision
;
2258 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2261 _mesa_ReleaseShaderCompiler(void)
2263 GET_CURRENT_CONTEXT(ctx
);
2265 if (ctx
->shader_builtin_ref
) {
2266 _mesa_glsl_builtin_functions_decref();
2267 ctx
->shader_builtin_ref
= false;
2273 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2276 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2277 const void* binary
, GLint length
)
2279 GET_CURRENT_CONTEXT(ctx
);
2280 struct gl_shader
**sh
;
2282 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2283 * page 88 of the OpenGL 4.5 specs state:
2285 * "An INVALID_VALUE error is generated if count or length is negative.
2286 * An INVALID_ENUM error is generated if binaryformat is not a supported
2287 * format returned in SHADER_BINARY_FORMATS."
2289 if (n
< 0 || length
< 0) {
2290 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2294 /* Get all shader objects at once so we can make the operation
2297 if (n
> SIZE_MAX
/ sizeof(*sh
)) {
2298 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary(count)");
2302 sh
= alloca(sizeof(*sh
) * (size_t)n
);
2304 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary");
2308 for (int i
= 0; i
< n
; ++i
) {
2309 sh
[i
] = _mesa_lookup_shader_err(ctx
, shaders
[i
], "glShaderBinary");
2314 if (binaryformat
== GL_SHADER_BINARY_FORMAT_SPIR_V_ARB
) {
2315 if (!ctx
->Extensions
.ARB_gl_spirv
) {
2316 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary(SPIR-V)");
2318 _mesa_spirv_shader_binary(ctx
, (unsigned) n
, sh
, binary
,
2325 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2330 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2331 GLenum
*binaryFormat
, GLvoid
*binary
)
2333 struct gl_shader_program
*shProg
;
2334 GLsizei length_dummy
;
2335 GET_CURRENT_CONTEXT(ctx
);
2338 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2342 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2346 /* The ARB_get_program_binary spec says:
2348 * "If <length> is NULL, then no length is returned."
2350 * Ensure that length always points to valid storage to avoid multiple NULL
2351 * pointer checks below.
2354 length
= &length_dummy
;
2357 /* The ARB_get_program_binary spec says:
2359 * "When a program object's LINK_STATUS is FALSE, its program binary
2360 * length is zero, and a call to GetProgramBinary will generate an
2361 * INVALID_OPERATION error.
2363 if (!shProg
->data
->LinkStatus
) {
2364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2365 "glGetProgramBinary(program %u not linked)",
2371 if (ctx
->Const
.NumProgramBinaryFormats
== 0) {
2373 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2374 "glGetProgramBinary(driver supports zero binary formats)");
2376 _mesa_get_program_binary(ctx
, shProg
, bufSize
, length
, binaryFormat
,
2378 assert(*length
== 0 || *binaryFormat
== GL_PROGRAM_BINARY_FORMAT_MESA
);
2383 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2384 const GLvoid
*binary
, GLsizei length
)
2386 struct gl_shader_program
*shProg
;
2387 GET_CURRENT_CONTEXT(ctx
);
2389 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2393 _mesa_clear_shader_program_data(ctx
, shProg
);
2394 shProg
->data
= _mesa_create_shader_program_data();
2396 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2398 * "If a negative number is provided where an argument of type sizei or
2399 * sizeiptr is specified, an INVALID_VALUE error is generated."
2402 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2406 if (ctx
->Const
.NumProgramBinaryFormats
== 0 ||
2407 binaryFormat
!= GL_PROGRAM_BINARY_FORMAT_MESA
) {
2408 /* The ARB_get_program_binary spec says:
2410 * "<binaryFormat> and <binary> must be those returned by a previous
2411 * call to GetProgramBinary, and <length> must be the length of the
2412 * program binary as returned by GetProgramBinary or GetProgramiv with
2413 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2414 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2417 * Since any value of binaryFormat passed "is not one of those specified as
2418 * allowable for [this] command, an INVALID_ENUM error is generated."
2420 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2421 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2423 _mesa_program_binary(ctx
, shProg
, binaryFormat
, binary
, length
);
2428 static ALWAYS_INLINE
void
2429 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2430 GLuint pname
, GLint value
, bool no_error
)
2433 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2434 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2435 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2436 * even be in the dispatch table, so we shouldn't need to expclicitly
2439 * On desktop, we ignore the 3.0+ requirement because it is silly.
2442 /* The ARB_get_program_binary extension spec says:
2444 * "An INVALID_VALUE error is generated if the <value> argument to
2445 * ProgramParameteri is not TRUE or FALSE."
2447 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2451 /* No need to notify the driver. Any changes will actually take effect
2452 * the next time the shader is linked.
2454 * The ARB_get_program_binary extension spec says:
2456 * "To indicate that a program binary is likely to be retrieved,
2457 * ProgramParameteri should be called with <pname>
2458 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2459 * will not be in effect until the next time LinkProgram or
2460 * ProgramBinary has been called successfully."
2462 * The resolution of issue 9 in the extension spec also says:
2464 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2465 * to indicate to the GL implementation that this program will
2466 * likely be saved with GetProgramBinary at some point. This will
2467 * give the GL implementation the opportunity to track any state
2468 * changes made to the program before being saved such that when it
2469 * is loaded again a recompile can be avoided."
2471 shProg
->BinaryRetrievableHintPending
= value
;
2474 case GL_PROGRAM_SEPARABLE
:
2475 /* Spec imply that the behavior is the same as ARB_get_program_binary
2476 * Chapter 7.3 Program Objects
2478 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2481 shProg
->SeparateShader
= value
;
2486 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2487 _mesa_enum_to_string(pname
));
2493 _mesa_error(ctx
, GL_INVALID_VALUE
,
2494 "glProgramParameteri(pname=%s, value=%d): "
2495 "value must be 0 or 1.",
2496 _mesa_enum_to_string(pname
),
2502 _mesa_ProgramParameteri_no_error(GLuint program
, GLenum pname
, GLint value
)
2504 GET_CURRENT_CONTEXT(ctx
);
2506 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
2507 program_parameteri(ctx
, shProg
, pname
, value
, true);
2512 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2514 struct gl_shader_program
*shProg
;
2515 GET_CURRENT_CONTEXT(ctx
);
2517 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2518 "glProgramParameteri");
2522 program_parameteri(ctx
, shProg
, pname
, value
, false);
2527 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2528 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2529 struct gl_pipeline_object
*shTarget
)
2531 struct gl_program
**target
;
2533 target
= &shTarget
->CurrentProgram
[stage
];
2535 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2538 if (*target
!= prog
) {
2539 /* Program is current, flush it */
2540 if (shTarget
== ctx
->_Shader
) {
2541 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2544 _mesa_reference_shader_program(ctx
,
2545 &shTarget
->ReferencedPrograms
[stage
],
2547 _mesa_reference_program(ctx
, target
, prog
);
2548 if (stage
== MESA_SHADER_VERTEX
)
2549 _mesa_update_vertex_processing_mode(ctx
);
2557 * Copy program-specific data generated by linking from the gl_shader_program
2558 * object to the gl_program object referred to by the gl_linked_shader.
2560 * This function expects _mesa_reference_program() to have been previously
2561 * called setting the gl_linked_shaders program reference.
2564 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2565 struct gl_linked_shader
*dst_sh
)
2567 assert(dst_sh
->Program
);
2569 struct gl_program
*dst
= dst_sh
->Program
;
2571 dst
->info
.separate_shader
= src
->SeparateShader
;
2573 switch (dst_sh
->Stage
) {
2574 case MESA_SHADER_GEOMETRY
: {
2575 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2576 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2577 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2580 case MESA_SHADER_FRAGMENT
: {
2581 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2584 case MESA_SHADER_COMPUTE
: {
2585 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2594 * ARB_separate_shader_objects: Compile & Link Program
2597 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2598 const GLchar
* const *strings
)
2600 GET_CURRENT_CONTEXT(ctx
);
2602 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2606 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2607 * GL_INVALID_VALUE should be generated if count < 0
2610 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2615 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2617 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2618 _mesa_compile_shader(ctx
, sh
);
2620 program
= create_shader_program(ctx
);
2622 struct gl_shader_program
*shProg
;
2623 GLint compiled
= GL_FALSE
;
2625 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2627 shProg
->SeparateShader
= GL_TRUE
;
2629 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2631 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2632 _mesa_link_program(ctx
, shProg
);
2633 detach_shader_error(ctx
, program
, shader
);
2637 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2638 append
-error
-to
-info
-log
;
2639 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2644 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2647 delete_shader(ctx
, shader
);
2655 * For GL_ARB_tessellation_shader
2658 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2660 GET_CURRENT_CONTEXT(ctx
);
2661 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2665 extern void GLAPIENTRY
2666 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2668 GET_CURRENT_CONTEXT(ctx
);
2670 if (!_mesa_has_tessellation(ctx
)) {
2671 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2675 if (pname
!= GL_PATCH_VERTICES
) {
2676 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2680 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2681 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2685 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2689 extern void GLAPIENTRY
2690 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2692 GET_CURRENT_CONTEXT(ctx
);
2694 if (!_mesa_has_tessellation(ctx
)) {
2695 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2700 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2701 FLUSH_VERTICES(ctx
, 0);
2702 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2703 4 * sizeof(GLfloat
));
2704 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2706 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2707 FLUSH_VERTICES(ctx
, 0);
2708 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2709 2 * sizeof(GLfloat
));
2710 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2713 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2719 * ARB_shader_subroutine
2722 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2725 GET_CURRENT_CONTEXT(ctx
);
2726 const char *api_name
= "glGetSubroutineUniformLocation";
2727 struct gl_shader_program
*shProg
;
2728 GLenum resource_type
;
2729 gl_shader_stage stage
;
2731 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2732 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2736 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2740 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2741 if (!shProg
->_LinkedShaders
[stage
]) {
2742 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2746 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2747 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2751 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2754 GET_CURRENT_CONTEXT(ctx
);
2755 const char *api_name
= "glGetSubroutineIndex";
2756 struct gl_shader_program
*shProg
;
2757 struct gl_program_resource
*res
;
2758 GLenum resource_type
;
2759 gl_shader_stage stage
;
2761 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2762 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2766 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2770 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2771 if (!shProg
->_LinkedShaders
[stage
]) {
2772 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2776 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2777 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2782 return _mesa_program_resource_index(shProg
, res
);
2787 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2788 GLuint index
, GLenum pname
, GLint
*values
)
2790 GET_CURRENT_CONTEXT(ctx
);
2791 const char *api_name
= "glGetActiveSubroutineUniformiv";
2792 struct gl_shader_program
*shProg
;
2793 struct gl_linked_shader
*sh
;
2794 gl_shader_stage stage
;
2795 struct gl_program_resource
*res
;
2796 const struct gl_uniform_storage
*uni
;
2797 GLenum resource_type
;
2800 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2801 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2805 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2809 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2810 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2812 sh
= shProg
->_LinkedShaders
[stage
];
2814 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2818 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2819 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2820 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2825 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2826 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2829 values
[0] = uni
->num_compatible_subroutines
;
2833 case GL_COMPATIBLE_SUBROUTINES
: {
2834 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2838 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2839 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2840 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2841 if (fn
->types
[j
] == uni
->type
) {
2842 values
[count
++] = i
;
2850 case GL_UNIFORM_SIZE
:
2851 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2854 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2857 case GL_UNIFORM_NAME_LENGTH
:
2858 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2860 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2861 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2865 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2872 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2873 GLuint index
, GLsizei bufsize
,
2874 GLsizei
*length
, GLchar
*name
)
2876 GET_CURRENT_CONTEXT(ctx
);
2877 const char *api_name
= "glGetActiveSubroutineUniformName";
2878 struct gl_shader_program
*shProg
;
2879 GLenum resource_type
;
2880 gl_shader_stage stage
;
2882 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2883 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2887 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2891 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2892 if (!shProg
->_LinkedShaders
[stage
]) {
2893 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2897 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2898 /* get program resource name */
2899 _mesa_get_program_resource_name(shProg
, resource_type
,
2901 length
, name
, api_name
);
2906 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2907 GLuint index
, GLsizei bufsize
,
2908 GLsizei
*length
, GLchar
*name
)
2910 GET_CURRENT_CONTEXT(ctx
);
2911 const char *api_name
= "glGetActiveSubroutineName";
2912 struct gl_shader_program
*shProg
;
2913 GLenum resource_type
;
2914 gl_shader_stage stage
;
2916 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2917 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2921 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2925 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2926 if (!shProg
->_LinkedShaders
[stage
]) {
2927 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2930 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2931 _mesa_get_program_resource_name(shProg
, resource_type
,
2933 length
, name
, api_name
);
2937 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2938 const GLuint
*indices
)
2940 GET_CURRENT_CONTEXT(ctx
);
2941 const char *api_name
= "glUniformSubroutinesuiv";
2942 gl_shader_stage stage
;
2945 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2946 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2950 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2951 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2953 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2957 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2958 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2963 bool flushed
= false;
2965 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2972 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2976 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2979 for (j
= i
; j
< i
+ uni_count
; j
++) {
2980 struct gl_subroutine_function
*subfn
= NULL
;
2981 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2982 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2986 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2987 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2988 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2995 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2996 if (subfn
->types
[k
] == uni
->type
)
2999 if (k
== subfn
->num_compat_types
) {
3000 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3004 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
3012 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
3015 GET_CURRENT_CONTEXT(ctx
);
3016 const char *api_name
= "glGetUniformSubroutineuiv";
3017 gl_shader_stage stage
;
3019 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
3020 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3024 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
3025 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
3027 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3031 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
3032 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
3036 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
3041 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
3042 GLenum pname
, GLint
*values
)
3044 GET_CURRENT_CONTEXT(ctx
);
3045 const char *api_name
= "glGetProgramStageiv";
3046 struct gl_shader_program
*shProg
;
3047 struct gl_linked_shader
*sh
;
3048 gl_shader_stage stage
;
3050 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
3051 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3055 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
3059 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
3060 sh
= shProg
->_LinkedShaders
[stage
];
3062 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
3063 * INVALID_OPERATION in the case of not be linked.
3065 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
3066 * same info using other specs (ARB_program_interface_query), without the
3067 * need of the program to be linked, being the value for that case 0.
3069 * But at the same time, some other methods require the program to be
3070 * linked for pname related to locations, so it would be inconsistent to
3071 * not do the same here. So we are:
3072 * * Return GL_INVALID_OPERATION if not linked only for locations.
3073 * * Setting a default value of 0, to be returned if not linked.
3077 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
3078 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3083 struct gl_program
*p
= sh
->Program
;
3085 case GL_ACTIVE_SUBROUTINES
:
3086 values
[0] = p
->sh
.NumSubroutineFunctions
;
3088 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
3089 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
3091 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
3092 values
[0] = p
->sh
.NumSubroutineUniforms
;
3094 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
3098 GLenum resource_type
;
3099 struct gl_program_resource
*res
;
3101 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
3102 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3103 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3105 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
3110 values
[0] = max_len
;
3113 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
3117 GLenum resource_type
;
3118 struct gl_program_resource
*res
;
3120 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
3121 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3122 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3124 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
3125 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
3131 values
[0] = max_len
;
3135 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
3142 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
3146 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3147 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
3148 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
3149 if (fn
->types
[j
] == type
)
3157 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
3158 struct gl_program
*p
)
3162 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
3167 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3176 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
3177 for (j
= 0; j
< uni_count
; j
++) {
3178 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
3179 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
3182 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
3184 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
3188 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
3189 gl_shader_stage stage
)
3191 if (ctx
->_Shader
->CurrentProgram
[stage
])
3192 _mesa_shader_write_subroutine_index(ctx
,
3193 ctx
->_Shader
->CurrentProgram
[stage
]);
3197 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
3198 struct gl_program
*p
)
3202 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
3203 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
3204 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
3205 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
3206 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
3209 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3210 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3215 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);