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/hash.h"
43 #include "main/shaderapi.h"
44 #include "main/shaderobj.h"
45 #include "shader/program.h"
46 #include "shader/prog_parameter.h"
47 #include "shader/prog_uniform.h"
48 #include "slang/slang_compile.h"
49 #include "slang/slang_link.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
.Flags
= get_shader_flags();
103 /* Default pragma settings */
104 ctx
->Shader
.DefaultPragmas
.IgnoreOptimize
= GL_FALSE
;
105 ctx
->Shader
.DefaultPragmas
.IgnoreDebug
= GL_FALSE
;
106 ctx
->Shader
.DefaultPragmas
.Optimize
= GL_TRUE
;
107 ctx
->Shader
.DefaultPragmas
.Debug
= GL_FALSE
;
112 * Free the per-context shader-related state.
115 _mesa_free_shader_state(GLcontext
*ctx
)
117 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
122 * Return the size of the given GLSL datatype, in floats (components).
125 _mesa_sizeof_glsl_type(GLenum type
)
134 case GL_SAMPLER_CUBE
:
135 case GL_SAMPLER_1D_SHADOW
:
136 case GL_SAMPLER_2D_SHADOW
:
137 case GL_SAMPLER_2D_RECT_ARB
:
138 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
139 case GL_SAMPLER_1D_ARRAY_EXT
:
140 case GL_SAMPLER_2D_ARRAY_EXT
:
141 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
142 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
143 case GL_SAMPLER_CUBE_SHADOW_EXT
:
147 case GL_UNSIGNED_INT_VEC2
:
152 case GL_UNSIGNED_INT_VEC3
:
157 case GL_UNSIGNED_INT_VEC4
:
161 case GL_FLOAT_MAT2x3
:
162 case GL_FLOAT_MAT2x4
:
163 return 8; /* two float[4] vectors */
165 case GL_FLOAT_MAT3x2
:
166 case GL_FLOAT_MAT3x4
:
167 return 12; /* three float[4] vectors */
169 case GL_FLOAT_MAT4x2
:
170 case GL_FLOAT_MAT4x3
:
171 return 16; /* four float[4] vectors */
173 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
180 * Copy string from <src> to <dst>, up to maxLength characters, returning
181 * length of <dst> in <length>.
182 * \param src the strings source
183 * \param maxLength max chars to copy
184 * \param length returns number of chars copied
185 * \param dst the string destination
188 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
189 GLsizei
*length
, const GLchar
*src
)
192 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
203 * Find the length of the longest transform feedback varying name
204 * which was specified with glTransformFeedbackVaryings().
207 longest_feedback_varying_name(const struct gl_shader_program
*shProg
)
211 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
212 GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]);
222 is_program(GLcontext
*ctx
, GLuint name
)
224 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
225 return shProg
? GL_TRUE
: GL_FALSE
;
230 is_shader(GLcontext
*ctx
, GLuint name
)
232 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
233 return shader
? GL_TRUE
: GL_FALSE
;
238 * Attach shader to a shader program.
241 attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
243 struct gl_shader_program
*shProg
;
244 struct gl_shader
*sh
;
247 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
251 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
256 n
= shProg
->NumShaders
;
257 for (i
= 0; i
< n
; i
++) {
258 if (shProg
->Shaders
[i
] == sh
) {
259 /* The shader is already attched to this program. The
260 * GL_ARB_shader_objects spec says:
262 * "The error INVALID_OPERATION is generated by AttachObjectARB
263 * if <obj> is already attached to <containerObj>."
265 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
271 shProg
->Shaders
= (struct gl_shader
**)
272 _mesa_realloc(shProg
->Shaders
,
273 n
* sizeof(struct gl_shader
*),
274 (n
+ 1) * sizeof(struct gl_shader
*));
275 if (!shProg
->Shaders
) {
276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
281 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
282 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
283 shProg
->NumShaders
++;
288 get_attrib_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
290 struct gl_shader_program
*shProg
291 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
297 if (!shProg
->LinkStatus
) {
298 _mesa_error(ctx
, GL_INVALID_OPERATION
,
299 "glGetAttribLocation(program not linked)");
306 if (shProg
->VertexProgram
) {
307 const struct gl_program_parameter_list
*attribs
=
308 shProg
->VertexProgram
->Base
.Attributes
;
310 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
312 return attribs
->Parameters
[i
].StateIndexes
[0];
321 bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
324 struct gl_shader_program
*shProg
;
325 const GLint size
= -1; /* unknown size */
327 GLenum datatype
= GL_FLOAT_VEC4
;
329 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
330 "glBindAttribLocation");
338 if (strncmp(name
, "gl_", 3) == 0) {
339 _mesa_error(ctx
, GL_INVALID_OPERATION
,
340 "glBindAttribLocation(illegal name)");
344 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
345 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
349 if (shProg
->LinkStatus
) {
350 /* get current index/location for the attribute */
351 oldIndex
= get_attrib_location(ctx
, program
, name
);
357 /* this will replace the current value if it's already in the list */
358 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
360 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
365 * Note that this attribute binding won't go into effect until
366 * glLinkProgram is called again.
372 create_shader(GLcontext
*ctx
, GLenum type
)
374 struct gl_shader
*sh
;
377 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
380 case GL_FRAGMENT_SHADER
:
381 case GL_VERTEX_SHADER
:
382 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
385 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
389 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
396 create_shader_program(GLcontext
*ctx
)
399 struct gl_shader_program
*shProg
;
401 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
403 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
405 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
407 assert(shProg
->RefCount
== 1);
414 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
418 delete_shader_program(GLcontext
*ctx
, GLuint name
)
421 * NOTE: deleting shaders/programs works a bit differently than
422 * texture objects (and buffer objects, etc). Shader/program
423 * handles/IDs exist in the hash table until the object is really
424 * deleted (refcount==0). With texture objects, the handle/ID is
425 * removed from the hash table in glDeleteTextures() while the tex
426 * object itself might linger until its refcount goes to zero.
428 struct gl_shader_program
*shProg
;
430 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
434 shProg
->DeletePending
= GL_TRUE
;
436 /* effectively, decr shProg's refcount */
437 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
442 delete_shader(GLcontext
*ctx
, GLuint shader
)
444 struct gl_shader
*sh
;
446 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
450 sh
->DeletePending
= GL_TRUE
;
452 /* effectively, decr sh's refcount */
453 _mesa_reference_shader(ctx
, &sh
, NULL
);
458 detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
460 struct gl_shader_program
*shProg
;
464 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
468 n
= shProg
->NumShaders
;
470 for (i
= 0; i
< n
; i
++) {
471 if (shProg
->Shaders
[i
]->Name
== shader
) {
473 struct gl_shader
**newList
;
476 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
478 /* alloc new, smaller array */
479 newList
= (struct gl_shader
**)
480 malloc((n
- 1) * sizeof(struct gl_shader
*));
482 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
485 for (j
= 0; j
< i
; j
++) {
486 newList
[j
] = shProg
->Shaders
[j
];
489 newList
[j
++] = shProg
->Shaders
[i
];
490 free(shProg
->Shaders
);
492 shProg
->Shaders
= newList
;
493 shProg
->NumShaders
= n
- 1;
498 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
499 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
500 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
501 assert(shProg
->Shaders
[j
]->RefCount
> 0);
513 if (is_shader(ctx
, shader
))
514 err
= GL_INVALID_OPERATION
;
515 else if (is_program(ctx
, shader
))
516 err
= GL_INVALID_OPERATION
;
518 err
= GL_INVALID_VALUE
;
519 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
526 get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
527 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
528 GLenum
*type
, GLchar
*nameOut
)
530 const struct gl_program_parameter_list
*attribs
= NULL
;
531 struct gl_shader_program
*shProg
;
533 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
537 if (shProg
->VertexProgram
)
538 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
540 if (!attribs
|| index
>= attribs
->NumParameters
) {
541 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
545 _mesa_copy_string(nameOut
, maxLength
, length
,
546 attribs
->Parameters
[index
].Name
);
549 *size
= attribs
->Parameters
[index
].Size
550 / _mesa_sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
553 *type
= attribs
->Parameters
[index
].DataType
;
558 * Return list of shaders attached to shader program.
561 get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
562 GLsizei
*count
, GLuint
*obj
)
564 struct gl_shader_program
*shProg
=
565 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
568 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
569 obj
[i
] = shProg
->Shaders
[i
]->Name
;
578 * glGetHandleARB() - return ID/name of currently bound shader program.
581 get_handle(GLcontext
*ctx
, GLenum pname
)
583 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
584 if (ctx
->Shader
.CurrentProgram
)
585 return ctx
->Shader
.CurrentProgram
->Name
;
590 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
597 * glGetProgramiv() - get shader program state.
598 * Note that this is for GLSL shader programs, not ARB vertex/fragment
599 * programs (see glGetProgramivARB).
602 get_programiv(GLcontext
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
604 const struct gl_program_parameter_list
*attribs
;
605 struct gl_shader_program
*shProg
606 = _mesa_lookup_shader_program(ctx
, program
);
609 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
613 if (shProg
->VertexProgram
)
614 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
619 case GL_DELETE_STATUS
:
620 *params
= shProg
->DeletePending
;
623 *params
= shProg
->LinkStatus
;
625 case GL_VALIDATE_STATUS
:
626 *params
= shProg
->Validated
;
628 case GL_INFO_LOG_LENGTH
:
629 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
631 case GL_ATTACHED_SHADERS
:
632 *params
= shProg
->NumShaders
;
634 case GL_ACTIVE_ATTRIBUTES
:
635 *params
= attribs
? attribs
->NumParameters
: 0;
637 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
638 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
640 case GL_ACTIVE_UNIFORMS
:
641 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
643 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
644 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
646 (*params
)++; /* add one for terminating zero */
648 case GL_PROGRAM_BINARY_LENGTH_OES
:
651 #if FEATURE_EXT_transform_feedback
652 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
653 *params
= shProg
->TransformFeedback
.NumVarying
;
655 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
:
656 *params
= longest_feedback_varying_name(shProg
) + 1;
658 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
659 *params
= shProg
->TransformFeedback
.BufferMode
;
663 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
670 * glGetShaderiv() - get GLSL shader state
673 get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
675 struct gl_shader
*shader
=
676 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
684 *params
= shader
->Type
;
686 case GL_DELETE_STATUS
:
687 *params
= shader
->DeletePending
;
689 case GL_COMPILE_STATUS
:
690 *params
= shader
->CompileStatus
;
692 case GL_INFO_LOG_LENGTH
:
693 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
695 case GL_SHADER_SOURCE_LENGTH
:
696 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
699 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
706 get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
707 GLsizei
*length
, GLchar
*infoLog
)
709 struct gl_shader_program
*shProg
710 = _mesa_lookup_shader_program(ctx
, program
);
712 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
715 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
720 get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
721 GLsizei
*length
, GLchar
*infoLog
)
723 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
725 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
728 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
733 * Return shader source code.
736 get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
737 GLsizei
*length
, GLchar
*sourceOut
)
739 struct gl_shader
*sh
;
740 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
744 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
749 * Set/replace shader source code.
752 shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
754 struct gl_shader
*sh
;
756 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
760 /* free old shader source string and install new one */
762 free((void *) sh
->Source
);
765 sh
->CompileStatus
= GL_FALSE
;
767 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
776 compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
778 struct gl_shader
*sh
;
780 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
784 /* set default pragma state for shader */
785 sh
->Pragmas
= ctx
->Shader
.DefaultPragmas
;
787 /* this call will set the sh->CompileStatus field to indicate if
788 * compilation was successful.
790 (void) _slang_compile(ctx
, sh
);
795 * Link a program's shaders.
798 link_program(GLcontext
*ctx
, GLuint program
)
800 struct gl_shader_program
*shProg
;
801 struct gl_transform_feedback_object
*obj
=
802 ctx
->TransformFeedback
.CurrentObject
;
804 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
808 if (obj
->Active
&& shProg
== ctx
->Shader
.CurrentProgram
) {
809 _mesa_error(ctx
, GL_INVALID_OPERATION
,
810 "glLinkProgram(transform feedback active");
814 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
816 _slang_link(ctx
, program
, shProg
);
822 printf("Link %u shaders in program %u: %s\n",
823 shProg
->NumShaders
, shProg
->Name
,
824 shProg
->LinkStatus
? "Success" : "Failed");
826 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
827 printf(" shader %u, type 0x%x\n",
828 shProg
->Shaders
[i
]->Name
,
829 shProg
->Shaders
[i
]->Type
);
836 * Print basic shader info (for debug).
839 print_shader_info(const struct gl_shader_program
*shProg
)
843 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
844 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
846 switch (shProg
->Shaders
[i
]->Type
) {
847 case GL_VERTEX_SHADER
:
850 case GL_FRAGMENT_SHADER
:
853 case GL_GEOMETRY_SHADER
:
859 printf(" %s shader %u, checksum %u\n", s
,
860 shProg
->Shaders
[i
]->Name
,
861 shProg
->Shaders
[i
]->SourceChecksum
);
863 if (shProg
->VertexProgram
)
864 printf(" vert prog %u\n", shProg
->VertexProgram
->Base
.Id
);
865 if (shProg
->FragmentProgram
)
866 printf(" frag prog %u\n", shProg
->FragmentProgram
->Base
.Id
);
871 * Use the named shader program for subsequent rendering.
874 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
876 struct gl_shader_program
*shProg
;
877 struct gl_transform_feedback_object
*obj
=
878 ctx
->TransformFeedback
.CurrentObject
;
881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
882 "glUseProgram(transform feedback active)");
886 if (ctx
->Shader
.CurrentProgram
&&
887 ctx
->Shader
.CurrentProgram
->Name
== program
) {
893 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
897 if (!shProg
->LinkStatus
) {
898 _mesa_error(ctx
, GL_INVALID_OPERATION
,
899 "glUseProgram(program %u not linked)", program
);
904 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
905 print_shader_info(shProg
);
912 if (ctx
->Shader
.CurrentProgram
!= shProg
) {
913 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
914 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
917 if (ctx
->Driver
.UseProgram
)
918 ctx
->Driver
.UseProgram(ctx
, shProg
);
923 * Validate a program's samplers.
924 * Specifically, check that there aren't two samplers of different types
925 * pointing to the same texture unit.
926 * \return GL_TRUE if valid, GL_FALSE if invalid
929 validate_samplers(GLcontext
*ctx
, const struct gl_program
*prog
, char *errMsg
)
931 static const char *targetName
[] = {
940 GLint targetUsed
[MAX_TEXTURE_IMAGE_UNITS
];
941 GLbitfield samplersUsed
= prog
->SamplersUsed
;
944 assert(Elements(targetName
) == NUM_TEXTURE_TARGETS
);
946 if (samplersUsed
== 0x0)
949 for (i
= 0; i
< Elements(targetUsed
); i
++)
952 /* walk over bits which are set in 'samplers' */
953 while (samplersUsed
) {
955 gl_texture_index target
;
956 GLint sampler
= _mesa_ffs(samplersUsed
) - 1;
957 assert(sampler
>= 0);
958 assert(sampler
< MAX_TEXTURE_IMAGE_UNITS
);
959 unit
= prog
->SamplerUnits
[sampler
];
960 target
= prog
->SamplerTargets
[sampler
];
961 if (targetUsed
[unit
] != -1 && targetUsed
[unit
] != target
) {
962 _mesa_snprintf(errMsg
, 100,
963 "Texture unit %d is accessed both as %s and %s",
964 unit
, targetName
[targetUsed
[unit
]], targetName
[target
]);
967 targetUsed
[unit
] = target
;
968 samplersUsed
^= (1 << sampler
);
976 * Do validation of the given shader program.
977 * \param errMsg returns error message if validation fails.
978 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
981 validate_shader_program(GLcontext
*ctx
,
982 const struct gl_shader_program
*shProg
,
985 const struct gl_vertex_program
*vp
= shProg
->VertexProgram
;
986 const struct gl_fragment_program
*fp
= shProg
->FragmentProgram
;
988 if (!shProg
->LinkStatus
) {
992 /* From the GL spec, a program is invalid if any of these are true:
994 any two active samplers in the current program object are of
995 different types, but refer to the same texture image unit,
997 any active sampler in the current program object refers to a texture
998 image unit where fixed-function fragment processing accesses a
999 texture target that does not match the sampler type, or
1001 the sum of the number of active samplers in the program and the
1002 number of texture image units enabled for fixed-function fragment
1003 processing exceeds the combined limit on the total number of texture
1004 image units allowed.
1009 * Check: any two active samplers in the current program object are of
1010 * different types, but refer to the same texture image unit,
1012 if (vp
&& !validate_samplers(ctx
, &vp
->Base
, errMsg
)) {
1015 if (fp
&& !validate_samplers(ctx
, &fp
->Base
, errMsg
)) {
1024 * Called via glValidateProgram()
1027 validate_program(GLcontext
*ctx
, GLuint program
)
1029 struct gl_shader_program
*shProg
;
1032 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1037 shProg
->Validated
= validate_shader_program(ctx
, shProg
, errMsg
);
1038 if (!shProg
->Validated
) {
1039 /* update info log */
1040 if (shProg
->InfoLog
) {
1041 free(shProg
->InfoLog
);
1043 shProg
->InfoLog
= _mesa_strdup(errMsg
);
1050 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1052 GET_CURRENT_CONTEXT(ctx
);
1053 attach_shader(ctx
, program
, shader
);
1058 _mesa_AttachShader(GLuint program
, GLuint shader
)
1060 GET_CURRENT_CONTEXT(ctx
);
1061 attach_shader(ctx
, program
, shader
);
1066 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
1067 const GLcharARB
*name
)
1069 GET_CURRENT_CONTEXT(ctx
);
1070 bind_attrib_location(ctx
, program
, index
, name
);
1075 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
1077 GET_CURRENT_CONTEXT(ctx
);
1078 compile_shader(ctx
, shaderObj
);
1083 _mesa_CreateShader(GLenum type
)
1085 GET_CURRENT_CONTEXT(ctx
);
1086 return create_shader(ctx
, type
);
1090 GLhandleARB GLAPIENTRY
1091 _mesa_CreateShaderObjectARB(GLenum type
)
1093 GET_CURRENT_CONTEXT(ctx
);
1094 return create_shader(ctx
, type
);
1099 _mesa_CreateProgram(void)
1101 GET_CURRENT_CONTEXT(ctx
);
1102 return create_shader_program(ctx
);
1106 GLhandleARB GLAPIENTRY
1107 _mesa_CreateProgramObjectARB(void)
1109 GET_CURRENT_CONTEXT(ctx
);
1110 return create_shader_program(ctx
);
1115 _mesa_DeleteObjectARB(GLhandleARB obj
)
1118 GET_CURRENT_CONTEXT(ctx
);
1119 if (is_program(ctx
, obj
)) {
1120 delete_shader_program(ctx
, obj
);
1122 else if (is_shader(ctx
, obj
)) {
1123 delete_shader(ctx
, obj
);
1133 _mesa_DeleteProgram(GLuint name
)
1136 GET_CURRENT_CONTEXT(ctx
);
1137 delete_shader_program(ctx
, name
);
1143 _mesa_DeleteShader(GLuint name
)
1146 GET_CURRENT_CONTEXT(ctx
);
1147 delete_shader(ctx
, name
);
1153 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1155 GET_CURRENT_CONTEXT(ctx
);
1156 detach_shader(ctx
, program
, shader
);
1161 _mesa_DetachShader(GLuint program
, GLuint shader
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 detach_shader(ctx
, program
, shader
);
1169 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint index
,
1170 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1171 GLenum
* type
, GLcharARB
* name
)
1173 GET_CURRENT_CONTEXT(ctx
);
1174 get_active_attrib(ctx
, program
, index
, maxLength
, length
, size
, type
, name
);
1179 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1180 GLsizei
* count
, GLhandleARB
* obj
)
1182 GET_CURRENT_CONTEXT(ctx
);
1183 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1188 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1189 GLsizei
*count
, GLuint
*obj
)
1191 GET_CURRENT_CONTEXT(ctx
);
1192 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1197 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
1199 GET_CURRENT_CONTEXT(ctx
);
1200 return get_attrib_location(ctx
, program
, name
);
1205 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1206 GLcharARB
* infoLog
)
1208 GET_CURRENT_CONTEXT(ctx
);
1209 if (is_program(ctx
, object
)) {
1210 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1212 else if (is_shader(ctx
, object
)) {
1213 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1216 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1222 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1224 GET_CURRENT_CONTEXT(ctx
);
1225 /* Implement in terms of GetProgramiv, GetShaderiv */
1226 if (is_program(ctx
, object
)) {
1227 if (pname
== GL_OBJECT_TYPE_ARB
) {
1228 *params
= GL_PROGRAM_OBJECT_ARB
;
1231 get_programiv(ctx
, object
, pname
, params
);
1234 else if (is_shader(ctx
, object
)) {
1235 if (pname
== GL_OBJECT_TYPE_ARB
) {
1236 *params
= GL_SHADER_OBJECT_ARB
;
1239 get_shaderiv(ctx
, object
, pname
, params
);
1243 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1249 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1252 GLint iparams
[1]; /* XXX is one element enough? */
1253 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1254 params
[0] = (GLfloat
) iparams
[0];
1259 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1261 GET_CURRENT_CONTEXT(ctx
);
1262 get_programiv(ctx
, program
, pname
, params
);
1267 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1269 GET_CURRENT_CONTEXT(ctx
);
1270 get_shaderiv(ctx
, shader
, pname
, params
);
1275 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1276 GLsizei
*length
, GLchar
*infoLog
)
1278 GET_CURRENT_CONTEXT(ctx
);
1279 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1284 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1285 GLsizei
*length
, GLchar
*infoLog
)
1287 GET_CURRENT_CONTEXT(ctx
);
1288 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1293 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1294 GLsizei
*length
, GLcharARB
*sourceOut
)
1296 GET_CURRENT_CONTEXT(ctx
);
1297 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1301 GLhandleARB GLAPIENTRY
1302 _mesa_GetHandleARB(GLenum pname
)
1304 GET_CURRENT_CONTEXT(ctx
);
1305 return get_handle(ctx
, pname
);
1309 GLboolean GLAPIENTRY
1310 _mesa_IsProgram(GLuint name
)
1312 GET_CURRENT_CONTEXT(ctx
);
1313 return is_program(ctx
, name
);
1317 GLboolean GLAPIENTRY
1318 _mesa_IsShader(GLuint name
)
1320 GET_CURRENT_CONTEXT(ctx
);
1321 return is_shader(ctx
, name
);
1326 _mesa_LinkProgramARB(GLhandleARB programObj
)
1328 GET_CURRENT_CONTEXT(ctx
);
1329 link_program(ctx
, programObj
);
1335 * Read shader source code from a file.
1336 * Useful for debugging to override an app's shader.
1339 read_shader(const char *fname
)
1341 const int max
= 50*1000;
1342 FILE *f
= fopen(fname
, "r");
1343 GLcharARB
*buffer
, *shader
;
1350 buffer
= (char *) malloc(max
);
1351 len
= fread(buffer
, 1, max
, f
);
1356 shader
= _mesa_strdup(buffer
);
1364 * Called via glShaderSource() and glShaderSourceARB() API functions.
1365 * Basically, concatenate the source code strings into one long string
1366 * and pass it to _mesa_shader_source().
1369 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1370 const GLcharARB
** string
, const GLint
* length
)
1372 GET_CURRENT_CONTEXT(ctx
);
1374 GLsizei i
, totalLength
;
1378 if (!shaderObj
|| string
== NULL
) {
1379 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1384 * This array holds offsets of where the appropriate string ends, thus the
1385 * last element will be set to the total length of the source code.
1387 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1388 if (offsets
== NULL
) {
1389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1393 for (i
= 0; i
< count
; i
++) {
1394 if (string
[i
] == NULL
) {
1395 free((GLvoid
*) offsets
);
1396 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderSourceARB(null string)");
1399 if (length
== NULL
|| length
[i
] < 0)
1400 offsets
[i
] = strlen(string
[i
]);
1402 offsets
[i
] = length
[i
];
1403 /* accumulate string lengths */
1405 offsets
[i
] += offsets
[i
- 1];
1408 /* Total length of source string is sum off all strings plus two.
1409 * One extra byte for terminating zero, another extra byte to silence
1410 * valgrind warnings in the parser/grammer code.
1412 totalLength
= offsets
[count
- 1] + 2;
1413 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1414 if (source
== NULL
) {
1415 free((GLvoid
*) offsets
);
1416 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1420 for (i
= 0; i
< count
; i
++) {
1421 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1422 memcpy(source
+ start
, string
[i
],
1423 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1425 source
[totalLength
- 1] = '\0';
1426 source
[totalLength
- 2] = '\0';
1429 /* Compute the shader's source code checksum then try to open a file
1430 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1431 * original shader source code. For debugging.
1434 GLcharARB
*newSource
;
1436 checksum
= _mesa_str_checksum(source
);
1438 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1440 newSource
= read_shader(filename
);
1442 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1443 shaderObj
, checksum
, filename
);
1449 shader_source(ctx
, shaderObj
, source
);
1452 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1454 sh
->SourceChecksum
= checksum
; /* save original checksum */
1462 _mesa_UseProgramObjectARB(GLhandleARB program
)
1464 GET_CURRENT_CONTEXT(ctx
);
1465 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1466 _mesa_use_program(ctx
, program
);
1471 _mesa_ValidateProgramARB(GLhandleARB program
)
1473 GET_CURRENT_CONTEXT(ctx
);
1474 validate_program(ctx
, program
);
1480 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1481 GLint
* range
, GLint
* precision
)
1483 GET_CURRENT_CONTEXT(ctx
);
1484 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1489 _mesa_ReleaseShaderCompiler(void)
1491 GET_CURRENT_CONTEXT(ctx
);
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1497 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1498 const void* binary
, GLint length
)
1500 GET_CURRENT_CONTEXT(ctx
);
1501 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1504 #endif /* FEATURE_ES2 */
1508 * Plug in shader-related functions into API dispatch table.
1511 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1513 /* GL_ARB_vertex/fragment_shader */
1514 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1515 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1516 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1517 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1518 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1519 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1520 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1521 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1522 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1523 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1524 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1525 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1526 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1527 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1528 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1529 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1532 SET_AttachShader(exec
, _mesa_AttachShader
);
1533 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1534 SET_CreateShader(exec
, _mesa_CreateShader
);
1535 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1536 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1537 SET_DetachShader(exec
, _mesa_DetachShader
);
1538 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1539 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1540 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1541 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1542 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1543 SET_IsProgram(exec
, _mesa_IsProgram
);
1544 SET_IsShader(exec
, _mesa_IsShader
);
1546 #if FEATURE_ARB_vertex_shader
1547 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1548 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1549 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);