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