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