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
35 * 1. Check that the right error code is generated for all _mesa_error() calls.
36 * 2. Insert FLUSH_VERTICES calls in various places
41 #include <c99_alloca.h>
42 #include "main/glheader.h"
43 #include "main/context.h"
44 #include "main/enums.h"
45 #include "main/glspirv.h"
46 #include "main/hash.h"
47 #include "main/mtypes.h"
48 #include "main/pipelineobj.h"
49 #include "main/program_binary.h"
50 #include "main/shaderapi.h"
51 #include "main/shaderobj.h"
52 #include "main/state.h"
53 #include "main/transformfeedback.h"
54 #include "main/uniforms.h"
55 #include "compiler/glsl/glsl_parser_extras.h"
56 #include "compiler/glsl/ir.h"
57 #include "compiler/glsl/ir_uniform.h"
58 #include "compiler/glsl/program.h"
59 #include "program/program.h"
60 #include "program/prog_print.h"
61 #include "program/prog_parameter.h"
62 #include "util/ralloc.h"
63 #include "util/hash_table.h"
64 #include "util/mesa-sha1.h"
65 #include "util/crc32.h"
66 #include "util/os_file.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
],
167 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
169 /* Extended for ARB_separate_shader_objects */
170 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
172 assert(ctx
->Shader
.RefCount
== 1);
177 * Copy string from <src> to <dst>, up to maxLength characters, returning
178 * length of <dst> in <length>.
179 * \param src the strings source
180 * \param maxLength max chars to copy
181 * \param length returns number of chars copied
182 * \param dst the string destination
185 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
186 GLsizei
*length
, const GLchar
*src
)
189 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
200 * Confirm that the a shader type is valid and supported by the implementation
202 * \param ctx Current GL context
203 * \param type Shader target
207 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
209 /* Note: when building built-in GLSL functions, this function may be
210 * invoked with ctx == NULL. In that case, we can only validate that it's
211 * a shader target we recognize, not that it's supported in the current
212 * context. But that's fine--we don't need any further validation than
213 * that when building built-in GLSL functions.
217 case GL_FRAGMENT_SHADER
:
218 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
219 case GL_VERTEX_SHADER
:
220 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
221 case GL_GEOMETRY_SHADER_ARB
:
222 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
223 case GL_TESS_CONTROL_SHADER
:
224 case GL_TESS_EVALUATION_SHADER
:
225 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
226 case GL_COMPUTE_SHADER
:
227 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
235 is_program(struct gl_context
*ctx
, GLuint name
)
237 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
238 return shProg
? GL_TRUE
: GL_FALSE
;
243 is_shader(struct gl_context
*ctx
, GLuint name
)
245 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
246 return shader
? GL_TRUE
: GL_FALSE
;
251 * Attach shader to a shader program.
254 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
255 struct gl_shader
*sh
)
257 GLuint n
= shProg
->NumShaders
;
259 shProg
->Shaders
= realloc(shProg
->Shaders
,
260 (n
+ 1) * sizeof(struct gl_shader
*));
261 if (!shProg
->Shaders
) {
262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
267 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
268 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
269 shProg
->NumShaders
++;
273 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
276 struct gl_shader_program
*shProg
;
277 struct gl_shader
*sh
;
280 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
282 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
286 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
291 n
= shProg
->NumShaders
;
292 for (i
= 0; i
< n
; i
++) {
293 if (shProg
->Shaders
[i
] == sh
) {
294 /* The shader is already attched to this program. The
295 * GL_ARB_shader_objects spec says:
297 * "The error INVALID_OPERATION is generated by AttachObjectARB
298 * if <obj> is already attached to <containerObj>."
300 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
302 } else if (same_type_disallowed
&&
303 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
304 /* Shader with the same type is already attached to this program,
305 * OpenGL ES 2.0 and 3.0 specs say:
307 * "Multiple shader objects of the same type may not be attached
308 * to a single program object. [...] The error INVALID_OPERATION
309 * is generated if [...] another shader object of the same type
310 * as shader is already attached to program."
312 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
317 attach_shader(ctx
, shProg
, sh
);
321 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
323 struct gl_shader_program
*shProg
;
324 struct gl_shader
*sh
;
326 shProg
= _mesa_lookup_shader_program(ctx
, program
);
327 sh
= _mesa_lookup_shader(ctx
, shader
);
329 attach_shader(ctx
, shProg
, sh
);
333 create_shader(struct gl_context
*ctx
, GLenum type
)
335 struct gl_shader
*sh
;
338 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
339 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
340 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
342 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
343 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
350 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
352 if (!_mesa_validate_shader_target(ctx
, type
)) {
353 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
354 caller
, _mesa_enum_to_string(type
));
358 return create_shader(ctx
, type
);
363 create_shader_program(struct gl_context
*ctx
)
366 struct gl_shader_program
*shProg
;
368 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
370 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
372 shProg
= _mesa_new_shader_program(name
);
374 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
376 assert(shProg
->RefCount
== 1);
378 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
385 * Delete a shader program. Actually, just decrement the program's
386 * reference count and mark it as DeletePending.
387 * Used to implement glDeleteProgram() and glDeleteObjectARB().
390 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
393 * NOTE: deleting shaders/programs works a bit differently than
394 * texture objects (and buffer objects, etc). Shader/program
395 * handles/IDs exist in the hash table until the object is really
396 * deleted (refcount==0). With texture objects, the handle/ID is
397 * removed from the hash table in glDeleteTextures() while the tex
398 * object itself might linger until its refcount goes to zero.
400 struct gl_shader_program
*shProg
;
402 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
406 if (!shProg
->DeletePending
) {
407 shProg
->DeletePending
= GL_TRUE
;
409 /* effectively, decr shProg's refcount */
410 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
416 delete_shader(struct gl_context
*ctx
, GLuint shader
)
418 struct gl_shader
*sh
;
420 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
424 if (!sh
->DeletePending
) {
425 sh
->DeletePending
= GL_TRUE
;
427 /* effectively, decr sh's refcount */
428 _mesa_reference_shader(ctx
, &sh
, NULL
);
433 static ALWAYS_INLINE
void
434 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
437 struct gl_shader_program
*shProg
;
442 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
446 shProg
= _mesa_lookup_shader_program(ctx
, program
);
449 n
= shProg
->NumShaders
;
451 for (i
= 0; i
< n
; i
++) {
452 if (shProg
->Shaders
[i
]->Name
== shader
) {
454 struct gl_shader
**newList
;
457 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
459 /* alloc new, smaller array */
460 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
462 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
465 /* Copy old list entries to new list, skipping removed entry at [i] */
466 for (j
= 0; j
< i
; j
++) {
467 newList
[j
] = shProg
->Shaders
[j
];
470 newList
[j
++] = shProg
->Shaders
[i
];
473 /* Free old list and install new one */
474 free(shProg
->Shaders
);
475 shProg
->Shaders
= newList
;
476 shProg
->NumShaders
= n
- 1;
479 /* sanity check - make sure the new list's entries are sensible */
480 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
481 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
482 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
483 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
484 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
485 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
486 assert(shProg
->Shaders
[j
]->RefCount
> 0);
497 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
498 err
= GL_INVALID_OPERATION
;
500 err
= GL_INVALID_VALUE
;
501 _mesa_error(ctx
, err
, "glDetachShader(shader)");
508 detach_shader_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
510 detach_shader(ctx
, program
, shader
, false);
515 detach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
517 detach_shader(ctx
, program
, shader
, true);
522 * Return list of shaders attached to shader program.
523 * \param objOut returns GLuint ids
524 * \param handleOut returns GLhandleARB handles
527 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
528 GLsizei
*countOut
, GLuint
*objOut
, GLhandleARB
*handleOut
)
530 struct gl_shader_program
*shProg
;
533 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
538 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
542 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
544 objOut
[i
] = shProg
->Shaders
[i
]->Name
;
548 handleOut
[i
] = (GLhandleARB
) shProg
->Shaders
[i
]->Name
;
558 * glGetHandleARB() - return ID/name of currently bound shader program.
561 get_handle(struct gl_context
*ctx
, GLenum pname
)
563 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
564 if (ctx
->_Shader
->ActiveProgram
)
565 return ctx
->_Shader
->ActiveProgram
->Name
;
570 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
577 * Check if a geometry shader query is valid at this time. If not, report an
578 * error and return false.
580 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
582 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
583 * are queried for a program which has not been linked successfully, or
584 * which does not contain objects to form a geometry shader, then an
585 * INVALID_OPERATION error is generated."
588 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
590 if (shProg
->data
->LinkStatus
&&
591 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
595 _mesa_error(ctx
, GL_INVALID_OPERATION
,
596 "glGetProgramv(linked geometry shader required)");
602 * Check if a tessellation control shader query is valid at this time.
603 * If not, report an error and return false.
605 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
607 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
608 * not been linked successfully, or which does not contain objects to
609 * form a tessellation control shader, then an INVALID_OPERATION error is
613 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
615 if (shProg
->data
->LinkStatus
&&
616 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
620 _mesa_error(ctx
, GL_INVALID_OPERATION
,
621 "glGetProgramv(linked tessellation control shader required)");
627 * Check if a tessellation evaluation shader query is valid at this time.
628 * If not, report an error and return false.
630 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
632 * "If any of the pname values in this paragraph are queried for a program
633 * which has not been linked successfully, or which does not contain
634 * objects to form a tessellation evaluation shader, then an
635 * INVALID_OPERATION error is generated."
639 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
641 if (shProg
->data
->LinkStatus
&&
642 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
646 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
647 "evaluation shader required)");
653 * glGetProgramiv() - get shader program state.
654 * Note that this is for GLSL shader programs, not ARB vertex/fragment
655 * programs (see glGetProgramivARB).
658 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
661 struct gl_shader_program
*shProg
662 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
664 /* Is transform feedback available in this context?
667 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
668 || ctx
->API
== API_OPENGL_CORE
669 || _mesa_is_gles3(ctx
);
671 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
672 * and GL 3.2) are available in this context
674 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
675 const bool has_tess
= _mesa_has_tessellation(ctx
);
677 /* Are uniform buffer objects available in this context?
680 (ctx
->API
== API_OPENGL_COMPAT
&&
681 ctx
->Extensions
.ARB_uniform_buffer_object
)
682 || ctx
->API
== API_OPENGL_CORE
683 || _mesa_is_gles3(ctx
);
690 case GL_DELETE_STATUS
:
691 *params
= shProg
->DeletePending
;
693 case GL_COMPLETION_STATUS_ARB
:
694 if (ctx
->Driver
.GetShaderProgramCompletionStatus
)
695 *params
= ctx
->Driver
.GetShaderProgramCompletionStatus(ctx
, shProg
);
700 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
702 case GL_VALIDATE_STATUS
:
703 *params
= shProg
->data
->Validated
;
705 case GL_INFO_LOG_LENGTH
:
706 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
707 strlen(shProg
->data
->InfoLog
) + 1 : 0;
709 case GL_ATTACHED_SHADERS
:
710 *params
= shProg
->NumShaders
;
712 case GL_ACTIVE_ATTRIBUTES
:
713 *params
= _mesa_count_active_attribs(shProg
);
715 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
716 *params
= _mesa_longest_attribute_name_length(shProg
);
718 case GL_ACTIVE_UNIFORMS
: {
720 const unsigned num_uniforms
=
721 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
722 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
723 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
728 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
731 const unsigned num_uniforms
=
732 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
734 for (i
= 0; i
< num_uniforms
; i
++) {
735 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
738 /* Add one for the terminating NUL character for a non-array, and
739 * 4 for the "[0]" and the NUL for an array.
741 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
742 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
751 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
754 *params
= shProg
->TransformFeedback
.NumVarying
;
756 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
762 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
763 /* Add one for the terminating NUL character.
766 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
775 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
778 *params
= shProg
->TransformFeedback
.BufferMode
;
780 case GL_GEOMETRY_VERTICES_OUT
:
783 if (check_gs_query(ctx
, shProg
)) {
784 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
785 Program
->info
.gs
.vertices_out
;
788 case GL_GEOMETRY_SHADER_INVOCATIONS
:
789 if (!has_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
791 if (check_gs_query(ctx
, shProg
)) {
792 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
793 Program
->info
.gs
.invocations
;
796 case GL_GEOMETRY_INPUT_TYPE
:
799 if (check_gs_query(ctx
, shProg
)) {
800 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
801 Program
->info
.gs
.input_primitive
;
804 case GL_GEOMETRY_OUTPUT_TYPE
:
807 if (check_gs_query(ctx
, shProg
)) {
808 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
809 Program
->info
.gs
.output_primitive
;
812 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
819 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
820 /* Add one for the terminating NUL character.
822 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
831 case GL_ACTIVE_UNIFORM_BLOCKS
:
835 *params
= shProg
->data
->NumUniformBlocks
;
837 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
838 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
839 * only available with desktop OpenGL 3.0+ with the
840 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
842 * On desktop, we ignore the 3.0+ requirement because it is silly.
844 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
847 *params
= shProg
->BinaryRetrievableHint
;
849 case GL_PROGRAM_BINARY_LENGTH
:
850 if (ctx
->Const
.NumProgramBinaryFormats
== 0 || !shProg
->data
->LinkStatus
) {
853 _mesa_get_program_binary_length(ctx
, shProg
, params
);
856 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
857 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
860 *params
= shProg
->data
->NumAtomicBuffers
;
862 case GL_COMPUTE_WORK_GROUP_SIZE
: {
864 if (!_mesa_has_compute_shaders(ctx
))
866 if (!shProg
->data
->LinkStatus
) {
867 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
871 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
872 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
876 for (i
= 0; i
< 3; i
++)
877 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
878 Program
->info
.cs
.local_size
[i
];
881 case GL_PROGRAM_SEPARABLE
:
882 /* If the program has not been linked, return initial value 0. */
883 *params
= (shProg
->data
->LinkStatus
== LINKING_FAILURE
) ? 0 : shProg
->SeparateShader
;
886 /* ARB_tessellation_shader */
887 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
890 if (check_tcs_query(ctx
, shProg
)) {
891 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
892 Program
->info
.tess
.tcs_vertices_out
;
895 case GL_TESS_GEN_MODE
:
898 if (check_tes_query(ctx
, shProg
)) {
899 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
900 Program
->info
.tess
.primitive_mode
;
903 case GL_TESS_GEN_SPACING
:
906 if (check_tes_query(ctx
, shProg
)) {
907 const struct gl_linked_shader
*tes
=
908 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
909 switch (tes
->Program
->info
.tess
.spacing
) {
910 case TESS_SPACING_EQUAL
:
913 case TESS_SPACING_FRACTIONAL_ODD
:
914 *params
= GL_FRACTIONAL_ODD
;
916 case TESS_SPACING_FRACTIONAL_EVEN
:
917 *params
= GL_FRACTIONAL_EVEN
;
919 case TESS_SPACING_UNSPECIFIED
:
925 case GL_TESS_GEN_VERTEX_ORDER
:
928 if (check_tes_query(ctx
, shProg
)) {
929 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
930 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
933 case GL_TESS_GEN_POINT_MODE
:
936 if (check_tes_query(ctx
, shProg
)) {
937 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
938 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
945 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
946 _mesa_enum_to_string(pname
));
951 * glGetShaderiv() - get GLSL shader state
954 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
956 struct gl_shader
*shader
=
957 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
965 *params
= shader
->Type
;
967 case GL_DELETE_STATUS
:
968 *params
= shader
->DeletePending
;
970 case GL_COMPLETION_STATUS_ARB
:
971 /* _mesa_glsl_compile_shader is not offloaded to other threads. */
974 case GL_COMPILE_STATUS
:
975 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
977 case GL_INFO_LOG_LENGTH
:
978 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
979 strlen(shader
->InfoLog
) + 1 : 0;
981 case GL_SHADER_SOURCE_LENGTH
:
982 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
984 case GL_SPIR_V_BINARY_ARB
:
985 *params
= (shader
->spirv_data
!= NULL
);
988 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
995 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
996 GLsizei
*length
, GLchar
*infoLog
)
998 struct gl_shader_program
*shProg
;
1000 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1001 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1003 * "If a negative number is provided where an argument of type sizei or
1004 * sizeiptr is specified, an INVALID_VALUE error is generated."
1007 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
1011 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1012 "glGetProgramInfoLog(program)");
1017 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
1022 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
1023 GLsizei
*length
, GLchar
*infoLog
)
1025 struct gl_shader
*sh
;
1027 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1028 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1030 * "If a negative number is provided where an argument of type sizei or
1031 * sizeiptr is specified, an INVALID_VALUE error is generated."
1034 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
1038 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
1043 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1048 * Return shader source code.
1051 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1052 GLsizei
*length
, GLchar
*sourceOut
)
1054 struct gl_shader
*sh
;
1056 if (maxLength
< 0) {
1057 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1061 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1065 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1070 * Set/replace shader source code. A helper function used by
1071 * glShaderSource[ARB].
1074 set_shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1078 /* The GL_ARB_gl_spirv spec adds the following to the end of the description
1081 * "If <shader> was previously associated with a SPIR-V module (via the
1082 * ShaderBinary command), that association is broken. Upon successful
1083 * completion of this command the SPIR_V_BINARY_ARB state of <shader>
1086 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
1088 if (sh
->CompileStatus
== COMPILE_SKIPPED
&& !sh
->FallbackSource
) {
1089 /* If shader was previously compiled back-up the source in case of cache
1092 sh
->FallbackSource
= sh
->Source
;
1093 sh
->Source
= source
;
1095 /* free old shader source string and install new one */
1096 free((void *)sh
->Source
);
1097 sh
->Source
= source
;
1101 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1110 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1115 /* The GL_ARB_gl_spirv spec says:
1117 * "Add a new error for the CompileShader command:
1119 * An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB
1120 * state of <shader> is TRUE."
1122 if (sh
->spirv_data
) {
1123 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCompileShader(SPIR-V)");
1128 /* If the user called glCompileShader without first calling
1129 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1131 sh
->CompileStatus
= COMPILE_FAILURE
;
1133 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1134 _mesa_log("GLSL source for %s shader %d:\n",
1135 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1136 _mesa_log("%s\n", sh
->Source
);
1139 /* this call will set the shader->CompileStatus field to indicate if
1140 * compilation was successful.
1142 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1144 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1145 _mesa_write_shader_to_file(sh
);
1148 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1149 if (sh
->CompileStatus
) {
1151 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1152 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1154 _mesa_log("No GLSL IR for shader %d (shader may be from "
1155 "cache)\n", sh
->Name
);
1159 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1161 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1162 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1163 _mesa_log("%s\n", sh
->InfoLog
);
1168 if (!sh
->CompileStatus
) {
1169 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1170 _mesa_log("GLSL source for %s shader %d:\n",
1171 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1172 _mesa_log("%s\n", sh
->Source
);
1173 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1176 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1177 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1178 sh
->Name
, sh
->InfoLog
);
1185 * Link a program's shaders.
1187 static ALWAYS_INLINE
void
1188 link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1195 /* From the ARB_transform_feedback2 specification:
1196 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1197 * is the name of a program being used by one or more transform feedback
1198 * objects, even if the objects are not currently bound or are paused."
1200 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1201 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1202 "glLinkProgram(transform feedback is using the program)");
1207 unsigned programs_in_use
= 0;
1209 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1210 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1211 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1212 programs_in_use
|= 1 << stage
;
1216 FLUSH_VERTICES(ctx
, 0);
1217 _mesa_glsl_link_shader(ctx
, shProg
);
1219 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1221 * "If LinkProgram or ProgramBinary successfully re-links a program
1222 * object that is active for any shader stage, then the newly generated
1223 * executable code will be installed as part of the current rendering
1224 * state for all shader stages where the program is active.
1225 * Additionally, the newly generated executable code is made part of
1226 * the state of any program pipeline for all stages where the program
1229 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1230 while (programs_in_use
) {
1231 const int stage
= u_bit_scan(&programs_in_use
);
1233 struct gl_program
*prog
= NULL
;
1234 if (shProg
->_LinkedShaders
[stage
])
1235 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1237 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1241 /* Capture .shader_test files. */
1242 const char *capture_path
= _mesa_get_shader_capture_path();
1243 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1244 /* Find an unused filename. */
1246 char *filename
= NULL
;
1247 for (unsigned i
= 0;; i
++) {
1249 filename
= ralloc_asprintf(NULL
, "%s/%u-%u.shader_test",
1250 capture_path
, shProg
->Name
, i
);
1252 filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1253 capture_path
, shProg
->Name
);
1255 file
= os_file_create_unique(filename
, 0644);
1258 ralloc_free(filename
);
1261 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1262 shProg
->IsES
? " ES" : "",
1263 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1264 if (shProg
->SeparateShader
)
1265 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1266 fprintf(file
, "\n");
1268 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1269 fprintf(file
, "[%s shader]\n%s\n",
1270 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1271 shProg
->Shaders
[i
]->Source
);
1275 _mesa_warning(ctx
, "Failed to open %s", filename
);
1278 ralloc_free(filename
);
1281 if (shProg
->data
->LinkStatus
== LINKING_FAILURE
&&
1282 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1283 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1284 shProg
->Name
, shProg
->data
->InfoLog
);
1287 _mesa_update_vertex_processing_mode(ctx
);
1289 shProg
->BinaryRetrievableHint
= shProg
->BinaryRetrievableHintPending
;
1295 printf("Link %u shaders in program %u: %s\n",
1296 shProg
->NumShaders
, shProg
->Name
,
1297 shProg
->data
->LinkStatus
? "Success" : "Failed");
1299 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1300 printf(" shader %u, stage %u\n",
1301 shProg
->Shaders
[i
]->Name
,
1302 shProg
->Shaders
[i
]->Stage
);
1309 link_program_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1311 link_program(ctx
, shProg
, false);
1316 link_program_no_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1318 link_program(ctx
, shProg
, true);
1323 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1325 link_program_error(ctx
, shProg
);
1330 * Print basic shader info (for debug).
1333 print_shader_info(const struct gl_shader_program
*shProg
)
1337 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1338 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1340 printf(" %s shader %u, checksum %u\n",
1341 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1342 shProg
->Shaders
[i
]->Name
,
1343 shProg
->Shaders
[i
]->SourceChecksum
);
1345 printf(" %s shader %u\n",
1346 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1347 shProg
->Shaders
[i
]->Name
);
1350 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1351 printf(" vert prog %u\n",
1352 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1353 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1354 printf(" frag prog %u\n",
1355 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1356 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1357 printf(" geom prog %u\n",
1358 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1359 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1360 printf(" tesc prog %u\n",
1361 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1362 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1363 printf(" tese prog %u\n",
1364 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1369 * Use the named shader program for subsequent glUniform calls
1372 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1375 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1376 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1377 "%s(program %u not linked)", caller
, shProg
->Name
);
1381 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1382 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1388 * Use the named shader program for subsequent rendering.
1391 _mesa_use_shader_program(struct gl_context
*ctx
,
1392 struct gl_shader_program
*shProg
)
1394 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1395 struct gl_program
*new_prog
= NULL
;
1396 if (shProg
&& shProg
->_LinkedShaders
[i
])
1397 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1398 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1400 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1405 * Do validation of the given shader program.
1406 * \param errMsg returns error message if validation fails.
1407 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1410 validate_shader_program(const struct gl_shader_program
*shProg
,
1413 if (!shProg
->data
->LinkStatus
) {
1417 /* From the GL spec, a program is invalid if any of these are true:
1419 any two active samplers in the current program object are of
1420 different types, but refer to the same texture image unit,
1422 any active sampler in the current program object refers to a texture
1423 image unit where fixed-function fragment processing accesses a
1424 texture target that does not match the sampler type, or
1426 the sum of the number of active samplers in the program and the
1427 number of texture image units enabled for fixed-function fragment
1428 processing exceeds the combined limit on the total number of texture
1429 image units allowed.
1433 * Check: any two active samplers in the current program object are of
1434 * different types, but refer to the same texture image unit,
1436 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1444 * Called via glValidateProgram()
1447 validate_program(struct gl_context
*ctx
, GLuint program
)
1449 struct gl_shader_program
*shProg
;
1450 char errMsg
[100] = "";
1452 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1457 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1458 if (!shProg
->data
->Validated
) {
1459 /* update info log */
1460 if (shProg
->data
->InfoLog
) {
1461 ralloc_free(shProg
->data
->InfoLog
);
1463 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1469 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1471 GET_CURRENT_CONTEXT(ctx
);
1472 attach_shader_no_error(ctx
, program
, shader
);
1477 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1479 GET_CURRENT_CONTEXT(ctx
);
1480 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1485 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1487 GET_CURRENT_CONTEXT(ctx
);
1488 attach_shader_no_error(ctx
, program
, shader
);
1493 _mesa_AttachShader(GLuint program
, GLuint shader
)
1495 GET_CURRENT_CONTEXT(ctx
);
1496 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1501 _mesa_CompileShader(GLuint shaderObj
)
1503 GET_CURRENT_CONTEXT(ctx
);
1504 if (MESA_VERBOSE
& VERBOSE_API
)
1505 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1506 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1507 "glCompileShader"));
1512 _mesa_CreateShader_no_error(GLenum type
)
1514 GET_CURRENT_CONTEXT(ctx
);
1515 return create_shader(ctx
, type
);
1520 _mesa_CreateShader(GLenum type
)
1522 GET_CURRENT_CONTEXT(ctx
);
1524 if (MESA_VERBOSE
& VERBOSE_API
)
1525 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1527 return create_shader_err(ctx
, type
, "glCreateShader");
1531 GLhandleARB GLAPIENTRY
1532 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1534 GET_CURRENT_CONTEXT(ctx
);
1535 return create_shader(ctx
, type
);
1539 GLhandleARB GLAPIENTRY
1540 _mesa_CreateShaderObjectARB(GLenum type
)
1542 GET_CURRENT_CONTEXT(ctx
);
1543 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1548 _mesa_CreateProgram(void)
1550 GET_CURRENT_CONTEXT(ctx
);
1551 if (MESA_VERBOSE
& VERBOSE_API
)
1552 _mesa_debug(ctx
, "glCreateProgram\n");
1553 return create_shader_program(ctx
);
1557 GLhandleARB GLAPIENTRY
1558 _mesa_CreateProgramObjectARB(void)
1560 GET_CURRENT_CONTEXT(ctx
);
1561 return create_shader_program(ctx
);
1566 _mesa_DeleteObjectARB(GLhandleARB obj
)
1568 if (MESA_VERBOSE
& VERBOSE_API
) {
1569 GET_CURRENT_CONTEXT(ctx
);
1570 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1574 GET_CURRENT_CONTEXT(ctx
);
1575 FLUSH_VERTICES(ctx
, 0);
1576 if (is_program(ctx
, obj
)) {
1577 delete_shader_program(ctx
, obj
);
1579 else if (is_shader(ctx
, obj
)) {
1580 delete_shader(ctx
, obj
);
1590 _mesa_DeleteProgram(GLuint name
)
1593 GET_CURRENT_CONTEXT(ctx
);
1594 FLUSH_VERTICES(ctx
, 0);
1595 delete_shader_program(ctx
, name
);
1601 _mesa_DeleteShader(GLuint name
)
1604 GET_CURRENT_CONTEXT(ctx
);
1605 FLUSH_VERTICES(ctx
, 0);
1606 delete_shader(ctx
, name
);
1612 _mesa_DetachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1614 GET_CURRENT_CONTEXT(ctx
);
1615 detach_shader_no_error(ctx
, program
, shader
);
1620 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1622 GET_CURRENT_CONTEXT(ctx
);
1623 detach_shader_error(ctx
, program
, shader
);
1628 _mesa_DetachShader_no_error(GLuint program
, GLuint shader
)
1630 GET_CURRENT_CONTEXT(ctx
);
1631 detach_shader_no_error(ctx
, program
, shader
);
1636 _mesa_DetachShader(GLuint program
, GLuint shader
)
1638 GET_CURRENT_CONTEXT(ctx
);
1639 detach_shader_error(ctx
, program
, shader
);
1644 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1645 GLsizei
* count
, GLhandleARB
* obj
)
1647 GET_CURRENT_CONTEXT(ctx
);
1648 get_attached_shaders(ctx
, (GLuint
)container
, maxCount
, count
, NULL
, obj
);
1653 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1654 GLsizei
*count
, GLuint
*obj
)
1656 GET_CURRENT_CONTEXT(ctx
);
1657 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
, NULL
);
1662 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1663 GLcharARB
* infoLog
)
1665 GET_CURRENT_CONTEXT(ctx
);
1666 if (is_program(ctx
, object
)) {
1667 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1669 else if (is_shader(ctx
, object
)) {
1670 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1673 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1679 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1681 GET_CURRENT_CONTEXT(ctx
);
1682 /* Implement in terms of GetProgramiv, GetShaderiv */
1683 if (is_program(ctx
, object
)) {
1684 if (pname
== GL_OBJECT_TYPE_ARB
) {
1685 *params
= GL_PROGRAM_OBJECT_ARB
;
1688 get_programiv(ctx
, object
, pname
, params
);
1691 else if (is_shader(ctx
, object
)) {
1692 if (pname
== GL_OBJECT_TYPE_ARB
) {
1693 *params
= GL_SHADER_OBJECT_ARB
;
1696 get_shaderiv(ctx
, object
, pname
, params
);
1700 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1706 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1709 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1710 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1711 params
[0] = (GLfloat
) iparams
[0];
1716 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1718 GET_CURRENT_CONTEXT(ctx
);
1719 get_programiv(ctx
, program
, pname
, params
);
1724 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1726 GET_CURRENT_CONTEXT(ctx
);
1727 get_shaderiv(ctx
, shader
, pname
, params
);
1732 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1733 GLsizei
*length
, GLchar
*infoLog
)
1735 GET_CURRENT_CONTEXT(ctx
);
1736 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1741 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1742 GLsizei
*length
, GLchar
*infoLog
)
1744 GET_CURRENT_CONTEXT(ctx
);
1745 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1750 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1751 GLsizei
*length
, GLchar
*sourceOut
)
1753 GET_CURRENT_CONTEXT(ctx
);
1754 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1758 GLhandleARB GLAPIENTRY
1759 _mesa_GetHandleARB(GLenum pname
)
1761 GET_CURRENT_CONTEXT(ctx
);
1762 return get_handle(ctx
, pname
);
1766 GLboolean GLAPIENTRY
1767 _mesa_IsProgram(GLuint name
)
1769 GET_CURRENT_CONTEXT(ctx
);
1770 return is_program(ctx
, name
);
1774 GLboolean GLAPIENTRY
1775 _mesa_IsShader(GLuint name
)
1777 GET_CURRENT_CONTEXT(ctx
);
1778 return is_shader(ctx
, name
);
1783 _mesa_LinkProgram_no_error(GLuint programObj
)
1785 GET_CURRENT_CONTEXT(ctx
);
1787 struct gl_shader_program
*shProg
=
1788 _mesa_lookup_shader_program(ctx
, programObj
);
1789 link_program_no_error(ctx
, shProg
);
1794 _mesa_LinkProgram(GLuint programObj
)
1796 GET_CURRENT_CONTEXT(ctx
);
1798 if (MESA_VERBOSE
& VERBOSE_API
)
1799 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1801 struct gl_shader_program
*shProg
=
1802 _mesa_lookup_shader_program_err(ctx
, programObj
, "glLinkProgram");
1803 link_program_error(ctx
, shProg
);
1806 #ifdef ENABLE_SHADER_CACHE
1808 * Generate a SHA-1 hash value string for given source string.
1811 generate_sha1(const char *source
, char sha_str
[64])
1813 unsigned char sha
[20];
1814 _mesa_sha1_compute(source
, strlen(source
), sha
);
1815 _mesa_sha1_format(sha_str
, sha
);
1819 * Construct a full path for shader replacement functionality using
1822 * <path>/<stage prefix>_<CHECKSUM>.glsl
1823 * <path>/<stage prefix>_<CHECKSUM>.arb
1826 construct_name(const gl_shader_stage stage
, const char *source
,
1830 static const char *types
[] = {
1831 "VS", "TC", "TE", "GS", "FS", "CS",
1834 const char *format
= strncmp(source
, "!!ARB", 5) ? "glsl" : "arb";
1836 generate_sha1(source
, sha
);
1837 return ralloc_asprintf(NULL
, "%s/%s_%s.%s", path
, types
[stage
], sha
, format
);
1841 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1844 _mesa_dump_shader_source(const gl_shader_stage stage
, const char *source
)
1846 static bool path_exists
= true;
1853 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1855 path_exists
= false;
1859 char *name
= construct_name(stage
, source
, dump_path
);
1861 f
= fopen(name
, "w");
1866 GET_CURRENT_CONTEXT(ctx
);
1867 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1874 * Read shader source code from a file.
1875 * Useful for debugging to override an app's shader.
1878 _mesa_read_shader_source(const gl_shader_stage stage
, const char *source
)
1881 static bool path_exists
= true;
1882 int len
, shader_size
= 0;
1889 read_path
= getenv("MESA_SHADER_READ_PATH");
1891 path_exists
= false;
1895 char *name
= construct_name(stage
, source
, read_path
);
1896 f
= fopen(name
, "r");
1901 /* allocate enough room for the entire shader */
1902 fseek(f
, 0, SEEK_END
);
1903 shader_size
= ftell(f
);
1905 assert(shader_size
);
1907 /* add one for terminating zero */
1910 buffer
= malloc(shader_size
);
1913 len
= fread(buffer
, 1, shader_size
, f
);
1921 #endif /* ENABLE_SHADER_CACHE */
1924 * Called via glShaderSource() and glShaderSourceARB() API functions.
1925 * Basically, concatenate the source code strings into one long string
1926 * and pass it to _mesa_shader_source().
1928 static ALWAYS_INLINE
void
1929 shader_source(struct gl_context
*ctx
, GLuint shaderObj
, GLsizei count
,
1930 const GLchar
*const *string
, const GLint
*length
, bool no_error
)
1933 GLsizei i
, totalLength
;
1935 struct gl_shader
*sh
;
1938 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1942 if (string
== NULL
) {
1943 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1947 sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1951 * This array holds offsets of where the appropriate string ends, thus the
1952 * last element will be set to the total length of the source code.
1954 offsets
= malloc(count
* sizeof(GLint
));
1955 if (offsets
== NULL
) {
1956 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1960 for (i
= 0; i
< count
; i
++) {
1961 if (!no_error
&& string
[i
] == NULL
) {
1962 free((GLvoid
*) offsets
);
1963 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1964 "glShaderSourceARB(null string)");
1967 if (length
== NULL
|| length
[i
] < 0)
1968 offsets
[i
] = strlen(string
[i
]);
1970 offsets
[i
] = length
[i
];
1971 /* accumulate string lengths */
1973 offsets
[i
] += offsets
[i
- 1];
1976 /* Total length of source string is sum off all strings plus two.
1977 * One extra byte for terminating zero, another extra byte to silence
1978 * valgrind warnings in the parser/grammer code.
1980 totalLength
= offsets
[count
- 1] + 2;
1981 source
= malloc(totalLength
* sizeof(GLcharARB
));
1982 if (source
== NULL
) {
1983 free((GLvoid
*) offsets
);
1984 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1988 for (i
= 0; i
< count
; i
++) {
1989 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1990 memcpy(source
+ start
, string
[i
],
1991 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1993 source
[totalLength
- 1] = '\0';
1994 source
[totalLength
- 2] = '\0';
1996 #ifdef ENABLE_SHADER_CACHE
1997 GLcharARB
*replacement
;
1999 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
2000 * if corresponding entry found from MESA_SHADER_READ_PATH.
2002 _mesa_dump_shader_source(sh
->Stage
, source
);
2004 replacement
= _mesa_read_shader_source(sh
->Stage
, source
);
2007 source
= replacement
;
2009 #endif /* ENABLE_SHADER_CACHE */
2011 set_shader_source(sh
, source
);
2018 _mesa_ShaderSource_no_error(GLuint shaderObj
, GLsizei count
,
2019 const GLchar
*const *string
, const GLint
*length
)
2021 GET_CURRENT_CONTEXT(ctx
);
2022 shader_source(ctx
, shaderObj
, count
, string
, length
, true);
2027 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
2028 const GLchar
*const *string
, const GLint
*length
)
2030 GET_CURRENT_CONTEXT(ctx
);
2031 shader_source(ctx
, shaderObj
, count
, string
, length
, false);
2035 static ALWAYS_INLINE
void
2036 use_program(GLuint program
, bool no_error
)
2038 GET_CURRENT_CONTEXT(ctx
);
2039 struct gl_shader_program
*shProg
= NULL
;
2041 if (MESA_VERBOSE
& VERBOSE_API
)
2042 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
2046 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2049 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
2050 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2051 "glUseProgram(transform feedback active)");
2057 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
2061 if (!shProg
->data
->LinkStatus
) {
2062 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2063 "glUseProgram(program %u not linked)", program
);
2068 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
2069 print_shader_info(shProg
);
2074 /* The ARB_separate_shader_object spec says:
2076 * "The executable code for an individual shader stage is taken from
2077 * the current program for that stage. If there is a current program
2078 * object established by UseProgram, that program is considered current
2079 * for all stages. Otherwise, if there is a bound program pipeline
2080 * object (section 2.14.PPO), the program bound to the appropriate
2081 * stage of the pipeline object is considered current."
2084 /* Attach shader state to the binding point */
2085 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
2086 /* Update the program */
2087 _mesa_use_shader_program(ctx
, shProg
);
2089 /* Must be done first: detach the progam */
2090 _mesa_use_shader_program(ctx
, shProg
);
2091 /* Unattach shader_state binding point */
2092 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
2093 ctx
->Pipeline
.Default
);
2094 /* If a pipeline was bound, rebind it */
2095 if (ctx
->Pipeline
.Current
) {
2097 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
2099 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
2103 _mesa_update_vertex_processing_mode(ctx
);
2108 _mesa_UseProgram_no_error(GLuint program
)
2110 use_program(program
, true);
2115 _mesa_UseProgram(GLuint program
)
2117 use_program(program
, false);
2122 _mesa_ValidateProgram(GLuint program
)
2124 GET_CURRENT_CONTEXT(ctx
);
2125 validate_program(ctx
, program
);
2130 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2133 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
2134 GLint
* range
, GLint
* precision
)
2136 const struct gl_program_constants
*limits
;
2137 const struct gl_precision
*p
;
2138 GET_CURRENT_CONTEXT(ctx
);
2140 switch (shadertype
) {
2141 case GL_VERTEX_SHADER
:
2142 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
2144 case GL_FRAGMENT_SHADER
:
2145 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
2148 _mesa_error(ctx
, GL_INVALID_ENUM
,
2149 "glGetShaderPrecisionFormat(shadertype)");
2153 switch (precisiontype
) {
2155 p
= &limits
->LowFloat
;
2157 case GL_MEDIUM_FLOAT
:
2158 p
= &limits
->MediumFloat
;
2161 p
= &limits
->HighFloat
;
2164 p
= &limits
->LowInt
;
2167 p
= &limits
->MediumInt
;
2170 p
= &limits
->HighInt
;
2173 _mesa_error(ctx
, GL_INVALID_ENUM
,
2174 "glGetShaderPrecisionFormat(precisiontype)");
2178 range
[0] = p
->RangeMin
;
2179 range
[1] = p
->RangeMax
;
2180 precision
[0] = p
->Precision
;
2185 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2188 _mesa_ReleaseShaderCompiler(void)
2190 _mesa_destroy_shader_compiler_caches();
2195 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2198 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2199 const void* binary
, GLint length
)
2201 GET_CURRENT_CONTEXT(ctx
);
2202 struct gl_shader
**sh
;
2204 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2205 * page 88 of the OpenGL 4.5 specs state:
2207 * "An INVALID_VALUE error is generated if count or length is negative.
2208 * An INVALID_ENUM error is generated if binaryformat is not a supported
2209 * format returned in SHADER_BINARY_FORMATS."
2211 if (n
< 0 || length
< 0) {
2212 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2216 /* Get all shader objects at once so we can make the operation
2219 if (n
> SIZE_MAX
/ sizeof(*sh
)) {
2220 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary(count)");
2224 sh
= alloca(sizeof(*sh
) * (size_t)n
);
2226 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary");
2230 for (int i
= 0; i
< n
; ++i
) {
2231 sh
[i
] = _mesa_lookup_shader_err(ctx
, shaders
[i
], "glShaderBinary");
2236 if (binaryformat
== GL_SHADER_BINARY_FORMAT_SPIR_V_ARB
) {
2237 if (!ctx
->Extensions
.ARB_gl_spirv
) {
2238 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary(SPIR-V)");
2240 _mesa_spirv_shader_binary(ctx
, (unsigned) n
, sh
, binary
,
2247 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2252 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2253 GLenum
*binaryFormat
, GLvoid
*binary
)
2255 struct gl_shader_program
*shProg
;
2256 GLsizei length_dummy
;
2257 GET_CURRENT_CONTEXT(ctx
);
2260 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2264 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2268 /* The ARB_get_program_binary spec says:
2270 * "If <length> is NULL, then no length is returned."
2272 * Ensure that length always points to valid storage to avoid multiple NULL
2273 * pointer checks below.
2276 length
= &length_dummy
;
2279 /* The ARB_get_program_binary spec says:
2281 * "When a program object's LINK_STATUS is FALSE, its program binary
2282 * length is zero, and a call to GetProgramBinary will generate an
2283 * INVALID_OPERATION error.
2285 if (!shProg
->data
->LinkStatus
) {
2286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2287 "glGetProgramBinary(program %u not linked)",
2293 if (ctx
->Const
.NumProgramBinaryFormats
== 0) {
2295 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2296 "glGetProgramBinary(driver supports zero binary formats)");
2298 _mesa_get_program_binary(ctx
, shProg
, bufSize
, length
, binaryFormat
,
2300 assert(*length
== 0 || *binaryFormat
== GL_PROGRAM_BINARY_FORMAT_MESA
);
2305 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2306 const GLvoid
*binary
, GLsizei length
)
2308 struct gl_shader_program
*shProg
;
2309 GET_CURRENT_CONTEXT(ctx
);
2311 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2315 _mesa_clear_shader_program_data(ctx
, shProg
);
2316 shProg
->data
= _mesa_create_shader_program_data();
2318 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2320 * "If a negative number is provided where an argument of type sizei or
2321 * sizeiptr is specified, an INVALID_VALUE error is generated."
2324 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2328 if (ctx
->Const
.NumProgramBinaryFormats
== 0 ||
2329 binaryFormat
!= GL_PROGRAM_BINARY_FORMAT_MESA
) {
2330 /* The ARB_get_program_binary spec says:
2332 * "<binaryFormat> and <binary> must be those returned by a previous
2333 * call to GetProgramBinary, and <length> must be the length of the
2334 * program binary as returned by GetProgramBinary or GetProgramiv with
2335 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2336 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2339 * Since any value of binaryFormat passed "is not one of those specified as
2340 * allowable for [this] command, an INVALID_ENUM error is generated."
2342 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2343 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2345 _mesa_program_binary(ctx
, shProg
, binaryFormat
, binary
, length
);
2350 static ALWAYS_INLINE
void
2351 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2352 GLuint pname
, GLint value
, bool no_error
)
2355 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2356 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2357 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2358 * even be in the dispatch table, so we shouldn't need to expclicitly
2361 * On desktop, we ignore the 3.0+ requirement because it is silly.
2364 /* The ARB_get_program_binary extension spec says:
2366 * "An INVALID_VALUE error is generated if the <value> argument to
2367 * ProgramParameteri is not TRUE or FALSE."
2369 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2373 /* No need to notify the driver. Any changes will actually take effect
2374 * the next time the shader is linked.
2376 * The ARB_get_program_binary extension spec says:
2378 * "To indicate that a program binary is likely to be retrieved,
2379 * ProgramParameteri should be called with <pname>
2380 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2381 * will not be in effect until the next time LinkProgram or
2382 * ProgramBinary has been called successfully."
2384 * The resolution of issue 9 in the extension spec also says:
2386 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2387 * to indicate to the GL implementation that this program will
2388 * likely be saved with GetProgramBinary at some point. This will
2389 * give the GL implementation the opportunity to track any state
2390 * changes made to the program before being saved such that when it
2391 * is loaded again a recompile can be avoided."
2393 shProg
->BinaryRetrievableHintPending
= value
;
2396 case GL_PROGRAM_SEPARABLE
:
2397 /* Spec imply that the behavior is the same as ARB_get_program_binary
2398 * Chapter 7.3 Program Objects
2400 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2403 shProg
->SeparateShader
= value
;
2408 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2409 _mesa_enum_to_string(pname
));
2415 _mesa_error(ctx
, GL_INVALID_VALUE
,
2416 "glProgramParameteri(pname=%s, value=%d): "
2417 "value must be 0 or 1.",
2418 _mesa_enum_to_string(pname
),
2424 _mesa_ProgramParameteri_no_error(GLuint program
, GLenum pname
, GLint value
)
2426 GET_CURRENT_CONTEXT(ctx
);
2428 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
2429 program_parameteri(ctx
, shProg
, pname
, value
, true);
2434 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2436 struct gl_shader_program
*shProg
;
2437 GET_CURRENT_CONTEXT(ctx
);
2439 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2440 "glProgramParameteri");
2444 program_parameteri(ctx
, shProg
, pname
, value
, false);
2449 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2450 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2451 struct gl_pipeline_object
*shTarget
)
2453 struct gl_program
**target
;
2455 target
= &shTarget
->CurrentProgram
[stage
];
2457 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2460 if (*target
!= prog
) {
2461 /* Program is current, flush it */
2462 if (shTarget
== ctx
->_Shader
) {
2463 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2466 _mesa_reference_shader_program(ctx
,
2467 &shTarget
->ReferencedPrograms
[stage
],
2469 _mesa_reference_program(ctx
, target
, prog
);
2470 if (stage
== MESA_SHADER_VERTEX
)
2471 _mesa_update_vertex_processing_mode(ctx
);
2479 * Copy program-specific data generated by linking from the gl_shader_program
2480 * object to the gl_program object referred to by the gl_linked_shader.
2482 * This function expects _mesa_reference_program() to have been previously
2483 * called setting the gl_linked_shaders program reference.
2486 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2487 struct gl_linked_shader
*dst_sh
)
2489 assert(dst_sh
->Program
);
2491 struct gl_program
*dst
= dst_sh
->Program
;
2493 dst
->info
.separate_shader
= src
->SeparateShader
;
2495 switch (dst_sh
->Stage
) {
2496 case MESA_SHADER_GEOMETRY
: {
2497 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2498 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2499 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2502 case MESA_SHADER_FRAGMENT
: {
2503 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2506 case MESA_SHADER_COMPUTE
: {
2507 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2516 * ARB_separate_shader_objects: Compile & Link Program
2519 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2520 const GLchar
* const *strings
)
2522 GET_CURRENT_CONTEXT(ctx
);
2524 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2528 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2529 * GL_INVALID_VALUE should be generated if count < 0
2532 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2537 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2539 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2540 _mesa_compile_shader(ctx
, sh
);
2542 program
= create_shader_program(ctx
);
2544 struct gl_shader_program
*shProg
;
2545 GLint compiled
= GL_FALSE
;
2547 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2549 shProg
->SeparateShader
= GL_TRUE
;
2551 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2553 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2554 _mesa_link_program(ctx
, shProg
);
2555 detach_shader_error(ctx
, program
, shader
);
2559 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2560 append
-error
-to
-info
-log
;
2561 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2566 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2569 delete_shader(ctx
, shader
);
2577 * For GL_ARB_tessellation_shader
2580 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2582 GET_CURRENT_CONTEXT(ctx
);
2583 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2587 extern void GLAPIENTRY
2588 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2590 GET_CURRENT_CONTEXT(ctx
);
2592 if (!_mesa_has_tessellation(ctx
)) {
2593 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2597 if (pname
!= GL_PATCH_VERTICES
) {
2598 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2602 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2603 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2607 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2611 extern void GLAPIENTRY
2612 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2614 GET_CURRENT_CONTEXT(ctx
);
2616 if (!_mesa_has_tessellation(ctx
)) {
2617 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2622 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2623 FLUSH_VERTICES(ctx
, 0);
2624 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2625 4 * sizeof(GLfloat
));
2626 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2628 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2629 FLUSH_VERTICES(ctx
, 0);
2630 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2631 2 * sizeof(GLfloat
));
2632 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2635 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2641 * ARB_shader_subroutine
2644 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2647 GET_CURRENT_CONTEXT(ctx
);
2648 const char *api_name
= "glGetSubroutineUniformLocation";
2649 struct gl_shader_program
*shProg
;
2650 GLenum resource_type
;
2651 gl_shader_stage stage
;
2653 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2654 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2658 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2662 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2663 if (!shProg
->_LinkedShaders
[stage
]) {
2664 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2668 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2669 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2673 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2676 GET_CURRENT_CONTEXT(ctx
);
2677 const char *api_name
= "glGetSubroutineIndex";
2678 struct gl_shader_program
*shProg
;
2679 struct gl_program_resource
*res
;
2680 GLenum resource_type
;
2681 gl_shader_stage stage
;
2683 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2684 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2688 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2692 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2693 if (!shProg
->_LinkedShaders
[stage
]) {
2694 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2698 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2699 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2704 return _mesa_program_resource_index(shProg
, res
);
2709 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2710 GLuint index
, GLenum pname
, GLint
*values
)
2712 GET_CURRENT_CONTEXT(ctx
);
2713 const char *api_name
= "glGetActiveSubroutineUniformiv";
2714 struct gl_shader_program
*shProg
;
2715 struct gl_linked_shader
*sh
;
2716 gl_shader_stage stage
;
2717 struct gl_program_resource
*res
;
2718 const struct gl_uniform_storage
*uni
;
2719 GLenum resource_type
;
2722 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2723 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2727 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2731 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2732 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2734 sh
= shProg
->_LinkedShaders
[stage
];
2736 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2740 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2741 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2742 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2747 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2748 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2751 values
[0] = uni
->num_compatible_subroutines
;
2755 case GL_COMPATIBLE_SUBROUTINES
: {
2756 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2760 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2761 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2762 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2763 if (fn
->types
[j
] == uni
->type
) {
2764 values
[count
++] = i
;
2772 case GL_UNIFORM_SIZE
:
2773 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2776 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2779 case GL_UNIFORM_NAME_LENGTH
:
2780 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2782 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2783 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2787 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2794 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2795 GLuint index
, GLsizei bufsize
,
2796 GLsizei
*length
, GLchar
*name
)
2798 GET_CURRENT_CONTEXT(ctx
);
2799 const char *api_name
= "glGetActiveSubroutineUniformName";
2800 struct gl_shader_program
*shProg
;
2801 GLenum resource_type
;
2802 gl_shader_stage stage
;
2804 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2805 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2809 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2813 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2814 if (!shProg
->_LinkedShaders
[stage
]) {
2815 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2819 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2820 /* get program resource name */
2821 _mesa_get_program_resource_name(shProg
, resource_type
,
2823 length
, name
, api_name
);
2828 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2829 GLuint index
, GLsizei bufsize
,
2830 GLsizei
*length
, GLchar
*name
)
2832 GET_CURRENT_CONTEXT(ctx
);
2833 const char *api_name
= "glGetActiveSubroutineName";
2834 struct gl_shader_program
*shProg
;
2835 GLenum resource_type
;
2836 gl_shader_stage stage
;
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 if (!shProg
->_LinkedShaders
[stage
]) {
2849 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2852 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2853 _mesa_get_program_resource_name(shProg
, resource_type
,
2855 length
, name
, api_name
);
2859 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2860 const GLuint
*indices
)
2862 GET_CURRENT_CONTEXT(ctx
);
2863 const char *api_name
= "glUniformSubroutinesuiv";
2864 gl_shader_stage stage
;
2867 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2868 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2872 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2873 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2875 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2879 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2880 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2885 bool flushed
= false;
2887 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2894 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2898 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2901 for (j
= i
; j
< i
+ uni_count
; j
++) {
2902 struct gl_subroutine_function
*subfn
= NULL
;
2903 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2904 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2908 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2909 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2910 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2917 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2918 if (subfn
->types
[k
] == uni
->type
)
2921 if (k
== subfn
->num_compat_types
) {
2922 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2926 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2934 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2937 GET_CURRENT_CONTEXT(ctx
);
2938 const char *api_name
= "glGetUniformSubroutineuiv";
2939 gl_shader_stage stage
;
2941 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2942 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2946 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2947 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2949 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2953 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2954 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2958 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2963 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2964 GLenum pname
, GLint
*values
)
2966 GET_CURRENT_CONTEXT(ctx
);
2967 const char *api_name
= "glGetProgramStageiv";
2968 struct gl_shader_program
*shProg
;
2969 struct gl_linked_shader
*sh
;
2970 gl_shader_stage stage
;
2972 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2973 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2977 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2981 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2982 sh
= shProg
->_LinkedShaders
[stage
];
2984 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2985 * INVALID_OPERATION in the case of not be linked.
2987 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2988 * same info using other specs (ARB_program_interface_query), without the
2989 * need of the program to be linked, being the value for that case 0.
2991 * But at the same time, some other methods require the program to be
2992 * linked for pname related to locations, so it would be inconsistent to
2993 * not do the same here. So we are:
2994 * * Return GL_INVALID_OPERATION if not linked only for locations.
2995 * * Setting a default value of 0, to be returned if not linked.
2999 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
3000 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3005 struct gl_program
*p
= sh
->Program
;
3007 case GL_ACTIVE_SUBROUTINES
:
3008 values
[0] = p
->sh
.NumSubroutineFunctions
;
3010 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
3011 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
3013 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
3014 values
[0] = p
->sh
.NumSubroutineUniforms
;
3016 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
3020 GLenum resource_type
;
3021 struct gl_program_resource
*res
;
3023 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
3024 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3025 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3027 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
3032 values
[0] = max_len
;
3035 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
3039 GLenum resource_type
;
3040 struct gl_program_resource
*res
;
3042 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
3043 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3044 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3046 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
3047 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
3053 values
[0] = max_len
;
3057 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
3064 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
3068 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3069 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
3070 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
3071 if (fn
->types
[j
] == type
)
3079 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
3080 struct gl_program
*p
)
3084 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
3089 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3098 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
3099 for (j
= 0; j
< uni_count
; j
++) {
3100 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
3101 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
3104 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
3106 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
3110 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
3111 gl_shader_stage stage
)
3113 if (ctx
->_Shader
->CurrentProgram
[stage
])
3114 _mesa_shader_write_subroutine_index(ctx
,
3115 ctx
->_Shader
->CurrentProgram
[stage
]);
3119 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
3120 struct gl_program
*p
)
3124 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
3125 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
3126 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
3127 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
3128 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
3131 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3132 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3137 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);