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
42 #include <c99_alloca.h>
43 #include "main/glheader.h"
44 #include "main/context.h"
45 #include "main/enums.h"
46 #include "main/glspirv.h"
47 #include "main/hash.h"
48 #include "main/mtypes.h"
49 #include "main/pipelineobj.h"
50 #include "main/program_binary.h"
51 #include "main/shaderapi.h"
52 #include "main/shaderobj.h"
53 #include "main/state.h"
54 #include "main/transformfeedback.h"
55 #include "main/uniforms.h"
56 #include "compiler/glsl/glsl_parser_extras.h"
57 #include "compiler/glsl/ir.h"
58 #include "compiler/glsl/ir_uniform.h"
59 #include "compiler/glsl/program.h"
60 #include "program/program.h"
61 #include "program/prog_print.h"
62 #include "program/prog_parameter.h"
63 #include "util/ralloc.h"
64 #include "util/hash_table.h"
65 #include "util/mesa-sha1.h"
66 #include "util/crc32.h"
67 #include "util/os_file.h"
70 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
73 _mesa_get_shader_flags(void)
75 GLbitfield flags
= 0x0;
76 const char *env
= getenv("MESA_GLSL");
79 if (strstr(env
, "dump_on_error"))
80 flags
|= GLSL_DUMP_ON_ERROR
;
81 else if (strstr(env
, "dump"))
83 if (strstr(env
, "log"))
85 if (strstr(env
, "cache_fb"))
86 flags
|= GLSL_CACHE_FALLBACK
;
87 if (strstr(env
, "cache_info"))
88 flags
|= GLSL_CACHE_INFO
;
89 if (strstr(env
, "nopvert"))
90 flags
|= GLSL_NOP_VERT
;
91 if (strstr(env
, "nopfrag"))
92 flags
|= GLSL_NOP_FRAG
;
93 if (strstr(env
, "uniform"))
94 flags
|= GLSL_UNIFORMS
;
95 if (strstr(env
, "useprog"))
96 flags
|= GLSL_USE_PROG
;
97 if (strstr(env
, "errors"))
98 flags
|= GLSL_REPORT_ERRORS
;
105 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
108 _mesa_get_shader_capture_path(void)
110 static bool read_env_var
= false;
111 static const char *path
= NULL
;
114 path
= getenv("MESA_SHADER_CAPTURE_PATH");
122 * Initialize context's shader state.
125 _mesa_init_shader_state(struct gl_context
*ctx
)
127 /* Device drivers may override these to control what kind of instructions
128 * are generated by the GLSL compiler.
130 struct gl_shader_compiler_options options
;
134 memset(&options
, 0, sizeof(options
));
135 options
.MaxUnrollIterations
= 32;
136 options
.MaxIfDepth
= UINT_MAX
;
138 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
139 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
141 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
143 if (ctx
->Shader
.Flags
!= 0)
144 ctx
->Const
.GenerateTemporaryNames
= true;
146 /* Extended for ARB_separate_shader_objects */
147 ctx
->Shader
.RefCount
= 1;
148 ctx
->TessCtrlProgram
.patch_vertices
= 3;
149 for (i
= 0; i
< 4; ++i
)
150 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
151 for (i
= 0; i
< 2; ++i
)
152 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
157 * Free the per-context shader-related state.
160 _mesa_free_shader_state(struct gl_context
*ctx
)
162 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
163 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
164 _mesa_reference_shader_program(ctx
,
165 &ctx
->Shader
.ReferencedPrograms
[i
],
168 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
170 /* Extended for ARB_separate_shader_objects */
171 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
173 assert(ctx
->Shader
.RefCount
== 1);
178 * Copy string from <src> to <dst>, up to maxLength characters, returning
179 * length of <dst> in <length>.
180 * \param src the strings source
181 * \param maxLength max chars to copy
182 * \param length returns number of chars copied
183 * \param dst the string destination
186 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
187 GLsizei
*length
, const GLchar
*src
)
190 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
201 * Confirm that the a shader type is valid and supported by the implementation
203 * \param ctx Current GL context
204 * \param type Shader target
208 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
210 /* Note: when building built-in GLSL functions, this function may be
211 * invoked with ctx == NULL. In that case, we can only validate that it's
212 * a shader target we recognize, not that it's supported in the current
213 * context. But that's fine--we don't need any further validation than
214 * that when building built-in GLSL functions.
218 case GL_FRAGMENT_SHADER
:
219 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
220 case GL_VERTEX_SHADER
:
221 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
222 case GL_GEOMETRY_SHADER_ARB
:
223 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
224 case GL_TESS_CONTROL_SHADER
:
225 case GL_TESS_EVALUATION_SHADER
:
226 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
227 case GL_COMPUTE_SHADER
:
228 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
236 is_program(struct gl_context
*ctx
, GLuint name
)
238 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
239 return shProg
? GL_TRUE
: GL_FALSE
;
244 is_shader(struct gl_context
*ctx
, GLuint name
)
246 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
247 return shader
? GL_TRUE
: GL_FALSE
;
252 * Attach shader to a shader program.
255 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
256 struct gl_shader
*sh
)
258 GLuint n
= shProg
->NumShaders
;
260 shProg
->Shaders
= realloc(shProg
->Shaders
,
261 (n
+ 1) * sizeof(struct gl_shader
*));
262 if (!shProg
->Shaders
) {
263 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
268 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
269 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
270 shProg
->NumShaders
++;
274 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
277 struct gl_shader_program
*shProg
;
278 struct gl_shader
*sh
;
281 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
283 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
287 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
292 n
= shProg
->NumShaders
;
293 for (i
= 0; i
< n
; i
++) {
294 if (shProg
->Shaders
[i
] == sh
) {
295 /* The shader is already attched to this program. The
296 * GL_ARB_shader_objects spec says:
298 * "The error INVALID_OPERATION is generated by AttachObjectARB
299 * if <obj> is already attached to <containerObj>."
301 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
303 } else if (same_type_disallowed
&&
304 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
305 /* Shader with the same type is already attached to this program,
306 * OpenGL ES 2.0 and 3.0 specs say:
308 * "Multiple shader objects of the same type may not be attached
309 * to a single program object. [...] The error INVALID_OPERATION
310 * is generated if [...] another shader object of the same type
311 * as shader is already attached to program."
313 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
318 attach_shader(ctx
, shProg
, sh
);
322 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
324 struct gl_shader_program
*shProg
;
325 struct gl_shader
*sh
;
327 shProg
= _mesa_lookup_shader_program(ctx
, program
);
328 sh
= _mesa_lookup_shader(ctx
, shader
);
330 attach_shader(ctx
, shProg
, sh
);
334 create_shader(struct gl_context
*ctx
, GLenum type
)
336 struct gl_shader
*sh
;
339 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
340 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
341 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
343 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
344 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
351 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
353 if (!_mesa_validate_shader_target(ctx
, type
)) {
354 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
355 caller
, _mesa_enum_to_string(type
));
359 return create_shader(ctx
, type
);
364 create_shader_program(struct gl_context
*ctx
)
367 struct gl_shader_program
*shProg
;
369 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
371 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
373 shProg
= _mesa_new_shader_program(name
);
375 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
377 assert(shProg
->RefCount
== 1);
379 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
386 * Delete a shader program. Actually, just decrement the program's
387 * reference count and mark it as DeletePending.
388 * Used to implement glDeleteProgram() and glDeleteObjectARB().
391 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
394 * NOTE: deleting shaders/programs works a bit differently than
395 * texture objects (and buffer objects, etc). Shader/program
396 * handles/IDs exist in the hash table until the object is really
397 * deleted (refcount==0). With texture objects, the handle/ID is
398 * removed from the hash table in glDeleteTextures() while the tex
399 * object itself might linger until its refcount goes to zero.
401 struct gl_shader_program
*shProg
;
403 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
407 if (!shProg
->DeletePending
) {
408 shProg
->DeletePending
= GL_TRUE
;
410 /* effectively, decr shProg's refcount */
411 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
417 delete_shader(struct gl_context
*ctx
, GLuint shader
)
419 struct gl_shader
*sh
;
421 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
425 if (!sh
->DeletePending
) {
426 sh
->DeletePending
= GL_TRUE
;
428 /* effectively, decr sh's refcount */
429 _mesa_reference_shader(ctx
, &sh
, NULL
);
434 static ALWAYS_INLINE
void
435 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
438 struct gl_shader_program
*shProg
;
443 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
447 shProg
= _mesa_lookup_shader_program(ctx
, program
);
450 n
= shProg
->NumShaders
;
452 for (i
= 0; i
< n
; i
++) {
453 if (shProg
->Shaders
[i
]->Name
== shader
) {
455 struct gl_shader
**newList
;
458 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
460 /* alloc new, smaller array */
461 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
463 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
466 /* Copy old list entries to new list, skipping removed entry at [i] */
467 for (j
= 0; j
< i
; j
++) {
468 newList
[j
] = shProg
->Shaders
[j
];
471 newList
[j
++] = shProg
->Shaders
[i
];
474 /* Free old list and install new one */
475 free(shProg
->Shaders
);
476 shProg
->Shaders
= newList
;
477 shProg
->NumShaders
= n
- 1;
480 /* sanity check - make sure the new list's entries are sensible */
481 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
482 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
483 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
484 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
485 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
486 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
487 assert(shProg
->Shaders
[j
]->RefCount
> 0);
498 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
499 err
= GL_INVALID_OPERATION
;
501 err
= GL_INVALID_VALUE
;
502 _mesa_error(ctx
, err
, "glDetachShader(shader)");
509 detach_shader_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
511 detach_shader(ctx
, program
, shader
, false);
516 detach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
518 detach_shader(ctx
, program
, shader
, true);
523 * Return list of shaders attached to shader program.
524 * \param objOut returns GLuint ids
525 * \param handleOut returns GLhandleARB handles
528 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
529 GLsizei
*countOut
, GLuint
*objOut
, GLhandleARB
*handleOut
)
531 struct gl_shader_program
*shProg
;
534 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
539 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
543 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
545 objOut
[i
] = shProg
->Shaders
[i
]->Name
;
549 handleOut
[i
] = (GLhandleARB
) shProg
->Shaders
[i
]->Name
;
559 * glGetHandleARB() - return ID/name of currently bound shader program.
562 get_handle(struct gl_context
*ctx
, GLenum pname
)
564 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
565 if (ctx
->_Shader
->ActiveProgram
)
566 return ctx
->_Shader
->ActiveProgram
->Name
;
571 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
578 * Check if a geometry shader query is valid at this time. If not, report an
579 * error and return false.
581 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
583 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
584 * are queried for a program which has not been linked successfully, or
585 * which does not contain objects to form a geometry shader, then an
586 * INVALID_OPERATION error is generated."
589 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
591 if (shProg
->data
->LinkStatus
&&
592 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
596 _mesa_error(ctx
, GL_INVALID_OPERATION
,
597 "glGetProgramv(linked geometry shader required)");
603 * Check if a tessellation control shader query is valid at this time.
604 * If not, report an error and return false.
606 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
608 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
609 * not been linked successfully, or which does not contain objects to
610 * form a tessellation control shader, then an INVALID_OPERATION error is
614 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
616 if (shProg
->data
->LinkStatus
&&
617 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
621 _mesa_error(ctx
, GL_INVALID_OPERATION
,
622 "glGetProgramv(linked tessellation control shader required)");
628 * Check if a tessellation evaluation shader query is valid at this time.
629 * If not, report an error and return false.
631 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
633 * "If any of the pname values in this paragraph are queried for a program
634 * which has not been linked successfully, or which does not contain
635 * objects to form a tessellation evaluation shader, then an
636 * INVALID_OPERATION error is generated."
640 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
642 if (shProg
->data
->LinkStatus
&&
643 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
647 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
648 "evaluation shader required)");
654 * glGetProgramiv() - get shader program state.
655 * Note that this is for GLSL shader programs, not ARB vertex/fragment
656 * programs (see glGetProgramivARB).
659 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
662 struct gl_shader_program
*shProg
663 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
665 /* Is transform feedback available in this context?
668 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
669 || ctx
->API
== API_OPENGL_CORE
670 || _mesa_is_gles3(ctx
);
672 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
673 * and GL 3.2) are available in this context
675 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
676 const bool has_tess
= _mesa_has_tessellation(ctx
);
678 /* Are uniform buffer objects available in this context?
681 (ctx
->API
== API_OPENGL_COMPAT
&&
682 ctx
->Extensions
.ARB_uniform_buffer_object
)
683 || ctx
->API
== API_OPENGL_CORE
684 || _mesa_is_gles3(ctx
);
691 case GL_DELETE_STATUS
:
692 *params
= shProg
->DeletePending
;
694 case GL_COMPLETION_STATUS_ARB
:
695 if (ctx
->Driver
.GetShaderProgramCompletionStatus
)
696 *params
= ctx
->Driver
.GetShaderProgramCompletionStatus(ctx
, shProg
);
701 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
703 case GL_VALIDATE_STATUS
:
704 *params
= shProg
->data
->Validated
;
706 case GL_INFO_LOG_LENGTH
:
707 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
708 strlen(shProg
->data
->InfoLog
) + 1 : 0;
710 case GL_ATTACHED_SHADERS
:
711 *params
= shProg
->NumShaders
;
713 case GL_ACTIVE_ATTRIBUTES
:
714 *params
= _mesa_count_active_attribs(shProg
);
716 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
717 *params
= _mesa_longest_attribute_name_length(shProg
);
719 case GL_ACTIVE_UNIFORMS
: {
721 const unsigned num_uniforms
=
722 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
723 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
724 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
729 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
732 const unsigned num_uniforms
=
733 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
735 for (i
= 0; i
< num_uniforms
; i
++) {
736 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
739 /* Add one for the terminating NUL character for a non-array, and
740 * 4 for the "[0]" and the NUL for an array.
742 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
743 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
752 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
755 *params
= shProg
->TransformFeedback
.NumVarying
;
757 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
763 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
764 /* Add one for the terminating NUL character.
767 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
776 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
779 *params
= shProg
->TransformFeedback
.BufferMode
;
781 case GL_GEOMETRY_VERTICES_OUT
:
784 if (check_gs_query(ctx
, shProg
)) {
785 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
786 Program
->info
.gs
.vertices_out
;
789 case GL_GEOMETRY_SHADER_INVOCATIONS
:
790 if (!has_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
792 if (check_gs_query(ctx
, shProg
)) {
793 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
794 Program
->info
.gs
.invocations
;
797 case GL_GEOMETRY_INPUT_TYPE
:
800 if (check_gs_query(ctx
, shProg
)) {
801 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
802 Program
->info
.gs
.input_primitive
;
805 case GL_GEOMETRY_OUTPUT_TYPE
:
808 if (check_gs_query(ctx
, shProg
)) {
809 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
810 Program
->info
.gs
.output_primitive
;
813 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
820 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
821 /* Add one for the terminating NUL character.
823 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
832 case GL_ACTIVE_UNIFORM_BLOCKS
:
836 *params
= shProg
->data
->NumUniformBlocks
;
838 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
839 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
840 * only available with desktop OpenGL 3.0+ with the
841 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
843 * On desktop, we ignore the 3.0+ requirement because it is silly.
845 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
848 *params
= shProg
->BinaryRetrievableHint
;
850 case GL_PROGRAM_BINARY_LENGTH
:
851 if (ctx
->Const
.NumProgramBinaryFormats
== 0 || !shProg
->data
->LinkStatus
) {
854 _mesa_get_program_binary_length(ctx
, shProg
, params
);
857 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
858 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
861 *params
= shProg
->data
->NumAtomicBuffers
;
863 case GL_COMPUTE_WORK_GROUP_SIZE
: {
865 if (!_mesa_has_compute_shaders(ctx
))
867 if (!shProg
->data
->LinkStatus
) {
868 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
872 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
873 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
877 for (i
= 0; i
< 3; i
++)
878 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
879 Program
->info
.cs
.local_size
[i
];
882 case GL_PROGRAM_SEPARABLE
:
883 /* If the program has not been linked, return initial value 0. */
884 *params
= (shProg
->data
->LinkStatus
== LINKING_FAILURE
) ? 0 : shProg
->SeparateShader
;
887 /* ARB_tessellation_shader */
888 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
891 if (check_tcs_query(ctx
, shProg
)) {
892 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
893 Program
->info
.tess
.tcs_vertices_out
;
896 case GL_TESS_GEN_MODE
:
899 if (check_tes_query(ctx
, shProg
)) {
900 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
901 Program
->info
.tess
.primitive_mode
;
904 case GL_TESS_GEN_SPACING
:
907 if (check_tes_query(ctx
, shProg
)) {
908 const struct gl_linked_shader
*tes
=
909 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
910 switch (tes
->Program
->info
.tess
.spacing
) {
911 case TESS_SPACING_EQUAL
:
914 case TESS_SPACING_FRACTIONAL_ODD
:
915 *params
= GL_FRACTIONAL_ODD
;
917 case TESS_SPACING_FRACTIONAL_EVEN
:
918 *params
= GL_FRACTIONAL_EVEN
;
920 case TESS_SPACING_UNSPECIFIED
:
926 case GL_TESS_GEN_VERTEX_ORDER
:
929 if (check_tes_query(ctx
, shProg
)) {
930 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
931 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
934 case GL_TESS_GEN_POINT_MODE
:
937 if (check_tes_query(ctx
, shProg
)) {
938 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
939 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
946 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
947 _mesa_enum_to_string(pname
));
952 * glGetShaderiv() - get GLSL shader state
955 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
957 struct gl_shader
*shader
=
958 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
966 *params
= shader
->Type
;
968 case GL_DELETE_STATUS
:
969 *params
= shader
->DeletePending
;
971 case GL_COMPLETION_STATUS_ARB
:
972 /* _mesa_glsl_compile_shader is not offloaded to other threads. */
975 case GL_COMPILE_STATUS
:
976 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
978 case GL_INFO_LOG_LENGTH
:
979 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
980 strlen(shader
->InfoLog
) + 1 : 0;
982 case GL_SHADER_SOURCE_LENGTH
:
983 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
985 case GL_SPIR_V_BINARY_ARB
:
986 *params
= (shader
->spirv_data
!= NULL
);
989 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
996 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
997 GLsizei
*length
, GLchar
*infoLog
)
999 struct gl_shader_program
*shProg
;
1001 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1002 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1004 * "If a negative number is provided where an argument of type sizei or
1005 * sizeiptr is specified, an INVALID_VALUE error is generated."
1008 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
1012 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1013 "glGetProgramInfoLog(program)");
1018 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
1023 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
1024 GLsizei
*length
, GLchar
*infoLog
)
1026 struct gl_shader
*sh
;
1028 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1029 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1031 * "If a negative number is provided where an argument of type sizei or
1032 * sizeiptr is specified, an INVALID_VALUE error is generated."
1035 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
1039 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
1044 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1049 * Return shader source code.
1052 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1053 GLsizei
*length
, GLchar
*sourceOut
)
1055 struct gl_shader
*sh
;
1057 if (maxLength
< 0) {
1058 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1062 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1066 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1071 * Set/replace shader source code. A helper function used by
1072 * glShaderSource[ARB].
1075 set_shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1079 /* The GL_ARB_gl_spirv spec adds the following to the end of the description
1082 * "If <shader> was previously associated with a SPIR-V module (via the
1083 * ShaderBinary command), that association is broken. Upon successful
1084 * completion of this command the SPIR_V_BINARY_ARB state of <shader>
1087 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
1089 if (sh
->CompileStatus
== COMPILE_SKIPPED
&& !sh
->FallbackSource
) {
1090 /* If shader was previously compiled back-up the source in case of cache
1093 sh
->FallbackSource
= sh
->Source
;
1094 sh
->Source
= source
;
1096 /* free old shader source string and install new one */
1097 free((void *)sh
->Source
);
1098 sh
->Source
= source
;
1102 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1111 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1116 /* The GL_ARB_gl_spirv spec says:
1118 * "Add a new error for the CompileShader command:
1120 * An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB
1121 * state of <shader> is TRUE."
1123 if (sh
->spirv_data
) {
1124 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCompileShader(SPIR-V)");
1129 /* If the user called glCompileShader without first calling
1130 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1132 sh
->CompileStatus
= COMPILE_FAILURE
;
1134 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1135 _mesa_log("GLSL source for %s shader %d:\n",
1136 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1137 _mesa_log("%s\n", sh
->Source
);
1140 /* this call will set the shader->CompileStatus field to indicate if
1141 * compilation was successful.
1143 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1145 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1146 _mesa_write_shader_to_file(sh
);
1149 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1150 if (sh
->CompileStatus
) {
1152 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1153 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1155 _mesa_log("No GLSL IR for shader %d (shader may be from "
1156 "cache)\n", sh
->Name
);
1160 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1162 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1163 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1164 _mesa_log("%s\n", sh
->InfoLog
);
1169 if (!sh
->CompileStatus
) {
1170 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1171 _mesa_log("GLSL source for %s shader %d:\n",
1172 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1173 _mesa_log("%s\n", sh
->Source
);
1174 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1177 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1178 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1179 sh
->Name
, sh
->InfoLog
);
1186 * Link a program's shaders.
1188 static ALWAYS_INLINE
void
1189 link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1196 /* From the ARB_transform_feedback2 specification:
1197 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1198 * is the name of a program being used by one or more transform feedback
1199 * objects, even if the objects are not currently bound or are paused."
1201 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1202 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1203 "glLinkProgram(transform feedback is using the program)");
1208 unsigned programs_in_use
= 0;
1210 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1211 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1212 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1213 programs_in_use
|= 1 << stage
;
1217 FLUSH_VERTICES(ctx
, 0);
1218 _mesa_glsl_link_shader(ctx
, shProg
);
1220 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1222 * "If LinkProgram or ProgramBinary successfully re-links a program
1223 * object that is active for any shader stage, then the newly generated
1224 * executable code will be installed as part of the current rendering
1225 * state for all shader stages where the program is active.
1226 * Additionally, the newly generated executable code is made part of
1227 * the state of any program pipeline for all stages where the program
1230 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1231 while (programs_in_use
) {
1232 const int stage
= u_bit_scan(&programs_in_use
);
1234 struct gl_program
*prog
= NULL
;
1235 if (shProg
->_LinkedShaders
[stage
])
1236 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1238 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1242 /* Capture .shader_test files. */
1243 const char *capture_path
= _mesa_get_shader_capture_path();
1244 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1245 /* Find an unused filename. */
1247 char *filename
= NULL
;
1248 for (unsigned i
= 0;; i
++) {
1250 filename
= ralloc_asprintf(NULL
, "%s/%u-%u.shader_test",
1251 capture_path
, shProg
->Name
, i
);
1253 filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1254 capture_path
, shProg
->Name
);
1256 file
= os_file_create_unique(filename
, 0644);
1259 /* If we are failing for another reason than "this filename already
1260 * exists", we are likely to fail again with another filename, so
1261 * let's just give up */
1262 if (errno
!= EEXIST
)
1264 ralloc_free(filename
);
1267 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1268 shProg
->IsES
? " ES" : "",
1269 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1270 if (shProg
->SeparateShader
)
1271 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1272 fprintf(file
, "\n");
1274 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1275 fprintf(file
, "[%s shader]\n%s\n",
1276 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1277 shProg
->Shaders
[i
]->Source
);
1281 _mesa_warning(ctx
, "Failed to open %s", filename
);
1284 ralloc_free(filename
);
1287 if (shProg
->data
->LinkStatus
== LINKING_FAILURE
&&
1288 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1289 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1290 shProg
->Name
, shProg
->data
->InfoLog
);
1293 _mesa_update_vertex_processing_mode(ctx
);
1295 shProg
->BinaryRetrievableHint
= shProg
->BinaryRetrievableHintPending
;
1301 printf("Link %u shaders in program %u: %s\n",
1302 shProg
->NumShaders
, shProg
->Name
,
1303 shProg
->data
->LinkStatus
? "Success" : "Failed");
1305 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1306 printf(" shader %u, stage %u\n",
1307 shProg
->Shaders
[i
]->Name
,
1308 shProg
->Shaders
[i
]->Stage
);
1315 link_program_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1317 link_program(ctx
, shProg
, false);
1322 link_program_no_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1324 link_program(ctx
, shProg
, true);
1329 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1331 link_program_error(ctx
, shProg
);
1336 * Print basic shader info (for debug).
1339 print_shader_info(const struct gl_shader_program
*shProg
)
1343 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1344 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1346 printf(" %s shader %u, checksum %u\n",
1347 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1348 shProg
->Shaders
[i
]->Name
,
1349 shProg
->Shaders
[i
]->SourceChecksum
);
1351 printf(" %s shader %u\n",
1352 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1353 shProg
->Shaders
[i
]->Name
);
1356 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1357 printf(" vert prog %u\n",
1358 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1359 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1360 printf(" frag prog %u\n",
1361 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1362 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1363 printf(" geom prog %u\n",
1364 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1365 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1366 printf(" tesc prog %u\n",
1367 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1368 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1369 printf(" tese prog %u\n",
1370 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1375 * Use the named shader program for subsequent glUniform calls
1378 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1381 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1382 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1383 "%s(program %u not linked)", caller
, shProg
->Name
);
1387 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1388 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1394 * Use the named shader program for subsequent rendering.
1397 _mesa_use_shader_program(struct gl_context
*ctx
,
1398 struct gl_shader_program
*shProg
)
1400 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1401 struct gl_program
*new_prog
= NULL
;
1402 if (shProg
&& shProg
->_LinkedShaders
[i
])
1403 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1404 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1406 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1411 * Do validation of the given shader program.
1412 * \param errMsg returns error message if validation fails.
1413 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1416 validate_shader_program(const struct gl_shader_program
*shProg
,
1419 if (!shProg
->data
->LinkStatus
) {
1423 /* From the GL spec, a program is invalid if any of these are true:
1425 any two active samplers in the current program object are of
1426 different types, but refer to the same texture image unit,
1428 any active sampler in the current program object refers to a texture
1429 image unit where fixed-function fragment processing accesses a
1430 texture target that does not match the sampler type, or
1432 the sum of the number of active samplers in the program and the
1433 number of texture image units enabled for fixed-function fragment
1434 processing exceeds the combined limit on the total number of texture
1435 image units allowed.
1439 * Check: any two active samplers in the current program object are of
1440 * different types, but refer to the same texture image unit,
1442 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1450 * Called via glValidateProgram()
1453 validate_program(struct gl_context
*ctx
, GLuint program
)
1455 struct gl_shader_program
*shProg
;
1456 char errMsg
[100] = "";
1458 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1463 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1464 if (!shProg
->data
->Validated
) {
1465 /* update info log */
1466 if (shProg
->data
->InfoLog
) {
1467 ralloc_free(shProg
->data
->InfoLog
);
1469 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1475 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1477 GET_CURRENT_CONTEXT(ctx
);
1478 attach_shader_no_error(ctx
, program
, shader
);
1483 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1485 GET_CURRENT_CONTEXT(ctx
);
1486 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1491 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1493 GET_CURRENT_CONTEXT(ctx
);
1494 attach_shader_no_error(ctx
, program
, shader
);
1499 _mesa_AttachShader(GLuint program
, GLuint shader
)
1501 GET_CURRENT_CONTEXT(ctx
);
1502 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1507 _mesa_CompileShader(GLuint shaderObj
)
1509 GET_CURRENT_CONTEXT(ctx
);
1510 if (MESA_VERBOSE
& VERBOSE_API
)
1511 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1512 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1513 "glCompileShader"));
1518 _mesa_CreateShader_no_error(GLenum type
)
1520 GET_CURRENT_CONTEXT(ctx
);
1521 return create_shader(ctx
, type
);
1526 _mesa_CreateShader(GLenum type
)
1528 GET_CURRENT_CONTEXT(ctx
);
1530 if (MESA_VERBOSE
& VERBOSE_API
)
1531 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1533 return create_shader_err(ctx
, type
, "glCreateShader");
1537 GLhandleARB GLAPIENTRY
1538 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1540 GET_CURRENT_CONTEXT(ctx
);
1541 return create_shader(ctx
, type
);
1545 GLhandleARB GLAPIENTRY
1546 _mesa_CreateShaderObjectARB(GLenum type
)
1548 GET_CURRENT_CONTEXT(ctx
);
1549 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1554 _mesa_CreateProgram(void)
1556 GET_CURRENT_CONTEXT(ctx
);
1557 if (MESA_VERBOSE
& VERBOSE_API
)
1558 _mesa_debug(ctx
, "glCreateProgram\n");
1559 return create_shader_program(ctx
);
1563 GLhandleARB GLAPIENTRY
1564 _mesa_CreateProgramObjectARB(void)
1566 GET_CURRENT_CONTEXT(ctx
);
1567 return create_shader_program(ctx
);
1572 _mesa_DeleteObjectARB(GLhandleARB obj
)
1574 if (MESA_VERBOSE
& VERBOSE_API
) {
1575 GET_CURRENT_CONTEXT(ctx
);
1576 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1580 GET_CURRENT_CONTEXT(ctx
);
1581 FLUSH_VERTICES(ctx
, 0);
1582 if (is_program(ctx
, obj
)) {
1583 delete_shader_program(ctx
, obj
);
1585 else if (is_shader(ctx
, obj
)) {
1586 delete_shader(ctx
, obj
);
1596 _mesa_DeleteProgram(GLuint name
)
1599 GET_CURRENT_CONTEXT(ctx
);
1600 FLUSH_VERTICES(ctx
, 0);
1601 delete_shader_program(ctx
, name
);
1607 _mesa_DeleteShader(GLuint name
)
1610 GET_CURRENT_CONTEXT(ctx
);
1611 FLUSH_VERTICES(ctx
, 0);
1612 delete_shader(ctx
, name
);
1618 _mesa_DetachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1620 GET_CURRENT_CONTEXT(ctx
);
1621 detach_shader_no_error(ctx
, program
, shader
);
1626 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1628 GET_CURRENT_CONTEXT(ctx
);
1629 detach_shader_error(ctx
, program
, shader
);
1634 _mesa_DetachShader_no_error(GLuint program
, GLuint shader
)
1636 GET_CURRENT_CONTEXT(ctx
);
1637 detach_shader_no_error(ctx
, program
, shader
);
1642 _mesa_DetachShader(GLuint program
, GLuint shader
)
1644 GET_CURRENT_CONTEXT(ctx
);
1645 detach_shader_error(ctx
, program
, shader
);
1650 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1651 GLsizei
* count
, GLhandleARB
* obj
)
1653 GET_CURRENT_CONTEXT(ctx
);
1654 get_attached_shaders(ctx
, (GLuint
)container
, maxCount
, count
, NULL
, obj
);
1659 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1660 GLsizei
*count
, GLuint
*obj
)
1662 GET_CURRENT_CONTEXT(ctx
);
1663 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
, NULL
);
1668 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1669 GLcharARB
* infoLog
)
1671 GET_CURRENT_CONTEXT(ctx
);
1672 if (is_program(ctx
, object
)) {
1673 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1675 else if (is_shader(ctx
, object
)) {
1676 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1679 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1685 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1687 GET_CURRENT_CONTEXT(ctx
);
1688 /* Implement in terms of GetProgramiv, GetShaderiv */
1689 if (is_program(ctx
, object
)) {
1690 if (pname
== GL_OBJECT_TYPE_ARB
) {
1691 *params
= GL_PROGRAM_OBJECT_ARB
;
1694 get_programiv(ctx
, object
, pname
, params
);
1697 else if (is_shader(ctx
, object
)) {
1698 if (pname
== GL_OBJECT_TYPE_ARB
) {
1699 *params
= GL_SHADER_OBJECT_ARB
;
1702 get_shaderiv(ctx
, object
, pname
, params
);
1706 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1712 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1715 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1716 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1717 params
[0] = (GLfloat
) iparams
[0];
1722 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1724 GET_CURRENT_CONTEXT(ctx
);
1725 get_programiv(ctx
, program
, pname
, params
);
1730 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1732 GET_CURRENT_CONTEXT(ctx
);
1733 get_shaderiv(ctx
, shader
, pname
, params
);
1738 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1739 GLsizei
*length
, GLchar
*infoLog
)
1741 GET_CURRENT_CONTEXT(ctx
);
1742 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1747 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1748 GLsizei
*length
, GLchar
*infoLog
)
1750 GET_CURRENT_CONTEXT(ctx
);
1751 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1756 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1757 GLsizei
*length
, GLchar
*sourceOut
)
1759 GET_CURRENT_CONTEXT(ctx
);
1760 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1764 GLhandleARB GLAPIENTRY
1765 _mesa_GetHandleARB(GLenum pname
)
1767 GET_CURRENT_CONTEXT(ctx
);
1768 return get_handle(ctx
, pname
);
1772 GLboolean GLAPIENTRY
1773 _mesa_IsProgram(GLuint name
)
1775 GET_CURRENT_CONTEXT(ctx
);
1776 return is_program(ctx
, name
);
1780 GLboolean GLAPIENTRY
1781 _mesa_IsShader(GLuint name
)
1783 GET_CURRENT_CONTEXT(ctx
);
1784 return is_shader(ctx
, name
);
1789 _mesa_LinkProgram_no_error(GLuint programObj
)
1791 GET_CURRENT_CONTEXT(ctx
);
1793 struct gl_shader_program
*shProg
=
1794 _mesa_lookup_shader_program(ctx
, programObj
);
1795 link_program_no_error(ctx
, shProg
);
1800 _mesa_LinkProgram(GLuint programObj
)
1802 GET_CURRENT_CONTEXT(ctx
);
1804 if (MESA_VERBOSE
& VERBOSE_API
)
1805 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1807 struct gl_shader_program
*shProg
=
1808 _mesa_lookup_shader_program_err(ctx
, programObj
, "glLinkProgram");
1809 link_program_error(ctx
, shProg
);
1812 #ifdef ENABLE_SHADER_CACHE
1814 * Generate a SHA-1 hash value string for given source string.
1817 generate_sha1(const char *source
, char sha_str
[64])
1819 unsigned char sha
[20];
1820 _mesa_sha1_compute(source
, strlen(source
), sha
);
1821 _mesa_sha1_format(sha_str
, sha
);
1825 * Construct a full path for shader replacement functionality using
1828 * <path>/<stage prefix>_<CHECKSUM>.glsl
1829 * <path>/<stage prefix>_<CHECKSUM>.arb
1832 construct_name(const gl_shader_stage stage
, const char *source
,
1836 static const char *types
[] = {
1837 "VS", "TC", "TE", "GS", "FS", "CS",
1840 const char *format
= strncmp(source
, "!!ARB", 5) ? "glsl" : "arb";
1842 generate_sha1(source
, sha
);
1843 return ralloc_asprintf(NULL
, "%s/%s_%s.%s", path
, types
[stage
], sha
, format
);
1847 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1850 _mesa_dump_shader_source(const gl_shader_stage stage
, const char *source
)
1852 static bool path_exists
= true;
1859 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1861 path_exists
= false;
1865 char *name
= construct_name(stage
, source
, dump_path
);
1867 f
= fopen(name
, "w");
1872 GET_CURRENT_CONTEXT(ctx
);
1873 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1880 * Read shader source code from a file.
1881 * Useful for debugging to override an app's shader.
1884 _mesa_read_shader_source(const gl_shader_stage stage
, const char *source
)
1887 static bool path_exists
= true;
1888 int len
, shader_size
= 0;
1895 read_path
= getenv("MESA_SHADER_READ_PATH");
1897 path_exists
= false;
1901 char *name
= construct_name(stage
, source
, read_path
);
1902 f
= fopen(name
, "r");
1907 /* allocate enough room for the entire shader */
1908 fseek(f
, 0, SEEK_END
);
1909 shader_size
= ftell(f
);
1911 assert(shader_size
);
1913 /* add one for terminating zero */
1916 buffer
= malloc(shader_size
);
1919 len
= fread(buffer
, 1, shader_size
, f
);
1927 #endif /* ENABLE_SHADER_CACHE */
1930 * Called via glShaderSource() and glShaderSourceARB() API functions.
1931 * Basically, concatenate the source code strings into one long string
1932 * and pass it to _mesa_shader_source().
1934 static ALWAYS_INLINE
void
1935 shader_source(struct gl_context
*ctx
, GLuint shaderObj
, GLsizei count
,
1936 const GLchar
*const *string
, const GLint
*length
, bool no_error
)
1939 GLsizei i
, totalLength
;
1941 struct gl_shader
*sh
;
1944 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1948 if (string
== NULL
) {
1949 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1953 sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1957 * This array holds offsets of where the appropriate string ends, thus the
1958 * last element will be set to the total length of the source code.
1960 offsets
= malloc(count
* sizeof(GLint
));
1961 if (offsets
== NULL
) {
1962 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1966 for (i
= 0; i
< count
; i
++) {
1967 if (!no_error
&& string
[i
] == NULL
) {
1968 free((GLvoid
*) offsets
);
1969 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1970 "glShaderSourceARB(null string)");
1973 if (length
== NULL
|| length
[i
] < 0)
1974 offsets
[i
] = strlen(string
[i
]);
1976 offsets
[i
] = length
[i
];
1977 /* accumulate string lengths */
1979 offsets
[i
] += offsets
[i
- 1];
1982 /* Total length of source string is sum off all strings plus two.
1983 * One extra byte for terminating zero, another extra byte to silence
1984 * valgrind warnings in the parser/grammer code.
1986 totalLength
= offsets
[count
- 1] + 2;
1987 source
= malloc(totalLength
* sizeof(GLcharARB
));
1988 if (source
== NULL
) {
1989 free((GLvoid
*) offsets
);
1990 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1994 for (i
= 0; i
< count
; i
++) {
1995 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1996 memcpy(source
+ start
, string
[i
],
1997 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1999 source
[totalLength
- 1] = '\0';
2000 source
[totalLength
- 2] = '\0';
2002 #ifdef ENABLE_SHADER_CACHE
2003 GLcharARB
*replacement
;
2005 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
2006 * if corresponding entry found from MESA_SHADER_READ_PATH.
2008 _mesa_dump_shader_source(sh
->Stage
, source
);
2010 replacement
= _mesa_read_shader_source(sh
->Stage
, source
);
2013 source
= replacement
;
2015 #endif /* ENABLE_SHADER_CACHE */
2017 set_shader_source(sh
, source
);
2024 _mesa_ShaderSource_no_error(GLuint shaderObj
, GLsizei count
,
2025 const GLchar
*const *string
, const GLint
*length
)
2027 GET_CURRENT_CONTEXT(ctx
);
2028 shader_source(ctx
, shaderObj
, count
, string
, length
, true);
2033 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
2034 const GLchar
*const *string
, const GLint
*length
)
2036 GET_CURRENT_CONTEXT(ctx
);
2037 shader_source(ctx
, shaderObj
, count
, string
, length
, false);
2041 static ALWAYS_INLINE
void
2042 use_program(GLuint program
, bool no_error
)
2044 GET_CURRENT_CONTEXT(ctx
);
2045 struct gl_shader_program
*shProg
= NULL
;
2047 if (MESA_VERBOSE
& VERBOSE_API
)
2048 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
2052 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2055 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
2056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2057 "glUseProgram(transform feedback active)");
2063 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
2067 if (!shProg
->data
->LinkStatus
) {
2068 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2069 "glUseProgram(program %u not linked)", program
);
2074 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
2075 print_shader_info(shProg
);
2080 /* The ARB_separate_shader_object spec says:
2082 * "The executable code for an individual shader stage is taken from
2083 * the current program for that stage. If there is a current program
2084 * object established by UseProgram, that program is considered current
2085 * for all stages. Otherwise, if there is a bound program pipeline
2086 * object (section 2.14.PPO), the program bound to the appropriate
2087 * stage of the pipeline object is considered current."
2090 /* Attach shader state to the binding point */
2091 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
2092 /* Update the program */
2093 _mesa_use_shader_program(ctx
, shProg
);
2095 /* Must be done first: detach the progam */
2096 _mesa_use_shader_program(ctx
, shProg
);
2097 /* Unattach shader_state binding point */
2098 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
2099 ctx
->Pipeline
.Default
);
2100 /* If a pipeline was bound, rebind it */
2101 if (ctx
->Pipeline
.Current
) {
2103 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
2105 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
2109 _mesa_update_vertex_processing_mode(ctx
);
2114 _mesa_UseProgram_no_error(GLuint program
)
2116 use_program(program
, true);
2121 _mesa_UseProgram(GLuint program
)
2123 use_program(program
, false);
2128 _mesa_ValidateProgram(GLuint program
)
2130 GET_CURRENT_CONTEXT(ctx
);
2131 validate_program(ctx
, program
);
2136 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2139 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
2140 GLint
* range
, GLint
* precision
)
2142 const struct gl_program_constants
*limits
;
2143 const struct gl_precision
*p
;
2144 GET_CURRENT_CONTEXT(ctx
);
2146 switch (shadertype
) {
2147 case GL_VERTEX_SHADER
:
2148 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
2150 case GL_FRAGMENT_SHADER
:
2151 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
2154 _mesa_error(ctx
, GL_INVALID_ENUM
,
2155 "glGetShaderPrecisionFormat(shadertype)");
2159 switch (precisiontype
) {
2161 p
= &limits
->LowFloat
;
2163 case GL_MEDIUM_FLOAT
:
2164 p
= &limits
->MediumFloat
;
2167 p
= &limits
->HighFloat
;
2170 p
= &limits
->LowInt
;
2173 p
= &limits
->MediumInt
;
2176 p
= &limits
->HighInt
;
2179 _mesa_error(ctx
, GL_INVALID_ENUM
,
2180 "glGetShaderPrecisionFormat(precisiontype)");
2184 range
[0] = p
->RangeMin
;
2185 range
[1] = p
->RangeMax
;
2186 precision
[0] = p
->Precision
;
2191 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2194 _mesa_ReleaseShaderCompiler(void)
2196 _mesa_destroy_shader_compiler_caches();
2201 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2204 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2205 const void* binary
, GLint length
)
2207 GET_CURRENT_CONTEXT(ctx
);
2208 struct gl_shader
**sh
;
2210 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2211 * page 88 of the OpenGL 4.5 specs state:
2213 * "An INVALID_VALUE error is generated if count or length is negative.
2214 * An INVALID_ENUM error is generated if binaryformat is not a supported
2215 * format returned in SHADER_BINARY_FORMATS."
2217 if (n
< 0 || length
< 0) {
2218 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2222 /* Get all shader objects at once so we can make the operation
2225 if (n
> SIZE_MAX
/ sizeof(*sh
)) {
2226 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary(count)");
2230 sh
= alloca(sizeof(*sh
) * (size_t)n
);
2232 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary");
2236 for (int i
= 0; i
< n
; ++i
) {
2237 sh
[i
] = _mesa_lookup_shader_err(ctx
, shaders
[i
], "glShaderBinary");
2242 if (binaryformat
== GL_SHADER_BINARY_FORMAT_SPIR_V_ARB
) {
2243 if (!ctx
->Extensions
.ARB_gl_spirv
) {
2244 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary(SPIR-V)");
2246 _mesa_spirv_shader_binary(ctx
, (unsigned) n
, sh
, binary
,
2253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2258 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2259 GLenum
*binaryFormat
, GLvoid
*binary
)
2261 struct gl_shader_program
*shProg
;
2262 GLsizei length_dummy
;
2263 GET_CURRENT_CONTEXT(ctx
);
2266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2270 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2274 /* The ARB_get_program_binary spec says:
2276 * "If <length> is NULL, then no length is returned."
2278 * Ensure that length always points to valid storage to avoid multiple NULL
2279 * pointer checks below.
2282 length
= &length_dummy
;
2285 /* The ARB_get_program_binary spec says:
2287 * "When a program object's LINK_STATUS is FALSE, its program binary
2288 * length is zero, and a call to GetProgramBinary will generate an
2289 * INVALID_OPERATION error.
2291 if (!shProg
->data
->LinkStatus
) {
2292 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2293 "glGetProgramBinary(program %u not linked)",
2299 if (ctx
->Const
.NumProgramBinaryFormats
== 0) {
2301 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2302 "glGetProgramBinary(driver supports zero binary formats)");
2304 _mesa_get_program_binary(ctx
, shProg
, bufSize
, length
, binaryFormat
,
2306 assert(*length
== 0 || *binaryFormat
== GL_PROGRAM_BINARY_FORMAT_MESA
);
2311 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2312 const GLvoid
*binary
, GLsizei length
)
2314 struct gl_shader_program
*shProg
;
2315 GET_CURRENT_CONTEXT(ctx
);
2317 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2321 _mesa_clear_shader_program_data(ctx
, shProg
);
2322 shProg
->data
= _mesa_create_shader_program_data();
2324 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2326 * "If a negative number is provided where an argument of type sizei or
2327 * sizeiptr is specified, an INVALID_VALUE error is generated."
2330 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2334 if (ctx
->Const
.NumProgramBinaryFormats
== 0 ||
2335 binaryFormat
!= GL_PROGRAM_BINARY_FORMAT_MESA
) {
2336 /* The ARB_get_program_binary spec says:
2338 * "<binaryFormat> and <binary> must be those returned by a previous
2339 * call to GetProgramBinary, and <length> must be the length of the
2340 * program binary as returned by GetProgramBinary or GetProgramiv with
2341 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2342 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2345 * Since any value of binaryFormat passed "is not one of those specified as
2346 * allowable for [this] command, an INVALID_ENUM error is generated."
2348 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2349 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2351 _mesa_program_binary(ctx
, shProg
, binaryFormat
, binary
, length
);
2356 static ALWAYS_INLINE
void
2357 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2358 GLuint pname
, GLint value
, bool no_error
)
2361 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2362 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2363 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2364 * even be in the dispatch table, so we shouldn't need to expclicitly
2367 * On desktop, we ignore the 3.0+ requirement because it is silly.
2370 /* The ARB_get_program_binary extension spec says:
2372 * "An INVALID_VALUE error is generated if the <value> argument to
2373 * ProgramParameteri is not TRUE or FALSE."
2375 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2379 /* No need to notify the driver. Any changes will actually take effect
2380 * the next time the shader is linked.
2382 * The ARB_get_program_binary extension spec says:
2384 * "To indicate that a program binary is likely to be retrieved,
2385 * ProgramParameteri should be called with <pname>
2386 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2387 * will not be in effect until the next time LinkProgram or
2388 * ProgramBinary has been called successfully."
2390 * The resolution of issue 9 in the extension spec also says:
2392 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2393 * to indicate to the GL implementation that this program will
2394 * likely be saved with GetProgramBinary at some point. This will
2395 * give the GL implementation the opportunity to track any state
2396 * changes made to the program before being saved such that when it
2397 * is loaded again a recompile can be avoided."
2399 shProg
->BinaryRetrievableHintPending
= value
;
2402 case GL_PROGRAM_SEPARABLE
:
2403 /* Spec imply that the behavior is the same as ARB_get_program_binary
2404 * Chapter 7.3 Program Objects
2406 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2409 shProg
->SeparateShader
= value
;
2414 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2415 _mesa_enum_to_string(pname
));
2421 _mesa_error(ctx
, GL_INVALID_VALUE
,
2422 "glProgramParameteri(pname=%s, value=%d): "
2423 "value must be 0 or 1.",
2424 _mesa_enum_to_string(pname
),
2430 _mesa_ProgramParameteri_no_error(GLuint program
, GLenum pname
, GLint value
)
2432 GET_CURRENT_CONTEXT(ctx
);
2434 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
2435 program_parameteri(ctx
, shProg
, pname
, value
, true);
2440 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2442 struct gl_shader_program
*shProg
;
2443 GET_CURRENT_CONTEXT(ctx
);
2445 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2446 "glProgramParameteri");
2450 program_parameteri(ctx
, shProg
, pname
, value
, false);
2455 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2456 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2457 struct gl_pipeline_object
*shTarget
)
2459 struct gl_program
**target
;
2461 target
= &shTarget
->CurrentProgram
[stage
];
2463 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2466 if (*target
!= prog
) {
2467 /* Program is current, flush it */
2468 if (shTarget
== ctx
->_Shader
) {
2469 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2472 _mesa_reference_shader_program(ctx
,
2473 &shTarget
->ReferencedPrograms
[stage
],
2475 _mesa_reference_program(ctx
, target
, prog
);
2476 if (stage
== MESA_SHADER_VERTEX
)
2477 _mesa_update_vertex_processing_mode(ctx
);
2485 * Copy program-specific data generated by linking from the gl_shader_program
2486 * object to the gl_program object referred to by the gl_linked_shader.
2488 * This function expects _mesa_reference_program() to have been previously
2489 * called setting the gl_linked_shaders program reference.
2492 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2493 struct gl_linked_shader
*dst_sh
)
2495 assert(dst_sh
->Program
);
2497 struct gl_program
*dst
= dst_sh
->Program
;
2499 dst
->info
.separate_shader
= src
->SeparateShader
;
2501 switch (dst_sh
->Stage
) {
2502 case MESA_SHADER_GEOMETRY
: {
2503 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2504 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2505 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2508 case MESA_SHADER_FRAGMENT
: {
2509 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2512 case MESA_SHADER_COMPUTE
: {
2513 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2522 * ARB_separate_shader_objects: Compile & Link Program
2525 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2526 const GLchar
* const *strings
)
2528 GET_CURRENT_CONTEXT(ctx
);
2530 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2534 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2535 * GL_INVALID_VALUE should be generated if count < 0
2538 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2543 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2545 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2546 _mesa_compile_shader(ctx
, sh
);
2548 program
= create_shader_program(ctx
);
2550 struct gl_shader_program
*shProg
;
2551 GLint compiled
= GL_FALSE
;
2553 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2555 shProg
->SeparateShader
= GL_TRUE
;
2557 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2559 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2560 _mesa_link_program(ctx
, shProg
);
2561 detach_shader_error(ctx
, program
, shader
);
2565 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2566 append
-error
-to
-info
-log
;
2567 shProg
->data
->LinkStatus
= LINKING_FAILURE
;
2572 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2575 delete_shader(ctx
, shader
);
2583 * For GL_ARB_tessellation_shader
2586 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2588 GET_CURRENT_CONTEXT(ctx
);
2589 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2593 extern void GLAPIENTRY
2594 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2596 GET_CURRENT_CONTEXT(ctx
);
2598 if (!_mesa_has_tessellation(ctx
)) {
2599 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2603 if (pname
!= GL_PATCH_VERTICES
) {
2604 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2608 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2609 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2613 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2617 extern void GLAPIENTRY
2618 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2620 GET_CURRENT_CONTEXT(ctx
);
2622 if (!_mesa_has_tessellation(ctx
)) {
2623 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2628 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2629 FLUSH_VERTICES(ctx
, 0);
2630 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2631 4 * sizeof(GLfloat
));
2632 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2634 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2635 FLUSH_VERTICES(ctx
, 0);
2636 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2637 2 * sizeof(GLfloat
));
2638 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2641 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2647 * ARB_shader_subroutine
2650 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2653 GET_CURRENT_CONTEXT(ctx
);
2654 const char *api_name
= "glGetSubroutineUniformLocation";
2655 struct gl_shader_program
*shProg
;
2656 GLenum resource_type
;
2657 gl_shader_stage stage
;
2659 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2660 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2664 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2668 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2669 if (!shProg
->_LinkedShaders
[stage
]) {
2670 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2674 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2675 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2679 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2682 GET_CURRENT_CONTEXT(ctx
);
2683 const char *api_name
= "glGetSubroutineIndex";
2684 struct gl_shader_program
*shProg
;
2685 struct gl_program_resource
*res
;
2686 GLenum resource_type
;
2687 gl_shader_stage stage
;
2689 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2690 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2694 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2698 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2699 if (!shProg
->_LinkedShaders
[stage
]) {
2700 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2704 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2705 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2710 return _mesa_program_resource_index(shProg
, res
);
2715 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2716 GLuint index
, GLenum pname
, GLint
*values
)
2718 GET_CURRENT_CONTEXT(ctx
);
2719 const char *api_name
= "glGetActiveSubroutineUniformiv";
2720 struct gl_shader_program
*shProg
;
2721 struct gl_linked_shader
*sh
;
2722 gl_shader_stage stage
;
2723 struct gl_program_resource
*res
;
2724 const struct gl_uniform_storage
*uni
;
2725 GLenum resource_type
;
2728 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2729 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2733 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2737 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2738 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2740 sh
= shProg
->_LinkedShaders
[stage
];
2742 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2746 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2747 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2748 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2753 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2754 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2757 values
[0] = uni
->num_compatible_subroutines
;
2761 case GL_COMPATIBLE_SUBROUTINES
: {
2762 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2766 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2767 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2768 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2769 if (fn
->types
[j
] == uni
->type
) {
2770 values
[count
++] = i
;
2778 case GL_UNIFORM_SIZE
:
2779 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2782 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2785 case GL_UNIFORM_NAME_LENGTH
:
2786 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2788 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2789 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2793 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2800 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2801 GLuint index
, GLsizei bufsize
,
2802 GLsizei
*length
, GLchar
*name
)
2804 GET_CURRENT_CONTEXT(ctx
);
2805 const char *api_name
= "glGetActiveSubroutineUniformName";
2806 struct gl_shader_program
*shProg
;
2807 GLenum resource_type
;
2808 gl_shader_stage stage
;
2810 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2811 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2815 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2819 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2820 if (!shProg
->_LinkedShaders
[stage
]) {
2821 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2825 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2826 /* get program resource name */
2827 _mesa_get_program_resource_name(shProg
, resource_type
,
2829 length
, name
, api_name
);
2834 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2835 GLuint index
, GLsizei bufsize
,
2836 GLsizei
*length
, GLchar
*name
)
2838 GET_CURRENT_CONTEXT(ctx
);
2839 const char *api_name
= "glGetActiveSubroutineName";
2840 struct gl_shader_program
*shProg
;
2841 GLenum resource_type
;
2842 gl_shader_stage stage
;
2844 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2845 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2849 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2853 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2854 if (!shProg
->_LinkedShaders
[stage
]) {
2855 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2858 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2859 _mesa_get_program_resource_name(shProg
, resource_type
,
2861 length
, name
, api_name
);
2865 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2866 const GLuint
*indices
)
2868 GET_CURRENT_CONTEXT(ctx
);
2869 const char *api_name
= "glUniformSubroutinesuiv";
2870 gl_shader_stage stage
;
2873 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2874 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2878 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2879 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2881 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2885 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2886 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2891 bool flushed
= false;
2893 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2900 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2904 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2907 for (j
= i
; j
< i
+ uni_count
; j
++) {
2908 struct gl_subroutine_function
*subfn
= NULL
;
2909 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2910 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2914 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2915 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2916 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2923 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2924 if (subfn
->types
[k
] == uni
->type
)
2927 if (k
== subfn
->num_compat_types
) {
2928 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2932 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2940 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2943 GET_CURRENT_CONTEXT(ctx
);
2944 const char *api_name
= "glGetUniformSubroutineuiv";
2945 gl_shader_stage stage
;
2947 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2948 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2952 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2953 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2955 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2959 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2960 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2964 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2969 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2970 GLenum pname
, GLint
*values
)
2972 GET_CURRENT_CONTEXT(ctx
);
2973 const char *api_name
= "glGetProgramStageiv";
2974 struct gl_shader_program
*shProg
;
2975 struct gl_linked_shader
*sh
;
2976 gl_shader_stage stage
;
2978 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2979 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2983 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2987 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2988 sh
= shProg
->_LinkedShaders
[stage
];
2990 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2991 * INVALID_OPERATION in the case of not be linked.
2993 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2994 * same info using other specs (ARB_program_interface_query), without the
2995 * need of the program to be linked, being the value for that case 0.
2997 * But at the same time, some other methods require the program to be
2998 * linked for pname related to locations, so it would be inconsistent to
2999 * not do the same here. So we are:
3000 * * Return GL_INVALID_OPERATION if not linked only for locations.
3001 * * Setting a default value of 0, to be returned if not linked.
3005 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
3006 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
3011 struct gl_program
*p
= sh
->Program
;
3013 case GL_ACTIVE_SUBROUTINES
:
3014 values
[0] = p
->sh
.NumSubroutineFunctions
;
3016 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
3017 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
3019 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
3020 values
[0] = p
->sh
.NumSubroutineUniforms
;
3022 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
3026 GLenum resource_type
;
3027 struct gl_program_resource
*res
;
3029 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
3030 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3031 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3033 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
3038 values
[0] = max_len
;
3041 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
3045 GLenum resource_type
;
3046 struct gl_program_resource
*res
;
3048 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
3049 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3050 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3052 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
3053 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
3059 values
[0] = max_len
;
3063 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
3070 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
3074 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3075 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
3076 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
3077 if (fn
->types
[j
] == type
)
3085 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
3086 struct gl_program
*p
)
3090 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
3095 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3104 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
3105 for (j
= 0; j
< uni_count
; j
++) {
3106 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
3107 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
3110 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
3112 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
3116 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
3117 gl_shader_stage stage
)
3119 if (ctx
->_Shader
->CurrentProgram
[stage
])
3120 _mesa_shader_write_subroutine_index(ctx
,
3121 ctx
->_Shader
->CurrentProgram
[stage
]);
3125 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
3126 struct gl_program
*p
)
3130 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
3131 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
3132 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
3133 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
3134 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
3137 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3138 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3143 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);