mesa: remove remaining tabs in api_validate.c
[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 * Check if OK to draw arrays/elements.
40 */
41 static bool
42 check_valid_to_render(struct gl_context *ctx, const char *function)
43 {
44 if (!_mesa_valid_to_render(ctx, function)) {
45 return false;
46 }
47
48 switch (ctx->API) {
49 case API_OPENGLES2:
50 /* For ES2, we can draw if we have a vertex program/shader). */
51 return ctx->VertexProgram._Current != NULL;
52
53 case API_OPENGLES:
54 /* For OpenGL ES, only draw if we have vertex positions
55 */
56 if (!ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled)
57 return false;
58 break;
59
60 case API_OPENGL_CORE:
61 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
62 * Core Profile spec says:
63 *
64 * "An INVALID_OPERATION error is generated if no vertex array
65 * object is bound (see section 10.3.1)."
66 */
67 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
68 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no VAO bound)", function);
69 return false;
70 }
71
72 /* The spec argues that this is allowed because a tess ctrl shader
73 * without a tess eval shader can be used with transform feedback.
74 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
75 * therefore doesn't allow tessellation.
76 *
77 * Further investigation showed that this is indeed a spec bug and
78 * a tess ctrl shader without a tess eval shader shouldn't have been
79 * allowed, because there is no API in GL 4.0 that can make use this
80 * to produce something useful.
81 *
82 * Also, all vendors except one don't support a tess ctrl shader without
83 * a tess eval shader anyway.
84 */
85 if (ctx->TessCtrlProgram._Current && !ctx->TessEvalProgram._Current) {
86 _mesa_error(ctx, GL_INVALID_OPERATION,
87 "%s(tess eval shader is missing)", function);
88 return false;
89 }
90
91 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
92 * says:
93 *
94 * "If there is no active program for the vertex or fragment shader
95 * stages, the results of vertex and/or fragment processing will be
96 * undefined. However, this is not an error."
97 *
98 * The fragment shader is not tested here because other state (e.g.,
99 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
100 */
101 return ctx->VertexProgram._Current != NULL;
102
103 case API_OPENGL_COMPAT:
104 if (ctx->VertexProgram._Current != NULL) {
105 /* Draw regardless of whether or not we have any vertex arrays.
106 * (Ex: could draw a point using a constant vertex pos)
107 */
108 return true;
109 } else {
110 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
111 * array [0]).
112 */
113 return (ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
114 ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
115 }
116 break;
117
118 default:
119 unreachable("Invalid API value in check_valid_to_render()");
120 }
121
122 return true;
123 }
124
125
126 /**
127 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
128 * etc? The set of legal values depends on whether geometry shaders/programs
129 * are supported.
130 * Note: This may be called during display list compilation.
131 */
132 bool
133 _mesa_is_valid_prim_mode(struct gl_context *ctx, GLenum mode)
134 {
135 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
136 * first and exit. You would think that a switch-statement would be the
137 * right approach, but at least GCC 4.7.2 generates some pretty dire code
138 * for the common case.
139 */
140 if (likely(mode <= GL_TRIANGLE_FAN))
141 return true;
142
143 if (mode <= GL_POLYGON)
144 return (ctx->API == API_OPENGL_COMPAT);
145
146 if (mode <= GL_TRIANGLE_STRIP_ADJACENCY)
147 return _mesa_has_geometry_shaders(ctx);
148
149 if (mode == GL_PATCHES)
150 return _mesa_has_tessellation(ctx);
151
152 return false;
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 * Also, do additional checking related to tessellation shaders.
162 */
163 GLboolean
164 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
165 {
166 bool valid_enum = _mesa_is_valid_prim_mode(ctx, mode);
167
168 if (!valid_enum) {
169 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
170 return GL_FALSE;
171 }
172
173 /* From the OpenGL 4.5 specification, section 11.3.1:
174 *
175 * The error INVALID_OPERATION is generated if Begin, or any command that
176 * implicitly calls Begin, is called when a geometry shader is active and:
177 *
178 * * the input primitive type of the current geometry shader is
179 * POINTS and <mode> is not POINTS,
180 *
181 * * the input primitive type of the current geometry shader is
182 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
183 *
184 * * the input primitive type of the current geometry shader is
185 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
186 * TRIANGLE_FAN,
187 *
188 * * the input primitive type of the current geometry shader is
189 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
190 * LINE_STRIP_ADJACENCY_ARB, or
191 *
192 * * the input primitive type of the current geometry shader is
193 * TRIANGLES_ADJACENCY_ARB and <mode> is not
194 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
195 *
196 * The GL spec doesn't mention any interaction with tessellation, which
197 * is clearly a spec bug. The same rule should apply, but instead of
198 * the draw primitive mode, the tessellation evaluation shader primitive
199 * mode should be used for the checking.
200 */
201 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
202 const GLenum geom_mode =
203 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->Geom.InputType;
204 struct gl_shader_program *tes =
205 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
206 GLenum mode_before_gs = mode;
207
208 if (tes) {
209 if (tes->TessEval.PointMode)
210 mode_before_gs = GL_POINTS;
211 else if (tes->TessEval.PrimitiveMode == GL_ISOLINES)
212 mode_before_gs = GL_LINES;
213 else
214 /* the GL_QUADS mode generates triangles too */
215 mode_before_gs = GL_TRIANGLES;
216 }
217
218 switch (mode_before_gs) {
219 case GL_POINTS:
220 valid_enum = (geom_mode == GL_POINTS);
221 break;
222 case GL_LINES:
223 case GL_LINE_LOOP:
224 case GL_LINE_STRIP:
225 valid_enum = (geom_mode == GL_LINES);
226 break;
227 case GL_TRIANGLES:
228 case GL_TRIANGLE_STRIP:
229 case GL_TRIANGLE_FAN:
230 valid_enum = (geom_mode == GL_TRIANGLES);
231 break;
232 case GL_QUADS:
233 case GL_QUAD_STRIP:
234 case GL_POLYGON:
235 valid_enum = false;
236 break;
237 case GL_LINES_ADJACENCY:
238 case GL_LINE_STRIP_ADJACENCY:
239 valid_enum = (geom_mode == GL_LINES_ADJACENCY);
240 break;
241 case GL_TRIANGLES_ADJACENCY:
242 case GL_TRIANGLE_STRIP_ADJACENCY:
243 valid_enum = (geom_mode == GL_TRIANGLES_ADJACENCY);
244 break;
245 default:
246 valid_enum = false;
247 break;
248 }
249 if (!valid_enum) {
250 _mesa_error(ctx, GL_INVALID_OPERATION,
251 "%s(mode=%s vs geometry shader input %s)",
252 name,
253 _mesa_lookup_prim_by_nr(mode_before_gs),
254 _mesa_lookup_prim_by_nr(geom_mode));
255 return GL_FALSE;
256 }
257 }
258
259 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
260 *
261 * "Tessellation operates only on patch primitives. If tessellation is
262 * active, any command that transfers vertices to the GL will
263 * generate an INVALID_OPERATION error if the primitive mode is not
264 * PATCHES.
265 * Patch primitives are not supported by pipeline stages below the
266 * tessellation evaluation shader. If there is no active program
267 * object or the active program object does not contain a tessellation
268 * evaluation shader, the error INVALID_OPERATION is generated by any
269 * command that transfers vertices to the GL if the primitive mode is
270 * PATCHES."
271 *
272 */
273 if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL] ||
274 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL]) {
275 if (mode != GL_PATCHES) {
276 _mesa_error(ctx, GL_INVALID_OPERATION,
277 "only GL_PATCHES valid with tessellation");
278 return GL_FALSE;
279 }
280 }
281 else {
282 if (mode == GL_PATCHES) {
283 _mesa_error(ctx, GL_INVALID_OPERATION,
284 "GL_PATCHES only valid with tessellation");
285 return GL_FALSE;
286 }
287 }
288
289 /* From the GL_EXT_transform_feedback spec:
290 *
291 * "The error INVALID_OPERATION is generated if Begin, or any command
292 * that performs an explicit Begin, is called when:
293 *
294 * * a geometry shader is not active and <mode> does not match the
295 * allowed begin modes for the current transform feedback state as
296 * given by table X.1.
297 *
298 * * a geometry shader is active and the output primitive type of the
299 * geometry shader does not match the allowed begin modes for the
300 * current transform feedback state as given by table X.1.
301 *
302 */
303 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
304 GLboolean pass = GL_TRUE;
305
306 if(ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
307 switch (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->Geom.OutputType) {
308 case GL_POINTS:
309 pass = ctx->TransformFeedback.Mode == GL_POINTS;
310 break;
311 case GL_LINE_STRIP:
312 pass = ctx->TransformFeedback.Mode == GL_LINES;
313 break;
314 case GL_TRIANGLE_STRIP:
315 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
316 break;
317 default:
318 pass = GL_FALSE;
319 }
320 }
321 else if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL]) {
322 struct gl_shader_program *tes =
323 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
324
325 if (tes->TessEval.PointMode)
326 pass = ctx->TransformFeedback.Mode == GL_POINTS;
327 else if (tes->TessEval.PrimitiveMode == GL_ISOLINES)
328 pass = ctx->TransformFeedback.Mode == GL_LINES;
329 else
330 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
331 }
332 else {
333 switch (mode) {
334 case GL_POINTS:
335 pass = ctx->TransformFeedback.Mode == GL_POINTS;
336 break;
337 case GL_LINES:
338 case GL_LINE_STRIP:
339 case GL_LINE_LOOP:
340 pass = ctx->TransformFeedback.Mode == GL_LINES;
341 break;
342 default:
343 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
344 break;
345 }
346 }
347 if (!pass) {
348 _mesa_error(ctx, GL_INVALID_OPERATION,
349 "%s(mode=%s vs transform feedback %s)",
350 name,
351 _mesa_lookup_prim_by_nr(mode),
352 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
353 return GL_FALSE;
354 }
355 }
356
357 return GL_TRUE;
358 }
359
360 /**
361 * Verify that the element type is valid.
362 *
363 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
364 */
365 static bool
366 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
367 {
368 switch (type) {
369 case GL_UNSIGNED_BYTE:
370 case GL_UNSIGNED_SHORT:
371 case GL_UNSIGNED_INT:
372 return true;
373
374 default:
375 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
376 _mesa_enum_to_string(type));
377 return false;
378 }
379 }
380
381 static bool
382 validate_DrawElements_common(struct gl_context *ctx,
383 GLenum mode, GLsizei count, GLenum type,
384 const GLvoid *indices,
385 const char *caller)
386 {
387 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
388 * Primitive Capture):
389 *
390 * The error INVALID_OPERATION is also generated by DrawElements,
391 * DrawElementsInstanced, and DrawRangeElements while transform feedback
392 * is active and not paused, regardless of mode.
393 */
394 if (_mesa_is_gles3(ctx) && !ctx->Extensions.OES_geometry_shader &&
395 _mesa_is_xfb_active_and_unpaused(ctx)) {
396 _mesa_error(ctx, GL_INVALID_OPERATION,
397 "%s(transform feedback active)", caller);
398 return false;
399 }
400
401 if (count < 0) {
402 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", caller);
403 return false;
404 }
405
406 if (!_mesa_valid_prim_mode(ctx, mode, caller)) {
407 return false;
408 }
409
410 if (!valid_elements_type(ctx, type, caller))
411 return false;
412
413 if (!check_valid_to_render(ctx, caller))
414 return false;
415
416 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
417 */
418 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj) && indices == NULL)
419 return false;
420
421 if (count == 0)
422 return false;
423
424 return true;
425 }
426
427 /**
428 * Error checking for glDrawElements(). Includes parameter checking
429 * and VBO bounds checking.
430 * \return GL_TRUE if OK to render, GL_FALSE if error found
431 */
432 GLboolean
433 _mesa_validate_DrawElements(struct gl_context *ctx,
434 GLenum mode, GLsizei count, GLenum type,
435 const GLvoid *indices)
436 {
437 FLUSH_CURRENT(ctx, 0);
438
439 return validate_DrawElements_common(ctx, mode, count, type, indices,
440 "glDrawElements");
441 }
442
443
444 /**
445 * Error checking for glMultiDrawElements(). Includes parameter checking
446 * and VBO bounds checking.
447 * \return GL_TRUE if OK to render, GL_FALSE if error found
448 */
449 GLboolean
450 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
451 GLenum mode, const GLsizei *count,
452 GLenum type, const GLvoid * const *indices,
453 GLuint primcount)
454 {
455 unsigned i;
456
457 FLUSH_CURRENT(ctx, 0);
458
459 for (i = 0; i < primcount; i++) {
460 if (count[i] < 0) {
461 _mesa_error(ctx, GL_INVALID_VALUE,
462 "glMultiDrawElements(count)" );
463 return GL_FALSE;
464 }
465 }
466
467 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
468 return GL_FALSE;
469 }
470
471 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
472 return GL_FALSE;
473
474 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
475 return GL_FALSE;
476
477 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
478 */
479 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
480 for (i = 0; i < primcount; i++) {
481 if (!indices[i])
482 return GL_FALSE;
483 }
484 }
485
486 return GL_TRUE;
487 }
488
489
490 /**
491 * Error checking for glDrawRangeElements(). Includes parameter checking
492 * and VBO bounds checking.
493 * \return GL_TRUE if OK to render, GL_FALSE if error found
494 */
495 GLboolean
496 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
497 GLuint start, GLuint end,
498 GLsizei count, GLenum type,
499 const GLvoid *indices)
500 {
501 FLUSH_CURRENT(ctx, 0);
502
503 if (end < start) {
504 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
505 return GL_FALSE;
506 }
507
508 return validate_DrawElements_common(ctx, mode, count, type, indices,
509 "glDrawRangeElements");
510 }
511
512
513 /**
514 * Called from the tnl module to error check the function parameters and
515 * verify that we really can draw something.
516 * \return GL_TRUE if OK to render, GL_FALSE if error found
517 */
518 GLboolean
519 _mesa_validate_DrawArrays(struct gl_context *ctx, GLenum mode, GLsizei count)
520 {
521 struct gl_transform_feedback_object *xfb_obj
522 = ctx->TransformFeedback.CurrentObject;
523 FLUSH_CURRENT(ctx, 0);
524
525 if (count < 0) {
526 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
527 return GL_FALSE;
528 }
529
530 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArrays")) {
531 return GL_FALSE;
532 }
533
534 if (!check_valid_to_render(ctx, "glDrawArrays"))
535 return GL_FALSE;
536
537 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
538 * Primitive Capture):
539 *
540 * The error INVALID_OPERATION is generated by DrawArrays and
541 * DrawArraysInstanced if recording the vertices of a primitive to the
542 * buffer objects being used for transform feedback purposes would result
543 * in either exceeding the limits of any buffer object’s size, or in
544 * exceeding the end position offset + size − 1, as set by
545 * BindBufferRange.
546 *
547 * This is in contrast to the behaviour of desktop GL, where the extra
548 * primitives are silently dropped from the transform feedback buffer.
549 */
550 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
551 size_t prim_count = vbo_count_tessellated_primitives(mode, count, 1);
552 if (xfb_obj->GlesRemainingPrims < prim_count) {
553 _mesa_error(ctx, GL_INVALID_OPERATION,
554 "glDrawArrays(exceeds transform feedback size)");
555 return GL_FALSE;
556 }
557 xfb_obj->GlesRemainingPrims -= prim_count;
558 }
559
560 if (count == 0)
561 return GL_FALSE;
562
563 return GL_TRUE;
564 }
565
566
567 GLboolean
568 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
569 GLsizei count, GLsizei numInstances)
570 {
571 struct gl_transform_feedback_object *xfb_obj
572 = ctx->TransformFeedback.CurrentObject;
573 FLUSH_CURRENT(ctx, 0);
574
575 if (count < 0) {
576 _mesa_error(ctx, GL_INVALID_VALUE,
577 "glDrawArraysInstanced(count=%d)", count);
578 return GL_FALSE;
579 }
580
581 if (first < 0) {
582 _mesa_error(ctx, GL_INVALID_VALUE,
583 "glDrawArraysInstanced(start=%d)", first);
584 return GL_FALSE;
585 }
586
587 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArraysInstanced")) {
588 return GL_FALSE;
589 }
590
591 if (numInstances <= 0) {
592 if (numInstances < 0)
593 _mesa_error(ctx, GL_INVALID_VALUE,
594 "glDrawArraysInstanced(numInstances=%d)", numInstances);
595 return GL_FALSE;
596 }
597
598 if (!check_valid_to_render(ctx, "glDrawArraysInstanced(invalid to render)"))
599 return GL_FALSE;
600
601 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
602 * Primitive Capture):
603 *
604 * The error INVALID_OPERATION is generated by DrawArrays and
605 * DrawArraysInstanced if recording the vertices of a primitive to the
606 * buffer objects being used for transform feedback purposes would result
607 * in either exceeding the limits of any buffer object’s size, or in
608 * exceeding the end position offset + size − 1, as set by
609 * BindBufferRange.
610 *
611 * This is in contrast to the behaviour of desktop GL, where the extra
612 * primitives are silently dropped from the transform feedback buffer.
613 */
614 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
615 size_t prim_count
616 = vbo_count_tessellated_primitives(mode, count, numInstances);
617 if (xfb_obj->GlesRemainingPrims < prim_count) {
618 _mesa_error(ctx, GL_INVALID_OPERATION,
619 "glDrawArraysInstanced(exceeds transform feedback size)");
620 return GL_FALSE;
621 }
622 xfb_obj->GlesRemainingPrims -= prim_count;
623 }
624
625 if (count == 0)
626 return GL_FALSE;
627
628 return GL_TRUE;
629 }
630
631
632 GLboolean
633 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
634 GLenum mode, GLsizei count, GLenum type,
635 const GLvoid *indices, GLsizei numInstances)
636 {
637 FLUSH_CURRENT(ctx, 0);
638
639 if (numInstances < 0) {
640 _mesa_error(ctx, GL_INVALID_VALUE,
641 "glDrawElementsInstanced(numInstances=%d)", numInstances);
642 return GL_FALSE;
643 }
644
645 return validate_DrawElements_common(ctx, mode, count, type, indices,
646 "glDrawElementsInstanced")
647 && (numInstances > 0);
648 }
649
650
651 GLboolean
652 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
653 GLenum mode,
654 struct gl_transform_feedback_object *obj,
655 GLuint stream,
656 GLsizei numInstances)
657 {
658 FLUSH_CURRENT(ctx, 0);
659
660 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
661 return GL_FALSE;
662 }
663
664 if (!obj) {
665 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
666 return GL_FALSE;
667 }
668
669 /* From the GL 4.5 specification, page 429:
670 * "An INVALID_VALUE error is generated if id is not the name of a
671 * transform feedback object."
672 */
673 if (!obj->EverBound) {
674 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
675 return GL_FALSE;
676 }
677
678 if (stream >= ctx->Const.MaxVertexStreams) {
679 _mesa_error(ctx, GL_INVALID_VALUE,
680 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
681 return GL_FALSE;
682 }
683
684 if (!obj->EndedAnytime) {
685 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
686 return GL_FALSE;
687 }
688
689 if (numInstances <= 0) {
690 if (numInstances < 0)
691 _mesa_error(ctx, GL_INVALID_VALUE,
692 "glDrawTransformFeedback*Instanced(numInstances=%d)",
693 numInstances);
694 return GL_FALSE;
695 }
696
697 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {
698 return GL_FALSE;
699 }
700
701 return GL_TRUE;
702 }
703
704 static GLboolean
705 valid_draw_indirect(struct gl_context *ctx,
706 GLenum mode, const GLvoid *indirect,
707 GLsizei size, const char *name)
708 {
709 const uint64_t end = (uint64_t) (uintptr_t) indirect + size;
710
711 /* OpenGL ES 3.1 spec. section 10.5:
712 *
713 * "DrawArraysIndirect requires that all data sourced for the
714 * command, including the DrawArraysIndirectCommand
715 * structure, be in buffer objects, and may not be called when
716 * the default vertex array object is bound."
717 */
718 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
719 _mesa_error(ctx, GL_INVALID_OPERATION, "(no VAO bound)");
720 return GL_FALSE;
721 }
722
723 /* From OpenGL ES 3.1 spec. section 10.5:
724 * "An INVALID_OPERATION error is generated if zero is bound to
725 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
726 * vertex array."
727 *
728 * Here we check that for each enabled vertex array we have a vertex
729 * buffer bound.
730 */
731 if (_mesa_is_gles31(ctx) &&
732 ctx->Array.VAO->_Enabled != ctx->Array.VAO->VertexAttribBufferMask) {
733 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(No VBO bound)", name);
734 return GL_FALSE;
735 }
736
737 if (!_mesa_valid_prim_mode(ctx, mode, name))
738 return GL_FALSE;
739
740 /* OpenGL ES 3.1 specification, section 10.5:
741 *
742 * "An INVALID_OPERATION error is generated if
743 * transform feedback is active and not paused."
744 */
745 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader &&
746 _mesa_is_xfb_active_and_unpaused(ctx)) {
747 _mesa_error(ctx, GL_INVALID_OPERATION,
748 "%s(TransformFeedback is active and not paused)", name);
749 }
750
751 /* From OpenGL version 4.4. section 10.5
752 * and OpenGL ES 3.1, section 10.6:
753 *
754 * "An INVALID_VALUE error is generated if indirect is not a
755 * multiple of the size, in basic machine units, of uint."
756 */
757 if ((GLsizeiptr)indirect & (sizeof(GLuint) - 1)) {
758 _mesa_error(ctx, GL_INVALID_VALUE,
759 "%s(indirect is not aligned)", name);
760 return GL_FALSE;
761 }
762
763 if (!_mesa_is_bufferobj(ctx->DrawIndirectBuffer)) {
764 _mesa_error(ctx, GL_INVALID_OPERATION,
765 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name);
766 return GL_FALSE;
767 }
768
769 if (_mesa_check_disallowed_mapping(ctx->DrawIndirectBuffer)) {
770 _mesa_error(ctx, GL_INVALID_OPERATION,
771 "%s(DRAW_INDIRECT_BUFFER is mapped)", name);
772 return GL_FALSE;
773 }
774
775 /* From the ARB_draw_indirect specification:
776 * "An INVALID_OPERATION error is generated if the commands source data
777 * beyond the end of the buffer object [...]"
778 */
779 if (ctx->DrawIndirectBuffer->Size < end) {
780 _mesa_error(ctx, GL_INVALID_OPERATION,
781 "%s(DRAW_INDIRECT_BUFFER too small)", name);
782 return GL_FALSE;
783 }
784
785 if (!check_valid_to_render(ctx, name))
786 return GL_FALSE;
787
788 return GL_TRUE;
789 }
790
791 static inline GLboolean
792 valid_draw_indirect_elements(struct gl_context *ctx,
793 GLenum mode, GLenum type, const GLvoid *indirect,
794 GLsizeiptr size, const char *name)
795 {
796 if (!valid_elements_type(ctx, type, name))
797 return GL_FALSE;
798
799 /*
800 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
801 * may not come from a client array and must come from an index buffer.
802 * If no element array buffer is bound, an INVALID_OPERATION error is
803 * generated.
804 */
805 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
806 _mesa_error(ctx, GL_INVALID_OPERATION,
807 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name);
808 return GL_FALSE;
809 }
810
811 return valid_draw_indirect(ctx, mode, indirect, size, name);
812 }
813
814 static inline GLboolean
815 valid_draw_indirect_multi(struct gl_context *ctx,
816 GLsizei primcount, GLsizei stride,
817 const char *name)
818 {
819
820 /* From the ARB_multi_draw_indirect specification:
821 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
822 * MultiDrawElementsIndirect if <primcount> is negative."
823 *
824 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
825 * be generated."
826 */
827 if (primcount < 0) {
828 _mesa_error(ctx, GL_INVALID_VALUE, "%s(primcount < 0)", name);
829 return GL_FALSE;
830 }
831
832
833 /* From the ARB_multi_draw_indirect specification:
834 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
835 * error is generated."
836 */
837 if (stride % 4) {
838 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride %% 4)", name);
839 return GL_FALSE;
840 }
841
842 return GL_TRUE;
843 }
844
845 GLboolean
846 _mesa_validate_DrawArraysIndirect(struct gl_context *ctx,
847 GLenum mode,
848 const GLvoid *indirect)
849 {
850 const unsigned drawArraysNumParams = 4;
851
852 FLUSH_CURRENT(ctx, 0);
853
854 return valid_draw_indirect(ctx, mode,
855 indirect, drawArraysNumParams * sizeof(GLuint),
856 "glDrawArraysIndirect");
857 }
858
859 GLboolean
860 _mesa_validate_DrawElementsIndirect(struct gl_context *ctx,
861 GLenum mode, GLenum type,
862 const GLvoid *indirect)
863 {
864 const unsigned drawElementsNumParams = 5;
865
866 FLUSH_CURRENT(ctx, 0);
867
868 return valid_draw_indirect_elements(ctx, mode, type,
869 indirect, drawElementsNumParams * sizeof(GLuint),
870 "glDrawElementsIndirect");
871 }
872
873 GLboolean
874 _mesa_validate_MultiDrawArraysIndirect(struct gl_context *ctx,
875 GLenum mode,
876 const GLvoid *indirect,
877 GLsizei primcount, GLsizei stride)
878 {
879 GLsizeiptr size = 0;
880 const unsigned drawArraysNumParams = 4;
881
882 FLUSH_CURRENT(ctx, 0);
883
884 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
885 assert(stride != 0);
886
887 if (!valid_draw_indirect_multi(ctx, primcount, stride,
888 "glMultiDrawArraysIndirect"))
889 return GL_FALSE;
890
891 /* number of bytes of the indirect buffer which will be read */
892 size = primcount
893 ? (primcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
894 : 0;
895
896 if (!valid_draw_indirect(ctx, mode, indirect, size,
897 "glMultiDrawArraysIndirect"))
898 return GL_FALSE;
899
900 return GL_TRUE;
901 }
902
903 GLboolean
904 _mesa_validate_MultiDrawElementsIndirect(struct gl_context *ctx,
905 GLenum mode, GLenum type,
906 const GLvoid *indirect,
907 GLsizei primcount, GLsizei stride)
908 {
909 GLsizeiptr size = 0;
910 const unsigned drawElementsNumParams = 5;
911
912 FLUSH_CURRENT(ctx, 0);
913
914 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
915 assert(stride != 0);
916
917 if (!valid_draw_indirect_multi(ctx, primcount, stride,
918 "glMultiDrawElementsIndirect"))
919 return GL_FALSE;
920
921 /* number of bytes of the indirect buffer which will be read */
922 size = primcount
923 ? (primcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
924 : 0;
925
926 if (!valid_draw_indirect_elements(ctx, mode, type,
927 indirect, size,
928 "glMultiDrawElementsIndirect"))
929 return GL_FALSE;
930
931 return GL_TRUE;
932 }
933
934 static GLboolean
935 valid_draw_indirect_parameters(struct gl_context *ctx,
936 const char *name,
937 GLintptr drawcount)
938 {
939 /* From the ARB_indirect_parameters specification:
940 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
941 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
942 * four."
943 */
944 if (drawcount & 3) {
945 _mesa_error(ctx, GL_INVALID_VALUE,
946 "%s(drawcount is not a multiple of 4)", name);
947 return GL_FALSE;
948 }
949
950 /* From the ARB_indirect_parameters specification:
951 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
952 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
953 * PARAMETER_BUFFER_ARB binding point."
954 */
955 if (!_mesa_is_bufferobj(ctx->ParameterBuffer)) {
956 _mesa_error(ctx, GL_INVALID_OPERATION,
957 "%s: no buffer bound to PARAMETER_BUFFER", name);
958 return GL_FALSE;
959 }
960
961 if (_mesa_check_disallowed_mapping(ctx->ParameterBuffer)) {
962 _mesa_error(ctx, GL_INVALID_OPERATION,
963 "%s(PARAMETER_BUFFER is mapped)", name);
964 return GL_FALSE;
965 }
966
967 /* From the ARB_indirect_parameters specification:
968 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
969 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
970 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
971 * specified by <drawcount> would result in an out-of-bounds access."
972 */
973 if (ctx->ParameterBuffer->Size < drawcount + sizeof(GLsizei)) {
974 _mesa_error(ctx, GL_INVALID_OPERATION,
975 "%s(PARAMETER_BUFFER too small)", name);
976 return GL_FALSE;
977 }
978
979 return GL_TRUE;
980 }
981
982 GLboolean
983 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context *ctx,
984 GLenum mode,
985 GLintptr indirect,
986 GLintptr drawcount,
987 GLsizei maxdrawcount,
988 GLsizei stride)
989 {
990 GLsizeiptr size = 0;
991 const unsigned drawArraysNumParams = 4;
992
993 FLUSH_CURRENT(ctx, 0);
994
995 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
996 assert(stride != 0);
997
998 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
999 "glMultiDrawArraysIndirectCountARB"))
1000 return GL_FALSE;
1001
1002 /* number of bytes of the indirect buffer which will be read */
1003 size = maxdrawcount
1004 ? (maxdrawcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
1005 : 0;
1006
1007 if (!valid_draw_indirect(ctx, mode, (void *)indirect, size,
1008 "glMultiDrawArraysIndirectCountARB"))
1009 return GL_FALSE;
1010
1011 return valid_draw_indirect_parameters(
1012 ctx, "glMultiDrawArraysIndirectCountARB", drawcount);
1013 }
1014
1015 GLboolean
1016 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context *ctx,
1017 GLenum mode, GLenum type,
1018 GLintptr indirect,
1019 GLintptr drawcount,
1020 GLsizei maxdrawcount,
1021 GLsizei stride)
1022 {
1023 GLsizeiptr size = 0;
1024 const unsigned drawElementsNumParams = 5;
1025
1026 FLUSH_CURRENT(ctx, 0);
1027
1028 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1029 assert(stride != 0);
1030
1031 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1032 "glMultiDrawElementsIndirectCountARB"))
1033 return GL_FALSE;
1034
1035 /* number of bytes of the indirect buffer which will be read */
1036 size = maxdrawcount
1037 ? (maxdrawcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1038 : 0;
1039
1040 if (!valid_draw_indirect_elements(ctx, mode, type,
1041 (void *)indirect, size,
1042 "glMultiDrawElementsIndirectCountARB"))
1043 return GL_FALSE;
1044
1045 return valid_draw_indirect_parameters(
1046 ctx, "glMultiDrawElementsIndirectCountARB", drawcount);
1047 }
1048
1049 static bool
1050 check_valid_to_compute(struct gl_context *ctx, const char *function)
1051 {
1052 struct gl_shader_program *prog;
1053
1054 if (!_mesa_has_compute_shaders(ctx)) {
1055 _mesa_error(ctx, GL_INVALID_OPERATION,
1056 "unsupported function (%s) called",
1057 function);
1058 return false;
1059 }
1060
1061 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1062 *
1063 * "An INVALID_OPERATION error is generated if there is no active program
1064 * for the compute shader stage."
1065 */
1066 prog = ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
1067 if (prog == NULL || prog->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
1068 _mesa_error(ctx, GL_INVALID_OPERATION,
1069 "%s(no active compute shader)",
1070 function);
1071 return false;
1072 }
1073
1074 return true;
1075 }
1076
1077 GLboolean
1078 _mesa_validate_DispatchCompute(struct gl_context *ctx,
1079 const GLuint *num_groups)
1080 {
1081 int i;
1082 FLUSH_CURRENT(ctx, 0);
1083
1084 if (!check_valid_to_compute(ctx, "glDispatchCompute"))
1085 return GL_FALSE;
1086
1087 for (i = 0; i < 3; i++) {
1088 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1089 *
1090 * "An INVALID_VALUE error is generated if any of num_groups_x,
1091 * num_groups_y and num_groups_z are greater than or equal to the
1092 * maximum work group count for the corresponding dimension."
1093 *
1094 * However, the "or equal to" portions appears to be a specification
1095 * bug. In all other areas, the specification appears to indicate that
1096 * the number of workgroups can match the MAX_COMPUTE_WORK_GROUP_COUNT
1097 * value. For example, under DispatchComputeIndirect:
1098 *
1099 * "If any of num_groups_x, num_groups_y or num_groups_z is greater than
1100 * the value of MAX_COMPUTE_WORK_GROUP_COUNT for the corresponding
1101 * dimension then the results are undefined."
1102 *
1103 * Additionally, the OpenGLES 3.1 specification does not contain "or
1104 * equal to" as an error condition.
1105 */
1106 if (num_groups[i] > ctx->Const.MaxComputeWorkGroupCount[i]) {
1107 _mesa_error(ctx, GL_INVALID_VALUE,
1108 "glDispatchCompute(num_groups_%c)", 'x' + i);
1109 return GL_FALSE;
1110 }
1111 }
1112
1113 return GL_TRUE;
1114 }
1115
1116 static GLboolean
1117 valid_dispatch_indirect(struct gl_context *ctx,
1118 GLintptr indirect,
1119 GLsizei size, const char *name)
1120 {
1121 const uint64_t end = (uint64_t) indirect + size;
1122
1123 if (!check_valid_to_compute(ctx, name))
1124 return GL_FALSE;
1125
1126 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1127 *
1128 * "An INVALID_VALUE error is generated if indirect is negative or is not a
1129 * multiple of four."
1130 */
1131 if (indirect & (sizeof(GLuint) - 1)) {
1132 _mesa_error(ctx, GL_INVALID_VALUE,
1133 "%s(indirect is not aligned)", name);
1134 return GL_FALSE;
1135 }
1136
1137 if (indirect < 0) {
1138 _mesa_error(ctx, GL_INVALID_VALUE,
1139 "%s(indirect is less than zero)", name);
1140 return GL_FALSE;
1141 }
1142
1143 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1144 *
1145 * "An INVALID_OPERATION error is generated if no buffer is bound to the
1146 * DRAW_INDIRECT_BUFFER binding, or if the command would source data
1147 * beyond the end of the buffer object."
1148 */
1149 if (!_mesa_is_bufferobj(ctx->DispatchIndirectBuffer)) {
1150 _mesa_error(ctx, GL_INVALID_OPERATION,
1151 "%s: no buffer bound to DISPATCH_INDIRECT_BUFFER", name);
1152 return GL_FALSE;
1153 }
1154
1155 if (_mesa_check_disallowed_mapping(ctx->DispatchIndirectBuffer)) {
1156 _mesa_error(ctx, GL_INVALID_OPERATION,
1157 "%s(DISPATCH_INDIRECT_BUFFER is mapped)", name);
1158 return GL_FALSE;
1159 }
1160
1161 if (ctx->DispatchIndirectBuffer->Size < end) {
1162 _mesa_error(ctx, GL_INVALID_OPERATION,
1163 "%s(DISPATCH_INDIRECT_BUFFER too small)", name);
1164 return GL_FALSE;
1165 }
1166
1167 return GL_TRUE;
1168 }
1169
1170 GLboolean
1171 _mesa_validate_DispatchComputeIndirect(struct gl_context *ctx,
1172 GLintptr indirect)
1173 {
1174 FLUSH_CURRENT(ctx, 0);
1175
1176 return valid_dispatch_indirect(ctx, indirect, 3 * sizeof(GLuint),
1177 "glDispatchComputeIndirect");
1178 }