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