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 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
747 /* set default pragma state for shader */
748 sh
->Pragmas
= options
->DefaultPragmas
;
751 /* If the user called glCompileShader without first calling
752 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
754 sh
->CompileStatus
= GL_FALSE
;
756 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
757 printf("GLSL source for %s shader %d:\n",
758 _mesa_glsl_shader_target_name(sh
->Type
), sh
->Name
);
759 printf("%s\n", sh
->Source
);
762 /* this call will set the shader->CompileStatus field to indicate if
763 * compilation was successful.
765 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
767 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
768 _mesa_write_shader_to_file(sh
);
771 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
772 if (sh
->CompileStatus
) {
773 printf("GLSL IR for shader %d:\n", sh
->Name
);
774 _mesa_print_ir(sh
->ir
, NULL
);
777 printf("GLSL shader %d failed to compile.\n", sh
->Name
);
779 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
780 printf("GLSL shader %d info log:\n", sh
->Name
);
781 printf("%s\n", sh
->InfoLog
);
787 if (!sh
->CompileStatus
) {
788 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
789 fprintf(stderr
, "GLSL source for %s shader %d:\n",
790 _mesa_glsl_shader_target_name(sh
->Type
), sh
->Name
);
791 fprintf(stderr
, "%s\n", sh
->Source
);
794 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
795 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
796 sh
->Name
, sh
->InfoLog
);
803 * Link a program's shaders.
806 link_program(struct gl_context
*ctx
, GLuint program
)
808 struct gl_shader_program
*shProg
;
809 struct gl_transform_feedback_object
*obj
=
810 ctx
->TransformFeedback
.CurrentObject
;
812 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
817 && (shProg
== ctx
->Shader
.CurrentVertexProgram
818 || shProg
== ctx
->Shader
.CurrentGeometryProgram
819 || shProg
== ctx
->Shader
.CurrentFragmentProgram
)) {
820 _mesa_error(ctx
, GL_INVALID_OPERATION
,
821 "glLinkProgram(transform feedback active)");
825 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
827 _mesa_glsl_link_shader(ctx
, shProg
);
829 if (shProg
->LinkStatus
== GL_FALSE
&&
830 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
831 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
832 shProg
->Name
, shProg
->InfoLog
);
839 printf("Link %u shaders in program %u: %s\n",
840 shProg
->NumShaders
, shProg
->Name
,
841 shProg
->LinkStatus
? "Success" : "Failed");
843 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
844 printf(" shader %u, type 0x%x\n",
845 shProg
->Shaders
[i
]->Name
,
846 shProg
->Shaders
[i
]->Type
);
853 * Print basic shader info (for debug).
856 print_shader_info(const struct gl_shader_program
*shProg
)
860 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
861 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
862 printf(" %s shader %u, checksum %u\n",
863 _mesa_glsl_shader_target_name(shProg
->Shaders
[i
]->Type
),
864 shProg
->Shaders
[i
]->Name
,
865 shProg
->Shaders
[i
]->SourceChecksum
);
867 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
868 printf(" vert prog %u\n",
869 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
870 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
871 printf(" frag prog %u\n",
872 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
873 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
874 printf(" geom prog %u\n",
875 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
880 * Use the named shader program for subsequent glUniform calls
883 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
886 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
888 "%s(program %u not linked)", caller
, shProg
->Name
);
892 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
893 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
900 use_shader_program(struct gl_context
*ctx
, GLenum type
,
901 struct gl_shader_program
*shProg
)
903 struct gl_shader_program
**target
;
906 case GL_VERTEX_SHADER
:
907 target
= &ctx
->Shader
.CurrentVertexProgram
;
909 || (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)) {
913 case GL_GEOMETRY_SHADER_ARB
:
914 target
= &ctx
->Shader
.CurrentGeometryProgram
;
916 || (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] == NULL
)) {
920 case GL_FRAGMENT_SHADER
:
921 target
= &ctx
->Shader
.CurrentFragmentProgram
;
923 || (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)) {
931 if (*target
!= shProg
) {
932 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
934 /* If the shader is also bound as the current rendering shader, unbind
935 * it from that binding point as well. This ensures that the correct
936 * semantics of glDeleteProgram are maintained.
939 case GL_VERTEX_SHADER
:
942 case GL_GEOMETRY_SHADER_ARB
:
945 case GL_FRAGMENT_SHADER
:
946 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
947 _mesa_reference_shader_program(ctx
,
948 &ctx
->Shader
._CurrentFragmentProgram
,
954 _mesa_reference_shader_program(ctx
, target
, shProg
);
962 * Use the named shader program for subsequent rendering.
965 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
967 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
968 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
969 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
970 _mesa_active_program(ctx
, shProg
, "glUseProgram");
972 if (ctx
->Driver
.UseProgram
)
973 ctx
->Driver
.UseProgram(ctx
, shProg
);
978 * Do validation of the given shader program.
979 * \param errMsg returns error message if validation fails.
980 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
983 validate_shader_program(const struct gl_shader_program
*shProg
,
986 if (!shProg
->LinkStatus
) {
990 /* From the GL spec, a program is invalid if any of these are true:
992 any two active samplers in the current program object are of
993 different types, but refer to the same texture image unit,
995 any active sampler in the current program object refers to a texture
996 image unit where fixed-function fragment processing accesses a
997 texture target that does not match the sampler type, or
999 the sum of the number of active samplers in the program and the
1000 number of texture image units enabled for fixed-function fragment
1001 processing exceeds the combined limit on the total number of texture
1002 image units allowed.
1007 * Check: any two active samplers in the current program object are of
1008 * different types, but refer to the same texture image unit,
1010 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1018 * Called via glValidateProgram()
1021 validate_program(struct gl_context
*ctx
, GLuint program
)
1023 struct gl_shader_program
*shProg
;
1024 char errMsg
[100] = "";
1026 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1031 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1032 if (!shProg
->Validated
) {
1033 /* update info log */
1034 if (shProg
->InfoLog
) {
1035 ralloc_free(shProg
->InfoLog
);
1037 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1044 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1046 GET_CURRENT_CONTEXT(ctx
);
1047 attach_shader(ctx
, program
, shader
);
1052 _mesa_AttachShader(GLuint program
, GLuint shader
)
1054 GET_CURRENT_CONTEXT(ctx
);
1055 attach_shader(ctx
, program
, shader
);
1060 _mesa_CompileShader(GLhandleARB shaderObj
)
1062 GET_CURRENT_CONTEXT(ctx
);
1063 if (MESA_VERBOSE
& VERBOSE_API
)
1064 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1065 compile_shader(ctx
, shaderObj
);
1070 _mesa_CreateShader(GLenum type
)
1072 GET_CURRENT_CONTEXT(ctx
);
1073 if (MESA_VERBOSE
& VERBOSE_API
)
1074 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1075 return create_shader(ctx
, type
);
1079 GLhandleARB GLAPIENTRY
1080 _mesa_CreateShaderObjectARB(GLenum type
)
1082 GET_CURRENT_CONTEXT(ctx
);
1083 return create_shader(ctx
, type
);
1088 _mesa_CreateProgram(void)
1090 GET_CURRENT_CONTEXT(ctx
);
1091 if (MESA_VERBOSE
& VERBOSE_API
)
1092 _mesa_debug(ctx
, "glCreateProgram\n");
1093 return create_shader_program(ctx
);
1097 GLhandleARB GLAPIENTRY
1098 _mesa_CreateProgramObjectARB(void)
1100 GET_CURRENT_CONTEXT(ctx
);
1101 return create_shader_program(ctx
);
1106 _mesa_DeleteObjectARB(GLhandleARB obj
)
1108 if (MESA_VERBOSE
& VERBOSE_API
) {
1109 GET_CURRENT_CONTEXT(ctx
);
1110 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1114 GET_CURRENT_CONTEXT(ctx
);
1115 FLUSH_VERTICES(ctx
, 0);
1116 if (is_program(ctx
, obj
)) {
1117 delete_shader_program(ctx
, obj
);
1119 else if (is_shader(ctx
, obj
)) {
1120 delete_shader(ctx
, obj
);
1130 _mesa_DeleteProgram(GLuint name
)
1133 GET_CURRENT_CONTEXT(ctx
);
1134 FLUSH_VERTICES(ctx
, 0);
1135 delete_shader_program(ctx
, name
);
1141 _mesa_DeleteShader(GLuint name
)
1144 GET_CURRENT_CONTEXT(ctx
);
1145 FLUSH_VERTICES(ctx
, 0);
1146 delete_shader(ctx
, name
);
1152 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1154 GET_CURRENT_CONTEXT(ctx
);
1155 detach_shader(ctx
, program
, shader
);
1160 _mesa_DetachShader(GLuint program
, GLuint shader
)
1162 GET_CURRENT_CONTEXT(ctx
);
1163 detach_shader(ctx
, program
, shader
);
1168 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1169 GLsizei
* count
, GLhandleARB
* obj
)
1171 GET_CURRENT_CONTEXT(ctx
);
1172 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1177 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1178 GLsizei
*count
, GLuint
*obj
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1186 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1187 GLcharARB
* infoLog
)
1189 GET_CURRENT_CONTEXT(ctx
);
1190 if (is_program(ctx
, object
)) {
1191 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1193 else if (is_shader(ctx
, object
)) {
1194 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1197 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1203 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1205 GET_CURRENT_CONTEXT(ctx
);
1206 /* Implement in terms of GetProgramiv, GetShaderiv */
1207 if (is_program(ctx
, object
)) {
1208 if (pname
== GL_OBJECT_TYPE_ARB
) {
1209 *params
= GL_PROGRAM_OBJECT_ARB
;
1212 get_programiv(ctx
, object
, pname
, params
);
1215 else if (is_shader(ctx
, object
)) {
1216 if (pname
== GL_OBJECT_TYPE_ARB
) {
1217 *params
= GL_SHADER_OBJECT_ARB
;
1220 get_shaderiv(ctx
, object
, pname
, params
);
1224 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1230 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1233 GLint iparams
[1]; /* XXX is one element enough? */
1234 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1235 params
[0] = (GLfloat
) iparams
[0];
1240 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1242 GET_CURRENT_CONTEXT(ctx
);
1243 get_programiv(ctx
, program
, pname
, params
);
1248 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1250 GET_CURRENT_CONTEXT(ctx
);
1251 get_shaderiv(ctx
, shader
, pname
, params
);
1256 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1257 GLsizei
*length
, GLchar
*infoLog
)
1259 GET_CURRENT_CONTEXT(ctx
);
1260 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1265 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1266 GLsizei
*length
, GLchar
*infoLog
)
1268 GET_CURRENT_CONTEXT(ctx
);
1269 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1274 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1275 GLsizei
*length
, GLcharARB
*sourceOut
)
1277 GET_CURRENT_CONTEXT(ctx
);
1278 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1282 GLhandleARB GLAPIENTRY
1283 _mesa_GetHandleARB(GLenum pname
)
1285 GET_CURRENT_CONTEXT(ctx
);
1286 return get_handle(ctx
, pname
);
1290 GLboolean GLAPIENTRY
1291 _mesa_IsProgram(GLuint name
)
1293 GET_CURRENT_CONTEXT(ctx
);
1294 return is_program(ctx
, name
);
1298 GLboolean GLAPIENTRY
1299 _mesa_IsShader(GLuint name
)
1301 GET_CURRENT_CONTEXT(ctx
);
1302 return is_shader(ctx
, name
);
1307 _mesa_LinkProgram(GLhandleARB programObj
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 link_program(ctx
, programObj
);
1316 * Read shader source code from a file.
1317 * Useful for debugging to override an app's shader.
1320 read_shader(const char *fname
)
1322 const int max
= 50*1000;
1323 FILE *f
= fopen(fname
, "r");
1324 GLcharARB
*buffer
, *shader
;
1331 buffer
= malloc(max
);
1332 len
= fread(buffer
, 1, max
, f
);
1337 shader
= _mesa_strdup(buffer
);
1345 * Called via glShaderSource() and glShaderSourceARB() API functions.
1346 * Basically, concatenate the source code strings into one long string
1347 * and pass it to _mesa_shader_source().
1350 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1351 const GLcharARB
* const * string
, const GLint
* length
)
1353 GET_CURRENT_CONTEXT(ctx
);
1355 GLsizei i
, totalLength
;
1359 if (!shaderObj
|| string
== NULL
) {
1360 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1365 * This array holds offsets of where the appropriate string ends, thus the
1366 * last element will be set to the total length of the source code.
1368 offsets
= malloc(count
* sizeof(GLint
));
1369 if (offsets
== NULL
) {
1370 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1374 for (i
= 0; i
< count
; i
++) {
1375 if (string
[i
] == NULL
) {
1376 free((GLvoid
*) offsets
);
1377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1378 "glShaderSourceARB(null string)");
1381 if (length
== NULL
|| length
[i
] < 0)
1382 offsets
[i
] = strlen(string
[i
]);
1384 offsets
[i
] = length
[i
];
1385 /* accumulate string lengths */
1387 offsets
[i
] += offsets
[i
- 1];
1390 /* Total length of source string is sum off all strings plus two.
1391 * One extra byte for terminating zero, another extra byte to silence
1392 * valgrind warnings in the parser/grammer code.
1394 totalLength
= offsets
[count
- 1] + 2;
1395 source
= malloc(totalLength
* sizeof(GLcharARB
));
1396 if (source
== NULL
) {
1397 free((GLvoid
*) offsets
);
1398 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1402 for (i
= 0; i
< count
; i
++) {
1403 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1404 memcpy(source
+ start
, string
[i
],
1405 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1407 source
[totalLength
- 1] = '\0';
1408 source
[totalLength
- 2] = '\0';
1411 /* Compute the shader's source code checksum then try to open a file
1412 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1413 * original shader source code. For debugging.
1416 GLcharARB
*newSource
;
1418 checksum
= _mesa_str_checksum(source
);
1420 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1422 newSource
= read_shader(filename
);
1424 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1425 shaderObj
, checksum
, filename
);
1431 shader_source(ctx
, shaderObj
, source
);
1434 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1436 sh
->SourceChecksum
= checksum
; /* save original checksum */
1444 _mesa_UseProgram(GLhandleARB program
)
1446 GET_CURRENT_CONTEXT(ctx
);
1447 struct gl_shader_program
*shProg
;
1449 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1451 "glUseProgram(transform feedback active)");
1456 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1460 if (!shProg
->LinkStatus
) {
1461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1462 "glUseProgram(program %u not linked)", program
);
1467 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1468 print_shader_info(shProg
);
1475 _mesa_use_program(ctx
, shProg
);
1480 _mesa_ValidateProgram(GLhandleARB program
)
1482 GET_CURRENT_CONTEXT(ctx
);
1483 validate_program(ctx
, program
);
1488 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1491 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1492 GLint
* range
, GLint
* precision
)
1494 const struct gl_program_constants
*limits
;
1495 const struct gl_precision
*p
;
1496 GET_CURRENT_CONTEXT(ctx
);
1498 switch (shadertype
) {
1499 case GL_VERTEX_SHADER
:
1500 limits
= &ctx
->Const
.VertexProgram
;
1502 case GL_FRAGMENT_SHADER
:
1503 limits
= &ctx
->Const
.FragmentProgram
;
1506 _mesa_error(ctx
, GL_INVALID_ENUM
,
1507 "glGetShaderPrecisionFormat(shadertype)");
1511 switch (precisiontype
) {
1513 p
= &limits
->LowFloat
;
1515 case GL_MEDIUM_FLOAT
:
1516 p
= &limits
->MediumFloat
;
1519 p
= &limits
->HighFloat
;
1522 p
= &limits
->LowInt
;
1525 p
= &limits
->MediumInt
;
1528 p
= &limits
->HighInt
;
1531 _mesa_error(ctx
, GL_INVALID_ENUM
,
1532 "glGetShaderPrecisionFormat(precisiontype)");
1536 range
[0] = p
->RangeMin
;
1537 range
[1] = p
->RangeMax
;
1538 precision
[0] = p
->Precision
;
1543 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1546 _mesa_ReleaseShaderCompiler(void)
1548 _mesa_destroy_shader_compiler_caches();
1553 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1556 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1557 const void* binary
, GLint length
)
1559 GET_CURRENT_CONTEXT(ctx
);
1562 (void) binaryformat
;
1565 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1570 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1571 GLenum
*binaryFormat
, GLvoid
*binary
)
1573 struct gl_shader_program
*shProg
;
1574 GET_CURRENT_CONTEXT(ctx
);
1576 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1580 if (!shProg
->LinkStatus
) {
1581 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1582 "glGetProgramBinary(program %u not linked)",
1588 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1592 /* The ARB_get_program_binary spec says:
1594 * "If <length> is NULL, then no length is returned."
1599 (void) binaryFormat
;
1604 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1605 const GLvoid
*binary
, GLsizei length
)
1607 struct gl_shader_program
*shProg
;
1608 GET_CURRENT_CONTEXT(ctx
);
1610 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1614 (void) binaryFormat
;
1617 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1622 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1624 struct gl_shader_program
*shProg
;
1625 GET_CURRENT_CONTEXT(ctx
);
1627 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1628 "glProgramParameteri");
1633 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1634 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1638 (unsigned) value
> ctx
->Const
.MaxGeometryOutputVertices
) {
1639 _mesa_error(ctx
, GL_INVALID_VALUE
,
1640 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d)",
1644 shProg
->Geom
.VerticesOut
= value
;
1646 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1647 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1653 case GL_LINES_ADJACENCY_ARB
:
1655 case GL_TRIANGLES_ADJACENCY_ARB
:
1656 shProg
->Geom
.InputType
= value
;
1659 _mesa_error(ctx
, GL_INVALID_VALUE
,
1660 "glProgramParameteri(geometry input type = %s)",
1661 _mesa_lookup_enum_by_nr(value
));
1665 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1666 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_geometry_shader4
)
1672 case GL_TRIANGLE_STRIP
:
1673 shProg
->Geom
.OutputType
= value
;
1676 _mesa_error(ctx
, GL_INVALID_VALUE
,
1677 "glProgramParameteri(geometry output type = %s)",
1678 _mesa_lookup_enum_by_nr(value
));
1682 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1683 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1684 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1685 * even be in the dispatch table, so we shouldn't need to expclicitly
1688 * On desktop, we ignore the 3.0+ requirement because it is silly.
1691 /* The ARB_get_program_binary extension spec says:
1693 * "An INVALID_VALUE error is generated if the <value> argument to
1694 * ProgramParameteri is not TRUE or FALSE."
1696 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1697 _mesa_error(ctx
, GL_INVALID_VALUE
,
1698 "glProgramParameteri(pname=%s, value=%d): "
1699 "value must be 0 or 1.",
1700 _mesa_lookup_enum_by_nr(pname
),
1705 /* No need to notify the driver. Any changes will actually take effect
1706 * the next time the shader is linked.
1708 * The ARB_get_program_binary extension spec says:
1710 * "To indicate that a program binary is likely to be retrieved,
1711 * ProgramParameteri should be called with <pname>
1712 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1713 * will not be in effect until the next time LinkProgram or
1714 * ProgramBinary has been called successfully."
1716 * The resloution of issue 9 in the extension spec also says:
1718 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1719 * to indicate to the GL implementation that this program will
1720 * likely be saved with GetProgramBinary at some point. This will
1721 * give the GL implementation the opportunity to track any state
1722 * changes made to the program before being saved such that when it
1723 * is loaded again a recompile can be avoided."
1725 shProg
->BinaryRetreivableHint
= value
;
1731 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1732 _mesa_lookup_enum_by_nr(pname
));
1736 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1737 struct gl_shader_program
*shProg
)
1739 use_shader_program(ctx
, type
, shProg
);
1741 if (ctx
->Driver
.UseProgram
)
1742 ctx
->Driver
.UseProgram(ctx
, shProg
);
1747 * For GL_EXT_separate_shader_objects
1750 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1752 GET_CURRENT_CONTEXT(ctx
);
1753 struct gl_shader_program
*shProg
= NULL
;
1755 if (!validate_shader_target(ctx
, type
)) {
1756 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1760 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1761 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1762 "glUseShaderProgramEXT(transform feedback is active)");
1767 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1768 "glUseShaderProgramEXT");
1772 if (!shProg
->LinkStatus
) {
1773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1774 "glUseShaderProgramEXT(program not linked)");
1779 _mesa_use_shader_program(ctx
, type
, shProg
);
1784 * For GL_EXT_separate_shader_objects
1787 _mesa_ActiveProgramEXT(GLuint program
)
1789 GET_CURRENT_CONTEXT(ctx
);
1790 struct gl_shader_program
*shProg
= (program
!= 0)
1791 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1794 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1800 * For GL_EXT_separate_shader_objects
1803 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1805 GET_CURRENT_CONTEXT(ctx
);
1806 const GLuint shader
= create_shader(ctx
, type
);
1810 shader_source(ctx
, shader
, _mesa_strdup(string
));
1811 compile_shader(ctx
, shader
);
1813 program
= create_shader_program(ctx
);
1815 struct gl_shader_program
*shProg
;
1816 struct gl_shader
*sh
;
1817 GLint compiled
= GL_FALSE
;
1819 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1820 sh
= _mesa_lookup_shader(ctx
, shader
);
1822 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1824 attach_shader(ctx
, program
, shader
);
1825 link_program(ctx
, program
);
1826 detach_shader(ctx
, program
, shader
);
1830 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1831 append
-error
-to
-info
-log
;
1832 shProg
->LinkStatus
= GL_FALSE
;
1837 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1840 delete_shader(ctx
, shader
);
1848 * Copy program-specific data generated by linking from the gl_shader_program
1849 * object to a specific gl_program object.
1852 _mesa_copy_linked_program_data(gl_shader_type type
,
1853 const struct gl_shader_program
*src
,
1854 struct gl_program
*dst
)
1857 case MESA_SHADER_VERTEX
: {
1858 struct gl_vertex_program
*dst_vp
= (struct gl_vertex_program
*) dst
;
1859 dst_vp
->UsesClipDistance
= src
->Vert
.UsesClipDistance
;