2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 #include "api_validate.h"
29 #include "bufferobj.h"
35 #include "transformfeedback.h"
40 * \return number of bytes in array [count] of type.
43 index_bytes(GLenum type
, GLsizei count
)
45 if (type
== GL_UNSIGNED_INT
) {
46 return count
* sizeof(GLuint
);
48 else if (type
== GL_UNSIGNED_BYTE
) {
49 return count
* sizeof(GLubyte
);
52 ASSERT(type
== GL_UNSIGNED_SHORT
);
53 return count
* sizeof(GLushort
);
59 * Find the max index in the given element/index buffer
62 _mesa_max_buffer_index(struct gl_context
*ctx
, GLuint count
, GLenum type
,
64 struct gl_buffer_object
*elementBuf
)
66 const GLubyte
*map
= NULL
;
70 if (_mesa_is_bufferobj(elementBuf
)) {
71 /* elements are in a user-defined buffer object. need to map it */
72 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, elementBuf
->Size
,
73 GL_MAP_READ_BIT
, elementBuf
);
74 /* Actual address is the sum of pointers */
75 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
78 if (type
== GL_UNSIGNED_INT
) {
79 for (i
= 0; i
< count
; i
++)
80 if (((GLuint
*) indices
)[i
] > max
)
81 max
= ((GLuint
*) indices
)[i
];
83 else if (type
== GL_UNSIGNED_SHORT
) {
84 for (i
= 0; i
< count
; i
++)
85 if (((GLushort
*) indices
)[i
] > max
)
86 max
= ((GLushort
*) indices
)[i
];
89 ASSERT(type
== GL_UNSIGNED_BYTE
);
90 for (i
= 0; i
< count
; i
++)
91 if (((GLubyte
*) indices
)[i
] > max
)
92 max
= ((GLubyte
*) indices
)[i
];
96 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
);
104 * Check if OK to draw arrays/elements.
107 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
109 if (!_mesa_valid_to_render(ctx
, function
)) {
115 /* For ES2, we can draw if any vertex array is enabled (and we
116 * should always have a vertex program/shader). */
117 if (ctx
->Array
.ArrayObj
->_Enabled
== 0x0 || !ctx
->VertexProgram
._Current
)
122 /* For OpenGL ES, only draw if we have vertex positions
124 if (!ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
128 case API_OPENGL_COMPAT
:
129 case API_OPENGL_CORE
:
131 const struct gl_shader_program
*vsProg
=
132 ctx
->Shader
.CurrentVertexProgram
;
133 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
134 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
135 if (haveVertexShader
|| haveVertexProgram
) {
136 /* Draw regardless of whether or not we have any vertex arrays.
137 * (Ex: could draw a point using a constant vertex pos)
142 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
145 return (ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
146 ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
152 assert(!"Invalid API value in check_valid_to_render()");
160 * Do bounds checking on array element indexes. Check that the vertices
161 * pointed to by the indices don't lie outside buffer object bounds.
162 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
165 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
166 const GLvoid
*indices
, GLint basevertex
)
168 struct _mesa_prim prim
;
169 struct _mesa_index_buffer ib
;
172 /* Only the X Server needs to do this -- otherwise, accessing outside
173 * array/BO bounds allows application termination.
175 if (!ctx
->Const
.CheckArrayBounds
)
178 memset(&prim
, 0, sizeof(prim
));
181 memset(&ib
, 0, sizeof(ib
));
184 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
186 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
188 if ((int)(min
+ basevertex
) < 0 ||
189 max
+ basevertex
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
190 /* the max element is out of bounds of one or more enabled arrays */
191 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
192 max
, ctx
->Array
.ArrayObj
->_MaxElement
);
201 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
202 * etc? The set of legal values depends on whether geometry shaders/programs
204 * Note: This may be called during display list compilation.
207 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
215 case GL_TRIANGLE_STRIP
:
216 case GL_TRIANGLE_FAN
:
221 return (ctx
->API
== API_OPENGL_COMPAT
);
222 case GL_LINES_ADJACENCY
:
223 case GL_LINE_STRIP_ADJACENCY
:
224 case GL_TRIANGLES_ADJACENCY
:
225 case GL_TRIANGLE_STRIP_ADJACENCY
:
226 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_geometry_shader4
;
234 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
235 * etc? Also, do additional checking related to transformation feedback.
236 * Note: this function cannot be called during glNewList(GL_COMPILE) because
237 * this code depends on current transform feedback state.
240 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
242 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
245 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
249 /* From the GL_EXT_transform_feedback spec:
251 * "The error INVALID_OPERATION is generated if Begin, or any command
252 * that performs an explicit Begin, is called when:
254 * * a geometry shader is not active and <mode> does not match the
255 * allowed begin modes for the current transform feedback state as
256 * given by table X.1.
258 * * a geometry shader is active and the output primitive type of the
259 * geometry shader does not match the allowed begin modes for the
260 * current transform feedback state as given by table X.1.
263 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
264 GLboolean pass
= GL_TRUE
;
268 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
273 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
276 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
280 _mesa_error(ctx
, GL_INVALID_OPERATION
,
281 "%s(mode=%s vs transform feedback %s)",
283 _mesa_lookup_prim_by_nr(mode
),
284 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
293 * Verify that the element type is valid.
295 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
298 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
301 case GL_UNSIGNED_BYTE
:
302 case GL_UNSIGNED_SHORT
:
303 case GL_UNSIGNED_INT
:
307 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
308 _mesa_lookup_enum_by_nr(type
));
314 * Error checking for glDrawElements(). Includes parameter checking
315 * and VBO bounds checking.
316 * \return GL_TRUE if OK to render, GL_FALSE if error found
319 _mesa_validate_DrawElements(struct gl_context
*ctx
,
320 GLenum mode
, GLsizei count
, GLenum type
,
321 const GLvoid
*indices
, GLint basevertex
)
323 FLUSH_CURRENT(ctx
, 0);
325 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
326 * Primitive Capture):
328 * The error INVALID_OPERATION is also generated by DrawElements,
329 * DrawElementsInstanced, and DrawRangeElements while transform feedback
330 * is active and not paused, regardless of mode.
332 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
334 "glDrawElements(transform feedback active)");
340 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
344 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
348 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
351 if (!check_valid_to_render(ctx
, "glDrawElements"))
354 /* Vertex buffer object tests */
355 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
356 /* use indices in the buffer object */
357 /* make sure count doesn't go outside buffer bounds */
358 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
359 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
364 /* not using a VBO */
369 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
377 * Error checking for glMultiDrawElements(). Includes parameter checking
378 * and VBO bounds checking.
379 * \return GL_TRUE if OK to render, GL_FALSE if error found
382 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
383 GLenum mode
, const GLsizei
*count
,
384 GLenum type
, const GLvoid
* const *indices
,
385 GLuint primcount
, const GLint
*basevertex
)
389 FLUSH_CURRENT(ctx
, 0);
391 for (i
= 0; i
< primcount
; i
++) {
394 _mesa_error(ctx
, GL_INVALID_VALUE
,
395 "glMultiDrawElements(count)" );
400 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
404 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
407 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
410 /* Vertex buffer object tests */
411 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
412 /* use indices in the buffer object */
413 /* make sure count doesn't go outside buffer bounds */
414 for (i
= 0; i
< primcount
; i
++) {
415 if (index_bytes(type
, count
[i
]) >
416 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
418 "glMultiDrawElements index out of buffer bounds");
424 /* not using a VBO */
425 for (i
= 0; i
< primcount
; i
++) {
431 for (i
= 0; i
< primcount
; i
++) {
432 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
433 basevertex
? basevertex
[i
] : 0))
442 * Error checking for glDrawRangeElements(). Includes parameter checking
443 * and VBO bounds checking.
444 * \return GL_TRUE if OK to render, GL_FALSE if error found
447 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
448 GLuint start
, GLuint end
,
449 GLsizei count
, GLenum type
,
450 const GLvoid
*indices
, GLint basevertex
)
452 FLUSH_CURRENT(ctx
, 0);
454 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
455 * Primitive Capture):
457 * The error INVALID_OPERATION is also generated by DrawElements,
458 * DrawElementsInstanced, and DrawRangeElements while transform feedback
459 * is active and not paused, regardless of mode.
461 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
462 _mesa_error(ctx
, GL_INVALID_OPERATION
,
463 "glDrawElements(transform feedback active)");
469 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
473 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
478 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
482 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
485 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
488 /* Vertex buffer object tests */
489 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
490 /* use indices in the buffer object */
491 /* make sure count doesn't go outside buffer bounds */
492 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
493 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
498 /* not using a VBO */
503 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
511 * Called from the tnl module to error check the function parameters and
512 * verify that we really can draw something.
513 * \return GL_TRUE if OK to render, GL_FALSE if error found
516 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
517 GLenum mode
, GLint start
, GLsizei count
)
519 struct gl_transform_feedback_object
*xfb_obj
520 = ctx
->TransformFeedback
.CurrentObject
;
521 FLUSH_CURRENT(ctx
, 0);
525 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
529 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
533 if (!check_valid_to_render(ctx
, "glDrawArrays"))
536 if (ctx
->Const
.CheckArrayBounds
) {
537 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
541 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
542 * Primitive Capture):
544 * The error INVALID_OPERATION is generated by DrawArrays and
545 * DrawArraysInstanced if recording the vertices of a primitive to the
546 * buffer objects being used for transform feedback purposes would result
547 * in either exceeding the limits of any buffer object’s size, or in
548 * exceeding the end position offset + size − 1, as set by
551 * This is in contrast to the behaviour of desktop GL, where the extra
552 * primitives are silently dropped from the transform feedback buffer.
554 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
555 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
556 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
557 _mesa_error(ctx
, GL_INVALID_OPERATION
,
558 "glDrawArrays(exceeds transform feedback size)");
561 xfb_obj
->GlesRemainingPrims
-= prim_count
;
569 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
570 GLsizei count
, GLsizei numInstances
)
572 struct gl_transform_feedback_object
*xfb_obj
573 = ctx
->TransformFeedback
.CurrentObject
;
574 FLUSH_CURRENT(ctx
, 0);
578 _mesa_error(ctx
, GL_INVALID_VALUE
,
579 "glDrawArraysInstanced(count=%d)", count
);
584 _mesa_error(ctx
, GL_INVALID_VALUE
,
585 "glDrawArraysInstanced(start=%d)", first
);
589 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
593 if (numInstances
<= 0) {
594 if (numInstances
< 0)
595 _mesa_error(ctx
, GL_INVALID_VALUE
,
596 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
600 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
603 if (ctx
->Const
.CheckArrayBounds
) {
604 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
608 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
609 * Primitive Capture):
611 * The error INVALID_OPERATION is generated by DrawArrays and
612 * DrawArraysInstanced if recording the vertices of a primitive to the
613 * buffer objects being used for transform feedback purposes would result
614 * in either exceeding the limits of any buffer object’s size, or in
615 * exceeding the end position offset + size − 1, as set by
618 * This is in contrast to the behaviour of desktop GL, where the extra
619 * primitives are silently dropped from the transform feedback buffer.
621 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
623 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
624 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
625 _mesa_error(ctx
, GL_INVALID_OPERATION
,
626 "glDrawArraysInstanced(exceeds transform feedback size)");
629 xfb_obj
->GlesRemainingPrims
-= prim_count
;
637 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
638 GLenum mode
, GLsizei count
, GLenum type
,
639 const GLvoid
*indices
, GLsizei numInstances
,
642 FLUSH_CURRENT(ctx
, 0);
644 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
645 * Primitive Capture):
647 * The error INVALID_OPERATION is also generated by DrawElements,
648 * DrawElementsInstanced, and DrawRangeElements while transform feedback
649 * is active and not paused, regardless of mode.
651 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
652 _mesa_error(ctx
, GL_INVALID_OPERATION
,
653 "glDrawElements(transform feedback active)");
659 _mesa_error(ctx
, GL_INVALID_VALUE
,
660 "glDrawElementsInstanced(count=%d)", count
);
664 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
668 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
671 if (numInstances
<= 0) {
672 if (numInstances
< 0)
673 _mesa_error(ctx
, GL_INVALID_VALUE
,
674 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
678 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
681 /* Vertex buffer object tests */
682 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
683 /* use indices in the buffer object */
684 /* make sure count doesn't go outside buffer bounds */
685 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
687 "glDrawElementsInstanced index out of buffer bounds");
692 /* not using a VBO */
697 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
705 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
707 struct gl_transform_feedback_object
*obj
,
709 GLsizei numInstances
)
711 FLUSH_CURRENT(ctx
, 0);
713 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
718 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
722 if (!obj
->EndedAnytime
) {
723 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
727 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
728 _mesa_error(ctx
, GL_INVALID_VALUE
,
729 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
733 if (numInstances
<= 0) {
734 if (numInstances
< 0)
735 _mesa_error(ctx
, GL_INVALID_VALUE
,
736 "glDrawTransformFeedback*Instanced(numInstances=%d)",
741 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {