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
;
115 memset(&options
, 0, sizeof(options
));
116 options
.MaxUnrollIterations
= 32;
117 options
.MaxIfDepth
= UINT_MAX
;
119 for (sh
= 0; sh
< MESA_SHADER_STAGES
; ++sh
)
120 memcpy(&ctx
->Const
.ShaderCompilerOptions
[sh
], &options
, sizeof(options
));
122 ctx
->Shader
.Flags
= _mesa_get_shader_flags();
124 if (ctx
->Shader
.Flags
!= 0)
125 ctx
->Const
.GenerateTemporaryNames
= true;
127 /* Extended for ARB_separate_shader_objects */
128 ctx
->Shader
.RefCount
= 1;
129 mtx_init(&ctx
->Shader
.Mutex
, mtx_plain
);
131 ctx
->TessCtrlProgram
.patch_vertices
= 3;
132 for (i
= 0; i
< 4; ++i
)
133 ctx
->TessCtrlProgram
.patch_default_outer_level
[i
] = 1.0;
134 for (i
= 0; i
< 2; ++i
)
135 ctx
->TessCtrlProgram
.patch_default_inner_level
[i
] = 1.0;
140 * Free the per-context shader-related state.
143 _mesa_free_shader_state(struct gl_context
*ctx
)
146 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
147 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.CurrentProgram
[i
],
150 _mesa_reference_shader_program(ctx
, &ctx
->Shader
._CurrentFragmentProgram
,
152 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, NULL
);
154 /* Extended for ARB_separate_shader_objects */
155 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
157 assert(ctx
->Shader
.RefCount
== 1);
158 mtx_destroy(&ctx
->Shader
.Mutex
);
163 * Copy string from <src> to <dst>, up to maxLength characters, returning
164 * length of <dst> in <length>.
165 * \param src the strings source
166 * \param maxLength max chars to copy
167 * \param length returns number of chars copied
168 * \param dst the string destination
171 _mesa_copy_string(GLchar
*dst
, GLsizei maxLength
,
172 GLsizei
*length
, const GLchar
*src
)
175 for (len
= 0; len
< maxLength
- 1 && src
&& src
[len
]; len
++)
186 * Confirm that the a shader type is valid and supported by the implementation
188 * \param ctx Current GL context
189 * \param type Shader target
193 _mesa_validate_shader_target(const struct gl_context
*ctx
, GLenum type
)
195 /* Note: when building built-in GLSL functions, this function may be
196 * invoked with ctx == NULL. In that case, we can only validate that it's
197 * a shader target we recognize, not that it's supported in the current
198 * context. But that's fine--we don't need any further validation than
199 * that when building built-in GLSL functions.
203 case GL_FRAGMENT_SHADER
:
204 return ctx
== NULL
|| ctx
->Extensions
.ARB_fragment_shader
;
205 case GL_VERTEX_SHADER
:
206 return ctx
== NULL
|| ctx
->Extensions
.ARB_vertex_shader
;
207 case GL_GEOMETRY_SHADER_ARB
:
208 return ctx
== NULL
|| _mesa_has_geometry_shaders(ctx
);
209 case GL_COMPUTE_SHADER
:
210 return ctx
== NULL
|| ctx
->Extensions
.ARB_compute_shader
;
218 is_program(struct gl_context
*ctx
, GLuint name
)
220 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, name
);
221 return shProg
? GL_TRUE
: GL_FALSE
;
226 is_shader(struct gl_context
*ctx
, GLuint name
)
228 struct gl_shader
*shader
= _mesa_lookup_shader(ctx
, name
);
229 return shader
? GL_TRUE
: GL_FALSE
;
234 * Attach shader to a shader program.
237 attach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
239 struct gl_shader_program
*shProg
;
240 struct gl_shader
*sh
;
243 const bool same_type_disallowed
= _mesa_is_gles(ctx
);
245 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glAttachShader");
249 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glAttachShader");
254 n
= shProg
->NumShaders
;
255 for (i
= 0; i
< n
; i
++) {
256 if (shProg
->Shaders
[i
] == sh
) {
257 /* The shader is already attched to this program. The
258 * GL_ARB_shader_objects spec says:
260 * "The error INVALID_OPERATION is generated by AttachObjectARB
261 * if <obj> is already attached to <containerObj>."
263 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
265 } else if (same_type_disallowed
&&
266 shProg
->Shaders
[i
]->Type
== sh
->Type
) {
267 /* Shader with the same type is already attached to this program,
268 * OpenGL ES 2.0 and 3.0 specs say:
270 * "Multiple shader objects of the same type may not be attached
271 * to a single program object. [...] The error INVALID_OPERATION
272 * is generated if [...] another shader object of the same type
273 * as shader is already attached to program."
275 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAttachShader");
281 shProg
->Shaders
= realloc(shProg
->Shaders
,
282 (n
+ 1) * sizeof(struct gl_shader
*));
283 if (!shProg
->Shaders
) {
284 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAttachShader");
289 shProg
->Shaders
[n
] = NULL
; /* since realloc() didn't zero the new space */
290 _mesa_reference_shader(ctx
, &shProg
->Shaders
[n
], sh
);
291 shProg
->NumShaders
++;
296 create_shader(struct gl_context
*ctx
, GLenum type
)
298 struct gl_shader
*sh
;
301 if (!_mesa_validate_shader_target(ctx
, type
)) {
302 _mesa_error(ctx
, GL_INVALID_ENUM
, "CreateShader(type)");
306 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
307 sh
= ctx
->Driver
.NewShader(ctx
, name
, type
);
308 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, sh
);
315 create_shader_program(struct gl_context
*ctx
)
318 struct gl_shader_program
*shProg
;
320 name
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ShaderObjects
, 1);
322 shProg
= ctx
->Driver
.NewShaderProgram(name
);
324 _mesa_HashInsert(ctx
->Shared
->ShaderObjects
, name
, shProg
);
326 assert(shProg
->RefCount
== 1);
333 * Delete a shader program. Actually, just decrement the program's
334 * reference count and mark it as DeletePending.
335 * Used to implement glDeleteProgram() and glDeleteObjectARB().
338 delete_shader_program(struct gl_context
*ctx
, GLuint name
)
341 * NOTE: deleting shaders/programs works a bit differently than
342 * texture objects (and buffer objects, etc). Shader/program
343 * handles/IDs exist in the hash table until the object is really
344 * deleted (refcount==0). With texture objects, the handle/ID is
345 * removed from the hash table in glDeleteTextures() while the tex
346 * object itself might linger until its refcount goes to zero.
348 struct gl_shader_program
*shProg
;
350 shProg
= _mesa_lookup_shader_program_err(ctx
, name
, "glDeleteProgram");
354 if (!shProg
->DeletePending
) {
355 shProg
->DeletePending
= GL_TRUE
;
357 /* effectively, decr shProg's refcount */
358 _mesa_reference_shader_program(ctx
, &shProg
, NULL
);
364 delete_shader(struct gl_context
*ctx
, GLuint shader
)
366 struct gl_shader
*sh
;
368 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glDeleteShader");
372 if (!sh
->DeletePending
) {
373 sh
->DeletePending
= GL_TRUE
;
375 /* effectively, decr sh's refcount */
376 _mesa_reference_shader(ctx
, &sh
, NULL
);
382 detach_shader(struct gl_context
*ctx
, GLuint program
, GLuint shader
)
384 struct gl_shader_program
*shProg
;
388 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glDetachShader");
392 n
= shProg
->NumShaders
;
394 for (i
= 0; i
< n
; i
++) {
395 if (shProg
->Shaders
[i
]->Name
== shader
) {
397 struct gl_shader
**newList
;
400 _mesa_reference_shader(ctx
, &shProg
->Shaders
[i
], NULL
);
402 /* alloc new, smaller array */
403 newList
= malloc((n
- 1) * sizeof(struct gl_shader
*));
405 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDetachShader");
408 /* Copy old list entries to new list, skipping removed entry at [i] */
409 for (j
= 0; j
< i
; j
++) {
410 newList
[j
] = shProg
->Shaders
[j
];
413 newList
[j
++] = shProg
->Shaders
[i
];
416 /* Free old list and install new one */
417 free(shProg
->Shaders
);
418 shProg
->Shaders
= newList
;
419 shProg
->NumShaders
= n
- 1;
422 /* sanity check - make sure the new list's entries are sensible */
423 for (j
= 0; j
< shProg
->NumShaders
; j
++) {
424 assert(shProg
->Shaders
[j
]->Type
== GL_VERTEX_SHADER
||
425 shProg
->Shaders
[j
]->Type
== GL_GEOMETRY_SHADER
||
426 shProg
->Shaders
[j
]->Type
== GL_FRAGMENT_SHADER
);
427 assert(shProg
->Shaders
[j
]->RefCount
> 0);
438 if (is_shader(ctx
, shader
) || is_program(ctx
, shader
))
439 err
= GL_INVALID_OPERATION
;
441 err
= GL_INVALID_VALUE
;
442 _mesa_error(ctx
, err
, "glDetachShader(shader)");
449 * Return list of shaders attached to shader program.
452 get_attached_shaders(struct gl_context
*ctx
, GLuint program
, GLsizei maxCount
,
453 GLsizei
*count
, GLuint
*obj
)
455 struct gl_shader_program
*shProg
;
458 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetAttachedShaders(maxCount < 0)");
463 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttachedShaders");
467 for (i
= 0; i
< (GLuint
) maxCount
&& i
< shProg
->NumShaders
; i
++) {
468 obj
[i
] = shProg
->Shaders
[i
]->Name
;
477 * glGetHandleARB() - return ID/name of currently bound shader program.
480 get_handle(struct gl_context
*ctx
, GLenum pname
)
482 if (pname
== GL_PROGRAM_OBJECT_ARB
) {
483 if (ctx
->_Shader
->ActiveProgram
)
484 return ctx
->_Shader
->ActiveProgram
->Name
;
489 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetHandleARB");
496 * Check if a geometry shader query is valid at this time. If not, report an
497 * error and return false.
499 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
501 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
502 * are queried for a program which has not been linked successfully, or
503 * which does not contain objects to form a geometry shader, then an
504 * INVALID_OPERATION error is generated."
507 check_gs_query(struct gl_context
*ctx
, const struct gl_shader_program
*shProg
)
509 if (shProg
->LinkStatus
&&
510 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
] != NULL
) {
514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
515 "glGetProgramv(linked geometry shader required)");
521 * glGetProgramiv() - get shader program state.
522 * Note that this is for GLSL shader programs, not ARB vertex/fragment
523 * programs (see glGetProgramivARB).
526 get_programiv(struct gl_context
*ctx
, GLuint program
, GLenum pname
,
529 struct gl_shader_program
*shProg
530 = _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramiv(program)");
532 /* Is transform feedback available in this context?
535 (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_transform_feedback
)
536 || ctx
->API
== API_OPENGL_CORE
537 || _mesa_is_gles3(ctx
);
539 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
540 * and GL 3.2) are available in this context
542 const bool has_core_gs
= _mesa_has_geometry_shaders(ctx
);
544 /* Are uniform buffer objects available in this context?
547 (ctx
->API
== API_OPENGL_COMPAT
&&
548 ctx
->Extensions
.ARB_uniform_buffer_object
)
549 || ctx
->API
== API_OPENGL_CORE
550 || _mesa_is_gles3(ctx
);
557 case GL_DELETE_STATUS
:
558 *params
= shProg
->DeletePending
;
561 *params
= shProg
->LinkStatus
;
563 case GL_VALIDATE_STATUS
:
564 *params
= shProg
->Validated
;
566 case GL_INFO_LOG_LENGTH
:
567 *params
= shProg
->InfoLog
? strlen(shProg
->InfoLog
) + 1 : 0;
569 case GL_ATTACHED_SHADERS
:
570 *params
= shProg
->NumShaders
;
572 case GL_ACTIVE_ATTRIBUTES
:
573 *params
= _mesa_count_active_attribs(shProg
);
575 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
576 *params
= _mesa_longest_attribute_name_length(shProg
);
578 case GL_ACTIVE_UNIFORMS
:
579 *params
= shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
581 case GL_ACTIVE_UNIFORM_MAX_LENGTH
: {
584 const unsigned num_uniforms
=
585 shProg
->NumUniformStorage
- shProg
->NumHiddenUniforms
;
587 for (i
= 0; i
< num_uniforms
; i
++) {
588 /* Add one for the terminating NUL character for a non-array, and
589 * 4 for the "[0]" and the NUL for an array.
591 const GLint len
= strlen(shProg
->UniformStorage
[i
].name
) + 1 +
592 ((shProg
->UniformStorage
[i
].array_elements
!= 0) ? 3 : 0);
601 case GL_TRANSFORM_FEEDBACK_VARYINGS
:
604 *params
= shProg
->TransformFeedback
.NumVarying
;
606 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH
: {
612 for (i
= 0; i
< shProg
->TransformFeedback
.NumVarying
; i
++) {
613 /* Add one for the terminating NUL character.
616 strlen(shProg
->TransformFeedback
.VaryingNames
[i
]) + 1;
625 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE
:
628 *params
= shProg
->TransformFeedback
.BufferMode
;
630 case GL_GEOMETRY_VERTICES_OUT
:
633 if (check_gs_query(ctx
, shProg
))
634 *params
= shProg
->Geom
.VerticesOut
;
636 case GL_GEOMETRY_SHADER_INVOCATIONS
:
637 if (!has_core_gs
|| !ctx
->Extensions
.ARB_gpu_shader5
)
639 if (check_gs_query(ctx
, shProg
))
640 *params
= shProg
->Geom
.Invocations
;
642 case GL_GEOMETRY_INPUT_TYPE
:
645 if (check_gs_query(ctx
, shProg
))
646 *params
= shProg
->Geom
.InputType
;
648 case GL_GEOMETRY_OUTPUT_TYPE
:
651 if (check_gs_query(ctx
, shProg
))
652 *params
= shProg
->Geom
.OutputType
;
654 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH
: {
661 for (i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
662 /* Add one for the terminating NUL character.
664 const GLint len
= strlen(shProg
->UniformBlocks
[i
].Name
) + 1;
673 case GL_ACTIVE_UNIFORM_BLOCKS
:
677 *params
= shProg
->NumUniformBlocks
;
679 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
680 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
681 * only available with desktop OpenGL 3.0+ with the
682 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
684 * On desktop, we ignore the 3.0+ requirement because it is silly.
686 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
689 *params
= shProg
->BinaryRetreivableHint
;
691 case GL_PROGRAM_BINARY_LENGTH
:
694 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS
:
695 if (!ctx
->Extensions
.ARB_shader_atomic_counters
)
698 *params
= shProg
->NumAtomicBuffers
;
700 case GL_COMPUTE_WORK_GROUP_SIZE
: {
702 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_compute_shader
)
704 if (!shProg
->LinkStatus
) {
705 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(program not "
709 if (shProg
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
710 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramiv(no compute "
714 for (i
= 0; i
< 3; i
++)
715 params
[i
] = shProg
->Comp
.LocalSize
[i
];
718 case GL_PROGRAM_SEPARABLE
:
719 *params
= shProg
->SeparateShader
;
725 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramiv(pname=%s)",
726 _mesa_enum_to_string(pname
));
731 * glGetShaderiv() - get GLSL shader state
734 get_shaderiv(struct gl_context
*ctx
, GLuint name
, GLenum pname
, GLint
*params
)
736 struct gl_shader
*shader
=
737 _mesa_lookup_shader_err(ctx
, name
, "glGetShaderiv");
745 *params
= shader
->Type
;
747 case GL_DELETE_STATUS
:
748 *params
= shader
->DeletePending
;
750 case GL_COMPILE_STATUS
:
751 *params
= shader
->CompileStatus
;
753 case GL_INFO_LOG_LENGTH
:
754 *params
= shader
->InfoLog
? strlen(shader
->InfoLog
) + 1 : 0;
756 case GL_SHADER_SOURCE_LENGTH
:
757 *params
= shader
->Source
? strlen((char *) shader
->Source
) + 1 : 0;
760 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetShaderiv(pname)");
767 get_program_info_log(struct gl_context
*ctx
, GLuint program
, GLsizei bufSize
,
768 GLsizei
*length
, GLchar
*infoLog
)
770 struct gl_shader_program
*shProg
;
772 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
773 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
775 * "If a negative number is provided where an argument of type sizei or
776 * sizeiptr is specified, an INVALID_VALUE error is generated."
779 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramInfoLog(bufSize < 0)");
783 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
784 "glGetProgramInfoLog(program)");
789 _mesa_copy_string(infoLog
, bufSize
, length
, shProg
->InfoLog
);
794 get_shader_info_log(struct gl_context
*ctx
, GLuint shader
, GLsizei bufSize
,
795 GLsizei
*length
, GLchar
*infoLog
)
797 struct gl_shader
*sh
;
799 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
800 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
802 * "If a negative number is provided where an argument of type sizei or
803 * sizeiptr is specified, an INVALID_VALUE error is generated."
806 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderInfoLog(bufSize < 0)");
810 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderInfoLog(shader)");
815 _mesa_copy_string(infoLog
, bufSize
, length
, sh
->InfoLog
);
820 * Return shader source code.
823 get_shader_source(struct gl_context
*ctx
, GLuint shader
, GLsizei maxLength
,
824 GLsizei
*length
, GLchar
*sourceOut
)
826 struct gl_shader
*sh
;
829 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetShaderSource(bufSize < 0)");
833 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glGetShaderSource");
837 _mesa_copy_string(sourceOut
, maxLength
, length
, sh
->Source
);
842 * Set/replace shader source code. A helper function used by
843 * glShaderSource[ARB].
846 shader_source(struct gl_context
*ctx
, GLuint shader
, const GLchar
*source
)
848 struct gl_shader
*sh
;
850 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glShaderSource");
854 /* free old shader source string and install new one */
855 free((void *)sh
->Source
);
857 sh
->CompileStatus
= GL_FALSE
;
859 sh
->SourceChecksum
= _mesa_str_checksum(sh
->Source
);
868 compile_shader(struct gl_context
*ctx
, GLuint shaderObj
)
870 struct gl_shader
*sh
;
872 sh
= _mesa_lookup_shader_err(ctx
, shaderObj
, "glCompileShader");
877 /* If the user called glCompileShader without first calling
878 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
880 sh
->CompileStatus
= GL_FALSE
;
882 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
883 _mesa_log("GLSL source for %s shader %d:\n",
884 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
885 _mesa_log("%s\n", sh
->Source
);
888 /* this call will set the shader->CompileStatus field to indicate if
889 * compilation was successful.
891 _mesa_glsl_compile_shader(ctx
, sh
, false, false);
893 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
894 _mesa_write_shader_to_file(sh
);
897 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
898 if (sh
->CompileStatus
) {
899 _mesa_log("GLSL IR for shader %d:\n", sh
->Name
);
900 _mesa_print_ir(_mesa_get_log_file(), sh
->ir
, NULL
);
903 _mesa_log("GLSL shader %d failed to compile.\n", sh
->Name
);
905 if (sh
->InfoLog
&& sh
->InfoLog
[0] != 0) {
906 _mesa_log("GLSL shader %d info log:\n", sh
->Name
);
907 _mesa_log("%s\n", sh
->InfoLog
);
912 if (!sh
->CompileStatus
) {
913 if (ctx
->_Shader
->Flags
& GLSL_DUMP_ON_ERROR
) {
914 _mesa_log("GLSL source for %s shader %d:\n",
915 _mesa_shader_stage_to_string(sh
->Stage
), sh
->Name
);
916 _mesa_log("%s\n", sh
->Source
);
917 _mesa_log("Info Log:\n%s\n", sh
->InfoLog
);
920 if (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
) {
921 _mesa_debug(ctx
, "Error compiling shader %u:\n%s\n",
922 sh
->Name
, sh
->InfoLog
);
929 * Link a program's shaders.
932 link_program(struct gl_context
*ctx
, GLuint program
)
934 struct gl_shader_program
*shProg
;
936 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glLinkProgram");
940 /* From the ARB_transform_feedback2 specification:
941 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
942 * the name of a program being used by one or more transform feedback
943 * objects, even if the objects are not currently bound or are paused."
945 if (_mesa_transform_feedback_is_using_program(ctx
, shProg
)) {
946 _mesa_error(ctx
, GL_INVALID_OPERATION
,
947 "glLinkProgram(transform feedback is using the program)");
951 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
953 _mesa_glsl_link_shader(ctx
, shProg
);
955 if (shProg
->LinkStatus
== GL_FALSE
&&
956 (ctx
->_Shader
->Flags
& GLSL_REPORT_ERRORS
)) {
957 _mesa_debug(ctx
, "Error linking program %u:\n%s\n",
958 shProg
->Name
, shProg
->InfoLog
);
965 printf("Link %u shaders in program %u: %s\n",
966 shProg
->NumShaders
, shProg
->Name
,
967 shProg
->LinkStatus
? "Success" : "Failed");
969 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
970 printf(" shader %u, type 0x%x\n",
971 shProg
->Shaders
[i
]->Name
,
972 shProg
->Shaders
[i
]->Type
);
979 * Print basic shader info (for debug).
982 print_shader_info(const struct gl_shader_program
*shProg
)
986 printf("Mesa: glUseProgram(%u)\n", shProg
->Name
);
987 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
988 printf(" %s shader %u, checksum %u\n",
989 _mesa_shader_stage_to_string(shProg
->Shaders
[i
]->Stage
),
990 shProg
->Shaders
[i
]->Name
,
991 shProg
->Shaders
[i
]->SourceChecksum
);
993 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
])
994 printf(" vert prog %u\n",
995 shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
->Id
);
996 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
])
997 printf(" frag prog %u\n",
998 shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->Id
);
999 if (shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
])
1000 printf(" geom prog %u\n",
1001 shProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
->Id
);
1006 * Use the named shader program for subsequent glUniform calls
1009 _mesa_active_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
1012 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
1013 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1014 "%s(program %u not linked)", caller
, shProg
->Name
);
1018 if (ctx
->Shader
.ActiveProgram
!= shProg
) {
1019 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
, shProg
);
1025 use_shader_program(struct gl_context
*ctx
, gl_shader_stage stage
,
1026 struct gl_shader_program
*shProg
,
1027 struct gl_pipeline_object
*shTarget
)
1029 struct gl_shader_program
**target
;
1031 target
= &shTarget
->CurrentProgram
[stage
];
1032 if ((shProg
!= NULL
) && (shProg
->_LinkedShaders
[stage
] == NULL
))
1035 if (*target
!= shProg
) {
1036 /* Program is current, flush it */
1037 if (shTarget
== ctx
->_Shader
) {
1038 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1041 /* If the shader is also bound as the current rendering shader, unbind
1042 * it from that binding point as well. This ensures that the correct
1043 * semantics of glDeleteProgram are maintained.
1046 case MESA_SHADER_VERTEX
:
1047 /* Empty for now. */
1049 case MESA_SHADER_GEOMETRY
:
1050 /* Empty for now. */
1052 case MESA_SHADER_COMPUTE
:
1053 /* Empty for now. */
1055 case MESA_SHADER_FRAGMENT
:
1056 if (*target
== ctx
->_Shader
->_CurrentFragmentProgram
) {
1057 _mesa_reference_shader_program(ctx
,
1058 &ctx
->_Shader
->_CurrentFragmentProgram
,
1064 _mesa_reference_shader_program(ctx
, target
, shProg
);
1071 * Use the named shader program for subsequent rendering.
1074 _mesa_use_program(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
1077 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
1078 use_shader_program(ctx
, i
, shProg
, &ctx
->Shader
);
1079 _mesa_active_program(ctx
, shProg
, "glUseProgram");
1081 if (ctx
->Driver
.UseProgram
)
1082 ctx
->Driver
.UseProgram(ctx
, shProg
);
1087 * Do validation of the given shader program.
1088 * \param errMsg returns error message if validation fails.
1089 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1092 validate_shader_program(const struct gl_shader_program
*shProg
,
1095 if (!shProg
->LinkStatus
) {
1099 /* From the GL spec, a program is invalid if any of these are true:
1101 any two active samplers in the current program object are of
1102 different types, but refer to the same texture image unit,
1104 any active sampler in the current program object refers to a texture
1105 image unit where fixed-function fragment processing accesses a
1106 texture target that does not match the sampler type, or
1108 the sum of the number of active samplers in the program and the
1109 number of texture image units enabled for fixed-function fragment
1110 processing exceeds the combined limit on the total number of texture
1111 image units allowed.
1115 * Check: any two active samplers in the current program object are of
1116 * different types, but refer to the same texture image unit,
1118 if (!_mesa_sampler_uniforms_are_valid(shProg
, errMsg
, 100))
1126 * Called via glValidateProgram()
1129 validate_program(struct gl_context
*ctx
, GLuint program
)
1131 struct gl_shader_program
*shProg
;
1132 char errMsg
[100] = "";
1134 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glValidateProgram");
1139 shProg
->Validated
= validate_shader_program(shProg
, errMsg
);
1140 if (!shProg
->Validated
) {
1141 /* update info log */
1142 if (shProg
->InfoLog
) {
1143 ralloc_free(shProg
->InfoLog
);
1145 shProg
->InfoLog
= ralloc_strdup(shProg
, errMsg
);
1152 _mesa_AttachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1154 GET_CURRENT_CONTEXT(ctx
);
1155 attach_shader(ctx
, program
, shader
);
1160 _mesa_AttachShader(GLuint program
, GLuint shader
)
1162 GET_CURRENT_CONTEXT(ctx
);
1163 attach_shader(ctx
, program
, shader
);
1168 _mesa_CompileShader(GLhandleARB shaderObj
)
1170 GET_CURRENT_CONTEXT(ctx
);
1171 if (MESA_VERBOSE
& VERBOSE_API
)
1172 _mesa_debug(ctx
, "glCompileShader %u\n", shaderObj
);
1173 compile_shader(ctx
, shaderObj
);
1178 _mesa_CreateShader(GLenum type
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 if (MESA_VERBOSE
& VERBOSE_API
)
1182 _mesa_debug(ctx
, "glCreateShader %s\n", _mesa_enum_to_string(type
));
1183 return create_shader(ctx
, type
);
1187 GLhandleARB GLAPIENTRY
1188 _mesa_CreateShaderObjectARB(GLenum type
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 return create_shader(ctx
, type
);
1196 _mesa_CreateProgram(void)
1198 GET_CURRENT_CONTEXT(ctx
);
1199 if (MESA_VERBOSE
& VERBOSE_API
)
1200 _mesa_debug(ctx
, "glCreateProgram\n");
1201 return create_shader_program(ctx
);
1205 GLhandleARB GLAPIENTRY
1206 _mesa_CreateProgramObjectARB(void)
1208 GET_CURRENT_CONTEXT(ctx
);
1209 return create_shader_program(ctx
);
1214 _mesa_DeleteObjectARB(GLhandleARB obj
)
1216 if (MESA_VERBOSE
& VERBOSE_API
) {
1217 GET_CURRENT_CONTEXT(ctx
);
1218 _mesa_debug(ctx
, "glDeleteObjectARB(%u)\n", obj
);
1222 GET_CURRENT_CONTEXT(ctx
);
1223 FLUSH_VERTICES(ctx
, 0);
1224 if (is_program(ctx
, obj
)) {
1225 delete_shader_program(ctx
, obj
);
1227 else if (is_shader(ctx
, obj
)) {
1228 delete_shader(ctx
, obj
);
1238 _mesa_DeleteProgram(GLuint name
)
1241 GET_CURRENT_CONTEXT(ctx
);
1242 FLUSH_VERTICES(ctx
, 0);
1243 delete_shader_program(ctx
, name
);
1249 _mesa_DeleteShader(GLuint name
)
1252 GET_CURRENT_CONTEXT(ctx
);
1253 FLUSH_VERTICES(ctx
, 0);
1254 delete_shader(ctx
, name
);
1260 _mesa_DetachObjectARB(GLhandleARB program
, GLhandleARB shader
)
1262 GET_CURRENT_CONTEXT(ctx
);
1263 detach_shader(ctx
, program
, shader
);
1268 _mesa_DetachShader(GLuint program
, GLuint shader
)
1270 GET_CURRENT_CONTEXT(ctx
);
1271 detach_shader(ctx
, program
, shader
);
1276 _mesa_GetAttachedObjectsARB(GLhandleARB container
, GLsizei maxCount
,
1277 GLsizei
* count
, GLhandleARB
* obj
)
1279 GET_CURRENT_CONTEXT(ctx
);
1280 get_attached_shaders(ctx
, container
, maxCount
, count
, obj
);
1285 _mesa_GetAttachedShaders(GLuint program
, GLsizei maxCount
,
1286 GLsizei
*count
, GLuint
*obj
)
1288 GET_CURRENT_CONTEXT(ctx
);
1289 get_attached_shaders(ctx
, program
, maxCount
, count
, obj
);
1294 _mesa_GetInfoLogARB(GLhandleARB object
, GLsizei maxLength
, GLsizei
* length
,
1295 GLcharARB
* infoLog
)
1297 GET_CURRENT_CONTEXT(ctx
);
1298 if (is_program(ctx
, object
)) {
1299 get_program_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1301 else if (is_shader(ctx
, object
)) {
1302 get_shader_info_log(ctx
, object
, maxLength
, length
, infoLog
);
1305 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetInfoLogARB");
1311 _mesa_GetObjectParameterivARB(GLhandleARB object
, GLenum pname
, GLint
*params
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 /* Implement in terms of GetProgramiv, GetShaderiv */
1315 if (is_program(ctx
, object
)) {
1316 if (pname
== GL_OBJECT_TYPE_ARB
) {
1317 *params
= GL_PROGRAM_OBJECT_ARB
;
1320 get_programiv(ctx
, object
, pname
, params
);
1323 else if (is_shader(ctx
, object
)) {
1324 if (pname
== GL_OBJECT_TYPE_ARB
) {
1325 *params
= GL_SHADER_OBJECT_ARB
;
1328 get_shaderiv(ctx
, object
, pname
, params
);
1332 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetObjectParameterivARB");
1338 _mesa_GetObjectParameterfvARB(GLhandleARB object
, GLenum pname
,
1341 GLint iparams
[1]; /* XXX is one element enough? */
1342 _mesa_GetObjectParameterivARB(object
, pname
, iparams
);
1343 params
[0] = (GLfloat
) iparams
[0];
1348 _mesa_GetProgramiv(GLuint program
, GLenum pname
, GLint
*params
)
1350 GET_CURRENT_CONTEXT(ctx
);
1351 get_programiv(ctx
, program
, pname
, params
);
1356 _mesa_GetShaderiv(GLuint shader
, GLenum pname
, GLint
*params
)
1358 GET_CURRENT_CONTEXT(ctx
);
1359 get_shaderiv(ctx
, shader
, pname
, params
);
1364 _mesa_GetProgramInfoLog(GLuint program
, GLsizei bufSize
,
1365 GLsizei
*length
, GLchar
*infoLog
)
1367 GET_CURRENT_CONTEXT(ctx
);
1368 get_program_info_log(ctx
, program
, bufSize
, length
, infoLog
);
1373 _mesa_GetShaderInfoLog(GLuint shader
, GLsizei bufSize
,
1374 GLsizei
*length
, GLchar
*infoLog
)
1376 GET_CURRENT_CONTEXT(ctx
);
1377 get_shader_info_log(ctx
, shader
, bufSize
, length
, infoLog
);
1382 _mesa_GetShaderSource(GLhandleARB shader
, GLsizei maxLength
,
1383 GLsizei
*length
, GLcharARB
*sourceOut
)
1385 GET_CURRENT_CONTEXT(ctx
);
1386 get_shader_source(ctx
, shader
, maxLength
, length
, sourceOut
);
1390 GLhandleARB GLAPIENTRY
1391 _mesa_GetHandleARB(GLenum pname
)
1393 GET_CURRENT_CONTEXT(ctx
);
1394 return get_handle(ctx
, pname
);
1398 GLboolean GLAPIENTRY
1399 _mesa_IsProgram(GLuint name
)
1401 GET_CURRENT_CONTEXT(ctx
);
1402 return is_program(ctx
, name
);
1406 GLboolean GLAPIENTRY
1407 _mesa_IsShader(GLuint name
)
1409 GET_CURRENT_CONTEXT(ctx
);
1410 return is_shader(ctx
, name
);
1415 _mesa_LinkProgram(GLhandleARB programObj
)
1417 GET_CURRENT_CONTEXT(ctx
);
1418 link_program(ctx
, programObj
);
1424 * Read shader source code from a file.
1425 * Useful for debugging to override an app's shader.
1428 read_shader(const char *fname
)
1430 int shader_size
= 0;
1431 FILE *f
= fopen(fname
, "r");
1432 GLcharARB
*buffer
, *shader
;
1439 /* allocate enough room for the entire shader */
1440 fseek(f
, 0, SEEK_END
);
1441 shader_size
= ftell(f
);
1443 assert(shader_size
);
1445 /* add one for terminating zero */
1448 buffer
= malloc(shader_size
);
1451 len
= fread(buffer
, 1, shader_size
, f
);
1456 shader
= strdup(buffer
);
1464 * Called via glShaderSource() and glShaderSourceARB() API functions.
1465 * Basically, concatenate the source code strings into one long string
1466 * and pass it to _mesa_shader_source().
1469 _mesa_ShaderSource(GLhandleARB shaderObj
, GLsizei count
,
1470 const GLcharARB
* const * string
, const GLint
* length
)
1472 GET_CURRENT_CONTEXT(ctx
);
1474 GLsizei i
, totalLength
;
1478 if (!shaderObj
|| string
== NULL
) {
1479 _mesa_error(ctx
, GL_INVALID_VALUE
, "glShaderSourceARB");
1484 * This array holds offsets of where the appropriate string ends, thus the
1485 * last element will be set to the total length of the source code.
1487 offsets
= malloc(count
* sizeof(GLint
));
1488 if (offsets
== NULL
) {
1489 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1493 for (i
= 0; i
< count
; i
++) {
1494 if (string
[i
] == NULL
) {
1495 free((GLvoid
*) offsets
);
1496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1497 "glShaderSourceARB(null string)");
1500 if (length
== NULL
|| length
[i
] < 0)
1501 offsets
[i
] = strlen(string
[i
]);
1503 offsets
[i
] = length
[i
];
1504 /* accumulate string lengths */
1506 offsets
[i
] += offsets
[i
- 1];
1509 /* Total length of source string is sum off all strings plus two.
1510 * One extra byte for terminating zero, another extra byte to silence
1511 * valgrind warnings in the parser/grammer code.
1513 totalLength
= offsets
[count
- 1] + 2;
1514 source
= malloc(totalLength
* sizeof(GLcharARB
));
1515 if (source
== NULL
) {
1516 free((GLvoid
*) offsets
);
1517 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderSourceARB");
1521 for (i
= 0; i
< count
; i
++) {
1522 GLint start
= (i
> 0) ? offsets
[i
- 1] : 0;
1523 memcpy(source
+ start
, string
[i
],
1524 (offsets
[i
] - start
) * sizeof(GLcharARB
));
1526 source
[totalLength
- 1] = '\0';
1527 source
[totalLength
- 2] = '\0';
1530 /* Compute the shader's source code checksum then try to open a file
1531 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1532 * original shader source code. For debugging.
1535 GLcharARB
*newSource
;
1537 checksum
= _mesa_str_checksum(source
);
1539 _mesa_snprintf(filename
, sizeof(filename
), "newshader_%d", checksum
);
1541 newSource
= read_shader(filename
);
1543 fprintf(stderr
, "Mesa: Replacing shader %u chksum=%d with %s\n",
1544 shaderObj
, checksum
, filename
);
1550 shader_source(ctx
, shaderObj
, source
);
1553 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shaderObj
);
1555 sh
->SourceChecksum
= checksum
; /* save original checksum */
1563 _mesa_UseProgram(GLhandleARB program
)
1565 GET_CURRENT_CONTEXT(ctx
);
1566 struct gl_shader_program
*shProg
;
1568 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1569 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1570 "glUseProgram(transform feedback active)");
1575 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glUseProgram");
1579 if (!shProg
->LinkStatus
) {
1580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1581 "glUseProgram(program %u not linked)", program
);
1586 if (ctx
->_Shader
->Flags
& GLSL_USE_PROG
) {
1587 print_shader_info(shProg
);
1594 /* The ARB_separate_shader_object spec says:
1596 * "The executable code for an individual shader stage is taken from
1597 * the current program for that stage. If there is a current program
1598 * object established by UseProgram, that program is considered current
1599 * for all stages. Otherwise, if there is a bound program pipeline
1600 * object (section 2.14.PPO), the program bound to the appropriate
1601 * stage of the pipeline object is considered current."
1604 /* Attach shader state to the binding point */
1605 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
1606 /* Update the program */
1607 _mesa_use_program(ctx
, shProg
);
1609 /* Must be done first: detach the progam */
1610 _mesa_use_program(ctx
, shProg
);
1611 /* Unattach shader_state binding point */
1612 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
1613 /* If a pipeline was bound, rebind it */
1614 if (ctx
->Pipeline
.Current
) {
1615 _mesa_BindProgramPipeline(ctx
->Pipeline
.Current
->Name
);
1622 _mesa_ValidateProgram(GLhandleARB program
)
1624 GET_CURRENT_CONTEXT(ctx
);
1625 validate_program(ctx
, program
);
1630 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1633 _mesa_GetShaderPrecisionFormat(GLenum shadertype
, GLenum precisiontype
,
1634 GLint
* range
, GLint
* precision
)
1636 const struct gl_program_constants
*limits
;
1637 const struct gl_precision
*p
;
1638 GET_CURRENT_CONTEXT(ctx
);
1640 switch (shadertype
) {
1641 case GL_VERTEX_SHADER
:
1642 limits
= &ctx
->Const
.Program
[MESA_SHADER_VERTEX
];
1644 case GL_FRAGMENT_SHADER
:
1645 limits
= &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
1648 _mesa_error(ctx
, GL_INVALID_ENUM
,
1649 "glGetShaderPrecisionFormat(shadertype)");
1653 switch (precisiontype
) {
1655 p
= &limits
->LowFloat
;
1657 case GL_MEDIUM_FLOAT
:
1658 p
= &limits
->MediumFloat
;
1661 p
= &limits
->HighFloat
;
1664 p
= &limits
->LowInt
;
1667 p
= &limits
->MediumInt
;
1670 p
= &limits
->HighInt
;
1673 _mesa_error(ctx
, GL_INVALID_ENUM
,
1674 "glGetShaderPrecisionFormat(precisiontype)");
1678 range
[0] = p
->RangeMin
;
1679 range
[1] = p
->RangeMax
;
1680 precision
[0] = p
->Precision
;
1685 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1688 _mesa_ReleaseShaderCompiler(void)
1690 _mesa_destroy_shader_compiler_caches();
1695 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1698 _mesa_ShaderBinary(GLint n
, const GLuint
* shaders
, GLenum binaryformat
,
1699 const void* binary
, GLint length
)
1701 GET_CURRENT_CONTEXT(ctx
);
1704 (void) binaryformat
;
1707 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glShaderBinary");
1712 _mesa_GetProgramBinary(GLuint program
, GLsizei bufSize
, GLsizei
*length
,
1713 GLenum
*binaryFormat
, GLvoid
*binary
)
1715 struct gl_shader_program
*shProg
;
1716 GLsizei length_dummy
;
1717 GET_CURRENT_CONTEXT(ctx
);
1720 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetProgramBinary(bufSize < 0)");
1724 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetProgramBinary");
1728 /* The ARB_get_program_binary spec says:
1730 * "If <length> is NULL, then no length is returned."
1732 * Ensure that length always points to valid storage to avoid multiple NULL
1733 * pointer checks below.
1736 length
= &length_dummy
;
1739 /* The ARB_get_program_binary spec says:
1741 * "When a program object's LINK_STATUS is FALSE, its program binary
1742 * length is zero, and a call to GetProgramBinary will generate an
1743 * INVALID_OPERATION error.
1745 if (!shProg
->LinkStatus
) {
1746 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1747 "glGetProgramBinary(program %u not linked)",
1754 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1755 "glGetProgramBinary(driver supports zero binary formats)");
1757 (void) binaryFormat
;
1762 _mesa_ProgramBinary(GLuint program
, GLenum binaryFormat
,
1763 const GLvoid
*binary
, GLsizei length
)
1765 struct gl_shader_program
*shProg
;
1766 GET_CURRENT_CONTEXT(ctx
);
1768 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glProgramBinary");
1772 (void) binaryFormat
;
1775 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
1777 * "If a negative number is provided where an argument of type sizei or
1778 * sizeiptr is specified, an INVALID_VALUE error is generated."
1781 _mesa_error(ctx
, GL_INVALID_VALUE
, "glProgramBinary(length < 0)");
1785 /* The ARB_get_program_binary spec says:
1787 * "<binaryFormat> and <binary> must be those returned by a previous
1788 * call to GetProgramBinary, and <length> must be the length of the
1789 * program binary as returned by GetProgramBinary or GetProgramiv with
1790 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
1791 * setting the LINK_STATUS of <program> to FALSE, if these conditions
1794 * Since any value of binaryFormat passed "is not one of those specified as
1795 * allowable for [this] command, an INVALID_ENUM error is generated."
1797 shProg
->LinkStatus
= GL_FALSE
;
1798 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramBinary");
1803 _mesa_ProgramParameteri(GLuint program
, GLenum pname
, GLint value
)
1805 struct gl_shader_program
*shProg
;
1806 GET_CURRENT_CONTEXT(ctx
);
1808 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1809 "glProgramParameteri");
1814 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT
:
1815 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1816 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1817 * even be in the dispatch table, so we shouldn't need to expclicitly
1820 * On desktop, we ignore the 3.0+ requirement because it is silly.
1823 /* The ARB_get_program_binary extension spec says:
1825 * "An INVALID_VALUE error is generated if the <value> argument to
1826 * ProgramParameteri is not TRUE or FALSE."
1828 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1832 /* No need to notify the driver. Any changes will actually take effect
1833 * the next time the shader is linked.
1835 * The ARB_get_program_binary extension spec says:
1837 * "To indicate that a program binary is likely to be retrieved,
1838 * ProgramParameteri should be called with <pname>
1839 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1840 * will not be in effect until the next time LinkProgram or
1841 * ProgramBinary has been called successfully."
1843 * The resloution of issue 9 in the extension spec also says:
1845 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1846 * to indicate to the GL implementation that this program will
1847 * likely be saved with GetProgramBinary at some point. This will
1848 * give the GL implementation the opportunity to track any state
1849 * changes made to the program before being saved such that when it
1850 * is loaded again a recompile can be avoided."
1852 shProg
->BinaryRetreivableHint
= value
;
1855 case GL_PROGRAM_SEPARABLE
:
1856 /* Spec imply that the behavior is the same as ARB_get_program_binary
1857 * Chapter 7.3 Program Objects
1859 if (value
!= GL_TRUE
&& value
!= GL_FALSE
) {
1862 shProg
->SeparateShader
= value
;
1866 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramParameteri(pname=%s)",
1867 _mesa_enum_to_string(pname
));
1872 _mesa_error(ctx
, GL_INVALID_VALUE
,
1873 "glProgramParameteri(pname=%s, value=%d): "
1874 "value must be 0 or 1.",
1875 _mesa_enum_to_string(pname
),
1881 _mesa_use_shader_program(struct gl_context
*ctx
, GLenum type
,
1882 struct gl_shader_program
*shProg
,
1883 struct gl_pipeline_object
*shTarget
)
1885 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
1886 use_shader_program(ctx
, stage
, shProg
, shTarget
);
1888 if (ctx
->Driver
.UseProgram
)
1889 ctx
->Driver
.UseProgram(ctx
, shProg
);
1894 _mesa_create_shader_program(struct gl_context
* ctx
, GLboolean separate
,
1895 GLenum type
, GLsizei count
, const GLchar
* const *strings
)
1897 const GLuint shader
= create_shader(ctx
, type
);
1901 _mesa_ShaderSource(shader
, count
, strings
, NULL
);
1903 compile_shader(ctx
, shader
);
1905 program
= create_shader_program(ctx
);
1907 struct gl_shader_program
*shProg
;
1908 struct gl_shader
*sh
;
1909 GLint compiled
= GL_FALSE
;
1911 shProg
= _mesa_lookup_shader_program(ctx
, program
);
1912 sh
= _mesa_lookup_shader(ctx
, shader
);
1914 shProg
->SeparateShader
= separate
;
1916 get_shaderiv(ctx
, shader
, GL_COMPILE_STATUS
, &compiled
);
1918 attach_shader(ctx
, program
, shader
);
1919 link_program(ctx
, program
);
1920 detach_shader(ctx
, program
, shader
);
1924 if (active
-user
-defined
-varyings
-in
-linked
-program
) {
1925 append
-error
-to
-info
-log
;
1926 shProg
->LinkStatus
= GL_FALSE
;
1931 ralloc_strcat(&shProg
->InfoLog
, sh
->InfoLog
);
1934 delete_shader(ctx
, shader
);
1942 * Copy program-specific data generated by linking from the gl_shader_program
1943 * object to a specific gl_program object.
1946 _mesa_copy_linked_program_data(gl_shader_stage type
,
1947 const struct gl_shader_program
*src
,
1948 struct gl_program
*dst
)
1951 case MESA_SHADER_VERTEX
:
1952 dst
->UsesClipDistanceOut
= src
->Vert
.UsesClipDistance
;
1954 case MESA_SHADER_GEOMETRY
: {
1955 struct gl_geometry_program
*dst_gp
= (struct gl_geometry_program
*) dst
;
1956 dst_gp
->VerticesIn
= src
->Geom
.VerticesIn
;
1957 dst_gp
->VerticesOut
= src
->Geom
.VerticesOut
;
1958 dst_gp
->Invocations
= src
->Geom
.Invocations
;
1959 dst_gp
->InputType
= src
->Geom
.InputType
;
1960 dst_gp
->OutputType
= src
->Geom
.OutputType
;
1961 dst
->UsesClipDistanceOut
= src
->Geom
.UsesClipDistance
;
1962 dst_gp
->UsesEndPrimitive
= src
->Geom
.UsesEndPrimitive
;
1963 dst_gp
->UsesStreams
= src
->Geom
.UsesStreams
;
1966 case MESA_SHADER_FRAGMENT
: {
1967 struct gl_fragment_program
*dst_fp
= (struct gl_fragment_program
*) dst
;
1968 dst_fp
->FragDepthLayout
= src
->FragDepthLayout
;
1971 case MESA_SHADER_COMPUTE
: {
1972 struct gl_compute_program
*dst_cp
= (struct gl_compute_program
*) dst
;
1974 for (i
= 0; i
< 3; i
++)
1975 dst_cp
->LocalSize
[i
] = src
->Comp
.LocalSize
[i
];
1984 * ARB_separate_shader_objects: Compile & Link Program
1987 _mesa_CreateShaderProgramv(GLenum type
, GLsizei count
,
1988 const GLchar
* const *strings
)
1990 GET_CURRENT_CONTEXT(ctx
);
1992 return _mesa_create_shader_program(ctx
, GL_TRUE
, type
, count
, strings
);
1997 * For GL_ARB_tessellation_shader
1999 extern void GLAPIENTRY
2000 _mesa_PatchParameteri(GLenum pname
, GLint value
)
2002 GET_CURRENT_CONTEXT(ctx
);
2004 if (!_mesa_has_tessellation(ctx
)) {
2005 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameteri");
2009 if (pname
!= GL_PATCH_VERTICES
) {
2010 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameteri");
2014 if (value
<= 0 || value
> ctx
->Const
.MaxPatchVertices
) {
2015 _mesa_error(ctx
, GL_INVALID_VALUE
, "glPatchParameteri");
2019 ctx
->TessCtrlProgram
.patch_vertices
= value
;
2023 extern void GLAPIENTRY
2024 _mesa_PatchParameterfv(GLenum pname
, const GLfloat
*values
)
2026 GET_CURRENT_CONTEXT(ctx
);
2028 if (!_mesa_has_tessellation(ctx
)) {
2029 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPatchParameterfv");
2034 case GL_PATCH_DEFAULT_OUTER_LEVEL
:
2035 FLUSH_VERTICES(ctx
, 0);
2036 memcpy(ctx
->TessCtrlProgram
.patch_default_outer_level
, values
,
2037 4 * sizeof(GLfloat
));
2038 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2040 case GL_PATCH_DEFAULT_INNER_LEVEL
:
2041 FLUSH_VERTICES(ctx
, 0);
2042 memcpy(ctx
->TessCtrlProgram
.patch_default_inner_level
, values
,
2043 2 * sizeof(GLfloat
));
2044 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewDefaultTessLevels
;
2047 _mesa_error(ctx
, GL_INVALID_ENUM
, "glPatchParameterfv");