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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
30 * Implementation of GLSL-related API functions.
31 * The glUniform* functions are in uniforms.c
35 * 1. Check that the right error code is generated for all _mesa_error() calls.
36 * 2. Insert FLUSH_VERTICES calls in various places
40 #include "main/glheader.h"
41 #include "main/context.h"
42 #include "main/dispatch.h"
43 #include "main/enums.h"
44 #include "main/hash.h"
45 #include "main/hash_table.h"
46 #include "main/mtypes.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/transformfeedback.h"
50 #include "main/uniforms.h"
51 #include "program/program.h"
52 #include "program/prog_print.h"
53 #include "program/prog_parameter.h"
56 #include "../glsl/glsl_parser_extras.h"
57 #include "../glsl/ir.h"
58 #include "../glsl/ir_uniform.h"
59 #include "../glsl/program.h"
61 /** Define this to enable shader substitution (see below) */
62 #define SHADER_SUBST 0
66 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
69 _mesa_get_shader_flags(void)
71 GLbitfield flags
= 0x0;
72 const char *env
= _mesa_getenv("MESA_GLSL");
75 if (strstr(env
, "dump_on_error"))
76 flags
|= GLSL_DUMP_ON_ERROR
;
77 else if (strstr(env
, "dump"))
79 if (strstr(env
, "log"))
81 if (strstr(env
, "nopvert"))
82 flags
|= GLSL_NOP_VERT
;
83 if (strstr(env
, "nopfrag"))
84 flags
|= GLSL_NOP_FRAG
;
85 if (strstr(env
, "nopt"))
87 else if (strstr(env
, "opt"))
89 if (strstr(env
, "uniform"))
90 flags
|= GLSL_UNIFORMS
;
91 if (strstr(env
, "useprog"))
92 flags
|= GLSL_USE_PROG
;
93 if (strstr(env
, "errors"))
94 flags
|= GLSL_REPORT_ERRORS
;
102 * Initialize context's shader state.
105 _mesa_init_shader_state(struct gl_context
*ctx
)
107 /* Device drivers may override these to control what kind of instructions
108 * are generated by the GLSL compiler.
110 struct gl_shader_compiler_options options
;
113 memset(&options
, 0, sizeof(options
));
114 options
.MaxUnrollIterations
= 32;
115 options
.MaxIfDepth
= UINT_MAX
;
117 /* Default pragma settings */
118 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
120 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
121 memcpy(&ctx
->ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
123 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
125 /* Extended for ARB_separate_shader_objects */
126 ctx
->Shader
.RefCount
= 1;
127 _glthread_INIT_MUTEX(ctx
->Shader
.Mutex
);
132 * Free the per-context shader-related state.
135 _mesa_free_shader_state(struct gl_context
*ctx
)
138 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
139 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
142 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
144 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
146 /* Extended for ARB_separate_shader_objects */
147 assert(ctx
->Shader
.RefCount
== 1);
148 _glthread_DESTROY_MUTEX(ctx
->Shader
.Mutex
);
153 * Copy string from <src> to <dst>, up to maxLength characters, returning
154 * length of <dst> in <length>.
155 * \param src the strings source
156 * \param maxLength max chars to copy
157 * \param length returns number of chars copied
158 * \param dst the string destination
161 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
162 GLsizei
*length
, const GLchar
*src
)
165 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
176 * Confirm that the a shader type is valid and supported by the implementation
178 * \param ctx Current GL context
179 * \param type Shader target
183 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
185 /* Note: when building built-in GLSL functions, this function may be
186 * invoked with ctx == NULL. In that case, we can only validate that it's
187 * a shader target we recognize, not that it's supported in the current
188 * context. But that's fine--we don't need any further validation than
189 * that when building built-in GLSL functions.
193 case GL_FRAGMENT_SHADER
:
194 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
195 case GL_VERTEX_SHADER
:
196 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
197 case GL_GEOMETRY_SHADER_ARB
:
198 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
199 case GL_COMPUTE_SHADER
:
200 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
208 is_program(struct gl_context
*ctx
, GLuint name
)
210 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
211 return shProg
? GL_TRUE
: GL_FALSE
;
216 is_shader(struct gl_context
*ctx
, GLuint name
)
218 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
219 return shader
? GL_TRUE
: GL_FALSE
;
224 * Attach shader to a shader program.
227 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
229 struct gl_shader_program
*shProg
;
230 struct gl_shader
*sh
;
233 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
235 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
239 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
244 n
= shProg
->NumShaders
;
245 for (i
= 0; i
< n
; i
++) {
246 if (shProg
->Shaders
[i
] == sh
) {
247 /* The shader is already attched to this program. The
248 * GL_ARB_shader_objects spec says:
250 * "The error INVALID_OPERATION is generated by AttachObjectARB
251 * if <obj> is already attached to <containerObj>."
253 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
255 } else if (same_type_disallowed
&&
256 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
257 /* Shader with the same type is already attached to this program,
258 * OpenGL ES 2.0 and 3.0 specs say:
260 * "Multiple shader objects of the same type may not be attached
261 * to a single program object. [...] The error INVALID_OPERATION
262 * is generated if [...] another shader object of the same type
263 * as shader is already attached to program."
265 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
271 shProg
->Shaders
= (struct gl_shader
**)
272 _mesa_realloc(shProg
->Shaders
,
273 n
* sizeof(struct gl_shader
*),
274 (n
+ 1) * sizeof(struct gl_shader
*));
275 if (!shProg
->Shaders
) {
276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
281 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
282 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
283 shProg
->NumShaders
++;
288 create_shader(struct gl_context
*ctx
, GLenum type
)
290 struct gl_shader
*sh
;
293 if (!_mesa_validate_shader_target(ctx
, type
)) {
294 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
298 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
299 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
300 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
307 create_shader_program(struct gl_context
*ctx
)
310 struct gl_shader_program
*shProg
;
312 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
314 shProg
= ctx
->Driver
.NewShaderProgram(ctx
, name
);
316 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
318 assert(shProg
->RefCount
== 1);
325 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
329 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
332 * NOTE: deleting shaders/programs works a bit differently than
333 * texture objects (and buffer objects, etc). Shader/program
334 * handles/IDs exist in the hash table until the object is really
335 * deleted (refcount==0). With texture objects, the handle/ID is
336 * removed from the hash table in glDeleteTextures() while the tex
337 * object itself might linger until its refcount goes to zero.
339 struct gl_shader_program
*shProg
;
341 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
345 if (!shProg
->DeletePending
) {
346 shProg
->DeletePending
= GL_TRUE
;
348 /* effectively, decr shProg's refcount */
349 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
355 delete_shader(struct gl_context
*ctx
, GLuint shader
)
357 struct gl_shader
*sh
;
359 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
363 if (!sh
->DeletePending
) {
364 sh
->DeletePending
= GL_TRUE
;
366 /* effectively, decr sh's refcount */
367 _mesa_reference_shader(ctx
, &sh
, NULL
);
373 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
375 struct gl_shader_program
*shProg
;
379 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
383 n
= shProg
->NumShaders
;
385 for (i
= 0; i
< n
; i
++) {
386 if (shProg
->Shaders
[i
]->Name
== shader
) {
388 struct gl_shader
**newList
;
391 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
393 /* alloc new, smaller array */
394 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
396 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
399 /* Copy old list entries to new list, skipping removed entry at [i] */
400 for (j
= 0; j
< i
; j
++) {
401 newList
[j
] = shProg
->Shaders
[j
];
404 newList
[j
++] = shProg
->Shaders
[i
];
407 /* Free old list and install new one */
408 free(shProg
->Shaders
);
409 shProg
->Shaders
= newList
;
410 shProg
->NumShaders
= n
- 1;
413 /* sanity check - make sure the new list's entries are sensible */
414 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
415 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
416 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
417 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
418 assert(shProg
->Shaders
[j
]->RefCount
> 0);
429 if (is_shader(ctx
, shader
))
430 err
= GL_INVALID_OPERATION
;
431 else if (is_program(ctx
, shader
))
432 err
= GL_INVALID_OPERATION
;
434 err
= GL_INVALID_VALUE
;
435 _mesa_error(ctx
, err
, "glDetachShader(shader)");
442 * Return list of shaders attached to shader program.
445 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
446 GLsizei
*count
, GLuint
*obj
)
448 struct gl_shader_program
*shProg
=
449 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
452 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
453 obj
[i
] = shProg
->Shaders
[i
]->Name
;
462 * glGetHandleARB() - return ID/name of currently bound shader program.
465 get_handle(struct gl_context
*ctx
, GLenum pname
)
467 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
468 if (ctx
->Shader
.ActiveProgram
)
469 return ctx
->Shader
.ActiveProgram
->Name
;
474 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
481 * Check if a geometry shader query is valid at this time. If not, report an
482 * error and return false.
484 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
486 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
487 * are queried for a program which has not been linked successfully, or
488 * which does not contain objects to form a geometry shader, then an
489 * INVALID_OPERATION error is generated."
492 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
494 if (shProg
->LinkStatus
&&
495 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
499 _mesa_error(ctx
, GL_INVALID_OPERATION
,
500 "glGetProgramv(linked geometry shader required)");
506 * glGetProgramiv() - get shader program state.
507 * Note that this is for GLSL shader programs, not ARB vertex/fragment
508 * programs (see glGetProgramivARB).
511 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
, GLint
*params
)
513 struct gl_shader_program
*shProg
514 = _mesa_lookup_shader_program(ctx
, program
);
516 /* Is transform feedback available in this context?
519 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
520 || ctx
->API
== API_OPENGL_CORE
521 || _mesa_is_gles3(ctx
);
523 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
524 * and GL 3.2) are available in this context
526 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
528 /* Are uniform buffer objects available in this context?
531 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_uniform_buffer_object
)
532 || ctx
->API
== API_OPENGL_CORE
533 || _mesa_is_gles3(ctx
);
536 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramiv(program)");
541 case GL_DELETE_STATUS
:
542 *params
= shProg
->DeletePending
;
545 *params
= shProg
->LinkStatus
;
547 case GL_VALIDATE_STATUS
:
548 *params
= shProg
->Validated
;
550 case GL_INFO_LOG_LENGTH
:
551 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
553 case GL_ATTACHED_SHADERS
:
554 *params
= shProg
->NumShaders
;
556 case GL_ACTIVE_ATTRIBUTES
:
557 *params
= _mesa_count_active_attribs(shProg
);
559 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
560 *params
= _mesa_longest_attribute_name_length(shProg
);
562 case GL_ACTIVE_UNIFORMS
:
563 *params
= shProg
->NumUserUniformStorage
;
565 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
569 for (i
= 0; i
< shProg
->NumUserUniformStorage
; i
++) {
570 /* Add one for the terminating NUL character for a non-array, and
571 * 4 for the "[0]" and the NUL for an array.
573 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
574 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
583 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
586 *params
= shProg
->TransformFeedback
.NumVarying
;
588 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
594 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
595 /* Add one for the terminating NUL character.
597 const GLint len
= strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
606 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
609 *params
= shProg
->TransformFeedback
.BufferMode
;
611 case GL_GEOMETRY_VERTICES_OUT
:
614 if (check_gs_query(ctx
, shProg
))
615 *params
= shProg
->Geom
.VerticesOut
;
617 case GL_GEOMETRY_SHADER_INVOCATIONS
:
618 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
620 if (check_gs_query(ctx
, shProg
))
621 *params
= shProg
->Geom
.Invocations
;
623 case GL_GEOMETRY_INPUT_TYPE
:
626 if (check_gs_query(ctx
, shProg
))
627 *params
= shProg
->Geom
.InputType
;
629 case GL_GEOMETRY_OUTPUT_TYPE
:
632 if (check_gs_query(ctx
, shProg
))
633 *params
= shProg
->Geom
.OutputType
;
635 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
642 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
643 /* Add one for the terminating NUL character.
645 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
654 case GL_ACTIVE_UNIFORM_BLOCKS
:
658 *params
= shProg
->NumUniformBlocks
;
660 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
661 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
662 * only available with desktop OpenGL 3.0+ with the
663 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
665 * On desktop, we ignore the 3.0+ requirement because it is silly.
667 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
670 *params
= shProg
->BinaryRetreivableHint
;
672 case GL_PROGRAM_BINARY_LENGTH
:
675 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
676 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
679 *params
= shProg
->NumAtomicBuffers
;
681 case GL_COMPUTE_WORK_GROUP_SIZE
: {
683 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
685 if (!shProg
->LinkStatus
) {
686 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
690 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
691 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
695 for (i
= 0; i
< 3; i
++)
696 params
[i
] = shProg
->Comp
.LocalSize
[i
];
699 case GL_PROGRAM_SEPARABLE
:
700 if (!ctx
->Extensions
.ARB_separate_shader_objects
)
703 *params
= shProg
->SeparateShader
;
709 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
710 _mesa_lookup_enum_by_nr(pname
));
715 * glGetShaderiv() - get GLSL shader state
718 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
720 struct gl_shader
*shader
=
721 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
729 *params
= shader
->Type
;
731 case GL_DELETE_STATUS
:
732 *params
= shader
->DeletePending
;
734 case GL_COMPILE_STATUS
:
735 *params
= shader
->CompileStatus
;
737 case GL_INFO_LOG_LENGTH
:
738 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
740 case GL_SHADER_SOURCE_LENGTH
:
741 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
744 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
751 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
752 GLsizei
*length
, GLchar
*infoLog
)
754 struct gl_shader_program
*shProg
755 = _mesa_lookup_shader_program(ctx
, program
);
757 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(program)");
760 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
765 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
766 GLsizei
*length
, GLchar
*infoLog
)
768 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
770 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(shader)");
773 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
778 * Return shader source code.
781 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
782 GLsizei
*length
, GLchar
*sourceOut
)
784 struct gl_shader
*sh
;
785 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
789 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
794 * Set/replace shader source code. A helper function used by
795 * glShaderSource[ARB] and glCreateShaderProgramEXT.
798 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
800 struct gl_shader
*sh
;
802 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
806 /* free old shader source string and install new one */
807 free((void *)sh
->Source
);
809 sh
->CompileStatus
= GL_FALSE
;
811 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
820 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
822 struct gl_shader
*sh
;
823 struct gl_shader_compiler_options
*options
;
825 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
829 options
= &ctx
->ShaderCompilerOptions
[sh
->Stage
];
831 /* set default pragma state for shader */
832 sh
->Pragmas
= options
->DefaultPragmas
;
835 /* If the user called glCompileShader without first calling
836 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
838 sh
->CompileStatus
= GL_FALSE
;
840 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
841 printf("GLSL source for %s shader %d:\n",
842 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
843 printf("%s\n", sh
->Source
);
846 /* this call will set the shader->CompileStatus field to indicate if
847 * compilation was successful.
849 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
851 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
852 _mesa_write_shader_to_file(sh
);
855 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
856 if (sh
->CompileStatus
) {
857 printf("GLSL IR for shader %d:\n", sh
->Name
);
858 _mesa_print_ir(sh
->ir
, NULL
);
861 printf("GLSL shader %d failed to compile.\n", sh
->Name
);
863 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
864 printf("GLSL shader %d info log:\n", sh
->Name
);
865 printf("%s\n", sh
->InfoLog
);
871 if (!sh
->CompileStatus
) {
872 if (ctx
->Shader
.Flags
& GLSL_DUMP_ON_ERROR
) {
873 fprintf(stderr
, "GLSL source for %s shader %d:\n",
874 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
875 fprintf(stderr
, "%s\n", sh
->Source
);
876 fprintf(stderr
, "Info Log:\n%s\n", sh
->InfoLog
);
880 if (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
) {
881 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
882 sh
->Name
, sh
->InfoLog
);
889 * Link a program's shaders.
892 link_program(struct gl_context
*ctx
, GLuint program
)
894 struct gl_shader_program
*shProg
;
896 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
900 /* From the ARB_transform_feedback2 specification:
901 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
902 * the name of a program being used by one or more transform feedback
903 * objects, even if the objects are not currently bound or are paused."
905 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
906 _mesa_error(ctx
, GL_INVALID_OPERATION
,
907 "glLinkProgram(transform feedback is using the program)");
911 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
913 _mesa_glsl_link_shader(ctx
, shProg
);
915 if (shProg
->LinkStatus
== GL_FALSE
&&
916 (ctx
->Shader
.Flags
& GLSL_REPORT_ERRORS
)) {
917 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
918 shProg
->Name
, shProg
->InfoLog
);
925 printf("Link %u shaders in program %u: %s\n",
926 shProg
->NumShaders
, shProg
->Name
,
927 shProg
->LinkStatus
? "Success" : "Failed");
929 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
930 printf(" shader %u, type 0x%x\n",
931 shProg
->Shaders
[i
]->Name
,
932 shProg
->Shaders
[i
]->Type
);
939 * Print basic shader info (for debug).
942 print_shader_info(const struct gl_shader_program
*shProg
)
946 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
947 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
948 printf(" %s shader %u, checksum %u\n",
949 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
950 shProg
->Shaders
[i
]->Name
,
951 shProg
->Shaders
[i
]->SourceChecksum
);
953 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
954 printf(" vert prog %u\n",
955 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
956 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
957 printf(" frag prog %u\n",
958 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
959 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
960 printf(" geom prog %u\n",
961 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
966 * Use the named shader program for subsequent glUniform calls
969 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
972 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
973 _mesa_error(ctx
, GL_INVALID_OPERATION
,
974 "%s(program %u not linked)", caller
, shProg
->Name
);
978 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
979 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
986 use_shader_program(struct gl_context
*ctx
, GLenum type
,
987 struct gl_shader_program
*shProg
)
989 struct gl_shader_program
**target
;
990 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
992 target
= &ctx
->Shader
.CurrentProgram
[stage
];
993 if ((shProg
== NULL
) || (shProg
->_LinkedShaders
[stage
] == NULL
))
996 if (*target
!= shProg
) {
997 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
999 /* If the shader is also bound as the current rendering shader, unbind
1000 * it from that binding point as well. This ensures that the correct
1001 * semantics of glDeleteProgram are maintained.
1004 case GL_VERTEX_SHADER
:
1005 /* Empty for now. */
1007 case GL_GEOMETRY_SHADER_ARB
:
1008 /* Empty for now. */
1010 case GL_COMPUTE_SHADER
:
1011 /* Empty for now. */
1013 case GL_FRAGMENT_SHADER
:
1014 if (*target
== ctx
->Shader
._CurrentFragmentProgram
) {
1015 _mesa_reference_shader_program(ctx
,
1016 &ctx
->Shader
._CurrentFragmentProgram
,
1022 _mesa_reference_shader_program(ctx
, target
, shProg
);
1028 * Use the named shader program for subsequent rendering.
1031 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1033 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
);
1034 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
);
1035 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
);
1036 use_shader_program(ctx
, GL_COMPUTE_SHADER
, shProg
);
1037 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1039 if (ctx
->Driver
.UseProgram
)
1040 ctx
->Driver
.UseProgram(ctx
, shProg
);
1045 * Do validation of the given shader program.
1046 * \param errMsg returns error message if validation fails.
1047 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1050 validate_shader_program(const struct gl_shader_program
*shProg
,
1053 if (!shProg
->LinkStatus
) {
1057 /* From the GL spec, a program is invalid if any of these are true:
1059 any two active samplers in the current program object are of
1060 different types, but refer to the same texture image unit,
1062 any active sampler in the current program object refers to a texture
1063 image unit where fixed-function fragment processing accesses a
1064 texture target that does not match the sampler type, or
1066 the sum of the number of active samplers in the program and the
1067 number of texture image units enabled for fixed-function fragment
1068 processing exceeds the combined limit on the total number of texture
1069 image units allowed.
1074 * Check: any two active samplers in the current program object are of
1075 * different types, but refer to the same texture image unit,
1077 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1085 * Called via glValidateProgram()
1088 validate_program(struct gl_context
*ctx
, GLuint program
)
1090 struct gl_shader_program
*shProg
;
1091 char errMsg
[100] = "";
1093 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1098 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1099 if (!shProg
->Validated
) {
1100 /* update info log */
1101 if (shProg
->InfoLog
) {
1102 ralloc_free(shProg
->InfoLog
);
1104 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1111 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1113 GET_CURRENT_CONTEXT(ctx
);
1114 attach_shader(ctx
, program
, shader
);
1119 _mesa_AttachShader(GLuint program
, GLuint shader
)
1121 GET_CURRENT_CONTEXT(ctx
);
1122 attach_shader(ctx
, program
, shader
);
1127 _mesa_CompileShader(GLhandleARB shaderObj
)
1129 GET_CURRENT_CONTEXT(ctx
);
1130 if (MESA_VERBOSE
& VERBOSE_API
)
1131 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1132 compile_shader(ctx
, shaderObj
);
1137 _mesa_CreateShader(GLenum type
)
1139 GET_CURRENT_CONTEXT(ctx
);
1140 if (MESA_VERBOSE
& VERBOSE_API
)
1141 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1142 return create_shader(ctx
, type
);
1146 GLhandleARB GLAPIENTRY
1147 _mesa_CreateShaderObjectARB(GLenum type
)
1149 GET_CURRENT_CONTEXT(ctx
);
1150 return create_shader(ctx
, type
);
1155 _mesa_CreateProgram(void)
1157 GET_CURRENT_CONTEXT(ctx
);
1158 if (MESA_VERBOSE
& VERBOSE_API
)
1159 _mesa_debug(ctx
, "glCreateProgram\n");
1160 return create_shader_program(ctx
);
1164 GLhandleARB GLAPIENTRY
1165 _mesa_CreateProgramObjectARB(void)
1167 GET_CURRENT_CONTEXT(ctx
);
1168 return create_shader_program(ctx
);
1173 _mesa_DeleteObjectARB(GLhandleARB obj
)
1175 if (MESA_VERBOSE
& VERBOSE_API
) {
1176 GET_CURRENT_CONTEXT(ctx
);
1177 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1181 GET_CURRENT_CONTEXT(ctx
);
1182 FLUSH_VERTICES(ctx
, 0);
1183 if (is_program(ctx
, obj
)) {
1184 delete_shader_program(ctx
, obj
);
1186 else if (is_shader(ctx
, obj
)) {
1187 delete_shader(ctx
, obj
);
1197 _mesa_DeleteProgram(GLuint name
)
1200 GET_CURRENT_CONTEXT(ctx
);
1201 FLUSH_VERTICES(ctx
, 0);
1202 delete_shader_program(ctx
, name
);
1208 _mesa_DeleteShader(GLuint name
)
1211 GET_CURRENT_CONTEXT(ctx
);
1212 FLUSH_VERTICES(ctx
, 0);
1213 delete_shader(ctx
, name
);
1219 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1221 GET_CURRENT_CONTEXT(ctx
);
1222 detach_shader(ctx
, program
, shader
);
1227 _mesa_DetachShader(GLuint program
, GLuint shader
)
1229 GET_CURRENT_CONTEXT(ctx
);
1230 detach_shader(ctx
, program
, shader
);
1235 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1236 GLsizei
* count
, GLhandleARB
* obj
)
1238 GET_CURRENT_CONTEXT(ctx
);
1239 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1244 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1245 GLsizei
*count
, GLuint
*obj
)
1247 GET_CURRENT_CONTEXT(ctx
);
1248 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1253 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1254 GLcharARB
* infoLog
)
1256 GET_CURRENT_CONTEXT(ctx
);
1257 if (is_program(ctx
, object
)) {
1258 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1260 else if (is_shader(ctx
, object
)) {
1261 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1264 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1270 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1272 GET_CURRENT_CONTEXT(ctx
);
1273 /* Implement in terms of GetProgramiv, GetShaderiv */
1274 if (is_program(ctx
, object
)) {
1275 if (pname
== GL_OBJECT_TYPE_ARB
) {
1276 *params
= GL_PROGRAM_OBJECT_ARB
;
1279 get_programiv(ctx
, object
, pname
, params
);
1282 else if (is_shader(ctx
, object
)) {
1283 if (pname
== GL_OBJECT_TYPE_ARB
) {
1284 *params
= GL_SHADER_OBJECT_ARB
;
1287 get_shaderiv(ctx
, object
, pname
, params
);
1291 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1297 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1300 GLint iparams
[1]; /* XXX is one element enough? */
1301 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1302 params
[0] = (GLfloat
) iparams
[0];
1307 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 get_programiv(ctx
, program
, pname
, params
);
1315 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1317 GET_CURRENT_CONTEXT(ctx
);
1318 get_shaderiv(ctx
, shader
, pname
, params
);
1323 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1324 GLsizei
*length
, GLchar
*infoLog
)
1326 GET_CURRENT_CONTEXT(ctx
);
1327 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1332 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1333 GLsizei
*length
, GLchar
*infoLog
)
1335 GET_CURRENT_CONTEXT(ctx
);
1336 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1341 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1342 GLsizei
*length
, GLcharARB
*sourceOut
)
1344 GET_CURRENT_CONTEXT(ctx
);
1345 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1349 GLhandleARB GLAPIENTRY
1350 _mesa_GetHandleARB(GLenum pname
)
1352 GET_CURRENT_CONTEXT(ctx
);
1353 return get_handle(ctx
, pname
);
1357 GLboolean GLAPIENTRY
1358 _mesa_IsProgram(GLuint name
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 return is_program(ctx
, name
);
1365 GLboolean GLAPIENTRY
1366 _mesa_IsShader(GLuint name
)
1368 GET_CURRENT_CONTEXT(ctx
);
1369 return is_shader(ctx
, name
);
1374 _mesa_LinkProgram(GLhandleARB programObj
)
1376 GET_CURRENT_CONTEXT(ctx
);
1377 link_program(ctx
, programObj
);
1383 * Read shader source code from a file.
1384 * Useful for debugging to override an app's shader.
1387 read_shader(const char *fname
)
1389 const int max
= 50*1000;
1390 FILE *f
= fopen(fname
, "r");
1391 GLcharARB
*buffer
, *shader
;
1398 buffer
= malloc(max
);
1399 len
= fread(buffer
, 1, max
, f
);
1404 shader
= _mesa_strdup(buffer
);
1412 * Called via glShaderSource() and glShaderSourceARB() API functions.
1413 * Basically, concatenate the source code strings into one long string
1414 * and pass it to _mesa_shader_source().
1417 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1418 const GLcharARB
* const * string
, const GLint
* length
)
1420 GET_CURRENT_CONTEXT(ctx
);
1422 GLsizei i
, totalLength
;
1426 if (!shaderObj
|| string
== NULL
) {
1427 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1432 * This array holds offsets of where the appropriate string ends, thus the
1433 * last element will be set to the total length of the source code.
1435 offsets
= malloc(count
* sizeof(GLint
));
1436 if (offsets
== NULL
) {
1437 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1441 for (i
= 0; i
< count
; i
++) {
1442 if (string
[i
] == NULL
) {
1443 free((GLvoid
*) offsets
);
1444 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1445 "glShaderSourceARB(null string)");
1448 if (length
== NULL
|| length
[i
] < 0)
1449 offsets
[i
] = strlen(string
[i
]);
1451 offsets
[i
] = length
[i
];
1452 /* accumulate string lengths */
1454 offsets
[i
] += offsets
[i
- 1];
1457 /* Total length of source string is sum off all strings plus two.
1458 * One extra byte for terminating zero, another extra byte to silence
1459 * valgrind warnings in the parser/grammer code.
1461 totalLength
= offsets
[count
- 1] + 2;
1462 source
= malloc(totalLength
* sizeof(GLcharARB
));
1463 if (source
== NULL
) {
1464 free((GLvoid
*) offsets
);
1465 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1469 for (i
= 0; i
< count
; i
++) {
1470 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1471 memcpy(source
+ start
, string
[i
],
1472 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1474 source
[totalLength
- 1] = '\0';
1475 source
[totalLength
- 2] = '\0';
1478 /* Compute the shader's source code checksum then try to open a file
1479 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1480 * original shader source code. For debugging.
1483 GLcharARB
*newSource
;
1485 checksum
= _mesa_str_checksum(source
);
1487 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1489 newSource
= read_shader(filename
);
1491 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1492 shaderObj
, checksum
, filename
);
1498 shader_source(ctx
, shaderObj
, source
);
1501 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1503 sh
->SourceChecksum
= checksum
; /* save original checksum */
1511 _mesa_UseProgram(GLhandleARB program
)
1513 GET_CURRENT_CONTEXT(ctx
);
1514 struct gl_shader_program
*shProg
;
1516 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1517 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1518 "glUseProgram(transform feedback active)");
1523 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1527 if (!shProg
->LinkStatus
) {
1528 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1529 "glUseProgram(program %u not linked)", program
);
1534 if (ctx
->Shader
.Flags
& GLSL_USE_PROG
) {
1535 print_shader_info(shProg
);
1542 _mesa_use_program(ctx
, shProg
);
1547 _mesa_ValidateProgram(GLhandleARB program
)
1549 GET_CURRENT_CONTEXT(ctx
);
1550 validate_program(ctx
, program
);
1555 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1558 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1559 GLint
* range
, GLint
* precision
)
1561 const struct gl_program_constants
*limits
;
1562 const struct gl_precision
*p
;
1563 GET_CURRENT_CONTEXT(ctx
);
1565 switch (shadertype
) {
1566 case GL_VERTEX_SHADER
:
1567 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1569 case GL_FRAGMENT_SHADER
:
1570 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1573 _mesa_error(ctx
, GL_INVALID_ENUM
,
1574 "glGetShaderPrecisionFormat(shadertype)");
1578 switch (precisiontype
) {
1580 p
= &limits
->LowFloat
;
1582 case GL_MEDIUM_FLOAT
:
1583 p
= &limits
->MediumFloat
;
1586 p
= &limits
->HighFloat
;
1589 p
= &limits
->LowInt
;
1592 p
= &limits
->MediumInt
;
1595 p
= &limits
->HighInt
;
1598 _mesa_error(ctx
, GL_INVALID_ENUM
,
1599 "glGetShaderPrecisionFormat(precisiontype)");
1603 range
[0] = p
->RangeMin
;
1604 range
[1] = p
->RangeMax
;
1605 precision
[0] = p
->Precision
;
1610 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1613 _mesa_ReleaseShaderCompiler(void)
1615 _mesa_destroy_shader_compiler_caches();
1620 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1623 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1624 const void* binary
, GLint length
)
1626 GET_CURRENT_CONTEXT(ctx
);
1629 (void) binaryformat
;
1632 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1637 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1638 GLenum
*binaryFormat
, GLvoid
*binary
)
1640 struct gl_shader_program
*shProg
;
1641 GET_CURRENT_CONTEXT(ctx
);
1643 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1647 if (!shProg
->LinkStatus
) {
1648 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1649 "glGetProgramBinary(program %u not linked)",
1655 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1659 /* The ARB_get_program_binary spec says:
1661 * "If <length> is NULL, then no length is returned."
1666 (void) binaryFormat
;
1671 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1672 const GLvoid
*binary
, GLsizei length
)
1674 struct gl_shader_program
*shProg
;
1675 GET_CURRENT_CONTEXT(ctx
);
1677 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1681 (void) binaryFormat
;
1684 _mesa_error(ctx
, GL_INVALID_OPERATION
, __FUNCTION__
);
1689 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1691 struct gl_shader_program
*shProg
;
1692 GET_CURRENT_CONTEXT(ctx
);
1694 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1695 "glProgramParameteri");
1700 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1701 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1702 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1703 * even be in the dispatch table, so we shouldn't need to expclicitly
1706 * On desktop, we ignore the 3.0+ requirement because it is silly.
1709 /* The ARB_get_program_binary extension spec says:
1711 * "An INVALID_VALUE error is generated if the <value> argument to
1712 * ProgramParameteri is not TRUE or FALSE."
1714 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1715 _mesa_error(ctx
, GL_INVALID_VALUE
,
1716 "glProgramParameteri(pname=%s, value=%d): "
1717 "value must be 0 or 1.",
1718 _mesa_lookup_enum_by_nr(pname
),
1723 /* No need to notify the driver. Any changes will actually take effect
1724 * the next time the shader is linked.
1726 * The ARB_get_program_binary extension spec says:
1728 * "To indicate that a program binary is likely to be retrieved,
1729 * ProgramParameteri should be called with <pname>
1730 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1731 * will not be in effect until the next time LinkProgram or
1732 * ProgramBinary has been called successfully."
1734 * The resloution of issue 9 in the extension spec also says:
1736 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1737 * to indicate to the GL implementation that this program will
1738 * likely be saved with GetProgramBinary at some point. This will
1739 * give the GL implementation the opportunity to track any state
1740 * changes made to the program before being saved such that when it
1741 * is loaded again a recompile can be avoided."
1743 shProg
->BinaryRetreivableHint
= value
;
1746 case GL_PROGRAM_SEPARABLE
:
1747 if (!ctx
->Extensions
.ARB_separate_shader_objects
)
1750 /* Spec imply that the behavior is the same as ARB_get_program_binary
1751 * Chapter 7.3 Program Objects
1753 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1754 _mesa_error(ctx
, GL_INVALID_VALUE
,
1755 "glProgramParameteri(pname=%s, value=%d): "
1756 "value must be 0 or 1.",
1757 _mesa_lookup_enum_by_nr(pname
),
1761 shProg
->SeparateShader
= value
;
1768 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1769 _mesa_lookup_enum_by_nr(pname
));
1773 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1774 struct gl_shader_program
*shProg
)
1776 use_shader_program(ctx
, type
, shProg
);
1778 if (ctx
->Driver
.UseProgram
)
1779 ctx
->Driver
.UseProgram(ctx
, shProg
);
1784 * For GL_EXT_separate_shader_objects
1787 _mesa_UseShaderProgramEXT(GLenum type
, GLuint program
)
1789 GET_CURRENT_CONTEXT(ctx
);
1790 struct gl_shader_program
*shProg
= NULL
;
1792 if (!_mesa_validate_shader_target(ctx
, type
)) {
1793 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUseShaderProgramEXT(type)");
1797 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1798 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1799 "glUseShaderProgramEXT(transform feedback is active)");
1804 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1805 "glUseShaderProgramEXT");
1809 if (!shProg
->LinkStatus
) {
1810 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1811 "glUseShaderProgramEXT(program not linked)");
1816 _mesa_use_shader_program(ctx
, type
, shProg
);
1821 * For GL_EXT_separate_shader_objects
1824 _mesa_ActiveProgramEXT(GLuint program
)
1826 GET_CURRENT_CONTEXT(ctx
);
1827 struct gl_shader_program
*shProg
= (program
!= 0)
1828 ? _mesa_lookup_shader_program_err(ctx
, program
, "glActiveProgramEXT")
1831 _mesa_active_program(ctx
, shProg
, "glActiveProgramEXT");
1836 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1837 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1839 const GLuint shader
= create_shader(ctx
, type
);
1843 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1845 compile_shader(ctx
, shader
);
1847 program
= create_shader_program(ctx
);
1849 struct gl_shader_program
*shProg
;
1850 struct gl_shader
*sh
;
1851 GLint compiled
= GL_FALSE
;
1853 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1854 sh
= _mesa_lookup_shader(ctx
, shader
);
1856 shProg
->SeparateShader
= separate
;
1858 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1860 attach_shader(ctx
, program
, shader
);
1861 link_program(ctx
, program
);
1862 detach_shader(ctx
, program
, shader
);
1866 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1867 append
-error
-to
-info
-log
;
1868 shProg
->LinkStatus
= GL_FALSE
;
1873 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1876 delete_shader(ctx
, shader
);
1884 * Copy program-specific data generated by linking from the gl_shader_program
1885 * object to a specific gl_program object.
1888 _mesa_copy_linked_program_data(gl_shader_stage type
,
1889 const struct gl_shader_program
*src
,
1890 struct gl_program
*dst
)
1893 case MESA_SHADER_VERTEX
:
1894 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1896 case MESA_SHADER_GEOMETRY
: {
1897 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1898 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1899 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1900 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1901 dst_gp
->InputType
= src
->Geom
.InputType
;
1902 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1903 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1904 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1907 case MESA_SHADER_COMPUTE
: {
1908 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1910 for (i
= 0; i
< 3; i
++)
1911 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
1921 * For GL_EXT_separate_shader_objects
1924 _mesa_CreateShaderProgramEXT(GLenum type
, const GLchar
*string
)
1926 GET_CURRENT_CONTEXT(ctx
);
1928 return _mesa_create_shader_program(ctx
, GL_FALSE
, type
, 1, &string
);
1932 * ARB_separate_shader_objects: Compile & Link Program
1935 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
1936 const GLchar
* const *strings
)