Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / main / api_validate.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 #include "glheader.h"
26 #include "api_validate.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "imports.h"
30 #include "mtypes.h"
31 #include "state.h"
32 #include "vbo/vbo.h"
33
34
35 /**
36 * \return number of bytes in array [count] of type.
37 */
38 static GLsizei
39 index_bytes(GLenum type, GLsizei count)
40 {
41 if (type == GL_UNSIGNED_INT) {
42 return count * sizeof(GLuint);
43 }
44 else if (type == GL_UNSIGNED_BYTE) {
45 return count * sizeof(GLubyte);
46 }
47 else {
48 ASSERT(type == GL_UNSIGNED_SHORT);
49 return count * sizeof(GLushort);
50 }
51 }
52
53
54 /**
55 * Check if OK to draw arrays/elements.
56 */
57 static GLboolean
58 check_valid_to_render(GLcontext *ctx, const char *function)
59 {
60 if (!_mesa_valid_to_render(ctx, function)) {
61 return GL_FALSE;
62 }
63
64 #if FEATURE_es2_glsl
65 /* For ES2, we can draw if any vertex array is enabled (and we should
66 * always have a vertex program/shader).
67 */
68 if (ctx->Array.ArrayObj->_Enabled == 0x0 || !ctx->VertexProgram._Current)
69 return GL_FALSE;
70 #else
71 /* For regular OpenGL, only draw if we have vertex positions (regardless
72 * of whether or not we have a vertex program/shader).
73 */
74 if (!ctx->Array.ArrayObj->Vertex.Enabled &&
75 !ctx->Array.ArrayObj->VertexAttrib[0].Enabled)
76 return GL_FALSE;
77 #endif
78
79 return GL_TRUE;
80 }
81
82 static GLboolean
83 check_index_bounds(GLcontext *ctx, GLsizei count, GLenum type,
84 const GLvoid *indices, GLint basevertex)
85 {
86 struct _mesa_prim prim;
87 struct _mesa_index_buffer ib;
88 GLuint min, max;
89
90 /* Only the X Server needs to do this -- otherwise, accessing outside
91 * array/BO bounds allows application termination.
92 */
93 if (!ctx->Const.CheckArrayBounds)
94 return GL_TRUE;
95
96 memset(&prim, 0, sizeof(prim));
97 prim.count = count;
98
99 memset(&ib, 0, sizeof(ib));
100 ib.type = type;
101 ib.ptr = indices;
102 ib.obj = ctx->Array.ElementArrayBufferObj;
103
104 vbo_get_minmax_index(ctx, &prim, &ib, &min, &max);
105
106 if (min + basevertex < 0 ||
107 max + basevertex > ctx->Array.ArrayObj->_MaxElement) {
108 /* the max element is out of bounds of one or more enabled arrays */
109 _mesa_warning(ctx, "glDrawElements() index=%u is "
110 "out of bounds (max=%u)", max, ctx->Array.ArrayObj->_MaxElement);
111 return GL_FALSE;
112 }
113
114 return GL_TRUE;
115 }
116
117 /**
118 * Error checking for glDrawElements(). Includes parameter checking
119 * and VBO bounds checking.
120 * \return GL_TRUE if OK to render, GL_FALSE if error found
121 */
122 GLboolean
123 _mesa_validate_DrawElements(GLcontext *ctx,
124 GLenum mode, GLsizei count, GLenum type,
125 const GLvoid *indices, GLint basevertex)
126 {
127 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
128
129 if (count <= 0) {
130 if (count < 0)
131 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
132 return GL_FALSE;
133 }
134
135 if (mode > GL_POLYGON) {
136 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)" );
137 return GL_FALSE;
138 }
139
140 if (type != GL_UNSIGNED_INT &&
141 type != GL_UNSIGNED_BYTE &&
142 type != GL_UNSIGNED_SHORT)
143 {
144 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
145 return GL_FALSE;
146 }
147
148 if (ctx->NewState)
149 _mesa_update_state(ctx);
150
151 if (!check_valid_to_render(ctx, "glDrawElements"))
152 return GL_FALSE;
153
154 /* Vertex buffer object tests */
155 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
156 /* use indices in the buffer object */
157 /* make sure count doesn't go outside buffer bounds */
158 if (index_bytes(type, count) > ctx->Array.ElementArrayBufferObj->Size) {
159 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
160 return GL_FALSE;
161 }
162 }
163 else {
164 /* not using a VBO */
165 if (!indices)
166 return GL_FALSE;
167 }
168
169 if (!check_index_bounds(ctx, count, type, indices, basevertex))
170 return GL_FALSE;
171
172 return GL_TRUE;
173 }
174
175
176 /**
177 * Error checking for glDrawRangeElements(). Includes parameter checking
178 * and VBO bounds checking.
179 * \return GL_TRUE if OK to render, GL_FALSE if error found
180 */
181 GLboolean
182 _mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
183 GLuint start, GLuint end,
184 GLsizei count, GLenum type,
185 const GLvoid *indices, GLint basevertex)
186 {
187 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
188
189 if (count <= 0) {
190 if (count < 0)
191 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
192 return GL_FALSE;
193 }
194
195 if (mode > GL_POLYGON) {
196 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)" );
197 return GL_FALSE;
198 }
199
200 if (end < start) {
201 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
202 return GL_FALSE;
203 }
204
205 if (type != GL_UNSIGNED_INT &&
206 type != GL_UNSIGNED_BYTE &&
207 type != GL_UNSIGNED_SHORT) {
208 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)" );
209 return GL_FALSE;
210 }
211
212 if (ctx->NewState)
213 _mesa_update_state(ctx);
214
215 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
216 return GL_FALSE;
217
218 /* Vertex buffer object tests */
219 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
220 /* use indices in the buffer object */
221 /* make sure count doesn't go outside buffer bounds */
222 if (index_bytes(type, count) > ctx->Array.ElementArrayBufferObj->Size) {
223 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
224 return GL_FALSE;
225 }
226 }
227 else {
228 /* not using a VBO */
229 if (!indices)
230 return GL_FALSE;
231 }
232
233 if (!check_index_bounds(ctx, count, type, indices, basevertex))
234 return GL_FALSE;
235
236 return GL_TRUE;
237 }
238
239
240 /**
241 * Called from the tnl module to error check the function parameters and
242 * verify that we really can draw something.
243 * \return GL_TRUE if OK to render, GL_FALSE if error found
244 */
245 GLboolean
246 _mesa_validate_DrawArrays(GLcontext *ctx,
247 GLenum mode, GLint start, GLsizei count)
248 {
249 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
250
251 if (count <= 0) {
252 if (count < 0)
253 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
254 return GL_FALSE;
255 }
256
257 if (mode > GL_POLYGON) {
258 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
259 return GL_FALSE;
260 }
261
262 if (ctx->NewState)
263 _mesa_update_state(ctx);
264
265 if (!check_valid_to_render(ctx, "glDrawArrays"))
266 return GL_FALSE;
267
268 if (ctx->Const.CheckArrayBounds) {
269 if (start + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
270 return GL_FALSE;
271 }
272
273 return GL_TRUE;
274 }