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"
38 * \return number of bytes in array [count] of type.
41 index_bytes(GLenum type
, GLsizei count
)
43 if (type
== GL_UNSIGNED_INT
) {
44 return count
* sizeof(GLuint
);
46 else if (type
== GL_UNSIGNED_BYTE
) {
47 return count
* sizeof(GLubyte
);
50 ASSERT(type
== GL_UNSIGNED_SHORT
);
51 return count
* sizeof(GLushort
);
57 * Find the max index in the given element/index buffer
60 _mesa_max_buffer_index(struct gl_context
*ctx
, GLuint count
, GLenum type
,
62 struct gl_buffer_object
*elementBuf
)
64 const GLubyte
*map
= NULL
;
68 if (_mesa_is_bufferobj(elementBuf
)) {
69 /* elements are in a user-defined buffer object. need to map it */
70 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, elementBuf
->Size
,
71 GL_MAP_READ_BIT
, elementBuf
);
72 /* Actual address is the sum of pointers */
73 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
76 if (type
== GL_UNSIGNED_INT
) {
77 for (i
= 0; i
< count
; i
++)
78 if (((GLuint
*) indices
)[i
] > max
)
79 max
= ((GLuint
*) indices
)[i
];
81 else if (type
== GL_UNSIGNED_SHORT
) {
82 for (i
= 0; i
< count
; i
++)
83 if (((GLushort
*) indices
)[i
] > max
)
84 max
= ((GLushort
*) indices
)[i
];
87 ASSERT(type
== GL_UNSIGNED_BYTE
);
88 for (i
= 0; i
< count
; i
++)
89 if (((GLubyte
*) indices
)[i
] > max
)
90 max
= ((GLubyte
*) indices
)[i
];
94 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
);
102 * Check if OK to draw arrays/elements.
105 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
107 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
)
123 /* For OpenGL ES, only draw if we have vertex positions
125 if (!ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
132 case API_OPENGL_CORE
:
134 const struct gl_shader_program
*vsProg
=
135 ctx
->Shader
.CurrentVertexProgram
;
136 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
137 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
138 if (haveVertexShader
|| haveVertexProgram
) {
139 /* Draw regardless of whether or not we have any vertex arrays.
140 * (Ex: could draw a point using a constant vertex pos)
145 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
148 return (ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
149 ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
164 * Do bounds checking on array element indexes. Check that the vertices
165 * pointed to by the indices don't lie outside buffer object bounds.
166 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
169 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
170 const GLvoid
*indices
, GLint basevertex
)
172 struct _mesa_prim prim
;
173 struct _mesa_index_buffer ib
;
176 /* Only the X Server needs to do this -- otherwise, accessing outside
177 * array/BO bounds allows application termination.
179 if (!ctx
->Const
.CheckArrayBounds
)
182 memset(&prim
, 0, sizeof(prim
));
185 memset(&ib
, 0, sizeof(ib
));
188 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
190 vbo_get_minmax_indices(ctx
, &prim
, &ib
, &min
, &max
, 1);
192 if ((int)(min
+ basevertex
) < 0 ||
193 max
+ basevertex
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
194 /* the max element is out of bounds of one or more enabled arrays */
195 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
196 max
, ctx
->Array
.ArrayObj
->_MaxElement
);
205 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
206 * etc? The set of legal values depends on whether geometry shaders/programs
210 _mesa_valid_prim_mode(struct gl_context
*ctx
, GLenum mode
, const char *name
)
212 if (ctx
->Extensions
.ARB_geometry_shader4
&&
213 mode
> GL_TRIANGLE_STRIP_ADJACENCY_ARB
) {
214 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
217 else if (mode
> GL_POLYGON
) {
218 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(mode=%x)", name
, mode
);
222 /* From the GL_EXT_transform_feedback spec:
224 * "The error INVALID_OPERATION is generated if Begin, or any command
225 * that performs an explicit Begin, is called when:
227 * * a geometry shader is not active and <mode> does not match the
228 * allowed begin modes for the current transform feedback state as
229 * given by table X.1.
231 * * a geometry shader is active and the output primitive type of the
232 * geometry shader does not match the allowed begin modes for the
233 * current transform feedback state as given by table X.1.
236 if (ctx
->TransformFeedback
.CurrentObject
->Active
&&
237 !ctx
->TransformFeedback
.CurrentObject
->Paused
) {
238 GLboolean pass
= GL_TRUE
;
242 pass
= ctx
->TransformFeedback
.Mode
== GL_POINTS
;
247 pass
= ctx
->TransformFeedback
.Mode
== GL_LINES
;
250 pass
= ctx
->TransformFeedback
.Mode
== GL_TRIANGLES
;
254 _mesa_error(ctx
, GL_INVALID_OPERATION
,
255 "%s(mode=%s vs transform feedback %s)",
257 _mesa_lookup_prim_by_nr(mode
),
258 _mesa_lookup_prim_by_nr(ctx
->TransformFeedback
.Mode
));
267 * Verify that the element type is valid.
269 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
272 valid_elements_type(struct gl_context
*ctx
, GLenum type
, const char *name
)
275 case GL_UNSIGNED_BYTE
:
276 case GL_UNSIGNED_SHORT
:
277 case GL_UNSIGNED_INT
:
281 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)", name
,
282 _mesa_lookup_enum_by_nr(type
));
288 * Error checking for glDrawElements(). Includes parameter checking
289 * and VBO bounds checking.
290 * \return GL_TRUE if OK to render, GL_FALSE if error found
293 _mesa_validate_DrawElements(struct gl_context
*ctx
,
294 GLenum mode
, GLsizei count
, GLenum type
,
295 const GLvoid
*indices
, GLint basevertex
)
297 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
298 FLUSH_CURRENT(ctx
, 0);
302 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
306 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElements")) {
310 if (!valid_elements_type(ctx
, type
, "glDrawElements"))
313 if (!check_valid_to_render(ctx
, "glDrawElements"))
316 /* Vertex buffer object tests */
317 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
318 /* use indices in the buffer object */
319 /* make sure count doesn't go outside buffer bounds */
320 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
321 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
326 /* not using a VBO */
331 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
339 * Error checking for glMultiDrawElements(). Includes parameter checking
340 * and VBO bounds checking.
341 * \return GL_TRUE if OK to render, GL_FALSE if error found
344 _mesa_validate_MultiDrawElements(struct gl_context
*ctx
,
345 GLenum mode
, const GLsizei
*count
,
346 GLenum type
, const GLvoid
* const *indices
,
347 GLuint primcount
, const GLint
*basevertex
)
351 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
352 FLUSH_CURRENT(ctx
, 0);
354 for (i
= 0; i
< primcount
; i
++) {
357 _mesa_error(ctx
, GL_INVALID_VALUE
,
358 "glMultiDrawElements(count)" );
363 if (!_mesa_valid_prim_mode(ctx
, mode
, "glMultiDrawElements")) {
367 if (!valid_elements_type(ctx
, type
, "glMultiDrawElements"))
370 if (!check_valid_to_render(ctx
, "glMultiDrawElements"))
373 /* Vertex buffer object tests */
374 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
375 /* use indices in the buffer object */
376 /* make sure count doesn't go outside buffer bounds */
377 for (i
= 0; i
< primcount
; i
++) {
378 if (index_bytes(type
, count
[i
]) >
379 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
381 "glMultiDrawElements index out of buffer bounds");
387 /* not using a VBO */
388 for (i
= 0; i
< primcount
; i
++) {
394 for (i
= 0; i
< primcount
; i
++) {
395 if (!check_index_bounds(ctx
, count
[i
], type
, indices
[i
],
396 basevertex
? basevertex
[i
] : 0))
405 * Error checking for glDrawRangeElements(). Includes parameter checking
406 * and VBO bounds checking.
407 * \return GL_TRUE if OK to render, GL_FALSE if error found
410 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
411 GLuint start
, GLuint end
,
412 GLsizei count
, GLenum type
,
413 const GLvoid
*indices
, GLint basevertex
)
415 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
416 FLUSH_CURRENT(ctx
, 0);
420 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
424 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawRangeElements")) {
429 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
433 if (!valid_elements_type(ctx
, type
, "glDrawRangeElements"))
436 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
439 /* Vertex buffer object tests */
440 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
441 /* use indices in the buffer object */
442 /* make sure count doesn't go outside buffer bounds */
443 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
444 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
449 /* not using a VBO */
454 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
462 * Called from the tnl module to error check the function parameters and
463 * verify that we really can draw something.
464 * \return GL_TRUE if OK to render, GL_FALSE if error found
467 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
468 GLenum mode
, GLint start
, GLsizei count
)
470 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
471 FLUSH_CURRENT(ctx
, 0);
475 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
479 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArrays")) {
483 if (!check_valid_to_render(ctx
, "glDrawArrays"))
486 if (ctx
->Const
.CheckArrayBounds
) {
487 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
496 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
497 GLsizei count
, GLsizei numInstances
)
499 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
500 FLUSH_CURRENT(ctx
, 0);
504 _mesa_error(ctx
, GL_INVALID_VALUE
,
505 "glDrawArraysInstanced(count=%d)", count
);
510 _mesa_error(ctx
, GL_INVALID_VALUE
,
511 "glDrawArraysInstanced(start=%d)", first
);
515 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawArraysInstanced")) {
519 if (numInstances
<= 0) {
520 if (numInstances
< 0)
521 _mesa_error(ctx
, GL_INVALID_VALUE
,
522 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
526 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
529 if (ctx
->Const
.CheckArrayBounds
) {
530 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
539 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
540 GLenum mode
, GLsizei count
, GLenum type
,
541 const GLvoid
*indices
, GLsizei numInstances
,
544 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
545 FLUSH_CURRENT(ctx
, 0);
549 _mesa_error(ctx
, GL_INVALID_VALUE
,
550 "glDrawElementsInstanced(count=%d)", count
);
554 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawElementsInstanced")) {
558 if (!valid_elements_type(ctx
, type
, "glDrawElementsInstanced"))
561 if (numInstances
<= 0) {
562 if (numInstances
< 0)
563 _mesa_error(ctx
, GL_INVALID_VALUE
,
564 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
568 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
571 /* Vertex buffer object tests */
572 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
573 /* use indices in the buffer object */
574 /* make sure count doesn't go outside buffer bounds */
575 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
577 "glDrawElementsInstanced index out of buffer bounds");
582 /* not using a VBO */
587 if (!check_index_bounds(ctx
, count
, type
, indices
, basevertex
))
594 #if FEATURE_EXT_transform_feedback
597 _mesa_validate_DrawTransformFeedback(struct gl_context
*ctx
,
599 struct gl_transform_feedback_object
*obj
,
601 GLsizei numInstances
)
603 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
604 FLUSH_CURRENT(ctx
, 0);
606 if (!_mesa_valid_prim_mode(ctx
, mode
, "glDrawTransformFeedback*(mode)")) {
611 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawTransformFeedback*(name)");
615 if (!obj
->EndedAnytime
) {
616 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawTransformFeedback*");
620 if (stream
>= ctx
->Const
.MaxVertexStreams
) {
621 _mesa_error(ctx
, GL_INVALID_VALUE
,
622 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
626 if (numInstances
<= 0) {
627 if (numInstances
< 0)
628 _mesa_error(ctx
, GL_INVALID_VALUE
,
629 "glDrawTransformFeedback*Instanced(numInstances=%d)",
634 if (!check_valid_to_render(ctx
, "glDrawTransformFeedback*")) {