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