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