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