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