2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "api_validate.h"
28 #include "bufferobj.h"
34 #include "transformfeedback.h"
39 * Check if OK to draw arrays/elements.
42 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
44 if (!_mesa_valid_to_render(ctx
, function
)) {
50 /* For ES2, we can draw if we have a vertex program/shader). */
51 return ctx
->VertexProgram
._Current
!= NULL
;
54 /* For OpenGL ES, only draw if we have vertex positions
56 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
61 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
62 * Core Profile spec says:
64 * "An INVALID_OPERATION error is generated if no vertex array
65 * object is bound (see section 10.3.1)."
67 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
68 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no VAO bound)", function
);
72 /* The spec argues that this is allowed because a tess ctrl shader
73 * without a tess eval shader can be used with transform feedback.
74 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
75 * therefore doesn't allow tessellation.
77 * Further investigation showed that this is indeed a spec bug and
78 * a tess ctrl shader without a tess eval shader shouldn't have been
79 * allowed, because there is no API in GL 4.0 that can make use this
80 * to produce something useful.
82 * Also, all vendors except one don't support a tess ctrl shader without
83 * a tess eval shader anyway.
85 if (ctx
->TessCtrlProgram
._Current
&& !ctx
->TessEvalProgram
._Current
) {
86 _mesa_error(ctx
, GL_INVALID_OPERATION
,
87 "%s(tess eval shader is missing)", function
);
91 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
94 * "If there is no active program for the vertex or fragment shader
95 * stages, the results of vertex and/or fragment processing will be
96 * undefined. However, this is not an error."
98 * The fragment shader is not tested here because other state (e.g.,
99 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
101 return ctx
->VertexProgram
._Current
!= NULL
;
103 case API_OPENGL_COMPAT
:
104 if (ctx
->VertexProgram
._Current
!= NULL
) {
105 /* Draw regardless of whether or not we have any vertex arrays.
106 * (Ex: could draw a point using a constant vertex pos)
110 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
113 return (ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
114 ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
119 unreachable("Invalid API value in check_valid_to_render()");
127 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
128 * etc? The set of legal values depends on whether geometry shaders/programs
130 * Note: This may be called during display list compilation.
133 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
135 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
136 * first and exit. You would think that a switch-statement would be the
137 * right approach, but at least GCC 4.7.2 generates some pretty dire code
138 * for the common case.
140 if (likely(mode
<= GL_TRIANGLE_FAN
))
143 if (mode
<= GL_POLYGON
)
144 return (ctx
->API
== API_OPENGL_COMPAT
);
146 if (mode
<= GL_TRIANGLE_STRIP_ADJACENCY
)
147 return _mesa_has_geometry_shaders(ctx
);
149 if (mode
== GL_PATCHES
)
150 return _mesa_has_tessellation(ctx
);
157 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
158 * etc? Also, do additional checking related to transformation feedback.
159 * Note: this function cannot be called during glNewList(GL_COMPILE) because
160 * this code depends on current transform feedback state.
161 * Also, do additional checking related to tessellation shaders.
164 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
166 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
169 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
173 /* From the OpenGL 4.5 specification, section 11.3.1:
175 * The error INVALID_OPERATION is generated if Begin, or any command that
176 * implicitly calls Begin, is called when a geometry shader is active and:
178 * * the input primitive type of the current geometry shader is
179 * POINTS and <mode> is not POINTS,
181 * * the input primitive type of the current geometry shader is
182 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
184 * * the input primitive type of the current geometry shader is
185 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
188 * * the input primitive type of the current geometry shader is
189 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
190 * LINE_STRIP_ADJACENCY_ARB, or
192 * * the input primitive type of the current geometry shader is
193 * TRIANGLES_ADJACENCY_ARB and <mode> is not
194 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
196 * The GL spec doesn't mention any interaction with tessellation, which
197 * is clearly a spec bug. The same rule should apply, but instead of
198 * the draw primitive mode, the tessellation evaluation shader primitive
199 * mode should be used for the checking.
201 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
202 const GLenum geom_mode
=
203 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
204 _LinkedShaders
[MESA_SHADER_GEOMETRY
]->Geom
.InputType
;
205 struct gl_shader_program
*tes
=
206 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
207 GLenum mode_before_gs
= mode
;
210 struct gl_shader
*tes_sh
= tes
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
211 if (tes_sh
->TessEval
.PointMode
)
212 mode_before_gs
= GL_POINTS
;
213 else if (tes_sh
->TessEval
.PrimitiveMode
== GL_ISOLINES
)
214 mode_before_gs
= GL_LINES
;
216 /* the GL_QUADS mode generates triangles too */
217 mode_before_gs
= GL_TRIANGLES
;
220 switch (mode_before_gs
) {
222 valid_enum
= (geom_mode
== GL_POINTS
);
227 valid_enum
= (geom_mode
== GL_LINES
);
230 case GL_TRIANGLE_STRIP
:
231 case GL_TRIANGLE_FAN
:
232 valid_enum
= (geom_mode
== GL_TRIANGLES
);
239 case GL_LINES_ADJACENCY
:
240 case GL_LINE_STRIP_ADJACENCY
:
241 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
243 case GL_TRIANGLES_ADJACENCY
:
244 case GL_TRIANGLE_STRIP_ADJACENCY
:
245 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
252 _mesa_error(ctx
, GL_INVALID_OPERATION
,
253 "%s(mode=%s vs geometry shader input %s)",
255 _mesa_lookup_prim_by_nr(mode_before_gs
),
256 _mesa_lookup_prim_by_nr(geom_mode
));
261 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
263 * "Tessellation operates only on patch primitives. If tessellation is
264 * active, any command that transfers vertices to the GL will
265 * generate an INVALID_OPERATION error if the primitive mode is not
267 * Patch primitives are not supported by pipeline stages below the
268 * tessellation evaluation shader. If there is no active program
269 * object or the active program object does not contain a tessellation
270 * evaluation shader, the error INVALID_OPERATION is generated by any
271 * command that transfers vertices to the GL if the primitive mode is
275 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
] ||
276 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]) {
277 if (mode
!= GL_PATCHES
) {
278 _mesa_error(ctx
, GL_INVALID_OPERATION
,
279 "only GL_PATCHES valid with tessellation");
284 if (mode
== GL_PATCHES
) {
285 _mesa_error(ctx
, GL_INVALID_OPERATION
,
286 "GL_PATCHES only valid with tessellation");
291 /* From the GL_EXT_transform_feedback spec:
293 * "The error INVALID_OPERATION is generated if Begin, or any command
294 * that performs an explicit Begin, is called when:
296 * * a geometry shader is not active and <mode> does not match the
297 * allowed begin modes for the current transform feedback state as
298 * given by table X.1.
300 * * a geometry shader is active and the output primitive type of the
301 * geometry shader does not match the allowed begin modes for the
302 * current transform feedback state as given by table X.1.
305 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
306 GLboolean pass
= GL_TRUE
;
308 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
309 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
310 _LinkedShaders
[MESA_SHADER_GEOMETRY
]->Geom
.OutputType
) {
312 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
315 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
317 case GL_TRIANGLE_STRIP
:
318 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
324 else if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]) {
325 struct gl_shader_program
*tes
=
326 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
327 struct gl_shader
*tes_sh
= tes
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
328 if (tes_sh
->TessEval
.PointMode
)
329 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
330 else if (tes_sh
->TessEval
.PrimitiveMode
== GL_ISOLINES
)
331 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
333 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
338 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
343 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
346 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "%s(mode=%s vs transform feedback %s)",
354 _mesa_lookup_prim_by_nr(mode
),
355 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
364 * Verify that the element type is valid.
366 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
369 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
372 case GL_UNSIGNED_BYTE
:
373 case GL_UNSIGNED_SHORT
:
374 case GL_UNSIGNED_INT
:
378 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
379 _mesa_enum_to_string(type
));
385 validate_DrawElements_common(struct gl_context
*ctx
,
386 GLenum mode
, GLsizei count
, GLenum type
,
387 const GLvoid
*indices
,
390 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
391 * Primitive Capture):
393 * The error INVALID_OPERATION is also generated by DrawElements,
394 * DrawElementsInstanced, and DrawRangeElements while transform feedback
395 * is active and not paused, regardless of mode.
397 if (_mesa_is_gles3(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
398 _mesa_is_xfb_active_and_unpaused(ctx
)) {
399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
400 "%s(transform feedback active)", caller
);
405 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
409 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
413 if (!valid_elements_type(ctx
, type
, caller
))
416 if (!check_valid_to_render(ctx
, caller
))
419 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
421 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
431 * Error checking for glDrawElements(). Includes parameter checking
432 * and VBO bounds checking.
433 * \return GL_TRUE if OK to render, GL_FALSE if error found
436 _mesa_validate_DrawElements(struct gl_context
*ctx
,
437 GLenum mode
, GLsizei count
, GLenum type
,
438 const GLvoid
*indices
)
440 FLUSH_CURRENT(ctx
, 0);
442 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
448 * Error checking for glMultiDrawElements(). Includes parameter checking
449 * and VBO bounds checking.
450 * \return GL_TRUE if OK to render, GL_FALSE if error found
453 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
454 GLenum mode
, const GLsizei
*count
,
455 GLenum type
, const GLvoid
* const *indices
,
460 FLUSH_CURRENT(ctx
, 0);
462 for (i
= 0; i
< primcount
; i
++) {
464 _mesa_error(ctx
, GL_INVALID_VALUE
,
465 "glMultiDrawElements(count)" );
470 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
474 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
477 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
480 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
482 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
483 for (i
= 0; i
< primcount
; i
++) {
494 * Error checking for glDrawRangeElements(). Includes parameter checking
495 * and VBO bounds checking.
496 * \return GL_TRUE if OK to render, GL_FALSE if error found
499 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
500 GLuint start
, GLuint end
,
501 GLsizei count
, GLenum type
,
502 const GLvoid
*indices
)
504 FLUSH_CURRENT(ctx
, 0);
507 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
511 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
512 "glDrawRangeElements");
517 * Called from the tnl module to error check the function parameters and
518 * verify that we really can draw something.
519 * \return GL_TRUE if OK to render, GL_FALSE if error found
522 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
524 struct gl_transform_feedback_object
*xfb_obj
525 = ctx
->TransformFeedback
.CurrentObject
;
526 FLUSH_CURRENT(ctx
, 0);
529 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
533 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
537 if (!check_valid_to_render(ctx
, "glDrawArrays"))
540 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
541 * Primitive Capture):
543 * The error INVALID_OPERATION is generated by DrawArrays and
544 * DrawArraysInstanced if recording the vertices of a primitive to the
545 * buffer objects being used for transform feedback purposes would result
546 * in either exceeding the limits of any buffer object’s size, or in
547 * exceeding the end position offset + size − 1, as set by
550 * This is in contrast to the behaviour of desktop GL, where the extra
551 * primitives are silently dropped from the transform feedback buffer.
553 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
554 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
555 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
556 _mesa_error(ctx
, GL_INVALID_OPERATION
,
557 "glDrawArrays(exceeds transform feedback size)");
560 xfb_obj
->GlesRemainingPrims
-= prim_count
;
571 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
572 GLsizei count
, GLsizei numInstances
)
574 struct gl_transform_feedback_object
*xfb_obj
575 = ctx
->TransformFeedback
.CurrentObject
;
576 FLUSH_CURRENT(ctx
, 0);
579 _mesa_error(ctx
, GL_INVALID_VALUE
,
580 "glDrawArraysInstanced(count=%d)", count
);
585 _mesa_error(ctx
, GL_INVALID_VALUE
,
586 "glDrawArraysInstanced(start=%d)", first
);
590 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
594 if (numInstances
<= 0) {
595 if (numInstances
< 0)
596 _mesa_error(ctx
, GL_INVALID_VALUE
,
597 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
601 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
604 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
605 * Primitive Capture):
607 * The error INVALID_OPERATION is generated by DrawArrays and
608 * DrawArraysInstanced if recording the vertices of a primitive to the
609 * buffer objects being used for transform feedback purposes would result
610 * in either exceeding the limits of any buffer object’s size, or in
611 * exceeding the end position offset + size − 1, as set by
614 * This is in contrast to the behaviour of desktop GL, where the extra
615 * primitives are silently dropped from the transform feedback buffer.
617 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
619 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
620 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
621 _mesa_error(ctx
, GL_INVALID_OPERATION
,
622 "glDrawArraysInstanced(exceeds transform feedback size)");
625 xfb_obj
->GlesRemainingPrims
-= prim_count
;
636 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
637 GLenum mode
, GLsizei count
, GLenum type
,
638 const GLvoid
*indices
, GLsizei numInstances
)
640 FLUSH_CURRENT(ctx
, 0);
642 if (numInstances
< 0) {
643 _mesa_error(ctx
, GL_INVALID_VALUE
,
644 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
648 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
649 "glDrawElementsInstanced")
650 && (numInstances
> 0);
655 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
657 struct gl_transform_feedback_object
*obj
,
659 GLsizei numInstances
)
661 FLUSH_CURRENT(ctx
, 0);
663 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
668 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
672 /* From the GL 4.5 specification, page 429:
673 * "An INVALID_VALUE error is generated if id is not the name of a
674 * transform feedback object."
676 if (!obj
->EverBound
) {
677 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
681 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
682 _mesa_error(ctx
, GL_INVALID_VALUE
,
683 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
687 if (!obj
->EndedAnytime
) {
688 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
692 if (numInstances
<= 0) {
693 if (numInstances
< 0)
694 _mesa_error(ctx
, GL_INVALID_VALUE
,
695 "glDrawTransformFeedback*Instanced(numInstances=%d)",
700 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
708 valid_draw_indirect(struct gl_context
*ctx
,
709 GLenum mode
, const GLvoid
*indirect
,
710 GLsizei size
, const char *name
)
712 const uint64_t end
= (uint64_t) (uintptr_t) indirect
+ size
;
714 /* OpenGL ES 3.1 spec. section 10.5:
716 * "DrawArraysIndirect requires that all data sourced for the
717 * command, including the DrawArraysIndirectCommand
718 * structure, be in buffer objects, and may not be called when
719 * the default vertex array object is bound."
721 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
722 _mesa_error(ctx
, GL_INVALID_OPERATION
, "(no VAO bound)");
726 /* From OpenGL ES 3.1 spec. section 10.5:
727 * "An INVALID_OPERATION error is generated if zero is bound to
728 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
731 * Here we check that for each enabled vertex array we have a vertex
734 if (_mesa_is_gles31(ctx
) &&
735 ctx
->Array
.VAO
->_Enabled
!= ctx
->Array
.VAO
->VertexAttribBufferMask
) {
736 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(No VBO bound)", name
);
740 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
743 /* OpenGL ES 3.1 specification, section 10.5:
745 * "An INVALID_OPERATION error is generated if
746 * transform feedback is active and not paused."
748 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
749 _mesa_is_xfb_active_and_unpaused(ctx
)) {
750 _mesa_error(ctx
, GL_INVALID_OPERATION
,
751 "%s(TransformFeedback is active and not paused)", name
);
754 /* From OpenGL version 4.4. section 10.5
755 * and OpenGL ES 3.1, section 10.6:
757 * "An INVALID_VALUE error is generated if indirect is not a
758 * multiple of the size, in basic machine units, of uint."
760 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
761 _mesa_error(ctx
, GL_INVALID_VALUE
,
762 "%s(indirect is not aligned)", name
);
766 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
767 _mesa_error(ctx
, GL_INVALID_OPERATION
,
768 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
772 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
774 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
778 /* From the ARB_draw_indirect specification:
779 * "An INVALID_OPERATION error is generated if the commands source data
780 * beyond the end of the buffer object [...]"
782 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
783 _mesa_error(ctx
, GL_INVALID_OPERATION
,
784 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
788 if (!check_valid_to_render(ctx
, name
))
794 static inline GLboolean
795 valid_draw_indirect_elements(struct gl_context
*ctx
,
796 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
797 GLsizeiptr size
, const char *name
)
799 if (!valid_elements_type(ctx
, type
, name
))
803 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
804 * may not come from a client array and must come from an index buffer.
805 * If no element array buffer is bound, an INVALID_OPERATION error is
808 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
809 _mesa_error(ctx
, GL_INVALID_OPERATION
,
810 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
814 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
817 static inline GLboolean
818 valid_draw_indirect_multi(struct gl_context
*ctx
,
819 GLsizei primcount
, GLsizei stride
,
823 /* From the ARB_multi_draw_indirect specification:
824 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
825 * MultiDrawElementsIndirect if <primcount> is negative."
827 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
831 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
836 /* From the ARB_multi_draw_indirect specification:
837 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
838 * error is generated."
841 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
849 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
851 const GLvoid
*indirect
)
853 const unsigned drawArraysNumParams
= 4;
855 FLUSH_CURRENT(ctx
, 0);
857 return valid_draw_indirect(ctx
, mode
,
858 indirect
, drawArraysNumParams
* sizeof(GLuint
),
859 "glDrawArraysIndirect");
863 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
864 GLenum mode
, GLenum type
,
865 const GLvoid
*indirect
)
867 const unsigned drawElementsNumParams
= 5;
869 FLUSH_CURRENT(ctx
, 0);
871 return valid_draw_indirect_elements(ctx
, mode
, type
,
872 indirect
, drawElementsNumParams
* sizeof(GLuint
),
873 "glDrawElementsIndirect");
877 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
879 const GLvoid
*indirect
,
880 GLsizei primcount
, GLsizei stride
)
883 const unsigned drawArraysNumParams
= 4;
885 FLUSH_CURRENT(ctx
, 0);
887 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
890 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
891 "glMultiDrawArraysIndirect"))
894 /* number of bytes of the indirect buffer which will be read */
896 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
899 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
900 "glMultiDrawArraysIndirect"))
907 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
908 GLenum mode
, GLenum type
,
909 const GLvoid
*indirect
,
910 GLsizei primcount
, GLsizei stride
)
913 const unsigned drawElementsNumParams
= 5;
915 FLUSH_CURRENT(ctx
, 0);
917 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
920 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
921 "glMultiDrawElementsIndirect"))
924 /* number of bytes of the indirect buffer which will be read */
926 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
929 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
931 "glMultiDrawElementsIndirect"))
938 valid_draw_indirect_parameters(struct gl_context
*ctx
,
942 /* From the ARB_indirect_parameters specification:
943 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
944 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
948 _mesa_error(ctx
, GL_INVALID_VALUE
,
949 "%s(drawcount is not a multiple of 4)", name
);
953 /* From the ARB_indirect_parameters specification:
954 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
955 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
956 * PARAMETER_BUFFER_ARB binding point."
958 if (!_mesa_is_bufferobj(ctx
->ParameterBuffer
)) {
959 _mesa_error(ctx
, GL_INVALID_OPERATION
,
960 "%s: no buffer bound to PARAMETER_BUFFER", name
);
964 if (_mesa_check_disallowed_mapping(ctx
->ParameterBuffer
)) {
965 _mesa_error(ctx
, GL_INVALID_OPERATION
,
966 "%s(PARAMETER_BUFFER is mapped)", name
);
970 /* From the ARB_indirect_parameters specification:
971 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
972 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
973 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
974 * specified by <drawcount> would result in an out-of-bounds access."
976 if (ctx
->ParameterBuffer
->Size
< drawcount
+ sizeof(GLsizei
)) {
977 _mesa_error(ctx
, GL_INVALID_OPERATION
,
978 "%s(PARAMETER_BUFFER too small)", name
);
986 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context
*ctx
,
990 GLsizei maxdrawcount
,
994 const unsigned drawArraysNumParams
= 4;
996 FLUSH_CURRENT(ctx
, 0);
998 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1001 if (!valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1002 "glMultiDrawArraysIndirectCountARB"))
1005 /* number of bytes of the indirect buffer which will be read */
1007 ? (maxdrawcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
1010 if (!valid_draw_indirect(ctx
, mode
, (void *)indirect
, size
,
1011 "glMultiDrawArraysIndirectCountARB"))
1014 return valid_draw_indirect_parameters(
1015 ctx
, "glMultiDrawArraysIndirectCountARB", drawcount
);
1019 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context
*ctx
,
1020 GLenum mode
, GLenum type
,
1023 GLsizei maxdrawcount
,
1026 GLsizeiptr size
= 0;
1027 const unsigned drawElementsNumParams
= 5;
1029 FLUSH_CURRENT(ctx
, 0);
1031 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1032 assert(stride
!= 0);
1034 if (!valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1035 "glMultiDrawElementsIndirectCountARB"))
1038 /* number of bytes of the indirect buffer which will be read */
1040 ? (maxdrawcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1043 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1044 (void *)indirect
, size
,
1045 "glMultiDrawElementsIndirectCountARB"))
1048 return valid_draw_indirect_parameters(
1049 ctx
, "glMultiDrawElementsIndirectCountARB", drawcount
);
1053 check_valid_to_compute(struct gl_context
*ctx
, const char *function
)
1055 struct gl_shader_program
*prog
;
1057 if (!_mesa_has_compute_shaders(ctx
)) {
1058 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1059 "unsupported function (%s) called",
1064 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1066 * "An INVALID_OPERATION error is generated if there is no active program
1067 * for the compute shader stage."
1069 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
1070 if (prog
== NULL
|| prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
1071 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1072 "%s(no active compute shader)",
1081 _mesa_validate_DispatchCompute(struct gl_context
*ctx
,
1082 const GLuint
*num_groups
)
1085 FLUSH_CURRENT(ctx
, 0);
1087 if (!check_valid_to_compute(ctx
, "glDispatchCompute"))
1090 for (i
= 0; i
< 3; i
++) {
1091 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1093 * "An INVALID_VALUE error is generated if any of num_groups_x,
1094 * num_groups_y and num_groups_z are greater than or equal to the
1095 * maximum work group count for the corresponding dimension."
1097 * However, the "or equal to" portions appears to be a specification
1098 * bug. In all other areas, the specification appears to indicate that
1099 * the number of workgroups can match the MAX_COMPUTE_WORK_GROUP_COUNT
1100 * value. For example, under DispatchComputeIndirect:
1102 * "If any of num_groups_x, num_groups_y or num_groups_z is greater than
1103 * the value of MAX_COMPUTE_WORK_GROUP_COUNT for the corresponding
1104 * dimension then the results are undefined."
1106 * Additionally, the OpenGLES 3.1 specification does not contain "or
1107 * equal to" as an error condition.
1109 if (num_groups
[i
] > ctx
->Const
.MaxComputeWorkGroupCount
[i
]) {
1110 _mesa_error(ctx
, GL_INVALID_VALUE
,
1111 "glDispatchCompute(num_groups_%c)", 'x' + i
);
1120 valid_dispatch_indirect(struct gl_context
*ctx
,
1122 GLsizei size
, const char *name
)
1124 const uint64_t end
= (uint64_t) indirect
+ size
;
1126 if (!check_valid_to_compute(ctx
, name
))
1129 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1131 * "An INVALID_VALUE error is generated if indirect is negative or is not a
1132 * multiple of four."
1134 if (indirect
& (sizeof(GLuint
) - 1)) {
1135 _mesa_error(ctx
, GL_INVALID_VALUE
,
1136 "%s(indirect is not aligned)", name
);
1141 _mesa_error(ctx
, GL_INVALID_VALUE
,
1142 "%s(indirect is less than zero)", name
);
1146 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1148 * "An INVALID_OPERATION error is generated if no buffer is bound to the
1149 * DRAW_INDIRECT_BUFFER binding, or if the command would source data
1150 * beyond the end of the buffer object."
1152 if (!_mesa_is_bufferobj(ctx
->DispatchIndirectBuffer
)) {
1153 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1154 "%s: no buffer bound to DISPATCH_INDIRECT_BUFFER", name
);
1158 if (_mesa_check_disallowed_mapping(ctx
->DispatchIndirectBuffer
)) {
1159 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1160 "%s(DISPATCH_INDIRECT_BUFFER is mapped)", name
);
1164 if (ctx
->DispatchIndirectBuffer
->Size
< end
) {
1165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1166 "%s(DISPATCH_INDIRECT_BUFFER too small)", name
);
1174 _mesa_validate_DispatchComputeIndirect(struct gl_context
*ctx
,
1177 FLUSH_CURRENT(ctx
, 0);
1179 return valid_dispatch_indirect(ctx
, indirect
, 3 * sizeof(GLuint
),
1180 "glDispatchComputeIndirect");