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