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
->NumUserUniformStorage
- shProg
->NumHiddenUniforms
;
574 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
577 const unsigned num_uniforms
=
578 shProg
->NumUserUniformStorage
- 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
;
864 struct gl_shader_compiler_options
*options
;
866 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
870 options
= &ctx
->Const
.ShaderCompilerOptions
[sh
->Stage
];
873 /* If the user called glCompileShader without first calling
874 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
876 sh
->CompileStatus
= GL_FALSE
;
878 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
879 _mesa_log("GLSL source for %s shader %d:\n",
880 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
881 _mesa_log("%s\n", sh
->Source
);
884 /* this call will set the shader->CompileStatus field to indicate if
885 * compilation was successful.
887 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
889 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
890 _mesa_write_shader_to_file(sh
);
893 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
894 if (sh
->CompileStatus
) {
895 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
896 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
899 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
901 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
902 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
903 _mesa_log("%s\n", sh
->InfoLog
);
908 if (!sh
->CompileStatus
) {
909 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
910 _mesa_log("GLSL source for %s shader %d:\n",
911 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
912 _mesa_log("%s\n", sh
->Source
);
913 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
916 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
917 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
918 sh
->Name
, sh
->InfoLog
);
925 * Link a program's shaders.
928 link_program(struct gl_context
*ctx
, GLuint program
)
930 struct gl_shader_program
*shProg
;
932 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
936 /* From the ARB_transform_feedback2 specification:
937 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
938 * the name of a program being used by one or more transform feedback
939 * objects, even if the objects are not currently bound or are paused."
941 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
942 _mesa_error(ctx
, GL_INVALID_OPERATION
,
943 "glLinkProgram(transform feedback is using the program)");
947 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
949 _mesa_glsl_link_shader(ctx
, shProg
);
951 if (shProg
->LinkStatus
== GL_FALSE
&&
952 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
953 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
954 shProg
->Name
, shProg
->InfoLog
);
961 printf("Link %u shaders in program %u: %s\n",
962 shProg
->NumShaders
, shProg
->Name
,
963 shProg
->LinkStatus
? "Success" : "Failed");
965 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
966 printf(" shader %u, type 0x%x\n",
967 shProg
->Shaders
[i
]->Name
,
968 shProg
->Shaders
[i
]->Type
);
975 * Print basic shader info (for debug).
978 print_shader_info(const struct gl_shader_program
*shProg
)
982 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
983 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
984 printf(" %s shader %u, checksum %u\n",
985 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
986 shProg
->Shaders
[i
]->Name
,
987 shProg
->Shaders
[i
]->SourceChecksum
);
989 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
990 printf(" vert prog %u\n",
991 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
992 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
993 printf(" frag prog %u\n",
994 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
995 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
996 printf(" geom prog %u\n",
997 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1002 * Use the named shader program for subsequent glUniform calls
1005 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1008 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
1009 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1010 "%s(program %u not linked)", caller
, shProg
->Name
);
1014 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1015 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1021 use_shader_program(struct gl_context
*ctx
, gl_shader_stage stage
,
1022 struct gl_shader_program
*shProg
,
1023 struct gl_pipeline_object
*shTarget
)
1025 struct gl_shader_program
**target
;
1027 target
= &shTarget
->CurrentProgram
[stage
];
1028 if ((shProg
!= NULL
) && (shProg
->_LinkedShaders
[stage
] == NULL
))
1031 if (*target
!= shProg
) {
1032 /* Program is current, flush it */
1033 if (shTarget
== ctx
->_Shader
) {
1034 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1037 /* If the shader is also bound as the current rendering shader, unbind
1038 * it from that binding point as well. This ensures that the correct
1039 * semantics of glDeleteProgram are maintained.
1042 case MESA_SHADER_VERTEX
:
1043 /* Empty for now. */
1045 case MESA_SHADER_GEOMETRY
:
1046 /* Empty for now. */
1048 case MESA_SHADER_COMPUTE
:
1049 /* Empty for now. */
1051 case MESA_SHADER_FRAGMENT
:
1052 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1053 _mesa_reference_shader_program(ctx
,
1054 &ctx
->_Shader
->_CurrentFragmentProgram
,
1060 _mesa_reference_shader_program(ctx
, target
, shProg
);
1067 * Use the named shader program for subsequent rendering.
1070 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1073 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
1074 use_shader_program(ctx
, i
, shProg
, &ctx
->Shader
);
1075 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1077 if (ctx
->Driver
.UseProgram
)
1078 ctx
->Driver
.UseProgram(ctx
, shProg
);
1083 * Do validation of the given shader program.
1084 * \param errMsg returns error message if validation fails.
1085 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1088 validate_shader_program(const struct gl_shader_program
*shProg
,
1091 if (!shProg
->LinkStatus
) {
1095 /* From the GL spec, a program is invalid if any of these are true:
1097 any two active samplers in the current program object are of
1098 different types, but refer to the same texture image unit,
1100 any active sampler in the current program object refers to a texture
1101 image unit where fixed-function fragment processing accesses a
1102 texture target that does not match the sampler type, or
1104 the sum of the number of active samplers in the program and the
1105 number of texture image units enabled for fixed-function fragment
1106 processing exceeds the combined limit on the total number of texture
1107 image units allowed.
1111 * Check: any two active samplers in the current program object are of
1112 * different types, but refer to the same texture image unit,
1114 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1122 * Called via glValidateProgram()
1125 validate_program(struct gl_context
*ctx
, GLuint program
)
1127 struct gl_shader_program
*shProg
;
1128 char errMsg
[100] = "";
1130 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1135 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1136 if (!shProg
->Validated
) {
1137 /* update info log */
1138 if (shProg
->InfoLog
) {
1139 ralloc_free(shProg
->InfoLog
);
1141 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1148 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1150 GET_CURRENT_CONTEXT(ctx
);
1151 attach_shader(ctx
, program
, shader
);
1156 _mesa_AttachShader(GLuint program
, GLuint shader
)
1158 GET_CURRENT_CONTEXT(ctx
);
1159 attach_shader(ctx
, program
, shader
);
1164 _mesa_CompileShader(GLhandleARB shaderObj
)
1166 GET_CURRENT_CONTEXT(ctx
);
1167 if (MESA_VERBOSE
& VERBOSE_API
)
1168 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1169 compile_shader(ctx
, shaderObj
);
1174 _mesa_CreateShader(GLenum type
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 if (MESA_VERBOSE
& VERBOSE_API
)
1178 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1179 return create_shader(ctx
, type
);
1183 GLhandleARB GLAPIENTRY
1184 _mesa_CreateShaderObjectARB(GLenum type
)
1186 GET_CURRENT_CONTEXT(ctx
);
1187 return create_shader(ctx
, type
);
1192 _mesa_CreateProgram(void)
1194 GET_CURRENT_CONTEXT(ctx
);
1195 if (MESA_VERBOSE
& VERBOSE_API
)
1196 _mesa_debug(ctx
, "glCreateProgram\n");
1197 return create_shader_program(ctx
);
1201 GLhandleARB GLAPIENTRY
1202 _mesa_CreateProgramObjectARB(void)
1204 GET_CURRENT_CONTEXT(ctx
);
1205 return create_shader_program(ctx
);
1210 _mesa_DeleteObjectARB(GLhandleARB obj
)
1212 if (MESA_VERBOSE
& VERBOSE_API
) {
1213 GET_CURRENT_CONTEXT(ctx
);
1214 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1218 GET_CURRENT_CONTEXT(ctx
);
1219 FLUSH_VERTICES(ctx
, 0);
1220 if (is_program(ctx
, obj
)) {
1221 delete_shader_program(ctx
, obj
);
1223 else if (is_shader(ctx
, obj
)) {
1224 delete_shader(ctx
, obj
);
1234 _mesa_DeleteProgram(GLuint name
)
1237 GET_CURRENT_CONTEXT(ctx
);
1238 FLUSH_VERTICES(ctx
, 0);
1239 delete_shader_program(ctx
, name
);
1245 _mesa_DeleteShader(GLuint name
)
1248 GET_CURRENT_CONTEXT(ctx
);
1249 FLUSH_VERTICES(ctx
, 0);
1250 delete_shader(ctx
, name
);
1256 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1258 GET_CURRENT_CONTEXT(ctx
);
1259 detach_shader(ctx
, program
, shader
);
1264 _mesa_DetachShader(GLuint program
, GLuint shader
)
1266 GET_CURRENT_CONTEXT(ctx
);
1267 detach_shader(ctx
, program
, shader
);
1272 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1273 GLsizei
* count
, GLhandleARB
* obj
)
1275 GET_CURRENT_CONTEXT(ctx
);
1276 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1281 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1282 GLsizei
*count
, GLuint
*obj
)
1284 GET_CURRENT_CONTEXT(ctx
);
1285 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1290 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1291 GLcharARB
* infoLog
)
1293 GET_CURRENT_CONTEXT(ctx
);
1294 if (is_program(ctx
, object
)) {
1295 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1297 else if (is_shader(ctx
, object
)) {
1298 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1301 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1307 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 /* Implement in terms of GetProgramiv, GetShaderiv */
1311 if (is_program(ctx
, object
)) {
1312 if (pname
== GL_OBJECT_TYPE_ARB
) {
1313 *params
= GL_PROGRAM_OBJECT_ARB
;
1316 get_programiv(ctx
, object
, pname
, params
);
1319 else if (is_shader(ctx
, object
)) {
1320 if (pname
== GL_OBJECT_TYPE_ARB
) {
1321 *params
= GL_SHADER_OBJECT_ARB
;
1324 get_shaderiv(ctx
, object
, pname
, params
);
1328 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1334 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1337 GLint iparams
[1]; /* XXX is one element enough? */
1338 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1339 params
[0] = (GLfloat
) iparams
[0];
1344 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1346 GET_CURRENT_CONTEXT(ctx
);
1347 get_programiv(ctx
, program
, pname
, params
);
1352 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1354 GET_CURRENT_CONTEXT(ctx
);
1355 get_shaderiv(ctx
, shader
, pname
, params
);
1360 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1361 GLsizei
*length
, GLchar
*infoLog
)
1363 GET_CURRENT_CONTEXT(ctx
);
1364 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1369 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1370 GLsizei
*length
, GLchar
*infoLog
)
1372 GET_CURRENT_CONTEXT(ctx
);
1373 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1378 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1379 GLsizei
*length
, GLcharARB
*sourceOut
)
1381 GET_CURRENT_CONTEXT(ctx
);
1382 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1386 GLhandleARB GLAPIENTRY
1387 _mesa_GetHandleARB(GLenum pname
)
1389 GET_CURRENT_CONTEXT(ctx
);
1390 return get_handle(ctx
, pname
);
1394 GLboolean GLAPIENTRY
1395 _mesa_IsProgram(GLuint name
)
1397 GET_CURRENT_CONTEXT(ctx
);
1398 return is_program(ctx
, name
);
1402 GLboolean GLAPIENTRY
1403 _mesa_IsShader(GLuint name
)
1405 GET_CURRENT_CONTEXT(ctx
);
1406 return is_shader(ctx
, name
);
1411 _mesa_LinkProgram(GLhandleARB programObj
)
1413 GET_CURRENT_CONTEXT(ctx
);
1414 link_program(ctx
, programObj
);
1420 * Read shader source code from a file.
1421 * Useful for debugging to override an app's shader.
1424 read_shader(const char *fname
)
1426 int shader_size
= 0;
1427 FILE *f
= fopen(fname
, "r");
1428 GLcharARB
*buffer
, *shader
;
1435 /* allocate enough room for the entire shader */
1436 fseek(f
, 0, SEEK_END
);
1437 shader_size
= ftell(f
);
1439 assert(shader_size
);
1441 /* add one for terminating zero */
1444 buffer
= malloc(shader_size
);
1447 len
= fread(buffer
, 1, shader_size
, f
);
1452 shader
= strdup(buffer
);
1460 * Called via glShaderSource() and glShaderSourceARB() API functions.
1461 * Basically, concatenate the source code strings into one long string
1462 * and pass it to _mesa_shader_source().
1465 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1466 const GLcharARB
* const * string
, const GLint
* length
)
1468 GET_CURRENT_CONTEXT(ctx
);
1470 GLsizei i
, totalLength
;
1474 if (!shaderObj
|| string
== NULL
) {
1475 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1480 * This array holds offsets of where the appropriate string ends, thus the
1481 * last element will be set to the total length of the source code.
1483 offsets
= malloc(count
* sizeof(GLint
));
1484 if (offsets
== NULL
) {
1485 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1489 for (i
= 0; i
< count
; i
++) {
1490 if (string
[i
] == NULL
) {
1491 free((GLvoid
*) offsets
);
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1493 "glShaderSourceARB(null string)");
1496 if (length
== NULL
|| length
[i
] < 0)
1497 offsets
[i
] = strlen(string
[i
]);
1499 offsets
[i
] = length
[i
];
1500 /* accumulate string lengths */
1502 offsets
[i
] += offsets
[i
- 1];
1505 /* Total length of source string is sum off all strings plus two.
1506 * One extra byte for terminating zero, another extra byte to silence
1507 * valgrind warnings in the parser/grammer code.
1509 totalLength
= offsets
[count
- 1] + 2;
1510 source
= malloc(totalLength
* sizeof(GLcharARB
));
1511 if (source
== NULL
) {
1512 free((GLvoid
*) offsets
);
1513 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1517 for (i
= 0; i
< count
; i
++) {
1518 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1519 memcpy(source
+ start
, string
[i
],
1520 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1522 source
[totalLength
- 1] = '\0';
1523 source
[totalLength
- 2] = '\0';
1526 /* Compute the shader's source code checksum then try to open a file
1527 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1528 * original shader source code. For debugging.
1531 GLcharARB
*newSource
;
1533 checksum
= _mesa_str_checksum(source
);
1535 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1537 newSource
= read_shader(filename
);
1539 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1540 shaderObj
, checksum
, filename
);
1546 shader_source(ctx
, shaderObj
, source
);
1549 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1551 sh
->SourceChecksum
= checksum
; /* save original checksum */
1559 _mesa_UseProgram(GLhandleARB program
)
1561 GET_CURRENT_CONTEXT(ctx
);
1562 struct gl_shader_program
*shProg
;
1564 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1566 "glUseProgram(transform feedback active)");
1571 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1575 if (!shProg
->LinkStatus
) {
1576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1577 "glUseProgram(program %u not linked)", program
);
1582 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1583 print_shader_info(shProg
);
1590 /* The ARB_separate_shader_object spec says:
1592 * "The executable code for an individual shader stage is taken from
1593 * the current program for that stage. If there is a current program
1594 * object established by UseProgram, that program is considered current
1595 * for all stages. Otherwise, if there is a bound program pipeline
1596 * object (section 2.14.PPO), the program bound to the appropriate
1597 * stage of the pipeline object is considered current."
1600 /* Attach shader state to the binding point */
1601 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1602 /* Update the program */
1603 _mesa_use_program(ctx
, shProg
);
1605 /* Must be done first: detach the progam */
1606 _mesa_use_program(ctx
, shProg
);
1607 /* Unattach shader_state binding point */
1608 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1609 /* If a pipeline was bound, rebind it */
1610 if (ctx
->Pipeline
.Current
) {
1611 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1618 _mesa_ValidateProgram(GLhandleARB program
)
1620 GET_CURRENT_CONTEXT(ctx
);
1621 validate_program(ctx
, program
);
1626 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1629 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1630 GLint
* range
, GLint
* precision
)
1632 const struct gl_program_constants
*limits
;
1633 const struct gl_precision
*p
;
1634 GET_CURRENT_CONTEXT(ctx
);
1636 switch (shadertype
) {
1637 case GL_VERTEX_SHADER
:
1638 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1640 case GL_FRAGMENT_SHADER
:
1641 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1644 _mesa_error(ctx
, GL_INVALID_ENUM
,
1645 "glGetShaderPrecisionFormat(shadertype)");
1649 switch (precisiontype
) {
1651 p
= &limits
->LowFloat
;
1653 case GL_MEDIUM_FLOAT
:
1654 p
= &limits
->MediumFloat
;
1657 p
= &limits
->HighFloat
;
1660 p
= &limits
->LowInt
;
1663 p
= &limits
->MediumInt
;
1666 p
= &limits
->HighInt
;
1669 _mesa_error(ctx
, GL_INVALID_ENUM
,
1670 "glGetShaderPrecisionFormat(precisiontype)");
1674 range
[0] = p
->RangeMin
;
1675 range
[1] = p
->RangeMax
;
1676 precision
[0] = p
->Precision
;
1681 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1684 _mesa_ReleaseShaderCompiler(void)
1686 _mesa_destroy_shader_compiler_caches();
1691 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1694 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1695 const void* binary
, GLint length
)
1697 GET_CURRENT_CONTEXT(ctx
);
1700 (void) binaryformat
;
1703 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary");
1708 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1709 GLenum
*binaryFormat
, GLvoid
*binary
)
1711 struct gl_shader_program
*shProg
;
1712 GLsizei length_dummy
;
1713 GET_CURRENT_CONTEXT(ctx
);
1716 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1720 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1724 /* The ARB_get_program_binary spec says:
1726 * "If <length> is NULL, then no length is returned."
1728 * Ensure that length always points to valid storage to avoid multiple NULL
1729 * pointer checks below.
1732 length
= &length_dummy
;
1735 /* The ARB_get_program_binary spec says:
1737 * "When a program object's LINK_STATUS is FALSE, its program binary
1738 * length is zero, and a call to GetProgramBinary will generate an
1739 * INVALID_OPERATION error.
1741 if (!shProg
->LinkStatus
) {
1742 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1743 "glGetProgramBinary(program %u not linked)",
1750 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1751 "glGetProgramBinary(driver supports zero binary formats)");
1753 (void) binaryFormat
;
1758 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1759 const GLvoid
*binary
, GLsizei length
)
1761 struct gl_shader_program
*shProg
;
1762 GET_CURRENT_CONTEXT(ctx
);
1764 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1768 (void) binaryFormat
;
1771 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
1773 * "If a negative number is provided where an argument of type sizei or
1774 * sizeiptr is specified, an INVALID_VALUE error is generated."
1777 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
1781 /* The ARB_get_program_binary spec says:
1783 * "<binaryFormat> and <binary> must be those returned by a previous
1784 * call to GetProgramBinary, and <length> must be the length of the
1785 * program binary as returned by GetProgramBinary or GetProgramiv with
1786 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
1787 * setting the LINK_STATUS of <program> to FALSE, if these conditions
1790 * Since any value of binaryFormat passed "is not one of those specified as
1791 * allowable for [this] command, an INVALID_ENUM error is generated."
1793 shProg
->LinkStatus
= GL_FALSE
;
1794 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
1799 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1801 struct gl_shader_program
*shProg
;
1802 GET_CURRENT_CONTEXT(ctx
);
1804 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1805 "glProgramParameteri");
1810 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1811 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1812 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1813 * even be in the dispatch table, so we shouldn't need to expclicitly
1816 * On desktop, we ignore the 3.0+ requirement because it is silly.
1819 /* The ARB_get_program_binary extension spec says:
1821 * "An INVALID_VALUE error is generated if the <value> argument to
1822 * ProgramParameteri is not TRUE or FALSE."
1824 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1828 /* No need to notify the driver. Any changes will actually take effect
1829 * the next time the shader is linked.
1831 * The ARB_get_program_binary extension spec says:
1833 * "To indicate that a program binary is likely to be retrieved,
1834 * ProgramParameteri should be called with <pname>
1835 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1836 * will not be in effect until the next time LinkProgram or
1837 * ProgramBinary has been called successfully."
1839 * The resloution of issue 9 in the extension spec also says:
1841 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1842 * to indicate to the GL implementation that this program will
1843 * likely be saved with GetProgramBinary at some point. This will
1844 * give the GL implementation the opportunity to track any state
1845 * changes made to the program before being saved such that when it
1846 * is loaded again a recompile can be avoided."
1848 shProg
->BinaryRetreivableHint
= value
;
1851 case GL_PROGRAM_SEPARABLE
:
1852 /* Spec imply that the behavior is the same as ARB_get_program_binary
1853 * Chapter 7.3 Program Objects
1855 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1858 shProg
->SeparateShader
= value
;
1862 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1863 _mesa_lookup_enum_by_nr(pname
));
1868 _mesa_error(ctx
, GL_INVALID_VALUE
,
1869 "glProgramParameteri(pname=%s, value=%d): "
1870 "value must be 0 or 1.",
1871 _mesa_lookup_enum_by_nr(pname
),
1877 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1878 struct gl_shader_program
*shProg
,
1879 struct gl_pipeline_object
*shTarget
)
1881 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
1882 use_shader_program(ctx
, stage
, shProg
, shTarget
);
1884 if (ctx
->Driver
.UseProgram
)
1885 ctx
->Driver
.UseProgram(ctx
, shProg
);
1890 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1891 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1893 const GLuint shader
= create_shader(ctx
, type
);
1897 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1899 compile_shader(ctx
, shader
);
1901 program
= create_shader_program(ctx
);
1903 struct gl_shader_program
*shProg
;
1904 struct gl_shader
*sh
;
1905 GLint compiled
= GL_FALSE
;
1907 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1908 sh
= _mesa_lookup_shader(ctx
, shader
);
1910 shProg
->SeparateShader
= separate
;
1912 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1914 attach_shader(ctx
, program
, shader
);
1915 link_program(ctx
, program
);
1916 detach_shader(ctx
, program
, shader
);
1920 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1921 append
-error
-to
-info
-log
;
1922 shProg
->LinkStatus
= GL_FALSE
;
1927 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1930 delete_shader(ctx
, shader
);
1938 * Copy program-specific data generated by linking from the gl_shader_program
1939 * object to a specific gl_program object.
1942 _mesa_copy_linked_program_data(gl_shader_stage type
,
1943 const struct gl_shader_program
*src
,
1944 struct gl_program
*dst
)
1947 case MESA_SHADER_VERTEX
:
1948 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1950 case MESA_SHADER_GEOMETRY
: {
1951 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1952 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1953 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1954 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1955 dst_gp
->InputType
= src
->Geom
.InputType
;
1956 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1957 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1958 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1959 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
1962 case MESA_SHADER_FRAGMENT
: {
1963 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
1964 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
1967 case MESA_SHADER_COMPUTE
: {
1968 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1970 for (i
= 0; i
< 3; i
++)
1971 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
1980 * ARB_separate_shader_objects: Compile & Link Program
1983 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
1984 const GLchar
* const *strings
)
1986 GET_CURRENT_CONTEXT(ctx
);
1988 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);