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 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"
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
205 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
215 case GL_TRIANGLE_STRIP
:
216 case GL_TRIANGLE_FAN
:
222 valid_enum
= (ctx
->API
== API_OPENGL_COMPAT
);
224 case GL_LINES_ADJACENCY
:
225 case GL_LINE_STRIP_ADJACENCY
:
226 case GL_TRIANGLES_ADJACENCY
:
227 case GL_TRIANGLE_STRIP_ADJACENCY
:
228 valid_enum
= _mesa_is_desktop_gl(ctx
)
229 && ctx
->Extensions
.ARB_geometry_shader4
;
237 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
241 /* From the GL_EXT_transform_feedback spec:
243 * "The error INVALID_OPERATION is generated if Begin, or any command
244 * that performs an explicit Begin, is called when:
246 * * a geometry shader is not active and <mode> does not match the
247 * allowed begin modes for the current transform feedback state as
248 * given by table X.1.
250 * * a geometry shader is active and the output primitive type of the
251 * geometry shader does not match the allowed begin modes for the
252 * current transform feedback state as given by table X.1.
255 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
256 GLboolean pass
= GL_TRUE
;
260 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
265 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
268 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
272 _mesa_error(ctx
, GL_INVALID_OPERATION
,
273 "%s(mode=%s vs transform feedback %s)",
275 _mesa_lookup_prim_by_nr(mode
),
276 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
285 * Verify that the element type is valid.
287 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
290 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
293 case GL_UNSIGNED_BYTE
:
294 case GL_UNSIGNED_SHORT
:
295 case GL_UNSIGNED_INT
:
299 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
300 _mesa_lookup_enum_by_nr(type
));
306 * Error checking for glDrawElements(). Includes parameter checking
307 * and VBO bounds checking.
308 * \return GL_TRUE if OK to render, GL_FALSE if error found
311 _mesa_validate_DrawElements(struct gl_context
*ctx
,
312 GLenum mode
, GLsizei count
, GLenum type
,
313 const GLvoid
*indices
, GLint basevertex
)
315 FLUSH_CURRENT(ctx
, 0);
317 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
318 * Primitive Capture):
320 * The error INVALID_OPERATION is also generated by DrawElements,
321 * DrawElementsInstanced, and DrawRangeElements while transform feedback
322 * is active and not paused, regardless of mode.
324 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
325 _mesa_error(ctx
, GL_INVALID_OPERATION
,
326 "glDrawElements(transform feedback active)");
332 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
336 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
340 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
343 if (!check_valid_to_render(ctx
, "glDrawElements"))
346 /* Vertex buffer object tests */
347 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
348 /* use indices in the buffer object */
349 /* make sure count doesn't go outside buffer bounds */
350 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
351 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
356 /* not using a VBO */
361 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
369 * Error checking for glMultiDrawElements(). Includes parameter checking
370 * and VBO bounds checking.
371 * \return GL_TRUE if OK to render, GL_FALSE if error found
374 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
375 GLenum mode
, const GLsizei
*count
,
376 GLenum type
, const GLvoid
* const *indices
,
377 GLuint primcount
, const GLint
*basevertex
)
381 FLUSH_CURRENT(ctx
, 0);
383 for (i
= 0; i
< primcount
; i
++) {
386 _mesa_error(ctx
, GL_INVALID_VALUE
,
387 "glMultiDrawElements(count)" );
392 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
396 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
399 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
402 /* Vertex buffer object tests */
403 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
404 /* use indices in the buffer object */
405 /* make sure count doesn't go outside buffer bounds */
406 for (i
= 0; i
< primcount
; i
++) {
407 if (index_bytes(type
, count
[i
]) >
408 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
410 "glMultiDrawElements index out of buffer bounds");
416 /* not using a VBO */
417 for (i
= 0; i
< primcount
; i
++) {
423 for (i
= 0; i
< primcount
; i
++) {
424 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
425 basevertex
? basevertex
[i
] : 0))
434 * Error checking for glDrawRangeElements(). Includes parameter checking
435 * and VBO bounds checking.
436 * \return GL_TRUE if OK to render, GL_FALSE if error found
439 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
440 GLuint start
, GLuint end
,
441 GLsizei count
, GLenum type
,
442 const GLvoid
*indices
, GLint basevertex
)
444 FLUSH_CURRENT(ctx
, 0);
446 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
447 * Primitive Capture):
449 * The error INVALID_OPERATION is also generated by DrawElements,
450 * DrawElementsInstanced, and DrawRangeElements while transform feedback
451 * is active and not paused, regardless of mode.
453 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
454 _mesa_error(ctx
, GL_INVALID_OPERATION
,
455 "glDrawElements(transform feedback active)");
461 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
465 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
470 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
474 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
477 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
480 /* Vertex buffer object tests */
481 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
482 /* use indices in the buffer object */
483 /* make sure count doesn't go outside buffer bounds */
484 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
485 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
490 /* not using a VBO */
495 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
503 * Called from the tnl module to error check the function parameters and
504 * verify that we really can draw something.
505 * \return GL_TRUE if OK to render, GL_FALSE if error found
508 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
509 GLenum mode
, GLint start
, GLsizei count
)
511 struct gl_transform_feedback_object
*xfb_obj
512 = ctx
->TransformFeedback
.CurrentObject
;
513 FLUSH_CURRENT(ctx
, 0);
517 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
521 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
525 if (!check_valid_to_render(ctx
, "glDrawArrays"))
528 if (ctx
->Const
.CheckArrayBounds
) {
529 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
533 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
534 * Primitive Capture):
536 * The error INVALID_OPERATION is generated by DrawArrays and
537 * DrawArraysInstanced if recording the vertices of a primitive to the
538 * buffer objects being used for transform feedback purposes would result
539 * in either exceeding the limits of any buffer object’s size, or in
540 * exceeding the end position offset + size − 1, as set by
543 * This is in contrast to the behaviour of desktop GL, where the extra
544 * primitives are silently dropped from the transform feedback buffer.
546 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
547 size_t prim_count
= vbo_count_tessellated_primitives(mode
, count
, 1);
548 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
549 _mesa_error(ctx
, GL_INVALID_OPERATION
,
550 "glDrawArrays(exceeds transform feedback size)");
553 xfb_obj
->GlesRemainingPrims
-= prim_count
;
561 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
562 GLsizei count
, GLsizei numInstances
)
564 struct gl_transform_feedback_object
*xfb_obj
565 = ctx
->TransformFeedback
.CurrentObject
;
566 FLUSH_CURRENT(ctx
, 0);
570 _mesa_error(ctx
, GL_INVALID_VALUE
,
571 "glDrawArraysInstanced(count=%d)", count
);
576 _mesa_error(ctx
, GL_INVALID_VALUE
,
577 "glDrawArraysInstanced(start=%d)", first
);
581 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
585 if (numInstances
<= 0) {
586 if (numInstances
< 0)
587 _mesa_error(ctx
, GL_INVALID_VALUE
,
588 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
592 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
595 if (ctx
->Const
.CheckArrayBounds
) {
596 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
600 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
601 * Primitive Capture):
603 * The error INVALID_OPERATION is generated by DrawArrays and
604 * DrawArraysInstanced if recording the vertices of a primitive to the
605 * buffer objects being used for transform feedback purposes would result
606 * in either exceeding the limits of any buffer object’s size, or in
607 * exceeding the end position offset + size − 1, as set by
610 * This is in contrast to the behaviour of desktop GL, where the extra
611 * primitives are silently dropped from the transform feedback buffer.
613 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
615 = vbo_count_tessellated_primitives(mode
, count
, numInstances
);
616 if (xfb_obj
->GlesRemainingPrims
< prim_count
) {
617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
618 "glDrawArraysInstanced(exceeds transform feedback size)");
621 xfb_obj
->GlesRemainingPrims
-= prim_count
;
629 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
630 GLenum mode
, GLsizei count
, GLenum type
,
631 const GLvoid
*indices
, GLsizei numInstances
,
634 FLUSH_CURRENT(ctx
, 0);
636 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
637 * Primitive Capture):
639 * The error INVALID_OPERATION is also generated by DrawElements,
640 * DrawElementsInstanced, and DrawRangeElements while transform feedback
641 * is active and not paused, regardless of mode.
643 if (_mesa_is_gles3(ctx
) && _mesa_is_xfb_active_and_unpaused(ctx
)) {
644 _mesa_error(ctx
, GL_INVALID_OPERATION
,
645 "glDrawElements(transform feedback active)");
651 _mesa_error(ctx
, GL_INVALID_VALUE
,
652 "glDrawElementsInstanced(count=%d)", count
);
656 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
660 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
663 if (numInstances
<= 0) {
664 if (numInstances
< 0)
665 _mesa_error(ctx
, GL_INVALID_VALUE
,
666 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
670 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
673 /* Vertex buffer object tests */
674 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
675 /* use indices in the buffer object */
676 /* make sure count doesn't go outside buffer bounds */
677 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
679 "glDrawElementsInstanced index out of buffer bounds");
684 /* not using a VBO */
689 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
697 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
699 struct gl_transform_feedback_object
*obj
,
701 GLsizei numInstances
)
703 FLUSH_CURRENT(ctx
, 0);
705 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
710 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
714 if (!obj
->EndedAnytime
) {
715 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
719 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
720 _mesa_error(ctx
, GL_INVALID_VALUE
,
721 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
725 if (numInstances
<= 0) {
726 if (numInstances
< 0)
727 _mesa_error(ctx
, GL_INVALID_VALUE
,
728 "glDrawTransformFeedback*Instanced(numInstances=%d)",
733 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {