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"
28 #include "bufferobj.h"
34 #include "transformfeedback.h"
39 * Check if OK to draw arrays/elements.
42 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
44 if (!_mesa_valid_to_render(ctx
, function
)) {
50 /* For ES2, we can draw if we have a vertex program/shader). */
51 return ctx
->VertexProgram
._Current
!= NULL
;
54 /* For OpenGL ES, only draw if we have vertex positions
56 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
61 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
62 * Core Profile spec says:
64 * "An INVALID_OPERATION error is generated if no vertex array
65 * object is bound (see section 10.3.1)."
67 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
68 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no VAO bound)", function
);
72 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
75 * "If there is no active program for the vertex or fragment shader
76 * stages, the results of vertex and/or fragment processing will be
77 * undefined. However, this is not an error."
79 * The fragment shader is not tested here because other state (e.g.,
80 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
82 return ctx
->VertexProgram
._Current
!= NULL
;
84 case API_OPENGL_COMPAT
:
85 if (ctx
->VertexProgram
._Current
!= NULL
) {
86 /* Draw regardless of whether or not we have any vertex arrays.
87 * (Ex: could draw a point using a constant vertex pos)
91 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
94 return (ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
95 ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
100 unreachable("Invalid API value in check_valid_to_render()");
108 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
109 * etc? The set of legal values depends on whether geometry shaders/programs
111 * Note: This may be called during display list compilation.
114 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
122 case GL_TRIANGLE_STRIP
:
123 case GL_TRIANGLE_FAN
:
128 return (ctx
->API
== API_OPENGL_COMPAT
);
129 case GL_LINES_ADJACENCY
:
130 case GL_LINE_STRIP_ADJACENCY
:
131 case GL_TRIANGLES_ADJACENCY
:
132 case GL_TRIANGLE_STRIP_ADJACENCY
:
133 return _mesa_has_geometry_shaders(ctx
);
141 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
142 * etc? Also, do additional checking related to transformation feedback.
143 * Note: this function cannot be called during glNewList(GL_COMPILE) because
144 * this code depends on current transform feedback state.
147 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
149 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
152 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
156 /* From the ARB_geometry_shader4 spec:
158 * The error INVALID_OPERATION is generated if Begin, or any command that
159 * implicitly calls Begin, is called when a geometry shader is active and:
161 * * the input primitive type of the current geometry shader is
162 * POINTS and <mode> is not POINTS,
164 * * the input primitive type of the current geometry shader is
165 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
167 * * the input primitive type of the current geometry shader is
168 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
171 * * the input primitive type of the current geometry shader is
172 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
173 * LINE_STRIP_ADJACENCY_ARB, or
175 * * the input primitive type of the current geometry shader is
176 * TRIANGLES_ADJACENCY_ARB and <mode> is not
177 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
180 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
181 const GLenum geom_mode
=
182 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.InputType
;
185 valid_enum
= (geom_mode
== GL_POINTS
);
190 valid_enum
= (geom_mode
== GL_LINES
);
193 case GL_TRIANGLE_STRIP
:
194 case GL_TRIANGLE_FAN
:
195 valid_enum
= (geom_mode
== GL_TRIANGLES
);
202 case GL_LINES_ADJACENCY
:
203 case GL_LINE_STRIP_ADJACENCY
:
204 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
206 case GL_TRIANGLES_ADJACENCY
:
207 case GL_TRIANGLE_STRIP_ADJACENCY
:
208 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
215 _mesa_error(ctx
, GL_INVALID_OPERATION
,
216 "%s(mode=%s vs geometry shader input %s)",
218 _mesa_lookup_prim_by_nr(mode
),
219 _mesa_lookup_prim_by_nr(geom_mode
));
224 /* From the GL_EXT_transform_feedback spec:
226 * "The error INVALID_OPERATION is generated if Begin, or any command
227 * that performs an explicit Begin, is called when:
229 * * a geometry shader is not active and <mode> does not match the
230 * allowed begin modes for the current transform feedback state as
231 * given by table X.1.
233 * * a geometry shader is active and the output primitive type of the
234 * geometry shader does not match the allowed begin modes for the
235 * current transform feedback state as given by table X.1.
238 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
239 GLboolean pass
= GL_TRUE
;
241 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
242 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.OutputType
) {
244 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
247 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
249 case GL_TRIANGLE_STRIP
:
250 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
259 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
264 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
267 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
272 _mesa_error(ctx
, GL_INVALID_OPERATION
,
273 "%s(mode=%s vs transform feedback %s)",
275 _mesa_lookup_prim_by_nr(mode
),
276 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
285 * Verify that the element type is valid.
287 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
290 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
293 case GL_UNSIGNED_BYTE
:
294 case GL_UNSIGNED_SHORT
:
295 case GL_UNSIGNED_INT
:
299 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
300 _mesa_lookup_enum_by_nr(type
));
306 validate_DrawElements_common(struct gl_context
*ctx
,
307 GLenum mode
, GLsizei count
, GLenum type
,
308 const GLvoid
*indices
,
311 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
312 * Primitive Capture):
314 * The error INVALID_OPERATION is also generated by DrawElements,
315 * DrawElementsInstanced, and DrawRangeElements while transform feedback
316 * is active and not paused, regardless of mode.
318 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
319 _mesa_error(ctx
, GL_INVALID_OPERATION
,
320 "%s(transform feedback active)", caller
);
325 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
329 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
333 if (!valid_elements_type(ctx
, type
, caller
))
336 if (!check_valid_to_render(ctx
, caller
))
339 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
341 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
351 * Error checking for glDrawElements(). Includes parameter checking
352 * and VBO bounds checking.
353 * \return GL_TRUE if OK to render, GL_FALSE if error found
356 _mesa_validate_DrawElements(struct gl_context
*ctx
,
357 GLenum mode
, GLsizei count
, GLenum type
,
358 const GLvoid
*indices
)
360 FLUSH_CURRENT(ctx
, 0);
362 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
368 * Error checking for glMultiDrawElements(). Includes parameter checking
369 * and VBO bounds checking.
370 * \return GL_TRUE if OK to render, GL_FALSE if error found
373 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
374 GLenum mode
, const GLsizei
*count
,
375 GLenum type
, const GLvoid
* const *indices
,
380 FLUSH_CURRENT(ctx
, 0);
382 for (i
= 0; i
< primcount
; i
++) {
384 _mesa_error(ctx
, GL_INVALID_VALUE
,
385 "glMultiDrawElements(count)" );
390 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
394 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
397 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
400 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
402 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
403 for (i
= 0; i
< primcount
; i
++) {
414 * Error checking for glDrawRangeElements(). Includes parameter checking
415 * and VBO bounds checking.
416 * \return GL_TRUE if OK to render, GL_FALSE if error found
419 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
420 GLuint start
, GLuint end
,
421 GLsizei count
, GLenum type
,
422 const GLvoid
*indices
)
424 FLUSH_CURRENT(ctx
, 0);
427 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
431 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
432 "glDrawRangeElements");
437 * Called from the tnl module to error check the function parameters and
438 * verify that we really can draw something.
439 * \return GL_TRUE if OK to render, GL_FALSE if error found
442 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
444 struct gl_transform_feedback_object
*xfb_obj
445 = ctx
->TransformFeedback
.CurrentObject
;
446 FLUSH_CURRENT(ctx
, 0);
449 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
453 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
457 if (!check_valid_to_render(ctx
, "glDrawArrays"))
460 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
461 * Primitive Capture):
463 * The error INVALID_OPERATION is generated by DrawArrays and
464 * DrawArraysInstanced if recording the vertices of a primitive to the
465 * buffer objects being used for transform feedback purposes would result
466 * in either exceeding the limits of any buffer object’s size, or in
467 * exceeding the end position offset + size − 1, as set by
470 * This is in contrast to the behaviour of desktop GL, where the extra
471 * primitives are silently dropped from the transform feedback buffer.
473 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
474 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
475 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
476 _mesa_error(ctx
, GL_INVALID_OPERATION
,
477 "glDrawArrays(exceeds transform feedback size)");
480 xfb_obj
->GlesRemainingPrims
-= prim_count
;
491 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
492 GLsizei count
, GLsizei numInstances
)
494 struct gl_transform_feedback_object
*xfb_obj
495 = ctx
->TransformFeedback
.CurrentObject
;
496 FLUSH_CURRENT(ctx
, 0);
499 _mesa_error(ctx
, GL_INVALID_VALUE
,
500 "glDrawArraysInstanced(count=%d)", count
);
505 _mesa_error(ctx
, GL_INVALID_VALUE
,
506 "glDrawArraysInstanced(start=%d)", first
);
510 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
514 if (numInstances
<= 0) {
515 if (numInstances
< 0)
516 _mesa_error(ctx
, GL_INVALID_VALUE
,
517 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
521 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
524 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
525 * Primitive Capture):
527 * The error INVALID_OPERATION is generated by DrawArrays and
528 * DrawArraysInstanced if recording the vertices of a primitive to the
529 * buffer objects being used for transform feedback purposes would result
530 * in either exceeding the limits of any buffer object’s size, or in
531 * exceeding the end position offset + size − 1, as set by
534 * This is in contrast to the behaviour of desktop GL, where the extra
535 * primitives are silently dropped from the transform feedback buffer.
537 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
539 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
540 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
541 _mesa_error(ctx
, GL_INVALID_OPERATION
,
542 "glDrawArraysInstanced(exceeds transform feedback size)");
545 xfb_obj
->GlesRemainingPrims
-= prim_count
;
556 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
557 GLenum mode
, GLsizei count
, GLenum type
,
558 const GLvoid
*indices
, GLsizei numInstances
)
560 FLUSH_CURRENT(ctx
, 0);
562 if (numInstances
< 0) {
563 _mesa_error(ctx
, GL_INVALID_VALUE
,
564 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
568 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
569 "glDrawElementsInstanced")
570 && (numInstances
> 0);
575 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
577 struct gl_transform_feedback_object
*obj
,
579 GLsizei numInstances
)
581 FLUSH_CURRENT(ctx
, 0);
583 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
588 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
592 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
593 _mesa_error(ctx
, GL_INVALID_VALUE
,
594 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
598 if (!obj
->EndedAnytime
) {
599 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
603 if (numInstances
<= 0) {
604 if (numInstances
< 0)
605 _mesa_error(ctx
, GL_INVALID_VALUE
,
606 "glDrawTransformFeedback*Instanced(numInstances=%d)",
611 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
619 valid_draw_indirect(struct gl_context
*ctx
,
620 GLenum mode
, const GLvoid
*indirect
,
621 GLsizei size
, const char *name
)
623 const GLsizeiptr end
= (GLsizeiptr
)indirect
+ size
;
625 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
629 /* From the ARB_draw_indirect specification:
630 * "An INVALID_OPERATION error is generated [...] if <indirect> is no
633 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
634 _mesa_error(ctx
, GL_INVALID_OPERATION
,
635 "%s(indirect is not aligned)", name
);
639 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
640 _mesa_error(ctx
, GL_INVALID_OPERATION
,
641 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
645 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
646 _mesa_error(ctx
, GL_INVALID_OPERATION
,
647 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
651 /* From the ARB_draw_indirect specification:
652 * "An INVALID_OPERATION error is generated if the commands source data
653 * beyond the end of the buffer object [...]"
655 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
656 _mesa_error(ctx
, GL_INVALID_OPERATION
,
657 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
661 if (!check_valid_to_render(ctx
, name
))
667 static inline GLboolean
668 valid_draw_indirect_elements(struct gl_context
*ctx
,
669 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
670 GLsizeiptr size
, const char *name
)
672 if (!valid_elements_type(ctx
, type
, name
))
676 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
677 * may not come from a client array and must come from an index buffer.
678 * If no element array buffer is bound, an INVALID_OPERATION error is
681 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
682 _mesa_error(ctx
, GL_INVALID_OPERATION
,
683 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
687 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
690 static inline GLboolean
691 valid_draw_indirect_multi(struct gl_context
*ctx
,
692 GLsizei primcount
, GLsizei stride
,
696 /* From the ARB_multi_draw_indirect specification:
697 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
698 * MultiDrawElementsIndirect if <primcount> is negative."
700 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
704 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
709 /* From the ARB_multi_draw_indirect specification:
710 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
711 * error is generated."
714 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
722 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
724 const GLvoid
*indirect
)
726 const unsigned drawArraysNumParams
= 4;
728 FLUSH_CURRENT(ctx
, 0);
730 return valid_draw_indirect(ctx
, mode
,
731 indirect
, drawArraysNumParams
* sizeof(GLuint
),
732 "glDrawArraysIndirect");
736 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
737 GLenum mode
, GLenum type
,
738 const GLvoid
*indirect
)
740 const unsigned drawElementsNumParams
= 5;
742 FLUSH_CURRENT(ctx
, 0);
744 return valid_draw_indirect_elements(ctx
, mode
, type
,
745 indirect
, drawElementsNumParams
* sizeof(GLuint
),
746 "glDrawElementsIndirect");
750 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
752 const GLvoid
*indirect
,
753 GLsizei primcount
, GLsizei stride
)
756 const unsigned drawArraysNumParams
= 4;
758 FLUSH_CURRENT(ctx
, 0);
760 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
763 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
764 "glMultiDrawArraysIndirect"))
767 /* number of bytes of the indirect buffer which will be read */
769 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
772 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
773 "glMultiDrawArraysIndirect"))
780 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
781 GLenum mode
, GLenum type
,
782 const GLvoid
*indirect
,
783 GLsizei primcount
, GLsizei stride
)
786 const unsigned drawElementsNumParams
= 5;
788 FLUSH_CURRENT(ctx
, 0);
790 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
793 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
794 "glMultiDrawElementsIndirect"))
797 /* number of bytes of the indirect buffer which will be read */
799 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
802 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
804 "glMultiDrawElementsIndirect"))