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>
39 #include "main/glheader.h"
40 #include "main/context.h"
41 #include "main/enums.h"
42 #include "main/glspirv.h"
43 #include "main/hash.h"
44 #include "main/mtypes.h"
45 #include "main/pipelineobj.h"
46 #include "main/program_binary.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/state.h"
50 #include "main/transformfeedback.h"
51 #include "main/uniforms.h"
52 #include "compiler/glsl/builtin_functions.h"
53 #include "compiler/glsl/glsl_parser_extras.h"
54 #include "compiler/glsl/ir.h"
55 #include "compiler/glsl/ir_uniform.h"
56 #include "compiler/glsl/program.h"
57 #include "program/program.h"
58 #include "program/prog_print.h"
59 #include "program/prog_parameter.h"
60 #include "util/ralloc.h"
61 #include "util/hash_table.h"
62 #include "util/mesa-sha1.h"
63 #include "util/crc32.h"
64 #include "util/os_file.h"
65 #include "util/simple_list.h"
66 #include "util/u_string.h"
69 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
72 _mesa_get_shader_flags(void)
74 GLbitfield flags
= 0x0;
75 const char *env
= getenv("MESA_GLSL");
78 if (strstr(env
, "dump_on_error"))
79 flags
|= GLSL_DUMP_ON_ERROR
;
80 else if (strstr(env
, "dump"))
82 if (strstr(env
, "log"))
84 if (strstr(env
, "cache_fb"))
85 flags
|= GLSL_CACHE_FALLBACK
;
86 if (strstr(env
, "cache_info"))
87 flags
|= GLSL_CACHE_INFO
;
88 if (strstr(env
, "nopvert"))
89 flags
|= GLSL_NOP_VERT
;
90 if (strstr(env
, "nopfrag"))
91 flags
|= GLSL_NOP_FRAG
;
92 if (strstr(env
, "uniform"))
93 flags
|= GLSL_UNIFORMS
;
94 if (strstr(env
, "useprog"))
95 flags
|= GLSL_USE_PROG
;
96 if (strstr(env
, "errors"))
97 flags
|= GLSL_REPORT_ERRORS
;
104 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
107 _mesa_get_shader_capture_path(void)
109 static bool read_env_var
= false;
110 static const char *path
= NULL
;
113 path
= getenv("MESA_SHADER_CAPTURE_PATH");
121 * Initialize context's shader state.
124 _mesa_init_shader_state(struct gl_context
*ctx
)
126 /* Device drivers may override these to control what kind of instructions
127 * are generated by the GLSL compiler.
129 struct gl_shader_compiler_options options
;
133 memset(&options
, 0, sizeof(options
));
134 options
.MaxUnrollIterations
= 32;
135 options
.MaxIfDepth
= UINT_MAX
;
137 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
138 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
140 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
142 if (ctx
->Shader
.Flags
!= 0)
143 ctx
->Const
.GenerateTemporaryNames
= true;
145 /* Extended for ARB_separate_shader_objects */
146 ctx
->Shader
.RefCount
= 1;
147 ctx
->TessCtrlProgram
.patch_vertices
= 3;
148 for (i
= 0; i
< 4; ++i
)
149 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
150 for (i
= 0; i
< 2; ++i
)
151 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
156 * Free the per-context shader-related state.
159 _mesa_free_shader_state(struct gl_context
*ctx
)
161 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
162 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
163 _mesa_reference_shader_program(ctx
,
164 &ctx
->Shader
.ReferencedPrograms
[i
],
166 free(ctx
->SubroutineIndex
[i
].IndexPtr
);
167 ctx
->SubroutineIndex
[i
].IndexPtr
= NULL
;
169 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
171 /* Extended for ARB_separate_shader_objects */
172 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
174 assert(ctx
->Shader
.RefCount
== 1);
179 * Copy string from <src> to <dst>, up to maxLength characters, returning
180 * length of <dst> in <length>.
181 * \param src the strings source
182 * \param maxLength max chars to copy
183 * \param length returns number of chars copied
184 * \param dst the string destination
187 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
188 GLsizei
*length
, const GLchar
*src
)
191 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
202 * Confirm that the a shader type is valid and supported by the implementation
204 * \param ctx Current GL context
205 * \param type Shader target
209 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
211 /* Note: when building built-in GLSL functions, this function may be
212 * invoked with ctx == NULL. In that case, we can only validate that it's
213 * a shader target we recognize, not that it's supported in the current
214 * context. But that's fine--we don't need any further validation than
215 * that when building built-in GLSL functions.
219 case GL_FRAGMENT_SHADER
:
220 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
221 case GL_VERTEX_SHADER
:
222 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
223 case GL_GEOMETRY_SHADER_ARB
:
224 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
225 case GL_TESS_CONTROL_SHADER
:
226 case GL_TESS_EVALUATION_SHADER
:
227 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
228 case GL_COMPUTE_SHADER
:
229 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
237 is_program(struct gl_context
*ctx
, GLuint name
)
239 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
240 return shProg
? GL_TRUE
: GL_FALSE
;
245 is_shader(struct gl_context
*ctx
, GLuint name
)
247 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
248 return shader
? GL_TRUE
: GL_FALSE
;
253 * Attach shader to a shader program.
256 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
257 struct gl_shader
*sh
)
259 GLuint n
= shProg
->NumShaders
;
261 shProg
->Shaders
= realloc(shProg
->Shaders
,
262 (n
+ 1) * sizeof(struct gl_shader
*));
263 if (!shProg
->Shaders
) {
264 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
269 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
270 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
271 shProg
->NumShaders
++;
275 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
278 struct gl_shader_program
*shProg
;
279 struct gl_shader
*sh
;
282 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
284 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
288 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
293 n
= shProg
->NumShaders
;
294 for (i
= 0; i
< n
; i
++) {
295 if (shProg
->Shaders
[i
] == sh
) {
296 /* The shader is already attched to this program. The
297 * GL_ARB_shader_objects spec says:
299 * "The error INVALID_OPERATION is generated by AttachObjectARB
300 * if <obj> is already attached to <containerObj>."
302 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
304 } else if (same_type_disallowed
&&
305 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
306 /* Shader with the same type is already attached to this program,
307 * OpenGL ES 2.0 and 3.0 specs say:
309 * "Multiple shader objects of the same type may not be attached
310 * to a single program object. [...] The error INVALID_OPERATION
311 * is generated if [...] another shader object of the same type
312 * as shader is already attached to program."
314 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
319 attach_shader(ctx
, shProg
, sh
);
323 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
325 struct gl_shader_program
*shProg
;
326 struct gl_shader
*sh
;
328 shProg
= _mesa_lookup_shader_program(ctx
, program
);
329 sh
= _mesa_lookup_shader(ctx
, shader
);
331 attach_shader(ctx
, shProg
, sh
);
335 create_shader(struct gl_context
*ctx
, GLenum type
)
337 struct gl_shader
*sh
;
340 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
341 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
342 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
344 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
345 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
352 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
354 if (!_mesa_validate_shader_target(ctx
, type
)) {
355 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
356 caller
, _mesa_enum_to_string(type
));
360 return create_shader(ctx
, type
);
365 create_shader_program(struct gl_context
*ctx
)
368 struct gl_shader_program
*shProg
;
370 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
372 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
374 shProg
= _mesa_new_shader_program(name
);
376 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
378 assert(shProg
->RefCount
== 1);
380 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
387 * Delete a shader program. Actually, just decrement the program's
388 * reference count and mark it as DeletePending.
389 * Used to implement glDeleteProgram() and glDeleteObjectARB().
392 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
395 * NOTE: deleting shaders/programs works a bit differently than
396 * texture objects (and buffer objects, etc). Shader/program
397 * handles/IDs exist in the hash table until the object is really
398 * deleted (refcount==0). With texture objects, the handle/ID is
399 * removed from the hash table in glDeleteTextures() while the tex
400 * object itself might linger until its refcount goes to zero.
402 struct gl_shader_program
*shProg
;
404 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
408 if (!shProg
->DeletePending
) {
409 shProg
->DeletePending
= GL_TRUE
;
411 /* effectively, decr shProg's refcount */
412 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
418 delete_shader(struct gl_context
*ctx
, GLuint shader
)
420 struct gl_shader
*sh
;
422 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
426 if (!sh
->DeletePending
) {
427 sh
->DeletePending
= GL_TRUE
;
429 /* effectively, decr sh's refcount */
430 _mesa_reference_shader(ctx
, &sh
, NULL
);
435 static ALWAYS_INLINE
void
436 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
439 struct gl_shader_program
*shProg
;
444 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
448 shProg
= _mesa_lookup_shader_program(ctx
, program
);
451 n
= shProg
->NumShaders
;
453 for (i
= 0; i
< n
; i
++) {
454 if (shProg
->Shaders
[i
]->Name
== shader
) {
456 struct gl_shader
**newList
;
459 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
461 /* alloc new, smaller array */
462 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
464 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
467 /* Copy old list entries to new list, skipping removed entry at [i] */
468 for (j
= 0; j
< i
; j
++) {
469 newList
[j
] = shProg
->Shaders
[j
];
472 newList
[j
++] = shProg
->Shaders
[i
];
475 /* Free old list and install new one */
476 free(shProg
->Shaders
);
477 shProg
->Shaders
= newList
;
478 shProg
->NumShaders
= n
- 1;
481 /* sanity check - make sure the new list's entries are sensible */
482 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
483 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
484 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
485 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
486 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
487 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
488 assert(shProg
->Shaders
[j
]->RefCount
> 0);
499 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
500 err
= GL_INVALID_OPERATION
;
502 err
= GL_INVALID_VALUE
;
503 _mesa_error(ctx
, err
, "glDetachShader(shader)");
510 detach_shader_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
512 detach_shader(ctx
, program
, shader
, false);
517 detach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
519 detach_shader(ctx
, program
, shader
, true);
524 * Return list of shaders attached to shader program.
525 * \param objOut returns GLuint ids
526 * \param handleOut returns GLhandleARB handles
529 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
530 GLsizei
*countOut
, GLuint
*objOut
, GLhandleARB
*handleOut
)
532 struct gl_shader_program
*shProg
;
535 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
540 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
544 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
546 objOut
[i
] = shProg
->Shaders
[i
]->Name
;
550 handleOut
[i
] = (GLhandleARB
) shProg
->Shaders
[i
]->Name
;
560 * glGetHandleARB() - return ID/name of currently bound shader program.
563 get_handle(struct gl_context
*ctx
, GLenum pname
)
565 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
566 if (ctx
->_Shader
->ActiveProgram
)
567 return ctx
->_Shader
->ActiveProgram
->Name
;
572 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
579 * Check if a geometry shader query is valid at this time. If not, report an
580 * error and return false.
582 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
584 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
585 * are queried for a program which has not been linked successfully, or
586 * which does not contain objects to form a geometry shader, then an
587 * INVALID_OPERATION error is generated."
590 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
592 if (shProg
->data
->LinkStatus
&&
593 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
597 _mesa_error(ctx
, GL_INVALID_OPERATION
,
598 "glGetProgramv(linked geometry shader required)");
604 * Check if a tessellation control shader query is valid at this time.
605 * If not, report an error and return false.
607 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
609 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
610 * not been linked successfully, or which does not contain objects to
611 * form a tessellation control shader, then an INVALID_OPERATION error is
615 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
617 if (shProg
->data
->LinkStatus
&&
618 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
622 _mesa_error(ctx
, GL_INVALID_OPERATION
,
623 "glGetProgramv(linked tessellation control shader required)");
629 * Check if a tessellation evaluation shader query is valid at this time.
630 * If not, report an error and return false.
632 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
634 * "If any of the pname values in this paragraph are queried for a program
635 * which has not been linked successfully, or which does not contain
636 * objects to form a tessellation evaluation shader, then an
637 * INVALID_OPERATION error is generated."
641 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
643 if (shProg
->data
->LinkStatus
&&
644 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
648 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
649 "evaluation shader required)");
654 * Return the length of a string, or 0 if the pointer passed in is NULL
656 static size_t strlen_or_zero(const char *s
)
658 return s
? strlen(s
) : 0;
662 * glGetProgramiv() - get shader program state.
663 * Note that this is for GLSL shader programs, not ARB vertex/fragment
664 * programs (see glGetProgramivARB).
667 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
670 struct gl_shader_program
*shProg
671 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
673 /* Is transform feedback available in this context?
676 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
677 || ctx
->API
== API_OPENGL_CORE
678 || _mesa_is_gles3(ctx
);
680 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
681 * and GL 3.2) are available in this context
683 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
684 const bool has_tess
= _mesa_has_tessellation(ctx
);
686 /* Are uniform buffer objects available in this context?
689 (ctx
->API
== API_OPENGL_COMPAT
&&
690 ctx
->Extensions
.ARB_uniform_buffer_object
)
691 || ctx
->API
== API_OPENGL_CORE
692 || _mesa_is_gles3(ctx
);
699 case GL_DELETE_STATUS
:
700 *params
= shProg
->DeletePending
;
702 case GL_COMPLETION_STATUS_ARB
:
703 if (ctx
->Driver
.GetShaderProgramCompletionStatus
)
704 *params
= ctx
->Driver
.GetShaderProgramCompletionStatus(ctx
, shProg
);
709 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
711 case GL_VALIDATE_STATUS
:
712 *params
= shProg
->data
->Validated
;
714 case GL_INFO_LOG_LENGTH
:
715 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
716 strlen(shProg
->data
->InfoLog
) + 1 : 0;
718 case GL_ATTACHED_SHADERS
:
719 *params
= shProg
->NumShaders
;
721 case GL_ACTIVE_ATTRIBUTES
:
722 *params
= _mesa_count_active_attribs(shProg
);
724 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
725 *params
= _mesa_longest_attribute_name_length(shProg
);
727 case GL_ACTIVE_UNIFORMS
: {
729 const unsigned num_uniforms
=
730 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
731 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
732 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
737 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
740 const unsigned num_uniforms
=
741 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
743 for (i
= 0; i
< num_uniforms
; i
++) {
744 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
747 /* From ARB_gl_spirv spec:
749 * "If pname is ACTIVE_UNIFORM_MAX_LENGTH, the length of the
750 * longest active uniform name, including a null terminator, is
751 * returned. If no active uniforms exist, zero is returned. If no
752 * name reflection information is available, one is returned."
754 * We are setting 0 here, as below it will add 1 for the NUL character.
756 const GLint base_len
=
757 strlen_or_zero(shProg
->data
->UniformStorage
[i
].name
);
759 /* Add one for the terminating NUL character for a non-array, and
760 * 4 for the "[0]" and the NUL for an array.
762 const GLint len
= base_len
+ 1 +
763 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
772 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
776 /* Check first if there are transform feedback varyings specified in the
777 * shader (ARB_enhanced_layouts). If there isn't any, return the number of
778 * varyings specified using the API.
780 if (shProg
->last_vert_prog
&&
781 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
> 0)
783 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
;
785 *params
= shProg
->TransformFeedback
.NumVarying
;
787 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
790 bool in_shader_varyings
;
796 /* Check first if there are transform feedback varyings specified in the
797 * shader (ARB_enhanced_layouts). If there isn't any, use the ones
798 * specified using the API.
800 in_shader_varyings
= shProg
->last_vert_prog
&&
801 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
> 0;
803 num_varying
= in_shader_varyings
?
804 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
:
805 shProg
->TransformFeedback
.NumVarying
;
807 for (i
= 0; i
< num_varying
; i
++) {
808 const char *name
= in_shader_varyings
?
809 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
->Varyings
[i
].Name
810 : shProg
->TransformFeedback
.VaryingNames
[i
];
812 /* Add one for the terminating NUL character. We have to use
813 * strlen_or_zero, as for shaders constructed from SPIR-V binaries,
814 * it is possible that no name reflection information is available.
816 const GLint len
= strlen_or_zero(name
) + 1;
825 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
828 *params
= shProg
->TransformFeedback
.BufferMode
;
830 case GL_GEOMETRY_VERTICES_OUT
:
833 if (check_gs_query(ctx
, shProg
)) {
834 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
835 Program
->info
.gs
.vertices_out
;
838 case GL_GEOMETRY_SHADER_INVOCATIONS
:
840 (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_gpu_shader5
)) {
843 if (check_gs_query(ctx
, shProg
)) {
844 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
845 Program
->info
.gs
.invocations
;
848 case GL_GEOMETRY_INPUT_TYPE
:
851 if (check_gs_query(ctx
, shProg
)) {
852 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
853 Program
->info
.gs
.input_primitive
;
856 case GL_GEOMETRY_OUTPUT_TYPE
:
859 if (check_gs_query(ctx
, shProg
)) {
860 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
861 Program
->info
.gs
.output_primitive
;
864 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
871 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
872 /* Add one for the terminating NUL character. Name can be NULL, in
873 * that case, from ARB_gl_spirv:
874 * "If pname is ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, the length of
875 * the longest active uniform block name, including the null
876 * terminator, is returned. If no active uniform blocks exist,
877 * zero is returned. If no name reflection information is
878 * available, one is returned."
881 strlen_or_zero(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
890 case GL_ACTIVE_UNIFORM_BLOCKS
:
894 *params
= shProg
->data
->NumUniformBlocks
;
896 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
897 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
898 * only available with desktop OpenGL 3.0+ with the
899 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
901 * On desktop, we ignore the 3.0+ requirement because it is silly.
903 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
906 *params
= shProg
->BinaryRetrievableHint
;
908 case GL_PROGRAM_BINARY_LENGTH
:
909 if (ctx
->Const
.NumProgramBinaryFormats
== 0 || !shProg
->data
->LinkStatus
) {
912 _mesa_get_program_binary_length(ctx
, shProg
, params
);
915 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
916 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
919 *params
= shProg
->data
->NumAtomicBuffers
;
921 case GL_COMPUTE_WORK_GROUP_SIZE
: {
923 if (!_mesa_has_compute_shaders(ctx
))
925 if (!shProg
->data
->LinkStatus
) {
926 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
930 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
931 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
935 for (i
= 0; i
< 3; i
++)
936 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
937 Program
->info
.cs
.local_size
[i
];
940 case GL_PROGRAM_SEPARABLE
:
941 /* If the program has not been linked, return initial value 0. */
942 *params
= (shProg
->data
->LinkStatus
== LINKING_FAILURE
) ? 0 : shProg
->SeparateShader
;
945 /* ARB_tessellation_shader */
946 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
949 if (check_tcs_query(ctx
, shProg
)) {
950 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
951 Program
->info
.tess
.tcs_vertices_out
;
954 case GL_TESS_GEN_MODE
:
957 if (check_tes_query(ctx
, shProg
)) {
958 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
959 Program
->info
.tess
.primitive_mode
;
962 case GL_TESS_GEN_SPACING
:
965 if (check_tes_query(ctx
, shProg
)) {
966 const struct gl_linked_shader
*tes
=
967 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
968 switch (tes
->Program
->info
.tess
.spacing
) {
969 case TESS_SPACING_EQUAL
:
972 case TESS_SPACING_FRACTIONAL_ODD
:
973 *params
= GL_FRACTIONAL_ODD
;
975 case TESS_SPACING_FRACTIONAL_EVEN
:
976 *params
= GL_FRACTIONAL_EVEN
;
978 case TESS_SPACING_UNSPECIFIED
:
984 case GL_TESS_GEN_VERTEX_ORDER
:
987 if (check_tes_query(ctx
, shProg
)) {
988 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
989 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
992 case GL_TESS_GEN_POINT_MODE
:
995 if (check_tes_query(ctx
, shProg
)) {
996 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
997 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
1004 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
1005 _mesa_enum_to_string(pname
));
1010 * glGetShaderiv() - get GLSL shader state
1013 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
1015 struct gl_shader
*shader
=
1016 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
1023 case GL_SHADER_TYPE
:
1024 *params
= shader
->Type
;
1026 case GL_DELETE_STATUS
:
1027 *params
= shader
->DeletePending
;
1029 case GL_COMPLETION_STATUS_ARB
:
1030 /* _mesa_glsl_compile_shader is not offloaded to other threads. */
1033 case GL_COMPILE_STATUS
:
1034 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
1036 case GL_INFO_LOG_LENGTH
:
1037 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
1038 strlen(shader
->InfoLog
) + 1 : 0;
1040 case GL_SHADER_SOURCE_LENGTH
:
1041 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
1043 case GL_SPIR_V_BINARY_ARB
:
1044 *params
= (shader
->spirv_data
!= NULL
);
1047 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
1054 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
1055 GLsizei
*length
, GLchar
*infoLog
)
1057 struct gl_shader_program
*shProg
;
1059 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1060 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1062 * "If a negative number is provided where an argument of type sizei or
1063 * sizeiptr is specified, an INVALID_VALUE error is generated."
1066 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
1070 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1071 "glGetProgramInfoLog(program)");
1076 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
1081 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
1082 GLsizei
*length
, GLchar
*infoLog
)
1084 struct gl_shader
*sh
;
1086 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1087 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1089 * "If a negative number is provided where an argument of type sizei or
1090 * sizeiptr is specified, an INVALID_VALUE error is generated."
1093 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
1097 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
1102 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1107 * Return shader source code.
1110 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1111 GLsizei
*length
, GLchar
*sourceOut
)
1113 struct gl_shader
*sh
;
1115 if (maxLength
< 0) {
1116 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1120 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1124 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1129 * Set/replace shader source code. A helper function used by
1130 * glShaderSource[ARB].
1133 set_shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1137 /* The GL_ARB_gl_spirv spec adds the following to the end of the description
1140 * "If <shader> was previously associated with a SPIR-V module (via the
1141 * ShaderBinary command), that association is broken. Upon successful
1142 * completion of this command the SPIR_V_BINARY_ARB state of <shader>
1145 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
1147 if (sh
->CompileStatus
== COMPILE_SKIPPED
&& !sh
->FallbackSource
) {
1148 /* If shader was previously compiled back-up the source in case of cache
1151 sh
->FallbackSource
= sh
->Source
;
1152 sh
->Source
= source
;
1154 /* free old shader source string and install new one */
1155 free((void *)sh
->Source
);
1156 sh
->Source
= source
;
1160 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1165 ensure_builtin_types(struct gl_context
*ctx
)
1167 if (!ctx
->shader_builtin_ref
) {
1168 _mesa_glsl_builtin_functions_init_or_ref();
1169 ctx
->shader_builtin_ref
= true;
1177 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1182 /* The GL_ARB_gl_spirv spec says:
1184 * "Add a new error for the CompileShader command:
1186 * An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB
1187 * state of <shader> is TRUE."
1189 if (sh
->spirv_data
) {
1190 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCompileShader(SPIR-V)");
1195 /* If the user called glCompileShader without first calling
1196 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1198 sh
->CompileStatus
= COMPILE_FAILURE
;
1200 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1201 _mesa_log("GLSL source for %s shader %d:\n",
1202 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1203 _mesa_log("%s\n", sh
->Source
);
1206 ensure_builtin_types(ctx
);
1208 /* this call will set the shader->CompileStatus field to indicate if
1209 * compilation was successful.
1211 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1213 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1214 _mesa_write_shader_to_file(sh
);
1217 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1218 if (sh
->CompileStatus
) {
1220 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1221 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1223 _mesa_log("No GLSL IR for shader %d (shader may be from "
1224 "cache)\n", sh
->Name
);
1228 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1230 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1231 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1232 _mesa_log("%s\n", sh
->InfoLog
);
1237 if (!sh
->CompileStatus
) {
1238 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1239 _mesa_log("GLSL source for %s shader %d:\n",
1240 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1241 _mesa_log("%s\n", sh
->Source
);
1242 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1245 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1246 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1247 sh
->Name
, sh
->InfoLog
);
1253 struct update_programs_in_pipeline_params
1255 struct gl_context
*ctx
;
1256 struct gl_shader_program
*shProg
;
1260 update_programs_in_pipeline(GLuint key
, void *data
, void *userData
)
1262 struct update_programs_in_pipeline_params
*params
=
1263 (struct update_programs_in_pipeline_params
*) userData
;
1264 struct gl_pipeline_object
*obj
= (struct gl_pipeline_object
*) data
;
1266 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1267 if (obj
->CurrentProgram
[stage
] &&
1268 obj
->CurrentProgram
[stage
]->Id
== params
->shProg
->Name
) {
1269 struct gl_program
*prog
= params
->shProg
->_LinkedShaders
[stage
]->Program
;
1270 _mesa_use_program(params
->ctx
, stage
, params
->shProg
, prog
, obj
);
1277 * Link a program's shaders.
1279 static ALWAYS_INLINE
void
1280 link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1287 /* From the ARB_transform_feedback2 specification:
1288 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1289 * is the name of a program being used by one or more transform feedback
1290 * objects, even if the objects are not currently bound or are paused."
1292 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1294 "glLinkProgram(transform feedback is using the program)");
1299 unsigned programs_in_use
= 0;
1301 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1302 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1303 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1304 programs_in_use
|= 1 << stage
;
1308 ensure_builtin_types(ctx
);
1310 FLUSH_VERTICES(ctx
, 0);
1311 _mesa_glsl_link_shader(ctx
, shProg
);
1313 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1315 * "If LinkProgram or ProgramBinary successfully re-links a program
1316 * object that is active for any shader stage, then the newly generated
1317 * executable code will be installed as part of the current rendering
1318 * state for all shader stages where the program is active.
1319 * Additionally, the newly generated executable code is made part of
1320 * the state of any program pipeline for all stages where the program
1323 if (shProg
->data
->LinkStatus
) {
1324 while (programs_in_use
) {
1325 const int stage
= u_bit_scan(&programs_in_use
);
1327 struct gl_program
*prog
= NULL
;
1328 if (shProg
->_LinkedShaders
[stage
])
1329 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1331 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1334 if (ctx
->Pipeline
.Objects
) {
1335 struct update_programs_in_pipeline_params params
= {
1339 _mesa_HashWalk(ctx
->Pipeline
.Objects
, update_programs_in_pipeline
,
1344 /* Capture .shader_test files. */
1345 const char *capture_path
= _mesa_get_shader_capture_path();
1346 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1347 /* Find an unused filename. */
1349 char *filename
= NULL
;
1350 for (unsigned i
= 0;; i
++) {
1352 filename
= ralloc_asprintf(NULL
, "%s/%u-%u.shader_test",
1353 capture_path
, shProg
->Name
, i
);
1355 filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1356 capture_path
, shProg
->Name
);
1358 file
= os_file_create_unique(filename
, 0644);
1361 /* If we are failing for another reason than "this filename already
1362 * exists", we are likely to fail again with another filename, so
1363 * let's just give up */
1364 if (errno
!= EEXIST
)
1366 ralloc_free(filename
);
1369 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1370 shProg
->IsES
? " ES" : "",
1371 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1372 if (shProg
->SeparateShader
)
1373 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1374 fprintf(file
, "\n");
1376 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1377 fprintf(file
, "[%s shader]\n%s\n",
1378 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1379 shProg
->Shaders
[i
]->Source
);
1383 _mesa_warning(ctx
, "Failed to open %s", filename
);
1386 ralloc_free(filename
);
1389 if (shProg
->data
->LinkStatus
== LINKING_FAILURE
&&
1390 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1391 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1392 shProg
->Name
, shProg
->data
->InfoLog
);
1395 _mesa_update_vertex_processing_mode(ctx
);
1397 shProg
->BinaryRetrievableHint
= shProg
->BinaryRetrievableHintPending
;
1403 printf("Link %u shaders in program %u: %s\n",
1404 shProg
->NumShaders
, shProg
->Name
,
1405 shProg
->data
->LinkStatus
? "Success" : "Failed");
1407 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1408 printf(" shader %u, stage %u\n",
1409 shProg
->Shaders
[i
]->Name
,
1410 shProg
->Shaders
[i
]->Stage
);
1417 link_program_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1419 link_program(ctx
, shProg
, false);
1424 link_program_no_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1426 link_program(ctx
, shProg
, true);
1431 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1433 link_program_error(ctx
, shProg
);
1438 * Print basic shader info (for debug).
1441 print_shader_info(const struct gl_shader_program
*shProg
)
1445 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1446 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1448 printf(" %s shader %u, checksum %u\n",
1449 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1450 shProg
->Shaders
[i
]->Name
,
1451 shProg
->Shaders
[i
]->SourceChecksum
);
1453 printf(" %s shader %u\n",
1454 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1455 shProg
->Shaders
[i
]->Name
);
1458 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1459 printf(" vert prog %u\n",
1460 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1461 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1462 printf(" frag prog %u\n",
1463 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1464 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1465 printf(" geom prog %u\n",
1466 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1467 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1468 printf(" tesc prog %u\n",
1469 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1470 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1471 printf(" tese prog %u\n",
1472 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1477 * Use the named shader program for subsequent glUniform calls
1480 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1483 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1484 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1485 "%s(program %u not linked)", caller
, shProg
->Name
);
1489 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1490 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1496 * Use the named shader program for subsequent rendering.
1499 _mesa_use_shader_program(struct gl_context
*ctx
,
1500 struct gl_shader_program
*shProg
)
1502 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1503 struct gl_program
*new_prog
= NULL
;
1504 if (shProg
&& shProg
->_LinkedShaders
[i
])
1505 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1506 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1508 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1513 * Do validation of the given shader program.
1514 * \param errMsg returns error message if validation fails.
1515 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1518 validate_shader_program(const struct gl_shader_program
*shProg
,
1521 if (!shProg
->data
->LinkStatus
) {
1525 /* From the GL spec, a program is invalid if any of these are true:
1527 any two active samplers in the current program object are of
1528 different types, but refer to the same texture image unit,
1530 any active sampler in the current program object refers to a texture
1531 image unit where fixed-function fragment processing accesses a
1532 texture target that does not match the sampler type, or
1534 the sum of the number of active samplers in the program and the
1535 number of texture image units enabled for fixed-function fragment
1536 processing exceeds the combined limit on the total number of texture
1537 image units allowed.
1541 * Check: any two active samplers in the current program object are of
1542 * different types, but refer to the same texture image unit,
1544 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1552 * Called via glValidateProgram()
1555 validate_program(struct gl_context
*ctx
, GLuint program
)
1557 struct gl_shader_program
*shProg
;
1558 char errMsg
[100] = "";
1560 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1565 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1566 if (!shProg
->data
->Validated
) {
1567 /* update info log */
1568 if (shProg
->data
->InfoLog
) {
1569 ralloc_free(shProg
->data
->InfoLog
);
1571 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1577 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1579 GET_CURRENT_CONTEXT(ctx
);
1580 attach_shader_no_error(ctx
, program
, shader
);
1585 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1587 GET_CURRENT_CONTEXT(ctx
);
1588 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1593 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1595 GET_CURRENT_CONTEXT(ctx
);
1596 attach_shader_no_error(ctx
, program
, shader
);
1601 _mesa_AttachShader(GLuint program
, GLuint shader
)
1603 GET_CURRENT_CONTEXT(ctx
);
1604 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1609 _mesa_CompileShader(GLuint shaderObj
)
1611 GET_CURRENT_CONTEXT(ctx
);
1612 if (MESA_VERBOSE
& VERBOSE_API
)
1613 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1614 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1615 "glCompileShader"));
1620 _mesa_CreateShader_no_error(GLenum type
)
1622 GET_CURRENT_CONTEXT(ctx
);
1623 return create_shader(ctx
, type
);
1628 _mesa_CreateShader(GLenum type
)
1630 GET_CURRENT_CONTEXT(ctx
);
1632 if (MESA_VERBOSE
& VERBOSE_API
)
1633 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1635 return create_shader_err(ctx
, type
, "glCreateShader");
1639 GLhandleARB GLAPIENTRY
1640 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1642 GET_CURRENT_CONTEXT(ctx
);
1643 return create_shader(ctx
, type
);
1647 GLhandleARB GLAPIENTRY
1648 _mesa_CreateShaderObjectARB(GLenum type
)
1650 GET_CURRENT_CONTEXT(ctx
);
1651 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1656 _mesa_CreateProgram(void)
1658 GET_CURRENT_CONTEXT(ctx
);
1659 if (MESA_VERBOSE
& VERBOSE_API
)
1660 _mesa_debug(ctx
, "glCreateProgram\n");
1661 return create_shader_program(ctx
);
1665 GLhandleARB GLAPIENTRY
1666 _mesa_CreateProgramObjectARB(void)
1668 GET_CURRENT_CONTEXT(ctx
);
1669 return create_shader_program(ctx
);
1674 _mesa_DeleteObjectARB(GLhandleARB obj
)
1676 if (MESA_VERBOSE
& VERBOSE_API
) {
1677 GET_CURRENT_CONTEXT(ctx
);
1678 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1682 GET_CURRENT_CONTEXT(ctx
);
1683 FLUSH_VERTICES(ctx
, 0);
1684 if (is_program(ctx
, obj
)) {
1685 delete_shader_program(ctx
, obj
);
1687 else if (is_shader(ctx
, obj
)) {
1688 delete_shader(ctx
, obj
);
1698 _mesa_DeleteProgram(GLuint name
)
1701 GET_CURRENT_CONTEXT(ctx
);
1702 FLUSH_VERTICES(ctx
, 0);
1703 delete_shader_program(ctx
, name
);
1709 _mesa_DeleteShader(GLuint name
)
1712 GET_CURRENT_CONTEXT(ctx
);
1713 FLUSH_VERTICES(ctx
, 0);
1714 delete_shader(ctx
, name
);
1720 _mesa_DetachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1722 GET_CURRENT_CONTEXT(ctx
);
1723 detach_shader_no_error(ctx
, program
, shader
);
1728 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1730 GET_CURRENT_CONTEXT(ctx
);
1731 detach_shader_error(ctx
, program
, shader
);
1736 _mesa_DetachShader_no_error(GLuint program
, GLuint shader
)
1738 GET_CURRENT_CONTEXT(ctx
);
1739 detach_shader_no_error(ctx
, program
, shader
);
1744 _mesa_DetachShader(GLuint program
, GLuint shader
)
1746 GET_CURRENT_CONTEXT(ctx
);
1747 detach_shader_error(ctx
, program
, shader
);
1752 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1753 GLsizei
* count
, GLhandleARB
* obj
)
1755 GET_CURRENT_CONTEXT(ctx
);
1756 get_attached_shaders(ctx
, (GLuint
)container
, maxCount
, count
, NULL
, obj
);
1761 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1762 GLsizei
*count
, GLuint
*obj
)
1764 GET_CURRENT_CONTEXT(ctx
);
1765 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
, NULL
);
1770 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1771 GLcharARB
* infoLog
)
1773 GET_CURRENT_CONTEXT(ctx
);
1774 if (is_program(ctx
, object
)) {
1775 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1777 else if (is_shader(ctx
, object
)) {
1778 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1781 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1787 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1789 GET_CURRENT_CONTEXT(ctx
);
1790 /* Implement in terms of GetProgramiv, GetShaderiv */
1791 if (is_program(ctx
, object
)) {
1792 if (pname
== GL_OBJECT_TYPE_ARB
) {
1793 *params
= GL_PROGRAM_OBJECT_ARB
;
1796 get_programiv(ctx
, object
, pname
, params
);
1799 else if (is_shader(ctx
, object
)) {
1800 if (pname
== GL_OBJECT_TYPE_ARB
) {
1801 *params
= GL_SHADER_OBJECT_ARB
;
1804 get_shaderiv(ctx
, object
, pname
, params
);
1808 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1814 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1817 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1818 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1819 params
[0] = (GLfloat
) iparams
[0];
1824 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1826 GET_CURRENT_CONTEXT(ctx
);
1827 get_programiv(ctx
, program
, pname
, params
);
1832 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1834 GET_CURRENT_CONTEXT(ctx
);
1835 get_shaderiv(ctx
, shader
, pname
, params
);
1840 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1841 GLsizei
*length
, GLchar
*infoLog
)
1843 GET_CURRENT_CONTEXT(ctx
);
1844 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1849 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1850 GLsizei
*length
, GLchar
*infoLog
)
1852 GET_CURRENT_CONTEXT(ctx
);
1853 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1858 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1859 GLsizei
*length
, GLchar
*sourceOut
)
1861 GET_CURRENT_CONTEXT(ctx
);
1862 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1866 GLhandleARB GLAPIENTRY
1867 _mesa_GetHandleARB(GLenum pname
)
1869 GET_CURRENT_CONTEXT(ctx
);
1870 return get_handle(ctx
, pname
);
1874 GLboolean GLAPIENTRY
1875 _mesa_IsProgram(GLuint name
)
1877 GET_CURRENT_CONTEXT(ctx
);
1878 return is_program(ctx
, name
);
1882 GLboolean GLAPIENTRY
1883 _mesa_IsShader(GLuint name
)
1885 GET_CURRENT_CONTEXT(ctx
);
1886 return is_shader(ctx
, name
);
1891 _mesa_LinkProgram_no_error(GLuint programObj
)
1893 GET_CURRENT_CONTEXT(ctx
);
1895 struct gl_shader_program
*shProg
=
1896 _mesa_lookup_shader_program(ctx
, programObj
);
1897 link_program_no_error(ctx
, shProg
);
1902 _mesa_LinkProgram(GLuint programObj
)
1904 GET_CURRENT_CONTEXT(ctx
);
1906 if (MESA_VERBOSE
& VERBOSE_API
)
1907 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1909 struct gl_shader_program
*shProg
=
1910 _mesa_lookup_shader_program_err(ctx
, programObj
, "glLinkProgram");
1911 link_program_error(ctx
, shProg
);
1914 #ifdef ENABLE_SHADER_CACHE
1916 * Generate a SHA-1 hash value string for given source string.
1919 generate_sha1(const char *source
, char sha_str
[64])
1921 unsigned char sha
[20];
1922 _mesa_sha1_compute(source
, strlen(source
), sha
);
1923 _mesa_sha1_format(sha_str
, sha
);
1927 * Construct a full path for shader replacement functionality using
1930 * <path>/<stage prefix>_<CHECKSUM>.glsl
1931 * <path>/<stage prefix>_<CHECKSUM>.arb
1934 construct_name(const gl_shader_stage stage
, const char *source
,
1938 static const char *types
[] = {
1939 "VS", "TC", "TE", "GS", "FS", "CS",
1942 const char *format
= strncmp(source
, "!!ARB", 5) ? "glsl" : "arb";
1944 generate_sha1(source
, sha
);
1945 return ralloc_asprintf(NULL
, "%s/%s_%s.%s", path
, types
[stage
], sha
, format
);
1949 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1952 _mesa_dump_shader_source(const gl_shader_stage stage
, const char *source
)
1954 static bool path_exists
= true;
1961 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1963 path_exists
= false;
1967 char *name
= construct_name(stage
, source
, dump_path
);
1969 f
= fopen(name
, "w");
1974 GET_CURRENT_CONTEXT(ctx
);
1975 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1982 * Read shader source code from a file.
1983 * Useful for debugging to override an app's shader.
1986 _mesa_read_shader_source(const gl_shader_stage stage
, const char *source
)
1989 static bool path_exists
= true;
1990 int len
, shader_size
= 0;
1997 read_path
= getenv("MESA_SHADER_READ_PATH");
1999 path_exists
= false;
2003 char *name
= construct_name(stage
, source
, read_path
);
2004 f
= fopen(name
, "r");
2009 /* allocate enough room for the entire shader */
2010 fseek(f
, 0, SEEK_END
);
2011 shader_size
= ftell(f
);
2013 assert(shader_size
);
2015 /* add one for terminating zero */
2018 buffer
= malloc(shader_size
);
2021 len
= fread(buffer
, 1, shader_size
, f
);
2029 #endif /* ENABLE_SHADER_CACHE */
2032 * Called via glShaderSource() and glShaderSourceARB() API functions.
2033 * Basically, concatenate the source code strings into one long string
2034 * and pass it to _mesa_shader_source().
2036 static ALWAYS_INLINE
void
2037 shader_source(struct gl_context
*ctx
, GLuint shaderObj
, GLsizei count
,
2038 const GLchar
*const *string
, const GLint
*length
, bool no_error
)
2041 GLsizei i
, totalLength
;
2043 struct gl_shader
*sh
;
2046 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
2050 if (string
== NULL
) {
2051 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
2055 sh
= _mesa_lookup_shader(ctx
, shaderObj
);
2059 * This array holds offsets of where the appropriate string ends, thus the
2060 * last element will be set to the total length of the source code.
2062 offsets
= malloc(count
* sizeof(GLint
));
2063 if (offsets
== NULL
) {
2064 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
2068 for (i
= 0; i
< count
; i
++) {
2069 if (!no_error
&& string
[i
] == NULL
) {
2070 free((GLvoid
*) offsets
);
2071 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2072 "glShaderSourceARB(null string)");
2075 if (length
== NULL
|| length
[i
] < 0)
2076 offsets
[i
] = strlen(string
[i
]);
2078 offsets
[i
] = length
[i
];
2079 /* accumulate string lengths */
2081 offsets
[i
] += offsets
[i
- 1];
2084 /* Total length of source string is sum off all strings plus two.
2085 * One extra byte for terminating zero, another extra byte to silence
2086 * valgrind warnings in the parser/grammer code.
2088 totalLength
= offsets
[count
- 1] + 2;
2089 source
= malloc(totalLength
* sizeof(GLcharARB
));
2090 if (source
== NULL
) {
2091 free((GLvoid
*) offsets
);
2092 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
2096 for (i
= 0; i
< count
; i
++) {
2097 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
2098 memcpy(source
+ start
, string
[i
],
2099 (offsets
[i
] - start
) * sizeof(GLcharARB
));
2101 source
[totalLength
- 1] = '\0';
2102 source
[totalLength
- 2] = '\0';
2104 #ifdef ENABLE_SHADER_CACHE
2105 GLcharARB
*replacement
;
2107 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
2108 * if corresponding entry found from MESA_SHADER_READ_PATH.
2110 _mesa_dump_shader_source(sh
->Stage
, source
);
2112 replacement
= _mesa_read_shader_source(sh
->Stage
, source
);
2115 source
= replacement
;
2117 #endif /* ENABLE_SHADER_CACHE */
2119 set_shader_source(sh
, source
);
2126 _mesa_ShaderSource_no_error(GLuint shaderObj
, GLsizei count
,
2127 const GLchar
*const *string
, const GLint
*length
)
2129 GET_CURRENT_CONTEXT(ctx
);
2130 shader_source(ctx
, shaderObj
, count
, string
, length
, true);
2135 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
2136 const GLchar
*const *string
, const GLint
*length
)
2138 GET_CURRENT_CONTEXT(ctx
);
2139 shader_source(ctx
, shaderObj
, count
, string
, length
, false);
2143 static ALWAYS_INLINE
void
2144 use_program(GLuint program
, bool no_error
)
2146 GET_CURRENT_CONTEXT(ctx
);
2147 struct gl_shader_program
*shProg
= NULL
;
2149 if (MESA_VERBOSE
& VERBOSE_API
)
2150 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
2154 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2157 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
2158 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2159 "glUseProgram(transform feedback active)");
2165 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
2169 if (!shProg
->data
->LinkStatus
) {
2170 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2171 "glUseProgram(program %u not linked)", program
);
2176 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
2177 print_shader_info(shProg
);
2182 /* The ARB_separate_shader_object spec says:
2184 * "The executable code for an individual shader stage is taken from
2185 * the current program for that stage. If there is a current program
2186 * object established by UseProgram, that program is considered current
2187 * for all stages. Otherwise, if there is a bound program pipeline
2188 * object (section 2.14.PPO), the program bound to the appropriate
2189 * stage of the pipeline object is considered current."
2192 /* Attach shader state to the binding point */
2193 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
2194 /* Update the program */
2195 _mesa_use_shader_program(ctx
, shProg
);
2197 /* Must be done first: detach the progam */
2198 _mesa_use_shader_program(ctx
, shProg
);
2199 /* Unattach shader_state binding point */
2200 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
2201 ctx
->Pipeline
.Default
);
2202 /* If a pipeline was bound, rebind it */
2203 if (ctx
->Pipeline
.Current
) {
2205 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
2207 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
2211 _mesa_update_vertex_processing_mode(ctx
);
2216 _mesa_UseProgram_no_error(GLuint program
)
2218 use_program(program
, true);
2223 _mesa_UseProgram(GLuint program
)
2225 use_program(program
, false);
2230 _mesa_ValidateProgram(GLuint program
)
2232 GET_CURRENT_CONTEXT(ctx
);
2233 validate_program(ctx
, program
);
2238 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2241 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
2242 GLint
* range
, GLint
* precision
)
2244 const struct gl_program_constants
*limits
;
2245 const struct gl_precision
*p
;
2246 GET_CURRENT_CONTEXT(ctx
);
2248 switch (shadertype
) {
2249 case GL_VERTEX_SHADER
:
2250 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
2252 case GL_FRAGMENT_SHADER
:
2253 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
2256 _mesa_error(ctx
, GL_INVALID_ENUM
,
2257 "glGetShaderPrecisionFormat(shadertype)");
2261 switch (precisiontype
) {
2263 p
= &limits
->LowFloat
;
2265 case GL_MEDIUM_FLOAT
:
2266 p
= &limits
->MediumFloat
;
2269 p
= &limits
->HighFloat
;
2272 p
= &limits
->LowInt
;
2275 p
= &limits
->MediumInt
;
2278 p
= &limits
->HighInt
;
2281 _mesa_error(ctx
, GL_INVALID_ENUM
,
2282 "glGetShaderPrecisionFormat(precisiontype)");
2286 range
[0] = p
->RangeMin
;
2287 range
[1] = p
->RangeMax
;
2288 precision
[0] = p
->Precision
;
2293 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2296 _mesa_ReleaseShaderCompiler(void)
2298 GET_CURRENT_CONTEXT(ctx
);
2300 if (ctx
->shader_builtin_ref
) {
2301 _mesa_glsl_builtin_functions_decref();
2302 ctx
->shader_builtin_ref
= false;
2308 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2311 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2312 const void* binary
, GLint length
)
2314 GET_CURRENT_CONTEXT(ctx
);
2315 struct gl_shader
**sh
;
2317 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2318 * page 88 of the OpenGL 4.5 specs state:
2320 * "An INVALID_VALUE error is generated if count or length is negative.
2321 * An INVALID_ENUM error is generated if binaryformat is not a supported
2322 * format returned in SHADER_BINARY_FORMATS."
2324 if (n
< 0 || length
< 0) {
2325 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2329 /* Get all shader objects at once so we can make the operation
2332 if (n
> SIZE_MAX
/ sizeof(*sh
)) {
2333 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary(count)");
2337 sh
= alloca(sizeof(*sh
) * (size_t)n
);
2339 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary");
2343 for (int i
= 0; i
< n
; ++i
) {
2344 sh
[i
] = _mesa_lookup_shader_err(ctx
, shaders
[i
], "glShaderBinary");
2349 if (binaryformat
== GL_SHADER_BINARY_FORMAT_SPIR_V_ARB
) {
2350 if (!ctx
->Extensions
.ARB_gl_spirv
) {
2351 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary(SPIR-V)");
2353 _mesa_spirv_shader_binary(ctx
, (unsigned) n
, sh
, binary
,
2360 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2365 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2366 GLenum
*binaryFormat
, GLvoid
*binary
)
2368 struct gl_shader_program
*shProg
;
2369 GLsizei length_dummy
;
2370 GET_CURRENT_CONTEXT(ctx
);
2373 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2377 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2381 /* The ARB_get_program_binary spec says:
2383 * "If <length> is NULL, then no length is returned."
2385 * Ensure that length always points to valid storage to avoid multiple NULL
2386 * pointer checks below.
2389 length
= &length_dummy
;
2392 /* The ARB_get_program_binary spec says:
2394 * "When a program object's LINK_STATUS is FALSE, its program binary
2395 * length is zero, and a call to GetProgramBinary will generate an
2396 * INVALID_OPERATION error.
2398 if (!shProg
->data
->LinkStatus
) {
2399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2400 "glGetProgramBinary(program %u not linked)",
2406 if (ctx
->Const
.NumProgramBinaryFormats
== 0) {
2408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2409 "glGetProgramBinary(driver supports zero binary formats)");
2411 _mesa_get_program_binary(ctx
, shProg
, bufSize
, length
, binaryFormat
,
2413 assert(*length
== 0 || *binaryFormat
== GL_PROGRAM_BINARY_FORMAT_MESA
);
2418 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2419 const GLvoid
*binary
, GLsizei length
)
2421 struct gl_shader_program
*shProg
;
2422 GET_CURRENT_CONTEXT(ctx
);
2424 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2428 _mesa_clear_shader_program_data(ctx
, shProg
);
2429 shProg
->data
= _mesa_create_shader_program_data();
2431 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2433 * "If a negative number is provided where an argument of type sizei or
2434 * sizeiptr is specified, an INVALID_VALUE error is generated."
2437 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2441 if (ctx
->Const
.NumProgramBinaryFormats
== 0 ||
2442 binaryFormat
!= GL_PROGRAM_BINARY_FORMAT_MESA
) {
2443 /* The ARB_get_program_binary spec says:
2445 * "<binaryFormat> and <binary> must be those returned by a previous
2446 * call to GetProgramBinary, and <length> must be the length of the
2447 * program binary as returned by GetProgramBinary or GetProgramiv with
2448 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2449 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2452 * Since any value of binaryFormat passed "is not one of those specified as
2453 * allowable for [this] command, an INVALID_ENUM error is generated."
2455 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2456 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2458 _mesa_program_binary(ctx
, shProg
, binaryFormat
, binary
, length
);
2463 static ALWAYS_INLINE
void
2464 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2465 GLuint pname
, GLint value
, bool no_error
)
2468 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2469 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2470 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2471 * even be in the dispatch table, so we shouldn't need to expclicitly
2474 * On desktop, we ignore the 3.0+ requirement because it is silly.
2477 /* The ARB_get_program_binary extension spec says:
2479 * "An INVALID_VALUE error is generated if the <value> argument to
2480 * ProgramParameteri is not TRUE or FALSE."
2482 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2486 /* No need to notify the driver. Any changes will actually take effect
2487 * the next time the shader is linked.
2489 * The ARB_get_program_binary extension spec says:
2491 * "To indicate that a program binary is likely to be retrieved,
2492 * ProgramParameteri should be called with <pname>
2493 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2494 * will not be in effect until the next time LinkProgram or
2495 * ProgramBinary has been called successfully."
2497 * The resolution of issue 9 in the extension spec also says:
2499 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2500 * to indicate to the GL implementation that this program will
2501 * likely be saved with GetProgramBinary at some point. This will
2502 * give the GL implementation the opportunity to track any state
2503 * changes made to the program before being saved such that when it
2504 * is loaded again a recompile can be avoided."
2506 shProg
->BinaryRetrievableHintPending
= value
;
2509 case GL_PROGRAM_SEPARABLE
:
2510 /* Spec imply that the behavior is the same as ARB_get_program_binary
2511 * Chapter 7.3 Program Objects
2513 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2516 shProg
->SeparateShader
= value
;
2521 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2522 _mesa_enum_to_string(pname
));
2528 _mesa_error(ctx
, GL_INVALID_VALUE
,
2529 "glProgramParameteri(pname=%s, value=%d): "
2530 "value must be 0 or 1.",
2531 _mesa_enum_to_string(pname
),
2537 _mesa_ProgramParameteri_no_error(GLuint program
, GLenum pname
, GLint value
)
2539 GET_CURRENT_CONTEXT(ctx
);
2541 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
2542 program_parameteri(ctx
, shProg
, pname
, value
, true);
2547 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2549 struct gl_shader_program
*shProg
;
2550 GET_CURRENT_CONTEXT(ctx
);
2552 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2553 "glProgramParameteri");
2557 program_parameteri(ctx
, shProg
, pname
, value
, false);
2562 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2563 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2564 struct gl_pipeline_object
*shTarget
)
2566 struct gl_program
**target
;
2568 target
= &shTarget
->CurrentProgram
[stage
];
2570 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2573 if (*target
!= prog
) {
2574 /* Program is current, flush it */
2575 if (shTarget
== ctx
->_Shader
) {
2576 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2579 _mesa_reference_shader_program(ctx
,
2580 &shTarget
->ReferencedPrograms
[stage
],
2582 _mesa_reference_program(ctx
, target
, prog
);
2583 _mesa_update_allow_draw_out_of_order(ctx
);
2584 if (stage
== MESA_SHADER_VERTEX
)
2585 _mesa_update_vertex_processing_mode(ctx
);
2593 * Copy program-specific data generated by linking from the gl_shader_program
2594 * object to the gl_program object referred to by the gl_linked_shader.
2596 * This function expects _mesa_reference_program() to have been previously
2597 * called setting the gl_linked_shaders program reference.
2600 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2601 struct gl_linked_shader
*dst_sh
)
2603 assert(dst_sh
->Program
);
2605 struct gl_program
*dst
= dst_sh
->Program
;
2607 dst
->info
.separate_shader
= src
->SeparateShader
;
2609 switch (dst_sh
->Stage
) {
2610 case MESA_SHADER_GEOMETRY
: {
2611 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2612 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2613 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2616 case MESA_SHADER_FRAGMENT
: {
2617 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2620 case MESA_SHADER_COMPUTE
: {
2621 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2630 * ARB_separate_shader_objects: Compile & Link Program
2633 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2634 const GLchar
* const *strings
)
2636 GET_CURRENT_CONTEXT(ctx
);
2638 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2642 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2643 * GL_INVALID_VALUE should be generated if count < 0
2646 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2651 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2653 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2654 _mesa_compile_shader(ctx
, sh
);
2656 program
= create_shader_program(ctx
);
2658 struct gl_shader_program
*shProg
;
2659 GLint compiled
= GL_FALSE
;
2661 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2663 shProg
->SeparateShader
= GL_TRUE
;
2665 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2667 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2668 _mesa_link_program(ctx
, shProg
);
2669 detach_shader_error(ctx
, program
, shader
);
2673 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2674 append
-error
-to
-info
-log
;
2675 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2680 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2683 delete_shader(ctx
, shader
);
2691 * For GL_ARB_tessellation_shader
2694 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2696 GET_CURRENT_CONTEXT(ctx
);
2697 FLUSH_VERTICES(ctx
, 0);
2698 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2702 extern void GLAPIENTRY
2703 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2705 GET_CURRENT_CONTEXT(ctx
);
2707 if (!_mesa_has_tessellation(ctx
)) {
2708 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2712 if (pname
!= GL_PATCH_VERTICES
) {
2713 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2717 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2718 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2722 FLUSH_VERTICES(ctx
, 0);
2723 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2727 extern void GLAPIENTRY
2728 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2730 GET_CURRENT_CONTEXT(ctx
);
2732 if (!_mesa_has_tessellation(ctx
)) {
2733 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2738 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2739 FLUSH_VERTICES(ctx
, 0);
2740 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2741 4 * sizeof(GLfloat
));
2742 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2744 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2745 FLUSH_VERTICES(ctx
, 0);
2746 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2747 2 * sizeof(GLfloat
));
2748 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2751 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2757 * ARB_shader_subroutine
2760 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2763 GET_CURRENT_CONTEXT(ctx
);
2764 const char *api_name
= "glGetSubroutineUniformLocation";
2765 struct gl_shader_program
*shProg
;
2766 GLenum resource_type
;
2767 gl_shader_stage stage
;
2769 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2770 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2774 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2778 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2779 if (!shProg
->_LinkedShaders
[stage
]) {
2780 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2784 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2785 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2789 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2792 GET_CURRENT_CONTEXT(ctx
);
2793 const char *api_name
= "glGetSubroutineIndex";
2794 struct gl_shader_program
*shProg
;
2795 struct gl_program_resource
*res
;
2796 GLenum resource_type
;
2797 gl_shader_stage stage
;
2799 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2800 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2804 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2808 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2809 if (!shProg
->_LinkedShaders
[stage
]) {
2810 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2814 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2815 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2820 return _mesa_program_resource_index(shProg
, res
);
2825 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2826 GLuint index
, GLenum pname
, GLint
*values
)
2828 GET_CURRENT_CONTEXT(ctx
);
2829 const char *api_name
= "glGetActiveSubroutineUniformiv";
2830 struct gl_shader_program
*shProg
;
2831 struct gl_linked_shader
*sh
;
2832 gl_shader_stage stage
;
2833 struct gl_program_resource
*res
;
2834 const struct gl_uniform_storage
*uni
;
2835 GLenum resource_type
;
2838 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2839 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2843 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2847 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2848 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2850 sh
= shProg
->_LinkedShaders
[stage
];
2852 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2856 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2857 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2858 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2863 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2864 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2867 values
[0] = uni
->num_compatible_subroutines
;
2871 case GL_COMPATIBLE_SUBROUTINES
: {
2872 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2876 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2877 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2878 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2879 if (fn
->types
[j
] == uni
->type
) {
2880 values
[count
++] = i
;
2888 case GL_UNIFORM_SIZE
:
2889 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2892 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2895 case GL_UNIFORM_NAME_LENGTH
:
2896 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2898 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2899 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2903 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2910 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2911 GLuint index
, GLsizei bufsize
,
2912 GLsizei
*length
, GLchar
*name
)
2914 GET_CURRENT_CONTEXT(ctx
);
2915 const char *api_name
= "glGetActiveSubroutineUniformName";
2916 struct gl_shader_program
*shProg
;
2917 GLenum resource_type
;
2918 gl_shader_stage stage
;
2920 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2921 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2925 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2929 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2930 if (!shProg
->_LinkedShaders
[stage
]) {
2931 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2935 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2936 /* get program resource name */
2937 _mesa_get_program_resource_name(shProg
, resource_type
,
2939 length
, name
, api_name
);
2944 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2945 GLuint index
, GLsizei bufsize
,
2946 GLsizei
*length
, GLchar
*name
)
2948 GET_CURRENT_CONTEXT(ctx
);
2949 const char *api_name
= "glGetActiveSubroutineName";
2950 struct gl_shader_program
*shProg
;
2951 GLenum resource_type
;
2952 gl_shader_stage stage
;
2954 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2955 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2959 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2963 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2964 if (!shProg
->_LinkedShaders
[stage
]) {
2965 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2968 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2969 _mesa_get_program_resource_name(shProg
, resource_type
,
2971 length
, name
, api_name
);
2975 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2976 const GLuint
*indices
)
2978 GET_CURRENT_CONTEXT(ctx
);
2979 const char *api_name
= "glUniformSubroutinesuiv";
2980 gl_shader_stage stage
;
2983 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2984 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2988 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2989 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2991 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2995 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2996 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
3001 bool flushed
= false;
3003 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3010 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
3014 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
3017 for (j
= i
; j
< i
+ uni_count
; j
++) {
3018 struct gl_subroutine_function
*subfn
= NULL
;
3019 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
3020 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
3024 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
3025 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
3026 subfn
= &p
->sh
.SubroutineFunctions
[f
];
3033 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
3034 if (subfn
->types
[k
] == uni
->type
)
3037 if (k
== subfn
->num_compat_types
) {
3038 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3042 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
3050 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
3053 GET_CURRENT_CONTEXT(ctx
);
3054 const char *api_name
= "glGetUniformSubroutineuiv";
3055 gl_shader_stage stage
;
3057 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
3058 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3062 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
3063 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
3065 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3069 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
3070 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
3074 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
3079 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
3080 GLenum pname
, GLint
*values
)
3082 GET_CURRENT_CONTEXT(ctx
);
3083 const char *api_name
= "glGetProgramStageiv";
3084 struct gl_shader_program
*shProg
;
3085 struct gl_linked_shader
*sh
;
3086 gl_shader_stage stage
;
3088 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
3089 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3093 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
3097 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
3098 sh
= shProg
->_LinkedShaders
[stage
];
3100 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
3101 * INVALID_OPERATION in the case of not be linked.
3103 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
3104 * same info using other specs (ARB_program_interface_query), without the
3105 * need of the program to be linked, being the value for that case 0.
3107 * But at the same time, some other methods require the program to be
3108 * linked for pname related to locations, so it would be inconsistent to
3109 * not do the same here. So we are:
3110 * * Return GL_INVALID_OPERATION if not linked only for locations.
3111 * * Setting a default value of 0, to be returned if not linked.
3115 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
3116 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3121 struct gl_program
*p
= sh
->Program
;
3123 case GL_ACTIVE_SUBROUTINES
:
3124 values
[0] = p
->sh
.NumSubroutineFunctions
;
3126 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
3127 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
3129 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
3130 values
[0] = p
->sh
.NumSubroutineUniforms
;
3132 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
3136 GLenum resource_type
;
3137 struct gl_program_resource
*res
;
3139 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
3140 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3141 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3143 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
3148 values
[0] = max_len
;
3151 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
3155 GLenum resource_type
;
3156 struct gl_program_resource
*res
;
3158 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
3159 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3160 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3162 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
3163 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
3169 values
[0] = max_len
;
3173 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
3179 /* This is simple list entry that will be used to hold a list of string
3180 * tokens of a parsed shader include path.
3182 struct sh_incl_path_entry
3184 struct sh_incl_path_entry
*next
;
3185 struct sh_incl_path_entry
*prev
;
3190 /* Nodes of the shader include tree */
3191 struct sh_incl_path_ht_entry
3193 struct hash_table
*path
;
3194 char *shader_source
;
3197 struct shader_includes
{
3198 /* Array to hold include paths given to glCompileShaderIncludeARB() */
3199 struct sh_incl_path_entry
**include_paths
;
3200 size_t num_include_paths
;
3201 size_t relative_path_cursor
;
3203 /* Root hash table holding the shader include tree */
3204 struct hash_table
*shader_include_tree
;
3208 _mesa_init_shader_includes(struct gl_shared_state
*shared
)
3210 shared
->ShaderIncludes
= calloc(1, sizeof(struct shader_includes
));
3211 shared
->ShaderIncludes
->shader_include_tree
=
3212 _mesa_hash_table_create(NULL
, _mesa_hash_string
,
3213 _mesa_key_string_equal
);
3217 _mesa_get_shader_include_cursor(struct gl_shared_state
*shared
)
3219 return shared
->ShaderIncludes
->relative_path_cursor
;
3223 _mesa_set_shader_include_cursor(struct gl_shared_state
*shared
, size_t cursor
)
3225 shared
->ShaderIncludes
->relative_path_cursor
= cursor
;
3229 destroy_shader_include(struct hash_entry
*entry
)
3231 struct sh_incl_path_ht_entry
*sh_incl_ht_entry
=
3232 (struct sh_incl_path_ht_entry
*) entry
->data
;
3234 _mesa_hash_table_destroy(sh_incl_ht_entry
->path
, destroy_shader_include
);
3235 free(sh_incl_ht_entry
->shader_source
);
3236 free(sh_incl_ht_entry
);
3240 _mesa_destroy_shader_includes(struct gl_shared_state
*shared
)
3242 _mesa_hash_table_destroy(shared
->ShaderIncludes
->shader_include_tree
,
3243 destroy_shader_include
);
3244 free(shared
->ShaderIncludes
);
3248 valid_path_format(const char *str
, bool relative_path
)
3252 if (!str
[i
] || (!relative_path
&& str
[i
] != '/'))
3258 const char c
= str
[i
++];
3259 if (('A' <= c
&& c
<= 'Z') ||
3260 ('a' <= c
&& c
<= 'z') ||
3261 ('0' <= c
&& c
<= '9'))
3265 if (str
[i
- 2] == '/')
3271 if (strchr("^. _+*%[](){}|&~=!:;,?-", c
) == NULL
)
3275 if (str
[i
- 1] == '/')
3283 validate_and_tokenise_sh_incl(struct gl_context
*ctx
,
3285 struct sh_incl_path_entry
**path_list
,
3286 char *full_path
, bool error_check
)
3288 bool relative_path
= ctx
->Shared
->ShaderIncludes
->num_include_paths
;
3290 if (!valid_path_format(full_path
, relative_path
)) {
3292 _mesa_error(ctx
, GL_INVALID_VALUE
,
3293 "glNamedStringARB(invalid name %s)", full_path
);
3298 char *save_ptr
= NULL
;
3299 char *path_str
= strtok_r(full_path
, "/", &save_ptr
);
3301 *path_list
= rzalloc(mem_ctx
, struct sh_incl_path_entry
);
3303 make_empty_list(*path_list
);
3305 while (path_str
!= NULL
) {
3306 if (strlen(path_str
) == 0) {
3308 _mesa_error(ctx
, GL_INVALID_VALUE
,
3309 "glNamedStringARB(invalid name %s)", full_path
);
3315 if (strcmp(path_str
, ".") == 0) {
3317 } else if (strcmp(path_str
, "..") == 0) {
3318 struct sh_incl_path_entry
*last
= last_elem(*path_list
);
3319 remove_from_list(last
);
3321 struct sh_incl_path_entry
*path
=
3322 rzalloc(mem_ctx
, struct sh_incl_path_entry
);
3324 path
->path
= strdup(path_str
);
3325 insert_at_tail(*path_list
, path
);
3328 path_str
= strtok_r(NULL
, "/", &save_ptr
);
3334 static struct sh_incl_path_ht_entry
*
3335 lookup_shader_include(struct gl_context
*ctx
, char *path
,
3338 void *mem_ctx
= ralloc_context(NULL
);
3339 struct sh_incl_path_entry
*path_list
;
3341 if (!validate_and_tokenise_sh_incl(ctx
, mem_ctx
, &path_list
, path
,
3343 ralloc_free(mem_ctx
);
3347 struct sh_incl_path_ht_entry
*sh_incl_ht_entry
= NULL
;
3348 struct hash_table
*path_ht
=
3349 ctx
->Shared
->ShaderIncludes
->shader_include_tree
;
3351 size_t count
= ctx
->Shared
->ShaderIncludes
->num_include_paths
;
3352 bool relative_path
= path
[0] != '/';
3354 size_t i
= ctx
->Shared
->ShaderIncludes
->relative_path_cursor
;
3355 bool use_cursor
= ctx
->Shared
->ShaderIncludes
->relative_path_cursor
;
3358 struct sh_incl_path_entry
*entry
;
3360 if (relative_path
) {
3363 struct sh_incl_path_entry
*rel_path_list
=
3364 ctx
->Shared
->ShaderIncludes
->include_paths
[i
];
3365 foreach(entry
, rel_path_list
) {
3366 struct hash_entry
*ht_entry
=
3367 _mesa_hash_table_search(path_ht
, entry
->path
);
3370 /* Reset search path and skip to the next include path */
3371 path_ht
= ctx
->Shared
->ShaderIncludes
->shader_include_tree
;
3372 sh_incl_ht_entry
= NULL
;
3377 goto next_relative_path
;
3381 goto next_relative_path
;
3386 (struct sh_incl_path_ht_entry
*) ht_entry
->data
;
3389 path_ht
= sh_incl_ht_entry
->path
;
3394 foreach(entry
, path_list
) {
3395 struct hash_entry
*ht_entry
=
3396 _mesa_hash_table_search(path_ht
, entry
->path
);
3399 /* Reset search path and skip to the next include path */
3400 path_ht
= ctx
->Shared
->ShaderIncludes
->shader_include_tree
;
3401 sh_incl_ht_entry
= NULL
;
3413 (struct sh_incl_path_ht_entry
*) ht_entry
->data
;
3416 path_ht
= sh_incl_ht_entry
->path
;
3420 (sh_incl_ht_entry
== NULL
|| !sh_incl_ht_entry
->shader_source
))
3423 /* If we get here then we have found a matching path or exahusted our
3424 * relative search paths.
3426 ctx
->Shared
->ShaderIncludes
->relative_path_cursor
= i
;
3428 } while (i
< count
);
3430 ralloc_free(mem_ctx
);
3432 return sh_incl_ht_entry
;
3436 _mesa_lookup_shader_include(struct gl_context
*ctx
, char *path
,
3439 struct sh_incl_path_ht_entry
*shader_include
=
3440 lookup_shader_include(ctx
, path
, error_check
);
3442 return shader_include
? shader_include
->shader_source
: NULL
;
3446 copy_string(struct gl_context
*ctx
, const char *str
, int str_len
,
3450 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(NULL string)", caller
);
3458 cp
= calloc(sizeof(char), str_len
+ 1);
3459 memcpy(cp
, str
, str_len
);
3466 _mesa_NamedStringARB(GLenum type
, GLint namelen
, const GLchar
*name
,
3467 GLint stringlen
, const GLchar
*string
)
3469 GET_CURRENT_CONTEXT(ctx
);
3470 const char *caller
= "glNamedStringARB";
3472 if (type
!= GL_SHADER_INCLUDE_ARB
) {
3473 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid type)", caller
);
3477 char *name_cp
= copy_string(ctx
, name
, namelen
, caller
);
3478 char *string_cp
= copy_string(ctx
, string
, stringlen
, caller
);
3479 if (!name_cp
|| !string_cp
) {
3485 void *mem_ctx
= ralloc_context(NULL
);
3486 struct sh_incl_path_entry
*path_list
;
3488 if (!validate_and_tokenise_sh_incl(ctx
, mem_ctx
, &path_list
, name_cp
,
3492 ralloc_free(mem_ctx
);
3496 mtx_lock(&ctx
->Shared
->ShaderIncludeMutex
);
3498 struct hash_table
*path_ht
=
3499 ctx
->Shared
->ShaderIncludes
->shader_include_tree
;
3501 struct sh_incl_path_entry
*entry
;
3502 foreach(entry
, path_list
) {
3503 struct hash_entry
*ht_entry
=
3504 _mesa_hash_table_search(path_ht
, entry
->path
);
3506 struct sh_incl_path_ht_entry
*sh_incl_ht_entry
;
3508 sh_incl_ht_entry
= calloc(1, sizeof(struct sh_incl_path_ht_entry
));
3509 sh_incl_ht_entry
->path
=
3510 _mesa_hash_table_create(NULL
, _mesa_hash_string
,
3511 _mesa_key_string_equal
);
3512 _mesa_hash_table_insert(path_ht
, entry
->path
, sh_incl_ht_entry
);
3514 sh_incl_ht_entry
= (struct sh_incl_path_ht_entry
*) ht_entry
->data
;
3517 path_ht
= sh_incl_ht_entry
->path
;
3519 if (last_elem(path_list
) == entry
) {
3520 free(sh_incl_ht_entry
->shader_source
);
3521 sh_incl_ht_entry
->shader_source
= string_cp
;
3525 mtx_unlock(&ctx
->Shared
->ShaderIncludeMutex
);
3528 ralloc_free(mem_ctx
);
3532 _mesa_DeleteNamedStringARB(GLint namelen
, const GLchar
*name
)
3534 GET_CURRENT_CONTEXT(ctx
);
3535 const char *caller
= "glDeleteNamedStringARB";
3537 char *name_cp
= copy_string(ctx
, name
, namelen
, caller
);
3541 struct sh_incl_path_ht_entry
*shader_include
=
3542 lookup_shader_include(ctx
, name_cp
, true);
3544 if (!shader_include
) {
3545 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3546 "%s(no string associated with path %s)", caller
, name_cp
);
3551 mtx_lock(&ctx
->Shared
->ShaderIncludeMutex
);
3553 free(shader_include
->shader_source
);
3554 shader_include
->shader_source
= NULL
;
3556 mtx_unlock(&ctx
->Shared
->ShaderIncludeMutex
);
3562 _mesa_CompileShaderIncludeARB(GLuint shader
, GLsizei count
,
3563 const GLchar
* const *path
, const GLint
*length
)
3565 GET_CURRENT_CONTEXT(ctx
);
3566 const char *caller
= "glCompileShaderIncludeARB";
3568 if (count
> 0 && path
== NULL
) {
3569 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count > 0 && path == NULL)",
3574 void *mem_ctx
= ralloc_context(NULL
);
3576 mtx_lock(&ctx
->Shared
->ShaderIncludeMutex
);
3578 ctx
->Shared
->ShaderIncludes
->include_paths
=
3579 ralloc_array_size(mem_ctx
, sizeof(struct sh_incl_path_entry
*), count
);
3581 for (size_t i
= 0; i
< count
; i
++) {
3582 char *path_cp
= copy_string(ctx
, path
[i
], length
? length
[i
] : -1,
3588 struct sh_incl_path_entry
*path_list
;
3590 if (!validate_and_tokenise_sh_incl(ctx
, mem_ctx
, &path_list
, path_cp
,
3596 ctx
->Shared
->ShaderIncludes
->include_paths
[i
] = path_list
;
3601 /* We must set this *after* all calls to validate_and_tokenise_sh_incl()
3602 * are done as we use this to decide if we need to check the start of the
3605 ctx
->Shared
->ShaderIncludes
->num_include_paths
= count
;
3607 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
3609 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(shader)", caller
);
3613 _mesa_compile_shader(ctx
, sh
);
3616 ctx
->Shared
->ShaderIncludes
->num_include_paths
= 0;
3617 ctx
->Shared
->ShaderIncludes
->relative_path_cursor
= 0;
3618 ctx
->Shared
->ShaderIncludes
->include_paths
= NULL
;
3620 mtx_unlock(&ctx
->Shared
->ShaderIncludeMutex
);
3622 ralloc_free(mem_ctx
);
3625 GLboolean GLAPIENTRY
3626 _mesa_IsNamedStringARB(GLint namelen
, const GLchar
*name
)
3628 GET_CURRENT_CONTEXT(ctx
);
3633 char *name_cp
= copy_string(ctx
, name
, namelen
, "");
3635 const char *source
= _mesa_lookup_shader_include(ctx
, name_cp
, false);
3645 _mesa_GetNamedStringARB(GLint namelen
, const GLchar
*name
, GLsizei bufSize
,
3646 GLint
*stringlen
, GLchar
*string
)
3648 GET_CURRENT_CONTEXT(ctx
);
3649 const char *caller
= "glGetNamedStringARB";
3651 char *name_cp
= copy_string(ctx
, name
, namelen
, caller
);
3655 const char *source
= _mesa_lookup_shader_include(ctx
, name_cp
, true);
3657 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3658 "%s(no string associated with path %s)", caller
, name_cp
);
3663 size_t size
= MIN2(strlen(source
), bufSize
- 1);
3664 memcpy(string
, source
, size
);
3665 string
[size
] = '\0';
3673 _mesa_GetNamedStringivARB(GLint namelen
, const GLchar
*name
,
3674 GLenum pname
, GLint
*params
)
3676 GET_CURRENT_CONTEXT(ctx
);
3677 const char *caller
= "glGetNamedStringivARB";
3679 char *name_cp
= copy_string(ctx
, name
, namelen
, caller
);
3683 const char *source
= _mesa_lookup_shader_include(ctx
, name_cp
, true);
3685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3686 "%s(no string associated with path %s)", caller
, name_cp
);
3692 case GL_NAMED_STRING_LENGTH_ARB
:
3693 *params
= strlen(source
) + 1;
3695 case GL_NAMED_STRING_TYPE_ARB
:
3696 *params
= GL_SHADER_INCLUDE_ARB
;
3699 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname)", caller
);
3707 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
3711 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3712 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
3713 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
3714 if (fn
->types
[j
] == type
)
3722 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
3723 struct gl_program
*p
)
3727 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
3732 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3741 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
3742 for (j
= 0; j
< uni_count
; j
++) {
3743 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
3744 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
3747 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
3749 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
3753 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
3754 gl_shader_stage stage
)
3756 if (ctx
->_Shader
->CurrentProgram
[stage
])
3757 _mesa_shader_write_subroutine_index(ctx
,
3758 ctx
->_Shader
->CurrentProgram
[stage
]);
3762 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
3763 struct gl_program
*p
)
3767 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
3768 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
3769 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
3770 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
3771 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
3774 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3775 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3780 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);