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"
29 #include "bufferobj.h"
35 #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()");
122 if (!_mesa_all_buffers_are_unmapped(ctx
->Array
.VAO
)) {
123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
124 "%s(vertex buffers are mapped)", function
);
133 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
134 * etc? The set of legal values depends on whether geometry shaders/programs
136 * Note: This may be called during display list compilation.
139 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
141 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
142 * first and exit. You would think that a switch-statement would be the
143 * right approach, but at least GCC 4.7.2 generates some pretty dire code
144 * for the common case.
146 if (likely(mode
<= GL_TRIANGLE_FAN
))
149 if (mode
<= GL_POLYGON
)
150 return (ctx
->API
== API_OPENGL_COMPAT
);
152 if (mode
<= GL_TRIANGLE_STRIP_ADJACENCY
)
153 return _mesa_has_geometry_shaders(ctx
);
155 if (mode
== GL_PATCHES
)
156 return _mesa_has_tessellation(ctx
);
163 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
164 * etc? Also, do additional checking related to transformation feedback.
165 * Note: this function cannot be called during glNewList(GL_COMPILE) because
166 * this code depends on current transform feedback state.
167 * Also, do additional checking related to tessellation shaders.
170 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
172 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
175 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
179 /* From the OpenGL 4.5 specification, section 11.3.1:
181 * The error INVALID_OPERATION is generated if Begin, or any command that
182 * implicitly calls Begin, is called when a geometry shader is active and:
184 * * the input primitive type of the current geometry shader is
185 * POINTS and <mode> is not POINTS,
187 * * the input primitive type of the current geometry shader is
188 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
190 * * the input primitive type of the current geometry shader is
191 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
194 * * the input primitive type of the current geometry shader is
195 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
196 * LINE_STRIP_ADJACENCY_ARB, or
198 * * the input primitive type of the current geometry shader is
199 * TRIANGLES_ADJACENCY_ARB and <mode> is not
200 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
202 * The GL spec doesn't mention any interaction with tessellation, which
203 * is clearly a spec bug. The same rule should apply, but instead of
204 * the draw primitive mode, the tessellation evaluation shader primitive
205 * mode should be used for the checking.
207 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
208 const GLenum geom_mode
=
209 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
210 _LinkedShaders
[MESA_SHADER_GEOMETRY
]->info
.Geom
.InputType
;
211 struct gl_shader_program
*tes
=
212 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
213 GLenum mode_before_gs
= mode
;
216 struct gl_linked_shader
*tes_sh
=
217 tes
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
218 if (tes_sh
->info
.TessEval
.PointMode
)
219 mode_before_gs
= GL_POINTS
;
220 else if (tes_sh
->info
.TessEval
.PrimitiveMode
== GL_ISOLINES
)
221 mode_before_gs
= GL_LINES
;
223 /* the GL_QUADS mode generates triangles too */
224 mode_before_gs
= GL_TRIANGLES
;
227 switch (mode_before_gs
) {
229 valid_enum
= (geom_mode
== GL_POINTS
);
234 valid_enum
= (geom_mode
== GL_LINES
);
237 case GL_TRIANGLE_STRIP
:
238 case GL_TRIANGLE_FAN
:
239 valid_enum
= (geom_mode
== GL_TRIANGLES
);
246 case GL_LINES_ADJACENCY
:
247 case GL_LINE_STRIP_ADJACENCY
:
248 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
250 case GL_TRIANGLES_ADJACENCY
:
251 case GL_TRIANGLE_STRIP_ADJACENCY
:
252 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
259 _mesa_error(ctx
, GL_INVALID_OPERATION
,
260 "%s(mode=%s vs geometry shader input %s)",
262 _mesa_lookup_prim_by_nr(mode_before_gs
),
263 _mesa_lookup_prim_by_nr(geom_mode
));
268 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
270 * "Tessellation operates only on patch primitives. If tessellation is
271 * active, any command that transfers vertices to the GL will
272 * generate an INVALID_OPERATION error if the primitive mode is not
274 * Patch primitives are not supported by pipeline stages below the
275 * tessellation evaluation shader. If there is no active program
276 * object or the active program object does not contain a tessellation
277 * evaluation shader, the error INVALID_OPERATION is generated by any
278 * command that transfers vertices to the GL if the primitive mode is
282 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
] ||
283 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]) {
284 if (mode
!= GL_PATCHES
) {
285 _mesa_error(ctx
, GL_INVALID_OPERATION
,
286 "only GL_PATCHES valid with tessellation");
291 if (mode
== GL_PATCHES
) {
292 _mesa_error(ctx
, GL_INVALID_OPERATION
,
293 "GL_PATCHES only valid with tessellation");
298 /* From the GL_EXT_transform_feedback spec:
300 * "The error INVALID_OPERATION is generated if Begin, or any command
301 * that performs an explicit Begin, is called when:
303 * * a geometry shader is not active and <mode> does not match the
304 * allowed begin modes for the current transform feedback state as
305 * given by table X.1.
307 * * a geometry shader is active and the output primitive type of the
308 * geometry shader does not match the allowed begin modes for the
309 * current transform feedback state as given by table X.1.
312 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
313 GLboolean pass
= GL_TRUE
;
315 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
316 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
317 _LinkedShaders
[MESA_SHADER_GEOMETRY
]->
318 info
.Geom
.OutputType
) {
320 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
323 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
325 case GL_TRIANGLE_STRIP
:
326 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
332 else if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]) {
333 struct gl_shader_program
*tes
=
334 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
335 struct gl_linked_shader
*tes_sh
=
336 tes
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
337 if (tes_sh
->info
.TessEval
.PointMode
)
338 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
339 else if (tes_sh
->info
.TessEval
.PrimitiveMode
== GL_ISOLINES
)
340 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
342 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
347 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
352 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
355 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
361 "%s(mode=%s vs transform feedback %s)",
363 _mesa_lookup_prim_by_nr(mode
),
364 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
373 * Verify that the element type is valid.
375 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
378 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
381 case GL_UNSIGNED_BYTE
:
382 case GL_UNSIGNED_SHORT
:
383 case GL_UNSIGNED_INT
:
387 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
388 _mesa_enum_to_string(type
));
394 validate_DrawElements_common(struct gl_context
*ctx
,
395 GLenum mode
, GLsizei count
, GLenum type
,
396 const GLvoid
*indices
,
399 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
400 * Primitive Capture):
402 * The error INVALID_OPERATION is also generated by DrawElements,
403 * DrawElementsInstanced, and DrawRangeElements while transform feedback
404 * is active and not paused, regardless of mode.
406 if (_mesa_is_gles3(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
407 _mesa_is_xfb_active_and_unpaused(ctx
)) {
408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
409 "%s(transform feedback active)", caller
);
414 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
418 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
422 if (!valid_elements_type(ctx
, type
, caller
))
425 if (!check_valid_to_render(ctx
, caller
))
428 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
430 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
440 * Error checking for glDrawElements(). Includes parameter checking
441 * and VBO bounds checking.
442 * \return GL_TRUE if OK to render, GL_FALSE if error found
445 _mesa_validate_DrawElements(struct gl_context
*ctx
,
446 GLenum mode
, GLsizei count
, GLenum type
,
447 const GLvoid
*indices
)
449 FLUSH_CURRENT(ctx
, 0);
451 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
457 * Error checking for glMultiDrawElements(). Includes parameter checking
458 * and VBO bounds checking.
459 * \return GL_TRUE if OK to render, GL_FALSE if error found
462 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
463 GLenum mode
, const GLsizei
*count
,
464 GLenum type
, const GLvoid
* const *indices
,
469 FLUSH_CURRENT(ctx
, 0);
471 for (i
= 0; i
< primcount
; i
++) {
473 _mesa_error(ctx
, GL_INVALID_VALUE
,
474 "glMultiDrawElements(count)" );
479 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
483 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
486 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
489 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
491 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
492 for (i
= 0; i
< primcount
; i
++) {
503 * Error checking for glDrawRangeElements(). Includes parameter checking
504 * and VBO bounds checking.
505 * \return GL_TRUE if OK to render, GL_FALSE if error found
508 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
509 GLuint start
, GLuint end
,
510 GLsizei count
, GLenum type
,
511 const GLvoid
*indices
)
513 FLUSH_CURRENT(ctx
, 0);
516 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
520 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
521 "glDrawRangeElements");
525 validate_draw_arrays(struct gl_context
*ctx
, const char *func
,
526 GLenum mode
, GLsizei count
, GLsizei numInstances
)
528 struct gl_transform_feedback_object
*xfb_obj
529 = ctx
->TransformFeedback
.CurrentObject
;
530 FLUSH_CURRENT(ctx
, 0);
533 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", func
);
537 if (!_mesa_valid_prim_mode(ctx
, mode
, func
))
540 if (!check_valid_to_render(ctx
, func
))
543 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
544 * Primitive Capture):
546 * The error INVALID_OPERATION is generated by DrawArrays and
547 * DrawArraysInstanced if recording the vertices of a primitive to the
548 * buffer objects being used for transform feedback purposes would result
549 * in either exceeding the limits of any buffer object’s size, or in
550 * exceeding the end position offset + size − 1, as set by
553 * This is in contrast to the behaviour of desktop GL, where the extra
554 * primitives are silently dropped from the transform feedback buffer.
556 * This text is removed in ES 3.2, presumably because it's not really
557 * implementable with geometry and tessellation shaders. In fact,
558 * the OES_geometry_shader spec says:
560 * "(13) Does this extension change how transform feedback operates
561 * compared to unextended OpenGL ES 3.0 or 3.1?
563 * RESOLVED: Yes. Because dynamic geometry amplification in a geometry
564 * shader can make it difficult if not impossible to predict the amount
565 * of geometry that may be generated in advance of executing the shader,
566 * the draw-time error for transform feedback buffer overflow conditions
567 * is removed and replaced with the GL behavior (primitives are not
568 * written and the corresponding counter is not updated)..."
570 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
) &&
571 !_mesa_has_OES_geometry_shader(ctx
) &&
572 !_mesa_has_OES_tessellation_shader(ctx
)) {
573 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
574 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
575 _mesa_error(ctx
, GL_INVALID_OPERATION
,
576 "%s(exceeds transform feedback size)", func
);
579 xfb_obj
->GlesRemainingPrims
-= prim_count
;
589 * Called from the tnl module to error check the function parameters and
590 * verify that we really can draw something.
591 * \return GL_TRUE if OK to render, GL_FALSE if error found
594 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
596 return validate_draw_arrays(ctx
, "glDrawArrays", mode
, count
, 1);
601 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
602 GLsizei count
, GLsizei numInstances
)
605 _mesa_error(ctx
, GL_INVALID_VALUE
,
606 "glDrawArraysInstanced(start=%d)", first
);
610 if (numInstances
<= 0) {
611 if (numInstances
< 0)
612 _mesa_error(ctx
, GL_INVALID_VALUE
,
613 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
617 return validate_draw_arrays(ctx
, "glDrawArraysInstanced", mode
, count
, 1);
622 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
623 GLenum mode
, GLsizei count
, GLenum type
,
624 const GLvoid
*indices
, GLsizei numInstances
)
626 FLUSH_CURRENT(ctx
, 0);
628 if (numInstances
< 0) {
629 _mesa_error(ctx
, GL_INVALID_VALUE
,
630 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
634 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
635 "glDrawElementsInstanced")
636 && (numInstances
> 0);
641 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
643 struct gl_transform_feedback_object
*obj
,
645 GLsizei numInstances
)
647 FLUSH_CURRENT(ctx
, 0);
649 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
654 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
658 /* From the GL 4.5 specification, page 429:
659 * "An INVALID_VALUE error is generated if id is not the name of a
660 * transform feedback object."
662 if (!obj
->EverBound
) {
663 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
667 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
668 _mesa_error(ctx
, GL_INVALID_VALUE
,
669 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
673 if (!obj
->EndedAnytime
) {
674 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
678 if (numInstances
<= 0) {
679 if (numInstances
< 0)
680 _mesa_error(ctx
, GL_INVALID_VALUE
,
681 "glDrawTransformFeedback*Instanced(numInstances=%d)",
686 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
694 valid_draw_indirect(struct gl_context
*ctx
,
695 GLenum mode
, const GLvoid
*indirect
,
696 GLsizei size
, const char *name
)
698 const uint64_t end
= (uint64_t) (uintptr_t) indirect
+ size
;
700 /* OpenGL ES 3.1 spec. section 10.5:
702 * "DrawArraysIndirect requires that all data sourced for the
703 * command, including the DrawArraysIndirectCommand
704 * structure, be in buffer objects, and may not be called when
705 * the default vertex array object is bound."
707 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
708 _mesa_error(ctx
, GL_INVALID_OPERATION
, "(no VAO bound)");
712 /* From OpenGL ES 3.1 spec. section 10.5:
713 * "An INVALID_OPERATION error is generated if zero is bound to
714 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
717 * Here we check that for each enabled vertex array we have a vertex
720 if (_mesa_is_gles31(ctx
) &&
721 ctx
->Array
.VAO
->_Enabled
!= ctx
->Array
.VAO
->VertexAttribBufferMask
) {
722 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(No VBO bound)", name
);
726 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
729 /* OpenGL ES 3.1 specification, section 10.5:
731 * "An INVALID_OPERATION error is generated if
732 * transform feedback is active and not paused."
734 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
735 _mesa_is_xfb_active_and_unpaused(ctx
)) {
736 _mesa_error(ctx
, GL_INVALID_OPERATION
,
737 "%s(TransformFeedback is active and not paused)", name
);
740 /* From OpenGL version 4.4. section 10.5
741 * and OpenGL ES 3.1, section 10.6:
743 * "An INVALID_VALUE error is generated if indirect is not a
744 * multiple of the size, in basic machine units, of uint."
746 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
747 _mesa_error(ctx
, GL_INVALID_VALUE
,
748 "%s(indirect is not aligned)", name
);
752 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
753 _mesa_error(ctx
, GL_INVALID_OPERATION
,
754 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
758 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
759 _mesa_error(ctx
, GL_INVALID_OPERATION
,
760 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
764 /* From the ARB_draw_indirect specification:
765 * "An INVALID_OPERATION error is generated if the commands source data
766 * beyond the end of the buffer object [...]"
768 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
769 _mesa_error(ctx
, GL_INVALID_OPERATION
,
770 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
774 if (!check_valid_to_render(ctx
, name
))
780 static inline GLboolean
781 valid_draw_indirect_elements(struct gl_context
*ctx
,
782 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
783 GLsizeiptr size
, const char *name
)
785 if (!valid_elements_type(ctx
, type
, name
))
789 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
790 * may not come from a client array and must come from an index buffer.
791 * If no element array buffer is bound, an INVALID_OPERATION error is
794 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
795 _mesa_error(ctx
, GL_INVALID_OPERATION
,
796 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
800 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
803 static inline GLboolean
804 valid_draw_indirect_multi(struct gl_context
*ctx
,
805 GLsizei primcount
, GLsizei stride
,
809 /* From the ARB_multi_draw_indirect specification:
810 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
811 * MultiDrawElementsIndirect if <primcount> is negative."
813 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
817 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
822 /* From the ARB_multi_draw_indirect specification:
823 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
824 * error is generated."
827 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
835 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
837 const GLvoid
*indirect
)
839 const unsigned drawArraysNumParams
= 4;
841 FLUSH_CURRENT(ctx
, 0);
843 return valid_draw_indirect(ctx
, mode
,
844 indirect
, drawArraysNumParams
* sizeof(GLuint
),
845 "glDrawArraysIndirect");
849 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
850 GLenum mode
, GLenum type
,
851 const GLvoid
*indirect
)
853 const unsigned drawElementsNumParams
= 5;
855 FLUSH_CURRENT(ctx
, 0);
857 return valid_draw_indirect_elements(ctx
, mode
, type
,
858 indirect
, drawElementsNumParams
* sizeof(GLuint
),
859 "glDrawElementsIndirect");
863 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
865 const GLvoid
*indirect
,
866 GLsizei primcount
, GLsizei stride
)
869 const unsigned drawArraysNumParams
= 4;
871 FLUSH_CURRENT(ctx
, 0);
873 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
876 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
877 "glMultiDrawArraysIndirect"))
880 /* number of bytes of the indirect buffer which will be read */
882 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
885 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
886 "glMultiDrawArraysIndirect"))
893 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
894 GLenum mode
, GLenum type
,
895 const GLvoid
*indirect
,
896 GLsizei primcount
, GLsizei stride
)
899 const unsigned drawElementsNumParams
= 5;
901 FLUSH_CURRENT(ctx
, 0);
903 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
906 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
907 "glMultiDrawElementsIndirect"))
910 /* number of bytes of the indirect buffer which will be read */
912 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
915 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
917 "glMultiDrawElementsIndirect"))
924 valid_draw_indirect_parameters(struct gl_context
*ctx
,
928 /* From the ARB_indirect_parameters specification:
929 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
930 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
934 _mesa_error(ctx
, GL_INVALID_VALUE
,
935 "%s(drawcount is not a multiple of 4)", name
);
939 /* From the ARB_indirect_parameters specification:
940 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
941 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
942 * PARAMETER_BUFFER_ARB binding point."
944 if (!_mesa_is_bufferobj(ctx
->ParameterBuffer
)) {
945 _mesa_error(ctx
, GL_INVALID_OPERATION
,
946 "%s: no buffer bound to PARAMETER_BUFFER", name
);
950 if (_mesa_check_disallowed_mapping(ctx
->ParameterBuffer
)) {
951 _mesa_error(ctx
, GL_INVALID_OPERATION
,
952 "%s(PARAMETER_BUFFER is mapped)", name
);
956 /* From the ARB_indirect_parameters specification:
957 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
958 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
959 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
960 * specified by <drawcount> would result in an out-of-bounds access."
962 if (ctx
->ParameterBuffer
->Size
< drawcount
+ sizeof(GLsizei
)) {
963 _mesa_error(ctx
, GL_INVALID_OPERATION
,
964 "%s(PARAMETER_BUFFER too small)", name
);
972 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context
*ctx
,
976 GLsizei maxdrawcount
,
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
, maxdrawcount
, stride
,
988 "glMultiDrawArraysIndirectCountARB"))
991 /* number of bytes of the indirect buffer which will be read */
993 ? (maxdrawcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
996 if (!valid_draw_indirect(ctx
, mode
, (void *)indirect
, size
,
997 "glMultiDrawArraysIndirectCountARB"))
1000 return valid_draw_indirect_parameters(
1001 ctx
, "glMultiDrawArraysIndirectCountARB", drawcount
);
1005 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context
*ctx
,
1006 GLenum mode
, GLenum type
,
1009 GLsizei maxdrawcount
,
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
, maxdrawcount
, stride
,
1021 "glMultiDrawElementsIndirectCountARB"))
1024 /* number of bytes of the indirect buffer which will be read */
1026 ? (maxdrawcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1029 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1030 (void *)indirect
, size
,
1031 "glMultiDrawElementsIndirectCountARB"))
1034 return valid_draw_indirect_parameters(
1035 ctx
, "glMultiDrawElementsIndirectCountARB", drawcount
);
1039 check_valid_to_compute(struct gl_context
*ctx
, const char *function
)
1041 struct gl_shader_program
*prog
;
1043 if (!_mesa_has_compute_shaders(ctx
)) {
1044 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1045 "unsupported function (%s) called",
1050 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1052 * "An INVALID_OPERATION error is generated if there is no active program
1053 * for the compute shader stage."
1055 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
1056 if (prog
== NULL
|| prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
1057 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1058 "%s(no active compute shader)",
1067 _mesa_validate_DispatchCompute(struct gl_context
*ctx
,
1068 const GLuint
*num_groups
)
1071 FLUSH_CURRENT(ctx
, 0);
1073 if (!check_valid_to_compute(ctx
, "glDispatchCompute"))
1076 for (i
= 0; i
< 3; i
++) {
1077 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1079 * "An INVALID_VALUE error is generated if any of num_groups_x,
1080 * num_groups_y and num_groups_z are greater than or equal to the
1081 * maximum work group count for the corresponding dimension."
1083 * However, the "or equal to" portions appears to be a specification
1084 * bug. In all other areas, the specification appears to indicate that
1085 * the number of workgroups can match the MAX_COMPUTE_WORK_GROUP_COUNT
1086 * value. For example, under DispatchComputeIndirect:
1088 * "If any of num_groups_x, num_groups_y or num_groups_z is greater than
1089 * the value of MAX_COMPUTE_WORK_GROUP_COUNT for the corresponding
1090 * dimension then the results are undefined."
1092 * Additionally, the OpenGLES 3.1 specification does not contain "or
1093 * equal to" as an error condition.
1095 if (num_groups
[i
] > ctx
->Const
.MaxComputeWorkGroupCount
[i
]) {
1096 _mesa_error(ctx
, GL_INVALID_VALUE
,
1097 "glDispatchCompute(num_groups_%c)", 'x' + i
);
1106 valid_dispatch_indirect(struct gl_context
*ctx
,
1108 GLsizei size
, const char *name
)
1110 const uint64_t end
= (uint64_t) indirect
+ size
;
1112 if (!check_valid_to_compute(ctx
, name
))
1115 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1117 * "An INVALID_VALUE error is generated if indirect is negative or is not a
1118 * multiple of four."
1120 if (indirect
& (sizeof(GLuint
) - 1)) {
1121 _mesa_error(ctx
, GL_INVALID_VALUE
,
1122 "%s(indirect is not aligned)", name
);
1127 _mesa_error(ctx
, GL_INVALID_VALUE
,
1128 "%s(indirect is less than zero)", name
);
1132 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1134 * "An INVALID_OPERATION error is generated if no buffer is bound to the
1135 * DRAW_INDIRECT_BUFFER binding, or if the command would source data
1136 * beyond the end of the buffer object."
1138 if (!_mesa_is_bufferobj(ctx
->DispatchIndirectBuffer
)) {
1139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1140 "%s: no buffer bound to DISPATCH_INDIRECT_BUFFER", name
);
1144 if (_mesa_check_disallowed_mapping(ctx
->DispatchIndirectBuffer
)) {
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1146 "%s(DISPATCH_INDIRECT_BUFFER is mapped)", name
);
1150 if (ctx
->DispatchIndirectBuffer
->Size
< end
) {
1151 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1152 "%s(DISPATCH_INDIRECT_BUFFER too small)", name
);
1160 _mesa_validate_DispatchComputeIndirect(struct gl_context
*ctx
,
1163 FLUSH_CURRENT(ctx
, 0);
1165 return valid_dispatch_indirect(ctx
, indirect
, 3 * sizeof(GLuint
),
1166 "glDispatchComputeIndirect");