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/hash_table.h"
46 #include "main/mtypes.h"
47 #include "main/pipelineobj.h"
48 #include "main/shaderapi.h"
49 #include "main/shaderobj.h"
50 #include "main/transformfeedback.h"
51 #include "main/uniforms.h"
52 #include "program/program.h"
53 #include "program/prog_print.h"
54 #include "program/prog_parameter.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
= _mesa_getenv("MESA_GLSL");
76 if (strstr(env
, "dump_on_error"))
77 flags
|= GLSL_DUMP_ON_ERROR
;
78 else if (strstr(env
, "dump"))
80 if (strstr(env
, "log"))
82 if (strstr(env
, "nopvert"))
83 flags
|= GLSL_NOP_VERT
;
84 if (strstr(env
, "nopfrag"))
85 flags
|= GLSL_NOP_FRAG
;
86 if (strstr(env
, "nopt"))
88 else if (strstr(env
, "opt"))
90 if (strstr(env
, "uniform"))
91 flags
|= GLSL_UNIFORMS
;
92 if (strstr(env
, "useprog"))
93 flags
|= GLSL_USE_PROG
;
94 if (strstr(env
, "errors"))
95 flags
|= GLSL_REPORT_ERRORS
;
103 * Initialize context's shader state.
106 _mesa_init_shader_state(struct gl_context
*ctx
)
108 /* Device drivers may override these to control what kind of instructions
109 * are generated by the GLSL compiler.
111 struct gl_shader_compiler_options options
;
114 memset(&options
, 0, sizeof(options
));
115 options
.MaxUnrollIterations
= 32;
116 options
.MaxIfDepth
= UINT_MAX
;
118 /* Default pragma settings */
119 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
121 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
122 memcpy(&ctx
->ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
124 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
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
= (struct gl_shader
**)
275 _mesa_realloc(shProg
->Shaders
,
276 n
* sizeof(struct gl_shader
*),
277 (n
+ 1) * sizeof(struct gl_shader
*));
278 if (!shProg
->Shaders
) {
279 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
284 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
285 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
286 shProg
->NumShaders
++;
291 create_shader(struct gl_context
*ctx
, GLenum type
)
293 struct gl_shader
*sh
;
296 if (!_mesa_validate_shader_target(ctx
, type
)) {
297 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
301 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
302 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
303 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
310 create_shader_program(struct gl_context
*ctx
)
313 struct gl_shader_program
*shProg
;
315 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
317 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
319 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
321 assert(shProg
->RefCount
== 1);
328 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
332 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
335 * NOTE: deleting shaders/programs works a bit differently than
336 * texture objects (and buffer objects, etc). Shader/program
337 * handles/IDs exist in the hash table until the object is really
338 * deleted (refcount==0). With texture objects, the handle/ID is
339 * removed from the hash table in glDeleteTextures() while the tex
340 * object itself might linger until its refcount goes to zero.
342 struct gl_shader_program
*shProg
;
344 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
348 if (!shProg
->DeletePending
) {
349 shProg
->DeletePending
= GL_TRUE
;
351 /* effectively, decr shProg's refcount */
352 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
358 delete_shader(struct gl_context
*ctx
, GLuint shader
)
360 struct gl_shader
*sh
;
362 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
366 if (!sh
->DeletePending
) {
367 sh
->DeletePending
= GL_TRUE
;
369 /* effectively, decr sh's refcount */
370 _mesa_reference_shader(ctx
, &sh
, NULL
);
376 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
378 struct gl_shader_program
*shProg
;
382 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
386 n
= shProg
->NumShaders
;
388 for (i
= 0; i
< n
; i
++) {
389 if (shProg
->Shaders
[i
]->Name
== shader
) {
391 struct gl_shader
**newList
;
394 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
396 /* alloc new, smaller array */
397 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
399 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
402 /* Copy old list entries to new list, skipping removed entry at [i] */
403 for (j
= 0; j
< i
; j
++) {
404 newList
[j
] = shProg
->Shaders
[j
];
407 newList
[j
++] = shProg
->Shaders
[i
];
410 /* Free old list and install new one */
411 free(shProg
->Shaders
);
412 shProg
->Shaders
= newList
;
413 shProg
->NumShaders
= n
- 1;
416 /* sanity check - make sure the new list's entries are sensible */
417 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
418 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
419 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
420 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
421 assert(shProg
->Shaders
[j
]->RefCount
> 0);
432 if (is_shader(ctx
, shader
))
433 err
= GL_INVALID_OPERATION
;
434 else if (is_program(ctx
, shader
))
435 err
= GL_INVALID_OPERATION
;
437 err
= GL_INVALID_VALUE
;
438 _mesa_error(ctx
, err
, "glDetachShader(shader)");
445 * Return list of shaders attached to shader program.
448 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
449 GLsizei
*count
, GLuint
*obj
)
451 struct gl_shader_program
*shProg
=
452 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
455 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
456 obj
[i
] = shProg
->Shaders
[i
]->Name
;
465 * glGetHandleARB() - return ID/name of currently bound shader program.
468 get_handle(struct gl_context
*ctx
, GLenum pname
)
470 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
471 if (ctx
->Shader
.ActiveProgram
)
472 return ctx
->Shader
.ActiveProgram
->Name
;
477 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
484 * Check if a geometry shader query is valid at this time. If not, report an
485 * error and return false.
487 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
489 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
490 * are queried for a program which has not been linked successfully, or
491 * which does not contain objects to form a geometry shader, then an
492 * INVALID_OPERATION error is generated."
495 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
497 if (shProg
->LinkStatus
&&
498 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
502 _mesa_error(ctx
, GL_INVALID_OPERATION
,
503 "glGetProgramv(linked geometry shader required)");
509 * glGetProgramiv() - get shader program state.
510 * Note that this is for GLSL shader programs, not ARB vertex/fragment
511 * programs (see glGetProgramivARB).
514 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
516 struct gl_shader_program
*shProg
517 = _mesa_lookup_shader_program(ctx
, program
);
519 /* Is transform feedback available in this context?
522 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
523 || ctx
->API
== API_OPENGL_CORE
524 || _mesa_is_gles3(ctx
);
526 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
527 * and GL 3.2) are available in this context
529 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
531 /* Are uniform buffer objects available in this context?
534 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
535 || ctx
->API
== API_OPENGL_CORE
536 || _mesa_is_gles3(ctx
);
539 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
544 case GL_DELETE_STATUS
:
545 *params
= shProg
->DeletePending
;
548 *params
= shProg
->LinkStatus
;
550 case GL_VALIDATE_STATUS
:
551 *params
= shProg
->Validated
;
553 case GL_INFO_LOG_LENGTH
:
554 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
556 case GL_ATTACHED_SHADERS
:
557 *params
= shProg
->NumShaders
;
559 case GL_ACTIVE_ATTRIBUTES
:
560 *params
= _mesa_count_active_attribs(shProg
);
562 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
563 *params
= _mesa_longest_attribute_name_length(shProg
);
565 case GL_ACTIVE_UNIFORMS
:
566 *params
= shProg
->NumUserUniformStorage
;
568 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
572 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
573 /* Add one for the terminating NUL character for a non-array, and
574 * 4 for the "[0]" and the NUL for an array.
576 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
577 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
586 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
589 *params
= shProg
->TransformFeedback
.NumVarying
;
591 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
597 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
598 /* Add one for the terminating NUL character.
600 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
609 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
612 *params
= shProg
->TransformFeedback
.BufferMode
;
614 case GL_GEOMETRY_VERTICES_OUT
:
617 if (check_gs_query(ctx
, shProg
))
618 *params
= shProg
->Geom
.VerticesOut
;
620 case GL_GEOMETRY_SHADER_INVOCATIONS
:
621 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
623 if (check_gs_query(ctx
, shProg
))
624 *params
= shProg
->Geom
.Invocations
;
626 case GL_GEOMETRY_INPUT_TYPE
:
629 if (check_gs_query(ctx
, shProg
))
630 *params
= shProg
->Geom
.InputType
;
632 case GL_GEOMETRY_OUTPUT_TYPE
:
635 if (check_gs_query(ctx
, shProg
))
636 *params
= shProg
->Geom
.OutputType
;
638 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
645 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
646 /* Add one for the terminating NUL character.
648 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
657 case GL_ACTIVE_UNIFORM_BLOCKS
:
661 *params
= shProg
->NumUniformBlocks
;
663 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
664 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
665 * only available with desktop OpenGL 3.0+ with the
666 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
668 * On desktop, we ignore the 3.0+ requirement because it is silly.
670 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
673 *params
= shProg
->BinaryRetreivableHint
;
675 case GL_PROGRAM_BINARY_LENGTH
:
678 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
679 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
682 *params
= shProg
->NumAtomicBuffers
;
684 case GL_COMPUTE_WORK_GROUP_SIZE
: {
686 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
688 if (!shProg
->LinkStatus
) {
689 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
693 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
694 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
698 for (i
= 0; i
< 3; i
++)
699 params
[i
] = shProg
->Comp
.LocalSize
[i
];
702 case GL_PROGRAM_SEPARABLE
:
703 if (!ctx
->Extensions
.ARB_separate_shader_objects
)
706 *params
= shProg
->SeparateShader
;
712 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
713 _mesa_lookup_enum_by_nr(pname
));
718 * glGetShaderiv() - get GLSL shader state
721 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
723 struct gl_shader
*shader
=
724 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
732 *params
= shader
->Type
;
734 case GL_DELETE_STATUS
:
735 *params
= shader
->DeletePending
;
737 case GL_COMPILE_STATUS
:
738 *params
= shader
->CompileStatus
;
740 case GL_INFO_LOG_LENGTH
:
741 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
743 case GL_SHADER_SOURCE_LENGTH
:
744 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
747 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
754 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
755 GLsizei
*length
, GLchar
*infoLog
)
757 struct gl_shader_program
*shProg
758 = _mesa_lookup_shader_program(ctx
, program
);
760 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
763 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
768 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
769 GLsizei
*length
, GLchar
*infoLog
)
771 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
773 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
776 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
781 * Return shader source code.
784 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
785 GLsizei
*length
, GLchar
*sourceOut
)
787 struct gl_shader
*sh
;
788 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
792 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
797 * Set/replace shader source code. A helper function used by
798 * glShaderSource[ARB] and glCreateShaderProgramEXT.
801 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
803 struct gl_shader
*sh
;
805 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
809 /* free old shader source string and install new one */
810 free((void *)sh
->Source
);
812 sh
->CompileStatus
= GL_FALSE
;
814 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
823 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
825 struct gl_shader
*sh
;
826 struct gl_shader_compiler_options
*options
;
828 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
832 options
= &ctx
->ShaderCompilerOptions
[sh
->Stage
];
834 /* set default pragma state for shader */
835 sh
->Pragmas
= options
->DefaultPragmas
;
838 /* If the user called glCompileShader without first calling
839 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
841 sh
->CompileStatus
= GL_FALSE
;
843 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
844 fprintf(stderr
, "GLSL source for %s shader %d:\n",
845 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
846 fprintf(stderr
, "%s\n", sh
->Source
);
850 /* this call will set the shader->CompileStatus field to indicate if
851 * compilation was successful.
853 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
855 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
856 _mesa_write_shader_to_file(sh
);
859 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
860 if (sh
->CompileStatus
) {
861 fprintf(stderr
, "GLSL IR for shader %d:\n", sh
->Name
);
862 _mesa_print_ir(stderr
, sh
->ir
, NULL
);
863 fprintf(stderr
, "\n\n");
865 fprintf(stderr
, "GLSL shader %d failed to compile.\n", sh
->Name
);
867 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
868 fprintf(stderr
, "GLSL shader %d info log:\n", sh
->Name
);
869 fprintf(stderr
, "%s\n", sh
->InfoLog
);
876 if (!sh
->CompileStatus
) {
877 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
878 fprintf(stderr
, "GLSL source for %s shader %d:\n",
879 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
880 fprintf(stderr
, "%s\n", sh
->Source
);
881 fprintf(stderr
, "Info Log:\n%s\n", sh
->InfoLog
);
885 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
886 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
887 sh
->Name
, sh
->InfoLog
);
894 * Link a program's shaders.
897 link_program(struct gl_context
*ctx
, GLuint program
)
899 struct gl_shader_program
*shProg
;
901 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
905 /* From the ARB_transform_feedback2 specification:
906 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
907 * the name of a program being used by one or more transform feedback
908 * objects, even if the objects are not currently bound or are paused."
910 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
911 _mesa_error(ctx
, GL_INVALID_OPERATION
,
912 "glLinkProgram(transform feedback is using the program)");
916 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
918 _mesa_glsl_link_shader(ctx
, shProg
);
920 if (shProg
->LinkStatus
== GL_FALSE
&&
921 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
922 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
923 shProg
->Name
, shProg
->InfoLog
);
930 printf("Link %u shaders in program %u: %s\n",
931 shProg
->NumShaders
, shProg
->Name
,
932 shProg
->LinkStatus
? "Success" : "Failed");
934 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
935 printf(" shader %u, type 0x%x\n",
936 shProg
->Shaders
[i
]->Name
,
937 shProg
->Shaders
[i
]->Type
);
944 * Print basic shader info (for debug).
947 print_shader_info(const struct gl_shader_program
*shProg
)
951 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
952 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
953 printf(" %s shader %u, checksum %u\n",
954 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
955 shProg
->Shaders
[i
]->Name
,
956 shProg
->Shaders
[i
]->SourceChecksum
);
958 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
959 printf(" vert prog %u\n",
960 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
961 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
962 printf(" frag prog %u\n",
963 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
964 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
965 printf(" geom prog %u\n",
966 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
971 * Use the named shader program for subsequent glUniform calls
974 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
977 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
978 _mesa_error(ctx
, GL_INVALID_OPERATION
,
979 "%s(program %u not linked)", caller
, shProg
->Name
);
983 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
984 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
991 use_shader_program(struct gl_context
*ctx
, GLenum type
,
992 struct gl_shader_program
*shProg
)
994 struct gl_shader_program
**target
;
995 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
997 target
= &ctx
->Shader
.CurrentProgram
[stage
];
998 if ((shProg
== NULL
) || (shProg
->_LinkedShaders
[stage
] == NULL
))
1001 if (*target
!= shProg
) {
1002 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1004 /* If the shader is also bound as the current rendering shader, unbind
1005 * it from that binding point as well. This ensures that the correct
1006 * semantics of glDeleteProgram are maintained.
1009 case GL_VERTEX_SHADER
:
1010 /* Empty for now. */
1012 case GL_GEOMETRY_SHADER_ARB
:
1013 /* Empty for now. */
1015 case GL_COMPUTE_SHADER
:
1016 /* Empty for now. */
1018 case GL_FRAGMENT_SHADER
:
1019 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
1020 _mesa_reference_shader_program(ctx
,
1021 &ctx
->Shader
._CurrentFragmentProgram
,
1027 _mesa_reference_shader_program(ctx
, target
, shProg
);
1033 * Use the named shader program for subsequent rendering.
1036 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1038 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
1039 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
1040 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
1041 use_shader_program(ctx
, GL_COMPUTE_SHADER
, shProg
);
1042 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1044 if (ctx
->Driver
.UseProgram
)
1045 ctx
->Driver
.UseProgram(ctx
, shProg
);
1050 * Do validation of the given shader program.
1051 * \param errMsg returns error message if validation fails.
1052 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1055 validate_shader_program(const struct gl_shader_program
*shProg
,
1058 if (!shProg
->LinkStatus
) {
1062 /* From the GL spec, a program is invalid if any of these are true:
1064 any two active samplers in the current program object are of
1065 different types, but refer to the same texture image unit,
1067 any active sampler in the current program object refers to a texture
1068 image unit where fixed-function fragment processing accesses a
1069 texture target that does not match the sampler type, or
1071 the sum of the number of active samplers in the program and the
1072 number of texture image units enabled for fixed-function fragment
1073 processing exceeds the combined limit on the total number of texture
1074 image units allowed.
1079 * Check: any two active samplers in the current program object are of
1080 * different types, but refer to the same texture image unit,
1082 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1090 * Called via glValidateProgram()
1093 validate_program(struct gl_context
*ctx
, GLuint program
)
1095 struct gl_shader_program
*shProg
;
1096 char errMsg
[100] = "";
1098 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1103 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1104 if (!shProg
->Validated
) {
1105 /* update info log */
1106 if (shProg
->InfoLog
) {
1107 ralloc_free(shProg
->InfoLog
);
1109 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1116 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1118 GET_CURRENT_CONTEXT(ctx
);
1119 attach_shader(ctx
, program
, shader
);
1124 _mesa_AttachShader(GLuint program
, GLuint shader
)
1126 GET_CURRENT_CONTEXT(ctx
);
1127 attach_shader(ctx
, program
, shader
);
1132 _mesa_CompileShader(GLhandleARB shaderObj
)
1134 GET_CURRENT_CONTEXT(ctx
);
1135 if (MESA_VERBOSE
& VERBOSE_API
)
1136 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1137 compile_shader(ctx
, shaderObj
);
1142 _mesa_CreateShader(GLenum type
)
1144 GET_CURRENT_CONTEXT(ctx
);
1145 if (MESA_VERBOSE
& VERBOSE_API
)
1146 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1147 return create_shader(ctx
, type
);
1151 GLhandleARB GLAPIENTRY
1152 _mesa_CreateShaderObjectARB(GLenum type
)
1154 GET_CURRENT_CONTEXT(ctx
);
1155 return create_shader(ctx
, type
);
1160 _mesa_CreateProgram(void)
1162 GET_CURRENT_CONTEXT(ctx
);
1163 if (MESA_VERBOSE
& VERBOSE_API
)
1164 _mesa_debug(ctx
, "glCreateProgram\n");
1165 return create_shader_program(ctx
);
1169 GLhandleARB GLAPIENTRY
1170 _mesa_CreateProgramObjectARB(void)
1172 GET_CURRENT_CONTEXT(ctx
);
1173 return create_shader_program(ctx
);
1178 _mesa_DeleteObjectARB(GLhandleARB obj
)
1180 if (MESA_VERBOSE
& VERBOSE_API
) {
1181 GET_CURRENT_CONTEXT(ctx
);
1182 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1186 GET_CURRENT_CONTEXT(ctx
);
1187 FLUSH_VERTICES(ctx
, 0);
1188 if (is_program(ctx
, obj
)) {
1189 delete_shader_program(ctx
, obj
);
1191 else if (is_shader(ctx
, obj
)) {
1192 delete_shader(ctx
, obj
);
1202 _mesa_DeleteProgram(GLuint name
)
1205 GET_CURRENT_CONTEXT(ctx
);
1206 FLUSH_VERTICES(ctx
, 0);
1207 delete_shader_program(ctx
, name
);
1213 _mesa_DeleteShader(GLuint name
)
1216 GET_CURRENT_CONTEXT(ctx
);
1217 FLUSH_VERTICES(ctx
, 0);
1218 delete_shader(ctx
, name
);
1224 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1226 GET_CURRENT_CONTEXT(ctx
);
1227 detach_shader(ctx
, program
, shader
);
1232 _mesa_DetachShader(GLuint program
, GLuint shader
)
1234 GET_CURRENT_CONTEXT(ctx
);
1235 detach_shader(ctx
, program
, shader
);
1240 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1241 GLsizei
* count
, GLhandleARB
* obj
)
1243 GET_CURRENT_CONTEXT(ctx
);
1244 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1249 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1250 GLsizei
*count
, GLuint
*obj
)
1252 GET_CURRENT_CONTEXT(ctx
);
1253 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1258 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1259 GLcharARB
* infoLog
)
1261 GET_CURRENT_CONTEXT(ctx
);
1262 if (is_program(ctx
, object
)) {
1263 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1265 else if (is_shader(ctx
, object
)) {
1266 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1269 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1275 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1277 GET_CURRENT_CONTEXT(ctx
);
1278 /* Implement in terms of GetProgramiv, GetShaderiv */
1279 if (is_program(ctx
, object
)) {
1280 if (pname
== GL_OBJECT_TYPE_ARB
) {
1281 *params
= GL_PROGRAM_OBJECT_ARB
;
1284 get_programiv(ctx
, object
, pname
, params
);
1287 else if (is_shader(ctx
, object
)) {
1288 if (pname
== GL_OBJECT_TYPE_ARB
) {
1289 *params
= GL_SHADER_OBJECT_ARB
;
1292 get_shaderiv(ctx
, object
, pname
, params
);
1296 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1302 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1305 GLint iparams
[1]; /* XXX is one element enough? */
1306 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1307 params
[0] = (GLfloat
) iparams
[0];
1312 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1314 GET_CURRENT_CONTEXT(ctx
);
1315 get_programiv(ctx
, program
, pname
, params
);
1320 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1322 GET_CURRENT_CONTEXT(ctx
);
1323 get_shaderiv(ctx
, shader
, pname
, params
);
1328 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1329 GLsizei
*length
, GLchar
*infoLog
)
1331 GET_CURRENT_CONTEXT(ctx
);
1332 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1337 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1338 GLsizei
*length
, GLchar
*infoLog
)
1340 GET_CURRENT_CONTEXT(ctx
);
1341 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1346 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1347 GLsizei
*length
, GLcharARB
*sourceOut
)
1349 GET_CURRENT_CONTEXT(ctx
);
1350 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1354 GLhandleARB GLAPIENTRY
1355 _mesa_GetHandleARB(GLenum pname
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 return get_handle(ctx
, pname
);
1362 GLboolean GLAPIENTRY
1363 _mesa_IsProgram(GLuint name
)
1365 GET_CURRENT_CONTEXT(ctx
);
1366 return is_program(ctx
, name
);
1370 GLboolean GLAPIENTRY
1371 _mesa_IsShader(GLuint name
)
1373 GET_CURRENT_CONTEXT(ctx
);
1374 return is_shader(ctx
, name
);
1379 _mesa_LinkProgram(GLhandleARB programObj
)
1381 GET_CURRENT_CONTEXT(ctx
);
1382 link_program(ctx
, programObj
);
1388 * Read shader source code from a file.
1389 * Useful for debugging to override an app's shader.
1392 read_shader(const char *fname
)
1394 const int max
= 50*1000;
1395 FILE *f
= fopen(fname
, "r");
1396 GLcharARB
*buffer
, *shader
;
1403 buffer
= malloc(max
);
1404 len
= fread(buffer
, 1, max
, f
);
1409 shader
= _mesa_strdup(buffer
);
1417 * Called via glShaderSource() and glShaderSourceARB() API functions.
1418 * Basically, concatenate the source code strings into one long string
1419 * and pass it to _mesa_shader_source().
1422 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1423 const GLcharARB
* const * string
, const GLint
* length
)
1425 GET_CURRENT_CONTEXT(ctx
);
1427 GLsizei i
, totalLength
;
1431 if (!shaderObj
|| string
== NULL
) {
1432 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1437 * This array holds offsets of where the appropriate string ends, thus the
1438 * last element will be set to the total length of the source code.
1440 offsets
= malloc(count
* sizeof(GLint
));
1441 if (offsets
== NULL
) {
1442 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1446 for (i
= 0; i
< count
; i
++) {
1447 if (string
[i
] == NULL
) {
1448 free((GLvoid
*) offsets
);
1449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1450 "glShaderSourceARB(null string)");
1453 if (length
== NULL
|| length
[i
] < 0)
1454 offsets
[i
] = strlen(string
[i
]);
1456 offsets
[i
] = length
[i
];
1457 /* accumulate string lengths */
1459 offsets
[i
] += offsets
[i
- 1];
1462 /* Total length of source string is sum off all strings plus two.
1463 * One extra byte for terminating zero, another extra byte to silence
1464 * valgrind warnings in the parser/grammer code.
1466 totalLength
= offsets
[count
- 1] + 2;
1467 source
= malloc(totalLength
* sizeof(GLcharARB
));
1468 if (source
== NULL
) {
1469 free((GLvoid
*) offsets
);
1470 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1474 for (i
= 0; i
< count
; i
++) {
1475 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1476 memcpy(source
+ start
, string
[i
],
1477 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1479 source
[totalLength
- 1] = '\0';
1480 source
[totalLength
- 2] = '\0';
1483 /* Compute the shader's source code checksum then try to open a file
1484 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1485 * original shader source code. For debugging.
1488 GLcharARB
*newSource
;
1490 checksum
= _mesa_str_checksum(source
);
1492 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1494 newSource
= read_shader(filename
);
1496 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1497 shaderObj
, checksum
, filename
);
1503 shader_source(ctx
, shaderObj
, source
);
1506 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1508 sh
->SourceChecksum
= checksum
; /* save original checksum */
1516 _mesa_UseProgram(GLhandleARB program
)
1518 GET_CURRENT_CONTEXT(ctx
);
1519 struct gl_shader_program
*shProg
;
1521 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1523 "glUseProgram(transform feedback active)");
1528 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1532 if (!shProg
->LinkStatus
) {
1533 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1534 "glUseProgram(program %u not linked)", program
);
1539 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1540 print_shader_info(shProg
);
1547 /* The "Dependencies on EXT_separate_shader_objects" section of the
1548 * ARB_separate_shader_object spec says:
1550 * "The executable code for an individual shader stage is taken from
1551 * the current program for that stage. If there is a current program
1552 * object for any shader stage or for uniform updates established by
1553 * UseProgram, UseShaderProgramEXT, or ActiveProgramEXT, the current
1554 * program for that stage (if any) is considered current. Otherwise,
1555 * if there is a bound program pipeline object ..."
1558 /* Attach shader state to the binding point */
1559 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1560 /* Update the program */
1561 _mesa_use_program(ctx
, shProg
);
1563 /* Must be done first: detach the progam */
1564 _mesa_use_program(ctx
, shProg
);
1565 /* Unattach shader_state binding point */
1566 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1567 /* If a pipeline was bound, rebind it */
1568 if (ctx
->Pipeline
.Current
) {
1569 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1576 _mesa_ValidateProgram(GLhandleARB program
)
1578 GET_CURRENT_CONTEXT(ctx
);
1579 validate_program(ctx
, program
);
1584 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1587 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1588 GLint
* range
, GLint
* precision
)
1590 const struct gl_program_constants
*limits
;
1591 const struct gl_precision
*p
;
1592 GET_CURRENT_CONTEXT(ctx
);
1594 switch (shadertype
) {
1595 case GL_VERTEX_SHADER
:
1596 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1598 case GL_FRAGMENT_SHADER
:
1599 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1602 _mesa_error(ctx
, GL_INVALID_ENUM
,
1603 "glGetShaderPrecisionFormat(shadertype)");
1607 switch (precisiontype
) {
1609 p
= &limits
->LowFloat
;
1611 case GL_MEDIUM_FLOAT
:
1612 p
= &limits
->MediumFloat
;
1615 p
= &limits
->HighFloat
;
1618 p
= &limits
->LowInt
;
1621 p
= &limits
->MediumInt
;
1624 p
= &limits
->HighInt
;
1627 _mesa_error(ctx
, GL_INVALID_ENUM
,
1628 "glGetShaderPrecisionFormat(precisiontype)");
1632 range
[0] = p
->RangeMin
;
1633 range
[1] = p
->RangeMax
;
1634 precision
[0] = p
->Precision
;
1639 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1642 _mesa_ReleaseShaderCompiler(void)
1644 _mesa_destroy_shader_compiler_caches();
1649 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1652 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1653 const void* binary
, GLint length
)
1655 GET_CURRENT_CONTEXT(ctx
);
1658 (void) binaryformat
;
1661 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1666 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1667 GLenum
*binaryFormat
, GLvoid
*binary
)
1669 struct gl_shader_program
*shProg
;
1670 GET_CURRENT_CONTEXT(ctx
);
1672 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1676 if (!shProg
->LinkStatus
) {
1677 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1678 "glGetProgramBinary(program %u not linked)",
1684 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1688 /* The ARB_get_program_binary spec says:
1690 * "If <length> is NULL, then no length is returned."
1695 (void) binaryFormat
;
1700 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1701 const GLvoid
*binary
, GLsizei length
)
1703 struct gl_shader_program
*shProg
;
1704 GET_CURRENT_CONTEXT(ctx
);
1706 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1710 (void) binaryFormat
;
1713 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1718 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1720 struct gl_shader_program
*shProg
;
1721 GET_CURRENT_CONTEXT(ctx
);
1723 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1724 "glProgramParameteri");
1729 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1730 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1731 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1732 * even be in the dispatch table, so we shouldn't need to expclicitly
1735 * On desktop, we ignore the 3.0+ requirement because it is silly.
1738 /* The ARB_get_program_binary extension spec says:
1740 * "An INVALID_VALUE error is generated if the <value> argument to
1741 * ProgramParameteri is not TRUE or FALSE."
1743 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1744 _mesa_error(ctx
, GL_INVALID_VALUE
,
1745 "glProgramParameteri(pname=%s, value=%d): "
1746 "value must be 0 or 1.",
1747 _mesa_lookup_enum_by_nr(pname
),
1752 /* No need to notify the driver. Any changes will actually take effect
1753 * the next time the shader is linked.
1755 * The ARB_get_program_binary extension spec says:
1757 * "To indicate that a program binary is likely to be retrieved,
1758 * ProgramParameteri should be called with <pname>
1759 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1760 * will not be in effect until the next time LinkProgram or
1761 * ProgramBinary has been called successfully."
1763 * The resloution of issue 9 in the extension spec also says:
1765 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1766 * to indicate to the GL implementation that this program will
1767 * likely be saved with GetProgramBinary at some point. This will
1768 * give the GL implementation the opportunity to track any state
1769 * changes made to the program before being saved such that when it
1770 * is loaded again a recompile can be avoided."
1772 shProg
->BinaryRetreivableHint
= value
;
1775 case GL_PROGRAM_SEPARABLE
:
1776 if (!ctx
->Extensions
.ARB_separate_shader_objects
)
1779 /* Spec imply that the behavior is the same as ARB_get_program_binary
1780 * Chapter 7.3 Program Objects
1782 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1783 _mesa_error(ctx
, GL_INVALID_VALUE
,
1784 "glProgramParameteri(pname=%s, value=%d): "
1785 "value must be 0 or 1.",
1786 _mesa_lookup_enum_by_nr(pname
),
1790 shProg
->SeparateShader
= value
;
1797 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1798 _mesa_lookup_enum_by_nr(pname
));
1802 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1803 struct gl_shader_program
*shProg
)
1805 use_shader_program(ctx
, type
, shProg
);
1807 if (ctx
->Driver
.UseProgram
)
1808 ctx
->Driver
.UseProgram(ctx
, shProg
);
1813 * For GL_EXT_separate_shader_objects
1816 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1818 GET_CURRENT_CONTEXT(ctx
);
1819 struct gl_shader_program
*shProg
= NULL
;
1821 if (!_mesa_validate_shader_target(ctx
, type
)) {
1822 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1826 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1828 "glUseShaderProgramEXT(transform feedback is active)");
1833 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1834 "glUseShaderProgramEXT");
1838 if (!shProg
->LinkStatus
) {
1839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1840 "glUseShaderProgramEXT(program not linked)");
1845 /* The "Dependencies on EXT_separate_shader_objects" section of the
1846 * ARB_separate_shader_object spec says:
1848 * "The executable code for an individual shader stage is taken from
1849 * the current program for that stage. If there is a current program
1850 * object for any shader stage or for uniform updates established by
1851 * UseProgram, UseShaderProgramEXT, or ActiveProgramEXT, the current
1852 * program for that stage (if any) is considered current. Otherwise,
1853 * if there is a bound program pipeline object ..."
1856 /* Attach shader state to the binding point */
1857 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1858 /* Update the program */
1859 _mesa_use_shader_program(ctx
, type
, shProg
);
1861 /* Must be done first: detach the progam */
1862 _mesa_use_shader_program(ctx
, type
, shProg
);
1864 /* Nothing remains current */
1865 if (!ctx
->Shader
.CurrentVertexProgram
&&
1866 !ctx
->Shader
.CurrentGeometryProgram
&&
1867 !ctx
->Shader
.CurrentFragmentProgram
&&
1868 !ctx
->Shader
.ActiveProgram
) {
1870 /* Unattach shader_state binding point */
1871 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1872 ctx
->Pipeline
.Default
);
1874 /* If a pipeline was bound, rebind it */
1875 if (ctx
->Pipeline
.Current
) {
1876 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1884 * For GL_EXT_separate_shader_objects
1887 _mesa_ActiveProgramEXT(GLuint program
)
1889 GET_CURRENT_CONTEXT(ctx
);
1890 struct gl_shader_program
*shProg
= (program
!= 0)
1891 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1894 /* The "Dependencies on EXT_separate_shader_objects" section of the
1895 * ARB_separate_shader_object spec says:
1897 * "The executable code for an individual shader stage is taken from
1898 * the current program for that stage. If there is a current program
1899 * object for any shader stage or for uniform updates established by
1900 * UseProgram, UseShaderProgramEXT, or ActiveProgramEXT, the current
1901 * program for that stage (if any) is considered current. Otherwise,
1902 * if there is a bound program pipeline object ..."
1904 if (shProg
!= NULL
) {
1905 /* Attach shader state to the binding point */
1906 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1907 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1909 /* Must be done first: unset the current active progam */
1910 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1912 /* Nothing remains current */
1913 if (!ctx
->Shader
.CurrentVertexProgram
&& !ctx
->Shader
.CurrentGeometryProgram
&&
1914 !ctx
->Shader
.CurrentFragmentProgram
&& !ctx
->Shader
.ActiveProgram
) {
1916 /* Unattach shader_state binding point */
1917 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1918 /* If a pipeline was bound, rebind it */
1919 if (ctx
->Pipeline
.Current
) {
1920 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1929 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1930 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1932 const GLuint shader
= create_shader(ctx
, type
);
1936 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1938 compile_shader(ctx
, shader
);
1940 program
= create_shader_program(ctx
);
1942 struct gl_shader_program
*shProg
;
1943 struct gl_shader
*sh
;
1944 GLint compiled
= GL_FALSE
;
1946 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1947 sh
= _mesa_lookup_shader(ctx
, shader
);
1949 shProg
->SeparateShader
= separate
;
1951 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1953 attach_shader(ctx
, program
, shader
);
1954 link_program(ctx
, program
);
1955 detach_shader(ctx
, program
, shader
);
1959 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1960 append
-error
-to
-info
-log
;
1961 shProg
->LinkStatus
= GL_FALSE
;
1966 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1969 delete_shader(ctx
, shader
);
1977 * Copy program-specific data generated by linking from the gl_shader_program
1978 * object to a specific gl_program object.
1981 _mesa_copy_linked_program_data(gl_shader_stage type
,
1982 const struct gl_shader_program
*src
,
1983 struct gl_program
*dst
)
1986 case MESA_SHADER_VERTEX
:
1987 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1989 case MESA_SHADER_GEOMETRY
: {
1990 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1991 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1992 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1993 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1994 dst_gp
->InputType
= src
->Geom
.InputType
;
1995 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1996 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1997 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
2000 case MESA_SHADER_COMPUTE
: {
2001 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
2003 for (i
= 0; i
< 3; i
++)
2004 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
2014 * For GL_EXT_separate_shader_objects
2017 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
2019 GET_CURRENT_CONTEXT(ctx
);
2021 return _mesa_create_shader_program(ctx
, GL_FALSE
, type
, 1, &string
);
2025 * ARB_separate_shader_objects: Compile & Link Program
2027 * Basically the same as _mesa_CreateShaderProgramEXT but with support of
2028 * multiple strings and sets the SeparateShader flag to true.
2031 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
2032 const GLchar
* const *strings
)
2034 GET_CURRENT_CONTEXT(ctx
);
2036 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);