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"
49 #include "slang/slang_compile.h"
50 #include "slang/slang_link.h"
54 /** Define this to enable shader substitution (see below) */
55 #define SHADER_SUBST 0
59 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
62 get_shader_flags(void)
64 GLbitfield flags
= 0x0;
65 const char *env
= _mesa_getenv("MESA_GLSL");
68 if (strstr(env
, "dump"))
70 if (strstr(env
, "log"))
72 if (strstr(env
, "nopvert"))
73 flags
|= GLSL_NOP_VERT
;
74 if (strstr(env
, "nopfrag"))
75 flags
|= GLSL_NOP_FRAG
;
76 if (strstr(env
, "nopt"))
78 else if (strstr(env
, "opt"))
80 if (strstr(env
, "uniform"))
81 flags
|= GLSL_UNIFORMS
;
82 if (strstr(env
, "useprog"))
83 flags
|= GLSL_USE_PROG
;
91 * Initialize context's shader state.
94 _mesa_init_shader_state(GLcontext
*ctx
)
96 /* Device drivers may override these to control what kind of instructions
97 * are generated by the GLSL compiler.
99 ctx
->Shader
.EmitHighLevelInstructions
= GL_TRUE
;
100 ctx
->Shader
.EmitContReturn
= GL_TRUE
;
101 ctx
->Shader
.EmitCondCodes
= GL_FALSE
;
102 ctx
->Shader
.EmitComments
= GL_FALSE
;
103 ctx
->Shader
.EmitNoIfs
= GL_FALSE
;
104 ctx
->Shader
.Flags
= get_shader_flags();
106 /* Default pragma settings */
107 ctx
->Shader
.DefaultPragmas
.IgnoreOptimize
= GL_FALSE
;
108 ctx
->Shader
.DefaultPragmas
.IgnoreDebug
= GL_FALSE
;
109 ctx
->Shader
.DefaultPragmas
.Optimize
= GL_TRUE
;
110 ctx
->Shader
.DefaultPragmas
.Debug
= GL_FALSE
;
115 * Free the per-context shader-related state.
118 _mesa_free_shader_state(GLcontext
*ctx
)
120 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
125 * Return the size of the given GLSL datatype, in floats (components).
128 _mesa_sizeof_glsl_type(GLenum type
)
137 case GL_SAMPLER_CUBE
:
138 case GL_SAMPLER_1D_SHADOW
:
139 case GL_SAMPLER_2D_SHADOW
:
140 case GL_SAMPLER_2D_RECT_ARB
:
141 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
142 case GL_SAMPLER_1D_ARRAY_EXT
:
143 case GL_SAMPLER_2D_ARRAY_EXT
:
144 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
145 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
146 case GL_SAMPLER_CUBE_SHADOW_EXT
:
150 case GL_UNSIGNED_INT_VEC2
:
155 case GL_UNSIGNED_INT_VEC3
:
160 case GL_UNSIGNED_INT_VEC4
:
164 case GL_FLOAT_MAT2x3
:
165 case GL_FLOAT_MAT2x4
:
166 return 8; /* two float[4] vectors */
168 case GL_FLOAT_MAT3x2
:
169 case GL_FLOAT_MAT3x4
:
170 return 12; /* three float[4] vectors */
172 case GL_FLOAT_MAT4x2
:
173 case GL_FLOAT_MAT4x3
:
174 return 16; /* four float[4] vectors */
176 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
183 * Copy string from <src> to <dst>, up to maxLength characters, returning
184 * length of <dst> in <length>.
185 * \param src the strings source
186 * \param maxLength max chars to copy
187 * \param length returns number of chars copied
188 * \param dst the string destination
191 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
192 GLsizei
*length
, const GLchar
*src
)
195 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
206 * Find the length of the longest transform feedback varying name
207 * which was specified with glTransformFeedbackVaryings().
210 longest_feedback_varying_name(const struct gl_shader_program
*shProg
)
214 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
215 GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]);
225 is_program(GLcontext
*ctx
, GLuint name
)
227 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
228 return shProg
? GL_TRUE
: GL_FALSE
;
233 is_shader(GLcontext
*ctx
, GLuint name
)
235 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
236 return shader
? GL_TRUE
: GL_FALSE
;
241 * Attach shader to a shader program.
244 attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
246 struct gl_shader_program
*shProg
;
247 struct gl_shader
*sh
;
250 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
254 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
259 n
= shProg
->NumShaders
;
260 for (i
= 0; i
< n
; i
++) {
261 if (shProg
->Shaders
[i
] == sh
) {
262 /* The shader is already attched to this program. The
263 * GL_ARB_shader_objects spec says:
265 * "The error INVALID_OPERATION is generated by AttachObjectARB
266 * if <obj> is already attached to <containerObj>."
268 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
274 shProg
->Shaders
= (struct gl_shader
**)
275 _mesa_realloc(shProg
->Shaders
,
276 n
* sizeof(struct gl_shader
*),
277 (n
+ 1) * sizeof(struct gl_shader
*));
278 if (!shProg
->Shaders
) {
279 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
284 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
285 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
286 shProg
->NumShaders
++;
291 get_attrib_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
293 struct gl_shader_program
*shProg
294 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
300 if (!shProg
->LinkStatus
) {
301 _mesa_error(ctx
, GL_INVALID_OPERATION
,
302 "glGetAttribLocation(program not linked)");
309 if (shProg
->VertexProgram
) {
310 const struct gl_program_parameter_list
*attribs
=
311 shProg
->VertexProgram
->Base
.Attributes
;
313 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
315 return attribs
->Parameters
[i
].StateIndexes
[0];
324 bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
327 struct gl_shader_program
*shProg
;
328 const GLint size
= -1; /* unknown size */
330 GLenum datatype
= GL_FLOAT_VEC4
;
332 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
333 "glBindAttribLocation");
341 if (strncmp(name
, "gl_", 3) == 0) {
342 _mesa_error(ctx
, GL_INVALID_OPERATION
,
343 "glBindAttribLocation(illegal name)");
347 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
348 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
352 if (shProg
->LinkStatus
) {
353 /* get current index/location for the attribute */
354 oldIndex
= get_attrib_location(ctx
, program
, name
);
360 /* this will replace the current value if it's already in the list */
361 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
368 * Note that this attribute binding won't go into effect until
369 * glLinkProgram is called again.
375 create_shader(GLcontext
*ctx
, GLenum type
)
377 struct gl_shader
*sh
;
380 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
383 case GL_FRAGMENT_SHADER
:
384 case GL_VERTEX_SHADER
:
385 case GL_GEOMETRY_SHADER_ARB
:
386 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
389 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
393 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
400 create_shader_program(GLcontext
*ctx
)
403 struct gl_shader_program
*shProg
;
405 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
407 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
409 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
411 assert(shProg
->RefCount
== 1);
418 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
422 delete_shader_program(GLcontext
*ctx
, GLuint name
)
425 * NOTE: deleting shaders/programs works a bit differently than
426 * texture objects (and buffer objects, etc). Shader/program
427 * handles/IDs exist in the hash table until the object is really
428 * deleted (refcount==0). With texture objects, the handle/ID is
429 * removed from the hash table in glDeleteTextures() while the tex
430 * object itself might linger until its refcount goes to zero.
432 struct gl_shader_program
*shProg
;
434 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
438 shProg
->DeletePending
= GL_TRUE
;
440 /* effectively, decr shProg's refcount */
441 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
446 delete_shader(GLcontext
*ctx
, GLuint shader
)
448 struct gl_shader
*sh
;
450 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
454 sh
->DeletePending
= GL_TRUE
;
456 /* effectively, decr sh's refcount */
457 _mesa_reference_shader(ctx
, &sh
, NULL
);
462 detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
464 struct gl_shader_program
*shProg
;
468 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
472 n
= shProg
->NumShaders
;
474 for (i
= 0; i
< n
; i
++) {
475 if (shProg
->Shaders
[i
]->Name
== shader
) {
477 struct gl_shader
**newList
;
480 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
482 /* alloc new, smaller array */
483 newList
= (struct gl_shader
**)
484 malloc((n
- 1) * sizeof(struct gl_shader
*));
486 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
489 for (j
= 0; j
< i
; j
++) {
490 newList
[j
] = shProg
->Shaders
[j
];
493 newList
[j
++] = shProg
->Shaders
[i
];
494 free(shProg
->Shaders
);
496 shProg
->Shaders
= newList
;
497 shProg
->NumShaders
= n
- 1;
502 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
503 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
504 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
505 assert(shProg
->Shaders
[j
]->RefCount
> 0);
517 if (is_shader(ctx
, shader
))
518 err
= GL_INVALID_OPERATION
;
519 else if (is_program(ctx
, shader
))
520 err
= GL_INVALID_OPERATION
;
522 err
= GL_INVALID_VALUE
;
523 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
530 get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
531 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
532 GLenum
*type
, GLchar
*nameOut
)
534 const struct gl_program_parameter_list
*attribs
= NULL
;
535 struct gl_shader_program
*shProg
;
537 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
541 if (shProg
->VertexProgram
)
542 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
544 if (!attribs
|| index
>= attribs
->NumParameters
) {
545 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
549 _mesa_copy_string(nameOut
, maxLength
, length
,
550 attribs
->Parameters
[index
].Name
);
553 *size
= attribs
->Parameters
[index
].Size
554 / _mesa_sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
557 *type
= attribs
->Parameters
[index
].DataType
;
562 * Return list of shaders attached to shader program.
565 get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
566 GLsizei
*count
, GLuint
*obj
)
568 struct gl_shader_program
*shProg
=
569 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
572 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
573 obj
[i
] = shProg
->Shaders
[i
]->Name
;
582 * glGetHandleARB() - return ID/name of currently bound shader program.
585 get_handle(GLcontext
*ctx
, GLenum pname
)
587 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
588 if (ctx
->Shader
.CurrentProgram
)
589 return ctx
->Shader
.CurrentProgram
->Name
;
594 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
601 * glGetProgramiv() - get shader program state.
602 * Note that this is for GLSL shader programs, not ARB vertex/fragment
603 * programs (see glGetProgramivARB).
606 get_programiv(GLcontext
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
608 const struct gl_program_parameter_list
*attribs
;
609 struct gl_shader_program
*shProg
610 = _mesa_lookup_shader_program(ctx
, program
);
613 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
617 if (shProg
->VertexProgram
)
618 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
623 case GL_DELETE_STATUS
:
624 *params
= shProg
->DeletePending
;
627 *params
= shProg
->LinkStatus
;
629 case GL_VALIDATE_STATUS
:
630 *params
= shProg
->Validated
;
632 case GL_INFO_LOG_LENGTH
:
633 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
635 case GL_ATTACHED_SHADERS
:
636 *params
= shProg
->NumShaders
;
638 case GL_ACTIVE_ATTRIBUTES
:
639 *params
= attribs
? attribs
->NumParameters
: 0;
641 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
642 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
644 case GL_ACTIVE_UNIFORMS
:
645 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
647 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
648 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
650 (*params
)++; /* add one for terminating zero */
652 case GL_PROGRAM_BINARY_LENGTH_OES
:
655 #if FEATURE_EXT_transform_feedback
656 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
657 *params
= shProg
->TransformFeedback
.NumVarying
;
659 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
:
660 *params
= longest_feedback_varying_name(shProg
) + 1;
662 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
663 *params
= shProg
->TransformFeedback
.BufferMode
;
666 #if FEATURE_ARB_geometry_shader4
667 case GL_GEOMETRY_VERTICES_OUT_ARB
:
668 *params
= shProg
->Geom
.VerticesOut
;
670 case GL_GEOMETRY_INPUT_TYPE_ARB
:
671 *params
= shProg
->Geom
.InputType
;
673 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
674 *params
= shProg
->Geom
.OutputType
;
678 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
685 * glGetShaderiv() - get GLSL shader state
688 get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
690 struct gl_shader
*shader
=
691 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
699 *params
= shader
->Type
;
701 case GL_DELETE_STATUS
:
702 *params
= shader
->DeletePending
;
704 case GL_COMPILE_STATUS
:
705 *params
= shader
->CompileStatus
;
707 case GL_INFO_LOG_LENGTH
:
708 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
710 case GL_SHADER_SOURCE_LENGTH
:
711 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
714 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
721 get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
722 GLsizei
*length
, GLchar
*infoLog
)
724 struct gl_shader_program
*shProg
725 = _mesa_lookup_shader_program(ctx
, program
);
727 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
730 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
735 get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
736 GLsizei
*length
, GLchar
*infoLog
)
738 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
740 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
743 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
748 * Return shader source code.
751 get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
752 GLsizei
*length
, GLchar
*sourceOut
)
754 struct gl_shader
*sh
;
755 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
759 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
764 * Set/replace shader source code.
767 shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
769 struct gl_shader
*sh
;
771 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
775 /* free old shader source string and install new one */
777 free((void *) sh
->Source
);
780 sh
->CompileStatus
= GL_FALSE
;
782 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
791 compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
793 struct gl_shader
*sh
;
795 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
799 /* set default pragma state for shader */
800 sh
->Pragmas
= ctx
->Shader
.DefaultPragmas
;
802 /* this call will set the sh->CompileStatus field to indicate if
803 * compilation was successful.
805 _mesa_glsl_compile_shader(ctx
, sh
);
810 * Link a program's shaders.
813 link_program(GLcontext
*ctx
, GLuint program
)
815 struct gl_shader_program
*shProg
;
816 struct gl_transform_feedback_object
*obj
=
817 ctx
->TransformFeedback
.CurrentObject
;
819 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
823 if (obj
->Active
&& shProg
== ctx
->Shader
.CurrentProgram
) {
824 _mesa_error(ctx
, GL_INVALID_OPERATION
,
825 "glLinkProgram(transform feedback active");
829 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
831 _mesa_glsl_link_shader(ctx
, shProg
);
837 printf("Link %u shaders in program %u: %s\n",
838 shProg
->NumShaders
, shProg
->Name
,
839 shProg
->LinkStatus
? "Success" : "Failed");
841 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
842 printf(" shader %u, type 0x%x\n",
843 shProg
->Shaders
[i
]->Name
,
844 shProg
->Shaders
[i
]->Type
);
851 * Print basic shader info (for debug).
854 print_shader_info(const struct gl_shader_program
*shProg
)
858 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
859 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
861 switch (shProg
->Shaders
[i
]->Type
) {
862 case GL_VERTEX_SHADER
:
865 case GL_FRAGMENT_SHADER
:
868 case GL_GEOMETRY_SHADER
:
874 printf(" %s shader %u, checksum %u\n", s
,
875 shProg
->Shaders
[i
]->Name
,
876 shProg
->Shaders
[i
]->SourceChecksum
);
878 if (shProg
->VertexProgram
)
879 printf(" vert prog %u\n", shProg
->VertexProgram
->Base
.Id
);
880 if (shProg
->FragmentProgram
)
881 printf(" frag prog %u\n", shProg
->FragmentProgram
->Base
.Id
);
886 * Use the named shader program for subsequent rendering.
889 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
891 struct gl_shader_program
*shProg
;
892 struct gl_transform_feedback_object
*obj
=
893 ctx
->TransformFeedback
.CurrentObject
;
896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
897 "glUseProgram(transform feedback active)");
901 if (ctx
->Shader
.CurrentProgram
&&
902 ctx
->Shader
.CurrentProgram
->Name
== program
) {
908 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
912 if (!shProg
->LinkStatus
) {
913 _mesa_error(ctx
, GL_INVALID_OPERATION
,
914 "glUseProgram(program %u not linked)", program
);
919 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
920 print_shader_info(shProg
);
927 if (ctx
->Shader
.CurrentProgram
!= shProg
) {
928 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
929 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
932 if (ctx
->Driver
.UseProgram
)
933 ctx
->Driver
.UseProgram(ctx
, shProg
);
938 * Validate a program's samplers.
939 * Specifically, check that there aren't two samplers of different types
940 * pointing to the same texture unit.
941 * \return GL_TRUE if valid, GL_FALSE if invalid
944 validate_samplers(GLcontext
*ctx
, const struct gl_program
*prog
, char *errMsg
)
946 static const char *targetName
[] = {
955 GLint targetUsed
[MAX_TEXTURE_IMAGE_UNITS
];
956 GLbitfield samplersUsed
= prog
->SamplersUsed
;
959 assert(Elements(targetName
) == NUM_TEXTURE_TARGETS
);
961 if (samplersUsed
== 0x0)
964 for (i
= 0; i
< Elements(targetUsed
); i
++)
967 /* walk over bits which are set in 'samplers' */
968 while (samplersUsed
) {
970 gl_texture_index target
;
971 GLint sampler
= _mesa_ffs(samplersUsed
) - 1;
972 assert(sampler
>= 0);
973 assert(sampler
< MAX_TEXTURE_IMAGE_UNITS
);
974 unit
= prog
->SamplerUnits
[sampler
];
975 target
= prog
->SamplerTargets
[sampler
];
976 if (targetUsed
[unit
] != -1 && targetUsed
[unit
] != target
) {
977 _mesa_snprintf(errMsg
, 100,
978 "Texture unit %d is accessed both as %s and %s",
979 unit
, targetName
[targetUsed
[unit
]], targetName
[target
]);
982 targetUsed
[unit
] = target
;
983 samplersUsed
^= (1 << sampler
);
991 * Do validation of the given shader program.
992 * \param errMsg returns error message if validation fails.
993 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
996 validate_shader_program(GLcontext
*ctx
,
997 const struct gl_shader_program
*shProg
,
1000 const struct gl_vertex_program
*vp
= shProg
->VertexProgram
;
1001 const struct gl_fragment_program
*fp
= shProg
->FragmentProgram
;
1003 if (!shProg
->LinkStatus
) {
1007 /* From the GL spec, a program is invalid if any of these are true:
1009 any two active samplers in the current program object are of
1010 different types, but refer to the same texture image unit,
1012 any active sampler in the current program object refers to a texture
1013 image unit where fixed-function fragment processing accesses a
1014 texture target that does not match the sampler type, or
1016 the sum of the number of active samplers in the program and the
1017 number of texture image units enabled for fixed-function fragment
1018 processing exceeds the combined limit on the total number of texture
1019 image units allowed.
1024 * Check: any two active samplers in the current program object are of
1025 * different types, but refer to the same texture image unit,
1027 if (vp
&& !validate_samplers(ctx
, &vp
->Base
, errMsg
)) {
1030 if (fp
&& !validate_samplers(ctx
, &fp
->Base
, errMsg
)) {
1039 * Called via glValidateProgram()
1042 validate_program(GLcontext
*ctx
, GLuint program
)
1044 struct gl_shader_program
*shProg
;
1047 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1052 shProg
->Validated
= validate_shader_program(ctx
, shProg
, errMsg
);
1053 if (!shProg
->Validated
) {
1054 /* update info log */
1055 if (shProg
->InfoLog
) {
1056 talloc_free(shProg
->InfoLog
);
1058 shProg
->InfoLog
= talloc_strdup(shProg
, errMsg
);
1065 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1067 GET_CURRENT_CONTEXT(ctx
);
1068 attach_shader(ctx
, program
, shader
);
1073 _mesa_AttachShader(GLuint program
, GLuint shader
)
1075 GET_CURRENT_CONTEXT(ctx
);
1076 attach_shader(ctx
, program
, shader
);
1081 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
1082 const GLcharARB
*name
)
1084 GET_CURRENT_CONTEXT(ctx
);
1085 bind_attrib_location(ctx
, program
, index
, name
);
1090 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
1092 GET_CURRENT_CONTEXT(ctx
);
1093 compile_shader(ctx
, shaderObj
);
1098 _mesa_CreateShader(GLenum type
)
1100 GET_CURRENT_CONTEXT(ctx
);
1101 return create_shader(ctx
, type
);
1105 GLhandleARB GLAPIENTRY
1106 _mesa_CreateShaderObjectARB(GLenum type
)
1108 GET_CURRENT_CONTEXT(ctx
);
1109 return create_shader(ctx
, type
);
1114 _mesa_CreateProgram(void)
1116 GET_CURRENT_CONTEXT(ctx
);
1117 return create_shader_program(ctx
);
1121 GLhandleARB GLAPIENTRY
1122 _mesa_CreateProgramObjectARB(void)
1124 GET_CURRENT_CONTEXT(ctx
);
1125 return create_shader_program(ctx
);
1130 _mesa_DeleteObjectARB(GLhandleARB obj
)
1133 GET_CURRENT_CONTEXT(ctx
);
1134 if (is_program(ctx
, obj
)) {
1135 delete_shader_program(ctx
, obj
);
1137 else if (is_shader(ctx
, obj
)) {
1138 delete_shader(ctx
, obj
);
1148 _mesa_DeleteProgram(GLuint name
)
1151 GET_CURRENT_CONTEXT(ctx
);
1152 delete_shader_program(ctx
, name
);
1158 _mesa_DeleteShader(GLuint name
)
1161 GET_CURRENT_CONTEXT(ctx
);
1162 delete_shader(ctx
, name
);
1168 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1170 GET_CURRENT_CONTEXT(ctx
);
1171 detach_shader(ctx
, program
, shader
);
1176 _mesa_DetachShader(GLuint program
, GLuint shader
)
1178 GET_CURRENT_CONTEXT(ctx
);
1179 detach_shader(ctx
, program
, shader
);
1184 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint index
,
1185 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1186 GLenum
* type
, GLcharARB
* name
)
1188 GET_CURRENT_CONTEXT(ctx
);
1189 get_active_attrib(ctx
, program
, index
, maxLength
, length
, size
, type
, name
);
1194 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1195 GLsizei
* count
, GLhandleARB
* obj
)
1197 GET_CURRENT_CONTEXT(ctx
);
1198 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1203 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1204 GLsizei
*count
, GLuint
*obj
)
1206 GET_CURRENT_CONTEXT(ctx
);
1207 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1212 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
1214 GET_CURRENT_CONTEXT(ctx
);
1215 return get_attrib_location(ctx
, program
, name
);
1220 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1221 GLcharARB
* infoLog
)
1223 GET_CURRENT_CONTEXT(ctx
);
1224 if (is_program(ctx
, object
)) {
1225 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1227 else if (is_shader(ctx
, object
)) {
1228 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1231 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1237 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1239 GET_CURRENT_CONTEXT(ctx
);
1240 /* Implement in terms of GetProgramiv, GetShaderiv */
1241 if (is_program(ctx
, object
)) {
1242 if (pname
== GL_OBJECT_TYPE_ARB
) {
1243 *params
= GL_PROGRAM_OBJECT_ARB
;
1246 get_programiv(ctx
, object
, pname
, params
);
1249 else if (is_shader(ctx
, object
)) {
1250 if (pname
== GL_OBJECT_TYPE_ARB
) {
1251 *params
= GL_SHADER_OBJECT_ARB
;
1254 get_shaderiv(ctx
, object
, pname
, params
);
1258 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1264 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1267 GLint iparams
[1]; /* XXX is one element enough? */
1268 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1269 params
[0] = (GLfloat
) iparams
[0];
1274 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1276 GET_CURRENT_CONTEXT(ctx
);
1277 get_programiv(ctx
, program
, pname
, params
);
1282 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1284 GET_CURRENT_CONTEXT(ctx
);
1285 get_shaderiv(ctx
, shader
, pname
, params
);
1290 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1291 GLsizei
*length
, GLchar
*infoLog
)
1293 GET_CURRENT_CONTEXT(ctx
);
1294 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1299 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1300 GLsizei
*length
, GLchar
*infoLog
)
1302 GET_CURRENT_CONTEXT(ctx
);
1303 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1308 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1309 GLsizei
*length
, GLcharARB
*sourceOut
)
1311 GET_CURRENT_CONTEXT(ctx
);
1312 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1316 GLhandleARB GLAPIENTRY
1317 _mesa_GetHandleARB(GLenum pname
)
1319 GET_CURRENT_CONTEXT(ctx
);
1320 return get_handle(ctx
, pname
);
1324 GLboolean GLAPIENTRY
1325 _mesa_IsProgram(GLuint name
)
1327 GET_CURRENT_CONTEXT(ctx
);
1328 return is_program(ctx
, name
);
1332 GLboolean GLAPIENTRY
1333 _mesa_IsShader(GLuint name
)
1335 GET_CURRENT_CONTEXT(ctx
);
1336 return is_shader(ctx
, name
);
1341 _mesa_LinkProgramARB(GLhandleARB programObj
)
1343 GET_CURRENT_CONTEXT(ctx
);
1344 link_program(ctx
, programObj
);
1350 * Read shader source code from a file.
1351 * Useful for debugging to override an app's shader.
1354 read_shader(const char *fname
)
1356 const int max
= 50*1000;
1357 FILE *f
= fopen(fname
, "r");
1358 GLcharARB
*buffer
, *shader
;
1365 buffer
= (char *) malloc(max
);
1366 len
= fread(buffer
, 1, max
, f
);
1371 shader
= _mesa_strdup(buffer
);
1379 * Called via glShaderSource() and glShaderSourceARB() API functions.
1380 * Basically, concatenate the source code strings into one long string
1381 * and pass it to _mesa_shader_source().
1384 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1385 const GLcharARB
** string
, const GLint
* length
)
1387 GET_CURRENT_CONTEXT(ctx
);
1389 GLsizei i
, totalLength
;
1393 if (!shaderObj
|| string
== NULL
) {
1394 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1399 * This array holds offsets of where the appropriate string ends, thus the
1400 * last element will be set to the total length of the source code.
1402 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1403 if (offsets
== NULL
) {
1404 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1408 for (i
= 0; i
< count
; i
++) {
1409 if (string
[i
] == NULL
) {
1410 free((GLvoid
*) offsets
);
1411 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderSourceARB(null string)");
1414 if (length
== NULL
|| length
[i
] < 0)
1415 offsets
[i
] = strlen(string
[i
]);
1417 offsets
[i
] = length
[i
];
1418 /* accumulate string lengths */
1420 offsets
[i
] += offsets
[i
- 1];
1423 /* Total length of source string is sum off all strings plus two.
1424 * One extra byte for terminating zero, another extra byte to silence
1425 * valgrind warnings in the parser/grammer code.
1427 totalLength
= offsets
[count
- 1] + 2;
1428 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1429 if (source
== NULL
) {
1430 free((GLvoid
*) offsets
);
1431 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1435 for (i
= 0; i
< count
; i
++) {
1436 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1437 memcpy(source
+ start
, string
[i
],
1438 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1440 source
[totalLength
- 1] = '\0';
1441 source
[totalLength
- 2] = '\0';
1444 /* Compute the shader's source code checksum then try to open a file
1445 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1446 * original shader source code. For debugging.
1449 GLcharARB
*newSource
;
1451 checksum
= _mesa_str_checksum(source
);
1453 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1455 newSource
= read_shader(filename
);
1457 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1458 shaderObj
, checksum
, filename
);
1464 shader_source(ctx
, shaderObj
, source
);
1467 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1469 sh
->SourceChecksum
= checksum
; /* save original checksum */
1477 _mesa_UseProgramObjectARB(GLhandleARB program
)
1479 GET_CURRENT_CONTEXT(ctx
);
1480 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1481 _mesa_use_program(ctx
, program
);
1486 _mesa_ValidateProgramARB(GLhandleARB program
)
1488 GET_CURRENT_CONTEXT(ctx
);
1489 validate_program(ctx
, program
);
1495 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1496 GLint
* range
, GLint
* precision
)
1498 GET_CURRENT_CONTEXT(ctx
);
1499 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1504 _mesa_ReleaseShaderCompiler(void)
1506 GET_CURRENT_CONTEXT(ctx
);
1507 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1512 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1513 const void* binary
, GLint length
)
1515 GET_CURRENT_CONTEXT(ctx
);
1516 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1519 #endif /* FEATURE_ES2 */
1522 #if FEATURE_ARB_geometry_shader4
1525 _mesa_ProgramParameteriARB(GLuint program
, GLenum pname
,
1528 struct gl_shader_program
*shProg
;
1529 GET_CURRENT_CONTEXT(ctx
);
1531 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1533 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1534 "glProgramParameteri");
1539 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1541 value
> ctx
->Const
.GeometryProgram
.MaxGeometryOutputVertices
) {
1542 _mesa_error(ctx
, GL_INVALID_VALUE
,
1543 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1547 shProg
->Geom
.VerticesOut
= value
;
1549 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1553 case GL_LINES_ADJACENCY_ARB
:
1555 case GL_TRIANGLES_ADJACENCY_ARB
:
1556 shProg
->Geom
.InputType
= value
;
1559 _mesa_error(ctx
, GL_INVALID_VALUE
,
1560 "glProgramParameteri(geometry input type = %s",
1561 _mesa_lookup_enum_by_nr(value
));
1565 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1569 case GL_TRIANGLE_STRIP
:
1570 shProg
->Geom
.OutputType
= value
;
1573 _mesa_error(ctx
, GL_INVALID_VALUE
,
1574 "glProgramParameteri(geometry output type = %s",
1575 _mesa_lookup_enum_by_nr(value
));
1580 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteriARB(pname=%s)",
1581 _mesa_lookup_enum_by_nr(pname
));
1590 * Plug in shader-related functions into API dispatch table.
1593 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1596 /* GL_ARB_vertex/fragment_shader */
1597 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1598 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1599 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1600 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1601 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1602 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1603 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1604 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1605 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1606 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1607 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1608 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1609 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1610 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1611 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1612 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1615 SET_AttachShader(exec
, _mesa_AttachShader
);
1616 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1617 SET_CreateShader(exec
, _mesa_CreateShader
);
1618 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1619 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1620 SET_DetachShader(exec
, _mesa_DetachShader
);
1621 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1622 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1623 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1624 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1625 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1626 SET_IsProgram(exec
, _mesa_IsProgram
);
1627 SET_IsShader(exec
, _mesa_IsShader
);
1629 #if FEATURE_ARB_vertex_shader
1630 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1631 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1632 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);
1635 #if FEATURE_ARB_geometry_shader4
1636 SET_ProgramParameteriARB(exec
, _mesa_ProgramParameteriARB
);
1638 #endif /* FEATURE_GL */