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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "api_validate.h"
28 #include "bufferobj.h"
31 #include "mfeatures.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
206 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
216 case GL_TRIANGLE_STRIP
:
217 case GL_TRIANGLE_FAN
:
223 valid_enum
= (ctx
->API
== API_OPENGL_COMPAT
);
225 case GL_LINES_ADJACENCY
:
226 case GL_LINE_STRIP_ADJACENCY
:
227 case GL_TRIANGLES_ADJACENCY
:
228 case GL_TRIANGLE_STRIP_ADJACENCY
:
229 valid_enum
= _mesa_is_desktop_gl(ctx
)
230 && ctx
->Extensions
.ARB_geometry_shader4
;
238 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
242 /* From the GL_EXT_transform_feedback spec:
244 * "The error INVALID_OPERATION is generated if Begin, or any command
245 * that performs an explicit Begin, is called when:
247 * * a geometry shader is not active and <mode> does not match the
248 * allowed begin modes for the current transform feedback state as
249 * given by table X.1.
251 * * a geometry shader is active and the output primitive type of the
252 * geometry shader does not match the allowed begin modes for the
253 * current transform feedback state as given by table X.1.
256 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
257 GLboolean pass
= GL_TRUE
;
261 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
266 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
269 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
273 _mesa_error(ctx
, GL_INVALID_OPERATION
,
274 "%s(mode=%s vs transform feedback %s)",
276 _mesa_lookup_prim_by_nr(mode
),
277 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
286 * Verify that the element type is valid.
288 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
291 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
294 case GL_UNSIGNED_BYTE
:
295 case GL_UNSIGNED_SHORT
:
296 case GL_UNSIGNED_INT
:
300 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
301 _mesa_lookup_enum_by_nr(type
));
307 * Error checking for glDrawElements(). Includes parameter checking
308 * and VBO bounds checking.
309 * \return GL_TRUE if OK to render, GL_FALSE if error found
312 _mesa_validate_DrawElements(struct gl_context
*ctx
,
313 GLenum mode
, GLsizei count
, GLenum type
,
314 const GLvoid
*indices
, GLint basevertex
)
316 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
317 FLUSH_CURRENT(ctx
, 0);
319 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
320 * Primitive Capture):
322 * The error INVALID_OPERATION is also generated by DrawElements,
323 * DrawElementsInstanced, and DrawRangeElements while transform feedback
324 * is active and not paused, regardless of mode.
326 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
328 "glDrawElements(transform feedback active)");
334 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
338 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
342 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
345 if (!check_valid_to_render(ctx
, "glDrawElements"))
348 /* Vertex buffer object tests */
349 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
350 /* use indices in the buffer object */
351 /* make sure count doesn't go outside buffer bounds */
352 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
353 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
358 /* not using a VBO */
363 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
371 * Error checking for glMultiDrawElements(). Includes parameter checking
372 * and VBO bounds checking.
373 * \return GL_TRUE if OK to render, GL_FALSE if error found
376 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
377 GLenum mode
, const GLsizei
*count
,
378 GLenum type
, const GLvoid
* const *indices
,
379 GLuint primcount
, const GLint
*basevertex
)
383 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
384 FLUSH_CURRENT(ctx
, 0);
386 for (i
= 0; i
< primcount
; i
++) {
389 _mesa_error(ctx
, GL_INVALID_VALUE
,
390 "glMultiDrawElements(count)" );
395 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
399 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
402 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
405 /* Vertex buffer object tests */
406 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
407 /* use indices in the buffer object */
408 /* make sure count doesn't go outside buffer bounds */
409 for (i
= 0; i
< primcount
; i
++) {
410 if (index_bytes(type
, count
[i
]) >
411 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
413 "glMultiDrawElements index out of buffer bounds");
419 /* not using a VBO */
420 for (i
= 0; i
< primcount
; i
++) {
426 for (i
= 0; i
< primcount
; i
++) {
427 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
428 basevertex
? basevertex
[i
] : 0))
437 * Error checking for glDrawRangeElements(). Includes parameter checking
438 * and VBO bounds checking.
439 * \return GL_TRUE if OK to render, GL_FALSE if error found
442 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
443 GLuint start
, GLuint end
,
444 GLsizei count
, GLenum type
,
445 const GLvoid
*indices
, GLint basevertex
)
447 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
448 FLUSH_CURRENT(ctx
, 0);
450 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
451 * Primitive Capture):
453 * The error INVALID_OPERATION is also generated by DrawElements,
454 * DrawElementsInstanced, and DrawRangeElements while transform feedback
455 * is active and not paused, regardless of mode.
457 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
458 _mesa_error(ctx
, GL_INVALID_OPERATION
,
459 "glDrawElements(transform feedback active)");
465 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
469 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
474 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
478 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
481 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
484 /* Vertex buffer object tests */
485 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
486 /* use indices in the buffer object */
487 /* make sure count doesn't go outside buffer bounds */
488 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
489 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
494 /* not using a VBO */
499 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
507 * Called from the tnl module to error check the function parameters and
508 * verify that we really can draw something.
509 * \return GL_TRUE if OK to render, GL_FALSE if error found
512 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
513 GLenum mode
, GLint start
, GLsizei count
)
515 struct gl_transform_feedback_object
*xfb_obj
516 = ctx
->TransformFeedback
.CurrentObject
;
517 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
518 FLUSH_CURRENT(ctx
, 0);
522 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
526 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
530 if (!check_valid_to_render(ctx
, "glDrawArrays"))
533 if (ctx
->Const
.CheckArrayBounds
) {
534 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
538 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
539 * Primitive Capture):
541 * The error INVALID_OPERATION is generated by DrawArrays and
542 * DrawArraysInstanced if recording the vertices of a primitive to the
543 * buffer objects being used for transform feedback purposes would result
544 * in either exceeding the limits of any buffer object’s size, or in
545 * exceeding the end position offset + size − 1, as set by
548 * This is in contrast to the behaviour of desktop GL, where the extra
549 * primitives are silently dropped from the transform feedback buffer.
551 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
552 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
553 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
554 _mesa_error(ctx
, GL_INVALID_OPERATION
,
555 "glDrawArrays(exceeds transform feedback size)");
558 xfb_obj
->GlesRemainingPrims
-= prim_count
;
566 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
567 GLsizei count
, GLsizei numInstances
)
569 struct gl_transform_feedback_object
*xfb_obj
570 = ctx
->TransformFeedback
.CurrentObject
;
571 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
572 FLUSH_CURRENT(ctx
, 0);
576 _mesa_error(ctx
, GL_INVALID_VALUE
,
577 "glDrawArraysInstanced(count=%d)", count
);
582 _mesa_error(ctx
, GL_INVALID_VALUE
,
583 "glDrawArraysInstanced(start=%d)", first
);
587 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
591 if (numInstances
<= 0) {
592 if (numInstances
< 0)
593 _mesa_error(ctx
, GL_INVALID_VALUE
,
594 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
598 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
601 if (ctx
->Const
.CheckArrayBounds
) {
602 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
606 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
607 * Primitive Capture):
609 * The error INVALID_OPERATION is generated by DrawArrays and
610 * DrawArraysInstanced if recording the vertices of a primitive to the
611 * buffer objects being used for transform feedback purposes would result
612 * in either exceeding the limits of any buffer object’s size, or in
613 * exceeding the end position offset + size − 1, as set by
616 * This is in contrast to the behaviour of desktop GL, where the extra
617 * primitives are silently dropped from the transform feedback buffer.
619 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
621 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
622 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
624 "glDrawArraysInstanced(exceeds transform feedback size)");
627 xfb_obj
->GlesRemainingPrims
-= prim_count
;
635 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
636 GLenum mode
, GLsizei count
, GLenum type
,
637 const GLvoid
*indices
, GLsizei numInstances
,
640 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
641 FLUSH_CURRENT(ctx
, 0);
643 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
644 * Primitive Capture):
646 * The error INVALID_OPERATION is also generated by DrawElements,
647 * DrawElementsInstanced, and DrawRangeElements while transform feedback
648 * is active and not paused, regardless of mode.
650 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
651 _mesa_error(ctx
, GL_INVALID_OPERATION
,
652 "glDrawElements(transform feedback active)");
658 _mesa_error(ctx
, GL_INVALID_VALUE
,
659 "glDrawElementsInstanced(count=%d)", count
);
663 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
667 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
670 if (numInstances
<= 0) {
671 if (numInstances
< 0)
672 _mesa_error(ctx
, GL_INVALID_VALUE
,
673 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
677 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
680 /* Vertex buffer object tests */
681 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
682 /* use indices in the buffer object */
683 /* make sure count doesn't go outside buffer bounds */
684 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
686 "glDrawElementsInstanced index out of buffer bounds");
691 /* not using a VBO */
696 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
704 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
706 struct gl_transform_feedback_object
*obj
,
708 GLsizei numInstances
)
710 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
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*")) {