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