2 * Mesa 3-D graphics library
5 * Copyright (C) 2004-2007 Brian Paul 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.
27 * Implementation of GLSL-related API functions
33 * 1. Check that the right error code is generated for all _mesa_error() calls.
34 * 2. Insert FLUSH_VERTICES calls in various places
38 #include "main/glheader.h"
39 #include "main/context.h"
40 #include "main/hash.h"
41 #include "main/macros.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_statevars.h"
46 #include "prog_uniform.h"
47 #include "shader/shader_api.h"
48 #include "shader/slang/slang_compile.h"
49 #include "shader/slang/slang_link.h"
54 * Allocate a new gl_shader_program object, initialize it.
56 static struct gl_shader_program
*
57 _mesa_new_shader_program(GLcontext
*ctx
, GLuint name
)
59 struct gl_shader_program
*shProg
;
60 shProg
= CALLOC_STRUCT(gl_shader_program
);
62 shProg
->Type
= GL_SHADER_PROGRAM_MESA
;
65 shProg
->Attributes
= _mesa_new_parameter_list();
72 * Clear (free) the shader program state that gets produced by linking.
75 _mesa_clear_shader_program_data(GLcontext
*ctx
,
76 struct gl_shader_program
*shProg
)
78 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
79 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
81 if (shProg
->Uniforms
) {
82 _mesa_free_uniform_list(shProg
->Uniforms
);
83 shProg
->Uniforms
= NULL
;
86 if (shProg
->Varying
) {
87 _mesa_free_parameter_list(shProg
->Varying
);
88 shProg
->Varying
= NULL
;
94 * Free all the data that hangs off a shader program object, but not the
98 _mesa_free_shader_program_data(GLcontext
*ctx
,
99 struct gl_shader_program
*shProg
)
103 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
105 _mesa_clear_shader_program_data(ctx
, shProg
);
107 if (shProg
->Attributes
) {
108 _mesa_free_parameter_list(shProg
->Attributes
);
109 shProg
->Attributes
= NULL
;
113 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
114 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
116 shProg
->NumShaders
= 0;
118 if (shProg
->Shaders
) {
119 _mesa_free(shProg
->Shaders
);
120 shProg
->Shaders
= NULL
;
123 if (shProg
->InfoLog
) {
124 _mesa_free(shProg
->InfoLog
);
125 shProg
->InfoLog
= NULL
;
131 * Free/delete a shader program object.
134 _mesa_free_shader_program(GLcontext
*ctx
, struct gl_shader_program
*shProg
)
136 _mesa_free_shader_program_data(ctx
, shProg
);
143 * Set ptr to point to shProg.
144 * If ptr is pointing to another object, decrement its refcount (and delete
145 * if refcount hits zero).
146 * Then set ptr to point to shProg, incrementing its refcount.
148 /* XXX this could be static */
150 _mesa_reference_shader_program(GLcontext
*ctx
,
151 struct gl_shader_program
**ptr
,
152 struct gl_shader_program
*shProg
)
155 if (*ptr
== shProg
) {
160 /* Unreference the old shader program */
161 GLboolean deleteFlag
= GL_FALSE
;
162 struct gl_shader_program
*old
= *ptr
;
164 ASSERT(old
->RefCount
> 0);
167 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
168 (void *) old
, old
->Name
, old
->RefCount
);
170 deleteFlag
= (old
->RefCount
== 0);
173 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
174 _mesa_free_shader_program(ctx
, old
);
184 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
185 (void *) shProg
, shProg
->Name
, shProg
->RefCount
);
193 * Lookup a GLSL program object.
195 struct gl_shader_program
*
196 _mesa_lookup_shader_program(GLcontext
*ctx
, GLuint name
)
198 struct gl_shader_program
*shProg
;
200 shProg
= (struct gl_shader_program
*)
201 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
202 /* Note that both gl_shader and gl_shader_program objects are kept
203 * in the same hash table. Check the object's type to be sure it's
204 * what we're expecting.
206 if (shProg
&& shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
216 * As above, but record an error if program is not found.
218 static struct gl_shader_program
*
219 _mesa_lookup_shader_program_err(GLcontext
*ctx
, GLuint name
,
223 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
227 struct gl_shader_program
*shProg
= (struct gl_shader_program
*)
228 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
230 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
233 if (shProg
->Type
!= GL_SHADER_PROGRAM_MESA
) {
234 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
245 * Allocate a new gl_shader object, initialize it.
248 _mesa_new_shader(GLcontext
*ctx
, GLuint name
, GLenum type
)
250 struct gl_shader
*shader
;
251 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
);
252 shader
= CALLOC_STRUCT(gl_shader
);
256 shader
->RefCount
= 1;
263 _mesa_free_shader(GLcontext
*ctx
, struct gl_shader
*sh
)
267 _mesa_free((void *) sh
->Source
);
269 _mesa_free(sh
->InfoLog
);
270 for (i
= 0; i
< sh
->NumPrograms
; i
++)
271 _mesa_reference_program(ctx
, &sh
->Programs
[i
], NULL
);
273 _mesa_free(sh
->Programs
);
279 * Set ptr to point to sh.
280 * If ptr is pointing to another shader, decrement its refcount (and delete
281 * if refcount hits zero).
282 * Then set ptr to point to sh, incrementing its refcount.
284 /* XXX this could be static */
286 _mesa_reference_shader(GLcontext
*ctx
, struct gl_shader
**ptr
,
287 struct gl_shader
*sh
)
295 /* Unreference the old shader */
296 GLboolean deleteFlag
= GL_FALSE
;
297 struct gl_shader
*old
= *ptr
;
299 ASSERT(old
->RefCount
> 0);
301 /*printf("SHADER DECR %p (%d) to %d\n",
302 (void*) old, old->Name, old->RefCount);*/
303 deleteFlag
= (old
->RefCount
== 0);
306 _mesa_HashRemove(ctx
->Shared
->ShaderObjects
, old
->Name
);
307 _mesa_free_shader(ctx
, old
);
317 /*printf("SHADER INCR %p (%d) to %d\n",
318 (void*) sh, sh->Name, sh->RefCount);*/
325 * Lookup a GLSL shader object.
328 _mesa_lookup_shader(GLcontext
*ctx
, GLuint name
)
331 struct gl_shader
*sh
= (struct gl_shader
*)
332 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
333 /* Note that both gl_shader and gl_shader_program objects are kept
334 * in the same hash table. Check the object's type to be sure it's
335 * what we're expecting.
337 if (sh
&& sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
347 * As above, but record an error if shader is not found.
349 static struct gl_shader
*
350 _mesa_lookup_shader_err(GLcontext
*ctx
, GLuint name
, const char *caller
)
353 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
357 struct gl_shader
*sh
= (struct gl_shader
*)
358 _mesa_HashLookup(ctx
->Shared
->ShaderObjects
, name
);
360 _mesa_error(ctx
, GL_INVALID_VALUE
, caller
);
363 if (sh
->Type
== GL_SHADER_PROGRAM_MESA
) {
364 _mesa_error(ctx
, GL_INVALID_OPERATION
, caller
);
374 * Initialize context's shader state.
377 _mesa_init_shader_state(GLcontext
* ctx
)
379 /* Device drivers may override these to control what kind of instructions
380 * are generated by the GLSL compiler.
382 ctx
->Shader
.EmitHighLevelInstructions
= GL_TRUE
;
383 ctx
->Shader
.EmitCondCodes
= GL_FALSE
;/*GL_TRUE;*/ /* XXX probably want GL_FALSE... */
384 ctx
->Shader
.EmitComments
= GL_FALSE
;
389 * Free the per-context shader-related state.
392 _mesa_free_shader_state(GLcontext
*ctx
)
394 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, NULL
);
399 * Copy string from <src> to <dst>, up to maxLength characters, returning
400 * length of <dst> in <length>.
401 * \param src the strings source
402 * \param maxLength max chars to copy
403 * \param length returns number of chars copied
404 * \param dst the string destination
407 copy_string(GLchar
*dst
, GLsizei maxLength
, GLsizei
*length
, const GLchar
*src
)
410 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
420 _mesa_is_program(GLcontext
*ctx
, GLuint name
)
422 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
423 return shProg
? GL_TRUE
: GL_FALSE
;
428 _mesa_is_shader(GLcontext
*ctx
, GLuint name
)
430 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
431 return shader
? GL_TRUE
: GL_FALSE
;
436 * Called via ctx->Driver.AttachShader()
439 _mesa_attach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
441 struct gl_shader_program
*shProg
;
442 struct gl_shader
*sh
;
445 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
449 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
454 n
= shProg
->NumShaders
;
455 for (i
= 0; i
< n
; i
++) {
456 if (shProg
->Shaders
[i
] == sh
) {
457 /* already attached */
463 shProg
->Shaders
= (struct gl_shader
**)
464 _mesa_realloc(shProg
->Shaders
,
465 n
* sizeof(struct gl_shader
*),
466 (n
+ 1) * sizeof(struct gl_shader
*));
467 if (!shProg
->Shaders
) {
468 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
473 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
474 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
475 shProg
->NumShaders
++;
480 _mesa_get_attrib_location(GLcontext
*ctx
, GLuint program
,
483 struct gl_shader_program
*shProg
484 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
490 if (!shProg
->LinkStatus
) {
491 _mesa_error(ctx
, GL_INVALID_OPERATION
,
492 "glGetAttribLocation(program not linked)");
499 if (shProg
->Attributes
) {
500 GLint i
= _mesa_lookup_parameter_index(shProg
->Attributes
, -1, name
);
502 return shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
510 _mesa_bind_attrib_location(GLcontext
*ctx
, GLuint program
, GLuint index
,
513 struct gl_shader_program
*shProg
;
514 const GLint size
= -1; /* unknown size */
518 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
519 "glBindAttribLocation");
527 if (strncmp(name
, "gl_", 3) == 0) {
528 _mesa_error(ctx
, GL_INVALID_OPERATION
,
529 "glBindAttribLocation(illegal name)");
533 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
534 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
538 if (shProg
->LinkStatus
) {
539 /* get current index/location for the attribute */
540 oldIndex
= _mesa_get_attrib_location(ctx
, program
, name
);
546 /* this will replace the current value if it's already in the list */
547 i
= _mesa_add_attribute(shProg
->Attributes
, name
, size
, datatype
, index
);
549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindAttribLocation");
552 if (shProg
->VertexProgram
&& oldIndex
>= 0 && oldIndex
!= index
) {
553 /* If the index changed, need to search/replace references to that attribute
554 * in the vertex program.
556 _slang_remap_attribute(&shProg
->VertexProgram
->Base
, oldIndex
, index
);
562 _mesa_create_shader(GLcontext
*ctx
, GLenum type
)
564 struct gl_shader
*sh
;
567 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
570 case GL_FRAGMENT_SHADER
:
571 case GL_VERTEX_SHADER
:
572 sh
= _mesa_new_shader(ctx
, name
, type
);
575 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
579 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
586 _mesa_create_program(GLcontext
*ctx
)
589 struct gl_shader_program
*shProg
;
591 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
592 shProg
= _mesa_new_shader_program(ctx
, name
);
594 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
596 assert(shProg
->RefCount
== 1);
603 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
607 _mesa_delete_program2(GLcontext
*ctx
, GLuint name
)
610 * NOTE: deleting shaders/programs works a bit differently than
611 * texture objects (and buffer objects, etc). Shader/program
612 * handles/IDs exist in the hash table until the object is really
613 * deleted (refcount==0). With texture objects, the handle/ID is
614 * removed from the hash table in glDeleteTextures() while the tex
615 * object itself might linger until its refcount goes to zero.
617 struct gl_shader_program
*shProg
;
619 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
623 shProg
->DeletePending
= GL_TRUE
;
625 /* effectively, decr shProg's refcount */
626 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
631 _mesa_delete_shader(GLcontext
*ctx
, GLuint shader
)
633 struct gl_shader
*sh
;
635 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
639 sh
->DeletePending
= GL_TRUE
;
641 /* effectively, decr sh's refcount */
642 _mesa_reference_shader(ctx
, &sh
, NULL
);
647 _mesa_detach_shader(GLcontext
*ctx
, GLuint program
, GLuint shader
)
649 struct gl_shader_program
*shProg
;
653 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
657 n
= shProg
->NumShaders
;
659 for (i
= 0; i
< n
; i
++) {
660 if (shProg
->Shaders
[i
]->Name
== shader
) {
662 struct gl_shader
**newList
;
665 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
667 /* alloc new, smaller array */
668 newList
= (struct gl_shader
**)
669 _mesa_malloc((n
- 1) * sizeof(struct gl_shader
*));
671 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
674 for (j
= 0; j
< i
; j
++) {
675 newList
[j
] = shProg
->Shaders
[j
];
678 newList
[j
++] = shProg
->Shaders
[i
];
679 _mesa_free(shProg
->Shaders
);
681 shProg
->Shaders
= newList
;
682 shProg
->NumShaders
= n
- 1;
687 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
688 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
689 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
690 assert(shProg
->Shaders
[j
]->RefCount
> 0);
702 if (_mesa_is_shader(ctx
, shader
))
703 err
= GL_INVALID_OPERATION
;
704 else if (_mesa_is_program(ctx
, shader
))
705 err
= GL_INVALID_OPERATION
;
707 err
= GL_INVALID_VALUE
;
708 _mesa_error(ctx
, err
, "glDetachProgram(shader)");
715 sizeof_glsl_type(GLenum type
)
735 case GL_FLOAT_MAT2x3
:
736 case GL_FLOAT_MAT2x4
:
737 return 8; /* two float[4] vectors */
739 case GL_FLOAT_MAT3x2
:
740 case GL_FLOAT_MAT3x4
:
741 return 12; /* three float[4] vectors */
743 case GL_FLOAT_MAT4x2
:
744 case GL_FLOAT_MAT4x3
:
745 return 16; /* four float[4] vectors */
747 _mesa_problem(NULL
, "Invalid type in sizeof_glsl_type()");
754 _mesa_get_active_attrib(GLcontext
*ctx
, GLuint program
, GLuint index
,
755 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
756 GLenum
*type
, GLchar
*nameOut
)
758 struct gl_shader_program
*shProg
;
760 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
764 if (!shProg
->Attributes
|| index
>= shProg
->Attributes
->NumParameters
) {
765 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
769 copy_string(nameOut
, maxLength
, length
,
770 shProg
->Attributes
->Parameters
[index
].Name
);
772 *size
= shProg
->Attributes
->Parameters
[index
].Size
773 / sizeof_glsl_type(shProg
->Attributes
->Parameters
[index
].DataType
);
775 *type
= shProg
->Attributes
->Parameters
[index
].DataType
;
780 * Called via ctx->Driver.GetActiveUniform().
783 _mesa_get_active_uniform(GLcontext
*ctx
, GLuint program
, GLuint index
,
784 GLsizei maxLength
, GLsizei
*length
, GLint
*size
,
785 GLenum
*type
, GLchar
*nameOut
)
787 const struct gl_shader_program
*shProg
;
788 const struct gl_program
*prog
;
791 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveUniform");
795 if (!shProg
->Uniforms
|| index
>= shProg
->Uniforms
->NumUniforms
) {
796 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveUniform(index)");
800 progPos
= shProg
->Uniforms
->Uniforms
[index
].VertPos
;
802 prog
= &shProg
->VertexProgram
->Base
;
805 progPos
= shProg
->Uniforms
->Uniforms
[index
].FragPos
;
807 prog
= &shProg
->FragmentProgram
->Base
;
811 if (!prog
|| progPos
< 0)
812 return; /* should never happen */
815 copy_string(nameOut
, maxLength
, length
,
816 prog
->Parameters
->Parameters
[progPos
].Name
);
818 *size
= prog
->Parameters
->Parameters
[progPos
].Size
819 / sizeof_glsl_type(prog
->Parameters
->Parameters
[progPos
].DataType
);
821 *type
= prog
->Parameters
->Parameters
[progPos
].DataType
;
826 * Called via ctx->Driver.GetAttachedShaders().
829 _mesa_get_attached_shaders(GLcontext
*ctx
, GLuint program
, GLsizei maxCount
,
830 GLsizei
*count
, GLuint
*obj
)
832 struct gl_shader_program
*shProg
=
833 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
836 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
837 obj
[i
] = shProg
->Shaders
[i
]->Name
;
846 _mesa_get_handle(GLcontext
*ctx
, GLenum pname
)
849 GET_CURRENT_CONTEXT(ctx
);
852 case GL_PROGRAM_OBJECT_ARB
:
854 struct gl2_program_intf
**pro
= ctx
->Shader
.CurrentProgram
;
857 return (**pro
)._container
._generic
.
858 GetName((struct gl2_generic_intf
**) (pro
));
862 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
870 _mesa_get_programiv(GLcontext
*ctx
, GLuint program
,
871 GLenum pname
, GLint
*params
)
873 struct gl_shader_program
*shProg
874 = _mesa_lookup_shader_program(ctx
, program
);
877 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
882 case GL_DELETE_STATUS
:
883 *params
= shProg
->DeletePending
;
886 *params
= shProg
->LinkStatus
;
888 case GL_VALIDATE_STATUS
:
889 *params
= shProg
->Validated
;
891 case GL_INFO_LOG_LENGTH
:
892 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
894 case GL_ATTACHED_SHADERS
:
895 *params
= shProg
->NumShaders
;
897 case GL_ACTIVE_ATTRIBUTES
:
898 *params
= shProg
->Attributes
? shProg
->Attributes
->NumParameters
: 0;
900 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
901 *params
= _mesa_longest_parameter_name(shProg
->Attributes
,
904 case GL_ACTIVE_UNIFORMS
:
905 *params
= shProg
->Uniforms
? shProg
->Uniforms
->NumUniforms
: 0;
907 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
908 *params
= _mesa_longest_uniform_name(shProg
->Uniforms
);
910 (*params
)++; /* add one for terminating zero */
913 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname)");
920 _mesa_get_shaderiv(GLcontext
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
922 struct gl_shader
*shader
= _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
930 *params
= shader
->Type
;
932 case GL_DELETE_STATUS
:
933 *params
= shader
->DeletePending
;
935 case GL_COMPILE_STATUS
:
936 *params
= shader
->CompileStatus
;
938 case GL_INFO_LOG_LENGTH
:
939 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
941 case GL_SHADER_SOURCE_LENGTH
:
942 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
945 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
952 _mesa_get_program_info_log(GLcontext
*ctx
, GLuint program
, GLsizei bufSize
,
953 GLsizei
*length
, GLchar
*infoLog
)
955 struct gl_shader_program
*shProg
956 = _mesa_lookup_shader_program(ctx
, program
);
958 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
961 copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
966 _mesa_get_shader_info_log(GLcontext
*ctx
, GLuint shader
, GLsizei bufSize
,
967 GLsizei
*length
, GLchar
*infoLog
)
969 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
971 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
974 copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
979 * Called via ctx->Driver.GetShaderSource().
982 _mesa_get_shader_source(GLcontext
*ctx
, GLuint shader
, GLsizei maxLength
,
983 GLsizei
*length
, GLchar
*sourceOut
)
985 struct gl_shader
*sh
;
986 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
990 copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
994 #define MAX_UNIFORM_ELEMENTS 16
997 * Helper for GetUniformfv(), GetUniformiv()
998 * Returns number of elements written to 'params' output.
1001 get_uniformfv(GLcontext
*ctx
, GLuint program
, GLint location
,
1004 struct gl_shader_program
*shProg
1005 = _mesa_lookup_shader_program(ctx
, program
);
1007 if (shProg
->Uniforms
&&
1008 location
>= 0 && location
< (GLint
) shProg
->Uniforms
->NumUniforms
) {
1011 const struct gl_program
*prog
= NULL
;
1013 progPos
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
1015 prog
= &shProg
->VertexProgram
->Base
;
1018 progPos
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
1020 prog
= &shProg
->FragmentProgram
->Base
;
1026 /* See uniformiv() below */
1027 assert(prog
->Parameters
->Parameters
[progPos
].Size
<= MAX_UNIFORM_ELEMENTS
);
1029 for (i
= 0; i
< prog
->Parameters
->Parameters
[progPos
].Size
; i
++) {
1030 params
[i
] = prog
->Parameters
->ParameterValues
[progPos
][i
];
1032 return prog
->Parameters
->Parameters
[progPos
].Size
;
1036 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(location)");
1040 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
1047 * Called via ctx->Driver.GetUniformfv().
1050 _mesa_get_uniformfv(GLcontext
*ctx
, GLuint program
, GLint location
,
1053 (void) get_uniformfv(ctx
, program
, location
, params
);
1058 * Called via ctx->Driver.GetUniformiv().
1061 _mesa_get_uniformiv(GLcontext
*ctx
, GLuint program
, GLint location
,
1064 GLfloat fparams
[MAX_UNIFORM_ELEMENTS
];
1065 GLuint n
= get_uniformfv(ctx
, program
, location
, fparams
);
1067 assert(n
<= MAX_UNIFORM_ELEMENTS
);
1068 for (i
= 0; i
< n
; i
++) {
1069 params
[i
] = (GLint
) fparams
[i
];
1075 * Called via ctx->Driver.GetUniformLocation().
1078 _mesa_get_uniform_location(GLcontext
*ctx
, GLuint program
, const GLchar
*name
)
1080 struct gl_shader_program
*shProg
=
1081 _mesa_lookup_shader_program_err(ctx
, program
, "glGetUniformLocation");
1086 if (shProg
->LinkStatus
== GL_FALSE
) {
1087 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformfv(program)");
1091 /* XXX we should return -1 if the uniform was declared, but not
1095 return _mesa_lookup_uniform(shProg
->Uniforms
, name
);
1101 * Called via ctx->Driver.ShaderSource()
1104 _mesa_shader_source(GLcontext
*ctx
, GLuint shader
, const GLchar
*source
)
1106 struct gl_shader
*sh
;
1108 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
1112 /* free old shader source string and install new one */
1114 _mesa_free((void *) sh
->Source
);
1116 sh
->Source
= source
;
1117 sh
->CompileStatus
= GL_FALSE
;
1122 * Called via ctx->Driver.CompileShader()
1125 _mesa_compile_shader(GLcontext
*ctx
, GLuint shaderObj
)
1127 struct gl_shader
*sh
;
1129 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
1133 sh
->CompileStatus
= _slang_compile(ctx
, sh
);
1138 * Called via ctx->Driver.LinkProgram()
1141 _mesa_link_program(GLcontext
*ctx
, GLuint program
)
1143 struct gl_shader_program
*shProg
;
1145 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
1149 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1151 _slang_link(ctx
, program
, shProg
);
1156 * Called via ctx->Driver.UseProgram()
1159 _mesa_use_program(GLcontext
*ctx
, GLuint program
)
1161 struct gl_shader_program
*shProg
;
1163 if (ctx
->Shader
.CurrentProgram
&&
1164 ctx
->Shader
.CurrentProgram
->Name
== program
) {
1169 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1172 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1176 if (!shProg
->LinkStatus
) {
1177 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUseProgram");
1185 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
, shProg
);
1191 * Update the vertex and fragment program's TexturesUsed arrays.
1194 update_textures_used(struct gl_program
*prog
)
1198 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
1200 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
1201 if (prog
->SamplersUsed
& (1 << s
)) {
1202 GLuint u
= prog
->SamplerUnits
[s
];
1203 GLuint t
= prog
->SamplerTargets
[s
];
1204 assert(u
< MAX_TEXTURE_IMAGE_UNITS
);
1205 prog
->TexturesUsed
[u
] |= (1 << t
);
1212 is_sampler_type(GLenum type
)
1218 case GL_SAMPLER_CUBE
:
1219 case GL_SAMPLER_1D_SHADOW
:
1220 case GL_SAMPLER_2D_SHADOW
:
1221 case GL_SAMPLER_2D_RECT_ARB
:
1222 case GL_SAMPLER_2D_RECT_SHADOW_ARB
:
1223 case GL_SAMPLER_1D_ARRAY_EXT
:
1224 case GL_SAMPLER_2D_ARRAY_EXT
:
1233 * Check if the type given by userType is allowed to set a uniform of the
1234 * target type. Generally, equivalence is required, but setting Boolean
1235 * uniforms can be done with glUniformiv or glUniformfv.
1238 compatible_types(GLenum userType
, GLenum targetType
)
1240 if (userType
== targetType
)
1243 if (targetType
== GL_BOOL
&& (userType
== GL_FLOAT
|| userType
== GL_INT
))
1246 if (targetType
== GL_BOOL_VEC2
&& (userType
== GL_FLOAT_VEC2
||
1247 userType
== GL_INT_VEC2
))
1250 if (targetType
== GL_BOOL_VEC3
&& (userType
== GL_FLOAT_VEC3
||
1251 userType
== GL_INT_VEC3
))
1254 if (targetType
== GL_BOOL_VEC4
&& (userType
== GL_FLOAT_VEC4
||
1255 userType
== GL_INT_VEC4
))
1258 if (is_sampler_type(targetType
) && userType
== GL_INT
)
1266 * Set the value of a program's uniform variable.
1267 * \param program the program whose uniform to update
1268 * \param location the location/index of the uniform
1269 * \param type the datatype of the uniform
1270 * \param count the number of uniforms to set
1271 * \param elems number of elements per uniform
1272 * \param values the new values
1275 set_program_uniform(GLcontext
*ctx
, struct gl_program
*program
, GLint location
,
1276 GLenum type
, GLsizei count
, GLint elems
, const void *values
)
1278 if (!compatible_types(type
,
1279 program
->Parameters
->Parameters
[location
].DataType
)) {
1280 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(type mismatch)");
1284 if (program
->Parameters
->Parameters
[location
].Type
== PROGRAM_SAMPLER
) {
1285 /* This controls which texture unit which is used by a sampler */
1286 GLuint texUnit
, sampler
;
1288 /* data type for setting samplers must be int */
1289 if (type
!= GL_INT
|| count
!= 1) {
1290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1291 "glUniform(only glUniform1i can be used "
1292 "to set sampler uniforms)");
1296 sampler
= (GLuint
) program
->Parameters
->ParameterValues
[location
][0];
1297 texUnit
= ((GLuint
*) values
)[0];
1299 /* check that the sampler (tex unit index) is legal */
1300 if (texUnit
>= ctx
->Const
.MaxTextureImageUnits
) {
1301 _mesa_error(ctx
, GL_INVALID_VALUE
,
1302 "glUniform1(invalid sampler/tex unit index)");
1306 /* This maps a sampler to a texture unit: */
1307 program
->SamplerUnits
[sampler
] = texUnit
;
1308 update_textures_used(program
);
1310 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1313 /* ordinary uniform variable */
1316 if (count
* elems
> (GLint
) program
->Parameters
->Parameters
[location
].Size
) {
1317 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(count too large)");
1321 for (k
= 0; k
< count
; k
++) {
1322 GLfloat
*uniformVal
= program
->Parameters
->ParameterValues
[location
+ k
];
1323 if (type
== GL_INT
||
1324 type
== GL_INT_VEC2
||
1325 type
== GL_INT_VEC3
||
1326 type
== GL_INT_VEC4
) {
1327 const GLint
*iValues
= ((const GLint
*) values
) + k
* elems
;
1328 for (i
= 0; i
< elems
; i
++) {
1329 uniformVal
[i
] = (GLfloat
) iValues
[i
];
1333 const GLfloat
*fValues
= ((const GLfloat
*) values
) + k
* elems
;
1334 for (i
= 0; i
< elems
; i
++) {
1335 uniformVal
[i
] = fValues
[i
];
1344 * Called via ctx->Driver.Uniform().
1347 _mesa_uniform(GLcontext
*ctx
, GLint location
, GLsizei count
,
1348 const GLvoid
*values
, GLenum type
)
1350 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1353 if (!shProg
|| !shProg
->LinkStatus
) {
1354 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUniform(program not linked)");
1359 return; /* The standard specifies this as a no-op */
1361 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1362 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(location)");
1367 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniform(count < 0)");
1389 _mesa_problem(ctx
, "Invalid type in _mesa_uniform");
1393 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1395 /* A uniform var may be used by both a vertex shader and a fragment
1396 * shader. We may need to update one or both shader's uniform here:
1398 if (shProg
->VertexProgram
) {
1399 GLint loc
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
1401 set_program_uniform(ctx
, &shProg
->VertexProgram
->Base
,
1402 loc
, type
, count
, elems
, values
);
1406 if (shProg
->FragmentProgram
) {
1407 GLint loc
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
1409 set_program_uniform(ctx
, &shProg
->FragmentProgram
->Base
,
1410 loc
, type
, count
, elems
, values
);
1417 get_matrix_dims(GLenum type
, GLint
*rows
, GLint
*cols
)
1423 case GL_FLOAT_MAT2x3
:
1427 case GL_FLOAT_MAT2x4
:
1435 case GL_FLOAT_MAT3x2
:
1439 case GL_FLOAT_MAT3x4
:
1447 case GL_FLOAT_MAT4x2
:
1451 case GL_FLOAT_MAT4x3
:
1462 set_program_uniform_matrix(GLcontext
*ctx
, struct gl_program
*program
,
1463 GLuint location
, GLuint count
,
1464 GLuint rows
, GLuint cols
,
1465 GLboolean transpose
, const GLfloat
*values
)
1467 GLuint mat
, row
, col
;
1468 GLuint dst
= location
, src
= 0;
1471 /* check that the number of rows, columns is correct */
1472 get_matrix_dims(program
->Parameters
->Parameters
[location
].DataType
, &nr
, &nc
);
1473 if (rows
!= nr
|| cols
!= nc
) {
1474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1475 "glUniformMatrix(matrix size mismatch");
1480 * Note: the _columns_ of a matrix are stored in program registers, not
1481 * the rows. So, the loops below look a little funny.
1482 * XXX could optimize this a bit...
1485 /* loop over matrices */
1486 for (mat
= 0; mat
< count
; mat
++) {
1489 for (col
= 0; col
< cols
; col
++) {
1490 GLfloat
*v
= program
->Parameters
->ParameterValues
[dst
];
1491 for (row
= 0; row
< rows
; row
++) {
1493 v
[row
] = values
[src
+ row
* cols
+ col
];
1496 v
[row
] = values
[src
+ col
* rows
+ row
];
1502 src
+= rows
* cols
; /* next matrix */
1508 * Called by ctx->Driver.UniformMatrix().
1509 * Note: cols=2, rows=4 ==> array[2] of vec4
1512 _mesa_uniform_matrix(GLcontext
*ctx
, GLint cols
, GLint rows
,
1513 GLenum matrixType
, GLint location
, GLsizei count
,
1514 GLboolean transpose
, const GLfloat
*values
)
1516 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1518 if (!shProg
|| !shProg
->LinkStatus
) {
1519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1520 "glUniformMatrix(program not linked)");
1525 return; /* The standard specifies this as a no-op */
1527 if (location
< 0 || location
>= (GLint
) shProg
->Uniforms
->NumUniforms
) {
1528 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix(location)");
1531 if (values
== NULL
) {
1532 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUniformMatrix");
1536 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1538 if (shProg
->VertexProgram
) {
1539 GLint loc
= shProg
->Uniforms
->Uniforms
[location
].VertPos
;
1541 set_program_uniform_matrix(ctx
, &shProg
->VertexProgram
->Base
,
1542 loc
, count
, rows
, cols
, transpose
, values
);
1546 if (shProg
->FragmentProgram
) {
1547 GLint loc
= shProg
->Uniforms
->Uniforms
[location
].FragPos
;
1549 set_program_uniform_matrix(ctx
, &shProg
->FragmentProgram
->Base
,
1550 loc
, count
, rows
, cols
, transpose
, values
);
1557 _mesa_validate_program(GLcontext
*ctx
, GLuint program
)
1559 struct gl_shader_program
*shProg
;
1561 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1566 if (!shProg
->LinkStatus
) {
1567 shProg
->Validated
= GL_FALSE
;
1571 /* From the GL spec, a program is invalid if any of these are true:
1573 any two active samplers in the current program object are of
1574 different types, but refer to the same texture image unit,
1576 any active sampler in the current program object refers to a texture
1577 image unit where fixed-function fragment processing accesses a
1578 texture target that does not match the sampler type, or
1580 the sum of the number of active samplers in the program and the
1581 number of texture image units enabled for fixed-function fragment
1582 processing exceeds the combined limit on the total number of texture
1583 image units allowed.
1586 shProg
->Validated
= GL_TRUE
;
1591 * Plug in Mesa's GLSL functions into the device driver function table.
1594 _mesa_init_glsl_driver_functions(struct dd_function_table
*driver
)
1596 driver
->AttachShader
= _mesa_attach_shader
;
1597 driver
->BindAttribLocation
= _mesa_bind_attrib_location
;
1598 driver
->CompileShader
= _mesa_compile_shader
;
1599 driver
->CreateProgram
= _mesa_create_program
;
1600 driver
->CreateShader
= _mesa_create_shader
;
1601 driver
->DeleteProgram2
= _mesa_delete_program2
;
1602 driver
->DeleteShader
= _mesa_delete_shader
;
1603 driver
->DetachShader
= _mesa_detach_shader
;
1604 driver
->GetActiveAttrib
= _mesa_get_active_attrib
;
1605 driver
->GetActiveUniform
= _mesa_get_active_uniform
;
1606 driver
->GetAttachedShaders
= _mesa_get_attached_shaders
;
1607 driver
->GetAttribLocation
= _mesa_get_attrib_location
;
1608 driver
->GetHandle
= _mesa_get_handle
;
1609 driver
->GetProgramiv
= _mesa_get_programiv
;
1610 driver
->GetProgramInfoLog
= _mesa_get_program_info_log
;
1611 driver
->GetShaderiv
= _mesa_get_shaderiv
;
1612 driver
->GetShaderInfoLog
= _mesa_get_shader_info_log
;
1613 driver
->GetShaderSource
= _mesa_get_shader_source
;
1614 driver
->GetUniformfv
= _mesa_get_uniformfv
;
1615 driver
->GetUniformiv
= _mesa_get_uniformiv
;
1616 driver
->GetUniformLocation
= _mesa_get_uniform_location
;
1617 driver
->IsProgram
= _mesa_is_program
;
1618 driver
->IsShader
= _mesa_is_shader
;
1619 driver
->LinkProgram
= _mesa_link_program
;
1620 driver
->ShaderSource
= _mesa_shader_source
;
1621 driver
->Uniform
= _mesa_uniform
;
1622 driver
->UniformMatrix
= _mesa_uniform_matrix
;
1623 driver
->UseProgram
= _mesa_use_program
;
1624 driver
->ValidateProgram
= _mesa_validate_program
;