blob: e71e5a6ce867c529d508d5ab148069771e7fb0d7 [file] [log] [blame]
Keith Whitwellcab974c2000-12-26 05:09:27 +00001/*
2 * Mesa 3-D graphics library
Briana3c3bc92007-08-20 12:55:34 +01003 * Version: 7.1
Keith Whitwellcab974c2000-12-26 05:09:27 +00004 *
Brian37ece4d2007-07-08 09:20:42 -06005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Keith Whitwellcab974c2000-12-26 05:09:27 +00006 *
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"
Brian Paul8446d1b2001-01-02 21:40:57 +000026#include "api_validate.h"
Brian Paul434ec3a2009-08-12 13:46:16 -060027#include "bufferobj.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000028#include "context.h"
Brian Paul3c634522002-10-24 23:57:19 +000029#include "imports.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000030#include "mtypes.h"
Brian Paul8446d1b2001-01-02 21:40:57 +000031#include "state.h"
Eric Anholt92d7ed82009-08-27 10:09:24 -070032#include "vbo/vbo.h"
Keith Whitwell58e99172001-01-05 02:26:48 +000033
Brian Paulbb1fb2a2009-05-06 12:37:10 -060034
35/**
36 * \return number of bytes in array [count] of type.
37 */
38static GLsizei
39index_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
Briand8c67192007-08-20 13:12:20 +010054/**
Brian Paule5d29eb2009-09-21 14:07:35 -060055 * Find the max index in the given element/index buffer
56 */
57GLuint
58_mesa_max_buffer_index(GLcontext *ctx, GLuint count, GLenum type,
59 const void *indices,
60 struct gl_buffer_object *elementBuf)
61{
62 const GLubyte *map = NULL;
63 GLuint max = 0;
64 GLuint i;
65
66 if (_mesa_is_bufferobj(elementBuf)) {
67 /* elements are in a user-defined buffer object. need to map it */
68 map = ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER,
69 GL_READ_ONLY, elementBuf);
70 /* Actual address is the sum of pointers */
71 indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
72 }
73
74 if (type == GL_UNSIGNED_INT) {
75 for (i = 0; i < count; i++)
76 if (((GLuint *) indices)[i] > max)
77 max = ((GLuint *) indices)[i];
78 }
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];
83 }
84 else {
85 ASSERT(type == GL_UNSIGNED_BYTE);
86 for (i = 0; i < count; i++)
87 if (((GLubyte *) indices)[i] > max)
88 max = ((GLubyte *) indices)[i];
89 }
90
91 if (map) {
92 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, elementBuf);
93 }
94
95 return max;
96}
97
98
99/**
Brian Paulb6e56002009-08-14 10:48:31 -0600100 * Check if OK to draw arrays/elements.
Brian Paul88af3f82009-05-06 12:27:38 -0600101 */
Alan Hourihane263b96e2009-01-15 11:51:39 +0000102static GLboolean
Brian Paula48b0a52009-08-14 10:41:03 -0600103check_valid_to_render(GLcontext *ctx, const char *function)
Alan Hourihane263b96e2009-01-15 11:51:39 +0000104{
Brian Paulb6e56002009-08-14 10:48:31 -0600105 if (!_mesa_valid_to_render(ctx, function)) {
Alan Hourihane263b96e2009-01-15 11:51:39 +0000106 return GL_FALSE;
107 }
108
Brian Paul97dd2dd2009-03-02 12:27:16 -0700109#if FEATURE_es2_glsl
110 /* For ES2, we can draw if any vertex array is enabled (and we should
111 * always have a vertex program/shader).
112 */
113 if (ctx->Array.ArrayObj->_Enabled == 0x0 || !ctx->VertexProgram._Current)
114 return GL_FALSE;
115#else
116 /* For regular OpenGL, only draw if we have vertex positions (regardless
117 * of whether or not we have a vertex program/shader).
118 */
119 if (!ctx->Array.ArrayObj->Vertex.Enabled &&
Alan Hourihane263b96e2009-01-15 11:51:39 +0000120 !ctx->Array.ArrayObj->VertexAttrib[0].Enabled)
121 return GL_FALSE;
Brian Paul97dd2dd2009-03-02 12:27:16 -0700122#endif
Alan Hourihane263b96e2009-01-15 11:51:39 +0000123
124 return GL_TRUE;
125}
Briand8c67192007-08-20 13:12:20 +0100126
Eric Anholt92d7ed82009-08-27 10:09:24 -0700127static GLboolean
128check_index_bounds(GLcontext *ctx, GLsizei count, GLenum type,
129 const GLvoid *indices, GLint basevertex)
130{
131 struct _mesa_prim prim;
132 struct _mesa_index_buffer ib;
133 GLuint min, max;
134
135 /* Only the X Server needs to do this -- otherwise, accessing outside
136 * array/BO bounds allows application termination.
137 */
138 if (!ctx->Const.CheckArrayBounds)
139 return GL_TRUE;
140
141 memset(&prim, 0, sizeof(prim));
142 prim.count = count;
143
144 memset(&ib, 0, sizeof(ib));
145 ib.type = type;
146 ib.ptr = indices;
147 ib.obj = ctx->Array.ElementArrayBufferObj;
148
149 vbo_get_minmax_index(ctx, &prim, &ib, &min, &max);
150
151 if (min + basevertex < 0 ||
152 max + basevertex > ctx->Array.ArrayObj->_MaxElement) {
153 /* the max element is out of bounds of one or more enabled arrays */
154 _mesa_warning(ctx, "glDrawElements() index=%u is "
155 "out of bounds (max=%u)", max, ctx->Array.ArrayObj->_MaxElement);
156 return GL_FALSE;
157 }
158
159 return GL_TRUE;
160}
Brian Paul88af3f82009-05-06 12:27:38 -0600161
162/**
163 * Error checking for glDrawElements(). Includes parameter checking
164 * and VBO bounds checking.
165 * \return GL_TRUE if OK to render, GL_FALSE if error found
166 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000167GLboolean
168_mesa_validate_DrawElements(GLcontext *ctx,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000169 GLenum mode, GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700170 const GLvoid *indices, GLint basevertex)
Keith Whitwellcab974c2000-12-26 05:09:27 +0000171{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000172 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000173
174 if (count <= 0) {
175 if (count < 0)
Brian Paul08836342001-03-03 20:33:27 +0000176 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000177 return GL_FALSE;
178 }
179
Brian Paul464bc3b2003-04-21 15:04:30 +0000180 if (mode > GL_POLYGON) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000181 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000182 return GL_FALSE;
183 }
184
Gareth Hughes22144ab2001-03-12 00:48:37 +0000185 if (type != GL_UNSIGNED_INT &&
186 type != GL_UNSIGNED_BYTE &&
Keith Whitwellcab974c2000-12-26 05:09:27 +0000187 type != GL_UNSIGNED_SHORT)
188 {
Brian Paul08836342001-03-03 20:33:27 +0000189 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000190 return GL_FALSE;
191 }
192
193 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000194 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000195
Brian Paula48b0a52009-08-14 10:41:03 -0600196 if (!check_valid_to_render(ctx, "glDrawElements"))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000197 return GL_FALSE;
198
Brian Paul03e29a52003-12-04 03:16:27 +0000199 /* Vertex buffer object tests */
Brian Paul434ec3a2009-08-12 13:46:16 -0600200 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
Briand8c67192007-08-20 13:12:20 +0100201 /* use indices in the buffer object */
Briand8c67192007-08-20 13:12:20 +0100202 /* make sure count doesn't go outside buffer bounds */
Brian Paulbb1fb2a2009-05-06 12:37:10 -0600203 if (index_bytes(type, count) > ctx->Array.ElementArrayBufferObj->Size) {
Brian Paul03e29a52003-12-04 03:16:27 +0000204 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
205 return GL_FALSE;
206 }
Brian Paul03e29a52003-12-04 03:16:27 +0000207 }
Brian37ece4d2007-07-08 09:20:42 -0600208 else {
209 /* not using a VBO */
210 if (!indices)
211 return GL_FALSE;
212 }
Brian Paul03e29a52003-12-04 03:16:27 +0000213
Eric Anholt92d7ed82009-08-27 10:09:24 -0700214 if (!check_index_bounds(ctx, count, type, indices, basevertex))
215 return GL_FALSE;
Xiang, Haihao2394d202007-07-30 23:50:52 +0800216
Keith Whitwellcab974c2000-12-26 05:09:27 +0000217 return GL_TRUE;
218}
219
Brian Paul88af3f82009-05-06 12:27:38 -0600220
221/**
222 * Error checking for glDrawRangeElements(). Includes parameter checking
223 * and VBO bounds checking.
224 * \return GL_TRUE if OK to render, GL_FALSE if error found
225 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000226GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000227_mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
228 GLuint start, GLuint end,
229 GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700230 const GLvoid *indices, GLint basevertex)
Keith Whitwellcab974c2000-12-26 05:09:27 +0000231{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000232 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000233
234 if (count <= 0) {
235 if (count < 0)
Brian Paula2b9bad2003-11-10 19:08:37 +0000236 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000237 return GL_FALSE;
238 }
239
Brian Paul464bc3b2003-04-21 15:04:30 +0000240 if (mode > GL_POLYGON) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000241 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000242 return GL_FALSE;
243 }
244
245 if (end < start) {
Brian Paul08836342001-03-03 20:33:27 +0000246 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
Keith Whitwellcab974c2000-12-26 05:09:27 +0000247 return GL_FALSE;
248 }
249
Gareth Hughes22144ab2001-03-12 00:48:37 +0000250 if (type != GL_UNSIGNED_INT &&
251 type != GL_UNSIGNED_BYTE &&
Brian Paul61bac602002-04-21 21:04:46 +0000252 type != GL_UNSIGNED_SHORT) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000253 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000254 return GL_FALSE;
255 }
256
257 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000258 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000259
Brian Paula48b0a52009-08-14 10:41:03 -0600260 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000261 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000262
Brian37ece4d2007-07-08 09:20:42 -0600263 /* Vertex buffer object tests */
Brian Paul434ec3a2009-08-12 13:46:16 -0600264 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
Briand8c67192007-08-20 13:12:20 +0100265 /* use indices in the buffer object */
Briand8c67192007-08-20 13:12:20 +0100266 /* make sure count doesn't go outside buffer bounds */
Brian Paulbb1fb2a2009-05-06 12:37:10 -0600267 if (index_bytes(type, count) > ctx->Array.ElementArrayBufferObj->Size) {
Brianef5935b2007-09-23 13:56:46 -0600268 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
Briand8c67192007-08-20 13:12:20 +0100269 return GL_FALSE;
270 }
Brian37ece4d2007-07-08 09:20:42 -0600271 }
272 else {
Briand8c67192007-08-20 13:12:20 +0100273 /* not using a VBO */
Brian37ece4d2007-07-08 09:20:42 -0600274 if (!indices)
275 return GL_FALSE;
276 }
277
Eric Anholt92d7ed82009-08-27 10:09:24 -0700278 if (!check_index_bounds(ctx, count, type, indices, basevertex))
279 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000280
Brian Paulc5b1e812003-10-22 22:59:07 +0000281 return GL_TRUE;
282}
Keith Whitwellcab974c2000-12-26 05:09:27 +0000283
Brian Paulc5b1e812003-10-22 22:59:07 +0000284
285/**
286 * Called from the tnl module to error check the function parameters and
287 * verify that we really can draw something.
Brian Paul88af3f82009-05-06 12:27:38 -0600288 * \return GL_TRUE if OK to render, GL_FALSE if error found
Brian Paulc5b1e812003-10-22 22:59:07 +0000289 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000290GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000291_mesa_validate_DrawArrays(GLcontext *ctx,
Keith Whitwellcab974c2000-12-26 05:09:27 +0000292 GLenum mode, GLint start, GLsizei count)
293{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000294 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000295
Brian5cb20342007-10-31 09:38:51 -0600296 if (count <= 0) {
297 if (count < 0)
298 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000299 return GL_FALSE;
300 }
301
Brian Paul464bc3b2003-04-21 15:04:30 +0000302 if (mode > GL_POLYGON) {
Brian Paul08836342001-03-03 20:33:27 +0000303 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000304 return GL_FALSE;
305 }
306
307 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000308 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000309
Brian Paula48b0a52009-08-14 10:41:03 -0600310 if (!check_valid_to_render(ctx, "glDrawArrays"))
Brian Paula2b9bad2003-11-10 19:08:37 +0000311 return GL_FALSE;
312
313 if (ctx->Const.CheckArrayBounds) {
Brian Paula185bcb2009-05-14 13:24:24 -0600314 if (start + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
Brian Paulc5b1e812003-10-22 22:59:07 +0000315 return GL_FALSE;
316 }
Brian Paulc5b1e812003-10-22 22:59:07 +0000317
318 return GL_TRUE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000319}