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 struct gl_shader_compiler_options options
;
99 options
.EmitHighLevelInstructions
= GL_TRUE
;
100 options
.EmitCondCodes
= GL_FALSE
;
101 options
.EmitComments
= GL_FALSE
;
102 options
.EmitNoIfs
= GL_FALSE
;
103 options
.EmitNoLoops
= GL_FALSE
;
104 options
.EmitNoFunctions
= GL_FALSE
;
105 options
.EmitNoCont
= GL_FALSE
;
106 options
.EmitNoMainReturn
= GL_FALSE
;
107 options
.MaxUnrollIterations
= 32;
109 /* Default pragma settings */
110 options
.DefaultPragmas
.IgnoreOptimize
= GL_FALSE
;
111 options
.DefaultPragmas
.IgnoreDebug
= GL_FALSE
;
112 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
113 options
.DefaultPragmas
.Debug
= GL_FALSE
;
115 for(i
= 0; i
< MESA_SHADER_TYPES
; ++i
)
116 memcpy(&ctx
->ShaderCompilerOptions
[i
], &options
, sizeof(options
));
118 ctx
->Shader
.Flags
= get_shader_flags();
123 * Free the per-context shader-related state.
126 _mesa_free_shader_state(GLcontext
*ctx
)
128 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
133 * Return the size of the given GLSL datatype, in floats (components).
136 _mesa_sizeof_glsl_type(GLenum type
)
145 case GL_SAMPLER_CUBE
:
146 case GL_SAMPLER_1D_SHADOW
:
147 case GL_SAMPLER_2D_SHADOW
:
148 case GL_SAMPLER_2D_RECT_ARB
:
149 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
150 case GL_SAMPLER_1D_ARRAY_EXT
:
151 case GL_SAMPLER_2D_ARRAY_EXT
:
152 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
153 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
154 case GL_SAMPLER_CUBE_SHADOW_EXT
:
158 case GL_UNSIGNED_INT_VEC2
:
163 case GL_UNSIGNED_INT_VEC3
:
168 case GL_UNSIGNED_INT_VEC4
:
172 case GL_FLOAT_MAT2x3
:
173 case GL_FLOAT_MAT2x4
:
174 return 8; /* two float[4] vectors */
176 case GL_FLOAT_MAT3x2
:
177 case GL_FLOAT_MAT3x4
:
178 return 12; /* three float[4] vectors */
180 case GL_FLOAT_MAT4x2
:
181 case GL_FLOAT_MAT4x3
:
182 return 16; /* four float[4] vectors */
184 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
191 * Copy string from <src> to <dst>, up to maxLength characters, returning
192 * length of <dst> in <length>.
193 * \param src the strings source
194 * \param maxLength max chars to copy
195 * \param length returns number of chars copied
196 * \param dst the string destination
199 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
200 GLsizei
*length
, const GLchar
*src
)
203 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
214 * Find the length of the longest transform feedback varying name
215 * which was specified with glTransformFeedbackVaryings().
218 longest_feedback_varying_name(const struct gl_shader_program
*shProg
)
222 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
223 GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]);
233 is_program(GLcontext
*ctx
, GLuint name
)
235 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
236 return shProg
? GL_TRUE
: GL_FALSE
;
241 is_shader(GLcontext
*ctx
, GLuint name
)
243 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
244 return shader
? GL_TRUE
: GL_FALSE
;
249 * Attach shader to a shader program.
252 attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
254 struct gl_shader_program
*shProg
;
255 struct gl_shader
*sh
;
258 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
262 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
267 n
= shProg
->NumShaders
;
268 for (i
= 0; i
< n
; i
++) {
269 if (shProg
->Shaders
[i
] == sh
) {
270 /* The shader is already attched to this program. The
271 * GL_ARB_shader_objects spec says:
273 * "The error INVALID_OPERATION is generated by AttachObjectARB
274 * if <obj> is already attached to <containerObj>."
276 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
282 shProg
->Shaders
= (struct gl_shader
**)
283 _mesa_realloc(shProg
->Shaders
,
284 n
* sizeof(struct gl_shader
*),
285 (n
+ 1) * sizeof(struct gl_shader
*));
286 if (!shProg
->Shaders
) {
287 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
292 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
293 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
294 shProg
->NumShaders
++;
299 get_attrib_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
301 struct gl_shader_program
*shProg
302 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
308 if (!shProg
->LinkStatus
) {
309 _mesa_error(ctx
, GL_INVALID_OPERATION
,
310 "glGetAttribLocation(program not linked)");
317 if (shProg
->VertexProgram
) {
318 const struct gl_program_parameter_list
*attribs
=
319 shProg
->VertexProgram
->Base
.Attributes
;
321 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
323 return attribs
->Parameters
[i
].StateIndexes
[0];
332 bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
335 struct gl_shader_program
*shProg
;
336 const GLint size
= -1; /* unknown size */
338 GLenum datatype
= GL_FLOAT_VEC4
;
340 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
341 "glBindAttribLocation");
349 if (strncmp(name
, "gl_", 3) == 0) {
350 _mesa_error(ctx
, GL_INVALID_OPERATION
,
351 "glBindAttribLocation(illegal name)");
355 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
356 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
360 if (shProg
->LinkStatus
) {
361 /* get current index/location for the attribute */
362 oldIndex
= get_attrib_location(ctx
, program
, name
);
368 /* this will replace the current value if it's already in the list */
369 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
371 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
376 * Note that this attribute binding won't go into effect until
377 * glLinkProgram is called again.
383 create_shader(GLcontext
*ctx
, GLenum type
)
385 struct gl_shader
*sh
;
388 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
391 case GL_FRAGMENT_SHADER
:
392 case GL_VERTEX_SHADER
:
393 case GL_GEOMETRY_SHADER_ARB
:
394 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
397 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
401 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
408 create_shader_program(GLcontext
*ctx
)
411 struct gl_shader_program
*shProg
;
413 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
415 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
417 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
419 assert(shProg
->RefCount
== 1);
426 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
430 delete_shader_program(GLcontext
*ctx
, GLuint name
)
433 * NOTE: deleting shaders/programs works a bit differently than
434 * texture objects (and buffer objects, etc). Shader/program
435 * handles/IDs exist in the hash table until the object is really
436 * deleted (refcount==0). With texture objects, the handle/ID is
437 * removed from the hash table in glDeleteTextures() while the tex
438 * object itself might linger until its refcount goes to zero.
440 struct gl_shader_program
*shProg
;
442 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
446 shProg
->DeletePending
= GL_TRUE
;
448 /* effectively, decr shProg's refcount */
449 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
454 delete_shader(GLcontext
*ctx
, GLuint shader
)
456 struct gl_shader
*sh
;
458 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
462 sh
->DeletePending
= GL_TRUE
;
464 /* effectively, decr sh's refcount */
465 _mesa_reference_shader(ctx
, &sh
, NULL
);
470 detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
472 struct gl_shader_program
*shProg
;
476 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
480 n
= shProg
->NumShaders
;
482 for (i
= 0; i
< n
; i
++) {
483 if (shProg
->Shaders
[i
]->Name
== shader
) {
485 struct gl_shader
**newList
;
488 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
490 /* alloc new, smaller array */
491 newList
= (struct gl_shader
**)
492 malloc((n
- 1) * sizeof(struct gl_shader
*));
494 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
497 for (j
= 0; j
< i
; j
++) {
498 newList
[j
] = shProg
->Shaders
[j
];
501 newList
[j
++] = shProg
->Shaders
[i
];
502 free(shProg
->Shaders
);
504 shProg
->Shaders
= newList
;
505 shProg
->NumShaders
= n
- 1;
510 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
511 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
512 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
513 assert(shProg
->Shaders
[j
]->RefCount
> 0);
525 if (is_shader(ctx
, shader
))
526 err
= GL_INVALID_OPERATION
;
527 else if (is_program(ctx
, shader
))
528 err
= GL_INVALID_OPERATION
;
530 err
= GL_INVALID_VALUE
;
531 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
538 get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
539 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
540 GLenum
*type
, GLchar
*nameOut
)
542 const struct gl_program_parameter_list
*attribs
= NULL
;
543 struct gl_shader_program
*shProg
;
545 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
549 if (shProg
->VertexProgram
)
550 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
552 if (!attribs
|| index
>= attribs
->NumParameters
) {
553 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
557 _mesa_copy_string(nameOut
, maxLength
, length
,
558 attribs
->Parameters
[index
].Name
);
561 *size
= attribs
->Parameters
[index
].Size
562 / _mesa_sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
565 *type
= attribs
->Parameters
[index
].DataType
;
570 * Return list of shaders attached to shader program.
573 get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
574 GLsizei
*count
, GLuint
*obj
)
576 struct gl_shader_program
*shProg
=
577 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
580 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
581 obj
[i
] = shProg
->Shaders
[i
]->Name
;
590 * glGetHandleARB() - return ID/name of currently bound shader program.
593 get_handle(GLcontext
*ctx
, GLenum pname
)
595 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
596 if (ctx
->Shader
.CurrentProgram
)
597 return ctx
->Shader
.CurrentProgram
->Name
;
602 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
609 * glGetProgramiv() - get shader program state.
610 * Note that this is for GLSL shader programs, not ARB vertex/fragment
611 * programs (see glGetProgramivARB).
614 get_programiv(GLcontext
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
616 const struct gl_program_parameter_list
*attribs
;
617 struct gl_shader_program
*shProg
618 = _mesa_lookup_shader_program(ctx
, program
);
621 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
625 if (shProg
->VertexProgram
)
626 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
631 case GL_DELETE_STATUS
:
632 *params
= shProg
->DeletePending
;
635 *params
= shProg
->LinkStatus
;
637 case GL_VALIDATE_STATUS
:
638 *params
= shProg
->Validated
;
640 case GL_INFO_LOG_LENGTH
:
641 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
643 case GL_ATTACHED_SHADERS
:
644 *params
= shProg
->NumShaders
;
646 case GL_ACTIVE_ATTRIBUTES
:
647 *params
= attribs
? attribs
->NumParameters
: 0;
649 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
650 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
652 case GL_ACTIVE_UNIFORMS
:
653 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
655 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
656 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
658 (*params
)++; /* add one for terminating zero */
660 case GL_PROGRAM_BINARY_LENGTH_OES
:
663 #if FEATURE_EXT_transform_feedback
664 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
665 *params
= shProg
->TransformFeedback
.NumVarying
;
667 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
:
668 *params
= longest_feedback_varying_name(shProg
) + 1;
670 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
671 *params
= shProg
->TransformFeedback
.BufferMode
;
674 #if FEATURE_ARB_geometry_shader4
675 case GL_GEOMETRY_VERTICES_OUT_ARB
:
676 *params
= shProg
->Geom
.VerticesOut
;
678 case GL_GEOMETRY_INPUT_TYPE_ARB
:
679 *params
= shProg
->Geom
.InputType
;
681 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
682 *params
= shProg
->Geom
.OutputType
;
686 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
693 * glGetShaderiv() - get GLSL shader state
696 get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
698 struct gl_shader
*shader
=
699 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
707 *params
= shader
->Type
;
709 case GL_DELETE_STATUS
:
710 *params
= shader
->DeletePending
;
712 case GL_COMPILE_STATUS
:
713 *params
= shader
->CompileStatus
;
715 case GL_INFO_LOG_LENGTH
:
716 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
718 case GL_SHADER_SOURCE_LENGTH
:
719 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
722 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
729 get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
730 GLsizei
*length
, GLchar
*infoLog
)
732 struct gl_shader_program
*shProg
733 = _mesa_lookup_shader_program(ctx
, program
);
735 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
738 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
743 get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
744 GLsizei
*length
, GLchar
*infoLog
)
746 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
748 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
751 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
756 * Return shader source code.
759 get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
760 GLsizei
*length
, GLchar
*sourceOut
)
762 struct gl_shader
*sh
;
763 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
767 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
772 * Set/replace shader source code.
775 shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
777 struct gl_shader
*sh
;
779 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
783 /* free old shader source string and install new one */
785 free((void *) sh
->Source
);
788 sh
->CompileStatus
= GL_FALSE
;
790 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
799 compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
801 struct gl_shader
*sh
;
802 struct gl_shader_compiler_options
*options
;
804 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
808 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
810 /* set default pragma state for shader */
811 sh
->Pragmas
= options
->DefaultPragmas
;
813 /* this call will set the sh->CompileStatus field to indicate if
814 * compilation was successful.
816 _mesa_glsl_compile_shader(ctx
, sh
);
821 * Link a program's shaders.
824 link_program(GLcontext
*ctx
, GLuint program
)
826 struct gl_shader_program
*shProg
;
827 struct gl_transform_feedback_object
*obj
=
828 ctx
->TransformFeedback
.CurrentObject
;
830 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
834 if (obj
->Active
&& shProg
== ctx
->Shader
.CurrentProgram
) {
835 _mesa_error(ctx
, GL_INVALID_OPERATION
,
836 "glLinkProgram(transform feedback active");
840 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
842 _mesa_glsl_link_shader(ctx
, shProg
);
848 printf("Link %u shaders in program %u: %s\n",
849 shProg
->NumShaders
, shProg
->Name
,
850 shProg
->LinkStatus
? "Success" : "Failed");
852 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
853 printf(" shader %u, type 0x%x\n",
854 shProg
->Shaders
[i
]->Name
,
855 shProg
->Shaders
[i
]->Type
);
862 * Print basic shader info (for debug).
865 print_shader_info(const struct gl_shader_program
*shProg
)
869 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
870 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
872 switch (shProg
->Shaders
[i
]->Type
) {
873 case GL_VERTEX_SHADER
:
876 case GL_FRAGMENT_SHADER
:
879 case GL_GEOMETRY_SHADER
:
885 printf(" %s shader %u, checksum %u\n", s
,
886 shProg
->Shaders
[i
]->Name
,
887 shProg
->Shaders
[i
]->SourceChecksum
);
889 if (shProg
->VertexProgram
)
890 printf(" vert prog %u\n", shProg
->VertexProgram
->Base
.Id
);
891 if (shProg
->FragmentProgram
)
892 printf(" frag prog %u\n", shProg
->FragmentProgram
->Base
.Id
);
897 * Use the named shader program for subsequent rendering.
900 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
902 struct gl_shader_program
*shProg
;
903 struct gl_transform_feedback_object
*obj
=
904 ctx
->TransformFeedback
.CurrentObject
;
907 _mesa_error(ctx
, GL_INVALID_OPERATION
,
908 "glUseProgram(transform feedback active)");
912 if (ctx
->Shader
.CurrentProgram
&&
913 ctx
->Shader
.CurrentProgram
->Name
== program
) {
919 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
923 if (!shProg
->LinkStatus
) {
924 _mesa_error(ctx
, GL_INVALID_OPERATION
,
925 "glUseProgram(program %u not linked)", program
);
930 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
931 print_shader_info(shProg
);
938 if (ctx
->Shader
.CurrentProgram
!= shProg
) {
939 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
940 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
943 if (ctx
->Driver
.UseProgram
)
944 ctx
->Driver
.UseProgram(ctx
, shProg
);
949 * Validate a program's samplers.
950 * Specifically, check that there aren't two samplers of different types
951 * pointing to the same texture unit.
952 * \return GL_TRUE if valid, GL_FALSE if invalid
955 validate_samplers(GLcontext
*ctx
, const struct gl_program
*prog
, char *errMsg
)
957 static const char *targetName
[] = {
966 GLint targetUsed
[MAX_TEXTURE_IMAGE_UNITS
];
967 GLbitfield samplersUsed
= prog
->SamplersUsed
;
970 assert(Elements(targetName
) == NUM_TEXTURE_TARGETS
);
972 if (samplersUsed
== 0x0)
975 for (i
= 0; i
< Elements(targetUsed
); i
++)
978 /* walk over bits which are set in 'samplers' */
979 while (samplersUsed
) {
981 gl_texture_index target
;
982 GLint sampler
= _mesa_ffs(samplersUsed
) - 1;
983 assert(sampler
>= 0);
984 assert(sampler
< MAX_TEXTURE_IMAGE_UNITS
);
985 unit
= prog
->SamplerUnits
[sampler
];
986 target
= prog
->SamplerTargets
[sampler
];
987 if (targetUsed
[unit
] != -1 && targetUsed
[unit
] != target
) {
988 _mesa_snprintf(errMsg
, 100,
989 "Texture unit %d is accessed both as %s and %s",
990 unit
, targetName
[targetUsed
[unit
]], targetName
[target
]);
993 targetUsed
[unit
] = target
;
994 samplersUsed
^= (1 << sampler
);
1002 * Do validation of the given shader program.
1003 * \param errMsg returns error message if validation fails.
1004 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1007 validate_shader_program(GLcontext
*ctx
,
1008 const struct gl_shader_program
*shProg
,
1011 const struct gl_vertex_program
*vp
= shProg
->VertexProgram
;
1012 const struct gl_fragment_program
*fp
= shProg
->FragmentProgram
;
1014 if (!shProg
->LinkStatus
) {
1018 /* From the GL spec, a program is invalid if any of these are true:
1020 any two active samplers in the current program object are of
1021 different types, but refer to the same texture image unit,
1023 any active sampler in the current program object refers to a texture
1024 image unit where fixed-function fragment processing accesses a
1025 texture target that does not match the sampler type, or
1027 the sum of the number of active samplers in the program and the
1028 number of texture image units enabled for fixed-function fragment
1029 processing exceeds the combined limit on the total number of texture
1030 image units allowed.
1035 * Check: any two active samplers in the current program object are of
1036 * different types, but refer to the same texture image unit,
1038 if (vp
&& !validate_samplers(ctx
, &vp
->Base
, errMsg
)) {
1041 if (fp
&& !validate_samplers(ctx
, &fp
->Base
, errMsg
)) {
1050 * Called via glValidateProgram()
1053 validate_program(GLcontext
*ctx
, GLuint program
)
1055 struct gl_shader_program
*shProg
;
1058 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1063 shProg
->Validated
= validate_shader_program(ctx
, shProg
, errMsg
);
1064 if (!shProg
->Validated
) {
1065 /* update info log */
1066 if (shProg
->InfoLog
) {
1067 talloc_free(shProg
->InfoLog
);
1069 shProg
->InfoLog
= talloc_strdup(shProg
, errMsg
);
1076 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1078 GET_CURRENT_CONTEXT(ctx
);
1079 attach_shader(ctx
, program
, shader
);
1084 _mesa_AttachShader(GLuint program
, GLuint shader
)
1086 GET_CURRENT_CONTEXT(ctx
);
1087 attach_shader(ctx
, program
, shader
);
1092 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
1093 const GLcharARB
*name
)
1095 GET_CURRENT_CONTEXT(ctx
);
1096 bind_attrib_location(ctx
, program
, index
, name
);
1101 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
1103 GET_CURRENT_CONTEXT(ctx
);
1104 compile_shader(ctx
, shaderObj
);
1109 _mesa_CreateShader(GLenum type
)
1111 GET_CURRENT_CONTEXT(ctx
);
1112 return create_shader(ctx
, type
);
1116 GLhandleARB GLAPIENTRY
1117 _mesa_CreateShaderObjectARB(GLenum type
)
1119 GET_CURRENT_CONTEXT(ctx
);
1120 return create_shader(ctx
, type
);
1125 _mesa_CreateProgram(void)
1127 GET_CURRENT_CONTEXT(ctx
);
1128 return create_shader_program(ctx
);
1132 GLhandleARB GLAPIENTRY
1133 _mesa_CreateProgramObjectARB(void)
1135 GET_CURRENT_CONTEXT(ctx
);
1136 return create_shader_program(ctx
);
1141 _mesa_DeleteObjectARB(GLhandleARB obj
)
1144 GET_CURRENT_CONTEXT(ctx
);
1145 if (is_program(ctx
, obj
)) {
1146 delete_shader_program(ctx
, obj
);
1148 else if (is_shader(ctx
, obj
)) {
1149 delete_shader(ctx
, obj
);
1159 _mesa_DeleteProgram(GLuint name
)
1162 GET_CURRENT_CONTEXT(ctx
);
1163 delete_shader_program(ctx
, name
);
1169 _mesa_DeleteShader(GLuint name
)
1172 GET_CURRENT_CONTEXT(ctx
);
1173 delete_shader(ctx
, name
);
1179 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1181 GET_CURRENT_CONTEXT(ctx
);
1182 detach_shader(ctx
, program
, shader
);
1187 _mesa_DetachShader(GLuint program
, GLuint shader
)
1189 GET_CURRENT_CONTEXT(ctx
);
1190 detach_shader(ctx
, program
, shader
);
1195 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint index
,
1196 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1197 GLenum
* type
, GLcharARB
* name
)
1199 GET_CURRENT_CONTEXT(ctx
);
1200 get_active_attrib(ctx
, program
, index
, maxLength
, length
, size
, type
, name
);
1205 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1206 GLsizei
* count
, GLhandleARB
* obj
)
1208 GET_CURRENT_CONTEXT(ctx
);
1209 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1214 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1215 GLsizei
*count
, GLuint
*obj
)
1217 GET_CURRENT_CONTEXT(ctx
);
1218 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1223 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
1225 GET_CURRENT_CONTEXT(ctx
);
1226 return get_attrib_location(ctx
, program
, name
);
1231 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1232 GLcharARB
* infoLog
)
1234 GET_CURRENT_CONTEXT(ctx
);
1235 if (is_program(ctx
, object
)) {
1236 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1238 else if (is_shader(ctx
, object
)) {
1239 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1242 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1248 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1250 GET_CURRENT_CONTEXT(ctx
);
1251 /* Implement in terms of GetProgramiv, GetShaderiv */
1252 if (is_program(ctx
, object
)) {
1253 if (pname
== GL_OBJECT_TYPE_ARB
) {
1254 *params
= GL_PROGRAM_OBJECT_ARB
;
1257 get_programiv(ctx
, object
, pname
, params
);
1260 else if (is_shader(ctx
, object
)) {
1261 if (pname
== GL_OBJECT_TYPE_ARB
) {
1262 *params
= GL_SHADER_OBJECT_ARB
;
1265 get_shaderiv(ctx
, object
, pname
, params
);
1269 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1275 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1278 GLint iparams
[1]; /* XXX is one element enough? */
1279 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1280 params
[0] = (GLfloat
) iparams
[0];
1285 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1287 GET_CURRENT_CONTEXT(ctx
);
1288 get_programiv(ctx
, program
, pname
, params
);
1293 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 get_shaderiv(ctx
, shader
, pname
, params
);
1301 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1302 GLsizei
*length
, GLchar
*infoLog
)
1304 GET_CURRENT_CONTEXT(ctx
);
1305 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1310 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1311 GLsizei
*length
, GLchar
*infoLog
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1319 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1320 GLsizei
*length
, GLcharARB
*sourceOut
)
1322 GET_CURRENT_CONTEXT(ctx
);
1323 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1327 GLhandleARB GLAPIENTRY
1328 _mesa_GetHandleARB(GLenum pname
)
1330 GET_CURRENT_CONTEXT(ctx
);
1331 return get_handle(ctx
, pname
);
1335 GLboolean GLAPIENTRY
1336 _mesa_IsProgram(GLuint name
)
1338 GET_CURRENT_CONTEXT(ctx
);
1339 return is_program(ctx
, name
);
1343 GLboolean GLAPIENTRY
1344 _mesa_IsShader(GLuint name
)
1346 GET_CURRENT_CONTEXT(ctx
);
1347 return is_shader(ctx
, name
);
1352 _mesa_LinkProgramARB(GLhandleARB programObj
)
1354 GET_CURRENT_CONTEXT(ctx
);
1355 link_program(ctx
, programObj
);
1361 * Read shader source code from a file.
1362 * Useful for debugging to override an app's shader.
1365 read_shader(const char *fname
)
1367 const int max
= 50*1000;
1368 FILE *f
= fopen(fname
, "r");
1369 GLcharARB
*buffer
, *shader
;
1376 buffer
= (char *) malloc(max
);
1377 len
= fread(buffer
, 1, max
, f
);
1382 shader
= _mesa_strdup(buffer
);
1390 * Called via glShaderSource() and glShaderSourceARB() API functions.
1391 * Basically, concatenate the source code strings into one long string
1392 * and pass it to _mesa_shader_source().
1395 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1396 const GLcharARB
** string
, const GLint
* length
)
1398 GET_CURRENT_CONTEXT(ctx
);
1400 GLsizei i
, totalLength
;
1404 if (!shaderObj
|| string
== NULL
) {
1405 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1410 * This array holds offsets of where the appropriate string ends, thus the
1411 * last element will be set to the total length of the source code.
1413 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1414 if (offsets
== NULL
) {
1415 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1419 for (i
= 0; i
< count
; i
++) {
1420 if (string
[i
] == NULL
) {
1421 free((GLvoid
*) offsets
);
1422 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderSourceARB(null string)");
1425 if (length
== NULL
|| length
[i
] < 0)
1426 offsets
[i
] = strlen(string
[i
]);
1428 offsets
[i
] = length
[i
];
1429 /* accumulate string lengths */
1431 offsets
[i
] += offsets
[i
- 1];
1434 /* Total length of source string is sum off all strings plus two.
1435 * One extra byte for terminating zero, another extra byte to silence
1436 * valgrind warnings in the parser/grammer code.
1438 totalLength
= offsets
[count
- 1] + 2;
1439 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1440 if (source
== NULL
) {
1441 free((GLvoid
*) offsets
);
1442 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1446 for (i
= 0; i
< count
; i
++) {
1447 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1448 memcpy(source
+ start
, string
[i
],
1449 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1451 source
[totalLength
- 1] = '\0';
1452 source
[totalLength
- 2] = '\0';
1455 /* Compute the shader's source code checksum then try to open a file
1456 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1457 * original shader source code. For debugging.
1460 GLcharARB
*newSource
;
1462 checksum
= _mesa_str_checksum(source
);
1464 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1466 newSource
= read_shader(filename
);
1468 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1469 shaderObj
, checksum
, filename
);
1475 shader_source(ctx
, shaderObj
, source
);
1478 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1480 sh
->SourceChecksum
= checksum
; /* save original checksum */
1488 _mesa_UseProgramObjectARB(GLhandleARB program
)
1490 GET_CURRENT_CONTEXT(ctx
);
1491 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1492 _mesa_use_program(ctx
, program
);
1497 _mesa_ValidateProgramARB(GLhandleARB program
)
1499 GET_CURRENT_CONTEXT(ctx
);
1500 validate_program(ctx
, program
);
1506 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1507 GLint
* range
, GLint
* precision
)
1509 GET_CURRENT_CONTEXT(ctx
);
1510 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1515 _mesa_ReleaseShaderCompiler(void)
1517 GET_CURRENT_CONTEXT(ctx
);
1518 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1523 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1524 const void* binary
, GLint length
)
1526 GET_CURRENT_CONTEXT(ctx
);
1527 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1530 #endif /* FEATURE_ES2 */
1533 #if FEATURE_ARB_geometry_shader4
1536 _mesa_ProgramParameteriARB(GLuint program
, GLenum pname
,
1539 struct gl_shader_program
*shProg
;
1540 GET_CURRENT_CONTEXT(ctx
);
1542 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1544 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1545 "glProgramParameteri");
1550 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1552 value
> ctx
->Const
.GeometryProgram
.MaxGeometryOutputVertices
) {
1553 _mesa_error(ctx
, GL_INVALID_VALUE
,
1554 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1558 shProg
->Geom
.VerticesOut
= value
;
1560 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1564 case GL_LINES_ADJACENCY_ARB
:
1566 case GL_TRIANGLES_ADJACENCY_ARB
:
1567 shProg
->Geom
.InputType
= value
;
1570 _mesa_error(ctx
, GL_INVALID_VALUE
,
1571 "glProgramParameteri(geometry input type = %s",
1572 _mesa_lookup_enum_by_nr(value
));
1576 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1580 case GL_TRIANGLE_STRIP
:
1581 shProg
->Geom
.OutputType
= value
;
1584 _mesa_error(ctx
, GL_INVALID_VALUE
,
1585 "glProgramParameteri(geometry output type = %s",
1586 _mesa_lookup_enum_by_nr(value
));
1591 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteriARB(pname=%s)",
1592 _mesa_lookup_enum_by_nr(pname
));
1601 * Plug in shader-related functions into API dispatch table.
1604 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1607 /* GL_ARB_vertex/fragment_shader */
1608 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1609 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1610 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1611 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1612 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1613 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1614 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1615 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1616 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1617 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1618 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1619 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1620 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1621 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1622 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1623 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1626 SET_AttachShader(exec
, _mesa_AttachShader
);
1627 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1628 SET_CreateShader(exec
, _mesa_CreateShader
);
1629 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1630 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1631 SET_DetachShader(exec
, _mesa_DetachShader
);
1632 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1633 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1634 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1635 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1636 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1637 SET_IsProgram(exec
, _mesa_IsProgram
);
1638 SET_IsShader(exec
, _mesa_IsShader
);
1640 #if FEATURE_ARB_vertex_shader
1641 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1642 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1643 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);
1646 #if FEATURE_ARB_geometry_shader4
1647 SET_ProgramParameteriARB(exec
, _mesa_ProgramParameteriARB
);
1649 #endif /* FEATURE_GL */