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 "draw_validate.h"
29 #include "bufferobj.h"
33 #include "pipelineobj.h"
36 #include "transformfeedback.h"
38 #include "program/prog_print.h"
42 check_blend_func_error(struct gl_context
*ctx
)
44 /* The ARB_blend_func_extended spec's ERRORS section says:
46 * "The error INVALID_OPERATION is generated by Begin or any procedure
47 * that implicitly calls Begin if any draw buffer has a blend function
48 * requiring the second color input (SRC1_COLOR, ONE_MINUS_SRC1_COLOR,
49 * SRC1_ALPHA or ONE_MINUS_SRC1_ALPHA), and a framebuffer is bound that
50 * has more than the value of MAX_DUAL_SOURCE_DRAW_BUFFERS-1 active
51 * color attachements."
53 for (unsigned i
= ctx
->Const
.MaxDualSourceDrawBuffers
;
54 i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
;
56 if (ctx
->Color
.Blend
[i
]._UsesDualSrc
) {
57 _mesa_error(ctx
, GL_INVALID_OPERATION
,
58 "dual source blend on illegal attachment");
63 if (ctx
->Color
.BlendEnabled
&& ctx
->Color
._AdvancedBlendMode
) {
64 /* The KHR_blend_equation_advanced spec says:
66 * "If any non-NONE draw buffer uses a blend equation found in table
67 * X.1 or X.2, the error INVALID_OPERATION is generated by Begin or
68 * any operation that implicitly calls Begin (such as DrawElements)
71 * * the draw buffer for color output zero selects multiple color
72 * buffers (e.g., FRONT_AND_BACK in the default framebuffer); or
74 * * the draw buffer for any other color output is not NONE."
76 if (ctx
->DrawBuffer
->ColorDrawBuffer
[0] == GL_FRONT_AND_BACK
) {
77 _mesa_error(ctx
, GL_INVALID_OPERATION
,
78 "advanced blending is active and draw buffer for color "
79 "output zero selects multiple color buffers");
83 for (unsigned i
= 1; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
84 if (ctx
->DrawBuffer
->ColorDrawBuffer
[i
] != GL_NONE
) {
85 _mesa_error(ctx
, GL_INVALID_OPERATION
,
86 "advanced blending is active with multiple color "
92 /* The KHR_blend_equation_advanced spec says:
94 * "Advanced blending equations require the use of a fragment shader
95 * with a matching "blend_support" layout qualifier. If the current
96 * blend equation is found in table X.1 or X.2, and the active
97 * fragment shader does not include the layout qualifier matching
98 * the blend equation or "blend_support_all_equations", the error
99 * INVALID_OPERATION is generated [...]"
101 const struct gl_program
*prog
= ctx
->FragmentProgram
._Current
;
102 const GLbitfield blend_support
= !prog
? 0 : prog
->sh
.fs
.BlendSupport
;
104 if ((blend_support
& ctx
->Color
._AdvancedBlendMode
) == 0) {
105 _mesa_error(ctx
, GL_INVALID_OPERATION
,
106 "fragment shader does not allow advanced blending mode "
108 _mesa_enum_to_string(ctx
->Color
.Blend
[0].EquationRGB
));
117 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
118 * is called to see if it's valid to render. This involves checking that
119 * the current shader is valid and the framebuffer is complete.
120 * It also check the current pipeline object is valid if any.
121 * If an error is detected it'll be recorded here.
122 * \return GL_TRUE if OK to render, GL_FALSE if not
125 _mesa_valid_to_render(struct gl_context
*ctx
, const char *where
)
127 /* This depends on having up to date derived state (shaders) */
129 _mesa_update_state(ctx
);
131 if (ctx
->API
== API_OPENGL_COMPAT
) {
132 /* Any shader stages that are not supplied by the GLSL shader and have
133 * assembly shaders enabled must now be validated.
135 if (!ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] &&
136 ctx
->VertexProgram
.Enabled
&&
137 !_mesa_arb_vertex_program_enabled(ctx
)) {
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
&&
145 !_mesa_arb_fragment_program_enabled(ctx
)) {
146 _mesa_error(ctx
, GL_INVALID_OPERATION
,
147 "%s(fragment program not valid)", where
);
151 /* If drawing to integer-valued color buffers, there must be an
152 * active fragment shader (GL_EXT_texture_integer).
154 if (ctx
->DrawBuffer
&& ctx
->DrawBuffer
->_IntegerBuffers
) {
155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
156 "%s(integer format but no fragment shader)", where
);
162 /* A pipeline object is bound */
163 if (ctx
->_Shader
->Name
&& !ctx
->_Shader
->Validated
) {
164 if (!_mesa_validate_program_pipeline(ctx
, ctx
->_Shader
)) {
165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
166 "glValidateProgramPipeline failed to validate the "
172 /* If a program is active and SSO not in use, check if validation of
173 * samplers succeeded for the active program. */
174 if (ctx
->_Shader
->ActiveProgram
&& ctx
->_Shader
!= ctx
->Pipeline
.Current
) {
176 if (!_mesa_sampler_uniforms_are_valid(ctx
->_Shader
->ActiveProgram
,
178 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", errMsg
);
183 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
184 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
185 "%s(incomplete framebuffer)", where
);
189 if (!check_blend_func_error(ctx
)) {
193 /* From the GL_NV_fill_rectangle spec:
195 * "An INVALID_OPERATION error is generated by Begin or any Draw command if
196 * only one of the front and back polygon mode is FILL_RECTANGLE_NV."
198 if ((ctx
->Polygon
.FrontMode
== GL_FILL_RECTANGLE_NV
) !=
199 (ctx
->Polygon
.BackMode
== GL_FILL_RECTANGLE_NV
)) {
200 _mesa_error(ctx
, GL_INVALID_OPERATION
,
201 "GL_FILL_RECTANGLE_NV must be used as both front/back "
202 "polygon mode or neither");
207 if (ctx
->_Shader
->Flags
& GLSL_LOG
) {
208 struct gl_program
**prog
= ctx
->_Shader
->CurrentProgram
;
210 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
211 if (prog
[i
] == NULL
|| prog
[i
]->_Used
)
214 /* This is the first time this shader is being used.
215 * Append shader's constants/uniforms to log file.
217 * Only log data for the program target that matches the shader
218 * target. It's possible to have a program bound to the vertex
219 * shader target that also supplied a fragment shader. If that
220 * program isn't also bound to the fragment shader target we don't
221 * want to log its fragment data.
223 _mesa_append_uniforms_to_file(prog
[i
]);
226 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
228 prog
[i
]->_Used
= GL_TRUE
;
238 * Check if OK to draw arrays/elements.
241 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
243 if (!_mesa_valid_to_render(ctx
, function
)) {
247 /* Section 6.3.2 from the GL 4.5:
248 * "Any GL command which attempts to read from, write to, or change
249 * the state of a buffer object may generate an INVALID_OPERATION error if
250 * all or part of the buffer object is mapped ... However, only commands
251 * which explicitly describe this error are required to do so. If an error
252 * is not generated, such commands will have undefined results and may
253 * result in GL interruption or termination."
255 * Only some buffer API functions require INVALID_OPERATION with mapped
256 * buffers. No other functions list such an error, thus it's not required
257 * to report INVALID_OPERATION for draw calls with mapped buffers.
259 if (!ctx
->Const
.AllowMappedBuffersDuringExecution
&&
260 !_mesa_all_buffers_are_unmapped(ctx
->Array
.VAO
)) {
261 _mesa_error(ctx
, GL_INVALID_OPERATION
,
262 "%s(vertex buffers are mapped)", function
);
266 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
268 * "An INVALID_OPERATION error is generated by any command that
269 * transfers vertices to the GL if the current program state has
270 * one but not both of a tessellation control shader and tessellation
271 * evaluation shader."
273 * The OpenGL spec argues that this is allowed because a tess ctrl shader
274 * without a tess eval shader can be used with transform feedback.
275 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
276 * therefore doesn't allow tessellation.
278 * Further investigation showed that this is indeed a spec bug and
279 * a tess ctrl shader without a tess eval shader shouldn't have been
280 * allowed, because there is no API in GL 4.0 that can make use this
281 * to produce something useful.
283 * Also, all vendors except one don't support a tess ctrl shader without
284 * a tess eval shader anyway.
286 if (ctx
->TessCtrlProgram
._Current
&& !ctx
->TessEvalProgram
._Current
) {
287 _mesa_error(ctx
, GL_INVALID_OPERATION
,
288 "%s(tess eval shader is missing)", function
);
294 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
296 * "An INVALID_OPERATION error is generated by any command that
297 * transfers vertices to the GL if the current program state has
298 * one but not both of a tessellation control shader and tessellation
299 * evaluation shader."
301 if (_mesa_is_gles3(ctx
) &&
302 ctx
->TessEvalProgram
._Current
&& !ctx
->TessCtrlProgram
._Current
) {
303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
304 "%s(tess ctrl shader is missing)", function
);
308 /* From GL_EXT_color_buffer_float:
310 * "Blending applies only if the color buffer has a fixed-point or
311 * or floating-point format. If the color buffer has an integer
312 * format, proceed to the next operation. Furthermore, an
313 * INVALID_OPERATION error is generated by DrawArrays and the other
314 * drawing commands defined in section 2.8.3 (10.5 in ES 3.1) if
315 * blending is enabled (see below) and any draw buffer has 32-bit
316 * floating-point format components."
318 * However GL_EXT_float_blend removes this text.
320 if (!ctx
->Extensions
.EXT_float_blend
&&
321 (ctx
->DrawBuffer
->_FP32Buffers
& ctx
->Color
.BlendEnabled
)) {
322 _mesa_error(ctx
, GL_INVALID_OPERATION
,
323 "%s(32-bit float output + blending)", function
);
328 case API_OPENGL_CORE
:
329 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
330 * Core Profile spec says:
332 * "An INVALID_OPERATION error is generated if no vertex array
333 * object is bound (see section 10.3.1)."
335 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
336 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no VAO bound)", function
);
342 case API_OPENGL_COMPAT
:
346 unreachable("Invalid API value in check_valid_to_render()");
354 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
355 * etc? The set of legal values depends on whether geometry shaders/programs
357 * Note: This may be called during display list compilation.
360 _mesa_is_valid_prim_mode(const struct gl_context
*ctx
, GLenum mode
)
362 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
363 * first and exit. You would think that a switch-statement would be the
364 * right approach, but at least GCC 4.7.2 generates some pretty dire code
365 * for the common case.
367 if (likely(mode
<= GL_TRIANGLE_FAN
))
370 if (mode
<= GL_POLYGON
)
371 return (ctx
->API
== API_OPENGL_COMPAT
);
373 if (mode
<= GL_TRIANGLE_STRIP_ADJACENCY
)
374 return _mesa_has_geometry_shaders(ctx
);
376 if (mode
== GL_PATCHES
)
377 return _mesa_has_tessellation(ctx
);
384 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
385 * etc? Also, do additional checking related to transformation feedback.
386 * Note: this function cannot be called during glNewList(GL_COMPILE) because
387 * this code depends on current transform feedback state.
388 * Also, do additional checking related to tessellation shaders.
391 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
393 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
396 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
400 /* From the OpenGL 4.5 specification, section 11.3.1:
402 * The error INVALID_OPERATION is generated if Begin, or any command that
403 * implicitly calls Begin, is called when a geometry shader is active and:
405 * * the input primitive type of the current geometry shader is
406 * POINTS and <mode> is not POINTS,
408 * * the input primitive type of the current geometry shader is
409 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
411 * * the input primitive type of the current geometry shader is
412 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
415 * * the input primitive type of the current geometry shader is
416 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
417 * LINE_STRIP_ADJACENCY_ARB, or
419 * * the input primitive type of the current geometry shader is
420 * TRIANGLES_ADJACENCY_ARB and <mode> is not
421 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
423 * The GL spec doesn't mention any interaction with tessellation, which
424 * is clearly a spec bug. The same rule should apply, but instead of
425 * the draw primitive mode, the tessellation evaluation shader primitive
426 * mode should be used for the checking.
428 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
429 const GLenum geom_mode
=
430 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
431 info
.gs
.input_primitive
;
432 struct gl_program
*tes
=
433 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
434 GLenum mode_before_gs
= mode
;
437 if (tes
->info
.tess
.point_mode
)
438 mode_before_gs
= GL_POINTS
;
439 else if (tes
->info
.tess
.primitive_mode
== GL_ISOLINES
)
440 mode_before_gs
= GL_LINES
;
442 /* the GL_QUADS mode generates triangles too */
443 mode_before_gs
= GL_TRIANGLES
;
446 switch (mode_before_gs
) {
448 valid_enum
= (geom_mode
== GL_POINTS
);
453 valid_enum
= (geom_mode
== GL_LINES
);
456 case GL_TRIANGLE_STRIP
:
457 case GL_TRIANGLE_FAN
:
458 valid_enum
= (geom_mode
== GL_TRIANGLES
);
465 case GL_LINES_ADJACENCY
:
466 case GL_LINE_STRIP_ADJACENCY
:
467 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
469 case GL_TRIANGLES_ADJACENCY
:
470 case GL_TRIANGLE_STRIP_ADJACENCY
:
471 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
479 "%s(mode=%s vs geometry shader input %s)",
481 _mesa_lookup_prim_by_nr(mode_before_gs
),
482 _mesa_lookup_prim_by_nr(geom_mode
));
487 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
489 * "Tessellation operates only on patch primitives. If tessellation is
490 * active, any command that transfers vertices to the GL will
491 * generate an INVALID_OPERATION error if the primitive mode is not
493 * Patch primitives are not supported by pipeline stages below the
494 * tessellation evaluation shader. If there is no active program
495 * object or the active program object does not contain a tessellation
496 * evaluation shader, the error INVALID_OPERATION is generated by any
497 * command that transfers vertices to the GL if the primitive mode is
501 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
] ||
502 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]) {
503 if (mode
!= GL_PATCHES
) {
504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
505 "only GL_PATCHES valid with tessellation");
510 if (mode
== GL_PATCHES
) {
511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
512 "GL_PATCHES only valid with tessellation");
517 /* From the GL_EXT_transform_feedback spec:
519 * "The error INVALID_OPERATION is generated if Begin, or any command
520 * that performs an explicit Begin, is called when:
522 * * a geometry shader is not active and <mode> does not match the
523 * allowed begin modes for the current transform feedback state as
524 * given by table X.1.
526 * * a geometry shader is active and the output primitive type of the
527 * geometry shader does not match the allowed begin modes for the
528 * current transform feedback state as given by table X.1.
531 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
532 GLboolean pass
= GL_TRUE
;
534 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
535 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
536 info
.gs
.output_primitive
) {
538 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
541 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
543 case GL_TRIANGLE_STRIP
:
544 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
550 else if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]) {
551 struct gl_program
*tes
=
552 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
553 if (tes
->info
.tess
.point_mode
)
554 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
555 else if (tes
->info
.tess
.primitive_mode
== GL_ISOLINES
)
556 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
558 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
563 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
568 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
571 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
577 "%s(mode=%s vs transform feedback %s)",
579 _mesa_lookup_prim_by_nr(mode
),
580 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
585 /* From GL_INTEL_conservative_rasterization spec:
587 * The conservative rasterization option applies only to polygons with
588 * PolygonMode state set to FILL. Draw requests for polygons with different
589 * PolygonMode setting or for other primitive types (points/lines) generate
590 * INVALID_OPERATION error.
592 if (ctx
->IntelConservativeRasterization
) {
593 GLboolean pass
= GL_TRUE
;
600 case GL_LINES_ADJACENCY
:
601 case GL_LINE_STRIP_ADJACENCY
:
605 case GL_TRIANGLE_STRIP
:
606 case GL_TRIANGLE_FAN
:
610 case GL_TRIANGLES_ADJACENCY
:
611 case GL_TRIANGLE_STRIP_ADJACENCY
:
612 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
613 ctx
->Polygon
.BackMode
!= GL_FILL
)
620 _mesa_error(ctx
, GL_INVALID_OPERATION
,
621 "mode=%s invalid with GL_INTEL_conservative_rasterization",
622 _mesa_lookup_prim_by_nr(mode
));
631 * Verify that the element type is valid.
633 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
636 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
639 case GL_UNSIGNED_BYTE
:
640 case GL_UNSIGNED_SHORT
:
641 case GL_UNSIGNED_INT
:
645 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
646 _mesa_enum_to_string(type
));
652 validate_DrawElements_common(struct gl_context
*ctx
,
653 GLenum mode
, GLsizei count
, GLenum type
,
654 const GLvoid
*indices
,
657 /* Section 2.14.2 (Transform Feedback Primitive Capture) of the OpenGL ES
660 * The error INVALID_OPERATION is also generated by DrawElements,
661 * DrawElementsInstanced, and DrawRangeElements while transform feedback
662 * is active and not paused, regardless of mode.
664 * The OES_geometry_shader_spec says:
670 * (13) Does this extension change how transform feedback operates
671 * compared to unextended OpenGL ES 3.0 or 3.1?
673 * RESOLVED: Yes... Since we no longer require being able to predict how
674 * much geometry will be generated, we also lift the restriction that
675 * only DrawArray* commands are supported and also support the
676 * DrawElements* commands for transform feedback.
678 * This should also be reflected in the body of the spec, but that appears
679 * to have been overlooked. The body of the spec only explicitly allows
680 * the indirect versions.
682 if (_mesa_is_gles3(ctx
) &&
683 !_mesa_has_OES_geometry_shader(ctx
) &&
684 _mesa_is_xfb_active_and_unpaused(ctx
)) {
685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
686 "%s(transform feedback active)", caller
);
691 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
695 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
699 if (!valid_elements_type(ctx
, type
, caller
))
702 if (!check_valid_to_render(ctx
, caller
))
709 * Error checking for glDrawElements(). Includes parameter checking
710 * and VBO bounds checking.
711 * \return GL_TRUE if OK to render, GL_FALSE if error found
714 _mesa_validate_DrawElements(struct gl_context
*ctx
,
715 GLenum mode
, GLsizei count
, GLenum type
,
716 const GLvoid
*indices
)
718 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
724 * Error checking for glMultiDrawElements(). Includes parameter checking
725 * and VBO bounds checking.
726 * \return GL_TRUE if OK to render, GL_FALSE if error found
729 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
730 GLenum mode
, const GLsizei
*count
,
731 GLenum type
, const GLvoid
* const *indices
,
737 * Section 2.3.1 (Errors) of the OpenGL 4.5 (Core Profile) spec says:
739 * "If a negative number is provided where an argument of type sizei or
740 * sizeiptr is specified, an INVALID_VALUE error is generated."
742 * and in the same section:
744 * "In other cases, there are no side effects unless otherwise noted;
745 * the command which generates the error is ignored so that it has no
746 * effect on GL state or framebuffer contents."
748 * Hence, check both primcount and all the count[i].
751 _mesa_error(ctx
, GL_INVALID_VALUE
,
752 "glMultiDrawElements(primcount=%d)", primcount
);
756 for (i
= 0; i
< primcount
; i
++) {
758 _mesa_error(ctx
, GL_INVALID_VALUE
,
759 "glMultiDrawElements(count)" );
764 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
768 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
771 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
774 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
776 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
777 for (i
= 0; i
< primcount
; i
++) {
788 * Error checking for glDrawRangeElements(). Includes parameter checking
789 * and VBO bounds checking.
790 * \return GL_TRUE if OK to render, GL_FALSE if error found
793 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
794 GLuint start
, GLuint end
,
795 GLsizei count
, GLenum type
,
796 const GLvoid
*indices
)
799 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
803 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
804 "glDrawRangeElements");
809 need_xfb_remaining_prims_check(const struct gl_context
*ctx
)
811 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
812 * Primitive Capture):
814 * The error INVALID_OPERATION is generated by DrawArrays and
815 * DrawArraysInstanced if recording the vertices of a primitive to the
816 * buffer objects being used for transform feedback purposes would result
817 * in either exceeding the limits of any buffer object’s size, or in
818 * exceeding the end position offset + size − 1, as set by
821 * This is in contrast to the behaviour of desktop GL, where the extra
822 * primitives are silently dropped from the transform feedback buffer.
824 * This text is removed in ES 3.2, presumably because it's not really
825 * implementable with geometry and tessellation shaders. In fact,
826 * the OES_geometry_shader spec says:
828 * "(13) Does this extension change how transform feedback operates
829 * compared to unextended OpenGL ES 3.0 or 3.1?
831 * RESOLVED: Yes. Because dynamic geometry amplification in a geometry
832 * shader can make it difficult if not impossible to predict the amount
833 * of geometry that may be generated in advance of executing the shader,
834 * the draw-time error for transform feedback buffer overflow conditions
835 * is removed and replaced with the GL behavior (primitives are not
836 * written and the corresponding counter is not updated)..."
838 return _mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
) &&
839 !_mesa_has_OES_geometry_shader(ctx
) &&
840 !_mesa_has_OES_tessellation_shader(ctx
);
845 * Figure out the number of transform feedback primitives that will be output
846 * considering the drawing mode, number of vertices, and instance count,
847 * assuming that no geometry shading is done and primitive restart is not
850 * This is used by driver back-ends in implementing the PRIMITIVES_GENERATED
851 * and TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN queries. It is also used to
852 * pre-validate draw calls in GLES3 (where draw calls only succeed if there is
853 * enough room in the transform feedback buffer for the result).
856 count_tessellated_primitives(GLenum mode
, GLuint count
, GLuint num_instances
)
858 size_t num_primitives
;
861 num_primitives
= count
;
864 num_primitives
= count
>= 2 ? count
- 1 : 0;
867 num_primitives
= count
>= 2 ? count
: 0;
870 num_primitives
= count
/ 2;
872 case GL_TRIANGLE_STRIP
:
873 case GL_TRIANGLE_FAN
:
875 num_primitives
= count
>= 3 ? count
- 2 : 0;
878 num_primitives
= count
/ 3;
881 num_primitives
= count
>= 4 ? ((count
/ 2) - 1) * 2 : 0;
884 num_primitives
= (count
/ 4) * 2;
886 case GL_LINES_ADJACENCY
:
887 num_primitives
= count
/ 4;
889 case GL_LINE_STRIP_ADJACENCY
:
890 num_primitives
= count
>= 4 ? count
- 3 : 0;
892 case GL_TRIANGLES_ADJACENCY
:
893 num_primitives
= count
/ 6;
895 case GL_TRIANGLE_STRIP_ADJACENCY
:
896 num_primitives
= count
>= 6 ? (count
- 4) / 2 : 0;
899 assert(!"Unexpected primitive type in count_tessellated_primitives");
903 return num_primitives
* num_instances
;
908 validate_draw_arrays(struct gl_context
*ctx
, const char *func
,
909 GLenum mode
, GLsizei count
, GLsizei numInstances
)
912 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", func
);
916 if (!_mesa_valid_prim_mode(ctx
, mode
, func
))
919 if (!check_valid_to_render(ctx
, func
))
922 if (need_xfb_remaining_prims_check(ctx
)) {
923 struct gl_transform_feedback_object
*xfb_obj
924 = ctx
->TransformFeedback
.CurrentObject
;
925 size_t prim_count
= count_tessellated_primitives(mode
, count
, numInstances
);
926 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
927 _mesa_error(ctx
, GL_INVALID_OPERATION
,
928 "%s(exceeds transform feedback size)", func
);
931 xfb_obj
->GlesRemainingPrims
-= prim_count
;
941 * Called from the tnl module to error check the function parameters and
942 * verify that we really can draw something.
943 * \return GL_TRUE if OK to render, GL_FALSE if error found
946 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
948 return validate_draw_arrays(ctx
, "glDrawArrays", mode
, count
, 1);
953 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
954 GLsizei count
, GLsizei numInstances
)
957 _mesa_error(ctx
, GL_INVALID_VALUE
,
958 "glDrawArraysInstanced(start=%d)", first
);
962 if (numInstances
<= 0) {
963 if (numInstances
< 0)
964 _mesa_error(ctx
, GL_INVALID_VALUE
,
965 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
969 return validate_draw_arrays(ctx
, "glDrawArraysInstanced", mode
, count
, 1);
974 * Called to error check the function parameters.
976 * Note that glMultiDrawArrays is not part of GLES, so there's limited scope
977 * for sharing code with the validation of glDrawArrays.
980 _mesa_validate_MultiDrawArrays(struct gl_context
*ctx
, GLenum mode
,
981 const GLsizei
*count
, GLsizei primcount
)
985 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawArrays"))
988 if (!check_valid_to_render(ctx
, "glMultiDrawArrays"))
992 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMultiDrawArrays(primcount=%d)",
997 for (i
= 0; i
< primcount
; ++i
) {
999 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMultiDrawArrays(count[%d]=%d)",
1005 if (need_xfb_remaining_prims_check(ctx
)) {
1006 struct gl_transform_feedback_object
*xfb_obj
1007 = ctx
->TransformFeedback
.CurrentObject
;
1008 size_t xfb_prim_count
= 0;
1010 for (i
= 0; i
< primcount
; ++i
)
1011 xfb_prim_count
+= count_tessellated_primitives(mode
, count
[i
], 1);
1013 if (xfb_obj
->GlesRemainingPrims
< xfb_prim_count
) {
1014 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1015 "glMultiDrawArrays(exceeds transform feedback size)");
1018 xfb_obj
->GlesRemainingPrims
-= xfb_prim_count
;
1026 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
1027 GLenum mode
, GLsizei count
, GLenum type
,
1028 const GLvoid
*indices
, GLsizei numInstances
)
1030 if (numInstances
< 0) {
1031 _mesa_error(ctx
, GL_INVALID_VALUE
,
1032 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
1036 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
1037 "glDrawElementsInstanced")
1038 && (numInstances
> 0);
1043 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
1045 struct gl_transform_feedback_object
*obj
,
1047 GLsizei numInstances
)
1049 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
1054 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
1058 /* From the GL 4.5 specification, page 429:
1059 * "An INVALID_VALUE error is generated if id is not the name of a
1060 * transform feedback object."
1062 if (!obj
->EverBound
) {
1063 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
1067 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
1068 _mesa_error(ctx
, GL_INVALID_VALUE
,
1069 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
1073 if (!obj
->EndedAnytime
) {
1074 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
1078 if (numInstances
<= 0) {
1079 if (numInstances
< 0)
1080 _mesa_error(ctx
, GL_INVALID_VALUE
,
1081 "glDrawTransformFeedback*Instanced(numInstances=%d)",
1086 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
1094 valid_draw_indirect(struct gl_context
*ctx
,
1095 GLenum mode
, const GLvoid
*indirect
,
1096 GLsizei size
, const char *name
)
1098 const uint64_t end
= (uint64_t) (uintptr_t) indirect
+ size
;
1100 /* OpenGL ES 3.1 spec. section 10.5:
1102 * "DrawArraysIndirect requires that all data sourced for the
1103 * command, including the DrawArraysIndirectCommand
1104 * structure, be in buffer objects, and may not be called when
1105 * the default vertex array object is bound."
1107 if (ctx
->API
!= API_OPENGL_COMPAT
&&
1108 ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
1109 _mesa_error(ctx
, GL_INVALID_OPERATION
, "(no VAO bound)");
1113 /* From OpenGL ES 3.1 spec. section 10.5:
1114 * "An INVALID_OPERATION error is generated if zero is bound to
1115 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
1118 * Here we check that for each enabled vertex array we have a vertex
1121 if (_mesa_is_gles31(ctx
) &&
1122 ctx
->Array
.VAO
->Enabled
& ~ctx
->Array
.VAO
->VertexAttribBufferMask
) {
1123 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(No VBO bound)", name
);
1127 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
1130 /* OpenGL ES 3.1 specification, section 10.5:
1132 * "An INVALID_OPERATION error is generated if
1133 * transform feedback is active and not paused."
1135 * The OES_geometry_shader spec says:
1137 * On p. 250 in the errors section for the DrawArraysIndirect command,
1138 * and on p. 254 in the errors section for the DrawElementsIndirect
1139 * command, delete the errors which state:
1141 * "An INVALID_OPERATION error is generated if transform feedback is
1142 * active and not paused."
1144 * (thus allowing transform feedback to work with indirect draw commands).
1146 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
1147 _mesa_is_xfb_active_and_unpaused(ctx
)) {
1148 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1149 "%s(TransformFeedback is active and not paused)", name
);
1152 /* From OpenGL version 4.4. section 10.5
1153 * and OpenGL ES 3.1, section 10.6:
1155 * "An INVALID_VALUE error is generated if indirect is not a
1156 * multiple of the size, in basic machine units, of uint."
1158 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
1159 _mesa_error(ctx
, GL_INVALID_VALUE
,
1160 "%s(indirect is not aligned)", name
);
1164 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1166 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
1170 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
1171 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1172 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
1176 /* From the ARB_draw_indirect specification:
1177 * "An INVALID_OPERATION error is generated if the commands source data
1178 * beyond the end of the buffer object [...]"
1180 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
1181 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1182 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
1186 if (!check_valid_to_render(ctx
, name
))
1192 static inline GLboolean
1193 valid_draw_indirect_elements(struct gl_context
*ctx
,
1194 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
1195 GLsizeiptr size
, const char *name
)
1197 if (!valid_elements_type(ctx
, type
, name
))
1201 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1202 * may not come from a client array and must come from an index buffer.
1203 * If no element array buffer is bound, an INVALID_OPERATION error is
1206 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1207 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1208 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
1212 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
1216 _mesa_valid_draw_indirect_multi(struct gl_context
*ctx
,
1217 GLsizei primcount
, GLsizei stride
,
1221 /* From the ARB_multi_draw_indirect specification:
1222 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
1223 * MultiDrawElementsIndirect if <primcount> is negative."
1225 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
1228 if (primcount
< 0) {
1229 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
1234 /* From the ARB_multi_draw_indirect specification:
1235 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
1236 * error is generated."
1239 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
1247 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
1249 const GLvoid
*indirect
)
1251 const unsigned drawArraysNumParams
= 4;
1253 return valid_draw_indirect(ctx
, mode
,
1254 indirect
, drawArraysNumParams
* sizeof(GLuint
),
1255 "glDrawArraysIndirect");
1259 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
1260 GLenum mode
, GLenum type
,
1261 const GLvoid
*indirect
)
1263 const unsigned drawElementsNumParams
= 5;
1265 return valid_draw_indirect_elements(ctx
, mode
, type
,
1266 indirect
, drawElementsNumParams
* sizeof(GLuint
),
1267 "glDrawElementsIndirect");
1271 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
1273 const GLvoid
*indirect
,
1274 GLsizei primcount
, GLsizei stride
)
1276 GLsizeiptr size
= 0;
1277 const unsigned drawArraysNumParams
= 4;
1279 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1280 assert(stride
!= 0);
1282 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1283 "glMultiDrawArraysIndirect"))
1286 /* number of bytes of the indirect buffer which will be read */
1288 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
1291 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
1292 "glMultiDrawArraysIndirect"))
1299 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
1300 GLenum mode
, GLenum type
,
1301 const GLvoid
*indirect
,
1302 GLsizei primcount
, GLsizei stride
)
1304 GLsizeiptr size
= 0;
1305 const unsigned drawElementsNumParams
= 5;
1307 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1308 assert(stride
!= 0);
1310 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1311 "glMultiDrawElementsIndirect"))
1314 /* number of bytes of the indirect buffer which will be read */
1316 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1319 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1321 "glMultiDrawElementsIndirect"))
1328 valid_draw_indirect_parameters(struct gl_context
*ctx
,
1332 /* From the ARB_indirect_parameters specification:
1333 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
1334 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
1337 if (drawcount
& 3) {
1338 _mesa_error(ctx
, GL_INVALID_VALUE
,
1339 "%s(drawcount is not a multiple of 4)", name
);
1343 /* From the ARB_indirect_parameters specification:
1344 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1345 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
1346 * PARAMETER_BUFFER_ARB binding point."
1348 if (!_mesa_is_bufferobj(ctx
->ParameterBuffer
)) {
1349 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1350 "%s: no buffer bound to PARAMETER_BUFFER", name
);
1354 if (_mesa_check_disallowed_mapping(ctx
->ParameterBuffer
)) {
1355 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1356 "%s(PARAMETER_BUFFER is mapped)", name
);
1360 /* From the ARB_indirect_parameters specification:
1361 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1362 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
1363 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
1364 * specified by <drawcount> would result in an out-of-bounds access."
1366 if (ctx
->ParameterBuffer
->Size
< drawcount
+ sizeof(GLsizei
)) {
1367 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1368 "%s(PARAMETER_BUFFER too small)", name
);
1376 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context
*ctx
,
1380 GLsizei maxdrawcount
,
1383 GLsizeiptr size
= 0;
1384 const unsigned drawArraysNumParams
= 4;
1386 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1387 assert(stride
!= 0);
1389 if (!_mesa_valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1390 "glMultiDrawArraysIndirectCountARB"))
1393 /* number of bytes of the indirect buffer which will be read */
1395 ? (maxdrawcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
1398 if (!valid_draw_indirect(ctx
, mode
, (void *)indirect
, size
,
1399 "glMultiDrawArraysIndirectCountARB"))
1402 return valid_draw_indirect_parameters(
1403 ctx
, "glMultiDrawArraysIndirectCountARB", drawcount
);
1407 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context
*ctx
,
1408 GLenum mode
, GLenum type
,
1411 GLsizei maxdrawcount
,
1414 GLsizeiptr size
= 0;
1415 const unsigned drawElementsNumParams
= 5;
1417 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1418 assert(stride
!= 0);
1420 if (!_mesa_valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1421 "glMultiDrawElementsIndirectCountARB"))
1424 /* number of bytes of the indirect buffer which will be read */
1426 ? (maxdrawcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1429 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1430 (void *)indirect
, size
,
1431 "glMultiDrawElementsIndirectCountARB"))
1434 return valid_draw_indirect_parameters(
1435 ctx
, "glMultiDrawElementsIndirectCountARB", drawcount
);