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"
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
, "nopvert"))
81 flags
|= GLSL_NOP_VERT
;
82 if (strstr(env
, "nopfrag"))
83 flags
|= GLSL_NOP_FRAG
;
84 if (strstr(env
, "nopt"))
86 else if (strstr(env
, "opt"))
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");
112 strlen(path
) > PATH_MAX
- strlen("/fp-4294967295.shader_test")) {
113 GET_CURRENT_CONTEXT(ctx
);
114 _mesa_warning(ctx
, "MESA_SHADER_CAPTURE_PATH too long; ignoring "
115 "request to capture shaders");
124 * Initialize context's shader state.
127 _mesa_init_shader_state(struct gl_context
*ctx
)
129 /* Device drivers may override these to control what kind of instructions
130 * are generated by the GLSL compiler.
132 struct gl_shader_compiler_options options
;
136 memset(&options
, 0, sizeof(options
));
137 options
.MaxUnrollIterations
= 32;
138 options
.MaxIfDepth
= UINT_MAX
;
140 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
141 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
143 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
145 if (ctx
->Shader
.Flags
!= 0)
146 ctx
->Const
.GenerateTemporaryNames
= true;
148 /* Extended for ARB_separate_shader_objects */
149 ctx
->Shader
.RefCount
= 1;
150 mtx_init(&ctx
->Shader
.Mutex
, mtx_plain
);
152 ctx
->TessCtrlProgram
.patch_vertices
= 3;
153 for (i
= 0; i
< 4; ++i
)
154 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
155 for (i
= 0; i
< 2; ++i
)
156 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
161 * Free the per-context shader-related state.
164 _mesa_free_shader_state(struct gl_context
*ctx
)
167 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
168 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
171 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
173 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
175 /* Extended for ARB_separate_shader_objects */
176 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
178 assert(ctx
->Shader
.RefCount
== 1);
179 mtx_destroy(&ctx
->Shader
.Mutex
);
184 * Copy string from <src> to <dst>, up to maxLength characters, returning
185 * length of <dst> in <length>.
186 * \param src the strings source
187 * \param maxLength max chars to copy
188 * \param length returns number of chars copied
189 * \param dst the string destination
192 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
193 GLsizei
*length
, const GLchar
*src
)
196 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
207 * Confirm that the a shader type is valid and supported by the implementation
209 * \param ctx Current GL context
210 * \param type Shader target
214 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
216 /* Note: when building built-in GLSL functions, this function may be
217 * invoked with ctx == NULL. In that case, we can only validate that it's
218 * a shader target we recognize, not that it's supported in the current
219 * context. But that's fine--we don't need any further validation than
220 * that when building built-in GLSL functions.
224 case GL_FRAGMENT_SHADER
:
225 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
226 case GL_VERTEX_SHADER
:
227 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
228 case GL_GEOMETRY_SHADER_ARB
:
229 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
230 case GL_TESS_CONTROL_SHADER
:
231 case GL_TESS_EVALUATION_SHADER
:
232 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
233 case GL_COMPUTE_SHADER
:
234 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
242 is_program(struct gl_context
*ctx
, GLuint name
)
244 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
245 return shProg
? GL_TRUE
: GL_FALSE
;
250 is_shader(struct gl_context
*ctx
, GLuint name
)
252 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
253 return shader
? GL_TRUE
: GL_FALSE
;
258 * Attach shader to a shader program.
261 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
263 struct gl_shader_program
*shProg
;
264 struct gl_shader
*sh
;
267 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
269 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
273 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
278 n
= shProg
->NumShaders
;
279 for (i
= 0; i
< n
; i
++) {
280 if (shProg
->Shaders
[i
] == sh
) {
281 /* The shader is already attched to this program. The
282 * GL_ARB_shader_objects spec says:
284 * "The error INVALID_OPERATION is generated by AttachObjectARB
285 * if <obj> is already attached to <containerObj>."
287 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
289 } else if (same_type_disallowed
&&
290 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
291 /* Shader with the same type is already attached to this program,
292 * OpenGL ES 2.0 and 3.0 specs say:
294 * "Multiple shader objects of the same type may not be attached
295 * to a single program object. [...] The error INVALID_OPERATION
296 * is generated if [...] another shader object of the same type
297 * as shader is already attached to program."
299 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
305 shProg
->Shaders
= realloc(shProg
->Shaders
,
306 (n
+ 1) * sizeof(struct gl_shader
*));
307 if (!shProg
->Shaders
) {
308 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
313 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
314 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
315 shProg
->NumShaders
++;
320 create_shader(struct gl_context
*ctx
, GLenum type
)
322 struct gl_shader
*sh
;
325 if (!_mesa_validate_shader_target(ctx
, type
)) {
326 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(%s)",
327 _mesa_enum_to_string(type
));
331 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
332 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
333 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
334 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
335 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
342 create_shader_program(struct gl_context
*ctx
)
345 struct gl_shader_program
*shProg
;
347 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
349 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
351 shProg
= _mesa_new_shader_program(name
);
353 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
355 assert(shProg
->RefCount
== 1);
357 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
364 * Delete a shader program. Actually, just decrement the program's
365 * reference count and mark it as DeletePending.
366 * Used to implement glDeleteProgram() and glDeleteObjectARB().
369 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
372 * NOTE: deleting shaders/programs works a bit differently than
373 * texture objects (and buffer objects, etc). Shader/program
374 * handles/IDs exist in the hash table until the object is really
375 * deleted (refcount==0). With texture objects, the handle/ID is
376 * removed from the hash table in glDeleteTextures() while the tex
377 * object itself might linger until its refcount goes to zero.
379 struct gl_shader_program
*shProg
;
381 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
385 if (!shProg
->DeletePending
) {
386 shProg
->DeletePending
= GL_TRUE
;
388 /* effectively, decr shProg's refcount */
389 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
395 delete_shader(struct gl_context
*ctx
, GLuint shader
)
397 struct gl_shader
*sh
;
399 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
403 if (!sh
->DeletePending
) {
404 sh
->DeletePending
= GL_TRUE
;
406 /* effectively, decr sh's refcount */
407 _mesa_reference_shader(ctx
, &sh
, NULL
);
413 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
415 struct gl_shader_program
*shProg
;
419 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
423 n
= shProg
->NumShaders
;
425 for (i
= 0; i
< n
; i
++) {
426 if (shProg
->Shaders
[i
]->Name
== shader
) {
428 struct gl_shader
**newList
;
431 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
433 /* alloc new, smaller array */
434 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
436 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
439 /* Copy old list entries to new list, skipping removed entry at [i] */
440 for (j
= 0; j
< i
; j
++) {
441 newList
[j
] = shProg
->Shaders
[j
];
444 newList
[j
++] = shProg
->Shaders
[i
];
447 /* Free old list and install new one */
448 free(shProg
->Shaders
);
449 shProg
->Shaders
= newList
;
450 shProg
->NumShaders
= n
- 1;
453 /* sanity check - make sure the new list's entries are sensible */
454 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
455 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
456 shProg
->Shaders
[j
]->Type
== GL_TESS_CONTROL_SHADER
||
457 shProg
->Shaders
[j
]->Type
== GL_TESS_EVALUATION_SHADER
||
458 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
459 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
460 assert(shProg
->Shaders
[j
]->RefCount
> 0);
471 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
472 err
= GL_INVALID_OPERATION
;
474 err
= GL_INVALID_VALUE
;
475 _mesa_error(ctx
, err
, "glDetachShader(shader)");
482 * Return list of shaders attached to shader program.
485 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
486 GLsizei
*count
, GLuint
*obj
)
488 struct gl_shader_program
*shProg
;
491 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
496 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
500 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
501 obj
[i
] = shProg
->Shaders
[i
]->Name
;
510 * glGetHandleARB() - return ID/name of currently bound shader program.
513 get_handle(struct gl_context
*ctx
, GLenum pname
)
515 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
516 if (ctx
->_Shader
->ActiveProgram
)
517 return ctx
->_Shader
->ActiveProgram
->Name
;
522 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
529 * Check if a geometry shader query is valid at this time. If not, report an
530 * error and return false.
532 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
534 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
535 * are queried for a program which has not been linked successfully, or
536 * which does not contain objects to form a geometry shader, then an
537 * INVALID_OPERATION error is generated."
540 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
542 if (shProg
->LinkStatus
&&
543 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
547 _mesa_error(ctx
, GL_INVALID_OPERATION
,
548 "glGetProgramv(linked geometry shader required)");
554 * Check if a tessellation control shader query is valid at this time.
555 * If not, report an error and return false.
557 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
559 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
560 * not been linked successfully, or which does not contain objects to
561 * form a tessellation control shader, then an INVALID_OPERATION error is
565 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
567 if (shProg
->LinkStatus
&&
568 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
572 _mesa_error(ctx
, GL_INVALID_OPERATION
,
573 "glGetProgramv(linked tessellation control shader required)");
579 * Check if a tessellation evaluation shader query is valid at this time.
580 * If not, report an error and return false.
582 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
584 * "If any of the pname values in this paragraph are queried for a program
585 * which has not been linked successfully, or which does not contain
586 * objects to form a tessellation evaluation shader, then an
587 * INVALID_OPERATION error is generated."
591 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
593 if (shProg
->LinkStatus
&&
594 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
598 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
599 "evaluation shader required)");
605 * glGetProgramiv() - get shader program state.
606 * Note that this is for GLSL shader programs, not ARB vertex/fragment
607 * programs (see glGetProgramivARB).
610 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
613 struct gl_shader_program
*shProg
614 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
616 /* Is transform feedback available in this context?
619 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
620 || ctx
->API
== API_OPENGL_CORE
621 || _mesa_is_gles3(ctx
);
623 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
624 * and GL 3.2) are available in this context
626 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
627 const bool has_tess
= _mesa_has_tessellation(ctx
);
629 /* Are uniform buffer objects available in this context?
632 (ctx
->API
== API_OPENGL_COMPAT
&&
633 ctx
->Extensions
.ARB_uniform_buffer_object
)
634 || ctx
->API
== API_OPENGL_CORE
635 || _mesa_is_gles3(ctx
);
642 case GL_DELETE_STATUS
:
643 *params
= shProg
->DeletePending
;
646 *params
= shProg
->LinkStatus
;
648 case GL_VALIDATE_STATUS
:
649 *params
= shProg
->Validated
;
651 case GL_INFO_LOG_LENGTH
:
652 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
654 case GL_ATTACHED_SHADERS
:
655 *params
= shProg
->NumShaders
;
657 case GL_ACTIVE_ATTRIBUTES
:
658 *params
= _mesa_count_active_attribs(shProg
);
660 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
661 *params
= _mesa_longest_attribute_name_length(shProg
);
663 case GL_ACTIVE_UNIFORMS
: {
665 const unsigned num_uniforms
=
666 shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
667 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
668 if (!shProg
->UniformStorage
[i
].is_shader_storage
)
673 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
676 const unsigned num_uniforms
=
677 shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
679 for (i
= 0; i
< num_uniforms
; i
++) {
680 if (shProg
->UniformStorage
[i
].is_shader_storage
)
683 /* Add one for the terminating NUL character for a non-array, and
684 * 4 for the "[0]" and the NUL for an array.
686 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
687 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
696 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
699 *params
= shProg
->TransformFeedback
.NumVarying
;
701 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
707 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
708 /* Add one for the terminating NUL character.
711 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
720 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
723 *params
= shProg
->TransformFeedback
.BufferMode
;
725 case GL_GEOMETRY_VERTICES_OUT
:
728 if (check_gs_query(ctx
, shProg
))
729 *params
= shProg
->Geom
.VerticesOut
;
731 case GL_GEOMETRY_SHADER_INVOCATIONS
:
732 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
734 if (check_gs_query(ctx
, shProg
))
735 *params
= shProg
->Geom
.Invocations
;
737 case GL_GEOMETRY_INPUT_TYPE
:
740 if (check_gs_query(ctx
, shProg
))
741 *params
= shProg
->Geom
.InputType
;
743 case GL_GEOMETRY_OUTPUT_TYPE
:
746 if (check_gs_query(ctx
, shProg
))
747 *params
= shProg
->Geom
.OutputType
;
749 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
756 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
757 /* Add one for the terminating NUL character.
759 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
768 case GL_ACTIVE_UNIFORM_BLOCKS
:
772 *params
= shProg
->NumUniformBlocks
;
774 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
775 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
776 * only available with desktop OpenGL 3.0+ with the
777 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
779 * On desktop, we ignore the 3.0+ requirement because it is silly.
781 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
784 *params
= shProg
->BinaryRetreivableHint
;
786 case GL_PROGRAM_BINARY_LENGTH
:
789 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
790 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
793 *params
= shProg
->NumAtomicBuffers
;
795 case GL_COMPUTE_WORK_GROUP_SIZE
: {
797 if (!_mesa_has_compute_shaders(ctx
))
799 if (!shProg
->LinkStatus
) {
800 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
804 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
805 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
809 for (i
= 0; i
< 3; i
++)
810 params
[i
] = shProg
->Comp
.LocalSize
[i
];
813 case GL_PROGRAM_SEPARABLE
:
814 /* If the program has not been linked, return initial value 0. */
815 *params
= (shProg
->LinkStatus
== GL_FALSE
) ? 0 : shProg
->SeparateShader
;
818 /* ARB_tessellation_shader */
819 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
822 if (check_tcs_query(ctx
, shProg
))
823 *params
= shProg
->TessCtrl
.VerticesOut
;
825 case GL_TESS_GEN_MODE
:
828 if (check_tes_query(ctx
, shProg
))
829 *params
= shProg
->TessEval
.PrimitiveMode
;
831 case GL_TESS_GEN_SPACING
:
834 if (check_tes_query(ctx
, shProg
))
835 *params
= shProg
->TessEval
.Spacing
;
837 case GL_TESS_GEN_VERTEX_ORDER
:
840 if (check_tes_query(ctx
, shProg
))
841 *params
= shProg
->TessEval
.VertexOrder
;
843 case GL_TESS_GEN_POINT_MODE
:
846 if (check_tes_query(ctx
, shProg
))
847 *params
= shProg
->TessEval
.PointMode
;
853 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
854 _mesa_enum_to_string(pname
));
859 * glGetShaderiv() - get GLSL shader state
862 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
864 struct gl_shader
*shader
=
865 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
873 *params
= shader
->Type
;
875 case GL_DELETE_STATUS
:
876 *params
= shader
->DeletePending
;
878 case GL_COMPILE_STATUS
:
879 *params
= shader
->CompileStatus
;
881 case GL_INFO_LOG_LENGTH
:
882 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
884 case GL_SHADER_SOURCE_LENGTH
:
885 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
888 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
895 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
896 GLsizei
*length
, GLchar
*infoLog
)
898 struct gl_shader_program
*shProg
;
900 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
901 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
903 * "If a negative number is provided where an argument of type sizei or
904 * sizeiptr is specified, an INVALID_VALUE error is generated."
907 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
911 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
912 "glGetProgramInfoLog(program)");
917 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
922 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
923 GLsizei
*length
, GLchar
*infoLog
)
925 struct gl_shader
*sh
;
927 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
928 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
930 * "If a negative number is provided where an argument of type sizei or
931 * sizeiptr is specified, an INVALID_VALUE error is generated."
934 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
938 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
943 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
948 * Return shader source code.
951 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
952 GLsizei
*length
, GLchar
*sourceOut
)
954 struct gl_shader
*sh
;
957 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
961 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
965 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
970 * Set/replace shader source code. A helper function used by
971 * glShaderSource[ARB].
974 shader_source(struct gl_shader
*sh
, const GLchar
*source
)
978 /* free old shader source string and install new one */
979 free((void *)sh
->Source
);
982 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
991 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
997 /* If the user called glCompileShader without first calling
998 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1000 sh
->CompileStatus
= GL_FALSE
;
1002 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1003 _mesa_log("GLSL source for %s shader %d:\n",
1004 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1005 _mesa_log("%s\n", sh
->Source
);
1008 /* this call will set the shader->CompileStatus field to indicate if
1009 * compilation was successful.
1011 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
1013 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1014 _mesa_write_shader_to_file(sh
);
1017 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1018 if (sh
->CompileStatus
) {
1019 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1020 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1023 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1025 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1026 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1027 _mesa_log("%s\n", sh
->InfoLog
);
1032 if (!sh
->CompileStatus
) {
1033 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1034 _mesa_log("GLSL source for %s shader %d:\n",
1035 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1036 _mesa_log("%s\n", sh
->Source
);
1037 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1040 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1041 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1042 sh
->Name
, sh
->InfoLog
);
1049 * Link a program's shaders.
1052 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1057 /* From the ARB_transform_feedback2 specification:
1058 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1059 * the name of a program being used by one or more transform feedback
1060 * objects, even if the objects are not currently bound or are paused."
1062 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1063 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1064 "glLinkProgram(transform feedback is using the program)");
1068 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1070 _mesa_glsl_link_shader(ctx
, shProg
);
1072 /* Capture .shader_test files. */
1073 const char *capture_path
= _mesa_get_shader_capture_path();
1074 if (shProg
->Name
!= 0 && capture_path
!= NULL
) {
1076 char filename
[PATH_MAX
];
1078 _mesa_snprintf(filename
, sizeof(filename
), "%s/%u.shader_test",
1079 capture_path
, shProg
->Name
);
1081 file
= fopen(filename
, "w");
1083 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1084 shProg
->IsES
? " ES" : "",
1085 shProg
->Version
/ 100, shProg
->Version
% 100);
1086 if (shProg
->SeparateShader
)
1087 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1088 fprintf(file
, "\n");
1090 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1091 fprintf(file
, "[%s shader]\n%s\n",
1092 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1093 shProg
->Shaders
[i
]->Source
);
1097 _mesa_warning(ctx
, "Failed to open %s", filename
);
1101 if (shProg
->LinkStatus
== GL_FALSE
&&
1102 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1103 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1104 shProg
->Name
, shProg
->InfoLog
);
1111 printf("Link %u shaders in program %u: %s\n",
1112 shProg
->NumShaders
, shProg
->Name
,
1113 shProg
->LinkStatus
? "Success" : "Failed");
1115 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1116 printf(" shader %u, type 0x%x\n",
1117 shProg
->Shaders
[i
]->Name
,
1118 shProg
->Shaders
[i
]->Type
);
1125 * Print basic shader info (for debug).
1128 print_shader_info(const struct gl_shader_program
*shProg
)
1132 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1133 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1134 printf(" %s shader %u, checksum %u\n",
1135 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1136 shProg
->Shaders
[i
]->Name
,
1137 shProg
->Shaders
[i
]->SourceChecksum
);
1139 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1140 printf(" vert prog %u\n",
1141 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1142 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1143 printf(" frag prog %u\n",
1144 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1145 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1146 printf(" geom prog %u\n",
1147 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1148 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1149 printf(" tesc prog %u\n",
1150 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1151 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1152 printf(" tese prog %u\n",
1153 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1158 * Use the named shader program for subsequent glUniform calls
1161 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1164 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
1165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1166 "%s(program %u not linked)", caller
, shProg
->Name
);
1170 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1171 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1177 use_shader_program(struct gl_context
*ctx
, gl_shader_stage stage
,
1178 struct gl_shader_program
*shProg
,
1179 struct gl_pipeline_object
*shTarget
)
1181 struct gl_shader_program
**target
;
1183 target
= &shTarget
->CurrentProgram
[stage
];
1184 if ((shProg
!= NULL
) && (shProg
->_LinkedShaders
[stage
] == NULL
))
1188 _mesa_shader_program_init_subroutine_defaults(shProg
);
1190 if (*target
!= shProg
) {
1191 /* Program is current, flush it */
1192 if (shTarget
== ctx
->_Shader
) {
1193 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1196 /* If the shader is also bound as the current rendering shader, unbind
1197 * it from that binding point as well. This ensures that the correct
1198 * semantics of glDeleteProgram are maintained.
1201 case MESA_SHADER_VERTEX
:
1202 case MESA_SHADER_TESS_CTRL
:
1203 case MESA_SHADER_TESS_EVAL
:
1204 case MESA_SHADER_GEOMETRY
:
1205 case MESA_SHADER_COMPUTE
:
1206 /* Empty for now. */
1208 case MESA_SHADER_FRAGMENT
:
1209 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1210 _mesa_reference_shader_program(ctx
,
1211 &ctx
->_Shader
->_CurrentFragmentProgram
,
1217 _mesa_reference_shader_program(ctx
, target
, shProg
);
1224 * Use the named shader program for subsequent rendering.
1227 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1230 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
1231 use_shader_program(ctx
, i
, shProg
, &ctx
->Shader
);
1232 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1234 if (ctx
->Driver
.UseProgram
)
1235 ctx
->Driver
.UseProgram(ctx
, shProg
);
1240 * Do validation of the given shader program.
1241 * \param errMsg returns error message if validation fails.
1242 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1245 validate_shader_program(const struct gl_shader_program
*shProg
,
1248 if (!shProg
->LinkStatus
) {
1252 /* From the GL spec, a program is invalid if any of these are true:
1254 any two active samplers in the current program object are of
1255 different types, but refer to the same texture image unit,
1257 any active sampler in the current program object refers to a texture
1258 image unit where fixed-function fragment processing accesses a
1259 texture target that does not match the sampler type, or
1261 the sum of the number of active samplers in the program and the
1262 number of texture image units enabled for fixed-function fragment
1263 processing exceeds the combined limit on the total number of texture
1264 image units allowed.
1268 * Check: any two active samplers in the current program object are of
1269 * different types, but refer to the same texture image unit,
1271 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1279 * Called via glValidateProgram()
1282 validate_program(struct gl_context
*ctx
, GLuint program
)
1284 struct gl_shader_program
*shProg
;
1285 char errMsg
[100] = "";
1287 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1292 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1293 if (!shProg
->Validated
) {
1294 /* update info log */
1295 if (shProg
->InfoLog
) {
1296 ralloc_free(shProg
->InfoLog
);
1298 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1305 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1307 GET_CURRENT_CONTEXT(ctx
);
1308 attach_shader(ctx
, program
, shader
);
1313 _mesa_AttachShader(GLuint program
, GLuint shader
)
1315 GET_CURRENT_CONTEXT(ctx
);
1316 attach_shader(ctx
, program
, shader
);
1321 _mesa_CompileShader(GLuint shaderObj
)
1323 GET_CURRENT_CONTEXT(ctx
);
1324 if (MESA_VERBOSE
& VERBOSE_API
)
1325 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1326 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1327 "glCompileShader"));
1332 _mesa_CreateShader(GLenum type
)
1334 GET_CURRENT_CONTEXT(ctx
);
1335 if (MESA_VERBOSE
& VERBOSE_API
)
1336 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1337 return create_shader(ctx
, type
);
1341 GLhandleARB GLAPIENTRY
1342 _mesa_CreateShaderObjectARB(GLenum type
)
1344 GET_CURRENT_CONTEXT(ctx
);
1345 return create_shader(ctx
, type
);
1350 _mesa_CreateProgram(void)
1352 GET_CURRENT_CONTEXT(ctx
);
1353 if (MESA_VERBOSE
& VERBOSE_API
)
1354 _mesa_debug(ctx
, "glCreateProgram\n");
1355 return create_shader_program(ctx
);
1359 GLhandleARB GLAPIENTRY
1360 _mesa_CreateProgramObjectARB(void)
1362 GET_CURRENT_CONTEXT(ctx
);
1363 return create_shader_program(ctx
);
1368 _mesa_DeleteObjectARB(GLhandleARB obj
)
1370 if (MESA_VERBOSE
& VERBOSE_API
) {
1371 GET_CURRENT_CONTEXT(ctx
);
1372 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1376 GET_CURRENT_CONTEXT(ctx
);
1377 FLUSH_VERTICES(ctx
, 0);
1378 if (is_program(ctx
, obj
)) {
1379 delete_shader_program(ctx
, obj
);
1381 else if (is_shader(ctx
, obj
)) {
1382 delete_shader(ctx
, obj
);
1392 _mesa_DeleteProgram(GLuint name
)
1395 GET_CURRENT_CONTEXT(ctx
);
1396 FLUSH_VERTICES(ctx
, 0);
1397 delete_shader_program(ctx
, name
);
1403 _mesa_DeleteShader(GLuint name
)
1406 GET_CURRENT_CONTEXT(ctx
);
1407 FLUSH_VERTICES(ctx
, 0);
1408 delete_shader(ctx
, name
);
1414 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1416 GET_CURRENT_CONTEXT(ctx
);
1417 detach_shader(ctx
, program
, shader
);
1422 _mesa_DetachShader(GLuint program
, GLuint shader
)
1424 GET_CURRENT_CONTEXT(ctx
);
1425 detach_shader(ctx
, program
, shader
);
1430 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1431 GLsizei
* count
, GLhandleARB
* obj
)
1433 GET_CURRENT_CONTEXT(ctx
);
1434 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1439 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1440 GLsizei
*count
, GLuint
*obj
)
1442 GET_CURRENT_CONTEXT(ctx
);
1443 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1448 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1449 GLcharARB
* infoLog
)
1451 GET_CURRENT_CONTEXT(ctx
);
1452 if (is_program(ctx
, object
)) {
1453 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1455 else if (is_shader(ctx
, object
)) {
1456 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1459 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1465 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1467 GET_CURRENT_CONTEXT(ctx
);
1468 /* Implement in terms of GetProgramiv, GetShaderiv */
1469 if (is_program(ctx
, object
)) {
1470 if (pname
== GL_OBJECT_TYPE_ARB
) {
1471 *params
= GL_PROGRAM_OBJECT_ARB
;
1474 get_programiv(ctx
, object
, pname
, params
);
1477 else if (is_shader(ctx
, object
)) {
1478 if (pname
== GL_OBJECT_TYPE_ARB
) {
1479 *params
= GL_SHADER_OBJECT_ARB
;
1482 get_shaderiv(ctx
, object
, pname
, params
);
1486 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1492 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1495 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1496 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1497 params
[0] = (GLfloat
) iparams
[0];
1502 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1504 GET_CURRENT_CONTEXT(ctx
);
1505 get_programiv(ctx
, program
, pname
, params
);
1510 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1512 GET_CURRENT_CONTEXT(ctx
);
1513 get_shaderiv(ctx
, shader
, pname
, params
);
1518 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1519 GLsizei
*length
, GLchar
*infoLog
)
1521 GET_CURRENT_CONTEXT(ctx
);
1522 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1527 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1528 GLsizei
*length
, GLchar
*infoLog
)
1530 GET_CURRENT_CONTEXT(ctx
);
1531 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1536 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1537 GLsizei
*length
, GLchar
*sourceOut
)
1539 GET_CURRENT_CONTEXT(ctx
);
1540 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1544 GLhandleARB GLAPIENTRY
1545 _mesa_GetHandleARB(GLenum pname
)
1547 GET_CURRENT_CONTEXT(ctx
);
1548 return get_handle(ctx
, pname
);
1552 GLboolean GLAPIENTRY
1553 _mesa_IsProgram(GLuint name
)
1555 GET_CURRENT_CONTEXT(ctx
);
1556 return is_program(ctx
, name
);
1560 GLboolean GLAPIENTRY
1561 _mesa_IsShader(GLuint name
)
1563 GET_CURRENT_CONTEXT(ctx
);
1564 return is_shader(ctx
, name
);
1569 _mesa_LinkProgram(GLuint programObj
)
1571 GET_CURRENT_CONTEXT(ctx
);
1572 if (MESA_VERBOSE
& VERBOSE_API
)
1573 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1574 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1578 #if defined(HAVE_SHA1)
1580 * Generate a SHA-1 hash value string for given source string.
1583 generate_sha1(const char *source
, char sha_str
[64])
1585 unsigned char sha
[20];
1586 _mesa_sha1_compute(source
, strlen(source
), sha
);
1587 _mesa_sha1_format(sha_str
, sha
);
1591 * Construct a full path for shader replacement functionality using
1594 * <path>/<stage prefix>_<CHECKSUM>.glsl
1597 construct_name(const gl_shader_stage stage
, const char *source
,
1598 const char *path
, char *name
, unsigned length
)
1601 static const char *types
[] = {
1602 "VS", "TC", "TE", "GS", "FS", "CS",
1605 generate_sha1(source
, sha
);
1606 _mesa_snprintf(name
, length
, "%s/%s_%s.glsl", path
, types
[stage
],
1611 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1614 dump_shader(const gl_shader_stage stage
, const char *source
)
1616 char name
[PATH_MAX
];
1617 static bool path_exists
= true;
1624 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1626 path_exists
= false;
1630 construct_name(stage
, source
, dump_path
, name
, PATH_MAX
);
1632 f
= fopen(name
, "w");
1637 GET_CURRENT_CONTEXT(ctx
);
1638 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1644 * Read shader source code from a file.
1645 * Useful for debugging to override an app's shader.
1648 read_shader(const gl_shader_stage stage
, const char *source
)
1650 char name
[PATH_MAX
];
1652 static bool path_exists
= true;
1653 int len
, shader_size
= 0;
1660 read_path
= getenv("MESA_SHADER_READ_PATH");
1662 path_exists
= false;
1666 construct_name(stage
, source
, read_path
, name
, PATH_MAX
);
1668 f
= fopen(name
, "r");
1672 /* allocate enough room for the entire shader */
1673 fseek(f
, 0, SEEK_END
);
1674 shader_size
= ftell(f
);
1676 assert(shader_size
);
1678 /* add one for terminating zero */
1681 buffer
= malloc(shader_size
);
1684 len
= fread(buffer
, 1, shader_size
, f
);
1691 #endif /* HAVE_SHA1 */
1694 * Called via glShaderSource() and glShaderSourceARB() API functions.
1695 * Basically, concatenate the source code strings into one long string
1696 * and pass it to _mesa_shader_source().
1699 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1700 const GLchar
* const * string
, const GLint
* length
)
1702 GET_CURRENT_CONTEXT(ctx
);
1704 GLsizei i
, totalLength
;
1706 struct gl_shader
*sh
;
1708 #if defined(HAVE_SHA1)
1709 GLcharARB
*replacement
;
1710 #endif /* HAVE_SHA1 */
1712 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1716 if (string
== NULL
) {
1717 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1722 * This array holds offsets of where the appropriate string ends, thus the
1723 * last element will be set to the total length of the source code.
1725 offsets
= malloc(count
* sizeof(GLint
));
1726 if (offsets
== NULL
) {
1727 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1731 for (i
= 0; i
< count
; i
++) {
1732 if (string
[i
] == NULL
) {
1733 free((GLvoid
*) offsets
);
1734 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1735 "glShaderSourceARB(null string)");
1738 if (length
== NULL
|| length
[i
] < 0)
1739 offsets
[i
] = strlen(string
[i
]);
1741 offsets
[i
] = length
[i
];
1742 /* accumulate string lengths */
1744 offsets
[i
] += offsets
[i
- 1];
1747 /* Total length of source string is sum off all strings plus two.
1748 * One extra byte for terminating zero, another extra byte to silence
1749 * valgrind warnings in the parser/grammer code.
1751 totalLength
= offsets
[count
- 1] + 2;
1752 source
= malloc(totalLength
* sizeof(GLcharARB
));
1753 if (source
== NULL
) {
1754 free((GLvoid
*) offsets
);
1755 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1759 for (i
= 0; i
< count
; i
++) {
1760 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1761 memcpy(source
+ start
, string
[i
],
1762 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1764 source
[totalLength
- 1] = '\0';
1765 source
[totalLength
- 2] = '\0';
1767 #if defined(HAVE_SHA1)
1768 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1769 * if corresponding entry found from MESA_SHADER_READ_PATH.
1771 dump_shader(sh
->Stage
, source
);
1773 replacement
= read_shader(sh
->Stage
, source
);
1776 source
= replacement
;
1778 #endif /* HAVE_SHA1 */
1780 shader_source(sh
, source
);
1787 _mesa_UseProgram(GLuint program
)
1789 GET_CURRENT_CONTEXT(ctx
);
1790 struct gl_shader_program
*shProg
;
1792 if (MESA_VERBOSE
& VERBOSE_API
)
1793 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1795 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1796 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1797 "glUseProgram(transform feedback active)");
1802 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1806 if (!shProg
->LinkStatus
) {
1807 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1808 "glUseProgram(program %u not linked)", program
);
1813 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1814 print_shader_info(shProg
);
1821 /* The ARB_separate_shader_object spec says:
1823 * "The executable code for an individual shader stage is taken from
1824 * the current program for that stage. If there is a current program
1825 * object established by UseProgram, that program is considered current
1826 * for all stages. Otherwise, if there is a bound program pipeline
1827 * object (section 2.14.PPO), the program bound to the appropriate
1828 * stage of the pipeline object is considered current."
1831 /* Attach shader state to the binding point */
1832 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1833 /* Update the program */
1834 _mesa_use_program(ctx
, shProg
);
1836 /* Must be done first: detach the progam */
1837 _mesa_use_program(ctx
, shProg
);
1838 /* Unattach shader_state binding point */
1839 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1840 /* If a pipeline was bound, rebind it */
1841 if (ctx
->Pipeline
.Current
) {
1842 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1849 _mesa_ValidateProgram(GLuint program
)
1851 GET_CURRENT_CONTEXT(ctx
);
1852 validate_program(ctx
, program
);
1857 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1860 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1861 GLint
* range
, GLint
* precision
)
1863 const struct gl_program_constants
*limits
;
1864 const struct gl_precision
*p
;
1865 GET_CURRENT_CONTEXT(ctx
);
1867 switch (shadertype
) {
1868 case GL_VERTEX_SHADER
:
1869 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1871 case GL_FRAGMENT_SHADER
:
1872 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1875 _mesa_error(ctx
, GL_INVALID_ENUM
,
1876 "glGetShaderPrecisionFormat(shadertype)");
1880 switch (precisiontype
) {
1882 p
= &limits
->LowFloat
;
1884 case GL_MEDIUM_FLOAT
:
1885 p
= &limits
->MediumFloat
;
1888 p
= &limits
->HighFloat
;
1891 p
= &limits
->LowInt
;
1894 p
= &limits
->MediumInt
;
1897 p
= &limits
->HighInt
;
1900 _mesa_error(ctx
, GL_INVALID_ENUM
,
1901 "glGetShaderPrecisionFormat(precisiontype)");
1905 range
[0] = p
->RangeMin
;
1906 range
[1] = p
->RangeMax
;
1907 precision
[0] = p
->Precision
;
1912 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1915 _mesa_ReleaseShaderCompiler(void)
1917 _mesa_destroy_shader_compiler_caches();
1922 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1925 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1926 const void* binary
, GLint length
)
1928 GET_CURRENT_CONTEXT(ctx
);
1930 (void) binaryformat
;
1933 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
1934 * page 88 of the OpenGL 4.5 specs state:
1936 * "An INVALID_VALUE error is generated if count or length is negative.
1937 * An INVALID_ENUM error is generated if binaryformat is not a supported
1938 * format returned in SHADER_BINARY_FORMATS."
1940 if (n
< 0 || length
< 0) {
1941 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
1945 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
1950 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1951 GLenum
*binaryFormat
, GLvoid
*binary
)
1953 struct gl_shader_program
*shProg
;
1954 GLsizei length_dummy
;
1955 GET_CURRENT_CONTEXT(ctx
);
1958 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1962 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1966 /* The ARB_get_program_binary spec says:
1968 * "If <length> is NULL, then no length is returned."
1970 * Ensure that length always points to valid storage to avoid multiple NULL
1971 * pointer checks below.
1974 length
= &length_dummy
;
1977 /* The ARB_get_program_binary spec says:
1979 * "When a program object's LINK_STATUS is FALSE, its program binary
1980 * length is zero, and a call to GetProgramBinary will generate an
1981 * INVALID_OPERATION error.
1983 if (!shProg
->LinkStatus
) {
1984 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1985 "glGetProgramBinary(program %u not linked)",
1992 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1993 "glGetProgramBinary(driver supports zero binary formats)");
1995 (void) binaryFormat
;
2000 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2001 const GLvoid
*binary
, GLsizei length
)
2003 struct gl_shader_program
*shProg
;
2004 GET_CURRENT_CONTEXT(ctx
);
2006 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2010 (void) binaryFormat
;
2013 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2015 * "If a negative number is provided where an argument of type sizei or
2016 * sizeiptr is specified, an INVALID_VALUE error is generated."
2019 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2023 /* The ARB_get_program_binary spec says:
2025 * "<binaryFormat> and <binary> must be those returned by a previous
2026 * call to GetProgramBinary, and <length> must be the length of the
2027 * program binary as returned by GetProgramBinary or GetProgramiv with
2028 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2029 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2032 * Since any value of binaryFormat passed "is not one of those specified as
2033 * allowable for [this] command, an INVALID_ENUM error is generated."
2035 shProg
->LinkStatus
= GL_FALSE
;
2036 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2041 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2043 struct gl_shader_program
*shProg
;
2044 GET_CURRENT_CONTEXT(ctx
);
2046 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2047 "glProgramParameteri");
2052 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2053 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2054 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2055 * even be in the dispatch table, so we shouldn't need to expclicitly
2058 * On desktop, we ignore the 3.0+ requirement because it is silly.
2061 /* The ARB_get_program_binary extension spec says:
2063 * "An INVALID_VALUE error is generated if the <value> argument to
2064 * ProgramParameteri is not TRUE or FALSE."
2066 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2070 /* No need to notify the driver. Any changes will actually take effect
2071 * the next time the shader is linked.
2073 * The ARB_get_program_binary extension spec says:
2075 * "To indicate that a program binary is likely to be retrieved,
2076 * ProgramParameteri should be called with <pname>
2077 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2078 * will not be in effect until the next time LinkProgram or
2079 * ProgramBinary has been called successfully."
2081 * The resloution of issue 9 in the extension spec also says:
2083 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2084 * to indicate to the GL implementation that this program will
2085 * likely be saved with GetProgramBinary at some point. This will
2086 * give the GL implementation the opportunity to track any state
2087 * changes made to the program before being saved such that when it
2088 * is loaded again a recompile can be avoided."
2090 shProg
->BinaryRetreivableHint
= value
;
2093 case GL_PROGRAM_SEPARABLE
:
2094 /* Spec imply that the behavior is the same as ARB_get_program_binary
2095 * Chapter 7.3 Program Objects
2097 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2100 shProg
->SeparateShader
= value
;
2104 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2105 _mesa_enum_to_string(pname
));
2110 _mesa_error(ctx
, GL_INVALID_VALUE
,
2111 "glProgramParameteri(pname=%s, value=%d): "
2112 "value must be 0 or 1.",
2113 _mesa_enum_to_string(pname
),
2119 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
2120 struct gl_shader_program
*shProg
,
2121 struct gl_pipeline_object
*shTarget
)
2123 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
2124 use_shader_program(ctx
, stage
, shProg
, shTarget
);
2126 if (ctx
->Driver
.UseProgram
)
2127 ctx
->Driver
.UseProgram(ctx
, shProg
);
2132 * Copy program-specific data generated by linking from the gl_shader_program
2133 * object to a specific gl_program object.
2136 _mesa_copy_linked_program_data(gl_shader_stage type
,
2137 const struct gl_shader_program
*src
,
2138 struct gl_program
*dst
)
2141 case MESA_SHADER_VERTEX
:
2142 dst
->ClipDistanceArraySize
= src
->Vert
.ClipDistanceArraySize
;
2143 dst
->CullDistanceArraySize
= src
->Vert
.CullDistanceArraySize
;
2145 case MESA_SHADER_TESS_CTRL
: {
2146 struct gl_tess_ctrl_program
*dst_tcp
=
2147 (struct gl_tess_ctrl_program
*) dst
;
2148 dst_tcp
->VerticesOut
= src
->TessCtrl
.VerticesOut
;
2151 case MESA_SHADER_TESS_EVAL
: {
2152 struct gl_tess_eval_program
*dst_tep
=
2153 (struct gl_tess_eval_program
*) dst
;
2154 dst_tep
->PrimitiveMode
= src
->TessEval
.PrimitiveMode
;
2155 dst_tep
->Spacing
= src
->TessEval
.Spacing
;
2156 dst_tep
->VertexOrder
= src
->TessEval
.VertexOrder
;
2157 dst_tep
->PointMode
= src
->TessEval
.PointMode
;
2158 dst
->ClipDistanceArraySize
= src
->TessEval
.ClipDistanceArraySize
;
2159 dst
->CullDistanceArraySize
= src
->TessEval
.CullDistanceArraySize
;
2162 case MESA_SHADER_GEOMETRY
: {
2163 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
2164 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
2165 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
2166 dst_gp
->Invocations
= src
->Geom
.Invocations
;
2167 dst_gp
->InputType
= src
->Geom
.InputType
;
2168 dst_gp
->OutputType
= src
->Geom
.OutputType
;
2169 dst
->ClipDistanceArraySize
= src
->Geom
.ClipDistanceArraySize
;
2170 dst
->CullDistanceArraySize
= src
->Geom
.CullDistanceArraySize
;
2171 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
2172 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
2175 case MESA_SHADER_FRAGMENT
: {
2176 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
2177 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
2180 case MESA_SHADER_COMPUTE
: {
2181 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
2183 for (i
= 0; i
< 3; i
++)
2184 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
2185 dst_cp
->SharedSize
= src
->Comp
.SharedSize
;
2194 * ARB_separate_shader_objects: Compile & Link Program
2197 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2198 const GLchar
* const *strings
)
2200 GET_CURRENT_CONTEXT(ctx
);
2202 const GLuint shader
= create_shader(ctx
, type
);
2206 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2207 * GL_INVALID_VALUE should be generated if count < 0
2210 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2215 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2217 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2218 _mesa_compile_shader(ctx
, sh
);
2220 program
= create_shader_program(ctx
);
2222 struct gl_shader_program
*shProg
;
2223 GLint compiled
= GL_FALSE
;
2225 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2227 shProg
->SeparateShader
= GL_TRUE
;
2229 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2231 attach_shader(ctx
, program
, shader
);
2232 _mesa_link_program(ctx
, shProg
);
2233 detach_shader(ctx
, program
, shader
);
2237 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2238 append
-error
-to
-info
-log
;
2239 shProg
->LinkStatus
= GL_FALSE
;
2244 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
2247 delete_shader(ctx
, shader
);
2255 * For GL_ARB_tessellation_shader
2257 extern void GLAPIENTRY
2258 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2260 GET_CURRENT_CONTEXT(ctx
);
2262 if (!_mesa_has_tessellation(ctx
)) {
2263 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2267 if (pname
!= GL_PATCH_VERTICES
) {
2268 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2272 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2273 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2277 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2281 extern void GLAPIENTRY
2282 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2284 GET_CURRENT_CONTEXT(ctx
);
2286 if (!_mesa_has_tessellation(ctx
)) {
2287 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2292 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2293 FLUSH_VERTICES(ctx
, 0);
2294 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2295 4 * sizeof(GLfloat
));
2296 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2298 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2299 FLUSH_VERTICES(ctx
, 0);
2300 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2301 2 * sizeof(GLfloat
));
2302 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2305 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2311 * ARB_shader_subroutine
2314 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2317 GET_CURRENT_CONTEXT(ctx
);
2318 const char *api_name
= "glGetSubroutineUniformLocation";
2319 struct gl_shader_program
*shProg
;
2320 GLenum resource_type
;
2321 gl_shader_stage stage
;
2323 if (!_mesa_has_shader_subroutine(ctx
)) {
2324 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2328 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2329 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2333 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2337 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2338 if (!shProg
->_LinkedShaders
[stage
]) {
2339 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2343 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2344 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2348 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2351 GET_CURRENT_CONTEXT(ctx
);
2352 const char *api_name
= "glGetSubroutineIndex";
2353 struct gl_shader_program
*shProg
;
2354 struct gl_program_resource
*res
;
2355 GLenum resource_type
;
2356 gl_shader_stage stage
;
2358 if (!_mesa_has_shader_subroutine(ctx
)) {
2359 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2363 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2364 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2368 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2372 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2373 if (!shProg
->_LinkedShaders
[stage
]) {
2374 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2378 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2379 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2384 return _mesa_program_resource_index(shProg
, res
);
2389 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2390 GLuint index
, GLenum pname
, GLint
*values
)
2392 GET_CURRENT_CONTEXT(ctx
);
2393 const char *api_name
= "glGetActiveSubroutineUniformiv";
2394 struct gl_shader_program
*shProg
;
2395 struct gl_shader
*sh
;
2396 gl_shader_stage stage
;
2397 struct gl_program_resource
*res
;
2398 const struct gl_uniform_storage
*uni
;
2399 GLenum resource_type
;
2402 if (!_mesa_has_shader_subroutine(ctx
)) {
2403 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2407 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2408 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2412 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2416 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2417 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2419 sh
= shProg
->_LinkedShaders
[stage
];
2421 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2425 if (index
>= sh
->NumSubroutineUniforms
) {
2426 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2431 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2432 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2435 values
[0] = uni
->num_compatible_subroutines
;
2439 case GL_COMPATIBLE_SUBROUTINES
: {
2440 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2444 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
2445 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
2446 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2447 if (fn
->types
[j
] == uni
->type
) {
2448 values
[count
++] = i
;
2456 case GL_UNIFORM_SIZE
:
2457 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2460 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2463 case GL_UNIFORM_NAME_LENGTH
:
2464 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2466 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2467 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2471 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2478 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2479 GLuint index
, GLsizei bufsize
,
2480 GLsizei
*length
, GLchar
*name
)
2482 GET_CURRENT_CONTEXT(ctx
);
2483 const char *api_name
= "glGetActiveSubroutineUniformName";
2484 struct gl_shader_program
*shProg
;
2485 GLenum resource_type
;
2486 gl_shader_stage stage
;
2488 if (!_mesa_has_shader_subroutine(ctx
)) {
2489 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2493 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2494 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2498 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2502 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2503 if (!shProg
->_LinkedShaders
[stage
]) {
2504 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2508 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2509 /* get program resource name */
2510 _mesa_get_program_resource_name(shProg
, resource_type
,
2512 length
, name
, api_name
);
2517 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2518 GLuint index
, GLsizei bufsize
,
2519 GLsizei
*length
, GLchar
*name
)
2521 GET_CURRENT_CONTEXT(ctx
);
2522 const char *api_name
= "glGetActiveSubroutineName";
2523 struct gl_shader_program
*shProg
;
2524 GLenum resource_type
;
2525 gl_shader_stage stage
;
2527 if (!_mesa_has_shader_subroutine(ctx
)) {
2528 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2532 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2533 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2537 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2541 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2542 if (!shProg
->_LinkedShaders
[stage
]) {
2543 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2546 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2547 _mesa_get_program_resource_name(shProg
, resource_type
,
2549 length
, name
, api_name
);
2554 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2555 const GLuint
*indices
)
2557 GET_CURRENT_CONTEXT(ctx
);
2558 const char *api_name
= "glUniformSubroutinesuiv";
2559 struct gl_shader_program
*shProg
;
2560 struct gl_shader
*sh
;
2561 gl_shader_stage stage
;
2564 if (!_mesa_has_shader_subroutine(ctx
)) {
2565 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2569 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2570 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2574 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2575 shProg
= ctx
->_Shader
->CurrentProgram
[stage
];
2577 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2581 sh
= shProg
->_LinkedShaders
[stage
];
2583 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2587 if (count
!= sh
->NumSubroutineUniformRemapTable
) {
2588 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2594 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[i
];
2600 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2603 for (j
= i
; j
< i
+ uni_count
; j
++) {
2604 struct gl_subroutine_function
*subfn
= NULL
;
2605 if (indices
[j
] > sh
->MaxSubroutineFunctionIndex
) {
2606 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2610 for (f
= 0; f
< sh
->NumSubroutineFunctions
; f
++) {
2611 if (sh
->SubroutineFunctions
[f
].index
== indices
[j
])
2612 subfn
= &sh
->SubroutineFunctions
[f
];
2619 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2620 if (subfn
->types
[k
] == uni
->type
)
2623 if (k
== subfn
->num_compat_types
) {
2624 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2631 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
2634 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[i
];
2640 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2642 memcpy(&uni
->storage
[0], &indices
[i
],
2643 sizeof(GLuint
) * uni_count
);
2645 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2652 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2655 GET_CURRENT_CONTEXT(ctx
);
2656 const char *api_name
= "glGetUniformSubroutineuiv";
2657 struct gl_shader_program
*shProg
;
2658 struct gl_shader
*sh
;
2659 gl_shader_stage stage
;
2661 if (!_mesa_has_shader_subroutine(ctx
)) {
2662 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2666 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2667 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2671 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2672 shProg
= ctx
->_Shader
->CurrentProgram
[stage
];
2674 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2678 sh
= shProg
->_LinkedShaders
[stage
];
2680 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2684 if (location
>= sh
->NumSubroutineUniformRemapTable
) {
2685 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2690 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[location
];
2691 int offset
= location
- uni
->opaque
[stage
].index
;
2692 memcpy(params
, &uni
->storage
[offset
],
2699 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2700 GLenum pname
, GLint
*values
)
2702 GET_CURRENT_CONTEXT(ctx
);
2703 const char *api_name
= "glGetProgramStageiv";
2704 struct gl_shader_program
*shProg
;
2705 struct gl_shader
*sh
;
2706 gl_shader_stage stage
;
2708 if (!_mesa_has_shader_subroutine(ctx
)) {
2709 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2713 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2714 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2718 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2722 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2723 sh
= shProg
->_LinkedShaders
[stage
];
2725 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2730 case GL_ACTIVE_SUBROUTINES
:
2731 values
[0] = sh
->NumSubroutineFunctions
;
2733 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2734 values
[0] = sh
->NumSubroutineUniformRemapTable
;
2736 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2737 values
[0] = sh
->NumSubroutineUniforms
;
2739 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2743 GLenum resource_type
;
2744 struct gl_program_resource
*res
;
2746 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2747 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
2748 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2750 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2755 values
[0] = max_len
;
2758 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2762 GLenum resource_type
;
2763 struct gl_program_resource
*res
;
2765 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2766 for (i
= 0; i
< sh
->NumSubroutineUniformRemapTable
; i
++) {
2767 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2769 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2770 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2776 values
[0] = max_len
;
2780 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2787 find_compat_subroutine(struct gl_shader
*sh
, const struct glsl_type
*type
)
2791 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
2792 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
2793 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2794 if (fn
->types
[j
] == type
)
2802 _mesa_shader_init_subroutine_defaults(struct gl_shader
*sh
)
2806 for (i
= 0; i
< sh
->NumSubroutineUniformRemapTable
; i
++) {
2807 struct gl_uniform_storage
*uni
= sh
->SubroutineUniformRemapTable
[i
];
2813 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2814 val
= find_compat_subroutine(sh
, uni
->type
);
2816 for (j
= 0; j
< uni_count
; j
++)
2817 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2819 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2824 _mesa_shader_program_init_subroutine_defaults(struct gl_shader_program
*shProg
)
2831 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
2832 if (!shProg
->_LinkedShaders
[i
])
2835 _mesa_shader_init_subroutine_defaults(shProg
->_LinkedShaders
[i
]);