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 "main/glheader.h"
42 #include "main/context.h"
43 #include "main/dispatch.h"
44 #include "main/enums.h"
45 #include "main/hash.h"
46 #include "main/mtypes.h"
47 #include "main/pipelineobj.h"
48 #include "main/shaderapi.h"
49 #include "main/shaderobj.h"
50 #include "main/transformfeedback.h"
51 #include "main/uniforms.h"
52 #include "compiler/glsl/glsl_parser_extras.h"
53 #include "compiler/glsl/ir.h"
54 #include "compiler/glsl/ir_uniform.h"
55 #include "compiler/glsl/program.h"
56 #include "program/program.h"
57 #include "program/prog_print.h"
58 #include "program/prog_parameter.h"
59 #include "util/ralloc.h"
60 #include "util/hash_table.h"
61 #include "util/mesa-sha1.h"
62 #include "util/crc32.h"
65 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
68 _mesa_get_shader_flags(void)
70 GLbitfield flags
= 0x0;
71 const char *env
= getenv("MESA_GLSL");
74 if (strstr(env
, "dump_on_error"))
75 flags
|= GLSL_DUMP_ON_ERROR
;
76 else if (strstr(env
, "dump"))
78 if (strstr(env
, "log"))
80 if (strstr(env
, "cache_fb"))
81 flags
|= GLSL_CACHE_FALLBACK
;
82 if (strstr(env
, "cache_info"))
83 flags
|= GLSL_CACHE_INFO
;
84 if (strstr(env
, "nopvert"))
85 flags
|= GLSL_NOP_VERT
;
86 if (strstr(env
, "nopfrag"))
87 flags
|= GLSL_NOP_FRAG
;
88 if (strstr(env
, "uniform"))
89 flags
|= GLSL_UNIFORMS
;
90 if (strstr(env
, "useprog"))
91 flags
|= GLSL_USE_PROG
;
92 if (strstr(env
, "errors"))
93 flags
|= GLSL_REPORT_ERRORS
;
100 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
103 _mesa_get_shader_capture_path(void)
105 static bool read_env_var
= false;
106 static const char *path
= NULL
;
109 path
= getenv("MESA_SHADER_CAPTURE_PATH");
117 * Initialize context's shader state.
120 _mesa_init_shader_state(struct gl_context
*ctx
)
122 /* Device drivers may override these to control what kind of instructions
123 * are generated by the GLSL compiler.
125 struct gl_shader_compiler_options options
;
129 memset(&options
, 0, sizeof(options
));
130 options
.MaxUnrollIterations
= 32;
131 options
.MaxIfDepth
= UINT_MAX
;
133 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
134 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
136 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
138 if (ctx
->Shader
.Flags
!= 0)
139 ctx
->Const
.GenerateTemporaryNames
= true;
141 /* Extended for ARB_separate_shader_objects */
142 ctx
->Shader
.RefCount
= 1;
143 ctx
->TessCtrlProgram
.patch_vertices
= 3;
144 for (i
= 0; i
< 4; ++i
)
145 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
146 for (i
= 0; i
< 2; ++i
)
147 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
152 * Free the per-context shader-related state.
155 _mesa_free_shader_state(struct gl_context
*ctx
)
157 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
158 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
160 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
162 /* Extended for ARB_separate_shader_objects */
163 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
165 assert(ctx
->Shader
.RefCount
== 1);
170 * Copy string from <src> to <dst>, up to maxLength characters, returning
171 * length of <dst> in <length>.
172 * \param src the strings source
173 * \param maxLength max chars to copy
174 * \param length returns number of chars copied
175 * \param dst the string destination
178 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
179 GLsizei
*length
, const GLchar
*src
)
182 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
193 * Confirm that the a shader type is valid and supported by the implementation
195 * \param ctx Current GL context
196 * \param type Shader target
200 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
202 /* Note: when building built-in GLSL functions, this function may be
203 * invoked with ctx == NULL. In that case, we can only validate that it's
204 * a shader target we recognize, not that it's supported in the current
205 * context. But that's fine--we don't need any further validation than
206 * that when building built-in GLSL functions.
210 case GL_FRAGMENT_SHADER
:
211 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
212 case GL_VERTEX_SHADER
:
213 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
214 case GL_GEOMETRY_SHADER_ARB
:
215 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
216 case GL_TESS_CONTROL_SHADER
:
217 case GL_TESS_EVALUATION_SHADER
:
218 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
219 case GL_COMPUTE_SHADER
:
220 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
228 is_program(struct gl_context
*ctx
, GLuint name
)
230 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
231 return shProg
? GL_TRUE
: GL_FALSE
;
236 is_shader(struct gl_context
*ctx
, GLuint name
)
238 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
239 return shader
? GL_TRUE
: GL_FALSE
;
244 * Attach shader to a shader program.
247 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
248 struct gl_shader
*sh
)
250 GLuint n
= shProg
->NumShaders
;
252 shProg
->Shaders
= realloc(shProg
->Shaders
,
253 (n
+ 1) * sizeof(struct gl_shader
*));
254 if (!shProg
->Shaders
) {
255 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
260 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
261 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
262 shProg
->NumShaders
++;
266 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
269 struct gl_shader_program
*shProg
;
270 struct gl_shader
*sh
;
273 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
275 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
279 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
284 n
= shProg
->NumShaders
;
285 for (i
= 0; i
< n
; i
++) {
286 if (shProg
->Shaders
[i
] == sh
) {
287 /* The shader is already attched to this program. The
288 * GL_ARB_shader_objects spec says:
290 * "The error INVALID_OPERATION is generated by AttachObjectARB
291 * if <obj> is already attached to <containerObj>."
293 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
295 } else if (same_type_disallowed
&&
296 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
297 /* Shader with the same type is already attached to this program,
298 * OpenGL ES 2.0 and 3.0 specs say:
300 * "Multiple shader objects of the same type may not be attached
301 * to a single program object. [...] The error INVALID_OPERATION
302 * is generated if [...] another shader object of the same type
303 * as shader is already attached to program."
305 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
310 attach_shader(ctx
, shProg
, sh
);
314 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
316 struct gl_shader_program
*shProg
;
317 struct gl_shader
*sh
;
319 shProg
= _mesa_lookup_shader_program(ctx
, program
);
320 sh
= _mesa_lookup_shader(ctx
, shader
);
322 attach_shader(ctx
, shProg
, sh
);
326 create_shader(struct gl_context
*ctx
, GLenum type
)
328 struct gl_shader
*sh
;
331 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
332 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
333 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
335 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
336 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
343 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
345 if (!_mesa_validate_shader_target(ctx
, type
)) {
346 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
347 caller
, _mesa_enum_to_string(type
));
351 return create_shader(ctx
, type
);
356 create_shader_program(struct gl_context
*ctx
)
359 struct gl_shader_program
*shProg
;
361 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
363 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
365 shProg
= _mesa_new_shader_program(name
);
367 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
369 assert(shProg
->RefCount
== 1);
371 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
378 * Delete a shader program. Actually, just decrement the program's
379 * reference count and mark it as DeletePending.
380 * Used to implement glDeleteProgram() and glDeleteObjectARB().
383 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
386 * NOTE: deleting shaders/programs works a bit differently than
387 * texture objects (and buffer objects, etc). Shader/program
388 * handles/IDs exist in the hash table until the object is really
389 * deleted (refcount==0). With texture objects, the handle/ID is
390 * removed from the hash table in glDeleteTextures() while the tex
391 * object itself might linger until its refcount goes to zero.
393 struct gl_shader_program
*shProg
;
395 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
399 if (!shProg
->DeletePending
) {
400 shProg
->DeletePending
= GL_TRUE
;
402 /* effectively, decr shProg's refcount */
403 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
409 delete_shader(struct gl_context
*ctx
, GLuint shader
)
411 struct gl_shader
*sh
;
413 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
417 if (!sh
->DeletePending
) {
418 sh
->DeletePending
= GL_TRUE
;
420 /* effectively, decr sh's refcount */
421 _mesa_reference_shader(ctx
, &sh
, NULL
);
427 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
429 struct gl_shader_program
*shProg
;
433 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
437 n
= shProg
->NumShaders
;
439 for (i
= 0; i
< n
; i
++) {
440 if (shProg
->Shaders
[i
]->Name
== shader
) {
442 struct gl_shader
**newList
;
445 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
447 /* alloc new, smaller array */
448 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
450 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
453 /* Copy old list entries to new list, skipping removed entry at [i] */
454 for (j
= 0; j
< i
; j
++) {
455 newList
[j
] = shProg
->Shaders
[j
];
458 newList
[j
++] = shProg
->Shaders
[i
];
461 /* Free old list and install new one */
462 free(shProg
->Shaders
);
463 shProg
->Shaders
= newList
;
464 shProg
->NumShaders
= n
- 1;
467 /* sanity check - make sure the new list's entries are sensible */
468 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
469 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
470 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
471 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
472 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
473 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
474 assert(shProg
->Shaders
[j
]->RefCount
> 0);
485 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
486 err
= GL_INVALID_OPERATION
;
488 err
= GL_INVALID_VALUE
;
489 _mesa_error(ctx
, err
, "glDetachShader(shader)");
496 * Return list of shaders attached to shader program.
499 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
500 GLsizei
*count
, GLuint
*obj
)
502 struct gl_shader_program
*shProg
;
505 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
510 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
514 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
515 obj
[i
] = shProg
->Shaders
[i
]->Name
;
524 * glGetHandleARB() - return ID/name of currently bound shader program.
527 get_handle(struct gl_context
*ctx
, GLenum pname
)
529 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
530 if (ctx
->_Shader
->ActiveProgram
)
531 return ctx
->_Shader
->ActiveProgram
->Name
;
536 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
543 * Check if a geometry shader query is valid at this time. If not, report an
544 * error and return false.
546 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
548 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
549 * are queried for a program which has not been linked successfully, or
550 * which does not contain objects to form a geometry shader, then an
551 * INVALID_OPERATION error is generated."
554 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
556 if (shProg
->data
->LinkStatus
&&
557 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
561 _mesa_error(ctx
, GL_INVALID_OPERATION
,
562 "glGetProgramv(linked geometry shader required)");
568 * Check if a tessellation control shader query is valid at this time.
569 * If not, report an error and return false.
571 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
573 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
574 * not been linked successfully, or which does not contain objects to
575 * form a tessellation control shader, then an INVALID_OPERATION error is
579 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
581 if (shProg
->data
->LinkStatus
&&
582 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
586 _mesa_error(ctx
, GL_INVALID_OPERATION
,
587 "glGetProgramv(linked tessellation control shader required)");
593 * Check if a tessellation evaluation shader query is valid at this time.
594 * If not, report an error and return false.
596 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
598 * "If any of the pname values in this paragraph are queried for a program
599 * which has not been linked successfully, or which does not contain
600 * objects to form a tessellation evaluation shader, then an
601 * INVALID_OPERATION error is generated."
605 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
607 if (shProg
->data
->LinkStatus
&&
608 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
612 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
613 "evaluation shader required)");
619 * glGetProgramiv() - get shader program state.
620 * Note that this is for GLSL shader programs, not ARB vertex/fragment
621 * programs (see glGetProgramivARB).
624 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
627 struct gl_shader_program
*shProg
628 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
630 /* Is transform feedback available in this context?
633 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
634 || ctx
->API
== API_OPENGL_CORE
635 || _mesa_is_gles3(ctx
);
637 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
638 * and GL 3.2) are available in this context
640 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
641 const bool has_tess
= _mesa_has_tessellation(ctx
);
643 /* Are uniform buffer objects available in this context?
646 (ctx
->API
== API_OPENGL_COMPAT
&&
647 ctx
->Extensions
.ARB_uniform_buffer_object
)
648 || ctx
->API
== API_OPENGL_CORE
649 || _mesa_is_gles3(ctx
);
656 case GL_DELETE_STATUS
:
657 *params
= shProg
->DeletePending
;
660 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
662 case GL_VALIDATE_STATUS
:
663 *params
= shProg
->data
->Validated
;
665 case GL_INFO_LOG_LENGTH
:
666 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
667 strlen(shProg
->data
->InfoLog
) + 1 : 0;
669 case GL_ATTACHED_SHADERS
:
670 *params
= shProg
->NumShaders
;
672 case GL_ACTIVE_ATTRIBUTES
:
673 *params
= _mesa_count_active_attribs(shProg
);
675 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
676 *params
= _mesa_longest_attribute_name_length(shProg
);
678 case GL_ACTIVE_UNIFORMS
: {
680 const unsigned num_uniforms
=
681 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
682 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
683 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
688 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
691 const unsigned num_uniforms
=
692 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
694 for (i
= 0; i
< num_uniforms
; i
++) {
695 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
698 /* Add one for the terminating NUL character for a non-array, and
699 * 4 for the "[0]" and the NUL for an array.
701 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
702 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
711 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
714 *params
= shProg
->TransformFeedback
.NumVarying
;
716 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
722 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
723 /* Add one for the terminating NUL character.
726 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
735 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
738 *params
= shProg
->TransformFeedback
.BufferMode
;
740 case GL_GEOMETRY_VERTICES_OUT
:
743 if (check_gs_query(ctx
, shProg
)) {
744 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
745 Program
->info
.gs
.vertices_out
;
748 case GL_GEOMETRY_SHADER_INVOCATIONS
:
749 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
751 if (check_gs_query(ctx
, shProg
)) {
752 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
753 Program
->info
.gs
.invocations
;
756 case GL_GEOMETRY_INPUT_TYPE
:
759 if (check_gs_query(ctx
, shProg
)) {
760 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
761 Program
->info
.gs
.input_primitive
;
764 case GL_GEOMETRY_OUTPUT_TYPE
:
767 if (check_gs_query(ctx
, shProg
)) {
768 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
769 Program
->info
.gs
.output_primitive
;
772 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
779 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
780 /* Add one for the terminating NUL character.
782 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
791 case GL_ACTIVE_UNIFORM_BLOCKS
:
795 *params
= shProg
->data
->NumUniformBlocks
;
797 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
798 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
799 * only available with desktop OpenGL 3.0+ with the
800 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
802 * On desktop, we ignore the 3.0+ requirement because it is silly.
804 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
807 *params
= shProg
->BinaryRetreivableHint
;
809 case GL_PROGRAM_BINARY_LENGTH
:
812 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
813 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
816 *params
= shProg
->data
->NumAtomicBuffers
;
818 case GL_COMPUTE_WORK_GROUP_SIZE
: {
820 if (!_mesa_has_compute_shaders(ctx
))
822 if (!shProg
->data
->LinkStatus
) {
823 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
827 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
828 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
832 for (i
= 0; i
< 3; i
++)
833 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
834 Program
->info
.cs
.local_size
[i
];
837 case GL_PROGRAM_SEPARABLE
:
838 /* If the program has not been linked, return initial value 0. */
839 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
842 /* ARB_tessellation_shader */
843 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
846 if (check_tcs_query(ctx
, shProg
)) {
847 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
848 Program
->info
.tess
.tcs_vertices_out
;
851 case GL_TESS_GEN_MODE
:
854 if (check_tes_query(ctx
, shProg
)) {
855 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
856 Program
->info
.tess
.primitive_mode
;
859 case GL_TESS_GEN_SPACING
:
862 if (check_tes_query(ctx
, shProg
)) {
863 const struct gl_linked_shader
*tes
=
864 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
865 switch (tes
->Program
->info
.tess
.spacing
) {
866 case TESS_SPACING_EQUAL
:
869 case TESS_SPACING_FRACTIONAL_ODD
:
870 *params
= GL_FRACTIONAL_ODD
;
872 case TESS_SPACING_FRACTIONAL_EVEN
:
873 *params
= GL_FRACTIONAL_EVEN
;
875 case TESS_SPACING_UNSPECIFIED
:
881 case GL_TESS_GEN_VERTEX_ORDER
:
884 if (check_tes_query(ctx
, shProg
)) {
885 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
886 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
889 case GL_TESS_GEN_POINT_MODE
:
892 if (check_tes_query(ctx
, shProg
)) {
893 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
894 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
901 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
902 _mesa_enum_to_string(pname
));
907 * glGetShaderiv() - get GLSL shader state
910 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
912 struct gl_shader
*shader
=
913 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
921 *params
= shader
->Type
;
923 case GL_DELETE_STATUS
:
924 *params
= shader
->DeletePending
;
926 case GL_COMPILE_STATUS
:
927 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
929 case GL_INFO_LOG_LENGTH
:
930 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
931 strlen(shader
->InfoLog
) + 1 : 0;
933 case GL_SHADER_SOURCE_LENGTH
:
934 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
937 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
944 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
945 GLsizei
*length
, GLchar
*infoLog
)
947 struct gl_shader_program
*shProg
;
949 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
950 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
952 * "If a negative number is provided where an argument of type sizei or
953 * sizeiptr is specified, an INVALID_VALUE error is generated."
956 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
960 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
961 "glGetProgramInfoLog(program)");
966 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
971 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
972 GLsizei
*length
, GLchar
*infoLog
)
974 struct gl_shader
*sh
;
976 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
977 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
979 * "If a negative number is provided where an argument of type sizei or
980 * sizeiptr is specified, an INVALID_VALUE error is generated."
983 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
987 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
992 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
997 * Return shader source code.
1000 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1001 GLsizei
*length
, GLchar
*sourceOut
)
1003 struct gl_shader
*sh
;
1005 if (maxLength
< 0) {
1006 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1010 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1014 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1019 * Set/replace shader source code. A helper function used by
1020 * glShaderSource[ARB].
1023 shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1027 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
1028 /* If shader was previously compiled back-up the source in case of cache
1031 sh
->FallbackSource
= sh
->Source
;
1032 sh
->Source
= source
;
1034 /* free old shader source string and install new one */
1035 free((void *)sh
->Source
);
1036 sh
->Source
= source
;
1040 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1049 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1055 /* If the user called glCompileShader without first calling
1056 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1058 sh
->CompileStatus
= compile_failure
;
1060 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1061 _mesa_log("GLSL source for %s shader %d:\n",
1062 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1063 _mesa_log("%s\n", sh
->Source
);
1066 /* this call will set the shader->CompileStatus field to indicate if
1067 * compilation was successful.
1069 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1071 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1072 _mesa_write_shader_to_file(sh
);
1075 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1076 if (sh
->CompileStatus
) {
1078 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1079 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1081 _mesa_log("No GLSL IR for shader %d (shader may be from "
1082 "cache)\n", sh
->Name
);
1086 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1088 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1089 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1090 _mesa_log("%s\n", sh
->InfoLog
);
1095 if (!sh
->CompileStatus
) {
1096 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1097 _mesa_log("GLSL source for %s shader %d:\n",
1098 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1099 _mesa_log("%s\n", sh
->Source
);
1100 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1103 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1104 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1105 sh
->Name
, sh
->InfoLog
);
1112 * Link a program's shaders.
1115 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1120 /* From the ARB_transform_feedback2 specification:
1121 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1122 * the name of a program being used by one or more transform feedback
1123 * objects, even if the objects are not currently bound or are paused."
1125 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1126 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1127 "glLinkProgram(transform feedback is using the program)");
1131 unsigned programs_in_use
= 0;
1133 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1134 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1135 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1136 programs_in_use
|= 1 << stage
;
1140 FLUSH_VERTICES(ctx
, 0);
1141 _mesa_glsl_link_shader(ctx
, shProg
);
1143 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1145 * "If LinkProgram or ProgramBinary successfully re-links a program
1146 * object that is active for any shader stage, then the newly generated
1147 * executable code will be installed as part of the current rendering
1148 * state for all shader stages where the program is active.
1149 * Additionally, the newly generated executable code is made part of
1150 * the state of any program pipeline for all stages where the program
1153 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1154 while (programs_in_use
) {
1155 const int stage
= u_bit_scan(&programs_in_use
);
1157 struct gl_program
*prog
= NULL
;
1158 if (shProg
->_LinkedShaders
[stage
])
1159 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1161 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1165 /* Capture .shader_test files. */
1166 const char *capture_path
= _mesa_get_shader_capture_path();
1167 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1169 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1170 capture_path
, shProg
->Name
);
1171 file
= fopen(filename
, "w");
1173 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1174 shProg
->IsES
? " ES" : "",
1175 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1176 if (shProg
->SeparateShader
)
1177 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1178 fprintf(file
, "\n");
1180 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1181 fprintf(file
, "[%s shader]\n%s\n",
1182 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1183 shProg
->Shaders
[i
]->Source
);
1187 _mesa_warning(ctx
, "Failed to open %s", filename
);
1190 ralloc_free(filename
);
1193 if (shProg
->data
->LinkStatus
== linking_failure
&&
1194 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1195 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1196 shProg
->Name
, shProg
->data
->InfoLog
);
1203 printf("Link %u shaders in program %u: %s\n",
1204 shProg
->NumShaders
, shProg
->Name
,
1205 shProg
->data
->LinkStatus
? "Success" : "Failed");
1207 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1208 printf(" shader %u, stage %u\n",
1209 shProg
->Shaders
[i
]->Name
,
1210 shProg
->Shaders
[i
]->Stage
);
1217 * Print basic shader info (for debug).
1220 print_shader_info(const struct gl_shader_program
*shProg
)
1224 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1225 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1227 printf(" %s shader %u, checksum %u\n",
1228 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1229 shProg
->Shaders
[i
]->Name
,
1230 shProg
->Shaders
[i
]->SourceChecksum
);
1232 printf(" %s shader %u\n",
1233 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1234 shProg
->Shaders
[i
]->Name
);
1237 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1238 printf(" vert prog %u\n",
1239 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1240 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1241 printf(" frag prog %u\n",
1242 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1243 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1244 printf(" geom prog %u\n",
1245 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1246 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1247 printf(" tesc prog %u\n",
1248 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1249 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1250 printf(" tese prog %u\n",
1251 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1256 * Use the named shader program for subsequent glUniform calls
1259 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1262 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1263 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1264 "%s(program %u not linked)", caller
, shProg
->Name
);
1268 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1269 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1275 * Use the named shader program for subsequent rendering.
1278 _mesa_use_shader_program(struct gl_context
*ctx
,
1279 struct gl_shader_program
*shProg
)
1281 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1282 struct gl_program
*new_prog
= NULL
;
1283 if (shProg
&& shProg
->_LinkedShaders
[i
])
1284 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1285 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1287 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1292 * Do validation of the given shader program.
1293 * \param errMsg returns error message if validation fails.
1294 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1297 validate_shader_program(const struct gl_shader_program
*shProg
,
1300 if (!shProg
->data
->LinkStatus
) {
1304 /* From the GL spec, a program is invalid if any of these are true:
1306 any two active samplers in the current program object are of
1307 different types, but refer to the same texture image unit,
1309 any active sampler in the current program object refers to a texture
1310 image unit where fixed-function fragment processing accesses a
1311 texture target that does not match the sampler type, or
1313 the sum of the number of active samplers in the program and the
1314 number of texture image units enabled for fixed-function fragment
1315 processing exceeds the combined limit on the total number of texture
1316 image units allowed.
1320 * Check: any two active samplers in the current program object are of
1321 * different types, but refer to the same texture image unit,
1323 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1331 * Called via glValidateProgram()
1334 validate_program(struct gl_context
*ctx
, GLuint program
)
1336 struct gl_shader_program
*shProg
;
1337 char errMsg
[100] = "";
1339 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1344 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1345 if (!shProg
->data
->Validated
) {
1346 /* update info log */
1347 if (shProg
->data
->InfoLog
) {
1348 ralloc_free(shProg
->data
->InfoLog
);
1350 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1356 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1358 GET_CURRENT_CONTEXT(ctx
);
1359 attach_shader_no_error(ctx
, program
, shader
);
1364 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1366 GET_CURRENT_CONTEXT(ctx
);
1367 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1372 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1374 GET_CURRENT_CONTEXT(ctx
);
1375 attach_shader_no_error(ctx
, program
, shader
);
1380 _mesa_AttachShader(GLuint program
, GLuint shader
)
1382 GET_CURRENT_CONTEXT(ctx
);
1383 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1388 _mesa_CompileShader(GLuint shaderObj
)
1390 GET_CURRENT_CONTEXT(ctx
);
1391 if (MESA_VERBOSE
& VERBOSE_API
)
1392 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1393 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1394 "glCompileShader"));
1399 _mesa_CreateShader(GLenum type
)
1401 GET_CURRENT_CONTEXT(ctx
);
1403 if (MESA_VERBOSE
& VERBOSE_API
)
1404 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1406 return create_shader_err(ctx
, type
, "glCreateShader");
1410 GLhandleARB GLAPIENTRY
1411 _mesa_CreateShaderObjectARB(GLenum type
)
1413 GET_CURRENT_CONTEXT(ctx
);
1414 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1419 _mesa_CreateProgram(void)
1421 GET_CURRENT_CONTEXT(ctx
);
1422 if (MESA_VERBOSE
& VERBOSE_API
)
1423 _mesa_debug(ctx
, "glCreateProgram\n");
1424 return create_shader_program(ctx
);
1428 GLhandleARB GLAPIENTRY
1429 _mesa_CreateProgramObjectARB(void)
1431 GET_CURRENT_CONTEXT(ctx
);
1432 return create_shader_program(ctx
);
1437 _mesa_DeleteObjectARB(GLhandleARB obj
)
1439 if (MESA_VERBOSE
& VERBOSE_API
) {
1440 GET_CURRENT_CONTEXT(ctx
);
1441 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1445 GET_CURRENT_CONTEXT(ctx
);
1446 FLUSH_VERTICES(ctx
, 0);
1447 if (is_program(ctx
, obj
)) {
1448 delete_shader_program(ctx
, obj
);
1450 else if (is_shader(ctx
, obj
)) {
1451 delete_shader(ctx
, obj
);
1461 _mesa_DeleteProgram(GLuint name
)
1464 GET_CURRENT_CONTEXT(ctx
);
1465 FLUSH_VERTICES(ctx
, 0);
1466 delete_shader_program(ctx
, name
);
1472 _mesa_DeleteShader(GLuint name
)
1475 GET_CURRENT_CONTEXT(ctx
);
1476 FLUSH_VERTICES(ctx
, 0);
1477 delete_shader(ctx
, name
);
1483 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1485 GET_CURRENT_CONTEXT(ctx
);
1486 detach_shader(ctx
, program
, shader
);
1491 _mesa_DetachShader(GLuint program
, GLuint shader
)
1493 GET_CURRENT_CONTEXT(ctx
);
1494 detach_shader(ctx
, program
, shader
);
1499 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1500 GLsizei
* count
, GLhandleARB
* obj
)
1502 GET_CURRENT_CONTEXT(ctx
);
1503 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1508 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1509 GLsizei
*count
, GLuint
*obj
)
1511 GET_CURRENT_CONTEXT(ctx
);
1512 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1517 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1518 GLcharARB
* infoLog
)
1520 GET_CURRENT_CONTEXT(ctx
);
1521 if (is_program(ctx
, object
)) {
1522 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1524 else if (is_shader(ctx
, object
)) {
1525 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1528 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1534 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1536 GET_CURRENT_CONTEXT(ctx
);
1537 /* Implement in terms of GetProgramiv, GetShaderiv */
1538 if (is_program(ctx
, object
)) {
1539 if (pname
== GL_OBJECT_TYPE_ARB
) {
1540 *params
= GL_PROGRAM_OBJECT_ARB
;
1543 get_programiv(ctx
, object
, pname
, params
);
1546 else if (is_shader(ctx
, object
)) {
1547 if (pname
== GL_OBJECT_TYPE_ARB
) {
1548 *params
= GL_SHADER_OBJECT_ARB
;
1551 get_shaderiv(ctx
, object
, pname
, params
);
1555 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1561 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1564 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1565 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1566 params
[0] = (GLfloat
) iparams
[0];
1571 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1573 GET_CURRENT_CONTEXT(ctx
);
1574 get_programiv(ctx
, program
, pname
, params
);
1579 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1581 GET_CURRENT_CONTEXT(ctx
);
1582 get_shaderiv(ctx
, shader
, pname
, params
);
1587 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1588 GLsizei
*length
, GLchar
*infoLog
)
1590 GET_CURRENT_CONTEXT(ctx
);
1591 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1596 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1597 GLsizei
*length
, GLchar
*infoLog
)
1599 GET_CURRENT_CONTEXT(ctx
);
1600 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1605 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1606 GLsizei
*length
, GLchar
*sourceOut
)
1608 GET_CURRENT_CONTEXT(ctx
);
1609 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1613 GLhandleARB GLAPIENTRY
1614 _mesa_GetHandleARB(GLenum pname
)
1616 GET_CURRENT_CONTEXT(ctx
);
1617 return get_handle(ctx
, pname
);
1621 GLboolean GLAPIENTRY
1622 _mesa_IsProgram(GLuint name
)
1624 GET_CURRENT_CONTEXT(ctx
);
1625 return is_program(ctx
, name
);
1629 GLboolean GLAPIENTRY
1630 _mesa_IsShader(GLuint name
)
1632 GET_CURRENT_CONTEXT(ctx
);
1633 return is_shader(ctx
, name
);
1638 _mesa_LinkProgram(GLuint programObj
)
1640 GET_CURRENT_CONTEXT(ctx
);
1641 if (MESA_VERBOSE
& VERBOSE_API
)
1642 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1643 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1647 #ifdef ENABLE_SHADER_CACHE
1649 * Generate a SHA-1 hash value string for given source string.
1652 generate_sha1(const char *source
, char sha_str
[64])
1654 unsigned char sha
[20];
1655 _mesa_sha1_compute(source
, strlen(source
), sha
);
1656 _mesa_sha1_format(sha_str
, sha
);
1660 * Construct a full path for shader replacement functionality using
1663 * <path>/<stage prefix>_<CHECKSUM>.glsl
1666 construct_name(const gl_shader_stage stage
, const char *source
,
1670 static const char *types
[] = {
1671 "VS", "TC", "TE", "GS", "FS", "CS",
1674 generate_sha1(source
, sha
);
1675 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1679 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1682 dump_shader(const gl_shader_stage stage
, const char *source
)
1684 static bool path_exists
= true;
1691 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1693 path_exists
= false;
1697 char *name
= construct_name(stage
, source
, dump_path
);
1699 f
= fopen(name
, "w");
1704 GET_CURRENT_CONTEXT(ctx
);
1705 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1712 * Read shader source code from a file.
1713 * Useful for debugging to override an app's shader.
1716 read_shader(const gl_shader_stage stage
, const char *source
)
1719 static bool path_exists
= true;
1720 int len
, shader_size
= 0;
1727 read_path
= getenv("MESA_SHADER_READ_PATH");
1729 path_exists
= false;
1733 char *name
= construct_name(stage
, source
, read_path
);
1734 f
= fopen(name
, "r");
1739 /* allocate enough room for the entire shader */
1740 fseek(f
, 0, SEEK_END
);
1741 shader_size
= ftell(f
);
1743 assert(shader_size
);
1745 /* add one for terminating zero */
1748 buffer
= malloc(shader_size
);
1751 len
= fread(buffer
, 1, shader_size
, f
);
1759 #endif /* ENABLE_SHADER_CACHE */
1762 * Called via glShaderSource() and glShaderSourceARB() API functions.
1763 * Basically, concatenate the source code strings into one long string
1764 * and pass it to _mesa_shader_source().
1767 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1768 const GLchar
* const * string
, const GLint
* length
)
1770 GET_CURRENT_CONTEXT(ctx
);
1772 GLsizei i
, totalLength
;
1774 struct gl_shader
*sh
;
1776 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1780 if (string
== NULL
) {
1781 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1786 * This array holds offsets of where the appropriate string ends, thus the
1787 * last element will be set to the total length of the source code.
1789 offsets
= malloc(count
* sizeof(GLint
));
1790 if (offsets
== NULL
) {
1791 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1795 for (i
= 0; i
< count
; i
++) {
1796 if (string
[i
] == NULL
) {
1797 free((GLvoid
*) offsets
);
1798 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1799 "glShaderSourceARB(null string)");
1802 if (length
== NULL
|| length
[i
] < 0)
1803 offsets
[i
] = strlen(string
[i
]);
1805 offsets
[i
] = length
[i
];
1806 /* accumulate string lengths */
1808 offsets
[i
] += offsets
[i
- 1];
1811 /* Total length of source string is sum off all strings plus two.
1812 * One extra byte for terminating zero, another extra byte to silence
1813 * valgrind warnings in the parser/grammer code.
1815 totalLength
= offsets
[count
- 1] + 2;
1816 source
= malloc(totalLength
* sizeof(GLcharARB
));
1817 if (source
== NULL
) {
1818 free((GLvoid
*) offsets
);
1819 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1823 for (i
= 0; i
< count
; i
++) {
1824 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1825 memcpy(source
+ start
, string
[i
],
1826 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1828 source
[totalLength
- 1] = '\0';
1829 source
[totalLength
- 2] = '\0';
1831 #ifdef ENABLE_SHADER_CACHE
1832 GLcharARB
*replacement
;
1834 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1835 * if corresponding entry found from MESA_SHADER_READ_PATH.
1837 dump_shader(sh
->Stage
, source
);
1839 replacement
= read_shader(sh
->Stage
, source
);
1842 source
= replacement
;
1844 #endif /* ENABLE_SHADER_CACHE */
1846 shader_source(sh
, source
);
1852 static ALWAYS_INLINE
void
1853 use_program(GLuint program
, bool no_error
)
1855 GET_CURRENT_CONTEXT(ctx
);
1856 struct gl_shader_program
*shProg
= NULL
;
1858 if (MESA_VERBOSE
& VERBOSE_API
)
1859 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1863 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1866 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1867 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1868 "glUseProgram(transform feedback active)");
1874 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1878 if (!shProg
->data
->LinkStatus
) {
1879 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1880 "glUseProgram(program %u not linked)", program
);
1885 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1886 print_shader_info(shProg
);
1891 /* The ARB_separate_shader_object spec says:
1893 * "The executable code for an individual shader stage is taken from
1894 * the current program for that stage. If there is a current program
1895 * object established by UseProgram, that program is considered current
1896 * for all stages. Otherwise, if there is a bound program pipeline
1897 * object (section 2.14.PPO), the program bound to the appropriate
1898 * stage of the pipeline object is considered current."
1901 /* Attach shader state to the binding point */
1902 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1903 /* Update the program */
1904 _mesa_use_shader_program(ctx
, shProg
);
1906 /* Must be done first: detach the progam */
1907 _mesa_use_shader_program(ctx
, shProg
);
1908 /* Unattach shader_state binding point */
1909 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1910 ctx
->Pipeline
.Default
);
1911 /* If a pipeline was bound, rebind it */
1912 if (ctx
->Pipeline
.Current
) {
1914 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
1916 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1923 _mesa_UseProgram_no_error(GLuint program
)
1925 use_program(program
, true);
1930 _mesa_UseProgram(GLuint program
)
1932 use_program(program
, false);
1937 _mesa_ValidateProgram(GLuint program
)
1939 GET_CURRENT_CONTEXT(ctx
);
1940 validate_program(ctx
, program
);
1945 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1948 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1949 GLint
* range
, GLint
* precision
)
1951 const struct gl_program_constants
*limits
;
1952 const struct gl_precision
*p
;
1953 GET_CURRENT_CONTEXT(ctx
);
1955 switch (shadertype
) {
1956 case GL_VERTEX_SHADER
:
1957 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1959 case GL_FRAGMENT_SHADER
:
1960 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1963 _mesa_error(ctx
, GL_INVALID_ENUM
,
1964 "glGetShaderPrecisionFormat(shadertype)");
1968 switch (precisiontype
) {
1970 p
= &limits
->LowFloat
;
1972 case GL_MEDIUM_FLOAT
:
1973 p
= &limits
->MediumFloat
;
1976 p
= &limits
->HighFloat
;
1979 p
= &limits
->LowInt
;
1982 p
= &limits
->MediumInt
;
1985 p
= &limits
->HighInt
;
1988 _mesa_error(ctx
, GL_INVALID_ENUM
,
1989 "glGetShaderPrecisionFormat(precisiontype)");
1993 range
[0] = p
->RangeMin
;
1994 range
[1] = p
->RangeMax
;
1995 precision
[0] = p
->Precision
;
2000 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2003 _mesa_ReleaseShaderCompiler(void)
2005 _mesa_destroy_shader_compiler_caches();
2010 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2013 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2014 const void* binary
, GLint length
)
2016 GET_CURRENT_CONTEXT(ctx
);
2018 (void) binaryformat
;
2021 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2022 * page 88 of the OpenGL 4.5 specs state:
2024 * "An INVALID_VALUE error is generated if count or length is negative.
2025 * An INVALID_ENUM error is generated if binaryformat is not a supported
2026 * format returned in SHADER_BINARY_FORMATS."
2028 if (n
< 0 || length
< 0) {
2029 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2033 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2038 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2039 GLenum
*binaryFormat
, GLvoid
*binary
)
2041 struct gl_shader_program
*shProg
;
2042 GLsizei length_dummy
;
2043 GET_CURRENT_CONTEXT(ctx
);
2046 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2050 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2054 /* The ARB_get_program_binary spec says:
2056 * "If <length> is NULL, then no length is returned."
2058 * Ensure that length always points to valid storage to avoid multiple NULL
2059 * pointer checks below.
2062 length
= &length_dummy
;
2065 /* The ARB_get_program_binary spec says:
2067 * "When a program object's LINK_STATUS is FALSE, its program binary
2068 * length is zero, and a call to GetProgramBinary will generate an
2069 * INVALID_OPERATION error.
2071 if (!shProg
->data
->LinkStatus
) {
2072 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2073 "glGetProgramBinary(program %u not linked)",
2080 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2081 "glGetProgramBinary(driver supports zero binary formats)");
2083 (void) binaryFormat
;
2088 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2089 const GLvoid
*binary
, GLsizei length
)
2091 struct gl_shader_program
*shProg
;
2092 GET_CURRENT_CONTEXT(ctx
);
2094 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2098 (void) binaryFormat
;
2101 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2103 * "If a negative number is provided where an argument of type sizei or
2104 * sizeiptr is specified, an INVALID_VALUE error is generated."
2107 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2111 /* The ARB_get_program_binary spec says:
2113 * "<binaryFormat> and <binary> must be those returned by a previous
2114 * call to GetProgramBinary, and <length> must be the length of the
2115 * program binary as returned by GetProgramBinary or GetProgramiv with
2116 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2117 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2120 * Since any value of binaryFormat passed "is not one of those specified as
2121 * allowable for [this] command, an INVALID_ENUM error is generated."
2123 shProg
->data
->LinkStatus
= linking_failure
;
2124 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2129 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2131 struct gl_shader_program
*shProg
;
2132 GET_CURRENT_CONTEXT(ctx
);
2134 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2135 "glProgramParameteri");
2140 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2141 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2142 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2143 * even be in the dispatch table, so we shouldn't need to expclicitly
2146 * On desktop, we ignore the 3.0+ requirement because it is silly.
2149 /* The ARB_get_program_binary extension spec says:
2151 * "An INVALID_VALUE error is generated if the <value> argument to
2152 * ProgramParameteri is not TRUE or FALSE."
2154 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2158 /* No need to notify the driver. Any changes will actually take effect
2159 * the next time the shader is linked.
2161 * The ARB_get_program_binary extension spec says:
2163 * "To indicate that a program binary is likely to be retrieved,
2164 * ProgramParameteri should be called with <pname>
2165 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2166 * will not be in effect until the next time LinkProgram or
2167 * ProgramBinary has been called successfully."
2169 * The resloution of issue 9 in the extension spec also says:
2171 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2172 * to indicate to the GL implementation that this program will
2173 * likely be saved with GetProgramBinary at some point. This will
2174 * give the GL implementation the opportunity to track any state
2175 * changes made to the program before being saved such that when it
2176 * is loaded again a recompile can be avoided."
2178 shProg
->BinaryRetreivableHint
= value
;
2181 case GL_PROGRAM_SEPARABLE
:
2182 /* Spec imply that the behavior is the same as ARB_get_program_binary
2183 * Chapter 7.3 Program Objects
2185 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2188 shProg
->SeparateShader
= value
;
2192 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2193 _mesa_enum_to_string(pname
));
2198 _mesa_error(ctx
, GL_INVALID_VALUE
,
2199 "glProgramParameteri(pname=%s, value=%d): "
2200 "value must be 0 or 1.",
2201 _mesa_enum_to_string(pname
),
2207 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2208 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2209 struct gl_pipeline_object
*shTarget
)
2211 struct gl_program
**target
;
2213 target
= &shTarget
->CurrentProgram
[stage
];
2215 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2218 if (*target
!= prog
) {
2219 /* Program is current, flush it */
2220 if (shTarget
== ctx
->_Shader
) {
2221 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2224 _mesa_reference_shader_program(ctx
,
2225 &shTarget
->ReferencedPrograms
[stage
],
2227 _mesa_reference_program(ctx
, target
, prog
);
2235 * Copy program-specific data generated by linking from the gl_shader_program
2236 * object to the gl_program object referred to by the gl_linked_shader.
2238 * This function expects _mesa_reference_program() to have been previously
2239 * called setting the gl_linked_shaders program reference.
2242 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2243 struct gl_linked_shader
*dst_sh
)
2245 assert(dst_sh
->Program
);
2247 struct gl_program
*dst
= dst_sh
->Program
;
2249 dst
->info
.separate_shader
= src
->SeparateShader
;
2251 switch (dst_sh
->Stage
) {
2252 case MESA_SHADER_GEOMETRY
: {
2253 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2254 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2255 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2258 case MESA_SHADER_FRAGMENT
: {
2259 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2262 case MESA_SHADER_COMPUTE
: {
2263 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2272 * ARB_separate_shader_objects: Compile & Link Program
2275 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2276 const GLchar
* const *strings
)
2278 GET_CURRENT_CONTEXT(ctx
);
2280 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2284 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2285 * GL_INVALID_VALUE should be generated if count < 0
2288 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2293 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2295 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2296 _mesa_compile_shader(ctx
, sh
);
2298 program
= create_shader_program(ctx
);
2300 struct gl_shader_program
*shProg
;
2301 GLint compiled
= GL_FALSE
;
2303 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2305 shProg
->SeparateShader
= GL_TRUE
;
2307 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2309 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2310 _mesa_link_program(ctx
, shProg
);
2311 detach_shader(ctx
, program
, shader
);
2315 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2316 append
-error
-to
-info
-log
;
2317 shProg
->data
->LinkStatus
= linking_failure
;
2322 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2325 delete_shader(ctx
, shader
);
2333 * For GL_ARB_tessellation_shader
2335 extern void GLAPIENTRY
2336 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2338 GET_CURRENT_CONTEXT(ctx
);
2340 if (!_mesa_has_tessellation(ctx
)) {
2341 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2345 if (pname
!= GL_PATCH_VERTICES
) {
2346 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2350 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2351 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2355 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2359 extern void GLAPIENTRY
2360 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2362 GET_CURRENT_CONTEXT(ctx
);
2364 if (!_mesa_has_tessellation(ctx
)) {
2365 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2370 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2371 FLUSH_VERTICES(ctx
, 0);
2372 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2373 4 * sizeof(GLfloat
));
2374 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2376 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2377 FLUSH_VERTICES(ctx
, 0);
2378 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2379 2 * sizeof(GLfloat
));
2380 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2383 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2389 * ARB_shader_subroutine
2392 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2395 GET_CURRENT_CONTEXT(ctx
);
2396 const char *api_name
= "glGetSubroutineUniformLocation";
2397 struct gl_shader_program
*shProg
;
2398 GLenum resource_type
;
2399 gl_shader_stage stage
;
2401 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2402 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2406 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2410 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2411 if (!shProg
->_LinkedShaders
[stage
]) {
2412 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2416 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2417 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2421 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2424 GET_CURRENT_CONTEXT(ctx
);
2425 const char *api_name
= "glGetSubroutineIndex";
2426 struct gl_shader_program
*shProg
;
2427 struct gl_program_resource
*res
;
2428 GLenum resource_type
;
2429 gl_shader_stage stage
;
2431 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2436 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2440 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2441 if (!shProg
->_LinkedShaders
[stage
]) {
2442 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2446 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2447 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2452 return _mesa_program_resource_index(shProg
, res
);
2457 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2458 GLuint index
, GLenum pname
, GLint
*values
)
2460 GET_CURRENT_CONTEXT(ctx
);
2461 const char *api_name
= "glGetActiveSubroutineUniformiv";
2462 struct gl_shader_program
*shProg
;
2463 struct gl_linked_shader
*sh
;
2464 gl_shader_stage stage
;
2465 struct gl_program_resource
*res
;
2466 const struct gl_uniform_storage
*uni
;
2467 GLenum resource_type
;
2470 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2471 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2475 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2479 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2480 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2482 sh
= shProg
->_LinkedShaders
[stage
];
2484 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2488 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2489 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2490 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2495 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2496 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2499 values
[0] = uni
->num_compatible_subroutines
;
2503 case GL_COMPATIBLE_SUBROUTINES
: {
2504 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2508 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2509 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2510 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2511 if (fn
->types
[j
] == uni
->type
) {
2512 values
[count
++] = i
;
2520 case GL_UNIFORM_SIZE
:
2521 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2524 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2527 case GL_UNIFORM_NAME_LENGTH
:
2528 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2530 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2531 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2535 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2542 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2543 GLuint index
, GLsizei bufsize
,
2544 GLsizei
*length
, GLchar
*name
)
2546 GET_CURRENT_CONTEXT(ctx
);
2547 const char *api_name
= "glGetActiveSubroutineUniformName";
2548 struct gl_shader_program
*shProg
;
2549 GLenum resource_type
;
2550 gl_shader_stage stage
;
2552 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2553 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2557 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2561 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2562 if (!shProg
->_LinkedShaders
[stage
]) {
2563 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2567 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2568 /* get program resource name */
2569 _mesa_get_program_resource_name(shProg
, resource_type
,
2571 length
, name
, api_name
);
2576 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2577 GLuint index
, GLsizei bufsize
,
2578 GLsizei
*length
, GLchar
*name
)
2580 GET_CURRENT_CONTEXT(ctx
);
2581 const char *api_name
= "glGetActiveSubroutineName";
2582 struct gl_shader_program
*shProg
;
2583 GLenum resource_type
;
2584 gl_shader_stage stage
;
2586 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2587 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2591 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2595 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2596 if (!shProg
->_LinkedShaders
[stage
]) {
2597 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2600 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2601 _mesa_get_program_resource_name(shProg
, resource_type
,
2603 length
, name
, api_name
);
2607 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2608 const GLuint
*indices
)
2610 GET_CURRENT_CONTEXT(ctx
);
2611 const char *api_name
= "glUniformSubroutinesuiv";
2612 gl_shader_stage stage
;
2615 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2616 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2620 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2621 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2623 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2627 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2628 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2633 bool flushed
= false;
2635 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2642 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2646 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2649 for (j
= i
; j
< i
+ uni_count
; j
++) {
2650 struct gl_subroutine_function
*subfn
= NULL
;
2651 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2652 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2656 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2657 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2658 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2665 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2666 if (subfn
->types
[k
] == uni
->type
)
2669 if (k
== subfn
->num_compat_types
) {
2670 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2674 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2682 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2685 GET_CURRENT_CONTEXT(ctx
);
2686 const char *api_name
= "glGetUniformSubroutineuiv";
2687 gl_shader_stage stage
;
2689 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2690 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2694 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2695 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2697 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2701 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2702 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2706 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2711 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2712 GLenum pname
, GLint
*values
)
2714 GET_CURRENT_CONTEXT(ctx
);
2715 const char *api_name
= "glGetProgramStageiv";
2716 struct gl_shader_program
*shProg
;
2717 struct gl_linked_shader
*sh
;
2718 gl_shader_stage stage
;
2720 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2721 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2725 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2729 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2730 sh
= shProg
->_LinkedShaders
[stage
];
2732 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2733 * INVALID_OPERATION in the case of not be linked.
2735 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2736 * same info using other specs (ARB_program_interface_query), without the
2737 * need of the program to be linked, being the value for that case 0.
2739 * But at the same time, some other methods require the program to be
2740 * linked for pname related to locations, so it would be inconsistent to
2741 * not do the same here. So we are:
2742 * * Return GL_INVALID_OPERATION if not linked only for locations.
2743 * * Setting a default value of 0, to be returned if not linked.
2747 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2748 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2753 struct gl_program
*p
= sh
->Program
;
2755 case GL_ACTIVE_SUBROUTINES
:
2756 values
[0] = p
->sh
.NumSubroutineFunctions
;
2758 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2759 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2761 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2762 values
[0] = p
->sh
.NumSubroutineUniforms
;
2764 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2768 GLenum resource_type
;
2769 struct gl_program_resource
*res
;
2771 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2772 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2773 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2775 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2780 values
[0] = max_len
;
2783 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2787 GLenum resource_type
;
2788 struct gl_program_resource
*res
;
2790 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2791 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2792 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2794 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2795 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2801 values
[0] = max_len
;
2805 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2812 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2816 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2817 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2818 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2819 if (fn
->types
[j
] == type
)
2827 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2828 struct gl_program
*p
)
2832 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2837 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2846 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2847 for (j
= 0; j
< uni_count
; j
++) {
2848 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2849 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2852 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2854 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2858 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2859 gl_shader_stage stage
)
2861 if (ctx
->_Shader
->CurrentProgram
[stage
])
2862 _mesa_shader_write_subroutine_index(ctx
,
2863 ctx
->_Shader
->CurrentProgram
[stage
]);
2867 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2868 struct gl_program
*p
)
2872 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2873 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2874 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2875 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2876 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2879 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2880 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2885 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);