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