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 if (!ctx
->VertexProgram
._Current
)
56 /* For OpenGL ES, only draw if we have vertex positions
58 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
63 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
64 * Core Profile spec says:
66 * "An INVALID_OPERATION error is generated if no vertex array
67 * object is bound (see section 10.3.1)."
69 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
70 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no VAO bound)", function
);
74 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
77 * "If there is no active program for the vertex or fragment shader
78 * stages, the results of vertex and/or fragment processing will be
79 * undefined. However, this is not an error."
81 * The fragment shader is not tested here because other state (e.g.,
82 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
84 return ctx
->VertexProgram
._Current
!= NULL
;
86 case API_OPENGL_COMPAT
: {
87 const struct gl_shader_program
*const vsProg
=
88 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
89 const bool haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
90 const bool haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
92 if (haveVertexShader
|| haveVertexProgram
) {
93 /* Draw regardless of whether or not we have any vertex arrays.
94 * (Ex: could draw a point using a constant vertex pos)
98 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
101 return (ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
102 ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
108 unreachable("Invalid API value in check_valid_to_render()");
116 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
117 * etc? The set of legal values depends on whether geometry shaders/programs
119 * Note: This may be called during display list compilation.
122 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
130 case GL_TRIANGLE_STRIP
:
131 case GL_TRIANGLE_FAN
:
136 return (ctx
->API
== API_OPENGL_COMPAT
);
137 case GL_LINES_ADJACENCY
:
138 case GL_LINE_STRIP_ADJACENCY
:
139 case GL_TRIANGLES_ADJACENCY
:
140 case GL_TRIANGLE_STRIP_ADJACENCY
:
141 return _mesa_has_geometry_shaders(ctx
);
149 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
150 * etc? Also, do additional checking related to transformation feedback.
151 * Note: this function cannot be called during glNewList(GL_COMPILE) because
152 * this code depends on current transform feedback state.
155 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
157 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
160 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
164 /* From the ARB_geometry_shader4 spec:
166 * The error INVALID_OPERATION is generated if Begin, or any command that
167 * implicitly calls Begin, is called when a geometry shader is active and:
169 * * the input primitive type of the current geometry shader is
170 * POINTS and <mode> is not POINTS,
172 * * the input primitive type of the current geometry shader is
173 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
175 * * the input primitive type of the current geometry shader is
176 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
179 * * the input primitive type of the current geometry shader is
180 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
181 * LINE_STRIP_ADJACENCY_ARB, or
183 * * the input primitive type of the current geometry shader is
184 * TRIANGLES_ADJACENCY_ARB and <mode> is not
185 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
188 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
189 const GLenum geom_mode
=
190 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.InputType
;
193 valid_enum
= (geom_mode
== GL_POINTS
);
198 valid_enum
= (geom_mode
== GL_LINES
);
201 case GL_TRIANGLE_STRIP
:
202 case GL_TRIANGLE_FAN
:
203 valid_enum
= (geom_mode
== GL_TRIANGLES
);
210 case GL_LINES_ADJACENCY
:
211 case GL_LINE_STRIP_ADJACENCY
:
212 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
214 case GL_TRIANGLES_ADJACENCY
:
215 case GL_TRIANGLE_STRIP_ADJACENCY
:
216 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
223 _mesa_error(ctx
, GL_INVALID_OPERATION
,
224 "%s(mode=%s vs geometry shader input %s)",
226 _mesa_lookup_prim_by_nr(mode
),
227 _mesa_lookup_prim_by_nr(geom_mode
));
232 /* From the GL_EXT_transform_feedback spec:
234 * "The error INVALID_OPERATION is generated if Begin, or any command
235 * that performs an explicit Begin, is called when:
237 * * a geometry shader is not active and <mode> does not match the
238 * allowed begin modes for the current transform feedback state as
239 * given by table X.1.
241 * * a geometry shader is active and the output primitive type of the
242 * geometry shader does not match the allowed begin modes for the
243 * current transform feedback state as given by table X.1.
246 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
247 GLboolean pass
= GL_TRUE
;
249 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
250 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.OutputType
) {
252 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
255 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
257 case GL_TRIANGLE_STRIP
:
258 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
267 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
272 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
275 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
280 _mesa_error(ctx
, GL_INVALID_OPERATION
,
281 "%s(mode=%s vs transform feedback %s)",
283 _mesa_lookup_prim_by_nr(mode
),
284 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
293 * Verify that the element type is valid.
295 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
298 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
301 case GL_UNSIGNED_BYTE
:
302 case GL_UNSIGNED_SHORT
:
303 case GL_UNSIGNED_INT
:
307 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
308 _mesa_lookup_enum_by_nr(type
));
314 validate_DrawElements_common(struct gl_context
*ctx
,
315 GLenum mode
, GLsizei count
, GLenum type
,
316 const GLvoid
*indices
,
319 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
320 * Primitive Capture):
322 * The error INVALID_OPERATION is also generated by DrawElements,
323 * DrawElementsInstanced, and DrawRangeElements while transform feedback
324 * is active and not paused, regardless of mode.
326 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
328 "%s(transform feedback active)", caller
);
333 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
337 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
341 if (!valid_elements_type(ctx
, type
, caller
))
344 if (!check_valid_to_render(ctx
, caller
))
347 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
349 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
359 * Error checking for glDrawElements(). Includes parameter checking
360 * and VBO bounds checking.
361 * \return GL_TRUE if OK to render, GL_FALSE if error found
364 _mesa_validate_DrawElements(struct gl_context
*ctx
,
365 GLenum mode
, GLsizei count
, GLenum type
,
366 const GLvoid
*indices
)
368 FLUSH_CURRENT(ctx
, 0);
370 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
376 * Error checking for glMultiDrawElements(). Includes parameter checking
377 * and VBO bounds checking.
378 * \return GL_TRUE if OK to render, GL_FALSE if error found
381 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
382 GLenum mode
, const GLsizei
*count
,
383 GLenum type
, const GLvoid
* const *indices
,
388 FLUSH_CURRENT(ctx
, 0);
390 for (i
= 0; i
< primcount
; i
++) {
392 _mesa_error(ctx
, GL_INVALID_VALUE
,
393 "glMultiDrawElements(count)" );
398 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
402 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
405 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
408 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
410 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
411 for (i
= 0; i
< primcount
; i
++) {
422 * Error checking for glDrawRangeElements(). Includes parameter checking
423 * and VBO bounds checking.
424 * \return GL_TRUE if OK to render, GL_FALSE if error found
427 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
428 GLuint start
, GLuint end
,
429 GLsizei count
, GLenum type
,
430 const GLvoid
*indices
)
432 FLUSH_CURRENT(ctx
, 0);
435 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
439 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
440 "glDrawRangeElements");
445 * Called from the tnl module to error check the function parameters and
446 * verify that we really can draw something.
447 * \return GL_TRUE if OK to render, GL_FALSE if error found
450 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
452 struct gl_transform_feedback_object
*xfb_obj
453 = ctx
->TransformFeedback
.CurrentObject
;
454 FLUSH_CURRENT(ctx
, 0);
457 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
461 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
465 if (!check_valid_to_render(ctx
, "glDrawArrays"))
468 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
469 * Primitive Capture):
471 * The error INVALID_OPERATION is generated by DrawArrays and
472 * DrawArraysInstanced if recording the vertices of a primitive to the
473 * buffer objects being used for transform feedback purposes would result
474 * in either exceeding the limits of any buffer object’s size, or in
475 * exceeding the end position offset + size − 1, as set by
478 * This is in contrast to the behaviour of desktop GL, where the extra
479 * primitives are silently dropped from the transform feedback buffer.
481 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
482 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
483 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
484 _mesa_error(ctx
, GL_INVALID_OPERATION
,
485 "glDrawArrays(exceeds transform feedback size)");
488 xfb_obj
->GlesRemainingPrims
-= prim_count
;
499 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
500 GLsizei count
, GLsizei numInstances
)
502 struct gl_transform_feedback_object
*xfb_obj
503 = ctx
->TransformFeedback
.CurrentObject
;
504 FLUSH_CURRENT(ctx
, 0);
507 _mesa_error(ctx
, GL_INVALID_VALUE
,
508 "glDrawArraysInstanced(count=%d)", count
);
513 _mesa_error(ctx
, GL_INVALID_VALUE
,
514 "glDrawArraysInstanced(start=%d)", first
);
518 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
522 if (numInstances
<= 0) {
523 if (numInstances
< 0)
524 _mesa_error(ctx
, GL_INVALID_VALUE
,
525 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
529 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
532 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
533 * Primitive Capture):
535 * The error INVALID_OPERATION is generated by DrawArrays and
536 * DrawArraysInstanced if recording the vertices of a primitive to the
537 * buffer objects being used for transform feedback purposes would result
538 * in either exceeding the limits of any buffer object’s size, or in
539 * exceeding the end position offset + size − 1, as set by
542 * This is in contrast to the behaviour of desktop GL, where the extra
543 * primitives are silently dropped from the transform feedback buffer.
545 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
547 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
548 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
549 _mesa_error(ctx
, GL_INVALID_OPERATION
,
550 "glDrawArraysInstanced(exceeds transform feedback size)");
553 xfb_obj
->GlesRemainingPrims
-= prim_count
;
564 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
565 GLenum mode
, GLsizei count
, GLenum type
,
566 const GLvoid
*indices
, GLsizei numInstances
)
568 FLUSH_CURRENT(ctx
, 0);
570 if (numInstances
< 0) {
571 _mesa_error(ctx
, GL_INVALID_VALUE
,
572 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
576 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
577 "glDrawElementsInstanced")
578 && (numInstances
> 0);
583 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
585 struct gl_transform_feedback_object
*obj
,
587 GLsizei numInstances
)
589 FLUSH_CURRENT(ctx
, 0);
591 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
596 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
600 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
601 _mesa_error(ctx
, GL_INVALID_VALUE
,
602 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
606 if (!obj
->EndedAnytime
) {
607 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
611 if (numInstances
<= 0) {
612 if (numInstances
< 0)
613 _mesa_error(ctx
, GL_INVALID_VALUE
,
614 "glDrawTransformFeedback*Instanced(numInstances=%d)",
619 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
627 valid_draw_indirect(struct gl_context
*ctx
,
628 GLenum mode
, const GLvoid
*indirect
,
629 GLsizei size
, const char *name
)
631 const GLsizeiptr end
= (GLsizeiptr
)indirect
+ size
;
633 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
637 /* From the ARB_draw_indirect specification:
638 * "An INVALID_OPERATION error is generated [...] if <indirect> is no
641 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
643 "%s(indirect is not aligned)", name
);
647 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
648 _mesa_error(ctx
, GL_INVALID_OPERATION
,
649 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
653 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
654 _mesa_error(ctx
, GL_INVALID_OPERATION
,
655 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
659 /* From the ARB_draw_indirect specification:
660 * "An INVALID_OPERATION error is generated if the commands source data
661 * beyond the end of the buffer object [...]"
663 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
664 _mesa_error(ctx
, GL_INVALID_OPERATION
,
665 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
669 if (!check_valid_to_render(ctx
, name
))
675 static inline GLboolean
676 valid_draw_indirect_elements(struct gl_context
*ctx
,
677 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
678 GLsizeiptr size
, const char *name
)
680 if (!valid_elements_type(ctx
, type
, name
))
684 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
685 * may not come from a client array and must come from an index buffer.
686 * If no element array buffer is bound, an INVALID_OPERATION error is
689 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
690 _mesa_error(ctx
, GL_INVALID_OPERATION
,
691 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
695 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
698 static inline GLboolean
699 valid_draw_indirect_multi(struct gl_context
*ctx
,
700 GLsizei primcount
, GLsizei stride
,
704 /* From the ARB_multi_draw_indirect specification:
705 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
706 * MultiDrawElementsIndirect if <primcount> is negative."
708 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
712 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
717 /* From the ARB_multi_draw_indirect specification:
718 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
719 * error is generated."
722 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
730 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
732 const GLvoid
*indirect
)
734 const unsigned drawArraysNumParams
= 4;
736 FLUSH_CURRENT(ctx
, 0);
738 return valid_draw_indirect(ctx
, mode
,
739 indirect
, drawArraysNumParams
* sizeof(GLuint
),
740 "glDrawArraysIndirect");
744 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
745 GLenum mode
, GLenum type
,
746 const GLvoid
*indirect
)
748 const unsigned drawElementsNumParams
= 5;
750 FLUSH_CURRENT(ctx
, 0);
752 return valid_draw_indirect_elements(ctx
, mode
, type
,
753 indirect
, drawElementsNumParams
* sizeof(GLuint
),
754 "glDrawElementsIndirect");
758 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
760 const GLvoid
*indirect
,
761 GLsizei primcount
, GLsizei stride
)
764 const unsigned drawArraysNumParams
= 4;
766 FLUSH_CURRENT(ctx
, 0);
768 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
771 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
772 "glMultiDrawArraysIndirect"))
775 /* number of bytes of the indirect buffer which will be read */
777 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
780 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
781 "glMultiDrawArraysIndirect"))
788 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
789 GLenum mode
, GLenum type
,
790 const GLvoid
*indirect
,
791 GLsizei primcount
, GLsizei stride
)
794 const unsigned drawElementsNumParams
= 5;
796 FLUSH_CURRENT(ctx
, 0);
798 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
801 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
802 "glMultiDrawElementsIndirect"))
805 /* number of bytes of the indirect buffer which will be read */
807 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
810 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
812 "glMultiDrawElementsIndirect"))