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/shaderapi.h"
45 #include "main/shaderobj.h"
46 #include "program/program.h"
47 #include "program/prog_parameter.h"
48 #include "program/prog_uniform.h"
52 /** Define this to enable shader substitution (see below) */
53 #define SHADER_SUBST 0
57 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
60 get_shader_flags(void)
62 GLbitfield flags
= 0x0;
63 const char *env
= _mesa_getenv("MESA_GLSL");
66 if (strstr(env
, "dump"))
68 if (strstr(env
, "log"))
70 if (strstr(env
, "nopvert"))
71 flags
|= GLSL_NOP_VERT
;
72 if (strstr(env
, "nopfrag"))
73 flags
|= GLSL_NOP_FRAG
;
74 if (strstr(env
, "nopt"))
76 else if (strstr(env
, "opt"))
78 if (strstr(env
, "uniform"))
79 flags
|= GLSL_UNIFORMS
;
80 if (strstr(env
, "useprog"))
81 flags
|= GLSL_USE_PROG
;
89 * Initialize context's shader state.
92 _mesa_init_shader_state(GLcontext
*ctx
)
94 /* Device drivers may override these to control what kind of instructions
95 * are generated by the GLSL compiler.
97 ctx
->Shader
.EmitHighLevelInstructions
= GL_TRUE
;
98 ctx
->Shader
.EmitContReturn
= GL_TRUE
;
99 ctx
->Shader
.EmitCondCodes
= GL_FALSE
;
100 ctx
->Shader
.EmitComments
= GL_FALSE
;
101 ctx
->Shader
.EmitNoIfs
= GL_FALSE
;
102 ctx
->Shader
.Flags
= get_shader_flags();
104 /* Default pragma settings */
105 ctx
->Shader
.DefaultPragmas
.IgnoreOptimize
= GL_FALSE
;
106 ctx
->Shader
.DefaultPragmas
.IgnoreDebug
= GL_FALSE
;
107 ctx
->Shader
.DefaultPragmas
.Optimize
= GL_TRUE
;
108 ctx
->Shader
.DefaultPragmas
.Debug
= GL_FALSE
;
113 * Free the per-context shader-related state.
116 _mesa_free_shader_state(GLcontext
*ctx
)
118 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
123 * Return the size of the given GLSL datatype, in floats (components).
126 _mesa_sizeof_glsl_type(GLenum type
)
135 case GL_SAMPLER_CUBE
:
136 case GL_SAMPLER_1D_SHADOW
:
137 case GL_SAMPLER_2D_SHADOW
:
138 case GL_SAMPLER_2D_RECT_ARB
:
139 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
140 case GL_SAMPLER_1D_ARRAY_EXT
:
141 case GL_SAMPLER_2D_ARRAY_EXT
:
142 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
143 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
144 case GL_SAMPLER_CUBE_SHADOW_EXT
:
148 case GL_UNSIGNED_INT_VEC2
:
153 case GL_UNSIGNED_INT_VEC3
:
158 case GL_UNSIGNED_INT_VEC4
:
162 case GL_FLOAT_MAT2x3
:
163 case GL_FLOAT_MAT2x4
:
164 return 8; /* two float[4] vectors */
166 case GL_FLOAT_MAT3x2
:
167 case GL_FLOAT_MAT3x4
:
168 return 12; /* three float[4] vectors */
170 case GL_FLOAT_MAT4x2
:
171 case GL_FLOAT_MAT4x3
:
172 return 16; /* four float[4] vectors */
174 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
181 * Copy string from <src> to <dst>, up to maxLength characters, returning
182 * length of <dst> in <length>.
183 * \param src the strings source
184 * \param maxLength max chars to copy
185 * \param length returns number of chars copied
186 * \param dst the string destination
189 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
190 GLsizei
*length
, const GLchar
*src
)
193 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
204 * Find the length of the longest transform feedback varying name
205 * which was specified with glTransformFeedbackVaryings().
208 longest_feedback_varying_name(const struct gl_shader_program
*shProg
)
212 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
213 GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]);
223 is_program(GLcontext
*ctx
, GLuint name
)
225 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
226 return shProg
? GL_TRUE
: GL_FALSE
;
231 is_shader(GLcontext
*ctx
, GLuint name
)
233 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
234 return shader
? GL_TRUE
: GL_FALSE
;
239 * Attach shader to a shader program.
242 attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
244 struct gl_shader_program
*shProg
;
245 struct gl_shader
*sh
;
248 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
252 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
257 n
= shProg
->NumShaders
;
258 for (i
= 0; i
< n
; i
++) {
259 if (shProg
->Shaders
[i
] == sh
) {
260 /* The shader is already attched to this program. The
261 * GL_ARB_shader_objects spec says:
263 * "The error INVALID_OPERATION is generated by AttachObjectARB
264 * if <obj> is already attached to <containerObj>."
266 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
272 shProg
->Shaders
= (struct gl_shader
**)
273 _mesa_realloc(shProg
->Shaders
,
274 n
* sizeof(struct gl_shader
*),
275 (n
+ 1) * sizeof(struct gl_shader
*));
276 if (!shProg
->Shaders
) {
277 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
282 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
283 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
284 shProg
->NumShaders
++;
289 get_attrib_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
291 struct gl_shader_program
*shProg
292 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
298 if (!shProg
->LinkStatus
) {
299 _mesa_error(ctx
, GL_INVALID_OPERATION
,
300 "glGetAttribLocation(program not linked)");
307 if (shProg
->VertexProgram
) {
308 const struct gl_program_parameter_list
*attribs
=
309 shProg
->VertexProgram
->Base
.Attributes
;
311 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
313 return attribs
->Parameters
[i
].StateIndexes
[0];
322 bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
325 struct gl_shader_program
*shProg
;
326 const GLint size
= -1; /* unknown size */
328 GLenum datatype
= GL_FLOAT_VEC4
;
330 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
331 "glBindAttribLocation");
339 if (strncmp(name
, "gl_", 3) == 0) {
340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
341 "glBindAttribLocation(illegal name)");
345 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
346 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
350 if (shProg
->LinkStatus
) {
351 /* get current index/location for the attribute */
352 oldIndex
= get_attrib_location(ctx
, program
, name
);
358 /* this will replace the current value if it's already in the list */
359 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
361 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
366 * Note that this attribute binding won't go into effect until
367 * glLinkProgram is called again.
373 create_shader(GLcontext
*ctx
, GLenum type
)
375 struct gl_shader
*sh
;
378 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
381 case GL_FRAGMENT_SHADER
:
382 case GL_VERTEX_SHADER
:
383 case GL_GEOMETRY_SHADER_ARB
:
384 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
387 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
391 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
398 create_shader_program(GLcontext
*ctx
)
401 struct gl_shader_program
*shProg
;
403 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
405 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
407 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
409 assert(shProg
->RefCount
== 1);
416 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
420 delete_shader_program(GLcontext
*ctx
, GLuint name
)
423 * NOTE: deleting shaders/programs works a bit differently than
424 * texture objects (and buffer objects, etc). Shader/program
425 * handles/IDs exist in the hash table until the object is really
426 * deleted (refcount==0). With texture objects, the handle/ID is
427 * removed from the hash table in glDeleteTextures() while the tex
428 * object itself might linger until its refcount goes to zero.
430 struct gl_shader_program
*shProg
;
432 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
436 shProg
->DeletePending
= GL_TRUE
;
438 /* effectively, decr shProg's refcount */
439 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
444 delete_shader(GLcontext
*ctx
, GLuint shader
)
446 struct gl_shader
*sh
;
448 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
452 sh
->DeletePending
= GL_TRUE
;
454 /* effectively, decr sh's refcount */
455 _mesa_reference_shader(ctx
, &sh
, NULL
);
460 detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
462 struct gl_shader_program
*shProg
;
466 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
470 n
= shProg
->NumShaders
;
472 for (i
= 0; i
< n
; i
++) {
473 if (shProg
->Shaders
[i
]->Name
== shader
) {
475 struct gl_shader
**newList
;
478 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
480 /* alloc new, smaller array */
481 newList
= (struct gl_shader
**)
482 malloc((n
- 1) * sizeof(struct gl_shader
*));
484 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
487 for (j
= 0; j
< i
; j
++) {
488 newList
[j
] = shProg
->Shaders
[j
];
491 newList
[j
++] = shProg
->Shaders
[i
];
492 free(shProg
->Shaders
);
494 shProg
->Shaders
= newList
;
495 shProg
->NumShaders
= n
- 1;
500 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
501 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
502 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
503 assert(shProg
->Shaders
[j
]->RefCount
> 0);
515 if (is_shader(ctx
, shader
))
516 err
= GL_INVALID_OPERATION
;
517 else if (is_program(ctx
, shader
))
518 err
= GL_INVALID_OPERATION
;
520 err
= GL_INVALID_VALUE
;
521 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
528 get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
529 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
530 GLenum
*type
, GLchar
*nameOut
)
532 const struct gl_program_parameter_list
*attribs
= NULL
;
533 struct gl_shader_program
*shProg
;
535 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
539 if (shProg
->VertexProgram
)
540 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
542 if (!attribs
|| index
>= attribs
->NumParameters
) {
543 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
547 _mesa_copy_string(nameOut
, maxLength
, length
,
548 attribs
->Parameters
[index
].Name
);
551 *size
= attribs
->Parameters
[index
].Size
552 / _mesa_sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
555 *type
= attribs
->Parameters
[index
].DataType
;
560 * Return list of shaders attached to shader program.
563 get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
564 GLsizei
*count
, GLuint
*obj
)
566 struct gl_shader_program
*shProg
=
567 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
570 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
571 obj
[i
] = shProg
->Shaders
[i
]->Name
;
580 * glGetHandleARB() - return ID/name of currently bound shader program.
583 get_handle(GLcontext
*ctx
, GLenum pname
)
585 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
586 if (ctx
->Shader
.CurrentProgram
)
587 return ctx
->Shader
.CurrentProgram
->Name
;
592 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
599 * glGetProgramiv() - get shader program state.
600 * Note that this is for GLSL shader programs, not ARB vertex/fragment
601 * programs (see glGetProgramivARB).
604 get_programiv(GLcontext
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
606 const struct gl_program_parameter_list
*attribs
;
607 struct gl_shader_program
*shProg
608 = _mesa_lookup_shader_program(ctx
, program
);
611 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
615 if (shProg
->VertexProgram
)
616 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
621 case GL_DELETE_STATUS
:
622 *params
= shProg
->DeletePending
;
625 *params
= shProg
->LinkStatus
;
627 case GL_VALIDATE_STATUS
:
628 *params
= shProg
->Validated
;
630 case GL_INFO_LOG_LENGTH
:
631 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
633 case GL_ATTACHED_SHADERS
:
634 *params
= shProg
->NumShaders
;
636 case GL_ACTIVE_ATTRIBUTES
:
637 *params
= attribs
? attribs
->NumParameters
: 0;
639 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
640 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
642 case GL_ACTIVE_UNIFORMS
:
643 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
645 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
646 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
648 (*params
)++; /* add one for terminating zero */
650 case GL_PROGRAM_BINARY_LENGTH_OES
:
653 #if FEATURE_EXT_transform_feedback
654 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
655 *params
= shProg
->TransformFeedback
.NumVarying
;
657 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
:
658 *params
= longest_feedback_varying_name(shProg
) + 1;
660 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
661 *params
= shProg
->TransformFeedback
.BufferMode
;
664 #if FEATURE_ARB_geometry_shader4
665 case GL_GEOMETRY_VERTICES_OUT_ARB
:
666 *params
= shProg
->Geom
.VerticesOut
;
668 case GL_GEOMETRY_INPUT_TYPE_ARB
:
669 *params
= shProg
->Geom
.InputType
;
671 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
672 *params
= shProg
->Geom
.OutputType
;
676 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
683 * glGetShaderiv() - get GLSL shader state
686 get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
688 struct gl_shader
*shader
=
689 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
697 *params
= shader
->Type
;
699 case GL_DELETE_STATUS
:
700 *params
= shader
->DeletePending
;
702 case GL_COMPILE_STATUS
:
703 *params
= shader
->CompileStatus
;
705 case GL_INFO_LOG_LENGTH
:
706 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
708 case GL_SHADER_SOURCE_LENGTH
:
709 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
712 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
719 get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
720 GLsizei
*length
, GLchar
*infoLog
)
722 struct gl_shader_program
*shProg
723 = _mesa_lookup_shader_program(ctx
, program
);
725 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
728 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
733 get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
734 GLsizei
*length
, GLchar
*infoLog
)
736 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
738 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
741 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
746 * Return shader source code.
749 get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
750 GLsizei
*length
, GLchar
*sourceOut
)
752 struct gl_shader
*sh
;
753 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
757 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
762 * Set/replace shader source code.
765 shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
767 struct gl_shader
*sh
;
769 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
773 /* free old shader source string and install new one */
775 free((void *) sh
->Source
);
778 sh
->CompileStatus
= GL_FALSE
;
780 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
789 compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
791 struct gl_shader
*sh
;
793 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
797 /* set default pragma state for shader */
798 sh
->Pragmas
= ctx
->Shader
.DefaultPragmas
;
800 /* this call will set the sh->CompileStatus field to indicate if
801 * compilation was successful.
803 _mesa_glsl_compile_shader(ctx
, sh
);
808 * Link a program's shaders.
811 link_program(GLcontext
*ctx
, GLuint program
)
813 struct gl_shader_program
*shProg
;
814 struct gl_transform_feedback_object
*obj
=
815 ctx
->TransformFeedback
.CurrentObject
;
817 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
821 if (obj
->Active
&& shProg
== ctx
->Shader
.CurrentProgram
) {
822 _mesa_error(ctx
, GL_INVALID_OPERATION
,
823 "glLinkProgram(transform feedback active");
827 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
829 _mesa_glsl_link_shader(ctx
, shProg
);
835 printf("Link %u shaders in program %u: %s\n",
836 shProg
->NumShaders
, shProg
->Name
,
837 shProg
->LinkStatus
? "Success" : "Failed");
839 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
840 printf(" shader %u, type 0x%x\n",
841 shProg
->Shaders
[i
]->Name
,
842 shProg
->Shaders
[i
]->Type
);
849 * Print basic shader info (for debug).
852 print_shader_info(const struct gl_shader_program
*shProg
)
856 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
857 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
859 switch (shProg
->Shaders
[i
]->Type
) {
860 case GL_VERTEX_SHADER
:
863 case GL_FRAGMENT_SHADER
:
866 case GL_GEOMETRY_SHADER
:
872 printf(" %s shader %u, checksum %u\n", s
,
873 shProg
->Shaders
[i
]->Name
,
874 shProg
->Shaders
[i
]->SourceChecksum
);
876 if (shProg
->VertexProgram
)
877 printf(" vert prog %u\n", shProg
->VertexProgram
->Base
.Id
);
878 if (shProg
->FragmentProgram
)
879 printf(" frag prog %u\n", shProg
->FragmentProgram
->Base
.Id
);
884 * Use the named shader program for subsequent rendering.
887 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
889 struct gl_shader_program
*shProg
;
890 struct gl_transform_feedback_object
*obj
=
891 ctx
->TransformFeedback
.CurrentObject
;
894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
895 "glUseProgram(transform feedback active)");
899 if (ctx
->Shader
.CurrentProgram
&&
900 ctx
->Shader
.CurrentProgram
->Name
== program
) {
906 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
910 if (!shProg
->LinkStatus
) {
911 _mesa_error(ctx
, GL_INVALID_OPERATION
,
912 "glUseProgram(program %u not linked)", program
);
917 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
918 print_shader_info(shProg
);
925 if (ctx
->Shader
.CurrentProgram
!= shProg
) {
926 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
927 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
930 if (ctx
->Driver
.UseProgram
)
931 ctx
->Driver
.UseProgram(ctx
, shProg
);
936 * Validate a program's samplers.
937 * Specifically, check that there aren't two samplers of different types
938 * pointing to the same texture unit.
939 * \return GL_TRUE if valid, GL_FALSE if invalid
942 validate_samplers(GLcontext
*ctx
, const struct gl_program
*prog
, char *errMsg
)
944 static const char *targetName
[] = {
953 GLint targetUsed
[MAX_TEXTURE_IMAGE_UNITS
];
954 GLbitfield samplersUsed
= prog
->SamplersUsed
;
957 assert(Elements(targetName
) == NUM_TEXTURE_TARGETS
);
959 if (samplersUsed
== 0x0)
962 for (i
= 0; i
< Elements(targetUsed
); i
++)
965 /* walk over bits which are set in 'samplers' */
966 while (samplersUsed
) {
968 gl_texture_index target
;
969 GLint sampler
= _mesa_ffs(samplersUsed
) - 1;
970 assert(sampler
>= 0);
971 assert(sampler
< MAX_TEXTURE_IMAGE_UNITS
);
972 unit
= prog
->SamplerUnits
[sampler
];
973 target
= prog
->SamplerTargets
[sampler
];
974 if (targetUsed
[unit
] != -1 && targetUsed
[unit
] != target
) {
975 _mesa_snprintf(errMsg
, 100,
976 "Texture unit %d is accessed both as %s and %s",
977 unit
, targetName
[targetUsed
[unit
]], targetName
[target
]);
980 targetUsed
[unit
] = target
;
981 samplersUsed
^= (1 << sampler
);
989 * Do validation of the given shader program.
990 * \param errMsg returns error message if validation fails.
991 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
994 validate_shader_program(GLcontext
*ctx
,
995 const struct gl_shader_program
*shProg
,
998 const struct gl_vertex_program
*vp
= shProg
->VertexProgram
;
999 const struct gl_fragment_program
*fp
= shProg
->FragmentProgram
;
1001 if (!shProg
->LinkStatus
) {
1005 /* From the GL spec, a program is invalid if any of these are true:
1007 any two active samplers in the current program object are of
1008 different types, but refer to the same texture image unit,
1010 any active sampler in the current program object refers to a texture
1011 image unit where fixed-function fragment processing accesses a
1012 texture target that does not match the sampler type, or
1014 the sum of the number of active samplers in the program and the
1015 number of texture image units enabled for fixed-function fragment
1016 processing exceeds the combined limit on the total number of texture
1017 image units allowed.
1022 * Check: any two active samplers in the current program object are of
1023 * different types, but refer to the same texture image unit,
1025 if (vp
&& !validate_samplers(ctx
, &vp
->Base
, errMsg
)) {
1028 if (fp
&& !validate_samplers(ctx
, &fp
->Base
, errMsg
)) {
1037 * Called via glValidateProgram()
1040 validate_program(GLcontext
*ctx
, GLuint program
)
1042 struct gl_shader_program
*shProg
;
1045 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1050 shProg
->Validated
= validate_shader_program(ctx
, shProg
, errMsg
);
1051 if (!shProg
->Validated
) {
1052 /* update info log */
1053 if (shProg
->InfoLog
) {
1054 talloc_free(shProg
->InfoLog
);
1056 shProg
->InfoLog
= talloc_strdup(shProg
, errMsg
);
1063 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1065 GET_CURRENT_CONTEXT(ctx
);
1066 attach_shader(ctx
, program
, shader
);
1071 _mesa_AttachShader(GLuint program
, GLuint shader
)
1073 GET_CURRENT_CONTEXT(ctx
);
1074 attach_shader(ctx
, program
, shader
);
1079 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
1080 const GLcharARB
*name
)
1082 GET_CURRENT_CONTEXT(ctx
);
1083 bind_attrib_location(ctx
, program
, index
, name
);
1088 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
1090 GET_CURRENT_CONTEXT(ctx
);
1091 compile_shader(ctx
, shaderObj
);
1096 _mesa_CreateShader(GLenum type
)
1098 GET_CURRENT_CONTEXT(ctx
);
1099 return create_shader(ctx
, type
);
1103 GLhandleARB GLAPIENTRY
1104 _mesa_CreateShaderObjectARB(GLenum type
)
1106 GET_CURRENT_CONTEXT(ctx
);
1107 return create_shader(ctx
, type
);
1112 _mesa_CreateProgram(void)
1114 GET_CURRENT_CONTEXT(ctx
);
1115 return create_shader_program(ctx
);
1119 GLhandleARB GLAPIENTRY
1120 _mesa_CreateProgramObjectARB(void)
1122 GET_CURRENT_CONTEXT(ctx
);
1123 return create_shader_program(ctx
);
1128 _mesa_DeleteObjectARB(GLhandleARB obj
)
1131 GET_CURRENT_CONTEXT(ctx
);
1132 if (is_program(ctx
, obj
)) {
1133 delete_shader_program(ctx
, obj
);
1135 else if (is_shader(ctx
, obj
)) {
1136 delete_shader(ctx
, obj
);
1146 _mesa_DeleteProgram(GLuint name
)
1149 GET_CURRENT_CONTEXT(ctx
);
1150 delete_shader_program(ctx
, name
);
1156 _mesa_DeleteShader(GLuint name
)
1159 GET_CURRENT_CONTEXT(ctx
);
1160 delete_shader(ctx
, name
);
1166 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1168 GET_CURRENT_CONTEXT(ctx
);
1169 detach_shader(ctx
, program
, shader
);
1174 _mesa_DetachShader(GLuint program
, GLuint shader
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 detach_shader(ctx
, program
, shader
);
1182 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint index
,
1183 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1184 GLenum
* type
, GLcharARB
* name
)
1186 GET_CURRENT_CONTEXT(ctx
);
1187 get_active_attrib(ctx
, program
, index
, maxLength
, length
, size
, type
, name
);
1192 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1193 GLsizei
* count
, GLhandleARB
* obj
)
1195 GET_CURRENT_CONTEXT(ctx
);
1196 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1201 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1202 GLsizei
*count
, GLuint
*obj
)
1204 GET_CURRENT_CONTEXT(ctx
);
1205 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1210 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
1212 GET_CURRENT_CONTEXT(ctx
);
1213 return get_attrib_location(ctx
, program
, name
);
1218 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1219 GLcharARB
* infoLog
)
1221 GET_CURRENT_CONTEXT(ctx
);
1222 if (is_program(ctx
, object
)) {
1223 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1225 else if (is_shader(ctx
, object
)) {
1226 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1229 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1235 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1237 GET_CURRENT_CONTEXT(ctx
);
1238 /* Implement in terms of GetProgramiv, GetShaderiv */
1239 if (is_program(ctx
, object
)) {
1240 if (pname
== GL_OBJECT_TYPE_ARB
) {
1241 *params
= GL_PROGRAM_OBJECT_ARB
;
1244 get_programiv(ctx
, object
, pname
, params
);
1247 else if (is_shader(ctx
, object
)) {
1248 if (pname
== GL_OBJECT_TYPE_ARB
) {
1249 *params
= GL_SHADER_OBJECT_ARB
;
1252 get_shaderiv(ctx
, object
, pname
, params
);
1256 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1262 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1265 GLint iparams
[1]; /* XXX is one element enough? */
1266 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1267 params
[0] = (GLfloat
) iparams
[0];
1272 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1274 GET_CURRENT_CONTEXT(ctx
);
1275 get_programiv(ctx
, program
, pname
, params
);
1280 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1282 GET_CURRENT_CONTEXT(ctx
);
1283 get_shaderiv(ctx
, shader
, pname
, params
);
1288 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1289 GLsizei
*length
, GLchar
*infoLog
)
1291 GET_CURRENT_CONTEXT(ctx
);
1292 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1297 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1298 GLsizei
*length
, GLchar
*infoLog
)
1300 GET_CURRENT_CONTEXT(ctx
);
1301 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1306 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1307 GLsizei
*length
, GLcharARB
*sourceOut
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1314 GLhandleARB GLAPIENTRY
1315 _mesa_GetHandleARB(GLenum pname
)
1317 GET_CURRENT_CONTEXT(ctx
);
1318 return get_handle(ctx
, pname
);
1322 GLboolean GLAPIENTRY
1323 _mesa_IsProgram(GLuint name
)
1325 GET_CURRENT_CONTEXT(ctx
);
1326 return is_program(ctx
, name
);
1330 GLboolean GLAPIENTRY
1331 _mesa_IsShader(GLuint name
)
1333 GET_CURRENT_CONTEXT(ctx
);
1334 return is_shader(ctx
, name
);
1339 _mesa_LinkProgramARB(GLhandleARB programObj
)
1341 GET_CURRENT_CONTEXT(ctx
);
1342 link_program(ctx
, programObj
);
1348 * Read shader source code from a file.
1349 * Useful for debugging to override an app's shader.
1352 read_shader(const char *fname
)
1354 const int max
= 50*1000;
1355 FILE *f
= fopen(fname
, "r");
1356 GLcharARB
*buffer
, *shader
;
1363 buffer
= (char *) malloc(max
);
1364 len
= fread(buffer
, 1, max
, f
);
1369 shader
= _mesa_strdup(buffer
);
1377 * Called via glShaderSource() and glShaderSourceARB() API functions.
1378 * Basically, concatenate the source code strings into one long string
1379 * and pass it to _mesa_shader_source().
1382 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1383 const GLcharARB
** string
, const GLint
* length
)
1385 GET_CURRENT_CONTEXT(ctx
);
1387 GLsizei i
, totalLength
;
1391 if (!shaderObj
|| string
== NULL
) {
1392 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1397 * This array holds offsets of where the appropriate string ends, thus the
1398 * last element will be set to the total length of the source code.
1400 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1401 if (offsets
== NULL
) {
1402 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1406 for (i
= 0; i
< count
; i
++) {
1407 if (string
[i
] == NULL
) {
1408 free((GLvoid
*) offsets
);
1409 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderSourceARB(null string)");
1412 if (length
== NULL
|| length
[i
] < 0)
1413 offsets
[i
] = strlen(string
[i
]);
1415 offsets
[i
] = length
[i
];
1416 /* accumulate string lengths */
1418 offsets
[i
] += offsets
[i
- 1];
1421 /* Total length of source string is sum off all strings plus two.
1422 * One extra byte for terminating zero, another extra byte to silence
1423 * valgrind warnings in the parser/grammer code.
1425 totalLength
= offsets
[count
- 1] + 2;
1426 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1427 if (source
== NULL
) {
1428 free((GLvoid
*) offsets
);
1429 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1433 for (i
= 0; i
< count
; i
++) {
1434 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1435 memcpy(source
+ start
, string
[i
],
1436 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1438 source
[totalLength
- 1] = '\0';
1439 source
[totalLength
- 2] = '\0';
1442 /* Compute the shader's source code checksum then try to open a file
1443 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1444 * original shader source code. For debugging.
1447 GLcharARB
*newSource
;
1449 checksum
= _mesa_str_checksum(source
);
1451 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1453 newSource
= read_shader(filename
);
1455 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1456 shaderObj
, checksum
, filename
);
1462 shader_source(ctx
, shaderObj
, source
);
1465 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1467 sh
->SourceChecksum
= checksum
; /* save original checksum */
1475 _mesa_UseProgramObjectARB(GLhandleARB program
)
1477 GET_CURRENT_CONTEXT(ctx
);
1478 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1479 _mesa_use_program(ctx
, program
);
1484 _mesa_ValidateProgramARB(GLhandleARB program
)
1486 GET_CURRENT_CONTEXT(ctx
);
1487 validate_program(ctx
, program
);
1493 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1494 GLint
* range
, GLint
* precision
)
1496 GET_CURRENT_CONTEXT(ctx
);
1497 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1502 _mesa_ReleaseShaderCompiler(void)
1504 GET_CURRENT_CONTEXT(ctx
);
1505 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1510 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1511 const void* binary
, GLint length
)
1513 GET_CURRENT_CONTEXT(ctx
);
1514 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1517 #endif /* FEATURE_ES2 */
1520 #if FEATURE_ARB_geometry_shader4
1523 _mesa_ProgramParameteriARB(GLuint program
, GLenum pname
,
1526 struct gl_shader_program
*shProg
;
1527 GET_CURRENT_CONTEXT(ctx
);
1529 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1531 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1532 "glProgramParameteri");
1537 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1539 value
> ctx
->Const
.GeometryProgram
.MaxGeometryOutputVertices
) {
1540 _mesa_error(ctx
, GL_INVALID_VALUE
,
1541 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1545 shProg
->Geom
.VerticesOut
= value
;
1547 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1551 case GL_LINES_ADJACENCY_ARB
:
1553 case GL_TRIANGLES_ADJACENCY_ARB
:
1554 shProg
->Geom
.InputType
= value
;
1557 _mesa_error(ctx
, GL_INVALID_VALUE
,
1558 "glProgramParameteri(geometry input type = %s",
1559 _mesa_lookup_enum_by_nr(value
));
1563 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1567 case GL_TRIANGLE_STRIP
:
1568 shProg
->Geom
.OutputType
= value
;
1571 _mesa_error(ctx
, GL_INVALID_VALUE
,
1572 "glProgramParameteri(geometry output type = %s",
1573 _mesa_lookup_enum_by_nr(value
));
1578 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteriARB(pname=%s)",
1579 _mesa_lookup_enum_by_nr(pname
));
1588 * Plug in shader-related functions into API dispatch table.
1591 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1594 /* GL_ARB_vertex/fragment_shader */
1595 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1596 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1597 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1598 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1599 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1600 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1601 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1602 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1603 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1604 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1605 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1606 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1607 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1608 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1609 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1610 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1613 SET_AttachShader(exec
, _mesa_AttachShader
);
1614 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1615 SET_CreateShader(exec
, _mesa_CreateShader
);
1616 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1617 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1618 SET_DetachShader(exec
, _mesa_DetachShader
);
1619 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1620 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1621 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1622 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1623 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1624 SET_IsProgram(exec
, _mesa_IsProgram
);
1625 SET_IsShader(exec
, _mesa_IsShader
);
1627 #if FEATURE_ARB_vertex_shader
1628 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1629 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1630 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);
1633 #if FEATURE_ARB_geometry_shader4
1634 SET_ProgramParameteriARB(exec
, _mesa_ProgramParameteriARB
);
1636 #endif /* FEATURE_GL */