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/dispatch.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/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"
68 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
71 _mesa_get_shader_flags(void)
73 GLbitfield flags
= 0x0;
74 const char *env
= getenv("MESA_GLSL");
77 if (strstr(env
, "dump_on_error"))
78 flags
|= GLSL_DUMP_ON_ERROR
;
79 else if (strstr(env
, "dump"))
81 if (strstr(env
, "log"))
83 if (strstr(env
, "cache_fb"))
84 flags
|= GLSL_CACHE_FALLBACK
;
85 if (strstr(env
, "cache_info"))
86 flags
|= GLSL_CACHE_INFO
;
87 if (strstr(env
, "nopvert"))
88 flags
|= GLSL_NOP_VERT
;
89 if (strstr(env
, "nopfrag"))
90 flags
|= GLSL_NOP_FRAG
;
91 if (strstr(env
, "uniform"))
92 flags
|= GLSL_UNIFORMS
;
93 if (strstr(env
, "useprog"))
94 flags
|= GLSL_USE_PROG
;
95 if (strstr(env
, "errors"))
96 flags
|= GLSL_REPORT_ERRORS
;
103 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
106 _mesa_get_shader_capture_path(void)
108 static bool read_env_var
= false;
109 static const char *path
= NULL
;
112 path
= getenv("MESA_SHADER_CAPTURE_PATH");
120 * Initialize context's shader state.
123 _mesa_init_shader_state(struct gl_context
*ctx
)
125 /* Device drivers may override these to control what kind of instructions
126 * are generated by the GLSL compiler.
128 struct gl_shader_compiler_options options
;
132 memset(&options
, 0, sizeof(options
));
133 options
.MaxUnrollIterations
= 32;
134 options
.MaxIfDepth
= UINT_MAX
;
136 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
137 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
139 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
141 if (ctx
->Shader
.Flags
!= 0)
142 ctx
->Const
.GenerateTemporaryNames
= true;
144 /* Extended for ARB_separate_shader_objects */
145 ctx
->Shader
.RefCount
= 1;
146 ctx
->TessCtrlProgram
.patch_vertices
= 3;
147 for (i
= 0; i
< 4; ++i
)
148 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
149 for (i
= 0; i
< 2; ++i
)
150 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
155 * Free the per-context shader-related state.
158 _mesa_free_shader_state(struct gl_context
*ctx
)
160 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
161 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
163 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
165 /* Extended for ARB_separate_shader_objects */
166 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
168 assert(ctx
->Shader
.RefCount
== 1);
173 * Copy string from <src> to <dst>, up to maxLength characters, returning
174 * length of <dst> in <length>.
175 * \param src the strings source
176 * \param maxLength max chars to copy
177 * \param length returns number of chars copied
178 * \param dst the string destination
181 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
182 GLsizei
*length
, const GLchar
*src
)
185 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
196 * Confirm that the a shader type is valid and supported by the implementation
198 * \param ctx Current GL context
199 * \param type Shader target
203 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
205 /* Note: when building built-in GLSL functions, this function may be
206 * invoked with ctx == NULL. In that case, we can only validate that it's
207 * a shader target we recognize, not that it's supported in the current
208 * context. But that's fine--we don't need any further validation than
209 * that when building built-in GLSL functions.
213 case GL_FRAGMENT_SHADER
:
214 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
215 case GL_VERTEX_SHADER
:
216 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
217 case GL_GEOMETRY_SHADER_ARB
:
218 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
219 case GL_TESS_CONTROL_SHADER
:
220 case GL_TESS_EVALUATION_SHADER
:
221 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
222 case GL_COMPUTE_SHADER
:
223 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
231 is_program(struct gl_context
*ctx
, GLuint name
)
233 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
234 return shProg
? GL_TRUE
: GL_FALSE
;
239 is_shader(struct gl_context
*ctx
, GLuint name
)
241 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
242 return shader
? GL_TRUE
: GL_FALSE
;
247 * Attach shader to a shader program.
250 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
251 struct gl_shader
*sh
)
253 GLuint n
= shProg
->NumShaders
;
255 shProg
->Shaders
= realloc(shProg
->Shaders
,
256 (n
+ 1) * sizeof(struct gl_shader
*));
257 if (!shProg
->Shaders
) {
258 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
263 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
264 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
265 shProg
->NumShaders
++;
269 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
272 struct gl_shader_program
*shProg
;
273 struct gl_shader
*sh
;
276 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
278 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
282 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
287 n
= shProg
->NumShaders
;
288 for (i
= 0; i
< n
; i
++) {
289 if (shProg
->Shaders
[i
] == sh
) {
290 /* The shader is already attched to this program. The
291 * GL_ARB_shader_objects spec says:
293 * "The error INVALID_OPERATION is generated by AttachObjectARB
294 * if <obj> is already attached to <containerObj>."
296 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
298 } else if (same_type_disallowed
&&
299 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
300 /* Shader with the same type is already attached to this program,
301 * OpenGL ES 2.0 and 3.0 specs say:
303 * "Multiple shader objects of the same type may not be attached
304 * to a single program object. [...] The error INVALID_OPERATION
305 * is generated if [...] another shader object of the same type
306 * as shader is already attached to program."
308 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
313 attach_shader(ctx
, shProg
, sh
);
317 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
319 struct gl_shader_program
*shProg
;
320 struct gl_shader
*sh
;
322 shProg
= _mesa_lookup_shader_program(ctx
, program
);
323 sh
= _mesa_lookup_shader(ctx
, shader
);
325 attach_shader(ctx
, shProg
, sh
);
329 create_shader(struct gl_context
*ctx
, GLenum type
)
331 struct gl_shader
*sh
;
334 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
335 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
336 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
338 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
339 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
346 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
348 if (!_mesa_validate_shader_target(ctx
, type
)) {
349 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
350 caller
, _mesa_enum_to_string(type
));
354 return create_shader(ctx
, type
);
359 create_shader_program(struct gl_context
*ctx
)
362 struct gl_shader_program
*shProg
;
364 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
366 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
368 shProg
= _mesa_new_shader_program(name
);
370 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
372 assert(shProg
->RefCount
== 1);
374 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
381 * Delete a shader program. Actually, just decrement the program's
382 * reference count and mark it as DeletePending.
383 * Used to implement glDeleteProgram() and glDeleteObjectARB().
386 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
389 * NOTE: deleting shaders/programs works a bit differently than
390 * texture objects (and buffer objects, etc). Shader/program
391 * handles/IDs exist in the hash table until the object is really
392 * deleted (refcount==0). With texture objects, the handle/ID is
393 * removed from the hash table in glDeleteTextures() while the tex
394 * object itself might linger until its refcount goes to zero.
396 struct gl_shader_program
*shProg
;
398 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
402 if (!shProg
->DeletePending
) {
403 shProg
->DeletePending
= GL_TRUE
;
405 /* effectively, decr shProg's refcount */
406 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
412 delete_shader(struct gl_context
*ctx
, GLuint shader
)
414 struct gl_shader
*sh
;
416 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
420 if (!sh
->DeletePending
) {
421 sh
->DeletePending
= GL_TRUE
;
423 /* effectively, decr sh's refcount */
424 _mesa_reference_shader(ctx
, &sh
, NULL
);
429 static ALWAYS_INLINE
void
430 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
433 struct gl_shader_program
*shProg
;
438 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
442 shProg
= _mesa_lookup_shader_program(ctx
, program
);
445 n
= shProg
->NumShaders
;
447 for (i
= 0; i
< n
; i
++) {
448 if (shProg
->Shaders
[i
]->Name
== shader
) {
450 struct gl_shader
**newList
;
453 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
455 /* alloc new, smaller array */
456 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
458 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
461 /* Copy old list entries to new list, skipping removed entry at [i] */
462 for (j
= 0; j
< i
; j
++) {
463 newList
[j
] = shProg
->Shaders
[j
];
466 newList
[j
++] = shProg
->Shaders
[i
];
469 /* Free old list and install new one */
470 free(shProg
->Shaders
);
471 shProg
->Shaders
= newList
;
472 shProg
->NumShaders
= n
- 1;
475 /* sanity check - make sure the new list's entries are sensible */
476 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
477 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
478 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
479 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
480 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
481 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
482 assert(shProg
->Shaders
[j
]->RefCount
> 0);
493 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
494 err
= GL_INVALID_OPERATION
;
496 err
= GL_INVALID_VALUE
;
497 _mesa_error(ctx
, err
, "glDetachShader(shader)");
504 detach_shader_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
506 detach_shader(ctx
, program
, shader
, false);
511 detach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
513 detach_shader(ctx
, program
, shader
, true);
518 * Return list of shaders attached to shader program.
519 * \param objOut returns GLuint ids
520 * \param handleOut returns GLhandleARB handles
523 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
524 GLsizei
*countOut
, GLuint
*objOut
, GLhandleARB
*handleOut
)
526 struct gl_shader_program
*shProg
;
529 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
534 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
538 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
540 objOut
[i
] = shProg
->Shaders
[i
]->Name
;
544 handleOut
[i
] = (GLhandleARB
) shProg
->Shaders
[i
]->Name
;
554 * glGetHandleARB() - return ID/name of currently bound shader program.
557 get_handle(struct gl_context
*ctx
, GLenum pname
)
559 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
560 if (ctx
->_Shader
->ActiveProgram
)
561 return ctx
->_Shader
->ActiveProgram
->Name
;
566 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
573 * Check if a geometry shader query is valid at this time. If not, report an
574 * error and return false.
576 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
578 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
579 * are queried for a program which has not been linked successfully, or
580 * which does not contain objects to form a geometry shader, then an
581 * INVALID_OPERATION error is generated."
584 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
586 if (shProg
->data
->LinkStatus
&&
587 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
591 _mesa_error(ctx
, GL_INVALID_OPERATION
,
592 "glGetProgramv(linked geometry shader required)");
598 * Check if a tessellation control shader query is valid at this time.
599 * If not, report an error and return false.
601 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
603 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
604 * not been linked successfully, or which does not contain objects to
605 * form a tessellation control shader, then an INVALID_OPERATION error is
609 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
611 if (shProg
->data
->LinkStatus
&&
612 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
617 "glGetProgramv(linked tessellation control shader required)");
623 * Check if a tessellation evaluation shader query is valid at this time.
624 * If not, report an error and return false.
626 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
628 * "If any of the pname values in this paragraph are queried for a program
629 * which has not been linked successfully, or which does not contain
630 * objects to form a tessellation evaluation shader, then an
631 * INVALID_OPERATION error is generated."
635 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
637 if (shProg
->data
->LinkStatus
&&
638 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
642 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
643 "evaluation shader required)");
649 * glGetProgramiv() - get shader program state.
650 * Note that this is for GLSL shader programs, not ARB vertex/fragment
651 * programs (see glGetProgramivARB).
654 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
657 struct gl_shader_program
*shProg
658 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
660 /* Is transform feedback available in this context?
663 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
664 || ctx
->API
== API_OPENGL_CORE
665 || _mesa_is_gles3(ctx
);
667 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
668 * and GL 3.2) are available in this context
670 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
671 const bool has_tess
= _mesa_has_tessellation(ctx
);
673 /* Are uniform buffer objects available in this context?
676 (ctx
->API
== API_OPENGL_COMPAT
&&
677 ctx
->Extensions
.ARB_uniform_buffer_object
)
678 || ctx
->API
== API_OPENGL_CORE
679 || _mesa_is_gles3(ctx
);
686 case GL_DELETE_STATUS
:
687 *params
= shProg
->DeletePending
;
690 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
692 case GL_VALIDATE_STATUS
:
693 *params
= shProg
->data
->Validated
;
695 case GL_INFO_LOG_LENGTH
:
696 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
697 strlen(shProg
->data
->InfoLog
) + 1 : 0;
699 case GL_ATTACHED_SHADERS
:
700 *params
= shProg
->NumShaders
;
702 case GL_ACTIVE_ATTRIBUTES
:
703 *params
= _mesa_count_active_attribs(shProg
);
705 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
706 *params
= _mesa_longest_attribute_name_length(shProg
);
708 case GL_ACTIVE_UNIFORMS
: {
710 const unsigned num_uniforms
=
711 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
712 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
713 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
718 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
721 const unsigned num_uniforms
=
722 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
724 for (i
= 0; i
< num_uniforms
; i
++) {
725 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
728 /* Add one for the terminating NUL character for a non-array, and
729 * 4 for the "[0]" and the NUL for an array.
731 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
732 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
741 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
744 *params
= shProg
->TransformFeedback
.NumVarying
;
746 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
752 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
753 /* Add one for the terminating NUL character.
756 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
765 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
768 *params
= shProg
->TransformFeedback
.BufferMode
;
770 case GL_GEOMETRY_VERTICES_OUT
:
773 if (check_gs_query(ctx
, shProg
)) {
774 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
775 Program
->info
.gs
.vertices_out
;
778 case GL_GEOMETRY_SHADER_INVOCATIONS
:
779 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
781 if (check_gs_query(ctx
, shProg
)) {
782 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
783 Program
->info
.gs
.invocations
;
786 case GL_GEOMETRY_INPUT_TYPE
:
789 if (check_gs_query(ctx
, shProg
)) {
790 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
791 Program
->info
.gs
.input_primitive
;
794 case GL_GEOMETRY_OUTPUT_TYPE
:
797 if (check_gs_query(ctx
, shProg
)) {
798 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
799 Program
->info
.gs
.output_primitive
;
802 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
809 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
810 /* Add one for the terminating NUL character.
812 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
821 case GL_ACTIVE_UNIFORM_BLOCKS
:
825 *params
= shProg
->data
->NumUniformBlocks
;
827 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
828 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
829 * only available with desktop OpenGL 3.0+ with the
830 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
832 * On desktop, we ignore the 3.0+ requirement because it is silly.
834 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
837 *params
= shProg
->BinaryRetreivableHint
;
839 case GL_PROGRAM_BINARY_LENGTH
:
840 if (ctx
->Const
.NumProgramBinaryFormats
== 0) {
843 _mesa_get_program_binary_length(ctx
, shProg
, params
);
846 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
847 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
850 *params
= shProg
->data
->NumAtomicBuffers
;
852 case GL_COMPUTE_WORK_GROUP_SIZE
: {
854 if (!_mesa_has_compute_shaders(ctx
))
856 if (!shProg
->data
->LinkStatus
) {
857 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
861 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
862 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
866 for (i
= 0; i
< 3; i
++)
867 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
868 Program
->info
.cs
.local_size
[i
];
871 case GL_PROGRAM_SEPARABLE
:
872 /* If the program has not been linked, return initial value 0. */
873 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
876 /* ARB_tessellation_shader */
877 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
880 if (check_tcs_query(ctx
, shProg
)) {
881 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
882 Program
->info
.tess
.tcs_vertices_out
;
885 case GL_TESS_GEN_MODE
:
888 if (check_tes_query(ctx
, shProg
)) {
889 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
890 Program
->info
.tess
.primitive_mode
;
893 case GL_TESS_GEN_SPACING
:
896 if (check_tes_query(ctx
, shProg
)) {
897 const struct gl_linked_shader
*tes
=
898 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
899 switch (tes
->Program
->info
.tess
.spacing
) {
900 case TESS_SPACING_EQUAL
:
903 case TESS_SPACING_FRACTIONAL_ODD
:
904 *params
= GL_FRACTIONAL_ODD
;
906 case TESS_SPACING_FRACTIONAL_EVEN
:
907 *params
= GL_FRACTIONAL_EVEN
;
909 case TESS_SPACING_UNSPECIFIED
:
915 case GL_TESS_GEN_VERTEX_ORDER
:
918 if (check_tes_query(ctx
, shProg
)) {
919 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
920 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
923 case GL_TESS_GEN_POINT_MODE
:
926 if (check_tes_query(ctx
, shProg
)) {
927 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
928 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
935 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
936 _mesa_enum_to_string(pname
));
941 * glGetShaderiv() - get GLSL shader state
944 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
946 struct gl_shader
*shader
=
947 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
955 *params
= shader
->Type
;
957 case GL_DELETE_STATUS
:
958 *params
= shader
->DeletePending
;
960 case GL_COMPILE_STATUS
:
961 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
963 case GL_INFO_LOG_LENGTH
:
964 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
965 strlen(shader
->InfoLog
) + 1 : 0;
967 case GL_SHADER_SOURCE_LENGTH
:
968 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
970 case GL_SPIR_V_BINARY_ARB
:
971 *params
= (shader
->spirv_data
!= NULL
);
974 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
981 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
982 GLsizei
*length
, GLchar
*infoLog
)
984 struct gl_shader_program
*shProg
;
986 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
987 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
989 * "If a negative number is provided where an argument of type sizei or
990 * sizeiptr is specified, an INVALID_VALUE error is generated."
993 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
997 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
998 "glGetProgramInfoLog(program)");
1003 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
1008 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
1009 GLsizei
*length
, GLchar
*infoLog
)
1011 struct gl_shader
*sh
;
1013 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
1014 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
1016 * "If a negative number is provided where an argument of type sizei or
1017 * sizeiptr is specified, an INVALID_VALUE error is generated."
1020 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
1024 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
1029 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1034 * Return shader source code.
1037 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1038 GLsizei
*length
, GLchar
*sourceOut
)
1040 struct gl_shader
*sh
;
1042 if (maxLength
< 0) {
1043 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1047 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1051 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1056 * Set/replace shader source code. A helper function used by
1057 * glShaderSource[ARB].
1060 set_shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1064 /* The GL_ARB_gl_spirv spec adds the following to the end of the description
1067 * "If <shader> was previously associated with a SPIR-V module (via the
1068 * ShaderBinary command), that association is broken. Upon successful
1069 * completion of this command the SPIR_V_BINARY_ARB state of <shader>
1072 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, NULL
);
1074 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
1075 /* If shader was previously compiled back-up the source in case of cache
1078 sh
->FallbackSource
= sh
->Source
;
1079 sh
->Source
= source
;
1081 /* free old shader source string and install new one */
1082 free((void *)sh
->Source
);
1083 sh
->Source
= source
;
1087 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1096 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1101 /* The GL_ARB_gl_spirv spec says:
1103 * "Add a new error for the CompileShader command:
1105 * An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB
1106 * state of <shader> is TRUE."
1108 if (sh
->spirv_data
) {
1109 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCompileShader(SPIR-V)");
1114 /* If the user called glCompileShader without first calling
1115 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1117 sh
->CompileStatus
= compile_failure
;
1119 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1120 _mesa_log("GLSL source for %s shader %d:\n",
1121 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1122 _mesa_log("%s\n", sh
->Source
);
1125 /* this call will set the shader->CompileStatus field to indicate if
1126 * compilation was successful.
1128 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1130 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1131 _mesa_write_shader_to_file(sh
);
1134 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1135 if (sh
->CompileStatus
) {
1137 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1138 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1140 _mesa_log("No GLSL IR for shader %d (shader may be from "
1141 "cache)\n", sh
->Name
);
1145 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1147 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1148 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1149 _mesa_log("%s\n", sh
->InfoLog
);
1154 if (!sh
->CompileStatus
) {
1155 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1156 _mesa_log("GLSL source for %s shader %d:\n",
1157 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1158 _mesa_log("%s\n", sh
->Source
);
1159 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1162 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1163 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1164 sh
->Name
, sh
->InfoLog
);
1171 * Link a program's shaders.
1173 static ALWAYS_INLINE
void
1174 link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1181 /* From the ARB_transform_feedback2 specification:
1182 * "The error INVALID_OPERATION is generated by LinkProgram if <program>
1183 * is the name of a program being used by one or more transform feedback
1184 * objects, even if the objects are not currently bound or are paused."
1186 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1187 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1188 "glLinkProgram(transform feedback is using the program)");
1193 unsigned programs_in_use
= 0;
1195 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1196 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1197 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1198 programs_in_use
|= 1 << stage
;
1202 FLUSH_VERTICES(ctx
, 0);
1203 _mesa_glsl_link_shader(ctx
, shProg
);
1205 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1207 * "If LinkProgram or ProgramBinary successfully re-links a program
1208 * object that is active for any shader stage, then the newly generated
1209 * executable code will be installed as part of the current rendering
1210 * state for all shader stages where the program is active.
1211 * Additionally, the newly generated executable code is made part of
1212 * the state of any program pipeline for all stages where the program
1215 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1216 while (programs_in_use
) {
1217 const int stage
= u_bit_scan(&programs_in_use
);
1219 struct gl_program
*prog
= NULL
;
1220 if (shProg
->_LinkedShaders
[stage
])
1221 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1223 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1227 /* Capture .shader_test files. */
1228 const char *capture_path
= _mesa_get_shader_capture_path();
1229 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1231 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1232 capture_path
, shProg
->Name
);
1233 file
= fopen(filename
, "w");
1235 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1236 shProg
->IsES
? " ES" : "",
1237 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1238 if (shProg
->SeparateShader
)
1239 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1240 fprintf(file
, "\n");
1242 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1243 fprintf(file
, "[%s shader]\n%s\n",
1244 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1245 shProg
->Shaders
[i
]->Source
);
1249 _mesa_warning(ctx
, "Failed to open %s", filename
);
1252 ralloc_free(filename
);
1255 if (shProg
->data
->LinkStatus
== linking_failure
&&
1256 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1257 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1258 shProg
->Name
, shProg
->data
->InfoLog
);
1265 printf("Link %u shaders in program %u: %s\n",
1266 shProg
->NumShaders
, shProg
->Name
,
1267 shProg
->data
->LinkStatus
? "Success" : "Failed");
1269 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1270 printf(" shader %u, stage %u\n",
1271 shProg
->Shaders
[i
]->Name
,
1272 shProg
->Shaders
[i
]->Stage
);
1279 link_program_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1281 link_program(ctx
, shProg
, false);
1286 link_program_no_error(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1288 link_program(ctx
, shProg
, true);
1293 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1295 link_program_error(ctx
, shProg
);
1300 * Print basic shader info (for debug).
1303 print_shader_info(const struct gl_shader_program
*shProg
)
1307 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1308 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1310 printf(" %s shader %u, checksum %u\n",
1311 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1312 shProg
->Shaders
[i
]->Name
,
1313 shProg
->Shaders
[i
]->SourceChecksum
);
1315 printf(" %s shader %u\n",
1316 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1317 shProg
->Shaders
[i
]->Name
);
1320 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1321 printf(" vert prog %u\n",
1322 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1323 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1324 printf(" frag prog %u\n",
1325 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1326 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1327 printf(" geom prog %u\n",
1328 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1329 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1330 printf(" tesc prog %u\n",
1331 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1332 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1333 printf(" tese prog %u\n",
1334 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1339 * Use the named shader program for subsequent glUniform calls
1342 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1345 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1346 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1347 "%s(program %u not linked)", caller
, shProg
->Name
);
1351 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1352 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1358 * Use the named shader program for subsequent rendering.
1361 _mesa_use_shader_program(struct gl_context
*ctx
,
1362 struct gl_shader_program
*shProg
)
1364 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1365 struct gl_program
*new_prog
= NULL
;
1366 if (shProg
&& shProg
->_LinkedShaders
[i
])
1367 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1368 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1370 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1375 * Do validation of the given shader program.
1376 * \param errMsg returns error message if validation fails.
1377 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1380 validate_shader_program(const struct gl_shader_program
*shProg
,
1383 if (!shProg
->data
->LinkStatus
) {
1387 /* From the GL spec, a program is invalid if any of these are true:
1389 any two active samplers in the current program object are of
1390 different types, but refer to the same texture image unit,
1392 any active sampler in the current program object refers to a texture
1393 image unit where fixed-function fragment processing accesses a
1394 texture target that does not match the sampler type, or
1396 the sum of the number of active samplers in the program and the
1397 number of texture image units enabled for fixed-function fragment
1398 processing exceeds the combined limit on the total number of texture
1399 image units allowed.
1403 * Check: any two active samplers in the current program object are of
1404 * different types, but refer to the same texture image unit,
1406 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1414 * Called via glValidateProgram()
1417 validate_program(struct gl_context
*ctx
, GLuint program
)
1419 struct gl_shader_program
*shProg
;
1420 char errMsg
[100] = "";
1422 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1427 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1428 if (!shProg
->data
->Validated
) {
1429 /* update info log */
1430 if (shProg
->data
->InfoLog
) {
1431 ralloc_free(shProg
->data
->InfoLog
);
1433 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1439 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1441 GET_CURRENT_CONTEXT(ctx
);
1442 attach_shader_no_error(ctx
, program
, shader
);
1447 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1449 GET_CURRENT_CONTEXT(ctx
);
1450 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1455 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1457 GET_CURRENT_CONTEXT(ctx
);
1458 attach_shader_no_error(ctx
, program
, shader
);
1463 _mesa_AttachShader(GLuint program
, GLuint shader
)
1465 GET_CURRENT_CONTEXT(ctx
);
1466 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1471 _mesa_CompileShader(GLuint shaderObj
)
1473 GET_CURRENT_CONTEXT(ctx
);
1474 if (MESA_VERBOSE
& VERBOSE_API
)
1475 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1476 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1477 "glCompileShader"));
1482 _mesa_CreateShader_no_error(GLenum type
)
1484 GET_CURRENT_CONTEXT(ctx
);
1485 return create_shader(ctx
, type
);
1490 _mesa_CreateShader(GLenum type
)
1492 GET_CURRENT_CONTEXT(ctx
);
1494 if (MESA_VERBOSE
& VERBOSE_API
)
1495 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1497 return create_shader_err(ctx
, type
, "glCreateShader");
1501 GLhandleARB GLAPIENTRY
1502 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1504 GET_CURRENT_CONTEXT(ctx
);
1505 return create_shader(ctx
, type
);
1509 GLhandleARB GLAPIENTRY
1510 _mesa_CreateShaderObjectARB(GLenum type
)
1512 GET_CURRENT_CONTEXT(ctx
);
1513 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1518 _mesa_CreateProgram(void)
1520 GET_CURRENT_CONTEXT(ctx
);
1521 if (MESA_VERBOSE
& VERBOSE_API
)
1522 _mesa_debug(ctx
, "glCreateProgram\n");
1523 return create_shader_program(ctx
);
1527 GLhandleARB GLAPIENTRY
1528 _mesa_CreateProgramObjectARB(void)
1530 GET_CURRENT_CONTEXT(ctx
);
1531 return create_shader_program(ctx
);
1536 _mesa_DeleteObjectARB(GLhandleARB obj
)
1538 if (MESA_VERBOSE
& VERBOSE_API
) {
1539 GET_CURRENT_CONTEXT(ctx
);
1540 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1544 GET_CURRENT_CONTEXT(ctx
);
1545 FLUSH_VERTICES(ctx
, 0);
1546 if (is_program(ctx
, obj
)) {
1547 delete_shader_program(ctx
, obj
);
1549 else if (is_shader(ctx
, obj
)) {
1550 delete_shader(ctx
, obj
);
1560 _mesa_DeleteProgram(GLuint name
)
1563 GET_CURRENT_CONTEXT(ctx
);
1564 FLUSH_VERTICES(ctx
, 0);
1565 delete_shader_program(ctx
, name
);
1571 _mesa_DeleteShader(GLuint name
)
1574 GET_CURRENT_CONTEXT(ctx
);
1575 FLUSH_VERTICES(ctx
, 0);
1576 delete_shader(ctx
, name
);
1582 _mesa_DetachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1584 GET_CURRENT_CONTEXT(ctx
);
1585 detach_shader_no_error(ctx
, program
, shader
);
1590 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1592 GET_CURRENT_CONTEXT(ctx
);
1593 detach_shader_error(ctx
, program
, shader
);
1598 _mesa_DetachShader_no_error(GLuint program
, GLuint shader
)
1600 GET_CURRENT_CONTEXT(ctx
);
1601 detach_shader_no_error(ctx
, program
, shader
);
1606 _mesa_DetachShader(GLuint program
, GLuint shader
)
1608 GET_CURRENT_CONTEXT(ctx
);
1609 detach_shader_error(ctx
, program
, shader
);
1614 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1615 GLsizei
* count
, GLhandleARB
* obj
)
1617 GET_CURRENT_CONTEXT(ctx
);
1618 get_attached_shaders(ctx
, (GLuint
)container
, maxCount
, count
, NULL
, obj
);
1623 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1624 GLsizei
*count
, GLuint
*obj
)
1626 GET_CURRENT_CONTEXT(ctx
);
1627 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
, NULL
);
1632 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1633 GLcharARB
* infoLog
)
1635 GET_CURRENT_CONTEXT(ctx
);
1636 if (is_program(ctx
, object
)) {
1637 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1639 else if (is_shader(ctx
, object
)) {
1640 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1643 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1649 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1651 GET_CURRENT_CONTEXT(ctx
);
1652 /* Implement in terms of GetProgramiv, GetShaderiv */
1653 if (is_program(ctx
, object
)) {
1654 if (pname
== GL_OBJECT_TYPE_ARB
) {
1655 *params
= GL_PROGRAM_OBJECT_ARB
;
1658 get_programiv(ctx
, object
, pname
, params
);
1661 else if (is_shader(ctx
, object
)) {
1662 if (pname
== GL_OBJECT_TYPE_ARB
) {
1663 *params
= GL_SHADER_OBJECT_ARB
;
1666 get_shaderiv(ctx
, object
, pname
, params
);
1670 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1676 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1679 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1680 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1681 params
[0] = (GLfloat
) iparams
[0];
1686 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1688 GET_CURRENT_CONTEXT(ctx
);
1689 get_programiv(ctx
, program
, pname
, params
);
1694 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1696 GET_CURRENT_CONTEXT(ctx
);
1697 get_shaderiv(ctx
, shader
, pname
, params
);
1702 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1703 GLsizei
*length
, GLchar
*infoLog
)
1705 GET_CURRENT_CONTEXT(ctx
);
1706 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1711 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1712 GLsizei
*length
, GLchar
*infoLog
)
1714 GET_CURRENT_CONTEXT(ctx
);
1715 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1720 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1721 GLsizei
*length
, GLchar
*sourceOut
)
1723 GET_CURRENT_CONTEXT(ctx
);
1724 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1728 GLhandleARB GLAPIENTRY
1729 _mesa_GetHandleARB(GLenum pname
)
1731 GET_CURRENT_CONTEXT(ctx
);
1732 return get_handle(ctx
, pname
);
1736 GLboolean GLAPIENTRY
1737 _mesa_IsProgram(GLuint name
)
1739 GET_CURRENT_CONTEXT(ctx
);
1740 return is_program(ctx
, name
);
1744 GLboolean GLAPIENTRY
1745 _mesa_IsShader(GLuint name
)
1747 GET_CURRENT_CONTEXT(ctx
);
1748 return is_shader(ctx
, name
);
1753 _mesa_LinkProgram_no_error(GLuint programObj
)
1755 GET_CURRENT_CONTEXT(ctx
);
1757 struct gl_shader_program
*shProg
=
1758 _mesa_lookup_shader_program(ctx
, programObj
);
1759 link_program_no_error(ctx
, shProg
);
1764 _mesa_LinkProgram(GLuint programObj
)
1766 GET_CURRENT_CONTEXT(ctx
);
1768 if (MESA_VERBOSE
& VERBOSE_API
)
1769 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1771 struct gl_shader_program
*shProg
=
1772 _mesa_lookup_shader_program_err(ctx
, programObj
, "glLinkProgram");
1773 link_program_error(ctx
, shProg
);
1776 #ifdef ENABLE_SHADER_CACHE
1778 * Generate a SHA-1 hash value string for given source string.
1781 generate_sha1(const char *source
, char sha_str
[64])
1783 unsigned char sha
[20];
1784 _mesa_sha1_compute(source
, strlen(source
), sha
);
1785 _mesa_sha1_format(sha_str
, sha
);
1789 * Construct a full path for shader replacement functionality using
1792 * <path>/<stage prefix>_<CHECKSUM>.glsl
1795 construct_name(const gl_shader_stage stage
, const char *source
,
1799 static const char *types
[] = {
1800 "VS", "TC", "TE", "GS", "FS", "CS",
1803 generate_sha1(source
, sha
);
1804 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1808 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1811 dump_shader(const gl_shader_stage stage
, const char *source
)
1813 static bool path_exists
= true;
1820 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1822 path_exists
= false;
1826 char *name
= construct_name(stage
, source
, dump_path
);
1828 f
= fopen(name
, "w");
1833 GET_CURRENT_CONTEXT(ctx
);
1834 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1841 * Read shader source code from a file.
1842 * Useful for debugging to override an app's shader.
1845 read_shader(const gl_shader_stage stage
, const char *source
)
1848 static bool path_exists
= true;
1849 int len
, shader_size
= 0;
1856 read_path
= getenv("MESA_SHADER_READ_PATH");
1858 path_exists
= false;
1862 char *name
= construct_name(stage
, source
, read_path
);
1863 f
= fopen(name
, "r");
1868 /* allocate enough room for the entire shader */
1869 fseek(f
, 0, SEEK_END
);
1870 shader_size
= ftell(f
);
1872 assert(shader_size
);
1874 /* add one for terminating zero */
1877 buffer
= malloc(shader_size
);
1880 len
= fread(buffer
, 1, shader_size
, f
);
1888 #endif /* ENABLE_SHADER_CACHE */
1891 * Called via glShaderSource() and glShaderSourceARB() API functions.
1892 * Basically, concatenate the source code strings into one long string
1893 * and pass it to _mesa_shader_source().
1895 static ALWAYS_INLINE
void
1896 shader_source(struct gl_context
*ctx
, GLuint shaderObj
, GLsizei count
,
1897 const GLchar
*const *string
, const GLint
*length
, bool no_error
)
1900 GLsizei i
, totalLength
;
1902 struct gl_shader
*sh
;
1905 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1909 if (string
== NULL
) {
1910 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1914 sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1918 * This array holds offsets of where the appropriate string ends, thus the
1919 * last element will be set to the total length of the source code.
1921 offsets
= malloc(count
* sizeof(GLint
));
1922 if (offsets
== NULL
) {
1923 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1927 for (i
= 0; i
< count
; i
++) {
1928 if (!no_error
&& string
[i
] == NULL
) {
1929 free((GLvoid
*) offsets
);
1930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1931 "glShaderSourceARB(null string)");
1934 if (length
== NULL
|| length
[i
] < 0)
1935 offsets
[i
] = strlen(string
[i
]);
1937 offsets
[i
] = length
[i
];
1938 /* accumulate string lengths */
1940 offsets
[i
] += offsets
[i
- 1];
1943 /* Total length of source string is sum off all strings plus two.
1944 * One extra byte for terminating zero, another extra byte to silence
1945 * valgrind warnings in the parser/grammer code.
1947 totalLength
= offsets
[count
- 1] + 2;
1948 source
= malloc(totalLength
* sizeof(GLcharARB
));
1949 if (source
== NULL
) {
1950 free((GLvoid
*) offsets
);
1951 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1955 for (i
= 0; i
< count
; i
++) {
1956 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1957 memcpy(source
+ start
, string
[i
],
1958 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1960 source
[totalLength
- 1] = '\0';
1961 source
[totalLength
- 2] = '\0';
1963 #ifdef ENABLE_SHADER_CACHE
1964 GLcharARB
*replacement
;
1966 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1967 * if corresponding entry found from MESA_SHADER_READ_PATH.
1969 dump_shader(sh
->Stage
, source
);
1971 replacement
= read_shader(sh
->Stage
, source
);
1974 source
= replacement
;
1976 #endif /* ENABLE_SHADER_CACHE */
1978 set_shader_source(sh
, source
);
1985 _mesa_ShaderSource_no_error(GLuint shaderObj
, GLsizei count
,
1986 const GLchar
*const *string
, const GLint
*length
)
1988 GET_CURRENT_CONTEXT(ctx
);
1989 shader_source(ctx
, shaderObj
, count
, string
, length
, true);
1994 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1995 const GLchar
*const *string
, const GLint
*length
)
1997 GET_CURRENT_CONTEXT(ctx
);
1998 shader_source(ctx
, shaderObj
, count
, string
, length
, false);
2002 static ALWAYS_INLINE
void
2003 use_program(GLuint program
, bool no_error
)
2005 GET_CURRENT_CONTEXT(ctx
);
2006 struct gl_shader_program
*shProg
= NULL
;
2008 if (MESA_VERBOSE
& VERBOSE_API
)
2009 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
2013 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2016 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
2017 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2018 "glUseProgram(transform feedback active)");
2024 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
2028 if (!shProg
->data
->LinkStatus
) {
2029 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2030 "glUseProgram(program %u not linked)", program
);
2035 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
2036 print_shader_info(shProg
);
2041 /* The ARB_separate_shader_object spec says:
2043 * "The executable code for an individual shader stage is taken from
2044 * the current program for that stage. If there is a current program
2045 * object established by UseProgram, that program is considered current
2046 * for all stages. Otherwise, if there is a bound program pipeline
2047 * object (section 2.14.PPO), the program bound to the appropriate
2048 * stage of the pipeline object is considered current."
2051 /* Attach shader state to the binding point */
2052 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
2053 /* Update the program */
2054 _mesa_use_shader_program(ctx
, shProg
);
2056 /* Must be done first: detach the progam */
2057 _mesa_use_shader_program(ctx
, shProg
);
2058 /* Unattach shader_state binding point */
2059 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
2060 ctx
->Pipeline
.Default
);
2061 /* If a pipeline was bound, rebind it */
2062 if (ctx
->Pipeline
.Current
) {
2064 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
2066 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
2073 _mesa_UseProgram_no_error(GLuint program
)
2075 use_program(program
, true);
2080 _mesa_UseProgram(GLuint program
)
2082 use_program(program
, false);
2087 _mesa_ValidateProgram(GLuint program
)
2089 GET_CURRENT_CONTEXT(ctx
);
2090 validate_program(ctx
, program
);
2095 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2098 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
2099 GLint
* range
, GLint
* precision
)
2101 const struct gl_program_constants
*limits
;
2102 const struct gl_precision
*p
;
2103 GET_CURRENT_CONTEXT(ctx
);
2105 switch (shadertype
) {
2106 case GL_VERTEX_SHADER
:
2107 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
2109 case GL_FRAGMENT_SHADER
:
2110 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
2113 _mesa_error(ctx
, GL_INVALID_ENUM
,
2114 "glGetShaderPrecisionFormat(shadertype)");
2118 switch (precisiontype
) {
2120 p
= &limits
->LowFloat
;
2122 case GL_MEDIUM_FLOAT
:
2123 p
= &limits
->MediumFloat
;
2126 p
= &limits
->HighFloat
;
2129 p
= &limits
->LowInt
;
2132 p
= &limits
->MediumInt
;
2135 p
= &limits
->HighInt
;
2138 _mesa_error(ctx
, GL_INVALID_ENUM
,
2139 "glGetShaderPrecisionFormat(precisiontype)");
2143 range
[0] = p
->RangeMin
;
2144 range
[1] = p
->RangeMax
;
2145 precision
[0] = p
->Precision
;
2150 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2153 _mesa_ReleaseShaderCompiler(void)
2155 _mesa_destroy_shader_compiler_caches();
2160 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2163 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2164 const void* binary
, GLint length
)
2166 GET_CURRENT_CONTEXT(ctx
);
2167 struct gl_shader
**sh
;
2169 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2170 * page 88 of the OpenGL 4.5 specs state:
2172 * "An INVALID_VALUE error is generated if count or length is negative.
2173 * An INVALID_ENUM error is generated if binaryformat is not a supported
2174 * format returned in SHADER_BINARY_FORMATS."
2176 if (n
< 0 || length
< 0) {
2177 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2181 /* Get all shader objects at once so we can make the operation
2184 if (n
> SIZE_MAX
/ sizeof(*sh
)) {
2185 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary(count)");
2189 sh
= alloca(sizeof(*sh
) * (size_t)n
);
2191 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary");
2195 for (int i
= 0; i
< n
; ++i
) {
2196 sh
[i
] = _mesa_lookup_shader_err(ctx
, shaders
[i
], "glShaderBinary");
2201 if (binaryformat
== GL_SHADER_BINARY_FORMAT_SPIR_V_ARB
) {
2202 if (!ctx
->Extensions
.ARB_gl_spirv
) {
2203 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary(SPIR-V)");
2205 _mesa_spirv_shader_binary(ctx
, (unsigned) n
, sh
, binary
,
2212 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2217 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2218 GLenum
*binaryFormat
, GLvoid
*binary
)
2220 struct gl_shader_program
*shProg
;
2221 GLsizei length_dummy
;
2222 GET_CURRENT_CONTEXT(ctx
);
2225 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2229 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2233 /* The ARB_get_program_binary spec says:
2235 * "If <length> is NULL, then no length is returned."
2237 * Ensure that length always points to valid storage to avoid multiple NULL
2238 * pointer checks below.
2241 length
= &length_dummy
;
2244 /* The ARB_get_program_binary spec says:
2246 * "When a program object's LINK_STATUS is FALSE, its program binary
2247 * length is zero, and a call to GetProgramBinary will generate an
2248 * INVALID_OPERATION error.
2250 if (!shProg
->data
->LinkStatus
) {
2251 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2252 "glGetProgramBinary(program %u not linked)",
2258 if (ctx
->Const
.NumProgramBinaryFormats
== 0) {
2260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2261 "glGetProgramBinary(driver supports zero binary formats)");
2263 _mesa_get_program_binary(ctx
, shProg
, bufSize
, length
, binaryFormat
,
2265 assert(*length
== 0 || *binaryFormat
== GL_PROGRAM_BINARY_FORMAT_MESA
);
2270 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2271 const GLvoid
*binary
, GLsizei length
)
2273 struct gl_shader_program
*shProg
;
2274 GET_CURRENT_CONTEXT(ctx
);
2276 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2280 _mesa_clear_shader_program_data(ctx
, shProg
);
2281 shProg
->data
= _mesa_create_shader_program_data();
2283 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2285 * "If a negative number is provided where an argument of type sizei or
2286 * sizeiptr is specified, an INVALID_VALUE error is generated."
2289 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2293 if (ctx
->Const
.NumProgramBinaryFormats
== 0 ||
2294 binaryFormat
!= GL_PROGRAM_BINARY_FORMAT_MESA
) {
2295 /* The ARB_get_program_binary spec says:
2297 * "<binaryFormat> and <binary> must be those returned by a previous
2298 * call to GetProgramBinary, and <length> must be the length of the
2299 * program binary as returned by GetProgramBinary or GetProgramiv with
2300 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2301 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2304 * Since any value of binaryFormat passed "is not one of those specified as
2305 * allowable for [this] command, an INVALID_ENUM error is generated."
2307 shProg
->data
->LinkStatus
= linking_failure
;
2308 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2310 _mesa_program_binary(ctx
, shProg
, binaryFormat
, binary
, length
);
2315 static ALWAYS_INLINE
void
2316 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2317 GLuint pname
, GLint value
, bool no_error
)
2320 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2321 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2322 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2323 * even be in the dispatch table, so we shouldn't need to expclicitly
2326 * On desktop, we ignore the 3.0+ requirement because it is silly.
2329 /* The ARB_get_program_binary extension spec says:
2331 * "An INVALID_VALUE error is generated if the <value> argument to
2332 * ProgramParameteri is not TRUE or FALSE."
2334 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2338 /* No need to notify the driver. Any changes will actually take effect
2339 * the next time the shader is linked.
2341 * The ARB_get_program_binary extension spec says:
2343 * "To indicate that a program binary is likely to be retrieved,
2344 * ProgramParameteri should be called with <pname>
2345 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2346 * will not be in effect until the next time LinkProgram or
2347 * ProgramBinary has been called successfully."
2349 * The resloution of issue 9 in the extension spec also says:
2351 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2352 * to indicate to the GL implementation that this program will
2353 * likely be saved with GetProgramBinary at some point. This will
2354 * give the GL implementation the opportunity to track any state
2355 * changes made to the program before being saved such that when it
2356 * is loaded again a recompile can be avoided."
2358 shProg
->BinaryRetreivableHint
= value
;
2361 case GL_PROGRAM_SEPARABLE
:
2362 /* Spec imply that the behavior is the same as ARB_get_program_binary
2363 * Chapter 7.3 Program Objects
2365 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2368 shProg
->SeparateShader
= value
;
2373 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2374 _mesa_enum_to_string(pname
));
2380 _mesa_error(ctx
, GL_INVALID_VALUE
,
2381 "glProgramParameteri(pname=%s, value=%d): "
2382 "value must be 0 or 1.",
2383 _mesa_enum_to_string(pname
),
2389 _mesa_ProgramParameteri_no_error(GLuint program
, GLenum pname
, GLint value
)
2391 GET_CURRENT_CONTEXT(ctx
);
2393 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
2394 program_parameteri(ctx
, shProg
, pname
, value
, true);
2399 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2401 struct gl_shader_program
*shProg
;
2402 GET_CURRENT_CONTEXT(ctx
);
2404 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2405 "glProgramParameteri");
2409 program_parameteri(ctx
, shProg
, pname
, value
, false);
2414 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2415 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2416 struct gl_pipeline_object
*shTarget
)
2418 struct gl_program
**target
;
2420 target
= &shTarget
->CurrentProgram
[stage
];
2422 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2425 if (*target
!= prog
) {
2426 /* Program is current, flush it */
2427 if (shTarget
== ctx
->_Shader
) {
2428 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2431 _mesa_reference_shader_program(ctx
,
2432 &shTarget
->ReferencedPrograms
[stage
],
2434 _mesa_reference_program(ctx
, target
, prog
);
2442 * Copy program-specific data generated by linking from the gl_shader_program
2443 * object to the gl_program object referred to by the gl_linked_shader.
2445 * This function expects _mesa_reference_program() to have been previously
2446 * called setting the gl_linked_shaders program reference.
2449 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2450 struct gl_linked_shader
*dst_sh
)
2452 assert(dst_sh
->Program
);
2454 struct gl_program
*dst
= dst_sh
->Program
;
2456 dst
->info
.separate_shader
= src
->SeparateShader
;
2458 switch (dst_sh
->Stage
) {
2459 case MESA_SHADER_GEOMETRY
: {
2460 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2461 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2462 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2465 case MESA_SHADER_FRAGMENT
: {
2466 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2469 case MESA_SHADER_COMPUTE
: {
2470 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2479 * ARB_separate_shader_objects: Compile & Link Program
2482 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2483 const GLchar
* const *strings
)
2485 GET_CURRENT_CONTEXT(ctx
);
2487 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2491 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2492 * GL_INVALID_VALUE should be generated if count < 0
2495 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2500 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2502 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2503 _mesa_compile_shader(ctx
, sh
);
2505 program
= create_shader_program(ctx
);
2507 struct gl_shader_program
*shProg
;
2508 GLint compiled
= GL_FALSE
;
2510 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2512 shProg
->SeparateShader
= GL_TRUE
;
2514 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2516 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2517 _mesa_link_program(ctx
, shProg
);
2518 detach_shader_error(ctx
, program
, shader
);
2522 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2523 append
-error
-to
-info
-log
;
2524 shProg
->data
->LinkStatus
= linking_failure
;
2529 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2532 delete_shader(ctx
, shader
);
2540 * For GL_ARB_tessellation_shader
2543 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2545 GET_CURRENT_CONTEXT(ctx
);
2546 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2550 extern void GLAPIENTRY
2551 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2553 GET_CURRENT_CONTEXT(ctx
);
2555 if (!_mesa_has_tessellation(ctx
)) {
2556 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2560 if (pname
!= GL_PATCH_VERTICES
) {
2561 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2565 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2566 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2570 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2574 extern void GLAPIENTRY
2575 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2577 GET_CURRENT_CONTEXT(ctx
);
2579 if (!_mesa_has_tessellation(ctx
)) {
2580 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2585 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2586 FLUSH_VERTICES(ctx
, 0);
2587 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2588 4 * sizeof(GLfloat
));
2589 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2591 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2592 FLUSH_VERTICES(ctx
, 0);
2593 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2594 2 * sizeof(GLfloat
));
2595 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2598 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2604 * ARB_shader_subroutine
2607 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2610 GET_CURRENT_CONTEXT(ctx
);
2611 const char *api_name
= "glGetSubroutineUniformLocation";
2612 struct gl_shader_program
*shProg
;
2613 GLenum resource_type
;
2614 gl_shader_stage stage
;
2616 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2617 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2621 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2625 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2626 if (!shProg
->_LinkedShaders
[stage
]) {
2627 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2631 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2632 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2636 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2639 GET_CURRENT_CONTEXT(ctx
);
2640 const char *api_name
= "glGetSubroutineIndex";
2641 struct gl_shader_program
*shProg
;
2642 struct gl_program_resource
*res
;
2643 GLenum resource_type
;
2644 gl_shader_stage stage
;
2646 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2647 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2651 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2655 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2656 if (!shProg
->_LinkedShaders
[stage
]) {
2657 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2661 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2662 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2667 return _mesa_program_resource_index(shProg
, res
);
2672 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2673 GLuint index
, GLenum pname
, GLint
*values
)
2675 GET_CURRENT_CONTEXT(ctx
);
2676 const char *api_name
= "glGetActiveSubroutineUniformiv";
2677 struct gl_shader_program
*shProg
;
2678 struct gl_linked_shader
*sh
;
2679 gl_shader_stage stage
;
2680 struct gl_program_resource
*res
;
2681 const struct gl_uniform_storage
*uni
;
2682 GLenum resource_type
;
2685 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2686 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2690 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2694 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2695 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2697 sh
= shProg
->_LinkedShaders
[stage
];
2699 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2703 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2704 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2705 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2710 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2711 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2714 values
[0] = uni
->num_compatible_subroutines
;
2718 case GL_COMPATIBLE_SUBROUTINES
: {
2719 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2723 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2724 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2725 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2726 if (fn
->types
[j
] == uni
->type
) {
2727 values
[count
++] = i
;
2735 case GL_UNIFORM_SIZE
:
2736 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2739 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2742 case GL_UNIFORM_NAME_LENGTH
:
2743 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2745 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2746 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2750 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2757 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2758 GLuint index
, GLsizei bufsize
,
2759 GLsizei
*length
, GLchar
*name
)
2761 GET_CURRENT_CONTEXT(ctx
);
2762 const char *api_name
= "glGetActiveSubroutineUniformName";
2763 struct gl_shader_program
*shProg
;
2764 GLenum resource_type
;
2765 gl_shader_stage stage
;
2767 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2768 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2772 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2776 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2777 if (!shProg
->_LinkedShaders
[stage
]) {
2778 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2782 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2783 /* get program resource name */
2784 _mesa_get_program_resource_name(shProg
, resource_type
,
2786 length
, name
, api_name
);
2791 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2792 GLuint index
, GLsizei bufsize
,
2793 GLsizei
*length
, GLchar
*name
)
2795 GET_CURRENT_CONTEXT(ctx
);
2796 const char *api_name
= "glGetActiveSubroutineName";
2797 struct gl_shader_program
*shProg
;
2798 GLenum resource_type
;
2799 gl_shader_stage stage
;
2801 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2802 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2806 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2810 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2811 if (!shProg
->_LinkedShaders
[stage
]) {
2812 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2815 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2816 _mesa_get_program_resource_name(shProg
, resource_type
,
2818 length
, name
, api_name
);
2822 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2823 const GLuint
*indices
)
2825 GET_CURRENT_CONTEXT(ctx
);
2826 const char *api_name
= "glUniformSubroutinesuiv";
2827 gl_shader_stage stage
;
2830 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2831 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2835 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2836 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2838 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2842 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2843 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2848 bool flushed
= false;
2850 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2857 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2861 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2864 for (j
= i
; j
< i
+ uni_count
; j
++) {
2865 struct gl_subroutine_function
*subfn
= NULL
;
2866 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2867 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2871 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2872 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2873 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2880 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2881 if (subfn
->types
[k
] == uni
->type
)
2884 if (k
== subfn
->num_compat_types
) {
2885 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2889 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2897 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2900 GET_CURRENT_CONTEXT(ctx
);
2901 const char *api_name
= "glGetUniformSubroutineuiv";
2902 gl_shader_stage stage
;
2904 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2905 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2909 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2910 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2912 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2916 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2917 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2921 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2926 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2927 GLenum pname
, GLint
*values
)
2929 GET_CURRENT_CONTEXT(ctx
);
2930 const char *api_name
= "glGetProgramStageiv";
2931 struct gl_shader_program
*shProg
;
2932 struct gl_linked_shader
*sh
;
2933 gl_shader_stage stage
;
2935 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2936 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2940 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2944 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2945 sh
= shProg
->_LinkedShaders
[stage
];
2947 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2948 * INVALID_OPERATION in the case of not be linked.
2950 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2951 * same info using other specs (ARB_program_interface_query), without the
2952 * need of the program to be linked, being the value for that case 0.
2954 * But at the same time, some other methods require the program to be
2955 * linked for pname related to locations, so it would be inconsistent to
2956 * not do the same here. So we are:
2957 * * Return GL_INVALID_OPERATION if not linked only for locations.
2958 * * Setting a default value of 0, to be returned if not linked.
2962 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2963 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2968 struct gl_program
*p
= sh
->Program
;
2970 case GL_ACTIVE_SUBROUTINES
:
2971 values
[0] = p
->sh
.NumSubroutineFunctions
;
2973 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2974 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2976 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2977 values
[0] = p
->sh
.NumSubroutineUniforms
;
2979 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2983 GLenum resource_type
;
2984 struct gl_program_resource
*res
;
2986 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2987 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2988 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2990 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2995 values
[0] = max_len
;
2998 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
3002 GLenum resource_type
;
3003 struct gl_program_resource
*res
;
3005 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
3006 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3007 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
3009 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
3010 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
3016 values
[0] = max_len
;
3020 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
3027 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
3031 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
3032 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
3033 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
3034 if (fn
->types
[j
] == type
)
3042 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
3043 struct gl_program
*p
)
3047 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
3052 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3061 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
3062 for (j
= 0; j
< uni_count
; j
++) {
3063 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
3064 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
3067 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
3069 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
3073 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
3074 gl_shader_stage stage
)
3076 if (ctx
->_Shader
->CurrentProgram
[stage
])
3077 _mesa_shader_write_subroutine_index(ctx
,
3078 ctx
->_Shader
->CurrentProgram
[stage
]);
3082 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
3083 struct gl_program
*p
)
3087 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
3088 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
3089 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
3090 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
3091 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
3094 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
3095 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
3100 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);