api_validate.c revision ef5935bc94a1439eb8f1731732a3eabd0e360407
1cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell/*
2cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * Mesa 3-D graphics library
3a3c3bc9ece7e7c55c8832dbc8c50ab1c34f5bfe9Brian * Version:  7.1
4cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell *
537ece4df7c654b30b6720044b35a83694d7e5bb3Brian * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell *
7cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
8cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * copy of this software and associated documentation files (the "Software"),
9cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * to deal in the Software without restriction, including without limitation
10cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
12cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * Software is furnished to do so, subject to the following conditions:
13cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell *
14cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * The above copyright notice and this permission notice shall be included
15cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * in all copies or substantial portions of the Software.
16cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell *
17cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell */
24cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
25cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell#include "glheader.h"
268446d1bab15ef82b35b8980a0a56072ace6feb04Brian Paul#include "api_validate.h"
27cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell#include "context.h"
283c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "imports.h"
29cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell#include "mtypes.h"
308446d1bab15ef82b35b8980a0a56072ace6feb04Brian Paul#include "state.h"
31cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
3258e991705392a2e17a1c8b034f4083a0adaf1943Keith Whitwell
33d8c6719f95b1543296ac954f95d13b048ae48634Brian/**
34d8c6719f95b1543296ac954f95d13b048ae48634Brian * Find the max index in the given element/index buffer
35d8c6719f95b1543296ac954f95d13b048ae48634Brian */
36d8c6719f95b1543296ac954f95d13b048ae48634Brianstatic GLuint
37d8c6719f95b1543296ac954f95d13b048ae48634Brianmax_buffer_index(GLcontext *ctx, GLuint count, GLenum type,
38d8c6719f95b1543296ac954f95d13b048ae48634Brian                 const void *indices,
39d8c6719f95b1543296ac954f95d13b048ae48634Brian                 struct gl_buffer_object *elementBuf)
40d8c6719f95b1543296ac954f95d13b048ae48634Brian{
41d8c6719f95b1543296ac954f95d13b048ae48634Brian   const GLubyte *map = NULL;
42d8c6719f95b1543296ac954f95d13b048ae48634Brian   GLuint max = 0;
43d8c6719f95b1543296ac954f95d13b048ae48634Brian   GLint i;
44d8c6719f95b1543296ac954f95d13b048ae48634Brian
45d8c6719f95b1543296ac954f95d13b048ae48634Brian   if (elementBuf->Name) {
46d8c6719f95b1543296ac954f95d13b048ae48634Brian      /* elements are in a user-defined buffer object.  need to map it */
47d8c6719f95b1543296ac954f95d13b048ae48634Brian      map = ctx->Driver.MapBuffer(ctx,
48d8c6719f95b1543296ac954f95d13b048ae48634Brian                                  GL_ELEMENT_ARRAY_BUFFER_ARB,
49d8c6719f95b1543296ac954f95d13b048ae48634Brian                                  GL_READ_ONLY,
50d8c6719f95b1543296ac954f95d13b048ae48634Brian                                  elementBuf);
51d8c6719f95b1543296ac954f95d13b048ae48634Brian      /* Actual address is the sum of pointers */
52d8c6719f95b1543296ac954f95d13b048ae48634Brian      indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
53d8c6719f95b1543296ac954f95d13b048ae48634Brian   }
54d8c6719f95b1543296ac954f95d13b048ae48634Brian
55d8c6719f95b1543296ac954f95d13b048ae48634Brian   if (type == GL_UNSIGNED_INT) {
56d8c6719f95b1543296ac954f95d13b048ae48634Brian      for (i = 0; i < count; i++)
57d8c6719f95b1543296ac954f95d13b048ae48634Brian         if (((GLuint *) indices)[i] > max)
58d8c6719f95b1543296ac954f95d13b048ae48634Brian            max = ((GLuint *) indices)[i];
59d8c6719f95b1543296ac954f95d13b048ae48634Brian   }
60d8c6719f95b1543296ac954f95d13b048ae48634Brian   else if (type == GL_UNSIGNED_SHORT) {
61d8c6719f95b1543296ac954f95d13b048ae48634Brian      for (i = 0; i < count; i++)
62d8c6719f95b1543296ac954f95d13b048ae48634Brian         if (((GLushort *) indices)[i] > max)
63d8c6719f95b1543296ac954f95d13b048ae48634Brian            max = ((GLushort *) indices)[i];
64d8c6719f95b1543296ac954f95d13b048ae48634Brian   }
65d8c6719f95b1543296ac954f95d13b048ae48634Brian   else {
66d8c6719f95b1543296ac954f95d13b048ae48634Brian      ASSERT(type == GL_UNSIGNED_BYTE);
67d8c6719f95b1543296ac954f95d13b048ae48634Brian      for (i = 0; i < count; i++)
68d8c6719f95b1543296ac954f95d13b048ae48634Brian         if (((GLubyte *) indices)[i] > max)
69d8c6719f95b1543296ac954f95d13b048ae48634Brian            max = ((GLubyte *) indices)[i];
70d8c6719f95b1543296ac954f95d13b048ae48634Brian   }
71d8c6719f95b1543296ac954f95d13b048ae48634Brian
72d8c6719f95b1543296ac954f95d13b048ae48634Brian   if (map) {
73d8c6719f95b1543296ac954f95d13b048ae48634Brian      ctx->Driver.UnmapBuffer(ctx,
74d8c6719f95b1543296ac954f95d13b048ae48634Brian                              GL_ELEMENT_ARRAY_BUFFER_ARB,
75d8c6719f95b1543296ac954f95d13b048ae48634Brian                              ctx->Array.ElementArrayBufferObj);
76d8c6719f95b1543296ac954f95d13b048ae48634Brian   }
77d8c6719f95b1543296ac954f95d13b048ae48634Brian
78d8c6719f95b1543296ac954f95d13b048ae48634Brian   return max;
79d8c6719f95b1543296ac954f95d13b048ae48634Brian}
80d8c6719f95b1543296ac954f95d13b048ae48634Brian
81d8c6719f95b1543296ac954f95d13b048ae48634Brian
82cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith WhitwellGLboolean
83cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell_mesa_validate_DrawElements(GLcontext *ctx,
8422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes			    GLenum mode, GLsizei count, GLenum type,
85cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell			    const GLvoid *indices)
86cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell{
8722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx,  GL_FALSE);
88cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
89cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (count <= 0) {
90cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      if (count < 0)
9108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul	 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
92cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
93cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
94cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
95464bc3b6747108dd32e4a9fcfe6549a9d4a52a95Brian Paul   if (mode > GL_POLYGON) {
96a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)" );
97cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
98cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
99cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
10022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes   if (type != GL_UNSIGNED_INT &&
10122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes       type != GL_UNSIGNED_BYTE &&
102cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell       type != GL_UNSIGNED_SHORT)
103cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   {
10408836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
105cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
106cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
107cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
108cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (ctx->NewState)
109a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      _mesa_update_state(ctx);
110a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul
111a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   /* Always need vertex positions */
112ee34e6ef716bb630440299ac1efbc2055ef09ffdIan Romanick   if (!ctx->Array.ArrayObj->Vertex.Enabled
113d8c6719f95b1543296ac954f95d13b048ae48634Brian       && !(ctx->VertexProgram._Enabled
114d8c6719f95b1543296ac954f95d13b048ae48634Brian            && ctx->Array.ArrayObj->VertexAttrib[0].Enabled))
115a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      return GL_FALSE;
116a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul
11703e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul   /* Vertex buffer object tests */
11803e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul   if (ctx->Array.ElementArrayBufferObj->Name) {
11903e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      /* use indices in the buffer object */
120d8c6719f95b1543296ac954f95d13b048ae48634Brian      GLuint indexBytes;
12103e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul
12203e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      if (type == GL_UNSIGNED_INT) {
12303e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul         indexBytes = count * sizeof(GLuint);
12403e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      }
12503e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      else if (type == GL_UNSIGNED_BYTE) {
12603e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul         indexBytes = count * sizeof(GLubyte);
12703e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      }
12803e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      else {
12903e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul         ASSERT(type == GL_UNSIGNED_SHORT);
13003e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul         indexBytes = count * sizeof(GLushort);
13103e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      }
13203e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul
133d8c6719f95b1543296ac954f95d13b048ae48634Brian      /* make sure count doesn't go outside buffer bounds */
134a3c3bc9ece7e7c55c8832dbc8c50ab1c34f5bfe9Brian      if (indexBytes > ctx->Array.ElementArrayBufferObj->Size) {
13503e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul         _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
13603e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul         return GL_FALSE;
13703e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul      }
13803e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul   }
13937ece4df7c654b30b6720044b35a83694d7e5bb3Brian   else {
14037ece4df7c654b30b6720044b35a83694d7e5bb3Brian      /* not using a VBO */
14137ece4df7c654b30b6720044b35a83694d7e5bb3Brian      if (!indices)
14237ece4df7c654b30b6720044b35a83694d7e5bb3Brian         return GL_FALSE;
14337ece4df7c654b30b6720044b35a83694d7e5bb3Brian   }
14403e29a5f77c13b7b888bd8443cb2752850e47d6aBrian Paul
145a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   if (ctx->Const.CheckArrayBounds) {
146a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      /* find max array index */
147d8c6719f95b1543296ac954f95d13b048ae48634Brian      GLuint max = max_buffer_index(ctx, count, type, indices,
148d8c6719f95b1543296ac954f95d13b048ae48634Brian                                    ctx->Array.ElementArrayBufferObj);
149a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      if (max >= ctx->Array._MaxElement) {
150a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul         /* the max element is out of bounds of one or more enabled arrays */
151a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul         return GL_FALSE;
152a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      }
153a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   }
154cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
155cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   return GL_TRUE;
156cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell}
157cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
158cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
159cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith WhitwellGLboolean
16022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
16122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes				 GLuint start, GLuint end,
16222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes				 GLsizei count, GLenum type,
163cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell				 const GLvoid *indices)
164cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell{
16522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
166cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
167cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (count <= 0) {
168cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      if (count < 0)
169a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul	 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
170cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
171cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
172cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
173464bc3b6747108dd32e4a9fcfe6549a9d4a52a95Brian Paul   if (mode > GL_POLYGON) {
174a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)" );
175cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
176cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
177cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
178cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (end < start) {
17908836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
180cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
181cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
182cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
18322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes   if (type != GL_UNSIGNED_INT &&
18422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes       type != GL_UNSIGNED_BYTE &&
18561bac6014aa15e0bec134e290aebac18f9815299Brian Paul       type != GL_UNSIGNED_SHORT) {
186a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)" );
187cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
188cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
189cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
190cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (ctx->NewState)
191a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      _mesa_update_state(ctx);
192a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul
193a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   /* Always need vertex positions */
194ee34e6ef716bb630440299ac1efbc2055ef09ffdIan Romanick   if (!ctx->Array.ArrayObj->Vertex.Enabled
195d8c6719f95b1543296ac954f95d13b048ae48634Brian       && !(ctx->VertexProgram._Enabled
196d8c6719f95b1543296ac954f95d13b048ae48634Brian            && ctx->Array.ArrayObj->VertexAttrib[0].Enabled))
197a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      return GL_FALSE;
198a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul
19937ece4df7c654b30b6720044b35a83694d7e5bb3Brian   /* Vertex buffer object tests */
20037ece4df7c654b30b6720044b35a83694d7e5bb3Brian   if (ctx->Array.ElementArrayBufferObj->Name) {
201d8c6719f95b1543296ac954f95d13b048ae48634Brian      /* use indices in the buffer object */
202d8c6719f95b1543296ac954f95d13b048ae48634Brian      GLuint indexBytes;
203d8c6719f95b1543296ac954f95d13b048ae48634Brian
204d8c6719f95b1543296ac954f95d13b048ae48634Brian      if (type == GL_UNSIGNED_INT) {
205d8c6719f95b1543296ac954f95d13b048ae48634Brian         indexBytes = count * sizeof(GLuint);
206d8c6719f95b1543296ac954f95d13b048ae48634Brian      }
207d8c6719f95b1543296ac954f95d13b048ae48634Brian      else if (type == GL_UNSIGNED_BYTE) {
208d8c6719f95b1543296ac954f95d13b048ae48634Brian         indexBytes = count * sizeof(GLubyte);
209d8c6719f95b1543296ac954f95d13b048ae48634Brian      }
210d8c6719f95b1543296ac954f95d13b048ae48634Brian      else {
211d8c6719f95b1543296ac954f95d13b048ae48634Brian         ASSERT(type == GL_UNSIGNED_SHORT);
212d8c6719f95b1543296ac954f95d13b048ae48634Brian         indexBytes = count * sizeof(GLushort);
213d8c6719f95b1543296ac954f95d13b048ae48634Brian      }
214d8c6719f95b1543296ac954f95d13b048ae48634Brian
215d8c6719f95b1543296ac954f95d13b048ae48634Brian      /* make sure count doesn't go outside buffer bounds */
216d8c6719f95b1543296ac954f95d13b048ae48634Brian      if (indexBytes > ctx->Array.ElementArrayBufferObj->Size) {
217ef5935bc94a1439eb8f1731732a3eabd0e360407Brian         _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
218d8c6719f95b1543296ac954f95d13b048ae48634Brian         return GL_FALSE;
219d8c6719f95b1543296ac954f95d13b048ae48634Brian      }
22037ece4df7c654b30b6720044b35a83694d7e5bb3Brian   }
22137ece4df7c654b30b6720044b35a83694d7e5bb3Brian   else {
222d8c6719f95b1543296ac954f95d13b048ae48634Brian      /* not using a VBO */
22337ece4df7c654b30b6720044b35a83694d7e5bb3Brian      if (!indices)
22437ece4df7c654b30b6720044b35a83694d7e5bb3Brian         return GL_FALSE;
22537ece4df7c654b30b6720044b35a83694d7e5bb3Brian   }
22637ece4df7c654b30b6720044b35a83694d7e5bb3Brian
227a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   if (ctx->Const.CheckArrayBounds) {
228d8c6719f95b1543296ac954f95d13b048ae48634Brian      GLuint max = max_buffer_index(ctx, count, type, indices,
229d8c6719f95b1543296ac954f95d13b048ae48634Brian                                    ctx->Array.ElementArrayBufferObj);
230a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      if (max >= ctx->Array._MaxElement) {
231a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul         /* the max element is out of bounds of one or more enabled arrays */
232c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul         return GL_FALSE;
233a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      }
234c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul   }
235a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul
236c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul   return GL_TRUE;
237c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul}
238c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul
239cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
240c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul/**
241c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul * Called from the tnl module to error check the function parameters and
242c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul * verify that we really can draw something.
243c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul */
244cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith WhitwellGLboolean
24522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_mesa_validate_DrawArrays(GLcontext *ctx,
246cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell			  GLenum mode, GLint start, GLsizei count)
247cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell{
24822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
249cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
250c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul   if (count < 0) {
25108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
252cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
253cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
254cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
255464bc3b6747108dd32e4a9fcfe6549a9d4a52a95Brian Paul   if (mode > GL_POLYGON) {
25608836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
257cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return GL_FALSE;
258cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   }
259cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
260cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (ctx->NewState)
261a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul      _mesa_update_state(ctx);
262c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul
263a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   /* Always need vertex positions */
264d8c6719f95b1543296ac954f95d13b048ae48634Brian   if (!ctx->Array.ArrayObj->Vertex.Enabled
265d8c6719f95b1543296ac954f95d13b048ae48634Brian       && !ctx->Array.ArrayObj->VertexAttrib[0].Enabled)
266c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul      return GL_FALSE;
267c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul
268a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   if (ctx->Const.CheckArrayBounds) {
2692c9f50dd4acc65ddfeb8e3fde98137711167e579Brian Paul      if (start + count > (GLint) ctx->Array._MaxElement)
270c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul         return GL_FALSE;
271a2b9bad251b058f6255fa037b842c5465c0609a2Brian Paul   }
272c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul
273c5b1e81de48de5d8830bf5d92ff767ad1985e46eBrian Paul   return GL_TRUE;
274cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell}
275