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 * \return number of bytes in array [count] of type.
42 index_bytes(GLenum type
, GLsizei count
)
44 if (type
== GL_UNSIGNED_INT
) {
45 return count
* sizeof(GLuint
);
47 else if (type
== GL_UNSIGNED_BYTE
) {
48 return count
* sizeof(GLubyte
);
51 ASSERT(type
== GL_UNSIGNED_SHORT
);
52 return count
* sizeof(GLushort
);
58 * Find the max index in the given element/index buffer
61 _mesa_max_buffer_index(struct gl_context
*ctx
, GLuint count
, GLenum type
,
63 struct gl_buffer_object
*elementBuf
)
65 const GLubyte
*map
= NULL
;
69 if (_mesa_is_bufferobj(elementBuf
)) {
70 /* elements are in a user-defined buffer object. need to map it */
71 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, elementBuf
->Size
,
72 GL_MAP_READ_BIT
, elementBuf
);
73 /* Actual address is the sum of pointers */
74 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
77 if (type
== GL_UNSIGNED_INT
) {
78 for (i
= 0; i
< count
; i
++)
79 if (((GLuint
*) indices
)[i
] > max
)
80 max
= ((GLuint
*) indices
)[i
];
82 else if (type
== GL_UNSIGNED_SHORT
) {
83 for (i
= 0; i
< count
; i
++)
84 if (((GLushort
*) indices
)[i
] > max
)
85 max
= ((GLushort
*) indices
)[i
];
88 ASSERT(type
== GL_UNSIGNED_BYTE
);
89 for (i
= 0; i
< count
; i
++)
90 if (((GLubyte
*) indices
)[i
] > max
)
91 max
= ((GLubyte
*) indices
)[i
];
95 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
);
103 * Check if OK to draw arrays/elements.
106 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
108 if (!_mesa_valid_to_render(ctx
, function
)) {
114 /* For ES2, we can draw if any vertex array is enabled (and we
115 * should always have a vertex program/shader). */
116 if (ctx
->Array
.ArrayObj
->_Enabled
== 0x0 || !ctx
->VertexProgram
._Current
)
121 /* For OpenGL ES, only draw if we have vertex positions
123 if (!ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
127 case API_OPENGL_COMPAT
:
128 case API_OPENGL_CORE
:
130 const struct gl_shader_program
*vsProg
=
131 ctx
->Shader
.CurrentVertexProgram
;
132 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
133 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
134 if (haveVertexShader
|| haveVertexProgram
) {
135 /* Draw regardless of whether or not we have any vertex arrays.
136 * (Ex: could draw a point using a constant vertex pos)
141 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
144 return (ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
145 ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
151 assert(!"Invalid API value in check_valid_to_render()");
159 * Do bounds checking on array element indexes. Check that the vertices
160 * pointed to by the indices don't lie outside buffer object bounds.
161 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
164 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
165 const GLvoid
*indices
, GLint basevertex
)
167 struct _mesa_prim prim
;
168 struct _mesa_index_buffer ib
;
171 /* Only the X Server needs to do this -- otherwise, accessing outside
172 * array/BO bounds allows application termination.
174 if (!ctx
->Const
.CheckArrayBounds
)
177 memset(&prim
, 0, sizeof(prim
));
180 memset(&ib
, 0, sizeof(ib
));
183 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
185 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
187 if ((int)(min
+ basevertex
) < 0 ||
188 max
+ basevertex
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
189 /* the max element is out of bounds of one or more enabled arrays */
190 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
191 max
, ctx
->Array
.ArrayObj
->_MaxElement
);
200 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
201 * etc? The set of legal values depends on whether geometry shaders/programs
203 * Note: This may be called during display list compilation.
206 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
214 case GL_TRIANGLE_STRIP
:
215 case GL_TRIANGLE_FAN
:
220 return (ctx
->API
== API_OPENGL_COMPAT
);
221 case GL_LINES_ADJACENCY
:
222 case GL_LINE_STRIP_ADJACENCY
:
223 case GL_TRIANGLES_ADJACENCY
:
224 case GL_TRIANGLE_STRIP_ADJACENCY
:
225 return _mesa_has_geometry_shaders(ctx
);
233 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
234 * etc? Also, do additional checking related to transformation feedback.
235 * Note: this function cannot be called during glNewList(GL_COMPILE) because
236 * this code depends on current transform feedback state.
239 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
241 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
244 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
248 /* From the ARB_geometry_shader4 spec:
250 * The error INVALID_OPERATION is generated if Begin, or any command that
251 * implicitly calls Begin, is called when a geometry shader is active and:
253 * * the input primitive type of the current geometry shader is
254 * POINTS and <mode> is not POINTS,
256 * * the input primitive type of the current geometry shader is
257 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
259 * * the input primitive type of the current geometry shader is
260 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
263 * * the input primitive type of the current geometry shader is
264 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
265 * LINE_STRIP_ADJACENCY_ARB, or
267 * * the input primitive type of the current geometry shader is
268 * TRIANGLES_ADJACENCY_ARB and <mode> is not
269 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
272 if (ctx
->Shader
.CurrentGeometryProgram
) {
273 const GLenum geom_mode
=
274 ctx
->Shader
.CurrentGeometryProgram
->Geom
.InputType
;
277 valid_enum
= (geom_mode
== GL_POINTS
);
282 valid_enum
= (geom_mode
== GL_LINES
);
285 case GL_TRIANGLE_STRIP
:
286 case GL_TRIANGLE_FAN
:
287 valid_enum
= (geom_mode
== GL_TRIANGLES
);
294 case GL_LINES_ADJACENCY
:
295 case GL_LINE_STRIP_ADJACENCY
:
296 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
298 case GL_TRIANGLES_ADJACENCY
:
299 case GL_TRIANGLE_STRIP_ADJACENCY
:
300 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
307 _mesa_error(ctx
, GL_INVALID_OPERATION
,
308 "%s(mode=%s vs geometry shader input %s)",
310 _mesa_lookup_prim_by_nr(mode
),
311 _mesa_lookup_prim_by_nr(geom_mode
));
316 /* From the GL_EXT_transform_feedback spec:
318 * "The error INVALID_OPERATION is generated if Begin, or any command
319 * that performs an explicit Begin, is called when:
321 * * a geometry shader is not active and <mode> does not match the
322 * allowed begin modes for the current transform feedback state as
323 * given by table X.1.
325 * * a geometry shader is active and the output primitive type of the
326 * geometry shader does not match the allowed begin modes for the
327 * current transform feedback state as given by table X.1.
330 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
331 GLboolean pass
= GL_TRUE
;
333 if(ctx
->Shader
.CurrentGeometryProgram
) {
334 switch (ctx
->Shader
.CurrentGeometryProgram
->Geom
.OutputType
) {
336 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
339 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
341 case GL_TRIANGLE_STRIP
:
342 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
351 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
356 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
359 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
365 "%s(mode=%s vs transform feedback %s)",
367 _mesa_lookup_prim_by_nr(mode
),
368 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
377 * Verify that the element type is valid.
379 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
382 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
385 case GL_UNSIGNED_BYTE
:
386 case GL_UNSIGNED_SHORT
:
387 case GL_UNSIGNED_INT
:
391 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
392 _mesa_lookup_enum_by_nr(type
));
398 * Error checking for glDrawElements(). Includes parameter checking
399 * and VBO bounds checking.
400 * \return GL_TRUE if OK to render, GL_FALSE if error found
403 _mesa_validate_DrawElements(struct gl_context
*ctx
,
404 GLenum mode
, GLsizei count
, GLenum type
,
405 const GLvoid
*indices
, GLint basevertex
)
407 FLUSH_CURRENT(ctx
, 0);
409 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
410 * Primitive Capture):
412 * The error INVALID_OPERATION is also generated by DrawElements,
413 * DrawElementsInstanced, and DrawRangeElements while transform feedback
414 * is active and not paused, regardless of mode.
416 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
417 _mesa_error(ctx
, GL_INVALID_OPERATION
,
418 "glDrawElements(transform feedback active)");
423 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
427 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
431 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
434 if (!check_valid_to_render(ctx
, "glDrawElements"))
437 /* Vertex buffer object tests */
438 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
439 /* use indices in the buffer object */
440 /* make sure count doesn't go outside buffer bounds */
441 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
442 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
447 /* not using a VBO */
452 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
463 * Error checking for glMultiDrawElements(). Includes parameter checking
464 * and VBO bounds checking.
465 * \return GL_TRUE if OK to render, GL_FALSE if error found
468 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
469 GLenum mode
, const GLsizei
*count
,
470 GLenum type
, const GLvoid
* const *indices
,
471 GLuint primcount
, const GLint
*basevertex
)
475 FLUSH_CURRENT(ctx
, 0);
477 for (i
= 0; i
< primcount
; i
++) {
479 _mesa_error(ctx
, GL_INVALID_VALUE
,
480 "glMultiDrawElements(count)" );
485 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
489 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
492 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
495 /* Vertex buffer object tests */
496 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
497 /* use indices in the buffer object */
498 /* make sure count doesn't go outside buffer bounds */
499 for (i
= 0; i
< primcount
; i
++) {
500 if (index_bytes(type
, count
[i
]) >
501 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
503 "glMultiDrawElements index out of buffer bounds");
509 /* not using a VBO */
510 for (i
= 0; i
< primcount
; i
++) {
516 for (i
= 0; i
< primcount
; i
++) {
517 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
518 basevertex
? basevertex
[i
] : 0))
527 * Error checking for glDrawRangeElements(). Includes parameter checking
528 * and VBO bounds checking.
529 * \return GL_TRUE if OK to render, GL_FALSE if error found
532 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
533 GLuint start
, GLuint end
,
534 GLsizei count
, GLenum type
,
535 const GLvoid
*indices
, GLint basevertex
)
537 FLUSH_CURRENT(ctx
, 0);
539 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
540 * Primitive Capture):
542 * The error INVALID_OPERATION is also generated by DrawElements,
543 * DrawElementsInstanced, and DrawRangeElements while transform feedback
544 * is active and not paused, regardless of mode.
546 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
547 _mesa_error(ctx
, GL_INVALID_OPERATION
,
548 "glDrawElements(transform feedback active)");
553 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
557 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
562 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
566 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
569 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
572 /* Vertex buffer object tests */
573 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
574 /* use indices in the buffer object */
575 /* make sure count doesn't go outside buffer bounds */
576 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
577 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
582 /* not using a VBO */
587 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
598 * Called from the tnl module to error check the function parameters and
599 * verify that we really can draw something.
600 * \return GL_TRUE if OK to render, GL_FALSE if error found
603 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
604 GLenum mode
, GLint start
, GLsizei count
)
606 struct gl_transform_feedback_object
*xfb_obj
607 = ctx
->TransformFeedback
.CurrentObject
;
608 FLUSH_CURRENT(ctx
, 0);
611 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
615 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
619 if (!check_valid_to_render(ctx
, "glDrawArrays"))
622 if (ctx
->Const
.CheckArrayBounds
) {
623 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
627 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
628 * Primitive Capture):
630 * The error INVALID_OPERATION is generated by DrawArrays and
631 * DrawArraysInstanced if recording the vertices of a primitive to the
632 * buffer objects being used for transform feedback purposes would result
633 * in either exceeding the limits of any buffer object’s size, or in
634 * exceeding the end position offset + size − 1, as set by
637 * This is in contrast to the behaviour of desktop GL, where the extra
638 * primitives are silently dropped from the transform feedback buffer.
640 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
641 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
642 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
643 _mesa_error(ctx
, GL_INVALID_OPERATION
,
644 "glDrawArrays(exceeds transform feedback size)");
647 xfb_obj
->GlesRemainingPrims
-= prim_count
;
658 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
659 GLsizei count
, GLsizei numInstances
)
661 struct gl_transform_feedback_object
*xfb_obj
662 = ctx
->TransformFeedback
.CurrentObject
;
663 FLUSH_CURRENT(ctx
, 0);
666 _mesa_error(ctx
, GL_INVALID_VALUE
,
667 "glDrawArraysInstanced(count=%d)", count
);
672 _mesa_error(ctx
, GL_INVALID_VALUE
,
673 "glDrawArraysInstanced(start=%d)", first
);
677 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
681 if (numInstances
<= 0) {
682 if (numInstances
< 0)
683 _mesa_error(ctx
, GL_INVALID_VALUE
,
684 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
688 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
691 if (ctx
->Const
.CheckArrayBounds
) {
692 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
696 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
697 * Primitive Capture):
699 * The error INVALID_OPERATION is generated by DrawArrays and
700 * DrawArraysInstanced if recording the vertices of a primitive to the
701 * buffer objects being used for transform feedback purposes would result
702 * in either exceeding the limits of any buffer object’s size, or in
703 * exceeding the end position offset + size − 1, as set by
706 * This is in contrast to the behaviour of desktop GL, where the extra
707 * primitives are silently dropped from the transform feedback buffer.
709 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
711 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
712 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
713 _mesa_error(ctx
, GL_INVALID_OPERATION
,
714 "glDrawArraysInstanced(exceeds transform feedback size)");
717 xfb_obj
->GlesRemainingPrims
-= prim_count
;
728 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
729 GLenum mode
, GLsizei count
, GLenum type
,
730 const GLvoid
*indices
, GLsizei numInstances
,
733 FLUSH_CURRENT(ctx
, 0);
735 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
736 * Primitive Capture):
738 * The error INVALID_OPERATION is also generated by DrawElements,
739 * DrawElementsInstanced, and DrawRangeElements while transform feedback
740 * is active and not paused, regardless of mode.
742 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
743 _mesa_error(ctx
, GL_INVALID_OPERATION
,
744 "glDrawElements(transform feedback active)");
749 _mesa_error(ctx
, GL_INVALID_VALUE
,
750 "glDrawElementsInstanced(count=%d)", count
);
754 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
758 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
761 if (numInstances
<= 0) {
762 if (numInstances
< 0)
763 _mesa_error(ctx
, GL_INVALID_VALUE
,
764 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
768 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
771 /* Vertex buffer object tests */
772 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
773 /* use indices in the buffer object */
774 /* make sure count doesn't go outside buffer bounds */
775 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
777 "glDrawElementsInstanced index out of buffer bounds");
782 /* not using a VBO */
790 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
798 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
800 struct gl_transform_feedback_object
*obj
,
802 GLsizei numInstances
)
804 FLUSH_CURRENT(ctx
, 0);
806 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
811 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
815 if (!obj
->EndedAnytime
) {
816 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
820 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
821 _mesa_error(ctx
, GL_INVALID_VALUE
,
822 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
826 if (numInstances
<= 0) {
827 if (numInstances
< 0)
828 _mesa_error(ctx
, GL_INVALID_VALUE
,
829 "glDrawTransformFeedback*Instanced(numInstances=%d)",
834 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
842 valid_draw_indirect(struct gl_context
*ctx
,
843 GLenum mode
, const GLvoid
*indirect
,
844 GLsizei size
, const char *name
)
846 const GLsizeiptr end
= (GLsizeiptr
)indirect
+ size
;
848 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
852 /* From the ARB_draw_indirect specification:
853 * "An INVALID_OPERATION error is generated [...] if <indirect> is no
856 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
858 "%s(indirect is not aligned)", name
);
862 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
864 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
868 if (_mesa_bufferobj_mapped(ctx
->DrawIndirectBuffer
)) {
869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
870 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
874 /* From the ARB_draw_indirect specification:
875 * "An INVALID_OPERATION error is generated if the commands source data
876 * beyond the end of the buffer object [...]"
878 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
879 _mesa_error(ctx
, GL_INVALID_OPERATION
,
880 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
884 if (!check_valid_to_render(ctx
, name
))
890 static inline GLboolean
891 valid_draw_indirect_elements(struct gl_context
*ctx
,
892 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
893 GLsizeiptr size
, const char *name
)
895 if (!valid_elements_type(ctx
, type
, name
))
899 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
900 * may not come from a client array and must come from an index buffer.
901 * If no element array buffer is bound, an INVALID_OPERATION error is
904 if (!_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
906 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
910 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
913 static inline GLboolean
914 valid_draw_indirect_multi(struct gl_context
*ctx
,
915 GLsizei primcount
, GLsizei stride
,
919 /* From the ARB_multi_draw_indirect specification:
920 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
921 * MultiDrawElementsIndirect if <primcount> is negative."
923 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
927 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
932 /* From the ARB_multi_draw_indirect specification:
933 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
934 * error is generated."
937 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
945 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
947 const GLvoid
*indirect
)
949 const unsigned drawArraysNumParams
= 4;
951 FLUSH_CURRENT(ctx
, 0);
953 return valid_draw_indirect(ctx
, mode
,
954 indirect
, drawArraysNumParams
* sizeof(GLuint
),
955 "glDrawArraysIndirect");
959 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
960 GLenum mode
, GLenum type
,
961 const GLvoid
*indirect
)
963 const unsigned drawElementsNumParams
= 5;
965 FLUSH_CURRENT(ctx
, 0);
967 return valid_draw_indirect_elements(ctx
, mode
, type
,
968 indirect
, drawElementsNumParams
* sizeof(GLuint
),
969 "glDrawElementsIndirect");
973 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
975 const GLvoid
*indirect
,
976 GLsizei primcount
, GLsizei stride
)
979 const unsigned drawArraysNumParams
= 4;
981 FLUSH_CURRENT(ctx
, 0);
983 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
986 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
987 "glMultiDrawArraysIndirect"))
990 /* number of bytes of the indirect buffer which will be read */
992 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
995 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
996 "glMultiDrawArraysIndirect"))
1003 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
1004 GLenum mode
, GLenum type
,
1005 const GLvoid
*indirect
,
1006 GLsizei primcount
, GLsizei stride
)
1008 GLsizeiptr size
= 0;
1009 const unsigned drawElementsNumParams
= 5;
1011 FLUSH_CURRENT(ctx
, 0);
1013 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1014 assert(stride
!= 0);
1016 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
1017 "glMultiDrawElementsIndirect"))
1020 /* number of bytes of the indirect buffer which will be read */
1022 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1025 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1027 "glMultiDrawElementsIndirect"))