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
, "%s", 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
, "%s", 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
);
426 static ALWAYS_INLINE
void
427 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
430 struct gl_shader_program
*shProg
;
435 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
439 shProg
= _mesa_lookup_shader_program(ctx
, program
);
442 n
= shProg
->NumShaders
;
444 for (i
= 0; i
< n
; i
++) {
445 if (shProg
->Shaders
[i
]->Name
== shader
) {
447 struct gl_shader
**newList
;
450 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
452 /* alloc new, smaller array */
453 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
455 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
458 /* Copy old list entries to new list, skipping removed entry at [i] */
459 for (j
= 0; j
< i
; j
++) {
460 newList
[j
] = shProg
->Shaders
[j
];
463 newList
[j
++] = shProg
->Shaders
[i
];
466 /* Free old list and install new one */
467 free(shProg
->Shaders
);
468 shProg
->Shaders
= newList
;
469 shProg
->NumShaders
= n
- 1;
472 /* sanity check - make sure the new list's entries are sensible */
473 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
474 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
475 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
476 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
477 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
478 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
479 assert(shProg
->Shaders
[j
]->RefCount
> 0);
490 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
491 err
= GL_INVALID_OPERATION
;
493 err
= GL_INVALID_VALUE
;
494 _mesa_error(ctx
, err
, "glDetachShader(shader)");
501 detach_shader_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
503 detach_shader(ctx
, program
, shader
, false);
508 detach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
510 detach_shader(ctx
, program
, shader
, true);
515 * Return list of shaders attached to shader program.
518 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
519 GLsizei
*count
, GLuint
*obj
)
521 struct gl_shader_program
*shProg
;
524 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
529 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
533 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
534 obj
[i
] = shProg
->Shaders
[i
]->Name
;
543 * glGetHandleARB() - return ID/name of currently bound shader program.
546 get_handle(struct gl_context
*ctx
, GLenum pname
)
548 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
549 if (ctx
->_Shader
->ActiveProgram
)
550 return ctx
->_Shader
->ActiveProgram
->Name
;
555 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
562 * Check if a geometry shader query is valid at this time. If not, report an
563 * error and return false.
565 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
567 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
568 * are queried for a program which has not been linked successfully, or
569 * which does not contain objects to form a geometry shader, then an
570 * INVALID_OPERATION error is generated."
573 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
575 if (shProg
->data
->LinkStatus
&&
576 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
581 "glGetProgramv(linked geometry shader required)");
587 * Check if a tessellation control shader query is valid at this time.
588 * If not, report an error and return false.
590 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
592 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
593 * not been linked successfully, or which does not contain objects to
594 * form a tessellation control shader, then an INVALID_OPERATION error is
598 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
600 if (shProg
->data
->LinkStatus
&&
601 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
605 _mesa_error(ctx
, GL_INVALID_OPERATION
,
606 "glGetProgramv(linked tessellation control shader required)");
612 * Check if a tessellation evaluation shader query is valid at this time.
613 * If not, report an error and return false.
615 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
617 * "If any of the pname values in this paragraph are queried for a program
618 * which has not been linked successfully, or which does not contain
619 * objects to form a tessellation evaluation shader, then an
620 * INVALID_OPERATION error is generated."
624 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
626 if (shProg
->data
->LinkStatus
&&
627 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
631 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
632 "evaluation shader required)");
638 * glGetProgramiv() - get shader program state.
639 * Note that this is for GLSL shader programs, not ARB vertex/fragment
640 * programs (see glGetProgramivARB).
643 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
646 struct gl_shader_program
*shProg
647 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
649 /* Is transform feedback available in this context?
652 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
653 || ctx
->API
== API_OPENGL_CORE
654 || _mesa_is_gles3(ctx
);
656 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
657 * and GL 3.2) are available in this context
659 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
660 const bool has_tess
= _mesa_has_tessellation(ctx
);
662 /* Are uniform buffer objects available in this context?
665 (ctx
->API
== API_OPENGL_COMPAT
&&
666 ctx
->Extensions
.ARB_uniform_buffer_object
)
667 || ctx
->API
== API_OPENGL_CORE
668 || _mesa_is_gles3(ctx
);
675 case GL_DELETE_STATUS
:
676 *params
= shProg
->DeletePending
;
679 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
681 case GL_VALIDATE_STATUS
:
682 *params
= shProg
->data
->Validated
;
684 case GL_INFO_LOG_LENGTH
:
685 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
686 strlen(shProg
->data
->InfoLog
) + 1 : 0;
688 case GL_ATTACHED_SHADERS
:
689 *params
= shProg
->NumShaders
;
691 case GL_ACTIVE_ATTRIBUTES
:
692 *params
= _mesa_count_active_attribs(shProg
);
694 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
695 *params
= _mesa_longest_attribute_name_length(shProg
);
697 case GL_ACTIVE_UNIFORMS
: {
699 const unsigned num_uniforms
=
700 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
701 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
702 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
707 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
710 const unsigned num_uniforms
=
711 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
713 for (i
= 0; i
< num_uniforms
; i
++) {
714 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
717 /* Add one for the terminating NUL character for a non-array, and
718 * 4 for the "[0]" and the NUL for an array.
720 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
721 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
730 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
733 *params
= shProg
->TransformFeedback
.NumVarying
;
735 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
741 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
742 /* Add one for the terminating NUL character.
745 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
754 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
757 *params
= shProg
->TransformFeedback
.BufferMode
;
759 case GL_GEOMETRY_VERTICES_OUT
:
762 if (check_gs_query(ctx
, shProg
)) {
763 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
764 Program
->info
.gs
.vertices_out
;
767 case GL_GEOMETRY_SHADER_INVOCATIONS
:
768 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
770 if (check_gs_query(ctx
, shProg
)) {
771 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
772 Program
->info
.gs
.invocations
;
775 case GL_GEOMETRY_INPUT_TYPE
:
778 if (check_gs_query(ctx
, shProg
)) {
779 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
780 Program
->info
.gs
.input_primitive
;
783 case GL_GEOMETRY_OUTPUT_TYPE
:
786 if (check_gs_query(ctx
, shProg
)) {
787 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
788 Program
->info
.gs
.output_primitive
;
791 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
798 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
799 /* Add one for the terminating NUL character.
801 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
810 case GL_ACTIVE_UNIFORM_BLOCKS
:
814 *params
= shProg
->data
->NumUniformBlocks
;
816 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
817 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
818 * only available with desktop OpenGL 3.0+ with the
819 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
821 * On desktop, we ignore the 3.0+ requirement because it is silly.
823 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
826 *params
= shProg
->BinaryRetreivableHint
;
828 case GL_PROGRAM_BINARY_LENGTH
:
831 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
832 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
835 *params
= shProg
->data
->NumAtomicBuffers
;
837 case GL_COMPUTE_WORK_GROUP_SIZE
: {
839 if (!_mesa_has_compute_shaders(ctx
))
841 if (!shProg
->data
->LinkStatus
) {
842 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
846 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
847 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
851 for (i
= 0; i
< 3; i
++)
852 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
853 Program
->info
.cs
.local_size
[i
];
856 case GL_PROGRAM_SEPARABLE
:
857 /* If the program has not been linked, return initial value 0. */
858 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
861 /* ARB_tessellation_shader */
862 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
865 if (check_tcs_query(ctx
, shProg
)) {
866 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
867 Program
->info
.tess
.tcs_vertices_out
;
870 case GL_TESS_GEN_MODE
:
873 if (check_tes_query(ctx
, shProg
)) {
874 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
875 Program
->info
.tess
.primitive_mode
;
878 case GL_TESS_GEN_SPACING
:
881 if (check_tes_query(ctx
, shProg
)) {
882 const struct gl_linked_shader
*tes
=
883 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
884 switch (tes
->Program
->info
.tess
.spacing
) {
885 case TESS_SPACING_EQUAL
:
888 case TESS_SPACING_FRACTIONAL_ODD
:
889 *params
= GL_FRACTIONAL_ODD
;
891 case TESS_SPACING_FRACTIONAL_EVEN
:
892 *params
= GL_FRACTIONAL_EVEN
;
894 case TESS_SPACING_UNSPECIFIED
:
900 case GL_TESS_GEN_VERTEX_ORDER
:
903 if (check_tes_query(ctx
, shProg
)) {
904 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
905 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
908 case GL_TESS_GEN_POINT_MODE
:
911 if (check_tes_query(ctx
, shProg
)) {
912 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
913 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
920 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
921 _mesa_enum_to_string(pname
));
926 * glGetShaderiv() - get GLSL shader state
929 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
931 struct gl_shader
*shader
=
932 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
940 *params
= shader
->Type
;
942 case GL_DELETE_STATUS
:
943 *params
= shader
->DeletePending
;
945 case GL_COMPILE_STATUS
:
946 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
948 case GL_INFO_LOG_LENGTH
:
949 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
950 strlen(shader
->InfoLog
) + 1 : 0;
952 case GL_SHADER_SOURCE_LENGTH
:
953 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
956 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
963 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
964 GLsizei
*length
, GLchar
*infoLog
)
966 struct gl_shader_program
*shProg
;
968 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
969 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
971 * "If a negative number is provided where an argument of type sizei or
972 * sizeiptr is specified, an INVALID_VALUE error is generated."
975 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
979 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
980 "glGetProgramInfoLog(program)");
985 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
990 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
991 GLsizei
*length
, GLchar
*infoLog
)
993 struct gl_shader
*sh
;
995 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
996 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
998 * "If a negative number is provided where an argument of type sizei or
999 * sizeiptr is specified, an INVALID_VALUE error is generated."
1002 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
1006 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
1011 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
1016 * Return shader source code.
1019 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1020 GLsizei
*length
, GLchar
*sourceOut
)
1022 struct gl_shader
*sh
;
1024 if (maxLength
< 0) {
1025 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1029 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1033 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1038 * Set/replace shader source code. A helper function used by
1039 * glShaderSource[ARB].
1042 set_shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1046 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
1047 /* If shader was previously compiled back-up the source in case of cache
1050 sh
->FallbackSource
= sh
->Source
;
1051 sh
->Source
= source
;
1053 /* free old shader source string and install new one */
1054 free((void *)sh
->Source
);
1055 sh
->Source
= source
;
1059 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1068 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1074 /* If the user called glCompileShader without first calling
1075 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1077 sh
->CompileStatus
= compile_failure
;
1079 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1080 _mesa_log("GLSL source for %s shader %d:\n",
1081 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1082 _mesa_log("%s\n", sh
->Source
);
1085 /* this call will set the shader->CompileStatus field to indicate if
1086 * compilation was successful.
1088 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1090 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1091 _mesa_write_shader_to_file(sh
);
1094 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1095 if (sh
->CompileStatus
) {
1097 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1098 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1100 _mesa_log("No GLSL IR for shader %d (shader may be from "
1101 "cache)\n", sh
->Name
);
1105 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1107 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1108 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1109 _mesa_log("%s\n", sh
->InfoLog
);
1114 if (!sh
->CompileStatus
) {
1115 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1116 _mesa_log("GLSL source for %s shader %d:\n",
1117 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1118 _mesa_log("%s\n", sh
->Source
);
1119 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1122 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1123 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1124 sh
->Name
, sh
->InfoLog
);
1131 * Link a program's shaders.
1134 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1139 /* From the ARB_transform_feedback2 specification:
1140 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1141 * the name of a program being used by one or more transform feedback
1142 * objects, even if the objects are not currently bound or are paused."
1144 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1146 "glLinkProgram(transform feedback is using the program)");
1150 unsigned programs_in_use
= 0;
1152 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1153 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1154 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1155 programs_in_use
|= 1 << stage
;
1159 FLUSH_VERTICES(ctx
, 0);
1160 _mesa_glsl_link_shader(ctx
, shProg
);
1162 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1164 * "If LinkProgram or ProgramBinary successfully re-links a program
1165 * object that is active for any shader stage, then the newly generated
1166 * executable code will be installed as part of the current rendering
1167 * state for all shader stages where the program is active.
1168 * Additionally, the newly generated executable code is made part of
1169 * the state of any program pipeline for all stages where the program
1172 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1173 while (programs_in_use
) {
1174 const int stage
= u_bit_scan(&programs_in_use
);
1176 struct gl_program
*prog
= NULL
;
1177 if (shProg
->_LinkedShaders
[stage
])
1178 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1180 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1184 /* Capture .shader_test files. */
1185 const char *capture_path
= _mesa_get_shader_capture_path();
1186 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1188 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1189 capture_path
, shProg
->Name
);
1190 file
= fopen(filename
, "w");
1192 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1193 shProg
->IsES
? " ES" : "",
1194 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1195 if (shProg
->SeparateShader
)
1196 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1197 fprintf(file
, "\n");
1199 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1200 fprintf(file
, "[%s shader]\n%s\n",
1201 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1202 shProg
->Shaders
[i
]->Source
);
1206 _mesa_warning(ctx
, "Failed to open %s", filename
);
1209 ralloc_free(filename
);
1212 if (shProg
->data
->LinkStatus
== linking_failure
&&
1213 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1214 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1215 shProg
->Name
, shProg
->data
->InfoLog
);
1222 printf("Link %u shaders in program %u: %s\n",
1223 shProg
->NumShaders
, shProg
->Name
,
1224 shProg
->data
->LinkStatus
? "Success" : "Failed");
1226 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1227 printf(" shader %u, stage %u\n",
1228 shProg
->Shaders
[i
]->Name
,
1229 shProg
->Shaders
[i
]->Stage
);
1236 * Print basic shader info (for debug).
1239 print_shader_info(const struct gl_shader_program
*shProg
)
1243 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1244 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1246 printf(" %s shader %u, checksum %u\n",
1247 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1248 shProg
->Shaders
[i
]->Name
,
1249 shProg
->Shaders
[i
]->SourceChecksum
);
1251 printf(" %s shader %u\n",
1252 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1253 shProg
->Shaders
[i
]->Name
);
1256 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1257 printf(" vert prog %u\n",
1258 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1259 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1260 printf(" frag prog %u\n",
1261 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1262 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1263 printf(" geom prog %u\n",
1264 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1265 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1266 printf(" tesc prog %u\n",
1267 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1268 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1269 printf(" tese prog %u\n",
1270 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1275 * Use the named shader program for subsequent glUniform calls
1278 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1281 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1282 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1283 "%s(program %u not linked)", caller
, shProg
->Name
);
1287 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1288 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1294 * Use the named shader program for subsequent rendering.
1297 _mesa_use_shader_program(struct gl_context
*ctx
,
1298 struct gl_shader_program
*shProg
)
1300 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1301 struct gl_program
*new_prog
= NULL
;
1302 if (shProg
&& shProg
->_LinkedShaders
[i
])
1303 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1304 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1306 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1311 * Do validation of the given shader program.
1312 * \param errMsg returns error message if validation fails.
1313 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1316 validate_shader_program(const struct gl_shader_program
*shProg
,
1319 if (!shProg
->data
->LinkStatus
) {
1323 /* From the GL spec, a program is invalid if any of these are true:
1325 any two active samplers in the current program object are of
1326 different types, but refer to the same texture image unit,
1328 any active sampler in the current program object refers to a texture
1329 image unit where fixed-function fragment processing accesses a
1330 texture target that does not match the sampler type, or
1332 the sum of the number of active samplers in the program and the
1333 number of texture image units enabled for fixed-function fragment
1334 processing exceeds the combined limit on the total number of texture
1335 image units allowed.
1339 * Check: any two active samplers in the current program object are of
1340 * different types, but refer to the same texture image unit,
1342 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1350 * Called via glValidateProgram()
1353 validate_program(struct gl_context
*ctx
, GLuint program
)
1355 struct gl_shader_program
*shProg
;
1356 char errMsg
[100] = "";
1358 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1363 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1364 if (!shProg
->data
->Validated
) {
1365 /* update info log */
1366 if (shProg
->data
->InfoLog
) {
1367 ralloc_free(shProg
->data
->InfoLog
);
1369 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1375 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1377 GET_CURRENT_CONTEXT(ctx
);
1378 attach_shader_no_error(ctx
, program
, shader
);
1383 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1385 GET_CURRENT_CONTEXT(ctx
);
1386 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1391 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1393 GET_CURRENT_CONTEXT(ctx
);
1394 attach_shader_no_error(ctx
, program
, shader
);
1399 _mesa_AttachShader(GLuint program
, GLuint shader
)
1401 GET_CURRENT_CONTEXT(ctx
);
1402 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1407 _mesa_CompileShader(GLuint shaderObj
)
1409 GET_CURRENT_CONTEXT(ctx
);
1410 if (MESA_VERBOSE
& VERBOSE_API
)
1411 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1412 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1413 "glCompileShader"));
1418 _mesa_CreateShader_no_error(GLenum type
)
1420 GET_CURRENT_CONTEXT(ctx
);
1421 return create_shader(ctx
, type
);
1426 _mesa_CreateShader(GLenum type
)
1428 GET_CURRENT_CONTEXT(ctx
);
1430 if (MESA_VERBOSE
& VERBOSE_API
)
1431 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1433 return create_shader_err(ctx
, type
, "glCreateShader");
1437 GLhandleARB GLAPIENTRY
1438 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1440 GET_CURRENT_CONTEXT(ctx
);
1441 return create_shader(ctx
, type
);
1445 GLhandleARB GLAPIENTRY
1446 _mesa_CreateShaderObjectARB(GLenum type
)
1448 GET_CURRENT_CONTEXT(ctx
);
1449 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1454 _mesa_CreateProgram(void)
1456 GET_CURRENT_CONTEXT(ctx
);
1457 if (MESA_VERBOSE
& VERBOSE_API
)
1458 _mesa_debug(ctx
, "glCreateProgram\n");
1459 return create_shader_program(ctx
);
1463 GLhandleARB GLAPIENTRY
1464 _mesa_CreateProgramObjectARB(void)
1466 GET_CURRENT_CONTEXT(ctx
);
1467 return create_shader_program(ctx
);
1472 _mesa_DeleteObjectARB(GLhandleARB obj
)
1474 if (MESA_VERBOSE
& VERBOSE_API
) {
1475 GET_CURRENT_CONTEXT(ctx
);
1476 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1480 GET_CURRENT_CONTEXT(ctx
);
1481 FLUSH_VERTICES(ctx
, 0);
1482 if (is_program(ctx
, obj
)) {
1483 delete_shader_program(ctx
, obj
);
1485 else if (is_shader(ctx
, obj
)) {
1486 delete_shader(ctx
, obj
);
1496 _mesa_DeleteProgram(GLuint name
)
1499 GET_CURRENT_CONTEXT(ctx
);
1500 FLUSH_VERTICES(ctx
, 0);
1501 delete_shader_program(ctx
, name
);
1507 _mesa_DeleteShader(GLuint name
)
1510 GET_CURRENT_CONTEXT(ctx
);
1511 FLUSH_VERTICES(ctx
, 0);
1512 delete_shader(ctx
, name
);
1518 _mesa_DetachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1520 GET_CURRENT_CONTEXT(ctx
);
1521 detach_shader_no_error(ctx
, program
, shader
);
1526 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1528 GET_CURRENT_CONTEXT(ctx
);
1529 detach_shader_error(ctx
, program
, shader
);
1534 _mesa_DetachShader_no_error(GLuint program
, GLuint shader
)
1536 GET_CURRENT_CONTEXT(ctx
);
1537 detach_shader_no_error(ctx
, program
, shader
);
1542 _mesa_DetachShader(GLuint program
, GLuint shader
)
1544 GET_CURRENT_CONTEXT(ctx
);
1545 detach_shader_error(ctx
, program
, shader
);
1550 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1551 GLsizei
* count
, GLhandleARB
* obj
)
1553 GET_CURRENT_CONTEXT(ctx
);
1554 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1559 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1560 GLsizei
*count
, GLuint
*obj
)
1562 GET_CURRENT_CONTEXT(ctx
);
1563 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1568 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1569 GLcharARB
* infoLog
)
1571 GET_CURRENT_CONTEXT(ctx
);
1572 if (is_program(ctx
, object
)) {
1573 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1575 else if (is_shader(ctx
, object
)) {
1576 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1579 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1585 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1587 GET_CURRENT_CONTEXT(ctx
);
1588 /* Implement in terms of GetProgramiv, GetShaderiv */
1589 if (is_program(ctx
, object
)) {
1590 if (pname
== GL_OBJECT_TYPE_ARB
) {
1591 *params
= GL_PROGRAM_OBJECT_ARB
;
1594 get_programiv(ctx
, object
, pname
, params
);
1597 else if (is_shader(ctx
, object
)) {
1598 if (pname
== GL_OBJECT_TYPE_ARB
) {
1599 *params
= GL_SHADER_OBJECT_ARB
;
1602 get_shaderiv(ctx
, object
, pname
, params
);
1606 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1612 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1615 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1616 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1617 params
[0] = (GLfloat
) iparams
[0];
1622 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1624 GET_CURRENT_CONTEXT(ctx
);
1625 get_programiv(ctx
, program
, pname
, params
);
1630 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1632 GET_CURRENT_CONTEXT(ctx
);
1633 get_shaderiv(ctx
, shader
, pname
, params
);
1638 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1639 GLsizei
*length
, GLchar
*infoLog
)
1641 GET_CURRENT_CONTEXT(ctx
);
1642 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1647 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1648 GLsizei
*length
, GLchar
*infoLog
)
1650 GET_CURRENT_CONTEXT(ctx
);
1651 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1656 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1657 GLsizei
*length
, GLchar
*sourceOut
)
1659 GET_CURRENT_CONTEXT(ctx
);
1660 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1664 GLhandleARB GLAPIENTRY
1665 _mesa_GetHandleARB(GLenum pname
)
1667 GET_CURRENT_CONTEXT(ctx
);
1668 return get_handle(ctx
, pname
);
1672 GLboolean GLAPIENTRY
1673 _mesa_IsProgram(GLuint name
)
1675 GET_CURRENT_CONTEXT(ctx
);
1676 return is_program(ctx
, name
);
1680 GLboolean GLAPIENTRY
1681 _mesa_IsShader(GLuint name
)
1683 GET_CURRENT_CONTEXT(ctx
);
1684 return is_shader(ctx
, name
);
1689 _mesa_LinkProgram(GLuint programObj
)
1691 GET_CURRENT_CONTEXT(ctx
);
1692 if (MESA_VERBOSE
& VERBOSE_API
)
1693 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1694 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1698 #ifdef ENABLE_SHADER_CACHE
1700 * Generate a SHA-1 hash value string for given source string.
1703 generate_sha1(const char *source
, char sha_str
[64])
1705 unsigned char sha
[20];
1706 _mesa_sha1_compute(source
, strlen(source
), sha
);
1707 _mesa_sha1_format(sha_str
, sha
);
1711 * Construct a full path for shader replacement functionality using
1714 * <path>/<stage prefix>_<CHECKSUM>.glsl
1717 construct_name(const gl_shader_stage stage
, const char *source
,
1721 static const char *types
[] = {
1722 "VS", "TC", "TE", "GS", "FS", "CS",
1725 generate_sha1(source
, sha
);
1726 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1730 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1733 dump_shader(const gl_shader_stage stage
, const char *source
)
1735 static bool path_exists
= true;
1742 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1744 path_exists
= false;
1748 char *name
= construct_name(stage
, source
, dump_path
);
1750 f
= fopen(name
, "w");
1755 GET_CURRENT_CONTEXT(ctx
);
1756 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1763 * Read shader source code from a file.
1764 * Useful for debugging to override an app's shader.
1767 read_shader(const gl_shader_stage stage
, const char *source
)
1770 static bool path_exists
= true;
1771 int len
, shader_size
= 0;
1778 read_path
= getenv("MESA_SHADER_READ_PATH");
1780 path_exists
= false;
1784 char *name
= construct_name(stage
, source
, read_path
);
1785 f
= fopen(name
, "r");
1790 /* allocate enough room for the entire shader */
1791 fseek(f
, 0, SEEK_END
);
1792 shader_size
= ftell(f
);
1794 assert(shader_size
);
1796 /* add one for terminating zero */
1799 buffer
= malloc(shader_size
);
1802 len
= fread(buffer
, 1, shader_size
, f
);
1810 #endif /* ENABLE_SHADER_CACHE */
1813 * Called via glShaderSource() and glShaderSourceARB() API functions.
1814 * Basically, concatenate the source code strings into one long string
1815 * and pass it to _mesa_shader_source().
1817 static ALWAYS_INLINE
void
1818 shader_source(struct gl_context
*ctx
, GLuint shaderObj
, GLsizei count
,
1819 const GLchar
*const *string
, const GLint
*length
, bool no_error
)
1822 GLsizei i
, totalLength
;
1824 struct gl_shader
*sh
;
1827 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1831 if (string
== NULL
) {
1832 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1836 sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1840 * This array holds offsets of where the appropriate string ends, thus the
1841 * last element will be set to the total length of the source code.
1843 offsets
= malloc(count
* sizeof(GLint
));
1844 if (offsets
== NULL
) {
1845 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1849 for (i
= 0; i
< count
; i
++) {
1850 if (!no_error
&& string
[i
] == NULL
) {
1851 free((GLvoid
*) offsets
);
1852 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1853 "glShaderSourceARB(null string)");
1856 if (length
== NULL
|| length
[i
] < 0)
1857 offsets
[i
] = strlen(string
[i
]);
1859 offsets
[i
] = length
[i
];
1860 /* accumulate string lengths */
1862 offsets
[i
] += offsets
[i
- 1];
1865 /* Total length of source string is sum off all strings plus two.
1866 * One extra byte for terminating zero, another extra byte to silence
1867 * valgrind warnings in the parser/grammer code.
1869 totalLength
= offsets
[count
- 1] + 2;
1870 source
= malloc(totalLength
* sizeof(GLcharARB
));
1871 if (source
== NULL
) {
1872 free((GLvoid
*) offsets
);
1873 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1877 for (i
= 0; i
< count
; i
++) {
1878 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1879 memcpy(source
+ start
, string
[i
],
1880 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1882 source
[totalLength
- 1] = '\0';
1883 source
[totalLength
- 2] = '\0';
1885 #ifdef ENABLE_SHADER_CACHE
1886 GLcharARB
*replacement
;
1888 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1889 * if corresponding entry found from MESA_SHADER_READ_PATH.
1891 dump_shader(sh
->Stage
, source
);
1893 replacement
= read_shader(sh
->Stage
, source
);
1896 source
= replacement
;
1898 #endif /* ENABLE_SHADER_CACHE */
1900 set_shader_source(sh
, source
);
1907 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1908 const GLchar
*const *string
, const GLint
*length
)
1910 GET_CURRENT_CONTEXT(ctx
);
1911 shader_source(ctx
, shaderObj
, count
, string
, length
, false);
1915 static ALWAYS_INLINE
void
1916 use_program(GLuint program
, bool no_error
)
1918 GET_CURRENT_CONTEXT(ctx
);
1919 struct gl_shader_program
*shProg
= NULL
;
1921 if (MESA_VERBOSE
& VERBOSE_API
)
1922 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1926 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1929 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1931 "glUseProgram(transform feedback active)");
1937 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1941 if (!shProg
->data
->LinkStatus
) {
1942 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1943 "glUseProgram(program %u not linked)", program
);
1948 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1949 print_shader_info(shProg
);
1954 /* The ARB_separate_shader_object spec says:
1956 * "The executable code for an individual shader stage is taken from
1957 * the current program for that stage. If there is a current program
1958 * object established by UseProgram, that program is considered current
1959 * for all stages. Otherwise, if there is a bound program pipeline
1960 * object (section 2.14.PPO), the program bound to the appropriate
1961 * stage of the pipeline object is considered current."
1964 /* Attach shader state to the binding point */
1965 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1966 /* Update the program */
1967 _mesa_use_shader_program(ctx
, shProg
);
1969 /* Must be done first: detach the progam */
1970 _mesa_use_shader_program(ctx
, shProg
);
1971 /* Unattach shader_state binding point */
1972 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1973 ctx
->Pipeline
.Default
);
1974 /* If a pipeline was bound, rebind it */
1975 if (ctx
->Pipeline
.Current
) {
1977 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
1979 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1986 _mesa_UseProgram_no_error(GLuint program
)
1988 use_program(program
, true);
1993 _mesa_UseProgram(GLuint program
)
1995 use_program(program
, false);
2000 _mesa_ValidateProgram(GLuint program
)
2002 GET_CURRENT_CONTEXT(ctx
);
2003 validate_program(ctx
, program
);
2008 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2011 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
2012 GLint
* range
, GLint
* precision
)
2014 const struct gl_program_constants
*limits
;
2015 const struct gl_precision
*p
;
2016 GET_CURRENT_CONTEXT(ctx
);
2018 switch (shadertype
) {
2019 case GL_VERTEX_SHADER
:
2020 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
2022 case GL_FRAGMENT_SHADER
:
2023 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
2026 _mesa_error(ctx
, GL_INVALID_ENUM
,
2027 "glGetShaderPrecisionFormat(shadertype)");
2031 switch (precisiontype
) {
2033 p
= &limits
->LowFloat
;
2035 case GL_MEDIUM_FLOAT
:
2036 p
= &limits
->MediumFloat
;
2039 p
= &limits
->HighFloat
;
2042 p
= &limits
->LowInt
;
2045 p
= &limits
->MediumInt
;
2048 p
= &limits
->HighInt
;
2051 _mesa_error(ctx
, GL_INVALID_ENUM
,
2052 "glGetShaderPrecisionFormat(precisiontype)");
2056 range
[0] = p
->RangeMin
;
2057 range
[1] = p
->RangeMax
;
2058 precision
[0] = p
->Precision
;
2063 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2066 _mesa_ReleaseShaderCompiler(void)
2068 _mesa_destroy_shader_compiler_caches();
2073 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2076 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2077 const void* binary
, GLint length
)
2079 GET_CURRENT_CONTEXT(ctx
);
2081 (void) binaryformat
;
2084 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2085 * page 88 of the OpenGL 4.5 specs state:
2087 * "An INVALID_VALUE error is generated if count or length is negative.
2088 * An INVALID_ENUM error is generated if binaryformat is not a supported
2089 * format returned in SHADER_BINARY_FORMATS."
2091 if (n
< 0 || length
< 0) {
2092 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2096 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2101 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2102 GLenum
*binaryFormat
, GLvoid
*binary
)
2104 struct gl_shader_program
*shProg
;
2105 GLsizei length_dummy
;
2106 GET_CURRENT_CONTEXT(ctx
);
2109 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2113 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2117 /* The ARB_get_program_binary spec says:
2119 * "If <length> is NULL, then no length is returned."
2121 * Ensure that length always points to valid storage to avoid multiple NULL
2122 * pointer checks below.
2125 length
= &length_dummy
;
2128 /* The ARB_get_program_binary spec says:
2130 * "When a program object's LINK_STATUS is FALSE, its program binary
2131 * length is zero, and a call to GetProgramBinary will generate an
2132 * INVALID_OPERATION error.
2134 if (!shProg
->data
->LinkStatus
) {
2135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2136 "glGetProgramBinary(program %u not linked)",
2143 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2144 "glGetProgramBinary(driver supports zero binary formats)");
2146 (void) binaryFormat
;
2151 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2152 const GLvoid
*binary
, GLsizei length
)
2154 struct gl_shader_program
*shProg
;
2155 GET_CURRENT_CONTEXT(ctx
);
2157 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2161 (void) binaryFormat
;
2164 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2166 * "If a negative number is provided where an argument of type sizei or
2167 * sizeiptr is specified, an INVALID_VALUE error is generated."
2170 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2174 /* The ARB_get_program_binary spec says:
2176 * "<binaryFormat> and <binary> must be those returned by a previous
2177 * call to GetProgramBinary, and <length> must be the length of the
2178 * program binary as returned by GetProgramBinary or GetProgramiv with
2179 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2180 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2183 * Since any value of binaryFormat passed "is not one of those specified as
2184 * allowable for [this] command, an INVALID_ENUM error is generated."
2186 shProg
->data
->LinkStatus
= linking_failure
;
2187 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2191 static ALWAYS_INLINE
void
2192 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2193 GLuint pname
, GLint value
, bool no_error
)
2196 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2197 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2198 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2199 * even be in the dispatch table, so we shouldn't need to expclicitly
2202 * On desktop, we ignore the 3.0+ requirement because it is silly.
2205 /* The ARB_get_program_binary extension spec says:
2207 * "An INVALID_VALUE error is generated if the <value> argument to
2208 * ProgramParameteri is not TRUE or FALSE."
2210 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2214 /* No need to notify the driver. Any changes will actually take effect
2215 * the next time the shader is linked.
2217 * The ARB_get_program_binary extension spec says:
2219 * "To indicate that a program binary is likely to be retrieved,
2220 * ProgramParameteri should be called with <pname>
2221 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2222 * will not be in effect until the next time LinkProgram or
2223 * ProgramBinary has been called successfully."
2225 * The resloution of issue 9 in the extension spec also says:
2227 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2228 * to indicate to the GL implementation that this program will
2229 * likely be saved with GetProgramBinary at some point. This will
2230 * give the GL implementation the opportunity to track any state
2231 * changes made to the program before being saved such that when it
2232 * is loaded again a recompile can be avoided."
2234 shProg
->BinaryRetreivableHint
= value
;
2237 case GL_PROGRAM_SEPARABLE
:
2238 /* Spec imply that the behavior is the same as ARB_get_program_binary
2239 * Chapter 7.3 Program Objects
2241 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2244 shProg
->SeparateShader
= value
;
2249 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2250 _mesa_enum_to_string(pname
));
2256 _mesa_error(ctx
, GL_INVALID_VALUE
,
2257 "glProgramParameteri(pname=%s, value=%d): "
2258 "value must be 0 or 1.",
2259 _mesa_enum_to_string(pname
),
2265 _mesa_ProgramParameteri_no_error(GLuint program
, GLenum pname
, GLint value
)
2267 GET_CURRENT_CONTEXT(ctx
);
2269 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
2270 program_parameteri(ctx
, shProg
, pname
, value
, true);
2275 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2277 struct gl_shader_program
*shProg
;
2278 GET_CURRENT_CONTEXT(ctx
);
2280 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2281 "glProgramParameteri");
2285 program_parameteri(ctx
, shProg
, pname
, value
, false);
2290 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2291 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2292 struct gl_pipeline_object
*shTarget
)
2294 struct gl_program
**target
;
2296 target
= &shTarget
->CurrentProgram
[stage
];
2298 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2301 if (*target
!= prog
) {
2302 /* Program is current, flush it */
2303 if (shTarget
== ctx
->_Shader
) {
2304 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2307 _mesa_reference_shader_program(ctx
,
2308 &shTarget
->ReferencedPrograms
[stage
],
2310 _mesa_reference_program(ctx
, target
, prog
);
2318 * Copy program-specific data generated by linking from the gl_shader_program
2319 * object to the gl_program object referred to by the gl_linked_shader.
2321 * This function expects _mesa_reference_program() to have been previously
2322 * called setting the gl_linked_shaders program reference.
2325 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2326 struct gl_linked_shader
*dst_sh
)
2328 assert(dst_sh
->Program
);
2330 struct gl_program
*dst
= dst_sh
->Program
;
2332 dst
->info
.separate_shader
= src
->SeparateShader
;
2334 switch (dst_sh
->Stage
) {
2335 case MESA_SHADER_GEOMETRY
: {
2336 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2337 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2338 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2341 case MESA_SHADER_FRAGMENT
: {
2342 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2345 case MESA_SHADER_COMPUTE
: {
2346 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2355 * ARB_separate_shader_objects: Compile & Link Program
2358 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2359 const GLchar
* const *strings
)
2361 GET_CURRENT_CONTEXT(ctx
);
2363 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2367 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2368 * GL_INVALID_VALUE should be generated if count < 0
2371 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2376 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2378 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2379 _mesa_compile_shader(ctx
, sh
);
2381 program
= create_shader_program(ctx
);
2383 struct gl_shader_program
*shProg
;
2384 GLint compiled
= GL_FALSE
;
2386 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2388 shProg
->SeparateShader
= GL_TRUE
;
2390 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2392 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2393 _mesa_link_program(ctx
, shProg
);
2394 detach_shader_error(ctx
, program
, shader
);
2398 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2399 append
-error
-to
-info
-log
;
2400 shProg
->data
->LinkStatus
= linking_failure
;
2405 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2408 delete_shader(ctx
, shader
);
2416 * For GL_ARB_tessellation_shader
2419 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2421 GET_CURRENT_CONTEXT(ctx
);
2422 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2426 extern void GLAPIENTRY
2427 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2429 GET_CURRENT_CONTEXT(ctx
);
2431 if (!_mesa_has_tessellation(ctx
)) {
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2436 if (pname
!= GL_PATCH_VERTICES
) {
2437 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2441 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2442 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2446 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2450 extern void GLAPIENTRY
2451 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2453 GET_CURRENT_CONTEXT(ctx
);
2455 if (!_mesa_has_tessellation(ctx
)) {
2456 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2461 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2462 FLUSH_VERTICES(ctx
, 0);
2463 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2464 4 * sizeof(GLfloat
));
2465 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2467 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2468 FLUSH_VERTICES(ctx
, 0);
2469 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2470 2 * sizeof(GLfloat
));
2471 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2474 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2480 * ARB_shader_subroutine
2483 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2486 GET_CURRENT_CONTEXT(ctx
);
2487 const char *api_name
= "glGetSubroutineUniformLocation";
2488 struct gl_shader_program
*shProg
;
2489 GLenum resource_type
;
2490 gl_shader_stage stage
;
2492 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2493 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2497 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2501 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2502 if (!shProg
->_LinkedShaders
[stage
]) {
2503 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2507 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2508 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2512 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2515 GET_CURRENT_CONTEXT(ctx
);
2516 const char *api_name
= "glGetSubroutineIndex";
2517 struct gl_shader_program
*shProg
;
2518 struct gl_program_resource
*res
;
2519 GLenum resource_type
;
2520 gl_shader_stage stage
;
2522 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2523 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2527 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2531 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2532 if (!shProg
->_LinkedShaders
[stage
]) {
2533 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2537 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2538 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2543 return _mesa_program_resource_index(shProg
, res
);
2548 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2549 GLuint index
, GLenum pname
, GLint
*values
)
2551 GET_CURRENT_CONTEXT(ctx
);
2552 const char *api_name
= "glGetActiveSubroutineUniformiv";
2553 struct gl_shader_program
*shProg
;
2554 struct gl_linked_shader
*sh
;
2555 gl_shader_stage stage
;
2556 struct gl_program_resource
*res
;
2557 const struct gl_uniform_storage
*uni
;
2558 GLenum resource_type
;
2561 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2562 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2566 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2570 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2571 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2573 sh
= shProg
->_LinkedShaders
[stage
];
2575 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2579 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2580 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2581 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2586 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2587 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2590 values
[0] = uni
->num_compatible_subroutines
;
2594 case GL_COMPATIBLE_SUBROUTINES
: {
2595 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2599 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2600 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2601 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2602 if (fn
->types
[j
] == uni
->type
) {
2603 values
[count
++] = i
;
2611 case GL_UNIFORM_SIZE
:
2612 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2615 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2618 case GL_UNIFORM_NAME_LENGTH
:
2619 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2621 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2622 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2626 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2633 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2634 GLuint index
, GLsizei bufsize
,
2635 GLsizei
*length
, GLchar
*name
)
2637 GET_CURRENT_CONTEXT(ctx
);
2638 const char *api_name
= "glGetActiveSubroutineUniformName";
2639 struct gl_shader_program
*shProg
;
2640 GLenum resource_type
;
2641 gl_shader_stage stage
;
2643 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2644 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2648 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2652 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2653 if (!shProg
->_LinkedShaders
[stage
]) {
2654 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2658 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2659 /* get program resource name */
2660 _mesa_get_program_resource_name(shProg
, resource_type
,
2662 length
, name
, api_name
);
2667 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2668 GLuint index
, GLsizei bufsize
,
2669 GLsizei
*length
, GLchar
*name
)
2671 GET_CURRENT_CONTEXT(ctx
);
2672 const char *api_name
= "glGetActiveSubroutineName";
2673 struct gl_shader_program
*shProg
;
2674 GLenum resource_type
;
2675 gl_shader_stage stage
;
2677 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2678 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2682 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2686 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2687 if (!shProg
->_LinkedShaders
[stage
]) {
2688 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2691 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2692 _mesa_get_program_resource_name(shProg
, resource_type
,
2694 length
, name
, api_name
);
2698 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2699 const GLuint
*indices
)
2701 GET_CURRENT_CONTEXT(ctx
);
2702 const char *api_name
= "glUniformSubroutinesuiv";
2703 gl_shader_stage stage
;
2706 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2707 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2711 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2712 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2714 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2718 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2719 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2724 bool flushed
= false;
2726 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2733 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2737 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2740 for (j
= i
; j
< i
+ uni_count
; j
++) {
2741 struct gl_subroutine_function
*subfn
= NULL
;
2742 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2743 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2747 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2748 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2749 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2756 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2757 if (subfn
->types
[k
] == uni
->type
)
2760 if (k
== subfn
->num_compat_types
) {
2761 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2765 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2773 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2776 GET_CURRENT_CONTEXT(ctx
);
2777 const char *api_name
= "glGetUniformSubroutineuiv";
2778 gl_shader_stage stage
;
2780 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2781 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2785 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2786 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2788 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2792 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2793 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2797 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2802 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2803 GLenum pname
, GLint
*values
)
2805 GET_CURRENT_CONTEXT(ctx
);
2806 const char *api_name
= "glGetProgramStageiv";
2807 struct gl_shader_program
*shProg
;
2808 struct gl_linked_shader
*sh
;
2809 gl_shader_stage stage
;
2811 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2812 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2816 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2820 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2821 sh
= shProg
->_LinkedShaders
[stage
];
2823 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2824 * INVALID_OPERATION in the case of not be linked.
2826 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2827 * same info using other specs (ARB_program_interface_query), without the
2828 * need of the program to be linked, being the value for that case 0.
2830 * But at the same time, some other methods require the program to be
2831 * linked for pname related to locations, so it would be inconsistent to
2832 * not do the same here. So we are:
2833 * * Return GL_INVALID_OPERATION if not linked only for locations.
2834 * * Setting a default value of 0, to be returned if not linked.
2838 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2839 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2844 struct gl_program
*p
= sh
->Program
;
2846 case GL_ACTIVE_SUBROUTINES
:
2847 values
[0] = p
->sh
.NumSubroutineFunctions
;
2849 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2850 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2852 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2853 values
[0] = p
->sh
.NumSubroutineUniforms
;
2855 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2859 GLenum resource_type
;
2860 struct gl_program_resource
*res
;
2862 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2863 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2864 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2866 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2871 values
[0] = max_len
;
2874 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2878 GLenum resource_type
;
2879 struct gl_program_resource
*res
;
2881 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2882 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2883 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2885 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2886 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2892 values
[0] = max_len
;
2896 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2903 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2907 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2908 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2909 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2910 if (fn
->types
[j
] == type
)
2918 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2919 struct gl_program
*p
)
2923 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2928 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2937 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2938 for (j
= 0; j
< uni_count
; j
++) {
2939 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2940 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2943 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2945 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2949 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2950 gl_shader_stage stage
)
2952 if (ctx
->_Shader
->CurrentProgram
[stage
])
2953 _mesa_shader_write_subroutine_index(ctx
,
2954 ctx
->_Shader
->CurrentProgram
[stage
]);
2958 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2959 struct gl_program
*p
)
2963 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2964 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2965 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2966 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2967 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2970 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2971 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2976 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);