glsl/mesa: move duplicate shader fields into new struct gl_shader_info
[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 "bufferobj.h"
29 #include "context.h"
30 #include "imports.h"
31 #include "mtypes.h"
32 #include "enums.h"
33 #include "vbo/vbo.h"
34 #include "transformfeedback.h"
35 #include <stdbool.h>
36
37
38 /**
39 * Check if OK to draw arrays/elements.
40 */
41 static bool
42 check_valid_to_render(struct gl_context *ctx, const char *function)
43 {
44 if (!_mesa_valid_to_render(ctx, function)) {
45 return false;
46 }
47
48 switch (ctx->API) {
49 case API_OPENGLES2:
50 /* For ES2, we can draw if we have a vertex program/shader). */
51 return ctx->VertexProgram._Current != NULL;
52
53 case API_OPENGLES:
54 /* For OpenGL ES, only draw if we have vertex positions
55 */
56 if (!ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled)
57 return false;
58 break;
59
60 case API_OPENGL_CORE:
61 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
62 * Core Profile spec says:
63 *
64 * "An INVALID_OPERATION error is generated if no vertex array
65 * object is bound (see section 10.3.1)."
66 */
67 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
68 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no VAO bound)", function);
69 return false;
70 }
71
72 /* The spec argues that this is allowed because a tess ctrl shader
73 * without a tess eval shader can be used with transform feedback.
74 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
75 * therefore doesn't allow tessellation.
76 *
77 * Further investigation showed that this is indeed a spec bug and
78 * a tess ctrl shader without a tess eval shader shouldn't have been
79 * allowed, because there is no API in GL 4.0 that can make use this
80 * to produce something useful.
81 *
82 * Also, all vendors except one don't support a tess ctrl shader without
83 * a tess eval shader anyway.
84 */
85 if (ctx->TessCtrlProgram._Current && !ctx->TessEvalProgram._Current) {
86 _mesa_error(ctx, GL_INVALID_OPERATION,
87 "%s(tess eval shader is missing)", function);
88 return false;
89 }
90
91 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
92 * says:
93 *
94 * "If there is no active program for the vertex or fragment shader
95 * stages, the results of vertex and/or fragment processing will be
96 * undefined. However, this is not an error."
97 *
98 * The fragment shader is not tested here because other state (e.g.,
99 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
100 */
101 return ctx->VertexProgram._Current != NULL;
102
103 case API_OPENGL_COMPAT:
104 if (ctx->VertexProgram._Current != NULL) {
105 /* Draw regardless of whether or not we have any vertex arrays.
106 * (Ex: could draw a point using a constant vertex pos)
107 */
108 return true;
109 } else {
110 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
111 * array [0]).
112 */
113 return (ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
114 ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
115 }
116 break;
117
118 default:
119 unreachable("Invalid API value in check_valid_to_render()");
120 }
121
122 return true;
123 }
124
125
126 /**
127 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
128 * etc? The set of legal values depends on whether geometry shaders/programs
129 * are supported.
130 * Note: This may be called during display list compilation.
131 */
132 bool
133 _mesa_is_valid_prim_mode(struct gl_context *ctx, GLenum mode)
134 {
135 /* The overwhelmingly common case is (mode <= GL_TRIANGLE_FAN). Test that
136 * first and exit. You would think that a switch-statement would be the
137 * right approach, but at least GCC 4.7.2 generates some pretty dire code
138 * for the common case.
139 */
140 if (likely(mode <= GL_TRIANGLE_FAN))
141 return true;
142
143 if (mode <= GL_POLYGON)
144 return (ctx->API == API_OPENGL_COMPAT);
145
146 if (mode <= GL_TRIANGLE_STRIP_ADJACENCY)
147 return _mesa_has_geometry_shaders(ctx);
148
149 if (mode == GL_PATCHES)
150 return _mesa_has_tessellation(ctx);
151
152 return false;
153 }
154
155
156 /**
157 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
158 * etc? Also, do additional checking related to transformation feedback.
159 * Note: this function cannot be called during glNewList(GL_COMPILE) because
160 * this code depends on current transform feedback state.
161 * Also, do additional checking related to tessellation shaders.
162 */
163 GLboolean
164 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
165 {
166 bool valid_enum = _mesa_is_valid_prim_mode(ctx, mode);
167
168 if (!valid_enum) {
169 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
170 return GL_FALSE;
171 }
172
173 /* From the OpenGL 4.5 specification, section 11.3.1:
174 *
175 * The error INVALID_OPERATION is generated if Begin, or any command that
176 * implicitly calls Begin, is called when a geometry shader is active and:
177 *
178 * * the input primitive type of the current geometry shader is
179 * POINTS and <mode> is not POINTS,
180 *
181 * * the input primitive type of the current geometry shader is
182 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
183 *
184 * * the input primitive type of the current geometry shader is
185 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
186 * TRIANGLE_FAN,
187 *
188 * * the input primitive type of the current geometry shader is
189 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
190 * LINE_STRIP_ADJACENCY_ARB, or
191 *
192 * * the input primitive type of the current geometry shader is
193 * TRIANGLES_ADJACENCY_ARB and <mode> is not
194 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
195 *
196 * The GL spec doesn't mention any interaction with tessellation, which
197 * is clearly a spec bug. The same rule should apply, but instead of
198 * the draw primitive mode, the tessellation evaluation shader primitive
199 * mode should be used for the checking.
200 */
201 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
202 const GLenum geom_mode =
203 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
204 _LinkedShaders[MESA_SHADER_GEOMETRY]->info.Geom.InputType;
205 struct gl_shader_program *tes =
206 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
207 GLenum mode_before_gs = mode;
208
209 if (tes) {
210 struct gl_linked_shader *tes_sh =
211 tes->_LinkedShaders[MESA_SHADER_TESS_EVAL];
212 if (tes_sh->info.TessEval.PointMode)
213 mode_before_gs = GL_POINTS;
214 else if (tes_sh->info.TessEval.PrimitiveMode == GL_ISOLINES)
215 mode_before_gs = GL_LINES;
216 else
217 /* the GL_QUADS mode generates triangles too */
218 mode_before_gs = GL_TRIANGLES;
219 }
220
221 switch (mode_before_gs) {
222 case GL_POINTS:
223 valid_enum = (geom_mode == GL_POINTS);
224 break;
225 case GL_LINES:
226 case GL_LINE_LOOP:
227 case GL_LINE_STRIP:
228 valid_enum = (geom_mode == GL_LINES);
229 break;
230 case GL_TRIANGLES:
231 case GL_TRIANGLE_STRIP:
232 case GL_TRIANGLE_FAN:
233 valid_enum = (geom_mode == GL_TRIANGLES);
234 break;
235 case GL_QUADS:
236 case GL_QUAD_STRIP:
237 case GL_POLYGON:
238 valid_enum = false;
239 break;
240 case GL_LINES_ADJACENCY:
241 case GL_LINE_STRIP_ADJACENCY:
242 valid_enum = (geom_mode == GL_LINES_ADJACENCY);
243 break;
244 case GL_TRIANGLES_ADJACENCY:
245 case GL_TRIANGLE_STRIP_ADJACENCY:
246 valid_enum = (geom_mode == GL_TRIANGLES_ADJACENCY);
247 break;
248 default:
249 valid_enum = false;
250 break;
251 }
252 if (!valid_enum) {
253 _mesa_error(ctx, GL_INVALID_OPERATION,
254 "%s(mode=%s vs geometry shader input %s)",
255 name,
256 _mesa_lookup_prim_by_nr(mode_before_gs),
257 _mesa_lookup_prim_by_nr(geom_mode));
258 return GL_FALSE;
259 }
260 }
261
262 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
263 *
264 * "Tessellation operates only on patch primitives. If tessellation is
265 * active, any command that transfers vertices to the GL will
266 * generate an INVALID_OPERATION error if the primitive mode is not
267 * PATCHES.
268 * Patch primitives are not supported by pipeline stages below the
269 * tessellation evaluation shader. If there is no active program
270 * object or the active program object does not contain a tessellation
271 * evaluation shader, the error INVALID_OPERATION is generated by any
272 * command that transfers vertices to the GL if the primitive mode is
273 * PATCHES."
274 *
275 */
276 if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL] ||
277 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL]) {
278 if (mode != GL_PATCHES) {
279 _mesa_error(ctx, GL_INVALID_OPERATION,
280 "only GL_PATCHES valid with tessellation");
281 return GL_FALSE;
282 }
283 }
284 else {
285 if (mode == GL_PATCHES) {
286 _mesa_error(ctx, GL_INVALID_OPERATION,
287 "GL_PATCHES only valid with tessellation");
288 return GL_FALSE;
289 }
290 }
291
292 /* From the GL_EXT_transform_feedback spec:
293 *
294 * "The error INVALID_OPERATION is generated if Begin, or any command
295 * that performs an explicit Begin, is called when:
296 *
297 * * a geometry shader is not active and <mode> does not match the
298 * allowed begin modes for the current transform feedback state as
299 * given by table X.1.
300 *
301 * * a geometry shader is active and the output primitive type of the
302 * geometry shader does not match the allowed begin modes for the
303 * current transform feedback state as given by table X.1.
304 *
305 */
306 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
307 GLboolean pass = GL_TRUE;
308
309 if(ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
310 switch (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
311 _LinkedShaders[MESA_SHADER_GEOMETRY]->
312 info.Geom.OutputType) {
313 case GL_POINTS:
314 pass = ctx->TransformFeedback.Mode == GL_POINTS;
315 break;
316 case GL_LINE_STRIP:
317 pass = ctx->TransformFeedback.Mode == GL_LINES;
318 break;
319 case GL_TRIANGLE_STRIP:
320 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
321 break;
322 default:
323 pass = GL_FALSE;
324 }
325 }
326 else if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL]) {
327 struct gl_shader_program *tes =
328 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
329 struct gl_linked_shader *tes_sh =
330 tes->_LinkedShaders[MESA_SHADER_TESS_EVAL];
331 if (tes_sh->info.TessEval.PointMode)
332 pass = ctx->TransformFeedback.Mode == GL_POINTS;
333 else if (tes_sh->info.TessEval.PrimitiveMode == GL_ISOLINES)
334 pass = ctx->TransformFeedback.Mode == GL_LINES;
335 else
336 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
337 }
338 else {
339 switch (mode) {
340 case GL_POINTS:
341 pass = ctx->TransformFeedback.Mode == GL_POINTS;
342 break;
343 case GL_LINES:
344 case GL_LINE_STRIP:
345 case GL_LINE_LOOP:
346 pass = ctx->TransformFeedback.Mode == GL_LINES;
347 break;
348 default:
349 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
350 break;
351 }
352 }
353 if (!pass) {
354 _mesa_error(ctx, GL_INVALID_OPERATION,
355 "%s(mode=%s vs transform feedback %s)",
356 name,
357 _mesa_lookup_prim_by_nr(mode),
358 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
359 return GL_FALSE;
360 }
361 }
362
363 return GL_TRUE;
364 }
365
366 /**
367 * Verify that the element type is valid.
368 *
369 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
370 */
371 static bool
372 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
373 {
374 switch (type) {
375 case GL_UNSIGNED_BYTE:
376 case GL_UNSIGNED_SHORT:
377 case GL_UNSIGNED_INT:
378 return true;
379
380 default:
381 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
382 _mesa_enum_to_string(type));
383 return false;
384 }
385 }
386
387 static bool
388 validate_DrawElements_common(struct gl_context *ctx,
389 GLenum mode, GLsizei count, GLenum type,
390 const GLvoid *indices,
391 const char *caller)
392 {
393 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
394 * Primitive Capture):
395 *
396 * The error INVALID_OPERATION is also generated by DrawElements,
397 * DrawElementsInstanced, and DrawRangeElements while transform feedback
398 * is active and not paused, regardless of mode.
399 */
400 if (_mesa_is_gles3(ctx) && !ctx->Extensions.OES_geometry_shader &&
401 _mesa_is_xfb_active_and_unpaused(ctx)) {
402 _mesa_error(ctx, GL_INVALID_OPERATION,
403 "%s(transform feedback active)", caller);
404 return false;
405 }
406
407 if (count < 0) {
408 _mesa_error(ctx, GL_INVALID_VALUE, "%s(count)", caller);
409 return false;
410 }
411
412 if (!_mesa_valid_prim_mode(ctx, mode, caller)) {
413 return false;
414 }
415
416 if (!valid_elements_type(ctx, type, caller))
417 return false;
418
419 if (!check_valid_to_render(ctx, caller))
420 return false;
421
422 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
423 */
424 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj) && indices == NULL)
425 return false;
426
427 if (count == 0)
428 return false;
429
430 return true;
431 }
432
433 /**
434 * Error checking for glDrawElements(). Includes parameter checking
435 * and VBO bounds checking.
436 * \return GL_TRUE if OK to render, GL_FALSE if error found
437 */
438 GLboolean
439 _mesa_validate_DrawElements(struct gl_context *ctx,
440 GLenum mode, GLsizei count, GLenum type,
441 const GLvoid *indices)
442 {
443 FLUSH_CURRENT(ctx, 0);
444
445 return validate_DrawElements_common(ctx, mode, count, type, indices,
446 "glDrawElements");
447 }
448
449
450 /**
451 * Error checking for glMultiDrawElements(). Includes parameter checking
452 * and VBO bounds checking.
453 * \return GL_TRUE if OK to render, GL_FALSE if error found
454 */
455 GLboolean
456 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
457 GLenum mode, const GLsizei *count,
458 GLenum type, const GLvoid * const *indices,
459 GLuint primcount)
460 {
461 unsigned i;
462
463 FLUSH_CURRENT(ctx, 0);
464
465 for (i = 0; i < primcount; i++) {
466 if (count[i] < 0) {
467 _mesa_error(ctx, GL_INVALID_VALUE,
468 "glMultiDrawElements(count)" );
469 return GL_FALSE;
470 }
471 }
472
473 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
474 return GL_FALSE;
475 }
476
477 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
478 return GL_FALSE;
479
480 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
481 return GL_FALSE;
482
483 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
484 */
485 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
486 for (i = 0; i < primcount; i++) {
487 if (!indices[i])
488 return GL_FALSE;
489 }
490 }
491
492 return GL_TRUE;
493 }
494
495
496 /**
497 * Error checking for glDrawRangeElements(). Includes parameter checking
498 * and VBO bounds checking.
499 * \return GL_TRUE if OK to render, GL_FALSE if error found
500 */
501 GLboolean
502 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
503 GLuint start, GLuint end,
504 GLsizei count, GLenum type,
505 const GLvoid *indices)
506 {
507 FLUSH_CURRENT(ctx, 0);
508
509 if (end < start) {
510 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
511 return GL_FALSE;
512 }
513
514 return validate_DrawElements_common(ctx, mode, count, type, indices,
515 "glDrawRangeElements");
516 }
517
518
519 /**
520 * Called from the tnl module to error check the function parameters and
521 * verify that we really can draw something.
522 * \return GL_TRUE if OK to render, GL_FALSE if error found
523 */
524 GLboolean
525 _mesa_validate_DrawArrays(struct gl_context *ctx, GLenum mode, GLsizei count)
526 {
527 struct gl_transform_feedback_object *xfb_obj
528 = ctx->TransformFeedback.CurrentObject;
529 FLUSH_CURRENT(ctx, 0);
530
531 if (count < 0) {
532 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
533 return GL_FALSE;
534 }
535
536 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArrays")) {
537 return GL_FALSE;
538 }
539
540 if (!check_valid_to_render(ctx, "glDrawArrays"))
541 return GL_FALSE;
542
543 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
544 * Primitive Capture):
545 *
546 * The error INVALID_OPERATION is generated by DrawArrays and
547 * DrawArraysInstanced if recording the vertices of a primitive to the
548 * buffer objects being used for transform feedback purposes would result
549 * in either exceeding the limits of any buffer object’s size, or in
550 * exceeding the end position offset + size − 1, as set by
551 * BindBufferRange.
552 *
553 * This is in contrast to the behaviour of desktop GL, where the extra
554 * primitives are silently dropped from the transform feedback buffer.
555 */
556 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
557 size_t prim_count = vbo_count_tessellated_primitives(mode, count, 1);
558 if (xfb_obj->GlesRemainingPrims < prim_count) {
559 _mesa_error(ctx, GL_INVALID_OPERATION,
560 "glDrawArrays(exceeds transform feedback size)");
561 return GL_FALSE;
562 }
563 xfb_obj->GlesRemainingPrims -= prim_count;
564 }
565
566 if (count == 0)
567 return GL_FALSE;
568
569 return GL_TRUE;
570 }
571
572
573 GLboolean
574 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
575 GLsizei count, GLsizei numInstances)
576 {
577 struct gl_transform_feedback_object *xfb_obj
578 = ctx->TransformFeedback.CurrentObject;
579 FLUSH_CURRENT(ctx, 0);
580
581 if (count < 0) {
582 _mesa_error(ctx, GL_INVALID_VALUE,
583 "glDrawArraysInstanced(count=%d)", count);
584 return GL_FALSE;
585 }
586
587 if (first < 0) {
588 _mesa_error(ctx, GL_INVALID_VALUE,
589 "glDrawArraysInstanced(start=%d)", first);
590 return GL_FALSE;
591 }
592
593 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArraysInstanced")) {
594 return GL_FALSE;
595 }
596
597 if (numInstances <= 0) {
598 if (numInstances < 0)
599 _mesa_error(ctx, GL_INVALID_VALUE,
600 "glDrawArraysInstanced(numInstances=%d)", numInstances);
601 return GL_FALSE;
602 }
603
604 if (!check_valid_to_render(ctx, "glDrawArraysInstanced(invalid to render)"))
605 return GL_FALSE;
606
607 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
608 * Primitive Capture):
609 *
610 * The error INVALID_OPERATION is generated by DrawArrays and
611 * DrawArraysInstanced if recording the vertices of a primitive to the
612 * buffer objects being used for transform feedback purposes would result
613 * in either exceeding the limits of any buffer object’s size, or in
614 * exceeding the end position offset + size − 1, as set by
615 * BindBufferRange.
616 *
617 * This is in contrast to the behaviour of desktop GL, where the extra
618 * primitives are silently dropped from the transform feedback buffer.
619 */
620 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
621 size_t prim_count
622 = vbo_count_tessellated_primitives(mode, count, numInstances);
623 if (xfb_obj->GlesRemainingPrims < prim_count) {
624 _mesa_error(ctx, GL_INVALID_OPERATION,
625 "glDrawArraysInstanced(exceeds transform feedback size)");
626 return GL_FALSE;
627 }
628 xfb_obj->GlesRemainingPrims -= prim_count;
629 }
630
631 if (count == 0)
632 return GL_FALSE;
633
634 return GL_TRUE;
635 }
636
637
638 GLboolean
639 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
640 GLenum mode, GLsizei count, GLenum type,
641 const GLvoid *indices, GLsizei numInstances)
642 {
643 FLUSH_CURRENT(ctx, 0);
644
645 if (numInstances < 0) {
646 _mesa_error(ctx, GL_INVALID_VALUE,
647 "glDrawElementsInstanced(numInstances=%d)", numInstances);
648 return GL_FALSE;
649 }
650
651 return validate_DrawElements_common(ctx, mode, count, type, indices,
652 "glDrawElementsInstanced")
653 && (numInstances > 0);
654 }
655
656
657 GLboolean
658 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
659 GLenum mode,
660 struct gl_transform_feedback_object *obj,
661 GLuint stream,
662 GLsizei numInstances)
663 {
664 FLUSH_CURRENT(ctx, 0);
665
666 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
667 return GL_FALSE;
668 }
669
670 if (!obj) {
671 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
672 return GL_FALSE;
673 }
674
675 /* From the GL 4.5 specification, page 429:
676 * "An INVALID_VALUE error is generated if id is not the name of a
677 * transform feedback object."
678 */
679 if (!obj->EverBound) {
680 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
681 return GL_FALSE;
682 }
683
684 if (stream >= ctx->Const.MaxVertexStreams) {
685 _mesa_error(ctx, GL_INVALID_VALUE,
686 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
687 return GL_FALSE;
688 }
689
690 if (!obj->EndedAnytime) {
691 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
692 return GL_FALSE;
693 }
694
695 if (numInstances <= 0) {
696 if (numInstances < 0)
697 _mesa_error(ctx, GL_INVALID_VALUE,
698 "glDrawTransformFeedback*Instanced(numInstances=%d)",
699 numInstances);
700 return GL_FALSE;
701 }
702
703 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {
704 return GL_FALSE;
705 }
706
707 return GL_TRUE;
708 }
709
710 static GLboolean
711 valid_draw_indirect(struct gl_context *ctx,
712 GLenum mode, const GLvoid *indirect,
713 GLsizei size, const char *name)
714 {
715 const uint64_t end = (uint64_t) (uintptr_t) indirect + size;
716
717 /* OpenGL ES 3.1 spec. section 10.5:
718 *
719 * "DrawArraysIndirect requires that all data sourced for the
720 * command, including the DrawArraysIndirectCommand
721 * structure, be in buffer objects, and may not be called when
722 * the default vertex array object is bound."
723 */
724 if (ctx->Array.VAO == ctx->Array.DefaultVAO) {
725 _mesa_error(ctx, GL_INVALID_OPERATION, "(no VAO bound)");
726 return GL_FALSE;
727 }
728
729 /* From OpenGL ES 3.1 spec. section 10.5:
730 * "An INVALID_OPERATION error is generated if zero is bound to
731 * VERTEX_ARRAY_BINDING, DRAW_INDIRECT_BUFFER or to any enabled
732 * vertex array."
733 *
734 * Here we check that for each enabled vertex array we have a vertex
735 * buffer bound.
736 */
737 if (_mesa_is_gles31(ctx) &&
738 ctx->Array.VAO->_Enabled != ctx->Array.VAO->VertexAttribBufferMask) {
739 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(No VBO bound)", name);
740 return GL_FALSE;
741 }
742
743 if (!_mesa_valid_prim_mode(ctx, mode, name))
744 return GL_FALSE;
745
746 /* OpenGL ES 3.1 specification, section 10.5:
747 *
748 * "An INVALID_OPERATION error is generated if
749 * transform feedback is active and not paused."
750 */
751 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader &&
752 _mesa_is_xfb_active_and_unpaused(ctx)) {
753 _mesa_error(ctx, GL_INVALID_OPERATION,
754 "%s(TransformFeedback is active and not paused)", name);
755 }
756
757 /* From OpenGL version 4.4. section 10.5
758 * and OpenGL ES 3.1, section 10.6:
759 *
760 * "An INVALID_VALUE error is generated if indirect is not a
761 * multiple of the size, in basic machine units, of uint."
762 */
763 if ((GLsizeiptr)indirect & (sizeof(GLuint) - 1)) {
764 _mesa_error(ctx, GL_INVALID_VALUE,
765 "%s(indirect is not aligned)", name);
766 return GL_FALSE;
767 }
768
769 if (!_mesa_is_bufferobj(ctx->DrawIndirectBuffer)) {
770 _mesa_error(ctx, GL_INVALID_OPERATION,
771 "%s: no buffer bound to DRAW_INDIRECT_BUFFER", name);
772 return GL_FALSE;
773 }
774
775 if (_mesa_check_disallowed_mapping(ctx->DrawIndirectBuffer)) {
776 _mesa_error(ctx, GL_INVALID_OPERATION,
777 "%s(DRAW_INDIRECT_BUFFER is mapped)", name);
778 return GL_FALSE;
779 }
780
781 /* From the ARB_draw_indirect specification:
782 * "An INVALID_OPERATION error is generated if the commands source data
783 * beyond the end of the buffer object [...]"
784 */
785 if (ctx->DrawIndirectBuffer->Size < end) {
786 _mesa_error(ctx, GL_INVALID_OPERATION,
787 "%s(DRAW_INDIRECT_BUFFER too small)", name);
788 return GL_FALSE;
789 }
790
791 if (!check_valid_to_render(ctx, name))
792 return GL_FALSE;
793
794 return GL_TRUE;
795 }
796
797 static inline GLboolean
798 valid_draw_indirect_elements(struct gl_context *ctx,
799 GLenum mode, GLenum type, const GLvoid *indirect,
800 GLsizeiptr size, const char *name)
801 {
802 if (!valid_elements_type(ctx, type, name))
803 return GL_FALSE;
804
805 /*
806 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
807 * may not come from a client array and must come from an index buffer.
808 * If no element array buffer is bound, an INVALID_OPERATION error is
809 * generated.
810 */
811 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
812 _mesa_error(ctx, GL_INVALID_OPERATION,
813 "%s(no buffer bound to GL_ELEMENT_ARRAY_BUFFER)", name);
814 return GL_FALSE;
815 }
816
817 return valid_draw_indirect(ctx, mode, indirect, size, name);
818 }
819
820 static inline GLboolean
821 valid_draw_indirect_multi(struct gl_context *ctx,
822 GLsizei primcount, GLsizei stride,
823 const char *name)
824 {
825
826 /* From the ARB_multi_draw_indirect specification:
827 * "INVALID_VALUE is generated by MultiDrawArraysIndirect or
828 * MultiDrawElementsIndirect if <primcount> is negative."
829 *
830 * "<primcount> must be positive, otherwise an INVALID_VALUE error will
831 * be generated."
832 */
833 if (primcount < 0) {
834 _mesa_error(ctx, GL_INVALID_VALUE, "%s(primcount < 0)", name);
835 return GL_FALSE;
836 }
837
838
839 /* From the ARB_multi_draw_indirect specification:
840 * "<stride> must be a multiple of four, otherwise an INVALID_VALUE
841 * error is generated."
842 */
843 if (stride % 4) {
844 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride %% 4)", name);
845 return GL_FALSE;
846 }
847
848 return GL_TRUE;
849 }
850
851 GLboolean
852 _mesa_validate_DrawArraysIndirect(struct gl_context *ctx,
853 GLenum mode,
854 const GLvoid *indirect)
855 {
856 const unsigned drawArraysNumParams = 4;
857
858 FLUSH_CURRENT(ctx, 0);
859
860 return valid_draw_indirect(ctx, mode,
861 indirect, drawArraysNumParams * sizeof(GLuint),
862 "glDrawArraysIndirect");
863 }
864
865 GLboolean
866 _mesa_validate_DrawElementsIndirect(struct gl_context *ctx,
867 GLenum mode, GLenum type,
868 const GLvoid *indirect)
869 {
870 const unsigned drawElementsNumParams = 5;
871
872 FLUSH_CURRENT(ctx, 0);
873
874 return valid_draw_indirect_elements(ctx, mode, type,
875 indirect, drawElementsNumParams * sizeof(GLuint),
876 "glDrawElementsIndirect");
877 }
878
879 GLboolean
880 _mesa_validate_MultiDrawArraysIndirect(struct gl_context *ctx,
881 GLenum mode,
882 const GLvoid *indirect,
883 GLsizei primcount, GLsizei stride)
884 {
885 GLsizeiptr size = 0;
886 const unsigned drawArraysNumParams = 4;
887
888 FLUSH_CURRENT(ctx, 0);
889
890 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
891 assert(stride != 0);
892
893 if (!valid_draw_indirect_multi(ctx, primcount, stride,
894 "glMultiDrawArraysIndirect"))
895 return GL_FALSE;
896
897 /* number of bytes of the indirect buffer which will be read */
898 size = primcount
899 ? (primcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
900 : 0;
901
902 if (!valid_draw_indirect(ctx, mode, indirect, size,
903 "glMultiDrawArraysIndirect"))
904 return GL_FALSE;
905
906 return GL_TRUE;
907 }
908
909 GLboolean
910 _mesa_validate_MultiDrawElementsIndirect(struct gl_context *ctx,
911 GLenum mode, GLenum type,
912 const GLvoid *indirect,
913 GLsizei primcount, GLsizei stride)
914 {
915 GLsizeiptr size = 0;
916 const unsigned drawElementsNumParams = 5;
917
918 FLUSH_CURRENT(ctx, 0);
919
920 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
921 assert(stride != 0);
922
923 if (!valid_draw_indirect_multi(ctx, primcount, stride,
924 "glMultiDrawElementsIndirect"))
925 return GL_FALSE;
926
927 /* number of bytes of the indirect buffer which will be read */
928 size = primcount
929 ? (primcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
930 : 0;
931
932 if (!valid_draw_indirect_elements(ctx, mode, type,
933 indirect, size,
934 "glMultiDrawElementsIndirect"))
935 return GL_FALSE;
936
937 return GL_TRUE;
938 }
939
940 static GLboolean
941 valid_draw_indirect_parameters(struct gl_context *ctx,
942 const char *name,
943 GLintptr drawcount)
944 {
945 /* From the ARB_indirect_parameters specification:
946 * "INVALID_VALUE is generated by MultiDrawArraysIndirectCountARB or
947 * MultiDrawElementsIndirectCountARB if <drawcount> is not a multiple of
948 * four."
949 */
950 if (drawcount & 3) {
951 _mesa_error(ctx, GL_INVALID_VALUE,
952 "%s(drawcount is not a multiple of 4)", name);
953 return GL_FALSE;
954 }
955
956 /* From the ARB_indirect_parameters specification:
957 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
958 * MultiDrawElementsIndirectCountARB if no buffer is bound to the
959 * PARAMETER_BUFFER_ARB binding point."
960 */
961 if (!_mesa_is_bufferobj(ctx->ParameterBuffer)) {
962 _mesa_error(ctx, GL_INVALID_OPERATION,
963 "%s: no buffer bound to PARAMETER_BUFFER", name);
964 return GL_FALSE;
965 }
966
967 if (_mesa_check_disallowed_mapping(ctx->ParameterBuffer)) {
968 _mesa_error(ctx, GL_INVALID_OPERATION,
969 "%s(PARAMETER_BUFFER is mapped)", name);
970 return GL_FALSE;
971 }
972
973 /* From the ARB_indirect_parameters specification:
974 * "INVALID_OPERATION is generated by MultiDrawArraysIndirectCountARB or
975 * MultiDrawElementsIndirectCountARB if reading a <sizei> typed value
976 * from the buffer bound to the PARAMETER_BUFFER_ARB target at the offset
977 * specified by <drawcount> would result in an out-of-bounds access."
978 */
979 if (ctx->ParameterBuffer->Size < drawcount + sizeof(GLsizei)) {
980 _mesa_error(ctx, GL_INVALID_OPERATION,
981 "%s(PARAMETER_BUFFER too small)", name);
982 return GL_FALSE;
983 }
984
985 return GL_TRUE;
986 }
987
988 GLboolean
989 _mesa_validate_MultiDrawArraysIndirectCount(struct gl_context *ctx,
990 GLenum mode,
991 GLintptr indirect,
992 GLintptr drawcount,
993 GLsizei maxdrawcount,
994 GLsizei stride)
995 {
996 GLsizeiptr size = 0;
997 const unsigned drawArraysNumParams = 4;
998
999 FLUSH_CURRENT(ctx, 0);
1000
1001 /* caller has converted stride==0 to drawArraysNumParams * sizeof(GLuint) */
1002 assert(stride != 0);
1003
1004 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1005 "glMultiDrawArraysIndirectCountARB"))
1006 return GL_FALSE;
1007
1008 /* number of bytes of the indirect buffer which will be read */
1009 size = maxdrawcount
1010 ? (maxdrawcount - 1) * stride + drawArraysNumParams * sizeof(GLuint)
1011 : 0;
1012
1013 if (!valid_draw_indirect(ctx, mode, (void *)indirect, size,
1014 "glMultiDrawArraysIndirectCountARB"))
1015 return GL_FALSE;
1016
1017 return valid_draw_indirect_parameters(
1018 ctx, "glMultiDrawArraysIndirectCountARB", drawcount);
1019 }
1020
1021 GLboolean
1022 _mesa_validate_MultiDrawElementsIndirectCount(struct gl_context *ctx,
1023 GLenum mode, GLenum type,
1024 GLintptr indirect,
1025 GLintptr drawcount,
1026 GLsizei maxdrawcount,
1027 GLsizei stride)
1028 {
1029 GLsizeiptr size = 0;
1030 const unsigned drawElementsNumParams = 5;
1031
1032 FLUSH_CURRENT(ctx, 0);
1033
1034 /* caller has converted stride==0 to drawElementsNumParams * sizeof(GLuint) */
1035 assert(stride != 0);
1036
1037 if (!valid_draw_indirect_multi(ctx, maxdrawcount, stride,
1038 "glMultiDrawElementsIndirectCountARB"))
1039 return GL_FALSE;
1040
1041 /* number of bytes of the indirect buffer which will be read */
1042 size = maxdrawcount
1043 ? (maxdrawcount - 1) * stride + drawElementsNumParams * sizeof(GLuint)
1044 : 0;
1045
1046 if (!valid_draw_indirect_elements(ctx, mode, type,
1047 (void *)indirect, size,
1048 "glMultiDrawElementsIndirectCountARB"))
1049 return GL_FALSE;
1050
1051 return valid_draw_indirect_parameters(
1052 ctx, "glMultiDrawElementsIndirectCountARB", drawcount);
1053 }
1054
1055 static bool
1056 check_valid_to_compute(struct gl_context *ctx, const char *function)
1057 {
1058 struct gl_shader_program *prog;
1059
1060 if (!_mesa_has_compute_shaders(ctx)) {
1061 _mesa_error(ctx, GL_INVALID_OPERATION,
1062 "unsupported function (%s) called",
1063 function);
1064 return false;
1065 }
1066
1067 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1068 *
1069 * "An INVALID_OPERATION error is generated if there is no active program
1070 * for the compute shader stage."
1071 */
1072 prog = ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
1073 if (prog == NULL || prog->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
1074 _mesa_error(ctx, GL_INVALID_OPERATION,
1075 "%s(no active compute shader)",
1076 function);
1077 return false;
1078 }
1079
1080 return true;
1081 }
1082
1083 GLboolean
1084 _mesa_validate_DispatchCompute(struct gl_context *ctx,
1085 const GLuint *num_groups)
1086 {
1087 int i;
1088 FLUSH_CURRENT(ctx, 0);
1089
1090 if (!check_valid_to_compute(ctx, "glDispatchCompute"))
1091 return GL_FALSE;
1092
1093 for (i = 0; i < 3; i++) {
1094 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1095 *
1096 * "An INVALID_VALUE error is generated if any of num_groups_x,
1097 * num_groups_y and num_groups_z are greater than or equal to the
1098 * maximum work group count for the corresponding dimension."
1099 *
1100 * However, the "or equal to" portions appears to be a specification
1101 * bug. In all other areas, the specification appears to indicate that
1102 * the number of workgroups can match the MAX_COMPUTE_WORK_GROUP_COUNT
1103 * value. For example, under DispatchComputeIndirect:
1104 *
1105 * "If any of num_groups_x, num_groups_y or num_groups_z is greater than
1106 * the value of MAX_COMPUTE_WORK_GROUP_COUNT for the corresponding
1107 * dimension then the results are undefined."
1108 *
1109 * Additionally, the OpenGLES 3.1 specification does not contain "or
1110 * equal to" as an error condition.
1111 */
1112 if (num_groups[i] > ctx->Const.MaxComputeWorkGroupCount[i]) {
1113 _mesa_error(ctx, GL_INVALID_VALUE,
1114 "glDispatchCompute(num_groups_%c)", 'x' + i);
1115 return GL_FALSE;
1116 }
1117 }
1118
1119 return GL_TRUE;
1120 }
1121
1122 static GLboolean
1123 valid_dispatch_indirect(struct gl_context *ctx,
1124 GLintptr indirect,
1125 GLsizei size, const char *name)
1126 {
1127 const uint64_t end = (uint64_t) indirect + size;
1128
1129 if (!check_valid_to_compute(ctx, name))
1130 return GL_FALSE;
1131
1132 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1133 *
1134 * "An INVALID_VALUE error is generated if indirect is negative or is not a
1135 * multiple of four."
1136 */
1137 if (indirect & (sizeof(GLuint) - 1)) {
1138 _mesa_error(ctx, GL_INVALID_VALUE,
1139 "%s(indirect is not aligned)", name);
1140 return GL_FALSE;
1141 }
1142
1143 if (indirect < 0) {
1144 _mesa_error(ctx, GL_INVALID_VALUE,
1145 "%s(indirect is less than zero)", name);
1146 return GL_FALSE;
1147 }
1148
1149 /* From the OpenGL 4.3 Core Specification, Chapter 19, Compute Shaders:
1150 *
1151 * "An INVALID_OPERATION error is generated if no buffer is bound to the
1152 * DRAW_INDIRECT_BUFFER binding, or if the command would source data
1153 * beyond the end of the buffer object."
1154 */
1155 if (!_mesa_is_bufferobj(ctx->DispatchIndirectBuffer)) {
1156 _mesa_error(ctx, GL_INVALID_OPERATION,
1157 "%s: no buffer bound to DISPATCH_INDIRECT_BUFFER", name);
1158 return GL_FALSE;
1159 }
1160
1161 if (_mesa_check_disallowed_mapping(ctx->DispatchIndirectBuffer)) {
1162 _mesa_error(ctx, GL_INVALID_OPERATION,
1163 "%s(DISPATCH_INDIRECT_BUFFER is mapped)", name);
1164 return GL_FALSE;
1165 }
1166
1167 if (ctx->DispatchIndirectBuffer->Size < end) {
1168 _mesa_error(ctx, GL_INVALID_OPERATION,
1169 "%s(DISPATCH_INDIRECT_BUFFER too small)", name);
1170 return GL_FALSE;
1171 }
1172
1173 return GL_TRUE;
1174 }
1175
1176 GLboolean
1177 _mesa_validate_DispatchComputeIndirect(struct gl_context *ctx,
1178 GLintptr indirect)
1179 {
1180 FLUSH_CURRENT(ctx, 0);
1181
1182 return valid_dispatch_indirect(ctx, indirect, 3 * sizeof(GLuint),
1183 "glDispatchComputeIndirect");
1184 }