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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Implementation of GLSL-related API functions.
30 * The glUniform* functions are in uniforms.c
34 * 1. Check that the right error code is generated for all _mesa_error() calls.
35 * 2. Insert FLUSH_VERTICES calls in various places
39 #include "main/glheader.h"
40 #include "main/context.h"
41 #include "main/dispatch.h"
42 #include "main/enums.h"
43 #include "main/hash.h"
44 #include "main/mfeatures.h"
45 #include "main/mtypes.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "program/program.h"
49 #include "program/prog_parameter.h"
50 #include "program/prog_uniform.h"
53 #include "../glsl/glsl_parser_extras.h"
55 /** Define this to enable shader substitution (see below) */
56 #define SHADER_SUBST 0
60 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
63 get_shader_flags(void)
65 GLbitfield flags
= 0x0;
66 const char *env
= _mesa_getenv("MESA_GLSL");
69 if (strstr(env
, "dump"))
71 if (strstr(env
, "log"))
73 if (strstr(env
, "nopvert"))
74 flags
|= GLSL_NOP_VERT
;
75 if (strstr(env
, "nopfrag"))
76 flags
|= GLSL_NOP_FRAG
;
77 if (strstr(env
, "nopt"))
79 else if (strstr(env
, "opt"))
81 if (strstr(env
, "uniform"))
82 flags
|= GLSL_UNIFORMS
;
83 if (strstr(env
, "useprog"))
84 flags
|= GLSL_USE_PROG
;
92 * Initialize context's shader state.
95 _mesa_init_shader_state(struct gl_context
*ctx
)
97 /* Device drivers may override these to control what kind of instructions
98 * are generated by the GLSL compiler.
100 struct gl_shader_compiler_options options
;
103 memset(&options
, 0, sizeof(options
));
104 options
.MaxUnrollIterations
= 32;
106 /* Default pragma settings */
107 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
109 for (sh
= 0; sh
< MESA_SHADER_TYPES
; ++sh
)
110 memcpy(&ctx
->ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
112 ctx
->Shader
.Flags
= get_shader_flags();
117 * Free the per-context shader-related state.
120 _mesa_free_shader_state(struct gl_context
*ctx
)
122 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentVertexProgram
, NULL
);
123 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentGeometryProgram
,
125 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentFragmentProgram
,
127 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
132 * Return the size of the given GLSL datatype, in floats (components).
135 _mesa_sizeof_glsl_type(GLenum type
)
144 case GL_SAMPLER_CUBE
:
145 case GL_SAMPLER_1D_SHADOW
:
146 case GL_SAMPLER_2D_SHADOW
:
147 case GL_SAMPLER_2D_RECT_ARB
:
148 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
149 case GL_SAMPLER_1D_ARRAY_EXT
:
150 case GL_SAMPLER_2D_ARRAY_EXT
:
151 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
152 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
153 case GL_SAMPLER_CUBE_SHADOW_EXT
:
157 case GL_UNSIGNED_INT_VEC2
:
162 case GL_UNSIGNED_INT_VEC3
:
167 case GL_UNSIGNED_INT_VEC4
:
171 case GL_FLOAT_MAT2x3
:
172 case GL_FLOAT_MAT2x4
:
173 return 8; /* two float[4] vectors */
175 case GL_FLOAT_MAT3x2
:
176 case GL_FLOAT_MAT3x4
:
177 return 12; /* three float[4] vectors */
179 case GL_FLOAT_MAT4x2
:
180 case GL_FLOAT_MAT4x3
:
181 return 16; /* four float[4] vectors */
183 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
190 * Copy string from <src> to <dst>, up to maxLength characters, returning
191 * length of <dst> in <length>.
192 * \param src the strings source
193 * \param maxLength max chars to copy
194 * \param length returns number of chars copied
195 * \param dst the string destination
198 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
199 GLsizei
*length
, const GLchar
*src
)
202 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
213 * Confirm that the a shader type is valid and supported by the implementation
215 * \param ctx Current GL context
216 * \param type Shader target
220 validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
223 #if FEATURE_ARB_fragment_shader
224 case GL_FRAGMENT_SHADER
:
225 return ctx
->Extensions
.ARB_fragment_shader
;
227 #if FEATURE_ARB_vertex_shader
228 case GL_VERTEX_SHADER
:
229 return ctx
->Extensions
.ARB_vertex_shader
;
231 #if FEATURE_ARB_geometry_shader4
232 case GL_GEOMETRY_SHADER_ARB
:
233 return ctx
->Extensions
.ARB_geometry_shader4
;
242 * Find the length of the longest transform feedback varying name
243 * which was specified with glTransformFeedbackVaryings().
246 longest_feedback_varying_name(const struct gl_shader_program
*shProg
)
250 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
251 GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]);
261 is_program(struct gl_context
*ctx
, GLuint name
)
263 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
264 return shProg
? GL_TRUE
: GL_FALSE
;
269 is_shader(struct gl_context
*ctx
, GLuint name
)
271 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
272 return shader
? GL_TRUE
: GL_FALSE
;
277 * Attach shader to a shader program.
280 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
282 struct gl_shader_program
*shProg
;
283 struct gl_shader
*sh
;
286 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
290 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
295 n
= shProg
->NumShaders
;
296 for (i
= 0; i
< n
; i
++) {
297 if (shProg
->Shaders
[i
] == sh
) {
298 /* The shader is already attched to this program. The
299 * GL_ARB_shader_objects spec says:
301 * "The error INVALID_OPERATION is generated by AttachObjectARB
302 * if <obj> is already attached to <containerObj>."
304 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
310 shProg
->Shaders
= (struct gl_shader
**)
311 _mesa_realloc(shProg
->Shaders
,
312 n
* sizeof(struct gl_shader
*),
313 (n
+ 1) * sizeof(struct gl_shader
*));
314 if (!shProg
->Shaders
) {
315 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
320 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
321 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
322 shProg
->NumShaders
++;
327 get_attrib_location(struct gl_context
*ctx
, GLuint program
, const GLchar
*name
)
329 struct gl_shader_program
*shProg
330 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
336 if (!shProg
->LinkStatus
) {
337 _mesa_error(ctx
, GL_INVALID_OPERATION
,
338 "glGetAttribLocation(program not linked)");
345 if (shProg
->VertexProgram
) {
346 const struct gl_program_parameter_list
*attribs
=
347 shProg
->VertexProgram
->Base
.Attributes
;
349 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
351 return attribs
->Parameters
[i
].StateIndexes
[0];
360 bind_attrib_location(struct gl_context
*ctx
, GLuint program
, GLuint index
,
363 struct gl_shader_program
*shProg
;
364 const GLint size
= -1; /* unknown size */
366 GLenum datatype
= GL_FLOAT_VEC4
;
368 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
369 "glBindAttribLocation");
377 if (strncmp(name
, "gl_", 3) == 0) {
378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
379 "glBindAttribLocation(illegal name)");
383 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
384 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
388 /* this will replace the current value if it's already in the list */
389 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
391 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
396 * Note that this attribute binding won't go into effect until
397 * glLinkProgram is called again.
403 bind_frag_data_location(struct gl_context
*ctx
, GLuint program
,
404 GLuint colorNumber
, const GLchar
*name
)
406 _mesa_problem(ctx
, "bind_frag_data_location() not implemented yet");
411 create_shader(struct gl_context
*ctx
, GLenum type
)
413 struct gl_shader
*sh
;
416 if (!validate_shader_target(ctx
, type
)) {
417 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
421 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
422 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
423 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
430 create_shader_program(struct gl_context
*ctx
)
433 struct gl_shader_program
*shProg
;
435 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
437 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
439 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
441 assert(shProg
->RefCount
== 1);
448 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
452 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
455 * NOTE: deleting shaders/programs works a bit differently than
456 * texture objects (and buffer objects, etc). Shader/program
457 * handles/IDs exist in the hash table until the object is really
458 * deleted (refcount==0). With texture objects, the handle/ID is
459 * removed from the hash table in glDeleteTextures() while the tex
460 * object itself might linger until its refcount goes to zero.
462 struct gl_shader_program
*shProg
;
464 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
468 shProg
->DeletePending
= GL_TRUE
;
470 /* effectively, decr shProg's refcount */
471 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
476 delete_shader(struct gl_context
*ctx
, GLuint shader
)
478 struct gl_shader
*sh
;
480 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
484 sh
->DeletePending
= GL_TRUE
;
486 /* effectively, decr sh's refcount */
487 _mesa_reference_shader(ctx
, &sh
, NULL
);
492 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
494 struct gl_shader_program
*shProg
;
498 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
502 n
= shProg
->NumShaders
;
504 for (i
= 0; i
< n
; i
++) {
505 if (shProg
->Shaders
[i
]->Name
== shader
) {
507 struct gl_shader
**newList
;
510 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
512 /* alloc new, smaller array */
513 newList
= (struct gl_shader
**)
514 malloc((n
- 1) * sizeof(struct gl_shader
*));
516 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
519 for (j
= 0; j
< i
; j
++) {
520 newList
[j
] = shProg
->Shaders
[j
];
523 newList
[j
++] = shProg
->Shaders
[i
];
524 free(shProg
->Shaders
);
526 shProg
->Shaders
= newList
;
527 shProg
->NumShaders
= n
- 1;
532 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
533 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
534 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
535 assert(shProg
->Shaders
[j
]->RefCount
> 0);
547 if (is_shader(ctx
, shader
))
548 err
= GL_INVALID_OPERATION
;
549 else if (is_program(ctx
, shader
))
550 err
= GL_INVALID_OPERATION
;
552 err
= GL_INVALID_VALUE
;
553 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
560 get_active_attrib(struct gl_context
*ctx
, GLuint program
, GLuint index
,
561 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
562 GLenum
*type
, GLchar
*nameOut
)
564 const struct gl_program_parameter_list
*attribs
= NULL
;
565 struct gl_shader_program
*shProg
;
567 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
571 if (shProg
->VertexProgram
)
572 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
574 if (!attribs
|| index
>= attribs
->NumParameters
) {
575 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
579 _mesa_copy_string(nameOut
, maxLength
, length
,
580 attribs
->Parameters
[index
].Name
);
583 *size
= attribs
->Parameters
[index
].Size
584 / _mesa_sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
587 *type
= attribs
->Parameters
[index
].DataType
;
592 * Return list of shaders attached to shader program.
595 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
596 GLsizei
*count
, GLuint
*obj
)
598 struct gl_shader_program
*shProg
=
599 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
602 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
603 obj
[i
] = shProg
->Shaders
[i
]->Name
;
612 get_frag_data_location(struct gl_context
*ctx
, GLuint program
,
615 _mesa_problem(ctx
, "get_frag_data_location() not implemented yet");
622 * glGetHandleARB() - return ID/name of currently bound shader program.
625 get_handle(struct gl_context
*ctx
, GLenum pname
)
627 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
628 if (ctx
->Shader
.ActiveProgram
)
629 return ctx
->Shader
.ActiveProgram
->Name
;
634 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
641 * glGetProgramiv() - get shader program state.
642 * Note that this is for GLSL shader programs, not ARB vertex/fragment
643 * programs (see glGetProgramivARB).
646 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
648 const struct gl_program_parameter_list
*attribs
;
649 struct gl_shader_program
*shProg
650 = _mesa_lookup_shader_program(ctx
, program
);
653 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
657 if (shProg
->VertexProgram
)
658 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
663 case GL_DELETE_STATUS
:
664 *params
= shProg
->DeletePending
;
667 *params
= shProg
->LinkStatus
;
669 case GL_VALIDATE_STATUS
:
670 *params
= shProg
->Validated
;
672 case GL_INFO_LOG_LENGTH
:
673 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
675 case GL_ATTACHED_SHADERS
:
676 *params
= shProg
->NumShaders
;
678 case GL_ACTIVE_ATTRIBUTES
:
679 *params
= attribs
? attribs
->NumParameters
: 0;
681 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
682 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
684 case GL_ACTIVE_UNIFORMS
:
685 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
687 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
688 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
690 (*params
)++; /* add one for terminating zero */
692 case GL_PROGRAM_BINARY_LENGTH_OES
:
695 #if FEATURE_EXT_transform_feedback
696 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
697 *params
= shProg
->TransformFeedback
.NumVarying
;
699 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
:
700 *params
= longest_feedback_varying_name(shProg
) + 1;
702 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
703 *params
= shProg
->TransformFeedback
.BufferMode
;
706 #if FEATURE_ARB_geometry_shader4
707 case GL_GEOMETRY_VERTICES_OUT_ARB
:
708 *params
= shProg
->Geom
.VerticesOut
;
710 case GL_GEOMETRY_INPUT_TYPE_ARB
:
711 *params
= shProg
->Geom
.InputType
;
713 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
714 *params
= shProg
->Geom
.OutputType
;
718 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
725 * glGetShaderiv() - get GLSL shader state
728 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
730 struct gl_shader
*shader
=
731 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
739 *params
= shader
->Type
;
741 case GL_DELETE_STATUS
:
742 *params
= shader
->DeletePending
;
744 case GL_COMPILE_STATUS
:
745 *params
= shader
->CompileStatus
;
747 case GL_INFO_LOG_LENGTH
:
748 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
750 case GL_SHADER_SOURCE_LENGTH
:
751 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
754 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
761 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
762 GLsizei
*length
, GLchar
*infoLog
)
764 struct gl_shader_program
*shProg
765 = _mesa_lookup_shader_program(ctx
, program
);
767 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
770 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
775 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
776 GLsizei
*length
, GLchar
*infoLog
)
778 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
780 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
783 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
788 * Return shader source code.
791 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
792 GLsizei
*length
, GLchar
*sourceOut
)
794 struct gl_shader
*sh
;
795 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
799 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
804 * Set/replace shader source code.
807 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
809 struct gl_shader
*sh
;
811 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
815 /* free old shader source string and install new one */
817 free((void *) sh
->Source
);
820 sh
->CompileStatus
= GL_FALSE
;
822 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
831 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
833 struct gl_shader
*sh
;
834 struct gl_shader_compiler_options
*options
;
836 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
840 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
842 /* set default pragma state for shader */
843 sh
->Pragmas
= options
->DefaultPragmas
;
845 /* this call will set the sh->CompileStatus field to indicate if
846 * compilation was successful.
848 _mesa_glsl_compile_shader(ctx
, sh
);
853 * Link a program's shaders.
856 link_program(struct gl_context
*ctx
, GLuint program
)
858 struct gl_shader_program
*shProg
;
859 struct gl_transform_feedback_object
*obj
=
860 ctx
->TransformFeedback
.CurrentObject
;
862 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
867 && (shProg
== ctx
->Shader
.CurrentVertexProgram
868 || shProg
== ctx
->Shader
.CurrentGeometryProgram
869 || shProg
== ctx
->Shader
.CurrentFragmentProgram
)) {
870 _mesa_error(ctx
, GL_INVALID_OPERATION
,
871 "glLinkProgram(transform feedback active");
875 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
877 _mesa_glsl_link_shader(ctx
, shProg
);
883 printf("Link %u shaders in program %u: %s\n",
884 shProg
->NumShaders
, shProg
->Name
,
885 shProg
->LinkStatus
? "Success" : "Failed");
887 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
888 printf(" shader %u, type 0x%x\n",
889 shProg
->Shaders
[i
]->Name
,
890 shProg
->Shaders
[i
]->Type
);
897 * Print basic shader info (for debug).
900 print_shader_info(const struct gl_shader_program
*shProg
)
904 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
905 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
907 switch (shProg
->Shaders
[i
]->Type
) {
908 case GL_VERTEX_SHADER
:
911 case GL_FRAGMENT_SHADER
:
914 case GL_GEOMETRY_SHADER
:
920 printf(" %s shader %u, checksum %u\n", s
,
921 shProg
->Shaders
[i
]->Name
,
922 shProg
->Shaders
[i
]->SourceChecksum
);
924 if (shProg
->VertexProgram
)
925 printf(" vert prog %u\n", shProg
->VertexProgram
->Base
.Id
);
926 if (shProg
->FragmentProgram
)
927 printf(" frag prog %u\n", shProg
->FragmentProgram
->Base
.Id
);
928 if (shProg
->GeometryProgram
)
929 printf(" geom prog %u\n", shProg
->GeometryProgram
->Base
.Id
);
934 * Use the named shader program for subsequent glUniform calls
937 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
940 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
941 _mesa_error(ctx
, GL_INVALID_OPERATION
,
942 "%s(program %u not linked)", caller
, shProg
->Name
);
946 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
947 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
954 use_shader_program(struct gl_context
*ctx
, GLenum type
,
955 struct gl_shader_program
*shProg
)
957 struct gl_shader_program
**target
;
960 #if FEATURE_ARB_vertex_shader
961 case GL_VERTEX_SHADER
:
962 target
= &ctx
->Shader
.CurrentVertexProgram
;
964 || (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)) {
969 #if FEATURE_ARB_geometry_shader4
970 case GL_GEOMETRY_SHADER_ARB
:
971 target
= &ctx
->Shader
.CurrentGeometryProgram
;
973 || (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] == NULL
)) {
978 #if FEATURE_ARB_fragment_shader
979 case GL_FRAGMENT_SHADER
:
980 target
= &ctx
->Shader
.CurrentFragmentProgram
;
982 || (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)) {
991 if (*target
!= shProg
) {
992 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
993 _mesa_reference_shader_program(ctx
, target
, shProg
);
1001 * Use the named shader program for subsequent rendering.
1004 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1006 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
1007 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
1008 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
1009 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1011 if (ctx
->Driver
.UseProgram
)
1012 ctx
->Driver
.UseProgram(ctx
, shProg
);
1017 * Validate a program's samplers.
1018 * Specifically, check that there aren't two samplers of different types
1019 * pointing to the same texture unit.
1020 * \return GL_TRUE if valid, GL_FALSE if invalid
1023 validate_samplers(const struct gl_program
*prog
, char *errMsg
)
1025 static const char *targetName
[] = {
1035 GLint targetUsed
[MAX_TEXTURE_IMAGE_UNITS
];
1036 GLbitfield samplersUsed
= prog
->SamplersUsed
;
1039 assert(Elements(targetName
) == NUM_TEXTURE_TARGETS
);
1041 if (samplersUsed
== 0x0)
1044 for (i
= 0; i
< Elements(targetUsed
); i
++)
1047 /* walk over bits which are set in 'samplers' */
1048 while (samplersUsed
) {
1050 gl_texture_index target
;
1051 GLint sampler
= _mesa_ffs(samplersUsed
) - 1;
1052 assert(sampler
>= 0);
1053 assert(sampler
< MAX_TEXTURE_IMAGE_UNITS
);
1054 unit
= prog
->SamplerUnits
[sampler
];
1055 target
= prog
->SamplerTargets
[sampler
];
1056 if (targetUsed
[unit
] != -1 && targetUsed
[unit
] != (int) target
) {
1057 _mesa_snprintf(errMsg
, 100,
1058 "Texture unit %d is accessed both as %s and %s",
1059 unit
, targetName
[targetUsed
[unit
]], targetName
[target
]);
1062 targetUsed
[unit
] = target
;
1063 samplersUsed
^= (1 << sampler
);
1071 * Do validation of the given shader program.
1072 * \param errMsg returns error message if validation fails.
1073 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1076 validate_shader_program(const struct gl_shader_program
*shProg
,
1079 const struct gl_vertex_program
*vp
= shProg
->VertexProgram
;
1080 const struct gl_geometry_program
*gp
= shProg
->GeometryProgram
;
1081 const struct gl_fragment_program
*fp
= shProg
->FragmentProgram
;
1083 if (!shProg
->LinkStatus
) {
1087 /* From the GL spec, a program is invalid if any of these are true:
1089 any two active samplers in the current program object are of
1090 different types, but refer to the same texture image unit,
1092 any active sampler in the current program object refers to a texture
1093 image unit where fixed-function fragment processing accesses a
1094 texture target that does not match the sampler type, or
1096 the sum of the number of active samplers in the program and the
1097 number of texture image units enabled for fixed-function fragment
1098 processing exceeds the combined limit on the total number of texture
1099 image units allowed.
1104 * Check: any two active samplers in the current program object are of
1105 * different types, but refer to the same texture image unit,
1107 if (vp
&& !validate_samplers(&vp
->Base
, errMsg
)) {
1110 if (gp
&& !validate_samplers(&gp
->Base
, errMsg
)) {
1113 if (fp
&& !validate_samplers(&fp
->Base
, errMsg
)) {
1122 * Called via glValidateProgram()
1125 validate_program(struct gl_context
*ctx
, GLuint program
)
1127 struct gl_shader_program
*shProg
;
1130 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1135 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1136 if (!shProg
->Validated
) {
1137 /* update info log */
1138 if (shProg
->InfoLog
) {
1139 ralloc_free(shProg
->InfoLog
);
1141 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1148 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1150 GET_CURRENT_CONTEXT(ctx
);
1151 attach_shader(ctx
, program
, shader
);
1156 _mesa_AttachShader(GLuint program
, GLuint shader
)
1158 GET_CURRENT_CONTEXT(ctx
);
1159 attach_shader(ctx
, program
, shader
);
1164 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
1165 const GLcharARB
*name
)
1167 GET_CURRENT_CONTEXT(ctx
);
1168 bind_attrib_location(ctx
, program
, index
, name
);
1172 /* GL_EXT_gpu_shader4, GL3 */
1174 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
1177 GET_CURRENT_CONTEXT(ctx
);
1178 bind_frag_data_location(ctx
, program
, colorNumber
, name
);
1183 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
1185 GET_CURRENT_CONTEXT(ctx
);
1186 if (MESA_VERBOSE
& VERBOSE_API
)
1187 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1188 compile_shader(ctx
, shaderObj
);
1193 _mesa_CreateShader(GLenum type
)
1195 GET_CURRENT_CONTEXT(ctx
);
1196 if (MESA_VERBOSE
& VERBOSE_API
)
1197 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1198 return create_shader(ctx
, type
);
1202 GLhandleARB GLAPIENTRY
1203 _mesa_CreateShaderObjectARB(GLenum type
)
1205 GET_CURRENT_CONTEXT(ctx
);
1206 return create_shader(ctx
, type
);
1211 _mesa_CreateProgram(void)
1213 GET_CURRENT_CONTEXT(ctx
);
1214 if (MESA_VERBOSE
& VERBOSE_API
)
1215 _mesa_debug(ctx
, "glCreateProgram\n");
1216 return create_shader_program(ctx
);
1220 GLhandleARB GLAPIENTRY
1221 _mesa_CreateProgramObjectARB(void)
1223 GET_CURRENT_CONTEXT(ctx
);
1224 return create_shader_program(ctx
);
1229 _mesa_DeleteObjectARB(GLhandleARB obj
)
1231 if (MESA_VERBOSE
& VERBOSE_API
) {
1232 GET_CURRENT_CONTEXT(ctx
);
1233 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1237 GET_CURRENT_CONTEXT(ctx
);
1238 FLUSH_VERTICES(ctx
, 0);
1239 if (is_program(ctx
, obj
)) {
1240 delete_shader_program(ctx
, obj
);
1242 else if (is_shader(ctx
, obj
)) {
1243 delete_shader(ctx
, obj
);
1253 _mesa_DeleteProgram(GLuint name
)
1256 GET_CURRENT_CONTEXT(ctx
);
1257 FLUSH_VERTICES(ctx
, 0);
1258 delete_shader_program(ctx
, name
);
1264 _mesa_DeleteShader(GLuint name
)
1267 GET_CURRENT_CONTEXT(ctx
);
1268 FLUSH_VERTICES(ctx
, 0);
1269 delete_shader(ctx
, name
);
1275 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1277 GET_CURRENT_CONTEXT(ctx
);
1278 detach_shader(ctx
, program
, shader
);
1283 _mesa_DetachShader(GLuint program
, GLuint shader
)
1285 GET_CURRENT_CONTEXT(ctx
);
1286 detach_shader(ctx
, program
, shader
);
1291 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint index
,
1292 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1293 GLenum
* type
, GLcharARB
* name
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 get_active_attrib(ctx
, program
, index
, maxLength
, length
, size
, type
, name
);
1301 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1302 GLsizei
* count
, GLhandleARB
* obj
)
1304 GET_CURRENT_CONTEXT(ctx
);
1305 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1310 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1311 GLsizei
*count
, GLuint
*obj
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1319 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
1321 GET_CURRENT_CONTEXT(ctx
);
1322 return get_attrib_location(ctx
, program
, name
);
1326 /* GL_EXT_gpu_shader4, GL3 */
1328 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
1330 GET_CURRENT_CONTEXT(ctx
);
1331 return get_frag_data_location(ctx
, program
, name
);
1337 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1338 GLcharARB
* infoLog
)
1340 GET_CURRENT_CONTEXT(ctx
);
1341 if (is_program(ctx
, object
)) {
1342 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1344 else if (is_shader(ctx
, object
)) {
1345 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1348 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1354 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1356 GET_CURRENT_CONTEXT(ctx
);
1357 /* Implement in terms of GetProgramiv, GetShaderiv */
1358 if (is_program(ctx
, object
)) {
1359 if (pname
== GL_OBJECT_TYPE_ARB
) {
1360 *params
= GL_PROGRAM_OBJECT_ARB
;
1363 get_programiv(ctx
, object
, pname
, params
);
1366 else if (is_shader(ctx
, object
)) {
1367 if (pname
== GL_OBJECT_TYPE_ARB
) {
1368 *params
= GL_SHADER_OBJECT_ARB
;
1371 get_shaderiv(ctx
, object
, pname
, params
);
1375 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1381 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1384 GLint iparams
[1]; /* XXX is one element enough? */
1385 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1386 params
[0] = (GLfloat
) iparams
[0];
1391 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1393 GET_CURRENT_CONTEXT(ctx
);
1394 get_programiv(ctx
, program
, pname
, params
);
1399 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1401 GET_CURRENT_CONTEXT(ctx
);
1402 get_shaderiv(ctx
, shader
, pname
, params
);
1407 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1408 GLsizei
*length
, GLchar
*infoLog
)
1410 GET_CURRENT_CONTEXT(ctx
);
1411 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1416 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1417 GLsizei
*length
, GLchar
*infoLog
)
1419 GET_CURRENT_CONTEXT(ctx
);
1420 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1425 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1426 GLsizei
*length
, GLcharARB
*sourceOut
)
1428 GET_CURRENT_CONTEXT(ctx
);
1429 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1433 GLhandleARB GLAPIENTRY
1434 _mesa_GetHandleARB(GLenum pname
)
1436 GET_CURRENT_CONTEXT(ctx
);
1437 return get_handle(ctx
, pname
);
1441 GLboolean GLAPIENTRY
1442 _mesa_IsProgram(GLuint name
)
1444 GET_CURRENT_CONTEXT(ctx
);
1445 return is_program(ctx
, name
);
1449 GLboolean GLAPIENTRY
1450 _mesa_IsShader(GLuint name
)
1452 GET_CURRENT_CONTEXT(ctx
);
1453 return is_shader(ctx
, name
);
1458 _mesa_LinkProgramARB(GLhandleARB programObj
)
1460 GET_CURRENT_CONTEXT(ctx
);
1461 link_program(ctx
, programObj
);
1467 * Read shader source code from a file.
1468 * Useful for debugging to override an app's shader.
1471 read_shader(const char *fname
)
1473 const int max
= 50*1000;
1474 FILE *f
= fopen(fname
, "r");
1475 GLcharARB
*buffer
, *shader
;
1482 buffer
= (char *) malloc(max
);
1483 len
= fread(buffer
, 1, max
, f
);
1488 shader
= _mesa_strdup(buffer
);
1496 * Called via glShaderSource() and glShaderSourceARB() API functions.
1497 * Basically, concatenate the source code strings into one long string
1498 * and pass it to _mesa_shader_source().
1501 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1502 const GLcharARB
** string
, const GLint
* length
)
1504 GET_CURRENT_CONTEXT(ctx
);
1506 GLsizei i
, totalLength
;
1510 if (!shaderObj
|| string
== NULL
) {
1511 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1516 * This array holds offsets of where the appropriate string ends, thus the
1517 * last element will be set to the total length of the source code.
1519 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1520 if (offsets
== NULL
) {
1521 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1525 for (i
= 0; i
< count
; i
++) {
1526 if (string
[i
] == NULL
) {
1527 free((GLvoid
*) offsets
);
1528 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1529 "glShaderSourceARB(null string)");
1532 if (length
== NULL
|| length
[i
] < 0)
1533 offsets
[i
] = strlen(string
[i
]);
1535 offsets
[i
] = length
[i
];
1536 /* accumulate string lengths */
1538 offsets
[i
] += offsets
[i
- 1];
1541 /* Total length of source string is sum off all strings plus two.
1542 * One extra byte for terminating zero, another extra byte to silence
1543 * valgrind warnings in the parser/grammer code.
1545 totalLength
= offsets
[count
- 1] + 2;
1546 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1547 if (source
== NULL
) {
1548 free((GLvoid
*) offsets
);
1549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1553 for (i
= 0; i
< count
; i
++) {
1554 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1555 memcpy(source
+ start
, string
[i
],
1556 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1558 source
[totalLength
- 1] = '\0';
1559 source
[totalLength
- 2] = '\0';
1562 /* Compute the shader's source code checksum then try to open a file
1563 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1564 * original shader source code. For debugging.
1567 GLcharARB
*newSource
;
1569 checksum
= _mesa_str_checksum(source
);
1571 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1573 newSource
= read_shader(filename
);
1575 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1576 shaderObj
, checksum
, filename
);
1582 shader_source(ctx
, shaderObj
, source
);
1585 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1587 sh
->SourceChecksum
= checksum
; /* save original checksum */
1595 _mesa_UseProgramObjectARB(GLhandleARB program
)
1597 GET_CURRENT_CONTEXT(ctx
);
1598 struct gl_shader_program
*shProg
;
1599 struct gl_transform_feedback_object
*obj
=
1600 ctx
->TransformFeedback
.CurrentObject
;
1602 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1605 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1606 "glUseProgram(transform feedback active)");
1611 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1615 if (!shProg
->LinkStatus
) {
1616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1617 "glUseProgram(program %u not linked)", program
);
1622 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1623 print_shader_info(shProg
);
1630 _mesa_use_program(ctx
, shProg
);
1635 _mesa_ValidateProgramARB(GLhandleARB program
)
1637 GET_CURRENT_CONTEXT(ctx
);
1638 validate_program(ctx
, program
);
1644 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1645 GLint
* range
, GLint
* precision
)
1647 const struct gl_program_constants
*limits
;
1648 const struct gl_precision
*p
;
1649 GET_CURRENT_CONTEXT(ctx
);
1651 switch (shadertype
) {
1652 case GL_VERTEX_SHADER
:
1653 limits
= &ctx
->Const
.VertexProgram
;
1655 case GL_FRAGMENT_SHADER
:
1656 limits
= &ctx
->Const
.FragmentProgram
;
1659 _mesa_error(ctx
, GL_INVALID_ENUM
,
1660 "glGetShaderPrecisionFormat(shadertype)");
1664 switch (precisiontype
) {
1666 p
= &limits
->LowFloat
;
1668 case GL_MEDIUM_FLOAT
:
1669 p
= &limits
->MediumFloat
;
1672 p
= &limits
->HighFloat
;
1675 p
= &limits
->LowInt
;
1678 p
= &limits
->MediumInt
;
1681 p
= &limits
->HighInt
;
1684 _mesa_error(ctx
, GL_INVALID_ENUM
,
1685 "glGetShaderPrecisionFormat(precisiontype)");
1689 range
[0] = p
->RangeMin
;
1690 range
[1] = p
->RangeMax
;
1691 precision
[0] = p
->Precision
;
1696 _mesa_ReleaseShaderCompiler(void)
1698 _mesa_destroy_shader_compiler_caches();
1703 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1704 const void* binary
, GLint length
)
1706 GET_CURRENT_CONTEXT(ctx
);
1709 (void) binaryformat
;
1712 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1715 #endif /* FEATURE_ES2 */
1718 #if FEATURE_ARB_geometry_shader4
1721 _mesa_ProgramParameteriARB(GLuint program
, GLenum pname
, GLint value
)
1723 struct gl_shader_program
*shProg
;
1724 GET_CURRENT_CONTEXT(ctx
);
1726 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1728 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1729 "glProgramParameteri");
1734 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1736 (unsigned) value
> ctx
->Const
.MaxGeometryOutputVertices
) {
1737 _mesa_error(ctx
, GL_INVALID_VALUE
,
1738 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1742 shProg
->Geom
.VerticesOut
= value
;
1744 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1748 case GL_LINES_ADJACENCY_ARB
:
1750 case GL_TRIANGLES_ADJACENCY_ARB
:
1751 shProg
->Geom
.InputType
= value
;
1754 _mesa_error(ctx
, GL_INVALID_VALUE
,
1755 "glProgramParameteri(geometry input type = %s",
1756 _mesa_lookup_enum_by_nr(value
));
1760 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1764 case GL_TRIANGLE_STRIP
:
1765 shProg
->Geom
.OutputType
= value
;
1768 _mesa_error(ctx
, GL_INVALID_VALUE
,
1769 "glProgramParameteri(geometry output type = %s",
1770 _mesa_lookup_enum_by_nr(value
));
1775 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteriARB(pname=%s)",
1776 _mesa_lookup_enum_by_nr(pname
));
1784 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1785 struct gl_shader_program
*shProg
)
1787 use_shader_program(ctx
, type
, shProg
);
1789 if (ctx
->Driver
.UseProgram
)
1790 ctx
->Driver
.UseProgram(ctx
, shProg
);
1794 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1796 GET_CURRENT_CONTEXT(ctx
);
1797 struct gl_shader_program
*shProg
= NULL
;
1799 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1801 if (!validate_shader_target(ctx
, type
)) {
1802 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1806 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
1807 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1808 "glUseShaderProgramEXT(transform feedback is active)");
1813 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1814 "glUseShaderProgramEXT");
1818 if (!shProg
->LinkStatus
) {
1819 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1820 "glUseShaderProgramEXT(program not linked)");
1825 _mesa_use_shader_program(ctx
, type
, shProg
);
1829 _mesa_ActiveProgramEXT(GLuint program
)
1831 GET_CURRENT_CONTEXT(ctx
);
1832 struct gl_shader_program
*shProg
= (program
!= 0)
1833 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1836 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1841 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1843 GET_CURRENT_CONTEXT(ctx
);
1844 const GLuint shader
= create_shader(ctx
, type
);
1848 shader_source(ctx
, shader
, _mesa_strdup(string
));
1849 compile_shader(ctx
, shader
);
1851 program
= create_shader_program(ctx
);
1853 struct gl_shader_program
*shProg
;
1854 struct gl_shader
*sh
;
1855 GLint compiled
= GL_FALSE
;
1857 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1858 sh
= _mesa_lookup_shader(ctx
, shader
);
1860 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1862 attach_shader(ctx
, program
, shader
);
1863 link_program(ctx
, program
);
1864 detach_shader(ctx
, program
, shader
);
1868 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1869 append
-error
-to
-info
-log
;
1870 shProg
->LinkStatus
= GL_FALSE
;
1875 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1878 delete_shader(ctx
, shader
);
1885 * Plug in shader-related functions into API dispatch table.
1888 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1891 /* GL_ARB_vertex/fragment_shader */
1892 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1893 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1894 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1895 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1896 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1897 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1898 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1899 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1900 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1901 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1902 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1903 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1904 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1905 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1906 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1907 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1910 SET_AttachShader(exec
, _mesa_AttachShader
);
1911 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1912 SET_CreateShader(exec
, _mesa_CreateShader
);
1913 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1914 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1915 SET_DetachShader(exec
, _mesa_DetachShader
);
1916 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1917 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1918 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1919 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1920 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1921 SET_IsProgram(exec
, _mesa_IsProgram
);
1922 SET_IsShader(exec
, _mesa_IsShader
);
1924 #if FEATURE_ARB_vertex_shader
1925 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1926 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1927 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);
1930 #if FEATURE_ARB_geometry_shader4
1931 SET_ProgramParameteriARB(exec
, _mesa_ProgramParameteriARB
);
1934 SET_UseShaderProgramEXT(exec
, _mesa_UseShaderProgramEXT
);
1935 SET_ActiveProgramEXT(exec
, _mesa_ActiveProgramEXT
);
1936 SET_CreateShaderProgramEXT(exec
, _mesa_CreateShaderProgramEXT
);
1938 /* GL_EXT_gpu_shader4 / GL 3.0 */
1939 SET_BindFragDataLocationEXT(exec
, _mesa_BindFragDataLocation
);
1940 SET_GetFragDataLocationEXT(exec
, _mesa_GetFragDataLocation
);
1942 /* GL_ARB_ES2_compatibility */
1943 SET_ReleaseShaderCompiler(exec
, _mesa_ReleaseShaderCompiler
);
1944 SET_GetShaderPrecisionFormat(exec
, _mesa_GetShaderPrecisionFormat
);
1946 #endif /* FEATURE_GL */