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
);
191 case GL_COMPUTE_SHADER
:
192 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
200 is_program(struct gl_context
*ctx
, GLuint name
)
202 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
203 return shProg
? GL_TRUE
: GL_FALSE
;
208 is_shader(struct gl_context
*ctx
, GLuint name
)
210 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
211 return shader
? GL_TRUE
: GL_FALSE
;
216 * Attach shader to a shader program.
219 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
221 struct gl_shader_program
*shProg
;
222 struct gl_shader
*sh
;
225 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
227 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
231 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
236 n
= shProg
->NumShaders
;
237 for (i
= 0; i
< n
; i
++) {
238 if (shProg
->Shaders
[i
] == sh
) {
239 /* The shader is already attched to this program. The
240 * GL_ARB_shader_objects spec says:
242 * "The error INVALID_OPERATION is generated by AttachObjectARB
243 * if <obj> is already attached to <containerObj>."
245 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
247 } else if (same_type_disallowed
&&
248 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
249 /* Shader with the same type is already attached to this program,
250 * OpenGL ES 2.0 and 3.0 specs say:
252 * "Multiple shader objects of the same type may not be attached
253 * to a single program object. [...] The error INVALID_OPERATION
254 * is generated if [...] another shader object of the same type
255 * as shader is already attached to program."
257 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
263 shProg
->Shaders
= (struct gl_shader
**)
264 _mesa_realloc(shProg
->Shaders
,
265 n
* sizeof(struct gl_shader
*),
266 (n
+ 1) * sizeof(struct gl_shader
*));
267 if (!shProg
->Shaders
) {
268 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
273 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
274 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
275 shProg
->NumShaders
++;
280 create_shader(struct gl_context
*ctx
, GLenum type
)
282 struct gl_shader
*sh
;
285 if (!_mesa_validate_shader_target(ctx
, type
)) {
286 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
290 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
291 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
292 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
299 create_shader_program(struct gl_context
*ctx
)
302 struct gl_shader_program
*shProg
;
304 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
306 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
308 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
310 assert(shProg
->RefCount
== 1);
317 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
321 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
324 * NOTE: deleting shaders/programs works a bit differently than
325 * texture objects (and buffer objects, etc). Shader/program
326 * handles/IDs exist in the hash table until the object is really
327 * deleted (refcount==0). With texture objects, the handle/ID is
328 * removed from the hash table in glDeleteTextures() while the tex
329 * object itself might linger until its refcount goes to zero.
331 struct gl_shader_program
*shProg
;
333 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
337 if (!shProg
->DeletePending
) {
338 shProg
->DeletePending
= GL_TRUE
;
340 /* effectively, decr shProg's refcount */
341 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
347 delete_shader(struct gl_context
*ctx
, GLuint shader
)
349 struct gl_shader
*sh
;
351 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
355 if (!sh
->DeletePending
) {
356 sh
->DeletePending
= GL_TRUE
;
358 /* effectively, decr sh's refcount */
359 _mesa_reference_shader(ctx
, &sh
, NULL
);
365 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
367 struct gl_shader_program
*shProg
;
371 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
375 n
= shProg
->NumShaders
;
377 for (i
= 0; i
< n
; i
++) {
378 if (shProg
->Shaders
[i
]->Name
== shader
) {
380 struct gl_shader
**newList
;
383 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
385 /* alloc new, smaller array */
387 malloc((n
- 1) * sizeof(struct gl_shader
*));
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
392 for (j
= 0; j
< i
; j
++) {
393 newList
[j
] = shProg
->Shaders
[j
];
396 newList
[j
++] = shProg
->Shaders
[i
];
397 free(shProg
->Shaders
);
399 shProg
->Shaders
= newList
;
400 shProg
->NumShaders
= n
- 1;
405 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
406 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
407 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
408 assert(shProg
->Shaders
[j
]->RefCount
> 0);
420 if (is_shader(ctx
, shader
))
421 err
= GL_INVALID_OPERATION
;
422 else if (is_program(ctx
, shader
))
423 err
= GL_INVALID_OPERATION
;
425 err
= GL_INVALID_VALUE
;
426 _mesa_error(ctx
, err
, "glDetachShader(shader)");
433 * Return list of shaders attached to shader program.
436 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
437 GLsizei
*count
, GLuint
*obj
)
439 struct gl_shader_program
*shProg
=
440 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
443 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
444 obj
[i
] = shProg
->Shaders
[i
]->Name
;
453 * glGetHandleARB() - return ID/name of currently bound shader program.
456 get_handle(struct gl_context
*ctx
, GLenum pname
)
458 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
459 if (ctx
->Shader
.ActiveProgram
)
460 return ctx
->Shader
.ActiveProgram
->Name
;
465 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
472 * Check if a geometry shader query is valid at this time. If not, report an
473 * error and return false.
475 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
477 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
478 * are queried for a program which has not been linked successfully, or
479 * which does not contain objects to form a geometry shader, then an
480 * INVALID_OPERATION error is generated."
483 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
485 if (shProg
->LinkStatus
&&
486 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
490 _mesa_error(ctx
, GL_INVALID_OPERATION
,
491 "glGetProgramv(linked geometry shader required)");
497 * glGetProgramiv() - get shader program state.
498 * Note that this is for GLSL shader programs, not ARB vertex/fragment
499 * programs (see glGetProgramivARB).
502 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
504 struct gl_shader_program
*shProg
505 = _mesa_lookup_shader_program(ctx
, program
);
507 /* Is transform feedback available in this context?
510 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
511 || ctx
->API
== API_OPENGL_CORE
512 || _mesa_is_gles3(ctx
);
514 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
515 * and GL 3.2) are available in this context
517 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
519 /* Are uniform buffer objects available in this context?
522 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
523 || ctx
->API
== API_OPENGL_CORE
524 || _mesa_is_gles3(ctx
);
527 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
532 case GL_DELETE_STATUS
:
533 *params
= shProg
->DeletePending
;
536 *params
= shProg
->LinkStatus
;
538 case GL_VALIDATE_STATUS
:
539 *params
= shProg
->Validated
;
541 case GL_INFO_LOG_LENGTH
:
542 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
544 case GL_ATTACHED_SHADERS
:
545 *params
= shProg
->NumShaders
;
547 case GL_ACTIVE_ATTRIBUTES
:
548 *params
= _mesa_count_active_attribs(shProg
);
550 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
551 *params
= _mesa_longest_attribute_name_length(shProg
);
553 case GL_ACTIVE_UNIFORMS
:
554 *params
= shProg
->NumUserUniformStorage
;
556 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
560 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
561 /* Add one for the terminating NUL character for a non-array, and
562 * 4 for the "[0]" and the NUL for an array.
564 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
565 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
574 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
577 *params
= shProg
->TransformFeedback
.NumVarying
;
579 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
585 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
586 /* Add one for the terminating NUL character.
588 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
597 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
600 *params
= shProg
->TransformFeedback
.BufferMode
;
602 case GL_GEOMETRY_VERTICES_OUT
:
605 if (check_gs_query(ctx
, shProg
))
606 *params
= shProg
->Geom
.VerticesOut
;
608 case GL_GEOMETRY_INPUT_TYPE
:
611 if (check_gs_query(ctx
, shProg
))
612 *params
= shProg
->Geom
.InputType
;
614 case GL_GEOMETRY_OUTPUT_TYPE
:
617 if (check_gs_query(ctx
, shProg
))
618 *params
= shProg
->Geom
.OutputType
;
620 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
627 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
628 /* Add one for the terminating NUL character.
630 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
639 case GL_ACTIVE_UNIFORM_BLOCKS
:
643 *params
= shProg
->NumUniformBlocks
;
645 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
646 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
647 * only available with desktop OpenGL 3.0+ with the
648 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
650 * On desktop, we ignore the 3.0+ requirement because it is silly.
652 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
655 *params
= shProg
->BinaryRetreivableHint
;
657 case GL_PROGRAM_BINARY_LENGTH
:
660 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
661 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
664 *params
= shProg
->NumAtomicBuffers
;
670 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
671 _mesa_lookup_enum_by_nr(pname
));
676 * glGetShaderiv() - get GLSL shader state
679 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
681 struct gl_shader
*shader
=
682 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
690 *params
= shader
->Type
;
692 case GL_DELETE_STATUS
:
693 *params
= shader
->DeletePending
;
695 case GL_COMPILE_STATUS
:
696 *params
= shader
->CompileStatus
;
698 case GL_INFO_LOG_LENGTH
:
699 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
701 case GL_SHADER_SOURCE_LENGTH
:
702 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
705 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
712 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
713 GLsizei
*length
, GLchar
*infoLog
)
715 struct gl_shader_program
*shProg
716 = _mesa_lookup_shader_program(ctx
, program
);
718 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
721 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
726 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
727 GLsizei
*length
, GLchar
*infoLog
)
729 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
731 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
734 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
739 * Return shader source code.
742 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
743 GLsizei
*length
, GLchar
*sourceOut
)
745 struct gl_shader
*sh
;
746 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
750 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
755 * Set/replace shader source code. A helper function used by
756 * glShaderSource[ARB] and glCreateShaderProgramEXT.
759 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
761 struct gl_shader
*sh
;
763 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
767 /* free old shader source string and install new one */
768 free((void *)sh
->Source
);
770 sh
->CompileStatus
= GL_FALSE
;
772 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
781 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
783 struct gl_shader
*sh
;
784 struct gl_shader_compiler_options
*options
;
786 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
790 options
= &ctx
->ShaderCompilerOptions
[sh
->Stage
];
792 /* set default pragma state for shader */
793 sh
->Pragmas
= options
->DefaultPragmas
;
796 /* If the user called glCompileShader without first calling
797 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
799 sh
->CompileStatus
= GL_FALSE
;
801 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
802 printf("GLSL source for %s shader %d:\n",
803 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
804 printf("%s\n", sh
->Source
);
807 /* this call will set the shader->CompileStatus field to indicate if
808 * compilation was successful.
810 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
812 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
813 _mesa_write_shader_to_file(sh
);
816 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
817 if (sh
->CompileStatus
) {
818 printf("GLSL IR for shader %d:\n", sh
->Name
);
819 _mesa_print_ir(sh
->ir
, NULL
);
822 printf("GLSL shader %d failed to compile.\n", sh
->Name
);
824 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
825 printf("GLSL shader %d info log:\n", sh
->Name
);
826 printf("%s\n", sh
->InfoLog
);
832 if (!sh
->CompileStatus
) {
833 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
834 fprintf(stderr
, "GLSL source for %s shader %d:\n",
835 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
836 fprintf(stderr
, "%s\n", sh
->Source
);
837 fprintf(stderr
, "Info Log:\n%s\n", sh
->InfoLog
);
841 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
842 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
843 sh
->Name
, sh
->InfoLog
);
850 * Link a program's shaders.
853 link_program(struct gl_context
*ctx
, GLuint program
)
855 struct gl_shader_program
*shProg
;
857 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
861 /* From the ARB_transform_feedback2 specification:
862 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
863 * the name of a program being used by one or more transform feedback
864 * objects, even if the objects are not currently bound or are paused."
866 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
867 _mesa_error(ctx
, GL_INVALID_OPERATION
,
868 "glLinkProgram(transform feedback is using the program)");
872 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
874 _mesa_glsl_link_shader(ctx
, shProg
);
876 if (shProg
->LinkStatus
== GL_FALSE
&&
877 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
878 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
879 shProg
->Name
, shProg
->InfoLog
);
886 printf("Link %u shaders in program %u: %s\n",
887 shProg
->NumShaders
, shProg
->Name
,
888 shProg
->LinkStatus
? "Success" : "Failed");
890 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
891 printf(" shader %u, type 0x%x\n",
892 shProg
->Shaders
[i
]->Name
,
893 shProg
->Shaders
[i
]->Type
);
900 * Print basic shader info (for debug).
903 print_shader_info(const struct gl_shader_program
*shProg
)
907 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
908 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
909 printf(" %s shader %u, checksum %u\n",
910 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
911 shProg
->Shaders
[i
]->Name
,
912 shProg
->Shaders
[i
]->SourceChecksum
);
914 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
915 printf(" vert prog %u\n",
916 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
917 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
918 printf(" frag prog %u\n",
919 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
920 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
921 printf(" geom prog %u\n",
922 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
927 * Use the named shader program for subsequent glUniform calls
930 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
933 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
934 _mesa_error(ctx
, GL_INVALID_OPERATION
,
935 "%s(program %u not linked)", caller
, shProg
->Name
);
939 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
940 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
947 use_shader_program(struct gl_context
*ctx
, GLenum type
,
948 struct gl_shader_program
*shProg
)
950 struct gl_shader_program
**target
;
951 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
953 target
= &ctx
->Shader
.CurrentProgram
[stage
];
954 if ((shProg
== NULL
) || (shProg
->_LinkedShaders
[stage
] == NULL
))
957 if (*target
!= shProg
) {
958 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
960 /* If the shader is also bound as the current rendering shader, unbind
961 * it from that binding point as well. This ensures that the correct
962 * semantics of glDeleteProgram are maintained.
965 case GL_VERTEX_SHADER
:
968 case GL_GEOMETRY_SHADER_ARB
:
971 case GL_COMPUTE_SHADER
:
974 case GL_FRAGMENT_SHADER
:
975 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
976 _mesa_reference_shader_program(ctx
,
977 &ctx
->Shader
._CurrentFragmentProgram
,
983 _mesa_reference_shader_program(ctx
, target
, shProg
);
989 * Use the named shader program for subsequent rendering.
992 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
994 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
995 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
996 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
997 use_shader_program(ctx
, GL_COMPUTE_SHADER
, shProg
);
998 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1000 if (ctx
->Driver
.UseProgram
)
1001 ctx
->Driver
.UseProgram(ctx
, shProg
);
1006 * Do validation of the given shader program.
1007 * \param errMsg returns error message if validation fails.
1008 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1011 validate_shader_program(const struct gl_shader_program
*shProg
,
1014 if (!shProg
->LinkStatus
) {
1018 /* From the GL spec, a program is invalid if any of these are true:
1020 any two active samplers in the current program object are of
1021 different types, but refer to the same texture image unit,
1023 any active sampler in the current program object refers to a texture
1024 image unit where fixed-function fragment processing accesses a
1025 texture target that does not match the sampler type, or
1027 the sum of the number of active samplers in the program and the
1028 number of texture image units enabled for fixed-function fragment
1029 processing exceeds the combined limit on the total number of texture
1030 image units allowed.
1035 * Check: any two active samplers in the current program object are of
1036 * different types, but refer to the same texture image unit,
1038 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1046 * Called via glValidateProgram()
1049 validate_program(struct gl_context
*ctx
, GLuint program
)
1051 struct gl_shader_program
*shProg
;
1052 char errMsg
[100] = "";
1054 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1059 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1060 if (!shProg
->Validated
) {
1061 /* update info log */
1062 if (shProg
->InfoLog
) {
1063 ralloc_free(shProg
->InfoLog
);
1065 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1072 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1074 GET_CURRENT_CONTEXT(ctx
);
1075 attach_shader(ctx
, program
, shader
);
1080 _mesa_AttachShader(GLuint program
, GLuint shader
)
1082 GET_CURRENT_CONTEXT(ctx
);
1083 attach_shader(ctx
, program
, shader
);
1088 _mesa_CompileShader(GLhandleARB shaderObj
)
1090 GET_CURRENT_CONTEXT(ctx
);
1091 if (MESA_VERBOSE
& VERBOSE_API
)
1092 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1093 compile_shader(ctx
, shaderObj
);
1098 _mesa_CreateShader(GLenum type
)
1100 GET_CURRENT_CONTEXT(ctx
);
1101 if (MESA_VERBOSE
& VERBOSE_API
)
1102 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1103 return create_shader(ctx
, type
);
1107 GLhandleARB GLAPIENTRY
1108 _mesa_CreateShaderObjectARB(GLenum type
)
1110 GET_CURRENT_CONTEXT(ctx
);
1111 return create_shader(ctx
, type
);
1116 _mesa_CreateProgram(void)
1118 GET_CURRENT_CONTEXT(ctx
);
1119 if (MESA_VERBOSE
& VERBOSE_API
)
1120 _mesa_debug(ctx
, "glCreateProgram\n");
1121 return create_shader_program(ctx
);
1125 GLhandleARB GLAPIENTRY
1126 _mesa_CreateProgramObjectARB(void)
1128 GET_CURRENT_CONTEXT(ctx
);
1129 return create_shader_program(ctx
);
1134 _mesa_DeleteObjectARB(GLhandleARB obj
)
1136 if (MESA_VERBOSE
& VERBOSE_API
) {
1137 GET_CURRENT_CONTEXT(ctx
);
1138 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1142 GET_CURRENT_CONTEXT(ctx
);
1143 FLUSH_VERTICES(ctx
, 0);
1144 if (is_program(ctx
, obj
)) {
1145 delete_shader_program(ctx
, obj
);
1147 else if (is_shader(ctx
, obj
)) {
1148 delete_shader(ctx
, obj
);
1158 _mesa_DeleteProgram(GLuint name
)
1161 GET_CURRENT_CONTEXT(ctx
);
1162 FLUSH_VERTICES(ctx
, 0);
1163 delete_shader_program(ctx
, name
);
1169 _mesa_DeleteShader(GLuint name
)
1172 GET_CURRENT_CONTEXT(ctx
);
1173 FLUSH_VERTICES(ctx
, 0);
1174 delete_shader(ctx
, name
);
1180 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1182 GET_CURRENT_CONTEXT(ctx
);
1183 detach_shader(ctx
, program
, shader
);
1188 _mesa_DetachShader(GLuint program
, GLuint shader
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 detach_shader(ctx
, program
, shader
);
1196 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1197 GLsizei
* count
, GLhandleARB
* obj
)
1199 GET_CURRENT_CONTEXT(ctx
);
1200 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1205 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1206 GLsizei
*count
, GLuint
*obj
)
1208 GET_CURRENT_CONTEXT(ctx
);
1209 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1214 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1215 GLcharARB
* infoLog
)
1217 GET_CURRENT_CONTEXT(ctx
);
1218 if (is_program(ctx
, object
)) {
1219 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1221 else if (is_shader(ctx
, object
)) {
1222 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1225 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1231 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1233 GET_CURRENT_CONTEXT(ctx
);
1234 /* Implement in terms of GetProgramiv, GetShaderiv */
1235 if (is_program(ctx
, object
)) {
1236 if (pname
== GL_OBJECT_TYPE_ARB
) {
1237 *params
= GL_PROGRAM_OBJECT_ARB
;
1240 get_programiv(ctx
, object
, pname
, params
);
1243 else if (is_shader(ctx
, object
)) {
1244 if (pname
== GL_OBJECT_TYPE_ARB
) {
1245 *params
= GL_SHADER_OBJECT_ARB
;
1248 get_shaderiv(ctx
, object
, pname
, params
);
1252 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1258 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1261 GLint iparams
[1]; /* XXX is one element enough? */
1262 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1263 params
[0] = (GLfloat
) iparams
[0];
1268 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1270 GET_CURRENT_CONTEXT(ctx
);
1271 get_programiv(ctx
, program
, pname
, params
);
1276 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1278 GET_CURRENT_CONTEXT(ctx
);
1279 get_shaderiv(ctx
, shader
, pname
, params
);
1284 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1285 GLsizei
*length
, GLchar
*infoLog
)
1287 GET_CURRENT_CONTEXT(ctx
);
1288 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1293 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1294 GLsizei
*length
, GLchar
*infoLog
)
1296 GET_CURRENT_CONTEXT(ctx
);
1297 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1302 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1303 GLsizei
*length
, GLcharARB
*sourceOut
)
1305 GET_CURRENT_CONTEXT(ctx
);
1306 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1310 GLhandleARB GLAPIENTRY
1311 _mesa_GetHandleARB(GLenum pname
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 return get_handle(ctx
, pname
);
1318 GLboolean GLAPIENTRY
1319 _mesa_IsProgram(GLuint name
)
1321 GET_CURRENT_CONTEXT(ctx
);
1322 return is_program(ctx
, name
);
1326 GLboolean GLAPIENTRY
1327 _mesa_IsShader(GLuint name
)
1329 GET_CURRENT_CONTEXT(ctx
);
1330 return is_shader(ctx
, name
);
1335 _mesa_LinkProgram(GLhandleARB programObj
)
1337 GET_CURRENT_CONTEXT(ctx
);
1338 link_program(ctx
, programObj
);
1344 * Read shader source code from a file.
1345 * Useful for debugging to override an app's shader.
1348 read_shader(const char *fname
)
1350 const int max
= 50*1000;
1351 FILE *f
= fopen(fname
, "r");
1352 GLcharARB
*buffer
, *shader
;
1359 buffer
= malloc(max
);
1360 len
= fread(buffer
, 1, max
, f
);
1365 shader
= _mesa_strdup(buffer
);
1373 * Called via glShaderSource() and glShaderSourceARB() API functions.
1374 * Basically, concatenate the source code strings into one long string
1375 * and pass it to _mesa_shader_source().
1378 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1379 const GLcharARB
* const * string
, const GLint
* length
)
1381 GET_CURRENT_CONTEXT(ctx
);
1383 GLsizei i
, totalLength
;
1387 if (!shaderObj
|| string
== NULL
) {
1388 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1393 * This array holds offsets of where the appropriate string ends, thus the
1394 * last element will be set to the total length of the source code.
1396 offsets
= malloc(count
* sizeof(GLint
));
1397 if (offsets
== NULL
) {
1398 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1402 for (i
= 0; i
< count
; i
++) {
1403 if (string
[i
] == NULL
) {
1404 free((GLvoid
*) offsets
);
1405 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1406 "glShaderSourceARB(null string)");
1409 if (length
== NULL
|| length
[i
] < 0)
1410 offsets
[i
] = strlen(string
[i
]);
1412 offsets
[i
] = length
[i
];
1413 /* accumulate string lengths */
1415 offsets
[i
] += offsets
[i
- 1];
1418 /* Total length of source string is sum off all strings plus two.
1419 * One extra byte for terminating zero, another extra byte to silence
1420 * valgrind warnings in the parser/grammer code.
1422 totalLength
= offsets
[count
- 1] + 2;
1423 source
= malloc(totalLength
* sizeof(GLcharARB
));
1424 if (source
== NULL
) {
1425 free((GLvoid
*) offsets
);
1426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1430 for (i
= 0; i
< count
; i
++) {
1431 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1432 memcpy(source
+ start
, string
[i
],
1433 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1435 source
[totalLength
- 1] = '\0';
1436 source
[totalLength
- 2] = '\0';
1439 /* Compute the shader's source code checksum then try to open a file
1440 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1441 * original shader source code. For debugging.
1444 GLcharARB
*newSource
;
1446 checksum
= _mesa_str_checksum(source
);
1448 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1450 newSource
= read_shader(filename
);
1452 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1453 shaderObj
, checksum
, filename
);
1459 shader_source(ctx
, shaderObj
, source
);
1462 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1464 sh
->SourceChecksum
= checksum
; /* save original checksum */
1472 _mesa_UseProgram(GLhandleARB program
)
1474 GET_CURRENT_CONTEXT(ctx
);
1475 struct gl_shader_program
*shProg
;
1477 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1479 "glUseProgram(transform feedback active)");
1484 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1488 if (!shProg
->LinkStatus
) {
1489 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1490 "glUseProgram(program %u not linked)", program
);
1495 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1496 print_shader_info(shProg
);
1503 _mesa_use_program(ctx
, shProg
);
1508 _mesa_ValidateProgram(GLhandleARB program
)
1510 GET_CURRENT_CONTEXT(ctx
);
1511 validate_program(ctx
, program
);
1516 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1519 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1520 GLint
* range
, GLint
* precision
)
1522 const struct gl_program_constants
*limits
;
1523 const struct gl_precision
*p
;
1524 GET_CURRENT_CONTEXT(ctx
);
1526 switch (shadertype
) {
1527 case GL_VERTEX_SHADER
:
1528 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1530 case GL_FRAGMENT_SHADER
:
1531 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1534 _mesa_error(ctx
, GL_INVALID_ENUM
,
1535 "glGetShaderPrecisionFormat(shadertype)");
1539 switch (precisiontype
) {
1541 p
= &limits
->LowFloat
;
1543 case GL_MEDIUM_FLOAT
:
1544 p
= &limits
->MediumFloat
;
1547 p
= &limits
->HighFloat
;
1550 p
= &limits
->LowInt
;
1553 p
= &limits
->MediumInt
;
1556 p
= &limits
->HighInt
;
1559 _mesa_error(ctx
, GL_INVALID_ENUM
,
1560 "glGetShaderPrecisionFormat(precisiontype)");
1564 range
[0] = p
->RangeMin
;
1565 range
[1] = p
->RangeMax
;
1566 precision
[0] = p
->Precision
;
1571 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1574 _mesa_ReleaseShaderCompiler(void)
1576 _mesa_destroy_shader_compiler_caches();
1581 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1584 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1585 const void* binary
, GLint length
)
1587 GET_CURRENT_CONTEXT(ctx
);
1590 (void) binaryformat
;
1593 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1598 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1599 GLenum
*binaryFormat
, GLvoid
*binary
)
1601 struct gl_shader_program
*shProg
;
1602 GET_CURRENT_CONTEXT(ctx
);
1604 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1608 if (!shProg
->LinkStatus
) {
1609 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1610 "glGetProgramBinary(program %u not linked)",
1616 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1620 /* The ARB_get_program_binary spec says:
1622 * "If <length> is NULL, then no length is returned."
1627 (void) binaryFormat
;
1632 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1633 const GLvoid
*binary
, GLsizei length
)
1635 struct gl_shader_program
*shProg
;
1636 GET_CURRENT_CONTEXT(ctx
);
1638 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1642 (void) binaryFormat
;
1645 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1650 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1652 struct gl_shader_program
*shProg
;
1653 GET_CURRENT_CONTEXT(ctx
);
1655 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1656 "glProgramParameteri");
1661 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1662 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1663 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1664 * even be in the dispatch table, so we shouldn't need to expclicitly
1667 * On desktop, we ignore the 3.0+ requirement because it is silly.
1670 /* The ARB_get_program_binary extension spec says:
1672 * "An INVALID_VALUE error is generated if the <value> argument to
1673 * ProgramParameteri is not TRUE or FALSE."
1675 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1676 _mesa_error(ctx
, GL_INVALID_VALUE
,
1677 "glProgramParameteri(pname=%s, value=%d): "
1678 "value must be 0 or 1.",
1679 _mesa_lookup_enum_by_nr(pname
),
1684 /* No need to notify the driver. Any changes will actually take effect
1685 * the next time the shader is linked.
1687 * The ARB_get_program_binary extension spec says:
1689 * "To indicate that a program binary is likely to be retrieved,
1690 * ProgramParameteri should be called with <pname>
1691 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1692 * will not be in effect until the next time LinkProgram or
1693 * ProgramBinary has been called successfully."
1695 * The resloution of issue 9 in the extension spec also says:
1697 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1698 * to indicate to the GL implementation that this program will
1699 * likely be saved with GetProgramBinary at some point. This will
1700 * give the GL implementation the opportunity to track any state
1701 * changes made to the program before being saved such that when it
1702 * is loaded again a recompile can be avoided."
1704 shProg
->BinaryRetreivableHint
= value
;
1710 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1711 _mesa_lookup_enum_by_nr(pname
));
1715 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1716 struct gl_shader_program
*shProg
)
1718 use_shader_program(ctx
, type
, shProg
);
1720 if (ctx
->Driver
.UseProgram
)
1721 ctx
->Driver
.UseProgram(ctx
, shProg
);
1726 * For GL_EXT_separate_shader_objects
1729 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1731 GET_CURRENT_CONTEXT(ctx
);
1732 struct gl_shader_program
*shProg
= NULL
;
1734 if (!_mesa_validate_shader_target(ctx
, type
)) {
1735 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1739 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1740 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1741 "glUseShaderProgramEXT(transform feedback is active)");
1746 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1747 "glUseShaderProgramEXT");
1751 if (!shProg
->LinkStatus
) {
1752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1753 "glUseShaderProgramEXT(program not linked)");
1758 _mesa_use_shader_program(ctx
, type
, shProg
);
1763 * For GL_EXT_separate_shader_objects
1766 _mesa_ActiveProgramEXT(GLuint program
)
1768 GET_CURRENT_CONTEXT(ctx
);
1769 struct gl_shader_program
*shProg
= (program
!= 0)
1770 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1773 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1779 * For GL_EXT_separate_shader_objects
1782 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1784 GET_CURRENT_CONTEXT(ctx
);
1785 const GLuint shader
= create_shader(ctx
, type
);
1789 shader_source(ctx
, shader
, _mesa_strdup(string
));
1790 compile_shader(ctx
, shader
);
1792 program
= create_shader_program(ctx
);
1794 struct gl_shader_program
*shProg
;
1795 struct gl_shader
*sh
;
1796 GLint compiled
= GL_FALSE
;
1798 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1799 sh
= _mesa_lookup_shader(ctx
, shader
);
1801 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1803 attach_shader(ctx
, program
, shader
);
1804 link_program(ctx
, program
);
1805 detach_shader(ctx
, program
, shader
);
1809 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1810 append
-error
-to
-info
-log
;
1811 shProg
->LinkStatus
= GL_FALSE
;
1816 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1819 delete_shader(ctx
, shader
);
1827 * Copy program-specific data generated by linking from the gl_shader_program
1828 * object to a specific gl_program object.
1831 _mesa_copy_linked_program_data(gl_shader_stage type
,
1832 const struct gl_shader_program
*src
,
1833 struct gl_program
*dst
)
1836 case MESA_SHADER_VERTEX
:
1837 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1839 case MESA_SHADER_GEOMETRY
: {
1840 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1841 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1842 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1843 dst_gp
->InputType
= src
->Geom
.InputType
;
1844 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1845 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1846 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1849 case MESA_SHADER_COMPUTE
: {
1850 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1852 for (i
= 0; i
< 3; i
++)
1853 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];