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
,
74 /* Actual address is the sum of pointers */
75 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
78 if (type
== GL_UNSIGNED_INT
) {
79 for (i
= 0; i
< count
; i
++)
80 if (((GLuint
*) indices
)[i
] > max
)
81 max
= ((GLuint
*) indices
)[i
];
83 else if (type
== GL_UNSIGNED_SHORT
) {
84 for (i
= 0; i
< count
; i
++)
85 if (((GLushort
*) indices
)[i
] > max
)
86 max
= ((GLushort
*) indices
)[i
];
89 ASSERT(type
== GL_UNSIGNED_BYTE
);
90 for (i
= 0; i
< count
; i
++)
91 if (((GLubyte
*) indices
)[i
] > max
)
92 max
= ((GLubyte
*) indices
)[i
];
96 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
, MAP_INTERNAL
);
104 * Check if OK to draw arrays/elements.
107 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
109 if (!_mesa_valid_to_render(ctx
, function
)) {
115 /* For ES2, we can draw if any vertex array is enabled (and we
116 * should always have a vertex program/shader). */
117 if (ctx
->Array
.VAO
->_Enabled
== 0x0 || !ctx
->VertexProgram
._Current
)
122 /* For OpenGL ES, only draw if we have vertex positions
124 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
128 case API_OPENGL_COMPAT
:
129 case API_OPENGL_CORE
:
131 const struct gl_shader_program
*vsProg
=
132 ctx
->Shader
.CurrentProgram
[MESA_SHADER_VERTEX
];
133 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
134 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
135 if (haveVertexShader
|| haveVertexProgram
) {
136 /* Draw regardless of whether or not we have any vertex arrays.
137 * (Ex: could draw a point using a constant vertex pos)
142 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
145 return (ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
146 ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
152 assert(!"Invalid API value in check_valid_to_render()");
160 * Do bounds checking on array element indexes. Check that the vertices
161 * pointed to by the indices don't lie outside buffer object bounds.
162 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
165 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
166 const GLvoid
*indices
, GLint basevertex
)
168 struct _mesa_prim prim
;
169 struct _mesa_index_buffer ib
;
172 /* Only the X Server needs to do this -- otherwise, accessing outside
173 * array/BO bounds allows application termination.
175 if (!ctx
->Const
.CheckArrayBounds
)
178 memset(&prim
, 0, sizeof(prim
));
181 memset(&ib
, 0, sizeof(ib
));
184 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
186 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
188 if ((int)(min
+ basevertex
) < 0 ||
189 max
+ basevertex
>= ctx
->Array
.VAO
->_MaxElement
) {
190 /* the max element is out of bounds of one or more enabled arrays */
191 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
192 max
, ctx
->Array
.VAO
->_MaxElement
);
201 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
202 * etc? The set of legal values depends on whether geometry shaders/programs
204 * Note: This may be called during display list compilation.
207 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
215 case GL_TRIANGLE_STRIP
:
216 case GL_TRIANGLE_FAN
:
221 return (ctx
->API
== API_OPENGL_COMPAT
);
222 case GL_LINES_ADJACENCY
:
223 case GL_LINE_STRIP_ADJACENCY
:
224 case GL_TRIANGLES_ADJACENCY
:
225 case GL_TRIANGLE_STRIP_ADJACENCY
:
226 return _mesa_has_geometry_shaders(ctx
);
234 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
235 * etc? Also, do additional checking related to transformation feedback.
236 * Note: this function cannot be called during glNewList(GL_COMPILE) because
237 * this code depends on current transform feedback state.
240 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
242 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
245 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
249 /* From the ARB_geometry_shader4 spec:
251 * The error INVALID_OPERATION is generated if Begin, or any command that
252 * implicitly calls Begin, is called when a geometry shader is active and:
254 * * the input primitive type of the current geometry shader is
255 * POINTS and <mode> is not POINTS,
257 * * the input primitive type of the current geometry shader is
258 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
260 * * the input primitive type of the current geometry shader is
261 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
264 * * the input primitive type of the current geometry shader is
265 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
266 * LINE_STRIP_ADJACENCY_ARB, or
268 * * the input primitive type of the current geometry shader is
269 * TRIANGLES_ADJACENCY_ARB and <mode> is not
270 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
273 if (ctx
->Shader
.CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
274 const GLenum geom_mode
=
275 ctx
->Shader
.CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.InputType
;
278 valid_enum
= (geom_mode
== GL_POINTS
);
283 valid_enum
= (geom_mode
== GL_LINES
);
286 case GL_TRIANGLE_STRIP
:
287 case GL_TRIANGLE_FAN
:
288 valid_enum
= (geom_mode
== GL_TRIANGLES
);
295 case GL_LINES_ADJACENCY
:
296 case GL_LINE_STRIP_ADJACENCY
:
297 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
299 case GL_TRIANGLES_ADJACENCY
:
300 case GL_TRIANGLE_STRIP_ADJACENCY
:
301 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
308 _mesa_error(ctx
, GL_INVALID_OPERATION
,
309 "%s(mode=%s vs geometry shader input %s)",
311 _mesa_lookup_prim_by_nr(mode
),
312 _mesa_lookup_prim_by_nr(geom_mode
));
317 /* From the GL_EXT_transform_feedback spec:
319 * "The error INVALID_OPERATION is generated if Begin, or any command
320 * that performs an explicit Begin, is called when:
322 * * a geometry shader is not active and <mode> does not match the
323 * allowed begin modes for the current transform feedback state as
324 * given by table X.1.
326 * * a geometry shader is active and the output primitive type of the
327 * geometry shader does not match the allowed begin modes for the
328 * current transform feedback state as given by table X.1.
331 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
332 GLboolean pass
= GL_TRUE
;
334 if(ctx
->Shader
.CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
335 switch (ctx
->Shader
.CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.OutputType
) {
337 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
340 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
342 case GL_TRIANGLE_STRIP
:
343 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
352 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
357 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
360 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
365 _mesa_error(ctx
, GL_INVALID_OPERATION
,
366 "%s(mode=%s vs transform feedback %s)",
368 _mesa_lookup_prim_by_nr(mode
),
369 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
378 * Verify that the element type is valid.
380 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
383 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
386 case GL_UNSIGNED_BYTE
:
387 case GL_UNSIGNED_SHORT
:
388 case GL_UNSIGNED_INT
:
392 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
393 _mesa_lookup_enum_by_nr(type
));
399 * Error checking for glDrawElements(). Includes parameter checking
400 * and VBO bounds checking.
401 * \return GL_TRUE if OK to render, GL_FALSE if error found
404 _mesa_validate_DrawElements(struct gl_context
*ctx
,
405 GLenum mode
, GLsizei count
, GLenum type
,
406 const GLvoid
*indices
, GLint basevertex
)
408 FLUSH_CURRENT(ctx
, 0);
410 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
411 * Primitive Capture):
413 * The error INVALID_OPERATION is also generated by DrawElements,
414 * DrawElementsInstanced, and DrawRangeElements while transform feedback
415 * is active and not paused, regardless of mode.
417 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
418 _mesa_error(ctx
, GL_INVALID_OPERATION
,
419 "glDrawElements(transform feedback active)");
424 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
428 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
432 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
435 if (!check_valid_to_render(ctx
, "glDrawElements"))
438 /* Vertex buffer object tests */
439 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
440 /* use indices in the buffer object */
441 /* make sure count doesn't go outside buffer bounds */
442 if (index_bytes(type
, count
) > ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
443 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
448 /* not using a VBO */
453 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
464 * Error checking for glMultiDrawElements(). Includes parameter checking
465 * and VBO bounds checking.
466 * \return GL_TRUE if OK to render, GL_FALSE if error found
469 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
470 GLenum mode
, const GLsizei
*count
,
471 GLenum type
, const GLvoid
* const *indices
,
472 GLuint primcount
, const GLint
*basevertex
)
476 FLUSH_CURRENT(ctx
, 0);
478 for (i
= 0; i
< primcount
; i
++) {
480 _mesa_error(ctx
, GL_INVALID_VALUE
,
481 "glMultiDrawElements(count)" );
486 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
490 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
493 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
496 /* Vertex buffer object tests */
497 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
498 /* use indices in the buffer object */
499 /* make sure count doesn't go outside buffer bounds */
500 for (i
= 0; i
< primcount
; i
++) {
501 if (index_bytes(type
, count
[i
]) >
502 ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
504 "glMultiDrawElements index out of buffer bounds");
510 /* not using a VBO */
511 for (i
= 0; i
< primcount
; i
++) {
517 for (i
= 0; i
< primcount
; i
++) {
518 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
519 basevertex
? basevertex
[i
] : 0))
528 * Error checking for glDrawRangeElements(). Includes parameter checking
529 * and VBO bounds checking.
530 * \return GL_TRUE if OK to render, GL_FALSE if error found
533 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
534 GLuint start
, GLuint end
,
535 GLsizei count
, GLenum type
,
536 const GLvoid
*indices
, GLint basevertex
)
538 FLUSH_CURRENT(ctx
, 0);
540 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
541 * Primitive Capture):
543 * The error INVALID_OPERATION is also generated by DrawElements,
544 * DrawElementsInstanced, and DrawRangeElements while transform feedback
545 * is active and not paused, regardless of mode.
547 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
548 _mesa_error(ctx
, GL_INVALID_OPERATION
,
549 "glDrawElements(transform feedback active)");
554 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
558 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
563 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
567 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
570 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
573 /* Vertex buffer object tests */
574 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
575 /* use indices in the buffer object */
576 /* make sure count doesn't go outside buffer bounds */
577 if (index_bytes(type
, count
) > ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
578 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
583 /* not using a VBO */
588 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
599 * Called from the tnl module to error check the function parameters and
600 * verify that we really can draw something.
601 * \return GL_TRUE if OK to render, GL_FALSE if error found
604 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
605 GLenum mode
, GLint start
, GLsizei count
)
607 struct gl_transform_feedback_object
*xfb_obj
608 = ctx
->TransformFeedback
.CurrentObject
;
609 FLUSH_CURRENT(ctx
, 0);
612 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
616 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
620 if (!check_valid_to_render(ctx
, "glDrawArrays"))
623 if (ctx
->Const
.CheckArrayBounds
) {
624 if (start
+ count
> (GLint
) ctx
->Array
.VAO
->_MaxElement
)
628 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
629 * Primitive Capture):
631 * The error INVALID_OPERATION is generated by DrawArrays and
632 * DrawArraysInstanced if recording the vertices of a primitive to the
633 * buffer objects being used for transform feedback purposes would result
634 * in either exceeding the limits of any buffer object’s size, or in
635 * exceeding the end position offset + size − 1, as set by
638 * This is in contrast to the behaviour of desktop GL, where the extra
639 * primitives are silently dropped from the transform feedback buffer.
641 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
642 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
643 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
644 _mesa_error(ctx
, GL_INVALID_OPERATION
,
645 "glDrawArrays(exceeds transform feedback size)");
648 xfb_obj
->GlesRemainingPrims
-= prim_count
;
659 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
660 GLsizei count
, GLsizei numInstances
)
662 struct gl_transform_feedback_object
*xfb_obj
663 = ctx
->TransformFeedback
.CurrentObject
;
664 FLUSH_CURRENT(ctx
, 0);
667 _mesa_error(ctx
, GL_INVALID_VALUE
,
668 "glDrawArraysInstanced(count=%d)", count
);
673 _mesa_error(ctx
, GL_INVALID_VALUE
,
674 "glDrawArraysInstanced(start=%d)", first
);
678 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
682 if (numInstances
<= 0) {
683 if (numInstances
< 0)
684 _mesa_error(ctx
, GL_INVALID_VALUE
,
685 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
689 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
692 if (ctx
->Const
.CheckArrayBounds
) {
693 if (first
+ count
> (GLint
) ctx
->Array
.VAO
->_MaxElement
)
697 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
698 * Primitive Capture):
700 * The error INVALID_OPERATION is generated by DrawArrays and
701 * DrawArraysInstanced if recording the vertices of a primitive to the
702 * buffer objects being used for transform feedback purposes would result
703 * in either exceeding the limits of any buffer object’s size, or in
704 * exceeding the end position offset + size − 1, as set by
707 * This is in contrast to the behaviour of desktop GL, where the extra
708 * primitives are silently dropped from the transform feedback buffer.
710 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
712 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
713 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
714 _mesa_error(ctx
, GL_INVALID_OPERATION
,
715 "glDrawArraysInstanced(exceeds transform feedback size)");
718 xfb_obj
->GlesRemainingPrims
-= prim_count
;
729 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
730 GLenum mode
, GLsizei count
, GLenum type
,
731 const GLvoid
*indices
, GLsizei numInstances
,
734 FLUSH_CURRENT(ctx
, 0);
736 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
737 * Primitive Capture):
739 * The error INVALID_OPERATION is also generated by DrawElements,
740 * DrawElementsInstanced, and DrawRangeElements while transform feedback
741 * is active and not paused, regardless of mode.
743 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
744 _mesa_error(ctx
, GL_INVALID_OPERATION
,
745 "glDrawElements(transform feedback active)");
750 _mesa_error(ctx
, GL_INVALID_VALUE
,
751 "glDrawElementsInstanced(count=%d)", count
);
755 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
759 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
762 if (numInstances
<= 0) {
763 if (numInstances
< 0)
764 _mesa_error(ctx
, GL_INVALID_VALUE
,
765 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
769 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
772 /* Vertex buffer object tests */
773 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
774 /* use indices in the buffer object */
775 /* make sure count doesn't go outside buffer bounds */
776 if (index_bytes(type
, count
) > ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
778 "glDrawElementsInstanced index out of buffer bounds");
783 /* not using a VBO */
791 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
799 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
801 struct gl_transform_feedback_object
*obj
,
803 GLsizei numInstances
)
805 FLUSH_CURRENT(ctx
, 0);
807 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
812 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
816 if (!obj
->EndedAnytime
) {
817 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
821 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
822 _mesa_error(ctx
, GL_INVALID_VALUE
,
823 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
827 if (numInstances
<= 0) {
828 if (numInstances
< 0)
829 _mesa_error(ctx
, GL_INVALID_VALUE
,
830 "glDrawTransformFeedback*Instanced(numInstances=%d)",
835 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
843 valid_draw_indirect(struct gl_context
*ctx
,
844 GLenum mode
, const GLvoid
*indirect
,
845 GLsizei size
, const char *name
)
847 const GLsizeiptr end
= (GLsizeiptr
)indirect
+ size
;
849 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
853 /* From the ARB_draw_indirect specification:
854 * "An INVALID_OPERATION error is generated [...] if <indirect> is no
857 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
858 _mesa_error(ctx
, GL_INVALID_OPERATION
,
859 "%s(indirect is not aligned)", name
);
863 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
864 _mesa_error(ctx
, GL_INVALID_OPERATION
,
865 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
869 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
870 _mesa_error(ctx
, GL_INVALID_OPERATION
,
871 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
875 /* From the ARB_draw_indirect specification:
876 * "An INVALID_OPERATION error is generated if the commands source data
877 * beyond the end of the buffer object [...]"
879 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
880 _mesa_error(ctx
, GL_INVALID_OPERATION
,
881 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
885 if (!check_valid_to_render(ctx
, name
))
891 static inline GLboolean
892 valid_draw_indirect_elements(struct gl_context
*ctx
,
893 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
894 GLsizeiptr size
, const char *name
)
896 if (!valid_elements_type(ctx
, type
, name
))
900 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
901 * may not come from a client array and must come from an index buffer.
902 * If no element array buffer is bound, an INVALID_OPERATION error is
905 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
906 _mesa_error(ctx
, GL_INVALID_OPERATION
,
907 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
911 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
914 static inline GLboolean
915 valid_draw_indirect_multi(struct gl_context
*ctx
,
916 GLsizei primcount
, GLsizei stride
,
920 /* From the ARB_multi_draw_indirect specification:
921 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
922 * MultiDrawElementsIndirect if <primcount> is negative."
924 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
928 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
933 /* From the ARB_multi_draw_indirect specification:
934 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
935 * error is generated."
938 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
946 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
948 const GLvoid
*indirect
)
950 const unsigned drawArraysNumParams
= 4;
952 FLUSH_CURRENT(ctx
, 0);
954 return valid_draw_indirect(ctx
, mode
,
955 indirect
, drawArraysNumParams
* sizeof(GLuint
),
956 "glDrawArraysIndirect");
960 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
961 GLenum mode
, GLenum type
,
962 const GLvoid
*indirect
)
964 const unsigned drawElementsNumParams
= 5;
966 FLUSH_CURRENT(ctx
, 0);
968 return valid_draw_indirect_elements(ctx
, mode
, type
,
969 indirect
, drawElementsNumParams
* sizeof(GLuint
),
970 "glDrawElementsIndirect");
974 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
976 const GLvoid
*indirect
,
977 GLsizei primcount
, GLsizei stride
)
980 const unsigned drawArraysNumParams
= 4;
982 FLUSH_CURRENT(ctx
, 0);
984 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
987 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
988 "glMultiDrawArraysIndirect"))
991 /* number of bytes of the indirect buffer which will be read */
993 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
996 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
997 "glMultiDrawArraysIndirect"))
1004 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
1005 GLenum mode
, GLenum type
,
1006 const GLvoid
*indirect
,
1007 GLsizei primcount
, GLsizei stride
)
1009 GLsizeiptr size
= 0;
1010 const unsigned drawElementsNumParams
= 5;
1012 FLUSH_CURRENT(ctx
, 0);
1014 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1015 assert(stride
!= 0);
1017 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
1018 "glMultiDrawElementsIndirect"))
1021 /* number of bytes of the indirect buffer which will be read */
1023 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1026 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1028 "glMultiDrawElementsIndirect"))