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 * \return number of bytes in array [count] of type.
42 index_bytes(GLenum type
, GLsizei count
)
44 if (type
== GL_UNSIGNED_INT
) {
45 return count
* sizeof(GLuint
);
47 else if (type
== GL_UNSIGNED_BYTE
) {
48 return count
* sizeof(GLubyte
);
51 ASSERT(type
== GL_UNSIGNED_SHORT
);
52 return count
* sizeof(GLushort
);
58 * Find the max index in the given element/index buffer
61 _mesa_max_buffer_index(struct gl_context
*ctx
, GLuint count
, GLenum type
,
63 struct gl_buffer_object
*elementBuf
)
65 const GLubyte
*map
= NULL
;
69 if (_mesa_is_bufferobj(elementBuf
)) {
70 /* elements are in a user-defined buffer object. need to map it */
71 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, elementBuf
->Size
,
72 GL_MAP_READ_BIT
, elementBuf
);
73 /* Actual address is the sum of pointers */
74 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
77 if (type
== GL_UNSIGNED_INT
) {
78 for (i
= 0; i
< count
; i
++)
79 if (((GLuint
*) indices
)[i
] > max
)
80 max
= ((GLuint
*) indices
)[i
];
82 else if (type
== GL_UNSIGNED_SHORT
) {
83 for (i
= 0; i
< count
; i
++)
84 if (((GLushort
*) indices
)[i
] > max
)
85 max
= ((GLushort
*) indices
)[i
];
88 ASSERT(type
== GL_UNSIGNED_BYTE
);
89 for (i
= 0; i
< count
; i
++)
90 if (((GLubyte
*) indices
)[i
] > max
)
91 max
= ((GLubyte
*) indices
)[i
];
95 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
);
103 * Check if OK to draw arrays/elements.
106 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
108 if (!_mesa_valid_to_render(ctx
, function
)) {
114 /* For ES2, we can draw if any vertex array is enabled (and we
115 * should always have a vertex program/shader). */
116 if (ctx
->Array
.ArrayObj
->_Enabled
== 0x0 || !ctx
->VertexProgram
._Current
)
121 /* For OpenGL ES, only draw if we have vertex positions
123 if (!ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
127 case API_OPENGL_COMPAT
:
128 case API_OPENGL_CORE
:
130 const struct gl_shader_program
*vsProg
=
131 ctx
->Shader
.CurrentVertexProgram
;
132 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
133 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
134 if (haveVertexShader
|| haveVertexProgram
) {
135 /* Draw regardless of whether or not we have any vertex arrays.
136 * (Ex: could draw a point using a constant vertex pos)
141 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
144 return (ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
145 ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
151 assert(!"Invalid API value in check_valid_to_render()");
159 * Do bounds checking on array element indexes. Check that the vertices
160 * pointed to by the indices don't lie outside buffer object bounds.
161 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
164 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
165 const GLvoid
*indices
, GLint basevertex
)
167 struct _mesa_prim prim
;
168 struct _mesa_index_buffer ib
;
171 /* Only the X Server needs to do this -- otherwise, accessing outside
172 * array/BO bounds allows application termination.
174 if (!ctx
->Const
.CheckArrayBounds
)
177 memset(&prim
, 0, sizeof(prim
));
180 memset(&ib
, 0, sizeof(ib
));
183 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
185 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
187 if ((int)(min
+ basevertex
) < 0 ||
188 max
+ basevertex
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
189 /* the max element is out of bounds of one or more enabled arrays */
190 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
191 max
, ctx
->Array
.ArrayObj
->_MaxElement
);
200 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
201 * etc? The set of legal values depends on whether geometry shaders/programs
203 * Note: This may be called during display list compilation.
206 _mesa_is_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
)
214 case GL_TRIANGLE_STRIP
:
215 case GL_TRIANGLE_FAN
:
220 return (ctx
->API
== API_OPENGL_COMPAT
);
221 case GL_LINES_ADJACENCY
:
222 case GL_LINE_STRIP_ADJACENCY
:
223 case GL_TRIANGLES_ADJACENCY
:
224 case GL_TRIANGLE_STRIP_ADJACENCY
:
225 return _mesa_has_geometry_shaders(ctx
);
233 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
234 * etc? Also, do additional checking related to transformation feedback.
235 * Note: this function cannot be called during glNewList(GL_COMPILE) because
236 * this code depends on current transform feedback state.
239 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
241 bool valid_enum
= _mesa_is_valid_prim_mode(ctx
, mode
);
244 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
248 /* From the ARB_geometry_shader4 spec:
250 * The error INVALID_OPERATION is generated if Begin, or any command that
251 * implicitly calls Begin, is called when a geometry shader is active and:
253 * * the input primitive type of the current geometry shader is
254 * POINTS and <mode> is not POINTS,
256 * * the input primitive type of the current geometry shader is
257 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
259 * * the input primitive type of the current geometry shader is
260 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
263 * * the input primitive type of the current geometry shader is
264 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
265 * LINE_STRIP_ADJACENCY_ARB, or
267 * * the input primitive type of the current geometry shader is
268 * TRIANGLES_ADJACENCY_ARB and <mode> is not
269 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
272 if (ctx
->Shader
.CurrentGeometryProgram
) {
273 const GLenum geom_mode
=
274 ctx
->Shader
.CurrentGeometryProgram
->Geom
.InputType
;
277 valid_enum
= (geom_mode
== GL_POINTS
);
282 valid_enum
= (geom_mode
== GL_LINES
);
285 case GL_TRIANGLE_STRIP
:
286 case GL_TRIANGLE_FAN
:
287 valid_enum
= (geom_mode
== GL_TRIANGLES
);
294 case GL_LINES_ADJACENCY
:
295 case GL_LINE_STRIP_ADJACENCY
:
296 valid_enum
= (geom_mode
== GL_LINES_ADJACENCY
);
298 case GL_TRIANGLES_ADJACENCY
:
299 case GL_TRIANGLE_STRIP_ADJACENCY
:
300 valid_enum
= (geom_mode
== GL_TRIANGLES_ADJACENCY
);
307 _mesa_error(ctx
, GL_INVALID_OPERATION
,
308 "%s(mode=%s vs geometry shader input %s)",
310 _mesa_lookup_prim_by_nr(mode
),
311 _mesa_lookup_prim_by_nr(geom_mode
));
316 /* From the GL_EXT_transform_feedback spec:
318 * "The error INVALID_OPERATION is generated if Begin, or any command
319 * that performs an explicit Begin, is called when:
321 * * a geometry shader is not active and <mode> does not match the
322 * allowed begin modes for the current transform feedback state as
323 * given by table X.1.
325 * * a geometry shader is active and the output primitive type of the
326 * geometry shader does not match the allowed begin modes for the
327 * current transform feedback state as given by table X.1.
330 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
331 GLboolean pass
= GL_TRUE
;
335 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
340 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
343 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
347 _mesa_error(ctx
, GL_INVALID_OPERATION
,
348 "%s(mode=%s vs transform feedback %s)",
350 _mesa_lookup_prim_by_nr(mode
),
351 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
360 * Verify that the element type is valid.
362 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
365 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
368 case GL_UNSIGNED_BYTE
:
369 case GL_UNSIGNED_SHORT
:
370 case GL_UNSIGNED_INT
:
374 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
375 _mesa_lookup_enum_by_nr(type
));
381 * Error checking for glDrawElements(). Includes parameter checking
382 * and VBO bounds checking.
383 * \return GL_TRUE if OK to render, GL_FALSE if error found
386 _mesa_validate_DrawElements(struct gl_context
*ctx
,
387 GLenum mode
, GLsizei count
, GLenum type
,
388 const GLvoid
*indices
, GLint basevertex
)
390 FLUSH_CURRENT(ctx
, 0);
392 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
393 * Primitive Capture):
395 * The error INVALID_OPERATION is also generated by DrawElements,
396 * DrawElementsInstanced, and DrawRangeElements while transform feedback
397 * is active and not paused, regardless of mode.
399 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
401 "glDrawElements(transform feedback active)");
407 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
411 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
415 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
418 if (!check_valid_to_render(ctx
, "glDrawElements"))
421 /* Vertex buffer object tests */
422 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
423 /* use indices in the buffer object */
424 /* make sure count doesn't go outside buffer bounds */
425 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
426 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
431 /* not using a VBO */
436 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
444 * Error checking for glMultiDrawElements(). Includes parameter checking
445 * and VBO bounds checking.
446 * \return GL_TRUE if OK to render, GL_FALSE if error found
449 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
450 GLenum mode
, const GLsizei
*count
,
451 GLenum type
, const GLvoid
* const *indices
,
452 GLuint primcount
, const GLint
*basevertex
)
456 FLUSH_CURRENT(ctx
, 0);
458 for (i
= 0; i
< primcount
; i
++) {
461 _mesa_error(ctx
, GL_INVALID_VALUE
,
462 "glMultiDrawElements(count)" );
467 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
471 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
474 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
477 /* Vertex buffer object tests */
478 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
479 /* use indices in the buffer object */
480 /* make sure count doesn't go outside buffer bounds */
481 for (i
= 0; i
< primcount
; i
++) {
482 if (index_bytes(type
, count
[i
]) >
483 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
485 "glMultiDrawElements index out of buffer bounds");
491 /* not using a VBO */
492 for (i
= 0; i
< primcount
; i
++) {
498 for (i
= 0; i
< primcount
; i
++) {
499 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
500 basevertex
? basevertex
[i
] : 0))
509 * Error checking for glDrawRangeElements(). Includes parameter checking
510 * and VBO bounds checking.
511 * \return GL_TRUE if OK to render, GL_FALSE if error found
514 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
515 GLuint start
, GLuint end
,
516 GLsizei count
, GLenum type
,
517 const GLvoid
*indices
, GLint basevertex
)
519 FLUSH_CURRENT(ctx
, 0);
521 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
522 * Primitive Capture):
524 * The error INVALID_OPERATION is also generated by DrawElements,
525 * DrawElementsInstanced, and DrawRangeElements while transform feedback
526 * is active and not paused, regardless of mode.
528 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
529 _mesa_error(ctx
, GL_INVALID_OPERATION
,
530 "glDrawElements(transform feedback active)");
536 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
540 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
545 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
549 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
552 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
555 /* Vertex buffer object tests */
556 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
557 /* use indices in the buffer object */
558 /* make sure count doesn't go outside buffer bounds */
559 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
560 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
565 /* not using a VBO */
570 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
578 * Called from the tnl module to error check the function parameters and
579 * verify that we really can draw something.
580 * \return GL_TRUE if OK to render, GL_FALSE if error found
583 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
584 GLenum mode
, GLint start
, GLsizei count
)
586 struct gl_transform_feedback_object
*xfb_obj
587 = ctx
->TransformFeedback
.CurrentObject
;
588 FLUSH_CURRENT(ctx
, 0);
592 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
596 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
600 if (!check_valid_to_render(ctx
, "glDrawArrays"))
603 if (ctx
->Const
.CheckArrayBounds
) {
604 if (start
+ 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
)) {
622 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
623 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
624 _mesa_error(ctx
, GL_INVALID_OPERATION
,
625 "glDrawArrays(exceeds transform feedback size)");
628 xfb_obj
->GlesRemainingPrims
-= prim_count
;
636 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
637 GLsizei count
, GLsizei numInstances
)
639 struct gl_transform_feedback_object
*xfb_obj
640 = ctx
->TransformFeedback
.CurrentObject
;
641 FLUSH_CURRENT(ctx
, 0);
645 _mesa_error(ctx
, GL_INVALID_VALUE
,
646 "glDrawArraysInstanced(count=%d)", count
);
651 _mesa_error(ctx
, GL_INVALID_VALUE
,
652 "glDrawArraysInstanced(start=%d)", first
);
656 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
660 if (numInstances
<= 0) {
661 if (numInstances
< 0)
662 _mesa_error(ctx
, GL_INVALID_VALUE
,
663 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
667 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
670 if (ctx
->Const
.CheckArrayBounds
) {
671 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
675 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
676 * Primitive Capture):
678 * The error INVALID_OPERATION is generated by DrawArrays and
679 * DrawArraysInstanced if recording the vertices of a primitive to the
680 * buffer objects being used for transform feedback purposes would result
681 * in either exceeding the limits of any buffer object’s size, or in
682 * exceeding the end position offset + size − 1, as set by
685 * This is in contrast to the behaviour of desktop GL, where the extra
686 * primitives are silently dropped from the transform feedback buffer.
688 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
690 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
691 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
692 _mesa_error(ctx
, GL_INVALID_OPERATION
,
693 "glDrawArraysInstanced(exceeds transform feedback size)");
696 xfb_obj
->GlesRemainingPrims
-= prim_count
;
704 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
705 GLenum mode
, GLsizei count
, GLenum type
,
706 const GLvoid
*indices
, GLsizei numInstances
,
709 FLUSH_CURRENT(ctx
, 0);
711 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
712 * Primitive Capture):
714 * The error INVALID_OPERATION is also generated by DrawElements,
715 * DrawElementsInstanced, and DrawRangeElements while transform feedback
716 * is active and not paused, regardless of mode.
718 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
719 _mesa_error(ctx
, GL_INVALID_OPERATION
,
720 "glDrawElements(transform feedback active)");
726 _mesa_error(ctx
, GL_INVALID_VALUE
,
727 "glDrawElementsInstanced(count=%d)", count
);
731 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
735 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
738 if (numInstances
<= 0) {
739 if (numInstances
< 0)
740 _mesa_error(ctx
, GL_INVALID_VALUE
,
741 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
745 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
748 /* Vertex buffer object tests */
749 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
750 /* use indices in the buffer object */
751 /* make sure count doesn't go outside buffer bounds */
752 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
754 "glDrawElementsInstanced index out of buffer bounds");
759 /* not using a VBO */
764 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
772 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
774 struct gl_transform_feedback_object
*obj
,
776 GLsizei numInstances
)
778 FLUSH_CURRENT(ctx
, 0);
780 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
785 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
789 if (!obj
->EndedAnytime
) {
790 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
794 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
795 _mesa_error(ctx
, GL_INVALID_VALUE
,
796 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
800 if (numInstances
<= 0) {
801 if (numInstances
< 0)
802 _mesa_error(ctx
, GL_INVALID_VALUE
,
803 "glDrawTransformFeedback*Instanced(numInstances=%d)",
808 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {