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.
26 #include "api_validate.h"
27 #include "bufferobj.h"
30 #include "mfeatures.h"
37 * \return number of bytes in array [count] of type.
40 index_bytes(GLenum type
, GLsizei count
)
42 if (type
== GL_UNSIGNED_INT
) {
43 return count
* sizeof(GLuint
);
45 else if (type
== GL_UNSIGNED_BYTE
) {
46 return count
* sizeof(GLubyte
);
49 ASSERT(type
== GL_UNSIGNED_SHORT
);
50 return count
* sizeof(GLushort
);
56 * Find the max index in the given element/index buffer
59 _mesa_max_buffer_index(struct gl_context
*ctx
, GLuint count
, GLenum type
,
61 struct gl_buffer_object
*elementBuf
)
63 const GLubyte
*map
= NULL
;
67 if (_mesa_is_bufferobj(elementBuf
)) {
68 /* elements are in a user-defined buffer object. need to map it */
69 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, elementBuf
->Size
,
70 GL_MAP_READ_BIT
, elementBuf
);
71 /* Actual address is the sum of pointers */
72 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
75 if (type
== GL_UNSIGNED_INT
) {
76 for (i
= 0; i
< count
; i
++)
77 if (((GLuint
*) indices
)[i
] > max
)
78 max
= ((GLuint
*) indices
)[i
];
80 else if (type
== GL_UNSIGNED_SHORT
) {
81 for (i
= 0; i
< count
; i
++)
82 if (((GLushort
*) indices
)[i
] > max
)
83 max
= ((GLushort
*) indices
)[i
];
86 ASSERT(type
== GL_UNSIGNED_BYTE
);
87 for (i
= 0; i
< count
; i
++)
88 if (((GLubyte
*) indices
)[i
] > max
)
89 max
= ((GLubyte
*) indices
)[i
];
93 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
);
101 * Check if OK to draw arrays/elements.
104 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
106 if (!_mesa_valid_to_render(ctx
, function
)) {
113 /* For ES2, we can draw if any vertex array is enabled (and we
114 * should always have a vertex program/shader). */
115 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
)
132 const struct gl_shader_program
*vsProg
=
133 ctx
->Shader
.CurrentVertexProgram
;
134 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
135 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
136 if (haveVertexShader
|| haveVertexProgram
) {
137 /* Draw regardless of whether or not we have any vertex arrays.
138 * (Ex: could draw a point using a constant vertex pos)
143 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
146 return (ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
147 ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
162 * Do bounds checking on array element indexes. Check that the vertices
163 * pointed to by the indices don't lie outside buffer object bounds.
164 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
167 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
168 const GLvoid
*indices
, GLint basevertex
)
170 struct _mesa_prim prim
;
171 struct _mesa_index_buffer ib
;
174 /* Only the X Server needs to do this -- otherwise, accessing outside
175 * array/BO bounds allows application termination.
177 if (!ctx
->Const
.CheckArrayBounds
)
180 memset(&prim
, 0, sizeof(prim
));
183 memset(&ib
, 0, sizeof(ib
));
186 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
188 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
190 if ((int)(min
+ basevertex
) < 0 ||
191 max
+ basevertex
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
192 /* the max element is out of bounds of one or more enabled arrays */
193 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
194 max
, ctx
->Array
.ArrayObj
->_MaxElement
);
203 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
204 * etc? The set of legal values depends on whether geometry shaders/programs
208 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
210 if (ctx
->Extensions
.ARB_geometry_shader4
&&
211 mode
> GL_TRIANGLE_STRIP_ADJACENCY_ARB
) {
212 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
215 else if (mode
> GL_POLYGON
) {
216 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
220 /* From the GL_EXT_transform_feedback spec:
222 * "The error INVALID_OPERATION is generated if Begin, or any command
223 * that performs an explicit Begin, is called when:
225 * * a geometry shader is not active and <mode> does not match the
226 * allowed begin modes for the current transform feedback state as
227 * given by table X.1.
229 * * a geometry shader is active and the output primitive type of the
230 * geometry shader does not match the allowed begin modes for the
231 * current transform feedback state as given by table X.1.
234 if (ctx
->TransformFeedback
.CurrentObject
->Active
&&
235 !ctx
->TransformFeedback
.CurrentObject
->Paused
) {
236 GLboolean pass
= GL_TRUE
;
240 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
245 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
248 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
252 _mesa_error(ctx
, GL_INVALID_OPERATION
,
253 "%s(mode=%s vs transform feedback %s)",
255 _mesa_lookup_prim_by_nr(mode
),
256 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
266 * Error checking for glDrawElements(). Includes parameter checking
267 * and VBO bounds checking.
268 * \return GL_TRUE if OK to render, GL_FALSE if error found
271 _mesa_validate_DrawElements(struct gl_context
*ctx
,
272 GLenum mode
, GLsizei count
, GLenum type
,
273 const GLvoid
*indices
, GLint basevertex
)
275 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
276 FLUSH_CURRENT(ctx
, 0);
280 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
284 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
288 if (type
!= GL_UNSIGNED_INT
&&
289 type
!= GL_UNSIGNED_BYTE
&&
290 type
!= GL_UNSIGNED_SHORT
)
292 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(type)" );
296 if (!check_valid_to_render(ctx
, "glDrawElements"))
299 /* Vertex buffer object tests */
300 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
301 /* use indices in the buffer object */
302 /* make sure count doesn't go outside buffer bounds */
303 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
304 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
309 /* not using a VBO */
314 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
322 * Error checking for glMultiDrawElements(). Includes parameter checking
323 * and VBO bounds checking.
324 * \return GL_TRUE if OK to render, GL_FALSE if error found
327 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
328 GLenum mode
, const GLsizei
*count
,
329 GLenum type
, const GLvoid
* const *indices
,
330 GLuint primcount
, const GLint
*basevertex
)
334 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
335 FLUSH_CURRENT(ctx
, 0);
337 for (i
= 0; i
< primcount
; i
++) {
340 _mesa_error(ctx
, GL_INVALID_VALUE
,
341 "glMultiDrawElements(count)" );
346 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
350 if (type
!= GL_UNSIGNED_INT
&&
351 type
!= GL_UNSIGNED_BYTE
&&
352 type
!= GL_UNSIGNED_SHORT
)
354 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMultiDrawElements(type)" );
358 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
361 /* Vertex buffer object tests */
362 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
363 /* use indices in the buffer object */
364 /* make sure count doesn't go outside buffer bounds */
365 for (i
= 0; i
< primcount
; i
++) {
366 if (index_bytes(type
, count
[i
]) >
367 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
369 "glMultiDrawElements index out of buffer bounds");
375 /* not using a VBO */
376 for (i
= 0; i
< primcount
; i
++) {
382 for (i
= 0; i
< primcount
; i
++) {
383 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
384 basevertex
? basevertex
[i
] : 0))
393 * Error checking for glDrawRangeElements(). Includes parameter checking
394 * and VBO bounds checking.
395 * \return GL_TRUE if OK to render, GL_FALSE if error found
398 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
399 GLuint start
, GLuint end
,
400 GLsizei count
, GLenum type
,
401 const GLvoid
*indices
, GLint basevertex
)
403 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
404 FLUSH_CURRENT(ctx
, 0);
408 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
412 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
417 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
421 if (type
!= GL_UNSIGNED_INT
&&
422 type
!= GL_UNSIGNED_BYTE
&&
423 type
!= GL_UNSIGNED_SHORT
) {
424 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(type)" );
428 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
431 /* Vertex buffer object tests */
432 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
433 /* use indices in the buffer object */
434 /* make sure count doesn't go outside buffer bounds */
435 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
436 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
441 /* not using a VBO */
446 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
454 * Called from the tnl module to error check the function parameters and
455 * verify that we really can draw something.
456 * \return GL_TRUE if OK to render, GL_FALSE if error found
459 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
460 GLenum mode
, GLint start
, GLsizei count
)
462 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
463 FLUSH_CURRENT(ctx
, 0);
467 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
471 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
475 if (!check_valid_to_render(ctx
, "glDrawArrays"))
478 if (ctx
->Const
.CheckArrayBounds
) {
479 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
488 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
489 GLsizei count
, GLsizei numInstances
)
491 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
492 FLUSH_CURRENT(ctx
, 0);
496 _mesa_error(ctx
, GL_INVALID_VALUE
,
497 "glDrawArraysInstanced(count=%d)", count
);
502 _mesa_error(ctx
, GL_INVALID_VALUE
,
503 "glDrawArraysInstanced(start=%d)", first
);
507 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
511 if (numInstances
<= 0) {
512 if (numInstances
< 0)
513 _mesa_error(ctx
, GL_INVALID_VALUE
,
514 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
518 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
521 if (ctx
->Const
.CheckArrayBounds
) {
522 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
531 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
532 GLenum mode
, GLsizei count
, GLenum type
,
533 const GLvoid
*indices
, GLsizei numInstances
,
536 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
537 FLUSH_CURRENT(ctx
, 0);
541 _mesa_error(ctx
, GL_INVALID_VALUE
,
542 "glDrawElementsInstanced(count=%d)", count
);
546 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
550 if (type
!= GL_UNSIGNED_INT
&&
551 type
!= GL_UNSIGNED_BYTE
&&
552 type
!= GL_UNSIGNED_SHORT
) {
553 _mesa_error(ctx
, GL_INVALID_ENUM
,
554 "glDrawElementsInstanced(type=0x%x)", type
);
558 if (numInstances
<= 0) {
559 if (numInstances
< 0)
560 _mesa_error(ctx
, GL_INVALID_VALUE
,
561 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
565 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
568 /* Vertex buffer object tests */
569 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
570 /* use indices in the buffer object */
571 /* make sure count doesn't go outside buffer bounds */
572 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
574 "glDrawElementsInstanced index out of buffer bounds");
579 /* not using a VBO */
584 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
591 #if FEATURE_EXT_transform_feedback
594 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
596 struct gl_transform_feedback_object
*obj
)
598 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
599 FLUSH_CURRENT(ctx
, 0);
601 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback")) {
606 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback(name)");
610 if (!obj
->EndedAnytime
) {
611 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback");
615 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback")) {