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