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
)
249 struct gl_shader_program
*shProg
;
250 struct gl_shader
*sh
;
253 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
255 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
259 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
264 n
= shProg
->NumShaders
;
265 for (i
= 0; i
< n
; i
++) {
266 if (shProg
->Shaders
[i
] == sh
) {
267 /* The shader is already attched to this program. The
268 * GL_ARB_shader_objects spec says:
270 * "The error INVALID_OPERATION is generated by AttachObjectARB
271 * if <obj> is already attached to <containerObj>."
273 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
275 } else if (same_type_disallowed
&&
276 shProg
->Shaders
[i
]->Stage
== sh
->Stage
) {
277 /* Shader with the same type is already attached to this program,
278 * OpenGL ES 2.0 and 3.0 specs say:
280 * "Multiple shader objects of the same type may not be attached
281 * to a single program object. [...] The error INVALID_OPERATION
282 * is generated if [...] another shader object of the same type
283 * as shader is already attached to program."
285 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
291 shProg
->Shaders
= realloc(shProg
->Shaders
,
292 (n
+ 1) * sizeof(struct gl_shader
*));
293 if (!shProg
->Shaders
) {
294 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
299 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
300 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
301 shProg
->NumShaders
++;
306 create_shader(struct gl_context
*ctx
, GLenum type
)
308 struct gl_shader
*sh
;
311 if (!_mesa_validate_shader_target(ctx
, type
)) {
312 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(%s)",
313 _mesa_enum_to_string(type
));
317 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
318 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
319 sh
= _mesa_new_shader(name
, _mesa_shader_enum_to_shader_stage(type
));
321 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, sh
);
322 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
329 create_shader_program(struct gl_context
*ctx
)
332 struct gl_shader_program
*shProg
;
334 _mesa_HashLockMutex(ctx
->Shared
->ShaderObjects
);
336 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
338 shProg
= _mesa_new_shader_program(name
);
340 _mesa_HashInsertLocked(ctx
->Shared
->ShaderObjects
, name
, shProg
);
342 assert(shProg
->RefCount
== 1);
344 _mesa_HashUnlockMutex(ctx
->Shared
->ShaderObjects
);
351 * Delete a shader program. Actually, just decrement the program's
352 * reference count and mark it as DeletePending.
353 * Used to implement glDeleteProgram() and glDeleteObjectARB().
356 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
359 * NOTE: deleting shaders/programs works a bit differently than
360 * texture objects (and buffer objects, etc). Shader/program
361 * handles/IDs exist in the hash table until the object is really
362 * deleted (refcount==0). With texture objects, the handle/ID is
363 * removed from the hash table in glDeleteTextures() while the tex
364 * object itself might linger until its refcount goes to zero.
366 struct gl_shader_program
*shProg
;
368 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
372 if (!shProg
->DeletePending
) {
373 shProg
->DeletePending
= GL_TRUE
;
375 /* effectively, decr shProg's refcount */
376 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
382 delete_shader(struct gl_context
*ctx
, GLuint shader
)
384 struct gl_shader
*sh
;
386 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
390 if (!sh
->DeletePending
) {
391 sh
->DeletePending
= GL_TRUE
;
393 /* effectively, decr sh's refcount */
394 _mesa_reference_shader(ctx
, &sh
, NULL
);
400 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
402 struct gl_shader_program
*shProg
;
406 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
410 n
= shProg
->NumShaders
;
412 for (i
= 0; i
< n
; i
++) {
413 if (shProg
->Shaders
[i
]->Name
== shader
) {
415 struct gl_shader
**newList
;
418 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
420 /* alloc new, smaller array */
421 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
423 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
426 /* Copy old list entries to new list, skipping removed entry at [i] */
427 for (j
= 0; j
< i
; j
++) {
428 newList
[j
] = shProg
->Shaders
[j
];
431 newList
[j
++] = shProg
->Shaders
[i
];
434 /* Free old list and install new one */
435 free(shProg
->Shaders
);
436 shProg
->Shaders
= newList
;
437 shProg
->NumShaders
= n
- 1;
440 /* sanity check - make sure the new list's entries are sensible */
441 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
442 assert(shProg
->Shaders
[j
]->Stage
== MESA_SHADER_VERTEX
||
443 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_CTRL
||
444 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_TESS_EVAL
||
445 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_GEOMETRY
||
446 shProg
->Shaders
[j
]->Stage
== MESA_SHADER_FRAGMENT
);
447 assert(shProg
->Shaders
[j
]->RefCount
> 0);
458 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
459 err
= GL_INVALID_OPERATION
;
461 err
= GL_INVALID_VALUE
;
462 _mesa_error(ctx
, err
, "glDetachShader(shader)");
469 * Return list of shaders attached to shader program.
472 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
473 GLsizei
*count
, GLuint
*obj
)
475 struct gl_shader_program
*shProg
;
478 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
483 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
487 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
488 obj
[i
] = shProg
->Shaders
[i
]->Name
;
497 * glGetHandleARB() - return ID/name of currently bound shader program.
500 get_handle(struct gl_context
*ctx
, GLenum pname
)
502 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
503 if (ctx
->_Shader
->ActiveProgram
)
504 return ctx
->_Shader
->ActiveProgram
->Name
;
509 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
516 * Check if a geometry shader query is valid at this time. If not, report an
517 * error and return false.
519 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
521 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
522 * are queried for a program which has not been linked successfully, or
523 * which does not contain objects to form a geometry shader, then an
524 * INVALID_OPERATION error is generated."
527 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
529 if (shProg
->data
->LinkStatus
&&
530 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
534 _mesa_error(ctx
, GL_INVALID_OPERATION
,
535 "glGetProgramv(linked geometry shader required)");
541 * Check if a tessellation control shader query is valid at this time.
542 * If not, report an error and return false.
544 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
546 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
547 * not been linked successfully, or which does not contain objects to
548 * form a tessellation control shader, then an INVALID_OPERATION error is
552 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
554 if (shProg
->data
->LinkStatus
&&
555 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
559 _mesa_error(ctx
, GL_INVALID_OPERATION
,
560 "glGetProgramv(linked tessellation control shader required)");
566 * Check if a tessellation evaluation shader query is valid at this time.
567 * If not, report an error and return false.
569 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
571 * "If any of the pname values in this paragraph are queried for a program
572 * which has not been linked successfully, or which does not contain
573 * objects to form a tessellation evaluation shader, then an
574 * INVALID_OPERATION error is generated."
578 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
580 if (shProg
->data
->LinkStatus
&&
581 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
585 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
586 "evaluation shader required)");
592 * glGetProgramiv() - get shader program state.
593 * Note that this is for GLSL shader programs, not ARB vertex/fragment
594 * programs (see glGetProgramivARB).
597 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
600 struct gl_shader_program
*shProg
601 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
603 /* Is transform feedback available in this context?
606 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
607 || ctx
->API
== API_OPENGL_CORE
608 || _mesa_is_gles3(ctx
);
610 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
611 * and GL 3.2) are available in this context
613 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
614 const bool has_tess
= _mesa_has_tessellation(ctx
);
616 /* Are uniform buffer objects available in this context?
619 (ctx
->API
== API_OPENGL_COMPAT
&&
620 ctx
->Extensions
.ARB_uniform_buffer_object
)
621 || ctx
->API
== API_OPENGL_CORE
622 || _mesa_is_gles3(ctx
);
629 case GL_DELETE_STATUS
:
630 *params
= shProg
->DeletePending
;
633 *params
= shProg
->data
->LinkStatus
? GL_TRUE
: GL_FALSE
;
635 case GL_VALIDATE_STATUS
:
636 *params
= shProg
->data
->Validated
;
638 case GL_INFO_LOG_LENGTH
:
639 *params
= (shProg
->data
->InfoLog
&& shProg
->data
->InfoLog
[0] != '\0') ?
640 strlen(shProg
->data
->InfoLog
) + 1 : 0;
642 case GL_ATTACHED_SHADERS
:
643 *params
= shProg
->NumShaders
;
645 case GL_ACTIVE_ATTRIBUTES
:
646 *params
= _mesa_count_active_attribs(shProg
);
648 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
649 *params
= _mesa_longest_attribute_name_length(shProg
);
651 case GL_ACTIVE_UNIFORMS
: {
653 const unsigned num_uniforms
=
654 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
655 for (*params
= 0, i
= 0; i
< num_uniforms
; i
++) {
656 if (!shProg
->data
->UniformStorage
[i
].is_shader_storage
)
661 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
664 const unsigned num_uniforms
=
665 shProg
->data
->NumUniformStorage
- shProg
->data
->NumHiddenUniforms
;
667 for (i
= 0; i
< num_uniforms
; i
++) {
668 if (shProg
->data
->UniformStorage
[i
].is_shader_storage
)
671 /* Add one for the terminating NUL character for a non-array, and
672 * 4 for the "[0]" and the NUL for an array.
674 const GLint len
= strlen(shProg
->data
->UniformStorage
[i
].name
) + 1 +
675 ((shProg
->data
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
684 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
687 *params
= shProg
->TransformFeedback
.NumVarying
;
689 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
695 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
696 /* Add one for the terminating NUL character.
699 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
708 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
711 *params
= shProg
->TransformFeedback
.BufferMode
;
713 case GL_GEOMETRY_VERTICES_OUT
:
716 if (check_gs_query(ctx
, shProg
)) {
717 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
718 Program
->info
.gs
.vertices_out
;
721 case GL_GEOMETRY_SHADER_INVOCATIONS
:
722 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
724 if (check_gs_query(ctx
, shProg
)) {
725 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
726 Program
->info
.gs
.invocations
;
729 case GL_GEOMETRY_INPUT_TYPE
:
732 if (check_gs_query(ctx
, shProg
)) {
733 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
734 Program
->info
.gs
.input_primitive
;
737 case GL_GEOMETRY_OUTPUT_TYPE
:
740 if (check_gs_query(ctx
, shProg
)) {
741 *params
= shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->
742 Program
->info
.gs
.output_primitive
;
745 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
752 for (i
= 0; i
< shProg
->data
->NumUniformBlocks
; i
++) {
753 /* Add one for the terminating NUL character.
755 const GLint len
= strlen(shProg
->data
->UniformBlocks
[i
].Name
) + 1;
764 case GL_ACTIVE_UNIFORM_BLOCKS
:
768 *params
= shProg
->data
->NumUniformBlocks
;
770 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
771 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
772 * only available with desktop OpenGL 3.0+ with the
773 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
775 * On desktop, we ignore the 3.0+ requirement because it is silly.
777 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
780 *params
= shProg
->BinaryRetreivableHint
;
782 case GL_PROGRAM_BINARY_LENGTH
:
785 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
786 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
789 *params
= shProg
->data
->NumAtomicBuffers
;
791 case GL_COMPUTE_WORK_GROUP_SIZE
: {
793 if (!_mesa_has_compute_shaders(ctx
))
795 if (!shProg
->data
->LinkStatus
) {
796 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
800 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
801 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
805 for (i
= 0; i
< 3; i
++)
806 params
[i
] = shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
]->
807 Program
->info
.cs
.local_size
[i
];
810 case GL_PROGRAM_SEPARABLE
:
811 /* If the program has not been linked, return initial value 0. */
812 *params
= (shProg
->data
->LinkStatus
== linking_failure
) ? 0 : shProg
->SeparateShader
;
815 /* ARB_tessellation_shader */
816 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
819 if (check_tcs_query(ctx
, shProg
)) {
820 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->
821 Program
->info
.tess
.tcs_vertices_out
;
824 case GL_TESS_GEN_MODE
:
827 if (check_tes_query(ctx
, shProg
)) {
828 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
829 Program
->info
.tess
.primitive_mode
;
832 case GL_TESS_GEN_SPACING
:
835 if (check_tes_query(ctx
, shProg
)) {
836 const struct gl_linked_shader
*tes
=
837 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
838 switch (tes
->Program
->info
.tess
.spacing
) {
839 case TESS_SPACING_EQUAL
:
842 case TESS_SPACING_FRACTIONAL_ODD
:
843 *params
= GL_FRACTIONAL_ODD
;
845 case TESS_SPACING_FRACTIONAL_EVEN
:
846 *params
= GL_FRACTIONAL_EVEN
;
848 case TESS_SPACING_UNSPECIFIED
:
854 case GL_TESS_GEN_VERTEX_ORDER
:
857 if (check_tes_query(ctx
, shProg
)) {
858 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
859 Program
->info
.tess
.ccw
? GL_CCW
: GL_CW
;
862 case GL_TESS_GEN_POINT_MODE
:
865 if (check_tes_query(ctx
, shProg
)) {
866 *params
= shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->
867 Program
->info
.tess
.point_mode
? GL_TRUE
: GL_FALSE
;
874 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
875 _mesa_enum_to_string(pname
));
880 * glGetShaderiv() - get GLSL shader state
883 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
885 struct gl_shader
*shader
=
886 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
894 *params
= shader
->Type
;
896 case GL_DELETE_STATUS
:
897 *params
= shader
->DeletePending
;
899 case GL_COMPILE_STATUS
:
900 *params
= shader
->CompileStatus
? GL_TRUE
: GL_FALSE
;
902 case GL_INFO_LOG_LENGTH
:
903 *params
= (shader
->InfoLog
&& shader
->InfoLog
[0] != '\0') ?
904 strlen(shader
->InfoLog
) + 1 : 0;
906 case GL_SHADER_SOURCE_LENGTH
:
907 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
910 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
917 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
918 GLsizei
*length
, GLchar
*infoLog
)
920 struct gl_shader_program
*shProg
;
922 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
923 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
925 * "If a negative number is provided where an argument of type sizei or
926 * sizeiptr is specified, an INVALID_VALUE error is generated."
929 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
933 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
934 "glGetProgramInfoLog(program)");
939 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->data
->InfoLog
);
944 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
945 GLsizei
*length
, GLchar
*infoLog
)
947 struct gl_shader
*sh
;
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
, "glGetShaderInfoLog(bufSize < 0)");
960 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
965 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
970 * Return shader source code.
973 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
974 GLsizei
*length
, GLchar
*sourceOut
)
976 struct gl_shader
*sh
;
979 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
983 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
987 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
992 * Set/replace shader source code. A helper function used by
993 * glShaderSource[ARB].
996 shader_source(struct gl_shader
*sh
, const GLchar
*source
)
1000 if (sh
->CompileStatus
== compile_skipped
&& !sh
->FallbackSource
) {
1001 /* If shader was previously compiled back-up the source in case of cache
1004 sh
->FallbackSource
= sh
->Source
;
1005 sh
->Source
= source
;
1007 /* free old shader source string and install new one */
1008 free((void *)sh
->Source
);
1009 sh
->Source
= source
;
1013 sh
->SourceChecksum
= util_hash_crc32(sh
->Source
, strlen(sh
->Source
));
1022 _mesa_compile_shader(struct gl_context
*ctx
, struct gl_shader
*sh
)
1028 /* If the user called glCompileShader without first calling
1029 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
1031 sh
->CompileStatus
= compile_failure
;
1033 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1034 _mesa_log("GLSL source for %s shader %d:\n",
1035 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1036 _mesa_log("%s\n", sh
->Source
);
1039 /* this call will set the shader->CompileStatus field to indicate if
1040 * compilation was successful.
1042 _mesa_glsl_compile_shader(ctx
, sh
, false, false, false);
1044 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
1045 _mesa_write_shader_to_file(sh
);
1048 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
1049 if (sh
->CompileStatus
) {
1051 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
1052 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
1054 _mesa_log("No GLSL IR for shader %d (shader may be from "
1055 "cache)\n", sh
->Name
);
1059 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
1061 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
1062 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
1063 _mesa_log("%s\n", sh
->InfoLog
);
1068 if (!sh
->CompileStatus
) {
1069 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1070 _mesa_log("GLSL source for %s shader %d:\n",
1071 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1072 _mesa_log("%s\n", sh
->Source
);
1073 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1076 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1077 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1078 sh
->Name
, sh
->InfoLog
);
1085 * Link a program's shaders.
1088 _mesa_link_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1093 /* From the ARB_transform_feedback2 specification:
1094 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1095 * the name of a program being used by one or more transform feedback
1096 * objects, even if the objects are not currently bound or are paused."
1098 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1100 "glLinkProgram(transform feedback is using the program)");
1104 unsigned programs_in_use
= 0;
1106 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1107 if (ctx
->_Shader
->CurrentProgram
[stage
] &&
1108 ctx
->_Shader
->CurrentProgram
[stage
]->Id
== shProg
->Name
) {
1109 programs_in_use
|= 1 << stage
;
1113 FLUSH_VERTICES(ctx
, 0);
1114 _mesa_glsl_link_shader(ctx
, shProg
);
1116 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
1118 * "If LinkProgram or ProgramBinary successfully re-links a program
1119 * object that is active for any shader stage, then the newly generated
1120 * executable code will be installed as part of the current rendering
1121 * state for all shader stages where the program is active.
1122 * Additionally, the newly generated executable code is made part of
1123 * the state of any program pipeline for all stages where the program
1126 if (shProg
->data
->LinkStatus
&& programs_in_use
) {
1127 while (programs_in_use
) {
1128 const int stage
= u_bit_scan(&programs_in_use
);
1130 struct gl_program
*prog
= NULL
;
1131 if (shProg
->_LinkedShaders
[stage
])
1132 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
1134 _mesa_use_program(ctx
, stage
, shProg
, prog
, ctx
->_Shader
);
1138 /* Capture .shader_test files. */
1139 const char *capture_path
= _mesa_get_shader_capture_path();
1140 if (shProg
->Name
!= 0 && shProg
->Name
!= ~0 && capture_path
!= NULL
) {
1142 char *filename
= ralloc_asprintf(NULL
, "%s/%u.shader_test",
1143 capture_path
, shProg
->Name
);
1144 file
= fopen(filename
, "w");
1146 fprintf(file
, "[require]\nGLSL%s >= %u.%02u\n",
1147 shProg
->IsES
? " ES" : "",
1148 shProg
->data
->Version
/ 100, shProg
->data
->Version
% 100);
1149 if (shProg
->SeparateShader
)
1150 fprintf(file
, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
1151 fprintf(file
, "\n");
1153 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
1154 fprintf(file
, "[%s shader]\n%s\n",
1155 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1156 shProg
->Shaders
[i
]->Source
);
1160 _mesa_warning(ctx
, "Failed to open %s", filename
);
1163 ralloc_free(filename
);
1166 if (shProg
->data
->LinkStatus
== linking_failure
&&
1167 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1168 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1169 shProg
->Name
, shProg
->data
->InfoLog
);
1176 printf("Link %u shaders in program %u: %s\n",
1177 shProg
->NumShaders
, shProg
->Name
,
1178 shProg
->data
->LinkStatus
? "Success" : "Failed");
1180 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1181 printf(" shader %u, stage %u\n",
1182 shProg
->Shaders
[i
]->Name
,
1183 shProg
->Shaders
[i
]->Stage
);
1190 * Print basic shader info (for debug).
1193 print_shader_info(const struct gl_shader_program
*shProg
)
1197 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1198 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1200 printf(" %s shader %u, checksum %u\n",
1201 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1202 shProg
->Shaders
[i
]->Name
,
1203 shProg
->Shaders
[i
]->SourceChecksum
);
1205 printf(" %s shader %u\n",
1206 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1207 shProg
->Shaders
[i
]->Name
);
1210 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1211 printf(" vert prog %u\n",
1212 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1213 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1214 printf(" frag prog %u\n",
1215 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1216 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1217 printf(" geom prog %u\n",
1218 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1219 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1220 printf(" tesc prog %u\n",
1221 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1222 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1223 printf(" tese prog %u\n",
1224 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1229 * Use the named shader program for subsequent glUniform calls
1232 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1235 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
1236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1237 "%s(program %u not linked)", caller
, shProg
->Name
);
1241 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1242 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1248 * Use the named shader program for subsequent rendering.
1251 _mesa_use_shader_program(struct gl_context
*ctx
,
1252 struct gl_shader_program
*shProg
)
1254 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1255 struct gl_program
*new_prog
= NULL
;
1256 if (shProg
&& shProg
->_LinkedShaders
[i
])
1257 new_prog
= shProg
->_LinkedShaders
[i
]->Program
;
1258 _mesa_use_program(ctx
, i
, shProg
, new_prog
, &ctx
->Shader
);
1260 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1265 * Do validation of the given shader program.
1266 * \param errMsg returns error message if validation fails.
1267 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1270 validate_shader_program(const struct gl_shader_program
*shProg
,
1273 if (!shProg
->data
->LinkStatus
) {
1277 /* From the GL spec, a program is invalid if any of these are true:
1279 any two active samplers in the current program object are of
1280 different types, but refer to the same texture image unit,
1282 any active sampler in the current program object refers to a texture
1283 image unit where fixed-function fragment processing accesses a
1284 texture target that does not match the sampler type, or
1286 the sum of the number of active samplers in the program and the
1287 number of texture image units enabled for fixed-function fragment
1288 processing exceeds the combined limit on the total number of texture
1289 image units allowed.
1293 * Check: any two active samplers in the current program object are of
1294 * different types, but refer to the same texture image unit,
1296 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1304 * Called via glValidateProgram()
1307 validate_program(struct gl_context
*ctx
, GLuint program
)
1309 struct gl_shader_program
*shProg
;
1310 char errMsg
[100] = "";
1312 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1317 shProg
->data
->Validated
= validate_shader_program(shProg
, errMsg
);
1318 if (!shProg
->data
->Validated
) {
1319 /* update info log */
1320 if (shProg
->data
->InfoLog
) {
1321 ralloc_free(shProg
->data
->InfoLog
);
1323 shProg
->data
->InfoLog
= ralloc_strdup(shProg
->data
, errMsg
);
1330 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1332 GET_CURRENT_CONTEXT(ctx
);
1333 attach_shader(ctx
, program
, shader
);
1338 _mesa_AttachShader(GLuint program
, GLuint shader
)
1340 GET_CURRENT_CONTEXT(ctx
);
1341 attach_shader(ctx
, program
, shader
);
1346 _mesa_CompileShader(GLuint shaderObj
)
1348 GET_CURRENT_CONTEXT(ctx
);
1349 if (MESA_VERBOSE
& VERBOSE_API
)
1350 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1351 _mesa_compile_shader(ctx
, _mesa_lookup_shader_err(ctx
, shaderObj
,
1352 "glCompileShader"));
1357 _mesa_CreateShader(GLenum type
)
1359 GET_CURRENT_CONTEXT(ctx
);
1360 if (MESA_VERBOSE
& VERBOSE_API
)
1361 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1362 return create_shader(ctx
, type
);
1366 GLhandleARB GLAPIENTRY
1367 _mesa_CreateShaderObjectARB(GLenum type
)
1369 GET_CURRENT_CONTEXT(ctx
);
1370 return create_shader(ctx
, type
);
1375 _mesa_CreateProgram(void)
1377 GET_CURRENT_CONTEXT(ctx
);
1378 if (MESA_VERBOSE
& VERBOSE_API
)
1379 _mesa_debug(ctx
, "glCreateProgram\n");
1380 return create_shader_program(ctx
);
1384 GLhandleARB GLAPIENTRY
1385 _mesa_CreateProgramObjectARB(void)
1387 GET_CURRENT_CONTEXT(ctx
);
1388 return create_shader_program(ctx
);
1393 _mesa_DeleteObjectARB(GLhandleARB obj
)
1395 if (MESA_VERBOSE
& VERBOSE_API
) {
1396 GET_CURRENT_CONTEXT(ctx
);
1397 _mesa_debug(ctx
, "glDeleteObjectARB(%lu)\n", (unsigned long)obj
);
1401 GET_CURRENT_CONTEXT(ctx
);
1402 FLUSH_VERTICES(ctx
, 0);
1403 if (is_program(ctx
, obj
)) {
1404 delete_shader_program(ctx
, obj
);
1406 else if (is_shader(ctx
, obj
)) {
1407 delete_shader(ctx
, obj
);
1417 _mesa_DeleteProgram(GLuint name
)
1420 GET_CURRENT_CONTEXT(ctx
);
1421 FLUSH_VERTICES(ctx
, 0);
1422 delete_shader_program(ctx
, name
);
1428 _mesa_DeleteShader(GLuint name
)
1431 GET_CURRENT_CONTEXT(ctx
);
1432 FLUSH_VERTICES(ctx
, 0);
1433 delete_shader(ctx
, name
);
1439 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1441 GET_CURRENT_CONTEXT(ctx
);
1442 detach_shader(ctx
, program
, shader
);
1447 _mesa_DetachShader(GLuint program
, GLuint shader
)
1449 GET_CURRENT_CONTEXT(ctx
);
1450 detach_shader(ctx
, program
, shader
);
1455 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1456 GLsizei
* count
, GLhandleARB
* obj
)
1458 GET_CURRENT_CONTEXT(ctx
);
1459 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1464 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1465 GLsizei
*count
, GLuint
*obj
)
1467 GET_CURRENT_CONTEXT(ctx
);
1468 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1473 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1474 GLcharARB
* infoLog
)
1476 GET_CURRENT_CONTEXT(ctx
);
1477 if (is_program(ctx
, object
)) {
1478 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1480 else if (is_shader(ctx
, object
)) {
1481 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1484 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1490 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1492 GET_CURRENT_CONTEXT(ctx
);
1493 /* Implement in terms of GetProgramiv, GetShaderiv */
1494 if (is_program(ctx
, object
)) {
1495 if (pname
== GL_OBJECT_TYPE_ARB
) {
1496 *params
= GL_PROGRAM_OBJECT_ARB
;
1499 get_programiv(ctx
, object
, pname
, params
);
1502 else if (is_shader(ctx
, object
)) {
1503 if (pname
== GL_OBJECT_TYPE_ARB
) {
1504 *params
= GL_SHADER_OBJECT_ARB
;
1507 get_shaderiv(ctx
, object
, pname
, params
);
1511 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1517 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1520 GLint iparams
[1] = {0}; /* XXX is one element enough? */
1521 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1522 params
[0] = (GLfloat
) iparams
[0];
1527 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1529 GET_CURRENT_CONTEXT(ctx
);
1530 get_programiv(ctx
, program
, pname
, params
);
1535 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1537 GET_CURRENT_CONTEXT(ctx
);
1538 get_shaderiv(ctx
, shader
, pname
, params
);
1543 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1544 GLsizei
*length
, GLchar
*infoLog
)
1546 GET_CURRENT_CONTEXT(ctx
);
1547 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1552 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1553 GLsizei
*length
, GLchar
*infoLog
)
1555 GET_CURRENT_CONTEXT(ctx
);
1556 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1561 _mesa_GetShaderSource(GLuint shader
, GLsizei maxLength
,
1562 GLsizei
*length
, GLchar
*sourceOut
)
1564 GET_CURRENT_CONTEXT(ctx
);
1565 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1569 GLhandleARB GLAPIENTRY
1570 _mesa_GetHandleARB(GLenum pname
)
1572 GET_CURRENT_CONTEXT(ctx
);
1573 return get_handle(ctx
, pname
);
1577 GLboolean GLAPIENTRY
1578 _mesa_IsProgram(GLuint name
)
1580 GET_CURRENT_CONTEXT(ctx
);
1581 return is_program(ctx
, name
);
1585 GLboolean GLAPIENTRY
1586 _mesa_IsShader(GLuint name
)
1588 GET_CURRENT_CONTEXT(ctx
);
1589 return is_shader(ctx
, name
);
1594 _mesa_LinkProgram(GLuint programObj
)
1596 GET_CURRENT_CONTEXT(ctx
);
1597 if (MESA_VERBOSE
& VERBOSE_API
)
1598 _mesa_debug(ctx
, "glLinkProgram %u\n", programObj
);
1599 _mesa_link_program(ctx
, _mesa_lookup_shader_program_err(ctx
, programObj
,
1603 #ifdef ENABLE_SHADER_CACHE
1605 * Generate a SHA-1 hash value string for given source string.
1608 generate_sha1(const char *source
, char sha_str
[64])
1610 unsigned char sha
[20];
1611 _mesa_sha1_compute(source
, strlen(source
), sha
);
1612 _mesa_sha1_format(sha_str
, sha
);
1616 * Construct a full path for shader replacement functionality using
1619 * <path>/<stage prefix>_<CHECKSUM>.glsl
1622 construct_name(const gl_shader_stage stage
, const char *source
,
1626 static const char *types
[] = {
1627 "VS", "TC", "TE", "GS", "FS", "CS",
1630 generate_sha1(source
, sha
);
1631 return ralloc_asprintf(NULL
, "%s/%s_%s.glsl", path
, types
[stage
], sha
);
1635 * Write given shader source to a file in MESA_SHADER_DUMP_PATH.
1638 dump_shader(const gl_shader_stage stage
, const char *source
)
1640 static bool path_exists
= true;
1647 dump_path
= getenv("MESA_SHADER_DUMP_PATH");
1649 path_exists
= false;
1653 char *name
= construct_name(stage
, source
, dump_path
);
1655 f
= fopen(name
, "w");
1660 GET_CURRENT_CONTEXT(ctx
);
1661 _mesa_warning(ctx
, "could not open %s for dumping shader (%s)", name
,
1668 * Read shader source code from a file.
1669 * Useful for debugging to override an app's shader.
1672 read_shader(const gl_shader_stage stage
, const char *source
)
1675 static bool path_exists
= true;
1676 int len
, shader_size
= 0;
1683 read_path
= getenv("MESA_SHADER_READ_PATH");
1685 path_exists
= false;
1689 char *name
= construct_name(stage
, source
, read_path
);
1690 f
= fopen(name
, "r");
1695 /* allocate enough room for the entire shader */
1696 fseek(f
, 0, SEEK_END
);
1697 shader_size
= ftell(f
);
1699 assert(shader_size
);
1701 /* add one for terminating zero */
1704 buffer
= malloc(shader_size
);
1707 len
= fread(buffer
, 1, shader_size
, f
);
1715 #endif /* ENABLE_SHADER_CACHE */
1718 * Called via glShaderSource() and glShaderSourceARB() API functions.
1719 * Basically, concatenate the source code strings into one long string
1720 * and pass it to _mesa_shader_source().
1723 _mesa_ShaderSource(GLuint shaderObj
, GLsizei count
,
1724 const GLchar
* const * string
, const GLint
* length
)
1726 GET_CURRENT_CONTEXT(ctx
);
1728 GLsizei i
, totalLength
;
1730 struct gl_shader
*sh
;
1732 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glShaderSourceARB");
1736 if (string
== NULL
) {
1737 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1742 * This array holds offsets of where the appropriate string ends, thus the
1743 * last element will be set to the total length of the source code.
1745 offsets
= malloc(count
* sizeof(GLint
));
1746 if (offsets
== NULL
) {
1747 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1751 for (i
= 0; i
< count
; i
++) {
1752 if (string
[i
] == NULL
) {
1753 free((GLvoid
*) offsets
);
1754 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1755 "glShaderSourceARB(null string)");
1758 if (length
== NULL
|| length
[i
] < 0)
1759 offsets
[i
] = strlen(string
[i
]);
1761 offsets
[i
] = length
[i
];
1762 /* accumulate string lengths */
1764 offsets
[i
] += offsets
[i
- 1];
1767 /* Total length of source string is sum off all strings plus two.
1768 * One extra byte for terminating zero, another extra byte to silence
1769 * valgrind warnings in the parser/grammer code.
1771 totalLength
= offsets
[count
- 1] + 2;
1772 source
= malloc(totalLength
* sizeof(GLcharARB
));
1773 if (source
== NULL
) {
1774 free((GLvoid
*) offsets
);
1775 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1779 for (i
= 0; i
< count
; i
++) {
1780 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1781 memcpy(source
+ start
, string
[i
],
1782 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1784 source
[totalLength
- 1] = '\0';
1785 source
[totalLength
- 2] = '\0';
1787 #ifdef ENABLE_SHADER_CACHE
1788 GLcharARB
*replacement
;
1790 /* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
1791 * if corresponding entry found from MESA_SHADER_READ_PATH.
1793 dump_shader(sh
->Stage
, source
);
1795 replacement
= read_shader(sh
->Stage
, source
);
1798 source
= replacement
;
1800 #endif /* ENABLE_SHADER_CACHE */
1802 shader_source(sh
, source
);
1808 static ALWAYS_INLINE
void
1809 use_program(GLuint program
, bool no_error
)
1811 GET_CURRENT_CONTEXT(ctx
);
1812 struct gl_shader_program
*shProg
= NULL
;
1814 if (MESA_VERBOSE
& VERBOSE_API
)
1815 _mesa_debug(ctx
, "glUseProgram %u\n", program
);
1819 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1822 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1823 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1824 "glUseProgram(transform feedback active)");
1830 _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1834 if (!shProg
->data
->LinkStatus
) {
1835 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1836 "glUseProgram(program %u not linked)", program
);
1841 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1842 print_shader_info(shProg
);
1847 /* The ARB_separate_shader_object spec says:
1849 * "The executable code for an individual shader stage is taken from
1850 * the current program for that stage. If there is a current program
1851 * object established by UseProgram, that program is considered current
1852 * for all stages. Otherwise, if there is a bound program pipeline
1853 * object (section 2.14.PPO), the program bound to the appropriate
1854 * stage of the pipeline object is considered current."
1857 /* Attach shader state to the binding point */
1858 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1859 /* Update the program */
1860 _mesa_use_shader_program(ctx
, shProg
);
1862 /* Must be done first: detach the progam */
1863 _mesa_use_shader_program(ctx
, shProg
);
1864 /* Unattach shader_state binding point */
1865 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1866 ctx
->Pipeline
.Default
);
1867 /* If a pipeline was bound, rebind it */
1868 if (ctx
->Pipeline
.Current
) {
1870 _mesa_BindProgramPipeline_no_error(ctx
->Pipeline
.Current
->Name
);
1872 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1879 _mesa_UseProgram_no_error(GLuint program
)
1881 use_program(program
, true);
1886 _mesa_UseProgram(GLuint program
)
1888 use_program(program
, false);
1893 _mesa_ValidateProgram(GLuint program
)
1895 GET_CURRENT_CONTEXT(ctx
);
1896 validate_program(ctx
, program
);
1901 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1904 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1905 GLint
* range
, GLint
* precision
)
1907 const struct gl_program_constants
*limits
;
1908 const struct gl_precision
*p
;
1909 GET_CURRENT_CONTEXT(ctx
);
1911 switch (shadertype
) {
1912 case GL_VERTEX_SHADER
:
1913 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1915 case GL_FRAGMENT_SHADER
:
1916 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1919 _mesa_error(ctx
, GL_INVALID_ENUM
,
1920 "glGetShaderPrecisionFormat(shadertype)");
1924 switch (precisiontype
) {
1926 p
= &limits
->LowFloat
;
1928 case GL_MEDIUM_FLOAT
:
1929 p
= &limits
->MediumFloat
;
1932 p
= &limits
->HighFloat
;
1935 p
= &limits
->LowInt
;
1938 p
= &limits
->MediumInt
;
1941 p
= &limits
->HighInt
;
1944 _mesa_error(ctx
, GL_INVALID_ENUM
,
1945 "glGetShaderPrecisionFormat(precisiontype)");
1949 range
[0] = p
->RangeMin
;
1950 range
[1] = p
->RangeMax
;
1951 precision
[0] = p
->Precision
;
1956 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1959 _mesa_ReleaseShaderCompiler(void)
1961 _mesa_destroy_shader_compiler_caches();
1966 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1969 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1970 const void* binary
, GLint length
)
1972 GET_CURRENT_CONTEXT(ctx
);
1974 (void) binaryformat
;
1977 /* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
1978 * page 88 of the OpenGL 4.5 specs state:
1980 * "An INVALID_VALUE error is generated if count or length is negative.
1981 * An INVALID_ENUM error is generated if binaryformat is not a supported
1982 * format returned in SHADER_BINARY_FORMATS."
1984 if (n
< 0 || length
< 0) {
1985 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderBinary(count or length < 0)");
1989 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShaderBinary(format)");
1994 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1995 GLenum
*binaryFormat
, GLvoid
*binary
)
1997 struct gl_shader_program
*shProg
;
1998 GLsizei length_dummy
;
1999 GET_CURRENT_CONTEXT(ctx
);
2002 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
2006 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
2010 /* The ARB_get_program_binary spec says:
2012 * "If <length> is NULL, then no length is returned."
2014 * Ensure that length always points to valid storage to avoid multiple NULL
2015 * pointer checks below.
2018 length
= &length_dummy
;
2021 /* The ARB_get_program_binary spec says:
2023 * "When a program object's LINK_STATUS is FALSE, its program binary
2024 * length is zero, and a call to GetProgramBinary will generate an
2025 * INVALID_OPERATION error.
2027 if (!shProg
->data
->LinkStatus
) {
2028 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2029 "glGetProgramBinary(program %u not linked)",
2036 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2037 "glGetProgramBinary(driver supports zero binary formats)");
2039 (void) binaryFormat
;
2044 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
2045 const GLvoid
*binary
, GLsizei length
)
2047 struct gl_shader_program
*shProg
;
2048 GET_CURRENT_CONTEXT(ctx
);
2050 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
2054 (void) binaryFormat
;
2057 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
2059 * "If a negative number is provided where an argument of type sizei or
2060 * sizeiptr is specified, an INVALID_VALUE error is generated."
2063 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
2067 /* The ARB_get_program_binary spec says:
2069 * "<binaryFormat> and <binary> must be those returned by a previous
2070 * call to GetProgramBinary, and <length> must be the length of the
2071 * program binary as returned by GetProgramBinary or GetProgramiv with
2072 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
2073 * setting the LINK_STATUS of <program> to FALSE, if these conditions
2076 * Since any value of binaryFormat passed "is not one of those specified as
2077 * allowable for [this] command, an INVALID_ENUM error is generated."
2079 shProg
->data
->LinkStatus
= linking_failure
;
2080 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
2085 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
2087 struct gl_shader_program
*shProg
;
2088 GET_CURRENT_CONTEXT(ctx
);
2090 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
2091 "glProgramParameteri");
2096 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
2097 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
2098 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
2099 * even be in the dispatch table, so we shouldn't need to expclicitly
2102 * On desktop, we ignore the 3.0+ requirement because it is silly.
2105 /* The ARB_get_program_binary extension spec says:
2107 * "An INVALID_VALUE error is generated if the <value> argument to
2108 * ProgramParameteri is not TRUE or FALSE."
2110 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2114 /* No need to notify the driver. Any changes will actually take effect
2115 * the next time the shader is linked.
2117 * The ARB_get_program_binary extension spec says:
2119 * "To indicate that a program binary is likely to be retrieved,
2120 * ProgramParameteri should be called with <pname>
2121 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
2122 * will not be in effect until the next time LinkProgram or
2123 * ProgramBinary has been called successfully."
2125 * The resloution of issue 9 in the extension spec also says:
2127 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
2128 * to indicate to the GL implementation that this program will
2129 * likely be saved with GetProgramBinary at some point. This will
2130 * give the GL implementation the opportunity to track any state
2131 * changes made to the program before being saved such that when it
2132 * is loaded again a recompile can be avoided."
2134 shProg
->BinaryRetreivableHint
= value
;
2137 case GL_PROGRAM_SEPARABLE
:
2138 /* Spec imply that the behavior is the same as ARB_get_program_binary
2139 * Chapter 7.3 Program Objects
2141 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
2144 shProg
->SeparateShader
= value
;
2148 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
2149 _mesa_enum_to_string(pname
));
2154 _mesa_error(ctx
, GL_INVALID_VALUE
,
2155 "glProgramParameteri(pname=%s, value=%d): "
2156 "value must be 0 or 1.",
2157 _mesa_enum_to_string(pname
),
2163 _mesa_use_program(struct gl_context
*ctx
, gl_shader_stage stage
,
2164 struct gl_shader_program
*shProg
, struct gl_program
*prog
,
2165 struct gl_pipeline_object
*shTarget
)
2167 struct gl_program
**target
;
2169 target
= &shTarget
->CurrentProgram
[stage
];
2171 _mesa_program_init_subroutine_defaults(ctx
, prog
);
2174 if (*target
!= prog
) {
2175 /* Program is current, flush it */
2176 if (shTarget
== ctx
->_Shader
) {
2177 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
2180 _mesa_reference_shader_program(ctx
,
2181 &shTarget
->ReferencedPrograms
[stage
],
2183 _mesa_reference_program(ctx
, target
, prog
);
2191 * Copy program-specific data generated by linking from the gl_shader_program
2192 * object to the gl_program object referred to by the gl_linked_shader.
2194 * This function expects _mesa_reference_program() to have been previously
2195 * called setting the gl_linked_shaders program reference.
2198 _mesa_copy_linked_program_data(const struct gl_shader_program
*src
,
2199 struct gl_linked_shader
*dst_sh
)
2201 assert(dst_sh
->Program
);
2203 struct gl_program
*dst
= dst_sh
->Program
;
2205 dst
->info
.separate_shader
= src
->SeparateShader
;
2207 switch (dst_sh
->Stage
) {
2208 case MESA_SHADER_GEOMETRY
: {
2209 dst
->info
.gs
.vertices_in
= src
->Geom
.VerticesIn
;
2210 dst
->info
.gs
.uses_end_primitive
= src
->Geom
.UsesEndPrimitive
;
2211 dst
->info
.gs
.uses_streams
= src
->Geom
.UsesStreams
;
2214 case MESA_SHADER_FRAGMENT
: {
2215 dst
->info
.fs
.depth_layout
= src
->FragDepthLayout
;
2218 case MESA_SHADER_COMPUTE
: {
2219 dst
->info
.cs
.shared_size
= src
->Comp
.SharedSize
;
2228 * ARB_separate_shader_objects: Compile & Link Program
2231 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2232 const GLchar
* const *strings
)
2234 GET_CURRENT_CONTEXT(ctx
);
2236 const GLuint shader
= create_shader(ctx
, type
);
2240 * According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
2241 * GL_INVALID_VALUE should be generated if count < 0
2244 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCreateShaderProgram (count < 0)");
2249 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
2251 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
2252 _mesa_compile_shader(ctx
, sh
);
2254 program
= create_shader_program(ctx
);
2256 struct gl_shader_program
*shProg
;
2257 GLint compiled
= GL_FALSE
;
2259 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2261 shProg
->SeparateShader
= GL_TRUE
;
2263 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2265 attach_shader(ctx
, program
, shader
);
2266 _mesa_link_program(ctx
, shProg
);
2267 detach_shader(ctx
, program
, shader
);
2271 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2272 append
-error
-to
-info
-log
;
2273 shProg
->data
->LinkStatus
= linking_failure
;
2278 ralloc_strcat(&shProg
->data
->InfoLog
, sh
->InfoLog
);
2281 delete_shader(ctx
, shader
);
2289 * For GL_ARB_tessellation_shader
2291 extern void GLAPIENTRY
2292 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2294 GET_CURRENT_CONTEXT(ctx
);
2296 if (!_mesa_has_tessellation(ctx
)) {
2297 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2301 if (pname
!= GL_PATCH_VERTICES
) {
2302 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2306 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2307 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2311 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2315 extern void GLAPIENTRY
2316 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2318 GET_CURRENT_CONTEXT(ctx
);
2320 if (!_mesa_has_tessellation(ctx
)) {
2321 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2326 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2327 FLUSH_VERTICES(ctx
, 0);
2328 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2329 4 * sizeof(GLfloat
));
2330 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2332 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2333 FLUSH_VERTICES(ctx
, 0);
2334 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2335 2 * sizeof(GLfloat
));
2336 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");
2345 * ARB_shader_subroutine
2348 _mesa_GetSubroutineUniformLocation(GLuint program
, GLenum shadertype
,
2351 GET_CURRENT_CONTEXT(ctx
);
2352 const char *api_name
= "glGetSubroutineUniformLocation";
2353 struct gl_shader_program
*shProg
;
2354 GLenum resource_type
;
2355 gl_shader_stage stage
;
2357 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2358 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2362 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2366 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2367 if (!shProg
->_LinkedShaders
[stage
]) {
2368 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2372 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2373 return _mesa_program_resource_location(shProg
, resource_type
, name
);
2377 _mesa_GetSubroutineIndex(GLuint program
, GLenum shadertype
,
2380 GET_CURRENT_CONTEXT(ctx
);
2381 const char *api_name
= "glGetSubroutineIndex";
2382 struct gl_shader_program
*shProg
;
2383 struct gl_program_resource
*res
;
2384 GLenum resource_type
;
2385 gl_shader_stage stage
;
2387 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2388 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2392 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2396 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2397 if (!shProg
->_LinkedShaders
[stage
]) {
2398 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2402 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2403 res
= _mesa_program_resource_find_name(shProg
, resource_type
, name
, NULL
);
2408 return _mesa_program_resource_index(shProg
, res
);
2413 _mesa_GetActiveSubroutineUniformiv(GLuint program
, GLenum shadertype
,
2414 GLuint index
, GLenum pname
, GLint
*values
)
2416 GET_CURRENT_CONTEXT(ctx
);
2417 const char *api_name
= "glGetActiveSubroutineUniformiv";
2418 struct gl_shader_program
*shProg
;
2419 struct gl_linked_shader
*sh
;
2420 gl_shader_stage stage
;
2421 struct gl_program_resource
*res
;
2422 const struct gl_uniform_storage
*uni
;
2423 GLenum resource_type
;
2426 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2427 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2431 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2435 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2436 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2438 sh
= shProg
->_LinkedShaders
[stage
];
2440 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2444 struct gl_program
*p
= shProg
->_LinkedShaders
[stage
]->Program
;
2445 if (index
>= p
->sh
.NumSubroutineUniforms
) {
2446 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name
);
2451 case GL_NUM_COMPATIBLE_SUBROUTINES
: {
2452 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2455 values
[0] = uni
->num_compatible_subroutines
;
2459 case GL_COMPATIBLE_SUBROUTINES
: {
2460 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2464 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2465 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2466 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2467 if (fn
->types
[j
] == uni
->type
) {
2468 values
[count
++] = i
;
2476 case GL_UNIFORM_SIZE
:
2477 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2480 values
[0] = uni
->array_elements
? uni
->array_elements
: 1;
2483 case GL_UNIFORM_NAME_LENGTH
:
2484 res
= _mesa_program_resource_find_index(shProg
, resource_type
, index
);
2486 values
[0] = strlen(_mesa_program_resource_name(res
)) + 1
2487 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2491 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2498 _mesa_GetActiveSubroutineUniformName(GLuint program
, GLenum shadertype
,
2499 GLuint index
, GLsizei bufsize
,
2500 GLsizei
*length
, GLchar
*name
)
2502 GET_CURRENT_CONTEXT(ctx
);
2503 const char *api_name
= "glGetActiveSubroutineUniformName";
2504 struct gl_shader_program
*shProg
;
2505 GLenum resource_type
;
2506 gl_shader_stage stage
;
2508 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2509 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2513 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2517 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2518 if (!shProg
->_LinkedShaders
[stage
]) {
2519 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2523 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2524 /* get program resource name */
2525 _mesa_get_program_resource_name(shProg
, resource_type
,
2527 length
, name
, api_name
);
2532 _mesa_GetActiveSubroutineName(GLuint program
, GLenum shadertype
,
2533 GLuint index
, GLsizei bufsize
,
2534 GLsizei
*length
, GLchar
*name
)
2536 GET_CURRENT_CONTEXT(ctx
);
2537 const char *api_name
= "glGetActiveSubroutineName";
2538 struct gl_shader_program
*shProg
;
2539 GLenum resource_type
;
2540 gl_shader_stage stage
;
2542 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2543 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2547 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2551 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2552 if (!shProg
->_LinkedShaders
[stage
]) {
2553 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2556 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2557 _mesa_get_program_resource_name(shProg
, resource_type
,
2559 length
, name
, api_name
);
2563 _mesa_UniformSubroutinesuiv(GLenum shadertype
, GLsizei count
,
2564 const GLuint
*indices
)
2566 GET_CURRENT_CONTEXT(ctx
);
2567 const char *api_name
= "glUniformSubroutinesuiv";
2568 gl_shader_stage stage
;
2571 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2572 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2576 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2577 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2579 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2583 if (count
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2584 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2589 bool flushed
= false;
2591 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2598 _mesa_flush_vertices_for_uniforms(ctx
, uni
);
2602 int uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2605 for (j
= i
; j
< i
+ uni_count
; j
++) {
2606 struct gl_subroutine_function
*subfn
= NULL
;
2607 if (indices
[j
] > p
->sh
.MaxSubroutineFunctionIndex
) {
2608 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2612 for (f
= 0; f
< p
->sh
.NumSubroutineFunctions
; f
++) {
2613 if (p
->sh
.SubroutineFunctions
[f
].index
== indices
[j
])
2614 subfn
= &p
->sh
.SubroutineFunctions
[f
];
2621 for (k
= 0; k
< subfn
->num_compat_types
; k
++) {
2622 if (subfn
->types
[k
] == uni
->type
)
2625 if (k
== subfn
->num_compat_types
) {
2626 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2630 ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[j
] = indices
[j
];
2638 _mesa_GetUniformSubroutineuiv(GLenum shadertype
, GLint location
,
2641 GET_CURRENT_CONTEXT(ctx
);
2642 const char *api_name
= "glGetUniformSubroutineuiv";
2643 gl_shader_stage stage
;
2645 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2646 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2650 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2651 struct gl_program
*p
= ctx
->_Shader
->CurrentProgram
[stage
];
2653 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2657 if (location
>= p
->sh
.NumSubroutineUniformRemapTable
) {
2658 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", api_name
);
2662 *params
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[location
];
2667 _mesa_GetProgramStageiv(GLuint program
, GLenum shadertype
,
2668 GLenum pname
, GLint
*values
)
2670 GET_CURRENT_CONTEXT(ctx
);
2671 const char *api_name
= "glGetProgramStageiv";
2672 struct gl_shader_program
*shProg
;
2673 struct gl_linked_shader
*sh
;
2674 gl_shader_stage stage
;
2676 if (!_mesa_validate_shader_target(ctx
, shadertype
)) {
2677 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2681 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, api_name
);
2685 stage
= _mesa_shader_enum_to_shader_stage(shadertype
);
2686 sh
= shProg
->_LinkedShaders
[stage
];
2688 /* ARB_shader_subroutine doesn't ask the program to be linked, or list any
2689 * INVALID_OPERATION in the case of not be linked.
2691 * And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
2692 * same info using other specs (ARB_program_interface_query), without the
2693 * need of the program to be linked, being the value for that case 0.
2695 * But at the same time, some other methods require the program to be
2696 * linked for pname related to locations, so it would be inconsistent to
2697 * not do the same here. So we are:
2698 * * Return GL_INVALID_OPERATION if not linked only for locations.
2699 * * Setting a default value of 0, to be returned if not linked.
2703 if (pname
== GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
) {
2704 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", api_name
);
2709 struct gl_program
*p
= sh
->Program
;
2711 case GL_ACTIVE_SUBROUTINES
:
2712 values
[0] = p
->sh
.NumSubroutineFunctions
;
2714 case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS
:
2715 values
[0] = p
->sh
.NumSubroutineUniformRemapTable
;
2717 case GL_ACTIVE_SUBROUTINE_UNIFORMS
:
2718 values
[0] = p
->sh
.NumSubroutineUniforms
;
2720 case GL_ACTIVE_SUBROUTINE_MAX_LENGTH
:
2724 GLenum resource_type
;
2725 struct gl_program_resource
*res
;
2727 resource_type
= _mesa_shader_stage_to_subroutine(stage
);
2728 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2729 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2731 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1;
2736 values
[0] = max_len
;
2739 case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH
:
2743 GLenum resource_type
;
2744 struct gl_program_resource
*res
;
2746 resource_type
= _mesa_shader_stage_to_subroutine_uniform(stage
);
2747 for (i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2748 res
= _mesa_program_resource_find_index(shProg
, resource_type
, i
);
2750 const GLint len
= strlen(_mesa_program_resource_name(res
)) + 1
2751 + ((_mesa_program_resource_array_size(res
) != 0) ? 3 : 0);
2757 values
[0] = max_len
;
2761 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", api_name
);
2768 find_compat_subroutine(struct gl_program
*p
, const struct glsl_type
*type
)
2772 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
2773 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
2774 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
2775 if (fn
->types
[j
] == type
)
2783 _mesa_shader_write_subroutine_index(struct gl_context
*ctx
,
2784 struct gl_program
*p
)
2788 if (p
->sh
.NumSubroutineUniformRemapTable
== 0)
2793 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2802 uni_count
= uni
->array_elements
? uni
->array_elements
: 1;
2803 for (j
= 0; j
< uni_count
; j
++) {
2804 val
= ctx
->SubroutineIndex
[p
->info
.stage
].IndexPtr
[i
+ j
];
2805 memcpy(&uni
->storage
[j
], &val
, sizeof(int));
2808 _mesa_propagate_uniforms_to_driver_storage(uni
, 0, uni_count
);
2810 } while(i
< p
->sh
.NumSubroutineUniformRemapTable
);
2814 _mesa_shader_write_subroutine_indices(struct gl_context
*ctx
,
2815 gl_shader_stage stage
)
2817 if (ctx
->_Shader
->CurrentProgram
[stage
])
2818 _mesa_shader_write_subroutine_index(ctx
,
2819 ctx
->_Shader
->CurrentProgram
[stage
]);
2823 _mesa_program_init_subroutine_defaults(struct gl_context
*ctx
,
2824 struct gl_program
*p
)
2828 struct gl_subroutine_index_binding
*binding
= &ctx
->SubroutineIndex
[p
->info
.stage
];
2829 if (binding
->NumIndex
!= p
->sh
.NumSubroutineUniformRemapTable
) {
2830 binding
->IndexPtr
= realloc(binding
->IndexPtr
,
2831 p
->sh
.NumSubroutineUniformRemapTable
* (sizeof(GLuint
)));
2832 binding
->NumIndex
= p
->sh
.NumSubroutineUniformRemapTable
;
2835 for (int i
= 0; i
< p
->sh
.NumSubroutineUniformRemapTable
; i
++) {
2836 struct gl_uniform_storage
*uni
= p
->sh
.SubroutineUniformRemapTable
[i
];
2841 binding
->IndexPtr
[i
] = find_compat_subroutine(p
, uni
->type
);