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_info"))
81 flags
|= GLSL_CACHE_INFO
;
82 if (strstr(env
, "nopvert"))
83 flags
|= GLSL_NOP_VERT
;
84 if (strstr(env
, "nopfrag"))
85 flags
|= GLSL_NOP_FRAG
;
86 if (strstr(env
, "uniform"))
87 flags
|= GLSL_UNIFORMS
;
88 if (strstr(env
, "useprog"))
89 flags
|= GLSL_USE_PROG
;
90 if (strstr(env
, "errors"))
91 flags
|= GLSL_REPORT_ERRORS
;
98 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
101 _mesa_get_shader_capture_path(void)
103 static bool read_env_var
= false;
104 static const char *path
= NULL
;
107 path
= getenv("MESA_SHADER_CAPTURE_PATH");
115 * Initialize context's shader state.
118 _mesa_init_shader_state(struct gl_context
*ctx
)
120 /* Device drivers may override these to control what kind of instructions
121 * are generated by the GLSL compiler.
123 struct gl_shader_compiler_options options
;
127 memset(&options
, 0, sizeof(options
));
128 options
.MaxUnrollIterations
= 32;
129 options
.MaxIfDepth
= UINT_MAX
;
131 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
132 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
134 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
136 if (ctx
->Shader
.Flags
!= 0)
137 ctx
->Const
.GenerateTemporaryNames
= true;
139 /* Extended for ARB_separate_shader_objects */
140 ctx
->Shader
.RefCount
= 1;
141 ctx
->TessCtrlProgram
.patch_vertices
= 3;
142 for (i
= 0; i
< 4; ++i
)
143 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
144 for (i
= 0; i
< 2; ++i
)
145 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
150 * Free the per-context shader-related state.
153 _mesa_free_shader_state(struct gl_context
*ctx
)
155 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
156 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
158 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
160 /* Extended for ARB_separate_shader_objects */
161 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
163 assert(ctx
->Shader
.RefCount
== 1);
168 * Copy string from <src> to <dst>, up to maxLength characters, returning
169 * length of <dst> in <length>.
170 * \param src the strings source
171 * \param maxLength max chars to copy
172 * \param length returns number of chars copied
173 * \param dst the string destination
176 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
177 GLsizei
*length
, const GLchar
*src
)
180 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
191 * Confirm that the a shader type is valid and supported by the implementation
193 * \param ctx Current GL context
194 * \param type Shader target
198 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
200 /* Note: when building built-in GLSL functions, this function may be
201 * invoked with ctx == NULL. In that case, we can only validate that it's
202 * a shader target we recognize, not that it's supported in the current
203 * context. But that's fine--we don't need any further validation than
204 * that when building built-in GLSL functions.
208 case GL_FRAGMENT_SHADER
:
209 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
210 case GL_VERTEX_SHADER
:
211 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
212 case GL_GEOMETRY_SHADER_ARB
:
213 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
214 case GL_TESS_CONTROL_SHADER
:
215 case GL_TESS_EVALUATION_SHADER
:
216 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
217 case GL_COMPUTE_SHADER
:
218 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
226 is_program(struct gl_context
*ctx
, GLuint name
)
228 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
229 return shProg
? GL_TRUE
: GL_FALSE
;
234 is_shader(struct gl_context
*ctx
, GLuint name
)
236 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
237 return shader
? GL_TRUE
: GL_FALSE
;
242 * Attach shader to a shader program.
245 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
247 struct gl_shader_program
*shProg
;
248 struct gl_shader
*sh
;
251 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
253 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
257 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
262 n
= shProg
->NumShaders
;
263 for (i
= 0; i
< n
; i
++) {
264 if (shProg
->Shaders
[i
] == sh
) {
265 /* The shader is already attched to this program. The
266 * GL_ARB_shader_objects spec says:
268 * "The error INVALID_OPERATION is generated by AttachObjectARB
269 * if <obj> is already attached to <containerObj>."
271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
273 } else if (same_type_disallowed
&&
274 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
275 /* Shader with the same type is already attached to this program,
276 * OpenGL ES 2.0 and 3.0 specs say:
278 * "Multiple shader objects of the same type may not be attached
279 * to a single program object. [...] The error INVALID_OPERATION
280 * is generated if [...] another shader object of the same type
281 * as shader is already attached to program."
283 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
289 shProg
->Shaders
= realloc(shProg
->Shaders
,
290 (n
+ 1) * sizeof(struct gl_shader
*));
291 if (!shProg
->Shaders
) {
292 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
297 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
298 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
299 shProg
->NumShaders
++;
304 create_shader(struct gl_context
*ctx
, GLenum type
)
306 struct gl_shader
*sh
;
309 if (!_mesa_validate_shader_target(ctx
, type
)) {
310 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(%s)",
311 _mesa_enum_to_string(type
));
315 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
316 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
317 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
319 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
320 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
327 create_shader_program(struct gl_context
*ctx
)
330 struct gl_shader_program
*shProg
;
332 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
334 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
336 shProg
= _mesa_new_shader_program(name
);
338 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
340 assert(shProg
->RefCount
== 1);
342 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
349 * Delete a shader program. Actually, just decrement the program's
350 * reference count and mark it as DeletePending.
351 * Used to implement glDeleteProgram() and glDeleteObjectARB().
354 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
357 * NOTE: deleting shaders/programs works a bit differently than
358 * texture objects (and buffer objects, etc). Shader/program
359 * handles/IDs exist in the hash table until the object is really
360 * deleted (refcount==0). With texture objects, the handle/ID is
361 * removed from the hash table in glDeleteTextures() while the tex
362 * object itself might linger until its refcount goes to zero.
364 struct gl_shader_program
*shProg
;
366 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
370 if (!shProg
->DeletePending
) {
371 shProg
->DeletePending
= GL_TRUE
;
373 /* effectively, decr shProg's refcount */
374 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
380 delete_shader(struct gl_context
*ctx
, GLuint shader
)
382 struct gl_shader
*sh
;
384 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
388 if (!sh
->DeletePending
) {
389 sh
->DeletePending
= GL_TRUE
;
391 /* effectively, decr sh's refcount */
392 _mesa_reference_shader(ctx
, &sh
, NULL
);
398 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
400 struct gl_shader_program
*shProg
;
404 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
408 n
= shProg
->NumShaders
;
410 for (i
= 0; i
< n
; i
++) {
411 if (shProg
->Shaders
[i
]->Name
== shader
) {
413 struct gl_shader
**newList
;
416 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
418 /* alloc new, smaller array */
419 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
421 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
424 /* Copy old list entries to new list, skipping removed entry at [i] */
425 for (j
= 0; j
< i
; j
++) {
426 newList
[j
] = shProg
->Shaders
[j
];
429 newList
[j
++] = shProg
->Shaders
[i
];
432 /* Free old list and install new one */
433 free(shProg
->Shaders
);
434 shProg
->Shaders
= newList
;
435 shProg
->NumShaders
= n
- 1;
438 /* sanity check - make sure the new list's entries are sensible */
439 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
440 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
441 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
442 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
443 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
444 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
445 assert(shProg
->Shaders
[j
]->RefCount
> 0);
456 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
457 err
= GL_INVALID_OPERATION
;
459 err
= GL_INVALID_VALUE
;
460 _mesa_error(ctx
, err
, "glDetachShader(shader)");
467 * Return list of shaders attached to shader program.
470 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
471 GLsizei
*count
, GLuint
*obj
)
473 struct gl_shader_program
*shProg
;
476 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
481 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
485 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
486 obj
[i
] = shProg
->Shaders
[i
]->Name
;
495 * glGetHandleARB() - return ID/name of currently bound shader program.
498 get_handle(struct gl_context
*ctx
, GLenum pname
)
500 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
501 if (ctx
->_Shader
->ActiveProgram
)
502 return ctx
->_Shader
->ActiveProgram
->Name
;
507 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
514 * Check if a geometry shader query is valid at this time. If not, report an
515 * error and return false.
517 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
519 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
520 * are queried for a program which has not been linked successfully, or
521 * which does not contain objects to form a geometry shader, then an
522 * INVALID_OPERATION error is generated."
525 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
527 if (shProg
->data
->LinkStatus
&&
528 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
532 _mesa_error(ctx
, GL_INVALID_OPERATION
,
533 "glGetProgramv(linked geometry shader required)");
539 * Check if a tessellation control shader query is valid at this time.
540 * If not, report an error and return false.
542 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
544 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
545 * not been linked successfully, or which does not contain objects to
546 * form a tessellation control shader, then an INVALID_OPERATION error is
550 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
552 if (shProg
->data
->LinkStatus
&&
553 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
557 _mesa_error(ctx
, GL_INVALID_OPERATION
,
558 "glGetProgramv(linked tessellation control shader required)");
564 * Check if a tessellation evaluation shader query is valid at this time.
565 * If not, report an error and return false.
567 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
569 * "If any of the pname values in this paragraph are queried for a program
570 * which has not been linked successfully, or which does not contain
571 * objects to form a tessellation evaluation shader, then an
572 * INVALID_OPERATION error is generated."
576 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
578 if (shProg
->data
->LinkStatus
&&
579 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
583 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
584 "evaluation shader required)");
590 * glGetProgramiv() - get shader program state.
591 * Note that this is for GLSL shader programs, not ARB vertex/fragment
592 * programs (see glGetProgramivARB).
595 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
598 struct gl_shader_program
*shProg
599 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
601 /* Is transform feedback available in this context?
604 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
605 || ctx
->API
== API_OPENGL_CORE
606 || _mesa_is_gles3(ctx
);
608 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
609 * and GL 3.2) are available in this context
611 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
612 const bool has_tess
= _mesa_has_tessellation(ctx
);
614 /* Are uniform buffer objects available in this context?
617 (ctx
->API
== API_OPENGL_COMPAT
&&
618 ctx
->Extensions
.ARB_uniform_buffer_object
)
619 || ctx
->API
== API_OPENGL_CORE
620 || _mesa_is_gles3(ctx
);
627 case GL_DELETE_STATUS
:
628 *params
= shProg
->DeletePending
;
631 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
633 case GL_VALIDATE_STATUS
:
634 *params
= shProg
->data
->Validated
;
636 case GL_INFO_LOG_LENGTH
:
637 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
638 strlen(shProg
->data
->InfoLog
) + 1 : 0;
640 case GL_ATTACHED_SHADERS
:
641 *params
= shProg
->NumShaders
;
643 case GL_ACTIVE_ATTRIBUTES
:
644 *params
= _mesa_count_active_attribs(shProg
);
646 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
647 *params
= _mesa_longest_attribute_name_length(shProg
);
649 case GL_ACTIVE_UNIFORMS
: {
651 const unsigned num_uniforms
=
652 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
653 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
654 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
659 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
662 const unsigned num_uniforms
=
663 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
665 for (i
= 0; i
< num_uniforms
; i
++) {
666 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
669 /* Add one for the terminating NUL character for a non-array, and
670 * 4 for the "[0]" and the NUL for an array.
672 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
673 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
682 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
685 *params
= shProg
->TransformFeedback
.NumVarying
;
687 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
693 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
694 /* Add one for the terminating NUL character.
697 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
706 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
709 *params
= shProg
->TransformFeedback
.BufferMode
;
711 case GL_GEOMETRY_VERTICES_OUT
:
714 if (check_gs_query(ctx
, shProg
)) {
715 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
716 Program
->info
.gs
.vertices_out
;
719 case GL_GEOMETRY_SHADER_INVOCATIONS
:
720 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
722 if (check_gs_query(ctx
, shProg
)) {
723 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
724 Program
->info
.gs
.invocations
;
727 case GL_GEOMETRY_INPUT_TYPE
:
730 if (check_gs_query(ctx
, shProg
)) {
731 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
732 Program
->info
.gs
.input_primitive
;
735 case GL_GEOMETRY_OUTPUT_TYPE
:
738 if (check_gs_query(ctx
, shProg
)) {
739 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
740 Program
->info
.gs
.output_primitive
;
743 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
750 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
751 /* Add one for the terminating NUL character.
753 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
762 case GL_ACTIVE_UNIFORM_BLOCKS
:
766 *params
= shProg
->data
->NumUniformBlocks
;
768 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
769 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
770 * only available with desktop OpenGL 3.0+ with the
771 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
773 * On desktop, we ignore the 3.0+ requirement because it is silly.
775 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
778 *params
= shProg
->BinaryRetreivableHint
;
780 case GL_PROGRAM_BINARY_LENGTH
:
783 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
784 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
787 *params
= shProg
->data
->NumAtomicBuffers
;
789 case GL_COMPUTE_WORK_GROUP_SIZE
: {
791 if (!_mesa_has_compute_shaders(ctx
))
793 if (!shProg
->data
->LinkStatus
) {
794 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
798 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
799 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
803 for (i
= 0; i
< 3; i
++)
804 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
805 Program
->info
.cs
.local_size
[i
];
808 case GL_PROGRAM_SEPARABLE
:
809 /* If the program has not been linked, return initial value 0. */
810 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
813 /* ARB_tessellation_shader */
814 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
817 if (check_tcs_query(ctx
, shProg
)) {
818 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
819 Program
->info
.tess
.tcs_vertices_out
;
822 case GL_TESS_GEN_MODE
:
825 if (check_tes_query(ctx
, shProg
)) {
826 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
827 Program
->info
.tess
.primitive_mode
;
830 case GL_TESS_GEN_SPACING
:
833 if (check_tes_query(ctx
, shProg
)) {
834 const struct gl_linked_shader
*tes
=
835 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
836 switch (tes
->Program
->info
.tess
.spacing
) {
837 case TESS_SPACING_EQUAL
:
840 case TESS_SPACING_FRACTIONAL_ODD
:
841 *params
= GL_FRACTIONAL_ODD
;
843 case TESS_SPACING_FRACTIONAL_EVEN
:
844 *params
= GL_FRACTIONAL_EVEN
;
846 case TESS_SPACING_UNSPECIFIED
:
852 case GL_TESS_GEN_VERTEX_ORDER
:
855 if (check_tes_query(ctx
, shProg
)) {
856 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
857 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
860 case GL_TESS_GEN_POINT_MODE
:
863 if (check_tes_query(ctx
, shProg
)) {
864 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
865 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
872 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
873 _mesa_enum_to_string(pname
));
878 * glGetShaderiv() - get GLSL shader state
881 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
883 struct gl_shader
*shader
=
884 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
892 *params
= shader
->Type
;
894 case GL_DELETE_STATUS
:
895 *params
= shader
->DeletePending
;
897 case GL_COMPILE_STATUS
:
898 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
900 case GL_INFO_LOG_LENGTH
:
901 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
902 strlen(shader
->InfoLog
) + 1 : 0;
904 case GL_SHADER_SOURCE_LENGTH
:
905 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
908 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
915 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
916 GLsizei
*length
, GLchar
*infoLog
)
918 struct gl_shader_program
*shProg
;
920 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
921 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
923 * "If a negative number is provided where an argument of type sizei or
924 * sizeiptr is specified, an INVALID_VALUE error is generated."
927 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
931 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
932 "glGetProgramInfoLog(program)");
937 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
942 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
943 GLsizei
*length
, GLchar
*infoLog
)
945 struct gl_shader
*sh
;
947 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
948 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
950 * "If a negative number is provided where an argument of type sizei or
951 * sizeiptr is specified, an INVALID_VALUE error is generated."
954 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
958 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
963 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
968 * Return shader source code.
971 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
972 GLsizei
*length
, GLchar
*sourceOut
)
974 struct gl_shader
*sh
;
977 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
981 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
985 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
990 * Set/replace shader source code. A helper function used by
991 * glShaderSource[ARB].
994 shader_source(struct gl_shader
*sh
, const GLchar
*source
)
998 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
999 /* If shader was previously compiled back-up the source in case of cache
1002 sh
->FallbackSource
= sh
->Source
;
1003 sh
->Source
= source
;
1005 /* free old shader source string and install new one */
1006 free((void *)sh
->Source
);
1007 sh
->Source
= source
;
1011 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1020 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1026 /* If the user called glCompileShader without first calling
1027 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1029 sh
->CompileStatus
= compile_failure
;
1031 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1032 _mesa_log("GLSL source for %s shader %d:\n",
1033 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1034 _mesa_log("%s\n", sh
->Source
);
1037 /* this call will set the shader->CompileStatus field to indicate if
1038 * compilation was successful.
1040 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1042 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1043 _mesa_write_shader_to_file(sh
);
1046 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1047 if (sh
->CompileStatus
) {
1049 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1050 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1052 _mesa_log("No GLSL IR for shader %d (shader may be from "
1053 "cache)\n", sh
->Name
);
1057 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1059 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1060 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1061 _mesa_log("%s\n", sh
->InfoLog
);
1066 if (!sh
->CompileStatus
) {
1067 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1068 _mesa_log("GLSL source for %s shader %d:\n",
1069 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1070 _mesa_log("%s\n", sh
->Source
);
1071 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1074 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1075 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1076 sh
->Name
, sh
->InfoLog
);
1083 * Link a program's shaders.
1086 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1091 /* From the ARB_transform_feedback2 specification:
1092 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1093 * the name of a program being used by one or more transform feedback
1094 * objects, even if the objects are not currently bound or are paused."
1096 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1097 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1098 "glLinkProgram(transform feedback is using the program)");
1102 unsigned programs_in_use
= 0;
1104 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1105 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1106 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1107 programs_in_use
|= 1 << stage
;
1111 FLUSH_VERTICES(ctx
, 0);
1112 _mesa_glsl_link_shader(ctx
, shProg
);
1114 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1116 * "If LinkProgram or ProgramBinary successfully re-links a program
1117 * object that is active for any shader stage, then the newly generated
1118 * executable code will be installed as part of the current rendering
1119 * state for all shader stages where the program is active.
1120 * Additionally, the newly generated executable code is made part of
1121 * the state of any program pipeline for all stages where the program
1124 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1125 while (programs_in_use
) {
1126 const int stage
= u_bit_scan(&programs_in_use
);
1128 struct gl_program
*prog
= NULL
;
1129 if (shProg
->_LinkedShaders
[stage
])
1130 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1132 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1136 /* Capture .shader_test files. */
1137 const char *capture_path
= _mesa_get_shader_capture_path();
1138 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1140 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1141 capture_path
, shProg
->Name
);
1142 file
= fopen(filename
, "w");
1144 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1145 shProg
->IsES
? " ES" : "",
1146 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1147 if (shProg
->SeparateShader
)
1148 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1149 fprintf(file
, "\n");
1151 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1152 fprintf(file
, "[%s shader]\n%s\n",
1153 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1154 shProg
->Shaders
[i
]->Source
);
1158 _mesa_warning(ctx
, "Failed to open %s", filename
);
1161 ralloc_free(filename
);
1164 if (shProg
->data
->LinkStatus
== linking_failure
&&
1165 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1166 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1167 shProg
->Name
, shProg
->data
->InfoLog
);
1174 printf("Link %u shaders in program %u: %s\n",
1175 shProg
->NumShaders
, shProg
->Name
,
1176 shProg
->data
->LinkStatus
? "Success" : "Failed");
1178 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1179 printf(" shader %u, stage %u\n",
1180 shProg
->Shaders
[i
]->Name
,
1181 shProg
->Shaders
[i
]->Stage
);
1188 * Print basic shader info (for debug).
1191 print_shader_info(const struct gl_shader_program
*shProg
)
1195 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1196 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1198 printf(" %s shader %u, checksum %u\n",
1199 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1200 shProg
->Shaders
[i
]->Name
,
1201 shProg
->Shaders
[i
]->SourceChecksum
);
1203 printf(" %s shader %u\n",
1204 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1205 shProg
->Shaders
[i
]->Name
);
1208 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1209 printf(" vert prog %u\n",
1210 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1211 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1212 printf(" frag prog %u\n",
1213 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1214 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1215 printf(" geom prog %u\n",
1216 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1217 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1218 printf(" tesc prog %u\n",
1219 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1220 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1221 printf(" tese prog %u\n",
1222 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1227 * Use the named shader program for subsequent glUniform calls
1230 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1233 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1235 "%s(program %u not linked)", caller
, shProg
->Name
);
1239 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1240 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1246 use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
1247 struct gl_shader_program
*shProg
, struct gl_program
*new_prog
,
1248 struct gl_pipeline_object
*shTarget
)
1250 struct gl_program
**target
;
1252 target
= &shTarget
->CurrentProgram
[stage
];
1254 _mesa_program_init_subroutine_defaults(ctx
, new_prog
);
1257 if (*target
!= new_prog
) {
1258 /* Program is current, flush it */
1259 if (shTarget
== ctx
->_Shader
) {
1260 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1263 _mesa_reference_shader_program(ctx
,
1264 &shTarget
->ReferencedPrograms
[stage
],
1266 _mesa_reference_program(ctx
, target
, new_prog
);
1273 * Use the named shader program for subsequent rendering.
1276 _mesa_use_shader_program(struct gl_context
*ctx
,
1277 struct gl_shader_program
*shProg
)
1279 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1280 struct gl_program
*new_prog
= NULL
;
1281 if (shProg
&& shProg
->_LinkedShaders
[i
])
1282 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1283 use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1285 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1290 * Do validation of the given shader program.
1291 * \param errMsg returns error message if validation fails.
1292 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1295 validate_shader_program(const struct gl_shader_program
*shProg
,
1298 if (!shProg
->data
->LinkStatus
) {
1302 /* From the GL spec, a program is invalid if any of these are true:
1304 any two active samplers in the current program object are of
1305 different types, but refer to the same texture image unit,
1307 any active sampler in the current program object refers to a texture
1308 image unit where fixed-function fragment processing accesses a
1309 texture target that does not match the sampler type, or
1311 the sum of the number of active samplers in the program and the
1312 number of texture image units enabled for fixed-function fragment
1313 processing exceeds the combined limit on the total number of texture
1314 image units allowed.
1318 * Check: any two active samplers in the current program object are of
1319 * different types, but refer to the same texture image unit,
1321 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1329 * Called via glValidateProgram()
1332 validate_program(struct gl_context
*ctx
, GLuint program
)
1334 struct gl_shader_program
*shProg
;
1335 char errMsg
[100] = "";
1337 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1342 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1343 if (!shProg
->data
->Validated
) {
1344 /* update info log */
1345 if (shProg
->data
->InfoLog
) {
1346 ralloc_free(shProg
->data
->InfoLog
);
1348 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1355 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 attach_shader(ctx
, program
, shader
);
1363 _mesa_AttachShader(GLuint program
, GLuint shader
)
1365 GET_CURRENT_CONTEXT(ctx
);
1366 attach_shader(ctx
, program
, shader
);
1371 _mesa_CompileShader(GLuint shaderObj
)
1373 GET_CURRENT_CONTEXT(ctx
);
1374 if (MESA_VERBOSE
& VERBOSE_API
)
1375 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1376 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1377 "glCompileShader"));
1382 _mesa_CreateShader(GLenum type
)
1384 GET_CURRENT_CONTEXT(ctx
);
1385 if (MESA_VERBOSE
& VERBOSE_API
)
1386 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1387 return create_shader(ctx
, type
);
1391 GLhandleARB GLAPIENTRY
1392 _mesa_CreateShaderObjectARB(GLenum type
)
1394 GET_CURRENT_CONTEXT(ctx
);
1395 return create_shader(ctx
, type
);
1400 _mesa_CreateProgram(void)
1402 GET_CURRENT_CONTEXT(ctx
);
1403 if (MESA_VERBOSE
& VERBOSE_API
)
1404 _mesa_debug(ctx
, "glCreateProgram\n");
1405 return create_shader_program(ctx
);
1409 GLhandleARB GLAPIENTRY
1410 _mesa_CreateProgramObjectARB(void)
1412 GET_CURRENT_CONTEXT(ctx
);
1413 return create_shader_program(ctx
);
1418 _mesa_DeleteObjectARB(GLhandleARB obj
)
1420 if (MESA_VERBOSE
& VERBOSE_API
) {
1421 GET_CURRENT_CONTEXT(ctx
);
1422 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1426 GET_CURRENT_CONTEXT(ctx
);
1427 FLUSH_VERTICES(ctx
, 0);
1428 if (is_program(ctx
, obj
)) {
1429 delete_shader_program(ctx
, obj
);
1431 else if (is_shader(ctx
, obj
)) {
1432 delete_shader(ctx
, obj
);
1442 _mesa_DeleteProgram(GLuint name
)
1445 GET_CURRENT_CONTEXT(ctx
);
1446 FLUSH_VERTICES(ctx
, 0);
1447 delete_shader_program(ctx
, name
);
1453 _mesa_DeleteShader(GLuint name
)
1456 GET_CURRENT_CONTEXT(ctx
);
1457 FLUSH_VERTICES(ctx
, 0);
1458 delete_shader(ctx
, name
);
1464 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1466 GET_CURRENT_CONTEXT(ctx
);
1467 detach_shader(ctx
, program
, shader
);
1472 _mesa_DetachShader(GLuint program
, GLuint shader
)
1474 GET_CURRENT_CONTEXT(ctx
);
1475 detach_shader(ctx
, program
, shader
);
1480 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1481 GLsizei
* count
, GLhandleARB
* obj
)
1483 GET_CURRENT_CONTEXT(ctx
);
1484 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1489 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1490 GLsizei
*count
, GLuint
*obj
)
1492 GET_CURRENT_CONTEXT(ctx
);
1493 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1498 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1499 GLcharARB
* infoLog
)
1501 GET_CURRENT_CONTEXT(ctx
);
1502 if (is_program(ctx
, object
)) {
1503 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1505 else if (is_shader(ctx
, object
)) {
1506 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1509 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1515 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1517 GET_CURRENT_CONTEXT(ctx
);
1518 /* Implement in terms of GetProgramiv, GetShaderiv */
1519 if (is_program(ctx
, object
)) {
1520 if (pname
== GL_OBJECT_TYPE_ARB
) {
1521 *params
= GL_PROGRAM_OBJECT_ARB
;
1524 get_programiv(ctx
, object
, pname
, params
);
1527 else if (is_shader(ctx
, object
)) {
1528 if (pname
== GL_OBJECT_TYPE_ARB
) {
1529 *params
= GL_SHADER_OBJECT_ARB
;
1532 get_shaderiv(ctx
, object
, pname
, params
);
1536 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1542 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1545 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1546 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1547 params
[0] = (GLfloat
) iparams
[0];
1552 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1554 GET_CURRENT_CONTEXT(ctx
);
1555 get_programiv(ctx
, program
, pname
, params
);
1560 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1562 GET_CURRENT_CONTEXT(ctx
);
1563 get_shaderiv(ctx
, shader
, pname
, params
);
1568 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1569 GLsizei
*length
, GLchar
*infoLog
)
1571 GET_CURRENT_CONTEXT(ctx
);
1572 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1577 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1578 GLsizei
*length
, GLchar
*infoLog
)
1580 GET_CURRENT_CONTEXT(ctx
);
1581 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1586 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1587 GLsizei
*length
, GLchar
*sourceOut
)
1589 GET_CURRENT_CONTEXT(ctx
);
1590 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1594 GLhandleARB GLAPIENTRY
1595 _mesa_GetHandleARB(GLenum pname
)
1597 GET_CURRENT_CONTEXT(ctx
);
1598 return get_handle(ctx
, pname
);
1602 GLboolean GLAPIENTRY
1603 _mesa_IsProgram(GLuint name
)
1605 GET_CURRENT_CONTEXT(ctx
);
1606 return is_program(ctx
, name
);
1610 GLboolean GLAPIENTRY
1611 _mesa_IsShader(GLuint name
)
1613 GET_CURRENT_CONTEXT(ctx
);
1614 return is_shader(ctx
, name
);
1619 _mesa_LinkProgram(GLuint programObj
)
1621 GET_CURRENT_CONTEXT(ctx
);
1622 if (MESA_VERBOSE
& VERBOSE_API
)
1623 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1624 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1628 #ifdef ENABLE_SHADER_CACHE
1630 * Generate a SHA-1 hash value string for given source string.
1633 generate_sha1(const char *source
, char sha_str
[64])
1635 unsigned char sha
[20];
1636 _mesa_sha1_compute(source
, strlen(source
), sha
);
1637 _mesa_sha1_format(sha_str
, sha
);
1641 * Construct a full path for shader replacement functionality using
1644 * <path>/<stage prefix>_<CHECKSUM>.glsl
1647 construct_name(const gl_shader_stage stage
, const char *source
,
1651 static const char *types
[] = {
1652 "VS", "TC", "TE", "GS", "FS", "CS",
1655 generate_sha1(source
, sha
);
1656 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1660 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1663 dump_shader(const gl_shader_stage stage
, const char *source
)
1665 static bool path_exists
= true;
1672 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1674 path_exists
= false;
1678 char *name
= construct_name(stage
, source
, dump_path
);
1680 f
= fopen(name
, "w");
1685 GET_CURRENT_CONTEXT(ctx
);
1686 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1693 * Read shader source code from a file.
1694 * Useful for debugging to override an app's shader.
1697 read_shader(const gl_shader_stage stage
, const char *source
)
1700 static bool path_exists
= true;
1701 int len
, shader_size
= 0;
1708 read_path
= getenv("MESA_SHADER_READ_PATH");
1710 path_exists
= false;
1714 char *name
= construct_name(stage
, source
, read_path
);
1715 f
= fopen(name
, "r");
1720 /* allocate enough room for the entire shader */
1721 fseek(f
, 0, SEEK_END
);
1722 shader_size
= ftell(f
);
1724 assert(shader_size
);
1726 /* add one for terminating zero */
1729 buffer
= malloc(shader_size
);
1732 len
= fread(buffer
, 1, shader_size
, f
);
1740 #endif /* ENABLE_SHADER_CACHE */
1743 * Called via glShaderSource() and glShaderSourceARB() API functions.
1744 * Basically, concatenate the source code strings into one long string
1745 * and pass it to _mesa_shader_source().
1748 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1749 const GLchar
* const * string
, const GLint
* length
)
1751 GET_CURRENT_CONTEXT(ctx
);
1753 GLsizei i
, totalLength
;
1755 struct gl_shader
*sh
;
1757 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1761 if (string
== NULL
) {
1762 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1767 * This array holds offsets of where the appropriate string ends, thus the
1768 * last element will be set to the total length of the source code.
1770 offsets
= malloc(count
* sizeof(GLint
));
1771 if (offsets
== NULL
) {
1772 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1776 for (i
= 0; i
< count
; i
++) {
1777 if (string
[i
] == NULL
) {
1778 free((GLvoid
*) offsets
);
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "glShaderSourceARB(null string)");
1783 if (length
== NULL
|| length
[i
] < 0)
1784 offsets
[i
] = strlen(string
[i
]);
1786 offsets
[i
] = length
[i
];
1787 /* accumulate string lengths */
1789 offsets
[i
] += offsets
[i
- 1];
1792 /* Total length of source string is sum off all strings plus two.
1793 * One extra byte for terminating zero, another extra byte to silence
1794 * valgrind warnings in the parser/grammer code.
1796 totalLength
= offsets
[count
- 1] + 2;
1797 source
= malloc(totalLength
* sizeof(GLcharARB
));
1798 if (source
== NULL
) {
1799 free((GLvoid
*) offsets
);
1800 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1804 for (i
= 0; i
< count
; i
++) {
1805 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1806 memcpy(source
+ start
, string
[i
],
1807 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1809 source
[totalLength
- 1] = '\0';
1810 source
[totalLength
- 2] = '\0';
1812 #ifdef ENABLE_SHADER_CACHE
1813 GLcharARB
*replacement
;
1815 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1816 * if corresponding entry found from MESA_SHADER_READ_PATH.
1818 dump_shader(sh
->Stage
, source
);
1820 replacement
= read_shader(sh
->Stage
, source
);
1823 source
= replacement
;
1825 #endif /* ENABLE_SHADER_CACHE */
1827 shader_source(sh
, source
);
1834 _mesa_UseProgram(GLuint program
)
1836 GET_CURRENT_CONTEXT(ctx
);
1837 struct gl_shader_program
*shProg
= NULL
;
1839 if (MESA_VERBOSE
& VERBOSE_API
)
1840 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1842 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1843 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1844 "glUseProgram(transform feedback active)");
1849 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1853 if (!shProg
->data
->LinkStatus
) {
1854 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1855 "glUseProgram(program %u not linked)", program
);
1860 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1861 print_shader_info(shProg
);
1865 /* The ARB_separate_shader_object spec says:
1867 * "The executable code for an individual shader stage is taken from
1868 * the current program for that stage. If there is a current program
1869 * object established by UseProgram, that program is considered current
1870 * for all stages. Otherwise, if there is a bound program pipeline
1871 * object (section 2.14.PPO), the program bound to the appropriate
1872 * stage of the pipeline object is considered current."
1875 /* Attach shader state to the binding point */
1876 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1877 /* Update the program */
1878 _mesa_use_shader_program(ctx
, shProg
);
1880 /* Must be done first: detach the progam */
1881 _mesa_use_shader_program(ctx
, shProg
);
1882 /* Unattach shader_state binding point */
1883 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1884 /* If a pipeline was bound, rebind it */
1885 if (ctx
->Pipeline
.Current
) {
1886 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1893 _mesa_ValidateProgram(GLuint program
)
1895 GET_CURRENT_CONTEXT(ctx
);
1896 validate_program(ctx
, program
);
1901 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1904 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1905 GLint
* range
, GLint
* precision
)
1907 const struct gl_program_constants
*limits
;
1908 const struct gl_precision
*p
;
1909 GET_CURRENT_CONTEXT(ctx
);
1911 switch (shadertype
) {
1912 case GL_VERTEX_SHADER
:
1913 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1915 case GL_FRAGMENT_SHADER
:
1916 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1919 _mesa_error(ctx
, GL_INVALID_ENUM
,
1920 "glGetShaderPrecisionFormat(shadertype)");
1924 switch (precisiontype
) {
1926 p
= &limits
->LowFloat
;
1928 case GL_MEDIUM_FLOAT
:
1929 p
= &limits
->MediumFloat
;
1932 p
= &limits
->HighFloat
;
1935 p
= &limits
->LowInt
;
1938 p
= &limits
->MediumInt
;
1941 p
= &limits
->HighInt
;
1944 _mesa_error(ctx
, GL_INVALID_ENUM
,
1945 "glGetShaderPrecisionFormat(precisiontype)");
1949 range
[0] = p
->RangeMin
;
1950 range
[1] = p
->RangeMax
;
1951 precision
[0] = p
->Precision
;
1956 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1959 _mesa_ReleaseShaderCompiler(void)
1961 _mesa_destroy_shader_compiler_caches();
1966 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1969 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1970 const void* binary
, GLint length
)
1972 GET_CURRENT_CONTEXT(ctx
);
1974 (void) binaryformat
;
1977 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
1978 * page 88 of the OpenGL 4.5 specs state:
1980 * "An INVALID_VALUE error is generated if count or length is negative.
1981 * An INVALID_ENUM error is generated if binaryformat is not a supported
1982 * format returned in SHADER_BINARY_FORMATS."
1984 if (n
< 0 || length
< 0) {
1985 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
1989 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
1994 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1995 GLenum
*binaryFormat
, GLvoid
*binary
)
1997 struct gl_shader_program
*shProg
;
1998 GLsizei length_dummy
;
1999 GET_CURRENT_CONTEXT(ctx
);
2002 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2006 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2010 /* The ARB_get_program_binary spec says:
2012 * "If <length> is NULL, then no length is returned."
2014 * Ensure that length always points to valid storage to avoid multiple NULL
2015 * pointer checks below.
2018 length
= &length_dummy
;
2021 /* The ARB_get_program_binary spec says:
2023 * "When a program object's LINK_STATUS is FALSE, its program binary
2024 * length is zero, and a call to GetProgramBinary will generate an
2025 * INVALID_OPERATION error.
2027 if (!shProg
->data
->LinkStatus
) {
2028 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2029 "glGetProgramBinary(program %u not linked)",
2036 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2037 "glGetProgramBinary(driver supports zero binary formats)");
2039 (void) binaryFormat
;
2044 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2045 const GLvoid
*binary
, GLsizei length
)
2047 struct gl_shader_program
*shProg
;
2048 GET_CURRENT_CONTEXT(ctx
);
2050 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2054 (void) binaryFormat
;
2057 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2059 * "If a negative number is provided where an argument of type sizei or
2060 * sizeiptr is specified, an INVALID_VALUE error is generated."
2063 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2067 /* The ARB_get_program_binary spec says:
2069 * "<binaryFormat> and <binary> must be those returned by a previous
2070 * call to GetProgramBinary, and <length> must be the length of the
2071 * program binary as returned by GetProgramBinary or GetProgramiv with
2072 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2073 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2076 * Since any value of binaryFormat passed "is not one of those specified as
2077 * allowable for [this] command, an INVALID_ENUM error is generated."
2079 shProg
->data
->LinkStatus
= linking_failure
;
2080 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2085 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2087 struct gl_shader_program
*shProg
;
2088 GET_CURRENT_CONTEXT(ctx
);
2090 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2091 "glProgramParameteri");
2096 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2097 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2098 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2099 * even be in the dispatch table, so we shouldn't need to expclicitly
2102 * On desktop, we ignore the 3.0+ requirement because it is silly.
2105 /* The ARB_get_program_binary extension spec says:
2107 * "An INVALID_VALUE error is generated if the <value> argument to
2108 * ProgramParameteri is not TRUE or FALSE."
2110 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2114 /* No need to notify the driver. Any changes will actually take effect
2115 * the next time the shader is linked.
2117 * The ARB_get_program_binary extension spec says:
2119 * "To indicate that a program binary is likely to be retrieved,
2120 * ProgramParameteri should be called with <pname>
2121 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2122 * will not be in effect until the next time LinkProgram or
2123 * ProgramBinary has been called successfully."
2125 * The resloution of issue 9 in the extension spec also says:
2127 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2128 * to indicate to the GL implementation that this program will
2129 * likely be saved with GetProgramBinary at some point. This will
2130 * give the GL implementation the opportunity to track any state
2131 * changes made to the program before being saved such that when it
2132 * is loaded again a recompile can be avoided."
2134 shProg
->BinaryRetreivableHint
= value
;
2137 case GL_PROGRAM_SEPARABLE
:
2138 /* Spec imply that the behavior is the same as ARB_get_program_binary
2139 * Chapter 7.3 Program Objects
2141 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2144 shProg
->SeparateShader
= value
;
2148 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2149 _mesa_enum_to_string(pname
));
2154 _mesa_error(ctx
, GL_INVALID_VALUE
,
2155 "glProgramParameteri(pname=%s, value=%d): "
2156 "value must be 0 or 1.",
2157 _mesa_enum_to_string(pname
),
2163 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2164 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2165 struct gl_pipeline_object
*shTarget
)
2167 use_program(ctx
, stage
, shProg
, prog
, shTarget
);
2172 * Copy program-specific data generated by linking from the gl_shader_program
2173 * object to the gl_program object referred to by the gl_linked_shader.
2175 * This function expects _mesa_reference_program() to have been previously
2176 * called setting the gl_linked_shaders program reference.
2179 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2180 struct gl_linked_shader
*dst_sh
)
2182 assert(dst_sh
->Program
);
2184 struct gl_program
*dst
= dst_sh
->Program
;
2186 dst
->info
.separate_shader
= src
->SeparateShader
;
2188 switch (dst_sh
->Stage
) {
2189 case MESA_SHADER_GEOMETRY
: {
2190 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2191 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2192 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2195 case MESA_SHADER_FRAGMENT
: {
2196 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2199 case MESA_SHADER_COMPUTE
: {
2200 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2209 * ARB_separate_shader_objects: Compile & Link Program
2212 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2213 const GLchar
* const *strings
)
2215 GET_CURRENT_CONTEXT(ctx
);
2217 const GLuint shader
= create_shader(ctx
, type
);
2221 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2222 * GL_INVALID_VALUE should be generated if count < 0
2225 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2230 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2232 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2233 _mesa_compile_shader(ctx
, sh
);
2235 program
= create_shader_program(ctx
);
2237 struct gl_shader_program
*shProg
;
2238 GLint compiled
= GL_FALSE
;
2240 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2242 shProg
->SeparateShader
= GL_TRUE
;
2244 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2246 attach_shader(ctx
, program
, shader
);
2247 _mesa_link_program(ctx
, shProg
);
2248 detach_shader(ctx
, program
, shader
);
2252 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2253 append
-error
-to
-info
-log
;
2254 shProg
->data
->LinkStatus
= linking_failure
;
2259 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2262 delete_shader(ctx
, shader
);
2270 * For GL_ARB_tessellation_shader
2272 extern void GLAPIENTRY
2273 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2275 GET_CURRENT_CONTEXT(ctx
);
2277 if (!_mesa_has_tessellation(ctx
)) {
2278 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2282 if (pname
!= GL_PATCH_VERTICES
) {
2283 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2287 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2288 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2292 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2296 extern void GLAPIENTRY
2297 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2299 GET_CURRENT_CONTEXT(ctx
);
2301 if (!_mesa_has_tessellation(ctx
)) {
2302 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2307 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2308 FLUSH_VERTICES(ctx
, 0);
2309 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2310 4 * sizeof(GLfloat
));
2311 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2313 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2314 FLUSH_VERTICES(ctx
, 0);
2315 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2316 2 * sizeof(GLfloat
));
2317 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2320 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2326 * ARB_shader_subroutine
2329 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2332 GET_CURRENT_CONTEXT(ctx
);
2333 const char *api_name
= "glGetSubroutineUniformLocation";
2334 struct gl_shader_program
*shProg
;
2335 GLenum resource_type
;
2336 gl_shader_stage stage
;
2338 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2339 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2343 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2344 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2348 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2352 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2353 if (!shProg
->_LinkedShaders
[stage
]) {
2354 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2358 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2359 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2363 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2366 GET_CURRENT_CONTEXT(ctx
);
2367 const char *api_name
= "glGetSubroutineIndex";
2368 struct gl_shader_program
*shProg
;
2369 struct gl_program_resource
*res
;
2370 GLenum resource_type
;
2371 gl_shader_stage stage
;
2373 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2374 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2378 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2379 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2383 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2387 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2388 if (!shProg
->_LinkedShaders
[stage
]) {
2389 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2393 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2394 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2399 return _mesa_program_resource_index(shProg
, res
);
2404 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2405 GLuint index
, GLenum pname
, GLint
*values
)
2407 GET_CURRENT_CONTEXT(ctx
);
2408 const char *api_name
= "glGetActiveSubroutineUniformiv";
2409 struct gl_shader_program
*shProg
;
2410 struct gl_linked_shader
*sh
;
2411 gl_shader_stage stage
;
2412 struct gl_program_resource
*res
;
2413 const struct gl_uniform_storage
*uni
;
2414 GLenum resource_type
;
2417 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2418 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2422 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2423 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2427 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2431 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2432 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2434 sh
= shProg
->_LinkedShaders
[stage
];
2436 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2440 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2441 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2442 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2447 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2448 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2451 values
[0] = uni
->num_compatible_subroutines
;
2455 case GL_COMPATIBLE_SUBROUTINES
: {
2456 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2460 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2461 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2462 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2463 if (fn
->types
[j
] == uni
->type
) {
2464 values
[count
++] = i
;
2472 case GL_UNIFORM_SIZE
:
2473 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2476 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2479 case GL_UNIFORM_NAME_LENGTH
:
2480 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2482 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2483 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2487 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2494 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2495 GLuint index
, GLsizei bufsize
,
2496 GLsizei
*length
, GLchar
*name
)
2498 GET_CURRENT_CONTEXT(ctx
);
2499 const char *api_name
= "glGetActiveSubroutineUniformName";
2500 struct gl_shader_program
*shProg
;
2501 GLenum resource_type
;
2502 gl_shader_stage stage
;
2504 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2505 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2509 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2510 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2514 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2518 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2519 if (!shProg
->_LinkedShaders
[stage
]) {
2520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2524 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2525 /* get program resource name */
2526 _mesa_get_program_resource_name(shProg
, resource_type
,
2528 length
, name
, api_name
);
2533 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2534 GLuint index
, GLsizei bufsize
,
2535 GLsizei
*length
, GLchar
*name
)
2537 GET_CURRENT_CONTEXT(ctx
);
2538 const char *api_name
= "glGetActiveSubroutineName";
2539 struct gl_shader_program
*shProg
;
2540 GLenum resource_type
;
2541 gl_shader_stage stage
;
2543 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2544 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2548 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2549 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2553 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2557 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2558 if (!shProg
->_LinkedShaders
[stage
]) {
2559 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2562 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2563 _mesa_get_program_resource_name(shProg
, resource_type
,
2565 length
, name
, api_name
);
2569 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2570 const GLuint
*indices
)
2572 GET_CURRENT_CONTEXT(ctx
);
2573 const char *api_name
= "glUniformSubroutinesuiv";
2574 gl_shader_stage stage
;
2577 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2578 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2582 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2583 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2587 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2588 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2590 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2594 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2595 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2601 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2607 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2610 for (j
= i
; j
< i
+ uni_count
; j
++) {
2611 struct gl_subroutine_function
*subfn
= NULL
;
2612 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2613 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2617 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2618 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2619 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2626 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2627 if (subfn
->types
[k
] == uni
->type
)
2630 if (k
== subfn
->num_compat_types
) {
2631 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2635 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2640 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
2645 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2648 GET_CURRENT_CONTEXT(ctx
);
2649 const char *api_name
= "glGetUniformSubroutineuiv";
2650 gl_shader_stage stage
;
2652 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2653 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2657 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2658 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2662 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2663 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2665 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2669 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2670 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2674 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2679 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2680 GLenum pname
, GLint
*values
)
2682 GET_CURRENT_CONTEXT(ctx
);
2683 const char *api_name
= "glGetProgramStageiv";
2684 struct gl_shader_program
*shProg
;
2685 struct gl_linked_shader
*sh
;
2686 gl_shader_stage stage
;
2688 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2689 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2693 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2694 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2698 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2702 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2703 sh
= shProg
->_LinkedShaders
[stage
];
2705 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2706 * INVALID_OPERATION in the case of not be linked.
2708 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2709 * same info using other specs (ARB_program_interface_query), without the
2710 * need of the program to be linked, being the value for that case 0.
2712 * But at the same time, some other methods require the program to be
2713 * linked for pname related to locations, so it would be inconsistent to
2714 * not do the same here. So we are:
2715 * * Return GL_INVALID_OPERATION if not linked only for locations.
2716 * * Setting a default value of 0, to be returned if not linked.
2720 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2721 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2726 struct gl_program
*p
= sh
->Program
;
2728 case GL_ACTIVE_SUBROUTINES
:
2729 values
[0] = p
->sh
.NumSubroutineFunctions
;
2731 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2732 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2734 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2735 values
[0] = p
->sh
.NumSubroutineUniforms
;
2737 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2741 GLenum resource_type
;
2742 struct gl_program_resource
*res
;
2744 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2745 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2746 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2748 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2753 values
[0] = max_len
;
2756 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2760 GLenum resource_type
;
2761 struct gl_program_resource
*res
;
2763 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2764 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2765 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2767 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2768 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2774 values
[0] = max_len
;
2778 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2785 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2789 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2790 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2791 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2792 if (fn
->types
[j
] == type
)
2800 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2801 struct gl_program
*p
)
2805 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2810 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2819 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2820 for (j
= 0; j
< uni_count
; j
++) {
2821 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2822 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2825 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2827 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2831 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2832 gl_shader_stage stage
)
2834 if (ctx
->_Shader
->CurrentProgram
[stage
])
2835 _mesa_shader_write_subroutine_index(ctx
,
2836 ctx
->_Shader
->CurrentProgram
[stage
]);
2840 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2841 struct gl_program
*p
)
2845 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2846 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2847 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2848 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2849 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2852 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2853 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2858 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);