3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "api_validate.h"
35 _mesa_validate_DrawElements(GLcontext
*ctx
,
36 GLenum mode
, GLsizei count
, GLenum type
,
37 const GLvoid
*indices
)
39 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
43 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count)" );
47 if (mode
> GL_POLYGON
) {
48 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(mode)" );
52 if (type
!= GL_UNSIGNED_INT
&&
53 type
!= GL_UNSIGNED_BYTE
&&
54 type
!= GL_UNSIGNED_SHORT
)
56 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(type)" );
61 _mesa_update_state(ctx
);
63 /* Always need vertex positions */
64 if (!ctx
->Array
.Vertex
.Enabled
65 && !(ctx
->VertexProgram
.Enabled
&& ctx
->Array
.VertexAttrib
[0].Enabled
))
68 /* Vertex buffer object tests */
69 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
72 /* use indices in the buffer object */
73 if (!ctx
->Array
.ElementArrayBufferObj
->Data
) {
74 _mesa_warning(ctx
, "DrawElements with empty vertex elements buffer!");
78 /* make sure count doesn't go outside buffer bounds */
79 if (type
== GL_UNSIGNED_INT
) {
80 indexBytes
= count
* sizeof(GLuint
);
82 else if (type
== GL_UNSIGNED_BYTE
) {
83 indexBytes
= count
* sizeof(GLubyte
);
86 ASSERT(type
== GL_UNSIGNED_SHORT
);
87 indexBytes
= count
* sizeof(GLushort
);
90 if ((GLubyte
*) indices
+ indexBytes
>
91 ctx
->Array
.ElementArrayBufferObj
->Data
+
92 ctx
->Array
.ElementArrayBufferObj
->Size
) {
93 _mesa_warning(ctx
, "glDrawElements index out of buffer bounds");
97 /* Actual address is the sum of pointers. Indices may be used below. */
98 if (ctx
->Const
.CheckArrayBounds
) {
99 indices
= (const GLvoid
*)
100 ADD_POINTERS(ctx
->Array
.ElementArrayBufferObj
->Data
,
101 (const GLubyte
*) indices
);
105 if (ctx
->Const
.CheckArrayBounds
) {
106 /* find max array index */
109 if (type
== GL_UNSIGNED_INT
) {
110 for (i
= 0; i
< count
; i
++)
111 if (((GLuint
*) indices
)[i
] > max
)
112 max
= ((GLuint
*) indices
)[i
];
114 else if (type
== GL_UNSIGNED_SHORT
) {
115 for (i
= 0; i
< count
; i
++)
116 if (((GLushort
*) indices
)[i
] > max
)
117 max
= ((GLushort
*) indices
)[i
];
120 ASSERT(type
== GL_UNSIGNED_BYTE
);
121 for (i
= 0; i
< count
; i
++)
122 if (((GLubyte
*) indices
)[i
] > max
)
123 max
= ((GLubyte
*) indices
)[i
];
125 if (max
>= ctx
->Array
._MaxElement
) {
126 /* the max element is out of bounds of one or more enabled arrays */
136 _mesa_validate_DrawRangeElements(GLcontext
*ctx
, GLenum mode
,
137 GLuint start
, GLuint end
,
138 GLsizei count
, GLenum type
,
139 const GLvoid
*indices
)
141 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
145 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(count)" );
149 if (mode
> GL_POLYGON
) {
150 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(mode)" );
155 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawRangeElements(end<start)");
159 if (type
!= GL_UNSIGNED_INT
&&
160 type
!= GL_UNSIGNED_BYTE
&&
161 type
!= GL_UNSIGNED_SHORT
) {
162 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(type)" );
167 _mesa_update_state(ctx
);
169 /* Always need vertex positions */
170 if (!ctx
->Array
.Vertex
.Enabled
171 && !(ctx
->VertexProgram
.Enabled
&& ctx
->Array
.VertexAttrib
[0].Enabled
))
174 if (ctx
->Const
.CheckArrayBounds
) {
175 /* Find max array index.
176 * We don't trust the user's start and end values.
180 if (type
== GL_UNSIGNED_INT
) {
181 for (i
= 0; i
< count
; i
++)
182 if (((GLuint
*) indices
)[i
] > max
)
183 max
= ((GLuint
*) indices
)[i
];
185 else if (type
== GL_UNSIGNED_SHORT
) {
186 for (i
= 0; i
< count
; i
++)
187 if (((GLushort
*) indices
)[i
] > max
)
188 max
= ((GLushort
*) indices
)[i
];
191 ASSERT(type
== GL_UNSIGNED_BYTE
);
192 for (i
= 0; i
< count
; i
++)
193 if (((GLubyte
*) indices
)[i
] > max
)
194 max
= ((GLubyte
*) indices
)[i
];
196 if (max
>= ctx
->Array
._MaxElement
) {
197 /* the max element is out of bounds of one or more enabled arrays */
207 * Called from the tnl module to error check the function parameters and
208 * verify that we really can draw something.
211 _mesa_validate_DrawArrays(GLcontext
*ctx
,
212 GLenum mode
, GLint start
, GLsizei count
)
214 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
217 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count)" );
221 if (mode
> GL_POLYGON
) {
222 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawArrays(mode)" );
227 _mesa_update_state(ctx
);
229 /* Always need vertex positions */
230 if (!ctx
->Array
.Vertex
.Enabled
231 && !(ctx
->VertexProgram
.Enabled
&& ctx
->Array
.VertexAttrib
[0].Enabled
))
234 if (ctx
->Const
.CheckArrayBounds
) {
235 if (start
+ count
> (GLint
) ctx
->Array
._MaxElement
)