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 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
119 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
121 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
123 if (ctx
->Shader
.Flags
!= 0)
124 ctx
->Const
.GenerateTemporaryNames
= true;
126 /* Extended for ARB_separate_shader_objects */
127 ctx
->Shader
.RefCount
= 1;
128 mtx_init(&ctx
->Shader
.Mutex
, mtx_plain
);
133 * Free the per-context shader-related state.
136 _mesa_free_shader_state(struct gl_context
*ctx
)
139 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
140 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
143 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
145 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
147 /* Extended for ARB_separate_shader_objects */
148 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
150 assert(ctx
->Shader
.RefCount
== 1);
151 mtx_destroy(&ctx
->Shader
.Mutex
);
156 * Copy string from <src> to <dst>, up to maxLength characters, returning
157 * length of <dst> in <length>.
158 * \param src the strings source
159 * \param maxLength max chars to copy
160 * \param length returns number of chars copied
161 * \param dst the string destination
164 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
165 GLsizei
*length
, const GLchar
*src
)
168 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
179 * Confirm that the a shader type is valid and supported by the implementation
181 * \param ctx Current GL context
182 * \param type Shader target
186 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
188 /* Note: when building built-in GLSL functions, this function may be
189 * invoked with ctx == NULL. In that case, we can only validate that it's
190 * a shader target we recognize, not that it's supported in the current
191 * context. But that's fine--we don't need any further validation than
192 * that when building built-in GLSL functions.
196 case GL_FRAGMENT_SHADER
:
197 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
198 case GL_VERTEX_SHADER
:
199 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
200 case GL_GEOMETRY_SHADER_ARB
:
201 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
202 case GL_COMPUTE_SHADER
:
203 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
211 is_program(struct gl_context
*ctx
, GLuint name
)
213 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
214 return shProg
? GL_TRUE
: GL_FALSE
;
219 is_shader(struct gl_context
*ctx
, GLuint name
)
221 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
222 return shader
? GL_TRUE
: GL_FALSE
;
227 * Attach shader to a shader program.
230 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
232 struct gl_shader_program
*shProg
;
233 struct gl_shader
*sh
;
236 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
238 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
242 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
247 n
= shProg
->NumShaders
;
248 for (i
= 0; i
< n
; i
++) {
249 if (shProg
->Shaders
[i
] == sh
) {
250 /* The shader is already attched to this program. The
251 * GL_ARB_shader_objects spec says:
253 * "The error INVALID_OPERATION is generated by AttachObjectARB
254 * if <obj> is already attached to <containerObj>."
256 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
258 } else if (same_type_disallowed
&&
259 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
260 /* Shader with the same type is already attached to this program,
261 * OpenGL ES 2.0 and 3.0 specs say:
263 * "Multiple shader objects of the same type may not be attached
264 * to a single program object. [...] The error INVALID_OPERATION
265 * is generated if [...] another shader object of the same type
266 * as shader is already attached to program."
268 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
274 shProg
->Shaders
= realloc(shProg
->Shaders
,
275 (n
+ 1) * sizeof(struct gl_shader
*));
276 if (!shProg
->Shaders
) {
277 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
282 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
283 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
284 shProg
->NumShaders
++;
289 create_shader(struct gl_context
*ctx
, GLenum type
)
291 struct gl_shader
*sh
;
294 if (!_mesa_validate_shader_target(ctx
, type
)) {
295 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
299 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
300 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
301 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
308 create_shader_program(struct gl_context
*ctx
)
311 struct gl_shader_program
*shProg
;
313 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
315 shProg
= ctx
->Driver
.NewShaderProgram(name
);
317 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
319 assert(shProg
->RefCount
== 1);
326 * Delete a shader program. Actually, just decrement the program's
327 * reference count and mark it as DeletePending.
328 * Used to implement glDeleteProgram() and glDeleteObjectARB().
331 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
334 * NOTE: deleting shaders/programs works a bit differently than
335 * texture objects (and buffer objects, etc). Shader/program
336 * handles/IDs exist in the hash table until the object is really
337 * deleted (refcount==0). With texture objects, the handle/ID is
338 * removed from the hash table in glDeleteTextures() while the tex
339 * object itself might linger until its refcount goes to zero.
341 struct gl_shader_program
*shProg
;
343 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
347 if (!shProg
->DeletePending
) {
348 shProg
->DeletePending
= GL_TRUE
;
350 /* effectively, decr shProg's refcount */
351 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
357 delete_shader(struct gl_context
*ctx
, GLuint shader
)
359 struct gl_shader
*sh
;
361 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
365 if (!sh
->DeletePending
) {
366 sh
->DeletePending
= GL_TRUE
;
368 /* effectively, decr sh's refcount */
369 _mesa_reference_shader(ctx
, &sh
, NULL
);
375 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
377 struct gl_shader_program
*shProg
;
381 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
385 n
= shProg
->NumShaders
;
387 for (i
= 0; i
< n
; i
++) {
388 if (shProg
->Shaders
[i
]->Name
== shader
) {
390 struct gl_shader
**newList
;
393 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
395 /* alloc new, smaller array */
396 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
398 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
401 /* Copy old list entries to new list, skipping removed entry at [i] */
402 for (j
= 0; j
< i
; j
++) {
403 newList
[j
] = shProg
->Shaders
[j
];
406 newList
[j
++] = shProg
->Shaders
[i
];
409 /* Free old list and install new one */
410 free(shProg
->Shaders
);
411 shProg
->Shaders
= newList
;
412 shProg
->NumShaders
= n
- 1;
415 /* sanity check - make sure the new list's entries are sensible */
416 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
417 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
418 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
419 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
420 assert(shProg
->Shaders
[j
]->RefCount
> 0);
431 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
432 err
= GL_INVALID_OPERATION
;
434 err
= GL_INVALID_VALUE
;
435 _mesa_error(ctx
, err
, "glDetachShader(shader)");
442 * Return list of shaders attached to shader program.
445 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
446 GLsizei
*count
, GLuint
*obj
)
448 struct gl_shader_program
*shProg
;
451 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
456 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
460 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
461 obj
[i
] = shProg
->Shaders
[i
]->Name
;
470 * glGetHandleARB() - return ID/name of currently bound shader program.
473 get_handle(struct gl_context
*ctx
, GLenum pname
)
475 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
476 if (ctx
->_Shader
->ActiveProgram
)
477 return ctx
->_Shader
->ActiveProgram
->Name
;
482 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
489 * Check if a geometry shader query is valid at this time. If not, report an
490 * error and return false.
492 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
494 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
495 * are queried for a program which has not been linked successfully, or
496 * which does not contain objects to form a geometry shader, then an
497 * INVALID_OPERATION error is generated."
500 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
502 if (shProg
->LinkStatus
&&
503 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
507 _mesa_error(ctx
, GL_INVALID_OPERATION
,
508 "glGetProgramv(linked geometry shader required)");
514 * glGetProgramiv() - get shader program state.
515 * Note that this is for GLSL shader programs, not ARB vertex/fragment
516 * programs (see glGetProgramivARB).
519 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
522 struct gl_shader_program
*shProg
523 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
525 /* Is transform feedback available in this context?
528 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
529 || ctx
->API
== API_OPENGL_CORE
530 || _mesa_is_gles3(ctx
);
532 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
533 * and GL 3.2) are available in this context
535 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
537 /* Are uniform buffer objects available in this context?
540 (ctx
->API
== API_OPENGL_COMPAT
&&
541 ctx
->Extensions
.ARB_uniform_buffer_object
)
542 || ctx
->API
== API_OPENGL_CORE
543 || _mesa_is_gles3(ctx
);
550 case GL_DELETE_STATUS
:
551 *params
= shProg
->DeletePending
;
554 *params
= shProg
->LinkStatus
;
556 case GL_VALIDATE_STATUS
:
557 *params
= shProg
->Validated
;
559 case GL_INFO_LOG_LENGTH
:
560 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
562 case GL_ATTACHED_SHADERS
:
563 *params
= shProg
->NumShaders
;
565 case GL_ACTIVE_ATTRIBUTES
:
566 *params
= _mesa_count_active_attribs(shProg
);
568 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
569 *params
= _mesa_longest_attribute_name_length(shProg
);
571 case GL_ACTIVE_UNIFORMS
:
572 *params
= shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
574 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
577 const unsigned num_uniforms
=
578 shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
580 for (i
= 0; i
< num_uniforms
; i
++) {
581 /* Add one for the terminating NUL character for a non-array, and
582 * 4 for the "[0]" and the NUL for an array.
584 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
585 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
594 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
597 *params
= shProg
->TransformFeedback
.NumVarying
;
599 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
605 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
606 /* Add one for the terminating NUL character.
609 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
618 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
621 *params
= shProg
->TransformFeedback
.BufferMode
;
623 case GL_GEOMETRY_VERTICES_OUT
:
626 if (check_gs_query(ctx
, shProg
))
627 *params
= shProg
->Geom
.VerticesOut
;
629 case GL_GEOMETRY_SHADER_INVOCATIONS
:
630 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
632 if (check_gs_query(ctx
, shProg
))
633 *params
= shProg
->Geom
.Invocations
;
635 case GL_GEOMETRY_INPUT_TYPE
:
638 if (check_gs_query(ctx
, shProg
))
639 *params
= shProg
->Geom
.InputType
;
641 case GL_GEOMETRY_OUTPUT_TYPE
:
644 if (check_gs_query(ctx
, shProg
))
645 *params
= shProg
->Geom
.OutputType
;
647 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
654 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
655 /* Add one for the terminating NUL character.
657 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
666 case GL_ACTIVE_UNIFORM_BLOCKS
:
670 *params
= shProg
->NumUniformBlocks
;
672 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
673 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
674 * only available with desktop OpenGL 3.0+ with the
675 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
677 * On desktop, we ignore the 3.0+ requirement because it is silly.
679 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
682 *params
= shProg
->BinaryRetreivableHint
;
684 case GL_PROGRAM_BINARY_LENGTH
:
687 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
688 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
691 *params
= shProg
->NumAtomicBuffers
;
693 case GL_COMPUTE_WORK_GROUP_SIZE
: {
695 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
697 if (!shProg
->LinkStatus
) {
698 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
702 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
703 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
707 for (i
= 0; i
< 3; i
++)
708 params
[i
] = shProg
->Comp
.LocalSize
[i
];
711 case GL_PROGRAM_SEPARABLE
:
712 *params
= shProg
->SeparateShader
;
718 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
719 _mesa_lookup_enum_by_nr(pname
));
724 * glGetShaderiv() - get GLSL shader state
727 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
729 struct gl_shader
*shader
=
730 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
738 *params
= shader
->Type
;
740 case GL_DELETE_STATUS
:
741 *params
= shader
->DeletePending
;
743 case GL_COMPILE_STATUS
:
744 *params
= shader
->CompileStatus
;
746 case GL_INFO_LOG_LENGTH
:
747 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
749 case GL_SHADER_SOURCE_LENGTH
:
750 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
753 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
760 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
761 GLsizei
*length
, GLchar
*infoLog
)
763 struct gl_shader_program
*shProg
;
765 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
766 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
768 * "If a negative number is provided where an argument of type sizei or
769 * sizeiptr is specified, an INVALID_VALUE error is generated."
772 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
776 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
777 "glGetProgramInfoLog(program)");
782 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
787 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
788 GLsizei
*length
, GLchar
*infoLog
)
790 struct gl_shader
*sh
;
792 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
793 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
795 * "If a negative number is provided where an argument of type sizei or
796 * sizeiptr is specified, an INVALID_VALUE error is generated."
799 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
803 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
808 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
813 * Return shader source code.
816 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
817 GLsizei
*length
, GLchar
*sourceOut
)
819 struct gl_shader
*sh
;
822 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
826 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
830 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
835 * Set/replace shader source code. A helper function used by
836 * glShaderSource[ARB].
839 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
841 struct gl_shader
*sh
;
843 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
847 /* free old shader source string and install new one */
848 free((void *)sh
->Source
);
850 sh
->CompileStatus
= GL_FALSE
;
852 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
861 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
863 struct gl_shader
*sh
;
865 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
870 /* If the user called glCompileShader without first calling
871 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
873 sh
->CompileStatus
= GL_FALSE
;
875 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
876 _mesa_log("GLSL source for %s shader %d:\n",
877 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
878 _mesa_log("%s\n", sh
->Source
);
881 /* this call will set the shader->CompileStatus field to indicate if
882 * compilation was successful.
884 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
886 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
887 _mesa_write_shader_to_file(sh
);
890 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
891 if (sh
->CompileStatus
) {
892 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
893 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
896 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
898 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
899 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
900 _mesa_log("%s\n", sh
->InfoLog
);
905 if (!sh
->CompileStatus
) {
906 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
907 _mesa_log("GLSL source for %s shader %d:\n",
908 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
909 _mesa_log("%s\n", sh
->Source
);
910 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
913 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
914 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
915 sh
->Name
, sh
->InfoLog
);
922 * Link a program's shaders.
925 link_program(struct gl_context
*ctx
, GLuint program
)
927 struct gl_shader_program
*shProg
;
929 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
933 /* From the ARB_transform_feedback2 specification:
934 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
935 * the name of a program being used by one or more transform feedback
936 * objects, even if the objects are not currently bound or are paused."
938 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
939 _mesa_error(ctx
, GL_INVALID_OPERATION
,
940 "glLinkProgram(transform feedback is using the program)");
944 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
946 _mesa_glsl_link_shader(ctx
, shProg
);
948 if (shProg
->LinkStatus
== GL_FALSE
&&
949 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
950 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
951 shProg
->Name
, shProg
->InfoLog
);
958 printf("Link %u shaders in program %u: %s\n",
959 shProg
->NumShaders
, shProg
->Name
,
960 shProg
->LinkStatus
? "Success" : "Failed");
962 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
963 printf(" shader %u, type 0x%x\n",
964 shProg
->Shaders
[i
]->Name
,
965 shProg
->Shaders
[i
]->Type
);
972 * Print basic shader info (for debug).
975 print_shader_info(const struct gl_shader_program
*shProg
)
979 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
980 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
981 printf(" %s shader %u, checksum %u\n",
982 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
983 shProg
->Shaders
[i
]->Name
,
984 shProg
->Shaders
[i
]->SourceChecksum
);
986 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
987 printf(" vert prog %u\n",
988 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
989 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
990 printf(" frag prog %u\n",
991 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
992 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
993 printf(" geom prog %u\n",
994 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
999 * Use the named shader program for subsequent glUniform calls
1002 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1005 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
1006 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1007 "%s(program %u not linked)", caller
, shProg
->Name
);
1011 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1012 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1018 use_shader_program(struct gl_context
*ctx
, gl_shader_stage stage
,
1019 struct gl_shader_program
*shProg
,
1020 struct gl_pipeline_object
*shTarget
)
1022 struct gl_shader_program
**target
;
1024 target
= &shTarget
->CurrentProgram
[stage
];
1025 if ((shProg
!= NULL
) && (shProg
->_LinkedShaders
[stage
] == NULL
))
1028 if (*target
!= shProg
) {
1029 /* Program is current, flush it */
1030 if (shTarget
== ctx
->_Shader
) {
1031 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1034 /* If the shader is also bound as the current rendering shader, unbind
1035 * it from that binding point as well. This ensures that the correct
1036 * semantics of glDeleteProgram are maintained.
1039 case MESA_SHADER_VERTEX
:
1040 /* Empty for now. */
1042 case MESA_SHADER_GEOMETRY
:
1043 /* Empty for now. */
1045 case MESA_SHADER_COMPUTE
:
1046 /* Empty for now. */
1048 case MESA_SHADER_FRAGMENT
:
1049 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1050 _mesa_reference_shader_program(ctx
,
1051 &ctx
->_Shader
->_CurrentFragmentProgram
,
1057 _mesa_reference_shader_program(ctx
, target
, shProg
);
1064 * Use the named shader program for subsequent rendering.
1067 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1070 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
1071 use_shader_program(ctx
, i
, shProg
, &ctx
->Shader
);
1072 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1074 if (ctx
->Driver
.UseProgram
)
1075 ctx
->Driver
.UseProgram(ctx
, shProg
);
1080 * Do validation of the given shader program.
1081 * \param errMsg returns error message if validation fails.
1082 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1085 validate_shader_program(const struct gl_shader_program
*shProg
,
1088 if (!shProg
->LinkStatus
) {
1092 /* From the GL spec, a program is invalid if any of these are true:
1094 any two active samplers in the current program object are of
1095 different types, but refer to the same texture image unit,
1097 any active sampler in the current program object refers to a texture
1098 image unit where fixed-function fragment processing accesses a
1099 texture target that does not match the sampler type, or
1101 the sum of the number of active samplers in the program and the
1102 number of texture image units enabled for fixed-function fragment
1103 processing exceeds the combined limit on the total number of texture
1104 image units allowed.
1108 * Check: any two active samplers in the current program object are of
1109 * different types, but refer to the same texture image unit,
1111 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1119 * Called via glValidateProgram()
1122 validate_program(struct gl_context
*ctx
, GLuint program
)
1124 struct gl_shader_program
*shProg
;
1125 char errMsg
[100] = "";
1127 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1132 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1133 if (!shProg
->Validated
) {
1134 /* update info log */
1135 if (shProg
->InfoLog
) {
1136 ralloc_free(shProg
->InfoLog
);
1138 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1145 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1147 GET_CURRENT_CONTEXT(ctx
);
1148 attach_shader(ctx
, program
, shader
);
1153 _mesa_AttachShader(GLuint program
, GLuint shader
)
1155 GET_CURRENT_CONTEXT(ctx
);
1156 attach_shader(ctx
, program
, shader
);
1161 _mesa_CompileShader(GLhandleARB shaderObj
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 if (MESA_VERBOSE
& VERBOSE_API
)
1165 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1166 compile_shader(ctx
, shaderObj
);
1171 _mesa_CreateShader(GLenum type
)
1173 GET_CURRENT_CONTEXT(ctx
);
1174 if (MESA_VERBOSE
& VERBOSE_API
)
1175 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1176 return create_shader(ctx
, type
);
1180 GLhandleARB GLAPIENTRY
1181 _mesa_CreateShaderObjectARB(GLenum type
)
1183 GET_CURRENT_CONTEXT(ctx
);
1184 return create_shader(ctx
, type
);
1189 _mesa_CreateProgram(void)
1191 GET_CURRENT_CONTEXT(ctx
);
1192 if (MESA_VERBOSE
& VERBOSE_API
)
1193 _mesa_debug(ctx
, "glCreateProgram\n");
1194 return create_shader_program(ctx
);
1198 GLhandleARB GLAPIENTRY
1199 _mesa_CreateProgramObjectARB(void)
1201 GET_CURRENT_CONTEXT(ctx
);
1202 return create_shader_program(ctx
);
1207 _mesa_DeleteObjectARB(GLhandleARB obj
)
1209 if (MESA_VERBOSE
& VERBOSE_API
) {
1210 GET_CURRENT_CONTEXT(ctx
);
1211 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1215 GET_CURRENT_CONTEXT(ctx
);
1216 FLUSH_VERTICES(ctx
, 0);
1217 if (is_program(ctx
, obj
)) {
1218 delete_shader_program(ctx
, obj
);
1220 else if (is_shader(ctx
, obj
)) {
1221 delete_shader(ctx
, obj
);
1231 _mesa_DeleteProgram(GLuint name
)
1234 GET_CURRENT_CONTEXT(ctx
);
1235 FLUSH_VERTICES(ctx
, 0);
1236 delete_shader_program(ctx
, name
);
1242 _mesa_DeleteShader(GLuint name
)
1245 GET_CURRENT_CONTEXT(ctx
);
1246 FLUSH_VERTICES(ctx
, 0);
1247 delete_shader(ctx
, name
);
1253 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1255 GET_CURRENT_CONTEXT(ctx
);
1256 detach_shader(ctx
, program
, shader
);
1261 _mesa_DetachShader(GLuint program
, GLuint shader
)
1263 GET_CURRENT_CONTEXT(ctx
);
1264 detach_shader(ctx
, program
, shader
);
1269 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1270 GLsizei
* count
, GLhandleARB
* obj
)
1272 GET_CURRENT_CONTEXT(ctx
);
1273 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1278 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1279 GLsizei
*count
, GLuint
*obj
)
1281 GET_CURRENT_CONTEXT(ctx
);
1282 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1287 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1288 GLcharARB
* infoLog
)
1290 GET_CURRENT_CONTEXT(ctx
);
1291 if (is_program(ctx
, object
)) {
1292 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1294 else if (is_shader(ctx
, object
)) {
1295 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1298 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1304 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1306 GET_CURRENT_CONTEXT(ctx
);
1307 /* Implement in terms of GetProgramiv, GetShaderiv */
1308 if (is_program(ctx
, object
)) {
1309 if (pname
== GL_OBJECT_TYPE_ARB
) {
1310 *params
= GL_PROGRAM_OBJECT_ARB
;
1313 get_programiv(ctx
, object
, pname
, params
);
1316 else if (is_shader(ctx
, object
)) {
1317 if (pname
== GL_OBJECT_TYPE_ARB
) {
1318 *params
= GL_SHADER_OBJECT_ARB
;
1321 get_shaderiv(ctx
, object
, pname
, params
);
1325 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1331 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1334 GLint iparams
[1]; /* XXX is one element enough? */
1335 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1336 params
[0] = (GLfloat
) iparams
[0];
1341 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1343 GET_CURRENT_CONTEXT(ctx
);
1344 get_programiv(ctx
, program
, pname
, params
);
1349 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1351 GET_CURRENT_CONTEXT(ctx
);
1352 get_shaderiv(ctx
, shader
, pname
, params
);
1357 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1358 GLsizei
*length
, GLchar
*infoLog
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1366 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1367 GLsizei
*length
, GLchar
*infoLog
)
1369 GET_CURRENT_CONTEXT(ctx
);
1370 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1375 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1376 GLsizei
*length
, GLcharARB
*sourceOut
)
1378 GET_CURRENT_CONTEXT(ctx
);
1379 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1383 GLhandleARB GLAPIENTRY
1384 _mesa_GetHandleARB(GLenum pname
)
1386 GET_CURRENT_CONTEXT(ctx
);
1387 return get_handle(ctx
, pname
);
1391 GLboolean GLAPIENTRY
1392 _mesa_IsProgram(GLuint name
)
1394 GET_CURRENT_CONTEXT(ctx
);
1395 return is_program(ctx
, name
);
1399 GLboolean GLAPIENTRY
1400 _mesa_IsShader(GLuint name
)
1402 GET_CURRENT_CONTEXT(ctx
);
1403 return is_shader(ctx
, name
);
1408 _mesa_LinkProgram(GLhandleARB programObj
)
1410 GET_CURRENT_CONTEXT(ctx
);
1411 link_program(ctx
, programObj
);
1417 * Read shader source code from a file.
1418 * Useful for debugging to override an app's shader.
1421 read_shader(const char *fname
)
1423 int shader_size
= 0;
1424 FILE *f
= fopen(fname
, "r");
1425 GLcharARB
*buffer
, *shader
;
1432 /* allocate enough room for the entire shader */
1433 fseek(f
, 0, SEEK_END
);
1434 shader_size
= ftell(f
);
1436 assert(shader_size
);
1438 /* add one for terminating zero */
1441 buffer
= malloc(shader_size
);
1444 len
= fread(buffer
, 1, shader_size
, f
);
1449 shader
= strdup(buffer
);
1457 * Called via glShaderSource() and glShaderSourceARB() API functions.
1458 * Basically, concatenate the source code strings into one long string
1459 * and pass it to _mesa_shader_source().
1462 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1463 const GLcharARB
* const * string
, const GLint
* length
)
1465 GET_CURRENT_CONTEXT(ctx
);
1467 GLsizei i
, totalLength
;
1471 if (!shaderObj
|| string
== NULL
) {
1472 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1477 * This array holds offsets of where the appropriate string ends, thus the
1478 * last element will be set to the total length of the source code.
1480 offsets
= malloc(count
* sizeof(GLint
));
1481 if (offsets
== NULL
) {
1482 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1486 for (i
= 0; i
< count
; i
++) {
1487 if (string
[i
] == NULL
) {
1488 free((GLvoid
*) offsets
);
1489 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1490 "glShaderSourceARB(null string)");
1493 if (length
== NULL
|| length
[i
] < 0)
1494 offsets
[i
] = strlen(string
[i
]);
1496 offsets
[i
] = length
[i
];
1497 /* accumulate string lengths */
1499 offsets
[i
] += offsets
[i
- 1];
1502 /* Total length of source string is sum off all strings plus two.
1503 * One extra byte for terminating zero, another extra byte to silence
1504 * valgrind warnings in the parser/grammer code.
1506 totalLength
= offsets
[count
- 1] + 2;
1507 source
= malloc(totalLength
* sizeof(GLcharARB
));
1508 if (source
== NULL
) {
1509 free((GLvoid
*) offsets
);
1510 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1514 for (i
= 0; i
< count
; i
++) {
1515 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1516 memcpy(source
+ start
, string
[i
],
1517 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1519 source
[totalLength
- 1] = '\0';
1520 source
[totalLength
- 2] = '\0';
1523 /* Compute the shader's source code checksum then try to open a file
1524 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1525 * original shader source code. For debugging.
1528 GLcharARB
*newSource
;
1530 checksum
= _mesa_str_checksum(source
);
1532 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1534 newSource
= read_shader(filename
);
1536 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1537 shaderObj
, checksum
, filename
);
1543 shader_source(ctx
, shaderObj
, source
);
1546 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1548 sh
->SourceChecksum
= checksum
; /* save original checksum */
1556 _mesa_UseProgram(GLhandleARB program
)
1558 GET_CURRENT_CONTEXT(ctx
);
1559 struct gl_shader_program
*shProg
;
1561 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1563 "glUseProgram(transform feedback active)");
1568 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1572 if (!shProg
->LinkStatus
) {
1573 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1574 "glUseProgram(program %u not linked)", program
);
1579 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1580 print_shader_info(shProg
);
1587 /* The ARB_separate_shader_object spec says:
1589 * "The executable code for an individual shader stage is taken from
1590 * the current program for that stage. If there is a current program
1591 * object established by UseProgram, that program is considered current
1592 * for all stages. Otherwise, if there is a bound program pipeline
1593 * object (section 2.14.PPO), the program bound to the appropriate
1594 * stage of the pipeline object is considered current."
1597 /* Attach shader state to the binding point */
1598 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1599 /* Update the program */
1600 _mesa_use_program(ctx
, shProg
);
1602 /* Must be done first: detach the progam */
1603 _mesa_use_program(ctx
, shProg
);
1604 /* Unattach shader_state binding point */
1605 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1606 /* If a pipeline was bound, rebind it */
1607 if (ctx
->Pipeline
.Current
) {
1608 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1615 _mesa_ValidateProgram(GLhandleARB program
)
1617 GET_CURRENT_CONTEXT(ctx
);
1618 validate_program(ctx
, program
);
1623 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1626 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1627 GLint
* range
, GLint
* precision
)
1629 const struct gl_program_constants
*limits
;
1630 const struct gl_precision
*p
;
1631 GET_CURRENT_CONTEXT(ctx
);
1633 switch (shadertype
) {
1634 case GL_VERTEX_SHADER
:
1635 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1637 case GL_FRAGMENT_SHADER
:
1638 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1641 _mesa_error(ctx
, GL_INVALID_ENUM
,
1642 "glGetShaderPrecisionFormat(shadertype)");
1646 switch (precisiontype
) {
1648 p
= &limits
->LowFloat
;
1650 case GL_MEDIUM_FLOAT
:
1651 p
= &limits
->MediumFloat
;
1654 p
= &limits
->HighFloat
;
1657 p
= &limits
->LowInt
;
1660 p
= &limits
->MediumInt
;
1663 p
= &limits
->HighInt
;
1666 _mesa_error(ctx
, GL_INVALID_ENUM
,
1667 "glGetShaderPrecisionFormat(precisiontype)");
1671 range
[0] = p
->RangeMin
;
1672 range
[1] = p
->RangeMax
;
1673 precision
[0] = p
->Precision
;
1678 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1681 _mesa_ReleaseShaderCompiler(void)
1683 _mesa_destroy_shader_compiler_caches();
1688 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1691 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1692 const void* binary
, GLint length
)
1694 GET_CURRENT_CONTEXT(ctx
);
1697 (void) binaryformat
;
1700 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary");
1705 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1706 GLenum
*binaryFormat
, GLvoid
*binary
)
1708 struct gl_shader_program
*shProg
;
1709 GLsizei length_dummy
;
1710 GET_CURRENT_CONTEXT(ctx
);
1713 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1717 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1721 /* The ARB_get_program_binary spec says:
1723 * "If <length> is NULL, then no length is returned."
1725 * Ensure that length always points to valid storage to avoid multiple NULL
1726 * pointer checks below.
1729 length
= &length_dummy
;
1732 /* The ARB_get_program_binary spec says:
1734 * "When a program object's LINK_STATUS is FALSE, its program binary
1735 * length is zero, and a call to GetProgramBinary will generate an
1736 * INVALID_OPERATION error.
1738 if (!shProg
->LinkStatus
) {
1739 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1740 "glGetProgramBinary(program %u not linked)",
1747 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1748 "glGetProgramBinary(driver supports zero binary formats)");
1750 (void) binaryFormat
;
1755 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1756 const GLvoid
*binary
, GLsizei length
)
1758 struct gl_shader_program
*shProg
;
1759 GET_CURRENT_CONTEXT(ctx
);
1761 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1765 (void) binaryFormat
;
1768 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
1770 * "If a negative number is provided where an argument of type sizei or
1771 * sizeiptr is specified, an INVALID_VALUE error is generated."
1774 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
1778 /* The ARB_get_program_binary spec says:
1780 * "<binaryFormat> and <binary> must be those returned by a previous
1781 * call to GetProgramBinary, and <length> must be the length of the
1782 * program binary as returned by GetProgramBinary or GetProgramiv with
1783 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
1784 * setting the LINK_STATUS of <program> to FALSE, if these conditions
1787 * Since any value of binaryFormat passed "is not one of those specified as
1788 * allowable for [this] command, an INVALID_ENUM error is generated."
1790 shProg
->LinkStatus
= GL_FALSE
;
1791 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
1796 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1798 struct gl_shader_program
*shProg
;
1799 GET_CURRENT_CONTEXT(ctx
);
1801 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1802 "glProgramParameteri");
1807 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1808 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1809 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1810 * even be in the dispatch table, so we shouldn't need to expclicitly
1813 * On desktop, we ignore the 3.0+ requirement because it is silly.
1816 /* The ARB_get_program_binary extension spec says:
1818 * "An INVALID_VALUE error is generated if the <value> argument to
1819 * ProgramParameteri is not TRUE or FALSE."
1821 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1825 /* No need to notify the driver. Any changes will actually take effect
1826 * the next time the shader is linked.
1828 * The ARB_get_program_binary extension spec says:
1830 * "To indicate that a program binary is likely to be retrieved,
1831 * ProgramParameteri should be called with <pname>
1832 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1833 * will not be in effect until the next time LinkProgram or
1834 * ProgramBinary has been called successfully."
1836 * The resloution of issue 9 in the extension spec also says:
1838 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1839 * to indicate to the GL implementation that this program will
1840 * likely be saved with GetProgramBinary at some point. This will
1841 * give the GL implementation the opportunity to track any state
1842 * changes made to the program before being saved such that when it
1843 * is loaded again a recompile can be avoided."
1845 shProg
->BinaryRetreivableHint
= value
;
1848 case GL_PROGRAM_SEPARABLE
:
1849 /* Spec imply that the behavior is the same as ARB_get_program_binary
1850 * Chapter 7.3 Program Objects
1852 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1855 shProg
->SeparateShader
= value
;
1859 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1860 _mesa_lookup_enum_by_nr(pname
));
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "glProgramParameteri(pname=%s, value=%d): "
1867 "value must be 0 or 1.",
1868 _mesa_lookup_enum_by_nr(pname
),
1874 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1875 struct gl_shader_program
*shProg
,
1876 struct gl_pipeline_object
*shTarget
)
1878 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
1879 use_shader_program(ctx
, stage
, shProg
, shTarget
);
1881 if (ctx
->Driver
.UseProgram
)
1882 ctx
->Driver
.UseProgram(ctx
, shProg
);
1887 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1888 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1890 const GLuint shader
= create_shader(ctx
, type
);
1894 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1896 compile_shader(ctx
, shader
);
1898 program
= create_shader_program(ctx
);
1900 struct gl_shader_program
*shProg
;
1901 struct gl_shader
*sh
;
1902 GLint compiled
= GL_FALSE
;
1904 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1905 sh
= _mesa_lookup_shader(ctx
, shader
);
1907 shProg
->SeparateShader
= separate
;
1909 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1911 attach_shader(ctx
, program
, shader
);
1912 link_program(ctx
, program
);
1913 detach_shader(ctx
, program
, shader
);
1917 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1918 append
-error
-to
-info
-log
;
1919 shProg
->LinkStatus
= GL_FALSE
;
1924 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1927 delete_shader(ctx
, shader
);
1935 * Copy program-specific data generated by linking from the gl_shader_program
1936 * object to a specific gl_program object.
1939 _mesa_copy_linked_program_data(gl_shader_stage type
,
1940 const struct gl_shader_program
*src
,
1941 struct gl_program
*dst
)
1944 case MESA_SHADER_VERTEX
:
1945 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1947 case MESA_SHADER_GEOMETRY
: {
1948 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1949 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1950 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1951 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1952 dst_gp
->InputType
= src
->Geom
.InputType
;
1953 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1954 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1955 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1956 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
1959 case MESA_SHADER_FRAGMENT
: {
1960 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
1961 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
1964 case MESA_SHADER_COMPUTE
: {
1965 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1967 for (i
= 0; i
< 3; i
++)
1968 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
1977 * ARB_separate_shader_objects: Compile & Link Program
1980 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
1981 const GLchar
* const *strings
)
1983 GET_CURRENT_CONTEXT(ctx
);
1985 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);