2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
30 * Implementation of GLSL-related API functions.
31 * The glUniform* functions are in uniforms.c
35 * 1. Check that the right error code is generated for all _mesa_error() calls.
36 * 2. Insert FLUSH_VERTICES calls in various places
40 #include "main/glheader.h"
41 #include "main/context.h"
42 #include "main/dispatch.h"
43 #include "main/enums.h"
44 #include "main/hash.h"
45 #include "main/mtypes.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "main/transformfeedback.h"
49 #include "main/uniforms.h"
50 #include "program/program.h"
51 #include "program/prog_print.h"
52 #include "program/prog_parameter.h"
55 #include "../glsl/glsl_parser_extras.h"
56 #include "../glsl/ir.h"
57 #include "../glsl/ir_uniform.h"
58 #include "../glsl/program.h"
60 /** Define this to enable shader substitution (see below) */
61 #define SHADER_SUBST 0
65 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
68 get_shader_flags(void)
70 GLbitfield flags
= 0x0;
71 const char *env
= _mesa_getenv("MESA_GLSL");
74 if (strstr(env
, "dump_on_error"))
75 flags
|= GLSL_DUMP_ON_ERROR
;
76 else if (strstr(env
, "dump"))
78 if (strstr(env
, "log"))
80 if (strstr(env
, "nopvert"))
81 flags
|= GLSL_NOP_VERT
;
82 if (strstr(env
, "nopfrag"))
83 flags
|= GLSL_NOP_FRAG
;
84 if (strstr(env
, "nopt"))
86 else if (strstr(env
, "opt"))
88 if (strstr(env
, "uniform"))
89 flags
|= GLSL_UNIFORMS
;
90 if (strstr(env
, "useprog"))
91 flags
|= GLSL_USE_PROG
;
92 if (strstr(env
, "errors"))
93 flags
|= GLSL_REPORT_ERRORS
;
101 * Initialize context's shader state.
104 _mesa_init_shader_state(struct gl_context
*ctx
)
106 /* Device drivers may override these to control what kind of instructions
107 * are generated by the GLSL compiler.
109 struct gl_shader_compiler_options options
;
112 memset(&options
, 0, sizeof(options
));
113 options
.MaxUnrollIterations
= 32;
114 options
.MaxIfDepth
= UINT_MAX
;
116 /* Default pragma settings */
117 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
119 for (sh
= 0; sh
< MESA_SHADER_TYPES
; ++sh
)
120 memcpy(&ctx
->ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
122 ctx
->Shader
.Flags
= get_shader_flags();
127 * Free the per-context shader-related state.
130 _mesa_free_shader_state(struct gl_context
*ctx
)
132 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentVertexProgram
, NULL
);
133 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentGeometryProgram
,
135 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentFragmentProgram
,
137 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
139 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
144 * Copy string from <src> to <dst>, up to maxLength characters, returning
145 * length of <dst> in <length>.
146 * \param src the strings source
147 * \param maxLength max chars to copy
148 * \param length returns number of chars copied
149 * \param dst the string destination
152 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
153 GLsizei
*length
, const GLchar
*src
)
156 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
167 * Confirm that the a shader type is valid and supported by the implementation
169 * \param ctx Current GL context
170 * \param type Shader target
174 validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
177 case GL_FRAGMENT_SHADER
:
178 return ctx
->Extensions
.ARB_fragment_shader
;
179 case GL_VERTEX_SHADER
:
180 return ctx
->Extensions
.ARB_vertex_shader
;
181 case GL_GEOMETRY_SHADER_ARB
:
182 return _mesa_has_geometry_shaders(ctx
);
190 is_program(struct gl_context
*ctx
, GLuint name
)
192 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
193 return shProg
? GL_TRUE
: GL_FALSE
;
198 is_shader(struct gl_context
*ctx
, GLuint name
)
200 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
201 return shader
? GL_TRUE
: GL_FALSE
;
206 * Attach shader to a shader program.
209 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
211 struct gl_shader_program
*shProg
;
212 struct gl_shader
*sh
;
215 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
217 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
221 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
226 n
= shProg
->NumShaders
;
227 for (i
= 0; i
< n
; i
++) {
228 if (shProg
->Shaders
[i
] == sh
) {
229 /* The shader is already attched to this program. The
230 * GL_ARB_shader_objects spec says:
232 * "The error INVALID_OPERATION is generated by AttachObjectARB
233 * if <obj> is already attached to <containerObj>."
235 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
237 } else if (same_type_disallowed
&&
238 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
239 /* Shader with the same type is already attached to this program,
240 * OpenGL ES 2.0 and 3.0 specs say:
242 * "Multiple shader objects of the same type may not be attached
243 * to a single program object. [...] The error INVALID_OPERATION
244 * is generated if [...] another shader object of the same type
245 * as shader is already attached to program."
247 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
253 shProg
->Shaders
= (struct gl_shader
**)
254 _mesa_realloc(shProg
->Shaders
,
255 n
* sizeof(struct gl_shader
*),
256 (n
+ 1) * sizeof(struct gl_shader
*));
257 if (!shProg
->Shaders
) {
258 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
263 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
264 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
265 shProg
->NumShaders
++;
270 create_shader(struct gl_context
*ctx
, GLenum type
)
272 struct gl_shader
*sh
;
275 if (!validate_shader_target(ctx
, type
)) {
276 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
280 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
281 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
282 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
289 create_shader_program(struct gl_context
*ctx
)
292 struct gl_shader_program
*shProg
;
294 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
296 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
298 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
300 assert(shProg
->RefCount
== 1);
307 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
311 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
314 * NOTE: deleting shaders/programs works a bit differently than
315 * texture objects (and buffer objects, etc). Shader/program
316 * handles/IDs exist in the hash table until the object is really
317 * deleted (refcount==0). With texture objects, the handle/ID is
318 * removed from the hash table in glDeleteTextures() while the tex
319 * object itself might linger until its refcount goes to zero.
321 struct gl_shader_program
*shProg
;
323 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
327 if (!shProg
->DeletePending
) {
328 shProg
->DeletePending
= GL_TRUE
;
330 /* effectively, decr shProg's refcount */
331 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
337 delete_shader(struct gl_context
*ctx
, GLuint shader
)
339 struct gl_shader
*sh
;
341 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
345 if (!sh
->DeletePending
) {
346 sh
->DeletePending
= GL_TRUE
;
348 /* effectively, decr sh's refcount */
349 _mesa_reference_shader(ctx
, &sh
, NULL
);
355 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
357 struct gl_shader_program
*shProg
;
361 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
365 n
= shProg
->NumShaders
;
367 for (i
= 0; i
< n
; i
++) {
368 if (shProg
->Shaders
[i
]->Name
== shader
) {
370 struct gl_shader
**newList
;
373 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
375 /* alloc new, smaller array */
377 malloc((n
- 1) * sizeof(struct gl_shader
*));
379 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
382 for (j
= 0; j
< i
; j
++) {
383 newList
[j
] = shProg
->Shaders
[j
];
386 newList
[j
++] = shProg
->Shaders
[i
];
387 free(shProg
->Shaders
);
389 shProg
->Shaders
= newList
;
390 shProg
->NumShaders
= n
- 1;
395 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
396 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
397 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
398 assert(shProg
->Shaders
[j
]->RefCount
> 0);
410 if (is_shader(ctx
, shader
))
411 err
= GL_INVALID_OPERATION
;
412 else if (is_program(ctx
, shader
))
413 err
= GL_INVALID_OPERATION
;
415 err
= GL_INVALID_VALUE
;
416 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
423 * Return list of shaders attached to shader program.
426 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
427 GLsizei
*count
, GLuint
*obj
)
429 struct gl_shader_program
*shProg
=
430 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
433 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
434 obj
[i
] = shProg
->Shaders
[i
]->Name
;
443 * glGetHandleARB() - return ID/name of currently bound shader program.
446 get_handle(struct gl_context
*ctx
, GLenum pname
)
448 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
449 if (ctx
->Shader
.ActiveProgram
)
450 return ctx
->Shader
.ActiveProgram
->Name
;
455 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
462 * glGetProgramiv() - get shader program state.
463 * Note that this is for GLSL shader programs, not ARB vertex/fragment
464 * programs (see glGetProgramivARB).
467 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
469 struct gl_shader_program
*shProg
470 = _mesa_lookup_shader_program(ctx
, program
);
472 /* Is transform feedback available in this context?
475 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
476 || ctx
->API
== API_OPENGL_CORE
477 || _mesa_is_gles3(ctx
);
479 /* Are geometry shaders available in this context?
481 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
483 /* Are uniform buffer objects available in this context?
486 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
487 || ctx
->API
== API_OPENGL_CORE
488 || _mesa_is_gles3(ctx
);
491 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
496 case GL_DELETE_STATUS
:
497 *params
= shProg
->DeletePending
;
500 *params
= shProg
->LinkStatus
;
502 case GL_VALIDATE_STATUS
:
503 *params
= shProg
->Validated
;
505 case GL_INFO_LOG_LENGTH
:
506 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
508 case GL_ATTACHED_SHADERS
:
509 *params
= shProg
->NumShaders
;
511 case GL_ACTIVE_ATTRIBUTES
:
512 *params
= _mesa_count_active_attribs(shProg
);
514 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
515 *params
= _mesa_longest_attribute_name_length(shProg
);
517 case GL_ACTIVE_UNIFORMS
:
518 *params
= shProg
->NumUserUniformStorage
;
520 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
524 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
525 /* Add one for the terminating NUL character for a non-array, and
526 * 4 for the "[0]" and the NUL for an array.
528 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
529 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
538 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
541 *params
= shProg
->TransformFeedback
.NumVarying
;
543 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
549 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
550 /* Add one for the terminating NUL character.
552 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
561 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
564 *params
= shProg
->TransformFeedback
.BufferMode
;
566 case GL_GEOMETRY_VERTICES_OUT_ARB
:
569 *params
= shProg
->Geom
.VerticesOut
;
571 case GL_GEOMETRY_INPUT_TYPE_ARB
:
574 *params
= shProg
->Geom
.InputType
;
576 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
579 *params
= shProg
->Geom
.OutputType
;
581 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
588 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
589 /* Add one for the terminating NUL character.
591 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
600 case GL_ACTIVE_UNIFORM_BLOCKS
:
604 *params
= shProg
->NumUniformBlocks
;
606 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
607 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
608 * only available with desktop OpenGL 3.0+ with the
609 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
611 * On desktop, we ignore the 3.0+ requirement because it is silly.
613 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
616 *params
= shProg
->BinaryRetreivableHint
;
618 case GL_PROGRAM_BINARY_LENGTH
:
625 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
626 _mesa_lookup_enum_by_nr(pname
));
631 * glGetShaderiv() - get GLSL shader state
634 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
636 struct gl_shader
*shader
=
637 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
645 *params
= shader
->Type
;
647 case GL_DELETE_STATUS
:
648 *params
= shader
->DeletePending
;
650 case GL_COMPILE_STATUS
:
651 *params
= shader
->CompileStatus
;
653 case GL_INFO_LOG_LENGTH
:
654 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
656 case GL_SHADER_SOURCE_LENGTH
:
657 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
660 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
667 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
668 GLsizei
*length
, GLchar
*infoLog
)
670 struct gl_shader_program
*shProg
671 = _mesa_lookup_shader_program(ctx
, program
);
673 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
676 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
681 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
682 GLsizei
*length
, GLchar
*infoLog
)
684 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
686 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
689 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
694 * Return shader source code.
697 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
698 GLsizei
*length
, GLchar
*sourceOut
)
700 struct gl_shader
*sh
;
701 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
705 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
710 * Set/replace shader source code. A helper function used by
711 * glShaderSource[ARB] and glCreateShaderProgramEXT.
714 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
716 struct gl_shader
*sh
;
718 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
722 /* free old shader source string and install new one */
723 free((void *)sh
->Source
);
725 sh
->CompileStatus
= GL_FALSE
;
727 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
736 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
738 struct gl_shader
*sh
;
739 struct gl_shader_compiler_options
*options
;
741 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
745 /* Geometry shaders are not yet fully supported, so issue a warning message
746 * if we're compiling one.
748 if (sh
->Type
== GL_GEOMETRY_SHADER
)
749 printf("WARNING: Geometry shader support is currently experimental.\n");
751 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
753 /* set default pragma state for shader */
754 sh
->Pragmas
= options
->DefaultPragmas
;
757 /* If the user called glCompileShader without first calling
758 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
760 sh
->CompileStatus
= GL_FALSE
;
762 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
763 printf("GLSL source for %s shader %d:\n",
764 _mesa_glsl_shader_target_name(sh
->Type
), sh
->Name
);
765 printf("%s\n", sh
->Source
);
768 /* this call will set the shader->CompileStatus field to indicate if
769 * compilation was successful.
771 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
773 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
774 _mesa_write_shader_to_file(sh
);
777 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
778 if (sh
->CompileStatus
) {
779 printf("GLSL IR for shader %d:\n", sh
->Name
);
780 _mesa_print_ir(sh
->ir
, NULL
);
783 printf("GLSL shader %d failed to compile.\n", sh
->Name
);
785 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
786 printf("GLSL shader %d info log:\n", sh
->Name
);
787 printf("%s\n", sh
->InfoLog
);
793 if (!sh
->CompileStatus
) {
794 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
795 fprintf(stderr
, "GLSL source for %s shader %d:\n",
796 _mesa_glsl_shader_target_name(sh
->Type
), sh
->Name
);
797 fprintf(stderr
, "%s\n", sh
->Source
);
800 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
801 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
802 sh
->Name
, sh
->InfoLog
);
809 * Link a program's shaders.
812 link_program(struct gl_context
*ctx
, GLuint program
)
814 struct gl_shader_program
*shProg
;
815 struct gl_transform_feedback_object
*obj
=
816 ctx
->TransformFeedback
.CurrentObject
;
818 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
823 && (shProg
== ctx
->Shader
.CurrentVertexProgram
824 || shProg
== ctx
->Shader
.CurrentGeometryProgram
825 || shProg
== ctx
->Shader
.CurrentFragmentProgram
)) {
826 _mesa_error(ctx
, GL_INVALID_OPERATION
,
827 "glLinkProgram(transform feedback active)");
831 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
833 _mesa_glsl_link_shader(ctx
, shProg
);
835 if (shProg
->LinkStatus
== GL_FALSE
&&
836 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
837 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
838 shProg
->Name
, shProg
->InfoLog
);
845 printf("Link %u shaders in program %u: %s\n",
846 shProg
->NumShaders
, shProg
->Name
,
847 shProg
->LinkStatus
? "Success" : "Failed");
849 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
850 printf(" shader %u, type 0x%x\n",
851 shProg
->Shaders
[i
]->Name
,
852 shProg
->Shaders
[i
]->Type
);
859 * Print basic shader info (for debug).
862 print_shader_info(const struct gl_shader_program
*shProg
)
866 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
867 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
868 printf(" %s shader %u, checksum %u\n",
869 _mesa_glsl_shader_target_name(shProg
->Shaders
[i
]->Type
),
870 shProg
->Shaders
[i
]->Name
,
871 shProg
->Shaders
[i
]->SourceChecksum
);
873 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
874 printf(" vert prog %u\n",
875 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
876 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
877 printf(" frag prog %u\n",
878 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
879 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
880 printf(" geom prog %u\n",
881 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
886 * Use the named shader program for subsequent glUniform calls
889 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
892 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
893 _mesa_error(ctx
, GL_INVALID_OPERATION
,
894 "%s(program %u not linked)", caller
, shProg
->Name
);
898 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
899 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
906 use_shader_program(struct gl_context
*ctx
, GLenum type
,
907 struct gl_shader_program
*shProg
)
909 struct gl_shader_program
**target
;
912 case GL_VERTEX_SHADER
:
913 target
= &ctx
->Shader
.CurrentVertexProgram
;
915 || (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)) {
919 case GL_GEOMETRY_SHADER_ARB
:
920 target
= &ctx
->Shader
.CurrentGeometryProgram
;
922 || (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] == NULL
)) {
926 case GL_FRAGMENT_SHADER
:
927 target
= &ctx
->Shader
.CurrentFragmentProgram
;
929 || (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)) {
937 if (*target
!= shProg
) {
938 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
940 /* If the shader is also bound as the current rendering shader, unbind
941 * it from that binding point as well. This ensures that the correct
942 * semantics of glDeleteProgram are maintained.
945 case GL_VERTEX_SHADER
:
948 case GL_GEOMETRY_SHADER_ARB
:
951 case GL_FRAGMENT_SHADER
:
952 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
953 _mesa_reference_shader_program(ctx
,
954 &ctx
->Shader
._CurrentFragmentProgram
,
960 _mesa_reference_shader_program(ctx
, target
, shProg
);
968 * Use the named shader program for subsequent rendering.
971 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
973 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
974 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
975 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
976 _mesa_active_program(ctx
, shProg
, "glUseProgram");
978 if (ctx
->Driver
.UseProgram
)
979 ctx
->Driver
.UseProgram(ctx
, shProg
);
984 * Do validation of the given shader program.
985 * \param errMsg returns error message if validation fails.
986 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
989 validate_shader_program(const struct gl_shader_program
*shProg
,
992 if (!shProg
->LinkStatus
) {
996 /* From the GL spec, a program is invalid if any of these are true:
998 any two active samplers in the current program object are of
999 different types, but refer to the same texture image unit,
1001 any active sampler in the current program object refers to a texture
1002 image unit where fixed-function fragment processing accesses a
1003 texture target that does not match the sampler type, or
1005 the sum of the number of active samplers in the program and the
1006 number of texture image units enabled for fixed-function fragment
1007 processing exceeds the combined limit on the total number of texture
1008 image units allowed.
1013 * Check: any two active samplers in the current program object are of
1014 * different types, but refer to the same texture image unit,
1016 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1024 * Called via glValidateProgram()
1027 validate_program(struct gl_context
*ctx
, GLuint program
)
1029 struct gl_shader_program
*shProg
;
1030 char errMsg
[100] = "";
1032 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1037 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1038 if (!shProg
->Validated
) {
1039 /* update info log */
1040 if (shProg
->InfoLog
) {
1041 ralloc_free(shProg
->InfoLog
);
1043 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1050 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1052 GET_CURRENT_CONTEXT(ctx
);
1053 attach_shader(ctx
, program
, shader
);
1058 _mesa_AttachShader(GLuint program
, GLuint shader
)
1060 GET_CURRENT_CONTEXT(ctx
);
1061 attach_shader(ctx
, program
, shader
);
1066 _mesa_CompileShader(GLhandleARB shaderObj
)
1068 GET_CURRENT_CONTEXT(ctx
);
1069 if (MESA_VERBOSE
& VERBOSE_API
)
1070 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1071 compile_shader(ctx
, shaderObj
);
1076 _mesa_CreateShader(GLenum type
)
1078 GET_CURRENT_CONTEXT(ctx
);
1079 if (MESA_VERBOSE
& VERBOSE_API
)
1080 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1081 return create_shader(ctx
, type
);
1085 GLhandleARB GLAPIENTRY
1086 _mesa_CreateShaderObjectARB(GLenum type
)
1088 GET_CURRENT_CONTEXT(ctx
);
1089 return create_shader(ctx
, type
);
1094 _mesa_CreateProgram(void)
1096 GET_CURRENT_CONTEXT(ctx
);
1097 if (MESA_VERBOSE
& VERBOSE_API
)
1098 _mesa_debug(ctx
, "glCreateProgram\n");
1099 return create_shader_program(ctx
);
1103 GLhandleARB GLAPIENTRY
1104 _mesa_CreateProgramObjectARB(void)
1106 GET_CURRENT_CONTEXT(ctx
);
1107 return create_shader_program(ctx
);
1112 _mesa_DeleteObjectARB(GLhandleARB obj
)
1114 if (MESA_VERBOSE
& VERBOSE_API
) {
1115 GET_CURRENT_CONTEXT(ctx
);
1116 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1120 GET_CURRENT_CONTEXT(ctx
);
1121 FLUSH_VERTICES(ctx
, 0);
1122 if (is_program(ctx
, obj
)) {
1123 delete_shader_program(ctx
, obj
);
1125 else if (is_shader(ctx
, obj
)) {
1126 delete_shader(ctx
, obj
);
1136 _mesa_DeleteProgram(GLuint name
)
1139 GET_CURRENT_CONTEXT(ctx
);
1140 FLUSH_VERTICES(ctx
, 0);
1141 delete_shader_program(ctx
, name
);
1147 _mesa_DeleteShader(GLuint name
)
1150 GET_CURRENT_CONTEXT(ctx
);
1151 FLUSH_VERTICES(ctx
, 0);
1152 delete_shader(ctx
, name
);
1158 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1160 GET_CURRENT_CONTEXT(ctx
);
1161 detach_shader(ctx
, program
, shader
);
1166 _mesa_DetachShader(GLuint program
, GLuint shader
)
1168 GET_CURRENT_CONTEXT(ctx
);
1169 detach_shader(ctx
, program
, shader
);
1174 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1175 GLsizei
* count
, GLhandleARB
* obj
)
1177 GET_CURRENT_CONTEXT(ctx
);
1178 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1183 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1184 GLsizei
*count
, GLuint
*obj
)
1186 GET_CURRENT_CONTEXT(ctx
);
1187 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1192 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1193 GLcharARB
* infoLog
)
1195 GET_CURRENT_CONTEXT(ctx
);
1196 if (is_program(ctx
, object
)) {
1197 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1199 else if (is_shader(ctx
, object
)) {
1200 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1203 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1209 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1211 GET_CURRENT_CONTEXT(ctx
);
1212 /* Implement in terms of GetProgramiv, GetShaderiv */
1213 if (is_program(ctx
, object
)) {
1214 if (pname
== GL_OBJECT_TYPE_ARB
) {
1215 *params
= GL_PROGRAM_OBJECT_ARB
;
1218 get_programiv(ctx
, object
, pname
, params
);
1221 else if (is_shader(ctx
, object
)) {
1222 if (pname
== GL_OBJECT_TYPE_ARB
) {
1223 *params
= GL_SHADER_OBJECT_ARB
;
1226 get_shaderiv(ctx
, object
, pname
, params
);
1230 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1236 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1239 GLint iparams
[1]; /* XXX is one element enough? */
1240 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1241 params
[0] = (GLfloat
) iparams
[0];
1246 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1248 GET_CURRENT_CONTEXT(ctx
);
1249 get_programiv(ctx
, program
, pname
, params
);
1254 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1256 GET_CURRENT_CONTEXT(ctx
);
1257 get_shaderiv(ctx
, shader
, pname
, params
);
1262 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1263 GLsizei
*length
, GLchar
*infoLog
)
1265 GET_CURRENT_CONTEXT(ctx
);
1266 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1271 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1272 GLsizei
*length
, GLchar
*infoLog
)
1274 GET_CURRENT_CONTEXT(ctx
);
1275 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1280 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1281 GLsizei
*length
, GLcharARB
*sourceOut
)
1283 GET_CURRENT_CONTEXT(ctx
);
1284 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1288 GLhandleARB GLAPIENTRY
1289 _mesa_GetHandleARB(GLenum pname
)
1291 GET_CURRENT_CONTEXT(ctx
);
1292 return get_handle(ctx
, pname
);
1296 GLboolean GLAPIENTRY
1297 _mesa_IsProgram(GLuint name
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 return is_program(ctx
, name
);
1304 GLboolean GLAPIENTRY
1305 _mesa_IsShader(GLuint name
)
1307 GET_CURRENT_CONTEXT(ctx
);
1308 return is_shader(ctx
, name
);
1313 _mesa_LinkProgram(GLhandleARB programObj
)
1315 GET_CURRENT_CONTEXT(ctx
);
1316 link_program(ctx
, programObj
);
1322 * Read shader source code from a file.
1323 * Useful for debugging to override an app's shader.
1326 read_shader(const char *fname
)
1328 const int max
= 50*1000;
1329 FILE *f
= fopen(fname
, "r");
1330 GLcharARB
*buffer
, *shader
;
1337 buffer
= malloc(max
);
1338 len
= fread(buffer
, 1, max
, f
);
1343 shader
= _mesa_strdup(buffer
);
1351 * Called via glShaderSource() and glShaderSourceARB() API functions.
1352 * Basically, concatenate the source code strings into one long string
1353 * and pass it to _mesa_shader_source().
1356 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1357 const GLcharARB
* const * string
, const GLint
* length
)
1359 GET_CURRENT_CONTEXT(ctx
);
1361 GLsizei i
, totalLength
;
1365 if (!shaderObj
|| string
== NULL
) {
1366 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1371 * This array holds offsets of where the appropriate string ends, thus the
1372 * last element will be set to the total length of the source code.
1374 offsets
= malloc(count
* sizeof(GLint
));
1375 if (offsets
== NULL
) {
1376 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1380 for (i
= 0; i
< count
; i
++) {
1381 if (string
[i
] == NULL
) {
1382 free((GLvoid
*) offsets
);
1383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1384 "glShaderSourceARB(null string)");
1387 if (length
== NULL
|| length
[i
] < 0)
1388 offsets
[i
] = strlen(string
[i
]);
1390 offsets
[i
] = length
[i
];
1391 /* accumulate string lengths */
1393 offsets
[i
] += offsets
[i
- 1];
1396 /* Total length of source string is sum off all strings plus two.
1397 * One extra byte for terminating zero, another extra byte to silence
1398 * valgrind warnings in the parser/grammer code.
1400 totalLength
= offsets
[count
- 1] + 2;
1401 source
= malloc(totalLength
* sizeof(GLcharARB
));
1402 if (source
== NULL
) {
1403 free((GLvoid
*) offsets
);
1404 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1408 for (i
= 0; i
< count
; i
++) {
1409 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1410 memcpy(source
+ start
, string
[i
],
1411 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1413 source
[totalLength
- 1] = '\0';
1414 source
[totalLength
- 2] = '\0';
1417 /* Compute the shader's source code checksum then try to open a file
1418 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1419 * original shader source code. For debugging.
1422 GLcharARB
*newSource
;
1424 checksum
= _mesa_str_checksum(source
);
1426 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1428 newSource
= read_shader(filename
);
1430 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1431 shaderObj
, checksum
, filename
);
1437 shader_source(ctx
, shaderObj
, source
);
1440 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1442 sh
->SourceChecksum
= checksum
; /* save original checksum */
1450 _mesa_UseProgram(GLhandleARB program
)
1452 GET_CURRENT_CONTEXT(ctx
);
1453 struct gl_shader_program
*shProg
;
1455 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1456 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1457 "glUseProgram(transform feedback active)");
1462 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1466 if (!shProg
->LinkStatus
) {
1467 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1468 "glUseProgram(program %u not linked)", program
);
1473 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1474 print_shader_info(shProg
);
1481 _mesa_use_program(ctx
, shProg
);
1486 _mesa_ValidateProgram(GLhandleARB program
)
1488 GET_CURRENT_CONTEXT(ctx
);
1489 validate_program(ctx
, program
);
1494 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1497 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1498 GLint
* range
, GLint
* precision
)
1500 const struct gl_program_constants
*limits
;
1501 const struct gl_precision
*p
;
1502 GET_CURRENT_CONTEXT(ctx
);
1504 switch (shadertype
) {
1505 case GL_VERTEX_SHADER
:
1506 limits
= &ctx
->Const
.VertexProgram
;
1508 case GL_FRAGMENT_SHADER
:
1509 limits
= &ctx
->Const
.FragmentProgram
;
1512 _mesa_error(ctx
, GL_INVALID_ENUM
,
1513 "glGetShaderPrecisionFormat(shadertype)");
1517 switch (precisiontype
) {
1519 p
= &limits
->LowFloat
;
1521 case GL_MEDIUM_FLOAT
:
1522 p
= &limits
->MediumFloat
;
1525 p
= &limits
->HighFloat
;
1528 p
= &limits
->LowInt
;
1531 p
= &limits
->MediumInt
;
1534 p
= &limits
->HighInt
;
1537 _mesa_error(ctx
, GL_INVALID_ENUM
,
1538 "glGetShaderPrecisionFormat(precisiontype)");
1542 range
[0] = p
->RangeMin
;
1543 range
[1] = p
->RangeMax
;
1544 precision
[0] = p
->Precision
;
1549 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1552 _mesa_ReleaseShaderCompiler(void)
1554 _mesa_destroy_shader_compiler_caches();
1559 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1562 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1563 const void* binary
, GLint length
)
1565 GET_CURRENT_CONTEXT(ctx
);
1568 (void) binaryformat
;
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1576 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1577 GLenum
*binaryFormat
, GLvoid
*binary
)
1579 struct gl_shader_program
*shProg
;
1580 GET_CURRENT_CONTEXT(ctx
);
1582 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1586 if (!shProg
->LinkStatus
) {
1587 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1588 "glGetProgramBinary(program %u not linked)",
1594 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1598 /* The ARB_get_program_binary spec says:
1600 * "If <length> is NULL, then no length is returned."
1605 (void) binaryFormat
;
1610 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1611 const GLvoid
*binary
, GLsizei length
)
1613 struct gl_shader_program
*shProg
;
1614 GET_CURRENT_CONTEXT(ctx
);
1616 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1620 (void) binaryFormat
;
1623 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1628 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1630 struct gl_shader_program
*shProg
;
1631 GET_CURRENT_CONTEXT(ctx
);
1633 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1634 "glProgramParameteri");
1639 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1640 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1644 (unsigned) value
> ctx
->Const
.MaxGeometryOutputVertices
) {
1645 _mesa_error(ctx
, GL_INVALID_VALUE
,
1646 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d)",
1650 shProg
->Geom
.VerticesOut
= value
;
1652 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1653 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1659 case GL_LINES_ADJACENCY_ARB
:
1661 case GL_TRIANGLES_ADJACENCY_ARB
:
1662 shProg
->Geom
.InputType
= value
;
1665 _mesa_error(ctx
, GL_INVALID_VALUE
,
1666 "glProgramParameteri(geometry input type = %s)",
1667 _mesa_lookup_enum_by_nr(value
));
1671 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1672 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1678 case GL_TRIANGLE_STRIP
:
1679 shProg
->Geom
.OutputType
= value
;
1682 _mesa_error(ctx
, GL_INVALID_VALUE
,
1683 "glProgramParameteri(geometry output type = %s)",
1684 _mesa_lookup_enum_by_nr(value
));
1688 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1689 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1690 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1691 * even be in the dispatch table, so we shouldn't need to expclicitly
1694 * On desktop, we ignore the 3.0+ requirement because it is silly.
1697 /* The ARB_get_program_binary extension spec says:
1699 * "An INVALID_VALUE error is generated if the <value> argument to
1700 * ProgramParameteri is not TRUE or FALSE."
1702 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1703 _mesa_error(ctx
, GL_INVALID_VALUE
,
1704 "glProgramParameteri(pname=%s, value=%d): "
1705 "value must be 0 or 1.",
1706 _mesa_lookup_enum_by_nr(pname
),
1711 /* No need to notify the driver. Any changes will actually take effect
1712 * the next time the shader is linked.
1714 * The ARB_get_program_binary extension spec says:
1716 * "To indicate that a program binary is likely to be retrieved,
1717 * ProgramParameteri should be called with <pname>
1718 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1719 * will not be in effect until the next time LinkProgram or
1720 * ProgramBinary has been called successfully."
1722 * The resloution of issue 9 in the extension spec also says:
1724 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1725 * to indicate to the GL implementation that this program will
1726 * likely be saved with GetProgramBinary at some point. This will
1727 * give the GL implementation the opportunity to track any state
1728 * changes made to the program before being saved such that when it
1729 * is loaded again a recompile can be avoided."
1731 shProg
->BinaryRetreivableHint
= value
;
1737 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1738 _mesa_lookup_enum_by_nr(pname
));
1742 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1743 struct gl_shader_program
*shProg
)
1745 use_shader_program(ctx
, type
, shProg
);
1747 if (ctx
->Driver
.UseProgram
)
1748 ctx
->Driver
.UseProgram(ctx
, shProg
);
1753 * For GL_EXT_separate_shader_objects
1756 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1758 GET_CURRENT_CONTEXT(ctx
);
1759 struct gl_shader_program
*shProg
= NULL
;
1761 if (!validate_shader_target(ctx
, type
)) {
1762 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1766 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1767 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1768 "glUseShaderProgramEXT(transform feedback is active)");
1773 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1774 "glUseShaderProgramEXT");
1778 if (!shProg
->LinkStatus
) {
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "glUseShaderProgramEXT(program not linked)");
1785 _mesa_use_shader_program(ctx
, type
, shProg
);
1790 * For GL_EXT_separate_shader_objects
1793 _mesa_ActiveProgramEXT(GLuint program
)
1795 GET_CURRENT_CONTEXT(ctx
);
1796 struct gl_shader_program
*shProg
= (program
!= 0)
1797 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1800 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1806 * For GL_EXT_separate_shader_objects
1809 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1811 GET_CURRENT_CONTEXT(ctx
);
1812 const GLuint shader
= create_shader(ctx
, type
);
1816 shader_source(ctx
, shader
, _mesa_strdup(string
));
1817 compile_shader(ctx
, shader
);
1819 program
= create_shader_program(ctx
);
1821 struct gl_shader_program
*shProg
;
1822 struct gl_shader
*sh
;
1823 GLint compiled
= GL_FALSE
;
1825 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1826 sh
= _mesa_lookup_shader(ctx
, shader
);
1828 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1830 attach_shader(ctx
, program
, shader
);
1831 link_program(ctx
, program
);
1832 detach_shader(ctx
, program
, shader
);
1836 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1837 append
-error
-to
-info
-log
;
1838 shProg
->LinkStatus
= GL_FALSE
;
1843 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1846 delete_shader(ctx
, shader
);
1854 * Copy program-specific data generated by linking from the gl_shader_program
1855 * object to a specific gl_program object.
1858 _mesa_copy_linked_program_data(gl_shader_type type
,
1859 const struct gl_shader_program
*src
,
1860 struct gl_program
*dst
)
1863 case MESA_SHADER_VERTEX
: {
1864 struct gl_vertex_program
*dst_vp
= (struct gl_vertex_program
*) dst
;
1865 dst_vp
->UsesClipDistance
= src
->Vert
.UsesClipDistance
;
1868 case MESA_SHADER_GEOMETRY
: {
1869 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1870 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1871 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1872 dst_gp
->InputType
= src
->Geom
.InputType
;
1873 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1874 dst_gp
->UsesClipDistance
= src
->Geom
.UsesClipDistance
;
1875 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;