mesa: rename api_validate.{c,h} -> draw_validate.{c,h}
[mesa.git] / src / mesa / main / draw_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 "draw_validate.h"
28 #include "arrayobj.h"
29 #include "bufferobj.h"
30 #include "context.h"
31 #include "imports.h"
32 #include "mtypes.h"
33 #include "pipelineobj.h"
34 #include "enums.h"
35 #include "state.h"
36 #include "transformfeedback.h"
37 #include "uniforms.h"
38 #include "program/prog_print.h"
39
40
41 static bool
42 check_blend_func_error(struct gl_context *ctx)
43 {
44 /* The ARB_blend_func_extended spec's ERRORS section says:
45 *
46 * "The error INVALID_OPERATION is generated by Begin or any procedure
47 * that implicitly calls Begin if any draw buffer has a blend function
48 * requiring the second color input (SRC1_COLOR, ONE_MINUS_SRC1_COLOR,
49 * SRC1_ALPHA or ONE_MINUS_SRC1_ALPHA), and a framebuffer is bound that
50 * has more than the value of MAX_DUAL_SOURCE_DRAW_BUFFERS-1 active
51 * color attachements."
52 */
53 for (unsigned i = ctx->Const.MaxDualSourceDrawBuffers;
54 i < ctx->DrawBuffer->_NumColorDrawBuffers;
55 i++) {
56 if (ctx->Color.Blend[i]._UsesDualSrc) {
57 _mesa_error(ctx, GL_INVALID_OPERATION,
58 "dual source blend on illegal attachment");
59 return false;
60 }
61 }
62
63 if (ctx->Color.BlendEnabled && ctx->Color._AdvancedBlendMode) {
64 /* The KHR_blend_equation_advanced spec says:
65 *
66 * "If any non-NONE draw buffer uses a blend equation found in table
67 * X.1 or X.2, the error INVALID_OPERATION is generated by Begin or
68 * any operation that implicitly calls Begin (such as DrawElements)
69 * if:
70 *
71 * * the draw buffer for color output zero selects multiple color
72 * buffers (e.g., FRONT_AND_BACK in the default framebuffer); or
73 *
74 * * the draw buffer for any other color output is not NONE."
75 */
76 if (ctx->DrawBuffer->ColorDrawBuffer[0] == GL_FRONT_AND_BACK) {
77 _mesa_error(ctx, GL_INVALID_OPERATION,
78 "advanced blending is active and draw buffer for color "
79 "output zero selects multiple color buffers");
80 return false;
81 }
82
83 for (unsigned i = 1; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
84 if (ctx->DrawBuffer->ColorDrawBuffer[i] != GL_NONE) {
85 _mesa_error(ctx, GL_INVALID_OPERATION,
86 "advanced blending is active with multiple color "
87 "draw buffers");
88 return false;
89 }
90 }
91
92 /* The KHR_blend_equation_advanced spec says:
93 *
94 * "Advanced blending equations require the use of a fragment shader
95 * with a matching "blend_support" layout qualifier. If the current
96 * blend equation is found in table X.1 or X.2, and the active
97 * fragment shader does not include the layout qualifier matching
98 * the blend equation or "blend_support_all_equations", the error
99 * INVALID_OPERATION is generated [...]"
100 */
101 const struct gl_program *prog = ctx->FragmentProgram._Current;
102 const GLbitfield blend_support = !prog ? 0 : prog->sh.fs.BlendSupport;
103
104 if ((blend_support & ctx->Color._AdvancedBlendMode) == 0) {
105 _mesa_error(ctx, GL_INVALID_OPERATION,
106 "fragment shader does not allow advanced blending mode "
107 "(%s)",
108 _mesa_enum_to_string(ctx->Color.Blend[0].EquationRGB));
109 }
110 }
111
112 return true;
113 }
114
115
116 /**
117 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
118 * is called to see if it's valid to render. This involves checking that
119 * the current shader is valid and the framebuffer is complete.
120 * It also check the current pipeline object is valid if any.
121 * If an error is detected it'll be recorded here.
122 * \return GL_TRUE if OK to render, GL_FALSE if not
123 */
124 GLboolean
125 _mesa_valid_to_render(struct gl_context *ctx, const char *where)
126 {
127 /* This depends on having up to date derived state (shaders) */
128 if (ctx->NewState)
129 _mesa_update_state(ctx);
130
131 if (ctx->API == API_OPENGL_COMPAT) {
132 /* Any shader stages that are not supplied by the GLSL shader and have
133 * assembly shaders enabled must now be validated.
134 */
135 if (!ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] &&
136 ctx->VertexProgram.Enabled &&
137 !_mesa_arb_vertex_program_enabled(ctx)) {
138 _mesa_error(ctx, GL_INVALID_OPERATION,
139 "%s(vertex program not valid)", where);
140 return GL_FALSE;
141 }
142
143 if (!ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT]) {
144 if (ctx->FragmentProgram.Enabled &&
145 !_mesa_arb_fragment_program_enabled(ctx)) {
146 _mesa_error(ctx, GL_INVALID_OPERATION,
147 "%s(fragment program not valid)", where);
148 return GL_FALSE;
149 }
150
151 /* If drawing to integer-valued color buffers, there must be an
152 * active fragment shader (GL_EXT_texture_integer).
153 */
154 if (ctx->DrawBuffer && ctx->DrawBuffer->_IntegerBuffers) {
155 _mesa_error(ctx, GL_INVALID_OPERATION,
156 "%s(integer format but no fragment shader)", where);
157 return GL_FALSE;
158 }
159 }
160 }
161
162 /* A pipeline object is bound */
163 if (ctx->_Shader->Name && !ctx->_Shader->Validated) {
164 if (!_mesa_validate_program_pipeline(ctx, ctx->_Shader)) {
165 _mesa_error(ctx, GL_INVALID_OPERATION,
166 "glValidateProgramPipeline failed to validate the "
167 "pipeline");
168 return GL_FALSE;
169 }
170 }
171
172 /* If a program is active and SSO not in use, check if validation of
173 * samplers succeeded for the active program. */
174 if (ctx->_Shader->ActiveProgram && ctx->_Shader != ctx->Pipeline.Current) {
175 char errMsg[100];
176 if (!_mesa_sampler_uniforms_are_valid(ctx->_Shader->ActiveProgram,
177 errMsg, 100)) {
178 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", errMsg);
179 return GL_FALSE;
180 }
181 }
182
183 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
184 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
185 "%s(incomplete framebuffer)", where);
186 return GL_FALSE;
187 }
188
189 if (!check_blend_func_error(ctx)) {
190 return GL_FALSE;
191 }
192
193 /* From the GL_NV_fill_rectangle spec:
194 *
195 * "An INVALID_OPERATION error is generated by Begin or any Draw command if
196 * only one of the front and back polygon mode is FILL_RECTANGLE_NV."
197 */
198 if ((ctx->Polygon.FrontMode == GL_FILL_RECTANGLE_NV) !=
199 (ctx->Polygon.BackMode == GL_FILL_RECTANGLE_NV)) {
200 _mesa_error(ctx, GL_INVALID_OPERATION,
201 "GL_FILL_RECTANGLE_NV must be used as both front/back "
202 "polygon mode or neither");
203 return GL_FALSE;
204 }
205
206 #ifdef DEBUG
207 if (ctx->_Shader->Flags & GLSL_LOG) {
208 struct gl_program **prog = ctx->_Shader->CurrentProgram;
209
210 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
211 if (prog[i] == NULL || prog[i]->_Used)
212 continue;
213
214 /* This is the first time this shader is being used.
215 * Append shader's constants/uniforms to log file.
216 *
217 * Only log data for the program target that matches the shader
218 * target. It's possible to have a program bound to the vertex
219 * shader target that also supplied a fragment shader. If that
220 * program isn't also bound to the fragment shader target we don't
221 * want to log its fragment data.
222 */
223 _mesa_append_uniforms_to_file(prog[i]);
224 }
225
226 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
227 if (prog[i] != NULL)
228 prog[i]->_Used = GL_TRUE;
229 }
230 }
231 #endif
232
233 return GL_TRUE;
234 }
235
236
237 /**
238 * Check if OK to draw arrays/elements.
239 */
240 static bool
241 check_valid_to_render(struct gl_context *ctx, const char *function)
242 {
243 if (!_mesa_valid_to_render(ctx, function)) {
244 return false;
245 }
246
247 /* Section 6.3.2 from the GL 4.5:
248 * "Any GL command which attempts to read from, write to, or change
249 * the state of a buffer object may generate an INVALID_OPERATION error if
250 * all or part of the buffer object is mapped ... However, only commands
251 * which explicitly describe this error are required to do so. If an error
252 * is not generated, such commands will have undefined results and may
253 * result in GL interruption or termination."
254 *
255 * Only some buffer API functions require INVALID_OPERATION with mapped
256 * buffers. No other functions list such an error, thus it's not required
257 * to report INVALID_OPERATION for draw calls with mapped buffers.
258 */
259 if (!ctx->Const.AllowMappedBuffersDuringExecution &&
260 !_mesa_all_buffers_are_unmapped(ctx->Array.VAO)) {
261 _mesa_error(ctx, GL_INVALID_OPERATION,
262 "%s(vertex buffers are mapped)", function);
263 return false;
264 }
265
266 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
267 *
268 * "An INVALID_OPERATION error is generated by any command that
269 * transfers vertices to the GL if the current program state has
270 * one but not both of a tessellation control shader and tessellation
271 * evaluation shader."
272 *
273 * The OpenGL spec argues that this is allowed because a tess ctrl shader
274 * without a tess eval shader can be used with transform feedback.
275 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
276 * therefore doesn't allow tessellation.
277 *
278 * Further investigation showed that this is indeed a spec bug and
279 * a tess ctrl shader without a tess eval shader shouldn't have been
280 * allowed, because there is no API in GL 4.0 that can make use this
281 * to produce something useful.
282 *
283 * Also, all vendors except one don't support a tess ctrl shader without
284 * a tess eval shader anyway.
285 */
286 if (ctx->TessCtrlProgram._Current && !ctx->TessEvalProgram._Current) {
287 _mesa_error(ctx, GL_INVALID_OPERATION,
288 "%s(tess eval shader is missing)", function);
289 return false;
290 }
291
292 switch (ctx->API) {
293 case API_OPENGLES2:
294 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
295 *
296 * "An INVALID_OPERATION error is generated by any command that
297 * transfers vertices to the GL if the current program state has
298 * one but not both of a tessellation control shader and tessellation
299 * evaluation shader."
300 */
301 if (_mesa_is_gles3(ctx) &&
302 ctx->TessEvalProgram._Current && !ctx->TessCtrlProgram._Current) {
303 _mesa_error(ctx, GL_INVALID_OPERATION,
304 "%s(tess ctrl shader is missing)", function);
305 return false;
306 }
307 break;
308
309 case API_OPENGL_CORE:
310 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
311 * Core Profile spec says:
312 *
313 * "An INVALID_OPERATION error is generated if no vertex array
314 * object is bound (see section 10.3.1)."
315 */
316 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
317 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no VAO bound)", function);
318 return false;
319 }
320 break;
321
322 case API_OPENGLES:
323 case API_OPENGL_COMPAT:
324 break;
325
326 default:
327 unreachable("Invalid API value in check_valid_to_render()");
328 }
329
330 return true;
331 }
332
333
334 /**
335 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
336 * etc? The set of legal values depends on whether geometry shaders/programs
337 * are supported.
338 * Note: This may be called during display list compilation.
339 */
340 bool
341 _mesa_is_valid_prim_mode(const struct gl_context *ctx, GLenum mode)
342 {
343 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
344 * first and exit. You would think that a switch-statement would be the
345 * right approach, but at least GCC 4.7.2 generates some pretty dire code
346 * for the common case.
347 */
348 if (likely(mode <= GL_TRIANGLE_FAN))
349 return true;
350
351 if (mode <= GL_POLYGON)
352 return (ctx->API == API_OPENGL_COMPAT);
353
354 if (mode <= GL_TRIANGLE_STRIP_ADJACENCY)
355 return _mesa_has_geometry_shaders(ctx);
356
357 if (mode == GL_PATCHES)
358 return _mesa_has_tessellation(ctx);
359
360 return false;
361 }
362
363
364 /**
365 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
366 * etc? Also, do additional checking related to transformation feedback.
367 * Note: this function cannot be called during glNewList(GL_COMPILE) because
368 * this code depends on current transform feedback state.
369 * Also, do additional checking related to tessellation shaders.
370 */
371 GLboolean
372 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
373 {
374 bool valid_enum = _mesa_is_valid_prim_mode(ctx, mode);
375
376 if (!valid_enum) {
377 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
378 return GL_FALSE;
379 }
380
381 /* From the OpenGL 4.5 specification, section 11.3.1:
382 *
383 * The error INVALID_OPERATION is generated if Begin, or any command that
384 * implicitly calls Begin, is called when a geometry shader is active and:
385 *
386 * * the input primitive type of the current geometry shader is
387 * POINTS and <mode> is not POINTS,
388 *
389 * * the input primitive type of the current geometry shader is
390 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
391 *
392 * * the input primitive type of the current geometry shader is
393 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
394 * TRIANGLE_FAN,
395 *
396 * * the input primitive type of the current geometry shader is
397 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
398 * LINE_STRIP_ADJACENCY_ARB, or
399 *
400 * * the input primitive type of the current geometry shader is
401 * TRIANGLES_ADJACENCY_ARB and <mode> is not
402 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
403 *
404 * The GL spec doesn't mention any interaction with tessellation, which
405 * is clearly a spec bug. The same rule should apply, but instead of
406 * the draw primitive mode, the tessellation evaluation shader primitive
407 * mode should be used for the checking.
408 */
409 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
410 const GLenum geom_mode =
411 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
412 info.gs.input_primitive;
413 struct gl_program *tes =
414 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
415 GLenum mode_before_gs = mode;
416
417 if (tes) {
418 if (tes->info.tess.point_mode)
419 mode_before_gs = GL_POINTS;
420 else if (tes->info.tess.primitive_mode == GL_ISOLINES)
421 mode_before_gs = GL_LINES;
422 else
423 /* the GL_QUADS mode generates triangles too */
424 mode_before_gs = GL_TRIANGLES;
425 }
426
427 switch (mode_before_gs) {
428 case GL_POINTS:
429 valid_enum = (geom_mode == GL_POINTS);
430 break;
431 case GL_LINES:
432 case GL_LINE_LOOP:
433 case GL_LINE_STRIP:
434 valid_enum = (geom_mode == GL_LINES);
435 break;
436 case GL_TRIANGLES:
437 case GL_TRIANGLE_STRIP:
438 case GL_TRIANGLE_FAN:
439 valid_enum = (geom_mode == GL_TRIANGLES);
440 break;
441 case GL_QUADS:
442 case GL_QUAD_STRIP:
443 case GL_POLYGON:
444 valid_enum = false;
445 break;
446 case GL_LINES_ADJACENCY:
447 case GL_LINE_STRIP_ADJACENCY:
448 valid_enum = (geom_mode == GL_LINES_ADJACENCY);
449 break;
450 case GL_TRIANGLES_ADJACENCY:
451 case GL_TRIANGLE_STRIP_ADJACENCY:
452 valid_enum = (geom_mode == GL_TRIANGLES_ADJACENCY);
453 break;
454 default:
455 valid_enum = false;
456 break;
457 }
458 if (!valid_enum) {
459 _mesa_error(ctx, GL_INVALID_OPERATION,
460 "%s(mode=%s vs geometry shader input %s)",
461 name,
462 _mesa_lookup_prim_by_nr(mode_before_gs),
463 _mesa_lookup_prim_by_nr(geom_mode));
464 return GL_FALSE;
465 }
466 }
467
468 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
469 *
470 * "Tessellation operates only on patch primitives. If tessellation is
471 * active, any command that transfers vertices to the GL will
472 * generate an INVALID_OPERATION error if the primitive mode is not
473 * PATCHES.
474 * Patch primitives are not supported by pipeline stages below the
475 * tessellation evaluation shader. If there is no active program
476 * object or the active program object does not contain a tessellation
477 * evaluation shader, the error INVALID_OPERATION is generated by any
478 * command that transfers vertices to the GL if the primitive mode is
479 * PATCHES."
480 *
481 */
482 if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL] ||
483 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL]) {
484 if (mode != GL_PATCHES) {
485 _mesa_error(ctx, GL_INVALID_OPERATION,
486 "only GL_PATCHES valid with tessellation");
487 return GL_FALSE;
488 }
489 }
490 else {
491 if (mode == GL_PATCHES) {
492 _mesa_error(ctx, GL_INVALID_OPERATION,
493 "GL_PATCHES only valid with tessellation");
494 return GL_FALSE;
495 }
496 }
497
498 /* From the GL_EXT_transform_feedback spec:
499 *
500 * "The error INVALID_OPERATION is generated if Begin, or any command
501 * that performs an explicit Begin, is called when:
502 *
503 * * a geometry shader is not active and <mode> does not match the
504 * allowed begin modes for the current transform feedback state as
505 * given by table X.1.
506 *
507 * * a geometry shader is active and the output primitive type of the
508 * geometry shader does not match the allowed begin modes for the
509 * current transform feedback state as given by table X.1.
510 *
511 */
512 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
513 GLboolean pass = GL_TRUE;
514
515 if(ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
516 switch (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
517 info.gs.output_primitive) {
518 case GL_POINTS:
519 pass = ctx->TransformFeedback.Mode == GL_POINTS;
520 break;
521 case GL_LINE_STRIP:
522 pass = ctx->TransformFeedback.Mode == GL_LINES;
523 break;
524 case GL_TRIANGLE_STRIP:
525 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
526 break;
527 default:
528 pass = GL_FALSE;
529 }
530 }
531 else if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL]) {
532 struct gl_program *tes =
533 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
534 if (tes->info.tess.point_mode)
535 pass = ctx->TransformFeedback.Mode == GL_POINTS;
536 else if (tes->info.tess.primitive_mode == GL_ISOLINES)
537 pass = ctx->TransformFeedback.Mode == GL_LINES;
538 else
539 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
540 }
541 else {
542 switch (mode) {
543 case GL_POINTS:
544 pass = ctx->TransformFeedback.Mode == GL_POINTS;
545 break;
546 case GL_LINES:
547 case GL_LINE_STRIP:
548 case GL_LINE_LOOP:
549 pass = ctx->TransformFeedback.Mode == GL_LINES;
550 break;
551 default:
552 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
553 break;
554 }
555 }
556 if (!pass) {
557 _mesa_error(ctx, GL_INVALID_OPERATION,
558 "%s(mode=%s vs transform feedback %s)",
559 name,
560 _mesa_lookup_prim_by_nr(mode),
561 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
562 return GL_FALSE;
563 }
564 }
565
566 /* From GL_INTEL_conservative_rasterization spec:
567 *
568 * The conservative rasterization option applies only to polygons with
569 * PolygonMode state set to FILL. Draw requests for polygons with different
570 * PolygonMode setting or for other primitive types (points/lines) generate
571 * INVALID_OPERATION error.
572 */
573 if (ctx->IntelConservativeRasterization) {
574 GLboolean pass = GL_TRUE;
575
576 switch (mode) {
577 case GL_POINTS:
578 case GL_LINES:
579 case GL_LINE_LOOP:
580 case GL_LINE_STRIP:
581 case GL_LINES_ADJACENCY:
582 case GL_LINE_STRIP_ADJACENCY:
583 pass = GL_FALSE;
584 break;
585 case GL_TRIANGLES:
586 case GL_TRIANGLE_STRIP:
587 case GL_TRIANGLE_FAN:
588 case GL_QUADS:
589 case GL_QUAD_STRIP:
590 case GL_POLYGON:
591 case GL_TRIANGLES_ADJACENCY:
592 case GL_TRIANGLE_STRIP_ADJACENCY:
593 if (ctx->Polygon.FrontMode != GL_FILL ||
594 ctx->Polygon.BackMode != GL_FILL)
595 pass = GL_FALSE;
596 break;
597 default:
598 pass = GL_FALSE;
599 }
600 if (!pass) {
601 _mesa_error(ctx, GL_INVALID_OPERATION,
602 "mode=%s invalid with GL_INTEL_conservative_rasterization",
603 _mesa_lookup_prim_by_nr(mode));
604 return GL_FALSE;
605 }
606 }
607
608 return GL_TRUE;
609 }
610
611 /**
612 * Verify that the element type is valid.
613 *
614 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
615 */
616 static bool
617 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
618 {
619 switch (type) {
620 case GL_UNSIGNED_BYTE:
621 case GL_UNSIGNED_SHORT:
622 case GL_UNSIGNED_INT:
623 return true;
624
625 default:
626 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
627 _mesa_enum_to_string(type));
628 return false;
629 }
630 }
631
632 static bool
633 validate_DrawElements_common(struct gl_context *ctx,
634 GLenum mode, GLsizei count, GLenum type,
635 const GLvoid *indices,
636 const char *caller)
637 {
638 /* Section 2.14.2 (Transform Feedback Primitive Capture) of the OpenGL ES
639 * 3.1 spec says:
640 *
641 * The error INVALID_OPERATION is also generated by DrawElements,
642 * DrawElementsInstanced, and DrawRangeElements while transform feedback
643 * is active and not paused, regardless of mode.
644 *
645 * The OES_geometry_shader_spec says:
646 *
647 * Issues:
648 *
649 * ...
650 *
651 * (13) Does this extension change how transform feedback operates
652 * compared to unextended OpenGL ES 3.0 or 3.1?
653 *
654 * RESOLVED: Yes... Since we no longer require being able to predict how
655 * much geometry will be generated, we also lift the restriction that
656 * only DrawArray* commands are supported and also support the
657 * DrawElements* commands for transform feedback.
658 *
659 * This should also be reflected in the body of the spec, but that appears
660 * to have been overlooked. The body of the spec only explicitly allows
661 * the indirect versions.
662 */
663 if (_mesa_is_gles3(ctx) &&
664 !_mesa_has_OES_geometry_shader(ctx) &&
665 _mesa_is_xfb_active_and_unpaused(ctx)) {
666 _mesa_error(ctx, GL_INVALID_OPERATION,
667 "%s(transform feedback active)", caller);
668 return false;
669 }
670
671 if (count < 0) {
672 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", caller);
673 return false;
674 }
675
676 if (!_mesa_valid_prim_mode(ctx, mode, caller)) {
677 return false;
678 }
679
680 if (!valid_elements_type(ctx, type, caller))
681 return false;
682
683 if (!check_valid_to_render(ctx, caller))
684 return false;
685
686 return true;
687 }
688
689 /**
690 * Error checking for glDrawElements(). Includes parameter checking
691 * and VBO bounds checking.
692 * \return GL_TRUE if OK to render, GL_FALSE if error found
693 */
694 GLboolean
695 _mesa_validate_DrawElements(struct gl_context *ctx,
696 GLenum mode, GLsizei count, GLenum type,
697 const GLvoid *indices)
698 {
699 FLUSH_CURRENT(ctx, 0);
700
701 return validate_DrawElements_common(ctx, mode, count, type, indices,
702 "glDrawElements");
703 }
704
705
706 /**
707 * Error checking for glMultiDrawElements(). Includes parameter checking
708 * and VBO bounds checking.
709 * \return GL_TRUE if OK to render, GL_FALSE if error found
710 */
711 GLboolean
712 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
713 GLenum mode, const GLsizei *count,
714 GLenum type, const GLvoid * const *indices,
715 GLsizei primcount)
716 {
717 GLsizei i;
718
719 FLUSH_CURRENT(ctx, 0);
720
721 /*
722 * Section 2.3.1 (Errors) of the OpenGL 4.5 (Core Profile) spec says:
723 *
724 * "If a negative number is provided where an argument of type sizei or
725 * sizeiptr is specified, an INVALID_VALUE error is generated."
726 *
727 * and in the same section:
728 *
729 * "In other cases, there are no side effects unless otherwise noted;
730 * the command which generates the error is ignored so that it has no
731 * effect on GL state or framebuffer contents."
732 *
733 * Hence, check both primcount and all the count[i].
734 */
735 if (primcount < 0) {
736 _mesa_error(ctx, GL_INVALID_VALUE,
737 "glMultiDrawElements(primcount=%d)", primcount);
738 return GL_FALSE;
739 }
740
741 for (i = 0; i < primcount; i++) {
742 if (count[i] < 0) {
743 _mesa_error(ctx, GL_INVALID_VALUE,
744 "glMultiDrawElements(count)" );
745 return GL_FALSE;
746 }
747 }
748
749 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
750 return GL_FALSE;
751 }
752
753 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
754 return GL_FALSE;
755
756 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
757 return GL_FALSE;
758
759 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
760 */
761 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
762 for (i = 0; i < primcount; i++) {
763 if (!indices[i])
764 return GL_FALSE;
765 }
766 }
767
768 return GL_TRUE;
769 }
770
771
772 /**
773 * Error checking for glDrawRangeElements(). Includes parameter checking
774 * and VBO bounds checking.
775 * \return GL_TRUE if OK to render, GL_FALSE if error found
776 */
777 GLboolean
778 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
779 GLuint start, GLuint end,
780 GLsizei count, GLenum type,
781 const GLvoid *indices)
782 {
783 FLUSH_CURRENT(ctx, 0);
784
785 if (end < start) {
786 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
787 return GL_FALSE;
788 }
789
790 return validate_DrawElements_common(ctx, mode, count, type, indices,
791 "glDrawRangeElements");
792 }
793
794
795 static bool
796 need_xfb_remaining_prims_check(const struct gl_context *ctx)
797 {
798 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
799 * Primitive Capture):
800 *
801 * The error INVALID_OPERATION is generated by DrawArrays and
802 * DrawArraysInstanced if recording the vertices of a primitive to the
803 * buffer objects being used for transform feedback purposes would result
804 * in either exceeding the limits of any buffer object’s size, or in
805 * exceeding the end position offset + size − 1, as set by
806 * BindBufferRange.
807 *
808 * This is in contrast to the behaviour of desktop GL, where the extra
809 * primitives are silently dropped from the transform feedback buffer.
810 *
811 * This text is removed in ES 3.2, presumably because it's not really
812 * implementable with geometry and tessellation shaders. In fact,
813 * the OES_geometry_shader spec says:
814 *
815 * "(13) Does this extension change how transform feedback operates
816 * compared to unextended OpenGL ES 3.0 or 3.1?
817 *
818 * RESOLVED: Yes. Because dynamic geometry amplification in a geometry
819 * shader can make it difficult if not impossible to predict the amount
820 * of geometry that may be generated in advance of executing the shader,
821 * the draw-time error for transform feedback buffer overflow conditions
822 * is removed and replaced with the GL behavior (primitives are not
823 * written and the corresponding counter is not updated)..."
824 */
825 return _mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx) &&
826 !_mesa_has_OES_geometry_shader(ctx) &&
827 !_mesa_has_OES_tessellation_shader(ctx);
828 }
829
830
831 /**
832 * Figure out the number of transform feedback primitives that will be output
833 * considering the drawing mode, number of vertices, and instance count,
834 * assuming that no geometry shading is done and primitive restart is not
835 * used.
836 *
837 * This is used by driver back-ends in implementing the PRIMITIVES_GENERATED
838 * and TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN queries. It is also used to
839 * pre-validate draw calls in GLES3 (where draw calls only succeed if there is
840 * enough room in the transform feedback buffer for the result).
841 */
842 static size_t
843 count_tessellated_primitives(GLenum mode, GLuint count, GLuint num_instances)
844 {
845 size_t num_primitives;
846 switch (mode) {
847 case GL_POINTS:
848 num_primitives = count;
849 break;
850 case GL_LINE_STRIP:
851 num_primitives = count >= 2 ? count - 1 : 0;
852 break;
853 case GL_LINE_LOOP:
854 num_primitives = count >= 2 ? count : 0;
855 break;
856 case GL_LINES:
857 num_primitives = count / 2;
858 break;
859 case GL_TRIANGLE_STRIP:
860 case GL_TRIANGLE_FAN:
861 case GL_POLYGON:
862 num_primitives = count >= 3 ? count - 2 : 0;
863 break;
864 case GL_TRIANGLES:
865 num_primitives = count / 3;
866 break;
867 case GL_QUAD_STRIP:
868 num_primitives = count >= 4 ? ((count / 2) - 1) * 2 : 0;
869 break;
870 case GL_QUADS:
871 num_primitives = (count / 4) * 2;
872 break;
873 case GL_LINES_ADJACENCY:
874 num_primitives = count / 4;
875 break;
876 case GL_LINE_STRIP_ADJACENCY:
877 num_primitives = count >= 4 ? count - 3 : 0;
878 break;
879 case GL_TRIANGLES_ADJACENCY:
880 num_primitives = count / 6;
881 break;
882 case GL_TRIANGLE_STRIP_ADJACENCY:
883 num_primitives = count >= 6 ? (count - 4) / 2 : 0;
884 break;
885 default:
886 assert(!"Unexpected primitive type in count_tessellated_primitives");
887 num_primitives = 0;
888 break;
889 }
890 return num_primitives * num_instances;
891 }
892
893
894 static bool
895 validate_draw_arrays(struct gl_context *ctx, const char *func,
896 GLenum mode, GLsizei count, GLsizei numInstances)
897 {
898 FLUSH_CURRENT(ctx, 0);
899
900 if (count < 0) {
901 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", func);
902 return false;
903 }
904
905 if (!_mesa_valid_prim_mode(ctx, mode, func))
906 return false;
907
908 if (!check_valid_to_render(ctx, func))
909 return false;
910
911 if (need_xfb_remaining_prims_check(ctx)) {
912 struct gl_transform_feedback_object *xfb_obj
913 = ctx->TransformFeedback.CurrentObject;
914 size_t prim_count = count_tessellated_primitives(mode, count, numInstances);
915 if (xfb_obj->GlesRemainingPrims < prim_count) {
916 _mesa_error(ctx, GL_INVALID_OPERATION,
917 "%s(exceeds transform feedback size)", func);
918 return false;
919 }
920 xfb_obj->GlesRemainingPrims -= prim_count;
921 }
922
923 if (count == 0)
924 return false;
925
926 return true;
927 }
928
929 /**
930 * Called from the tnl module to error check the function parameters and
931 * verify that we really can draw something.
932 * \return GL_TRUE if OK to render, GL_FALSE if error found
933 */
934 GLboolean
935 _mesa_validate_DrawArrays(struct gl_context *ctx, GLenum mode, GLsizei count)
936 {
937 return validate_draw_arrays(ctx, "glDrawArrays", mode, count, 1);
938 }
939
940
941 GLboolean
942 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
943 GLsizei count, GLsizei numInstances)
944 {
945 if (first < 0) {
946 _mesa_error(ctx, GL_INVALID_VALUE,
947 "glDrawArraysInstanced(start=%d)", first);
948 return GL_FALSE;
949 }
950
951 if (numInstances <= 0) {
952 if (numInstances < 0)
953 _mesa_error(ctx, GL_INVALID_VALUE,
954 "glDrawArraysInstanced(numInstances=%d)", numInstances);
955 return GL_FALSE;
956 }
957
958 return validate_draw_arrays(ctx, "glDrawArraysInstanced", mode, count, 1);
959 }
960
961
962 /**
963 * Called to error check the function parameters.
964 *
965 * Note that glMultiDrawArrays is not part of GLES, so there's limited scope
966 * for sharing code with the validation of glDrawArrays.
967 */
968 bool
969 _mesa_validate_MultiDrawArrays(struct gl_context *ctx, GLenum mode,
970 const GLsizei *count, GLsizei primcount)
971 {
972 int i;
973
974 FLUSH_CURRENT(ctx, 0);
975
976 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawArrays"))
977 return false;
978
979 if (!check_valid_to_render(ctx, "glMultiDrawArrays"))
980 return false;
981
982 if (primcount < 0) {
983 _mesa_error(ctx, GL_INVALID_VALUE, "glMultiDrawArrays(primcount=%d)",
984 primcount);
985 return false;
986 }
987
988 for (i = 0; i < primcount; ++i) {
989 if (count[i] < 0) {
990 _mesa_error(ctx, GL_INVALID_VALUE, "glMultiDrawArrays(count[%d]=%d)",
991 i, count[i]);
992 return false;
993 }
994 }
995
996 if (need_xfb_remaining_prims_check(ctx)) {
997 struct gl_transform_feedback_object *xfb_obj
998 = ctx->TransformFeedback.CurrentObject;
999 size_t xfb_prim_count = 0;
1000
1001 for (i = 0; i < primcount; ++i)
1002 xfb_prim_count += count_tessellated_primitives(mode, count[i], 1);
1003
1004 if (xfb_obj->GlesRemainingPrims < xfb_prim_count) {
1005 _mesa_error(ctx, GL_INVALID_OPERATION,
1006 "glMultiDrawArrays(exceeds transform feedback size)");
1007 return false;
1008 }
1009 xfb_obj->GlesRemainingPrims -= xfb_prim_count;
1010 }
1011
1012 return true;
1013 }
1014
1015
1016 GLboolean
1017 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
1018 GLenum mode, GLsizei count, GLenum type,
1019 const GLvoid *indices, GLsizei numInstances)
1020 {
1021 FLUSH_CURRENT(ctx, 0);
1022
1023 if (numInstances < 0) {
1024 _mesa_error(ctx, GL_INVALID_VALUE,
1025 "glDrawElementsInstanced(numInstances=%d)", numInstances);
1026 return GL_FALSE;
1027 }
1028
1029 return validate_DrawElements_common(ctx, mode, count, type, indices,
1030 "glDrawElementsInstanced")
1031 && (numInstances > 0);
1032 }
1033
1034
1035 GLboolean
1036 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
1037 GLenum mode,
1038 struct gl_transform_feedback_object *obj,
1039 GLuint stream,
1040 GLsizei numInstances)
1041 {
1042 FLUSH_CURRENT(ctx, 0);
1043
1044 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
1045 return GL_FALSE;
1046 }
1047
1048 if (!obj) {
1049 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
1050 return GL_FALSE;
1051 }
1052
1053 /* From the GL 4.5 specification, page 429:
1054 * "An INVALID_VALUE error is generated if id is not the name of a
1055 * transform feedback object."
1056 */
1057 if (!obj->EverBound) {
1058 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
1059 return GL_FALSE;
1060 }
1061
1062 if (stream >= ctx->Const.MaxVertexStreams) {
1063 _mesa_error(ctx, GL_INVALID_VALUE,
1064 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
1065 return GL_FALSE;
1066 }
1067
1068 if (!obj->EndedAnytime) {
1069 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
1070 return GL_FALSE;
1071 }
1072
1073 if (numInstances <= 0) {
1074 if (numInstances < 0)
1075 _mesa_error(ctx, GL_INVALID_VALUE,
1076 "glDrawTransformFeedback*Instanced(numInstances=%d)",
1077 numInstances);
1078 return GL_FALSE;
1079 }
1080
1081 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {
1082 return GL_FALSE;
1083 }
1084
1085 return GL_TRUE;
1086 }
1087
1088 static GLboolean
1089 valid_draw_indirect(struct gl_context *ctx,
1090 GLenum mode, const GLvoid *indirect,
1091 GLsizei size, const char *name)
1092 {
1093 const uint64_t end = (uint64_t) (uintptr_t) indirect + size;
1094
1095 /* OpenGL ES 3.1 spec. section 10.5:
1096 *
1097 * "DrawArraysIndirect requires that all data sourced for the
1098 * command, including the DrawArraysIndirectCommand
1099 * structure, be in buffer objects, and may not be called when
1100 * the default vertex array object is bound."
1101 */
1102 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
1103 _mesa_error(ctx, GL_INVALID_OPERATION, "(no VAO bound)");
1104 return GL_FALSE;
1105 }
1106
1107 /* From OpenGL ES 3.1 spec. section 10.5:
1108 * "An INVALID_OPERATION error is generated if zero is bound to
1109 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
1110 * vertex array."
1111 *
1112 * Here we check that for each enabled vertex array we have a vertex
1113 * buffer bound.
1114 */
1115 if (_mesa_is_gles31(ctx) &&
1116 ctx->Array.VAO->_Enabled & ~ctx->Array.VAO->VertexAttribBufferMask) {
1117 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(No VBO bound)", name);
1118 return GL_FALSE;
1119 }
1120
1121 if (!_mesa_valid_prim_mode(ctx, mode, name))
1122 return GL_FALSE;
1123
1124 /* OpenGL ES 3.1 specification, section 10.5:
1125 *
1126 * "An INVALID_OPERATION error is generated if
1127 * transform feedback is active and not paused."
1128 *
1129 * The OES_geometry_shader spec says:
1130 *
1131 * On p. 250 in the errors section for the DrawArraysIndirect command,
1132 * and on p. 254 in the errors section for the DrawElementsIndirect
1133 * command, delete the errors which state:
1134 *
1135 * "An INVALID_OPERATION error is generated if transform feedback is
1136 * active and not paused."
1137 *
1138 * (thus allowing transform feedback to work with indirect draw commands).
1139 */
1140 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader &&
1141 _mesa_is_xfb_active_and_unpaused(ctx)) {
1142 _mesa_error(ctx, GL_INVALID_OPERATION,
1143 "%s(TransformFeedback is active and not paused)", name);
1144 }
1145
1146 /* From OpenGL version 4.4. section 10.5
1147 * and OpenGL ES 3.1, section 10.6:
1148 *
1149 * "An INVALID_VALUE error is generated if indirect is not a
1150 * multiple of the size, in basic machine units, of uint."
1151 */
1152 if ((GLsizeiptr)indirect & (sizeof(GLuint) - 1)) {
1153 _mesa_error(ctx, GL_INVALID_VALUE,
1154 "%s(indirect is not aligned)", name);
1155 return GL_FALSE;
1156 }
1157
1158 if (!_mesa_is_bufferobj(ctx->DrawIndirectBuffer)) {
1159 _mesa_error(ctx, GL_INVALID_OPERATION,
1160 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name);
1161 return GL_FALSE;
1162 }
1163
1164 if (_mesa_check_disallowed_mapping(ctx->DrawIndirectBuffer)) {
1165 _mesa_error(ctx, GL_INVALID_OPERATION,
1166 "%s(DRAW_INDIRECT_BUFFER is mapped)", name);
1167 return GL_FALSE;
1168 }
1169
1170 /* From the ARB_draw_indirect specification:
1171 * "An INVALID_OPERATION error is generated if the commands source data
1172 * beyond the end of the buffer object [...]"
1173 */
1174 if (ctx->DrawIndirectBuffer->Size < end) {
1175 _mesa_error(ctx, GL_INVALID_OPERATION,
1176 "%s(DRAW_INDIRECT_BUFFER too small)", name);
1177 return GL_FALSE;
1178 }
1179
1180 if (!check_valid_to_render(ctx, name))
1181 return GL_FALSE;
1182
1183 return GL_TRUE;
1184 }
1185
1186 static inline GLboolean
1187 valid_draw_indirect_elements(struct gl_context *ctx,
1188 GLenum mode, GLenum type, const GLvoid *indirect,
1189 GLsizeiptr size, const char *name)
1190 {
1191 if (!valid_elements_type(ctx, type, name))
1192 return GL_FALSE;
1193
1194 /*
1195 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1196 * may not come from a client array and must come from an index buffer.
1197 * If no element array buffer is bound, an INVALID_OPERATION error is
1198 * generated.
1199 */
1200 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
1201 _mesa_error(ctx, GL_INVALID_OPERATION,
1202 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name);
1203 return GL_FALSE;
1204 }
1205
1206 return valid_draw_indirect(ctx, mode, indirect, size, name);
1207 }
1208
1209 static inline GLboolean
1210 valid_draw_indirect_multi(struct gl_context *ctx,
1211 GLsizei primcount, GLsizei stride,
1212 const char *name)
1213 {
1214
1215 /* From the ARB_multi_draw_indirect specification:
1216 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
1217 * MultiDrawElementsIndirect if <primcount> is negative."
1218 *
1219 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
1220 * be generated."
1221 */
1222 if (primcount < 0) {
1223 _mesa_error(ctx, GL_INVALID_VALUE, "%s(primcount < 0)", name);
1224 return GL_FALSE;
1225 }
1226
1227
1228 /* From the ARB_multi_draw_indirect specification:
1229 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
1230 * error is generated."
1231 */
1232 if (stride % 4) {
1233 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride %% 4)", name);
1234 return GL_FALSE;
1235 }
1236
1237 return GL_TRUE;
1238 }
1239
1240 GLboolean
1241 _mesa_validate_DrawArraysIndirect(struct gl_context *ctx,
1242 GLenum mode,
1243 const GLvoid *indirect)
1244 {
1245 const unsigned drawArraysNumParams = 4;
1246
1247 FLUSH_CURRENT(ctx, 0);
1248
1249 return valid_draw_indirect(ctx, mode,
1250 indirect, drawArraysNumParams * sizeof(GLuint),
1251 "glDrawArraysIndirect");
1252 }
1253
1254 GLboolean
1255 _mesa_validate_DrawElementsIndirect(struct gl_context *ctx,
1256 GLenum mode, GLenum type,
1257 const GLvoid *indirect)
1258 {
1259 const unsigned drawElementsNumParams = 5;
1260
1261 FLUSH_CURRENT(ctx, 0);
1262
1263 return valid_draw_indirect_elements(ctx, mode, type,
1264 indirect, drawElementsNumParams * sizeof(GLuint),
1265 "glDrawElementsIndirect");
1266 }
1267
1268 GLboolean
1269 _mesa_validate_MultiDrawArraysIndirect(struct gl_context *ctx,
1270 GLenum mode,
1271 const GLvoid *indirect,
1272 GLsizei primcount, GLsizei stride)
1273 {
1274 GLsizeiptr size = 0;
1275 const unsigned drawArraysNumParams = 4;
1276
1277 FLUSH_CURRENT(ctx, 0);
1278
1279 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1280 assert(stride != 0);
1281
1282 if (!valid_draw_indirect_multi(ctx, primcount, stride,
1283 "glMultiDrawArraysIndirect"))
1284 return GL_FALSE;
1285
1286 /* number of bytes of the indirect buffer which will be read */
1287 size = primcount
1288 ? (primcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
1289 : 0;
1290
1291 if (!valid_draw_indirect(ctx, mode, indirect, size,
1292 "glMultiDrawArraysIndirect"))
1293 return GL_FALSE;
1294
1295 return GL_TRUE;
1296 }
1297
1298 GLboolean
1299 _mesa_validate_MultiDrawElementsIndirect(struct gl_context *ctx,
1300 GLenum mode, GLenum type,
1301 const GLvoid *indirect,
1302 GLsizei primcount, GLsizei stride)
1303 {
1304 GLsizeiptr size = 0;
1305 const unsigned drawElementsNumParams = 5;
1306
1307 FLUSH_CURRENT(ctx, 0);
1308
1309 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1310 assert(stride != 0);
1311
1312 if (!valid_draw_indirect_multi(ctx, primcount, stride,
1313 "glMultiDrawElementsIndirect"))
1314 return GL_FALSE;
1315
1316 /* number of bytes of the indirect buffer which will be read */
1317 size = primcount
1318 ? (primcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1319 : 0;
1320
1321 if (!valid_draw_indirect_elements(ctx, mode, type,
1322 indirect, size,
1323 "glMultiDrawElementsIndirect"))
1324 return GL_FALSE;
1325
1326 return GL_TRUE;
1327 }
1328
1329 static GLboolean
1330 valid_draw_indirect_parameters(struct gl_context *ctx,
1331 const char *name,
1332 GLintptr drawcount)
1333 {
1334 /* From the ARB_indirect_parameters specification:
1335 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
1336 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
1337 * four."
1338 */
1339 if (drawcount & 3) {
1340 _mesa_error(ctx, GL_INVALID_VALUE,
1341 "%s(drawcount is not a multiple of 4)", name);
1342 return GL_FALSE;
1343 }
1344
1345 /* From the ARB_indirect_parameters specification:
1346 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1347 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
1348 * PARAMETER_BUFFER_ARB binding point."
1349 */
1350 if (!_mesa_is_bufferobj(ctx->ParameterBuffer)) {
1351 _mesa_error(ctx, GL_INVALID_OPERATION,
1352 "%s: no buffer bound to PARAMETER_BUFFER", name);
1353 return GL_FALSE;
1354 }
1355
1356 if (_mesa_check_disallowed_mapping(ctx->ParameterBuffer)) {
1357 _mesa_error(ctx, GL_INVALID_OPERATION,
1358 "%s(PARAMETER_BUFFER is mapped)", name);
1359 return GL_FALSE;
1360 }
1361
1362 /* From the ARB_indirect_parameters specification:
1363 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1364 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
1365 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
1366 * specified by <drawcount> would result in an out-of-bounds access."
1367 */
1368 if (ctx->ParameterBuffer->Size < drawcount + sizeof(GLsizei)) {
1369 _mesa_error(ctx, GL_INVALID_OPERATION,
1370 "%s(PARAMETER_BUFFER too small)", name);
1371 return GL_FALSE;
1372 }
1373
1374 return GL_TRUE;
1375 }
1376
1377 GLboolean
1378 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context *ctx,
1379 GLenum mode,
1380 GLintptr indirect,
1381 GLintptr drawcount,
1382 GLsizei maxdrawcount,
1383 GLsizei stride)
1384 {
1385 GLsizeiptr size = 0;
1386 const unsigned drawArraysNumParams = 4;
1387
1388 FLUSH_CURRENT(ctx, 0);
1389
1390 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1391 assert(stride != 0);
1392
1393 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1394 "glMultiDrawArraysIndirectCountARB"))
1395 return GL_FALSE;
1396
1397 /* number of bytes of the indirect buffer which will be read */
1398 size = maxdrawcount
1399 ? (maxdrawcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
1400 : 0;
1401
1402 if (!valid_draw_indirect(ctx, mode, (void *)indirect, size,
1403 "glMultiDrawArraysIndirectCountARB"))
1404 return GL_FALSE;
1405
1406 return valid_draw_indirect_parameters(
1407 ctx, "glMultiDrawArraysIndirectCountARB", drawcount);
1408 }
1409
1410 GLboolean
1411 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context *ctx,
1412 GLenum mode, GLenum type,
1413 GLintptr indirect,
1414 GLintptr drawcount,
1415 GLsizei maxdrawcount,
1416 GLsizei stride)
1417 {
1418 GLsizeiptr size = 0;
1419 const unsigned drawElementsNumParams = 5;
1420
1421 FLUSH_CURRENT(ctx, 0);
1422
1423 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1424 assert(stride != 0);
1425
1426 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1427 "glMultiDrawElementsIndirectCountARB"))
1428 return GL_FALSE;
1429
1430 /* number of bytes of the indirect buffer which will be read */
1431 size = maxdrawcount
1432 ? (maxdrawcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1433 : 0;
1434
1435 if (!valid_draw_indirect_elements(ctx, mode, type,
1436 (void *)indirect, size,
1437 "glMultiDrawElementsIndirectCountARB"))
1438 return GL_FALSE;
1439
1440 return valid_draw_indirect_parameters(
1441 ctx, "glMultiDrawElementsIndirectCountARB", drawcount);
1442 }