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 the named shader program for subsequent rendering.
1249 _mesa_use_shader_program(struct gl_context
*ctx
,
1250 struct gl_shader_program
*shProg
)
1252 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1253 struct gl_program
*new_prog
= NULL
;
1254 if (shProg
&& shProg
->_LinkedShaders
[i
])
1255 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1256 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1258 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1263 * Do validation of the given shader program.
1264 * \param errMsg returns error message if validation fails.
1265 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1268 validate_shader_program(const struct gl_shader_program
*shProg
,
1271 if (!shProg
->data
->LinkStatus
) {
1275 /* From the GL spec, a program is invalid if any of these are true:
1277 any two active samplers in the current program object are of
1278 different types, but refer to the same texture image unit,
1280 any active sampler in the current program object refers to a texture
1281 image unit where fixed-function fragment processing accesses a
1282 texture target that does not match the sampler type, or
1284 the sum of the number of active samplers in the program and the
1285 number of texture image units enabled for fixed-function fragment
1286 processing exceeds the combined limit on the total number of texture
1287 image units allowed.
1291 * Check: any two active samplers in the current program object are of
1292 * different types, but refer to the same texture image unit,
1294 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1302 * Called via glValidateProgram()
1305 validate_program(struct gl_context
*ctx
, GLuint program
)
1307 struct gl_shader_program
*shProg
;
1308 char errMsg
[100] = "";
1310 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1315 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1316 if (!shProg
->data
->Validated
) {
1317 /* update info log */
1318 if (shProg
->data
->InfoLog
) {
1319 ralloc_free(shProg
->data
->InfoLog
);
1321 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1328 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1330 GET_CURRENT_CONTEXT(ctx
);
1331 attach_shader(ctx
, program
, shader
);
1336 _mesa_AttachShader(GLuint program
, GLuint shader
)
1338 GET_CURRENT_CONTEXT(ctx
);
1339 attach_shader(ctx
, program
, shader
);
1344 _mesa_CompileShader(GLuint shaderObj
)
1346 GET_CURRENT_CONTEXT(ctx
);
1347 if (MESA_VERBOSE
& VERBOSE_API
)
1348 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1349 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1350 "glCompileShader"));
1355 _mesa_CreateShader(GLenum type
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 if (MESA_VERBOSE
& VERBOSE_API
)
1359 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1360 return create_shader(ctx
, type
);
1364 GLhandleARB GLAPIENTRY
1365 _mesa_CreateShaderObjectARB(GLenum type
)
1367 GET_CURRENT_CONTEXT(ctx
);
1368 return create_shader(ctx
, type
);
1373 _mesa_CreateProgram(void)
1375 GET_CURRENT_CONTEXT(ctx
);
1376 if (MESA_VERBOSE
& VERBOSE_API
)
1377 _mesa_debug(ctx
, "glCreateProgram\n");
1378 return create_shader_program(ctx
);
1382 GLhandleARB GLAPIENTRY
1383 _mesa_CreateProgramObjectARB(void)
1385 GET_CURRENT_CONTEXT(ctx
);
1386 return create_shader_program(ctx
);
1391 _mesa_DeleteObjectARB(GLhandleARB obj
)
1393 if (MESA_VERBOSE
& VERBOSE_API
) {
1394 GET_CURRENT_CONTEXT(ctx
);
1395 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1399 GET_CURRENT_CONTEXT(ctx
);
1400 FLUSH_VERTICES(ctx
, 0);
1401 if (is_program(ctx
, obj
)) {
1402 delete_shader_program(ctx
, obj
);
1404 else if (is_shader(ctx
, obj
)) {
1405 delete_shader(ctx
, obj
);
1415 _mesa_DeleteProgram(GLuint name
)
1418 GET_CURRENT_CONTEXT(ctx
);
1419 FLUSH_VERTICES(ctx
, 0);
1420 delete_shader_program(ctx
, name
);
1426 _mesa_DeleteShader(GLuint name
)
1429 GET_CURRENT_CONTEXT(ctx
);
1430 FLUSH_VERTICES(ctx
, 0);
1431 delete_shader(ctx
, name
);
1437 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1439 GET_CURRENT_CONTEXT(ctx
);
1440 detach_shader(ctx
, program
, shader
);
1445 _mesa_DetachShader(GLuint program
, GLuint shader
)
1447 GET_CURRENT_CONTEXT(ctx
);
1448 detach_shader(ctx
, program
, shader
);
1453 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1454 GLsizei
* count
, GLhandleARB
* obj
)
1456 GET_CURRENT_CONTEXT(ctx
);
1457 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1462 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1463 GLsizei
*count
, GLuint
*obj
)
1465 GET_CURRENT_CONTEXT(ctx
);
1466 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1471 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1472 GLcharARB
* infoLog
)
1474 GET_CURRENT_CONTEXT(ctx
);
1475 if (is_program(ctx
, object
)) {
1476 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1478 else if (is_shader(ctx
, object
)) {
1479 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1482 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1488 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1490 GET_CURRENT_CONTEXT(ctx
);
1491 /* Implement in terms of GetProgramiv, GetShaderiv */
1492 if (is_program(ctx
, object
)) {
1493 if (pname
== GL_OBJECT_TYPE_ARB
) {
1494 *params
= GL_PROGRAM_OBJECT_ARB
;
1497 get_programiv(ctx
, object
, pname
, params
);
1500 else if (is_shader(ctx
, object
)) {
1501 if (pname
== GL_OBJECT_TYPE_ARB
) {
1502 *params
= GL_SHADER_OBJECT_ARB
;
1505 get_shaderiv(ctx
, object
, pname
, params
);
1509 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1515 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1518 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1519 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1520 params
[0] = (GLfloat
) iparams
[0];
1525 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1527 GET_CURRENT_CONTEXT(ctx
);
1528 get_programiv(ctx
, program
, pname
, params
);
1533 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1535 GET_CURRENT_CONTEXT(ctx
);
1536 get_shaderiv(ctx
, shader
, pname
, params
);
1541 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1542 GLsizei
*length
, GLchar
*infoLog
)
1544 GET_CURRENT_CONTEXT(ctx
);
1545 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1550 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1551 GLsizei
*length
, GLchar
*infoLog
)
1553 GET_CURRENT_CONTEXT(ctx
);
1554 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1559 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1560 GLsizei
*length
, GLchar
*sourceOut
)
1562 GET_CURRENT_CONTEXT(ctx
);
1563 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1567 GLhandleARB GLAPIENTRY
1568 _mesa_GetHandleARB(GLenum pname
)
1570 GET_CURRENT_CONTEXT(ctx
);
1571 return get_handle(ctx
, pname
);
1575 GLboolean GLAPIENTRY
1576 _mesa_IsProgram(GLuint name
)
1578 GET_CURRENT_CONTEXT(ctx
);
1579 return is_program(ctx
, name
);
1583 GLboolean GLAPIENTRY
1584 _mesa_IsShader(GLuint name
)
1586 GET_CURRENT_CONTEXT(ctx
);
1587 return is_shader(ctx
, name
);
1592 _mesa_LinkProgram(GLuint programObj
)
1594 GET_CURRENT_CONTEXT(ctx
);
1595 if (MESA_VERBOSE
& VERBOSE_API
)
1596 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1597 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1601 #ifdef ENABLE_SHADER_CACHE
1603 * Generate a SHA-1 hash value string for given source string.
1606 generate_sha1(const char *source
, char sha_str
[64])
1608 unsigned char sha
[20];
1609 _mesa_sha1_compute(source
, strlen(source
), sha
);
1610 _mesa_sha1_format(sha_str
, sha
);
1614 * Construct a full path for shader replacement functionality using
1617 * <path>/<stage prefix>_<CHECKSUM>.glsl
1620 construct_name(const gl_shader_stage stage
, const char *source
,
1624 static const char *types
[] = {
1625 "VS", "TC", "TE", "GS", "FS", "CS",
1628 generate_sha1(source
, sha
);
1629 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1633 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1636 dump_shader(const gl_shader_stage stage
, const char *source
)
1638 static bool path_exists
= true;
1645 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1647 path_exists
= false;
1651 char *name
= construct_name(stage
, source
, dump_path
);
1653 f
= fopen(name
, "w");
1658 GET_CURRENT_CONTEXT(ctx
);
1659 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1666 * Read shader source code from a file.
1667 * Useful for debugging to override an app's shader.
1670 read_shader(const gl_shader_stage stage
, const char *source
)
1673 static bool path_exists
= true;
1674 int len
, shader_size
= 0;
1681 read_path
= getenv("MESA_SHADER_READ_PATH");
1683 path_exists
= false;
1687 char *name
= construct_name(stage
, source
, read_path
);
1688 f
= fopen(name
, "r");
1693 /* allocate enough room for the entire shader */
1694 fseek(f
, 0, SEEK_END
);
1695 shader_size
= ftell(f
);
1697 assert(shader_size
);
1699 /* add one for terminating zero */
1702 buffer
= malloc(shader_size
);
1705 len
= fread(buffer
, 1, shader_size
, f
);
1713 #endif /* ENABLE_SHADER_CACHE */
1716 * Called via glShaderSource() and glShaderSourceARB() API functions.
1717 * Basically, concatenate the source code strings into one long string
1718 * and pass it to _mesa_shader_source().
1721 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1722 const GLchar
* const * string
, const GLint
* length
)
1724 GET_CURRENT_CONTEXT(ctx
);
1726 GLsizei i
, totalLength
;
1728 struct gl_shader
*sh
;
1730 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1734 if (string
== NULL
) {
1735 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1740 * This array holds offsets of where the appropriate string ends, thus the
1741 * last element will be set to the total length of the source code.
1743 offsets
= malloc(count
* sizeof(GLint
));
1744 if (offsets
== NULL
) {
1745 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1749 for (i
= 0; i
< count
; i
++) {
1750 if (string
[i
] == NULL
) {
1751 free((GLvoid
*) offsets
);
1752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1753 "glShaderSourceARB(null string)");
1756 if (length
== NULL
|| length
[i
] < 0)
1757 offsets
[i
] = strlen(string
[i
]);
1759 offsets
[i
] = length
[i
];
1760 /* accumulate string lengths */
1762 offsets
[i
] += offsets
[i
- 1];
1765 /* Total length of source string is sum off all strings plus two.
1766 * One extra byte for terminating zero, another extra byte to silence
1767 * valgrind warnings in the parser/grammer code.
1769 totalLength
= offsets
[count
- 1] + 2;
1770 source
= malloc(totalLength
* sizeof(GLcharARB
));
1771 if (source
== NULL
) {
1772 free((GLvoid
*) offsets
);
1773 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1777 for (i
= 0; i
< count
; i
++) {
1778 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1779 memcpy(source
+ start
, string
[i
],
1780 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1782 source
[totalLength
- 1] = '\0';
1783 source
[totalLength
- 2] = '\0';
1785 #ifdef ENABLE_SHADER_CACHE
1786 GLcharARB
*replacement
;
1788 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1789 * if corresponding entry found from MESA_SHADER_READ_PATH.
1791 dump_shader(sh
->Stage
, source
);
1793 replacement
= read_shader(sh
->Stage
, source
);
1796 source
= replacement
;
1798 #endif /* ENABLE_SHADER_CACHE */
1800 shader_source(sh
, source
);
1807 _mesa_UseProgram(GLuint program
)
1809 GET_CURRENT_CONTEXT(ctx
);
1810 struct gl_shader_program
*shProg
= NULL
;
1812 if (MESA_VERBOSE
& VERBOSE_API
)
1813 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1815 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1816 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1817 "glUseProgram(transform feedback active)");
1822 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1826 if (!shProg
->data
->LinkStatus
) {
1827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1828 "glUseProgram(program %u not linked)", program
);
1833 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1834 print_shader_info(shProg
);
1838 /* The ARB_separate_shader_object spec says:
1840 * "The executable code for an individual shader stage is taken from
1841 * the current program for that stage. If there is a current program
1842 * object established by UseProgram, that program is considered current
1843 * for all stages. Otherwise, if there is a bound program pipeline
1844 * object (section 2.14.PPO), the program bound to the appropriate
1845 * stage of the pipeline object is considered current."
1848 /* Attach shader state to the binding point */
1849 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1850 /* Update the program */
1851 _mesa_use_shader_program(ctx
, shProg
);
1853 /* Must be done first: detach the progam */
1854 _mesa_use_shader_program(ctx
, shProg
);
1855 /* Unattach shader_state binding point */
1856 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1857 /* If a pipeline was bound, rebind it */
1858 if (ctx
->Pipeline
.Current
) {
1859 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1866 _mesa_ValidateProgram(GLuint program
)
1868 GET_CURRENT_CONTEXT(ctx
);
1869 validate_program(ctx
, program
);
1874 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1877 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1878 GLint
* range
, GLint
* precision
)
1880 const struct gl_program_constants
*limits
;
1881 const struct gl_precision
*p
;
1882 GET_CURRENT_CONTEXT(ctx
);
1884 switch (shadertype
) {
1885 case GL_VERTEX_SHADER
:
1886 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1888 case GL_FRAGMENT_SHADER
:
1889 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1892 _mesa_error(ctx
, GL_INVALID_ENUM
,
1893 "glGetShaderPrecisionFormat(shadertype)");
1897 switch (precisiontype
) {
1899 p
= &limits
->LowFloat
;
1901 case GL_MEDIUM_FLOAT
:
1902 p
= &limits
->MediumFloat
;
1905 p
= &limits
->HighFloat
;
1908 p
= &limits
->LowInt
;
1911 p
= &limits
->MediumInt
;
1914 p
= &limits
->HighInt
;
1917 _mesa_error(ctx
, GL_INVALID_ENUM
,
1918 "glGetShaderPrecisionFormat(precisiontype)");
1922 range
[0] = p
->RangeMin
;
1923 range
[1] = p
->RangeMax
;
1924 precision
[0] = p
->Precision
;
1929 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1932 _mesa_ReleaseShaderCompiler(void)
1934 _mesa_destroy_shader_compiler_caches();
1939 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1942 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1943 const void* binary
, GLint length
)
1945 GET_CURRENT_CONTEXT(ctx
);
1947 (void) binaryformat
;
1950 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
1951 * page 88 of the OpenGL 4.5 specs state:
1953 * "An INVALID_VALUE error is generated if count or length is negative.
1954 * An INVALID_ENUM error is generated if binaryformat is not a supported
1955 * format returned in SHADER_BINARY_FORMATS."
1957 if (n
< 0 || length
< 0) {
1958 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
1962 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
1967 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1968 GLenum
*binaryFormat
, GLvoid
*binary
)
1970 struct gl_shader_program
*shProg
;
1971 GLsizei length_dummy
;
1972 GET_CURRENT_CONTEXT(ctx
);
1975 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1979 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1983 /* The ARB_get_program_binary spec says:
1985 * "If <length> is NULL, then no length is returned."
1987 * Ensure that length always points to valid storage to avoid multiple NULL
1988 * pointer checks below.
1991 length
= &length_dummy
;
1994 /* The ARB_get_program_binary spec says:
1996 * "When a program object's LINK_STATUS is FALSE, its program binary
1997 * length is zero, and a call to GetProgramBinary will generate an
1998 * INVALID_OPERATION error.
2000 if (!shProg
->data
->LinkStatus
) {
2001 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2002 "glGetProgramBinary(program %u not linked)",
2009 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2010 "glGetProgramBinary(driver supports zero binary formats)");
2012 (void) binaryFormat
;
2017 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2018 const GLvoid
*binary
, GLsizei length
)
2020 struct gl_shader_program
*shProg
;
2021 GET_CURRENT_CONTEXT(ctx
);
2023 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2027 (void) binaryFormat
;
2030 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2032 * "If a negative number is provided where an argument of type sizei or
2033 * sizeiptr is specified, an INVALID_VALUE error is generated."
2036 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2040 /* The ARB_get_program_binary spec says:
2042 * "<binaryFormat> and <binary> must be those returned by a previous
2043 * call to GetProgramBinary, and <length> must be the length of the
2044 * program binary as returned by GetProgramBinary or GetProgramiv with
2045 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2046 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2049 * Since any value of binaryFormat passed "is not one of those specified as
2050 * allowable for [this] command, an INVALID_ENUM error is generated."
2052 shProg
->data
->LinkStatus
= linking_failure
;
2053 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2058 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2060 struct gl_shader_program
*shProg
;
2061 GET_CURRENT_CONTEXT(ctx
);
2063 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2064 "glProgramParameteri");
2069 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2070 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2071 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2072 * even be in the dispatch table, so we shouldn't need to expclicitly
2075 * On desktop, we ignore the 3.0+ requirement because it is silly.
2078 /* The ARB_get_program_binary extension spec says:
2080 * "An INVALID_VALUE error is generated if the <value> argument to
2081 * ProgramParameteri is not TRUE or FALSE."
2083 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2087 /* No need to notify the driver. Any changes will actually take effect
2088 * the next time the shader is linked.
2090 * The ARB_get_program_binary extension spec says:
2092 * "To indicate that a program binary is likely to be retrieved,
2093 * ProgramParameteri should be called with <pname>
2094 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2095 * will not be in effect until the next time LinkProgram or
2096 * ProgramBinary has been called successfully."
2098 * The resloution of issue 9 in the extension spec also says:
2100 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2101 * to indicate to the GL implementation that this program will
2102 * likely be saved with GetProgramBinary at some point. This will
2103 * give the GL implementation the opportunity to track any state
2104 * changes made to the program before being saved such that when it
2105 * is loaded again a recompile can be avoided."
2107 shProg
->BinaryRetreivableHint
= value
;
2110 case GL_PROGRAM_SEPARABLE
:
2111 /* Spec imply that the behavior is the same as ARB_get_program_binary
2112 * Chapter 7.3 Program Objects
2114 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2117 shProg
->SeparateShader
= value
;
2121 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2122 _mesa_enum_to_string(pname
));
2127 _mesa_error(ctx
, GL_INVALID_VALUE
,
2128 "glProgramParameteri(pname=%s, value=%d): "
2129 "value must be 0 or 1.",
2130 _mesa_enum_to_string(pname
),
2136 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2137 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2138 struct gl_pipeline_object
*shTarget
)
2140 struct gl_program
**target
;
2142 target
= &shTarget
->CurrentProgram
[stage
];
2144 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2147 if (*target
!= prog
) {
2148 /* Program is current, flush it */
2149 if (shTarget
== ctx
->_Shader
) {
2150 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2153 _mesa_reference_shader_program(ctx
,
2154 &shTarget
->ReferencedPrograms
[stage
],
2156 _mesa_reference_program(ctx
, target
, prog
);
2164 * Copy program-specific data generated by linking from the gl_shader_program
2165 * object to the gl_program object referred to by the gl_linked_shader.
2167 * This function expects _mesa_reference_program() to have been previously
2168 * called setting the gl_linked_shaders program reference.
2171 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2172 struct gl_linked_shader
*dst_sh
)
2174 assert(dst_sh
->Program
);
2176 struct gl_program
*dst
= dst_sh
->Program
;
2178 dst
->info
.separate_shader
= src
->SeparateShader
;
2180 switch (dst_sh
->Stage
) {
2181 case MESA_SHADER_GEOMETRY
: {
2182 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2183 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2184 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2187 case MESA_SHADER_FRAGMENT
: {
2188 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2191 case MESA_SHADER_COMPUTE
: {
2192 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2201 * ARB_separate_shader_objects: Compile & Link Program
2204 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2205 const GLchar
* const *strings
)
2207 GET_CURRENT_CONTEXT(ctx
);
2209 const GLuint shader
= create_shader(ctx
, type
);
2213 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2214 * GL_INVALID_VALUE should be generated if count < 0
2217 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2222 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2224 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2225 _mesa_compile_shader(ctx
, sh
);
2227 program
= create_shader_program(ctx
);
2229 struct gl_shader_program
*shProg
;
2230 GLint compiled
= GL_FALSE
;
2232 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2234 shProg
->SeparateShader
= GL_TRUE
;
2236 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2238 attach_shader(ctx
, program
, shader
);
2239 _mesa_link_program(ctx
, shProg
);
2240 detach_shader(ctx
, program
, shader
);
2244 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2245 append
-error
-to
-info
-log
;
2246 shProg
->data
->LinkStatus
= linking_failure
;
2251 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2254 delete_shader(ctx
, shader
);
2262 * For GL_ARB_tessellation_shader
2264 extern void GLAPIENTRY
2265 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2267 GET_CURRENT_CONTEXT(ctx
);
2269 if (!_mesa_has_tessellation(ctx
)) {
2270 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2274 if (pname
!= GL_PATCH_VERTICES
) {
2275 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2279 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2280 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2284 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2288 extern void GLAPIENTRY
2289 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2291 GET_CURRENT_CONTEXT(ctx
);
2293 if (!_mesa_has_tessellation(ctx
)) {
2294 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2299 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2300 FLUSH_VERTICES(ctx
, 0);
2301 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2302 4 * sizeof(GLfloat
));
2303 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2305 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2306 FLUSH_VERTICES(ctx
, 0);
2307 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2308 2 * sizeof(GLfloat
));
2309 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2312 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2318 * ARB_shader_subroutine
2321 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2324 GET_CURRENT_CONTEXT(ctx
);
2325 const char *api_name
= "glGetSubroutineUniformLocation";
2326 struct gl_shader_program
*shProg
;
2327 GLenum resource_type
;
2328 gl_shader_stage stage
;
2330 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2331 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2335 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2336 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2340 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2344 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2345 if (!shProg
->_LinkedShaders
[stage
]) {
2346 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2350 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2351 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2355 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2358 GET_CURRENT_CONTEXT(ctx
);
2359 const char *api_name
= "glGetSubroutineIndex";
2360 struct gl_shader_program
*shProg
;
2361 struct gl_program_resource
*res
;
2362 GLenum resource_type
;
2363 gl_shader_stage stage
;
2365 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2366 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2370 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2371 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2375 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2379 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2380 if (!shProg
->_LinkedShaders
[stage
]) {
2381 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2385 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2386 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2391 return _mesa_program_resource_index(shProg
, res
);
2396 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2397 GLuint index
, GLenum pname
, GLint
*values
)
2399 GET_CURRENT_CONTEXT(ctx
);
2400 const char *api_name
= "glGetActiveSubroutineUniformiv";
2401 struct gl_shader_program
*shProg
;
2402 struct gl_linked_shader
*sh
;
2403 gl_shader_stage stage
;
2404 struct gl_program_resource
*res
;
2405 const struct gl_uniform_storage
*uni
;
2406 GLenum resource_type
;
2409 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2410 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2414 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2419 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2423 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2424 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2426 sh
= shProg
->_LinkedShaders
[stage
];
2428 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2432 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2433 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2434 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2439 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2440 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2443 values
[0] = uni
->num_compatible_subroutines
;
2447 case GL_COMPATIBLE_SUBROUTINES
: {
2448 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2452 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2453 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2454 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2455 if (fn
->types
[j
] == uni
->type
) {
2456 values
[count
++] = i
;
2464 case GL_UNIFORM_SIZE
:
2465 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2468 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2471 case GL_UNIFORM_NAME_LENGTH
:
2472 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2474 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2475 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2479 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2486 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2487 GLuint index
, GLsizei bufsize
,
2488 GLsizei
*length
, GLchar
*name
)
2490 GET_CURRENT_CONTEXT(ctx
);
2491 const char *api_name
= "glGetActiveSubroutineUniformName";
2492 struct gl_shader_program
*shProg
;
2493 GLenum resource_type
;
2494 gl_shader_stage stage
;
2496 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2497 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2501 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2502 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2506 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2510 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2511 if (!shProg
->_LinkedShaders
[stage
]) {
2512 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2516 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2517 /* get program resource name */
2518 _mesa_get_program_resource_name(shProg
, resource_type
,
2520 length
, name
, api_name
);
2525 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2526 GLuint index
, GLsizei bufsize
,
2527 GLsizei
*length
, GLchar
*name
)
2529 GET_CURRENT_CONTEXT(ctx
);
2530 const char *api_name
= "glGetActiveSubroutineName";
2531 struct gl_shader_program
*shProg
;
2532 GLenum resource_type
;
2533 gl_shader_stage stage
;
2535 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2536 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2540 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2541 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2545 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2549 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2550 if (!shProg
->_LinkedShaders
[stage
]) {
2551 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2554 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2555 _mesa_get_program_resource_name(shProg
, resource_type
,
2557 length
, name
, api_name
);
2561 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2562 const GLuint
*indices
)
2564 GET_CURRENT_CONTEXT(ctx
);
2565 const char *api_name
= "glUniformSubroutinesuiv";
2566 gl_shader_stage stage
;
2569 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2570 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2574 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2575 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2579 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2580 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2582 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2586 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2587 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2593 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2599 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2602 for (j
= i
; j
< i
+ uni_count
; j
++) {
2603 struct gl_subroutine_function
*subfn
= NULL
;
2604 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2605 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2609 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2610 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2611 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2618 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2619 if (subfn
->types
[k
] == uni
->type
)
2622 if (k
== subfn
->num_compat_types
) {
2623 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2627 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2632 FLUSH_VERTICES(ctx
, _NEW_PROGRAM_CONSTANTS
);
2637 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2640 GET_CURRENT_CONTEXT(ctx
);
2641 const char *api_name
= "glGetUniformSubroutineuiv";
2642 gl_shader_stage stage
;
2644 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2645 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2649 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2650 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2654 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2655 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2657 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2661 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2662 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2666 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2671 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2672 GLenum pname
, GLint
*values
)
2674 GET_CURRENT_CONTEXT(ctx
);
2675 const char *api_name
= "glGetProgramStageiv";
2676 struct gl_shader_program
*shProg
;
2677 struct gl_linked_shader
*sh
;
2678 gl_shader_stage stage
;
2680 if (!_mesa_has_ARB_shader_subroutine(ctx
)) {
2681 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2685 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2686 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2690 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2694 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2695 sh
= shProg
->_LinkedShaders
[stage
];
2697 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2698 * INVALID_OPERATION in the case of not be linked.
2700 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2701 * same info using other specs (ARB_program_interface_query), without the
2702 * need of the program to be linked, being the value for that case 0.
2704 * But at the same time, some other methods require the program to be
2705 * linked for pname related to locations, so it would be inconsistent to
2706 * not do the same here. So we are:
2707 * * Return GL_INVALID_OPERATION if not linked only for locations.
2708 * * Setting a default value of 0, to be returned if not linked.
2712 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2713 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2718 struct gl_program
*p
= sh
->Program
;
2720 case GL_ACTIVE_SUBROUTINES
:
2721 values
[0] = p
->sh
.NumSubroutineFunctions
;
2723 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2724 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2726 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2727 values
[0] = p
->sh
.NumSubroutineUniforms
;
2729 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2733 GLenum resource_type
;
2734 struct gl_program_resource
*res
;
2736 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2737 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2738 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2740 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2745 values
[0] = max_len
;
2748 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2752 GLenum resource_type
;
2753 struct gl_program_resource
*res
;
2755 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2756 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2757 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2759 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2760 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2766 values
[0] = max_len
;
2770 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2777 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2781 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2782 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2783 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2784 if (fn
->types
[j
] == type
)
2792 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2793 struct gl_program
*p
)
2797 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2802 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2811 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2812 for (j
= 0; j
< uni_count
; j
++) {
2813 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2814 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2817 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2819 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2823 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2824 gl_shader_stage stage
)
2826 if (ctx
->_Shader
->CurrentProgram
[stage
])
2827 _mesa_shader_write_subroutine_index(ctx
,
2828 ctx
->_Shader
->CurrentProgram
[stage
]);
2832 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2833 struct gl_program
*p
)
2837 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2838 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2839 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2840 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2841 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2844 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2845 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2850 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);