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
40 #include "main/glheader.h"
41 #include "main/context.h"
42 #include "main/dispatch.h"
43 #include "main/enums.h"
44 #include "main/hash.h"
45 #include "main/mtypes.h"
46 #include "main/pipelineobj.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/transformfeedback.h"
50 #include "main/uniforms.h"
51 #include "program/program.h"
52 #include "program/prog_print.h"
53 #include "program/prog_parameter.h"
54 #include "util/ralloc.h"
55 #include "util/hash_table.h"
57 #include "../glsl/glsl_parser_extras.h"
58 #include "../glsl/ir.h"
59 #include "../glsl/ir_uniform.h"
60 #include "../glsl/program.h"
62 /** Define this to enable shader substitution (see below) */
63 #define SHADER_SUBST 0
67 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
70 _mesa_get_shader_flags(void)
72 GLbitfield flags
= 0x0;
73 const char *env
= getenv("MESA_GLSL");
76 if (strstr(env
, "dump_on_error"))
77 flags
|= GLSL_DUMP_ON_ERROR
;
78 else if (strstr(env
, "dump"))
80 if (strstr(env
, "log"))
82 if (strstr(env
, "nopvert"))
83 flags
|= GLSL_NOP_VERT
;
84 if (strstr(env
, "nopfrag"))
85 flags
|= GLSL_NOP_FRAG
;
86 if (strstr(env
, "nopt"))
88 else if (strstr(env
, "opt"))
90 if (strstr(env
, "uniform"))
91 flags
|= GLSL_UNIFORMS
;
92 if (strstr(env
, "useprog"))
93 flags
|= GLSL_USE_PROG
;
94 if (strstr(env
, "errors"))
95 flags
|= GLSL_REPORT_ERRORS
;
103 * Initialize context's shader state.
106 _mesa_init_shader_state(struct gl_context
*ctx
)
108 /* Device drivers may override these to control what kind of instructions
109 * are generated by the GLSL compiler.
111 struct gl_shader_compiler_options options
;
115 memset(&options
, 0, sizeof(options
));
116 options
.MaxUnrollIterations
= 32;
117 options
.MaxIfDepth
= UINT_MAX
;
119 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
120 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
122 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
124 if (ctx
->Shader
.Flags
!= 0)
125 ctx
->Const
.GenerateTemporaryNames
= true;
127 /* Extended for ARB_separate_shader_objects */
128 ctx
->Shader
.RefCount
= 1;
129 mtx_init(&ctx
->Shader
.Mutex
, mtx_plain
);
131 ctx
->TessCtrlProgram
.patch_vertices
= 3;
132 for (i
= 0; i
< 4; ++i
)
133 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
134 for (i
= 0; i
< 2; ++i
)
135 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
140 * Free the per-context shader-related state.
143 _mesa_free_shader_state(struct gl_context
*ctx
)
146 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
147 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
150 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
152 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
154 /* Extended for ARB_separate_shader_objects */
155 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
157 assert(ctx
->Shader
.RefCount
== 1);
158 mtx_destroy(&ctx
->Shader
.Mutex
);
163 * Copy string from <src> to <dst>, up to maxLength characters, returning
164 * length of <dst> in <length>.
165 * \param src the strings source
166 * \param maxLength max chars to copy
167 * \param length returns number of chars copied
168 * \param dst the string destination
171 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
172 GLsizei
*length
, const GLchar
*src
)
175 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
186 * Confirm that the a shader type is valid and supported by the implementation
188 * \param ctx Current GL context
189 * \param type Shader target
193 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
195 /* Note: when building built-in GLSL functions, this function may be
196 * invoked with ctx == NULL. In that case, we can only validate that it's
197 * a shader target we recognize, not that it's supported in the current
198 * context. But that's fine--we don't need any further validation than
199 * that when building built-in GLSL functions.
203 case GL_FRAGMENT_SHADER
:
204 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
205 case GL_VERTEX_SHADER
:
206 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
207 case GL_GEOMETRY_SHADER_ARB
:
208 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
209 case GL_TESS_CONTROL_SHADER
:
210 case GL_TESS_EVALUATION_SHADER
:
211 return ctx
== NULL
|| _mesa_has_tessellation(ctx
);
212 case GL_COMPUTE_SHADER
:
213 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
221 is_program(struct gl_context
*ctx
, GLuint name
)
223 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
224 return shProg
? GL_TRUE
: GL_FALSE
;
229 is_shader(struct gl_context
*ctx
, GLuint name
)
231 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
232 return shader
? GL_TRUE
: GL_FALSE
;
237 * Attach shader to a shader program.
240 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
242 struct gl_shader_program
*shProg
;
243 struct gl_shader
*sh
;
246 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
248 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
252 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
257 n
= shProg
->NumShaders
;
258 for (i
= 0; i
< n
; i
++) {
259 if (shProg
->Shaders
[i
] == sh
) {
260 /* The shader is already attched to this program. The
261 * GL_ARB_shader_objects spec says:
263 * "The error INVALID_OPERATION is generated by AttachObjectARB
264 * if <obj> is already attached to <containerObj>."
266 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
268 } else if (same_type_disallowed
&&
269 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
270 /* Shader with the same type is already attached to this program,
271 * OpenGL ES 2.0 and 3.0 specs say:
273 * "Multiple shader objects of the same type may not be attached
274 * to a single program object. [...] The error INVALID_OPERATION
275 * is generated if [...] another shader object of the same type
276 * as shader is already attached to program."
278 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
284 shProg
->Shaders
= realloc(shProg
->Shaders
,
285 (n
+ 1) * sizeof(struct gl_shader
*));
286 if (!shProg
->Shaders
) {
287 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
292 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
293 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
294 shProg
->NumShaders
++;
299 create_shader(struct gl_context
*ctx
, GLenum type
)
301 struct gl_shader
*sh
;
304 if (!_mesa_validate_shader_target(ctx
, type
)) {
305 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
309 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
310 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
311 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
318 create_shader_program(struct gl_context
*ctx
)
321 struct gl_shader_program
*shProg
;
323 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
325 shProg
= ctx
->Driver
.NewShaderProgram(name
);
327 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
329 assert(shProg
->RefCount
== 1);
336 * Delete a shader program. Actually, just decrement the program's
337 * reference count and mark it as DeletePending.
338 * Used to implement glDeleteProgram() and glDeleteObjectARB().
341 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
344 * NOTE: deleting shaders/programs works a bit differently than
345 * texture objects (and buffer objects, etc). Shader/program
346 * handles/IDs exist in the hash table until the object is really
347 * deleted (refcount==0). With texture objects, the handle/ID is
348 * removed from the hash table in glDeleteTextures() while the tex
349 * object itself might linger until its refcount goes to zero.
351 struct gl_shader_program
*shProg
;
353 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
357 if (!shProg
->DeletePending
) {
358 shProg
->DeletePending
= GL_TRUE
;
360 /* effectively, decr shProg's refcount */
361 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
367 delete_shader(struct gl_context
*ctx
, GLuint shader
)
369 struct gl_shader
*sh
;
371 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
375 if (!sh
->DeletePending
) {
376 sh
->DeletePending
= GL_TRUE
;
378 /* effectively, decr sh's refcount */
379 _mesa_reference_shader(ctx
, &sh
, NULL
);
385 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
387 struct gl_shader_program
*shProg
;
391 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
395 n
= shProg
->NumShaders
;
397 for (i
= 0; i
< n
; i
++) {
398 if (shProg
->Shaders
[i
]->Name
== shader
) {
400 struct gl_shader
**newList
;
403 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
405 /* alloc new, smaller array */
406 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
408 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
411 /* Copy old list entries to new list, skipping removed entry at [i] */
412 for (j
= 0; j
< i
; j
++) {
413 newList
[j
] = shProg
->Shaders
[j
];
416 newList
[j
++] = shProg
->Shaders
[i
];
419 /* Free old list and install new one */
420 free(shProg
->Shaders
);
421 shProg
->Shaders
= newList
;
422 shProg
->NumShaders
= n
- 1;
425 /* sanity check - make sure the new list's entries are sensible */
426 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
427 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
428 shProg
->Shaders
[j
]->Type
== GL_TESS_CONTROL_SHADER
||
429 shProg
->Shaders
[j
]->Type
== GL_TESS_EVALUATION_SHADER
||
430 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
431 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
432 assert(shProg
->Shaders
[j
]->RefCount
> 0);
443 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
444 err
= GL_INVALID_OPERATION
;
446 err
= GL_INVALID_VALUE
;
447 _mesa_error(ctx
, err
, "glDetachShader(shader)");
454 * Return list of shaders attached to shader program.
457 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
458 GLsizei
*count
, GLuint
*obj
)
460 struct gl_shader_program
*shProg
;
463 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
468 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
472 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
473 obj
[i
] = shProg
->Shaders
[i
]->Name
;
482 * glGetHandleARB() - return ID/name of currently bound shader program.
485 get_handle(struct gl_context
*ctx
, GLenum pname
)
487 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
488 if (ctx
->_Shader
->ActiveProgram
)
489 return ctx
->_Shader
->ActiveProgram
->Name
;
494 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
501 * Check if a geometry shader query is valid at this time. If not, report an
502 * error and return false.
504 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
506 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
507 * are queried for a program which has not been linked successfully, or
508 * which does not contain objects to form a geometry shader, then an
509 * INVALID_OPERATION error is generated."
512 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
514 if (shProg
->LinkStatus
&&
515 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
520 "glGetProgramv(linked geometry shader required)");
526 * Check if a tessellation control shader query is valid at this time.
527 * If not, report an error and return false.
529 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
531 * "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
532 * not been linked successfully, or which does not contain objects to
533 * form a tessellation control shader, then an INVALID_OPERATION error is
537 check_tcs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
539 if (shProg
->LinkStatus
&&
540 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] != NULL
) {
544 _mesa_error(ctx
, GL_INVALID_OPERATION
,
545 "glGetProgramv(linked tessellation control shader required)");
551 * Check if a tessellation evaluation shader query is valid at this time.
552 * If not, report an error and return false.
554 * From GL 4.0 section 6.1.12 (Shader and Program Queries):
556 * "If any of the pname values in this paragraph are queried for a program
557 * which has not been linked successfully, or which does not contain
558 * objects to form a tessellation evaluation shader, then an
559 * INVALID_OPERATION error is generated."
563 check_tes_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
565 if (shProg
->LinkStatus
&&
566 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
] != NULL
) {
570 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramv(linked tessellation "
571 "evaluation shader required)");
577 * glGetProgramiv() - get shader program state.
578 * Note that this is for GLSL shader programs, not ARB vertex/fragment
579 * programs (see glGetProgramivARB).
582 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
585 struct gl_shader_program
*shProg
586 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
588 /* Is transform feedback available in this context?
591 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
592 || ctx
->API
== API_OPENGL_CORE
593 || _mesa_is_gles3(ctx
);
595 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
596 * and GL 3.2) are available in this context
598 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
599 const bool has_tess
= _mesa_has_tessellation(ctx
);
601 /* Are uniform buffer objects available in this context?
604 (ctx
->API
== API_OPENGL_COMPAT
&&
605 ctx
->Extensions
.ARB_uniform_buffer_object
)
606 || ctx
->API
== API_OPENGL_CORE
607 || _mesa_is_gles3(ctx
);
614 case GL_DELETE_STATUS
:
615 *params
= shProg
->DeletePending
;
618 *params
= shProg
->LinkStatus
;
620 case GL_VALIDATE_STATUS
:
621 *params
= shProg
->Validated
;
623 case GL_INFO_LOG_LENGTH
:
624 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
626 case GL_ATTACHED_SHADERS
:
627 *params
= shProg
->NumShaders
;
629 case GL_ACTIVE_ATTRIBUTES
:
630 *params
= _mesa_count_active_attribs(shProg
);
632 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
633 *params
= _mesa_longest_attribute_name_length(shProg
);
635 case GL_ACTIVE_UNIFORMS
:
636 *params
= shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
638 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
641 const unsigned num_uniforms
=
642 shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
644 for (i
= 0; i
< num_uniforms
; i
++) {
645 /* Add one for the terminating NUL character for a non-array, and
646 * 4 for the "[0]" and the NUL for an array.
648 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
649 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
658 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
661 *params
= shProg
->TransformFeedback
.NumVarying
;
663 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
669 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
670 /* Add one for the terminating NUL character.
673 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
682 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
685 *params
= shProg
->TransformFeedback
.BufferMode
;
687 case GL_GEOMETRY_VERTICES_OUT
:
690 if (check_gs_query(ctx
, shProg
))
691 *params
= shProg
->Geom
.VerticesOut
;
693 case GL_GEOMETRY_SHADER_INVOCATIONS
:
694 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
696 if (check_gs_query(ctx
, shProg
))
697 *params
= shProg
->Geom
.Invocations
;
699 case GL_GEOMETRY_INPUT_TYPE
:
702 if (check_gs_query(ctx
, shProg
))
703 *params
= shProg
->Geom
.InputType
;
705 case GL_GEOMETRY_OUTPUT_TYPE
:
708 if (check_gs_query(ctx
, shProg
))
709 *params
= shProg
->Geom
.OutputType
;
711 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
718 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
719 /* Add one for the terminating NUL character.
721 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
730 case GL_ACTIVE_UNIFORM_BLOCKS
:
734 *params
= shProg
->NumUniformBlocks
;
736 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
737 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
738 * only available with desktop OpenGL 3.0+ with the
739 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
741 * On desktop, we ignore the 3.0+ requirement because it is silly.
743 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
746 *params
= shProg
->BinaryRetreivableHint
;
748 case GL_PROGRAM_BINARY_LENGTH
:
751 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
752 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
755 *params
= shProg
->NumAtomicBuffers
;
757 case GL_COMPUTE_WORK_GROUP_SIZE
: {
759 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
761 if (!shProg
->LinkStatus
) {
762 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
766 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
767 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
771 for (i
= 0; i
< 3; i
++)
772 params
[i
] = shProg
->Comp
.LocalSize
[i
];
775 case GL_PROGRAM_SEPARABLE
:
776 *params
= shProg
->SeparateShader
;
779 /* ARB_tessellation_shader */
780 case GL_TESS_CONTROL_OUTPUT_VERTICES
:
783 if (check_tcs_query(ctx
, shProg
))
784 *params
= shProg
->TessCtrl
.VerticesOut
;
786 case GL_TESS_GEN_MODE
:
789 if (check_tes_query(ctx
, shProg
))
790 *params
= shProg
->TessEval
.PrimitiveMode
;
792 case GL_TESS_GEN_SPACING
:
795 if (check_tes_query(ctx
, shProg
))
796 *params
= shProg
->TessEval
.Spacing
;
798 case GL_TESS_GEN_VERTEX_ORDER
:
801 if (check_tes_query(ctx
, shProg
))
802 *params
= shProg
->TessEval
.VertexOrder
;
804 case GL_TESS_GEN_POINT_MODE
:
807 if (check_tes_query(ctx
, shProg
))
808 *params
= shProg
->TessEval
.PointMode
;
814 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
815 _mesa_enum_to_string(pname
));
820 * glGetShaderiv() - get GLSL shader state
823 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
825 struct gl_shader
*shader
=
826 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
834 *params
= shader
->Type
;
836 case GL_DELETE_STATUS
:
837 *params
= shader
->DeletePending
;
839 case GL_COMPILE_STATUS
:
840 *params
= shader
->CompileStatus
;
842 case GL_INFO_LOG_LENGTH
:
843 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
845 case GL_SHADER_SOURCE_LENGTH
:
846 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
849 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
856 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
857 GLsizei
*length
, GLchar
*infoLog
)
859 struct gl_shader_program
*shProg
;
861 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
862 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
864 * "If a negative number is provided where an argument of type sizei or
865 * sizeiptr is specified, an INVALID_VALUE error is generated."
868 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
872 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
873 "glGetProgramInfoLog(program)");
878 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
883 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
884 GLsizei
*length
, GLchar
*infoLog
)
886 struct gl_shader
*sh
;
888 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
889 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
891 * "If a negative number is provided where an argument of type sizei or
892 * sizeiptr is specified, an INVALID_VALUE error is generated."
895 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
899 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
904 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
909 * Return shader source code.
912 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
913 GLsizei
*length
, GLchar
*sourceOut
)
915 struct gl_shader
*sh
;
918 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
922 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
926 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
931 * Set/replace shader source code. A helper function used by
932 * glShaderSource[ARB].
935 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
937 struct gl_shader
*sh
;
939 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
943 /* free old shader source string and install new one */
944 free((void *)sh
->Source
);
946 sh
->CompileStatus
= GL_FALSE
;
948 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
957 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
959 struct gl_shader
*sh
;
961 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
966 /* If the user called glCompileShader without first calling
967 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
969 sh
->CompileStatus
= GL_FALSE
;
971 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
972 _mesa_log("GLSL source for %s shader %d:\n",
973 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
974 _mesa_log("%s\n", sh
->Source
);
977 /* this call will set the shader->CompileStatus field to indicate if
978 * compilation was successful.
980 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
982 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
983 _mesa_write_shader_to_file(sh
);
986 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
987 if (sh
->CompileStatus
) {
988 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
989 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
992 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
994 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
995 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
996 _mesa_log("%s\n", sh
->InfoLog
);
1001 if (!sh
->CompileStatus
) {
1002 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
1003 _mesa_log("GLSL source for %s shader %d:\n",
1004 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
1005 _mesa_log("%s\n", sh
->Source
);
1006 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
1009 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
1010 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
1011 sh
->Name
, sh
->InfoLog
);
1018 * Link a program's shaders.
1021 link_program(struct gl_context
*ctx
, GLuint program
)
1023 struct gl_shader_program
*shProg
;
1025 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
1029 /* From the ARB_transform_feedback2 specification:
1030 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
1031 * the name of a program being used by one or more transform feedback
1032 * objects, even if the objects are not currently bound or are paused."
1034 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
1035 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1036 "glLinkProgram(transform feedback is using the program)");
1040 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1042 _mesa_glsl_link_shader(ctx
, shProg
);
1044 if (shProg
->LinkStatus
== GL_FALSE
&&
1045 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
1046 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
1047 shProg
->Name
, shProg
->InfoLog
);
1054 printf("Link %u shaders in program %u: %s\n",
1055 shProg
->NumShaders
, shProg
->Name
,
1056 shProg
->LinkStatus
? "Success" : "Failed");
1058 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1059 printf(" shader %u, type 0x%x\n",
1060 shProg
->Shaders
[i
]->Name
,
1061 shProg
->Shaders
[i
]->Type
);
1068 * Print basic shader info (for debug).
1071 print_shader_info(const struct gl_shader_program
*shProg
)
1075 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
1076 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1077 printf(" %s shader %u, checksum %u\n",
1078 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
1079 shProg
->Shaders
[i
]->Name
,
1080 shProg
->Shaders
[i
]->SourceChecksum
);
1082 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
1083 printf(" vert prog %u\n",
1084 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1085 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1086 printf(" frag prog %u\n",
1087 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1088 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1089 printf(" geom prog %u\n",
1090 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1091 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
])
1092 printf(" tesc prog %u\n",
1093 shProg
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
->Id
);
1094 if (shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
])
1095 printf(" tese prog %u\n",
1096 shProg
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
]->Program
->Id
);
1101 * Use the named shader program for subsequent glUniform calls
1104 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1107 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
1108 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1109 "%s(program %u not linked)", caller
, shProg
->Name
);
1113 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1114 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1120 use_shader_program(struct gl_context
*ctx
, gl_shader_stage stage
,
1121 struct gl_shader_program
*shProg
,
1122 struct gl_pipeline_object
*shTarget
)
1124 struct gl_shader_program
**target
;
1126 target
= &shTarget
->CurrentProgram
[stage
];
1127 if ((shProg
!= NULL
) && (shProg
->_LinkedShaders
[stage
] == NULL
))
1130 if (*target
!= shProg
) {
1131 /* Program is current, flush it */
1132 if (shTarget
== ctx
->_Shader
) {
1133 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1136 /* If the shader is also bound as the current rendering shader, unbind
1137 * it from that binding point as well. This ensures that the correct
1138 * semantics of glDeleteProgram are maintained.
1141 case MESA_SHADER_VERTEX
:
1142 case MESA_SHADER_TESS_CTRL
:
1143 case MESA_SHADER_TESS_EVAL
:
1144 case MESA_SHADER_GEOMETRY
:
1145 case MESA_SHADER_COMPUTE
:
1146 /* Empty for now. */
1148 case MESA_SHADER_FRAGMENT
:
1149 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1150 _mesa_reference_shader_program(ctx
,
1151 &ctx
->_Shader
->_CurrentFragmentProgram
,
1157 _mesa_reference_shader_program(ctx
, target
, shProg
);
1164 * Use the named shader program for subsequent rendering.
1167 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1170 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
1171 use_shader_program(ctx
, i
, shProg
, &ctx
->Shader
);
1172 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1174 if (ctx
->Driver
.UseProgram
)
1175 ctx
->Driver
.UseProgram(ctx
, shProg
);
1180 * Do validation of the given shader program.
1181 * \param errMsg returns error message if validation fails.
1182 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1185 validate_shader_program(const struct gl_shader_program
*shProg
,
1188 if (!shProg
->LinkStatus
) {
1192 /* From the GL spec, a program is invalid if any of these are true:
1194 any two active samplers in the current program object are of
1195 different types, but refer to the same texture image unit,
1197 any active sampler in the current program object refers to a texture
1198 image unit where fixed-function fragment processing accesses a
1199 texture target that does not match the sampler type, or
1201 the sum of the number of active samplers in the program and the
1202 number of texture image units enabled for fixed-function fragment
1203 processing exceeds the combined limit on the total number of texture
1204 image units allowed.
1208 * Check: any two active samplers in the current program object are of
1209 * different types, but refer to the same texture image unit,
1211 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1219 * Called via glValidateProgram()
1222 validate_program(struct gl_context
*ctx
, GLuint program
)
1224 struct gl_shader_program
*shProg
;
1225 char errMsg
[100] = "";
1227 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1232 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1233 if (!shProg
->Validated
) {
1234 /* update info log */
1235 if (shProg
->InfoLog
) {
1236 ralloc_free(shProg
->InfoLog
);
1238 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1245 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1247 GET_CURRENT_CONTEXT(ctx
);
1248 attach_shader(ctx
, program
, shader
);
1253 _mesa_AttachShader(GLuint program
, GLuint shader
)
1255 GET_CURRENT_CONTEXT(ctx
);
1256 attach_shader(ctx
, program
, shader
);
1261 _mesa_CompileShader(GLhandleARB shaderObj
)
1263 GET_CURRENT_CONTEXT(ctx
);
1264 if (MESA_VERBOSE
& VERBOSE_API
)
1265 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1266 compile_shader(ctx
, shaderObj
);
1271 _mesa_CreateShader(GLenum type
)
1273 GET_CURRENT_CONTEXT(ctx
);
1274 if (MESA_VERBOSE
& VERBOSE_API
)
1275 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1276 return create_shader(ctx
, type
);
1280 GLhandleARB GLAPIENTRY
1281 _mesa_CreateShaderObjectARB(GLenum type
)
1283 GET_CURRENT_CONTEXT(ctx
);
1284 return create_shader(ctx
, type
);
1289 _mesa_CreateProgram(void)
1291 GET_CURRENT_CONTEXT(ctx
);
1292 if (MESA_VERBOSE
& VERBOSE_API
)
1293 _mesa_debug(ctx
, "glCreateProgram\n");
1294 return create_shader_program(ctx
);
1298 GLhandleARB GLAPIENTRY
1299 _mesa_CreateProgramObjectARB(void)
1301 GET_CURRENT_CONTEXT(ctx
);
1302 return create_shader_program(ctx
);
1307 _mesa_DeleteObjectARB(GLhandleARB obj
)
1309 if (MESA_VERBOSE
& VERBOSE_API
) {
1310 GET_CURRENT_CONTEXT(ctx
);
1311 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1315 GET_CURRENT_CONTEXT(ctx
);
1316 FLUSH_VERTICES(ctx
, 0);
1317 if (is_program(ctx
, obj
)) {
1318 delete_shader_program(ctx
, obj
);
1320 else if (is_shader(ctx
, obj
)) {
1321 delete_shader(ctx
, obj
);
1331 _mesa_DeleteProgram(GLuint name
)
1334 GET_CURRENT_CONTEXT(ctx
);
1335 FLUSH_VERTICES(ctx
, 0);
1336 delete_shader_program(ctx
, name
);
1342 _mesa_DeleteShader(GLuint name
)
1345 GET_CURRENT_CONTEXT(ctx
);
1346 FLUSH_VERTICES(ctx
, 0);
1347 delete_shader(ctx
, name
);
1353 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1355 GET_CURRENT_CONTEXT(ctx
);
1356 detach_shader(ctx
, program
, shader
);
1361 _mesa_DetachShader(GLuint program
, GLuint shader
)
1363 GET_CURRENT_CONTEXT(ctx
);
1364 detach_shader(ctx
, program
, shader
);
1369 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1370 GLsizei
* count
, GLhandleARB
* obj
)
1372 GET_CURRENT_CONTEXT(ctx
);
1373 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1378 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1379 GLsizei
*count
, GLuint
*obj
)
1381 GET_CURRENT_CONTEXT(ctx
);
1382 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1387 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1388 GLcharARB
* infoLog
)
1390 GET_CURRENT_CONTEXT(ctx
);
1391 if (is_program(ctx
, object
)) {
1392 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1394 else if (is_shader(ctx
, object
)) {
1395 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1398 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1404 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1406 GET_CURRENT_CONTEXT(ctx
);
1407 /* Implement in terms of GetProgramiv, GetShaderiv */
1408 if (is_program(ctx
, object
)) {
1409 if (pname
== GL_OBJECT_TYPE_ARB
) {
1410 *params
= GL_PROGRAM_OBJECT_ARB
;
1413 get_programiv(ctx
, object
, pname
, params
);
1416 else if (is_shader(ctx
, object
)) {
1417 if (pname
== GL_OBJECT_TYPE_ARB
) {
1418 *params
= GL_SHADER_OBJECT_ARB
;
1421 get_shaderiv(ctx
, object
, pname
, params
);
1425 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1431 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1434 GLint iparams
[1]; /* XXX is one element enough? */
1435 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1436 params
[0] = (GLfloat
) iparams
[0];
1441 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1443 GET_CURRENT_CONTEXT(ctx
);
1444 get_programiv(ctx
, program
, pname
, params
);
1449 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1451 GET_CURRENT_CONTEXT(ctx
);
1452 get_shaderiv(ctx
, shader
, pname
, params
);
1457 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1458 GLsizei
*length
, GLchar
*infoLog
)
1460 GET_CURRENT_CONTEXT(ctx
);
1461 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1466 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1467 GLsizei
*length
, GLchar
*infoLog
)
1469 GET_CURRENT_CONTEXT(ctx
);
1470 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1475 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1476 GLsizei
*length
, GLcharARB
*sourceOut
)
1478 GET_CURRENT_CONTEXT(ctx
);
1479 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1483 GLhandleARB GLAPIENTRY
1484 _mesa_GetHandleARB(GLenum pname
)
1486 GET_CURRENT_CONTEXT(ctx
);
1487 return get_handle(ctx
, pname
);
1491 GLboolean GLAPIENTRY
1492 _mesa_IsProgram(GLuint name
)
1494 GET_CURRENT_CONTEXT(ctx
);
1495 return is_program(ctx
, name
);
1499 GLboolean GLAPIENTRY
1500 _mesa_IsShader(GLuint name
)
1502 GET_CURRENT_CONTEXT(ctx
);
1503 return is_shader(ctx
, name
);
1508 _mesa_LinkProgram(GLhandleARB programObj
)
1510 GET_CURRENT_CONTEXT(ctx
);
1511 link_program(ctx
, programObj
);
1517 * Read shader source code from a file.
1518 * Useful for debugging to override an app's shader.
1521 read_shader(const char *fname
)
1523 int shader_size
= 0;
1524 FILE *f
= fopen(fname
, "r");
1525 GLcharARB
*buffer
, *shader
;
1532 /* allocate enough room for the entire shader */
1533 fseek(f
, 0, SEEK_END
);
1534 shader_size
= ftell(f
);
1536 assert(shader_size
);
1538 /* add one for terminating zero */
1541 buffer
= malloc(shader_size
);
1544 len
= fread(buffer
, 1, shader_size
, f
);
1549 shader
= strdup(buffer
);
1557 * Called via glShaderSource() and glShaderSourceARB() API functions.
1558 * Basically, concatenate the source code strings into one long string
1559 * and pass it to _mesa_shader_source().
1562 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1563 const GLcharARB
* const * string
, const GLint
* length
)
1565 GET_CURRENT_CONTEXT(ctx
);
1567 GLsizei i
, totalLength
;
1571 if (!shaderObj
|| string
== NULL
) {
1572 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1577 * This array holds offsets of where the appropriate string ends, thus the
1578 * last element will be set to the total length of the source code.
1580 offsets
= malloc(count
* sizeof(GLint
));
1581 if (offsets
== NULL
) {
1582 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1586 for (i
= 0; i
< count
; i
++) {
1587 if (string
[i
] == NULL
) {
1588 free((GLvoid
*) offsets
);
1589 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1590 "glShaderSourceARB(null string)");
1593 if (length
== NULL
|| length
[i
] < 0)
1594 offsets
[i
] = strlen(string
[i
]);
1596 offsets
[i
] = length
[i
];
1597 /* accumulate string lengths */
1599 offsets
[i
] += offsets
[i
- 1];
1602 /* Total length of source string is sum off all strings plus two.
1603 * One extra byte for terminating zero, another extra byte to silence
1604 * valgrind warnings in the parser/grammer code.
1606 totalLength
= offsets
[count
- 1] + 2;
1607 source
= malloc(totalLength
* sizeof(GLcharARB
));
1608 if (source
== NULL
) {
1609 free((GLvoid
*) offsets
);
1610 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1614 for (i
= 0; i
< count
; i
++) {
1615 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1616 memcpy(source
+ start
, string
[i
],
1617 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1619 source
[totalLength
- 1] = '\0';
1620 source
[totalLength
- 2] = '\0';
1623 /* Compute the shader's source code checksum then try to open a file
1624 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1625 * original shader source code. For debugging.
1628 GLcharARB
*newSource
;
1630 checksum
= _mesa_str_checksum(source
);
1632 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1634 newSource
= read_shader(filename
);
1636 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1637 shaderObj
, checksum
, filename
);
1643 shader_source(ctx
, shaderObj
, source
);
1646 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1648 sh
->SourceChecksum
= checksum
; /* save original checksum */
1656 _mesa_UseProgram(GLhandleARB program
)
1658 GET_CURRENT_CONTEXT(ctx
);
1659 struct gl_shader_program
*shProg
;
1661 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1662 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1663 "glUseProgram(transform feedback active)");
1668 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1672 if (!shProg
->LinkStatus
) {
1673 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1674 "glUseProgram(program %u not linked)", program
);
1679 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1680 print_shader_info(shProg
);
1687 /* The ARB_separate_shader_object spec says:
1689 * "The executable code for an individual shader stage is taken from
1690 * the current program for that stage. If there is a current program
1691 * object established by UseProgram, that program is considered current
1692 * for all stages. Otherwise, if there is a bound program pipeline
1693 * object (section 2.14.PPO), the program bound to the appropriate
1694 * stage of the pipeline object is considered current."
1697 /* Attach shader state to the binding point */
1698 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1699 /* Update the program */
1700 _mesa_use_program(ctx
, shProg
);
1702 /* Must be done first: detach the progam */
1703 _mesa_use_program(ctx
, shProg
);
1704 /* Unattach shader_state binding point */
1705 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1706 /* If a pipeline was bound, rebind it */
1707 if (ctx
->Pipeline
.Current
) {
1708 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1715 _mesa_ValidateProgram(GLhandleARB program
)
1717 GET_CURRENT_CONTEXT(ctx
);
1718 validate_program(ctx
, program
);
1723 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1726 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1727 GLint
* range
, GLint
* precision
)
1729 const struct gl_program_constants
*limits
;
1730 const struct gl_precision
*p
;
1731 GET_CURRENT_CONTEXT(ctx
);
1733 switch (shadertype
) {
1734 case GL_VERTEX_SHADER
:
1735 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1737 case GL_FRAGMENT_SHADER
:
1738 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1741 _mesa_error(ctx
, GL_INVALID_ENUM
,
1742 "glGetShaderPrecisionFormat(shadertype)");
1746 switch (precisiontype
) {
1748 p
= &limits
->LowFloat
;
1750 case GL_MEDIUM_FLOAT
:
1751 p
= &limits
->MediumFloat
;
1754 p
= &limits
->HighFloat
;
1757 p
= &limits
->LowInt
;
1760 p
= &limits
->MediumInt
;
1763 p
= &limits
->HighInt
;
1766 _mesa_error(ctx
, GL_INVALID_ENUM
,
1767 "glGetShaderPrecisionFormat(precisiontype)");
1771 range
[0] = p
->RangeMin
;
1772 range
[1] = p
->RangeMax
;
1773 precision
[0] = p
->Precision
;
1778 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1781 _mesa_ReleaseShaderCompiler(void)
1783 _mesa_destroy_shader_compiler_caches();
1788 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1791 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1792 const void* binary
, GLint length
)
1794 GET_CURRENT_CONTEXT(ctx
);
1797 (void) binaryformat
;
1800 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary");
1805 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1806 GLenum
*binaryFormat
, GLvoid
*binary
)
1808 struct gl_shader_program
*shProg
;
1809 GLsizei length_dummy
;
1810 GET_CURRENT_CONTEXT(ctx
);
1813 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1817 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1821 /* The ARB_get_program_binary spec says:
1823 * "If <length> is NULL, then no length is returned."
1825 * Ensure that length always points to valid storage to avoid multiple NULL
1826 * pointer checks below.
1829 length
= &length_dummy
;
1832 /* The ARB_get_program_binary spec says:
1834 * "When a program object's LINK_STATUS is FALSE, its program binary
1835 * length is zero, and a call to GetProgramBinary will generate an
1836 * INVALID_OPERATION error.
1838 if (!shProg
->LinkStatus
) {
1839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1840 "glGetProgramBinary(program %u not linked)",
1847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1848 "glGetProgramBinary(driver supports zero binary formats)");
1850 (void) binaryFormat
;
1855 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1856 const GLvoid
*binary
, GLsizei length
)
1858 struct gl_shader_program
*shProg
;
1859 GET_CURRENT_CONTEXT(ctx
);
1861 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1865 (void) binaryFormat
;
1868 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
1870 * "If a negative number is provided where an argument of type sizei or
1871 * sizeiptr is specified, an INVALID_VALUE error is generated."
1874 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
1878 /* The ARB_get_program_binary spec says:
1880 * "<binaryFormat> and <binary> must be those returned by a previous
1881 * call to GetProgramBinary, and <length> must be the length of the
1882 * program binary as returned by GetProgramBinary or GetProgramiv with
1883 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
1884 * setting the LINK_STATUS of <program> to FALSE, if these conditions
1887 * Since any value of binaryFormat passed "is not one of those specified as
1888 * allowable for [this] command, an INVALID_ENUM error is generated."
1890 shProg
->LinkStatus
= GL_FALSE
;
1891 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
1896 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1898 struct gl_shader_program
*shProg
;
1899 GET_CURRENT_CONTEXT(ctx
);
1901 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1902 "glProgramParameteri");
1907 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1908 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1909 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1910 * even be in the dispatch table, so we shouldn't need to expclicitly
1913 * On desktop, we ignore the 3.0+ requirement because it is silly.
1916 /* The ARB_get_program_binary extension spec says:
1918 * "An INVALID_VALUE error is generated if the <value> argument to
1919 * ProgramParameteri is not TRUE or FALSE."
1921 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1925 /* No need to notify the driver. Any changes will actually take effect
1926 * the next time the shader is linked.
1928 * The ARB_get_program_binary extension spec says:
1930 * "To indicate that a program binary is likely to be retrieved,
1931 * ProgramParameteri should be called with <pname>
1932 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1933 * will not be in effect until the next time LinkProgram or
1934 * ProgramBinary has been called successfully."
1936 * The resloution of issue 9 in the extension spec also says:
1938 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1939 * to indicate to the GL implementation that this program will
1940 * likely be saved with GetProgramBinary at some point. This will
1941 * give the GL implementation the opportunity to track any state
1942 * changes made to the program before being saved such that when it
1943 * is loaded again a recompile can be avoided."
1945 shProg
->BinaryRetreivableHint
= value
;
1948 case GL_PROGRAM_SEPARABLE
:
1949 /* Spec imply that the behavior is the same as ARB_get_program_binary
1950 * Chapter 7.3 Program Objects
1952 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1955 shProg
->SeparateShader
= value
;
1959 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1960 _mesa_enum_to_string(pname
));
1965 _mesa_error(ctx
, GL_INVALID_VALUE
,
1966 "glProgramParameteri(pname=%s, value=%d): "
1967 "value must be 0 or 1.",
1968 _mesa_enum_to_string(pname
),
1974 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1975 struct gl_shader_program
*shProg
,
1976 struct gl_pipeline_object
*shTarget
)
1978 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
1979 use_shader_program(ctx
, stage
, shProg
, shTarget
);
1981 if (ctx
->Driver
.UseProgram
)
1982 ctx
->Driver
.UseProgram(ctx
, shProg
);
1987 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1988 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1990 const GLuint shader
= create_shader(ctx
, type
);
1994 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1996 compile_shader(ctx
, shader
);
1998 program
= create_shader_program(ctx
);
2000 struct gl_shader_program
*shProg
;
2001 struct gl_shader
*sh
;
2002 GLint compiled
= GL_FALSE
;
2004 shProg
= _mesa_lookup_shader_program(ctx
, program
);
2005 sh
= _mesa_lookup_shader(ctx
, shader
);
2007 shProg
->SeparateShader
= separate
;
2009 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
2011 attach_shader(ctx
, program
, shader
);
2012 link_program(ctx
, program
);
2013 detach_shader(ctx
, program
, shader
);
2017 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
2018 append
-error
-to
-info
-log
;
2019 shProg
->LinkStatus
= GL_FALSE
;
2024 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
2027 delete_shader(ctx
, shader
);
2035 * Copy program-specific data generated by linking from the gl_shader_program
2036 * object to a specific gl_program object.
2039 _mesa_copy_linked_program_data(gl_shader_stage type
,
2040 const struct gl_shader_program
*src
,
2041 struct gl_program
*dst
)
2044 case MESA_SHADER_VERTEX
:
2045 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
2047 case MESA_SHADER_TESS_CTRL
: {
2048 struct gl_tess_ctrl_program
*dst_tcp
=
2049 (struct gl_tess_ctrl_program
*) dst
;
2050 dst_tcp
->VerticesOut
= src
->TessCtrl
.VerticesOut
;
2053 case MESA_SHADER_TESS_EVAL
: {
2054 struct gl_tess_eval_program
*dst_tep
=
2055 (struct gl_tess_eval_program
*) dst
;
2056 dst_tep
->PrimitiveMode
= src
->TessEval
.PrimitiveMode
;
2057 dst_tep
->Spacing
= src
->TessEval
.Spacing
;
2058 dst_tep
->VertexOrder
= src
->TessEval
.VertexOrder
;
2059 dst_tep
->PointMode
= src
->TessEval
.PointMode
;
2060 dst
->UsesClipDistanceOut
= src
->TessEval
.UsesClipDistance
;
2063 case MESA_SHADER_GEOMETRY
: {
2064 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
2065 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
2066 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
2067 dst_gp
->Invocations
= src
->Geom
.Invocations
;
2068 dst_gp
->InputType
= src
->Geom
.InputType
;
2069 dst_gp
->OutputType
= src
->Geom
.OutputType
;
2070 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
2071 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
2072 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
2075 case MESA_SHADER_FRAGMENT
: {
2076 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
2077 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
2080 case MESA_SHADER_COMPUTE
: {
2081 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
2083 for (i
= 0; i
< 3; i
++)
2084 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
2093 * ARB_separate_shader_objects: Compile & Link Program
2096 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2097 const GLchar
* const *strings
)
2099 GET_CURRENT_CONTEXT(ctx
);
2101 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);
2106 * For GL_ARB_tessellation_shader
2108 extern void GLAPIENTRY
2109 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2111 GET_CURRENT_CONTEXT(ctx
);
2113 if (!_mesa_has_tessellation(ctx
)) {
2114 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2118 if (pname
!= GL_PATCH_VERTICES
) {
2119 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2123 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2124 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2128 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2132 extern void GLAPIENTRY
2133 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2135 GET_CURRENT_CONTEXT(ctx
);
2137 if (!_mesa_has_tessellation(ctx
)) {
2138 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2143 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2144 FLUSH_VERTICES(ctx
, 0);
2145 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2146 4 * sizeof(GLfloat
));
2147 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2149 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2150 FLUSH_VERTICES(ctx
, 0);
2151 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2152 2 * sizeof(GLfloat
));
2153 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2156 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");