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/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"
56 #include "../glsl/glsl_parser_extras.h"
57 #include "../glsl/ir.h"
58 #include "../glsl/ir_uniform.h"
59 #include "../glsl/program.h"
61 /** Define this to enable shader substitution (see below) */
62 #define SHADER_SUBST 0
66 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
69 get_shader_flags(void)
71 GLbitfield flags
= 0x0;
72 const char *env
= _mesa_getenv("MESA_GLSL");
75 if (strstr(env
, "dump_on_error"))
76 flags
|= GLSL_DUMP_ON_ERROR
;
77 else if (strstr(env
, "dump"))
79 if (strstr(env
, "log"))
81 if (strstr(env
, "nopvert"))
82 flags
|= GLSL_NOP_VERT
;
83 if (strstr(env
, "nopfrag"))
84 flags
|= GLSL_NOP_FRAG
;
85 if (strstr(env
, "nopt"))
87 else if (strstr(env
, "opt"))
89 if (strstr(env
, "uniform"))
90 flags
|= GLSL_UNIFORMS
;
91 if (strstr(env
, "useprog"))
92 flags
|= GLSL_USE_PROG
;
93 if (strstr(env
, "errors"))
94 flags
|= GLSL_REPORT_ERRORS
;
102 * Initialize context's shader state.
105 _mesa_init_shader_state(struct gl_context
*ctx
)
107 /* Device drivers may override these to control what kind of instructions
108 * are generated by the GLSL compiler.
110 struct gl_shader_compiler_options options
;
113 memset(&options
, 0, sizeof(options
));
114 options
.MaxUnrollIterations
= 32;
115 options
.MaxIfDepth
= UINT_MAX
;
117 /* Default pragma settings */
118 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
120 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
121 memcpy(&ctx
->ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
123 ctx
->Shader
.Flags
= get_shader_flags();
128 * Free the per-context shader-related state.
131 _mesa_free_shader_state(struct gl_context
*ctx
)
134 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
135 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
138 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
140 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
145 * Copy string from <src> to <dst>, up to maxLength characters, returning
146 * length of <dst> in <length>.
147 * \param src the strings source
148 * \param maxLength max chars to copy
149 * \param length returns number of chars copied
150 * \param dst the string destination
153 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
154 GLsizei
*length
, const GLchar
*src
)
157 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
168 * Confirm that the a shader type is valid and supported by the implementation
170 * \param ctx Current GL context
171 * \param type Shader target
175 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
177 /* Note: when building built-in GLSL functions, this function may be
178 * invoked with ctx == NULL. In that case, we can only validate that it's
179 * a shader target we recognize, not that it's supported in the current
180 * context. But that's fine--we don't need any further validation than
181 * that when building built-in GLSL functions.
185 case GL_FRAGMENT_SHADER
:
186 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
187 case GL_VERTEX_SHADER
:
188 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
189 case GL_GEOMETRY_SHADER_ARB
:
190 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
198 is_program(struct gl_context
*ctx
, GLuint name
)
200 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
201 return shProg
? GL_TRUE
: GL_FALSE
;
206 is_shader(struct gl_context
*ctx
, GLuint name
)
208 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
209 return shader
? GL_TRUE
: GL_FALSE
;
214 * Attach shader to a shader program.
217 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
219 struct gl_shader_program
*shProg
;
220 struct gl_shader
*sh
;
223 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
225 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
229 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
234 n
= shProg
->NumShaders
;
235 for (i
= 0; i
< n
; i
++) {
236 if (shProg
->Shaders
[i
] == sh
) {
237 /* The shader is already attched to this program. The
238 * GL_ARB_shader_objects spec says:
240 * "The error INVALID_OPERATION is generated by AttachObjectARB
241 * if <obj> is already attached to <containerObj>."
243 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
245 } else if (same_type_disallowed
&&
246 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
247 /* Shader with the same type is already attached to this program,
248 * OpenGL ES 2.0 and 3.0 specs say:
250 * "Multiple shader objects of the same type may not be attached
251 * to a single program object. [...] The error INVALID_OPERATION
252 * is generated if [...] another shader object of the same type
253 * as shader is already attached to program."
255 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
261 shProg
->Shaders
= (struct gl_shader
**)
262 _mesa_realloc(shProg
->Shaders
,
263 n
* sizeof(struct gl_shader
*),
264 (n
+ 1) * sizeof(struct gl_shader
*));
265 if (!shProg
->Shaders
) {
266 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
271 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
272 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
273 shProg
->NumShaders
++;
278 create_shader(struct gl_context
*ctx
, GLenum type
)
280 struct gl_shader
*sh
;
283 if (!_mesa_validate_shader_target(ctx
, type
)) {
284 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
288 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
289 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
290 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
297 create_shader_program(struct gl_context
*ctx
)
300 struct gl_shader_program
*shProg
;
302 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
304 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
306 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
308 assert(shProg
->RefCount
== 1);
315 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
319 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
322 * NOTE: deleting shaders/programs works a bit differently than
323 * texture objects (and buffer objects, etc). Shader/program
324 * handles/IDs exist in the hash table until the object is really
325 * deleted (refcount==0). With texture objects, the handle/ID is
326 * removed from the hash table in glDeleteTextures() while the tex
327 * object itself might linger until its refcount goes to zero.
329 struct gl_shader_program
*shProg
;
331 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
335 if (!shProg
->DeletePending
) {
336 shProg
->DeletePending
= GL_TRUE
;
338 /* effectively, decr shProg's refcount */
339 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
345 delete_shader(struct gl_context
*ctx
, GLuint shader
)
347 struct gl_shader
*sh
;
349 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
353 if (!sh
->DeletePending
) {
354 sh
->DeletePending
= GL_TRUE
;
356 /* effectively, decr sh's refcount */
357 _mesa_reference_shader(ctx
, &sh
, NULL
);
363 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
365 struct gl_shader_program
*shProg
;
369 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
373 n
= shProg
->NumShaders
;
375 for (i
= 0; i
< n
; i
++) {
376 if (shProg
->Shaders
[i
]->Name
== shader
) {
378 struct gl_shader
**newList
;
381 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
383 /* alloc new, smaller array */
385 malloc((n
- 1) * sizeof(struct gl_shader
*));
387 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
390 for (j
= 0; j
< i
; j
++) {
391 newList
[j
] = shProg
->Shaders
[j
];
394 newList
[j
++] = shProg
->Shaders
[i
];
395 free(shProg
->Shaders
);
397 shProg
->Shaders
= newList
;
398 shProg
->NumShaders
= n
- 1;
403 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
404 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
405 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
406 assert(shProg
->Shaders
[j
]->RefCount
> 0);
418 if (is_shader(ctx
, shader
))
419 err
= GL_INVALID_OPERATION
;
420 else if (is_program(ctx
, shader
))
421 err
= GL_INVALID_OPERATION
;
423 err
= GL_INVALID_VALUE
;
424 _mesa_error(ctx
, err
, "glDetachShader(shader)");
431 * Return list of shaders attached to shader program.
434 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
435 GLsizei
*count
, GLuint
*obj
)
437 struct gl_shader_program
*shProg
=
438 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
441 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
442 obj
[i
] = shProg
->Shaders
[i
]->Name
;
451 * glGetHandleARB() - return ID/name of currently bound shader program.
454 get_handle(struct gl_context
*ctx
, GLenum pname
)
456 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
457 if (ctx
->Shader
.ActiveProgram
)
458 return ctx
->Shader
.ActiveProgram
->Name
;
463 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
470 * Check if a geometry shader query is valid at this time. If not, report an
471 * error and return false.
473 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
475 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
476 * are queried for a program which has not been linked successfully, or
477 * which does not contain objects to form a geometry shader, then an
478 * INVALID_OPERATION error is generated."
481 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
483 if (shProg
->LinkStatus
&&
484 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
489 "glGetProgramv(linked geometry shader required)");
495 * glGetProgramiv() - get shader program state.
496 * Note that this is for GLSL shader programs, not ARB vertex/fragment
497 * programs (see glGetProgramivARB).
500 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
502 struct gl_shader_program
*shProg
503 = _mesa_lookup_shader_program(ctx
, program
);
505 /* Is transform feedback available in this context?
508 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
509 || ctx
->API
== API_OPENGL_CORE
510 || _mesa_is_gles3(ctx
);
512 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
513 * and GL 3.2) are available in this context
515 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
517 /* Are uniform buffer objects available in this context?
520 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
521 || ctx
->API
== API_OPENGL_CORE
522 || _mesa_is_gles3(ctx
);
525 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
530 case GL_DELETE_STATUS
:
531 *params
= shProg
->DeletePending
;
534 *params
= shProg
->LinkStatus
;
536 case GL_VALIDATE_STATUS
:
537 *params
= shProg
->Validated
;
539 case GL_INFO_LOG_LENGTH
:
540 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
542 case GL_ATTACHED_SHADERS
:
543 *params
= shProg
->NumShaders
;
545 case GL_ACTIVE_ATTRIBUTES
:
546 *params
= _mesa_count_active_attribs(shProg
);
548 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
549 *params
= _mesa_longest_attribute_name_length(shProg
);
551 case GL_ACTIVE_UNIFORMS
:
552 *params
= shProg
->NumUserUniformStorage
;
554 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
558 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
559 /* Add one for the terminating NUL character for a non-array, and
560 * 4 for the "[0]" and the NUL for an array.
562 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
563 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
572 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
575 *params
= shProg
->TransformFeedback
.NumVarying
;
577 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
583 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
584 /* Add one for the terminating NUL character.
586 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
595 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
598 *params
= shProg
->TransformFeedback
.BufferMode
;
600 case GL_GEOMETRY_VERTICES_OUT
:
603 if (check_gs_query(ctx
, shProg
))
604 *params
= shProg
->Geom
.VerticesOut
;
606 case GL_GEOMETRY_INPUT_TYPE
:
609 if (check_gs_query(ctx
, shProg
))
610 *params
= shProg
->Geom
.InputType
;
612 case GL_GEOMETRY_OUTPUT_TYPE
:
615 if (check_gs_query(ctx
, shProg
))
616 *params
= shProg
->Geom
.OutputType
;
618 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
625 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
626 /* Add one for the terminating NUL character.
628 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
637 case GL_ACTIVE_UNIFORM_BLOCKS
:
641 *params
= shProg
->NumUniformBlocks
;
643 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
644 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
645 * only available with desktop OpenGL 3.0+ with the
646 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
648 * On desktop, we ignore the 3.0+ requirement because it is silly.
650 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
653 *params
= shProg
->BinaryRetreivableHint
;
655 case GL_PROGRAM_BINARY_LENGTH
:
658 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
659 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
662 *params
= shProg
->NumAtomicBuffers
;
668 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
669 _mesa_lookup_enum_by_nr(pname
));
674 * glGetShaderiv() - get GLSL shader state
677 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
679 struct gl_shader
*shader
=
680 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
688 *params
= shader
->Type
;
690 case GL_DELETE_STATUS
:
691 *params
= shader
->DeletePending
;
693 case GL_COMPILE_STATUS
:
694 *params
= shader
->CompileStatus
;
696 case GL_INFO_LOG_LENGTH
:
697 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
699 case GL_SHADER_SOURCE_LENGTH
:
700 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
703 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
710 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
711 GLsizei
*length
, GLchar
*infoLog
)
713 struct gl_shader_program
*shProg
714 = _mesa_lookup_shader_program(ctx
, program
);
716 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
719 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
724 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
725 GLsizei
*length
, GLchar
*infoLog
)
727 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
729 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
732 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
737 * Return shader source code.
740 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
741 GLsizei
*length
, GLchar
*sourceOut
)
743 struct gl_shader
*sh
;
744 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
748 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
753 * Set/replace shader source code. A helper function used by
754 * glShaderSource[ARB] and glCreateShaderProgramEXT.
757 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
759 struct gl_shader
*sh
;
761 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
765 /* free old shader source string and install new one */
766 free((void *)sh
->Source
);
768 sh
->CompileStatus
= GL_FALSE
;
770 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
779 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
781 struct gl_shader
*sh
;
782 struct gl_shader_compiler_options
*options
;
784 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
788 options
= &ctx
->ShaderCompilerOptions
[sh
->Stage
];
790 /* set default pragma state for shader */
791 sh
->Pragmas
= options
->DefaultPragmas
;
794 /* If the user called glCompileShader without first calling
795 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
797 sh
->CompileStatus
= GL_FALSE
;
799 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
800 printf("GLSL source for %s shader %d:\n",
801 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
802 printf("%s\n", sh
->Source
);
805 /* this call will set the shader->CompileStatus field to indicate if
806 * compilation was successful.
808 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
810 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
811 _mesa_write_shader_to_file(sh
);
814 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
815 if (sh
->CompileStatus
) {
816 printf("GLSL IR for shader %d:\n", sh
->Name
);
817 _mesa_print_ir(sh
->ir
, NULL
);
820 printf("GLSL shader %d failed to compile.\n", sh
->Name
);
822 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
823 printf("GLSL shader %d info log:\n", sh
->Name
);
824 printf("%s\n", sh
->InfoLog
);
830 if (!sh
->CompileStatus
) {
831 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
832 fprintf(stderr
, "GLSL source for %s shader %d:\n",
833 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
834 fprintf(stderr
, "%s\n", sh
->Source
);
835 fprintf(stderr
, "Info Log:\n%s\n", sh
->InfoLog
);
839 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
840 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
841 sh
->Name
, sh
->InfoLog
);
848 * Link a program's shaders.
851 link_program(struct gl_context
*ctx
, GLuint program
)
853 struct gl_shader_program
*shProg
;
855 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
859 /* From the ARB_transform_feedback2 specification:
860 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
861 * the name of a program being used by one or more transform feedback
862 * objects, even if the objects are not currently bound or are paused."
864 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
865 _mesa_error(ctx
, GL_INVALID_OPERATION
,
866 "glLinkProgram(transform feedback is using the program)");
870 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
872 _mesa_glsl_link_shader(ctx
, shProg
);
874 if (shProg
->LinkStatus
== GL_FALSE
&&
875 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
876 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
877 shProg
->Name
, shProg
->InfoLog
);
884 printf("Link %u shaders in program %u: %s\n",
885 shProg
->NumShaders
, shProg
->Name
,
886 shProg
->LinkStatus
? "Success" : "Failed");
888 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
889 printf(" shader %u, type 0x%x\n",
890 shProg
->Shaders
[i
]->Name
,
891 shProg
->Shaders
[i
]->Type
);
898 * Print basic shader info (for debug).
901 print_shader_info(const struct gl_shader_program
*shProg
)
905 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
906 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
907 printf(" %s shader %u, checksum %u\n",
908 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
909 shProg
->Shaders
[i
]->Name
,
910 shProg
->Shaders
[i
]->SourceChecksum
);
912 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
913 printf(" vert prog %u\n",
914 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
915 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
916 printf(" frag prog %u\n",
917 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
918 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
919 printf(" geom prog %u\n",
920 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
925 * Use the named shader program for subsequent glUniform calls
928 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
931 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
932 _mesa_error(ctx
, GL_INVALID_OPERATION
,
933 "%s(program %u not linked)", caller
, shProg
->Name
);
937 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
938 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
945 use_shader_program(struct gl_context
*ctx
, GLenum type
,
946 struct gl_shader_program
*shProg
)
948 struct gl_shader_program
**target
;
949 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
951 target
= &ctx
->Shader
.CurrentProgram
[stage
];
952 if ((shProg
== NULL
) || (shProg
->_LinkedShaders
[stage
] == NULL
))
955 if (*target
!= shProg
) {
956 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
958 /* If the shader is also bound as the current rendering shader, unbind
959 * it from that binding point as well. This ensures that the correct
960 * semantics of glDeleteProgram are maintained.
963 case GL_VERTEX_SHADER
:
966 case GL_GEOMETRY_SHADER_ARB
:
969 case GL_FRAGMENT_SHADER
:
970 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
971 _mesa_reference_shader_program(ctx
,
972 &ctx
->Shader
._CurrentFragmentProgram
,
978 _mesa_reference_shader_program(ctx
, target
, shProg
);
984 * Use the named shader program for subsequent rendering.
987 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
989 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
990 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
991 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
992 _mesa_active_program(ctx
, shProg
, "glUseProgram");
994 if (ctx
->Driver
.UseProgram
)
995 ctx
->Driver
.UseProgram(ctx
, shProg
);
1000 * Do validation of the given shader program.
1001 * \param errMsg returns error message if validation fails.
1002 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1005 validate_shader_program(const struct gl_shader_program
*shProg
,
1008 if (!shProg
->LinkStatus
) {
1012 /* From the GL spec, a program is invalid if any of these are true:
1014 any two active samplers in the current program object are of
1015 different types, but refer to the same texture image unit,
1017 any active sampler in the current program object refers to a texture
1018 image unit where fixed-function fragment processing accesses a
1019 texture target that does not match the sampler type, or
1021 the sum of the number of active samplers in the program and the
1022 number of texture image units enabled for fixed-function fragment
1023 processing exceeds the combined limit on the total number of texture
1024 image units allowed.
1029 * Check: any two active samplers in the current program object are of
1030 * different types, but refer to the same texture image unit,
1032 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1040 * Called via glValidateProgram()
1043 validate_program(struct gl_context
*ctx
, GLuint program
)
1045 struct gl_shader_program
*shProg
;
1046 char errMsg
[100] = "";
1048 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1053 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1054 if (!shProg
->Validated
) {
1055 /* update info log */
1056 if (shProg
->InfoLog
) {
1057 ralloc_free(shProg
->InfoLog
);
1059 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1066 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1068 GET_CURRENT_CONTEXT(ctx
);
1069 attach_shader(ctx
, program
, shader
);
1074 _mesa_AttachShader(GLuint program
, GLuint shader
)
1076 GET_CURRENT_CONTEXT(ctx
);
1077 attach_shader(ctx
, program
, shader
);
1082 _mesa_CompileShader(GLhandleARB shaderObj
)
1084 GET_CURRENT_CONTEXT(ctx
);
1085 if (MESA_VERBOSE
& VERBOSE_API
)
1086 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1087 compile_shader(ctx
, shaderObj
);
1092 _mesa_CreateShader(GLenum type
)
1094 GET_CURRENT_CONTEXT(ctx
);
1095 if (MESA_VERBOSE
& VERBOSE_API
)
1096 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1097 return create_shader(ctx
, type
);
1101 GLhandleARB GLAPIENTRY
1102 _mesa_CreateShaderObjectARB(GLenum type
)
1104 GET_CURRENT_CONTEXT(ctx
);
1105 return create_shader(ctx
, type
);
1110 _mesa_CreateProgram(void)
1112 GET_CURRENT_CONTEXT(ctx
);
1113 if (MESA_VERBOSE
& VERBOSE_API
)
1114 _mesa_debug(ctx
, "glCreateProgram\n");
1115 return create_shader_program(ctx
);
1119 GLhandleARB GLAPIENTRY
1120 _mesa_CreateProgramObjectARB(void)
1122 GET_CURRENT_CONTEXT(ctx
);
1123 return create_shader_program(ctx
);
1128 _mesa_DeleteObjectARB(GLhandleARB obj
)
1130 if (MESA_VERBOSE
& VERBOSE_API
) {
1131 GET_CURRENT_CONTEXT(ctx
);
1132 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1136 GET_CURRENT_CONTEXT(ctx
);
1137 FLUSH_VERTICES(ctx
, 0);
1138 if (is_program(ctx
, obj
)) {
1139 delete_shader_program(ctx
, obj
);
1141 else if (is_shader(ctx
, obj
)) {
1142 delete_shader(ctx
, obj
);
1152 _mesa_DeleteProgram(GLuint name
)
1155 GET_CURRENT_CONTEXT(ctx
);
1156 FLUSH_VERTICES(ctx
, 0);
1157 delete_shader_program(ctx
, name
);
1163 _mesa_DeleteShader(GLuint name
)
1166 GET_CURRENT_CONTEXT(ctx
);
1167 FLUSH_VERTICES(ctx
, 0);
1168 delete_shader(ctx
, name
);
1174 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 detach_shader(ctx
, program
, shader
);
1182 _mesa_DetachShader(GLuint program
, GLuint shader
)
1184 GET_CURRENT_CONTEXT(ctx
);
1185 detach_shader(ctx
, program
, shader
);
1190 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1191 GLsizei
* count
, GLhandleARB
* obj
)
1193 GET_CURRENT_CONTEXT(ctx
);
1194 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1199 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1200 GLsizei
*count
, GLuint
*obj
)
1202 GET_CURRENT_CONTEXT(ctx
);
1203 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1208 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1209 GLcharARB
* infoLog
)
1211 GET_CURRENT_CONTEXT(ctx
);
1212 if (is_program(ctx
, object
)) {
1213 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1215 else if (is_shader(ctx
, object
)) {
1216 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1219 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1225 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1227 GET_CURRENT_CONTEXT(ctx
);
1228 /* Implement in terms of GetProgramiv, GetShaderiv */
1229 if (is_program(ctx
, object
)) {
1230 if (pname
== GL_OBJECT_TYPE_ARB
) {
1231 *params
= GL_PROGRAM_OBJECT_ARB
;
1234 get_programiv(ctx
, object
, pname
, params
);
1237 else if (is_shader(ctx
, object
)) {
1238 if (pname
== GL_OBJECT_TYPE_ARB
) {
1239 *params
= GL_SHADER_OBJECT_ARB
;
1242 get_shaderiv(ctx
, object
, pname
, params
);
1246 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1252 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1255 GLint iparams
[1]; /* XXX is one element enough? */
1256 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1257 params
[0] = (GLfloat
) iparams
[0];
1262 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 get_programiv(ctx
, program
, pname
, params
);
1270 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1272 GET_CURRENT_CONTEXT(ctx
);
1273 get_shaderiv(ctx
, shader
, pname
, params
);
1278 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1279 GLsizei
*length
, GLchar
*infoLog
)
1281 GET_CURRENT_CONTEXT(ctx
);
1282 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1287 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1288 GLsizei
*length
, GLchar
*infoLog
)
1290 GET_CURRENT_CONTEXT(ctx
);
1291 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1296 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1297 GLsizei
*length
, GLcharARB
*sourceOut
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1304 GLhandleARB GLAPIENTRY
1305 _mesa_GetHandleARB(GLenum pname
)
1307 GET_CURRENT_CONTEXT(ctx
);
1308 return get_handle(ctx
, pname
);
1312 GLboolean GLAPIENTRY
1313 _mesa_IsProgram(GLuint name
)
1315 GET_CURRENT_CONTEXT(ctx
);
1316 return is_program(ctx
, name
);
1320 GLboolean GLAPIENTRY
1321 _mesa_IsShader(GLuint name
)
1323 GET_CURRENT_CONTEXT(ctx
);
1324 return is_shader(ctx
, name
);
1329 _mesa_LinkProgram(GLhandleARB programObj
)
1331 GET_CURRENT_CONTEXT(ctx
);
1332 link_program(ctx
, programObj
);
1338 * Read shader source code from a file.
1339 * Useful for debugging to override an app's shader.
1342 read_shader(const char *fname
)
1344 const int max
= 50*1000;
1345 FILE *f
= fopen(fname
, "r");
1346 GLcharARB
*buffer
, *shader
;
1353 buffer
= malloc(max
);
1354 len
= fread(buffer
, 1, max
, f
);
1359 shader
= _mesa_strdup(buffer
);
1367 * Called via glShaderSource() and glShaderSourceARB() API functions.
1368 * Basically, concatenate the source code strings into one long string
1369 * and pass it to _mesa_shader_source().
1372 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1373 const GLcharARB
* const * string
, const GLint
* length
)
1375 GET_CURRENT_CONTEXT(ctx
);
1377 GLsizei i
, totalLength
;
1381 if (!shaderObj
|| string
== NULL
) {
1382 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1387 * This array holds offsets of where the appropriate string ends, thus the
1388 * last element will be set to the total length of the source code.
1390 offsets
= malloc(count
* sizeof(GLint
));
1391 if (offsets
== NULL
) {
1392 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1396 for (i
= 0; i
< count
; i
++) {
1397 if (string
[i
] == NULL
) {
1398 free((GLvoid
*) offsets
);
1399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1400 "glShaderSourceARB(null string)");
1403 if (length
== NULL
|| length
[i
] < 0)
1404 offsets
[i
] = strlen(string
[i
]);
1406 offsets
[i
] = length
[i
];
1407 /* accumulate string lengths */
1409 offsets
[i
] += offsets
[i
- 1];
1412 /* Total length of source string is sum off all strings plus two.
1413 * One extra byte for terminating zero, another extra byte to silence
1414 * valgrind warnings in the parser/grammer code.
1416 totalLength
= offsets
[count
- 1] + 2;
1417 source
= malloc(totalLength
* sizeof(GLcharARB
));
1418 if (source
== NULL
) {
1419 free((GLvoid
*) offsets
);
1420 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1424 for (i
= 0; i
< count
; i
++) {
1425 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1426 memcpy(source
+ start
, string
[i
],
1427 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1429 source
[totalLength
- 1] = '\0';
1430 source
[totalLength
- 2] = '\0';
1433 /* Compute the shader's source code checksum then try to open a file
1434 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1435 * original shader source code. For debugging.
1438 GLcharARB
*newSource
;
1440 checksum
= _mesa_str_checksum(source
);
1442 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1444 newSource
= read_shader(filename
);
1446 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1447 shaderObj
, checksum
, filename
);
1453 shader_source(ctx
, shaderObj
, source
);
1456 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1458 sh
->SourceChecksum
= checksum
; /* save original checksum */
1466 _mesa_UseProgram(GLhandleARB program
)
1468 GET_CURRENT_CONTEXT(ctx
);
1469 struct gl_shader_program
*shProg
;
1471 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1472 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1473 "glUseProgram(transform feedback active)");
1478 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1482 if (!shProg
->LinkStatus
) {
1483 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1484 "glUseProgram(program %u not linked)", program
);
1489 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1490 print_shader_info(shProg
);
1497 _mesa_use_program(ctx
, shProg
);
1502 _mesa_ValidateProgram(GLhandleARB program
)
1504 GET_CURRENT_CONTEXT(ctx
);
1505 validate_program(ctx
, program
);
1510 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1513 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1514 GLint
* range
, GLint
* precision
)
1516 const struct gl_program_constants
*limits
;
1517 const struct gl_precision
*p
;
1518 GET_CURRENT_CONTEXT(ctx
);
1520 switch (shadertype
) {
1521 case GL_VERTEX_SHADER
:
1522 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1524 case GL_FRAGMENT_SHADER
:
1525 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1528 _mesa_error(ctx
, GL_INVALID_ENUM
,
1529 "glGetShaderPrecisionFormat(shadertype)");
1533 switch (precisiontype
) {
1535 p
= &limits
->LowFloat
;
1537 case GL_MEDIUM_FLOAT
:
1538 p
= &limits
->MediumFloat
;
1541 p
= &limits
->HighFloat
;
1544 p
= &limits
->LowInt
;
1547 p
= &limits
->MediumInt
;
1550 p
= &limits
->HighInt
;
1553 _mesa_error(ctx
, GL_INVALID_ENUM
,
1554 "glGetShaderPrecisionFormat(precisiontype)");
1558 range
[0] = p
->RangeMin
;
1559 range
[1] = p
->RangeMax
;
1560 precision
[0] = p
->Precision
;
1565 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1568 _mesa_ReleaseShaderCompiler(void)
1570 _mesa_destroy_shader_compiler_caches();
1575 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1578 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1579 const void* binary
, GLint length
)
1581 GET_CURRENT_CONTEXT(ctx
);
1584 (void) binaryformat
;
1587 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1592 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1593 GLenum
*binaryFormat
, GLvoid
*binary
)
1595 struct gl_shader_program
*shProg
;
1596 GET_CURRENT_CONTEXT(ctx
);
1598 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1602 if (!shProg
->LinkStatus
) {
1603 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1604 "glGetProgramBinary(program %u not linked)",
1610 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1614 /* The ARB_get_program_binary spec says:
1616 * "If <length> is NULL, then no length is returned."
1621 (void) binaryFormat
;
1626 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1627 const GLvoid
*binary
, GLsizei length
)
1629 struct gl_shader_program
*shProg
;
1630 GET_CURRENT_CONTEXT(ctx
);
1632 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1636 (void) binaryFormat
;
1639 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1644 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1646 struct gl_shader_program
*shProg
;
1647 GET_CURRENT_CONTEXT(ctx
);
1649 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1650 "glProgramParameteri");
1655 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1656 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1657 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1658 * even be in the dispatch table, so we shouldn't need to expclicitly
1661 * On desktop, we ignore the 3.0+ requirement because it is silly.
1664 /* The ARB_get_program_binary extension spec says:
1666 * "An INVALID_VALUE error is generated if the <value> argument to
1667 * ProgramParameteri is not TRUE or FALSE."
1669 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1670 _mesa_error(ctx
, GL_INVALID_VALUE
,
1671 "glProgramParameteri(pname=%s, value=%d): "
1672 "value must be 0 or 1.",
1673 _mesa_lookup_enum_by_nr(pname
),
1678 /* No need to notify the driver. Any changes will actually take effect
1679 * the next time the shader is linked.
1681 * The ARB_get_program_binary extension spec says:
1683 * "To indicate that a program binary is likely to be retrieved,
1684 * ProgramParameteri should be called with <pname>
1685 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1686 * will not be in effect until the next time LinkProgram or
1687 * ProgramBinary has been called successfully."
1689 * The resloution of issue 9 in the extension spec also says:
1691 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1692 * to indicate to the GL implementation that this program will
1693 * likely be saved with GetProgramBinary at some point. This will
1694 * give the GL implementation the opportunity to track any state
1695 * changes made to the program before being saved such that when it
1696 * is loaded again a recompile can be avoided."
1698 shProg
->BinaryRetreivableHint
= value
;
1704 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1705 _mesa_lookup_enum_by_nr(pname
));
1709 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1710 struct gl_shader_program
*shProg
)
1712 use_shader_program(ctx
, type
, shProg
);
1714 if (ctx
->Driver
.UseProgram
)
1715 ctx
->Driver
.UseProgram(ctx
, shProg
);
1720 * For GL_EXT_separate_shader_objects
1723 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1725 GET_CURRENT_CONTEXT(ctx
);
1726 struct gl_shader_program
*shProg
= NULL
;
1728 if (!_mesa_validate_shader_target(ctx
, type
)) {
1729 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1733 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1734 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1735 "glUseShaderProgramEXT(transform feedback is active)");
1740 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1741 "glUseShaderProgramEXT");
1745 if (!shProg
->LinkStatus
) {
1746 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1747 "glUseShaderProgramEXT(program not linked)");
1752 _mesa_use_shader_program(ctx
, type
, shProg
);
1757 * For GL_EXT_separate_shader_objects
1760 _mesa_ActiveProgramEXT(GLuint program
)
1762 GET_CURRENT_CONTEXT(ctx
);
1763 struct gl_shader_program
*shProg
= (program
!= 0)
1764 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1767 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1773 * For GL_EXT_separate_shader_objects
1776 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1778 GET_CURRENT_CONTEXT(ctx
);
1779 const GLuint shader
= create_shader(ctx
, type
);
1783 shader_source(ctx
, shader
, _mesa_strdup(string
));
1784 compile_shader(ctx
, shader
);
1786 program
= create_shader_program(ctx
);
1788 struct gl_shader_program
*shProg
;
1789 struct gl_shader
*sh
;
1790 GLint compiled
= GL_FALSE
;
1792 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1793 sh
= _mesa_lookup_shader(ctx
, shader
);
1795 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1797 attach_shader(ctx
, program
, shader
);
1798 link_program(ctx
, program
);
1799 detach_shader(ctx
, program
, shader
);
1803 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1804 append
-error
-to
-info
-log
;
1805 shProg
->LinkStatus
= GL_FALSE
;
1810 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1813 delete_shader(ctx
, shader
);
1821 * Copy program-specific data generated by linking from the gl_shader_program
1822 * object to a specific gl_program object.
1825 _mesa_copy_linked_program_data(gl_shader_stage type
,
1826 const struct gl_shader_program
*src
,
1827 struct gl_program
*dst
)
1830 case MESA_SHADER_VERTEX
:
1831 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1833 case MESA_SHADER_GEOMETRY
: {
1834 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1835 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1836 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1837 dst_gp
->InputType
= src
->Geom
.InputType
;
1838 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1839 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1840 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;