blob: 64ab324af2269b02fb1f6d6702c21627f7709d14 [file] [log] [blame]
Keith Whitwellcab974c2000-12-26 05:09:27 +00001/*
2 * Mesa 3-D graphics library
Brian37ece4d2007-07-08 09:20:42 -06003 * Version: 7.0.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"
Keith Whitwellcab974c2000-12-26 05:09:27 +000027#include "context.h"
Brian Paul3c634522002-10-24 23:57:19 +000028#include "imports.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000029#include "mtypes.h"
Brian Paul8446d1b2001-01-02 21:40:57 +000030#include "state.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000031
Keith Whitwell58e99172001-01-05 02:26:48 +000032
Keith Whitwellcab974c2000-12-26 05:09:27 +000033GLboolean
34_mesa_validate_DrawElements(GLcontext *ctx,
Gareth Hughes22144ab2001-03-12 00:48:37 +000035 GLenum mode, GLsizei count, GLenum type,
Keith Whitwellcab974c2000-12-26 05:09:27 +000036 const GLvoid *indices)
37{
Gareth Hughes22144ab2001-03-12 00:48:37 +000038 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +000039
40 if (count <= 0) {
41 if (count < 0)
Brian Paul08836342001-03-03 20:33:27 +000042 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000043 return GL_FALSE;
44 }
45
Brian Paul464bc3b2003-04-21 15:04:30 +000046 if (mode > GL_POLYGON) {
Brian Paula2b9bad2003-11-10 19:08:37 +000047 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000048 return GL_FALSE;
49 }
50
Gareth Hughes22144ab2001-03-12 00:48:37 +000051 if (type != GL_UNSIGNED_INT &&
52 type != GL_UNSIGNED_BYTE &&
Keith Whitwellcab974c2000-12-26 05:09:27 +000053 type != GL_UNSIGNED_SHORT)
54 {
Brian Paul08836342001-03-03 20:33:27 +000055 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000056 return GL_FALSE;
57 }
58
59 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +000060 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +000061
Brian Paula2b9bad2003-11-10 19:08:37 +000062 /* Always need vertex positions */
Ian Romanickee34e6e2006-06-12 16:26:29 +000063 if (!ctx->Array.ArrayObj->Vertex.Enabled
64 && !(ctx->VertexProgram._Enabled && ctx->Array.ArrayObj->VertexAttrib[0].Enabled))
Keith Whitwellcab974c2000-12-26 05:09:27 +000065 return GL_FALSE;
66
Brian Paul03e29a52003-12-04 03:16:27 +000067 /* Vertex buffer object tests */
68 if (ctx->Array.ElementArrayBufferObj->Name) {
69 GLuint indexBytes;
70
71 /* use indices in the buffer object */
Brian730df762007-08-20 12:50:34 -060072 if (!ctx->Array.ElementArrayBufferObj->Size) {
73 _mesa_warning(ctx,
74 "glDrawElements called with empty array elements buffer");
Brian Paul03e29a52003-12-04 03:16:27 +000075 return GL_FALSE;
76 }
77
78 /* make sure count doesn't go outside buffer bounds */
79 if (type == GL_UNSIGNED_INT) {
80 indexBytes = count * sizeof(GLuint);
81 }
82 else if (type == GL_UNSIGNED_BYTE) {
83 indexBytes = count * sizeof(GLubyte);
84 }
85 else {
86 ASSERT(type == GL_UNSIGNED_SHORT);
87 indexBytes = count * sizeof(GLushort);
88 }
89
Brian730df762007-08-20 12:50:34 -060090 if (indexBytes > ctx->Array.ElementArrayBufferObj->Size) {
Brian Paul03e29a52003-12-04 03:16:27 +000091 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
92 return GL_FALSE;
93 }
Brian Paul03e29a52003-12-04 03:16:27 +000094 }
Brian37ece4d2007-07-08 09:20:42 -060095 else {
96 /* not using a VBO */
97 if (!indices)
98 return GL_FALSE;
99 }
Brian Paul03e29a52003-12-04 03:16:27 +0000100
Brian Paula2b9bad2003-11-10 19:08:37 +0000101 if (ctx->Const.CheckArrayBounds) {
102 /* find max array index */
Brian730df762007-08-20 12:50:34 -0600103 const GLubyte *map;
Brian Paula2b9bad2003-11-10 19:08:37 +0000104 GLuint max = 0;
105 GLint i;
Brian730df762007-08-20 12:50:34 -0600106
107 map = ctx->Driver.MapBuffer(ctx,
108 GL_ELEMENT_ARRAY_BUFFER_ARB,
109 GL_READ_ONLY,
110 ctx->Array.ElementArrayBufferObj);
111
112 /* Actual address is the sum of pointers */
113 indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
114
Brian Paula2b9bad2003-11-10 19:08:37 +0000115 if (type == GL_UNSIGNED_INT) {
116 for (i = 0; i < count; i++)
117 if (((GLuint *) indices)[i] > max)
118 max = ((GLuint *) indices)[i];
119 }
120 else if (type == GL_UNSIGNED_SHORT) {
121 for (i = 0; i < count; i++)
122 if (((GLushort *) indices)[i] > max)
123 max = ((GLushort *) indices)[i];
124 }
125 else {
126 ASSERT(type == GL_UNSIGNED_BYTE);
127 for (i = 0; i < count; i++)
128 if (((GLubyte *) indices)[i] > max)
129 max = ((GLubyte *) indices)[i];
130 }
Brian730df762007-08-20 12:50:34 -0600131
132 ctx->Driver.UnmapBuffer(ctx,
133 GL_ELEMENT_ARRAY_BUFFER_ARB,
134 ctx->Array.ElementArrayBufferObj);
135
Brian Paula2b9bad2003-11-10 19:08:37 +0000136 if (max >= ctx->Array._MaxElement) {
137 /* the max element is out of bounds of one or more enabled arrays */
138 return GL_FALSE;
139 }
140 }
141
Keith Whitwellcab974c2000-12-26 05:09:27 +0000142 return GL_TRUE;
143}
144
145
146GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000147_mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
148 GLuint start, GLuint end,
149 GLsizei count, GLenum type,
Keith Whitwellcab974c2000-12-26 05:09:27 +0000150 const GLvoid *indices)
151{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000152 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000153
154 if (count <= 0) {
155 if (count < 0)
Brian Paula2b9bad2003-11-10 19:08:37 +0000156 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000157 return GL_FALSE;
158 }
159
Brian Paul464bc3b2003-04-21 15:04:30 +0000160 if (mode > GL_POLYGON) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000161 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000162 return GL_FALSE;
163 }
164
165 if (end < start) {
Brian Paul08836342001-03-03 20:33:27 +0000166 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
Keith Whitwellcab974c2000-12-26 05:09:27 +0000167 return GL_FALSE;
168 }
169
Gareth Hughes22144ab2001-03-12 00:48:37 +0000170 if (type != GL_UNSIGNED_INT &&
171 type != GL_UNSIGNED_BYTE &&
Brian Paul61bac602002-04-21 21:04:46 +0000172 type != GL_UNSIGNED_SHORT) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000173 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000174 return GL_FALSE;
175 }
176
177 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000178 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000179
Brian Paula2b9bad2003-11-10 19:08:37 +0000180 /* Always need vertex positions */
Ian Romanickee34e6e2006-06-12 16:26:29 +0000181 if (!ctx->Array.ArrayObj->Vertex.Enabled
182 && !(ctx->VertexProgram._Enabled && ctx->Array.ArrayObj->VertexAttrib[0].Enabled))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000183 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000184
Brian37ece4d2007-07-08 09:20:42 -0600185 /* Vertex buffer object tests */
186 if (ctx->Array.ElementArrayBufferObj->Name) {
187 /* XXX re-use code from above? */
188 }
189 else {
190 /* not using VBO */
191 if (!indices)
192 return GL_FALSE;
193 }
194
Brian Paula2b9bad2003-11-10 19:08:37 +0000195 if (ctx->Const.CheckArrayBounds) {
196 /* Find max array index.
197 * We don't trust the user's start and end values.
198 */
199 GLuint max = 0;
200 GLint i;
201 if (type == GL_UNSIGNED_INT) {
202 for (i = 0; i < count; i++)
203 if (((GLuint *) indices)[i] > max)
204 max = ((GLuint *) indices)[i];
205 }
206 else if (type == GL_UNSIGNED_SHORT) {
207 for (i = 0; i < count; i++)
208 if (((GLushort *) indices)[i] > max)
209 max = ((GLushort *) indices)[i];
210 }
211 else {
212 ASSERT(type == GL_UNSIGNED_BYTE);
213 for (i = 0; i < count; i++)
214 if (((GLubyte *) indices)[i] > max)
215 max = ((GLubyte *) indices)[i];
216 }
217 if (max >= ctx->Array._MaxElement) {
218 /* the max element is out of bounds of one or more enabled arrays */
219 return GL_FALSE;
220 }
221 }
Keith Whitwellcab974c2000-12-26 05:09:27 +0000222
Brian Paulc5b1e812003-10-22 22:59:07 +0000223 return GL_TRUE;
224}
Keith Whitwellcab974c2000-12-26 05:09:27 +0000225
Brian Paulc5b1e812003-10-22 22:59:07 +0000226
227/**
228 * Called from the tnl module to error check the function parameters and
229 * verify that we really can draw something.
230 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000231GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000232_mesa_validate_DrawArrays(GLcontext *ctx,
Keith Whitwellcab974c2000-12-26 05:09:27 +0000233 GLenum mode, GLint start, GLsizei count)
234{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000235 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000236
Brian Paulc5b1e812003-10-22 22:59:07 +0000237 if (count < 0) {
Brian Paul08836342001-03-03 20:33:27 +0000238 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000239 return GL_FALSE;
240 }
241
Brian Paul464bc3b2003-04-21 15:04:30 +0000242 if (mode > GL_POLYGON) {
Brian Paul08836342001-03-03 20:33:27 +0000243 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000244 return GL_FALSE;
245 }
246
247 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000248 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000249
Brian Paula2b9bad2003-11-10 19:08:37 +0000250 /* Always need vertex positions */
Ian Romanickee34e6e2006-06-12 16:26:29 +0000251 if (!ctx->Array.ArrayObj->Vertex.Enabled && !ctx->Array.ArrayObj->VertexAttrib[0].Enabled)
Brian Paula2b9bad2003-11-10 19:08:37 +0000252 return GL_FALSE;
253
254 if (ctx->Const.CheckArrayBounds) {
Brian Paul2c9f50d2003-11-25 16:21:51 +0000255 if (start + count > (GLint) ctx->Array._MaxElement)
Brian Paulc5b1e812003-10-22 22:59:07 +0000256 return GL_FALSE;
257 }
Brian Paulc5b1e812003-10-22 22:59:07 +0000258
259 return GL_TRUE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000260}