blob: 23a8012a30c0fb3332689fd441bf7a8d0883e42a [file] [log] [blame]
Ian Romanick9ac51f52003-06-05 00:50:18 +00001
Keith Whitwellcab974c2000-12-26 05:09:27 +00002/*
3 * Mesa 3-D graphics library
Brian Paul464bc3b2003-04-21 15:04:30 +00004 * Version: 5.1
Keith Whitwellcab974c2000-12-26 05:09:27 +00005 *
Brian Paul464bc3b2003-04-21 15:04:30 +00006 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
Keith Whitwellcab974c2000-12-26 05:09:27 +00007 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26#include "glheader.h"
Brian Paul8446d1b2001-01-02 21:40:57 +000027#include "api_validate.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000028#include "context.h"
Brian Paulc5b1e812003-10-22 22:59:07 +000029#include "image.h" /* for _mesa_sizeof_type() */
Brian Paul3c634522002-10-24 23:57:19 +000030#include "imports.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000031#include "mtypes.h"
Brian Paul8446d1b2001-01-02 21:40:57 +000032#include "state.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000033
Keith Whitwell58e99172001-01-05 02:26:48 +000034
Keith Whitwellcab974c2000-12-26 05:09:27 +000035GLboolean
36_mesa_validate_DrawElements(GLcontext *ctx,
Gareth Hughes22144ab2001-03-12 00:48:37 +000037 GLenum mode, GLsizei count, GLenum type,
Keith Whitwellcab974c2000-12-26 05:09:27 +000038 const GLvoid *indices)
39{
Gareth Hughes22144ab2001-03-12 00:48:37 +000040 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +000041
42 if (count <= 0) {
43 if (count < 0)
Brian Paul08836342001-03-03 20:33:27 +000044 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000045 return GL_FALSE;
46 }
47
Brian Paul464bc3b2003-04-21 15:04:30 +000048 if (mode > GL_POLYGON) {
Brian Paul08836342001-03-03 20:33:27 +000049 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000050 return GL_FALSE;
51 }
52
Gareth Hughes22144ab2001-03-12 00:48:37 +000053 if (type != GL_UNSIGNED_INT &&
54 type != GL_UNSIGNED_BYTE &&
Keith Whitwellcab974c2000-12-26 05:09:27 +000055 type != GL_UNSIGNED_SHORT)
56 {
Brian Paul08836342001-03-03 20:33:27 +000057 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000058 return GL_FALSE;
59 }
60
61 if (ctx->NewState)
Brian Paul08836342001-03-03 20:33:27 +000062 _mesa_update_state( ctx );
Keith Whitwellcab974c2000-12-26 05:09:27 +000063
Brian Paul61bac602002-04-21 21:04:46 +000064 if (ctx->Array.Vertex.Enabled
65 || (ctx->VertexProgram.Enabled && ctx->Array.VertexAttrib[0].Enabled))
66 return GL_TRUE;
67 else
Keith Whitwellcab974c2000-12-26 05:09:27 +000068 return GL_FALSE;
69
70 return GL_TRUE;
71}
72
73
74GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +000075_mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
76 GLuint start, GLuint end,
77 GLsizei count, GLenum type,
Keith Whitwellcab974c2000-12-26 05:09:27 +000078 const GLvoid *indices)
79{
Gareth Hughes22144ab2001-03-12 00:48:37 +000080 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +000081
82 if (count <= 0) {
83 if (count < 0)
Brian Paul08836342001-03-03 20:33:27 +000084 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000085 return GL_FALSE;
86 }
87
Brian Paul464bc3b2003-04-21 15:04:30 +000088 if (mode > GL_POLYGON) {
Brian Paul08836342001-03-03 20:33:27 +000089 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000090 return GL_FALSE;
91 }
92
93 if (end < start) {
Brian Paul08836342001-03-03 20:33:27 +000094 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
Keith Whitwellcab974c2000-12-26 05:09:27 +000095 return GL_FALSE;
96 }
97
Gareth Hughes22144ab2001-03-12 00:48:37 +000098 if (type != GL_UNSIGNED_INT &&
99 type != GL_UNSIGNED_BYTE &&
Brian Paul61bac602002-04-21 21:04:46 +0000100 type != GL_UNSIGNED_SHORT) {
Brian Paul08836342001-03-03 20:33:27 +0000101 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000102 return GL_FALSE;
103 }
104
105 if (ctx->NewState)
Brian Paul08836342001-03-03 20:33:27 +0000106 _mesa_update_state( ctx );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000107
Brian Paul61bac602002-04-21 21:04:46 +0000108 if (ctx->Array.Vertex.Enabled
109 || (ctx->VertexProgram.Enabled && ctx->Array.VertexAttrib[0].Enabled))
110 return GL_TRUE;
111 else
Keith Whitwellcab974c2000-12-26 05:09:27 +0000112 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000113}
114
115
Brian Paulc5b1e812003-10-22 22:59:07 +0000116/**
117 * Helper routine for validating vertex array data to be sure the given
118 * element lies within the legal range (i.e. vertex buffer object).
119 */
120static INLINE GLboolean
121validate(GLcontext *ctx, GLint attribArray,
122 const struct gl_client_array *array, GLint element)
123{
124 if (ctx->VertexProgram.Enabled
125 && attribArray >= 0
126 && ctx->Array.VertexAttrib[attribArray].Enabled) {
127 if (element >= ctx->Array.VertexAttrib[attribArray]._MaxElement)
128 return GL_FALSE;
129 }
130 else if (array && array->Enabled) {
131 if (element >= array->_MaxElement)
132 return GL_FALSE;
133 }
134 return GL_TRUE;
135}
Keith Whitwellcab974c2000-12-26 05:09:27 +0000136
Brian Paulc5b1e812003-10-22 22:59:07 +0000137
138/**
139 * Called from the tnl module to error check the function parameters and
140 * verify that we really can draw something.
141 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000142GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000143_mesa_validate_DrawArrays(GLcontext *ctx,
Keith Whitwellcab974c2000-12-26 05:09:27 +0000144 GLenum mode, GLint start, GLsizei count)
145{
Brian Paulc5b1e812003-10-22 22:59:07 +0000146 GLint i;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000147 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000148
Brian Paulc5b1e812003-10-22 22:59:07 +0000149 if (count < 0) {
Brian Paul08836342001-03-03 20:33:27 +0000150 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000151 return GL_FALSE;
152 }
153
Brian Paul464bc3b2003-04-21 15:04:30 +0000154 if (mode > GL_POLYGON) {
Brian Paul08836342001-03-03 20:33:27 +0000155 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000156 return GL_FALSE;
157 }
158
159 if (ctx->NewState)
Brian Paul08836342001-03-03 20:33:27 +0000160 _mesa_update_state( ctx );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000161
Brian Paulc5b1e812003-10-22 22:59:07 +0000162 /* Either the conventional vertex position array, or the 0th
163 * generic vertex attribute array is required to be enabled.
164 */
165 if (ctx->VertexProgram.Enabled
166 && ctx->Array.VertexAttrib[VERT_ATTRIB_POS].Enabled) {
167 if (start + count >= ctx->Array.VertexAttrib[VERT_ATTRIB_POS]._MaxElement)
168 return GL_FALSE;
169 }
170 else if (ctx->Array.Vertex.Enabled) {
171 if (start + count >= ctx->Array.Vertex._MaxElement)
172 return GL_FALSE;
173 }
174 else {
175 /* no vertex position array! */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000176 return GL_FALSE;
Brian Paulc5b1e812003-10-22 22:59:07 +0000177 }
178
179 /*
180 * OK, now check all the other enabled arrays to be sure the elements
181 * are in bounds.
182 */
183 if (!validate(ctx, VERT_ATTRIB_WEIGHT, NULL, start + count))
184 return GL_FALSE;
185
186 if (!validate(ctx, VERT_ATTRIB_NORMAL, &ctx->Array.Normal, start + count))
187 return GL_FALSE;
188
189 if (!validate(ctx, VERT_ATTRIB_COLOR0, &ctx->Array.Color, start + count))
190 return GL_FALSE;
191
192 if (!validate(ctx, VERT_ATTRIB_COLOR1, &ctx->Array.SecondaryColor, start + count))
193 return GL_FALSE;
194
195 if (!validate(ctx, VERT_ATTRIB_FOG, &ctx->Array.FogCoord, start + count))
196 return GL_FALSE;
197
198 if (!validate(ctx, VERT_ATTRIB_SIX, NULL, start + count))
199 return GL_FALSE;
200
201 if (!validate(ctx, VERT_ATTRIB_SEVEN, &ctx->Array.FogCoord, start + count))
202 return GL_FALSE;
203
204 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
205 if (!validate(ctx, VERT_ATTRIB_TEX0 + i, &ctx->Array.TexCoord[i], start + count))
206 return GL_FALSE;
207
208 if (!validate(ctx, -1, &ctx->Array.Index, start + count))
209 return GL_FALSE;
210
211 if (!validate(ctx, -1, &ctx->Array.EdgeFlag, start + count))
212 return GL_FALSE;
213
214 return GL_TRUE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000215}