mesa/main: Check for 0 size draws after validation.
[mesa.git] / src / mesa / main / api_validate.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdbool.h>
26 #include "glheader.h"
27 #include "api_validate.h"
28 #include "bufferobj.h"
29 #include "context.h"
30 #include "imports.h"
31 #include "mtypes.h"
32 #include "enums.h"
33 #include "vbo/vbo.h"
34 #include "transformfeedback.h"
35 #include <stdbool.h>
36
37
38 /**
39 * \return number of bytes in array [count] of type.
40 */
41 static GLsizei
42 index_bytes(GLenum type, GLsizei count)
43 {
44 if (type == GL_UNSIGNED_INT) {
45 return count * sizeof(GLuint);
46 }
47 else if (type == GL_UNSIGNED_BYTE) {
48 return count * sizeof(GLubyte);
49 }
50 else {
51 ASSERT(type == GL_UNSIGNED_SHORT);
52 return count * sizeof(GLushort);
53 }
54 }
55
56
57 /**
58 * Find the max index in the given element/index buffer
59 */
60 GLuint
61 _mesa_max_buffer_index(struct gl_context *ctx, GLuint count, GLenum type,
62 const void *indices,
63 struct gl_buffer_object *elementBuf)
64 {
65 const GLubyte *map = NULL;
66 GLuint max = 0;
67 GLuint i;
68
69 if (_mesa_is_bufferobj(elementBuf)) {
70 /* elements are in a user-defined buffer object. need to map it */
71 map = ctx->Driver.MapBufferRange(ctx, 0, elementBuf->Size,
72 GL_MAP_READ_BIT, elementBuf);
73 /* Actual address is the sum of pointers */
74 indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
75 }
76
77 if (type == GL_UNSIGNED_INT) {
78 for (i = 0; i < count; i++)
79 if (((GLuint *) indices)[i] > max)
80 max = ((GLuint *) indices)[i];
81 }
82 else if (type == GL_UNSIGNED_SHORT) {
83 for (i = 0; i < count; i++)
84 if (((GLushort *) indices)[i] > max)
85 max = ((GLushort *) indices)[i];
86 }
87 else {
88 ASSERT(type == GL_UNSIGNED_BYTE);
89 for (i = 0; i < count; i++)
90 if (((GLubyte *) indices)[i] > max)
91 max = ((GLubyte *) indices)[i];
92 }
93
94 if (map) {
95 ctx->Driver.UnmapBuffer(ctx, elementBuf);
96 }
97
98 return max;
99 }
100
101
102 /**
103 * Check if OK to draw arrays/elements.
104 */
105 static GLboolean
106 check_valid_to_render(struct gl_context *ctx, const char *function)
107 {
108 if (!_mesa_valid_to_render(ctx, function)) {
109 return GL_FALSE;
110 }
111
112 switch (ctx->API) {
113 case API_OPENGLES2:
114 /* For ES2, we can draw if any vertex array is enabled (and we
115 * should always have a vertex program/shader). */
116 if (ctx->Array.ArrayObj->_Enabled == 0x0 || !ctx->VertexProgram._Current)
117 return GL_FALSE;
118 break;
119
120 case API_OPENGLES:
121 /* For OpenGL ES, only draw if we have vertex positions
122 */
123 if (!ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled)
124 return GL_FALSE;
125 break;
126
127 case API_OPENGL_COMPAT:
128 case API_OPENGL_CORE:
129 {
130 const struct gl_shader_program *vsProg =
131 ctx->Shader.CurrentVertexProgram;
132 GLboolean haveVertexShader = (vsProg && vsProg->LinkStatus);
133 GLboolean haveVertexProgram = ctx->VertexProgram._Enabled;
134 if (haveVertexShader || haveVertexProgram) {
135 /* Draw regardless of whether or not we have any vertex arrays.
136 * (Ex: could draw a point using a constant vertex pos)
137 */
138 return GL_TRUE;
139 }
140 else {
141 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
142 * array [0]).
143 */
144 return (ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
145 ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
146 }
147 }
148 break;
149
150 default:
151 assert(!"Invalid API value in check_valid_to_render()");
152 }
153
154 return GL_TRUE;
155 }
156
157
158 /**
159 * Do bounds checking on array element indexes. Check that the vertices
160 * pointed to by the indices don't lie outside buffer object bounds.
161 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
162 */
163 static GLboolean
164 check_index_bounds(struct gl_context *ctx, GLsizei count, GLenum type,
165 const GLvoid *indices, GLint basevertex)
166 {
167 struct _mesa_prim prim;
168 struct _mesa_index_buffer ib;
169 GLuint min, max;
170
171 /* Only the X Server needs to do this -- otherwise, accessing outside
172 * array/BO bounds allows application termination.
173 */
174 if (!ctx->Const.CheckArrayBounds)
175 return GL_TRUE;
176
177 memset(&prim, 0, sizeof(prim));
178 prim.count = count;
179
180 memset(&ib, 0, sizeof(ib));
181 ib.type = type;
182 ib.ptr = indices;
183 ib.obj = ctx->Array.ArrayObj->ElementArrayBufferObj;
184
185 vbo_get_minmax_indices(ctx, &prim, &ib, &min, &max, 1);
186
187 if ((int)(min + basevertex) < 0 ||
188 max + basevertex >= ctx->Array.ArrayObj->_MaxElement) {
189 /* the max element is out of bounds of one or more enabled arrays */
190 _mesa_warning(ctx, "glDrawElements() index=%u is out of bounds (max=%u)",
191 max, ctx->Array.ArrayObj->_MaxElement);
192 return GL_FALSE;
193 }
194
195 return GL_TRUE;
196 }
197
198
199 /**
200 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
201 * etc? The set of legal values depends on whether geometry shaders/programs
202 * are supported.
203 * Note: This may be called during display list compilation.
204 */
205 bool
206 _mesa_is_valid_prim_mode(struct gl_context *ctx, GLenum mode)
207 {
208 switch (mode) {
209 case GL_POINTS:
210 case GL_LINES:
211 case GL_LINE_LOOP:
212 case GL_LINE_STRIP:
213 case GL_TRIANGLES:
214 case GL_TRIANGLE_STRIP:
215 case GL_TRIANGLE_FAN:
216 return true;
217 case GL_QUADS:
218 case GL_QUAD_STRIP:
219 case GL_POLYGON:
220 return (ctx->API == API_OPENGL_COMPAT);
221 case GL_LINES_ADJACENCY:
222 case GL_LINE_STRIP_ADJACENCY:
223 case GL_TRIANGLES_ADJACENCY:
224 case GL_TRIANGLE_STRIP_ADJACENCY:
225 return _mesa_has_geometry_shaders(ctx);
226 default:
227 return false;
228 }
229 }
230
231
232 /**
233 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
234 * etc? Also, do additional checking related to transformation feedback.
235 * Note: this function cannot be called during glNewList(GL_COMPILE) because
236 * this code depends on current transform feedback state.
237 */
238 GLboolean
239 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
240 {
241 bool valid_enum = _mesa_is_valid_prim_mode(ctx, mode);
242
243 if (!valid_enum) {
244 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
245 return GL_FALSE;
246 }
247
248 /* From the ARB_geometry_shader4 spec:
249 *
250 * The error INVALID_OPERATION is generated if Begin, or any command that
251 * implicitly calls Begin, is called when a geometry shader is active and:
252 *
253 * * the input primitive type of the current geometry shader is
254 * POINTS and <mode> is not POINTS,
255 *
256 * * the input primitive type of the current geometry shader is
257 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
258 *
259 * * the input primitive type of the current geometry shader is
260 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
261 * TRIANGLE_FAN,
262 *
263 * * the input primitive type of the current geometry shader is
264 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
265 * LINE_STRIP_ADJACENCY_ARB, or
266 *
267 * * the input primitive type of the current geometry shader is
268 * TRIANGLES_ADJACENCY_ARB and <mode> is not
269 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
270 *
271 */
272 if (ctx->Shader.CurrentGeometryProgram) {
273 const GLenum geom_mode =
274 ctx->Shader.CurrentGeometryProgram->Geom.InputType;
275 switch (mode) {
276 case GL_POINTS:
277 valid_enum = (geom_mode == GL_POINTS);
278 break;
279 case GL_LINES:
280 case GL_LINE_LOOP:
281 case GL_LINE_STRIP:
282 valid_enum = (geom_mode == GL_LINES);
283 break;
284 case GL_TRIANGLES:
285 case GL_TRIANGLE_STRIP:
286 case GL_TRIANGLE_FAN:
287 valid_enum = (geom_mode == GL_TRIANGLES);
288 break;
289 case GL_QUADS:
290 case GL_QUAD_STRIP:
291 case GL_POLYGON:
292 valid_enum = false;
293 break;
294 case GL_LINES_ADJACENCY:
295 case GL_LINE_STRIP_ADJACENCY:
296 valid_enum = (geom_mode == GL_LINES_ADJACENCY);
297 break;
298 case GL_TRIANGLES_ADJACENCY:
299 case GL_TRIANGLE_STRIP_ADJACENCY:
300 valid_enum = (geom_mode == GL_TRIANGLES_ADJACENCY);
301 break;
302 default:
303 valid_enum = false;
304 break;
305 }
306 if (!valid_enum) {
307 _mesa_error(ctx, GL_INVALID_OPERATION,
308 "%s(mode=%s vs geometry shader input %s)",
309 name,
310 _mesa_lookup_prim_by_nr(mode),
311 _mesa_lookup_prim_by_nr(geom_mode));
312 return GL_FALSE;
313 }
314 }
315
316 /* From the GL_EXT_transform_feedback spec:
317 *
318 * "The error INVALID_OPERATION is generated if Begin, or any command
319 * that performs an explicit Begin, is called when:
320 *
321 * * a geometry shader is not active and <mode> does not match the
322 * allowed begin modes for the current transform feedback state as
323 * given by table X.1.
324 *
325 * * a geometry shader is active and the output primitive type of the
326 * geometry shader does not match the allowed begin modes for the
327 * current transform feedback state as given by table X.1.
328 *
329 */
330 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
331 GLboolean pass = GL_TRUE;
332
333 switch (mode) {
334 case GL_POINTS:
335 pass = ctx->TransformFeedback.Mode == GL_POINTS;
336 break;
337 case GL_LINES:
338 case GL_LINE_STRIP:
339 case GL_LINE_LOOP:
340 pass = ctx->TransformFeedback.Mode == GL_LINES;
341 break;
342 default:
343 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
344 break;
345 }
346 if (!pass) {
347 _mesa_error(ctx, GL_INVALID_OPERATION,
348 "%s(mode=%s vs transform feedback %s)",
349 name,
350 _mesa_lookup_prim_by_nr(mode),
351 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
352 return GL_FALSE;
353 }
354 }
355
356 return GL_TRUE;
357 }
358
359 /**
360 * Verify that the element type is valid.
361 *
362 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
363 */
364 static bool
365 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
366 {
367 switch (type) {
368 case GL_UNSIGNED_BYTE:
369 case GL_UNSIGNED_SHORT:
370 case GL_UNSIGNED_INT:
371 return true;
372
373 default:
374 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
375 _mesa_lookup_enum_by_nr(type));
376 return false;
377 }
378 }
379
380 /**
381 * Error checking for glDrawElements(). Includes parameter checking
382 * and VBO bounds checking.
383 * \return GL_TRUE if OK to render, GL_FALSE if error found
384 */
385 GLboolean
386 _mesa_validate_DrawElements(struct gl_context *ctx,
387 GLenum mode, GLsizei count, GLenum type,
388 const GLvoid *indices, GLint basevertex)
389 {
390 FLUSH_CURRENT(ctx, 0);
391
392 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
393 * Primitive Capture):
394 *
395 * The error INVALID_OPERATION is also generated by DrawElements,
396 * DrawElementsInstanced, and DrawRangeElements while transform feedback
397 * is active and not paused, regardless of mode.
398 */
399 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
400 _mesa_error(ctx, GL_INVALID_OPERATION,
401 "glDrawElements(transform feedback active)");
402 return GL_FALSE;
403 }
404
405 if (count < 0) {
406 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
407 return GL_FALSE;
408 }
409
410 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElements")) {
411 return GL_FALSE;
412 }
413
414 if (!valid_elements_type(ctx, type, "glDrawElements"))
415 return GL_FALSE;
416
417 if (!check_valid_to_render(ctx, "glDrawElements"))
418 return GL_FALSE;
419
420 /* Vertex buffer object tests */
421 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
422 /* use indices in the buffer object */
423 /* make sure count doesn't go outside buffer bounds */
424 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
425 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
426 return GL_FALSE;
427 }
428 }
429 else {
430 /* not using a VBO */
431 if (!indices)
432 return GL_FALSE;
433 }
434
435 if (!check_index_bounds(ctx, count, type, indices, basevertex))
436 return GL_FALSE;
437
438 if (count == 0)
439 return GL_FALSE;
440
441 return GL_TRUE;
442 }
443
444
445 /**
446 * Error checking for glMultiDrawElements(). Includes parameter checking
447 * and VBO bounds checking.
448 * \return GL_TRUE if OK to render, GL_FALSE if error found
449 */
450 GLboolean
451 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
452 GLenum mode, const GLsizei *count,
453 GLenum type, const GLvoid * const *indices,
454 GLuint primcount, const GLint *basevertex)
455 {
456 unsigned i;
457
458 FLUSH_CURRENT(ctx, 0);
459
460 for (i = 0; i < primcount; i++) {
461 if (count[i] < 0) {
462 _mesa_error(ctx, GL_INVALID_VALUE,
463 "glMultiDrawElements(count)" );
464 return GL_FALSE;
465 }
466 }
467
468 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
469 return GL_FALSE;
470 }
471
472 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
473 return GL_FALSE;
474
475 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
476 return GL_FALSE;
477
478 /* Vertex buffer object tests */
479 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
480 /* use indices in the buffer object */
481 /* make sure count doesn't go outside buffer bounds */
482 for (i = 0; i < primcount; i++) {
483 if (index_bytes(type, count[i]) >
484 ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
485 _mesa_warning(ctx,
486 "glMultiDrawElements index out of buffer bounds");
487 return GL_FALSE;
488 }
489 }
490 }
491 else {
492 /* not using a VBO */
493 for (i = 0; i < primcount; i++) {
494 if (!indices[i])
495 return GL_FALSE;
496 }
497 }
498
499 for (i = 0; i < primcount; i++) {
500 if (!check_index_bounds(ctx, count[i], type, indices[i],
501 basevertex ? basevertex[i] : 0))
502 return GL_FALSE;
503 }
504
505 return GL_TRUE;
506 }
507
508
509 /**
510 * Error checking for glDrawRangeElements(). Includes parameter checking
511 * and VBO bounds checking.
512 * \return GL_TRUE if OK to render, GL_FALSE if error found
513 */
514 GLboolean
515 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
516 GLuint start, GLuint end,
517 GLsizei count, GLenum type,
518 const GLvoid *indices, GLint basevertex)
519 {
520 FLUSH_CURRENT(ctx, 0);
521
522 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
523 * Primitive Capture):
524 *
525 * The error INVALID_OPERATION is also generated by DrawElements,
526 * DrawElementsInstanced, and DrawRangeElements while transform feedback
527 * is active and not paused, regardless of mode.
528 */
529 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
530 _mesa_error(ctx, GL_INVALID_OPERATION,
531 "glDrawElements(transform feedback active)");
532 return GL_FALSE;
533 }
534
535 if (count < 0) {
536 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
537 return GL_FALSE;
538 }
539
540 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawRangeElements")) {
541 return GL_FALSE;
542 }
543
544 if (end < start) {
545 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
546 return GL_FALSE;
547 }
548
549 if (!valid_elements_type(ctx, type, "glDrawRangeElements"))
550 return GL_FALSE;
551
552 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
553 return GL_FALSE;
554
555 /* Vertex buffer object tests */
556 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
557 /* use indices in the buffer object */
558 /* make sure count doesn't go outside buffer bounds */
559 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
560 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
561 return GL_FALSE;
562 }
563 }
564 else {
565 /* not using a VBO */
566 if (!indices)
567 return GL_FALSE;
568 }
569
570 if (!check_index_bounds(ctx, count, type, indices, basevertex))
571 return GL_FALSE;
572
573 if (count == 0)
574 return GL_FALSE;
575
576 return GL_TRUE;
577 }
578
579
580 /**
581 * Called from the tnl module to error check the function parameters and
582 * verify that we really can draw something.
583 * \return GL_TRUE if OK to render, GL_FALSE if error found
584 */
585 GLboolean
586 _mesa_validate_DrawArrays(struct gl_context *ctx,
587 GLenum mode, GLint start, GLsizei count)
588 {
589 struct gl_transform_feedback_object *xfb_obj
590 = ctx->TransformFeedback.CurrentObject;
591 FLUSH_CURRENT(ctx, 0);
592
593 if (count < 0) {
594 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
595 return GL_FALSE;
596 }
597
598 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArrays")) {
599 return GL_FALSE;
600 }
601
602 if (!check_valid_to_render(ctx, "glDrawArrays"))
603 return GL_FALSE;
604
605 if (ctx->Const.CheckArrayBounds) {
606 if (start + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
607 return GL_FALSE;
608 }
609
610 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
611 * Primitive Capture):
612 *
613 * The error INVALID_OPERATION is generated by DrawArrays and
614 * DrawArraysInstanced if recording the vertices of a primitive to the
615 * buffer objects being used for transform feedback purposes would result
616 * in either exceeding the limits of any buffer object’s size, or in
617 * exceeding the end position offset + size − 1, as set by
618 * BindBufferRange.
619 *
620 * This is in contrast to the behaviour of desktop GL, where the extra
621 * primitives are silently dropped from the transform feedback buffer.
622 */
623 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
624 size_t prim_count = vbo_count_tessellated_primitives(mode, count, 1);
625 if (xfb_obj->GlesRemainingPrims < prim_count) {
626 _mesa_error(ctx, GL_INVALID_OPERATION,
627 "glDrawArrays(exceeds transform feedback size)");
628 return GL_FALSE;
629 }
630 xfb_obj->GlesRemainingPrims -= prim_count;
631 }
632
633 if (count == 0)
634 return GL_FALSE;
635
636 return GL_TRUE;
637 }
638
639
640 GLboolean
641 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
642 GLsizei count, GLsizei numInstances)
643 {
644 struct gl_transform_feedback_object *xfb_obj
645 = ctx->TransformFeedback.CurrentObject;
646 FLUSH_CURRENT(ctx, 0);
647
648 if (count < 0) {
649 _mesa_error(ctx, GL_INVALID_VALUE,
650 "glDrawArraysInstanced(count=%d)", count);
651 return GL_FALSE;
652 }
653
654 if (first < 0) {
655 _mesa_error(ctx, GL_INVALID_VALUE,
656 "glDrawArraysInstanced(start=%d)", first);
657 return GL_FALSE;
658 }
659
660 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArraysInstanced")) {
661 return GL_FALSE;
662 }
663
664 if (numInstances <= 0) {
665 if (numInstances < 0)
666 _mesa_error(ctx, GL_INVALID_VALUE,
667 "glDrawArraysInstanced(numInstances=%d)", numInstances);
668 return GL_FALSE;
669 }
670
671 if (!check_valid_to_render(ctx, "glDrawArraysInstanced(invalid to render)"))
672 return GL_FALSE;
673
674 if (ctx->Const.CheckArrayBounds) {
675 if (first + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
676 return GL_FALSE;
677 }
678
679 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
680 * Primitive Capture):
681 *
682 * The error INVALID_OPERATION is generated by DrawArrays and
683 * DrawArraysInstanced if recording the vertices of a primitive to the
684 * buffer objects being used for transform feedback purposes would result
685 * in either exceeding the limits of any buffer object’s size, or in
686 * exceeding the end position offset + size − 1, as set by
687 * BindBufferRange.
688 *
689 * This is in contrast to the behaviour of desktop GL, where the extra
690 * primitives are silently dropped from the transform feedback buffer.
691 */
692 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
693 size_t prim_count
694 = vbo_count_tessellated_primitives(mode, count, numInstances);
695 if (xfb_obj->GlesRemainingPrims < prim_count) {
696 _mesa_error(ctx, GL_INVALID_OPERATION,
697 "glDrawArraysInstanced(exceeds transform feedback size)");
698 return GL_FALSE;
699 }
700 xfb_obj->GlesRemainingPrims -= prim_count;
701 }
702
703 if (count == 0)
704 return GL_FALSE;
705
706 return GL_TRUE;
707 }
708
709
710 GLboolean
711 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
712 GLenum mode, GLsizei count, GLenum type,
713 const GLvoid *indices, GLsizei numInstances,
714 GLint basevertex)
715 {
716 FLUSH_CURRENT(ctx, 0);
717
718 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
719 * Primitive Capture):
720 *
721 * The error INVALID_OPERATION is also generated by DrawElements,
722 * DrawElementsInstanced, and DrawRangeElements while transform feedback
723 * is active and not paused, regardless of mode.
724 */
725 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
726 _mesa_error(ctx, GL_INVALID_OPERATION,
727 "glDrawElements(transform feedback active)");
728 return GL_FALSE;
729 }
730
731 if (count < 0) {
732 _mesa_error(ctx, GL_INVALID_VALUE,
733 "glDrawElementsInstanced(count=%d)", count);
734 return GL_FALSE;
735 }
736
737 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElementsInstanced")) {
738 return GL_FALSE;
739 }
740
741 if (!valid_elements_type(ctx, type, "glDrawElementsInstanced"))
742 return GL_FALSE;
743
744 if (numInstances <= 0) {
745 if (numInstances < 0)
746 _mesa_error(ctx, GL_INVALID_VALUE,
747 "glDrawElementsInstanced(numInstances=%d)", numInstances);
748 return GL_FALSE;
749 }
750
751 if (!check_valid_to_render(ctx, "glDrawElementsInstanced"))
752 return GL_FALSE;
753
754 /* Vertex buffer object tests */
755 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
756 /* use indices in the buffer object */
757 /* make sure count doesn't go outside buffer bounds */
758 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
759 _mesa_warning(ctx,
760 "glDrawElementsInstanced index out of buffer bounds");
761 return GL_FALSE;
762 }
763 }
764 else {
765 /* not using a VBO */
766 if (!indices)
767 return GL_FALSE;
768 }
769
770 if (count == 0)
771 return GL_FALSE;
772
773 if (!check_index_bounds(ctx, count, type, indices, basevertex))
774 return GL_FALSE;
775
776 return GL_TRUE;
777 }
778
779
780 GLboolean
781 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
782 GLenum mode,
783 struct gl_transform_feedback_object *obj,
784 GLuint stream,
785 GLsizei numInstances)
786 {
787 FLUSH_CURRENT(ctx, 0);
788
789 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
790 return GL_FALSE;
791 }
792
793 if (!obj) {
794 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
795 return GL_FALSE;
796 }
797
798 if (!obj->EndedAnytime) {
799 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
800 return GL_FALSE;
801 }
802
803 if (stream >= ctx->Const.MaxVertexStreams) {
804 _mesa_error(ctx, GL_INVALID_VALUE,
805 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
806 return GL_FALSE;
807 }
808
809 if (numInstances <= 0) {
810 if (numInstances < 0)
811 _mesa_error(ctx, GL_INVALID_VALUE,
812 "glDrawTransformFeedback*Instanced(numInstances=%d)",
813 numInstances);
814 return GL_FALSE;
815 }
816
817 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {
818 return GL_FALSE;
819 }
820
821 return GL_TRUE;
822 }