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/mtypes.h"
46 #include "main/pipelineobj.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"
54 #include "util/ralloc.h"
55 #include "util/hash_table.h"
57 #include "../glsl/glsl_parser_extras.h"
58 #include "../glsl/ir.h"
59 #include "../glsl/ir_uniform.h"
60 #include "../glsl/program.h"
62 /** Define this to enable shader substitution (see below) */
63 #define SHADER_SUBST 0
67 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
70 _mesa_get_shader_flags(void)
72 GLbitfield flags
= 0x0;
73 const char *env
= getenv("MESA_GLSL");
76 if (strstr(env
, "dump_on_error"))
77 flags
|= GLSL_DUMP_ON_ERROR
;
78 else if (strstr(env
, "dump"))
80 if (strstr(env
, "log"))
82 if (strstr(env
, "nopvert"))
83 flags
|= GLSL_NOP_VERT
;
84 if (strstr(env
, "nopfrag"))
85 flags
|= GLSL_NOP_FRAG
;
86 if (strstr(env
, "nopt"))
88 else if (strstr(env
, "opt"))
90 if (strstr(env
, "uniform"))
91 flags
|= GLSL_UNIFORMS
;
92 if (strstr(env
, "useprog"))
93 flags
|= GLSL_USE_PROG
;
94 if (strstr(env
, "errors"))
95 flags
|= GLSL_REPORT_ERRORS
;
103 * Initialize context's shader state.
106 _mesa_init_shader_state(struct gl_context
*ctx
)
108 /* Device drivers may override these to control what kind of instructions
109 * are generated by the GLSL compiler.
111 struct gl_shader_compiler_options options
;
114 memset(&options
, 0, sizeof(options
));
115 options
.MaxUnrollIterations
= 32;
116 options
.MaxIfDepth
= UINT_MAX
;
118 /* Default pragma settings */
119 options
.DefaultPragmas
.Optimize
= GL_TRUE
;
121 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
122 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
124 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
126 if (ctx
->Shader
.Flags
!= 0)
127 ctx
->Const
.GenerateTemporaryNames
= true;
129 /* Extended for ARB_separate_shader_objects */
130 ctx
->Shader
.RefCount
= 1;
131 mtx_init(&ctx
->Shader
.Mutex
, mtx_plain
);
136 * Free the per-context shader-related state.
139 _mesa_free_shader_state(struct gl_context
*ctx
)
142 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
143 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
146 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
148 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
150 /* Extended for ARB_separate_shader_objects */
151 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
153 assert(ctx
->Shader
.RefCount
== 1);
154 mtx_destroy(&ctx
->Shader
.Mutex
);
159 * Copy string from <src> to <dst>, up to maxLength characters, returning
160 * length of <dst> in <length>.
161 * \param src the strings source
162 * \param maxLength max chars to copy
163 * \param length returns number of chars copied
164 * \param dst the string destination
167 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
168 GLsizei
*length
, const GLchar
*src
)
171 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
182 * Confirm that the a shader type is valid and supported by the implementation
184 * \param ctx Current GL context
185 * \param type Shader target
189 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
191 /* Note: when building built-in GLSL functions, this function may be
192 * invoked with ctx == NULL. In that case, we can only validate that it's
193 * a shader target we recognize, not that it's supported in the current
194 * context. But that's fine--we don't need any further validation than
195 * that when building built-in GLSL functions.
199 case GL_FRAGMENT_SHADER
:
200 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
201 case GL_VERTEX_SHADER
:
202 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
203 case GL_GEOMETRY_SHADER_ARB
:
204 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
205 case GL_COMPUTE_SHADER
:
206 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
214 is_program(struct gl_context
*ctx
, GLuint name
)
216 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
217 return shProg
? GL_TRUE
: GL_FALSE
;
222 is_shader(struct gl_context
*ctx
, GLuint name
)
224 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
225 return shader
? GL_TRUE
: GL_FALSE
;
230 * Attach shader to a shader program.
233 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
235 struct gl_shader_program
*shProg
;
236 struct gl_shader
*sh
;
239 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
241 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
245 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
250 n
= shProg
->NumShaders
;
251 for (i
= 0; i
< n
; i
++) {
252 if (shProg
->Shaders
[i
] == sh
) {
253 /* The shader is already attched to this program. The
254 * GL_ARB_shader_objects spec says:
256 * "The error INVALID_OPERATION is generated by AttachObjectARB
257 * if <obj> is already attached to <containerObj>."
259 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
261 } else if (same_type_disallowed
&&
262 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
263 /* Shader with the same type is already attached to this program,
264 * OpenGL ES 2.0 and 3.0 specs say:
266 * "Multiple shader objects of the same type may not be attached
267 * to a single program object. [...] The error INVALID_OPERATION
268 * is generated if [...] another shader object of the same type
269 * as shader is already attached to program."
271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
277 shProg
->Shaders
= realloc(shProg
->Shaders
,
278 (n
+ 1) * sizeof(struct gl_shader
*));
279 if (!shProg
->Shaders
) {
280 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
285 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
286 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
287 shProg
->NumShaders
++;
292 create_shader(struct gl_context
*ctx
, GLenum type
)
294 struct gl_shader
*sh
;
297 if (!_mesa_validate_shader_target(ctx
, type
)) {
298 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
302 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
303 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
304 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
311 create_shader_program(struct gl_context
*ctx
)
314 struct gl_shader_program
*shProg
;
316 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
318 shProg
= ctx
->Driver
.NewShaderProgram(name
);
320 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
322 assert(shProg
->RefCount
== 1);
329 * Delete a shader program. Actually, just decrement the program's
330 * reference count and mark it as DeletePending.
331 * Used to implement glDeleteProgram() and glDeleteObjectARB().
334 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
337 * NOTE: deleting shaders/programs works a bit differently than
338 * texture objects (and buffer objects, etc). Shader/program
339 * handles/IDs exist in the hash table until the object is really
340 * deleted (refcount==0). With texture objects, the handle/ID is
341 * removed from the hash table in glDeleteTextures() while the tex
342 * object itself might linger until its refcount goes to zero.
344 struct gl_shader_program
*shProg
;
346 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
350 if (!shProg
->DeletePending
) {
351 shProg
->DeletePending
= GL_TRUE
;
353 /* effectively, decr shProg's refcount */
354 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
360 delete_shader(struct gl_context
*ctx
, GLuint shader
)
362 struct gl_shader
*sh
;
364 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
368 if (!sh
->DeletePending
) {
369 sh
->DeletePending
= GL_TRUE
;
371 /* effectively, decr sh's refcount */
372 _mesa_reference_shader(ctx
, &sh
, NULL
);
378 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
380 struct gl_shader_program
*shProg
;
384 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
388 n
= shProg
->NumShaders
;
390 for (i
= 0; i
< n
; i
++) {
391 if (shProg
->Shaders
[i
]->Name
== shader
) {
393 struct gl_shader
**newList
;
396 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
398 /* alloc new, smaller array */
399 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
401 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
404 /* Copy old list entries to new list, skipping removed entry at [i] */
405 for (j
= 0; j
< i
; j
++) {
406 newList
[j
] = shProg
->Shaders
[j
];
409 newList
[j
++] = shProg
->Shaders
[i
];
412 /* Free old list and install new one */
413 free(shProg
->Shaders
);
414 shProg
->Shaders
= newList
;
415 shProg
->NumShaders
= n
- 1;
418 /* sanity check - make sure the new list's entries are sensible */
419 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
420 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
421 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
422 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
423 assert(shProg
->Shaders
[j
]->RefCount
> 0);
434 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
435 err
= GL_INVALID_OPERATION
;
437 err
= GL_INVALID_VALUE
;
438 _mesa_error(ctx
, err
, "glDetachShader(shader)");
445 * Return list of shaders attached to shader program.
448 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
449 GLsizei
*count
, GLuint
*obj
)
451 struct gl_shader_program
*shProg
;
454 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
459 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
463 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
464 obj
[i
] = shProg
->Shaders
[i
]->Name
;
473 * glGetHandleARB() - return ID/name of currently bound shader program.
476 get_handle(struct gl_context
*ctx
, GLenum pname
)
478 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
479 if (ctx
->_Shader
->ActiveProgram
)
480 return ctx
->_Shader
->ActiveProgram
->Name
;
485 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
492 * Check if a geometry shader query is valid at this time. If not, report an
493 * error and return false.
495 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
497 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
498 * are queried for a program which has not been linked successfully, or
499 * which does not contain objects to form a geometry shader, then an
500 * INVALID_OPERATION error is generated."
503 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
505 if (shProg
->LinkStatus
&&
506 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
510 _mesa_error(ctx
, GL_INVALID_OPERATION
,
511 "glGetProgramv(linked geometry shader required)");
517 * glGetProgramiv() - get shader program state.
518 * Note that this is for GLSL shader programs, not ARB vertex/fragment
519 * programs (see glGetProgramivARB).
522 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
525 struct gl_shader_program
*shProg
526 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
528 /* Is transform feedback available in this context?
531 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
532 || ctx
->API
== API_OPENGL_CORE
533 || _mesa_is_gles3(ctx
);
535 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
536 * and GL 3.2) are available in this context
538 const bool has_core_gs
= _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 32;
540 /* Are uniform buffer objects available in this context?
543 (ctx
->API
== API_OPENGL_COMPAT
&&
544 ctx
->Extensions
.ARB_uniform_buffer_object
)
545 || ctx
->API
== API_OPENGL_CORE
546 || _mesa_is_gles3(ctx
);
553 case GL_DELETE_STATUS
:
554 *params
= shProg
->DeletePending
;
557 *params
= shProg
->LinkStatus
;
559 case GL_VALIDATE_STATUS
:
560 *params
= shProg
->Validated
;
562 case GL_INFO_LOG_LENGTH
:
563 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
565 case GL_ATTACHED_SHADERS
:
566 *params
= shProg
->NumShaders
;
568 case GL_ACTIVE_ATTRIBUTES
:
569 *params
= _mesa_count_active_attribs(shProg
);
571 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
572 *params
= _mesa_longest_attribute_name_length(shProg
);
574 case GL_ACTIVE_UNIFORMS
:
575 *params
= shProg
->NumUserUniformStorage
- shProg
->NumHiddenUniforms
;
577 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
580 const unsigned num_uniforms
=
581 shProg
->NumUserUniformStorage
- shProg
->NumHiddenUniforms
;
583 for (i
= 0; i
< num_uniforms
; i
++) {
584 /* Add one for the terminating NUL character for a non-array, and
585 * 4 for the "[0]" and the NUL for an array.
587 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
588 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
597 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
600 *params
= shProg
->TransformFeedback
.NumVarying
;
602 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
608 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
609 /* Add one for the terminating NUL character.
612 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
621 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
624 *params
= shProg
->TransformFeedback
.BufferMode
;
626 case GL_GEOMETRY_VERTICES_OUT
:
629 if (check_gs_query(ctx
, shProg
))
630 *params
= shProg
->Geom
.VerticesOut
;
632 case GL_GEOMETRY_SHADER_INVOCATIONS
:
633 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
635 if (check_gs_query(ctx
, shProg
))
636 *params
= shProg
->Geom
.Invocations
;
638 case GL_GEOMETRY_INPUT_TYPE
:
641 if (check_gs_query(ctx
, shProg
))
642 *params
= shProg
->Geom
.InputType
;
644 case GL_GEOMETRY_OUTPUT_TYPE
:
647 if (check_gs_query(ctx
, shProg
))
648 *params
= shProg
->Geom
.OutputType
;
650 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
657 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
658 /* Add one for the terminating NUL character.
660 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
669 case GL_ACTIVE_UNIFORM_BLOCKS
:
673 *params
= shProg
->NumUniformBlocks
;
675 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
676 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
677 * only available with desktop OpenGL 3.0+ with the
678 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
680 * On desktop, we ignore the 3.0+ requirement because it is silly.
682 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
685 *params
= shProg
->BinaryRetreivableHint
;
687 case GL_PROGRAM_BINARY_LENGTH
:
690 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
691 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
694 *params
= shProg
->NumAtomicBuffers
;
696 case GL_COMPUTE_WORK_GROUP_SIZE
: {
698 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
700 if (!shProg
->LinkStatus
) {
701 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
705 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
706 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
710 for (i
= 0; i
< 3; i
++)
711 params
[i
] = shProg
->Comp
.LocalSize
[i
];
714 case GL_PROGRAM_SEPARABLE
:
715 *params
= shProg
->SeparateShader
;
721 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
722 _mesa_lookup_enum_by_nr(pname
));
727 * glGetShaderiv() - get GLSL shader state
730 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
732 struct gl_shader
*shader
=
733 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
741 *params
= shader
->Type
;
743 case GL_DELETE_STATUS
:
744 *params
= shader
->DeletePending
;
746 case GL_COMPILE_STATUS
:
747 *params
= shader
->CompileStatus
;
749 case GL_INFO_LOG_LENGTH
:
750 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
752 case GL_SHADER_SOURCE_LENGTH
:
753 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
756 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
763 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
764 GLsizei
*length
, GLchar
*infoLog
)
766 struct gl_shader_program
*shProg
;
768 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
769 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
771 * "If a negative number is provided where an argument of type sizei or
772 * sizeiptr is specified, an INVALID_VALUE error is generated."
775 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
779 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
780 "glGetProgramInfoLog(program)");
785 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
790 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
791 GLsizei
*length
, GLchar
*infoLog
)
793 struct gl_shader
*sh
;
795 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
796 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
798 * "If a negative number is provided where an argument of type sizei or
799 * sizeiptr is specified, an INVALID_VALUE error is generated."
802 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
806 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
811 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
816 * Return shader source code.
819 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
820 GLsizei
*length
, GLchar
*sourceOut
)
822 struct gl_shader
*sh
;
825 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
829 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
833 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
838 * Set/replace shader source code. A helper function used by
839 * glShaderSource[ARB].
842 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
844 struct gl_shader
*sh
;
846 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
850 /* free old shader source string and install new one */
851 free((void *)sh
->Source
);
853 sh
->CompileStatus
= GL_FALSE
;
855 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
864 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
866 struct gl_shader
*sh
;
867 struct gl_shader_compiler_options
*options
;
869 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
873 options
= &ctx
->Const
.ShaderCompilerOptions
[sh
->Stage
];
875 /* set default pragma state for shader */
876 sh
->Pragmas
= options
->DefaultPragmas
;
879 /* If the user called glCompileShader without first calling
880 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
882 sh
->CompileStatus
= GL_FALSE
;
884 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
885 fprintf(stderr
, "GLSL source for %s shader %d:\n",
886 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
887 fprintf(stderr
, "%s\n", sh
->Source
);
891 /* this call will set the shader->CompileStatus field to indicate if
892 * compilation was successful.
894 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
896 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
897 _mesa_write_shader_to_file(sh
);
900 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
901 if (sh
->CompileStatus
) {
902 fprintf(stderr
, "GLSL IR for shader %d:\n", sh
->Name
);
903 _mesa_print_ir(stderr
, sh
->ir
, NULL
);
904 fprintf(stderr
, "\n\n");
906 fprintf(stderr
, "GLSL shader %d failed to compile.\n", sh
->Name
);
908 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
909 fprintf(stderr
, "GLSL shader %d info log:\n", sh
->Name
);
910 fprintf(stderr
, "%s\n", sh
->InfoLog
);
916 if (!sh
->CompileStatus
) {
917 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
918 fprintf(stderr
, "GLSL source for %s shader %d:\n",
919 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
920 fprintf(stderr
, "%s\n", sh
->Source
);
921 fprintf(stderr
, "Info Log:\n%s\n", sh
->InfoLog
);
925 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
926 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
927 sh
->Name
, sh
->InfoLog
);
934 * Link a program's shaders.
937 link_program(struct gl_context
*ctx
, GLuint program
)
939 struct gl_shader_program
*shProg
;
941 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
945 /* From the ARB_transform_feedback2 specification:
946 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
947 * the name of a program being used by one or more transform feedback
948 * objects, even if the objects are not currently bound or are paused."
950 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
951 _mesa_error(ctx
, GL_INVALID_OPERATION
,
952 "glLinkProgram(transform feedback is using the program)");
956 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
958 _mesa_glsl_link_shader(ctx
, shProg
);
960 if (shProg
->LinkStatus
== GL_FALSE
&&
961 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
962 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
963 shProg
->Name
, shProg
->InfoLog
);
970 printf("Link %u shaders in program %u: %s\n",
971 shProg
->NumShaders
, shProg
->Name
,
972 shProg
->LinkStatus
? "Success" : "Failed");
974 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
975 printf(" shader %u, type 0x%x\n",
976 shProg
->Shaders
[i
]->Name
,
977 shProg
->Shaders
[i
]->Type
);
984 * Print basic shader info (for debug).
987 print_shader_info(const struct gl_shader_program
*shProg
)
991 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
992 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
993 printf(" %s shader %u, checksum %u\n",
994 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
995 shProg
->Shaders
[i
]->Name
,
996 shProg
->Shaders
[i
]->SourceChecksum
);
998 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
999 printf(" vert prog %u\n",
1000 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
1001 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
1002 printf(" frag prog %u\n",
1003 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
1004 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1005 printf(" geom prog %u\n",
1006 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1011 * Use the named shader program for subsequent glUniform calls
1014 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1017 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
1018 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1019 "%s(program %u not linked)", caller
, shProg
->Name
);
1023 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1024 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1030 use_shader_program(struct gl_context
*ctx
, GLenum type
,
1031 struct gl_shader_program
*shProg
,
1032 struct gl_pipeline_object
*shTarget
)
1034 struct gl_shader_program
**target
;
1035 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
1037 target
= &shTarget
->CurrentProgram
[stage
];
1038 if ((shProg
!= NULL
) && (shProg
->_LinkedShaders
[stage
] == NULL
))
1041 if (*target
!= shProg
) {
1042 /* Program is current, flush it */
1043 if (shTarget
== ctx
->_Shader
) {
1044 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1047 /* If the shader is also bound as the current rendering shader, unbind
1048 * it from that binding point as well. This ensures that the correct
1049 * semantics of glDeleteProgram are maintained.
1052 case GL_VERTEX_SHADER
:
1053 /* Empty for now. */
1055 case GL_GEOMETRY_SHADER_ARB
:
1056 /* Empty for now. */
1058 case GL_COMPUTE_SHADER
:
1059 /* Empty for now. */
1061 case GL_FRAGMENT_SHADER
:
1062 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1063 _mesa_reference_shader_program(ctx
,
1064 &ctx
->_Shader
->_CurrentFragmentProgram
,
1070 _mesa_reference_shader_program(ctx
, target
, shProg
);
1077 * Use the named shader program for subsequent rendering.
1080 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1082 use_shader_program(ctx
, GL_VERTEX_SHADER
, shProg
, &ctx
->Shader
);
1083 use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
, shProg
, &ctx
->Shader
);
1084 use_shader_program(ctx
, GL_FRAGMENT_SHADER
, shProg
, &ctx
->Shader
);
1085 use_shader_program(ctx
, GL_COMPUTE_SHADER
, shProg
, &ctx
->Shader
);
1086 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1088 if (ctx
->Driver
.UseProgram
)
1089 ctx
->Driver
.UseProgram(ctx
, shProg
);
1094 * Do validation of the given shader program.
1095 * \param errMsg returns error message if validation fails.
1096 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1099 validate_shader_program(const struct gl_shader_program
*shProg
,
1102 if (!shProg
->LinkStatus
) {
1106 /* From the GL spec, a program is invalid if any of these are true:
1108 any two active samplers in the current program object are of
1109 different types, but refer to the same texture image unit,
1111 any active sampler in the current program object refers to a texture
1112 image unit where fixed-function fragment processing accesses a
1113 texture target that does not match the sampler type, or
1115 the sum of the number of active samplers in the program and the
1116 number of texture image units enabled for fixed-function fragment
1117 processing exceeds the combined limit on the total number of texture
1118 image units allowed.
1122 * Check: any two active samplers in the current program object are of
1123 * different types, but refer to the same texture image unit,
1125 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1133 * Called via glValidateProgram()
1136 validate_program(struct gl_context
*ctx
, GLuint program
)
1138 struct gl_shader_program
*shProg
;
1139 char errMsg
[100] = "";
1141 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1146 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1147 if (!shProg
->Validated
) {
1148 /* update info log */
1149 if (shProg
->InfoLog
) {
1150 ralloc_free(shProg
->InfoLog
);
1152 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1159 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1161 GET_CURRENT_CONTEXT(ctx
);
1162 attach_shader(ctx
, program
, shader
);
1167 _mesa_AttachShader(GLuint program
, GLuint shader
)
1169 GET_CURRENT_CONTEXT(ctx
);
1170 attach_shader(ctx
, program
, shader
);
1175 _mesa_CompileShader(GLhandleARB shaderObj
)
1177 GET_CURRENT_CONTEXT(ctx
);
1178 if (MESA_VERBOSE
& VERBOSE_API
)
1179 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1180 compile_shader(ctx
, shaderObj
);
1185 _mesa_CreateShader(GLenum type
)
1187 GET_CURRENT_CONTEXT(ctx
);
1188 if (MESA_VERBOSE
& VERBOSE_API
)
1189 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type
));
1190 return create_shader(ctx
, type
);
1194 GLhandleARB GLAPIENTRY
1195 _mesa_CreateShaderObjectARB(GLenum type
)
1197 GET_CURRENT_CONTEXT(ctx
);
1198 return create_shader(ctx
, type
);
1203 _mesa_CreateProgram(void)
1205 GET_CURRENT_CONTEXT(ctx
);
1206 if (MESA_VERBOSE
& VERBOSE_API
)
1207 _mesa_debug(ctx
, "glCreateProgram\n");
1208 return create_shader_program(ctx
);
1212 GLhandleARB GLAPIENTRY
1213 _mesa_CreateProgramObjectARB(void)
1215 GET_CURRENT_CONTEXT(ctx
);
1216 return create_shader_program(ctx
);
1221 _mesa_DeleteObjectARB(GLhandleARB obj
)
1223 if (MESA_VERBOSE
& VERBOSE_API
) {
1224 GET_CURRENT_CONTEXT(ctx
);
1225 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1229 GET_CURRENT_CONTEXT(ctx
);
1230 FLUSH_VERTICES(ctx
, 0);
1231 if (is_program(ctx
, obj
)) {
1232 delete_shader_program(ctx
, obj
);
1234 else if (is_shader(ctx
, obj
)) {
1235 delete_shader(ctx
, obj
);
1245 _mesa_DeleteProgram(GLuint name
)
1248 GET_CURRENT_CONTEXT(ctx
);
1249 FLUSH_VERTICES(ctx
, 0);
1250 delete_shader_program(ctx
, name
);
1256 _mesa_DeleteShader(GLuint name
)
1259 GET_CURRENT_CONTEXT(ctx
);
1260 FLUSH_VERTICES(ctx
, 0);
1261 delete_shader(ctx
, name
);
1267 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1269 GET_CURRENT_CONTEXT(ctx
);
1270 detach_shader(ctx
, program
, shader
);
1275 _mesa_DetachShader(GLuint program
, GLuint shader
)
1277 GET_CURRENT_CONTEXT(ctx
);
1278 detach_shader(ctx
, program
, shader
);
1283 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1284 GLsizei
* count
, GLhandleARB
* obj
)
1286 GET_CURRENT_CONTEXT(ctx
);
1287 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1292 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1293 GLsizei
*count
, GLuint
*obj
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1301 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1302 GLcharARB
* infoLog
)
1304 GET_CURRENT_CONTEXT(ctx
);
1305 if (is_program(ctx
, object
)) {
1306 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1308 else if (is_shader(ctx
, object
)) {
1309 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1312 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1318 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1320 GET_CURRENT_CONTEXT(ctx
);
1321 /* Implement in terms of GetProgramiv, GetShaderiv */
1322 if (is_program(ctx
, object
)) {
1323 if (pname
== GL_OBJECT_TYPE_ARB
) {
1324 *params
= GL_PROGRAM_OBJECT_ARB
;
1327 get_programiv(ctx
, object
, pname
, params
);
1330 else if (is_shader(ctx
, object
)) {
1331 if (pname
== GL_OBJECT_TYPE_ARB
) {
1332 *params
= GL_SHADER_OBJECT_ARB
;
1335 get_shaderiv(ctx
, object
, pname
, params
);
1339 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1345 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1348 GLint iparams
[1]; /* XXX is one element enough? */
1349 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1350 params
[0] = (GLfloat
) iparams
[0];
1355 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 get_programiv(ctx
, program
, pname
, params
);
1363 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1365 GET_CURRENT_CONTEXT(ctx
);
1366 get_shaderiv(ctx
, shader
, pname
, params
);
1371 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1372 GLsizei
*length
, GLchar
*infoLog
)
1374 GET_CURRENT_CONTEXT(ctx
);
1375 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1380 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1381 GLsizei
*length
, GLchar
*infoLog
)
1383 GET_CURRENT_CONTEXT(ctx
);
1384 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1389 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1390 GLsizei
*length
, GLcharARB
*sourceOut
)
1392 GET_CURRENT_CONTEXT(ctx
);
1393 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1397 GLhandleARB GLAPIENTRY
1398 _mesa_GetHandleARB(GLenum pname
)
1400 GET_CURRENT_CONTEXT(ctx
);
1401 return get_handle(ctx
, pname
);
1405 GLboolean GLAPIENTRY
1406 _mesa_IsProgram(GLuint name
)
1408 GET_CURRENT_CONTEXT(ctx
);
1409 return is_program(ctx
, name
);
1413 GLboolean GLAPIENTRY
1414 _mesa_IsShader(GLuint name
)
1416 GET_CURRENT_CONTEXT(ctx
);
1417 return is_shader(ctx
, name
);
1422 _mesa_LinkProgram(GLhandleARB programObj
)
1424 GET_CURRENT_CONTEXT(ctx
);
1425 link_program(ctx
, programObj
);
1431 * Read shader source code from a file.
1432 * Useful for debugging to override an app's shader.
1435 read_shader(const char *fname
)
1437 int shader_size
= 0;
1438 FILE *f
= fopen(fname
, "r");
1439 GLcharARB
*buffer
, *shader
;
1446 /* allocate enough room for the entire shader */
1447 fseek(f
, 0, SEEK_END
);
1448 shader_size
= ftell(f
);
1450 assert(shader_size
);
1452 /* add one for terminating zero */
1455 buffer
= malloc(shader_size
);
1458 len
= fread(buffer
, 1, shader_size
, f
);
1463 shader
= strdup(buffer
);
1471 * Called via glShaderSource() and glShaderSourceARB() API functions.
1472 * Basically, concatenate the source code strings into one long string
1473 * and pass it to _mesa_shader_source().
1476 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1477 const GLcharARB
* const * string
, const GLint
* length
)
1479 GET_CURRENT_CONTEXT(ctx
);
1481 GLsizei i
, totalLength
;
1485 if (!shaderObj
|| string
== NULL
) {
1486 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1491 * This array holds offsets of where the appropriate string ends, thus the
1492 * last element will be set to the total length of the source code.
1494 offsets
= malloc(count
* sizeof(GLint
));
1495 if (offsets
== NULL
) {
1496 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1500 for (i
= 0; i
< count
; i
++) {
1501 if (string
[i
] == NULL
) {
1502 free((GLvoid
*) offsets
);
1503 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1504 "glShaderSourceARB(null string)");
1507 if (length
== NULL
|| length
[i
] < 0)
1508 offsets
[i
] = strlen(string
[i
]);
1510 offsets
[i
] = length
[i
];
1511 /* accumulate string lengths */
1513 offsets
[i
] += offsets
[i
- 1];
1516 /* Total length of source string is sum off all strings plus two.
1517 * One extra byte for terminating zero, another extra byte to silence
1518 * valgrind warnings in the parser/grammer code.
1520 totalLength
= offsets
[count
- 1] + 2;
1521 source
= malloc(totalLength
* sizeof(GLcharARB
));
1522 if (source
== NULL
) {
1523 free((GLvoid
*) offsets
);
1524 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1528 for (i
= 0; i
< count
; i
++) {
1529 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1530 memcpy(source
+ start
, string
[i
],
1531 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1533 source
[totalLength
- 1] = '\0';
1534 source
[totalLength
- 2] = '\0';
1537 /* Compute the shader's source code checksum then try to open a file
1538 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1539 * original shader source code. For debugging.
1542 GLcharARB
*newSource
;
1544 checksum
= _mesa_str_checksum(source
);
1546 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1548 newSource
= read_shader(filename
);
1550 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1551 shaderObj
, checksum
, filename
);
1557 shader_source(ctx
, shaderObj
, source
);
1560 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1562 sh
->SourceChecksum
= checksum
; /* save original checksum */
1570 _mesa_UseProgram(GLhandleARB program
)
1572 GET_CURRENT_CONTEXT(ctx
);
1573 struct gl_shader_program
*shProg
;
1575 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1577 "glUseProgram(transform feedback active)");
1582 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1586 if (!shProg
->LinkStatus
) {
1587 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1588 "glUseProgram(program %u not linked)", program
);
1593 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1594 print_shader_info(shProg
);
1601 /* The ARB_separate_shader_object spec says:
1603 * "The executable code for an individual shader stage is taken from
1604 * the current program for that stage. If there is a current program
1605 * object established by UseProgram, that program is considered current
1606 * for all stages. Otherwise, if there is a bound program pipeline
1607 * object (section 2.14.PPO), the program bound to the appropriate
1608 * stage of the pipeline object is considered current."
1611 /* Attach shader state to the binding point */
1612 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1613 /* Update the program */
1614 _mesa_use_program(ctx
, shProg
);
1616 /* Must be done first: detach the progam */
1617 _mesa_use_program(ctx
, shProg
);
1618 /* Unattach shader_state binding point */
1619 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1620 /* If a pipeline was bound, rebind it */
1621 if (ctx
->Pipeline
.Current
) {
1622 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1629 _mesa_ValidateProgram(GLhandleARB program
)
1631 GET_CURRENT_CONTEXT(ctx
);
1632 validate_program(ctx
, program
);
1637 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1640 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1641 GLint
* range
, GLint
* precision
)
1643 const struct gl_program_constants
*limits
;
1644 const struct gl_precision
*p
;
1645 GET_CURRENT_CONTEXT(ctx
);
1647 switch (shadertype
) {
1648 case GL_VERTEX_SHADER
:
1649 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1651 case GL_FRAGMENT_SHADER
:
1652 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1655 _mesa_error(ctx
, GL_INVALID_ENUM
,
1656 "glGetShaderPrecisionFormat(shadertype)");
1660 switch (precisiontype
) {
1662 p
= &limits
->LowFloat
;
1664 case GL_MEDIUM_FLOAT
:
1665 p
= &limits
->MediumFloat
;
1668 p
= &limits
->HighFloat
;
1671 p
= &limits
->LowInt
;
1674 p
= &limits
->MediumInt
;
1677 p
= &limits
->HighInt
;
1680 _mesa_error(ctx
, GL_INVALID_ENUM
,
1681 "glGetShaderPrecisionFormat(precisiontype)");
1685 range
[0] = p
->RangeMin
;
1686 range
[1] = p
->RangeMax
;
1687 precision
[0] = p
->Precision
;
1692 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1695 _mesa_ReleaseShaderCompiler(void)
1697 _mesa_destroy_shader_compiler_caches();
1702 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1705 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1706 const void* binary
, GLint length
)
1708 GET_CURRENT_CONTEXT(ctx
);
1711 (void) binaryformat
;
1714 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary");
1719 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1720 GLenum
*binaryFormat
, GLvoid
*binary
)
1722 struct gl_shader_program
*shProg
;
1723 GLsizei length_dummy
;
1724 GET_CURRENT_CONTEXT(ctx
);
1727 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1731 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1735 /* The ARB_get_program_binary spec says:
1737 * "If <length> is NULL, then no length is returned."
1739 * Ensure that length always points to valid storage to avoid multiple NULL
1740 * pointer checks below.
1743 length
= &length_dummy
;
1746 /* The ARB_get_program_binary spec says:
1748 * "When a program object's LINK_STATUS is FALSE, its program binary
1749 * length is zero, and a call to GetProgramBinary will generate an
1750 * INVALID_OPERATION error.
1752 if (!shProg
->LinkStatus
) {
1753 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1754 "glGetProgramBinary(program %u not linked)",
1761 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1762 "glGetProgramBinary(driver supports zero binary formats)");
1764 (void) binaryFormat
;
1769 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1770 const GLvoid
*binary
, GLsizei length
)
1772 struct gl_shader_program
*shProg
;
1773 GET_CURRENT_CONTEXT(ctx
);
1775 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1779 (void) binaryFormat
;
1782 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
1784 * "If a negative number is provided where an argument of type sizei or
1785 * sizeiptr is specified, an INVALID_VALUE error is generated."
1788 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
1792 /* The ARB_get_program_binary spec says:
1794 * "<binaryFormat> and <binary> must be those returned by a previous
1795 * call to GetProgramBinary, and <length> must be the length of the
1796 * program binary as returned by GetProgramBinary or GetProgramiv with
1797 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
1798 * setting the LINK_STATUS of <program> to FALSE, if these conditions
1801 * Since any value of binaryFormat passed "is not one of those specified as
1802 * allowable for [this] command, an INVALID_ENUM error is generated."
1804 shProg
->LinkStatus
= GL_FALSE
;
1805 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
1810 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1812 struct gl_shader_program
*shProg
;
1813 GET_CURRENT_CONTEXT(ctx
);
1815 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1816 "glProgramParameteri");
1821 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1822 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1823 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1824 * even be in the dispatch table, so we shouldn't need to expclicitly
1827 * On desktop, we ignore the 3.0+ requirement because it is silly.
1830 /* The ARB_get_program_binary extension spec says:
1832 * "An INVALID_VALUE error is generated if the <value> argument to
1833 * ProgramParameteri is not TRUE or FALSE."
1835 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1839 /* No need to notify the driver. Any changes will actually take effect
1840 * the next time the shader is linked.
1842 * The ARB_get_program_binary extension spec says:
1844 * "To indicate that a program binary is likely to be retrieved,
1845 * ProgramParameteri should be called with <pname>
1846 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1847 * will not be in effect until the next time LinkProgram or
1848 * ProgramBinary has been called successfully."
1850 * The resloution of issue 9 in the extension spec also says:
1852 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1853 * to indicate to the GL implementation that this program will
1854 * likely be saved with GetProgramBinary at some point. This will
1855 * give the GL implementation the opportunity to track any state
1856 * changes made to the program before being saved such that when it
1857 * is loaded again a recompile can be avoided."
1859 shProg
->BinaryRetreivableHint
= value
;
1862 case GL_PROGRAM_SEPARABLE
:
1863 /* Spec imply that the behavior is the same as ARB_get_program_binary
1864 * Chapter 7.3 Program Objects
1866 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1869 shProg
->SeparateShader
= value
;
1873 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1874 _mesa_lookup_enum_by_nr(pname
));
1879 _mesa_error(ctx
, GL_INVALID_VALUE
,
1880 "glProgramParameteri(pname=%s, value=%d): "
1881 "value must be 0 or 1.",
1882 _mesa_lookup_enum_by_nr(pname
),
1888 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1889 struct gl_shader_program
*shProg
,
1890 struct gl_pipeline_object
*shTarget
)
1892 use_shader_program(ctx
, type
, shProg
, shTarget
);
1894 if (ctx
->Driver
.UseProgram
)
1895 ctx
->Driver
.UseProgram(ctx
, shProg
);
1900 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1901 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1903 const GLuint shader
= create_shader(ctx
, type
);
1907 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1909 compile_shader(ctx
, shader
);
1911 program
= create_shader_program(ctx
);
1913 struct gl_shader_program
*shProg
;
1914 struct gl_shader
*sh
;
1915 GLint compiled
= GL_FALSE
;
1917 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1918 sh
= _mesa_lookup_shader(ctx
, shader
);
1920 shProg
->SeparateShader
= separate
;
1922 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1924 attach_shader(ctx
, program
, shader
);
1925 link_program(ctx
, program
);
1926 detach_shader(ctx
, program
, shader
);
1930 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1931 append
-error
-to
-info
-log
;
1932 shProg
->LinkStatus
= GL_FALSE
;
1937 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1940 delete_shader(ctx
, shader
);
1948 * Copy program-specific data generated by linking from the gl_shader_program
1949 * object to a specific gl_program object.
1952 _mesa_copy_linked_program_data(gl_shader_stage type
,
1953 const struct gl_shader_program
*src
,
1954 struct gl_program
*dst
)
1957 case MESA_SHADER_VERTEX
:
1958 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1960 case MESA_SHADER_GEOMETRY
: {
1961 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1962 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1963 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1964 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1965 dst_gp
->InputType
= src
->Geom
.InputType
;
1966 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1967 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1968 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1969 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
1972 case MESA_SHADER_FRAGMENT
: {
1973 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
1974 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
1977 case MESA_SHADER_COMPUTE
: {
1978 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1980 for (i
= 0; i
< 3; i
++)
1981 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
1990 * ARB_separate_shader_objects: Compile & Link Program
1993 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
1994 const GLchar
* const *strings
)
1996 GET_CURRENT_CONTEXT(ctx
);
1998 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);