2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "api_validate.h"
29 #include "bufferobj.h"
33 #include "pipelineobj.h"
36 #include "transformfeedback.h"
39 #include "program/prog_print.h"
43 check_blend_func_error(struct gl_context
*ctx
)
45 /* The ARB_blend_func_extended spec's ERRORS section says:
47 * "The error INVALID_OPERATION is generated by Begin or any procedure
48 * that implicitly calls Begin if any draw buffer has a blend function
49 * requiring the second color input (SRC1_COLOR, ONE_MINUS_SRC1_COLOR,
50 * SRC1_ALPHA or ONE_MINUS_SRC1_ALPHA), and a framebuffer is bound that
51 * has more than the value of MAX_DUAL_SOURCE_DRAW_BUFFERS-1 active
52 * color attachements."
54 for (unsigned i
= ctx
->Const
.MaxDualSourceDrawBuffers
;
55 i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
;
57 if (ctx
->Color
.Blend
[i
]._UsesDualSrc
) {
58 _mesa_error(ctx
, GL_INVALID_OPERATION
,
59 "dual source blend on illegal attachment");
64 if (ctx
->Color
.BlendEnabled
&& ctx
->Color
._AdvancedBlendMode
) {
65 /* The KHR_blend_equation_advanced spec says:
67 * "If any non-NONE draw buffer uses a blend equation found in table
68 * X.1 or X.2, the error INVALID_OPERATION is generated by Begin or
69 * any operation that implicitly calls Begin (such as DrawElements)
72 * * the draw buffer for color output zero selects multiple color
73 * buffers (e.g., FRONT_AND_BACK in the default framebuffer); or
75 * * the draw buffer for any other color output is not NONE."
77 if (ctx
->DrawBuffer
->ColorDrawBuffer
[0] == GL_FRONT_AND_BACK
) {
78 _mesa_error(ctx
, GL_INVALID_OPERATION
,
79 "advanced blending is active and draw buffer for color "
80 "output zero selects multiple color buffers");
84 for (unsigned i
= 1; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
85 if (ctx
->DrawBuffer
->ColorDrawBuffer
[i
] != GL_NONE
) {
86 _mesa_error(ctx
, GL_INVALID_OPERATION
,
87 "advanced blending is active with multiple color "
93 /* The KHR_blend_equation_advanced spec says:
95 * "Advanced blending equations require the use of a fragment shader
96 * with a matching "blend_support" layout qualifier. If the current
97 * blend equation is found in table X.1 or X.2, and the active
98 * fragment shader does not include the layout qualifier matching
99 * the blend equation or "blend_support_all_equations", the error
100 * INVALID_OPERATION is generated [...]"
102 const struct gl_program
*prog
= ctx
->FragmentProgram
._Current
;
103 const GLbitfield blend_support
= !prog
? 0 : prog
->sh
.fs
.BlendSupport
;
105 if ((blend_support
& ctx
->Color
._AdvancedBlendMode
) == 0) {
106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
107 "fragment shader does not allow advanced blending mode "
109 _mesa_enum_to_string(ctx
->Color
.Blend
[0].EquationRGB
));
118 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
119 * is called to see if it's valid to render. This involves checking that
120 * the current shader is valid and the framebuffer is complete.
121 * It also check the current pipeline object is valid if any.
122 * If an error is detected it'll be recorded here.
123 * \return GL_TRUE if OK to render, GL_FALSE if not
126 _mesa_valid_to_render(struct gl_context
*ctx
, const char *where
)
128 /* This depends on having up to date derived state (shaders) */
130 _mesa_update_state(ctx
);
132 if (ctx
->API
== API_OPENGL_COMPAT
) {
133 /* Any shader stages that are not supplied by the GLSL shader and have
134 * assembly shaders enabled must now be validated.
136 if (!ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]
137 && ctx
->VertexProgram
.Enabled
&& !ctx
->VertexProgram
._Enabled
) {
138 _mesa_error(ctx
, GL_INVALID_OPERATION
,
139 "%s(vertex program not valid)", where
);
143 if (!ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
]) {
144 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
145 _mesa_error(ctx
, GL_INVALID_OPERATION
,
146 "%s(fragment program not valid)", where
);
150 /* If drawing to integer-valued color buffers, there must be an
151 * active fragment shader (GL_EXT_texture_integer).
153 if (ctx
->DrawBuffer
&& ctx
->DrawBuffer
->_IntegerBuffers
) {
154 _mesa_error(ctx
, GL_INVALID_OPERATION
,
155 "%s(integer format but no fragment shader)", where
);
161 /* A pipeline object is bound */
162 if (ctx
->_Shader
->Name
&& !ctx
->_Shader
->Validated
) {
163 if (!_mesa_validate_program_pipeline(ctx
, ctx
->_Shader
)) {
164 _mesa_error(ctx
, GL_INVALID_OPERATION
,
165 "glValidateProgramPipeline failed to validate the "
171 /* If a program is active and SSO not in use, check if validation of
172 * samplers succeeded for the active program. */
173 if (ctx
->_Shader
->ActiveProgram
&& ctx
->_Shader
!= ctx
->Pipeline
.Current
) {
175 if (!_mesa_sampler_uniforms_are_valid(ctx
->_Shader
->ActiveProgram
,
177 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", errMsg
);
182 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
183 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
184 "%s(incomplete framebuffer)", where
);
188 if (!check_blend_func_error(ctx
)) {
192 /* From the GL_NV_fill_rectangle spec:
194 * "An INVALID_OPERATION error is generated by Begin or any Draw command if
195 * only one of the front and back polygon mode is FILL_RECTANGLE_NV."
197 if ((ctx
->Polygon
.FrontMode
== GL_FILL_RECTANGLE_NV
) !=
198 (ctx
->Polygon
.BackMode
== GL_FILL_RECTANGLE_NV
)) {
199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
200 "GL_FILL_RECTANGLE_NV must be used as both front/back "
201 "polygon mode or neither");
206 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
207 struct gl_program
**prog
= ctx
->_Shader
->CurrentProgram
;
209 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
210 if (prog
[i
] == NULL
|| prog
[i
]->_Used
)
213 /* This is the first time this shader is being used.
214 * Append shader's constants/uniforms to log file.
216 * Only log data for the program target that matches the shader
217 * target. It's possible to have a program bound to the vertex
218 * shader target that also supplied a fragment shader. If that
219 * program isn't also bound to the fragment shader target we don't
220 * want to log its fragment data.
222 _mesa_append_uniforms_to_file(prog
[i
]);
225 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
227 prog
[i
]->_Used
= GL_TRUE
;
237 * Check if OK to draw arrays/elements.
240 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
242 if (!_mesa_valid_to_render(ctx
, function
)) {
246 if (!_mesa_all_buffers_are_unmapped(ctx
->Array
.VAO
)) {
247 _mesa_error(ctx
, GL_INVALID_OPERATION
,
248 "%s(vertex buffers are mapped)", function
);
252 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
254 * "An INVALID_OPERATION error is generated by any command that
255 * transfers vertices to the GL if the current program state has
256 * one but not both of a tessellation control shader and tessellation
257 * evaluation shader."
259 * The OpenGL spec argues that this is allowed because a tess ctrl shader
260 * without a tess eval shader can be used with transform feedback.
261 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
262 * therefore doesn't allow tessellation.
264 * Further investigation showed that this is indeed a spec bug and
265 * a tess ctrl shader without a tess eval shader shouldn't have been
266 * allowed, because there is no API in GL 4.0 that can make use this
267 * to produce something useful.
269 * Also, all vendors except one don't support a tess ctrl shader without
270 * a tess eval shader anyway.
272 if (ctx
->TessCtrlProgram
._Current
&& !ctx
->TessEvalProgram
._Current
) {
273 _mesa_error(ctx
, GL_INVALID_OPERATION
,
274 "%s(tess eval shader is missing)", function
);
280 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
282 * "An INVALID_OPERATION error is generated by any command that
283 * transfers vertices to the GL if the current program state has
284 * one but not both of a tessellation control shader and tessellation
285 * evaluation shader."
287 if (_mesa_is_gles3(ctx
) &&
288 ctx
->TessEvalProgram
._Current
&& !ctx
->TessCtrlProgram
._Current
) {
289 _mesa_error(ctx
, GL_INVALID_OPERATION
,
290 "%s(tess ctrl shader is missing)", function
);
295 case API_OPENGL_CORE
:
296 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
297 * Core Profile spec says:
299 * "An INVALID_OPERATION error is generated if no vertex array
300 * object is bound (see section 10.3.1)."
302 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
303 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no VAO bound)", function
);
309 case API_OPENGL_COMPAT
:
313 unreachable("Invalid API value in check_valid_to_render()");
321 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
322 * etc? The set of legal values depends on whether geometry shaders/programs
324 * Note: This may be called during display list compilation.
327 _mesa_is_valid_prim_mode(const struct gl_context
*ctx
, GLenum mode
)
329 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
330 * first and exit. You would think that a switch-statement would be the
331 * right approach, but at least GCC 4.7.2 generates some pretty dire code
332 * for the common case.
334 if (likely(mode
<= GL_TRIANGLE_FAN
))
337 if (mode
<= GL_POLYGON
)
338 return (ctx
->API
== API_OPENGL_COMPAT
);
340 if (mode
<= GL_TRIANGLE_STRIP_ADJACENCY
)
341 return _mesa_has_geometry_shaders(ctx
);
343 if (mode
== GL_PATCHES
)
344 return _mesa_has_tessellation(ctx
);
351 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
352 * etc? Also, do additional checking related to transformation feedback.
353 * Note: this function cannot be called during glNewList(GL_COMPILE) because
354 * this code depends on current transform feedback state.
355 * Also, do additional checking related to tessellation shaders.
358 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
360 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
363 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
367 /* From the OpenGL 4.5 specification, section 11.3.1:
369 * The error INVALID_OPERATION is generated if Begin, or any command that
370 * implicitly calls Begin, is called when a geometry shader is active and:
372 * * the input primitive type of the current geometry shader is
373 * POINTS and <mode> is not POINTS,
375 * * the input primitive type of the current geometry shader is
376 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
378 * * the input primitive type of the current geometry shader is
379 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
382 * * the input primitive type of the current geometry shader is
383 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
384 * LINE_STRIP_ADJACENCY_ARB, or
386 * * the input primitive type of the current geometry shader is
387 * TRIANGLES_ADJACENCY_ARB and <mode> is not
388 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
390 * The GL spec doesn't mention any interaction with tessellation, which
391 * is clearly a spec bug. The same rule should apply, but instead of
392 * the draw primitive mode, the tessellation evaluation shader primitive
393 * mode should be used for the checking.
395 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
396 const GLenum geom_mode
=
397 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
398 info
.gs
.input_primitive
;
399 struct gl_program
*tes
=
400 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
401 GLenum mode_before_gs
= mode
;
404 if (tes
->info
.tess
.point_mode
)
405 mode_before_gs
= GL_POINTS
;
406 else if (tes
->info
.tess
.primitive_mode
== GL_ISOLINES
)
407 mode_before_gs
= GL_LINES
;
409 /* the GL_QUADS mode generates triangles too */
410 mode_before_gs
= GL_TRIANGLES
;
413 switch (mode_before_gs
) {
415 valid_enum
= (geom_mode
== GL_POINTS
);
420 valid_enum
= (geom_mode
== GL_LINES
);
423 case GL_TRIANGLE_STRIP
:
424 case GL_TRIANGLE_FAN
:
425 valid_enum
= (geom_mode
== GL_TRIANGLES
);
432 case GL_LINES_ADJACENCY
:
433 case GL_LINE_STRIP_ADJACENCY
:
434 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
436 case GL_TRIANGLES_ADJACENCY
:
437 case GL_TRIANGLE_STRIP_ADJACENCY
:
438 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
445 _mesa_error(ctx
, GL_INVALID_OPERATION
,
446 "%s(mode=%s vs geometry shader input %s)",
448 _mesa_lookup_prim_by_nr(mode_before_gs
),
449 _mesa_lookup_prim_by_nr(geom_mode
));
454 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
456 * "Tessellation operates only on patch primitives. If tessellation is
457 * active, any command that transfers vertices to the GL will
458 * generate an INVALID_OPERATION error if the primitive mode is not
460 * Patch primitives are not supported by pipeline stages below the
461 * tessellation evaluation shader. If there is no active program
462 * object or the active program object does not contain a tessellation
463 * evaluation shader, the error INVALID_OPERATION is generated by any
464 * command that transfers vertices to the GL if the primitive mode is
468 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
] ||
469 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]) {
470 if (mode
!= GL_PATCHES
) {
471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
472 "only GL_PATCHES valid with tessellation");
477 if (mode
== GL_PATCHES
) {
478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
479 "GL_PATCHES only valid with tessellation");
484 /* From the GL_EXT_transform_feedback spec:
486 * "The error INVALID_OPERATION is generated if Begin, or any command
487 * that performs an explicit Begin, is called when:
489 * * a geometry shader is not active and <mode> does not match the
490 * allowed begin modes for the current transform feedback state as
491 * given by table X.1.
493 * * a geometry shader is active and the output primitive type of the
494 * geometry shader does not match the allowed begin modes for the
495 * current transform feedback state as given by table X.1.
498 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
499 GLboolean pass
= GL_TRUE
;
501 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
502 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
503 info
.gs
.output_primitive
) {
505 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
508 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
510 case GL_TRIANGLE_STRIP
:
511 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
517 else if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]) {
518 struct gl_program
*tes
=
519 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
520 if (tes
->info
.tess
.point_mode
)
521 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
522 else if (tes
->info
.tess
.primitive_mode
== GL_ISOLINES
)
523 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
525 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
530 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
535 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
538 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
543 _mesa_error(ctx
, GL_INVALID_OPERATION
,
544 "%s(mode=%s vs transform feedback %s)",
546 _mesa_lookup_prim_by_nr(mode
),
547 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
552 /* From GL_INTEL_conservative_rasterization spec:
554 * The conservative rasterization option applies only to polygons with
555 * PolygonMode state set to FILL. Draw requests for polygons with different
556 * PolygonMode setting or for other primitive types (points/lines) generate
557 * INVALID_OPERATION error.
559 if (ctx
->IntelConservativeRasterization
) {
560 GLboolean pass
= GL_TRUE
;
567 case GL_LINES_ADJACENCY
:
568 case GL_LINE_STRIP_ADJACENCY
:
572 case GL_TRIANGLE_STRIP
:
573 case GL_TRIANGLE_FAN
:
577 case GL_TRIANGLES_ADJACENCY
:
578 case GL_TRIANGLE_STRIP_ADJACENCY
:
579 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
580 ctx
->Polygon
.BackMode
!= GL_FILL
)
587 _mesa_error(ctx
, GL_INVALID_OPERATION
,
588 "mode=%s invalid with GL_INTEL_conservative_rasterization",
589 _mesa_lookup_prim_by_nr(mode
));
598 * Verify that the element type is valid.
600 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
603 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
606 case GL_UNSIGNED_BYTE
:
607 case GL_UNSIGNED_SHORT
:
608 case GL_UNSIGNED_INT
:
612 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
613 _mesa_enum_to_string(type
));
619 validate_DrawElements_common(struct gl_context
*ctx
,
620 GLenum mode
, GLsizei count
, GLenum type
,
621 const GLvoid
*indices
,
624 /* Section 2.14.2 (Transform Feedback Primitive Capture) of the OpenGL ES
627 * The error INVALID_OPERATION is also generated by DrawElements,
628 * DrawElementsInstanced, and DrawRangeElements while transform feedback
629 * is active and not paused, regardless of mode.
631 * The OES_geometry_shader_spec says:
637 * (13) Does this extension change how transform feedback operates
638 * compared to unextended OpenGL ES 3.0 or 3.1?
640 * RESOLVED: Yes... Since we no longer require being able to predict how
641 * much geometry will be generated, we also lift the restriction that
642 * only DrawArray* commands are supported and also support the
643 * DrawElements* commands for transform feedback.
645 * This should also be reflected in the body of the spec, but that appears
646 * to have been overlooked. The body of the spec only explicitly allows
647 * the indirect versions.
649 if (_mesa_is_gles3(ctx
) &&
650 !_mesa_has_OES_geometry_shader(ctx
) &&
651 _mesa_is_xfb_active_and_unpaused(ctx
)) {
652 _mesa_error(ctx
, GL_INVALID_OPERATION
,
653 "%s(transform feedback active)", caller
);
658 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
662 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
666 if (!valid_elements_type(ctx
, type
, caller
))
669 if (!check_valid_to_render(ctx
, caller
))
676 * Error checking for glDrawElements(). Includes parameter checking
677 * and VBO bounds checking.
678 * \return GL_TRUE if OK to render, GL_FALSE if error found
681 _mesa_validate_DrawElements(struct gl_context
*ctx
,
682 GLenum mode
, GLsizei count
, GLenum type
,
683 const GLvoid
*indices
)
685 FLUSH_CURRENT(ctx
, 0);
687 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
693 * Error checking for glMultiDrawElements(). Includes parameter checking
694 * and VBO bounds checking.
695 * \return GL_TRUE if OK to render, GL_FALSE if error found
698 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
699 GLenum mode
, const GLsizei
*count
,
700 GLenum type
, const GLvoid
* const *indices
,
705 FLUSH_CURRENT(ctx
, 0);
708 * Section 2.3.1 (Errors) of the OpenGL 4.5 (Core Profile) spec says:
710 * "If a negative number is provided where an argument of type sizei or
711 * sizeiptr is specified, an INVALID_VALUE error is generated."
713 * and in the same section:
715 * "In other cases, there are no side effects unless otherwise noted;
716 * the command which generates the error is ignored so that it has no
717 * effect on GL state or framebuffer contents."
719 * Hence, check both primcount and all the count[i].
722 _mesa_error(ctx
, GL_INVALID_VALUE
,
723 "glMultiDrawElements(primcount=%d)", primcount
);
727 for (i
= 0; i
< primcount
; i
++) {
729 _mesa_error(ctx
, GL_INVALID_VALUE
,
730 "glMultiDrawElements(count)" );
735 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
739 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
742 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
745 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
747 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
748 for (i
= 0; i
< primcount
; i
++) {
759 * Error checking for glDrawRangeElements(). Includes parameter checking
760 * and VBO bounds checking.
761 * \return GL_TRUE if OK to render, GL_FALSE if error found
764 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
765 GLuint start
, GLuint end
,
766 GLsizei count
, GLenum type
,
767 const GLvoid
*indices
)
769 FLUSH_CURRENT(ctx
, 0);
772 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
776 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
777 "glDrawRangeElements");
782 need_xfb_remaining_prims_check(const struct gl_context
*ctx
)
784 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
785 * Primitive Capture):
787 * The error INVALID_OPERATION is generated by DrawArrays and
788 * DrawArraysInstanced if recording the vertices of a primitive to the
789 * buffer objects being used for transform feedback purposes would result
790 * in either exceeding the limits of any buffer object’s size, or in
791 * exceeding the end position offset + size − 1, as set by
794 * This is in contrast to the behaviour of desktop GL, where the extra
795 * primitives are silently dropped from the transform feedback buffer.
797 * This text is removed in ES 3.2, presumably because it's not really
798 * implementable with geometry and tessellation shaders. In fact,
799 * the OES_geometry_shader spec says:
801 * "(13) Does this extension change how transform feedback operates
802 * compared to unextended OpenGL ES 3.0 or 3.1?
804 * RESOLVED: Yes. Because dynamic geometry amplification in a geometry
805 * shader can make it difficult if not impossible to predict the amount
806 * of geometry that may be generated in advance of executing the shader,
807 * the draw-time error for transform feedback buffer overflow conditions
808 * is removed and replaced with the GL behavior (primitives are not
809 * written and the corresponding counter is not updated)..."
811 return _mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
) &&
812 !_mesa_has_OES_geometry_shader(ctx
) &&
813 !_mesa_has_OES_tessellation_shader(ctx
);
818 validate_draw_arrays(struct gl_context
*ctx
, const char *func
,
819 GLenum mode
, GLsizei count
, GLsizei numInstances
)
821 FLUSH_CURRENT(ctx
, 0);
824 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", func
);
828 if (!_mesa_valid_prim_mode(ctx
, mode
, func
))
831 if (!check_valid_to_render(ctx
, func
))
834 if (need_xfb_remaining_prims_check(ctx
)) {
835 struct gl_transform_feedback_object
*xfb_obj
836 = ctx
->TransformFeedback
.CurrentObject
;
837 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, numInstances
);
838 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
840 "%s(exceeds transform feedback size)", func
);
843 xfb_obj
->GlesRemainingPrims
-= prim_count
;
853 * Called from the tnl module to error check the function parameters and
854 * verify that we really can draw something.
855 * \return GL_TRUE if OK to render, GL_FALSE if error found
858 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
860 return validate_draw_arrays(ctx
, "glDrawArrays", mode
, count
, 1);
865 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
866 GLsizei count
, GLsizei numInstances
)
869 _mesa_error(ctx
, GL_INVALID_VALUE
,
870 "glDrawArraysInstanced(start=%d)", first
);
874 if (numInstances
<= 0) {
875 if (numInstances
< 0)
876 _mesa_error(ctx
, GL_INVALID_VALUE
,
877 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
881 return validate_draw_arrays(ctx
, "glDrawArraysInstanced", mode
, count
, 1);
886 * Called to error check the function parameters.
888 * Note that glMultiDrawArrays is not part of GLES, so there's limited scope
889 * for sharing code with the validation of glDrawArrays.
892 _mesa_validate_MultiDrawArrays(struct gl_context
*ctx
, GLenum mode
,
893 const GLsizei
*count
, GLsizei primcount
)
897 FLUSH_CURRENT(ctx
, 0);
899 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawArrays"))
902 if (!check_valid_to_render(ctx
, "glMultiDrawArrays"))
906 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMultiDrawArrays(primcount=%d)",
911 for (i
= 0; i
< primcount
; ++i
) {
913 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMultiDrawArrays(count[%d]=%d)",
919 if (need_xfb_remaining_prims_check(ctx
)) {
920 struct gl_transform_feedback_object
*xfb_obj
921 = ctx
->TransformFeedback
.CurrentObject
;
922 size_t xfb_prim_count
= 0;
924 for (i
= 0; i
< primcount
; ++i
)
925 xfb_prim_count
+= vbo_count_tessellated_primitives(mode
, count
[i
], 1);
927 if (xfb_obj
->GlesRemainingPrims
< xfb_prim_count
) {
928 _mesa_error(ctx
, GL_INVALID_OPERATION
,
929 "glMultiDrawArrays(exceeds transform feedback size)");
932 xfb_obj
->GlesRemainingPrims
-= xfb_prim_count
;
940 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
941 GLenum mode
, GLsizei count
, GLenum type
,
942 const GLvoid
*indices
, GLsizei numInstances
)
944 FLUSH_CURRENT(ctx
, 0);
946 if (numInstances
< 0) {
947 _mesa_error(ctx
, GL_INVALID_VALUE
,
948 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
952 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
953 "glDrawElementsInstanced")
954 && (numInstances
> 0);
959 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
961 struct gl_transform_feedback_object
*obj
,
963 GLsizei numInstances
)
965 FLUSH_CURRENT(ctx
, 0);
967 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
972 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
976 /* From the GL 4.5 specification, page 429:
977 * "An INVALID_VALUE error is generated if id is not the name of a
978 * transform feedback object."
980 if (!obj
->EverBound
) {
981 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
985 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
986 _mesa_error(ctx
, GL_INVALID_VALUE
,
987 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
991 if (!obj
->EndedAnytime
) {
992 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
996 if (numInstances
<= 0) {
997 if (numInstances
< 0)
998 _mesa_error(ctx
, GL_INVALID_VALUE
,
999 "glDrawTransformFeedback*Instanced(numInstances=%d)",
1004 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
1012 valid_draw_indirect(struct gl_context
*ctx
,
1013 GLenum mode
, const GLvoid
*indirect
,
1014 GLsizei size
, const char *name
)
1016 const uint64_t end
= (uint64_t) (uintptr_t) indirect
+ size
;
1018 /* OpenGL ES 3.1 spec. section 10.5:
1020 * "DrawArraysIndirect requires that all data sourced for the
1021 * command, including the DrawArraysIndirectCommand
1022 * structure, be in buffer objects, and may not be called when
1023 * the default vertex array object is bound."
1025 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
1026 _mesa_error(ctx
, GL_INVALID_OPERATION
, "(no VAO bound)");
1030 /* From OpenGL ES 3.1 spec. section 10.5:
1031 * "An INVALID_OPERATION error is generated if zero is bound to
1032 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
1035 * Here we check that for each enabled vertex array we have a vertex
1038 if (_mesa_is_gles31(ctx
) &&
1039 ctx
->Array
.VAO
->_Enabled
& ~ctx
->Array
.VAO
->VertexAttribBufferMask
) {
1040 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(No VBO bound)", name
);
1044 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
1047 /* OpenGL ES 3.1 specification, section 10.5:
1049 * "An INVALID_OPERATION error is generated if
1050 * transform feedback is active and not paused."
1052 * The OES_geometry_shader spec says:
1054 * On p. 250 in the errors section for the DrawArraysIndirect command,
1055 * and on p. 254 in the errors section for the DrawElementsIndirect
1056 * command, delete the errors which state:
1058 * "An INVALID_OPERATION error is generated if transform feedback is
1059 * active and not paused."
1061 * (thus allowing transform feedback to work with indirect draw commands).
1063 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
1064 _mesa_is_xfb_active_and_unpaused(ctx
)) {
1065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1066 "%s(TransformFeedback is active and not paused)", name
);
1069 /* From OpenGL version 4.4. section 10.5
1070 * and OpenGL ES 3.1, section 10.6:
1072 * "An INVALID_VALUE error is generated if indirect is not a
1073 * multiple of the size, in basic machine units, of uint."
1075 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
1076 _mesa_error(ctx
, GL_INVALID_VALUE
,
1077 "%s(indirect is not aligned)", name
);
1081 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1082 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1083 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
1087 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
1088 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1089 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
1093 /* From the ARB_draw_indirect specification:
1094 * "An INVALID_OPERATION error is generated if the commands source data
1095 * beyond the end of the buffer object [...]"
1097 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
1098 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1099 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
1103 if (!check_valid_to_render(ctx
, name
))
1109 static inline GLboolean
1110 valid_draw_indirect_elements(struct gl_context
*ctx
,
1111 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
1112 GLsizeiptr size
, const char *name
)
1114 if (!valid_elements_type(ctx
, type
, name
))
1118 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1119 * may not come from a client array and must come from an index buffer.
1120 * If no element array buffer is bound, an INVALID_OPERATION error is
1123 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1124 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1125 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
1129 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
1132 static inline GLboolean
1133 valid_draw_indirect_multi(struct gl_context
*ctx
,
1134 GLsizei primcount
, GLsizei stride
,
1138 /* From the ARB_multi_draw_indirect specification:
1139 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
1140 * MultiDrawElementsIndirect if <primcount> is negative."
1142 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
1145 if (primcount
< 0) {
1146 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
1151 /* From the ARB_multi_draw_indirect specification:
1152 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
1153 * error is generated."
1156 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
1164 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
1166 const GLvoid
*indirect
)
1168 const unsigned drawArraysNumParams
= 4;
1170 FLUSH_CURRENT(ctx
, 0);
1172 return valid_draw_indirect(ctx
, mode
,
1173 indirect
, drawArraysNumParams
* sizeof(GLuint
),
1174 "glDrawArraysIndirect");
1178 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
1179 GLenum mode
, GLenum type
,
1180 const GLvoid
*indirect
)
1182 const unsigned drawElementsNumParams
= 5;
1184 FLUSH_CURRENT(ctx
, 0);
1186 return valid_draw_indirect_elements(ctx
, mode
, type
,
1187 indirect
, drawElementsNumParams
* sizeof(GLuint
),
1188 "glDrawElementsIndirect");
1192 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
1194 const GLvoid
*indirect
,
1195 GLsizei primcount
, GLsizei stride
)
1197 GLsizeiptr size
= 0;
1198 const unsigned drawArraysNumParams
= 4;
1200 FLUSH_CURRENT(ctx
, 0);
1202 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1203 assert(stride
!= 0);
1205 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
1206 "glMultiDrawArraysIndirect"))
1209 /* number of bytes of the indirect buffer which will be read */
1211 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
1214 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
1215 "glMultiDrawArraysIndirect"))
1222 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
1223 GLenum mode
, GLenum type
,
1224 const GLvoid
*indirect
,
1225 GLsizei primcount
, GLsizei stride
)
1227 GLsizeiptr size
= 0;
1228 const unsigned drawElementsNumParams
= 5;
1230 FLUSH_CURRENT(ctx
, 0);
1232 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1233 assert(stride
!= 0);
1235 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
1236 "glMultiDrawElementsIndirect"))
1239 /* number of bytes of the indirect buffer which will be read */
1241 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1244 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1246 "glMultiDrawElementsIndirect"))
1253 valid_draw_indirect_parameters(struct gl_context
*ctx
,
1257 /* From the ARB_indirect_parameters specification:
1258 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
1259 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
1262 if (drawcount
& 3) {
1263 _mesa_error(ctx
, GL_INVALID_VALUE
,
1264 "%s(drawcount is not a multiple of 4)", name
);
1268 /* From the ARB_indirect_parameters specification:
1269 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1270 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
1271 * PARAMETER_BUFFER_ARB binding point."
1273 if (!_mesa_is_bufferobj(ctx
->ParameterBuffer
)) {
1274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1275 "%s: no buffer bound to PARAMETER_BUFFER", name
);
1279 if (_mesa_check_disallowed_mapping(ctx
->ParameterBuffer
)) {
1280 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1281 "%s(PARAMETER_BUFFER is mapped)", name
);
1285 /* From the ARB_indirect_parameters specification:
1286 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1287 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
1288 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
1289 * specified by <drawcount> would result in an out-of-bounds access."
1291 if (ctx
->ParameterBuffer
->Size
< drawcount
+ sizeof(GLsizei
)) {
1292 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1293 "%s(PARAMETER_BUFFER too small)", name
);
1301 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context
*ctx
,
1305 GLsizei maxdrawcount
,
1308 GLsizeiptr size
= 0;
1309 const unsigned drawArraysNumParams
= 4;
1311 FLUSH_CURRENT(ctx
, 0);
1313 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1314 assert(stride
!= 0);
1316 if (!valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1317 "glMultiDrawArraysIndirectCountARB"))
1320 /* number of bytes of the indirect buffer which will be read */
1322 ? (maxdrawcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
1325 if (!valid_draw_indirect(ctx
, mode
, (void *)indirect
, size
,
1326 "glMultiDrawArraysIndirectCountARB"))
1329 return valid_draw_indirect_parameters(
1330 ctx
, "glMultiDrawArraysIndirectCountARB", drawcount
);
1334 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context
*ctx
,
1335 GLenum mode
, GLenum type
,
1338 GLsizei maxdrawcount
,
1341 GLsizeiptr size
= 0;
1342 const unsigned drawElementsNumParams
= 5;
1344 FLUSH_CURRENT(ctx
, 0);
1346 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1347 assert(stride
!= 0);
1349 if (!valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1350 "glMultiDrawElementsIndirectCountARB"))
1353 /* number of bytes of the indirect buffer which will be read */
1355 ? (maxdrawcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1358 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1359 (void *)indirect
, size
,
1360 "glMultiDrawElementsIndirectCountARB"))
1363 return valid_draw_indirect_parameters(
1364 ctx
, "glMultiDrawElementsIndirectCountARB", drawcount
);