mesa: In core profile, refuse to draw unless a VAO is bound.
[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 MAP_INTERNAL);
74 /* Actual address is the sum of pointers */
75 indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
76 }
77
78 if (type == GL_UNSIGNED_INT) {
79 for (i = 0; i < count; i++)
80 if (((GLuint *) indices)[i] > max)
81 max = ((GLuint *) indices)[i];
82 }
83 else if (type == GL_UNSIGNED_SHORT) {
84 for (i = 0; i < count; i++)
85 if (((GLushort *) indices)[i] > max)
86 max = ((GLushort *) indices)[i];
87 }
88 else {
89 ASSERT(type == GL_UNSIGNED_BYTE);
90 for (i = 0; i < count; i++)
91 if (((GLubyte *) indices)[i] > max)
92 max = ((GLubyte *) indices)[i];
93 }
94
95 if (map) {
96 ctx->Driver.UnmapBuffer(ctx, elementBuf, MAP_INTERNAL);
97 }
98
99 return max;
100 }
101
102
103 /**
104 * Check if OK to draw arrays/elements.
105 */
106 static GLboolean
107 check_valid_to_render(struct gl_context *ctx, const char *function)
108 {
109 if (!_mesa_valid_to_render(ctx, function)) {
110 return GL_FALSE;
111 }
112
113 switch (ctx->API) {
114 case API_OPENGLES2:
115 /* For ES2, we can draw if any vertex array is enabled (and we
116 * should always have a vertex program/shader). */
117 if (ctx->Array.VAO->_Enabled == 0x0 || !ctx->VertexProgram._Current)
118 return GL_FALSE;
119 break;
120
121 case API_OPENGLES:
122 /* For OpenGL ES, only draw if we have vertex positions
123 */
124 if (!ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled)
125 return GL_FALSE;
126 break;
127
128 case API_OPENGL_CORE:
129 if (ctx->Array.VAO == ctx->Array.DefaultVAO)
130 return GL_FALSE;
131 /* fallthrough */
132 case API_OPENGL_COMPAT:
133 {
134 const struct gl_shader_program *vsProg =
135 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
136 GLboolean haveVertexShader = (vsProg && vsProg->LinkStatus);
137 GLboolean haveVertexProgram = ctx->VertexProgram._Enabled;
138 if (haveVertexShader || haveVertexProgram) {
139 /* Draw regardless of whether or not we have any vertex arrays.
140 * (Ex: could draw a point using a constant vertex pos)
141 */
142 return GL_TRUE;
143 }
144 else {
145 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
146 * array [0]).
147 */
148 return (ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
149 ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
150 }
151 }
152 break;
153
154 default:
155 assert(!"Invalid API value in check_valid_to_render()");
156 }
157
158 return GL_TRUE;
159 }
160
161
162 /**
163 * Do bounds checking on array element indexes. Check that the vertices
164 * pointed to by the indices don't lie outside buffer object bounds.
165 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
166 */
167 static GLboolean
168 check_index_bounds(struct gl_context *ctx, GLsizei count, GLenum type,
169 const GLvoid *indices, GLint basevertex)
170 {
171 struct _mesa_prim prim;
172 struct _mesa_index_buffer ib;
173 GLuint min, max;
174
175 /* Only the X Server needs to do this -- otherwise, accessing outside
176 * array/BO bounds allows application termination.
177 */
178 if (!ctx->Const.CheckArrayBounds)
179 return GL_TRUE;
180
181 memset(&prim, 0, sizeof(prim));
182 prim.count = count;
183
184 memset(&ib, 0, sizeof(ib));
185 ib.type = type;
186 ib.ptr = indices;
187 ib.obj = ctx->Array.VAO->IndexBufferObj;
188
189 vbo_get_minmax_indices(ctx, &prim, &ib, &min, &max, 1);
190
191 if ((int)(min + basevertex) < 0 ||
192 max + basevertex >= ctx->Array.VAO->_MaxElement) {
193 /* the max element is out of bounds of one or more enabled arrays */
194 _mesa_warning(ctx, "glDrawElements() index=%u is out of bounds (max=%u)",
195 max, ctx->Array.VAO->_MaxElement);
196 return GL_FALSE;
197 }
198
199 return GL_TRUE;
200 }
201
202
203 /**
204 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
205 * etc? The set of legal values depends on whether geometry shaders/programs
206 * are supported.
207 * Note: This may be called during display list compilation.
208 */
209 bool
210 _mesa_is_valid_prim_mode(struct gl_context *ctx, GLenum mode)
211 {
212 switch (mode) {
213 case GL_POINTS:
214 case GL_LINES:
215 case GL_LINE_LOOP:
216 case GL_LINE_STRIP:
217 case GL_TRIANGLES:
218 case GL_TRIANGLE_STRIP:
219 case GL_TRIANGLE_FAN:
220 return true;
221 case GL_QUADS:
222 case GL_QUAD_STRIP:
223 case GL_POLYGON:
224 return (ctx->API == API_OPENGL_COMPAT);
225 case GL_LINES_ADJACENCY:
226 case GL_LINE_STRIP_ADJACENCY:
227 case GL_TRIANGLES_ADJACENCY:
228 case GL_TRIANGLE_STRIP_ADJACENCY:
229 return _mesa_has_geometry_shaders(ctx);
230 default:
231 return false;
232 }
233 }
234
235
236 /**
237 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
238 * etc? Also, do additional checking related to transformation feedback.
239 * Note: this function cannot be called during glNewList(GL_COMPILE) because
240 * this code depends on current transform feedback state.
241 */
242 GLboolean
243 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
244 {
245 bool valid_enum = _mesa_is_valid_prim_mode(ctx, mode);
246
247 if (!valid_enum) {
248 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
249 return GL_FALSE;
250 }
251
252 /* From the ARB_geometry_shader4 spec:
253 *
254 * The error INVALID_OPERATION is generated if Begin, or any command that
255 * implicitly calls Begin, is called when a geometry shader is active and:
256 *
257 * * the input primitive type of the current geometry shader is
258 * POINTS and <mode> is not POINTS,
259 *
260 * * the input primitive type of the current geometry shader is
261 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
262 *
263 * * the input primitive type of the current geometry shader is
264 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
265 * TRIANGLE_FAN,
266 *
267 * * the input primitive type of the current geometry shader is
268 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
269 * LINE_STRIP_ADJACENCY_ARB, or
270 *
271 * * the input primitive type of the current geometry shader is
272 * TRIANGLES_ADJACENCY_ARB and <mode> is not
273 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
274 *
275 */
276 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
277 const GLenum geom_mode =
278 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->Geom.InputType;
279 switch (mode) {
280 case GL_POINTS:
281 valid_enum = (geom_mode == GL_POINTS);
282 break;
283 case GL_LINES:
284 case GL_LINE_LOOP:
285 case GL_LINE_STRIP:
286 valid_enum = (geom_mode == GL_LINES);
287 break;
288 case GL_TRIANGLES:
289 case GL_TRIANGLE_STRIP:
290 case GL_TRIANGLE_FAN:
291 valid_enum = (geom_mode == GL_TRIANGLES);
292 break;
293 case GL_QUADS:
294 case GL_QUAD_STRIP:
295 case GL_POLYGON:
296 valid_enum = false;
297 break;
298 case GL_LINES_ADJACENCY:
299 case GL_LINE_STRIP_ADJACENCY:
300 valid_enum = (geom_mode == GL_LINES_ADJACENCY);
301 break;
302 case GL_TRIANGLES_ADJACENCY:
303 case GL_TRIANGLE_STRIP_ADJACENCY:
304 valid_enum = (geom_mode == GL_TRIANGLES_ADJACENCY);
305 break;
306 default:
307 valid_enum = false;
308 break;
309 }
310 if (!valid_enum) {
311 _mesa_error(ctx, GL_INVALID_OPERATION,
312 "%s(mode=%s vs geometry shader input %s)",
313 name,
314 _mesa_lookup_prim_by_nr(mode),
315 _mesa_lookup_prim_by_nr(geom_mode));
316 return GL_FALSE;
317 }
318 }
319
320 /* From the GL_EXT_transform_feedback spec:
321 *
322 * "The error INVALID_OPERATION is generated if Begin, or any command
323 * that performs an explicit Begin, is called when:
324 *
325 * * a geometry shader is not active and <mode> does not match the
326 * allowed begin modes for the current transform feedback state as
327 * given by table X.1.
328 *
329 * * a geometry shader is active and the output primitive type of the
330 * geometry shader does not match the allowed begin modes for the
331 * current transform feedback state as given by table X.1.
332 *
333 */
334 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
335 GLboolean pass = GL_TRUE;
336
337 if(ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
338 switch (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->Geom.OutputType) {
339 case GL_POINTS:
340 pass = ctx->TransformFeedback.Mode == GL_POINTS;
341 break;
342 case GL_LINE_STRIP:
343 pass = ctx->TransformFeedback.Mode == GL_LINES;
344 break;
345 case GL_TRIANGLE_STRIP:
346 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
347 break;
348 default:
349 pass = GL_FALSE;
350 }
351 }
352 else {
353 switch (mode) {
354 case GL_POINTS:
355 pass = ctx->TransformFeedback.Mode == GL_POINTS;
356 break;
357 case GL_LINES:
358 case GL_LINE_STRIP:
359 case GL_LINE_LOOP:
360 pass = ctx->TransformFeedback.Mode == GL_LINES;
361 break;
362 default:
363 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
364 break;
365 }
366 }
367 if (!pass) {
368 _mesa_error(ctx, GL_INVALID_OPERATION,
369 "%s(mode=%s vs transform feedback %s)",
370 name,
371 _mesa_lookup_prim_by_nr(mode),
372 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
373 return GL_FALSE;
374 }
375 }
376
377 return GL_TRUE;
378 }
379
380 /**
381 * Verify that the element type is valid.
382 *
383 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
384 */
385 static bool
386 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
387 {
388 switch (type) {
389 case GL_UNSIGNED_BYTE:
390 case GL_UNSIGNED_SHORT:
391 case GL_UNSIGNED_INT:
392 return true;
393
394 default:
395 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
396 _mesa_lookup_enum_by_nr(type));
397 return false;
398 }
399 }
400
401 /**
402 * Error checking for glDrawElements(). Includes parameter checking
403 * and VBO bounds checking.
404 * \return GL_TRUE if OK to render, GL_FALSE if error found
405 */
406 GLboolean
407 _mesa_validate_DrawElements(struct gl_context *ctx,
408 GLenum mode, GLsizei count, GLenum type,
409 const GLvoid *indices, GLint basevertex)
410 {
411 FLUSH_CURRENT(ctx, 0);
412
413 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
414 * Primitive Capture):
415 *
416 * The error INVALID_OPERATION is also generated by DrawElements,
417 * DrawElementsInstanced, and DrawRangeElements while transform feedback
418 * is active and not paused, regardless of mode.
419 */
420 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
421 _mesa_error(ctx, GL_INVALID_OPERATION,
422 "glDrawElements(transform feedback active)");
423 return GL_FALSE;
424 }
425
426 if (count < 0) {
427 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
428 return GL_FALSE;
429 }
430
431 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElements")) {
432 return GL_FALSE;
433 }
434
435 if (!valid_elements_type(ctx, type, "glDrawElements"))
436 return GL_FALSE;
437
438 if (!check_valid_to_render(ctx, "glDrawElements"))
439 return GL_FALSE;
440
441 /* Vertex buffer object tests */
442 if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
443 /* use indices in the buffer object */
444 /* make sure count doesn't go outside buffer bounds */
445 if (index_bytes(type, count) > ctx->Array.VAO->IndexBufferObj->Size) {
446 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
447 return GL_FALSE;
448 }
449 }
450 else {
451 /* not using a VBO */
452 if (!indices)
453 return GL_FALSE;
454 }
455
456 if (!check_index_bounds(ctx, count, type, indices, basevertex))
457 return GL_FALSE;
458
459 if (count == 0)
460 return GL_FALSE;
461
462 return GL_TRUE;
463 }
464
465
466 /**
467 * Error checking for glMultiDrawElements(). Includes parameter checking
468 * and VBO bounds checking.
469 * \return GL_TRUE if OK to render, GL_FALSE if error found
470 */
471 GLboolean
472 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
473 GLenum mode, const GLsizei *count,
474 GLenum type, const GLvoid * const *indices,
475 GLuint primcount, const GLint *basevertex)
476 {
477 unsigned i;
478
479 FLUSH_CURRENT(ctx, 0);
480
481 for (i = 0; i < primcount; i++) {
482 if (count[i] < 0) {
483 _mesa_error(ctx, GL_INVALID_VALUE,
484 "glMultiDrawElements(count)" );
485 return GL_FALSE;
486 }
487 }
488
489 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
490 return GL_FALSE;
491 }
492
493 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
494 return GL_FALSE;
495
496 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
497 return GL_FALSE;
498
499 /* Vertex buffer object tests */
500 if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
501 /* use indices in the buffer object */
502 /* make sure count doesn't go outside buffer bounds */
503 for (i = 0; i < primcount; i++) {
504 if (index_bytes(type, count[i]) >
505 ctx->Array.VAO->IndexBufferObj->Size) {
506 _mesa_warning(ctx,
507 "glMultiDrawElements index out of buffer bounds");
508 return GL_FALSE;
509 }
510 }
511 }
512 else {
513 /* not using a VBO */
514 for (i = 0; i < primcount; i++) {
515 if (!indices[i])
516 return GL_FALSE;
517 }
518 }
519
520 for (i = 0; i < primcount; i++) {
521 if (!check_index_bounds(ctx, count[i], type, indices[i],
522 basevertex ? basevertex[i] : 0))
523 return GL_FALSE;
524 }
525
526 return GL_TRUE;
527 }
528
529
530 /**
531 * Error checking for glDrawRangeElements(). Includes parameter checking
532 * and VBO bounds checking.
533 * \return GL_TRUE if OK to render, GL_FALSE if error found
534 */
535 GLboolean
536 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
537 GLuint start, GLuint end,
538 GLsizei count, GLenum type,
539 const GLvoid *indices, GLint basevertex)
540 {
541 FLUSH_CURRENT(ctx, 0);
542
543 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
544 * Primitive Capture):
545 *
546 * The error INVALID_OPERATION is also generated by DrawElements,
547 * DrawElementsInstanced, and DrawRangeElements while transform feedback
548 * is active and not paused, regardless of mode.
549 */
550 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
551 _mesa_error(ctx, GL_INVALID_OPERATION,
552 "glDrawElements(transform feedback active)");
553 return GL_FALSE;
554 }
555
556 if (count < 0) {
557 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
558 return GL_FALSE;
559 }
560
561 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawRangeElements")) {
562 return GL_FALSE;
563 }
564
565 if (end < start) {
566 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
567 return GL_FALSE;
568 }
569
570 if (!valid_elements_type(ctx, type, "glDrawRangeElements"))
571 return GL_FALSE;
572
573 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
574 return GL_FALSE;
575
576 /* Vertex buffer object tests */
577 if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
578 /* use indices in the buffer object */
579 /* make sure count doesn't go outside buffer bounds */
580 if (index_bytes(type, count) > ctx->Array.VAO->IndexBufferObj->Size) {
581 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
582 return GL_FALSE;
583 }
584 }
585 else {
586 /* not using a VBO */
587 if (!indices)
588 return GL_FALSE;
589 }
590
591 if (!check_index_bounds(ctx, count, type, indices, basevertex))
592 return GL_FALSE;
593
594 if (count == 0)
595 return GL_FALSE;
596
597 return GL_TRUE;
598 }
599
600
601 /**
602 * Called from the tnl module to error check the function parameters and
603 * verify that we really can draw something.
604 * \return GL_TRUE if OK to render, GL_FALSE if error found
605 */
606 GLboolean
607 _mesa_validate_DrawArrays(struct gl_context *ctx,
608 GLenum mode, GLint start, GLsizei count)
609 {
610 struct gl_transform_feedback_object *xfb_obj
611 = ctx->TransformFeedback.CurrentObject;
612 FLUSH_CURRENT(ctx, 0);
613
614 if (count < 0) {
615 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
616 return GL_FALSE;
617 }
618
619 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArrays")) {
620 return GL_FALSE;
621 }
622
623 if (!check_valid_to_render(ctx, "glDrawArrays"))
624 return GL_FALSE;
625
626 if (ctx->Const.CheckArrayBounds) {
627 if (start + count > (GLint) ctx->Array.VAO->_MaxElement)
628 return GL_FALSE;
629 }
630
631 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
632 * Primitive Capture):
633 *
634 * The error INVALID_OPERATION is generated by DrawArrays and
635 * DrawArraysInstanced if recording the vertices of a primitive to the
636 * buffer objects being used for transform feedback purposes would result
637 * in either exceeding the limits of any buffer object’s size, or in
638 * exceeding the end position offset + size − 1, as set by
639 * BindBufferRange.
640 *
641 * This is in contrast to the behaviour of desktop GL, where the extra
642 * primitives are silently dropped from the transform feedback buffer.
643 */
644 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
645 size_t prim_count = vbo_count_tessellated_primitives(mode, count, 1);
646 if (xfb_obj->GlesRemainingPrims < prim_count) {
647 _mesa_error(ctx, GL_INVALID_OPERATION,
648 "glDrawArrays(exceeds transform feedback size)");
649 return GL_FALSE;
650 }
651 xfb_obj->GlesRemainingPrims -= prim_count;
652 }
653
654 if (count == 0)
655 return GL_FALSE;
656
657 return GL_TRUE;
658 }
659
660
661 GLboolean
662 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
663 GLsizei count, GLsizei numInstances)
664 {
665 struct gl_transform_feedback_object *xfb_obj
666 = ctx->TransformFeedback.CurrentObject;
667 FLUSH_CURRENT(ctx, 0);
668
669 if (count < 0) {
670 _mesa_error(ctx, GL_INVALID_VALUE,
671 "glDrawArraysInstanced(count=%d)", count);
672 return GL_FALSE;
673 }
674
675 if (first < 0) {
676 _mesa_error(ctx, GL_INVALID_VALUE,
677 "glDrawArraysInstanced(start=%d)", first);
678 return GL_FALSE;
679 }
680
681 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArraysInstanced")) {
682 return GL_FALSE;
683 }
684
685 if (numInstances <= 0) {
686 if (numInstances < 0)
687 _mesa_error(ctx, GL_INVALID_VALUE,
688 "glDrawArraysInstanced(numInstances=%d)", numInstances);
689 return GL_FALSE;
690 }
691
692 if (!check_valid_to_render(ctx, "glDrawArraysInstanced(invalid to render)"))
693 return GL_FALSE;
694
695 if (ctx->Const.CheckArrayBounds) {
696 if (first + count > (GLint) ctx->Array.VAO->_MaxElement)
697 return GL_FALSE;
698 }
699
700 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
701 * Primitive Capture):
702 *
703 * The error INVALID_OPERATION is generated by DrawArrays and
704 * DrawArraysInstanced if recording the vertices of a primitive to the
705 * buffer objects being used for transform feedback purposes would result
706 * in either exceeding the limits of any buffer object’s size, or in
707 * exceeding the end position offset + size − 1, as set by
708 * BindBufferRange.
709 *
710 * This is in contrast to the behaviour of desktop GL, where the extra
711 * primitives are silently dropped from the transform feedback buffer.
712 */
713 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
714 size_t prim_count
715 = vbo_count_tessellated_primitives(mode, count, numInstances);
716 if (xfb_obj->GlesRemainingPrims < prim_count) {
717 _mesa_error(ctx, GL_INVALID_OPERATION,
718 "glDrawArraysInstanced(exceeds transform feedback size)");
719 return GL_FALSE;
720 }
721 xfb_obj->GlesRemainingPrims -= prim_count;
722 }
723
724 if (count == 0)
725 return GL_FALSE;
726
727 return GL_TRUE;
728 }
729
730
731 GLboolean
732 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
733 GLenum mode, GLsizei count, GLenum type,
734 const GLvoid *indices, GLsizei numInstances,
735 GLint basevertex)
736 {
737 FLUSH_CURRENT(ctx, 0);
738
739 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
740 * Primitive Capture):
741 *
742 * The error INVALID_OPERATION is also generated by DrawElements,
743 * DrawElementsInstanced, and DrawRangeElements while transform feedback
744 * is active and not paused, regardless of mode.
745 */
746 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
747 _mesa_error(ctx, GL_INVALID_OPERATION,
748 "glDrawElements(transform feedback active)");
749 return GL_FALSE;
750 }
751
752 if (count < 0) {
753 _mesa_error(ctx, GL_INVALID_VALUE,
754 "glDrawElementsInstanced(count=%d)", count);
755 return GL_FALSE;
756 }
757
758 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElementsInstanced")) {
759 return GL_FALSE;
760 }
761
762 if (!valid_elements_type(ctx, type, "glDrawElementsInstanced"))
763 return GL_FALSE;
764
765 if (numInstances <= 0) {
766 if (numInstances < 0)
767 _mesa_error(ctx, GL_INVALID_VALUE,
768 "glDrawElementsInstanced(numInstances=%d)", numInstances);
769 return GL_FALSE;
770 }
771
772 if (!check_valid_to_render(ctx, "glDrawElementsInstanced"))
773 return GL_FALSE;
774
775 /* Vertex buffer object tests */
776 if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
777 /* use indices in the buffer object */
778 /* make sure count doesn't go outside buffer bounds */
779 if (index_bytes(type, count) > ctx->Array.VAO->IndexBufferObj->Size) {
780 _mesa_warning(ctx,
781 "glDrawElementsInstanced index out of buffer bounds");
782 return GL_FALSE;
783 }
784 }
785 else {
786 /* not using a VBO */
787 if (!indices)
788 return GL_FALSE;
789 }
790
791 if (count == 0)
792 return GL_FALSE;
793
794 if (!check_index_bounds(ctx, count, type, indices, basevertex))
795 return GL_FALSE;
796
797 return GL_TRUE;
798 }
799
800
801 GLboolean
802 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
803 GLenum mode,
804 struct gl_transform_feedback_object *obj,
805 GLuint stream,
806 GLsizei numInstances)
807 {
808 FLUSH_CURRENT(ctx, 0);
809
810 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
811 return GL_FALSE;
812 }
813
814 if (!obj) {
815 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
816 return GL_FALSE;
817 }
818
819 if (!obj->EndedAnytime) {
820 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
821 return GL_FALSE;
822 }
823
824 if (stream >= ctx->Const.MaxVertexStreams) {
825 _mesa_error(ctx, GL_INVALID_VALUE,
826 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
827 return GL_FALSE;
828 }
829
830 if (numInstances <= 0) {
831 if (numInstances < 0)
832 _mesa_error(ctx, GL_INVALID_VALUE,
833 "glDrawTransformFeedback*Instanced(numInstances=%d)",
834 numInstances);
835 return GL_FALSE;
836 }
837
838 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {
839 return GL_FALSE;
840 }
841
842 return GL_TRUE;
843 }
844
845 static GLboolean
846 valid_draw_indirect(struct gl_context *ctx,
847 GLenum mode, const GLvoid *indirect,
848 GLsizei size, const char *name)
849 {
850 const GLsizeiptr end = (GLsizeiptr)indirect + size;
851
852 if (!_mesa_valid_prim_mode(ctx, mode, name))
853 return GL_FALSE;
854
855
856 /* From the ARB_draw_indirect specification:
857 * "An INVALID_OPERATION error is generated [...] if <indirect> is no
858 * word aligned."
859 */
860 if ((GLsizeiptr)indirect & (sizeof(GLuint) - 1)) {
861 _mesa_error(ctx, GL_INVALID_OPERATION,
862 "%s(indirect is not aligned)", name);
863 return GL_FALSE;
864 }
865
866 if (!_mesa_is_bufferobj(ctx->DrawIndirectBuffer)) {
867 _mesa_error(ctx, GL_INVALID_OPERATION,
868 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name);
869 return GL_FALSE;
870 }
871
872 if (_mesa_check_disallowed_mapping(ctx->DrawIndirectBuffer)) {
873 _mesa_error(ctx, GL_INVALID_OPERATION,
874 "%s(DRAW_INDIRECT_BUFFER is mapped)", name);
875 return GL_FALSE;
876 }
877
878 /* From the ARB_draw_indirect specification:
879 * "An INVALID_OPERATION error is generated if the commands source data
880 * beyond the end of the buffer object [...]"
881 */
882 if (ctx->DrawIndirectBuffer->Size < end) {
883 _mesa_error(ctx, GL_INVALID_OPERATION,
884 "%s(DRAW_INDIRECT_BUFFER too small)", name);
885 return GL_FALSE;
886 }
887
888 if (!check_valid_to_render(ctx, name))
889 return GL_FALSE;
890
891 return GL_TRUE;
892 }
893
894 static inline GLboolean
895 valid_draw_indirect_elements(struct gl_context *ctx,
896 GLenum mode, GLenum type, const GLvoid *indirect,
897 GLsizeiptr size, const char *name)
898 {
899 if (!valid_elements_type(ctx, type, name))
900 return GL_FALSE;
901
902 /*
903 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
904 * may not come from a client array and must come from an index buffer.
905 * If no element array buffer is bound, an INVALID_OPERATION error is
906 * generated.
907 */
908 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
909 _mesa_error(ctx, GL_INVALID_OPERATION,
910 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name);
911 return GL_FALSE;
912 }
913
914 return valid_draw_indirect(ctx, mode, indirect, size, name);
915 }
916
917 static inline GLboolean
918 valid_draw_indirect_multi(struct gl_context *ctx,
919 GLsizei primcount, GLsizei stride,
920 const char *name)
921 {
922
923 /* From the ARB_multi_draw_indirect specification:
924 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
925 * MultiDrawElementsIndirect if <primcount> is negative."
926 *
927 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
928 * be generated."
929 */
930 if (primcount < 0) {
931 _mesa_error(ctx, GL_INVALID_VALUE, "%s(primcount < 0)", name);
932 return GL_FALSE;
933 }
934
935
936 /* From the ARB_multi_draw_indirect specification:
937 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
938 * error is generated."
939 */
940 if (stride % 4) {
941 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride %% 4)", name);
942 return GL_FALSE;
943 }
944
945 return GL_TRUE;
946 }
947
948 GLboolean
949 _mesa_validate_DrawArraysIndirect(struct gl_context *ctx,
950 GLenum mode,
951 const GLvoid *indirect)
952 {
953 const unsigned drawArraysNumParams = 4;
954
955 FLUSH_CURRENT(ctx, 0);
956
957 return valid_draw_indirect(ctx, mode,
958 indirect, drawArraysNumParams * sizeof(GLuint),
959 "glDrawArraysIndirect");
960 }
961
962 GLboolean
963 _mesa_validate_DrawElementsIndirect(struct gl_context *ctx,
964 GLenum mode, GLenum type,
965 const GLvoid *indirect)
966 {
967 const unsigned drawElementsNumParams = 5;
968
969 FLUSH_CURRENT(ctx, 0);
970
971 return valid_draw_indirect_elements(ctx, mode, type,
972 indirect, drawElementsNumParams * sizeof(GLuint),
973 "glDrawElementsIndirect");
974 }
975
976 GLboolean
977 _mesa_validate_MultiDrawArraysIndirect(struct gl_context *ctx,
978 GLenum mode,
979 const GLvoid *indirect,
980 GLsizei primcount, GLsizei stride)
981 {
982 GLsizeiptr size = 0;
983 const unsigned drawArraysNumParams = 4;
984
985 FLUSH_CURRENT(ctx, 0);
986
987 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
988 assert(stride != 0);
989
990 if (!valid_draw_indirect_multi(ctx, primcount, stride,
991 "glMultiDrawArraysIndirect"))
992 return GL_FALSE;
993
994 /* number of bytes of the indirect buffer which will be read */
995 size = primcount
996 ? (primcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
997 : 0;
998
999 if (!valid_draw_indirect(ctx, mode, indirect, size,
1000 "glMultiDrawArraysIndirect"))
1001 return GL_FALSE;
1002
1003 return GL_TRUE;
1004 }
1005
1006 GLboolean
1007 _mesa_validate_MultiDrawElementsIndirect(struct gl_context *ctx,
1008 GLenum mode, GLenum type,
1009 const GLvoid *indirect,
1010 GLsizei primcount, GLsizei stride)
1011 {
1012 GLsizeiptr size = 0;
1013 const unsigned drawElementsNumParams = 5;
1014
1015 FLUSH_CURRENT(ctx, 0);
1016
1017 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1018 assert(stride != 0);
1019
1020 if (!valid_draw_indirect_multi(ctx, primcount, stride,
1021 "glMultiDrawElementsIndirect"))
1022 return GL_FALSE;
1023
1024 /* number of bytes of the indirect buffer which will be read */
1025 size = primcount
1026 ? (primcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1027 : 0;
1028
1029 if (!valid_draw_indirect_elements(ctx, mode, type,
1030 indirect, size,
1031 "glMultiDrawElementsIndirect"))
1032 return GL_FALSE;
1033
1034 return GL_TRUE;
1035 }