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"
36 * \return number of bytes in array [count] of type.
39 index_bytes(GLenum type
, GLsizei count
)
41 if (type
== GL_UNSIGNED_INT
) {
42 return count
* sizeof(GLuint
);
44 else if (type
== GL_UNSIGNED_BYTE
) {
45 return count
* sizeof(GLubyte
);
48 ASSERT(type
== GL_UNSIGNED_SHORT
);
49 return count
* sizeof(GLushort
);
55 * Find the max index in the given element/index buffer
58 _mesa_max_buffer_index(struct gl_context
*ctx
, GLuint count
, GLenum type
,
60 struct gl_buffer_object
*elementBuf
)
62 const GLubyte
*map
= NULL
;
66 if (_mesa_is_bufferobj(elementBuf
)) {
67 /* elements are in a user-defined buffer object. need to map it */
68 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, elementBuf
->Size
,
69 GL_MAP_READ_BIT
, elementBuf
);
70 /* Actual address is the sum of pointers */
71 indices
= (const GLvoid
*) ADD_POINTERS(map
, (const GLubyte
*) indices
);
74 if (type
== GL_UNSIGNED_INT
) {
75 for (i
= 0; i
< count
; i
++)
76 if (((GLuint
*) indices
)[i
] > max
)
77 max
= ((GLuint
*) indices
)[i
];
79 else if (type
== GL_UNSIGNED_SHORT
) {
80 for (i
= 0; i
< count
; i
++)
81 if (((GLushort
*) indices
)[i
] > max
)
82 max
= ((GLushort
*) indices
)[i
];
85 ASSERT(type
== GL_UNSIGNED_BYTE
);
86 for (i
= 0; i
< count
; i
++)
87 if (((GLubyte
*) indices
)[i
] > max
)
88 max
= ((GLubyte
*) indices
)[i
];
92 ctx
->Driver
.UnmapBuffer(ctx
, elementBuf
);
100 * Check if OK to draw arrays/elements.
103 check_valid_to_render(struct gl_context
*ctx
, const char *function
)
105 if (!_mesa_valid_to_render(ctx
, function
)) {
110 const struct gl_shader_program
*vsProg
=
111 ctx
->Shader
.CurrentVertexProgram
;
112 GLboolean haveVertexShader
= (vsProg
&& vsProg
->LinkStatus
);
113 GLboolean haveVertexProgram
= ctx
->VertexProgram
._Enabled
;
114 if (haveVertexShader
|| haveVertexProgram
) {
115 /* Draw regardless of whether or not we have any vertex arrays.
116 * (Ex: could draw a point using a constant vertex pos)
121 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
124 return (ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
||
125 ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
133 * Do bounds checking on array element indexes. Check that the vertices
134 * pointed to by the indices don't lie outside buffer object bounds.
135 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
138 check_index_bounds(struct gl_context
*ctx
, GLsizei count
, GLenum type
,
139 const GLvoid
*indices
)
141 struct _mesa_prim prim
;
142 struct _mesa_index_buffer ib
;
145 /* Only the X Server needs to do this -- otherwise, accessing outside
146 * array/BO bounds allows application termination.
148 if (!ctx
->Const
.CheckArrayBounds
)
151 memset(&prim
, 0, sizeof(prim
));
154 memset(&ib
, 0, sizeof(ib
));
157 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
159 vbo_get_minmax_index(ctx
, &prim
, &ib
, &min
, &max
);
161 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
162 /* the max element is out of bounds of one or more enabled arrays */
163 _mesa_warning(ctx
, "glDrawElements() index=%u is out of bounds (max=%u)",
164 max
, ctx
->Array
.ArrayObj
->_MaxElement
);
173 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
174 * etc? The set of legal values depends on whether geometry shaders/programs
178 _mesa_valid_prim_mode(const struct gl_context
*ctx
, GLenum mode
)
180 if (mode
> GL_POLYGON
) {
190 * Error checking for glDrawElements(). Includes parameter checking
191 * and VBO bounds checking.
192 * \return GL_TRUE if OK to render, GL_FALSE if error found
195 _mesa_validate_DrawElements(struct gl_context
*ctx
,
196 GLenum mode
, GLsizei count
, GLenum type
,
197 const GLvoid
*indices
)
199 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
203 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
207 if (!_mesa_valid_prim_mode(ctx
, mode
)) {
208 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(mode)" );
212 if (type
!= GL_UNSIGNED_INT
&&
213 type
!= GL_UNSIGNED_BYTE
&&
214 type
!= GL_UNSIGNED_SHORT
)
216 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(type)" );
220 if (!check_valid_to_render(ctx
, "glDrawElements"))
223 /* Vertex buffer object tests */
224 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
225 /* use indices in the buffer object */
226 /* make sure count doesn't go outside buffer bounds */
227 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
228 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
233 /* not using a VBO */
238 if (!check_index_bounds(ctx
, count
, type
, indices
))
246 * Error checking for glDrawRangeElements(). Includes parameter checking
247 * and VBO bounds checking.
248 * \return GL_TRUE if OK to render, GL_FALSE if error found
251 _mesa_validate_DrawRangeElements(struct gl_context
*ctx
, GLenum mode
,
252 GLuint start
, GLuint end
,
253 GLsizei count
, GLenum type
,
254 const GLvoid
*indices
)
256 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
260 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
264 if (!_mesa_valid_prim_mode(ctx
, mode
)) {
265 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(mode)" );
270 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
274 if (type
!= GL_UNSIGNED_INT
&&
275 type
!= GL_UNSIGNED_BYTE
&&
276 type
!= GL_UNSIGNED_SHORT
) {
277 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(type)" );
281 if (!check_valid_to_render(ctx
, "glDrawRangeElements"))
284 /* Vertex buffer object tests */
285 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
286 /* use indices in the buffer object */
287 /* make sure count doesn't go outside buffer bounds */
288 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
289 _mesa_warning(ctx
, "glDrawRangeElements index out of buffer bounds");
294 /* not using a VBO */
299 if (!check_index_bounds(ctx
, count
, type
, indices
))
307 * Called from the tnl module to error check the function parameters and
308 * verify that we really can draw something.
309 * \return GL_TRUE if OK to render, GL_FALSE if error found
312 _mesa_validate_DrawArrays(struct gl_context
*ctx
,
313 GLenum mode
, GLint start
, GLsizei count
)
315 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
319 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
323 if (!_mesa_valid_prim_mode(ctx
, mode
)) {
324 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawArrays(mode)" );
328 if (!check_valid_to_render(ctx
, "glDrawArrays"))
331 if (ctx
->Const
.CheckArrayBounds
) {
332 if (start
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
341 _mesa_validate_DrawArraysInstanced(struct gl_context
*ctx
, GLenum mode
, GLint first
,
342 GLsizei count
, GLsizei numInstances
)
344 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
348 _mesa_error(ctx
, GL_INVALID_VALUE
,
349 "glDrawArraysInstanced(count=%d)", count
);
354 _mesa_error(ctx
, GL_INVALID_VALUE
,
355 "glDrawArraysInstanced(start=%d)", first
);
359 if (!_mesa_valid_prim_mode(ctx
, mode
)) {
360 _mesa_error(ctx
, GL_INVALID_ENUM
,
361 "glDrawArraysInstanced(mode=0x%x)", mode
);
365 if (numInstances
<= 0) {
366 if (numInstances
< 0)
367 _mesa_error(ctx
, GL_INVALID_VALUE
,
368 "glDrawArraysInstanced(numInstances=%d)", numInstances
);
372 if (!check_valid_to_render(ctx
, "glDrawArraysInstanced(invalid to render)"))
375 if (ctx
->Const
.CheckArrayBounds
) {
376 if (first
+ count
> (GLint
) ctx
->Array
.ArrayObj
->_MaxElement
)
385 _mesa_validate_DrawElementsInstanced(struct gl_context
*ctx
,
386 GLenum mode
, GLsizei count
, GLenum type
,
387 const GLvoid
*indices
, GLsizei numInstances
)
389 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
393 _mesa_error(ctx
, GL_INVALID_VALUE
,
394 "glDrawElementsInstanced(count=%d)", count
);
398 if (!_mesa_valid_prim_mode(ctx
, mode
)) {
399 _mesa_error(ctx
, GL_INVALID_ENUM
,
400 "glDrawElementsInstanced(mode = 0x%x)", mode
);
404 if (type
!= GL_UNSIGNED_INT
&&
405 type
!= GL_UNSIGNED_BYTE
&&
406 type
!= GL_UNSIGNED_SHORT
) {
407 _mesa_error(ctx
, GL_INVALID_ENUM
,
408 "glDrawElementsInstanced(type=0x%x)", type
);
412 if (numInstances
<= 0) {
413 if (numInstances
< 0)
414 _mesa_error(ctx
, GL_INVALID_VALUE
,
415 "glDrawElementsInstanced(numInstances=%d)", numInstances
);
419 if (!check_valid_to_render(ctx
, "glDrawElementsInstanced"))
422 /* Vertex buffer object tests */
423 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
424 /* use indices in the buffer object */
425 /* make sure count doesn't go outside buffer bounds */
426 if (index_bytes(type
, count
) > ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
) {
428 "glDrawElementsInstanced index out of buffer bounds");
433 /* not using a VBO */
438 if (!check_index_bounds(ctx
, count
, type
, indices
))