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
, GLuint program
, GLuint shader
,
250 struct gl_shader_program
*shProg
;
251 struct gl_shader
*sh
;
254 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
256 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, caller
);
260 sh
= _mesa_lookup_shader_err(ctx
, shader
, caller
);
265 n
= shProg
->NumShaders
;
266 for (i
= 0; i
< n
; i
++) {
267 if (shProg
->Shaders
[i
] == sh
) {
268 /* The shader is already attched to this program. The
269 * GL_ARB_shader_objects spec says:
271 * "The error INVALID_OPERATION is generated by AttachObjectARB
272 * if <obj> is already attached to <containerObj>."
274 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
276 } else if (same_type_disallowed
&&
277 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
278 /* Shader with the same type is already attached to this program,
279 * OpenGL ES 2.0 and 3.0 specs say:
281 * "Multiple shader objects of the same type may not be attached
282 * to a single program object. [...] The error INVALID_OPERATION
283 * is generated if [...] another shader object of the same type
284 * as shader is already attached to program."
286 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
292 shProg
->Shaders
= realloc(shProg
->Shaders
,
293 (n
+ 1) * sizeof(struct gl_shader
*));
294 if (!shProg
->Shaders
) {
295 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, caller
);
300 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
301 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
302 shProg
->NumShaders
++;
307 create_shader(struct gl_context
*ctx
, GLenum type
)
309 struct gl_shader
*sh
;
312 if (!_mesa_validate_shader_target(ctx
, type
)) {
313 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(%s)",
314 _mesa_enum_to_string(type
));
318 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
319 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
320 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
322 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
323 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
330 create_shader_program(struct gl_context
*ctx
)
333 struct gl_shader_program
*shProg
;
335 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
337 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
339 shProg
= _mesa_new_shader_program(name
);
341 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
343 assert(shProg
->RefCount
== 1);
345 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
352 * Delete a shader program. Actually, just decrement the program's
353 * reference count and mark it as DeletePending.
354 * Used to implement glDeleteProgram() and glDeleteObjectARB().
357 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
360 * NOTE: deleting shaders/programs works a bit differently than
361 * texture objects (and buffer objects, etc). Shader/program
362 * handles/IDs exist in the hash table until the object is really
363 * deleted (refcount==0). With texture objects, the handle/ID is
364 * removed from the hash table in glDeleteTextures() while the tex
365 * object itself might linger until its refcount goes to zero.
367 struct gl_shader_program
*shProg
;
369 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
373 if (!shProg
->DeletePending
) {
374 shProg
->DeletePending
= GL_TRUE
;
376 /* effectively, decr shProg's refcount */
377 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
383 delete_shader(struct gl_context
*ctx
, GLuint shader
)
385 struct gl_shader
*sh
;
387 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
391 if (!sh
->DeletePending
) {
392 sh
->DeletePending
= GL_TRUE
;
394 /* effectively, decr sh's refcount */
395 _mesa_reference_shader(ctx
, &sh
, NULL
);
401 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
403 struct gl_shader_program
*shProg
;
407 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
411 n
= shProg
->NumShaders
;
413 for (i
= 0; i
< n
; i
++) {
414 if (shProg
->Shaders
[i
]->Name
== shader
) {
416 struct gl_shader
**newList
;
419 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
421 /* alloc new, smaller array */
422 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
424 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
427 /* Copy old list entries to new list, skipping removed entry at [i] */
428 for (j
= 0; j
< i
; j
++) {
429 newList
[j
] = shProg
->Shaders
[j
];
432 newList
[j
++] = shProg
->Shaders
[i
];
435 /* Free old list and install new one */
436 free(shProg
->Shaders
);
437 shProg
->Shaders
= newList
;
438 shProg
->NumShaders
= n
- 1;
441 /* sanity check - make sure the new list's entries are sensible */
442 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
443 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
444 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
445 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
446 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
447 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
448 assert(shProg
->Shaders
[j
]->RefCount
> 0);
459 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
460 err
= GL_INVALID_OPERATION
;
462 err
= GL_INVALID_VALUE
;
463 _mesa_error(ctx
, err
, "glDetachShader(shader)");
470 * Return list of shaders attached to shader program.
473 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
474 GLsizei
*count
, GLuint
*obj
)
476 struct gl_shader_program
*shProg
;
479 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
484 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
488 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
489 obj
[i
] = shProg
->Shaders
[i
]->Name
;
498 * glGetHandleARB() - return ID/name of currently bound shader program.
501 get_handle(struct gl_context
*ctx
, GLenum pname
)
503 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
504 if (ctx
->_Shader
->ActiveProgram
)
505 return ctx
->_Shader
->ActiveProgram
->Name
;
510 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
517 * Check if a geometry shader query is valid at this time. If not, report an
518 * error and return false.
520 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
522 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
523 * are queried for a program which has not been linked successfully, or
524 * which does not contain objects to form a geometry shader, then an
525 * INVALID_OPERATION error is generated."
528 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
530 if (shProg
->data
->LinkStatus
&&
531 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
535 _mesa_error(ctx
, GL_INVALID_OPERATION
,
536 "glGetProgramv(linked geometry shader required)");
542 * Check if a tessellation control shader query is valid at this time.
543 * If not, report an error and return false.
545 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
547 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
548 * not been linked successfully, or which does not contain objects to
549 * form a tessellation control shader, then an INVALID_OPERATION error is
553 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
555 if (shProg
->data
->LinkStatus
&&
556 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
560 _mesa_error(ctx
, GL_INVALID_OPERATION
,
561 "glGetProgramv(linked tessellation control shader required)");
567 * Check if a tessellation evaluation shader query is valid at this time.
568 * If not, report an error and return false.
570 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
572 * "If any of the pname values in this paragraph are queried for a program
573 * which has not been linked successfully, or which does not contain
574 * objects to form a tessellation evaluation shader, then an
575 * INVALID_OPERATION error is generated."
579 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
581 if (shProg
->data
->LinkStatus
&&
582 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
586 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
587 "evaluation shader required)");
593 * glGetProgramiv() - get shader program state.
594 * Note that this is for GLSL shader programs, not ARB vertex/fragment
595 * programs (see glGetProgramivARB).
598 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
601 struct gl_shader_program
*shProg
602 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
604 /* Is transform feedback available in this context?
607 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
608 || ctx
->API
== API_OPENGL_CORE
609 || _mesa_is_gles3(ctx
);
611 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
612 * and GL 3.2) are available in this context
614 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
615 const bool has_tess
= _mesa_has_tessellation(ctx
);
617 /* Are uniform buffer objects available in this context?
620 (ctx
->API
== API_OPENGL_COMPAT
&&
621 ctx
->Extensions
.ARB_uniform_buffer_object
)
622 || ctx
->API
== API_OPENGL_CORE
623 || _mesa_is_gles3(ctx
);
630 case GL_DELETE_STATUS
:
631 *params
= shProg
->DeletePending
;
634 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
636 case GL_VALIDATE_STATUS
:
637 *params
= shProg
->data
->Validated
;
639 case GL_INFO_LOG_LENGTH
:
640 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
641 strlen(shProg
->data
->InfoLog
) + 1 : 0;
643 case GL_ATTACHED_SHADERS
:
644 *params
= shProg
->NumShaders
;
646 case GL_ACTIVE_ATTRIBUTES
:
647 *params
= _mesa_count_active_attribs(shProg
);
649 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
650 *params
= _mesa_longest_attribute_name_length(shProg
);
652 case GL_ACTIVE_UNIFORMS
: {
654 const unsigned num_uniforms
=
655 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
656 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
657 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
662 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
665 const unsigned num_uniforms
=
666 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
668 for (i
= 0; i
< num_uniforms
; i
++) {
669 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
672 /* Add one for the terminating NUL character for a non-array, and
673 * 4 for the "[0]" and the NUL for an array.
675 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
676 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
685 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
688 *params
= shProg
->TransformFeedback
.NumVarying
;
690 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
696 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
697 /* Add one for the terminating NUL character.
700 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
709 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
712 *params
= shProg
->TransformFeedback
.BufferMode
;
714 case GL_GEOMETRY_VERTICES_OUT
:
717 if (check_gs_query(ctx
, shProg
)) {
718 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
719 Program
->info
.gs
.vertices_out
;
722 case GL_GEOMETRY_SHADER_INVOCATIONS
:
723 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
725 if (check_gs_query(ctx
, shProg
)) {
726 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
727 Program
->info
.gs
.invocations
;
730 case GL_GEOMETRY_INPUT_TYPE
:
733 if (check_gs_query(ctx
, shProg
)) {
734 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
735 Program
->info
.gs
.input_primitive
;
738 case GL_GEOMETRY_OUTPUT_TYPE
:
741 if (check_gs_query(ctx
, shProg
)) {
742 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
743 Program
->info
.gs
.output_primitive
;
746 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
753 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
754 /* Add one for the terminating NUL character.
756 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
765 case GL_ACTIVE_UNIFORM_BLOCKS
:
769 *params
= shProg
->data
->NumUniformBlocks
;
771 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
772 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
773 * only available with desktop OpenGL 3.0+ with the
774 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
776 * On desktop, we ignore the 3.0+ requirement because it is silly.
778 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
781 *params
= shProg
->BinaryRetreivableHint
;
783 case GL_PROGRAM_BINARY_LENGTH
:
786 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
787 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
790 *params
= shProg
->data
->NumAtomicBuffers
;
792 case GL_COMPUTE_WORK_GROUP_SIZE
: {
794 if (!_mesa_has_compute_shaders(ctx
))
796 if (!shProg
->data
->LinkStatus
) {
797 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
801 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
802 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
806 for (i
= 0; i
< 3; i
++)
807 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
808 Program
->info
.cs
.local_size
[i
];
811 case GL_PROGRAM_SEPARABLE
:
812 /* If the program has not been linked, return initial value 0. */
813 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
816 /* ARB_tessellation_shader */
817 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
820 if (check_tcs_query(ctx
, shProg
)) {
821 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
822 Program
->info
.tess
.tcs_vertices_out
;
825 case GL_TESS_GEN_MODE
:
828 if (check_tes_query(ctx
, shProg
)) {
829 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
830 Program
->info
.tess
.primitive_mode
;
833 case GL_TESS_GEN_SPACING
:
836 if (check_tes_query(ctx
, shProg
)) {
837 const struct gl_linked_shader
*tes
=
838 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
839 switch (tes
->Program
->info
.tess
.spacing
) {
840 case TESS_SPACING_EQUAL
:
843 case TESS_SPACING_FRACTIONAL_ODD
:
844 *params
= GL_FRACTIONAL_ODD
;
846 case TESS_SPACING_FRACTIONAL_EVEN
:
847 *params
= GL_FRACTIONAL_EVEN
;
849 case TESS_SPACING_UNSPECIFIED
:
855 case GL_TESS_GEN_VERTEX_ORDER
:
858 if (check_tes_query(ctx
, shProg
)) {
859 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
860 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
863 case GL_TESS_GEN_POINT_MODE
:
866 if (check_tes_query(ctx
, shProg
)) {
867 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
868 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
875 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
876 _mesa_enum_to_string(pname
));
881 * glGetShaderiv() - get GLSL shader state
884 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
886 struct gl_shader
*shader
=
887 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
895 *params
= shader
->Type
;
897 case GL_DELETE_STATUS
:
898 *params
= shader
->DeletePending
;
900 case GL_COMPILE_STATUS
:
901 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
903 case GL_INFO_LOG_LENGTH
:
904 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
905 strlen(shader
->InfoLog
) + 1 : 0;
907 case GL_SHADER_SOURCE_LENGTH
:
908 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
911 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
918 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
919 GLsizei
*length
, GLchar
*infoLog
)
921 struct gl_shader_program
*shProg
;
923 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
924 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
926 * "If a negative number is provided where an argument of type sizei or
927 * sizeiptr is specified, an INVALID_VALUE error is generated."
930 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
934 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
935 "glGetProgramInfoLog(program)");
940 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
945 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
946 GLsizei
*length
, GLchar
*infoLog
)
948 struct gl_shader
*sh
;
950 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
951 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
953 * "If a negative number is provided where an argument of type sizei or
954 * sizeiptr is specified, an INVALID_VALUE error is generated."
957 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
961 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
966 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
971 * Return shader source code.
974 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
975 GLsizei
*length
, GLchar
*sourceOut
)
977 struct gl_shader
*sh
;
980 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
984 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
988 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
993 * Set/replace shader source code. A helper function used by
994 * glShaderSource[ARB].
997 shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1001 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
1002 /* If shader was previously compiled back-up the source in case of cache
1005 sh
->FallbackSource
= sh
->Source
;
1006 sh
->Source
= source
;
1008 /* free old shader source string and install new one */
1009 free((void *)sh
->Source
);
1010 sh
->Source
= source
;
1014 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1023 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1029 /* If the user called glCompileShader without first calling
1030 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1032 sh
->CompileStatus
= compile_failure
;
1034 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1035 _mesa_log("GLSL source for %s shader %d:\n",
1036 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1037 _mesa_log("%s\n", sh
->Source
);
1040 /* this call will set the shader->CompileStatus field to indicate if
1041 * compilation was successful.
1043 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1045 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1046 _mesa_write_shader_to_file(sh
);
1049 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1050 if (sh
->CompileStatus
) {
1052 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1053 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1055 _mesa_log("No GLSL IR for shader %d (shader may be from "
1056 "cache)\n", sh
->Name
);
1060 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1062 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1063 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1064 _mesa_log("%s\n", sh
->InfoLog
);
1069 if (!sh
->CompileStatus
) {
1070 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1071 _mesa_log("GLSL source for %s shader %d:\n",
1072 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1073 _mesa_log("%s\n", sh
->Source
);
1074 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1077 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1078 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1079 sh
->Name
, sh
->InfoLog
);
1086 * Link a program's shaders.
1089 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1094 /* From the ARB_transform_feedback2 specification:
1095 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1096 * the name of a program being used by one or more transform feedback
1097 * objects, even if the objects are not currently bound or are paused."
1099 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1100 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1101 "glLinkProgram(transform feedback is using the program)");
1105 unsigned programs_in_use
= 0;
1107 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1108 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1109 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1110 programs_in_use
|= 1 << stage
;
1114 FLUSH_VERTICES(ctx
, 0);
1115 _mesa_glsl_link_shader(ctx
, shProg
);
1117 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1119 * "If LinkProgram or ProgramBinary successfully re-links a program
1120 * object that is active for any shader stage, then the newly generated
1121 * executable code will be installed as part of the current rendering
1122 * state for all shader stages where the program is active.
1123 * Additionally, the newly generated executable code is made part of
1124 * the state of any program pipeline for all stages where the program
1127 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1128 while (programs_in_use
) {
1129 const int stage
= u_bit_scan(&programs_in_use
);
1131 struct gl_program
*prog
= NULL
;
1132 if (shProg
->_LinkedShaders
[stage
])
1133 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1135 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1139 /* Capture .shader_test files. */
1140 const char *capture_path
= _mesa_get_shader_capture_path();
1141 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1143 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1144 capture_path
, shProg
->Name
);
1145 file
= fopen(filename
, "w");
1147 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1148 shProg
->IsES
? " ES" : "",
1149 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1150 if (shProg
->SeparateShader
)
1151 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1152 fprintf(file
, "\n");
1154 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1155 fprintf(file
, "[%s shader]\n%s\n",
1156 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1157 shProg
->Shaders
[i
]->Source
);
1161 _mesa_warning(ctx
, "Failed to open %s", filename
);
1164 ralloc_free(filename
);
1167 if (shProg
->data
->LinkStatus
== linking_failure
&&
1168 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1169 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1170 shProg
->Name
, shProg
->data
->InfoLog
);
1177 printf("Link %u shaders in program %u: %s\n",
1178 shProg
->NumShaders
, shProg
->Name
,
1179 shProg
->data
->LinkStatus
? "Success" : "Failed");
1181 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1182 printf(" shader %u, stage %u\n",
1183 shProg
->Shaders
[i
]->Name
,
1184 shProg
->Shaders
[i
]->Stage
);
1191 * Print basic shader info (for debug).
1194 print_shader_info(const struct gl_shader_program
*shProg
)
1198 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1199 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1201 printf(" %s shader %u, checksum %u\n",
1202 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1203 shProg
->Shaders
[i
]->Name
,
1204 shProg
->Shaders
[i
]->SourceChecksum
);
1206 printf(" %s shader %u\n",
1207 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1208 shProg
->Shaders
[i
]->Name
);
1211 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1212 printf(" vert prog %u\n",
1213 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1214 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1215 printf(" frag prog %u\n",
1216 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1217 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1218 printf(" geom prog %u\n",
1219 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1220 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1221 printf(" tesc prog %u\n",
1222 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1223 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1224 printf(" tese prog %u\n",
1225 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1230 * Use the named shader program for subsequent glUniform calls
1233 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1236 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1238 "%s(program %u not linked)", caller
, shProg
->Name
);
1242 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1243 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1249 * Use the named shader program for subsequent rendering.
1252 _mesa_use_shader_program(struct gl_context
*ctx
,
1253 struct gl_shader_program
*shProg
)
1255 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1256 struct gl_program
*new_prog
= NULL
;
1257 if (shProg
&& shProg
->_LinkedShaders
[i
])
1258 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1259 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1261 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1266 * Do validation of the given shader program.
1267 * \param errMsg returns error message if validation fails.
1268 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1271 validate_shader_program(const struct gl_shader_program
*shProg
,
1274 if (!shProg
->data
->LinkStatus
) {
1278 /* From the GL spec, a program is invalid if any of these are true:
1280 any two active samplers in the current program object are of
1281 different types, but refer to the same texture image unit,
1283 any active sampler in the current program object refers to a texture
1284 image unit where fixed-function fragment processing accesses a
1285 texture target that does not match the sampler type, or
1287 the sum of the number of active samplers in the program and the
1288 number of texture image units enabled for fixed-function fragment
1289 processing exceeds the combined limit on the total number of texture
1290 image units allowed.
1294 * Check: any two active samplers in the current program object are of
1295 * different types, but refer to the same texture image unit,
1297 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1305 * Called via glValidateProgram()
1308 validate_program(struct gl_context
*ctx
, GLuint program
)
1310 struct gl_shader_program
*shProg
;
1311 char errMsg
[100] = "";
1313 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1318 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1319 if (!shProg
->data
->Validated
) {
1320 /* update info log */
1321 if (shProg
->data
->InfoLog
) {
1322 ralloc_free(shProg
->data
->InfoLog
);
1324 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1331 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1333 GET_CURRENT_CONTEXT(ctx
);
1334 attach_shader(ctx
, program
, shader
, "glAttachObjectARB");
1339 _mesa_AttachShader(GLuint program
, GLuint shader
)
1341 GET_CURRENT_CONTEXT(ctx
);
1342 attach_shader(ctx
, program
, shader
, "glAttachShader");
1347 _mesa_CompileShader(GLuint shaderObj
)
1349 GET_CURRENT_CONTEXT(ctx
);
1350 if (MESA_VERBOSE
& VERBOSE_API
)
1351 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1352 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1353 "glCompileShader"));
1358 _mesa_CreateShader(GLenum type
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 if (MESA_VERBOSE
& VERBOSE_API
)
1362 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1363 return create_shader(ctx
, type
);
1367 GLhandleARB GLAPIENTRY
1368 _mesa_CreateShaderObjectARB(GLenum type
)
1370 GET_CURRENT_CONTEXT(ctx
);
1371 return create_shader(ctx
, type
);
1376 _mesa_CreateProgram(void)
1378 GET_CURRENT_CONTEXT(ctx
);
1379 if (MESA_VERBOSE
& VERBOSE_API
)
1380 _mesa_debug(ctx
, "glCreateProgram\n");
1381 return create_shader_program(ctx
);
1385 GLhandleARB GLAPIENTRY
1386 _mesa_CreateProgramObjectARB(void)
1388 GET_CURRENT_CONTEXT(ctx
);
1389 return create_shader_program(ctx
);
1394 _mesa_DeleteObjectARB(GLhandleARB obj
)
1396 if (MESA_VERBOSE
& VERBOSE_API
) {
1397 GET_CURRENT_CONTEXT(ctx
);
1398 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1402 GET_CURRENT_CONTEXT(ctx
);
1403 FLUSH_VERTICES(ctx
, 0);
1404 if (is_program(ctx
, obj
)) {
1405 delete_shader_program(ctx
, obj
);
1407 else if (is_shader(ctx
, obj
)) {
1408 delete_shader(ctx
, obj
);
1418 _mesa_DeleteProgram(GLuint name
)
1421 GET_CURRENT_CONTEXT(ctx
);
1422 FLUSH_VERTICES(ctx
, 0);
1423 delete_shader_program(ctx
, name
);
1429 _mesa_DeleteShader(GLuint name
)
1432 GET_CURRENT_CONTEXT(ctx
);
1433 FLUSH_VERTICES(ctx
, 0);
1434 delete_shader(ctx
, name
);
1440 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1442 GET_CURRENT_CONTEXT(ctx
);
1443 detach_shader(ctx
, program
, shader
);
1448 _mesa_DetachShader(GLuint program
, GLuint shader
)
1450 GET_CURRENT_CONTEXT(ctx
);
1451 detach_shader(ctx
, program
, shader
);
1456 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1457 GLsizei
* count
, GLhandleARB
* obj
)
1459 GET_CURRENT_CONTEXT(ctx
);
1460 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1465 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1466 GLsizei
*count
, GLuint
*obj
)
1468 GET_CURRENT_CONTEXT(ctx
);
1469 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1474 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1475 GLcharARB
* infoLog
)
1477 GET_CURRENT_CONTEXT(ctx
);
1478 if (is_program(ctx
, object
)) {
1479 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1481 else if (is_shader(ctx
, object
)) {
1482 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1485 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1491 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1493 GET_CURRENT_CONTEXT(ctx
);
1494 /* Implement in terms of GetProgramiv, GetShaderiv */
1495 if (is_program(ctx
, object
)) {
1496 if (pname
== GL_OBJECT_TYPE_ARB
) {
1497 *params
= GL_PROGRAM_OBJECT_ARB
;
1500 get_programiv(ctx
, object
, pname
, params
);
1503 else if (is_shader(ctx
, object
)) {
1504 if (pname
== GL_OBJECT_TYPE_ARB
) {
1505 *params
= GL_SHADER_OBJECT_ARB
;
1508 get_shaderiv(ctx
, object
, pname
, params
);
1512 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1518 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1521 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1522 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1523 params
[0] = (GLfloat
) iparams
[0];
1528 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1530 GET_CURRENT_CONTEXT(ctx
);
1531 get_programiv(ctx
, program
, pname
, params
);
1536 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1538 GET_CURRENT_CONTEXT(ctx
);
1539 get_shaderiv(ctx
, shader
, pname
, params
);
1544 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1545 GLsizei
*length
, GLchar
*infoLog
)
1547 GET_CURRENT_CONTEXT(ctx
);
1548 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1553 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1554 GLsizei
*length
, GLchar
*infoLog
)
1556 GET_CURRENT_CONTEXT(ctx
);
1557 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1562 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1563 GLsizei
*length
, GLchar
*sourceOut
)
1565 GET_CURRENT_CONTEXT(ctx
);
1566 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1570 GLhandleARB GLAPIENTRY
1571 _mesa_GetHandleARB(GLenum pname
)
1573 GET_CURRENT_CONTEXT(ctx
);
1574 return get_handle(ctx
, pname
);
1578 GLboolean GLAPIENTRY
1579 _mesa_IsProgram(GLuint name
)
1581 GET_CURRENT_CONTEXT(ctx
);
1582 return is_program(ctx
, name
);
1586 GLboolean GLAPIENTRY
1587 _mesa_IsShader(GLuint name
)
1589 GET_CURRENT_CONTEXT(ctx
);
1590 return is_shader(ctx
, name
);
1595 _mesa_LinkProgram(GLuint programObj
)
1597 GET_CURRENT_CONTEXT(ctx
);
1598 if (MESA_VERBOSE
& VERBOSE_API
)
1599 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1600 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1604 #ifdef ENABLE_SHADER_CACHE
1606 * Generate a SHA-1 hash value string for given source string.
1609 generate_sha1(const char *source
, char sha_str
[64])
1611 unsigned char sha
[20];
1612 _mesa_sha1_compute(source
, strlen(source
), sha
);
1613 _mesa_sha1_format(sha_str
, sha
);
1617 * Construct a full path for shader replacement functionality using
1620 * <path>/<stage prefix>_<CHECKSUM>.glsl
1623 construct_name(const gl_shader_stage stage
, const char *source
,
1627 static const char *types
[] = {
1628 "VS", "TC", "TE", "GS", "FS", "CS",
1631 generate_sha1(source
, sha
);
1632 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1636 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1639 dump_shader(const gl_shader_stage stage
, const char *source
)
1641 static bool path_exists
= true;
1648 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1650 path_exists
= false;
1654 char *name
= construct_name(stage
, source
, dump_path
);
1656 f
= fopen(name
, "w");
1661 GET_CURRENT_CONTEXT(ctx
);
1662 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1669 * Read shader source code from a file.
1670 * Useful for debugging to override an app's shader.
1673 read_shader(const gl_shader_stage stage
, const char *source
)
1676 static bool path_exists
= true;
1677 int len
, shader_size
= 0;
1684 read_path
= getenv("MESA_SHADER_READ_PATH");
1686 path_exists
= false;
1690 char *name
= construct_name(stage
, source
, read_path
);
1691 f
= fopen(name
, "r");
1696 /* allocate enough room for the entire shader */
1697 fseek(f
, 0, SEEK_END
);
1698 shader_size
= ftell(f
);
1700 assert(shader_size
);
1702 /* add one for terminating zero */
1705 buffer
= malloc(shader_size
);
1708 len
= fread(buffer
, 1, shader_size
, f
);
1716 #endif /* ENABLE_SHADER_CACHE */
1719 * Called via glShaderSource() and glShaderSourceARB() API functions.
1720 * Basically, concatenate the source code strings into one long string
1721 * and pass it to _mesa_shader_source().
1724 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1725 const GLchar
* const * string
, const GLint
* length
)
1727 GET_CURRENT_CONTEXT(ctx
);
1729 GLsizei i
, totalLength
;
1731 struct gl_shader
*sh
;
1733 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1737 if (string
== NULL
) {
1738 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1743 * This array holds offsets of where the appropriate string ends, thus the
1744 * last element will be set to the total length of the source code.
1746 offsets
= malloc(count
* sizeof(GLint
));
1747 if (offsets
== NULL
) {
1748 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1752 for (i
= 0; i
< count
; i
++) {
1753 if (string
[i
] == NULL
) {
1754 free((GLvoid
*) offsets
);
1755 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1756 "glShaderSourceARB(null string)");
1759 if (length
== NULL
|| length
[i
] < 0)
1760 offsets
[i
] = strlen(string
[i
]);
1762 offsets
[i
] = length
[i
];
1763 /* accumulate string lengths */
1765 offsets
[i
] += offsets
[i
- 1];
1768 /* Total length of source string is sum off all strings plus two.
1769 * One extra byte for terminating zero, another extra byte to silence
1770 * valgrind warnings in the parser/grammer code.
1772 totalLength
= offsets
[count
- 1] + 2;
1773 source
= malloc(totalLength
* sizeof(GLcharARB
));
1774 if (source
== NULL
) {
1775 free((GLvoid
*) offsets
);
1776 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1780 for (i
= 0; i
< count
; i
++) {
1781 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1782 memcpy(source
+ start
, string
[i
],
1783 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1785 source
[totalLength
- 1] = '\0';
1786 source
[totalLength
- 2] = '\0';
1788 #ifdef ENABLE_SHADER_CACHE
1789 GLcharARB
*replacement
;
1791 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1792 * if corresponding entry found from MESA_SHADER_READ_PATH.
1794 dump_shader(sh
->Stage
, source
);
1796 replacement
= read_shader(sh
->Stage
, source
);
1799 source
= replacement
;
1801 #endif /* ENABLE_SHADER_CACHE */
1803 shader_source(sh
, source
);
1809 static ALWAYS_INLINE
void
1810 use_program(GLuint program
, bool no_error
)
1812 GET_CURRENT_CONTEXT(ctx
);
1813 struct gl_shader_program
*shProg
= NULL
;
1815 if (MESA_VERBOSE
& VERBOSE_API
)
1816 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1820 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1823 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1824 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1825 "glUseProgram(transform feedback active)");
1831 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1835 if (!shProg
->data
->LinkStatus
) {
1836 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1837 "glUseProgram(program %u not linked)", program
);
1842 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1843 print_shader_info(shProg
);
1848 /* The ARB_separate_shader_object spec says:
1850 * "The executable code for an individual shader stage is taken from
1851 * the current program for that stage. If there is a current program
1852 * object established by UseProgram, that program is considered current
1853 * for all stages. Otherwise, if there is a bound program pipeline
1854 * object (section 2.14.PPO), the program bound to the appropriate
1855 * stage of the pipeline object is considered current."
1858 /* Attach shader state to the binding point */
1859 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1860 /* Update the program */
1861 _mesa_use_shader_program(ctx
, shProg
);
1863 /* Must be done first: detach the progam */
1864 _mesa_use_shader_program(ctx
, shProg
);
1865 /* Unattach shader_state binding point */
1866 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1867 ctx
->Pipeline
.Default
);
1868 /* If a pipeline was bound, rebind it */
1869 if (ctx
->Pipeline
.Current
) {
1871 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
1873 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1880 _mesa_UseProgram_no_error(GLuint program
)
1882 use_program(program
, true);
1887 _mesa_UseProgram(GLuint program
)
1889 use_program(program
, false);
1894 _mesa_ValidateProgram(GLuint program
)
1896 GET_CURRENT_CONTEXT(ctx
);
1897 validate_program(ctx
, program
);
1902 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1905 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1906 GLint
* range
, GLint
* precision
)
1908 const struct gl_program_constants
*limits
;
1909 const struct gl_precision
*p
;
1910 GET_CURRENT_CONTEXT(ctx
);
1912 switch (shadertype
) {
1913 case GL_VERTEX_SHADER
:
1914 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1916 case GL_FRAGMENT_SHADER
:
1917 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1920 _mesa_error(ctx
, GL_INVALID_ENUM
,
1921 "glGetShaderPrecisionFormat(shadertype)");
1925 switch (precisiontype
) {
1927 p
= &limits
->LowFloat
;
1929 case GL_MEDIUM_FLOAT
:
1930 p
= &limits
->MediumFloat
;
1933 p
= &limits
->HighFloat
;
1936 p
= &limits
->LowInt
;
1939 p
= &limits
->MediumInt
;
1942 p
= &limits
->HighInt
;
1945 _mesa_error(ctx
, GL_INVALID_ENUM
,
1946 "glGetShaderPrecisionFormat(precisiontype)");
1950 range
[0] = p
->RangeMin
;
1951 range
[1] = p
->RangeMax
;
1952 precision
[0] = p
->Precision
;
1957 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1960 _mesa_ReleaseShaderCompiler(void)
1962 _mesa_destroy_shader_compiler_caches();
1967 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1970 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1971 const void* binary
, GLint length
)
1973 GET_CURRENT_CONTEXT(ctx
);
1975 (void) binaryformat
;
1978 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
1979 * page 88 of the OpenGL 4.5 specs state:
1981 * "An INVALID_VALUE error is generated if count or length is negative.
1982 * An INVALID_ENUM error is generated if binaryformat is not a supported
1983 * format returned in SHADER_BINARY_FORMATS."
1985 if (n
< 0 || length
< 0) {
1986 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
1990 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
1995 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1996 GLenum
*binaryFormat
, GLvoid
*binary
)
1998 struct gl_shader_program
*shProg
;
1999 GLsizei length_dummy
;
2000 GET_CURRENT_CONTEXT(ctx
);
2003 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2007 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2011 /* The ARB_get_program_binary spec says:
2013 * "If <length> is NULL, then no length is returned."
2015 * Ensure that length always points to valid storage to avoid multiple NULL
2016 * pointer checks below.
2019 length
= &length_dummy
;
2022 /* The ARB_get_program_binary spec says:
2024 * "When a program object's LINK_STATUS is FALSE, its program binary
2025 * length is zero, and a call to GetProgramBinary will generate an
2026 * INVALID_OPERATION error.
2028 if (!shProg
->data
->LinkStatus
) {
2029 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2030 "glGetProgramBinary(program %u not linked)",
2037 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2038 "glGetProgramBinary(driver supports zero binary formats)");
2040 (void) binaryFormat
;
2045 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2046 const GLvoid
*binary
, GLsizei length
)
2048 struct gl_shader_program
*shProg
;
2049 GET_CURRENT_CONTEXT(ctx
);
2051 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2055 (void) binaryFormat
;
2058 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2060 * "If a negative number is provided where an argument of type sizei or
2061 * sizeiptr is specified, an INVALID_VALUE error is generated."
2064 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2068 /* The ARB_get_program_binary spec says:
2070 * "<binaryFormat> and <binary> must be those returned by a previous
2071 * call to GetProgramBinary, and <length> must be the length of the
2072 * program binary as returned by GetProgramBinary or GetProgramiv with
2073 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2074 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2077 * Since any value of binaryFormat passed "is not one of those specified as
2078 * allowable for [this] command, an INVALID_ENUM error is generated."
2080 shProg
->data
->LinkStatus
= linking_failure
;
2081 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2086 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2088 struct gl_shader_program
*shProg
;
2089 GET_CURRENT_CONTEXT(ctx
);
2091 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2092 "glProgramParameteri");
2097 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2098 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2099 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2100 * even be in the dispatch table, so we shouldn't need to expclicitly
2103 * On desktop, we ignore the 3.0+ requirement because it is silly.
2106 /* The ARB_get_program_binary extension spec says:
2108 * "An INVALID_VALUE error is generated if the <value> argument to
2109 * ProgramParameteri is not TRUE or FALSE."
2111 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2115 /* No need to notify the driver. Any changes will actually take effect
2116 * the next time the shader is linked.
2118 * The ARB_get_program_binary extension spec says:
2120 * "To indicate that a program binary is likely to be retrieved,
2121 * ProgramParameteri should be called with <pname>
2122 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2123 * will not be in effect until the next time LinkProgram or
2124 * ProgramBinary has been called successfully."
2126 * The resloution of issue 9 in the extension spec also says:
2128 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2129 * to indicate to the GL implementation that this program will
2130 * likely be saved with GetProgramBinary at some point. This will
2131 * give the GL implementation the opportunity to track any state
2132 * changes made to the program before being saved such that when it
2133 * is loaded again a recompile can be avoided."
2135 shProg
->BinaryRetreivableHint
= value
;
2138 case GL_PROGRAM_SEPARABLE
:
2139 /* Spec imply that the behavior is the same as ARB_get_program_binary
2140 * Chapter 7.3 Program Objects
2142 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2145 shProg
->SeparateShader
= value
;
2149 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2150 _mesa_enum_to_string(pname
));
2155 _mesa_error(ctx
, GL_INVALID_VALUE
,
2156 "glProgramParameteri(pname=%s, value=%d): "
2157 "value must be 0 or 1.",
2158 _mesa_enum_to_string(pname
),
2164 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2165 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2166 struct gl_pipeline_object
*shTarget
)
2168 struct gl_program
**target
;
2170 target
= &shTarget
->CurrentProgram
[stage
];
2172 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2175 if (*target
!= prog
) {
2176 /* Program is current, flush it */
2177 if (shTarget
== ctx
->_Shader
) {
2178 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2181 _mesa_reference_shader_program(ctx
,
2182 &shTarget
->ReferencedPrograms
[stage
],
2184 _mesa_reference_program(ctx
, target
, prog
);
2192 * Copy program-specific data generated by linking from the gl_shader_program
2193 * object to the gl_program object referred to by the gl_linked_shader.
2195 * This function expects _mesa_reference_program() to have been previously
2196 * called setting the gl_linked_shaders program reference.
2199 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2200 struct gl_linked_shader
*dst_sh
)
2202 assert(dst_sh
->Program
);
2204 struct gl_program
*dst
= dst_sh
->Program
;
2206 dst
->info
.separate_shader
= src
->SeparateShader
;
2208 switch (dst_sh
->Stage
) {
2209 case MESA_SHADER_GEOMETRY
: {
2210 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2211 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2212 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2215 case MESA_SHADER_FRAGMENT
: {
2216 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2219 case MESA_SHADER_COMPUTE
: {
2220 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2229 * ARB_separate_shader_objects: Compile & Link Program
2232 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2233 const GLchar
* const *strings
)
2235 GET_CURRENT_CONTEXT(ctx
);
2237 const GLuint shader
= create_shader(ctx
, type
);
2241 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2242 * GL_INVALID_VALUE should be generated if count < 0
2245 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2250 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2252 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2253 _mesa_compile_shader(ctx
, sh
);
2255 program
= create_shader_program(ctx
);
2257 struct gl_shader_program
*shProg
;
2258 GLint compiled
= GL_FALSE
;
2260 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2262 shProg
->SeparateShader
= GL_TRUE
;
2264 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2266 attach_shader(ctx
, program
, shader
, "glCreateShaderProgramv");
2267 _mesa_link_program(ctx
, shProg
);
2268 detach_shader(ctx
, program
, shader
);
2272 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2273 append
-error
-to
-info
-log
;
2274 shProg
->data
->LinkStatus
= linking_failure
;
2279 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2282 delete_shader(ctx
, shader
);
2290 * For GL_ARB_tessellation_shader
2292 extern void GLAPIENTRY
2293 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2295 GET_CURRENT_CONTEXT(ctx
);
2297 if (!_mesa_has_tessellation(ctx
)) {
2298 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2302 if (pname
!= GL_PATCH_VERTICES
) {
2303 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2307 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2308 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2312 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2316 extern void GLAPIENTRY
2317 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2319 GET_CURRENT_CONTEXT(ctx
);
2321 if (!_mesa_has_tessellation(ctx
)) {
2322 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2327 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2328 FLUSH_VERTICES(ctx
, 0);
2329 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2330 4 * sizeof(GLfloat
));
2331 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2333 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2334 FLUSH_VERTICES(ctx
, 0);
2335 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2336 2 * sizeof(GLfloat
));
2337 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2340 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2346 * ARB_shader_subroutine
2349 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2352 GET_CURRENT_CONTEXT(ctx
);
2353 const char *api_name
= "glGetSubroutineUniformLocation";
2354 struct gl_shader_program
*shProg
;
2355 GLenum resource_type
;
2356 gl_shader_stage stage
;
2358 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2359 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2363 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2367 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2368 if (!shProg
->_LinkedShaders
[stage
]) {
2369 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2373 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2374 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2378 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2381 GET_CURRENT_CONTEXT(ctx
);
2382 const char *api_name
= "glGetSubroutineIndex";
2383 struct gl_shader_program
*shProg
;
2384 struct gl_program_resource
*res
;
2385 GLenum resource_type
;
2386 gl_shader_stage stage
;
2388 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2389 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2393 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2397 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2398 if (!shProg
->_LinkedShaders
[stage
]) {
2399 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2403 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2404 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2409 return _mesa_program_resource_index(shProg
, res
);
2414 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2415 GLuint index
, GLenum pname
, GLint
*values
)
2417 GET_CURRENT_CONTEXT(ctx
);
2418 const char *api_name
= "glGetActiveSubroutineUniformiv";
2419 struct gl_shader_program
*shProg
;
2420 struct gl_linked_shader
*sh
;
2421 gl_shader_stage stage
;
2422 struct gl_program_resource
*res
;
2423 const struct gl_uniform_storage
*uni
;
2424 GLenum resource_type
;
2427 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2428 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2432 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2436 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2437 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2439 sh
= shProg
->_LinkedShaders
[stage
];
2441 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2445 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2446 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2447 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2452 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2453 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2456 values
[0] = uni
->num_compatible_subroutines
;
2460 case GL_COMPATIBLE_SUBROUTINES
: {
2461 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2465 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2466 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2467 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2468 if (fn
->types
[j
] == uni
->type
) {
2469 values
[count
++] = i
;
2477 case GL_UNIFORM_SIZE
:
2478 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2481 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2484 case GL_UNIFORM_NAME_LENGTH
:
2485 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2487 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2488 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2492 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2499 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2500 GLuint index
, GLsizei bufsize
,
2501 GLsizei
*length
, GLchar
*name
)
2503 GET_CURRENT_CONTEXT(ctx
);
2504 const char *api_name
= "glGetActiveSubroutineUniformName";
2505 struct gl_shader_program
*shProg
;
2506 GLenum resource_type
;
2507 gl_shader_stage stage
;
2509 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2510 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2514 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2518 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2519 if (!shProg
->_LinkedShaders
[stage
]) {
2520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2524 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2525 /* get program resource name */
2526 _mesa_get_program_resource_name(shProg
, resource_type
,
2528 length
, name
, api_name
);
2533 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2534 GLuint index
, GLsizei bufsize
,
2535 GLsizei
*length
, GLchar
*name
)
2537 GET_CURRENT_CONTEXT(ctx
);
2538 const char *api_name
= "glGetActiveSubroutineName";
2539 struct gl_shader_program
*shProg
;
2540 GLenum resource_type
;
2541 gl_shader_stage stage
;
2543 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2544 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2548 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2552 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2553 if (!shProg
->_LinkedShaders
[stage
]) {
2554 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2557 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2558 _mesa_get_program_resource_name(shProg
, resource_type
,
2560 length
, name
, api_name
);
2564 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2565 const GLuint
*indices
)
2567 GET_CURRENT_CONTEXT(ctx
);
2568 const char *api_name
= "glUniformSubroutinesuiv";
2569 gl_shader_stage stage
;
2572 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2573 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2577 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2578 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2580 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2584 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2585 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2590 bool flushed
= false;
2592 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2599 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2603 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2606 for (j
= i
; j
< i
+ uni_count
; j
++) {
2607 struct gl_subroutine_function
*subfn
= NULL
;
2608 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2609 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2613 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2614 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2615 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2622 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2623 if (subfn
->types
[k
] == uni
->type
)
2626 if (k
== subfn
->num_compat_types
) {
2627 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2631 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2639 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2642 GET_CURRENT_CONTEXT(ctx
);
2643 const char *api_name
= "glGetUniformSubroutineuiv";
2644 gl_shader_stage stage
;
2646 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2647 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2651 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2652 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2654 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2658 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2659 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2663 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2668 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2669 GLenum pname
, GLint
*values
)
2671 GET_CURRENT_CONTEXT(ctx
);
2672 const char *api_name
= "glGetProgramStageiv";
2673 struct gl_shader_program
*shProg
;
2674 struct gl_linked_shader
*sh
;
2675 gl_shader_stage stage
;
2677 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2678 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2682 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2686 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2687 sh
= shProg
->_LinkedShaders
[stage
];
2689 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2690 * INVALID_OPERATION in the case of not be linked.
2692 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2693 * same info using other specs (ARB_program_interface_query), without the
2694 * need of the program to be linked, being the value for that case 0.
2696 * But at the same time, some other methods require the program to be
2697 * linked for pname related to locations, so it would be inconsistent to
2698 * not do the same here. So we are:
2699 * * Return GL_INVALID_OPERATION if not linked only for locations.
2700 * * Setting a default value of 0, to be returned if not linked.
2704 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2705 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2710 struct gl_program
*p
= sh
->Program
;
2712 case GL_ACTIVE_SUBROUTINES
:
2713 values
[0] = p
->sh
.NumSubroutineFunctions
;
2715 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2716 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2718 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2719 values
[0] = p
->sh
.NumSubroutineUniforms
;
2721 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2725 GLenum resource_type
;
2726 struct gl_program_resource
*res
;
2728 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2729 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2730 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2732 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2737 values
[0] = max_len
;
2740 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2744 GLenum resource_type
;
2745 struct gl_program_resource
*res
;
2747 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2748 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2749 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2751 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2752 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2758 values
[0] = max_len
;
2762 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2769 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2773 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2774 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2775 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2776 if (fn
->types
[j
] == type
)
2784 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2785 struct gl_program
*p
)
2789 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2794 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2803 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2804 for (j
= 0; j
< uni_count
; j
++) {
2805 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2806 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2809 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2811 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2815 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2816 gl_shader_stage stage
)
2818 if (ctx
->_Shader
->CurrentProgram
[stage
])
2819 _mesa_shader_write_subroutine_index(ctx
,
2820 ctx
->_Shader
->CurrentProgram
[stage
]);
2824 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2825 struct gl_program
*p
)
2829 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2830 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2831 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2832 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2833 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2836 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2837 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2842 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);