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
]->info
.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_linked_shader
*tes_sh
=
211 tes
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
212 if (tes_sh
->info
.TessEval
.PointMode
)
213 mode_before_gs
= GL_POINTS
;
214 else if (tes_sh
->info
.TessEval
.PrimitiveMode
== GL_ISOLINES
)
215 mode_before_gs
= GL_LINES
;
217 /* the GL_QUADS mode generates triangles too */
218 mode_before_gs
= GL_TRIANGLES
;
221 switch (mode_before_gs
) {
223 valid_enum
= (geom_mode
== GL_POINTS
);
228 valid_enum
= (geom_mode
== GL_LINES
);
231 case GL_TRIANGLE_STRIP
:
232 case GL_TRIANGLE_FAN
:
233 valid_enum
= (geom_mode
== GL_TRIANGLES
);
240 case GL_LINES_ADJACENCY
:
241 case GL_LINE_STRIP_ADJACENCY
:
242 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
244 case GL_TRIANGLES_ADJACENCY
:
245 case GL_TRIANGLE_STRIP_ADJACENCY
:
246 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
253 _mesa_error(ctx
, GL_INVALID_OPERATION
,
254 "%s(mode=%s vs geometry shader input %s)",
256 _mesa_lookup_prim_by_nr(mode_before_gs
),
257 _mesa_lookup_prim_by_nr(geom_mode
));
262 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
264 * "Tessellation operates only on patch primitives. If tessellation is
265 * active, any command that transfers vertices to the GL will
266 * generate an INVALID_OPERATION error if the primitive mode is not
268 * Patch primitives are not supported by pipeline stages below the
269 * tessellation evaluation shader. If there is no active program
270 * object or the active program object does not contain a tessellation
271 * evaluation shader, the error INVALID_OPERATION is generated by any
272 * command that transfers vertices to the GL if the primitive mode is
276 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
] ||
277 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]) {
278 if (mode
!= GL_PATCHES
) {
279 _mesa_error(ctx
, GL_INVALID_OPERATION
,
280 "only GL_PATCHES valid with tessellation");
285 if (mode
== GL_PATCHES
) {
286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
287 "GL_PATCHES only valid with tessellation");
292 /* From the GL_EXT_transform_feedback spec:
294 * "The error INVALID_OPERATION is generated if Begin, or any command
295 * that performs an explicit Begin, is called when:
297 * * a geometry shader is not active and <mode> does not match the
298 * allowed begin modes for the current transform feedback state as
299 * given by table X.1.
301 * * a geometry shader is active and the output primitive type of the
302 * geometry shader does not match the allowed begin modes for the
303 * current transform feedback state as given by table X.1.
306 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
307 GLboolean pass
= GL_TRUE
;
309 if(ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
310 switch (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->
311 _LinkedShaders
[MESA_SHADER_GEOMETRY
]->
312 info
.Geom
.OutputType
) {
314 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
317 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
319 case GL_TRIANGLE_STRIP
:
320 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
326 else if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]) {
327 struct gl_shader_program
*tes
=
328 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
329 struct gl_linked_shader
*tes_sh
=
330 tes
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
331 if (tes_sh
->info
.TessEval
.PointMode
)
332 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
333 else if (tes_sh
->info
.TessEval
.PrimitiveMode
== GL_ISOLINES
)
334 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
336 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
341 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
346 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
349 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
354 _mesa_error(ctx
, GL_INVALID_OPERATION
,
355 "%s(mode=%s vs transform feedback %s)",
357 _mesa_lookup_prim_by_nr(mode
),
358 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
367 * Verify that the element type is valid.
369 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
372 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
375 case GL_UNSIGNED_BYTE
:
376 case GL_UNSIGNED_SHORT
:
377 case GL_UNSIGNED_INT
:
381 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
382 _mesa_enum_to_string(type
));
388 validate_DrawElements_common(struct gl_context
*ctx
,
389 GLenum mode
, GLsizei count
, GLenum type
,
390 const GLvoid
*indices
,
393 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
394 * Primitive Capture):
396 * The error INVALID_OPERATION is also generated by DrawElements,
397 * DrawElementsInstanced, and DrawRangeElements while transform feedback
398 * is active and not paused, regardless of mode.
400 if (_mesa_is_gles3(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
401 _mesa_is_xfb_active_and_unpaused(ctx
)) {
402 _mesa_error(ctx
, GL_INVALID_OPERATION
,
403 "%s(transform feedback active)", caller
);
408 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", caller
);
412 if (!_mesa_valid_prim_mode(ctx
, mode
, caller
)) {
416 if (!valid_elements_type(ctx
, type
, caller
))
419 if (!check_valid_to_render(ctx
, caller
))
422 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
424 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
434 * Error checking for glDrawElements(). Includes parameter checking
435 * and VBO bounds checking.
436 * \return GL_TRUE if OK to render, GL_FALSE if error found
439 _mesa_validate_DrawElements(struct gl_context
*ctx
,
440 GLenum mode
, GLsizei count
, GLenum type
,
441 const GLvoid
*indices
)
443 FLUSH_CURRENT(ctx
, 0);
445 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
451 * Error checking for glMultiDrawElements(). Includes parameter checking
452 * and VBO bounds checking.
453 * \return GL_TRUE if OK to render, GL_FALSE if error found
456 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
457 GLenum mode
, const GLsizei
*count
,
458 GLenum type
, const GLvoid
* const *indices
,
463 FLUSH_CURRENT(ctx
, 0);
465 for (i
= 0; i
< primcount
; i
++) {
467 _mesa_error(ctx
, GL_INVALID_VALUE
,
468 "glMultiDrawElements(count)" );
473 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
477 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
480 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
483 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
485 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
486 for (i
= 0; i
< primcount
; i
++) {
497 * Error checking for glDrawRangeElements(). Includes parameter checking
498 * and VBO bounds checking.
499 * \return GL_TRUE if OK to render, GL_FALSE if error found
502 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
503 GLuint start
, GLuint end
,
504 GLsizei count
, GLenum type
,
505 const GLvoid
*indices
)
507 FLUSH_CURRENT(ctx
, 0);
510 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
514 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
515 "glDrawRangeElements");
519 validate_draw_arrays(struct gl_context
*ctx
, const char *func
,
520 GLenum mode
, GLsizei count
, GLsizei numInstances
)
522 struct gl_transform_feedback_object
*xfb_obj
523 = ctx
->TransformFeedback
.CurrentObject
;
524 FLUSH_CURRENT(ctx
, 0);
527 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(count)", func
);
531 if (!_mesa_valid_prim_mode(ctx
, mode
, func
))
534 if (!check_valid_to_render(ctx
, func
))
537 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
538 * Primitive Capture):
540 * The error INVALID_OPERATION is generated by DrawArrays and
541 * DrawArraysInstanced if recording the vertices of a primitive to the
542 * buffer objects being used for transform feedback purposes would result
543 * in either exceeding the limits of any buffer object’s size, or in
544 * exceeding the end position offset + size − 1, as set by
547 * This is in contrast to the behaviour of desktop GL, where the extra
548 * primitives are silently dropped from the transform feedback buffer.
550 * This text is removed in ES 3.2, presumably because it's not really
551 * implementable with geometry and tessellation shaders. In fact,
552 * the OES_geometry_shader spec says:
554 * "(13) Does this extension change how transform feedback operates
555 * compared to unextended OpenGL ES 3.0 or 3.1?
557 * RESOLVED: Yes. Because dynamic geometry amplification in a geometry
558 * shader can make it difficult if not impossible to predict the amount
559 * of geometry that may be generated in advance of executing the shader,
560 * the draw-time error for transform feedback buffer overflow conditions
561 * is removed and replaced with the GL behavior (primitives are not
562 * written and the corresponding counter is not updated)..."
564 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
) &&
565 !_mesa_has_OES_geometry_shader(ctx
) &&
566 !_mesa_has_OES_tessellation_shader(ctx
)) {
567 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
568 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
569 _mesa_error(ctx
, GL_INVALID_OPERATION
,
570 "%s(exceeds transform feedback size)", func
);
573 xfb_obj
->GlesRemainingPrims
-= prim_count
;
583 * Called from the tnl module to error check the function parameters and
584 * verify that we really can draw something.
585 * \return GL_TRUE if OK to render, GL_FALSE if error found
588 _mesa_validate_DrawArrays(struct gl_context
*ctx
, GLenum mode
, GLsizei count
)
590 return validate_draw_arrays(ctx
, "glDrawArrays", mode
, count
, 1);
595 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
596 GLsizei count
, GLsizei numInstances
)
599 _mesa_error(ctx
, GL_INVALID_VALUE
,
600 "glDrawArraysInstanced(start=%d)", first
);
604 if (numInstances
<= 0) {
605 if (numInstances
< 0)
606 _mesa_error(ctx
, GL_INVALID_VALUE
,
607 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
611 return validate_draw_arrays(ctx
, "glDrawArraysInstanced", mode
, count
, 1);
616 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
617 GLenum mode
, GLsizei count
, GLenum type
,
618 const GLvoid
*indices
, GLsizei numInstances
)
620 FLUSH_CURRENT(ctx
, 0);
622 if (numInstances
< 0) {
623 _mesa_error(ctx
, GL_INVALID_VALUE
,
624 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
628 return validate_DrawElements_common(ctx
, mode
, count
, type
, indices
,
629 "glDrawElementsInstanced")
630 && (numInstances
> 0);
635 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
637 struct gl_transform_feedback_object
*obj
,
639 GLsizei numInstances
)
641 FLUSH_CURRENT(ctx
, 0);
643 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
648 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
652 /* From the GL 4.5 specification, page 429:
653 * "An INVALID_VALUE error is generated if id is not the name of a
654 * transform feedback object."
656 if (!obj
->EverBound
) {
657 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
661 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
662 _mesa_error(ctx
, GL_INVALID_VALUE
,
663 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
667 if (!obj
->EndedAnytime
) {
668 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
672 if (numInstances
<= 0) {
673 if (numInstances
< 0)
674 _mesa_error(ctx
, GL_INVALID_VALUE
,
675 "glDrawTransformFeedback*Instanced(numInstances=%d)",
680 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {
688 valid_draw_indirect(struct gl_context
*ctx
,
689 GLenum mode
, const GLvoid
*indirect
,
690 GLsizei size
, const char *name
)
692 const uint64_t end
= (uint64_t) (uintptr_t) indirect
+ size
;
694 /* OpenGL ES 3.1 spec. section 10.5:
696 * "DrawArraysIndirect requires that all data sourced for the
697 * command, including the DrawArraysIndirectCommand
698 * structure, be in buffer objects, and may not be called when
699 * the default vertex array object is bound."
701 if (ctx
->Array
.VAO
== ctx
->Array
.DefaultVAO
) {
702 _mesa_error(ctx
, GL_INVALID_OPERATION
, "(no VAO bound)");
706 /* From OpenGL ES 3.1 spec. section 10.5:
707 * "An INVALID_OPERATION error is generated if zero is bound to
708 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
711 * Here we check that for each enabled vertex array we have a vertex
714 if (_mesa_is_gles31(ctx
) &&
715 ctx
->Array
.VAO
->_Enabled
!= ctx
->Array
.VAO
->VertexAttribBufferMask
) {
716 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(No VBO bound)", name
);
720 if (!_mesa_valid_prim_mode(ctx
, mode
, name
))
723 /* OpenGL ES 3.1 specification, section 10.5:
725 * "An INVALID_OPERATION error is generated if
726 * transform feedback is active and not paused."
728 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
&&
729 _mesa_is_xfb_active_and_unpaused(ctx
)) {
730 _mesa_error(ctx
, GL_INVALID_OPERATION
,
731 "%s(TransformFeedback is active and not paused)", name
);
734 /* From OpenGL version 4.4. section 10.5
735 * and OpenGL ES 3.1, section 10.6:
737 * "An INVALID_VALUE error is generated if indirect is not a
738 * multiple of the size, in basic machine units, of uint."
740 if ((GLsizeiptr
)indirect
& (sizeof(GLuint
) - 1)) {
741 _mesa_error(ctx
, GL_INVALID_VALUE
,
742 "%s(indirect is not aligned)", name
);
746 if (!_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
747 _mesa_error(ctx
, GL_INVALID_OPERATION
,
748 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name
);
752 if (_mesa_check_disallowed_mapping(ctx
->DrawIndirectBuffer
)) {
753 _mesa_error(ctx
, GL_INVALID_OPERATION
,
754 "%s(DRAW_INDIRECT_BUFFER is mapped)", name
);
758 /* From the ARB_draw_indirect specification:
759 * "An INVALID_OPERATION error is generated if the commands source data
760 * beyond the end of the buffer object [...]"
762 if (ctx
->DrawIndirectBuffer
->Size
< end
) {
763 _mesa_error(ctx
, GL_INVALID_OPERATION
,
764 "%s(DRAW_INDIRECT_BUFFER too small)", name
);
768 if (!check_valid_to_render(ctx
, name
))
774 static inline GLboolean
775 valid_draw_indirect_elements(struct gl_context
*ctx
,
776 GLenum mode
, GLenum type
, const GLvoid
*indirect
,
777 GLsizeiptr size
, const char *name
)
779 if (!valid_elements_type(ctx
, type
, name
))
783 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
784 * may not come from a client array and must come from an index buffer.
785 * If no element array buffer is bound, an INVALID_OPERATION error is
788 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
789 _mesa_error(ctx
, GL_INVALID_OPERATION
,
790 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name
);
794 return valid_draw_indirect(ctx
, mode
, indirect
, size
, name
);
797 static inline GLboolean
798 valid_draw_indirect_multi(struct gl_context
*ctx
,
799 GLsizei primcount
, GLsizei stride
,
803 /* From the ARB_multi_draw_indirect specification:
804 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
805 * MultiDrawElementsIndirect if <primcount> is negative."
807 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
811 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(primcount < 0)", name
);
816 /* From the ARB_multi_draw_indirect specification:
817 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
818 * error is generated."
821 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(stride %% 4)", name
);
829 _mesa_validate_DrawArraysIndirect(struct gl_context
*ctx
,
831 const GLvoid
*indirect
)
833 const unsigned drawArraysNumParams
= 4;
835 FLUSH_CURRENT(ctx
, 0);
837 return valid_draw_indirect(ctx
, mode
,
838 indirect
, drawArraysNumParams
* sizeof(GLuint
),
839 "glDrawArraysIndirect");
843 _mesa_validate_DrawElementsIndirect(struct gl_context
*ctx
,
844 GLenum mode
, GLenum type
,
845 const GLvoid
*indirect
)
847 const unsigned drawElementsNumParams
= 5;
849 FLUSH_CURRENT(ctx
, 0);
851 return valid_draw_indirect_elements(ctx
, mode
, type
,
852 indirect
, drawElementsNumParams
* sizeof(GLuint
),
853 "glDrawElementsIndirect");
857 _mesa_validate_MultiDrawArraysIndirect(struct gl_context
*ctx
,
859 const GLvoid
*indirect
,
860 GLsizei primcount
, GLsizei stride
)
863 const unsigned drawArraysNumParams
= 4;
865 FLUSH_CURRENT(ctx
, 0);
867 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
870 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
871 "glMultiDrawArraysIndirect"))
874 /* number of bytes of the indirect buffer which will be read */
876 ? (primcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
879 if (!valid_draw_indirect(ctx
, mode
, indirect
, size
,
880 "glMultiDrawArraysIndirect"))
887 _mesa_validate_MultiDrawElementsIndirect(struct gl_context
*ctx
,
888 GLenum mode
, GLenum type
,
889 const GLvoid
*indirect
,
890 GLsizei primcount
, GLsizei stride
)
893 const unsigned drawElementsNumParams
= 5;
895 FLUSH_CURRENT(ctx
, 0);
897 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
900 if (!valid_draw_indirect_multi(ctx
, primcount
, stride
,
901 "glMultiDrawElementsIndirect"))
904 /* number of bytes of the indirect buffer which will be read */
906 ? (primcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
909 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
911 "glMultiDrawElementsIndirect"))
918 valid_draw_indirect_parameters(struct gl_context
*ctx
,
922 /* From the ARB_indirect_parameters specification:
923 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
924 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
928 _mesa_error(ctx
, GL_INVALID_VALUE
,
929 "%s(drawcount is not a multiple of 4)", name
);
933 /* From the ARB_indirect_parameters specification:
934 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
935 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
936 * PARAMETER_BUFFER_ARB binding point."
938 if (!_mesa_is_bufferobj(ctx
->ParameterBuffer
)) {
939 _mesa_error(ctx
, GL_INVALID_OPERATION
,
940 "%s: no buffer bound to PARAMETER_BUFFER", name
);
944 if (_mesa_check_disallowed_mapping(ctx
->ParameterBuffer
)) {
945 _mesa_error(ctx
, GL_INVALID_OPERATION
,
946 "%s(PARAMETER_BUFFER is mapped)", name
);
950 /* From the ARB_indirect_parameters specification:
951 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
952 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
953 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
954 * specified by <drawcount> would result in an out-of-bounds access."
956 if (ctx
->ParameterBuffer
->Size
< drawcount
+ sizeof(GLsizei
)) {
957 _mesa_error(ctx
, GL_INVALID_OPERATION
,
958 "%s(PARAMETER_BUFFER too small)", name
);
966 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context
*ctx
,
970 GLsizei maxdrawcount
,
974 const unsigned drawArraysNumParams
= 4;
976 FLUSH_CURRENT(ctx
, 0);
978 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
981 if (!valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
982 "glMultiDrawArraysIndirectCountARB"))
985 /* number of bytes of the indirect buffer which will be read */
987 ? (maxdrawcount
- 1) * stride
+ drawArraysNumParams
* sizeof(GLuint
)
990 if (!valid_draw_indirect(ctx
, mode
, (void *)indirect
, size
,
991 "glMultiDrawArraysIndirectCountARB"))
994 return valid_draw_indirect_parameters(
995 ctx
, "glMultiDrawArraysIndirectCountARB", drawcount
);
999 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context
*ctx
,
1000 GLenum mode
, GLenum type
,
1003 GLsizei maxdrawcount
,
1006 GLsizeiptr size
= 0;
1007 const unsigned drawElementsNumParams
= 5;
1009 FLUSH_CURRENT(ctx
, 0);
1011 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1012 assert(stride
!= 0);
1014 if (!valid_draw_indirect_multi(ctx
, maxdrawcount
, stride
,
1015 "glMultiDrawElementsIndirectCountARB"))
1018 /* number of bytes of the indirect buffer which will be read */
1020 ? (maxdrawcount
- 1) * stride
+ drawElementsNumParams
* sizeof(GLuint
)
1023 if (!valid_draw_indirect_elements(ctx
, mode
, type
,
1024 (void *)indirect
, size
,
1025 "glMultiDrawElementsIndirectCountARB"))
1028 return valid_draw_indirect_parameters(
1029 ctx
, "glMultiDrawElementsIndirectCountARB", drawcount
);
1033 check_valid_to_compute(struct gl_context
*ctx
, const char *function
)
1035 struct gl_shader_program
*prog
;
1037 if (!_mesa_has_compute_shaders(ctx
)) {
1038 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1039 "unsupported function (%s) called",
1044 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1046 * "An INVALID_OPERATION error is generated if there is no active program
1047 * for the compute shader stage."
1049 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
1050 if (prog
== NULL
|| prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
] == NULL
) {
1051 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1052 "%s(no active compute shader)",
1061 _mesa_validate_DispatchCompute(struct gl_context
*ctx
,
1062 const GLuint
*num_groups
)
1065 FLUSH_CURRENT(ctx
, 0);
1067 if (!check_valid_to_compute(ctx
, "glDispatchCompute"))
1070 for (i
= 0; i
< 3; i
++) {
1071 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1073 * "An INVALID_VALUE error is generated if any of num_groups_x,
1074 * num_groups_y and num_groups_z are greater than or equal to the
1075 * maximum work group count for the corresponding dimension."
1077 * However, the "or equal to" portions appears to be a specification
1078 * bug. In all other areas, the specification appears to indicate that
1079 * the number of workgroups can match the MAX_COMPUTE_WORK_GROUP_COUNT
1080 * value. For example, under DispatchComputeIndirect:
1082 * "If any of num_groups_x, num_groups_y or num_groups_z is greater than
1083 * the value of MAX_COMPUTE_WORK_GROUP_COUNT for the corresponding
1084 * dimension then the results are undefined."
1086 * Additionally, the OpenGLES 3.1 specification does not contain "or
1087 * equal to" as an error condition.
1089 if (num_groups
[i
] > ctx
->Const
.MaxComputeWorkGroupCount
[i
]) {
1090 _mesa_error(ctx
, GL_INVALID_VALUE
,
1091 "glDispatchCompute(num_groups_%c)", 'x' + i
);
1100 valid_dispatch_indirect(struct gl_context
*ctx
,
1102 GLsizei size
, const char *name
)
1104 const uint64_t end
= (uint64_t) indirect
+ size
;
1106 if (!check_valid_to_compute(ctx
, name
))
1109 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1111 * "An INVALID_VALUE error is generated if indirect is negative or is not a
1112 * multiple of four."
1114 if (indirect
& (sizeof(GLuint
) - 1)) {
1115 _mesa_error(ctx
, GL_INVALID_VALUE
,
1116 "%s(indirect is not aligned)", name
);
1121 _mesa_error(ctx
, GL_INVALID_VALUE
,
1122 "%s(indirect is less than zero)", name
);
1126 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1128 * "An INVALID_OPERATION error is generated if no buffer is bound to the
1129 * DRAW_INDIRECT_BUFFER binding, or if the command would source data
1130 * beyond the end of the buffer object."
1132 if (!_mesa_is_bufferobj(ctx
->DispatchIndirectBuffer
)) {
1133 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1134 "%s: no buffer bound to DISPATCH_INDIRECT_BUFFER", name
);
1138 if (_mesa_check_disallowed_mapping(ctx
->DispatchIndirectBuffer
)) {
1139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1140 "%s(DISPATCH_INDIRECT_BUFFER is mapped)", name
);
1144 if (ctx
->DispatchIndirectBuffer
->Size
< end
) {
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1146 "%s(DISPATCH_INDIRECT_BUFFER too small)", name
);
1154 _mesa_validate_DispatchComputeIndirect(struct gl_context
*ctx
,
1157 FLUSH_CURRENT(ctx
, 0);
1159 return valid_dispatch_indirect(ctx
, indirect
, 3 * sizeof(GLuint
),
1160 "glDispatchComputeIndirect");