st/mesa: call glthread_destroy() before _vbo_DestroyContext()
[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->_Shader->_CurrentFragmentProgram;
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 #ifdef DEBUG
193 if (ctx->_Shader->Flags & GLSL_LOG) {
194 struct gl_program **prog = ctx->_Shader->CurrentProgram;
195
196 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
197 if (prog[i] == NULL || prog[i]->_Used)
198 continue;
199
200 /* This is the first time this shader is being used.
201 * Append shader's constants/uniforms to log file.
202 *
203 * Only log data for the program target that matches the shader
204 * target. It's possible to have a program bound to the vertex
205 * shader target that also supplied a fragment shader. If that
206 * program isn't also bound to the fragment shader target we don't
207 * want to log its fragment data.
208 */
209 _mesa_append_uniforms_to_file(prog[i]);
210 }
211
212 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
213 if (prog[i] != NULL)
214 prog[i]->_Used = GL_TRUE;
215 }
216 }
217 #endif
218
219 return GL_TRUE;
220 }
221
222
223 /**
224 * Check if OK to draw arrays/elements.
225 */
226 static bool
227 check_valid_to_render(struct gl_context *ctx, const char *function)
228 {
229 if (!_mesa_valid_to_render(ctx, function)) {
230 return false;
231 }
232
233 if (!_mesa_all_buffers_are_unmapped(ctx->Array.VAO)) {
234 _mesa_error(ctx, GL_INVALID_OPERATION,
235 "%s(vertex buffers are mapped)", function);
236 return false;
237 }
238
239 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
240 *
241 * "An INVALID_OPERATION error is generated by any command that
242 * transfers vertices to the GL if the current program state has
243 * one but not both of a tessellation control shader and tessellation
244 * evaluation shader."
245 *
246 * The OpenGL spec argues that this is allowed because a tess ctrl shader
247 * without a tess eval shader can be used with transform feedback.
248 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
249 * therefore doesn't allow tessellation.
250 *
251 * Further investigation showed that this is indeed a spec bug and
252 * a tess ctrl shader without a tess eval shader shouldn't have been
253 * allowed, because there is no API in GL 4.0 that can make use this
254 * to produce something useful.
255 *
256 * Also, all vendors except one don't support a tess ctrl shader without
257 * a tess eval shader anyway.
258 */
259 if (ctx->TessCtrlProgram._Current && !ctx->TessEvalProgram._Current) {
260 _mesa_error(ctx, GL_INVALID_OPERATION,
261 "%s(tess eval shader is missing)", function);
262 return false;
263 }
264
265 switch (ctx->API) {
266 case API_OPENGLES2:
267 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
268 *
269 * "An INVALID_OPERATION error is generated by any command that
270 * transfers vertices to the GL if the current program state has
271 * one but not both of a tessellation control shader and tessellation
272 * evaluation shader."
273 */
274 if (_mesa_is_gles3(ctx) &&
275 ctx->TessEvalProgram._Current && !ctx->TessCtrlProgram._Current) {
276 _mesa_error(ctx, GL_INVALID_OPERATION,
277 "%s(tess ctrl shader is missing)", function);
278 return false;
279 }
280
281 /* For ES2, we can draw if we have a vertex program/shader). */
282 return ctx->VertexProgram._Current != NULL;
283
284 case API_OPENGLES:
285 /* For OpenGL ES, only draw if we have vertex positions
286 */
287 if (!ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled)
288 return false;
289 break;
290
291 case API_OPENGL_CORE:
292 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
293 * Core Profile spec says:
294 *
295 * "An INVALID_OPERATION error is generated if no vertex array
296 * object is bound (see section 10.3.1)."
297 */
298 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
299 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no VAO bound)", function);
300 return false;
301 }
302
303 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
304 * says:
305 *
306 * "If there is no active program for the vertex or fragment shader
307 * stages, the results of vertex and/or fragment processing will be
308 * undefined. However, this is not an error."
309 *
310 * The fragment shader is not tested here because other state (e.g.,
311 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
312 */
313 return ctx->VertexProgram._Current != NULL;
314
315 case API_OPENGL_COMPAT:
316 if (ctx->VertexProgram._Current != NULL) {
317 /* Draw regardless of whether or not we have any vertex arrays.
318 * (Ex: could draw a point using a constant vertex pos)
319 */
320 return true;
321 } else {
322 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
323 * array [0]).
324 */
325 return (ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
326 ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
327 }
328 break;
329
330 default:
331 unreachable("Invalid API value in check_valid_to_render()");
332 }
333
334 return true;
335 }
336
337
338 /**
339 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
340 * etc? The set of legal values depends on whether geometry shaders/programs
341 * are supported.
342 * Note: This may be called during display list compilation.
343 */
344 bool
345 _mesa_is_valid_prim_mode(struct gl_context *ctx, GLenum mode)
346 {
347 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
348 * first and exit. You would think that a switch-statement would be the
349 * right approach, but at least GCC 4.7.2 generates some pretty dire code
350 * for the common case.
351 */
352 if (likely(mode <= GL_TRIANGLE_FAN))
353 return true;
354
355 if (mode <= GL_POLYGON)
356 return (ctx->API == API_OPENGL_COMPAT);
357
358 if (mode <= GL_TRIANGLE_STRIP_ADJACENCY)
359 return _mesa_has_geometry_shaders(ctx);
360
361 if (mode == GL_PATCHES)
362 return _mesa_has_tessellation(ctx);
363
364 return false;
365 }
366
367
368 /**
369 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
370 * etc? Also, do additional checking related to transformation feedback.
371 * Note: this function cannot be called during glNewList(GL_COMPILE) because
372 * this code depends on current transform feedback state.
373 * Also, do additional checking related to tessellation shaders.
374 */
375 GLboolean
376 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
377 {
378 bool valid_enum = _mesa_is_valid_prim_mode(ctx, mode);
379
380 if (!valid_enum) {
381 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
382 return GL_FALSE;
383 }
384
385 /* From the OpenGL 4.5 specification, section 11.3.1:
386 *
387 * The error INVALID_OPERATION is generated if Begin, or any command that
388 * implicitly calls Begin, is called when a geometry shader is active and:
389 *
390 * * the input primitive type of the current geometry shader is
391 * POINTS and <mode> is not POINTS,
392 *
393 * * the input primitive type of the current geometry shader is
394 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
395 *
396 * * the input primitive type of the current geometry shader is
397 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
398 * TRIANGLE_FAN,
399 *
400 * * the input primitive type of the current geometry shader is
401 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
402 * LINE_STRIP_ADJACENCY_ARB, or
403 *
404 * * the input primitive type of the current geometry shader is
405 * TRIANGLES_ADJACENCY_ARB and <mode> is not
406 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
407 *
408 * The GL spec doesn't mention any interaction with tessellation, which
409 * is clearly a spec bug. The same rule should apply, but instead of
410 * the draw primitive mode, the tessellation evaluation shader primitive
411 * mode should be used for the checking.
412 */
413 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
414 const GLenum geom_mode =
415 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
416 info.gs.input_primitive;
417 struct gl_program *tes =
418 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
419 GLenum mode_before_gs = mode;
420
421 if (tes) {
422 if (tes->info.tess.point_mode)
423 mode_before_gs = GL_POINTS;
424 else if (tes->info.tess.primitive_mode == GL_ISOLINES)
425 mode_before_gs = GL_LINES;
426 else
427 /* the GL_QUADS mode generates triangles too */
428 mode_before_gs = GL_TRIANGLES;
429 }
430
431 switch (mode_before_gs) {
432 case GL_POINTS:
433 valid_enum = (geom_mode == GL_POINTS);
434 break;
435 case GL_LINES:
436 case GL_LINE_LOOP:
437 case GL_LINE_STRIP:
438 valid_enum = (geom_mode == GL_LINES);
439 break;
440 case GL_TRIANGLES:
441 case GL_TRIANGLE_STRIP:
442 case GL_TRIANGLE_FAN:
443 valid_enum = (geom_mode == GL_TRIANGLES);
444 break;
445 case GL_QUADS:
446 case GL_QUAD_STRIP:
447 case GL_POLYGON:
448 valid_enum = false;
449 break;
450 case GL_LINES_ADJACENCY:
451 case GL_LINE_STRIP_ADJACENCY:
452 valid_enum = (geom_mode == GL_LINES_ADJACENCY);
453 break;
454 case GL_TRIANGLES_ADJACENCY:
455 case GL_TRIANGLE_STRIP_ADJACENCY:
456 valid_enum = (geom_mode == GL_TRIANGLES_ADJACENCY);
457 break;
458 default:
459 valid_enum = false;
460 break;
461 }
462 if (!valid_enum) {
463 _mesa_error(ctx, GL_INVALID_OPERATION,
464 "%s(mode=%s vs geometry shader input %s)",
465 name,
466 _mesa_lookup_prim_by_nr(mode_before_gs),
467 _mesa_lookup_prim_by_nr(geom_mode));
468 return GL_FALSE;
469 }
470 }
471
472 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
473 *
474 * "Tessellation operates only on patch primitives. If tessellation is
475 * active, any command that transfers vertices to the GL will
476 * generate an INVALID_OPERATION error if the primitive mode is not
477 * PATCHES.
478 * Patch primitives are not supported by pipeline stages below the
479 * tessellation evaluation shader. If there is no active program
480 * object or the active program object does not contain a tessellation
481 * evaluation shader, the error INVALID_OPERATION is generated by any
482 * command that transfers vertices to the GL if the primitive mode is
483 * PATCHES."
484 *
485 */
486 if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL] ||
487 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL]) {
488 if (mode != GL_PATCHES) {
489 _mesa_error(ctx, GL_INVALID_OPERATION,
490 "only GL_PATCHES valid with tessellation");
491 return GL_FALSE;
492 }
493 }
494 else {
495 if (mode == GL_PATCHES) {
496 _mesa_error(ctx, GL_INVALID_OPERATION,
497 "GL_PATCHES only valid with tessellation");
498 return GL_FALSE;
499 }
500 }
501
502 /* From the GL_EXT_transform_feedback spec:
503 *
504 * "The error INVALID_OPERATION is generated if Begin, or any command
505 * that performs an explicit Begin, is called when:
506 *
507 * * a geometry shader is not active and <mode> does not match the
508 * allowed begin modes for the current transform feedback state as
509 * given by table X.1.
510 *
511 * * a geometry shader is active and the output primitive type of the
512 * geometry shader does not match the allowed begin modes for the
513 * current transform feedback state as given by table X.1.
514 *
515 */
516 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
517 GLboolean pass = GL_TRUE;
518
519 if(ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
520 switch (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
521 info.gs.output_primitive) {
522 case GL_POINTS:
523 pass = ctx->TransformFeedback.Mode == GL_POINTS;
524 break;
525 case GL_LINE_STRIP:
526 pass = ctx->TransformFeedback.Mode == GL_LINES;
527 break;
528 case GL_TRIANGLE_STRIP:
529 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
530 break;
531 default:
532 pass = GL_FALSE;
533 }
534 }
535 else if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL]) {
536 struct gl_program *tes =
537 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
538 if (tes->info.tess.point_mode)
539 pass = ctx->TransformFeedback.Mode == GL_POINTS;
540 else if (tes->info.tess.primitive_mode == GL_ISOLINES)
541 pass = ctx->TransformFeedback.Mode == GL_LINES;
542 else
543 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
544 }
545 else {
546 switch (mode) {
547 case GL_POINTS:
548 pass = ctx->TransformFeedback.Mode == GL_POINTS;
549 break;
550 case GL_LINES:
551 case GL_LINE_STRIP:
552 case GL_LINE_LOOP:
553 pass = ctx->TransformFeedback.Mode == GL_LINES;
554 break;
555 default:
556 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
557 break;
558 }
559 }
560 if (!pass) {
561 _mesa_error(ctx, GL_INVALID_OPERATION,
562 "%s(mode=%s vs transform feedback %s)",
563 name,
564 _mesa_lookup_prim_by_nr(mode),
565 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
566 return GL_FALSE;
567 }
568 }
569
570 /* From GL_INTEL_conservative_rasterization spec:
571 *
572 * The conservative rasterization option applies only to polygons with
573 * PolygonMode state set to FILL. Draw requests for polygons with different
574 * PolygonMode setting or for other primitive types (points/lines) generate
575 * INVALID_OPERATION error.
576 */
577 if (ctx->IntelConservativeRasterization) {
578 GLboolean pass = GL_TRUE;
579
580 switch (mode) {
581 case GL_POINTS:
582 case GL_LINES:
583 case GL_LINE_LOOP:
584 case GL_LINE_STRIP:
585 case GL_LINES_ADJACENCY:
586 case GL_LINE_STRIP_ADJACENCY:
587 pass = GL_FALSE;
588 break;
589 case GL_TRIANGLES:
590 case GL_TRIANGLE_STRIP:
591 case GL_TRIANGLE_FAN:
592 case GL_QUADS:
593 case GL_QUAD_STRIP:
594 case GL_POLYGON:
595 case GL_TRIANGLES_ADJACENCY:
596 case GL_TRIANGLE_STRIP_ADJACENCY:
597 if (ctx->Polygon.FrontMode != GL_FILL ||
598 ctx->Polygon.BackMode != GL_FILL)
599 pass = GL_FALSE;
600 break;
601 default:
602 pass = GL_FALSE;
603 }
604 if (!pass) {
605 _mesa_error(ctx, GL_INVALID_OPERATION,
606 "mode=%s invalid with GL_INTEL_conservative_rasterization",
607 _mesa_lookup_prim_by_nr(mode));
608 return GL_FALSE;
609 }
610 }
611
612 return GL_TRUE;
613 }
614
615 /**
616 * Verify that the element type is valid.
617 *
618 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
619 */
620 static bool
621 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
622 {
623 switch (type) {
624 case GL_UNSIGNED_BYTE:
625 case GL_UNSIGNED_SHORT:
626 case GL_UNSIGNED_INT:
627 return true;
628
629 default:
630 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
631 _mesa_enum_to_string(type));
632 return false;
633 }
634 }
635
636 static bool
637 validate_DrawElements_common(struct gl_context *ctx,
638 GLenum mode, GLsizei count, GLenum type,
639 const GLvoid *indices,
640 const char *caller)
641 {
642 /* Section 2.14.2 (Transform Feedback Primitive Capture) of the OpenGL ES
643 * 3.1 spec says:
644 *
645 * The error INVALID_OPERATION is also generated by DrawElements,
646 * DrawElementsInstanced, and DrawRangeElements while transform feedback
647 * is active and not paused, regardless of mode.
648 *
649 * The OES_geometry_shader_spec says:
650 *
651 * Issues:
652 *
653 * ...
654 *
655 * (13) Does this extension change how transform feedback operates
656 * compared to unextended OpenGL ES 3.0 or 3.1?
657 *
658 * RESOLVED: Yes... Since we no longer require being able to predict how
659 * much geometry will be generated, we also lift the restriction that
660 * only DrawArray* commands are supported and also support the
661 * DrawElements* commands for transform feedback.
662 *
663 * This should also be reflected in the body of the spec, but that appears
664 * to have been overlooked. The body of the spec only explicitly allows
665 * the indirect versions.
666 */
667 if (_mesa_is_gles3(ctx) &&
668 !_mesa_has_OES_geometry_shader(ctx) &&
669 _mesa_is_xfb_active_and_unpaused(ctx)) {
670 _mesa_error(ctx, GL_INVALID_OPERATION,
671 "%s(transform feedback active)", caller);
672 return false;
673 }
674
675 if (count < 0) {
676 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", caller);
677 return false;
678 }
679
680 if (!_mesa_valid_prim_mode(ctx, mode, caller)) {
681 return false;
682 }
683
684 if (!valid_elements_type(ctx, type, caller))
685 return false;
686
687 if (!check_valid_to_render(ctx, caller))
688 return false;
689
690 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
691 */
692 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj) && indices == NULL)
693 return false;
694
695 if (count == 0)
696 return false;
697
698 return true;
699 }
700
701 /**
702 * Error checking for glDrawElements(). Includes parameter checking
703 * and VBO bounds checking.
704 * \return GL_TRUE if OK to render, GL_FALSE if error found
705 */
706 GLboolean
707 _mesa_validate_DrawElements(struct gl_context *ctx,
708 GLenum mode, GLsizei count, GLenum type,
709 const GLvoid *indices)
710 {
711 FLUSH_CURRENT(ctx, 0);
712
713 return validate_DrawElements_common(ctx, mode, count, type, indices,
714 "glDrawElements");
715 }
716
717
718 /**
719 * Error checking for glMultiDrawElements(). Includes parameter checking
720 * and VBO bounds checking.
721 * \return GL_TRUE if OK to render, GL_FALSE if error found
722 */
723 GLboolean
724 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
725 GLenum mode, const GLsizei *count,
726 GLenum type, const GLvoid * const *indices,
727 GLuint primcount)
728 {
729 unsigned i;
730
731 FLUSH_CURRENT(ctx, 0);
732
733 for (i = 0; i < primcount; i++) {
734 if (count[i] < 0) {
735 _mesa_error(ctx, GL_INVALID_VALUE,
736 "glMultiDrawElements(count)" );
737 return GL_FALSE;
738 }
739 }
740
741 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
742 return GL_FALSE;
743 }
744
745 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
746 return GL_FALSE;
747
748 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
749 return GL_FALSE;
750
751 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
752 */
753 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
754 for (i = 0; i < primcount; i++) {
755 if (!indices[i])
756 return GL_FALSE;
757 }
758 }
759
760 return GL_TRUE;
761 }
762
763
764 /**
765 * Error checking for glDrawRangeElements(). Includes parameter checking
766 * and VBO bounds checking.
767 * \return GL_TRUE if OK to render, GL_FALSE if error found
768 */
769 GLboolean
770 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
771 GLuint start, GLuint end,
772 GLsizei count, GLenum type,
773 const GLvoid *indices)
774 {
775 FLUSH_CURRENT(ctx, 0);
776
777 if (end < start) {
778 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
779 return GL_FALSE;
780 }
781
782 return validate_DrawElements_common(ctx, mode, count, type, indices,
783 "glDrawRangeElements");
784 }
785
786 static bool
787 validate_draw_arrays(struct gl_context *ctx, const char *func,
788 GLenum mode, GLsizei count, GLsizei numInstances)
789 {
790 struct gl_transform_feedback_object *xfb_obj
791 = ctx->TransformFeedback.CurrentObject;
792 FLUSH_CURRENT(ctx, 0);
793
794 if (count < 0) {
795 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", func);
796 return false;
797 }
798
799 if (!_mesa_valid_prim_mode(ctx, mode, func))
800 return false;
801
802 if (!check_valid_to_render(ctx, func))
803 return false;
804
805 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
806 * Primitive Capture):
807 *
808 * The error INVALID_OPERATION is generated by DrawArrays and
809 * DrawArraysInstanced if recording the vertices of a primitive to the
810 * buffer objects being used for transform feedback purposes would result
811 * in either exceeding the limits of any buffer object’s size, or in
812 * exceeding the end position offset + size − 1, as set by
813 * BindBufferRange.
814 *
815 * This is in contrast to the behaviour of desktop GL, where the extra
816 * primitives are silently dropped from the transform feedback buffer.
817 *
818 * This text is removed in ES 3.2, presumably because it's not really
819 * implementable with geometry and tessellation shaders. In fact,
820 * the OES_geometry_shader spec says:
821 *
822 * "(13) Does this extension change how transform feedback operates
823 * compared to unextended OpenGL ES 3.0 or 3.1?
824 *
825 * RESOLVED: Yes. Because dynamic geometry amplification in a geometry
826 * shader can make it difficult if not impossible to predict the amount
827 * of geometry that may be generated in advance of executing the shader,
828 * the draw-time error for transform feedback buffer overflow conditions
829 * is removed and replaced with the GL behavior (primitives are not
830 * written and the corresponding counter is not updated)..."
831 */
832 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx) &&
833 !_mesa_has_OES_geometry_shader(ctx) &&
834 !_mesa_has_OES_tessellation_shader(ctx)) {
835 size_t prim_count = vbo_count_tessellated_primitives(mode, count, 1);
836 if (xfb_obj->GlesRemainingPrims < prim_count) {
837 _mesa_error(ctx, GL_INVALID_OPERATION,
838 "%s(exceeds transform feedback size)", func);
839 return false;
840 }
841 xfb_obj->GlesRemainingPrims -= prim_count;
842 }
843
844 if (count == 0)
845 return false;
846
847 return true;
848 }
849
850 /**
851 * Called from the tnl module to error check the function parameters and
852 * verify that we really can draw something.
853 * \return GL_TRUE if OK to render, GL_FALSE if error found
854 */
855 GLboolean
856 _mesa_validate_DrawArrays(struct gl_context *ctx, GLenum mode, GLsizei count)
857 {
858 return validate_draw_arrays(ctx, "glDrawArrays", mode, count, 1);
859 }
860
861
862 GLboolean
863 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
864 GLsizei count, GLsizei numInstances)
865 {
866 if (first < 0) {
867 _mesa_error(ctx, GL_INVALID_VALUE,
868 "glDrawArraysInstanced(start=%d)", first);
869 return GL_FALSE;
870 }
871
872 if (numInstances <= 0) {
873 if (numInstances < 0)
874 _mesa_error(ctx, GL_INVALID_VALUE,
875 "glDrawArraysInstanced(numInstances=%d)", numInstances);
876 return GL_FALSE;
877 }
878
879 return validate_draw_arrays(ctx, "glDrawArraysInstanced", mode, count, 1);
880 }
881
882
883 GLboolean
884 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
885 GLenum mode, GLsizei count, GLenum type,
886 const GLvoid *indices, GLsizei numInstances)
887 {
888 FLUSH_CURRENT(ctx, 0);
889
890 if (numInstances < 0) {
891 _mesa_error(ctx, GL_INVALID_VALUE,
892 "glDrawElementsInstanced(numInstances=%d)", numInstances);
893 return GL_FALSE;
894 }
895
896 return validate_DrawElements_common(ctx, mode, count, type, indices,
897 "glDrawElementsInstanced")
898 && (numInstances > 0);
899 }
900
901
902 GLboolean
903 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
904 GLenum mode,
905 struct gl_transform_feedback_object *obj,
906 GLuint stream,
907 GLsizei numInstances)
908 {
909 FLUSH_CURRENT(ctx, 0);
910
911 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
912 return GL_FALSE;
913 }
914
915 if (!obj) {
916 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
917 return GL_FALSE;
918 }
919
920 /* From the GL 4.5 specification, page 429:
921 * "An INVALID_VALUE error is generated if id is not the name of a
922 * transform feedback object."
923 */
924 if (!obj->EverBound) {
925 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
926 return GL_FALSE;
927 }
928
929 if (stream >= ctx->Const.MaxVertexStreams) {
930 _mesa_error(ctx, GL_INVALID_VALUE,
931 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
932 return GL_FALSE;
933 }
934
935 if (!obj->EndedAnytime) {
936 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
937 return GL_FALSE;
938 }
939
940 if (numInstances <= 0) {
941 if (numInstances < 0)
942 _mesa_error(ctx, GL_INVALID_VALUE,
943 "glDrawTransformFeedback*Instanced(numInstances=%d)",
944 numInstances);
945 return GL_FALSE;
946 }
947
948 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {
949 return GL_FALSE;
950 }
951
952 return GL_TRUE;
953 }
954
955 static GLboolean
956 valid_draw_indirect(struct gl_context *ctx,
957 GLenum mode, const GLvoid *indirect,
958 GLsizei size, const char *name)
959 {
960 const uint64_t end = (uint64_t) (uintptr_t) indirect + size;
961
962 /* OpenGL ES 3.1 spec. section 10.5:
963 *
964 * "DrawArraysIndirect requires that all data sourced for the
965 * command, including the DrawArraysIndirectCommand
966 * structure, be in buffer objects, and may not be called when
967 * the default vertex array object is bound."
968 */
969 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
970 _mesa_error(ctx, GL_INVALID_OPERATION, "(no VAO bound)");
971 return GL_FALSE;
972 }
973
974 /* From OpenGL ES 3.1 spec. section 10.5:
975 * "An INVALID_OPERATION error is generated if zero is bound to
976 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
977 * vertex array."
978 *
979 * Here we check that for each enabled vertex array we have a vertex
980 * buffer bound.
981 */
982 if (_mesa_is_gles31(ctx) &&
983 ctx->Array.VAO->_Enabled & ~ctx->Array.VAO->VertexAttribBufferMask) {
984 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(No VBO bound)", name);
985 return GL_FALSE;
986 }
987
988 if (!_mesa_valid_prim_mode(ctx, mode, name))
989 return GL_FALSE;
990
991 /* OpenGL ES 3.1 specification, section 10.5:
992 *
993 * "An INVALID_OPERATION error is generated if
994 * transform feedback is active and not paused."
995 *
996 * The OES_geometry_shader spec says:
997 *
998 * On p. 250 in the errors section for the DrawArraysIndirect command,
999 * and on p. 254 in the errors section for the DrawElementsIndirect
1000 * command, delete the errors which state:
1001 *
1002 * "An INVALID_OPERATION error is generated if transform feedback is
1003 * active and not paused."
1004 *
1005 * (thus allowing transform feedback to work with indirect draw commands).
1006 */
1007 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader &&
1008 _mesa_is_xfb_active_and_unpaused(ctx)) {
1009 _mesa_error(ctx, GL_INVALID_OPERATION,
1010 "%s(TransformFeedback is active and not paused)", name);
1011 }
1012
1013 /* From OpenGL version 4.4. section 10.5
1014 * and OpenGL ES 3.1, section 10.6:
1015 *
1016 * "An INVALID_VALUE error is generated if indirect is not a
1017 * multiple of the size, in basic machine units, of uint."
1018 */
1019 if ((GLsizeiptr)indirect & (sizeof(GLuint) - 1)) {
1020 _mesa_error(ctx, GL_INVALID_VALUE,
1021 "%s(indirect is not aligned)", name);
1022 return GL_FALSE;
1023 }
1024
1025 if (!_mesa_is_bufferobj(ctx->DrawIndirectBuffer)) {
1026 _mesa_error(ctx, GL_INVALID_OPERATION,
1027 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name);
1028 return GL_FALSE;
1029 }
1030
1031 if (_mesa_check_disallowed_mapping(ctx->DrawIndirectBuffer)) {
1032 _mesa_error(ctx, GL_INVALID_OPERATION,
1033 "%s(DRAW_INDIRECT_BUFFER is mapped)", name);
1034 return GL_FALSE;
1035 }
1036
1037 /* From the ARB_draw_indirect specification:
1038 * "An INVALID_OPERATION error is generated if the commands source data
1039 * beyond the end of the buffer object [...]"
1040 */
1041 if (ctx->DrawIndirectBuffer->Size < end) {
1042 _mesa_error(ctx, GL_INVALID_OPERATION,
1043 "%s(DRAW_INDIRECT_BUFFER too small)", name);
1044 return GL_FALSE;
1045 }
1046
1047 if (!check_valid_to_render(ctx, name))
1048 return GL_FALSE;
1049
1050 return GL_TRUE;
1051 }
1052
1053 static inline GLboolean
1054 valid_draw_indirect_elements(struct gl_context *ctx,
1055 GLenum mode, GLenum type, const GLvoid *indirect,
1056 GLsizeiptr size, const char *name)
1057 {
1058 if (!valid_elements_type(ctx, type, name))
1059 return GL_FALSE;
1060
1061 /*
1062 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1063 * may not come from a client array and must come from an index buffer.
1064 * If no element array buffer is bound, an INVALID_OPERATION error is
1065 * generated.
1066 */
1067 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
1068 _mesa_error(ctx, GL_INVALID_OPERATION,
1069 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name);
1070 return GL_FALSE;
1071 }
1072
1073 return valid_draw_indirect(ctx, mode, indirect, size, name);
1074 }
1075
1076 static inline GLboolean
1077 valid_draw_indirect_multi(struct gl_context *ctx,
1078 GLsizei primcount, GLsizei stride,
1079 const char *name)
1080 {
1081
1082 /* From the ARB_multi_draw_indirect specification:
1083 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
1084 * MultiDrawElementsIndirect if <primcount> is negative."
1085 *
1086 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
1087 * be generated."
1088 */
1089 if (primcount < 0) {
1090 _mesa_error(ctx, GL_INVALID_VALUE, "%s(primcount < 0)", name);
1091 return GL_FALSE;
1092 }
1093
1094
1095 /* From the ARB_multi_draw_indirect specification:
1096 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
1097 * error is generated."
1098 */
1099 if (stride % 4) {
1100 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride %% 4)", name);
1101 return GL_FALSE;
1102 }
1103
1104 return GL_TRUE;
1105 }
1106
1107 GLboolean
1108 _mesa_validate_DrawArraysIndirect(struct gl_context *ctx,
1109 GLenum mode,
1110 const GLvoid *indirect)
1111 {
1112 const unsigned drawArraysNumParams = 4;
1113
1114 FLUSH_CURRENT(ctx, 0);
1115
1116 return valid_draw_indirect(ctx, mode,
1117 indirect, drawArraysNumParams * sizeof(GLuint),
1118 "glDrawArraysIndirect");
1119 }
1120
1121 GLboolean
1122 _mesa_validate_DrawElementsIndirect(struct gl_context *ctx,
1123 GLenum mode, GLenum type,
1124 const GLvoid *indirect)
1125 {
1126 const unsigned drawElementsNumParams = 5;
1127
1128 FLUSH_CURRENT(ctx, 0);
1129
1130 return valid_draw_indirect_elements(ctx, mode, type,
1131 indirect, drawElementsNumParams * sizeof(GLuint),
1132 "glDrawElementsIndirect");
1133 }
1134
1135 GLboolean
1136 _mesa_validate_MultiDrawArraysIndirect(struct gl_context *ctx,
1137 GLenum mode,
1138 const GLvoid *indirect,
1139 GLsizei primcount, GLsizei stride)
1140 {
1141 GLsizeiptr size = 0;
1142 const unsigned drawArraysNumParams = 4;
1143
1144 FLUSH_CURRENT(ctx, 0);
1145
1146 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1147 assert(stride != 0);
1148
1149 if (!valid_draw_indirect_multi(ctx, primcount, stride,
1150 "glMultiDrawArraysIndirect"))
1151 return GL_FALSE;
1152
1153 /* number of bytes of the indirect buffer which will be read */
1154 size = primcount
1155 ? (primcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
1156 : 0;
1157
1158 if (!valid_draw_indirect(ctx, mode, indirect, size,
1159 "glMultiDrawArraysIndirect"))
1160 return GL_FALSE;
1161
1162 return GL_TRUE;
1163 }
1164
1165 GLboolean
1166 _mesa_validate_MultiDrawElementsIndirect(struct gl_context *ctx,
1167 GLenum mode, GLenum type,
1168 const GLvoid *indirect,
1169 GLsizei primcount, GLsizei stride)
1170 {
1171 GLsizeiptr size = 0;
1172 const unsigned drawElementsNumParams = 5;
1173
1174 FLUSH_CURRENT(ctx, 0);
1175
1176 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1177 assert(stride != 0);
1178
1179 if (!valid_draw_indirect_multi(ctx, primcount, stride,
1180 "glMultiDrawElementsIndirect"))
1181 return GL_FALSE;
1182
1183 /* number of bytes of the indirect buffer which will be read */
1184 size = primcount
1185 ? (primcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1186 : 0;
1187
1188 if (!valid_draw_indirect_elements(ctx, mode, type,
1189 indirect, size,
1190 "glMultiDrawElementsIndirect"))
1191 return GL_FALSE;
1192
1193 return GL_TRUE;
1194 }
1195
1196 static GLboolean
1197 valid_draw_indirect_parameters(struct gl_context *ctx,
1198 const char *name,
1199 GLintptr drawcount)
1200 {
1201 /* From the ARB_indirect_parameters specification:
1202 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
1203 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
1204 * four."
1205 */
1206 if (drawcount & 3) {
1207 _mesa_error(ctx, GL_INVALID_VALUE,
1208 "%s(drawcount is not a multiple of 4)", name);
1209 return GL_FALSE;
1210 }
1211
1212 /* From the ARB_indirect_parameters specification:
1213 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1214 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
1215 * PARAMETER_BUFFER_ARB binding point."
1216 */
1217 if (!_mesa_is_bufferobj(ctx->ParameterBuffer)) {
1218 _mesa_error(ctx, GL_INVALID_OPERATION,
1219 "%s: no buffer bound to PARAMETER_BUFFER", name);
1220 return GL_FALSE;
1221 }
1222
1223 if (_mesa_check_disallowed_mapping(ctx->ParameterBuffer)) {
1224 _mesa_error(ctx, GL_INVALID_OPERATION,
1225 "%s(PARAMETER_BUFFER is mapped)", name);
1226 return GL_FALSE;
1227 }
1228
1229 /* From the ARB_indirect_parameters specification:
1230 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
1231 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
1232 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
1233 * specified by <drawcount> would result in an out-of-bounds access."
1234 */
1235 if (ctx->ParameterBuffer->Size < drawcount + sizeof(GLsizei)) {
1236 _mesa_error(ctx, GL_INVALID_OPERATION,
1237 "%s(PARAMETER_BUFFER too small)", name);
1238 return GL_FALSE;
1239 }
1240
1241 return GL_TRUE;
1242 }
1243
1244 GLboolean
1245 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context *ctx,
1246 GLenum mode,
1247 GLintptr indirect,
1248 GLintptr drawcount,
1249 GLsizei maxdrawcount,
1250 GLsizei stride)
1251 {
1252 GLsizeiptr size = 0;
1253 const unsigned drawArraysNumParams = 4;
1254
1255 FLUSH_CURRENT(ctx, 0);
1256
1257 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1258 assert(stride != 0);
1259
1260 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1261 "glMultiDrawArraysIndirectCountARB"))
1262 return GL_FALSE;
1263
1264 /* number of bytes of the indirect buffer which will be read */
1265 size = maxdrawcount
1266 ? (maxdrawcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
1267 : 0;
1268
1269 if (!valid_draw_indirect(ctx, mode, (void *)indirect, size,
1270 "glMultiDrawArraysIndirectCountARB"))
1271 return GL_FALSE;
1272
1273 return valid_draw_indirect_parameters(
1274 ctx, "glMultiDrawArraysIndirectCountARB", drawcount);
1275 }
1276
1277 GLboolean
1278 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context *ctx,
1279 GLenum mode, GLenum type,
1280 GLintptr indirect,
1281 GLintptr drawcount,
1282 GLsizei maxdrawcount,
1283 GLsizei stride)
1284 {
1285 GLsizeiptr size = 0;
1286 const unsigned drawElementsNumParams = 5;
1287
1288 FLUSH_CURRENT(ctx, 0);
1289
1290 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1291 assert(stride != 0);
1292
1293 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1294 "glMultiDrawElementsIndirectCountARB"))
1295 return GL_FALSE;
1296
1297 /* number of bytes of the indirect buffer which will be read */
1298 size = maxdrawcount
1299 ? (maxdrawcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1300 : 0;
1301
1302 if (!valid_draw_indirect_elements(ctx, mode, type,
1303 (void *)indirect, size,
1304 "glMultiDrawElementsIndirectCountARB"))
1305 return GL_FALSE;
1306
1307 return valid_draw_indirect_parameters(
1308 ctx, "glMultiDrawElementsIndirectCountARB", drawcount);
1309 }
1310
1311 static bool
1312 check_valid_to_compute(struct gl_context *ctx, const char *function)
1313 {
1314 if (!_mesa_has_compute_shaders(ctx)) {
1315 _mesa_error(ctx, GL_INVALID_OPERATION,
1316 "unsupported function (%s) called",
1317 function);
1318 return false;
1319 }
1320
1321 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1322 *
1323 * "An INVALID_OPERATION error is generated if there is no active program
1324 * for the compute shader stage."
1325 */
1326 if (ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE] == NULL) {
1327 _mesa_error(ctx, GL_INVALID_OPERATION,
1328 "%s(no active compute shader)",
1329 function);
1330 return false;
1331 }
1332
1333 return true;
1334 }
1335
1336 GLboolean
1337 _mesa_validate_DispatchCompute(struct gl_context *ctx,
1338 const GLuint *num_groups)
1339 {
1340 int i;
1341 FLUSH_CURRENT(ctx, 0);
1342
1343 if (!check_valid_to_compute(ctx, "glDispatchCompute"))
1344 return GL_FALSE;
1345
1346 for (i = 0; i < 3; i++) {
1347 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1348 *
1349 * "An INVALID_VALUE error is generated if any of num_groups_x,
1350 * num_groups_y and num_groups_z are greater than or equal to the
1351 * maximum work group count for the corresponding dimension."
1352 *
1353 * However, the "or equal to" portions appears to be a specification
1354 * bug. In all other areas, the specification appears to indicate that
1355 * the number of workgroups can match the MAX_COMPUTE_WORK_GROUP_COUNT
1356 * value. For example, under DispatchComputeIndirect:
1357 *
1358 * "If any of num_groups_x, num_groups_y or num_groups_z is greater than
1359 * the value of MAX_COMPUTE_WORK_GROUP_COUNT for the corresponding
1360 * dimension then the results are undefined."
1361 *
1362 * Additionally, the OpenGLES 3.1 specification does not contain "or
1363 * equal to" as an error condition.
1364 */
1365 if (num_groups[i] > ctx->Const.MaxComputeWorkGroupCount[i]) {
1366 _mesa_error(ctx, GL_INVALID_VALUE,
1367 "glDispatchCompute(num_groups_%c)", 'x' + i);
1368 return GL_FALSE;
1369 }
1370 }
1371
1372 /* The ARB_compute_variable_group_size spec says:
1373 *
1374 * "An INVALID_OPERATION error is generated by DispatchCompute if the active
1375 * program for the compute shader stage has a variable work group size."
1376 */
1377 struct gl_program *prog = ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
1378 if (prog->info.cs.local_size_variable) {
1379 _mesa_error(ctx, GL_INVALID_OPERATION,
1380 "glDispatchCompute(variable work group size forbidden)");
1381 return GL_FALSE;
1382 }
1383
1384 return GL_TRUE;
1385 }
1386
1387 GLboolean
1388 _mesa_validate_DispatchComputeGroupSizeARB(struct gl_context *ctx,
1389 const GLuint *num_groups,
1390 const GLuint *group_size)
1391 {
1392 GLuint total_invocations = 1;
1393 int i;
1394
1395 FLUSH_CURRENT(ctx, 0);
1396
1397 if (!check_valid_to_compute(ctx, "glDispatchComputeGroupSizeARB"))
1398 return GL_FALSE;
1399
1400 /* The ARB_compute_variable_group_size spec says:
1401 *
1402 * "An INVALID_OPERATION error is generated by
1403 * DispatchComputeGroupSizeARB if the active program for the compute
1404 * shader stage has a fixed work group size."
1405 */
1406 struct gl_program *prog = ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
1407 if (!prog->info.cs.local_size_variable) {
1408 _mesa_error(ctx, GL_INVALID_OPERATION,
1409 "glDispatchComputeGroupSizeARB(fixed work group size "
1410 "forbidden)");
1411 return GL_FALSE;
1412 }
1413
1414 for (i = 0; i < 3; i++) {
1415 /* The ARB_compute_variable_group_size spec says:
1416 *
1417 * "An INVALID_VALUE error is generated if any of num_groups_x,
1418 * num_groups_y and num_groups_z are greater than or equal to the
1419 * maximum work group count for the corresponding dimension."
1420 */
1421 if (num_groups[i] > ctx->Const.MaxComputeWorkGroupCount[i]) {
1422 _mesa_error(ctx, GL_INVALID_VALUE,
1423 "glDispatchComputeGroupSizeARB(num_groups_%c)", 'x' + i);
1424 return GL_FALSE;
1425 }
1426
1427 /* The ARB_compute_variable_group_size spec says:
1428 *
1429 * "An INVALID_VALUE error is generated by DispatchComputeGroupSizeARB if
1430 * any of <group_size_x>, <group_size_y>, or <group_size_z> is less than
1431 * or equal to zero or greater than the maximum local work group size
1432 * for compute shaders with variable group size
1433 * (MAX_COMPUTE_VARIABLE_GROUP_SIZE_ARB) in the corresponding
1434 * dimension."
1435 *
1436 * However, the "less than" is a spec bug because they are declared as
1437 * unsigned integers.
1438 */
1439 if (group_size[i] == 0 ||
1440 group_size[i] > ctx->Const.MaxComputeVariableGroupSize[i]) {
1441 _mesa_error(ctx, GL_INVALID_VALUE,
1442 "glDispatchComputeGroupSizeARB(group_size_%c)", 'x' + i);
1443 return GL_FALSE;
1444 }
1445
1446 total_invocations *= group_size[i];
1447 }
1448
1449 /* The ARB_compute_variable_group_size spec says:
1450 *
1451 * "An INVALID_VALUE error is generated by DispatchComputeGroupSizeARB if
1452 * the product of <group_size_x>, <group_size_y>, and <group_size_z> exceeds
1453 * the implementation-dependent maximum local work group invocation count
1454 * for compute shaders with variable group size
1455 * (MAX_COMPUTE_VARIABLE_GROUP_INVOCATIONS_ARB)."
1456 */
1457 if (total_invocations > ctx->Const.MaxComputeVariableGroupInvocations) {
1458 _mesa_error(ctx, GL_INVALID_VALUE,
1459 "glDispatchComputeGroupSizeARB(product of local_sizes "
1460 "exceeds MAX_COMPUTE_VARIABLE_GROUP_INVOCATIONS_ARB "
1461 "(%d > %d))", total_invocations,
1462 ctx->Const.MaxComputeVariableGroupInvocations);
1463 return GL_FALSE;
1464 }
1465
1466 return GL_TRUE;
1467 }
1468
1469 static GLboolean
1470 valid_dispatch_indirect(struct gl_context *ctx,
1471 GLintptr indirect,
1472 GLsizei size, const char *name)
1473 {
1474 const uint64_t end = (uint64_t) indirect + size;
1475
1476 if (!check_valid_to_compute(ctx, name))
1477 return GL_FALSE;
1478
1479 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1480 *
1481 * "An INVALID_VALUE error is generated if indirect is negative or is not a
1482 * multiple of four."
1483 */
1484 if (indirect & (sizeof(GLuint) - 1)) {
1485 _mesa_error(ctx, GL_INVALID_VALUE,
1486 "%s(indirect is not aligned)", name);
1487 return GL_FALSE;
1488 }
1489
1490 if (indirect < 0) {
1491 _mesa_error(ctx, GL_INVALID_VALUE,
1492 "%s(indirect is less than zero)", name);
1493 return GL_FALSE;
1494 }
1495
1496 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1497 *
1498 * "An INVALID_OPERATION error is generated if no buffer is bound to the
1499 * DRAW_INDIRECT_BUFFER binding, or if the command would source data
1500 * beyond the end of the buffer object."
1501 */
1502 if (!_mesa_is_bufferobj(ctx->DispatchIndirectBuffer)) {
1503 _mesa_error(ctx, GL_INVALID_OPERATION,
1504 "%s: no buffer bound to DISPATCH_INDIRECT_BUFFER", name);
1505 return GL_FALSE;
1506 }
1507
1508 if (_mesa_check_disallowed_mapping(ctx->DispatchIndirectBuffer)) {
1509 _mesa_error(ctx, GL_INVALID_OPERATION,
1510 "%s(DISPATCH_INDIRECT_BUFFER is mapped)", name);
1511 return GL_FALSE;
1512 }
1513
1514 if (ctx->DispatchIndirectBuffer->Size < end) {
1515 _mesa_error(ctx, GL_INVALID_OPERATION,
1516 "%s(DISPATCH_INDIRECT_BUFFER too small)", name);
1517 return GL_FALSE;
1518 }
1519
1520 /* The ARB_compute_variable_group_size spec says:
1521 *
1522 * "An INVALID_OPERATION error is generated if the active program for the
1523 * compute shader stage has a variable work group size."
1524 */
1525 struct gl_program *prog = ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
1526 if (prog->info.cs.local_size_variable) {
1527 _mesa_error(ctx, GL_INVALID_OPERATION,
1528 "%s(variable work group size forbidden)", name);
1529 return GL_FALSE;
1530 }
1531
1532 return GL_TRUE;
1533 }
1534
1535 GLboolean
1536 _mesa_validate_DispatchComputeIndirect(struct gl_context *ctx,
1537 GLintptr indirect)
1538 {
1539 FLUSH_CURRENT(ctx, 0);
1540
1541 return valid_dispatch_indirect(ctx, indirect, 3 * sizeof(GLuint),
1542 "glDispatchComputeIndirect");
1543 }