blob: ec47f1d6d87355f0fa3b9853c924d8ad7fae2141 [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"
Vinson Lee6bf0ac02010-11-06 21:13:40 -070030#include "mfeatures.h"
Keith Whitwellcab974c2000-12-26 05:09:27 +000031#include "mtypes.h"
Eric Anholt56118ef2012-03-14 14:44:22 -070032#include "enums.h"
Eric Anholt92d7ed82009-08-27 10:09:24 -070033#include "vbo/vbo.h"
Keith Whitwell58e99172001-01-05 02:26:48 +000034
Brian Paulbb1fb2a2009-05-06 12:37:10 -060035
36/**
37 * \return number of bytes in array [count] of type.
38 */
39static GLsizei
40index_bytes(GLenum type, GLsizei count)
41{
42 if (type == GL_UNSIGNED_INT) {
43 return count * sizeof(GLuint);
44 }
45 else if (type == GL_UNSIGNED_BYTE) {
46 return count * sizeof(GLubyte);
47 }
48 else {
49 ASSERT(type == GL_UNSIGNED_SHORT);
50 return count * sizeof(GLushort);
51 }
52}
53
54
Briand8c67192007-08-20 13:12:20 +010055/**
Brian Paule5d29eb2009-09-21 14:07:35 -060056 * Find the max index in the given element/index buffer
57 */
58GLuint
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040059_mesa_max_buffer_index(struct gl_context *ctx, GLuint count, GLenum type,
Brian Paule5d29eb2009-09-21 14:07:35 -060060 const void *indices,
61 struct gl_buffer_object *elementBuf)
62{
63 const GLubyte *map = NULL;
64 GLuint max = 0;
65 GLuint i;
66
67 if (_mesa_is_bufferobj(elementBuf)) {
68 /* elements are in a user-defined buffer object. need to map it */
Ian Romanick28249bd2011-08-21 18:34:27 -070069 map = ctx->Driver.MapBufferRange(ctx, 0, elementBuf->Size,
70 GL_MAP_READ_BIT, elementBuf);
Brian Paule5d29eb2009-09-21 14:07:35 -060071 /* Actual address is the sum of pointers */
72 indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
73 }
74
75 if (type == GL_UNSIGNED_INT) {
76 for (i = 0; i < count; i++)
77 if (((GLuint *) indices)[i] > max)
78 max = ((GLuint *) indices)[i];
79 }
80 else if (type == GL_UNSIGNED_SHORT) {
81 for (i = 0; i < count; i++)
82 if (((GLushort *) indices)[i] > max)
83 max = ((GLushort *) indices)[i];
84 }
85 else {
86 ASSERT(type == GL_UNSIGNED_BYTE);
87 for (i = 0; i < count; i++)
88 if (((GLubyte *) indices)[i] > max)
89 max = ((GLubyte *) indices)[i];
90 }
91
92 if (map) {
Ian Romanick56f0c002011-08-21 16:59:30 -070093 ctx->Driver.UnmapBuffer(ctx, elementBuf);
Brian Paule5d29eb2009-09-21 14:07:35 -060094 }
95
96 return max;
97}
98
99
100/**
Brian Paulb6e56002009-08-14 10:48:31 -0600101 * Check if OK to draw arrays/elements.
Brian Paul88af3f82009-05-06 12:27:38 -0600102 */
Alan Hourihane263b96e2009-01-15 11:51:39 +0000103static GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400104check_valid_to_render(struct gl_context *ctx, const char *function)
Alan Hourihane263b96e2009-01-15 11:51:39 +0000105{
Brian Paulb6e56002009-08-14 10:48:31 -0600106 if (!_mesa_valid_to_render(ctx, function)) {
Alan Hourihane263b96e2009-01-15 11:51:39 +0000107 return GL_FALSE;
108 }
109
Kristian Høgsbergf67b0202010-05-24 10:01:38 -0400110 switch (ctx->API) {
Brian Paul97dd2dd2009-03-02 12:27:16 -0700111#if FEATURE_es2_glsl
Kristian Høgsbergf67b0202010-05-24 10:01:38 -0400112 case API_OPENGLES2:
113 /* For ES2, we can draw if any vertex array is enabled (and we
114 * should always have a vertex program/shader). */
115 if (ctx->Array.ArrayObj->_Enabled == 0x0 || !ctx->VertexProgram._Current)
116 return GL_FALSE;
117 break;
Brian Paul97dd2dd2009-03-02 12:27:16 -0700118#endif
Alan Hourihane263b96e2009-01-15 11:51:39 +0000119
Brian Paul43bdabd2011-05-18 16:19:06 -0600120#if FEATURE_ES1
Kristian Høgsbergf67b0202010-05-24 10:01:38 -0400121 case API_OPENGLES:
Brian Paul43bdabd2011-05-18 16:19:06 -0600122 /* For OpenGL ES, only draw if we have vertex positions
123 */
Mathias Fröhlich762c9762011-10-31 22:23:51 +0100124 if (!ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled)
Kristian Høgsbergf67b0202010-05-24 10:01:38 -0400125 return GL_FALSE;
126 break;
127#endif
128
Brian Paul43bdabd2011-05-18 16:19:06 -0600129#if FEATURE_GL
130 case API_OPENGL:
131 {
132 const struct gl_shader_program *vsProg =
133 ctx->Shader.CurrentVertexProgram;
134 GLboolean haveVertexShader = (vsProg && vsProg->LinkStatus);
135 GLboolean haveVertexProgram = ctx->VertexProgram._Enabled;
136 if (haveVertexShader || haveVertexProgram) {
137 /* Draw regardless of whether or not we have any vertex arrays.
138 * (Ex: could draw a point using a constant vertex pos)
139 */
140 return GL_TRUE;
141 }
142 else {
143 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
144 * array [0]).
145 */
Mathias Fröhlich762c9762011-10-31 22:23:51 +0100146 return (ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
147 ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
Brian Paul43bdabd2011-05-18 16:19:06 -0600148 }
149 }
150 break;
151#endif
152
Kristian Høgsbergf67b0202010-05-24 10:01:38 -0400153 default:
154 ASSERT_NO_FEATURE();
155 }
156
Alan Hourihane263b96e2009-01-15 11:51:39 +0000157 return GL_TRUE;
158}
Briand8c67192007-08-20 13:12:20 +0100159
Brian Paule9968eb2010-03-05 12:32:32 -0700160
161/**
162 * Do bounds checking on array element indexes. Check that the vertices
163 * pointed to by the indices don't lie outside buffer object bounds.
164 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
165 */
Eric Anholt92d7ed82009-08-27 10:09:24 -0700166static GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400167check_index_bounds(struct gl_context *ctx, GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700168 const GLvoid *indices, GLint basevertex)
169{
170 struct _mesa_prim prim;
171 struct _mesa_index_buffer ib;
172 GLuint min, max;
173
174 /* Only the X Server needs to do this -- otherwise, accessing outside
175 * array/BO bounds allows application termination.
176 */
177 if (!ctx->Const.CheckArrayBounds)
178 return GL_TRUE;
179
180 memset(&prim, 0, sizeof(prim));
181 prim.count = count;
182
183 memset(&ib, 0, sizeof(ib));
184 ib.type = type;
185 ib.ptr = indices;
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800186 ib.obj = ctx->Array.ArrayObj->ElementArrayBufferObj;
Eric Anholt92d7ed82009-08-27 10:09:24 -0700187
Yuanhan Liu42d49722011-12-31 14:22:46 +0800188 vbo_get_minmax_indices(ctx, &prim, &ib, &min, &max, 1);
Eric Anholt92d7ed82009-08-27 10:09:24 -0700189
Michel Dänzer391b3962010-03-05 00:15:40 +0100190 if ((int)(min + basevertex) < 0 ||
Roland Scheidegger1f4a8532012-02-06 01:04:28 +0100191 max + basevertex >= ctx->Array.ArrayObj->_MaxElement) {
Eric Anholt92d7ed82009-08-27 10:09:24 -0700192 /* the max element is out of bounds of one or more enabled arrays */
Brian Paule9968eb2010-03-05 12:32:32 -0700193 _mesa_warning(ctx, "glDrawElements() index=%u is out of bounds (max=%u)",
194 max, ctx->Array.ArrayObj->_MaxElement);
Eric Anholt92d7ed82009-08-27 10:09:24 -0700195 return GL_FALSE;
196 }
197
198 return GL_TRUE;
199}
Brian Paul88af3f82009-05-06 12:27:38 -0600200
Brian Paule9968eb2010-03-05 12:32:32 -0700201
Brian Paul88af3f82009-05-06 12:27:38 -0600202/**
Brian Paul0e6646d2011-09-21 08:22:07 -0600203 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
204 * etc? The set of legal values depends on whether geometry shaders/programs
205 * are supported.
206 */
207GLboolean
Eric Anholt7ca4f072012-03-14 14:39:15 -0700208_mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
Brian Paul0e6646d2011-09-21 08:22:07 -0600209{
210 if (ctx->Extensions.ARB_geometry_shader4 &&
211 mode > GL_TRIANGLE_STRIP_ADJACENCY_ARB) {
Eric Anholt7ca4f072012-03-14 14:39:15 -0700212 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
Brian Paul0e6646d2011-09-21 08:22:07 -0600213 return GL_FALSE;
214 }
215 else if (mode > GL_POLYGON) {
Eric Anholt7ca4f072012-03-14 14:39:15 -0700216 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
Brian Paul0e6646d2011-09-21 08:22:07 -0600217 return GL_FALSE;
218 }
Eric Anholt56118ef2012-03-14 14:44:22 -0700219
220 /* From the GL_EXT_transform_feedback spec:
221 *
222 * "The error INVALID_OPERATION is generated if Begin, or any command
223 * that performs an explicit Begin, is called when:
224 *
225 * * a geometry shader is not active and <mode> does not match the
226 * allowed begin modes for the current transform feedback state as
227 * given by table X.1.
228 *
229 * * a geometry shader is active and the output primitive type of the
230 * geometry shader does not match the allowed begin modes for the
231 * current transform feedback state as given by table X.1.
232 *
233 */
234 if (ctx->TransformFeedback.CurrentObject->Active &&
235 !ctx->TransformFeedback.CurrentObject->Paused) {
236 GLboolean pass = GL_TRUE;
237
238 switch (mode) {
239 case GL_POINTS:
240 pass = ctx->TransformFeedback.Mode == GL_POINTS;
241 break;
242 case GL_LINES:
243 case GL_LINE_STRIP:
244 case GL_LINE_LOOP:
245 pass = ctx->TransformFeedback.Mode == GL_LINES;
246 break;
247 default:
248 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
249 break;
250 }
251 if (!pass) {
252 _mesa_error(ctx, GL_INVALID_OPERATION,
253 "%s(mode=%s vs transform feedback %s)",
254 name,
255 _mesa_lookup_prim_by_nr(mode),
256 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
257 return GL_FALSE;
258 }
Brian Paul0e6646d2011-09-21 08:22:07 -0600259 }
Eric Anholt56118ef2012-03-14 14:44:22 -0700260
261 return GL_TRUE;
Brian Paul0e6646d2011-09-21 08:22:07 -0600262}
263
264
265/**
Brian Paul88af3f82009-05-06 12:27:38 -0600266 * Error checking for glDrawElements(). Includes parameter checking
267 * and VBO bounds checking.
268 * \return GL_TRUE if OK to render, GL_FALSE if error found
269 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000270GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400271_mesa_validate_DrawElements(struct gl_context *ctx,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000272 GLenum mode, GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700273 const GLvoid *indices, GLint basevertex)
Keith Whitwellcab974c2000-12-26 05:09:27 +0000274{
Marek Olšák62b97162012-06-27 06:10:30 +0200275 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000276
277 if (count <= 0) {
278 if (count < 0)
Brian Paul08836342001-03-03 20:33:27 +0000279 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000280 return GL_FALSE;
281 }
282
Eric Anholt7ca4f072012-03-14 14:39:15 -0700283 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElements")) {
Keith Whitwellcab974c2000-12-26 05:09:27 +0000284 return GL_FALSE;
285 }
286
Gareth Hughes22144ab2001-03-12 00:48:37 +0000287 if (type != GL_UNSIGNED_INT &&
288 type != GL_UNSIGNED_BYTE &&
Keith Whitwellcab974c2000-12-26 05:09:27 +0000289 type != GL_UNSIGNED_SHORT)
290 {
Brian Paul08836342001-03-03 20:33:27 +0000291 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000292 return GL_FALSE;
293 }
294
Brian Paula48b0a52009-08-14 10:41:03 -0600295 if (!check_valid_to_render(ctx, "glDrawElements"))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000296 return GL_FALSE;
297
Brian Paul03e29a52003-12-04 03:16:27 +0000298 /* Vertex buffer object tests */
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800299 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
Briand8c67192007-08-20 13:12:20 +0100300 /* use indices in the buffer object */
Briand8c67192007-08-20 13:12:20 +0100301 /* make sure count doesn't go outside buffer bounds */
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800302 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
Brian Paul03e29a52003-12-04 03:16:27 +0000303 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
304 return GL_FALSE;
305 }
Brian Paul03e29a52003-12-04 03:16:27 +0000306 }
Brian37ece4d2007-07-08 09:20:42 -0600307 else {
308 /* not using a VBO */
309 if (!indices)
310 return GL_FALSE;
311 }
Brian Paul03e29a52003-12-04 03:16:27 +0000312
Eric Anholt92d7ed82009-08-27 10:09:24 -0700313 if (!check_index_bounds(ctx, count, type, indices, basevertex))
314 return GL_FALSE;
Xiang, Haihao2394d202007-07-30 23:50:52 +0800315
Keith Whitwellcab974c2000-12-26 05:09:27 +0000316 return GL_TRUE;
317}
318
Brian Paul88af3f82009-05-06 12:27:38 -0600319
320/**
321 * Error checking for glDrawRangeElements(). Includes parameter checking
322 * and VBO bounds checking.
323 * \return GL_TRUE if OK to render, GL_FALSE if error found
324 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000325GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400326_mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000327 GLuint start, GLuint end,
328 GLsizei count, GLenum type,
Eric Anholt92d7ed82009-08-27 10:09:24 -0700329 const GLvoid *indices, GLint basevertex)
Keith Whitwellcab974c2000-12-26 05:09:27 +0000330{
Marek Olšák62b97162012-06-27 06:10:30 +0200331 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000332
333 if (count <= 0) {
334 if (count < 0)
Brian Paula2b9bad2003-11-10 19:08:37 +0000335 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000336 return GL_FALSE;
337 }
338
Eric Anholt7ca4f072012-03-14 14:39:15 -0700339 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawRangeElements")) {
Keith Whitwellcab974c2000-12-26 05:09:27 +0000340 return GL_FALSE;
341 }
342
343 if (end < start) {
Brian Paul08836342001-03-03 20:33:27 +0000344 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
Keith Whitwellcab974c2000-12-26 05:09:27 +0000345 return GL_FALSE;
346 }
347
Gareth Hughes22144ab2001-03-12 00:48:37 +0000348 if (type != GL_UNSIGNED_INT &&
349 type != GL_UNSIGNED_BYTE &&
Brian Paul61bac602002-04-21 21:04:46 +0000350 type != GL_UNSIGNED_SHORT) {
Brian Paula2b9bad2003-11-10 19:08:37 +0000351 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000352 return GL_FALSE;
353 }
354
Brian Paula48b0a52009-08-14 10:41:03 -0600355 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
Keith Whitwellcab974c2000-12-26 05:09:27 +0000356 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000357
Brian37ece4d2007-07-08 09:20:42 -0600358 /* Vertex buffer object tests */
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800359 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
Briand8c67192007-08-20 13:12:20 +0100360 /* use indices in the buffer object */
Briand8c67192007-08-20 13:12:20 +0100361 /* make sure count doesn't go outside buffer bounds */
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800362 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
Brianef5935b2007-09-23 13:56:46 -0600363 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
Briand8c67192007-08-20 13:12:20 +0100364 return GL_FALSE;
365 }
Brian37ece4d2007-07-08 09:20:42 -0600366 }
367 else {
Briand8c67192007-08-20 13:12:20 +0100368 /* not using a VBO */
Brian37ece4d2007-07-08 09:20:42 -0600369 if (!indices)
370 return GL_FALSE;
371 }
372
Eric Anholt92d7ed82009-08-27 10:09:24 -0700373 if (!check_index_bounds(ctx, count, type, indices, basevertex))
374 return GL_FALSE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000375
Brian Paulc5b1e812003-10-22 22:59:07 +0000376 return GL_TRUE;
377}
Keith Whitwellcab974c2000-12-26 05:09:27 +0000378
Brian Paulc5b1e812003-10-22 22:59:07 +0000379
380/**
381 * Called from the tnl module to error check the function parameters and
382 * verify that we really can draw something.
Brian Paul88af3f82009-05-06 12:27:38 -0600383 * \return GL_TRUE if OK to render, GL_FALSE if error found
Brian Paulc5b1e812003-10-22 22:59:07 +0000384 */
Keith Whitwellcab974c2000-12-26 05:09:27 +0000385GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400386_mesa_validate_DrawArrays(struct gl_context *ctx,
Keith Whitwellcab974c2000-12-26 05:09:27 +0000387 GLenum mode, GLint start, GLsizei count)
388{
Marek Olšák62b97162012-06-27 06:10:30 +0200389 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, GL_FALSE);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000390
Brian5cb20342007-10-31 09:38:51 -0600391 if (count <= 0) {
392 if (count < 0)
393 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000394 return GL_FALSE;
395 }
396
Eric Anholt7ca4f072012-03-14 14:39:15 -0700397 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArrays")) {
Keith Whitwellcab974c2000-12-26 05:09:27 +0000398 return GL_FALSE;
399 }
400
Brian Paula48b0a52009-08-14 10:41:03 -0600401 if (!check_valid_to_render(ctx, "glDrawArrays"))
Brian Paula2b9bad2003-11-10 19:08:37 +0000402 return GL_FALSE;
403
404 if (ctx->Const.CheckArrayBounds) {
Brian Paula185bcb2009-05-14 13:24:24 -0600405 if (start + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
Brian Paulc5b1e812003-10-22 22:59:07 +0000406 return GL_FALSE;
407 }
Brian Paulc5b1e812003-10-22 22:59:07 +0000408
409 return GL_TRUE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000410}
Brian Paulcf3193a2010-04-04 18:18:28 -0600411
412
413GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400414_mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
Brian Paul72f25512011-01-17 09:33:47 -0700415 GLsizei count, GLsizei numInstances)
Brian Paulcf3193a2010-04-04 18:18:28 -0600416{
Marek Olšák62b97162012-06-27 06:10:30 +0200417 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, GL_FALSE);
Brian Paulcf3193a2010-04-04 18:18:28 -0600418
419 if (count <= 0) {
420 if (count < 0)
421 _mesa_error(ctx, GL_INVALID_VALUE,
422 "glDrawArraysInstanced(count=%d)", count);
423 return GL_FALSE;
424 }
425
Eric Anholt767ba602012-02-28 13:33:53 -0800426 if (first < 0) {
427 _mesa_error(ctx, GL_INVALID_VALUE,
428 "glDrawArraysInstanced(start=%d)", first);
429 return GL_FALSE;
430 }
431
Eric Anholt7ca4f072012-03-14 14:39:15 -0700432 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArraysInstanced")) {
Brian Paulcf3193a2010-04-04 18:18:28 -0600433 return GL_FALSE;
434 }
435
Brian Paul72f25512011-01-17 09:33:47 -0700436 if (numInstances <= 0) {
437 if (numInstances < 0)
Brian Paulcf3193a2010-04-04 18:18:28 -0600438 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul72f25512011-01-17 09:33:47 -0700439 "glDrawArraysInstanced(numInstances=%d)", numInstances);
Brian Paulcf3193a2010-04-04 18:18:28 -0600440 return GL_FALSE;
441 }
442
443 if (!check_valid_to_render(ctx, "glDrawArraysInstanced(invalid to render)"))
444 return GL_FALSE;
445
Brian Paulcf3193a2010-04-04 18:18:28 -0600446 if (ctx->Const.CheckArrayBounds) {
447 if (first + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
448 return GL_FALSE;
449 }
450
451 return GL_TRUE;
452}
453
454
455GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400456_mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
Brian Paulcf3193a2010-04-04 18:18:28 -0600457 GLenum mode, GLsizei count, GLenum type,
Pierre-Eric Pelloux-Prayer09201cc2011-05-31 13:33:54 +0200458 const GLvoid *indices, GLsizei numInstances,
459 GLint basevertex)
Brian Paulcf3193a2010-04-04 18:18:28 -0600460{
Marek Olšák62b97162012-06-27 06:10:30 +0200461 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, GL_FALSE);
Brian Paulcf3193a2010-04-04 18:18:28 -0600462
463 if (count <= 0) {
464 if (count < 0)
465 _mesa_error(ctx, GL_INVALID_VALUE,
466 "glDrawElementsInstanced(count=%d)", count);
467 return GL_FALSE;
468 }
469
Eric Anholt7ca4f072012-03-14 14:39:15 -0700470 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElementsInstanced")) {
Brian Paulcf3193a2010-04-04 18:18:28 -0600471 return GL_FALSE;
472 }
473
474 if (type != GL_UNSIGNED_INT &&
475 type != GL_UNSIGNED_BYTE &&
476 type != GL_UNSIGNED_SHORT) {
477 _mesa_error(ctx, GL_INVALID_ENUM,
478 "glDrawElementsInstanced(type=0x%x)", type);
479 return GL_FALSE;
480 }
481
Brian Paul72f25512011-01-17 09:33:47 -0700482 if (numInstances <= 0) {
483 if (numInstances < 0)
Brian Paulcf3193a2010-04-04 18:18:28 -0600484 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul72f25512011-01-17 09:33:47 -0700485 "glDrawElementsInstanced(numInstances=%d)", numInstances);
Brian Paulcf3193a2010-04-04 18:18:28 -0600486 return GL_FALSE;
487 }
488
489 if (!check_valid_to_render(ctx, "glDrawElementsInstanced"))
490 return GL_FALSE;
491
492 /* Vertex buffer object tests */
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800493 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
Brian Paulcf3193a2010-04-04 18:18:28 -0600494 /* use indices in the buffer object */
495 /* make sure count doesn't go outside buffer bounds */
Yuanhan Liua0a5bd42011-11-23 15:59:06 +0800496 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
Brian Paulcf3193a2010-04-04 18:18:28 -0600497 _mesa_warning(ctx,
498 "glDrawElementsInstanced index out of buffer bounds");
499 return GL_FALSE;
500 }
501 }
502 else {
503 /* not using a VBO */
504 if (!indices)
505 return GL_FALSE;
506 }
507
Pierre-Eric Pelloux-Prayer09201cc2011-05-31 13:33:54 +0200508 if (!check_index_bounds(ctx, count, type, indices, basevertex))
Brian Paulcf3193a2010-04-04 18:18:28 -0600509 return GL_FALSE;
510
511 return GL_TRUE;
512}
Marek Olšák14bb9572011-12-09 17:00:23 +0100513
514
515#if FEATURE_EXT_transform_feedback
516
517GLboolean
518_mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
519 GLenum mode,
520 struct gl_transform_feedback_object *obj)
521{
Marek Olšák62b97162012-06-27 06:10:30 +0200522 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, GL_FALSE);
Marek Olšák14bb9572011-12-09 17:00:23 +0100523
Eric Anholt7ca4f072012-03-14 14:39:15 -0700524 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback")) {
Marek Olšák14bb9572011-12-09 17:00:23 +0100525 return GL_FALSE;
526 }
527
528 if (!obj) {
529 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback(name)");
530 return GL_FALSE;
531 }
532
533 if (!obj->EndedAnytime) {
534 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback");
535 return GL_FALSE;
536 }
537
538 if (!check_valid_to_render(ctx, "glDrawTransformFeedback")) {
539 return GL_FALSE;
540 }
541
542 return GL_TRUE;
543}
544
545#endif