blob: 4a1448deee6519f5112f84973bef230337ad4a5d [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 Paulb6e56002009-08-14 10:48:31 -060055 * Check if OK to draw arrays/elements.
Brian Paul88af3f82009-05-06 12:27:38 -060056 */
Alan Hourihane263b96e2009-01-15 11:51:39 +000057static GLboolean
Brian Paula48b0a52009-08-14 10:41:03 -060058check_valid_to_render(GLcontext *ctx, const char *function)
Alan Hourihane263b96e2009-01-15 11:51:39 +000059{
Brian Paulb6e56002009-08-14 10:48:31 -060060 if (!_mesa_valid_to_render(ctx, function)) {
Alan Hourihane263b96e2009-01-15 11:51:39 +000061 return GL_FALSE;
62 }
63
Brian Paul97dd2dd2009-03-02 12:27:16 -070064#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 &&
Alan Hourihane263b96e2009-01-15 11:51:39 +000075 !ctx->Array.ArrayObj->VertexAttrib[0].Enabled)
76 return GL_FALSE;
Brian Paul97dd2dd2009-03-02 12:27:16 -070077#endif
Alan Hourihane263b96e2009-01-15 11:51:39 +000078
79 return GL_TRUE;
80}
Briand8c67192007-08-20 13:12:20 +010081
Eric Anholt92d7ed82009-08-27 10:09:24 -070082static GLboolean
83check_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}
Brian Paul88af3f82009-05-06 12:27:38 -0600116
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 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000122GLboolean
123_mesa_validate_DrawElements(GLcontext *ctx,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000124 GLenum mode, GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700125 const GLvoid *indices, GLint basevertex)
Keith Whitwellcab974c2000-12-26 05:09:27 +0000126{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000127 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000128
129 if (count <= 0) {
130 if (count < 0)
Brian Paul08836342001-03-03 20:33:27 +0000131 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000132 return GL_FALSE;
133 }
134
Brian Paul464bc3b2003-04-21 15:04:30 +0000135 if (mode > GL_POLYGON) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000136 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000137 return GL_FALSE;
138 }
139
Gareth Hughes22144ab2001-03-12 00:48:37 +0000140 if (type != GL_UNSIGNED_INT &&
141 type != GL_UNSIGNED_BYTE &&
Keith Whitwellcab974c2000-12-26 05:09:27 +0000142 type != GL_UNSIGNED_SHORT)
143 {
Brian Paul08836342001-03-03 20:33:27 +0000144 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000145 return GL_FALSE;
146 }
147
148 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000149 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000150
Brian Paula48b0a52009-08-14 10:41:03 -0600151 if (!check_valid_to_render(ctx, "glDrawElements"))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000152 return GL_FALSE;
153
Brian Paul03e29a52003-12-04 03:16:27 +0000154 /* Vertex buffer object tests */
Brian Paul434ec3a2009-08-12 13:46:16 -0600155 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
Briand8c67192007-08-20 13:12:20 +0100156 /* use indices in the buffer object */
Briand8c67192007-08-20 13:12:20 +0100157 /* make sure count doesn't go outside buffer bounds */
Brian Paulbb1fb2a2009-05-06 12:37:10 -0600158 if (index_bytes(type, count) > ctx->Array.ElementArrayBufferObj->Size) {
Brian Paul03e29a52003-12-04 03:16:27 +0000159 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
160 return GL_FALSE;
161 }
Brian Paul03e29a52003-12-04 03:16:27 +0000162 }
Brian37ece4d2007-07-08 09:20:42 -0600163 else {
164 /* not using a VBO */
165 if (!indices)
166 return GL_FALSE;
167 }
Brian Paul03e29a52003-12-04 03:16:27 +0000168
Eric Anholt92d7ed82009-08-27 10:09:24 -0700169 if (!check_index_bounds(ctx, count, type, indices, basevertex))
170 return GL_FALSE;
Xiang, Haihao2394d202007-07-30 23:50:52 +0800171
Keith Whitwellcab974c2000-12-26 05:09:27 +0000172 return GL_TRUE;
173}
174
Brian Paul88af3f82009-05-06 12:27:38 -0600175
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 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000181GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000182_mesa_validate_DrawRangeElements(GLcontext *ctx, GLenum mode,
183 GLuint start, GLuint end,
184 GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700185 const GLvoid *indices, GLint basevertex)
Keith Whitwellcab974c2000-12-26 05:09:27 +0000186{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000187 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000188
189 if (count <= 0) {
190 if (count < 0)
Brian Paula2b9bad2003-11-10 19:08:37 +0000191 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000192 return GL_FALSE;
193 }
194
Brian Paul464bc3b2003-04-21 15:04:30 +0000195 if (mode > GL_POLYGON) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000196 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000197 return GL_FALSE;
198 }
199
200 if (end < start) {
Brian Paul08836342001-03-03 20:33:27 +0000201 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
Keith Whitwellcab974c2000-12-26 05:09:27 +0000202 return GL_FALSE;
203 }
204
Gareth Hughes22144ab2001-03-12 00:48:37 +0000205 if (type != GL_UNSIGNED_INT &&
206 type != GL_UNSIGNED_BYTE &&
Brian Paul61bac602002-04-21 21:04:46 +0000207 type != GL_UNSIGNED_SHORT) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000208 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000209 return GL_FALSE;
210 }
211
212 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000213 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000214
Brian Paula48b0a52009-08-14 10:41:03 -0600215 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000216 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000217
Brian37ece4d2007-07-08 09:20:42 -0600218 /* Vertex buffer object tests */
Brian Paul434ec3a2009-08-12 13:46:16 -0600219 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
Briand8c67192007-08-20 13:12:20 +0100220 /* use indices in the buffer object */
Briand8c67192007-08-20 13:12:20 +0100221 /* make sure count doesn't go outside buffer bounds */
Brian Paulbb1fb2a2009-05-06 12:37:10 -0600222 if (index_bytes(type, count) > ctx->Array.ElementArrayBufferObj->Size) {
Brianef5935b2007-09-23 13:56:46 -0600223 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
Briand8c67192007-08-20 13:12:20 +0100224 return GL_FALSE;
225 }
Brian37ece4d2007-07-08 09:20:42 -0600226 }
227 else {
Briand8c67192007-08-20 13:12:20 +0100228 /* not using a VBO */
Brian37ece4d2007-07-08 09:20:42 -0600229 if (!indices)
230 return GL_FALSE;
231 }
232
Eric Anholt92d7ed82009-08-27 10:09:24 -0700233 if (!check_index_bounds(ctx, count, type, indices, basevertex))
234 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000235
Brian Paulc5b1e812003-10-22 22:59:07 +0000236 return GL_TRUE;
237}
Keith Whitwellcab974c2000-12-26 05:09:27 +0000238
Brian Paulc5b1e812003-10-22 22:59:07 +0000239
240/**
241 * Called from the tnl module to error check the function parameters and
242 * verify that we really can draw something.
Brian Paul88af3f82009-05-06 12:27:38 -0600243 * \return GL_TRUE if OK to render, GL_FALSE if error found
Brian Paulc5b1e812003-10-22 22:59:07 +0000244 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000245GLboolean
Gareth Hughes22144ab2001-03-12 00:48:37 +0000246_mesa_validate_DrawArrays(GLcontext *ctx,
Keith Whitwellcab974c2000-12-26 05:09:27 +0000247 GLenum mode, GLint start, GLsizei count)
248{
Gareth Hughes22144ab2001-03-12 00:48:37 +0000249 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000250
Brian5cb20342007-10-31 09:38:51 -0600251 if (count <= 0) {
252 if (count < 0)
253 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000254 return GL_FALSE;
255 }
256
Brian Paul464bc3b2003-04-21 15:04:30 +0000257 if (mode > GL_POLYGON) {
Brian Paul08836342001-03-03 20:33:27 +0000258 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000259 return GL_FALSE;
260 }
261
262 if (ctx->NewState)
Brian Paula2b9bad2003-11-10 19:08:37 +0000263 _mesa_update_state(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000264
Brian Paula48b0a52009-08-14 10:41:03 -0600265 if (!check_valid_to_render(ctx, "glDrawArrays"))
Brian Paula2b9bad2003-11-10 19:08:37 +0000266 return GL_FALSE;
267
268 if (ctx->Const.CheckArrayBounds) {
Brian Paula185bcb2009-05-14 13:24:24 -0600269 if (start + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
Brian Paulc5b1e812003-10-22 22:59:07 +0000270 return GL_FALSE;
271 }
Brian Paulc5b1e812003-10-22 22:59:07 +0000272
273 return GL_TRUE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000274}