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_CORE
:
129 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
)
132 case API_OPENGL_COMPAT
:
134 const struct gl_shader_program
*vsProg
=
135 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
136 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
137 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
138 if (haveVertexShader
|| haveVertexProgram
) {
139 /* Draw regardless of whether or not we have any vertex arrays.
140 * (Ex: could draw a point using a constant vertex pos)
145 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
148 return (ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
149 ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
155 assert(!"Invalid API value in check_valid_to_render()");
163 * Do bounds checking on array element indexes. Check that the vertices
164 * pointed to by the indices don't lie outside buffer object bounds.
165 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
168 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
169 const GLvoid
*indices
, GLint basevertex
)
171 struct _mesa_prim prim
;
172 struct _mesa_index_buffer ib
;
175 /* Only the X Server needs to do this -- otherwise, accessing outside
176 * array/BO bounds allows application termination.
178 if (!ctx
->Const
.CheckArrayBounds
)
181 memset(&prim
, 0, sizeof(prim
));
184 memset(&ib
, 0, sizeof(ib
));
187 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
189 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
191 if ((int)(min
+ basevertex
) < 0 ||
192 max
+ basevertex
>= ctx
->Array
.VAO
->_MaxElement
) {
193 /* the max element is out of bounds of one or more enabled arrays */
194 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
195 max
, ctx
->Array
.VAO
->_MaxElement
);
204 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
205 * etc? The set of legal values depends on whether geometry shaders/programs
207 * Note: This may be called during display list compilation.
210 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
218 case GL_TRIANGLE_STRIP
:
219 case GL_TRIANGLE_FAN
:
224 return (ctx
->API
== API_OPENGL_COMPAT
);
225 case GL_LINES_ADJACENCY
:
226 case GL_LINE_STRIP_ADJACENCY
:
227 case GL_TRIANGLES_ADJACENCY
:
228 case GL_TRIANGLE_STRIP_ADJACENCY
:
229 return _mesa_has_geometry_shaders(ctx
);
237 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
238 * etc? Also, do additional checking related to transformation feedback.
239 * Note: this function cannot be called during glNewList(GL_COMPILE) because
240 * this code depends on current transform feedback state.
243 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
245 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
248 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
252 /* From the ARB_geometry_shader4 spec:
254 * The error INVALID_OPERATION is generated if Begin, or any command that
255 * implicitly calls Begin, is called when a geometry shader is active and:
257 * * the input primitive type of the current geometry shader is
258 * POINTS and <mode> is not POINTS,
260 * * the input primitive type of the current geometry shader is
261 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
263 * * the input primitive type of the current geometry shader is
264 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
267 * * the input primitive type of the current geometry shader is
268 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
269 * LINE_STRIP_ADJACENCY_ARB, or
271 * * the input primitive type of the current geometry shader is
272 * TRIANGLES_ADJACENCY_ARB and <mode> is not
273 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
276 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
277 const GLenum geom_mode
=
278 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.InputType
;
281 valid_enum
= (geom_mode
== GL_POINTS
);
286 valid_enum
= (geom_mode
== GL_LINES
);
289 case GL_TRIANGLE_STRIP
:
290 case GL_TRIANGLE_FAN
:
291 valid_enum
= (geom_mode
== GL_TRIANGLES
);
298 case GL_LINES_ADJACENCY
:
299 case GL_LINE_STRIP_ADJACENCY
:
300 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
302 case GL_TRIANGLES_ADJACENCY
:
303 case GL_TRIANGLE_STRIP_ADJACENCY
:
304 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
311 _mesa_error(ctx
, GL_INVALID_OPERATION
,
312 "%s(mode=%s vs geometry shader input %s)",
314 _mesa_lookup_prim_by_nr(mode
),
315 _mesa_lookup_prim_by_nr(geom_mode
));
320 /* From the GL_EXT_transform_feedback spec:
322 * "The error INVALID_OPERATION is generated if Begin, or any command
323 * that performs an explicit Begin, is called when:
325 * * a geometry shader is not active and <mode> does not match the
326 * allowed begin modes for the current transform feedback state as
327 * given by table X.1.
329 * * a geometry shader is active and the output primitive type of the
330 * geometry shader does not match the allowed begin modes for the
331 * current transform feedback state as given by table X.1.
334 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
335 GLboolean pass
= GL_TRUE
;
337 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
338 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Geom
.OutputType
) {
340 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
343 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
345 case GL_TRIANGLE_STRIP
:
346 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
355 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
360 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
363 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
368 _mesa_error(ctx
, GL_INVALID_OPERATION
,
369 "%s(mode=%s vs transform feedback %s)",
371 _mesa_lookup_prim_by_nr(mode
),
372 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
381 * Verify that the element type is valid.
383 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
386 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
389 case GL_UNSIGNED_BYTE
:
390 case GL_UNSIGNED_SHORT
:
391 case GL_UNSIGNED_INT
:
395 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
396 _mesa_lookup_enum_by_nr(type
));
402 * Error checking for glDrawElements(). Includes parameter checking
403 * and VBO bounds checking.
404 * \return GL_TRUE if OK to render, GL_FALSE if error found
407 _mesa_validate_DrawElements(struct gl_context
*ctx
,
408 GLenum mode
, GLsizei count
, GLenum type
,
409 const GLvoid
*indices
, GLint basevertex
)
411 FLUSH_CURRENT(ctx
, 0);
413 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
414 * Primitive Capture):
416 * The error INVALID_OPERATION is also generated by DrawElements,
417 * DrawElementsInstanced, and DrawRangeElements while transform feedback
418 * is active and not paused, regardless of mode.
420 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
421 _mesa_error(ctx
, GL_INVALID_OPERATION
,
422 "glDrawElements(transform feedback active)");
427 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
431 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
435 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
438 if (!check_valid_to_render(ctx
, "glDrawElements"))
441 /* Vertex buffer object tests */
442 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
443 /* use indices in the buffer object */
444 /* make sure count doesn't go outside buffer bounds */
445 if (index_bytes(type
, count
) > ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
446 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
451 /* not using a VBO */
456 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
467 * Error checking for glMultiDrawElements(). Includes parameter checking
468 * and VBO bounds checking.
469 * \return GL_TRUE if OK to render, GL_FALSE if error found
472 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
473 GLenum mode
, const GLsizei
*count
,
474 GLenum type
, const GLvoid
* const *indices
,
475 GLuint primcount
, const GLint
*basevertex
)
479 FLUSH_CURRENT(ctx
, 0);
481 for (i
= 0; i
< primcount
; i
++) {
483 _mesa_error(ctx
, GL_INVALID_VALUE
,
484 "glMultiDrawElements(count)" );
489 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
493 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
496 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
499 /* Vertex buffer object tests */
500 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
501 /* use indices in the buffer object */
502 /* make sure count doesn't go outside buffer bounds */
503 for (i
= 0; i
< primcount
; i
++) {
504 if (index_bytes(type
, count
[i
]) >
505 ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
507 "glMultiDrawElements index out of buffer bounds");
513 /* not using a VBO */
514 for (i
= 0; i
< primcount
; i
++) {
520 for (i
= 0; i
< primcount
; i
++) {
521 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
522 basevertex
? basevertex
[i
] : 0))
531 * Error checking for glDrawRangeElements(). Includes parameter checking
532 * and VBO bounds checking.
533 * \return GL_TRUE if OK to render, GL_FALSE if error found
536 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
537 GLuint start
, GLuint end
,
538 GLsizei count
, GLenum type
,
539 const GLvoid
*indices
, GLint basevertex
)
541 FLUSH_CURRENT(ctx
, 0);
543 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
544 * Primitive Capture):
546 * The error INVALID_OPERATION is also generated by DrawElements,
547 * DrawElementsInstanced, and DrawRangeElements while transform feedback
548 * is active and not paused, regardless of mode.
550 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
551 _mesa_error(ctx
, GL_INVALID_OPERATION
,
552 "glDrawElements(transform feedback active)");
557 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
561 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
566 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
570 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
573 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
576 /* Vertex buffer object tests */
577 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
578 /* use indices in the buffer object */
579 /* make sure count doesn't go outside buffer bounds */
580 if (index_bytes(type
, count
) > ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
581 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
586 /* not using a VBO */
591 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
602 * Called from the tnl module to error check the function parameters and
603 * verify that we really can draw something.
604 * \return GL_TRUE if OK to render, GL_FALSE if error found
607 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
608 GLenum mode
, GLint start
, GLsizei count
)
610 struct gl_transform_feedback_object
*xfb_obj
611 = ctx
->TransformFeedback
.CurrentObject
;
612 FLUSH_CURRENT(ctx
, 0);
615 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
619 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
623 if (!check_valid_to_render(ctx
, "glDrawArrays"))
626 if (ctx
->Const
.CheckArrayBounds
) {
627 if (start
+ count
> (GLint
) ctx
->Array
.VAO
->_MaxElement
)
631 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
632 * Primitive Capture):
634 * The error INVALID_OPERATION is generated by DrawArrays and
635 * DrawArraysInstanced if recording the vertices of a primitive to the
636 * buffer objects being used for transform feedback purposes would result
637 * in either exceeding the limits of any buffer object’s size, or in
638 * exceeding the end position offset + size − 1, as set by
641 * This is in contrast to the behaviour of desktop GL, where the extra
642 * primitives are silently dropped from the transform feedback buffer.
644 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
645 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
646 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
647 _mesa_error(ctx
, GL_INVALID_OPERATION
,
648 "glDrawArrays(exceeds transform feedback size)");
651 xfb_obj
->GlesRemainingPrims
-= prim_count
;
662 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
663 GLsizei count
, GLsizei numInstances
)
665 struct gl_transform_feedback_object
*xfb_obj
666 = ctx
->TransformFeedback
.CurrentObject
;
667 FLUSH_CURRENT(ctx
, 0);
670 _mesa_error(ctx
, GL_INVALID_VALUE
,
671 "glDrawArraysInstanced(count=%d)", count
);
676 _mesa_error(ctx
, GL_INVALID_VALUE
,
677 "glDrawArraysInstanced(start=%d)", first
);
681 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
685 if (numInstances
<= 0) {
686 if (numInstances
< 0)
687 _mesa_error(ctx
, GL_INVALID_VALUE
,
688 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
692 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
695 if (ctx
->Const
.CheckArrayBounds
) {
696 if (first
+ count
> (GLint
) ctx
->Array
.VAO
->_MaxElement
)
700 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
701 * Primitive Capture):
703 * The error INVALID_OPERATION is generated by DrawArrays and
704 * DrawArraysInstanced if recording the vertices of a primitive to the
705 * buffer objects being used for transform feedback purposes would result
706 * in either exceeding the limits of any buffer object’s size, or in
707 * exceeding the end position offset + size − 1, as set by
710 * This is in contrast to the behaviour of desktop GL, where the extra
711 * primitives are silently dropped from the transform feedback buffer.
713 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
715 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
716 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
717 _mesa_error(ctx
, GL_INVALID_OPERATION
,
718 "glDrawArraysInstanced(exceeds transform feedback size)");
721 xfb_obj
->GlesRemainingPrims
-= prim_count
;
732 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
733 GLenum mode
, GLsizei count
, GLenum type
,
734 const GLvoid
*indices
, GLsizei numInstances
,
737 FLUSH_CURRENT(ctx
, 0);
739 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
740 * Primitive Capture):
742 * The error INVALID_OPERATION is also generated by DrawElements,
743 * DrawElementsInstanced, and DrawRangeElements while transform feedback
744 * is active and not paused, regardless of mode.
746 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
747 _mesa_error(ctx
, GL_INVALID_OPERATION
,
748 "glDrawElements(transform feedback active)");
753 _mesa_error(ctx
, GL_INVALID_VALUE
,
754 "glDrawElementsInstanced(count=%d)", count
);
758 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
762 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
765 if (numInstances
<= 0) {
766 if (numInstances
< 0)
767 _mesa_error(ctx
, GL_INVALID_VALUE
,
768 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
772 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
775 /* Vertex buffer object tests */
776 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
777 /* use indices in the buffer object */
778 /* make sure count doesn't go outside buffer bounds */
779 if (index_bytes(type
, count
) > ctx
->Array
.VAO
->IndexBufferObj
->Size
) {
781 "glDrawElementsInstanced index out of buffer bounds");
786 /* not using a VBO */
794 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
802 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
804 struct gl_transform_feedback_object
*obj
,
806 GLsizei numInstances
)
808 FLUSH_CURRENT(ctx
, 0);
810 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
815 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
819 if (!obj
->EndedAnytime
) {
820 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
824 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
825 _mesa_error(ctx
, GL_INVALID_VALUE
,
826 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
830 if (numInstances
<= 0) {
831 if (numInstances
< 0)
832 _mesa_error(ctx
, GL_INVALID_VALUE
,
833 "glDrawTransformFeedback*Instanced(numInstances=%d)",
838 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
846 valid_draw_indirect(struct gl_context
*ctx
,
847 GLenum mode
, const GLvoid
*indirect
,
848 GLsizei size
, const char *name
)
850 const GLsizeiptr end
= (GLsizeiptr
)indirect
+ size
;
852 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
856 /* From the ARB_draw_indirect specification:
857 * "An INVALID_OPERATION error is generated [...] if <indirect> is no
860 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
862 "%s(indirect is not aligned)", name
);
866 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
867 _mesa_error(ctx
, GL_INVALID_OPERATION
,
868 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
872 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
873 _mesa_error(ctx
, GL_INVALID_OPERATION
,
874 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
878 /* From the ARB_draw_indirect specification:
879 * "An INVALID_OPERATION error is generated if the commands source data
880 * beyond the end of the buffer object [...]"
882 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
884 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
888 if (!check_valid_to_render(ctx
, name
))
894 static inline GLboolean
895 valid_draw_indirect_elements(struct gl_context
*ctx
,
896 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
897 GLsizeiptr size
, const char *name
)
899 if (!valid_elements_type(ctx
, type
, name
))
903 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
904 * may not come from a client array and must come from an index buffer.
905 * If no element array buffer is bound, an INVALID_OPERATION error is
908 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
909 _mesa_error(ctx
, GL_INVALID_OPERATION
,
910 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
914 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
917 static inline GLboolean
918 valid_draw_indirect_multi(struct gl_context
*ctx
,
919 GLsizei primcount
, GLsizei stride
,
923 /* From the ARB_multi_draw_indirect specification:
924 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
925 * MultiDrawElementsIndirect if <primcount> is negative."
927 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
931 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
936 /* From the ARB_multi_draw_indirect specification:
937 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
938 * error is generated."
941 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
949 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
951 const GLvoid
*indirect
)
953 const unsigned drawArraysNumParams
= 4;
955 FLUSH_CURRENT(ctx
, 0);
957 return valid_draw_indirect(ctx
, mode
,
958 indirect
, drawArraysNumParams
* sizeof(GLuint
),
959 "glDrawArraysIndirect");
963 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
964 GLenum mode
, GLenum type
,
965 const GLvoid
*indirect
)
967 const unsigned drawElementsNumParams
= 5;
969 FLUSH_CURRENT(ctx
, 0);
971 return valid_draw_indirect_elements(ctx
, mode
, type
,
972 indirect
, drawElementsNumParams
* sizeof(GLuint
),
973 "glDrawElementsIndirect");
977 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
979 const GLvoid
*indirect
,
980 GLsizei primcount
, GLsizei stride
)
983 const unsigned drawArraysNumParams
= 4;
985 FLUSH_CURRENT(ctx
, 0);
987 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
990 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
991 "glMultiDrawArraysIndirect"))
994 /* number of bytes of the indirect buffer which will be read */
996 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
999 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
1000 "glMultiDrawArraysIndirect"))
1007 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
1008 GLenum mode
, GLenum type
,
1009 const GLvoid
*indirect
,
1010 GLsizei primcount
, GLsizei stride
)
1012 GLsizeiptr size
= 0;
1013 const unsigned drawElementsNumParams
= 5;
1015 FLUSH_CURRENT(ctx
, 0);
1017 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1018 assert(stride
!= 0);
1020 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
1021 "glMultiDrawElementsIndirect"))
1024 /* number of bytes of the indirect buffer which will be read */
1026 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1029 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1031 "glMultiDrawElementsIndirect"))