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/mfeatures.h"
45 #include "main/mtypes.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "main/uniforms.h"
49 #include "program/program.h"
50 #include "program/prog_parameter.h"
53 #include "../glsl/glsl_parser_extras.h"
54 #include "../glsl/ir_uniform.h"
56 /** Define this to enable shader substitution (see below) */
57 #define SHADER_SUBST 0
61 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
64 get_shader_flags(void)
66 GLbitfield flags
= 0x0;
67 const char *env
= _mesa_getenv("MESA_GLSL");
70 if (strstr(env
, "dump"))
72 if (strstr(env
, "log"))
74 if (strstr(env
, "nopvert"))
75 flags
|= GLSL_NOP_VERT
;
76 if (strstr(env
, "nopfrag"))
77 flags
|= GLSL_NOP_FRAG
;
78 if (strstr(env
, "nopt"))
80 else if (strstr(env
, "opt"))
82 if (strstr(env
, "uniform"))
83 flags
|= GLSL_UNIFORMS
;
84 if (strstr(env
, "useprog"))
85 flags
|= GLSL_USE_PROG
;
93 * Initialize context's shader state.
96 _mesa_init_shader_state(struct gl_context
*ctx
)
98 /* Device drivers may override these to control what kind of instructions
99 * are generated by the GLSL compiler.
101 struct gl_shader_compiler_options options
;
104 memset(&options
, 0, sizeof(options
));
105 options
.MaxUnrollIterations
= 32;
107 /* Default pragma settings */
108 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
110 for (sh
= 0; sh
< MESA_SHADER_TYPES
; ++sh
)
111 memcpy(&ctx
->ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
113 ctx
->Shader
.Flags
= get_shader_flags();
118 * Free the per-context shader-related state.
121 _mesa_free_shader_state(struct gl_context
*ctx
)
123 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentVertexProgram
, NULL
);
124 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentFragmentProgram
,
126 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
128 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
133 * Return the size of the given GLSL datatype, in floats (components).
136 _mesa_sizeof_glsl_type(GLenum type
)
141 case GL_UNSIGNED_INT
:
146 case GL_SAMPLER_CUBE
:
147 case GL_SAMPLER_1D_SHADOW
:
148 case GL_SAMPLER_2D_SHADOW
:
149 case GL_SAMPLER_CUBE_SHADOW_EXT
:
153 case GL_UNSIGNED_INT_VEC2
:
158 case GL_UNSIGNED_INT_VEC3
:
163 case GL_UNSIGNED_INT_VEC4
:
167 case GL_FLOAT_MAT2x3
:
168 case GL_FLOAT_MAT2x4
:
169 return 8; /* two float[4] vectors */
171 case GL_FLOAT_MAT3x2
:
172 case GL_FLOAT_MAT3x4
:
173 return 12; /* three float[4] vectors */
175 case GL_FLOAT_MAT4x2
:
176 case GL_FLOAT_MAT4x3
:
177 return 16; /* four float[4] vectors */
179 _mesa_problem(NULL
, "Invalid type in _mesa_sizeof_glsl_type()");
186 * Copy string from <src> to <dst>, up to maxLength characters, returning
187 * length of <dst> in <length>.
188 * \param src the strings source
189 * \param maxLength max chars to copy
190 * \param length returns number of chars copied
191 * \param dst the string destination
194 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
195 GLsizei
*length
, const GLchar
*src
)
198 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
209 * Confirm that the a shader type is valid and supported by the implementation
211 * \param ctx Current GL context
212 * \param type Shader target
216 validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
219 #if FEATURE_ARB_fragment_shader
220 case GL_FRAGMENT_SHADER
:
221 return ctx
->Extensions
.ARB_fragment_shader
;
223 #if FEATURE_ARB_vertex_shader
224 case GL_VERTEX_SHADER
:
225 return ctx
->Extensions
.ARB_vertex_shader
;
233 is_program(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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 create_shader(struct gl_context
*ctx
, GLenum type
)
301 struct gl_shader
*sh
;
304 if (!validate_shader_target(ctx
, type
)) {
305 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
309 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
310 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
311 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
318 create_shader_program(struct gl_context
*ctx
)
321 struct gl_shader_program
*shProg
;
323 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
325 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
327 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
329 assert(shProg
->RefCount
== 1);
336 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
340 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
343 * NOTE: deleting shaders/programs works a bit differently than
344 * texture objects (and buffer objects, etc). Shader/program
345 * handles/IDs exist in the hash table until the object is really
346 * deleted (refcount==0). With texture objects, the handle/ID is
347 * removed from the hash table in glDeleteTextures() while the tex
348 * object itself might linger until its refcount goes to zero.
350 struct gl_shader_program
*shProg
;
352 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
356 if (!shProg
->DeletePending
) {
357 shProg
->DeletePending
= GL_TRUE
;
359 /* effectively, decr shProg's refcount */
360 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
366 delete_shader(struct gl_context
*ctx
, GLuint shader
)
368 struct gl_shader
*sh
;
370 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
374 sh
->DeletePending
= GL_TRUE
;
376 /* effectively, decr sh's refcount */
377 _mesa_reference_shader(ctx
, &sh
, NULL
);
382 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
384 struct gl_shader_program
*shProg
;
388 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
392 n
= shProg
->NumShaders
;
394 for (i
= 0; i
< n
; i
++) {
395 if (shProg
->Shaders
[i
]->Name
== shader
) {
397 struct gl_shader
**newList
;
400 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
402 /* alloc new, smaller array */
403 newList
= (struct gl_shader
**)
404 malloc((n
- 1) * sizeof(struct gl_shader
*));
406 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
409 for (j
= 0; j
< i
; j
++) {
410 newList
[j
] = shProg
->Shaders
[j
];
413 newList
[j
++] = shProg
->Shaders
[i
];
414 free(shProg
->Shaders
);
416 shProg
->Shaders
= newList
;
417 shProg
->NumShaders
= n
- 1;
422 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
423 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
424 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
425 assert(shProg
->Shaders
[j
]->RefCount
> 0);
437 if (is_shader(ctx
, shader
))
438 err
= GL_INVALID_OPERATION
;
439 else if (is_program(ctx
, shader
))
440 err
= GL_INVALID_OPERATION
;
442 err
= GL_INVALID_VALUE
;
443 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
450 * Return list of shaders attached to shader program.
453 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
454 GLsizei
*count
, GLuint
*obj
)
456 struct gl_shader_program
*shProg
=
457 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
460 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
461 obj
[i
] = shProg
->Shaders
[i
]->Name
;
470 * glGetHandleARB() - return ID/name of currently bound shader program.
473 get_handle(struct gl_context
*ctx
, GLenum pname
)
475 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
476 if (ctx
->Shader
.ActiveProgram
)
477 return ctx
->Shader
.ActiveProgram
->Name
;
482 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
489 * glGetProgramiv() - get shader program state.
490 * Note that this is for GLSL shader programs, not ARB vertex/fragment
491 * programs (see glGetProgramivARB).
494 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
496 struct gl_shader_program
*shProg
497 = _mesa_lookup_shader_program(ctx
, program
);
500 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
505 case GL_DELETE_STATUS
:
506 *params
= shProg
->DeletePending
;
509 *params
= shProg
->LinkStatus
;
511 case GL_VALIDATE_STATUS
:
512 *params
= shProg
->Validated
;
514 case GL_INFO_LOG_LENGTH
:
515 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
517 case GL_ATTACHED_SHADERS
:
518 *params
= shProg
->NumShaders
;
520 case GL_ACTIVE_ATTRIBUTES
:
521 *params
= _mesa_count_active_attribs(shProg
);
523 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
524 *params
= _mesa_longest_attribute_name_length(shProg
);
526 case GL_ACTIVE_UNIFORMS
:
527 *params
= shProg
->NumUserUniformStorage
;
529 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
533 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
534 /* Add one for the terminating NUL character.
536 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1;
545 case GL_PROGRAM_BINARY_LENGTH_OES
:
549 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
556 * glGetShaderiv() - get GLSL shader state
559 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
561 struct gl_shader
*shader
=
562 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
570 *params
= shader
->Type
;
572 case GL_DELETE_STATUS
:
573 *params
= shader
->DeletePending
;
575 case GL_COMPILE_STATUS
:
576 *params
= shader
->CompileStatus
;
578 case GL_INFO_LOG_LENGTH
:
579 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
581 case GL_SHADER_SOURCE_LENGTH
:
582 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
585 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
592 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
593 GLsizei
*length
, GLchar
*infoLog
)
595 struct gl_shader_program
*shProg
596 = _mesa_lookup_shader_program(ctx
, program
);
598 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
601 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
606 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
607 GLsizei
*length
, GLchar
*infoLog
)
609 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
611 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
614 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
619 * Return shader source code.
622 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
623 GLsizei
*length
, GLchar
*sourceOut
)
625 struct gl_shader
*sh
;
626 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
630 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
635 * Set/replace shader source code.
638 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
640 struct gl_shader
*sh
;
642 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
646 /* free old shader source string and install new one */
648 free((void *) sh
->Source
);
651 sh
->CompileStatus
= GL_FALSE
;
653 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
662 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
664 struct gl_shader
*sh
;
665 struct gl_shader_compiler_options
*options
;
667 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
671 options
= &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(sh
->Type
)];
673 /* set default pragma state for shader */
674 sh
->Pragmas
= options
->DefaultPragmas
;
676 /* this call will set the sh->CompileStatus field to indicate if
677 * compilation was successful.
679 _mesa_glsl_compile_shader(ctx
, sh
);
684 * Link a program's shaders.
687 link_program(struct gl_context
*ctx
, GLuint program
)
689 struct gl_shader_program
*shProg
;
691 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
695 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
697 _mesa_glsl_link_shader(ctx
, shProg
);
703 printf("Link %u shaders in program %u: %s\n",
704 shProg
->NumShaders
, shProg
->Name
,
705 shProg
->LinkStatus
? "Success" : "Failed");
707 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
708 printf(" shader %u, type 0x%x\n",
709 shProg
->Shaders
[i
]->Name
,
710 shProg
->Shaders
[i
]->Type
);
717 * Print basic shader info (for debug).
720 print_shader_info(const struct gl_shader_program
*shProg
)
724 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
725 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
727 switch (shProg
->Shaders
[i
]->Type
) {
728 case GL_VERTEX_SHADER
:
731 case GL_FRAGMENT_SHADER
:
737 printf(" %s shader %u, checksum %u\n", s
,
738 shProg
->Shaders
[i
]->Name
,
739 shProg
->Shaders
[i
]->SourceChecksum
);
741 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
742 printf(" vert prog %u\n",
743 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
744 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
745 printf(" frag prog %u\n",
746 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
751 * Use the named shader program for subsequent glUniform calls
754 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
757 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
758 _mesa_error(ctx
, GL_INVALID_OPERATION
,
759 "%s(program %u not linked)", caller
, shProg
->Name
);
763 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
764 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
771 use_shader_program(struct gl_context
*ctx
, GLenum type
,
772 struct gl_shader_program
*shProg
)
774 struct gl_shader_program
**target
;
777 #if FEATURE_ARB_vertex_shader
778 case GL_VERTEX_SHADER
:
779 target
= &ctx
->Shader
.CurrentVertexProgram
;
781 || (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)) {
786 #if FEATURE_ARB_fragment_shader
787 case GL_FRAGMENT_SHADER
:
788 target
= &ctx
->Shader
.CurrentFragmentProgram
;
790 || (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)) {
799 if (*target
!= shProg
) {
800 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
802 /* If the shader is also bound as the current rendering shader, unbind
803 * it from that binding point as well. This ensures that the correct
804 * semantics of glDeleteProgram are maintained.
807 #if FEATURE_ARB_vertex_shader
808 case GL_VERTEX_SHADER
:
812 #if FEATURE_ARB_fragment_shader
813 case GL_FRAGMENT_SHADER
:
814 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
815 _mesa_reference_shader_program(ctx
,
816 &ctx
->Shader
._CurrentFragmentProgram
,
823 _mesa_reference_shader_program(ctx
, target
, shProg
);
831 * Use the named shader program for subsequent rendering.
834 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
836 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
837 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
838 _mesa_active_program(ctx
, shProg
, "glUseProgram");
840 if (ctx
->Driver
.UseProgram
)
841 ctx
->Driver
.UseProgram(ctx
, shProg
);
845 * Do validation of the given shader program.
846 * \param errMsg returns error message if validation fails.
847 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
850 validate_shader_program(const struct gl_shader_program
*shProg
,
853 if (!shProg
->LinkStatus
) {
857 /* From the GL spec, a program is invalid if any of these are true:
859 any two active samplers in the current program object are of
860 different types, but refer to the same texture image unit,
862 any active sampler in the current program object refers to a texture
863 image unit where fixed-function fragment processing accesses a
864 texture target that does not match the sampler type, or
866 the sum of the number of active samplers in the program and the
867 number of texture image units enabled for fixed-function fragment
868 processing exceeds the combined limit on the total number of texture
874 * Check: any two active samplers in the current program object are of
875 * different types, but refer to the same texture image unit,
877 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
885 * Called via glValidateProgram()
888 validate_program(struct gl_context
*ctx
, GLuint program
)
890 struct gl_shader_program
*shProg
;
891 char errMsg
[100] = "";
893 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
898 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
899 if (!shProg
->Validated
) {
900 /* update info log */
901 if (shProg
->InfoLog
) {
902 ralloc_free(shProg
->InfoLog
);
904 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
911 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
913 GET_CURRENT_CONTEXT(ctx
);
914 attach_shader(ctx
, program
, shader
);
919 _mesa_AttachShader(GLuint program
, GLuint shader
)
921 GET_CURRENT_CONTEXT(ctx
);
922 attach_shader(ctx
, program
, shader
);
927 _mesa_CompileShaderARB(GLhandleARB shaderObj
)
929 GET_CURRENT_CONTEXT(ctx
);
930 if (MESA_VERBOSE
& VERBOSE_API
)
931 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
932 compile_shader(ctx
, shaderObj
);
937 _mesa_CreateShader(GLenum type
)
939 GET_CURRENT_CONTEXT(ctx
);
940 if (MESA_VERBOSE
& VERBOSE_API
)
941 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
942 return create_shader(ctx
, type
);
946 GLhandleARB GLAPIENTRY
947 _mesa_CreateShaderObjectARB(GLenum type
)
949 GET_CURRENT_CONTEXT(ctx
);
950 return create_shader(ctx
, type
);
955 _mesa_CreateProgram(void)
957 GET_CURRENT_CONTEXT(ctx
);
958 if (MESA_VERBOSE
& VERBOSE_API
)
959 _mesa_debug(ctx
, "glCreateProgram\n");
960 return create_shader_program(ctx
);
964 GLhandleARB GLAPIENTRY
965 _mesa_CreateProgramObjectARB(void)
967 GET_CURRENT_CONTEXT(ctx
);
968 return create_shader_program(ctx
);
973 _mesa_DeleteObjectARB(GLhandleARB obj
)
975 if (MESA_VERBOSE
& VERBOSE_API
) {
976 GET_CURRENT_CONTEXT(ctx
);
977 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
981 GET_CURRENT_CONTEXT(ctx
);
982 FLUSH_VERTICES(ctx
, 0);
983 if (is_program(ctx
, obj
)) {
984 delete_shader_program(ctx
, obj
);
986 else if (is_shader(ctx
, obj
)) {
987 delete_shader(ctx
, obj
);
997 _mesa_DeleteProgram(GLuint name
)
1000 GET_CURRENT_CONTEXT(ctx
);
1001 FLUSH_VERTICES(ctx
, 0);
1002 delete_shader_program(ctx
, name
);
1008 _mesa_DeleteShader(GLuint name
)
1011 GET_CURRENT_CONTEXT(ctx
);
1012 FLUSH_VERTICES(ctx
, 0);
1013 delete_shader(ctx
, name
);
1019 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1021 GET_CURRENT_CONTEXT(ctx
);
1022 detach_shader(ctx
, program
, shader
);
1027 _mesa_DetachShader(GLuint program
, GLuint shader
)
1029 GET_CURRENT_CONTEXT(ctx
);
1030 detach_shader(ctx
, program
, shader
);
1035 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1036 GLsizei
* count
, GLhandleARB
* obj
)
1038 GET_CURRENT_CONTEXT(ctx
);
1039 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1044 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1045 GLsizei
*count
, GLuint
*obj
)
1047 GET_CURRENT_CONTEXT(ctx
);
1048 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1053 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1054 GLcharARB
* infoLog
)
1056 GET_CURRENT_CONTEXT(ctx
);
1057 if (is_program(ctx
, object
)) {
1058 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1060 else if (is_shader(ctx
, object
)) {
1061 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1064 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1070 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1072 GET_CURRENT_CONTEXT(ctx
);
1073 /* Implement in terms of GetProgramiv, GetShaderiv */
1074 if (is_program(ctx
, object
)) {
1075 if (pname
== GL_OBJECT_TYPE_ARB
) {
1076 *params
= GL_PROGRAM_OBJECT_ARB
;
1079 get_programiv(ctx
, object
, pname
, params
);
1082 else if (is_shader(ctx
, object
)) {
1083 if (pname
== GL_OBJECT_TYPE_ARB
) {
1084 *params
= GL_SHADER_OBJECT_ARB
;
1087 get_shaderiv(ctx
, object
, pname
, params
);
1091 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1097 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1100 GLint iparams
[1]; /* XXX is one element enough? */
1101 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1102 params
[0] = (GLfloat
) iparams
[0];
1107 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1109 GET_CURRENT_CONTEXT(ctx
);
1110 get_programiv(ctx
, program
, pname
, params
);
1115 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1117 GET_CURRENT_CONTEXT(ctx
);
1118 get_shaderiv(ctx
, shader
, pname
, params
);
1123 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1124 GLsizei
*length
, GLchar
*infoLog
)
1126 GET_CURRENT_CONTEXT(ctx
);
1127 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1132 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1133 GLsizei
*length
, GLchar
*infoLog
)
1135 GET_CURRENT_CONTEXT(ctx
);
1136 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1141 _mesa_GetShaderSourceARB(GLhandleARB shader
, GLsizei maxLength
,
1142 GLsizei
*length
, GLcharARB
*sourceOut
)
1144 GET_CURRENT_CONTEXT(ctx
);
1145 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1149 GLhandleARB GLAPIENTRY
1150 _mesa_GetHandleARB(GLenum pname
)
1152 GET_CURRENT_CONTEXT(ctx
);
1153 return get_handle(ctx
, pname
);
1157 GLboolean GLAPIENTRY
1158 _mesa_IsProgram(GLuint name
)
1160 GET_CURRENT_CONTEXT(ctx
);
1161 return is_program(ctx
, name
);
1165 GLboolean GLAPIENTRY
1166 _mesa_IsShader(GLuint name
)
1168 GET_CURRENT_CONTEXT(ctx
);
1169 return is_shader(ctx
, name
);
1174 _mesa_LinkProgramARB(GLhandleARB programObj
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 link_program(ctx
, programObj
);
1183 * Read shader source code from a file.
1184 * Useful for debugging to override an app's shader.
1187 read_shader(const char *fname
)
1189 const int max
= 50*1000;
1190 FILE *f
= fopen(fname
, "r");
1191 GLcharARB
*buffer
, *shader
;
1198 buffer
= (char *) malloc(max
);
1199 len
= fread(buffer
, 1, max
, f
);
1204 shader
= _mesa_strdup(buffer
);
1212 * Called via glShaderSource() and glShaderSourceARB() API functions.
1213 * Basically, concatenate the source code strings into one long string
1214 * and pass it to _mesa_shader_source().
1217 _mesa_ShaderSourceARB(GLhandleARB shaderObj
, GLsizei count
,
1218 const GLcharARB
** string
, const GLint
* length
)
1220 GET_CURRENT_CONTEXT(ctx
);
1222 GLsizei i
, totalLength
;
1226 if (!shaderObj
|| string
== NULL
) {
1227 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1232 * This array holds offsets of where the appropriate string ends, thus the
1233 * last element will be set to the total length of the source code.
1235 offsets
= (GLint
*) malloc(count
* sizeof(GLint
));
1236 if (offsets
== NULL
) {
1237 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1241 for (i
= 0; i
< count
; i
++) {
1242 if (string
[i
] == NULL
) {
1243 free((GLvoid
*) offsets
);
1244 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1245 "glShaderSourceARB(null string)");
1248 if (length
== NULL
|| length
[i
] < 0)
1249 offsets
[i
] = strlen(string
[i
]);
1251 offsets
[i
] = length
[i
];
1252 /* accumulate string lengths */
1254 offsets
[i
] += offsets
[i
- 1];
1257 /* Total length of source string is sum off all strings plus two.
1258 * One extra byte for terminating zero, another extra byte to silence
1259 * valgrind warnings in the parser/grammer code.
1261 totalLength
= offsets
[count
- 1] + 2;
1262 source
= (GLcharARB
*) malloc(totalLength
* sizeof(GLcharARB
));
1263 if (source
== NULL
) {
1264 free((GLvoid
*) offsets
);
1265 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1269 for (i
= 0; i
< count
; i
++) {
1270 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1271 memcpy(source
+ start
, string
[i
],
1272 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1274 source
[totalLength
- 1] = '\0';
1275 source
[totalLength
- 2] = '\0';
1278 /* Compute the shader's source code checksum then try to open a file
1279 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1280 * original shader source code. For debugging.
1283 GLcharARB
*newSource
;
1285 checksum
= _mesa_str_checksum(source
);
1287 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1289 newSource
= read_shader(filename
);
1291 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1292 shaderObj
, checksum
, filename
);
1298 shader_source(ctx
, shaderObj
, source
);
1301 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1303 sh
->SourceChecksum
= checksum
; /* save original checksum */
1311 _mesa_UseProgramObjectARB(GLhandleARB program
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 struct gl_shader_program
*shProg
;
1316 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1319 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1323 if (!shProg
->LinkStatus
) {
1324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1325 "glUseProgram(program %u not linked)", program
);
1330 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1331 print_shader_info(shProg
);
1338 _mesa_use_program(ctx
, shProg
);
1343 _mesa_ValidateProgramARB(GLhandleARB program
)
1345 GET_CURRENT_CONTEXT(ctx
);
1346 validate_program(ctx
, program
);
1351 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1352 struct gl_shader_program
*shProg
)
1354 use_shader_program(ctx
, type
, shProg
);
1356 if (ctx
->Driver
.UseProgram
)
1357 ctx
->Driver
.UseProgram(ctx
, shProg
);
1361 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1363 GET_CURRENT_CONTEXT(ctx
);
1364 struct gl_shader_program
*shProg
= NULL
;
1366 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1368 if (!validate_shader_target(ctx
, type
)) {
1369 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1374 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1375 "glUseShaderProgramEXT");
1379 if (!shProg
->LinkStatus
) {
1380 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1381 "glUseShaderProgramEXT(program not linked)");
1386 _mesa_use_shader_program(ctx
, type
, shProg
);
1390 _mesa_ActiveProgramEXT(GLuint program
)
1392 GET_CURRENT_CONTEXT(ctx
);
1393 struct gl_shader_program
*shProg
= (program
!= 0)
1394 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1397 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1402 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1404 GET_CURRENT_CONTEXT(ctx
);
1405 const GLuint shader
= create_shader(ctx
, type
);
1409 shader_source(ctx
, shader
, _mesa_strdup(string
));
1410 compile_shader(ctx
, shader
);
1412 program
= create_shader_program(ctx
);
1414 struct gl_shader_program
*shProg
;
1415 struct gl_shader
*sh
;
1416 GLint compiled
= GL_FALSE
;
1418 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1419 sh
= _mesa_lookup_shader(ctx
, shader
);
1421 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1423 attach_shader(ctx
, program
, shader
);
1424 link_program(ctx
, program
);
1425 detach_shader(ctx
, program
, shader
);
1429 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1430 append
-error
-to
-info
-log
;
1431 shProg
->LinkStatus
= GL_FALSE
;
1436 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1439 delete_shader(ctx
, shader
);
1446 * Plug in shader-related functions into API dispatch table.
1449 _mesa_init_shader_dispatch(struct _glapi_table
*exec
)
1452 /* GL_ARB_vertex/fragment_shader */
1453 SET_DeleteObjectARB(exec
, _mesa_DeleteObjectARB
);
1454 SET_GetHandleARB(exec
, _mesa_GetHandleARB
);
1455 SET_DetachObjectARB(exec
, _mesa_DetachObjectARB
);
1456 SET_CreateShaderObjectARB(exec
, _mesa_CreateShaderObjectARB
);
1457 SET_ShaderSourceARB(exec
, _mesa_ShaderSourceARB
);
1458 SET_CompileShaderARB(exec
, _mesa_CompileShaderARB
);
1459 SET_CreateProgramObjectARB(exec
, _mesa_CreateProgramObjectARB
);
1460 SET_AttachObjectARB(exec
, _mesa_AttachObjectARB
);
1461 SET_LinkProgramARB(exec
, _mesa_LinkProgramARB
);
1462 SET_UseProgramObjectARB(exec
, _mesa_UseProgramObjectARB
);
1463 SET_ValidateProgramARB(exec
, _mesa_ValidateProgramARB
);
1464 SET_GetObjectParameterfvARB(exec
, _mesa_GetObjectParameterfvARB
);
1465 SET_GetObjectParameterivARB(exec
, _mesa_GetObjectParameterivARB
);
1466 SET_GetInfoLogARB(exec
, _mesa_GetInfoLogARB
);
1467 SET_GetAttachedObjectsARB(exec
, _mesa_GetAttachedObjectsARB
);
1468 SET_GetShaderSourceARB(exec
, _mesa_GetShaderSourceARB
);
1471 SET_AttachShader(exec
, _mesa_AttachShader
);
1472 SET_CreateProgram(exec
, _mesa_CreateProgram
);
1473 SET_CreateShader(exec
, _mesa_CreateShader
);
1474 SET_DeleteProgram(exec
, _mesa_DeleteProgram
);
1475 SET_DeleteShader(exec
, _mesa_DeleteShader
);
1476 SET_DetachShader(exec
, _mesa_DetachShader
);
1477 SET_GetAttachedShaders(exec
, _mesa_GetAttachedShaders
);
1478 SET_GetProgramiv(exec
, _mesa_GetProgramiv
);
1479 SET_GetProgramInfoLog(exec
, _mesa_GetProgramInfoLog
);
1480 SET_GetShaderiv(exec
, _mesa_GetShaderiv
);
1481 SET_GetShaderInfoLog(exec
, _mesa_GetShaderInfoLog
);
1482 SET_IsProgram(exec
, _mesa_IsProgram
);
1483 SET_IsShader(exec
, _mesa_IsShader
);
1485 #if FEATURE_ARB_vertex_shader
1486 SET_BindAttribLocationARB(exec
, _mesa_BindAttribLocationARB
);
1487 SET_GetActiveAttribARB(exec
, _mesa_GetActiveAttribARB
);
1488 SET_GetAttribLocationARB(exec
, _mesa_GetAttribLocationARB
);
1491 SET_UseShaderProgramEXT(exec
, _mesa_UseShaderProgramEXT
);
1492 SET_ActiveProgramEXT(exec
, _mesa_ActiveProgramEXT
);
1493 SET_CreateShaderProgramEXT(exec
, _mesa_CreateShaderProgramEXT
);
1495 /* GL_EXT_gpu_shader4 / GL 3.0 */
1496 SET_BindFragDataLocationEXT(exec
, _mesa_BindFragDataLocation
);
1497 SET_GetFragDataLocationEXT(exec
, _mesa_GetFragDataLocation
);
1499 #endif /* FEATURE_GL */