2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
30 * Implementation of GLSL-related API functions.
31 * The glUniform* functions are in uniforms.c
35 * 1. Check that the right error code is generated for all _mesa_error() calls.
36 * 2. Insert FLUSH_VERTICES calls in various places
41 #include "main/glheader.h"
42 #include "main/context.h"
43 #include "main/dispatch.h"
44 #include "main/enums.h"
45 #include "main/hash.h"
46 #include "main/mtypes.h"
47 #include "main/pipelineobj.h"
48 #include "main/shaderapi.h"
49 #include "main/shaderobj.h"
50 #include "main/transformfeedback.h"
51 #include "main/uniforms.h"
52 #include "compiler/glsl/glsl_parser_extras.h"
53 #include "compiler/glsl/ir.h"
54 #include "compiler/glsl/ir_uniform.h"
55 #include "compiler/glsl/program.h"
56 #include "program/program.h"
57 #include "program/prog_print.h"
58 #include "program/prog_parameter.h"
59 #include "util/ralloc.h"
60 #include "util/hash_table.h"
61 #include "util/mesa-sha1.h"
62 #include "util/crc32.h"
65 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
68 _mesa_get_shader_flags(void)
70 GLbitfield flags
= 0x0;
71 const char *env
= getenv("MESA_GLSL");
74 if (strstr(env
, "dump_on_error"))
75 flags
|= GLSL_DUMP_ON_ERROR
;
76 else if (strstr(env
, "dump"))
78 if (strstr(env
, "log"))
80 if (strstr(env
, "cache_fb"))
81 flags
|= GLSL_CACHE_FALLBACK
;
82 if (strstr(env
, "cache_info"))
83 flags
|= GLSL_CACHE_INFO
;
84 if (strstr(env
, "nopvert"))
85 flags
|= GLSL_NOP_VERT
;
86 if (strstr(env
, "nopfrag"))
87 flags
|= GLSL_NOP_FRAG
;
88 if (strstr(env
, "uniform"))
89 flags
|= GLSL_UNIFORMS
;
90 if (strstr(env
, "useprog"))
91 flags
|= GLSL_USE_PROG
;
92 if (strstr(env
, "errors"))
93 flags
|= GLSL_REPORT_ERRORS
;
100 * Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
103 _mesa_get_shader_capture_path(void)
105 static bool read_env_var
= false;
106 static const char *path
= NULL
;
109 path
= getenv("MESA_SHADER_CAPTURE_PATH");
117 * Initialize context's shader state.
120 _mesa_init_shader_state(struct gl_context
*ctx
)
122 /* Device drivers may override these to control what kind of instructions
123 * are generated by the GLSL compiler.
125 struct gl_shader_compiler_options options
;
129 memset(&options
, 0, sizeof(options
));
130 options
.MaxUnrollIterations
= 32;
131 options
.MaxIfDepth
= UINT_MAX
;
133 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
134 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
136 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
138 if (ctx
->Shader
.Flags
!= 0)
139 ctx
->Const
.GenerateTemporaryNames
= true;
141 /* Extended for ARB_separate_shader_objects */
142 ctx
->Shader
.RefCount
= 1;
143 ctx
->TessCtrlProgram
.patch_vertices
= 3;
144 for (i
= 0; i
< 4; ++i
)
145 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
146 for (i
= 0; i
< 2; ++i
)
147 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
152 * Free the per-context shader-related state.
155 _mesa_free_shader_state(struct gl_context
*ctx
)
157 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
158 _mesa_reference_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
], NULL
);
160 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
162 /* Extended for ARB_separate_shader_objects */
163 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
165 assert(ctx
->Shader
.RefCount
== 1);
170 * Copy string from <src> to <dst>, up to maxLength characters, returning
171 * length of <dst> in <length>.
172 * \param src the strings source
173 * \param maxLength max chars to copy
174 * \param length returns number of chars copied
175 * \param dst the string destination
178 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
179 GLsizei
*length
, const GLchar
*src
)
182 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
193 * Confirm that the a shader type is valid and supported by the implementation
195 * \param ctx Current GL context
196 * \param type Shader target
200 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
202 /* Note: when building built-in GLSL functions, this function may be
203 * invoked with ctx == NULL. In that case, we can only validate that it's
204 * a shader target we recognize, not that it's supported in the current
205 * context. But that's fine--we don't need any further validation than
206 * that when building built-in GLSL functions.
210 case GL_FRAGMENT_SHADER
:
211 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
212 case GL_VERTEX_SHADER
:
213 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
214 case GL_GEOMETRY_SHADER_ARB
:
215 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
216 case GL_TESS_CONTROL_SHADER
:
217 case GL_TESS_EVALUATION_SHADER
:
218 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
219 case GL_COMPUTE_SHADER
:
220 return ctx
== NULL
|| _mesa_has_compute_shaders(ctx
);
228 is_program(struct gl_context
*ctx
, GLuint name
)
230 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
231 return shProg
? GL_TRUE
: GL_FALSE
;
236 is_shader(struct gl_context
*ctx
, GLuint name
)
238 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
239 return shader
? GL_TRUE
: GL_FALSE
;
244 * Attach shader to a shader program.
247 attach_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
248 struct gl_shader
*sh
)
250 GLuint n
= shProg
->NumShaders
;
252 shProg
->Shaders
= realloc(shProg
->Shaders
,
253 (n
+ 1) * sizeof(struct gl_shader
*));
254 if (!shProg
->Shaders
) {
255 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
260 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
261 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
262 shProg
->NumShaders
++;
266 attach_shader_err(struct gl_context
*ctx
, GLuint program
, GLuint shader
,
269 struct gl_shader_program
*shProg
;
270 struct gl_shader
*sh
;
273 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
275 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
279 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
284 n
= shProg
->NumShaders
;
285 for (i
= 0; i
< n
; i
++) {
286 if (shProg
->Shaders
[i
] == sh
) {
287 /* The shader is already attched to this program. The
288 * GL_ARB_shader_objects spec says:
290 * "The error INVALID_OPERATION is generated by AttachObjectARB
291 * if <obj> is already attached to <containerObj>."
293 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
295 } else if (same_type_disallowed
&&
296 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
297 /* Shader with the same type is already attached to this program,
298 * OpenGL ES 2.0 and 3.0 specs say:
300 * "Multiple shader objects of the same type may not be attached
301 * to a single program object. [...] The error INVALID_OPERATION
302 * is generated if [...] another shader object of the same type
303 * as shader is already attached to program."
305 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
310 attach_shader(ctx
, shProg
, sh
);
314 attach_shader_no_error(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
316 struct gl_shader_program
*shProg
;
317 struct gl_shader
*sh
;
319 shProg
= _mesa_lookup_shader_program(ctx
, program
);
320 sh
= _mesa_lookup_shader(ctx
, shader
);
322 attach_shader(ctx
, shProg
, sh
);
326 create_shader(struct gl_context
*ctx
, GLenum type
)
328 struct gl_shader
*sh
;
331 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
332 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
333 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
335 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
336 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
343 create_shader_err(struct gl_context
*ctx
, GLenum type
, const char *caller
)
345 if (!_mesa_validate_shader_target(ctx
, type
)) {
346 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s)",
347 caller
, _mesa_enum_to_string(type
));
351 return create_shader(ctx
, type
);
356 create_shader_program(struct gl_context
*ctx
)
359 struct gl_shader_program
*shProg
;
361 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
363 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
365 shProg
= _mesa_new_shader_program(name
);
367 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
369 assert(shProg
->RefCount
== 1);
371 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
378 * Delete a shader program. Actually, just decrement the program's
379 * reference count and mark it as DeletePending.
380 * Used to implement glDeleteProgram() and glDeleteObjectARB().
383 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
386 * NOTE: deleting shaders/programs works a bit differently than
387 * texture objects (and buffer objects, etc). Shader/program
388 * handles/IDs exist in the hash table until the object is really
389 * deleted (refcount==0). With texture objects, the handle/ID is
390 * removed from the hash table in glDeleteTextures() while the tex
391 * object itself might linger until its refcount goes to zero.
393 struct gl_shader_program
*shProg
;
395 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
399 if (!shProg
->DeletePending
) {
400 shProg
->DeletePending
= GL_TRUE
;
402 /* effectively, decr shProg's refcount */
403 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
409 delete_shader(struct gl_context
*ctx
, GLuint shader
)
411 struct gl_shader
*sh
;
413 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
417 if (!sh
->DeletePending
) {
418 sh
->DeletePending
= GL_TRUE
;
420 /* effectively, decr sh's refcount */
421 _mesa_reference_shader(ctx
, &sh
, NULL
);
427 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
429 struct gl_shader_program
*shProg
;
433 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
437 n
= shProg
->NumShaders
;
439 for (i
= 0; i
< n
; i
++) {
440 if (shProg
->Shaders
[i
]->Name
== shader
) {
442 struct gl_shader
**newList
;
445 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
447 /* alloc new, smaller array */
448 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
450 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
453 /* Copy old list entries to new list, skipping removed entry at [i] */
454 for (j
= 0; j
< i
; j
++) {
455 newList
[j
] = shProg
->Shaders
[j
];
458 newList
[j
++] = shProg
->Shaders
[i
];
461 /* Free old list and install new one */
462 free(shProg
->Shaders
);
463 shProg
->Shaders
= newList
;
464 shProg
->NumShaders
= n
- 1;
467 /* sanity check - make sure the new list's entries are sensible */
468 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
469 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
470 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
471 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
472 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
473 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
474 assert(shProg
->Shaders
[j
]->RefCount
> 0);
485 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
486 err
= GL_INVALID_OPERATION
;
488 err
= GL_INVALID_VALUE
;
489 _mesa_error(ctx
, err
, "glDetachShader(shader)");
496 * Return list of shaders attached to shader program.
499 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
500 GLsizei
*count
, GLuint
*obj
)
502 struct gl_shader_program
*shProg
;
505 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
510 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
514 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
515 obj
[i
] = shProg
->Shaders
[i
]->Name
;
524 * glGetHandleARB() - return ID/name of currently bound shader program.
527 get_handle(struct gl_context
*ctx
, GLenum pname
)
529 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
530 if (ctx
->_Shader
->ActiveProgram
)
531 return ctx
->_Shader
->ActiveProgram
->Name
;
536 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
543 * Check if a geometry shader query is valid at this time. If not, report an
544 * error and return false.
546 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
548 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
549 * are queried for a program which has not been linked successfully, or
550 * which does not contain objects to form a geometry shader, then an
551 * INVALID_OPERATION error is generated."
554 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
556 if (shProg
->data
->LinkStatus
&&
557 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
561 _mesa_error(ctx
, GL_INVALID_OPERATION
,
562 "glGetProgramv(linked geometry shader required)");
568 * Check if a tessellation control shader query is valid at this time.
569 * If not, report an error and return false.
571 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
573 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
574 * not been linked successfully, or which does not contain objects to
575 * form a tessellation control shader, then an INVALID_OPERATION error is
579 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
581 if (shProg
->data
->LinkStatus
&&
582 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
586 _mesa_error(ctx
, GL_INVALID_OPERATION
,
587 "glGetProgramv(linked tessellation control shader required)");
593 * Check if a tessellation evaluation shader query is valid at this time.
594 * If not, report an error and return false.
596 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
598 * "If any of the pname values in this paragraph are queried for a program
599 * which has not been linked successfully, or which does not contain
600 * objects to form a tessellation evaluation shader, then an
601 * INVALID_OPERATION error is generated."
605 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
607 if (shProg
->data
->LinkStatus
&&
608 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
612 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
613 "evaluation shader required)");
619 * glGetProgramiv() - get shader program state.
620 * Note that this is for GLSL shader programs, not ARB vertex/fragment
621 * programs (see glGetProgramivARB).
624 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
627 struct gl_shader_program
*shProg
628 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
630 /* Is transform feedback available in this context?
633 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
634 || ctx
->API
== API_OPENGL_CORE
635 || _mesa_is_gles3(ctx
);
637 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
638 * and GL 3.2) are available in this context
640 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
641 const bool has_tess
= _mesa_has_tessellation(ctx
);
643 /* Are uniform buffer objects available in this context?
646 (ctx
->API
== API_OPENGL_COMPAT
&&
647 ctx
->Extensions
.ARB_uniform_buffer_object
)
648 || ctx
->API
== API_OPENGL_CORE
649 || _mesa_is_gles3(ctx
);
656 case GL_DELETE_STATUS
:
657 *params
= shProg
->DeletePending
;
660 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
662 case GL_VALIDATE_STATUS
:
663 *params
= shProg
->data
->Validated
;
665 case GL_INFO_LOG_LENGTH
:
666 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
667 strlen(shProg
->data
->InfoLog
) + 1 : 0;
669 case GL_ATTACHED_SHADERS
:
670 *params
= shProg
->NumShaders
;
672 case GL_ACTIVE_ATTRIBUTES
:
673 *params
= _mesa_count_active_attribs(shProg
);
675 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
676 *params
= _mesa_longest_attribute_name_length(shProg
);
678 case GL_ACTIVE_UNIFORMS
: {
680 const unsigned num_uniforms
=
681 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
682 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
683 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
688 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
691 const unsigned num_uniforms
=
692 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
694 for (i
= 0; i
< num_uniforms
; i
++) {
695 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
698 /* Add one for the terminating NUL character for a non-array, and
699 * 4 for the "[0]" and the NUL for an array.
701 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
702 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
711 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
714 *params
= shProg
->TransformFeedback
.NumVarying
;
716 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
722 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
723 /* Add one for the terminating NUL character.
726 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
735 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
738 *params
= shProg
->TransformFeedback
.BufferMode
;
740 case GL_GEOMETRY_VERTICES_OUT
:
743 if (check_gs_query(ctx
, shProg
)) {
744 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
745 Program
->info
.gs
.vertices_out
;
748 case GL_GEOMETRY_SHADER_INVOCATIONS
:
749 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
751 if (check_gs_query(ctx
, shProg
)) {
752 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
753 Program
->info
.gs
.invocations
;
756 case GL_GEOMETRY_INPUT_TYPE
:
759 if (check_gs_query(ctx
, shProg
)) {
760 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
761 Program
->info
.gs
.input_primitive
;
764 case GL_GEOMETRY_OUTPUT_TYPE
:
767 if (check_gs_query(ctx
, shProg
)) {
768 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
769 Program
->info
.gs
.output_primitive
;
772 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
779 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
780 /* Add one for the terminating NUL character.
782 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
791 case GL_ACTIVE_UNIFORM_BLOCKS
:
795 *params
= shProg
->data
->NumUniformBlocks
;
797 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
798 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
799 * only available with desktop OpenGL 3.0+ with the
800 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
802 * On desktop, we ignore the 3.0+ requirement because it is silly.
804 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
807 *params
= shProg
->BinaryRetreivableHint
;
809 case GL_PROGRAM_BINARY_LENGTH
:
812 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
813 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
816 *params
= shProg
->data
->NumAtomicBuffers
;
818 case GL_COMPUTE_WORK_GROUP_SIZE
: {
820 if (!_mesa_has_compute_shaders(ctx
))
822 if (!shProg
->data
->LinkStatus
) {
823 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
827 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
828 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
832 for (i
= 0; i
< 3; i
++)
833 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
834 Program
->info
.cs
.local_size
[i
];
837 case GL_PROGRAM_SEPARABLE
:
838 /* If the program has not been linked, return initial value 0. */
839 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
842 /* ARB_tessellation_shader */
843 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
846 if (check_tcs_query(ctx
, shProg
)) {
847 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
848 Program
->info
.tess
.tcs_vertices_out
;
851 case GL_TESS_GEN_MODE
:
854 if (check_tes_query(ctx
, shProg
)) {
855 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
856 Program
->info
.tess
.primitive_mode
;
859 case GL_TESS_GEN_SPACING
:
862 if (check_tes_query(ctx
, shProg
)) {
863 const struct gl_linked_shader
*tes
=
864 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
865 switch (tes
->Program
->info
.tess
.spacing
) {
866 case TESS_SPACING_EQUAL
:
869 case TESS_SPACING_FRACTIONAL_ODD
:
870 *params
= GL_FRACTIONAL_ODD
;
872 case TESS_SPACING_FRACTIONAL_EVEN
:
873 *params
= GL_FRACTIONAL_EVEN
;
875 case TESS_SPACING_UNSPECIFIED
:
881 case GL_TESS_GEN_VERTEX_ORDER
:
884 if (check_tes_query(ctx
, shProg
)) {
885 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
886 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
889 case GL_TESS_GEN_POINT_MODE
:
892 if (check_tes_query(ctx
, shProg
)) {
893 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
894 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
901 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
902 _mesa_enum_to_string(pname
));
907 * glGetShaderiv() - get GLSL shader state
910 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
912 struct gl_shader
*shader
=
913 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
921 *params
= shader
->Type
;
923 case GL_DELETE_STATUS
:
924 *params
= shader
->DeletePending
;
926 case GL_COMPILE_STATUS
:
927 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
929 case GL_INFO_LOG_LENGTH
:
930 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
931 strlen(shader
->InfoLog
) + 1 : 0;
933 case GL_SHADER_SOURCE_LENGTH
:
934 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
937 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
944 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
945 GLsizei
*length
, GLchar
*infoLog
)
947 struct gl_shader_program
*shProg
;
949 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
950 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
952 * "If a negative number is provided where an argument of type sizei or
953 * sizeiptr is specified, an INVALID_VALUE error is generated."
956 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
960 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
961 "glGetProgramInfoLog(program)");
966 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
971 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
972 GLsizei
*length
, GLchar
*infoLog
)
974 struct gl_shader
*sh
;
976 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
977 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
979 * "If a negative number is provided where an argument of type sizei or
980 * sizeiptr is specified, an INVALID_VALUE error is generated."
983 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
987 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
992 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
997 * Return shader source code.
1000 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
1001 GLsizei
*length
, GLchar
*sourceOut
)
1003 struct gl_shader
*sh
;
1005 if (maxLength
< 0) {
1006 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
1010 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
1014 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
1019 * Set/replace shader source code. A helper function used by
1020 * glShaderSource[ARB].
1023 shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1027 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
1028 /* If shader was previously compiled back-up the source in case of cache
1031 sh
->FallbackSource
= sh
->Source
;
1032 sh
->Source
= source
;
1034 /* free old shader source string and install new one */
1035 free((void *)sh
->Source
);
1036 sh
->Source
= source
;
1040 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1049 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1055 /* If the user called glCompileShader without first calling
1056 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1058 sh
->CompileStatus
= compile_failure
;
1060 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1061 _mesa_log("GLSL source for %s shader %d:\n",
1062 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1063 _mesa_log("%s\n", sh
->Source
);
1066 /* this call will set the shader->CompileStatus field to indicate if
1067 * compilation was successful.
1069 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1071 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1072 _mesa_write_shader_to_file(sh
);
1075 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1076 if (sh
->CompileStatus
) {
1078 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1079 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1081 _mesa_log("No GLSL IR for shader %d (shader may be from "
1082 "cache)\n", sh
->Name
);
1086 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1088 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1089 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1090 _mesa_log("%s\n", sh
->InfoLog
);
1095 if (!sh
->CompileStatus
) {
1096 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1097 _mesa_log("GLSL source for %s shader %d:\n",
1098 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1099 _mesa_log("%s\n", sh
->Source
);
1100 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1103 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1104 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1105 sh
->Name
, sh
->InfoLog
);
1112 * Link a program's shaders.
1115 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1120 /* From the ARB_transform_feedback2 specification:
1121 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1122 * the name of a program being used by one or more transform feedback
1123 * objects, even if the objects are not currently bound or are paused."
1125 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1126 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1127 "glLinkProgram(transform feedback is using the program)");
1131 unsigned programs_in_use
= 0;
1133 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1134 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1135 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1136 programs_in_use
|= 1 << stage
;
1140 FLUSH_VERTICES(ctx
, 0);
1141 _mesa_glsl_link_shader(ctx
, shProg
);
1143 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1145 * "If LinkProgram or ProgramBinary successfully re-links a program
1146 * object that is active for any shader stage, then the newly generated
1147 * executable code will be installed as part of the current rendering
1148 * state for all shader stages where the program is active.
1149 * Additionally, the newly generated executable code is made part of
1150 * the state of any program pipeline for all stages where the program
1153 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1154 while (programs_in_use
) {
1155 const int stage
= u_bit_scan(&programs_in_use
);
1157 struct gl_program
*prog
= NULL
;
1158 if (shProg
->_LinkedShaders
[stage
])
1159 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1161 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1165 /* Capture .shader_test files. */
1166 const char *capture_path
= _mesa_get_shader_capture_path();
1167 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1169 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1170 capture_path
, shProg
->Name
);
1171 file
= fopen(filename
, "w");
1173 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1174 shProg
->IsES
? " ES" : "",
1175 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1176 if (shProg
->SeparateShader
)
1177 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1178 fprintf(file
, "\n");
1180 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1181 fprintf(file
, "[%s shader]\n%s\n",
1182 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1183 shProg
->Shaders
[i
]->Source
);
1187 _mesa_warning(ctx
, "Failed to open %s", filename
);
1190 ralloc_free(filename
);
1193 if (shProg
->data
->LinkStatus
== linking_failure
&&
1194 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1195 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1196 shProg
->Name
, shProg
->data
->InfoLog
);
1203 printf("Link %u shaders in program %u: %s\n",
1204 shProg
->NumShaders
, shProg
->Name
,
1205 shProg
->data
->LinkStatus
? "Success" : "Failed");
1207 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1208 printf(" shader %u, stage %u\n",
1209 shProg
->Shaders
[i
]->Name
,
1210 shProg
->Shaders
[i
]->Stage
);
1217 * Print basic shader info (for debug).
1220 print_shader_info(const struct gl_shader_program
*shProg
)
1224 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1225 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1227 printf(" %s shader %u, checksum %u\n",
1228 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1229 shProg
->Shaders
[i
]->Name
,
1230 shProg
->Shaders
[i
]->SourceChecksum
);
1232 printf(" %s shader %u\n",
1233 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1234 shProg
->Shaders
[i
]->Name
);
1237 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1238 printf(" vert prog %u\n",
1239 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1240 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1241 printf(" frag prog %u\n",
1242 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1243 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1244 printf(" geom prog %u\n",
1245 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1246 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1247 printf(" tesc prog %u\n",
1248 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1249 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1250 printf(" tese prog %u\n",
1251 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1256 * Use the named shader program for subsequent glUniform calls
1259 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1262 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1263 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1264 "%s(program %u not linked)", caller
, shProg
->Name
);
1268 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1269 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1275 * Use the named shader program for subsequent rendering.
1278 _mesa_use_shader_program(struct gl_context
*ctx
,
1279 struct gl_shader_program
*shProg
)
1281 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1282 struct gl_program
*new_prog
= NULL
;
1283 if (shProg
&& shProg
->_LinkedShaders
[i
])
1284 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1285 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1287 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1292 * Do validation of the given shader program.
1293 * \param errMsg returns error message if validation fails.
1294 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1297 validate_shader_program(const struct gl_shader_program
*shProg
,
1300 if (!shProg
->data
->LinkStatus
) {
1304 /* From the GL spec, a program is invalid if any of these are true:
1306 any two active samplers in the current program object are of
1307 different types, but refer to the same texture image unit,
1309 any active sampler in the current program object refers to a texture
1310 image unit where fixed-function fragment processing accesses a
1311 texture target that does not match the sampler type, or
1313 the sum of the number of active samplers in the program and the
1314 number of texture image units enabled for fixed-function fragment
1315 processing exceeds the combined limit on the total number of texture
1316 image units allowed.
1320 * Check: any two active samplers in the current program object are of
1321 * different types, but refer to the same texture image unit,
1323 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1331 * Called via glValidateProgram()
1334 validate_program(struct gl_context
*ctx
, GLuint program
)
1336 struct gl_shader_program
*shProg
;
1337 char errMsg
[100] = "";
1339 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1344 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1345 if (!shProg
->data
->Validated
) {
1346 /* update info log */
1347 if (shProg
->data
->InfoLog
) {
1348 ralloc_free(shProg
->data
->InfoLog
);
1350 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1356 _mesa_AttachObjectARB_no_error(GLhandleARB program
, GLhandleARB shader
)
1358 GET_CURRENT_CONTEXT(ctx
);
1359 attach_shader_no_error(ctx
, program
, shader
);
1364 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1366 GET_CURRENT_CONTEXT(ctx
);
1367 attach_shader_err(ctx
, program
, shader
, "glAttachObjectARB");
1372 _mesa_AttachShader_no_error(GLuint program
, GLuint shader
)
1374 GET_CURRENT_CONTEXT(ctx
);
1375 attach_shader_no_error(ctx
, program
, shader
);
1380 _mesa_AttachShader(GLuint program
, GLuint shader
)
1382 GET_CURRENT_CONTEXT(ctx
);
1383 attach_shader_err(ctx
, program
, shader
, "glAttachShader");
1388 _mesa_CompileShader(GLuint shaderObj
)
1390 GET_CURRENT_CONTEXT(ctx
);
1391 if (MESA_VERBOSE
& VERBOSE_API
)
1392 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1393 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1394 "glCompileShader"));
1399 _mesa_CreateShader_no_error(GLenum type
)
1401 GET_CURRENT_CONTEXT(ctx
);
1402 return create_shader(ctx
, type
);
1407 _mesa_CreateShader(GLenum type
)
1409 GET_CURRENT_CONTEXT(ctx
);
1411 if (MESA_VERBOSE
& VERBOSE_API
)
1412 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1414 return create_shader_err(ctx
, type
, "glCreateShader");
1418 GLhandleARB GLAPIENTRY
1419 _mesa_CreateShaderObjectARB_no_error(GLenum type
)
1421 GET_CURRENT_CONTEXT(ctx
);
1422 return create_shader(ctx
, type
);
1426 GLhandleARB GLAPIENTRY
1427 _mesa_CreateShaderObjectARB(GLenum type
)
1429 GET_CURRENT_CONTEXT(ctx
);
1430 return create_shader_err(ctx
, type
, "glCreateShaderObjectARB");
1435 _mesa_CreateProgram(void)
1437 GET_CURRENT_CONTEXT(ctx
);
1438 if (MESA_VERBOSE
& VERBOSE_API
)
1439 _mesa_debug(ctx
, "glCreateProgram\n");
1440 return create_shader_program(ctx
);
1444 GLhandleARB GLAPIENTRY
1445 _mesa_CreateProgramObjectARB(void)
1447 GET_CURRENT_CONTEXT(ctx
);
1448 return create_shader_program(ctx
);
1453 _mesa_DeleteObjectARB(GLhandleARB obj
)
1455 if (MESA_VERBOSE
& VERBOSE_API
) {
1456 GET_CURRENT_CONTEXT(ctx
);
1457 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1461 GET_CURRENT_CONTEXT(ctx
);
1462 FLUSH_VERTICES(ctx
, 0);
1463 if (is_program(ctx
, obj
)) {
1464 delete_shader_program(ctx
, obj
);
1466 else if (is_shader(ctx
, obj
)) {
1467 delete_shader(ctx
, obj
);
1477 _mesa_DeleteProgram(GLuint name
)
1480 GET_CURRENT_CONTEXT(ctx
);
1481 FLUSH_VERTICES(ctx
, 0);
1482 delete_shader_program(ctx
, name
);
1488 _mesa_DeleteShader(GLuint name
)
1491 GET_CURRENT_CONTEXT(ctx
);
1492 FLUSH_VERTICES(ctx
, 0);
1493 delete_shader(ctx
, name
);
1499 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1501 GET_CURRENT_CONTEXT(ctx
);
1502 detach_shader(ctx
, program
, shader
);
1507 _mesa_DetachShader(GLuint program
, GLuint shader
)
1509 GET_CURRENT_CONTEXT(ctx
);
1510 detach_shader(ctx
, program
, shader
);
1515 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1516 GLsizei
* count
, GLhandleARB
* obj
)
1518 GET_CURRENT_CONTEXT(ctx
);
1519 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1524 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1525 GLsizei
*count
, GLuint
*obj
)
1527 GET_CURRENT_CONTEXT(ctx
);
1528 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1533 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1534 GLcharARB
* infoLog
)
1536 GET_CURRENT_CONTEXT(ctx
);
1537 if (is_program(ctx
, object
)) {
1538 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1540 else if (is_shader(ctx
, object
)) {
1541 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1544 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1550 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1552 GET_CURRENT_CONTEXT(ctx
);
1553 /* Implement in terms of GetProgramiv, GetShaderiv */
1554 if (is_program(ctx
, object
)) {
1555 if (pname
== GL_OBJECT_TYPE_ARB
) {
1556 *params
= GL_PROGRAM_OBJECT_ARB
;
1559 get_programiv(ctx
, object
, pname
, params
);
1562 else if (is_shader(ctx
, object
)) {
1563 if (pname
== GL_OBJECT_TYPE_ARB
) {
1564 *params
= GL_SHADER_OBJECT_ARB
;
1567 get_shaderiv(ctx
, object
, pname
, params
);
1571 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1577 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1580 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1581 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1582 params
[0] = (GLfloat
) iparams
[0];
1587 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1589 GET_CURRENT_CONTEXT(ctx
);
1590 get_programiv(ctx
, program
, pname
, params
);
1595 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1597 GET_CURRENT_CONTEXT(ctx
);
1598 get_shaderiv(ctx
, shader
, pname
, params
);
1603 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1604 GLsizei
*length
, GLchar
*infoLog
)
1606 GET_CURRENT_CONTEXT(ctx
);
1607 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1612 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1613 GLsizei
*length
, GLchar
*infoLog
)
1615 GET_CURRENT_CONTEXT(ctx
);
1616 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1621 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1622 GLsizei
*length
, GLchar
*sourceOut
)
1624 GET_CURRENT_CONTEXT(ctx
);
1625 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1629 GLhandleARB GLAPIENTRY
1630 _mesa_GetHandleARB(GLenum pname
)
1632 GET_CURRENT_CONTEXT(ctx
);
1633 return get_handle(ctx
, pname
);
1637 GLboolean GLAPIENTRY
1638 _mesa_IsProgram(GLuint name
)
1640 GET_CURRENT_CONTEXT(ctx
);
1641 return is_program(ctx
, name
);
1645 GLboolean GLAPIENTRY
1646 _mesa_IsShader(GLuint name
)
1648 GET_CURRENT_CONTEXT(ctx
);
1649 return is_shader(ctx
, name
);
1654 _mesa_LinkProgram(GLuint programObj
)
1656 GET_CURRENT_CONTEXT(ctx
);
1657 if (MESA_VERBOSE
& VERBOSE_API
)
1658 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1659 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1663 #ifdef ENABLE_SHADER_CACHE
1665 * Generate a SHA-1 hash value string for given source string.
1668 generate_sha1(const char *source
, char sha_str
[64])
1670 unsigned char sha
[20];
1671 _mesa_sha1_compute(source
, strlen(source
), sha
);
1672 _mesa_sha1_format(sha_str
, sha
);
1676 * Construct a full path for shader replacement functionality using
1679 * <path>/<stage prefix>_<CHECKSUM>.glsl
1682 construct_name(const gl_shader_stage stage
, const char *source
,
1686 static const char *types
[] = {
1687 "VS", "TC", "TE", "GS", "FS", "CS",
1690 generate_sha1(source
, sha
);
1691 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1695 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1698 dump_shader(const gl_shader_stage stage
, const char *source
)
1700 static bool path_exists
= true;
1707 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1709 path_exists
= false;
1713 char *name
= construct_name(stage
, source
, dump_path
);
1715 f
= fopen(name
, "w");
1720 GET_CURRENT_CONTEXT(ctx
);
1721 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1728 * Read shader source code from a file.
1729 * Useful for debugging to override an app's shader.
1732 read_shader(const gl_shader_stage stage
, const char *source
)
1735 static bool path_exists
= true;
1736 int len
, shader_size
= 0;
1743 read_path
= getenv("MESA_SHADER_READ_PATH");
1745 path_exists
= false;
1749 char *name
= construct_name(stage
, source
, read_path
);
1750 f
= fopen(name
, "r");
1755 /* allocate enough room for the entire shader */
1756 fseek(f
, 0, SEEK_END
);
1757 shader_size
= ftell(f
);
1759 assert(shader_size
);
1761 /* add one for terminating zero */
1764 buffer
= malloc(shader_size
);
1767 len
= fread(buffer
, 1, shader_size
, f
);
1775 #endif /* ENABLE_SHADER_CACHE */
1778 * Called via glShaderSource() and glShaderSourceARB() API functions.
1779 * Basically, concatenate the source code strings into one long string
1780 * and pass it to _mesa_shader_source().
1783 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1784 const GLchar
* const * string
, const GLint
* length
)
1786 GET_CURRENT_CONTEXT(ctx
);
1788 GLsizei i
, totalLength
;
1790 struct gl_shader
*sh
;
1792 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1796 if (string
== NULL
) {
1797 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1802 * This array holds offsets of where the appropriate string ends, thus the
1803 * last element will be set to the total length of the source code.
1805 offsets
= malloc(count
* sizeof(GLint
));
1806 if (offsets
== NULL
) {
1807 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1811 for (i
= 0; i
< count
; i
++) {
1812 if (string
[i
] == NULL
) {
1813 free((GLvoid
*) offsets
);
1814 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1815 "glShaderSourceARB(null string)");
1818 if (length
== NULL
|| length
[i
] < 0)
1819 offsets
[i
] = strlen(string
[i
]);
1821 offsets
[i
] = length
[i
];
1822 /* accumulate string lengths */
1824 offsets
[i
] += offsets
[i
- 1];
1827 /* Total length of source string is sum off all strings plus two.
1828 * One extra byte for terminating zero, another extra byte to silence
1829 * valgrind warnings in the parser/grammer code.
1831 totalLength
= offsets
[count
- 1] + 2;
1832 source
= malloc(totalLength
* sizeof(GLcharARB
));
1833 if (source
== NULL
) {
1834 free((GLvoid
*) offsets
);
1835 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1839 for (i
= 0; i
< count
; i
++) {
1840 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1841 memcpy(source
+ start
, string
[i
],
1842 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1844 source
[totalLength
- 1] = '\0';
1845 source
[totalLength
- 2] = '\0';
1847 #ifdef ENABLE_SHADER_CACHE
1848 GLcharARB
*replacement
;
1850 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1851 * if corresponding entry found from MESA_SHADER_READ_PATH.
1853 dump_shader(sh
->Stage
, source
);
1855 replacement
= read_shader(sh
->Stage
, source
);
1858 source
= replacement
;
1860 #endif /* ENABLE_SHADER_CACHE */
1862 shader_source(sh
, source
);
1868 static ALWAYS_INLINE
void
1869 use_program(GLuint program
, bool no_error
)
1871 GET_CURRENT_CONTEXT(ctx
);
1872 struct gl_shader_program
*shProg
= NULL
;
1874 if (MESA_VERBOSE
& VERBOSE_API
)
1875 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1879 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1882 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1884 "glUseProgram(transform feedback active)");
1890 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1894 if (!shProg
->data
->LinkStatus
) {
1895 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1896 "glUseProgram(program %u not linked)", program
);
1901 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1902 print_shader_info(shProg
);
1907 /* The ARB_separate_shader_object spec says:
1909 * "The executable code for an individual shader stage is taken from
1910 * the current program for that stage. If there is a current program
1911 * object established by UseProgram, that program is considered current
1912 * for all stages. Otherwise, if there is a bound program pipeline
1913 * object (section 2.14.PPO), the program bound to the appropriate
1914 * stage of the pipeline object is considered current."
1917 /* Attach shader state to the binding point */
1918 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1919 /* Update the program */
1920 _mesa_use_shader_program(ctx
, shProg
);
1922 /* Must be done first: detach the progam */
1923 _mesa_use_shader_program(ctx
, shProg
);
1924 /* Unattach shader_state binding point */
1925 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1926 ctx
->Pipeline
.Default
);
1927 /* If a pipeline was bound, rebind it */
1928 if (ctx
->Pipeline
.Current
) {
1930 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
1932 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1939 _mesa_UseProgram_no_error(GLuint program
)
1941 use_program(program
, true);
1946 _mesa_UseProgram(GLuint program
)
1948 use_program(program
, false);
1953 _mesa_ValidateProgram(GLuint program
)
1955 GET_CURRENT_CONTEXT(ctx
);
1956 validate_program(ctx
, program
);
1961 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1964 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1965 GLint
* range
, GLint
* precision
)
1967 const struct gl_program_constants
*limits
;
1968 const struct gl_precision
*p
;
1969 GET_CURRENT_CONTEXT(ctx
);
1971 switch (shadertype
) {
1972 case GL_VERTEX_SHADER
:
1973 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1975 case GL_FRAGMENT_SHADER
:
1976 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1979 _mesa_error(ctx
, GL_INVALID_ENUM
,
1980 "glGetShaderPrecisionFormat(shadertype)");
1984 switch (precisiontype
) {
1986 p
= &limits
->LowFloat
;
1988 case GL_MEDIUM_FLOAT
:
1989 p
= &limits
->MediumFloat
;
1992 p
= &limits
->HighFloat
;
1995 p
= &limits
->LowInt
;
1998 p
= &limits
->MediumInt
;
2001 p
= &limits
->HighInt
;
2004 _mesa_error(ctx
, GL_INVALID_ENUM
,
2005 "glGetShaderPrecisionFormat(precisiontype)");
2009 range
[0] = p
->RangeMin
;
2010 range
[1] = p
->RangeMax
;
2011 precision
[0] = p
->Precision
;
2016 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2019 _mesa_ReleaseShaderCompiler(void)
2021 _mesa_destroy_shader_compiler_caches();
2026 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
2029 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
2030 const void* binary
, GLint length
)
2032 GET_CURRENT_CONTEXT(ctx
);
2034 (void) binaryformat
;
2037 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
2038 * page 88 of the OpenGL 4.5 specs state:
2040 * "An INVALID_VALUE error is generated if count or length is negative.
2041 * An INVALID_ENUM error is generated if binaryformat is not a supported
2042 * format returned in SHADER_BINARY_FORMATS."
2044 if (n
< 0 || length
< 0) {
2045 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
2049 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
2054 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
2055 GLenum
*binaryFormat
, GLvoid
*binary
)
2057 struct gl_shader_program
*shProg
;
2058 GLsizei length_dummy
;
2059 GET_CURRENT_CONTEXT(ctx
);
2062 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2066 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2070 /* The ARB_get_program_binary spec says:
2072 * "If <length> is NULL, then no length is returned."
2074 * Ensure that length always points to valid storage to avoid multiple NULL
2075 * pointer checks below.
2078 length
= &length_dummy
;
2081 /* The ARB_get_program_binary spec says:
2083 * "When a program object's LINK_STATUS is FALSE, its program binary
2084 * length is zero, and a call to GetProgramBinary will generate an
2085 * INVALID_OPERATION error.
2087 if (!shProg
->data
->LinkStatus
) {
2088 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2089 "glGetProgramBinary(program %u not linked)",
2096 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2097 "glGetProgramBinary(driver supports zero binary formats)");
2099 (void) binaryFormat
;
2104 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2105 const GLvoid
*binary
, GLsizei length
)
2107 struct gl_shader_program
*shProg
;
2108 GET_CURRENT_CONTEXT(ctx
);
2110 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2114 (void) binaryFormat
;
2117 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2119 * "If a negative number is provided where an argument of type sizei or
2120 * sizeiptr is specified, an INVALID_VALUE error is generated."
2123 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2127 /* The ARB_get_program_binary spec says:
2129 * "<binaryFormat> and <binary> must be those returned by a previous
2130 * call to GetProgramBinary, and <length> must be the length of the
2131 * program binary as returned by GetProgramBinary or GetProgramiv with
2132 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2133 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2136 * Since any value of binaryFormat passed "is not one of those specified as
2137 * allowable for [this] command, an INVALID_ENUM error is generated."
2139 shProg
->data
->LinkStatus
= linking_failure
;
2140 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2144 static ALWAYS_INLINE
void
2145 program_parameteri(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
2146 GLuint pname
, GLint value
, bool no_error
)
2149 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2150 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2151 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2152 * even be in the dispatch table, so we shouldn't need to expclicitly
2155 * On desktop, we ignore the 3.0+ requirement because it is silly.
2158 /* The ARB_get_program_binary extension spec says:
2160 * "An INVALID_VALUE error is generated if the <value> argument to
2161 * ProgramParameteri is not TRUE or FALSE."
2163 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2167 /* No need to notify the driver. Any changes will actually take effect
2168 * the next time the shader is linked.
2170 * The ARB_get_program_binary extension spec says:
2172 * "To indicate that a program binary is likely to be retrieved,
2173 * ProgramParameteri should be called with <pname>
2174 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2175 * will not be in effect until the next time LinkProgram or
2176 * ProgramBinary has been called successfully."
2178 * The resloution of issue 9 in the extension spec also says:
2180 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2181 * to indicate to the GL implementation that this program will
2182 * likely be saved with GetProgramBinary at some point. This will
2183 * give the GL implementation the opportunity to track any state
2184 * changes made to the program before being saved such that when it
2185 * is loaded again a recompile can be avoided."
2187 shProg
->BinaryRetreivableHint
= value
;
2190 case GL_PROGRAM_SEPARABLE
:
2191 /* Spec imply that the behavior is the same as ARB_get_program_binary
2192 * Chapter 7.3 Program Objects
2194 if (!no_error
&& value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2197 shProg
->SeparateShader
= value
;
2202 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2203 _mesa_enum_to_string(pname
));
2209 _mesa_error(ctx
, GL_INVALID_VALUE
,
2210 "glProgramParameteri(pname=%s, value=%d): "
2211 "value must be 0 or 1.",
2212 _mesa_enum_to_string(pname
),
2218 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2220 struct gl_shader_program
*shProg
;
2221 GET_CURRENT_CONTEXT(ctx
);
2223 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2224 "glProgramParameteri");
2228 program_parameteri(ctx
, shProg
, pname
, value
, false);
2233 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2234 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2235 struct gl_pipeline_object
*shTarget
)
2237 struct gl_program
**target
;
2239 target
= &shTarget
->CurrentProgram
[stage
];
2241 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2244 if (*target
!= prog
) {
2245 /* Program is current, flush it */
2246 if (shTarget
== ctx
->_Shader
) {
2247 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2250 _mesa_reference_shader_program(ctx
,
2251 &shTarget
->ReferencedPrograms
[stage
],
2253 _mesa_reference_program(ctx
, target
, prog
);
2261 * Copy program-specific data generated by linking from the gl_shader_program
2262 * object to the gl_program object referred to by the gl_linked_shader.
2264 * This function expects _mesa_reference_program() to have been previously
2265 * called setting the gl_linked_shaders program reference.
2268 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2269 struct gl_linked_shader
*dst_sh
)
2271 assert(dst_sh
->Program
);
2273 struct gl_program
*dst
= dst_sh
->Program
;
2275 dst
->info
.separate_shader
= src
->SeparateShader
;
2277 switch (dst_sh
->Stage
) {
2278 case MESA_SHADER_GEOMETRY
: {
2279 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2280 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2281 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2284 case MESA_SHADER_FRAGMENT
: {
2285 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2288 case MESA_SHADER_COMPUTE
: {
2289 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2298 * ARB_separate_shader_objects: Compile & Link Program
2301 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2302 const GLchar
* const *strings
)
2304 GET_CURRENT_CONTEXT(ctx
);
2306 const GLuint shader
= create_shader_err(ctx
, type
, "glCreateShaderProgramv");
2310 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2311 * GL_INVALID_VALUE should be generated if count < 0
2314 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2319 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2321 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2322 _mesa_compile_shader(ctx
, sh
);
2324 program
= create_shader_program(ctx
);
2326 struct gl_shader_program
*shProg
;
2327 GLint compiled
= GL_FALSE
;
2329 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2331 shProg
->SeparateShader
= GL_TRUE
;
2333 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2335 attach_shader_err(ctx
, program
, shader
, "glCreateShaderProgramv");
2336 _mesa_link_program(ctx
, shProg
);
2337 detach_shader(ctx
, program
, shader
);
2341 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2342 append
-error
-to
-info
-log
;
2343 shProg
->data
->LinkStatus
= linking_failure
;
2348 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2351 delete_shader(ctx
, shader
);
2359 * For GL_ARB_tessellation_shader
2362 _mesa_PatchParameteri_no_error(GLenum pname
, GLint value
)
2364 GET_CURRENT_CONTEXT(ctx
);
2365 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2369 extern void GLAPIENTRY
2370 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2372 GET_CURRENT_CONTEXT(ctx
);
2374 if (!_mesa_has_tessellation(ctx
)) {
2375 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2379 if (pname
!= GL_PATCH_VERTICES
) {
2380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2384 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2385 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2389 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2393 extern void GLAPIENTRY
2394 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2396 GET_CURRENT_CONTEXT(ctx
);
2398 if (!_mesa_has_tessellation(ctx
)) {
2399 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2404 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2405 FLUSH_VERTICES(ctx
, 0);
2406 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2407 4 * sizeof(GLfloat
));
2408 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2410 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2411 FLUSH_VERTICES(ctx
, 0);
2412 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2413 2 * sizeof(GLfloat
));
2414 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2417 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2423 * ARB_shader_subroutine
2426 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2429 GET_CURRENT_CONTEXT(ctx
);
2430 const char *api_name
= "glGetSubroutineUniformLocation";
2431 struct gl_shader_program
*shProg
;
2432 GLenum resource_type
;
2433 gl_shader_stage stage
;
2435 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2436 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2440 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2444 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2445 if (!shProg
->_LinkedShaders
[stage
]) {
2446 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2450 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2451 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2455 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2458 GET_CURRENT_CONTEXT(ctx
);
2459 const char *api_name
= "glGetSubroutineIndex";
2460 struct gl_shader_program
*shProg
;
2461 struct gl_program_resource
*res
;
2462 GLenum resource_type
;
2463 gl_shader_stage stage
;
2465 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2466 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2470 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2474 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2475 if (!shProg
->_LinkedShaders
[stage
]) {
2476 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2480 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2481 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2486 return _mesa_program_resource_index(shProg
, res
);
2491 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2492 GLuint index
, GLenum pname
, GLint
*values
)
2494 GET_CURRENT_CONTEXT(ctx
);
2495 const char *api_name
= "glGetActiveSubroutineUniformiv";
2496 struct gl_shader_program
*shProg
;
2497 struct gl_linked_shader
*sh
;
2498 gl_shader_stage stage
;
2499 struct gl_program_resource
*res
;
2500 const struct gl_uniform_storage
*uni
;
2501 GLenum resource_type
;
2504 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2505 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2509 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2513 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2514 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2516 sh
= shProg
->_LinkedShaders
[stage
];
2518 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2522 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2523 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2524 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2529 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2530 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2533 values
[0] = uni
->num_compatible_subroutines
;
2537 case GL_COMPATIBLE_SUBROUTINES
: {
2538 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2542 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2543 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2544 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2545 if (fn
->types
[j
] == uni
->type
) {
2546 values
[count
++] = i
;
2554 case GL_UNIFORM_SIZE
:
2555 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2558 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2561 case GL_UNIFORM_NAME_LENGTH
:
2562 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2564 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2565 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2569 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2576 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2577 GLuint index
, GLsizei bufsize
,
2578 GLsizei
*length
, GLchar
*name
)
2580 GET_CURRENT_CONTEXT(ctx
);
2581 const char *api_name
= "glGetActiveSubroutineUniformName";
2582 struct gl_shader_program
*shProg
;
2583 GLenum resource_type
;
2584 gl_shader_stage stage
;
2586 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2587 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2591 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2595 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2596 if (!shProg
->_LinkedShaders
[stage
]) {
2597 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2601 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2602 /* get program resource name */
2603 _mesa_get_program_resource_name(shProg
, resource_type
,
2605 length
, name
, api_name
);
2610 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2611 GLuint index
, GLsizei bufsize
,
2612 GLsizei
*length
, GLchar
*name
)
2614 GET_CURRENT_CONTEXT(ctx
);
2615 const char *api_name
= "glGetActiveSubroutineName";
2616 struct gl_shader_program
*shProg
;
2617 GLenum resource_type
;
2618 gl_shader_stage stage
;
2620 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2621 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2625 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2629 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2630 if (!shProg
->_LinkedShaders
[stage
]) {
2631 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2634 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2635 _mesa_get_program_resource_name(shProg
, resource_type
,
2637 length
, name
, api_name
);
2641 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2642 const GLuint
*indices
)
2644 GET_CURRENT_CONTEXT(ctx
);
2645 const char *api_name
= "glUniformSubroutinesuiv";
2646 gl_shader_stage stage
;
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 (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2662 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2667 bool flushed
= false;
2669 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2676 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2680 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2683 for (j
= i
; j
< i
+ uni_count
; j
++) {
2684 struct gl_subroutine_function
*subfn
= NULL
;
2685 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2686 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2690 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2691 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2692 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2699 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2700 if (subfn
->types
[k
] == uni
->type
)
2703 if (k
== subfn
->num_compat_types
) {
2704 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2708 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2716 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2719 GET_CURRENT_CONTEXT(ctx
);
2720 const char *api_name
= "glGetUniformSubroutineuiv";
2721 gl_shader_stage stage
;
2723 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2724 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2728 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2729 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2731 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2735 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2736 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2740 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2745 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2746 GLenum pname
, GLint
*values
)
2748 GET_CURRENT_CONTEXT(ctx
);
2749 const char *api_name
= "glGetProgramStageiv";
2750 struct gl_shader_program
*shProg
;
2751 struct gl_linked_shader
*sh
;
2752 gl_shader_stage stage
;
2754 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2755 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2759 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2763 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2764 sh
= shProg
->_LinkedShaders
[stage
];
2766 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2767 * INVALID_OPERATION in the case of not be linked.
2769 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2770 * same info using other specs (ARB_program_interface_query), without the
2771 * need of the program to be linked, being the value for that case 0.
2773 * But at the same time, some other methods require the program to be
2774 * linked for pname related to locations, so it would be inconsistent to
2775 * not do the same here. So we are:
2776 * * Return GL_INVALID_OPERATION if not linked only for locations.
2777 * * Setting a default value of 0, to be returned if not linked.
2781 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2782 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2787 struct gl_program
*p
= sh
->Program
;
2789 case GL_ACTIVE_SUBROUTINES
:
2790 values
[0] = p
->sh
.NumSubroutineFunctions
;
2792 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2793 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2795 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2796 values
[0] = p
->sh
.NumSubroutineUniforms
;
2798 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2802 GLenum resource_type
;
2803 struct gl_program_resource
*res
;
2805 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2806 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2807 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2809 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2814 values
[0] = max_len
;
2817 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2821 GLenum resource_type
;
2822 struct gl_program_resource
*res
;
2824 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2825 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2826 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2828 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2829 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2835 values
[0] = max_len
;
2839 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2846 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2850 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2851 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2852 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2853 if (fn
->types
[j
] == type
)
2861 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2862 struct gl_program
*p
)
2866 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2871 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2880 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2881 for (j
= 0; j
< uni_count
; j
++) {
2882 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2883 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2886 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2888 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2892 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2893 gl_shader_stage stage
)
2895 if (ctx
->_Shader
->CurrentProgram
[stage
])
2896 _mesa_shader_write_subroutine_index(ctx
,
2897 ctx
->_Shader
->CurrentProgram
[stage
]);
2901 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2902 struct gl_program
*p
)
2906 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2907 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2908 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2909 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2910 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2913 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2914 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2919 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);