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(struct gl_context
*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
;
100 memset(&options
, 0, sizeof(options
));
101 options
.MaxUnrollIterations
= 32;
103 /* Default pragma settings */
104 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
106 for(i
= 0; i
< MESA_SHADER_TYPES
; ++i
)
107 memcpy(&ctx
->ShaderCompilerOptions
[i
], &options
, sizeof(options
));
109 ctx
->Shader
.Flags
= get_shader_flags();
114 * Free the per-context shader-related state.
117 _mesa_free_shader_state(struct gl_context
*ctx
)
119 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
124 * Return the size of the given GLSL datatype, in floats (components).
127 _mesa_sizeof_glsl_type(GLenum type
)
136 case GL_SAMPLER_CUBE
:
137 case GL_SAMPLER_1D_SHADOW
:
138 case GL_SAMPLER_2D_SHADOW
:
139 case GL_SAMPLER_2D_RECT_ARB
:
140 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
141 case GL_SAMPLER_1D_ARRAY_EXT
:
142 case GL_SAMPLER_2D_ARRAY_EXT
:
143 case GL_SAMPLER_1D_ARRAY_SHADOW_EXT
:
144 case GL_SAMPLER_2D_ARRAY_SHADOW_EXT
:
145 case GL_SAMPLER_CUBE_SHADOW_EXT
:
149 case GL_UNSIGNED_INT_VEC2
:
154 case GL_UNSIGNED_INT_VEC3
:
159 case GL_UNSIGNED_INT_VEC4
:
163 case GL_FLOAT_MAT2x3
:
164 case GL_FLOAT_MAT2x4
:
165 return 8; /* two float[4] vectors */
167 case GL_FLOAT_MAT3x2
:
168 case GL_FLOAT_MAT3x4
:
169 return 12; /* three float[4] vectors */
171 case GL_FLOAT_MAT4x2
:
172 case GL_FLOAT_MAT4x3
:
173 return 16; /* four float[4] vectors */
175 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
182 * Copy string from <src> to <dst>, up to maxLength characters, returning
183 * length of <dst> in <length>.
184 * \param src the strings source
185 * \param maxLength max chars to copy
186 * \param length returns number of chars copied
187 * \param dst the string destination
190 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
191 GLsizei
*length
, const GLchar
*src
)
194 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
205 * Confirm that the a shader type is valid and supported by the implementation
207 * \param ctx Current GL context
208 * \param type Shader target
212 validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
215 #if FEATURE_ARB_fragment_shader
216 case GL_FRAGMENT_SHADER
:
217 return ctx
->Extensions
.ARB_fragment_shader
;
219 #if FEATURE_ARB_vertex_shader
220 case GL_VERTEX_SHADER
:
221 return ctx
->Extensions
.ARB_vertex_shader
;
223 #if FEATURE_ARB_geometry_shader4
224 case GL_GEOMETRY_SHADER_ARB
:
225 return ctx
->Extensions
.ARB_geometry_shader4
;
234 * Find the length of the longest transform feedback varying name
235 * which was specified with glTransformFeedbackVaryings().
238 longest_feedback_varying_name(const struct gl_shader_program
*shProg
)
242 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
243 GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]);
253 is_program(struct gl_context
*ctx
, GLuint name
)
255 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
256 return shProg
? GL_TRUE
: GL_FALSE
;
261 is_shader(struct gl_context
*ctx
, GLuint name
)
263 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
264 return shader
? GL_TRUE
: GL_FALSE
;
269 * Attach shader to a shader program.
272 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
274 struct gl_shader_program
*shProg
;
275 struct gl_shader
*sh
;
278 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
282 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
287 n
= shProg
->NumShaders
;
288 for (i
= 0; i
< n
; i
++) {
289 if (shProg
->Shaders
[i
] == sh
) {
290 /* The shader is already attched to this program. The
291 * GL_ARB_shader_objects spec says:
293 * "The error INVALID_OPERATION is generated by AttachObjectARB
294 * if <obj> is already attached to <containerObj>."
296 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
302 shProg
->Shaders
= (struct gl_shader
**)
303 _mesa_realloc(shProg
->Shaders
,
304 n
* sizeof(struct gl_shader
*),
305 (n
+ 1) * sizeof(struct gl_shader
*));
306 if (!shProg
->Shaders
) {
307 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
312 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
313 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
314 shProg
->NumShaders
++;
319 get_attrib_location(struct gl_context
*ctx
, GLuint program
, const GLchar
*name
)
321 struct gl_shader_program
*shProg
322 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
328 if (!shProg
->LinkStatus
) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
330 "glGetAttribLocation(program not linked)");
337 if (shProg
->VertexProgram
) {
338 const struct gl_program_parameter_list
*attribs
=
339 shProg
->VertexProgram
->Base
.Attributes
;
341 GLint i
= _mesa_lookup_parameter_index(attribs
, -1, name
);
343 return attribs
->Parameters
[i
].StateIndexes
[0];
352 bind_attrib_location(struct gl_context
*ctx
, GLuint program
, GLuint index
,
355 struct gl_shader_program
*shProg
;
356 const GLint size
= -1; /* unknown size */
358 GLenum datatype
= GL_FLOAT_VEC4
;
360 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
361 "glBindAttribLocation");
369 if (strncmp(name
, "gl_", 3) == 0) {
370 _mesa_error(ctx
, GL_INVALID_OPERATION
,
371 "glBindAttribLocation(illegal name)");
375 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
376 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
380 if (shProg
->LinkStatus
) {
381 /* get current index/location for the attribute */
382 oldIndex
= get_attrib_location(ctx
, program
, name
);
388 /* this will replace the current value if it's already in the list */
389 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
391 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
396 * Note that this attribute binding won't go into effect until
397 * glLinkProgram is called again.
403 create_shader(struct gl_context
*ctx
, GLenum type
)
405 struct gl_shader
*sh
;
408 if (!validate_shader_target(ctx
, type
)) {
409 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
413 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
414 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
415 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
422 create_shader_program(struct gl_context
*ctx
)
425 struct gl_shader_program
*shProg
;
427 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
429 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
431 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
433 assert(shProg
->RefCount
== 1);
440 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
444 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
447 * NOTE: deleting shaders/programs works a bit differently than
448 * texture objects (and buffer objects, etc). Shader/program
449 * handles/IDs exist in the hash table until the object is really
450 * deleted (refcount==0). With texture objects, the handle/ID is
451 * removed from the hash table in glDeleteTextures() while the tex
452 * object itself might linger until its refcount goes to zero.
454 struct gl_shader_program
*shProg
;
456 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
460 shProg
->DeletePending
= GL_TRUE
;
462 /* effectively, decr shProg's refcount */
463 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
468 delete_shader(struct gl_context
*ctx
, GLuint shader
)
470 struct gl_shader
*sh
;
472 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
476 sh
->DeletePending
= GL_TRUE
;
478 /* effectively, decr sh's refcount */
479 _mesa_reference_shader(ctx
, &sh
, NULL
);
484 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
486 struct gl_shader_program
*shProg
;
490 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
494 n
= shProg
->NumShaders
;
496 for (i
= 0; i
< n
; i
++) {
497 if (shProg
->Shaders
[i
]->Name
== shader
) {
499 struct gl_shader
**newList
;
502 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
504 /* alloc new, smaller array */
505 newList
= (struct gl_shader
**)
506 malloc((n
- 1) * sizeof(struct gl_shader
*));
508 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
511 for (j
= 0; j
< i
; j
++) {
512 newList
[j
] = shProg
->Shaders
[j
];
515 newList
[j
++] = shProg
->Shaders
[i
];
516 free(shProg
->Shaders
);
518 shProg
->Shaders
= newList
;
519 shProg
->NumShaders
= n
- 1;
524 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
525 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
526 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
527 assert(shProg
->Shaders
[j
]->RefCount
> 0);
539 if (is_shader(ctx
, shader
))
540 err
= GL_INVALID_OPERATION
;
541 else if (is_program(ctx
, shader
))
542 err
= GL_INVALID_OPERATION
;
544 err
= GL_INVALID_VALUE
;
545 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
552 get_active_attrib(struct gl_context
*ctx
, GLuint program
, GLuint index
,
553 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
554 GLenum
*type
, GLchar
*nameOut
)
556 const struct gl_program_parameter_list
*attribs
= NULL
;
557 struct gl_shader_program
*shProg
;
559 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
563 if (shProg
->VertexProgram
)
564 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
566 if (!attribs
|| index
>= attribs
->NumParameters
) {
567 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
571 _mesa_copy_string(nameOut
, maxLength
, length
,
572 attribs
->Parameters
[index
].Name
);
575 *size
= attribs
->Parameters
[index
].Size
576 / _mesa_sizeof_glsl_type(attribs
->Parameters
[index
].DataType
);
579 *type
= attribs
->Parameters
[index
].DataType
;
584 * Return list of shaders attached to shader program.
587 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
588 GLsizei
*count
, GLuint
*obj
)
590 struct gl_shader_program
*shProg
=
591 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
594 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
595 obj
[i
] = shProg
->Shaders
[i
]->Name
;
604 * glGetHandleARB() - return ID/name of currently bound shader program.
607 get_handle(struct gl_context
*ctx
, GLenum pname
)
609 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
610 if (ctx
->Shader
.CurrentProgram
)
611 return ctx
->Shader
.CurrentProgram
->Name
;
616 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
623 * glGetProgramiv() - get shader program state.
624 * Note that this is for GLSL shader programs, not ARB vertex/fragment
625 * programs (see glGetProgramivARB).
628 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
630 const struct gl_program_parameter_list
*attribs
;
631 struct gl_shader_program
*shProg
632 = _mesa_lookup_shader_program(ctx
, program
);
635 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
639 if (shProg
->VertexProgram
)
640 attribs
= shProg
->VertexProgram
->Base
.Attributes
;
645 case GL_DELETE_STATUS
:
646 *params
= shProg
->DeletePending
;
649 *params
= shProg
->LinkStatus
;
651 case GL_VALIDATE_STATUS
:
652 *params
= shProg
->Validated
;
654 case GL_INFO_LOG_LENGTH
:
655 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
657 case GL_ATTACHED_SHADERS
:
658 *params
= shProg
->NumShaders
;
660 case GL_ACTIVE_ATTRIBUTES
:
661 *params
= attribs
? attribs
->NumParameters
: 0;
663 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
664 *params
= _mesa_longest_parameter_name(attribs
, PROGRAM_INPUT
) + 1;
666 case GL_ACTIVE_UNIFORMS
:
667 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
669 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
670 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
672 (*params
)++; /* add one for terminating zero */
674 case GL_PROGRAM_BINARY_LENGTH_OES
:
677 #if FEATURE_EXT_transform_feedback
678 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
679 *params
= shProg
->TransformFeedback
.NumVarying
;
681 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
:
682 *params
= longest_feedback_varying_name(shProg
) + 1;
684 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
685 *params
= shProg
->TransformFeedback
.BufferMode
;
688 #if FEATURE_ARB_geometry_shader4
689 case GL_GEOMETRY_VERTICES_OUT_ARB
:
690 *params
= shProg
->Geom
.VerticesOut
;
692 case GL_GEOMETRY_INPUT_TYPE_ARB
:
693 *params
= shProg
->Geom
.InputType
;
695 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
696 *params
= shProg
->Geom
.OutputType
;
700 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
707 * glGetShaderiv() - get GLSL shader state
710 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
712 struct gl_shader
*shader
=
713 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
721 *params
= shader
->Type
;
723 case GL_DELETE_STATUS
:
724 *params
= shader
->DeletePending
;
726 case GL_COMPILE_STATUS
:
727 *params
= shader
->CompileStatus
;
729 case GL_INFO_LOG_LENGTH
:
730 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
732 case GL_SHADER_SOURCE_LENGTH
:
733 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
736 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
743 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
744 GLsizei
*length
, GLchar
*infoLog
)
746 struct gl_shader_program
*shProg
747 = _mesa_lookup_shader_program(ctx
, program
);
749 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
752 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
757 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
758 GLsizei
*length
, GLchar
*infoLog
)
760 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
762 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
765 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
770 * Return shader source code.
773 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
774 GLsizei
*length
, GLchar
*sourceOut
)
776 struct gl_shader
*sh
;
777 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
781 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
786 * Set/replace shader source code.
789 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
791 struct gl_shader
*sh
;
793 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
797 /* free old shader source string and install new one */
799 free((void *) sh
->Source
);
802 sh
->CompileStatus
= GL_FALSE
;
804 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
813 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
815 struct gl_shader
*sh
;
816 struct gl_shader_compiler_options
*options
;
818 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
822 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
824 /* set default pragma state for shader */
825 sh
->Pragmas
= options
->DefaultPragmas
;
827 /* this call will set the sh->CompileStatus field to indicate if
828 * compilation was successful.
830 _mesa_glsl_compile_shader(ctx
, sh
);
835 * Link a program's shaders.
838 link_program(struct gl_context
*ctx
, GLuint program
)
840 struct gl_shader_program
*shProg
;
841 struct gl_transform_feedback_object
*obj
=
842 ctx
->TransformFeedback
.CurrentObject
;
844 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
848 if (obj
->Active
&& shProg
== ctx
->Shader
.CurrentProgram
) {
849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
850 "glLinkProgram(transform feedback active");
854 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
856 _mesa_glsl_link_shader(ctx
, shProg
);
862 printf("Link %u shaders in program %u: %s\n",
863 shProg
->NumShaders
, shProg
->Name
,
864 shProg
->LinkStatus
? "Success" : "Failed");
866 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
867 printf(" shader %u, type 0x%x\n",
868 shProg
->Shaders
[i
]->Name
,
869 shProg
->Shaders
[i
]->Type
);
876 * Print basic shader info (for debug).
879 print_shader_info(const struct gl_shader_program
*shProg
)
883 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
884 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
886 switch (shProg
->Shaders
[i
]->Type
) {
887 case GL_VERTEX_SHADER
:
890 case GL_FRAGMENT_SHADER
:
893 case GL_GEOMETRY_SHADER
:
899 printf(" %s shader %u, checksum %u\n", s
,
900 shProg
->Shaders
[i
]->Name
,
901 shProg
->Shaders
[i
]->SourceChecksum
);
903 if (shProg
->VertexProgram
)
904 printf(" vert prog %u\n", shProg
->VertexProgram
->Base
.Id
);
905 if (shProg
->FragmentProgram
)
906 printf(" frag prog %u\n", shProg
->FragmentProgram
->Base
.Id
);
911 * Use the named shader program for subsequent rendering.
914 _mesa_use_program(struct gl_context
*ctx
, GLuint program
)
916 struct gl_shader_program
*shProg
;
917 struct gl_transform_feedback_object
*obj
=
918 ctx
->TransformFeedback
.CurrentObject
;
921 _mesa_error(ctx
, GL_INVALID_OPERATION
,
922 "glUseProgram(transform feedback active)");
926 if (ctx
->Shader
.CurrentProgram
&&
927 ctx
->Shader
.CurrentProgram
->Name
== program
) {
933 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
937 if (!shProg
->LinkStatus
) {
938 _mesa_error(ctx
, GL_INVALID_OPERATION
,
939 "glUseProgram(program %u not linked)", program
);
944 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
945 print_shader_info(shProg
);
952 if (ctx
->Shader
.CurrentProgram
!= shProg
) {
953 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
954 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
957 if (ctx
->Driver
.UseProgram
)
958 ctx
->Driver
.UseProgram(ctx
, shProg
);
963 * Validate a program's samplers.
964 * Specifically, check that there aren't two samplers of different types
965 * pointing to the same texture unit.
966 * \return GL_TRUE if valid, GL_FALSE if invalid
969 validate_samplers(struct gl_context
*ctx
, const struct gl_program
*prog
, char *errMsg
)
971 static const char *targetName
[] = {
980 GLint targetUsed
[MAX_TEXTURE_IMAGE_UNITS
];
981 GLbitfield samplersUsed
= prog
->SamplersUsed
;
984 assert(Elements(targetName
) == NUM_TEXTURE_TARGETS
);
986 if (samplersUsed
== 0x0)
989 for (i
= 0; i
< Elements(targetUsed
); i
++)
992 /* walk over bits which are set in 'samplers' */
993 while (samplersUsed
) {
995 gl_texture_index target
;
996 GLint sampler
= _mesa_ffs(samplersUsed
) - 1;
997 assert(sampler
>= 0);
998 assert(sampler
< MAX_TEXTURE_IMAGE_UNITS
);
999 unit
= prog
->SamplerUnits
[sampler
];
1000 target
= prog
->SamplerTargets
[sampler
];
1001 if (targetUsed
[unit
] != -1 && targetUsed
[unit
] != (int) target
) {
1002 _mesa_snprintf(errMsg
, 100,
1003 "Texture unit %d is accessed both as %s and %s",
1004 unit
, targetName
[targetUsed
[unit
]], targetName
[target
]);
1007 targetUsed
[unit
] = target
;
1008 samplersUsed
^= (1 << sampler
);
1016 * Do validation of the given shader program.
1017 * \param errMsg returns error message if validation fails.
1018 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1021 validate_shader_program(struct gl_context
*ctx
,
1022 const struct gl_shader_program
*shProg
,
1025 const struct gl_vertex_program
*vp
= shProg
->VertexProgram
;
1026 const struct gl_fragment_program
*fp
= shProg
->FragmentProgram
;
1028 if (!shProg
->LinkStatus
) {
1032 /* From the GL spec, a program is invalid if any of these are true:
1034 any two active samplers in the current program object are of
1035 different types, but refer to the same texture image unit,
1037 any active sampler in the current program object refers to a texture
1038 image unit where fixed-function fragment processing accesses a
1039 texture target that does not match the sampler type, or
1041 the sum of the number of active samplers in the program and the
1042 number of texture image units enabled for fixed-function fragment
1043 processing exceeds the combined limit on the total number of texture
1044 image units allowed.
1049 * Check: any two active samplers in the current program object are of
1050 * different types, but refer to the same texture image unit,
1052 if (vp
&& !validate_samplers(ctx
, &vp
->Base
, errMsg
)) {
1055 if (fp
&& !validate_samplers(ctx
, &fp
->Base
, errMsg
)) {
1064 * Called via glValidateProgram()
1067 validate_program(struct gl_context
*ctx
, GLuint program
)
1069 struct gl_shader_program
*shProg
;
1072 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1077 shProg
->Validated
= validate_shader_program(ctx
, shProg
, errMsg
);
1078 if (!shProg
->Validated
) {
1079 /* update info log */
1080 if (shProg
->InfoLog
) {
1081 talloc_free(shProg
->InfoLog
);
1083 shProg
->InfoLog
= talloc_strdup(shProg
, errMsg
);
1090 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1092 GET_CURRENT_CONTEXT(ctx
);
1093 attach_shader(ctx
, program
, shader
);
1098 _mesa_AttachShader(GLuint program
, GLuint shader
)
1100 GET_CURRENT_CONTEXT(ctx
);
1101 attach_shader(ctx
, program
, shader
);
1106 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
1107 const GLcharARB
*name
)
1109 GET_CURRENT_CONTEXT(ctx
);
1110 bind_attrib_location(ctx
, program
, index
, name
);
1115 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
1117 GET_CURRENT_CONTEXT(ctx
);
1118 compile_shader(ctx
, shaderObj
);
1123 _mesa_CreateShader(GLenum type
)
1125 GET_CURRENT_CONTEXT(ctx
);
1126 return create_shader(ctx
, type
);
1130 GLhandleARB GLAPIENTRY
1131 _mesa_CreateShaderObjectARB(GLenum type
)
1133 GET_CURRENT_CONTEXT(ctx
);
1134 return create_shader(ctx
, type
);
1139 _mesa_CreateProgram(void)
1141 GET_CURRENT_CONTEXT(ctx
);
1142 return create_shader_program(ctx
);
1146 GLhandleARB GLAPIENTRY
1147 _mesa_CreateProgramObjectARB(void)
1149 GET_CURRENT_CONTEXT(ctx
);
1150 return create_shader_program(ctx
);
1155 _mesa_DeleteObjectARB(GLhandleARB obj
)
1158 GET_CURRENT_CONTEXT(ctx
);
1159 if (is_program(ctx
, obj
)) {
1160 delete_shader_program(ctx
, obj
);
1162 else if (is_shader(ctx
, obj
)) {
1163 delete_shader(ctx
, obj
);
1173 _mesa_DeleteProgram(GLuint name
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 delete_shader_program(ctx
, name
);
1183 _mesa_DeleteShader(GLuint name
)
1186 GET_CURRENT_CONTEXT(ctx
);
1187 delete_shader(ctx
, name
);
1193 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1195 GET_CURRENT_CONTEXT(ctx
);
1196 detach_shader(ctx
, program
, shader
);
1201 _mesa_DetachShader(GLuint program
, GLuint shader
)
1203 GET_CURRENT_CONTEXT(ctx
);
1204 detach_shader(ctx
, program
, shader
);
1209 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint index
,
1210 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
1211 GLenum
* type
, GLcharARB
* name
)
1213 GET_CURRENT_CONTEXT(ctx
);
1214 get_active_attrib(ctx
, program
, index
, maxLength
, length
, size
, type
, name
);
1219 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1220 GLsizei
* count
, GLhandleARB
* obj
)
1222 GET_CURRENT_CONTEXT(ctx
);
1223 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1228 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1229 GLsizei
*count
, GLuint
*obj
)
1231 GET_CURRENT_CONTEXT(ctx
);
1232 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1237 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
1239 GET_CURRENT_CONTEXT(ctx
);
1240 return get_attrib_location(ctx
, program
, name
);
1245 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1246 GLcharARB
* infoLog
)
1248 GET_CURRENT_CONTEXT(ctx
);
1249 if (is_program(ctx
, object
)) {
1250 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1252 else if (is_shader(ctx
, object
)) {
1253 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1256 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1262 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 /* Implement in terms of GetProgramiv, GetShaderiv */
1266 if (is_program(ctx
, object
)) {
1267 if (pname
== GL_OBJECT_TYPE_ARB
) {
1268 *params
= GL_PROGRAM_OBJECT_ARB
;
1271 get_programiv(ctx
, object
, pname
, params
);
1274 else if (is_shader(ctx
, object
)) {
1275 if (pname
== GL_OBJECT_TYPE_ARB
) {
1276 *params
= GL_SHADER_OBJECT_ARB
;
1279 get_shaderiv(ctx
, object
, pname
, params
);
1283 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1289 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1292 GLint iparams
[1]; /* XXX is one element enough? */
1293 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1294 params
[0] = (GLfloat
) iparams
[0];
1299 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1301 GET_CURRENT_CONTEXT(ctx
);
1302 get_programiv(ctx
, program
, pname
, params
);
1307 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 get_shaderiv(ctx
, shader
, pname
, params
);
1315 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1316 GLsizei
*length
, GLchar
*infoLog
)
1318 GET_CURRENT_CONTEXT(ctx
);
1319 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1324 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1325 GLsizei
*length
, GLchar
*infoLog
)
1327 GET_CURRENT_CONTEXT(ctx
);
1328 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1333 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1334 GLsizei
*length
, GLcharARB
*sourceOut
)
1336 GET_CURRENT_CONTEXT(ctx
);
1337 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1341 GLhandleARB GLAPIENTRY
1342 _mesa_GetHandleARB(GLenum pname
)
1344 GET_CURRENT_CONTEXT(ctx
);
1345 return get_handle(ctx
, pname
);
1349 GLboolean GLAPIENTRY
1350 _mesa_IsProgram(GLuint name
)
1352 GET_CURRENT_CONTEXT(ctx
);
1353 return is_program(ctx
, name
);
1357 GLboolean GLAPIENTRY
1358 _mesa_IsShader(GLuint name
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 return is_shader(ctx
, name
);
1366 _mesa_LinkProgramARB(GLhandleARB programObj
)
1368 GET_CURRENT_CONTEXT(ctx
);
1369 link_program(ctx
, programObj
);
1375 * Read shader source code from a file.
1376 * Useful for debugging to override an app's shader.
1379 read_shader(const char *fname
)
1381 const int max
= 50*1000;
1382 FILE *f
= fopen(fname
, "r");
1383 GLcharARB
*buffer
, *shader
;
1390 buffer
= (char *) malloc(max
);
1391 len
= fread(buffer
, 1, max
, f
);
1396 shader
= _mesa_strdup(buffer
);
1404 * Called via glShaderSource() and glShaderSourceARB() API functions.
1405 * Basically, concatenate the source code strings into one long string
1406 * and pass it to _mesa_shader_source().
1409 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1410 const GLcharARB
** string
, const GLint
* length
)
1412 GET_CURRENT_CONTEXT(ctx
);
1414 GLsizei i
, totalLength
;
1418 if (!shaderObj
|| string
== NULL
) {
1419 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1424 * This array holds offsets of where the appropriate string ends, thus the
1425 * last element will be set to the total length of the source code.
1427 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1428 if (offsets
== NULL
) {
1429 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1433 for (i
= 0; i
< count
; i
++) {
1434 if (string
[i
] == NULL
) {
1435 free((GLvoid
*) offsets
);
1436 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderSourceARB(null string)");
1439 if (length
== NULL
|| length
[i
] < 0)
1440 offsets
[i
] = strlen(string
[i
]);
1442 offsets
[i
] = length
[i
];
1443 /* accumulate string lengths */
1445 offsets
[i
] += offsets
[i
- 1];
1448 /* Total length of source string is sum off all strings plus two.
1449 * One extra byte for terminating zero, another extra byte to silence
1450 * valgrind warnings in the parser/grammer code.
1452 totalLength
= offsets
[count
- 1] + 2;
1453 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1454 if (source
== NULL
) {
1455 free((GLvoid
*) offsets
);
1456 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1460 for (i
= 0; i
< count
; i
++) {
1461 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1462 memcpy(source
+ start
, string
[i
],
1463 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1465 source
[totalLength
- 1] = '\0';
1466 source
[totalLength
- 2] = '\0';
1469 /* Compute the shader's source code checksum then try to open a file
1470 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1471 * original shader source code. For debugging.
1474 GLcharARB
*newSource
;
1476 checksum
= _mesa_str_checksum(source
);
1478 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1480 newSource
= read_shader(filename
);
1482 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1483 shaderObj
, checksum
, filename
);
1489 shader_source(ctx
, shaderObj
, source
);
1492 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1494 sh
->SourceChecksum
= checksum
; /* save original checksum */
1502 _mesa_UseProgramObjectARB(GLhandleARB program
)
1504 GET_CURRENT_CONTEXT(ctx
);
1505 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1506 _mesa_use_program(ctx
, program
);
1511 _mesa_ValidateProgramARB(GLhandleARB program
)
1513 GET_CURRENT_CONTEXT(ctx
);
1514 validate_program(ctx
, program
);
1520 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1521 GLint
* range
, GLint
* precision
)
1523 GET_CURRENT_CONTEXT(ctx
);
1524 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1529 _mesa_ReleaseShaderCompiler(void)
1531 GET_CURRENT_CONTEXT(ctx
);
1532 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1537 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1538 const void* binary
, GLint length
)
1540 GET_CURRENT_CONTEXT(ctx
);
1541 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1544 #endif /* FEATURE_ES2 */
1547 #if FEATURE_ARB_geometry_shader4
1550 _mesa_ProgramParameteriARB(GLuint program
, GLenum pname
,
1553 struct gl_shader_program
*shProg
;
1554 GET_CURRENT_CONTEXT(ctx
);
1556 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1558 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1559 "glProgramParameteri");
1564 case GL_GEOMETRY_VERTICES_OUT_ARB
:
1566 (unsigned) value
> ctx
->Const
.GeometryProgram
.MaxGeometryOutputVertices
) {
1567 _mesa_error(ctx
, GL_INVALID_VALUE
,
1568 "glProgramParameteri(GL_GEOMETRY_VERTICES_OUT_ARB=%d",
1572 shProg
->Geom
.VerticesOut
= value
;
1574 case GL_GEOMETRY_INPUT_TYPE_ARB
:
1578 case GL_LINES_ADJACENCY_ARB
:
1580 case GL_TRIANGLES_ADJACENCY_ARB
:
1581 shProg
->Geom
.InputType
= value
;
1584 _mesa_error(ctx
, GL_INVALID_VALUE
,
1585 "glProgramParameteri(geometry input type = %s",
1586 _mesa_lookup_enum_by_nr(value
));
1590 case GL_GEOMETRY_OUTPUT_TYPE_ARB
:
1594 case GL_TRIANGLE_STRIP
:
1595 shProg
->Geom
.OutputType
= value
;
1598 _mesa_error(ctx
, GL_INVALID_VALUE
,
1599 "glProgramParameteri(geometry output type = %s",
1600 _mesa_lookup_enum_by_nr(value
));
1605 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteriARB(pname=%s)",
1606 _mesa_lookup_enum_by_nr(pname
));
1615 * Plug in shader-related functions into API dispatch table.
1618 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1621 /* GL_ARB_vertex/fragment_shader */
1622 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1623 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1624 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1625 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1626 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1627 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1628 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1629 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1630 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1631 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1632 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1633 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1634 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1635 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1636 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1637 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1640 SET_AttachShader(exec
, _mesa_AttachShader
);
1641 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1642 SET_CreateShader(exec
, _mesa_CreateShader
);
1643 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1644 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1645 SET_DetachShader(exec
, _mesa_DetachShader
);
1646 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1647 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1648 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1649 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1650 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1651 SET_IsProgram(exec
, _mesa_IsProgram
);
1652 SET_IsShader(exec
, _mesa_IsShader
);
1654 #if FEATURE_ARB_vertex_shader
1655 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1656 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1657 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);
1660 #if FEATURE_ARB_geometry_shader4
1661 SET_ProgramParameteriARB(exec
, _mesa_ProgramParameteriARB
);
1663 #endif /* FEATURE_GL */