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
;
114 memset(&options
, 0, sizeof(options
));
115 options
.MaxUnrollIterations
= 32;
116 options
.MaxIfDepth
= UINT_MAX
;
118 /* Default pragma settings */
119 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
121 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
122 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
124 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
126 if (ctx
->Shader
.Flags
!= 0)
127 ctx
->Const
.GenerateTemporaryNames
= true;
129 /* Extended for ARB_separate_shader_objects */
130 ctx
->Shader
.RefCount
= 1;
131 mtx_init(&ctx
->Shader
.Mutex
, mtx_plain
);
136 * Free the per-context shader-related state.
139 _mesa_free_shader_state(struct gl_context
*ctx
)
142 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
143 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
146 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
148 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
150 /* Extended for ARB_separate_shader_objects */
151 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
153 assert(ctx
->Shader
.RefCount
== 1);
154 mtx_destroy(&ctx
->Shader
.Mutex
);
159 * Copy string from <src> to <dst>, up to maxLength characters, returning
160 * length of <dst> in <length>.
161 * \param src the strings source
162 * \param maxLength max chars to copy
163 * \param length returns number of chars copied
164 * \param dst the string destination
167 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
168 GLsizei
*length
, const GLchar
*src
)
171 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
182 * Confirm that the a shader type is valid and supported by the implementation
184 * \param ctx Current GL context
185 * \param type Shader target
189 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
191 /* Note: when building built-in GLSL functions, this function may be
192 * invoked with ctx == NULL. In that case, we can only validate that it's
193 * a shader target we recognize, not that it's supported in the current
194 * context. But that's fine--we don't need any further validation than
195 * that when building built-in GLSL functions.
199 case GL_FRAGMENT_SHADER
:
200 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
201 case GL_VERTEX_SHADER
:
202 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
203 case GL_GEOMETRY_SHADER_ARB
:
204 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
205 case GL_COMPUTE_SHADER
:
206 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
214 is_program(struct gl_context
*ctx
, GLuint name
)
216 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
217 return shProg
? GL_TRUE
: GL_FALSE
;
222 is_shader(struct gl_context
*ctx
, GLuint name
)
224 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
225 return shader
? GL_TRUE
: GL_FALSE
;
230 * Attach shader to a shader program.
233 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
235 struct gl_shader_program
*shProg
;
236 struct gl_shader
*sh
;
239 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
241 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
245 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
250 n
= shProg
->NumShaders
;
251 for (i
= 0; i
< n
; i
++) {
252 if (shProg
->Shaders
[i
] == sh
) {
253 /* The shader is already attched to this program. The
254 * GL_ARB_shader_objects spec says:
256 * "The error INVALID_OPERATION is generated by AttachObjectARB
257 * if <obj> is already attached to <containerObj>."
259 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
261 } else if (same_type_disallowed
&&
262 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
263 /* Shader with the same type is already attached to this program,
264 * OpenGL ES 2.0 and 3.0 specs say:
266 * "Multiple shader objects of the same type may not be attached
267 * to a single program object. [...] The error INVALID_OPERATION
268 * is generated if [...] another shader object of the same type
269 * as shader is already attached to program."
271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
277 shProg
->Shaders
= (struct gl_shader
**)
278 realloc(shProg
->Shaders
,
279 (n
+ 1) * sizeof(struct gl_shader
*));
280 if (!shProg
->Shaders
) {
281 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
286 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
287 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
288 shProg
->NumShaders
++;
293 create_shader(struct gl_context
*ctx
, GLenum type
)
295 struct gl_shader
*sh
;
298 if (!_mesa_validate_shader_target(ctx
, type
)) {
299 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
303 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
304 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
305 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
312 create_shader_program(struct gl_context
*ctx
)
315 struct gl_shader_program
*shProg
;
317 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
319 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
321 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
323 assert(shProg
->RefCount
== 1);
330 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
334 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
337 * NOTE: deleting shaders/programs works a bit differently than
338 * texture objects (and buffer objects, etc). Shader/program
339 * handles/IDs exist in the hash table until the object is really
340 * deleted (refcount==0). With texture objects, the handle/ID is
341 * removed from the hash table in glDeleteTextures() while the tex
342 * object itself might linger until its refcount goes to zero.
344 struct gl_shader_program
*shProg
;
346 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
350 if (!shProg
->DeletePending
) {
351 shProg
->DeletePending
= GL_TRUE
;
353 /* effectively, decr shProg's refcount */
354 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
360 delete_shader(struct gl_context
*ctx
, GLuint shader
)
362 struct gl_shader
*sh
;
364 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
368 if (!sh
->DeletePending
) {
369 sh
->DeletePending
= GL_TRUE
;
371 /* effectively, decr sh's refcount */
372 _mesa_reference_shader(ctx
, &sh
, NULL
);
378 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
380 struct gl_shader_program
*shProg
;
384 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
388 n
= shProg
->NumShaders
;
390 for (i
= 0; i
< n
; i
++) {
391 if (shProg
->Shaders
[i
]->Name
== shader
) {
393 struct gl_shader
**newList
;
396 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
398 /* alloc new, smaller array */
399 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
401 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
404 /* Copy old list entries to new list, skipping removed entry at [i] */
405 for (j
= 0; j
< i
; j
++) {
406 newList
[j
] = shProg
->Shaders
[j
];
409 newList
[j
++] = shProg
->Shaders
[i
];
412 /* Free old list and install new one */
413 free(shProg
->Shaders
);
414 shProg
->Shaders
= newList
;
415 shProg
->NumShaders
= n
- 1;
418 /* sanity check - make sure the new list's entries are sensible */
419 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
420 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
421 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
422 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
423 assert(shProg
->Shaders
[j
]->RefCount
> 0);
434 if (is_shader(ctx
, shader
))
435 err
= GL_INVALID_OPERATION
;
436 else if (is_program(ctx
, shader
))
437 err
= GL_INVALID_OPERATION
;
439 err
= GL_INVALID_VALUE
;
440 _mesa_error(ctx
, err
, "glDetachShader(shader)");
447 * Return list of shaders attached to shader program.
450 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
451 GLsizei
*count
, GLuint
*obj
)
453 struct gl_shader_program
*shProg
=
454 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
457 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
458 obj
[i
] = shProg
->Shaders
[i
]->Name
;
467 * glGetHandleARB() - return ID/name of currently bound shader program.
470 get_handle(struct gl_context
*ctx
, GLenum pname
)
472 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
473 if (ctx
->_Shader
->ActiveProgram
)
474 return ctx
->_Shader
->ActiveProgram
->Name
;
479 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
486 * Check if a geometry shader query is valid at this time. If not, report an
487 * error and return false.
489 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
491 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
492 * are queried for a program which has not been linked successfully, or
493 * which does not contain objects to form a geometry shader, then an
494 * INVALID_OPERATION error is generated."
497 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
499 if (shProg
->LinkStatus
&&
500 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
505 "glGetProgramv(linked geometry shader required)");
511 * glGetProgramiv() - get shader program state.
512 * Note that this is for GLSL shader programs, not ARB vertex/fragment
513 * programs (see glGetProgramivARB).
516 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
518 struct gl_shader_program
*shProg
519 = _mesa_lookup_shader_program(ctx
, program
);
521 /* Is transform feedback available in this context?
524 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
525 || ctx
->API
== API_OPENGL_CORE
526 || _mesa_is_gles3(ctx
);
528 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
529 * and GL 3.2) are available in this context
531 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
533 /* Are uniform buffer objects available in this context?
536 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
537 || ctx
->API
== API_OPENGL_CORE
538 || _mesa_is_gles3(ctx
);
541 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
546 case GL_DELETE_STATUS
:
547 *params
= shProg
->DeletePending
;
550 *params
= shProg
->LinkStatus
;
552 case GL_VALIDATE_STATUS
:
553 *params
= shProg
->Validated
;
555 case GL_INFO_LOG_LENGTH
:
556 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
558 case GL_ATTACHED_SHADERS
:
559 *params
= shProg
->NumShaders
;
561 case GL_ACTIVE_ATTRIBUTES
:
562 *params
= _mesa_count_active_attribs(shProg
);
564 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
565 *params
= _mesa_longest_attribute_name_length(shProg
);
567 case GL_ACTIVE_UNIFORMS
:
568 *params
= shProg
->NumUserUniformStorage
;
570 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
574 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
575 /* Add one for the terminating NUL character for a non-array, and
576 * 4 for the "[0]" and the NUL for an array.
578 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
579 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
588 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
591 *params
= shProg
->TransformFeedback
.NumVarying
;
593 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
599 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
600 /* Add one for the terminating NUL character.
602 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
611 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
614 *params
= shProg
->TransformFeedback
.BufferMode
;
616 case GL_GEOMETRY_VERTICES_OUT
:
619 if (check_gs_query(ctx
, shProg
))
620 *params
= shProg
->Geom
.VerticesOut
;
622 case GL_GEOMETRY_SHADER_INVOCATIONS
:
623 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
625 if (check_gs_query(ctx
, shProg
))
626 *params
= shProg
->Geom
.Invocations
;
628 case GL_GEOMETRY_INPUT_TYPE
:
631 if (check_gs_query(ctx
, shProg
))
632 *params
= shProg
->Geom
.InputType
;
634 case GL_GEOMETRY_OUTPUT_TYPE
:
637 if (check_gs_query(ctx
, shProg
))
638 *params
= shProg
->Geom
.OutputType
;
640 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
647 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
648 /* Add one for the terminating NUL character.
650 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
659 case GL_ACTIVE_UNIFORM_BLOCKS
:
663 *params
= shProg
->NumUniformBlocks
;
665 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
666 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
667 * only available with desktop OpenGL 3.0+ with the
668 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
670 * On desktop, we ignore the 3.0+ requirement because it is silly.
672 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
675 *params
= shProg
->BinaryRetreivableHint
;
677 case GL_PROGRAM_BINARY_LENGTH
:
680 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
681 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
684 *params
= shProg
->NumAtomicBuffers
;
686 case GL_COMPUTE_WORK_GROUP_SIZE
: {
688 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
690 if (!shProg
->LinkStatus
) {
691 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
695 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
696 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
700 for (i
= 0; i
< 3; i
++)
701 params
[i
] = shProg
->Comp
.LocalSize
[i
];
704 case GL_PROGRAM_SEPARABLE
:
705 *params
= shProg
->SeparateShader
;
711 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
712 _mesa_lookup_enum_by_nr(pname
));
717 * glGetShaderiv() - get GLSL shader state
720 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
722 struct gl_shader
*shader
=
723 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
731 *params
= shader
->Type
;
733 case GL_DELETE_STATUS
:
734 *params
= shader
->DeletePending
;
736 case GL_COMPILE_STATUS
:
737 *params
= shader
->CompileStatus
;
739 case GL_INFO_LOG_LENGTH
:
740 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
742 case GL_SHADER_SOURCE_LENGTH
:
743 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
746 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
753 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
754 GLsizei
*length
, GLchar
*infoLog
)
756 struct gl_shader_program
*shProg
757 = _mesa_lookup_shader_program(ctx
, program
);
759 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
762 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
767 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
768 GLsizei
*length
, GLchar
*infoLog
)
770 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
772 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
775 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
780 * Return shader source code.
783 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
784 GLsizei
*length
, GLchar
*sourceOut
)
786 struct gl_shader
*sh
;
787 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
791 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
796 * Set/replace shader source code. A helper function used by
797 * glShaderSource[ARB].
800 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
802 struct gl_shader
*sh
;
804 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
808 /* free old shader source string and install new one */
809 free((void *)sh
->Source
);
811 sh
->CompileStatus
= GL_FALSE
;
813 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
822 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
824 struct gl_shader
*sh
;
825 struct gl_shader_compiler_options
*options
;
827 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
831 options
= &ctx
->Const
.ShaderCompilerOptions
[sh
->Stage
];
833 /* set default pragma state for shader */
834 sh
->Pragmas
= options
->DefaultPragmas
;
837 /* If the user called glCompileShader without first calling
838 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
840 sh
->CompileStatus
= GL_FALSE
;
842 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
843 fprintf(stderr
, "GLSL source for %s shader %d:\n",
844 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
845 fprintf(stderr
, "%s\n", sh
->Source
);
849 /* this call will set the shader->CompileStatus field to indicate if
850 * compilation was successful.
852 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
854 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
855 _mesa_write_shader_to_file(sh
);
858 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
859 if (sh
->CompileStatus
) {
860 fprintf(stderr
, "GLSL IR for shader %d:\n", sh
->Name
);
861 _mesa_print_ir(stderr
, sh
->ir
, NULL
);
862 fprintf(stderr
, "\n\n");
864 fprintf(stderr
, "GLSL shader %d failed to compile.\n", sh
->Name
);
866 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
867 fprintf(stderr
, "GLSL shader %d info log:\n", sh
->Name
);
868 fprintf(stderr
, "%s\n", sh
->InfoLog
);
875 if (!sh
->CompileStatus
) {
876 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
877 fprintf(stderr
, "GLSL source for %s shader %d:\n",
878 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
879 fprintf(stderr
, "%s\n", sh
->Source
);
880 fprintf(stderr
, "Info Log:\n%s\n", sh
->InfoLog
);
884 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
885 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
886 sh
->Name
, sh
->InfoLog
);
893 * Link a program's shaders.
896 link_program(struct gl_context
*ctx
, GLuint program
)
898 struct gl_shader_program
*shProg
;
900 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
904 /* From the ARB_transform_feedback2 specification:
905 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
906 * the name of a program being used by one or more transform feedback
907 * objects, even if the objects are not currently bound or are paused."
909 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
910 _mesa_error(ctx
, GL_INVALID_OPERATION
,
911 "glLinkProgram(transform feedback is using the program)");
915 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
917 _mesa_glsl_link_shader(ctx
, shProg
);
919 if (shProg
->LinkStatus
== GL_FALSE
&&
920 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
921 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
922 shProg
->Name
, shProg
->InfoLog
);
929 printf("Link %u shaders in program %u: %s\n",
930 shProg
->NumShaders
, shProg
->Name
,
931 shProg
->LinkStatus
? "Success" : "Failed");
933 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
934 printf(" shader %u, type 0x%x\n",
935 shProg
->Shaders
[i
]->Name
,
936 shProg
->Shaders
[i
]->Type
);
943 * Print basic shader info (for debug).
946 print_shader_info(const struct gl_shader_program
*shProg
)
950 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
951 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
952 printf(" %s shader %u, checksum %u\n",
953 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
954 shProg
->Shaders
[i
]->Name
,
955 shProg
->Shaders
[i
]->SourceChecksum
);
957 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
958 printf(" vert prog %u\n",
959 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
960 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
961 printf(" frag prog %u\n",
962 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
963 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
964 printf(" geom prog %u\n",
965 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
970 * Use the named shader program for subsequent glUniform calls
973 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
976 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
977 _mesa_error(ctx
, GL_INVALID_OPERATION
,
978 "%s(program %u not linked)", caller
, shProg
->Name
);
982 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
983 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
990 use_shader_program(struct gl_context
*ctx
, GLenum type
,
991 struct gl_shader_program
*shProg
,
992 struct gl_pipeline_object
*shTarget
)
994 struct gl_shader_program
**target
;
995 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
997 target
= &shTarget
->CurrentProgram
[stage
];
998 if ((shProg
== NULL
) || (shProg
->_LinkedShaders
[stage
] == NULL
))
1001 if (*target
!= shProg
) {
1002 /* Program is current, flush it */
1003 if (shTarget
== ctx
->_Shader
) {
1004 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1007 /* If the shader is also bound as the current rendering shader, unbind
1008 * it from that binding point as well. This ensures that the correct
1009 * semantics of glDeleteProgram are maintained.
1012 case GL_VERTEX_SHADER
:
1013 /* Empty for now. */
1015 case GL_GEOMETRY_SHADER_ARB
:
1016 /* Empty for now. */
1018 case GL_COMPUTE_SHADER
:
1019 /* Empty for now. */
1021 case GL_FRAGMENT_SHADER
:
1022 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1023 _mesa_reference_shader_program(ctx
,
1024 &ctx
->_Shader
->_CurrentFragmentProgram
,
1030 _mesa_reference_shader_program(ctx
, target
, shProg
);
1036 * Use the named shader program for subsequent rendering.
1039 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1041 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
, &ctx
->Shader
);
1042 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
, &ctx
->Shader
);
1043 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
, &ctx
->Shader
);
1044 use_shader_program(ctx
, GL_COMPUTE_SHADER
, shProg
, &ctx
->Shader
);
1045 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1047 if (ctx
->Driver
.UseProgram
)
1048 ctx
->Driver
.UseProgram(ctx
, shProg
);
1053 * Do validation of the given shader program.
1054 * \param errMsg returns error message if validation fails.
1055 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1058 validate_shader_program(const struct gl_shader_program
*shProg
,
1061 if (!shProg
->LinkStatus
) {
1065 /* From the GL spec, a program is invalid if any of these are true:
1067 any two active samplers in the current program object are of
1068 different types, but refer to the same texture image unit,
1070 any active sampler in the current program object refers to a texture
1071 image unit where fixed-function fragment processing accesses a
1072 texture target that does not match the sampler type, or
1074 the sum of the number of active samplers in the program and the
1075 number of texture image units enabled for fixed-function fragment
1076 processing exceeds the combined limit on the total number of texture
1077 image units allowed.
1082 * Check: any two active samplers in the current program object are of
1083 * different types, but refer to the same texture image unit,
1085 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1093 * Called via glValidateProgram()
1096 validate_program(struct gl_context
*ctx
, GLuint program
)
1098 struct gl_shader_program
*shProg
;
1099 char errMsg
[100] = "";
1101 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1106 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1107 if (!shProg
->Validated
) {
1108 /* update info log */
1109 if (shProg
->InfoLog
) {
1110 ralloc_free(shProg
->InfoLog
);
1112 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1119 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1121 GET_CURRENT_CONTEXT(ctx
);
1122 attach_shader(ctx
, program
, shader
);
1127 _mesa_AttachShader(GLuint program
, GLuint shader
)
1129 GET_CURRENT_CONTEXT(ctx
);
1130 attach_shader(ctx
, program
, shader
);
1135 _mesa_CompileShader(GLhandleARB shaderObj
)
1137 GET_CURRENT_CONTEXT(ctx
);
1138 if (MESA_VERBOSE
& VERBOSE_API
)
1139 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1140 compile_shader(ctx
, shaderObj
);
1145 _mesa_CreateShader(GLenum type
)
1147 GET_CURRENT_CONTEXT(ctx
);
1148 if (MESA_VERBOSE
& VERBOSE_API
)
1149 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1150 return create_shader(ctx
, type
);
1154 GLhandleARB GLAPIENTRY
1155 _mesa_CreateShaderObjectARB(GLenum type
)
1157 GET_CURRENT_CONTEXT(ctx
);
1158 return create_shader(ctx
, type
);
1163 _mesa_CreateProgram(void)
1165 GET_CURRENT_CONTEXT(ctx
);
1166 if (MESA_VERBOSE
& VERBOSE_API
)
1167 _mesa_debug(ctx
, "glCreateProgram\n");
1168 return create_shader_program(ctx
);
1172 GLhandleARB GLAPIENTRY
1173 _mesa_CreateProgramObjectARB(void)
1175 GET_CURRENT_CONTEXT(ctx
);
1176 return create_shader_program(ctx
);
1181 _mesa_DeleteObjectARB(GLhandleARB obj
)
1183 if (MESA_VERBOSE
& VERBOSE_API
) {
1184 GET_CURRENT_CONTEXT(ctx
);
1185 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1189 GET_CURRENT_CONTEXT(ctx
);
1190 FLUSH_VERTICES(ctx
, 0);
1191 if (is_program(ctx
, obj
)) {
1192 delete_shader_program(ctx
, obj
);
1194 else if (is_shader(ctx
, obj
)) {
1195 delete_shader(ctx
, obj
);
1205 _mesa_DeleteProgram(GLuint name
)
1208 GET_CURRENT_CONTEXT(ctx
);
1209 FLUSH_VERTICES(ctx
, 0);
1210 delete_shader_program(ctx
, name
);
1216 _mesa_DeleteShader(GLuint name
)
1219 GET_CURRENT_CONTEXT(ctx
);
1220 FLUSH_VERTICES(ctx
, 0);
1221 delete_shader(ctx
, name
);
1227 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1229 GET_CURRENT_CONTEXT(ctx
);
1230 detach_shader(ctx
, program
, shader
);
1235 _mesa_DetachShader(GLuint program
, GLuint shader
)
1237 GET_CURRENT_CONTEXT(ctx
);
1238 detach_shader(ctx
, program
, shader
);
1243 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1244 GLsizei
* count
, GLhandleARB
* obj
)
1246 GET_CURRENT_CONTEXT(ctx
);
1247 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1252 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1253 GLsizei
*count
, GLuint
*obj
)
1255 GET_CURRENT_CONTEXT(ctx
);
1256 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1261 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1262 GLcharARB
* infoLog
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 if (is_program(ctx
, object
)) {
1266 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1268 else if (is_shader(ctx
, object
)) {
1269 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1272 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1278 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1280 GET_CURRENT_CONTEXT(ctx
);
1281 /* Implement in terms of GetProgramiv, GetShaderiv */
1282 if (is_program(ctx
, object
)) {
1283 if (pname
== GL_OBJECT_TYPE_ARB
) {
1284 *params
= GL_PROGRAM_OBJECT_ARB
;
1287 get_programiv(ctx
, object
, pname
, params
);
1290 else if (is_shader(ctx
, object
)) {
1291 if (pname
== GL_OBJECT_TYPE_ARB
) {
1292 *params
= GL_SHADER_OBJECT_ARB
;
1295 get_shaderiv(ctx
, object
, pname
, params
);
1299 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1305 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1308 GLint iparams
[1]; /* XXX is one element enough? */
1309 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1310 params
[0] = (GLfloat
) iparams
[0];
1315 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1317 GET_CURRENT_CONTEXT(ctx
);
1318 get_programiv(ctx
, program
, pname
, params
);
1323 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1325 GET_CURRENT_CONTEXT(ctx
);
1326 get_shaderiv(ctx
, shader
, pname
, params
);
1331 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1332 GLsizei
*length
, GLchar
*infoLog
)
1334 GET_CURRENT_CONTEXT(ctx
);
1335 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1340 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1341 GLsizei
*length
, GLchar
*infoLog
)
1343 GET_CURRENT_CONTEXT(ctx
);
1344 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1349 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1350 GLsizei
*length
, GLcharARB
*sourceOut
)
1352 GET_CURRENT_CONTEXT(ctx
);
1353 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1357 GLhandleARB GLAPIENTRY
1358 _mesa_GetHandleARB(GLenum pname
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 return get_handle(ctx
, pname
);
1365 GLboolean GLAPIENTRY
1366 _mesa_IsProgram(GLuint name
)
1368 GET_CURRENT_CONTEXT(ctx
);
1369 return is_program(ctx
, name
);
1373 GLboolean GLAPIENTRY
1374 _mesa_IsShader(GLuint name
)
1376 GET_CURRENT_CONTEXT(ctx
);
1377 return is_shader(ctx
, name
);
1382 _mesa_LinkProgram(GLhandleARB programObj
)
1384 GET_CURRENT_CONTEXT(ctx
);
1385 link_program(ctx
, programObj
);
1391 * Read shader source code from a file.
1392 * Useful for debugging to override an app's shader.
1395 read_shader(const char *fname
)
1397 int shader_size
= 0;
1398 FILE *f
= fopen(fname
, "r");
1399 GLcharARB
*buffer
, *shader
;
1406 /* allocate enough room for the entire shader */
1407 fseek(f
, 0, SEEK_END
);
1408 shader_size
= ftell(f
);
1410 assert(shader_size
);
1412 /* add one for terminating zero */
1415 buffer
= malloc(shader_size
);
1418 len
= fread(buffer
, 1, shader_size
, f
);
1423 shader
= _mesa_strdup(buffer
);
1431 * Called via glShaderSource() and glShaderSourceARB() API functions.
1432 * Basically, concatenate the source code strings into one long string
1433 * and pass it to _mesa_shader_source().
1436 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1437 const GLcharARB
* const * string
, const GLint
* length
)
1439 GET_CURRENT_CONTEXT(ctx
);
1441 GLsizei i
, totalLength
;
1445 if (!shaderObj
|| string
== NULL
) {
1446 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1451 * This array holds offsets of where the appropriate string ends, thus the
1452 * last element will be set to the total length of the source code.
1454 offsets
= malloc(count
* sizeof(GLint
));
1455 if (offsets
== NULL
) {
1456 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1460 for (i
= 0; i
< count
; i
++) {
1461 if (string
[i
] == NULL
) {
1462 free((GLvoid
*) offsets
);
1463 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1464 "glShaderSourceARB(null string)");
1467 if (length
== NULL
|| length
[i
] < 0)
1468 offsets
[i
] = strlen(string
[i
]);
1470 offsets
[i
] = length
[i
];
1471 /* accumulate string lengths */
1473 offsets
[i
] += offsets
[i
- 1];
1476 /* Total length of source string is sum off all strings plus two.
1477 * One extra byte for terminating zero, another extra byte to silence
1478 * valgrind warnings in the parser/grammer code.
1480 totalLength
= offsets
[count
- 1] + 2;
1481 source
= malloc(totalLength
* sizeof(GLcharARB
));
1482 if (source
== NULL
) {
1483 free((GLvoid
*) offsets
);
1484 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1488 for (i
= 0; i
< count
; i
++) {
1489 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1490 memcpy(source
+ start
, string
[i
],
1491 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1493 source
[totalLength
- 1] = '\0';
1494 source
[totalLength
- 2] = '\0';
1497 /* Compute the shader's source code checksum then try to open a file
1498 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1499 * original shader source code. For debugging.
1502 GLcharARB
*newSource
;
1504 checksum
= _mesa_str_checksum(source
);
1506 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1508 newSource
= read_shader(filename
);
1510 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1511 shaderObj
, checksum
, filename
);
1517 shader_source(ctx
, shaderObj
, source
);
1520 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1522 sh
->SourceChecksum
= checksum
; /* save original checksum */
1530 _mesa_UseProgram(GLhandleARB program
)
1532 GET_CURRENT_CONTEXT(ctx
);
1533 struct gl_shader_program
*shProg
;
1535 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1536 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1537 "glUseProgram(transform feedback active)");
1542 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1546 if (!shProg
->LinkStatus
) {
1547 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1548 "glUseProgram(program %u not linked)", program
);
1553 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1554 print_shader_info(shProg
);
1561 /* The ARB_separate_shader_object spec says:
1563 * "The executable code for an individual shader stage is taken from
1564 * the current program for that stage. If there is a current program
1565 * object established by UseProgram, that program is considered current
1566 * for all stages. Otherwise, if there is a bound program pipeline
1567 * object (section 2.14.PPO), the program bound to the appropriate
1568 * stage of the pipeline object is considered current."
1571 /* Attach shader state to the binding point */
1572 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1573 /* Update the program */
1574 _mesa_use_program(ctx
, shProg
);
1576 /* Must be done first: detach the progam */
1577 _mesa_use_program(ctx
, shProg
);
1578 /* Unattach shader_state binding point */
1579 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1580 /* If a pipeline was bound, rebind it */
1581 if (ctx
->Pipeline
.Current
) {
1582 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1589 _mesa_ValidateProgram(GLhandleARB program
)
1591 GET_CURRENT_CONTEXT(ctx
);
1592 validate_program(ctx
, program
);
1597 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1600 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1601 GLint
* range
, GLint
* precision
)
1603 const struct gl_program_constants
*limits
;
1604 const struct gl_precision
*p
;
1605 GET_CURRENT_CONTEXT(ctx
);
1607 switch (shadertype
) {
1608 case GL_VERTEX_SHADER
:
1609 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1611 case GL_FRAGMENT_SHADER
:
1612 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1615 _mesa_error(ctx
, GL_INVALID_ENUM
,
1616 "glGetShaderPrecisionFormat(shadertype)");
1620 switch (precisiontype
) {
1622 p
= &limits
->LowFloat
;
1624 case GL_MEDIUM_FLOAT
:
1625 p
= &limits
->MediumFloat
;
1628 p
= &limits
->HighFloat
;
1631 p
= &limits
->LowInt
;
1634 p
= &limits
->MediumInt
;
1637 p
= &limits
->HighInt
;
1640 _mesa_error(ctx
, GL_INVALID_ENUM
,
1641 "glGetShaderPrecisionFormat(precisiontype)");
1645 range
[0] = p
->RangeMin
;
1646 range
[1] = p
->RangeMax
;
1647 precision
[0] = p
->Precision
;
1652 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1655 _mesa_ReleaseShaderCompiler(void)
1657 _mesa_destroy_shader_compiler_caches();
1662 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1665 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1666 const void* binary
, GLint length
)
1668 GET_CURRENT_CONTEXT(ctx
);
1671 (void) binaryformat
;
1674 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1679 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1680 GLenum
*binaryFormat
, GLvoid
*binary
)
1682 struct gl_shader_program
*shProg
;
1683 GET_CURRENT_CONTEXT(ctx
);
1685 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1689 if (!shProg
->LinkStatus
) {
1690 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1691 "glGetProgramBinary(program %u not linked)",
1697 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1701 /* The ARB_get_program_binary spec says:
1703 * "If <length> is NULL, then no length is returned."
1708 (void) binaryFormat
;
1713 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1714 const GLvoid
*binary
, GLsizei length
)
1716 struct gl_shader_program
*shProg
;
1717 GET_CURRENT_CONTEXT(ctx
);
1719 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1723 (void) binaryFormat
;
1726 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1731 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1733 struct gl_shader_program
*shProg
;
1734 GET_CURRENT_CONTEXT(ctx
);
1736 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1737 "glProgramParameteri");
1742 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1743 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1744 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1745 * even be in the dispatch table, so we shouldn't need to expclicitly
1748 * On desktop, we ignore the 3.0+ requirement because it is silly.
1751 /* The ARB_get_program_binary extension spec says:
1753 * "An INVALID_VALUE error is generated if the <value> argument to
1754 * ProgramParameteri is not TRUE or FALSE."
1756 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1757 _mesa_error(ctx
, GL_INVALID_VALUE
,
1758 "glProgramParameteri(pname=%s, value=%d): "
1759 "value must be 0 or 1.",
1760 _mesa_lookup_enum_by_nr(pname
),
1765 /* No need to notify the driver. Any changes will actually take effect
1766 * the next time the shader is linked.
1768 * The ARB_get_program_binary extension spec says:
1770 * "To indicate that a program binary is likely to be retrieved,
1771 * ProgramParameteri should be called with <pname>
1772 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1773 * will not be in effect until the next time LinkProgram or
1774 * ProgramBinary has been called successfully."
1776 * The resloution of issue 9 in the extension spec also says:
1778 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1779 * to indicate to the GL implementation that this program will
1780 * likely be saved with GetProgramBinary at some point. This will
1781 * give the GL implementation the opportunity to track any state
1782 * changes made to the program before being saved such that when it
1783 * is loaded again a recompile can be avoided."
1785 shProg
->BinaryRetreivableHint
= value
;
1788 case GL_PROGRAM_SEPARABLE
:
1789 /* Spec imply that the behavior is the same as ARB_get_program_binary
1790 * Chapter 7.3 Program Objects
1792 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1793 _mesa_error(ctx
, GL_INVALID_VALUE
,
1794 "glProgramParameteri(pname=%s, value=%d): "
1795 "value must be 0 or 1.",
1796 _mesa_lookup_enum_by_nr(pname
),
1800 shProg
->SeparateShader
= value
;
1807 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1808 _mesa_lookup_enum_by_nr(pname
));
1812 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1813 struct gl_shader_program
*shProg
,
1814 struct gl_pipeline_object
*shTarget
)
1816 use_shader_program(ctx
, type
, shProg
, shTarget
);
1818 if (ctx
->Driver
.UseProgram
)
1819 ctx
->Driver
.UseProgram(ctx
, shProg
);
1824 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1825 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1827 const GLuint shader
= create_shader(ctx
, type
);
1831 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1833 compile_shader(ctx
, shader
);
1835 program
= create_shader_program(ctx
);
1837 struct gl_shader_program
*shProg
;
1838 struct gl_shader
*sh
;
1839 GLint compiled
= GL_FALSE
;
1841 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1842 sh
= _mesa_lookup_shader(ctx
, shader
);
1844 shProg
->SeparateShader
= separate
;
1846 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1848 attach_shader(ctx
, program
, shader
);
1849 link_program(ctx
, program
);
1850 detach_shader(ctx
, program
, shader
);
1854 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1855 append
-error
-to
-info
-log
;
1856 shProg
->LinkStatus
= GL_FALSE
;
1861 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1864 delete_shader(ctx
, shader
);
1872 * Copy program-specific data generated by linking from the gl_shader_program
1873 * object to a specific gl_program object.
1876 _mesa_copy_linked_program_data(gl_shader_stage type
,
1877 const struct gl_shader_program
*src
,
1878 struct gl_program
*dst
)
1881 case MESA_SHADER_VERTEX
:
1882 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1884 case MESA_SHADER_GEOMETRY
: {
1885 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1886 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1887 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1888 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1889 dst_gp
->InputType
= src
->Geom
.InputType
;
1890 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1891 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1892 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1893 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
1896 case MESA_SHADER_FRAGMENT
: {
1897 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
1898 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
1901 case MESA_SHADER_COMPUTE
: {
1902 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1904 for (i
= 0; i
< 3; i
++)
1905 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
1914 * ARB_separate_shader_objects: Compile & Link Program
1917 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
1918 const GLchar
* const *strings
)
1920 GET_CURRENT_CONTEXT(ctx
);
1922 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);