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_TYPES
; ++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
)
133 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentVertexProgram
, NULL
);
134 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentGeometryProgram
,
136 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentFragmentProgram
,
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 validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
178 case GL_FRAGMENT_SHADER
:
179 return ctx
->Extensions
.ARB_fragment_shader
;
180 case GL_VERTEX_SHADER
:
181 return ctx
->Extensions
.ARB_vertex_shader
;
182 case GL_GEOMETRY_SHADER_ARB
:
183 return _mesa_has_geometry_shaders(ctx
);
191 is_program(struct gl_context
*ctx
, GLuint name
)
193 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
194 return shProg
? GL_TRUE
: GL_FALSE
;
199 is_shader(struct gl_context
*ctx
, GLuint name
)
201 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
202 return shader
? GL_TRUE
: GL_FALSE
;
207 * Attach shader to a shader program.
210 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
212 struct gl_shader_program
*shProg
;
213 struct gl_shader
*sh
;
216 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
218 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
222 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
227 n
= shProg
->NumShaders
;
228 for (i
= 0; i
< n
; i
++) {
229 if (shProg
->Shaders
[i
] == sh
) {
230 /* The shader is already attched to this program. The
231 * GL_ARB_shader_objects spec says:
233 * "The error INVALID_OPERATION is generated by AttachObjectARB
234 * if <obj> is already attached to <containerObj>."
236 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
238 } else if (same_type_disallowed
&&
239 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
240 /* Shader with the same type is already attached to this program,
241 * OpenGL ES 2.0 and 3.0 specs say:
243 * "Multiple shader objects of the same type may not be attached
244 * to a single program object. [...] The error INVALID_OPERATION
245 * is generated if [...] another shader object of the same type
246 * as shader is already attached to program."
248 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
254 shProg
->Shaders
= (struct gl_shader
**)
255 _mesa_realloc(shProg
->Shaders
,
256 n
* sizeof(struct gl_shader
*),
257 (n
+ 1) * sizeof(struct gl_shader
*));
258 if (!shProg
->Shaders
) {
259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
264 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
265 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
266 shProg
->NumShaders
++;
271 create_shader(struct gl_context
*ctx
, GLenum type
)
273 struct gl_shader
*sh
;
276 if (!validate_shader_target(ctx
, type
)) {
277 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
281 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
282 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
283 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
290 create_shader_program(struct gl_context
*ctx
)
293 struct gl_shader_program
*shProg
;
295 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
297 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
299 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
301 assert(shProg
->RefCount
== 1);
308 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
312 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
315 * NOTE: deleting shaders/programs works a bit differently than
316 * texture objects (and buffer objects, etc). Shader/program
317 * handles/IDs exist in the hash table until the object is really
318 * deleted (refcount==0). With texture objects, the handle/ID is
319 * removed from the hash table in glDeleteTextures() while the tex
320 * object itself might linger until its refcount goes to zero.
322 struct gl_shader_program
*shProg
;
324 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
328 if (!shProg
->DeletePending
) {
329 shProg
->DeletePending
= GL_TRUE
;
331 /* effectively, decr shProg's refcount */
332 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
338 delete_shader(struct gl_context
*ctx
, GLuint shader
)
340 struct gl_shader
*sh
;
342 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
346 if (!sh
->DeletePending
) {
347 sh
->DeletePending
= GL_TRUE
;
349 /* effectively, decr sh's refcount */
350 _mesa_reference_shader(ctx
, &sh
, NULL
);
356 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
358 struct gl_shader_program
*shProg
;
362 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
366 n
= shProg
->NumShaders
;
368 for (i
= 0; i
< n
; i
++) {
369 if (shProg
->Shaders
[i
]->Name
== shader
) {
371 struct gl_shader
**newList
;
374 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
376 /* alloc new, smaller array */
378 malloc((n
- 1) * sizeof(struct gl_shader
*));
380 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
383 for (j
= 0; j
< i
; j
++) {
384 newList
[j
] = shProg
->Shaders
[j
];
387 newList
[j
++] = shProg
->Shaders
[i
];
388 free(shProg
->Shaders
);
390 shProg
->Shaders
= newList
;
391 shProg
->NumShaders
= n
- 1;
396 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
397 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
398 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
399 assert(shProg
->Shaders
[j
]->RefCount
> 0);
411 if (is_shader(ctx
, shader
))
412 err
= GL_INVALID_OPERATION
;
413 else if (is_program(ctx
, shader
))
414 err
= GL_INVALID_OPERATION
;
416 err
= GL_INVALID_VALUE
;
417 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
424 * Return list of shaders attached to shader program.
427 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
428 GLsizei
*count
, GLuint
*obj
)
430 struct gl_shader_program
*shProg
=
431 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
434 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
435 obj
[i
] = shProg
->Shaders
[i
]->Name
;
444 * glGetHandleARB() - return ID/name of currently bound shader program.
447 get_handle(struct gl_context
*ctx
, GLenum pname
)
449 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
450 if (ctx
->Shader
.ActiveProgram
)
451 return ctx
->Shader
.ActiveProgram
->Name
;
456 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
463 * glGetProgramiv() - get shader program state.
464 * Note that this is for GLSL shader programs, not ARB vertex/fragment
465 * programs (see glGetProgramivARB).
468 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
470 struct gl_shader_program
*shProg
471 = _mesa_lookup_shader_program(ctx
, program
);
473 /* Is transform feedback available in this context?
476 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
477 || ctx
->API
== API_OPENGL_CORE
478 || _mesa_is_gles3(ctx
);
480 /* Are geometry shaders available in this context?
482 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
484 /* Are uniform buffer objects available in this context?
487 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
488 || ctx
->API
== API_OPENGL_CORE
489 || _mesa_is_gles3(ctx
);
492 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
497 case GL_DELETE_STATUS
:
498 *params
= shProg
->DeletePending
;
501 *params
= shProg
->LinkStatus
;
503 case GL_VALIDATE_STATUS
:
504 *params
= shProg
->Validated
;
506 case GL_INFO_LOG_LENGTH
:
507 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
509 case GL_ATTACHED_SHADERS
:
510 *params
= shProg
->NumShaders
;
512 case GL_ACTIVE_ATTRIBUTES
:
513 *params
= _mesa_count_active_attribs(shProg
);
515 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
516 *params
= _mesa_longest_attribute_name_length(shProg
);
518 case GL_ACTIVE_UNIFORMS
:
519 *params
= shProg
->NumUserUniformStorage
;
521 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
525 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
526 /* Add one for the terminating NUL character for a non-array, and
527 * 4 for the "[0]" and the NUL for an array.
529 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
530 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
539 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
542 *params
= shProg
->TransformFeedback
.NumVarying
;
544 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
550 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
551 /* Add one for the terminating NUL character.
553 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
562 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
565 *params
= shProg
->TransformFeedback
.BufferMode
;
567 case GL_GEOMETRY_VERTICES_OUT_ARB
:
570 *params
= shProg
->Geom
.VerticesOut
;
572 case GL_GEOMETRY_INPUT_TYPE_ARB
:
575 *params
= shProg
->Geom
.InputType
;
577 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
580 *params
= shProg
->Geom
.OutputType
;
582 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
589 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
590 /* Add one for the terminating NUL character.
592 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
601 case GL_ACTIVE_UNIFORM_BLOCKS
:
605 *params
= shProg
->NumUniformBlocks
;
607 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
608 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
609 * only available with desktop OpenGL 3.0+ with the
610 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
612 * On desktop, we ignore the 3.0+ requirement because it is silly.
614 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
617 *params
= shProg
->BinaryRetreivableHint
;
619 case GL_PROGRAM_BINARY_LENGTH
:
626 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
627 _mesa_lookup_enum_by_nr(pname
));
632 * glGetShaderiv() - get GLSL shader state
635 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
637 struct gl_shader
*shader
=
638 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
646 *params
= shader
->Type
;
648 case GL_DELETE_STATUS
:
649 *params
= shader
->DeletePending
;
651 case GL_COMPILE_STATUS
:
652 *params
= shader
->CompileStatus
;
654 case GL_INFO_LOG_LENGTH
:
655 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
657 case GL_SHADER_SOURCE_LENGTH
:
658 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
661 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
668 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
669 GLsizei
*length
, GLchar
*infoLog
)
671 struct gl_shader_program
*shProg
672 = _mesa_lookup_shader_program(ctx
, program
);
674 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
677 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
682 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
683 GLsizei
*length
, GLchar
*infoLog
)
685 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
687 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
690 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
695 * Return shader source code.
698 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
699 GLsizei
*length
, GLchar
*sourceOut
)
701 struct gl_shader
*sh
;
702 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
706 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
711 * Set/replace shader source code. A helper function used by
712 * glShaderSource[ARB] and glCreateShaderProgramEXT.
715 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
717 struct gl_shader
*sh
;
719 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
723 /* free old shader source string and install new one */
724 free((void *)sh
->Source
);
726 sh
->CompileStatus
= GL_FALSE
;
728 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
737 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
739 struct gl_shader
*sh
;
740 struct gl_shader_compiler_options
*options
;
742 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
746 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
748 /* set default pragma state for shader */
749 sh
->Pragmas
= options
->DefaultPragmas
;
752 /* If the user called glCompileShader without first calling
753 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
755 sh
->CompileStatus
= GL_FALSE
;
757 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
758 printf("GLSL source for %s shader %d:\n",
759 _mesa_glsl_shader_target_name(sh
->Type
), sh
->Name
);
760 printf("%s\n", sh
->Source
);
763 /* this call will set the shader->CompileStatus field to indicate if
764 * compilation was successful.
766 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
768 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
769 _mesa_write_shader_to_file(sh
);
772 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
773 if (sh
->CompileStatus
) {
774 printf("GLSL IR for shader %d:\n", sh
->Name
);
775 _mesa_print_ir(sh
->ir
, NULL
);
778 printf("GLSL shader %d failed to compile.\n", sh
->Name
);
780 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
781 printf("GLSL shader %d info log:\n", sh
->Name
);
782 printf("%s\n", sh
->InfoLog
);
788 if (!sh
->CompileStatus
) {
789 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
790 fprintf(stderr
, "GLSL source for %s shader %d:\n",
791 _mesa_glsl_shader_target_name(sh
->Type
), sh
->Name
);
792 fprintf(stderr
, "%s\n", sh
->Source
);
795 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
796 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
797 sh
->Name
, sh
->InfoLog
);
804 * Link a program's shaders.
807 link_program(struct gl_context
*ctx
, GLuint program
)
809 struct gl_shader_program
*shProg
;
811 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
815 /* From the ARB_transform_feedback2 specification:
816 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
817 * the name of a program being used by one or more transform feedback
818 * objects, even if the objects are not currently bound or are paused."
820 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
821 _mesa_error(ctx
, GL_INVALID_OPERATION
,
822 "glLinkProgram(transform feedback is using the program)");
826 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
828 _mesa_glsl_link_shader(ctx
, shProg
);
830 if (shProg
->LinkStatus
== GL_FALSE
&&
831 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
832 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
833 shProg
->Name
, shProg
->InfoLog
);
840 printf("Link %u shaders in program %u: %s\n",
841 shProg
->NumShaders
, shProg
->Name
,
842 shProg
->LinkStatus
? "Success" : "Failed");
844 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
845 printf(" shader %u, type 0x%x\n",
846 shProg
->Shaders
[i
]->Name
,
847 shProg
->Shaders
[i
]->Type
);
854 * Print basic shader info (for debug).
857 print_shader_info(const struct gl_shader_program
*shProg
)
861 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
862 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
863 printf(" %s shader %u, checksum %u\n",
864 _mesa_glsl_shader_target_name(shProg
->Shaders
[i
]->Type
),
865 shProg
->Shaders
[i
]->Name
,
866 shProg
->Shaders
[i
]->SourceChecksum
);
868 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
869 printf(" vert prog %u\n",
870 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
871 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
872 printf(" frag prog %u\n",
873 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
874 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
875 printf(" geom prog %u\n",
876 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
881 * Use the named shader program for subsequent glUniform calls
884 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
887 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
888 _mesa_error(ctx
, GL_INVALID_OPERATION
,
889 "%s(program %u not linked)", caller
, shProg
->Name
);
893 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
894 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
901 use_shader_program(struct gl_context
*ctx
, GLenum type
,
902 struct gl_shader_program
*shProg
)
904 struct gl_shader_program
**target
;
907 case GL_VERTEX_SHADER
:
908 target
= &ctx
->Shader
.CurrentVertexProgram
;
910 || (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)) {
914 case GL_GEOMETRY_SHADER_ARB
:
915 target
= &ctx
->Shader
.CurrentGeometryProgram
;
917 || (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] == NULL
)) {
921 case GL_FRAGMENT_SHADER
:
922 target
= &ctx
->Shader
.CurrentFragmentProgram
;
924 || (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)) {
932 if (*target
!= shProg
) {
933 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
935 /* If the shader is also bound as the current rendering shader, unbind
936 * it from that binding point as well. This ensures that the correct
937 * semantics of glDeleteProgram are maintained.
940 case GL_VERTEX_SHADER
:
943 case GL_GEOMETRY_SHADER_ARB
:
946 case GL_FRAGMENT_SHADER
:
947 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
948 _mesa_reference_shader_program(ctx
,
949 &ctx
->Shader
._CurrentFragmentProgram
,
955 _mesa_reference_shader_program(ctx
, target
, shProg
);
963 * Use the named shader program for subsequent rendering.
966 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
968 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
969 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
970 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
971 _mesa_active_program(ctx
, shProg
, "glUseProgram");
973 if (ctx
->Driver
.UseProgram
)
974 ctx
->Driver
.UseProgram(ctx
, shProg
);
979 * Do validation of the given shader program.
980 * \param errMsg returns error message if validation fails.
981 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
984 validate_shader_program(const struct gl_shader_program
*shProg
,
987 if (!shProg
->LinkStatus
) {
991 /* From the GL spec, a program is invalid if any of these are true:
993 any two active samplers in the current program object are of
994 different types, but refer to the same texture image unit,
996 any active sampler in the current program object refers to a texture
997 image unit where fixed-function fragment processing accesses a
998 texture target that does not match the sampler type, or
1000 the sum of the number of active samplers in the program and the
1001 number of texture image units enabled for fixed-function fragment
1002 processing exceeds the combined limit on the total number of texture
1003 image units allowed.
1008 * Check: any two active samplers in the current program object are of
1009 * different types, but refer to the same texture image unit,
1011 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1019 * Called via glValidateProgram()
1022 validate_program(struct gl_context
*ctx
, GLuint program
)
1024 struct gl_shader_program
*shProg
;
1025 char errMsg
[100] = "";
1027 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1032 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1033 if (!shProg
->Validated
) {
1034 /* update info log */
1035 if (shProg
->InfoLog
) {
1036 ralloc_free(shProg
->InfoLog
);
1038 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1045 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1047 GET_CURRENT_CONTEXT(ctx
);
1048 attach_shader(ctx
, program
, shader
);
1053 _mesa_AttachShader(GLuint program
, GLuint shader
)
1055 GET_CURRENT_CONTEXT(ctx
);
1056 attach_shader(ctx
, program
, shader
);
1061 _mesa_CompileShader(GLhandleARB shaderObj
)
1063 GET_CURRENT_CONTEXT(ctx
);
1064 if (MESA_VERBOSE
& VERBOSE_API
)
1065 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1066 compile_shader(ctx
, shaderObj
);
1071 _mesa_CreateShader(GLenum type
)
1073 GET_CURRENT_CONTEXT(ctx
);
1074 if (MESA_VERBOSE
& VERBOSE_API
)
1075 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1076 return create_shader(ctx
, type
);
1080 GLhandleARB GLAPIENTRY
1081 _mesa_CreateShaderObjectARB(GLenum type
)
1083 GET_CURRENT_CONTEXT(ctx
);
1084 return create_shader(ctx
, type
);
1089 _mesa_CreateProgram(void)
1091 GET_CURRENT_CONTEXT(ctx
);
1092 if (MESA_VERBOSE
& VERBOSE_API
)
1093 _mesa_debug(ctx
, "glCreateProgram\n");
1094 return create_shader_program(ctx
);
1098 GLhandleARB GLAPIENTRY
1099 _mesa_CreateProgramObjectARB(void)
1101 GET_CURRENT_CONTEXT(ctx
);
1102 return create_shader_program(ctx
);
1107 _mesa_DeleteObjectARB(GLhandleARB obj
)
1109 if (MESA_VERBOSE
& VERBOSE_API
) {
1110 GET_CURRENT_CONTEXT(ctx
);
1111 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1115 GET_CURRENT_CONTEXT(ctx
);
1116 FLUSH_VERTICES(ctx
, 0);
1117 if (is_program(ctx
, obj
)) {
1118 delete_shader_program(ctx
, obj
);
1120 else if (is_shader(ctx
, obj
)) {
1121 delete_shader(ctx
, obj
);
1131 _mesa_DeleteProgram(GLuint name
)
1134 GET_CURRENT_CONTEXT(ctx
);
1135 FLUSH_VERTICES(ctx
, 0);
1136 delete_shader_program(ctx
, name
);
1142 _mesa_DeleteShader(GLuint name
)
1145 GET_CURRENT_CONTEXT(ctx
);
1146 FLUSH_VERTICES(ctx
, 0);
1147 delete_shader(ctx
, name
);
1153 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1155 GET_CURRENT_CONTEXT(ctx
);
1156 detach_shader(ctx
, program
, shader
);
1161 _mesa_DetachShader(GLuint program
, GLuint shader
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 detach_shader(ctx
, program
, shader
);
1169 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1170 GLsizei
* count
, GLhandleARB
* obj
)
1172 GET_CURRENT_CONTEXT(ctx
);
1173 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1178 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1179 GLsizei
*count
, GLuint
*obj
)
1181 GET_CURRENT_CONTEXT(ctx
);
1182 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1187 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1188 GLcharARB
* infoLog
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 if (is_program(ctx
, object
)) {
1192 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1194 else if (is_shader(ctx
, object
)) {
1195 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1198 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1204 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1206 GET_CURRENT_CONTEXT(ctx
);
1207 /* Implement in terms of GetProgramiv, GetShaderiv */
1208 if (is_program(ctx
, object
)) {
1209 if (pname
== GL_OBJECT_TYPE_ARB
) {
1210 *params
= GL_PROGRAM_OBJECT_ARB
;
1213 get_programiv(ctx
, object
, pname
, params
);
1216 else if (is_shader(ctx
, object
)) {
1217 if (pname
== GL_OBJECT_TYPE_ARB
) {
1218 *params
= GL_SHADER_OBJECT_ARB
;
1221 get_shaderiv(ctx
, object
, pname
, params
);
1225 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1231 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1234 GLint iparams
[1]; /* XXX is one element enough? */
1235 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1236 params
[0] = (GLfloat
) iparams
[0];
1241 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1243 GET_CURRENT_CONTEXT(ctx
);
1244 get_programiv(ctx
, program
, pname
, params
);
1249 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1251 GET_CURRENT_CONTEXT(ctx
);
1252 get_shaderiv(ctx
, shader
, pname
, params
);
1257 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1258 GLsizei
*length
, GLchar
*infoLog
)
1260 GET_CURRENT_CONTEXT(ctx
);
1261 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1266 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1267 GLsizei
*length
, GLchar
*infoLog
)
1269 GET_CURRENT_CONTEXT(ctx
);
1270 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1275 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1276 GLsizei
*length
, GLcharARB
*sourceOut
)
1278 GET_CURRENT_CONTEXT(ctx
);
1279 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1283 GLhandleARB GLAPIENTRY
1284 _mesa_GetHandleARB(GLenum pname
)
1286 GET_CURRENT_CONTEXT(ctx
);
1287 return get_handle(ctx
, pname
);
1291 GLboolean GLAPIENTRY
1292 _mesa_IsProgram(GLuint name
)
1294 GET_CURRENT_CONTEXT(ctx
);
1295 return is_program(ctx
, name
);
1299 GLboolean GLAPIENTRY
1300 _mesa_IsShader(GLuint name
)
1302 GET_CURRENT_CONTEXT(ctx
);
1303 return is_shader(ctx
, name
);
1308 _mesa_LinkProgram(GLhandleARB programObj
)
1310 GET_CURRENT_CONTEXT(ctx
);
1311 link_program(ctx
, programObj
);
1317 * Read shader source code from a file.
1318 * Useful for debugging to override an app's shader.
1321 read_shader(const char *fname
)
1323 const int max
= 50*1000;
1324 FILE *f
= fopen(fname
, "r");
1325 GLcharARB
*buffer
, *shader
;
1332 buffer
= malloc(max
);
1333 len
= fread(buffer
, 1, max
, f
);
1338 shader
= _mesa_strdup(buffer
);
1346 * Called via glShaderSource() and glShaderSourceARB() API functions.
1347 * Basically, concatenate the source code strings into one long string
1348 * and pass it to _mesa_shader_source().
1351 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1352 const GLcharARB
* const * string
, const GLint
* length
)
1354 GET_CURRENT_CONTEXT(ctx
);
1356 GLsizei i
, totalLength
;
1360 if (!shaderObj
|| string
== NULL
) {
1361 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1366 * This array holds offsets of where the appropriate string ends, thus the
1367 * last element will be set to the total length of the source code.
1369 offsets
= malloc(count
* sizeof(GLint
));
1370 if (offsets
== NULL
) {
1371 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1375 for (i
= 0; i
< count
; i
++) {
1376 if (string
[i
] == NULL
) {
1377 free((GLvoid
*) offsets
);
1378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1379 "glShaderSourceARB(null string)");
1382 if (length
== NULL
|| length
[i
] < 0)
1383 offsets
[i
] = strlen(string
[i
]);
1385 offsets
[i
] = length
[i
];
1386 /* accumulate string lengths */
1388 offsets
[i
] += offsets
[i
- 1];
1391 /* Total length of source string is sum off all strings plus two.
1392 * One extra byte for terminating zero, another extra byte to silence
1393 * valgrind warnings in the parser/grammer code.
1395 totalLength
= offsets
[count
- 1] + 2;
1396 source
= malloc(totalLength
* sizeof(GLcharARB
));
1397 if (source
== NULL
) {
1398 free((GLvoid
*) offsets
);
1399 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1403 for (i
= 0; i
< count
; i
++) {
1404 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1405 memcpy(source
+ start
, string
[i
],
1406 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1408 source
[totalLength
- 1] = '\0';
1409 source
[totalLength
- 2] = '\0';
1412 /* Compute the shader's source code checksum then try to open a file
1413 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1414 * original shader source code. For debugging.
1417 GLcharARB
*newSource
;
1419 checksum
= _mesa_str_checksum(source
);
1421 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1423 newSource
= read_shader(filename
);
1425 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1426 shaderObj
, checksum
, filename
);
1432 shader_source(ctx
, shaderObj
, source
);
1435 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1437 sh
->SourceChecksum
= checksum
; /* save original checksum */
1445 _mesa_UseProgram(GLhandleARB program
)
1447 GET_CURRENT_CONTEXT(ctx
);
1448 struct gl_shader_program
*shProg
;
1450 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1451 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1452 "glUseProgram(transform feedback active)");
1457 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1461 if (!shProg
->LinkStatus
) {
1462 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1463 "glUseProgram(program %u not linked)", program
);
1468 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1469 print_shader_info(shProg
);
1476 _mesa_use_program(ctx
, shProg
);
1481 _mesa_ValidateProgram(GLhandleARB program
)
1483 GET_CURRENT_CONTEXT(ctx
);
1484 validate_program(ctx
, program
);
1489 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1492 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1493 GLint
* range
, GLint
* precision
)
1495 const struct gl_program_constants
*limits
;
1496 const struct gl_precision
*p
;
1497 GET_CURRENT_CONTEXT(ctx
);
1499 switch (shadertype
) {
1500 case GL_VERTEX_SHADER
:
1501 limits
= &ctx
->Const
.VertexProgram
;
1503 case GL_FRAGMENT_SHADER
:
1504 limits
= &ctx
->Const
.FragmentProgram
;
1507 _mesa_error(ctx
, GL_INVALID_ENUM
,
1508 "glGetShaderPrecisionFormat(shadertype)");
1512 switch (precisiontype
) {
1514 p
= &limits
->LowFloat
;
1516 case GL_MEDIUM_FLOAT
:
1517 p
= &limits
->MediumFloat
;
1520 p
= &limits
->HighFloat
;
1523 p
= &limits
->LowInt
;
1526 p
= &limits
->MediumInt
;
1529 p
= &limits
->HighInt
;
1532 _mesa_error(ctx
, GL_INVALID_ENUM
,
1533 "glGetShaderPrecisionFormat(precisiontype)");
1537 range
[0] = p
->RangeMin
;
1538 range
[1] = p
->RangeMax
;
1539 precision
[0] = p
->Precision
;
1544 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1547 _mesa_ReleaseShaderCompiler(void)
1549 _mesa_destroy_shader_compiler_caches();
1554 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1557 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1558 const void* binary
, GLint length
)
1560 GET_CURRENT_CONTEXT(ctx
);
1563 (void) binaryformat
;
1566 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1571 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1572 GLenum
*binaryFormat
, GLvoid
*binary
)
1574 struct gl_shader_program
*shProg
;
1575 GET_CURRENT_CONTEXT(ctx
);
1577 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1581 if (!shProg
->LinkStatus
) {
1582 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1583 "glGetProgramBinary(program %u not linked)",
1589 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1593 /* The ARB_get_program_binary spec says:
1595 * "If <length> is NULL, then no length is returned."
1600 (void) binaryFormat
;
1605 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1606 const GLvoid
*binary
, GLsizei length
)
1608 struct gl_shader_program
*shProg
;
1609 GET_CURRENT_CONTEXT(ctx
);
1611 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1615 (void) binaryFormat
;
1618 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1623 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1625 struct gl_shader_program
*shProg
;
1626 GET_CURRENT_CONTEXT(ctx
);
1628 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1629 "glProgramParameteri");
1634 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1635 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1639 (unsigned) value
> ctx
->Const
.MaxGeometryOutputVertices
) {
1640 _mesa_error(ctx
, GL_INVALID_VALUE
,
1641 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d)",
1645 shProg
->Geom
.VerticesOut
= value
;
1647 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1648 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1654 case GL_LINES_ADJACENCY_ARB
:
1656 case GL_TRIANGLES_ADJACENCY_ARB
:
1657 shProg
->Geom
.InputType
= value
;
1660 _mesa_error(ctx
, GL_INVALID_VALUE
,
1661 "glProgramParameteri(geometry input type = %s)",
1662 _mesa_lookup_enum_by_nr(value
));
1666 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1667 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1673 case GL_TRIANGLE_STRIP
:
1674 shProg
->Geom
.OutputType
= value
;
1677 _mesa_error(ctx
, GL_INVALID_VALUE
,
1678 "glProgramParameteri(geometry output type = %s)",
1679 _mesa_lookup_enum_by_nr(value
));
1683 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1684 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1685 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1686 * even be in the dispatch table, so we shouldn't need to expclicitly
1689 * On desktop, we ignore the 3.0+ requirement because it is silly.
1692 /* The ARB_get_program_binary extension spec says:
1694 * "An INVALID_VALUE error is generated if the <value> argument to
1695 * ProgramParameteri is not TRUE or FALSE."
1697 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1698 _mesa_error(ctx
, GL_INVALID_VALUE
,
1699 "glProgramParameteri(pname=%s, value=%d): "
1700 "value must be 0 or 1.",
1701 _mesa_lookup_enum_by_nr(pname
),
1706 /* No need to notify the driver. Any changes will actually take effect
1707 * the next time the shader is linked.
1709 * The ARB_get_program_binary extension spec says:
1711 * "To indicate that a program binary is likely to be retrieved,
1712 * ProgramParameteri should be called with <pname>
1713 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1714 * will not be in effect until the next time LinkProgram or
1715 * ProgramBinary has been called successfully."
1717 * The resloution of issue 9 in the extension spec also says:
1719 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1720 * to indicate to the GL implementation that this program will
1721 * likely be saved with GetProgramBinary at some point. This will
1722 * give the GL implementation the opportunity to track any state
1723 * changes made to the program before being saved such that when it
1724 * is loaded again a recompile can be avoided."
1726 shProg
->BinaryRetreivableHint
= value
;
1732 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1733 _mesa_lookup_enum_by_nr(pname
));
1737 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1738 struct gl_shader_program
*shProg
)
1740 use_shader_program(ctx
, type
, shProg
);
1742 if (ctx
->Driver
.UseProgram
)
1743 ctx
->Driver
.UseProgram(ctx
, shProg
);
1748 * For GL_EXT_separate_shader_objects
1751 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1753 GET_CURRENT_CONTEXT(ctx
);
1754 struct gl_shader_program
*shProg
= NULL
;
1756 if (!validate_shader_target(ctx
, type
)) {
1757 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1761 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1762 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1763 "glUseShaderProgramEXT(transform feedback is active)");
1768 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1769 "glUseShaderProgramEXT");
1773 if (!shProg
->LinkStatus
) {
1774 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1775 "glUseShaderProgramEXT(program not linked)");
1780 _mesa_use_shader_program(ctx
, type
, shProg
);
1785 * For GL_EXT_separate_shader_objects
1788 _mesa_ActiveProgramEXT(GLuint program
)
1790 GET_CURRENT_CONTEXT(ctx
);
1791 struct gl_shader_program
*shProg
= (program
!= 0)
1792 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1795 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1801 * For GL_EXT_separate_shader_objects
1804 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1806 GET_CURRENT_CONTEXT(ctx
);
1807 const GLuint shader
= create_shader(ctx
, type
);
1811 shader_source(ctx
, shader
, _mesa_strdup(string
));
1812 compile_shader(ctx
, shader
);
1814 program
= create_shader_program(ctx
);
1816 struct gl_shader_program
*shProg
;
1817 struct gl_shader
*sh
;
1818 GLint compiled
= GL_FALSE
;
1820 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1821 sh
= _mesa_lookup_shader(ctx
, shader
);
1823 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1825 attach_shader(ctx
, program
, shader
);
1826 link_program(ctx
, program
);
1827 detach_shader(ctx
, program
, shader
);
1831 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1832 append
-error
-to
-info
-log
;
1833 shProg
->LinkStatus
= GL_FALSE
;
1838 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1841 delete_shader(ctx
, shader
);
1849 * Copy program-specific data generated by linking from the gl_shader_program
1850 * object to a specific gl_program object.
1853 _mesa_copy_linked_program_data(gl_shader_type type
,
1854 const struct gl_shader_program
*src
,
1855 struct gl_program
*dst
)
1858 case MESA_SHADER_VERTEX
: {
1859 struct gl_vertex_program
*dst_vp
= (struct gl_vertex_program
*) dst
;
1860 dst_vp
->UsesClipDistance
= src
->Vert
.UsesClipDistance
;
1863 case MESA_SHADER_GEOMETRY
: {
1864 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1865 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1866 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1867 dst_gp
->InputType
= src
->Geom
.InputType
;
1868 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1869 dst_gp
->UsesClipDistance
= src
->Geom
.UsesClipDistance
;
1870 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;