1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
4 * Copyright 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
32 #include "c99_alloca.h"
36 #include "draw_validate.h"
39 #include "bufferobj.h"
42 #include "transformfeedback.h"
49 } DrawArraysIndirectCommand
;
57 } DrawElementsIndirectCommand
;
61 * Check that element 'j' of the array has reasonable data.
63 * For debugging purposes; not normally used.
66 check_array_data(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
67 GLuint attrib
, GLuint j
)
69 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
70 if (vao
->Enabled
& VERT_BIT(attrib
)) {
71 const struct gl_vertex_buffer_binding
*binding
=
72 &vao
->BufferBinding
[array
->BufferBindingIndex
];
73 struct gl_buffer_object
*bo
= binding
->BufferObj
;
74 const void *data
= array
->Ptr
;
75 if (_mesa_is_bufferobj(bo
)) {
76 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
77 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
79 switch (array
->Format
.Type
) {
82 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
84 for (k
= 0; k
< array
->Format
.Size
; k
++) {
85 if (IS_INF_OR_NAN(f
[k
]) || f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
86 printf("Bad array data:\n");
87 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
88 printf(" Array %u at %p\n", attrib
, (void *) array
);
89 printf(" Type 0x%x, Size %d, Stride %d\n",
90 array
->Format
.Type
, array
->Format
.Size
,
92 printf(" Address/offset %p in Buffer Object %u\n",
93 array
->Ptr
, bo
->Name
);
94 f
[k
] = 1.0F
; /* XXX replace the bad value! */
96 /*assert(!IS_INF_OR_NAN(f[k])); */
108 sizeof_ib_type(GLenum type
)
111 case GL_UNSIGNED_INT
:
112 return sizeof(GLuint
);
113 case GL_UNSIGNED_SHORT
:
114 return sizeof(GLushort
);
115 case GL_UNSIGNED_BYTE
:
116 return sizeof(GLubyte
);
118 assert(!"unsupported index data type");
119 /* In case assert is turned off */
125 * Examine the array's data for NaNs, etc.
126 * For debug purposes; not normally used.
129 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
130 GLenum elemType
, const void *elements
,
133 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
137 _mesa_vao_map(ctx
, vao
, GL_MAP_READ_BIT
);
139 if (_mesa_is_bufferobj(vao
->IndexBufferObj
))
141 ADD_POINTERS(vao
->IndexBufferObj
->Mappings
[MAP_INTERNAL
].Pointer
, elements
);
143 for (i
= 0; i
< count
; i
++) {
148 case GL_UNSIGNED_BYTE
:
149 j
= ((const GLubyte
*) elements
)[i
];
151 case GL_UNSIGNED_SHORT
:
152 j
= ((const GLushort
*) elements
)[i
];
154 case GL_UNSIGNED_INT
:
155 j
= ((const GLuint
*) elements
)[i
];
158 unreachable("Unexpected index buffer type");
161 /* check element j of each enabled array */
162 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
163 check_array_data(ctx
, vao
, k
, j
);
167 _mesa_vao_unmap(ctx
, vao
);
172 * Check array data, looking for NaNs, etc.
175 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
182 * Check if we should skip the draw call even after validation was successful.
185 skip_validated_draw(struct gl_context
*ctx
)
189 /* For ES2, we can draw if we have a vertex program/shader). */
190 return ctx
->VertexProgram
._Current
== NULL
;
193 /* For OpenGL ES, only draw if we have vertex positions
195 if (!(ctx
->Array
.VAO
->Enabled
& VERT_BIT_POS
))
199 case API_OPENGL_CORE
:
200 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
203 * "If there is no active program for the vertex or fragment shader
204 * stages, the results of vertex and/or fragment processing will be
205 * undefined. However, this is not an error."
207 * The fragment shader is not tested here because other state (e.g.,
208 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
210 return ctx
->VertexProgram
._Current
== NULL
;
212 case API_OPENGL_COMPAT
:
213 if (ctx
->VertexProgram
._Current
!= NULL
) {
214 /* Draw regardless of whether or not we have any vertex arrays.
215 * (Ex: could draw a point using a constant vertex pos)
219 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
222 return !(ctx
->Array
.VAO
->Enabled
& (VERT_BIT_POS
|VERT_BIT_GENERIC0
));
227 unreachable("Invalid API value in check_valid_to_render()");
235 * Print info/data for glDrawArrays(), for debugging.
238 print_draw_arrays(struct gl_context
*ctx
,
239 GLenum mode
, GLint start
, GLsizei count
)
241 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
243 printf("_mesa_DrawArrays(mode 0x%x, start %d, count %d):\n",
246 _mesa_vao_map_arrays(ctx
, vao
, GL_MAP_READ_BIT
);
248 GLbitfield mask
= vao
->Enabled
;
250 const gl_vert_attrib i
= u_bit_scan(&mask
);
251 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
253 const struct gl_vertex_buffer_binding
*binding
=
254 &vao
->BufferBinding
[array
->BufferBindingIndex
];
255 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
257 printf("attr %s: size %d stride %d "
258 "ptr %p Bufobj %u\n",
259 gl_vert_attrib_name((gl_vert_attrib
) i
),
260 array
->Format
.Size
, binding
->Stride
,
261 array
->Ptr
, bufObj
->Name
);
263 if (_mesa_is_bufferobj(bufObj
)) {
264 GLubyte
*p
= bufObj
->Mappings
[MAP_INTERNAL
].Pointer
;
265 int offset
= (int) (GLintptr
)
266 _mesa_vertex_attrib_address(array
, binding
);
269 switch (array
->Format
.Type
) {
272 case GL_UNSIGNED_INT64_ARB
:
279 float *f
= (float *) (p
+ offset
);
282 int n
= (count
- 1) * (binding
->Stride
/ (4 * multiplier
))
283 + array
->Format
.Size
;
286 printf(" Data at offset %d:\n", offset
);
289 printf(" double[%d] = 0x%016llx %lf\n", i
,
290 ((unsigned long long *) k
)[i
], ((double *) f
)[i
]);
292 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
298 _mesa_vao_unmap_arrays(ctx
, vao
);
303 * Return a filter mask for the net enabled vao arrays.
304 * This is to mask out arrays that would otherwise supersed required current
305 * values for the fixed function shaders for example.
308 enabled_filter(const struct gl_context
*ctx
)
310 switch (ctx
->VertexProgram
._VPMode
) {
312 /* When no vertex program is active (or the vertex program is generated
313 * from fixed-function state). We put the material values into the
314 * generic slots. Since the vao has no material arrays, mute these
315 * slots from the enabled arrays so that the current material values
316 * are pulled instead of the vao arrays.
318 return VERT_BIT_FF_ALL
;
321 /* There are no shaders in OpenGL ES 1.x, so this code path should be
322 * impossible to reach. The meta code is careful to not use shaders in
325 assert(ctx
->API
!= API_OPENGLES
);
327 /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
328 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
329 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
332 if (ctx
->API
!= API_OPENGL_COMPAT
)
333 return VERT_BIT_GENERIC_ALL
;
345 * Helper function called by the other DrawArrays() functions below.
346 * This is where we handle primitive restart for drawing non-indexed
347 * arrays. If primitive restart is enabled, it typically means
348 * splitting one DrawArrays() into two.
351 _mesa_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
352 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
355 if (skip_validated_draw(ctx
))
358 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
361 struct _mesa_prim prim
= {
365 .num_instances
= numInstances
,
366 .base_instance
= baseInstance
,
372 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
373 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0);
375 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
382 * Execute a glRectf() function.
384 static void GLAPIENTRY
385 _mesa_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
387 GET_CURRENT_CONTEXT(ctx
);
388 ASSERT_OUTSIDE_BEGIN_END(ctx
);
390 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
391 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
392 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
393 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
394 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
395 CALL_End(GET_DISPATCH(), ());
399 static void GLAPIENTRY
400 _mesa_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
402 GET_CURRENT_CONTEXT(ctx
);
412 prim
= GL_LINE_STRIP
;
415 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
419 /* No effect if vertex maps disabled.
421 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
424 du
= ctx
->Eval
.MapGrid1du
;
425 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
427 CALL_Begin(GET_DISPATCH(), (prim
));
428 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
429 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
431 CALL_End(GET_DISPATCH(), ());
435 static void GLAPIENTRY
436 _mesa_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
438 GET_CURRENT_CONTEXT(ctx
);
439 GLfloat u
, du
, v
, dv
, v1
, u1
;
448 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
452 /* No effect if vertex maps disabled.
454 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
457 du
= ctx
->Eval
.MapGrid2du
;
458 dv
= ctx
->Eval
.MapGrid2dv
;
459 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
460 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
464 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
465 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
466 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
467 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
470 CALL_End(GET_DISPATCH(), ());
473 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
474 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
475 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
476 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
478 CALL_End(GET_DISPATCH(), ());
480 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
481 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
482 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
483 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
485 CALL_End(GET_DISPATCH(), ());
489 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
490 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
491 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
492 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
493 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
495 CALL_End(GET_DISPATCH(), ());
503 * Called from glDrawArrays when in immediate mode (not display list mode).
506 _mesa_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
508 GET_CURRENT_CONTEXT(ctx
);
510 if (MESA_VERBOSE
& VERBOSE_DRAW
)
511 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
512 _mesa_enum_to_string(mode
), start
, count
);
516 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
518 if (_mesa_is_no_error_enabled(ctx
)) {
520 _mesa_update_state(ctx
);
522 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
527 check_draw_arrays_data(ctx
, start
, count
);
529 _mesa_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
532 print_draw_arrays(ctx
, mode
, start
, count
);
537 * Called from glDrawArraysInstanced when in immediate mode (not
538 * display list mode).
541 _mesa_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
542 GLsizei numInstances
)
544 GET_CURRENT_CONTEXT(ctx
);
546 if (MESA_VERBOSE
& VERBOSE_DRAW
)
547 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
548 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
552 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
554 if (_mesa_is_no_error_enabled(ctx
)) {
556 _mesa_update_state(ctx
);
558 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
564 check_draw_arrays_data(ctx
, start
, count
);
566 _mesa_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
569 print_draw_arrays(ctx
, mode
, start
, count
);
574 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
576 static void GLAPIENTRY
577 _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
578 GLsizei count
, GLsizei numInstances
,
581 GET_CURRENT_CONTEXT(ctx
);
583 if (MESA_VERBOSE
& VERBOSE_DRAW
)
585 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
586 _mesa_enum_to_string(mode
), first
, count
,
587 numInstances
, baseInstance
);
591 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
593 if (_mesa_is_no_error_enabled(ctx
)) {
595 _mesa_update_state(ctx
);
597 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
603 check_draw_arrays_data(ctx
, first
, count
);
605 _mesa_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
608 print_draw_arrays(ctx
, mode
, first
, count
);
613 * Called from glMultiDrawArrays when in immediate mode.
615 static void GLAPIENTRY
616 _mesa_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
617 const GLsizei
*count
, GLsizei primcount
)
619 GET_CURRENT_CONTEXT(ctx
);
622 if (MESA_VERBOSE
& VERBOSE_DRAW
)
624 "glMultiDrawArrays(%s, %p, %p, %d)\n",
625 _mesa_enum_to_string(mode
), first
, count
, primcount
);
629 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
631 if (_mesa_is_no_error_enabled(ctx
)) {
633 _mesa_update_state(ctx
);
635 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
639 for (i
= 0; i
< primcount
; i
++) {
642 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
644 /* The GL_ARB_shader_draw_parameters spec adds the following after the
645 * pseudo-code describing glMultiDrawArrays:
647 * "The index of the draw (<i> in the above pseudo-code) may be
648 * read by a vertex shader as <gl_DrawIDARB>, as described in
651 _mesa_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
654 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
662 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
667 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
670 ctx
->Driver
.MapBufferRange(ctx
, 0,
671 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
673 ctx
->Array
.VAO
->IndexBufferObj
,
676 case GL_UNSIGNED_BYTE
:
678 const GLubyte
*us
= (const GLubyte
*) map
;
680 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
681 printf("%02x ", us
[i
]);
688 case GL_UNSIGNED_SHORT
:
690 const GLushort
*us
= (const GLushort
*) map
;
692 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
693 printf("%04x ", us
[i
]);
700 case GL_UNSIGNED_INT
:
702 const GLuint
*us
= (const GLuint
*) map
;
704 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
705 printf("%08x ", us
[i
]);
716 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
722 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
723 const GLvoid
*indices
)
728 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
730 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
733 if (skip_validated_draw(ctx
))
741 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
742 * Do the rendering for a glDrawElements or glDrawRangeElements call after
743 * we've validated buffer bounds, etc.
746 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
747 GLboolean index_bounds_valid
,
748 GLuint start
, GLuint end
,
749 GLsizei count
, GLenum type
,
750 const GLvoid
* indices
,
751 GLint basevertex
, GLuint numInstances
,
754 struct _mesa_index_buffer ib
;
755 struct _mesa_prim prim
;
757 if (!index_bounds_valid
) {
762 if (skip_draw_elements(ctx
, count
, indices
))
766 ib
.index_size
= sizeof_ib_type(type
);
767 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
776 prim
.basevertex
= basevertex
;
777 prim
.num_instances
= numInstances
;
778 prim
.base_instance
= baseInstance
;
781 /* Need to give special consideration to rendering a range of
782 * indices starting somewhere above zero. Typically the
783 * application is issuing multiple DrawRangeElements() to draw
784 * successive primitives layed out linearly in the vertex arrays.
785 * Unless the vertex arrays are all in a VBO (or locked as with
786 * CVA), the OpenGL semantics imply that we need to re-read or
787 * re-upload the vertex data on each draw call.
789 * In the case of hardware tnl, we want to avoid starting the
790 * upload at zero, as it will mean every draw call uploads an
791 * increasing amount of not-used vertex data. Worse - in the
792 * software tnl module, all those vertices might be transformed and
793 * lit but never rendered.
795 * If we just upload or transform the vertices in start..end,
796 * however, the indices will be incorrect.
798 * At this level, we don't know exactly what the requirements of
799 * the backend are going to be, though it will likely boil down to
802 * 1) Do nothing, everything is in a VBO and is processed once
805 * 2) Adjust the indices and vertex arrays so that start becomes
808 * Rather than doing anything here, I'll provide a helper function
809 * for the latter case elsewhere.
812 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
813 index_bounds_valid
, start
, end
, NULL
, 0);
815 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
822 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
825 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
826 GLsizei count
, GLenum type
,
827 const GLvoid
* indices
, GLint basevertex
)
829 static GLuint warnCount
= 0;
830 GLboolean index_bounds_valid
= GL_TRUE
;
832 /* This is only useful to catch invalid values in the "end" parameter
835 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
837 GET_CURRENT_CONTEXT(ctx
);
839 if (MESA_VERBOSE
& VERBOSE_DRAW
)
841 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
842 _mesa_enum_to_string(mode
), start
, end
, count
,
843 _mesa_enum_to_string(type
), indices
, basevertex
);
847 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
849 if (_mesa_is_no_error_enabled(ctx
)) {
851 _mesa_update_state(ctx
);
853 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
858 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
859 /* The application requested we draw using a range of indices that's
860 * outside the bounds of the current VBO. This is invalid and appears
861 * to give undefined results. The safest thing to do is to simply
862 * ignore the range, in case the application botched their range tracking
863 * but did provide valid indices. Also issue a warning indicating that
864 * the application is broken.
866 if (warnCount
++ < 10) {
867 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
868 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
869 "\trange is outside VBO bounds (max=%u); ignoring.\n"
870 "\tThis should be fixed in the application.",
871 start
, end
, basevertex
, count
, type
, indices
,
874 index_bounds_valid
= GL_FALSE
;
877 /* NOTE: It's important that 'end' is a reasonable value.
878 * in _tnl_draw_prims(), we use end to determine how many vertices
879 * to transform. If it's too large, we can unnecessarily split prims
880 * or we can read/write out of memory in several different places!
883 /* Catch/fix some potential user errors */
884 if (type
== GL_UNSIGNED_BYTE
) {
885 start
= MIN2(start
, 0xff);
886 end
= MIN2(end
, 0xff);
888 else if (type
== GL_UNSIGNED_SHORT
) {
889 start
= MIN2(start
, 0xffff);
890 end
= MIN2(end
, 0xffff);
894 printf("glDraw[Range]Elements{,BaseVertex}"
895 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
897 start
, end
, type
, count
,
898 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
901 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
902 index_bounds_valid
= GL_FALSE
;
905 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
907 (void) check_draw_elements_data
;
910 if (!index_bounds_valid
) {
915 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
916 count
, type
, indices
, basevertex
, 1, 0);
921 * Called by glDrawRangeElements() in immediate mode.
924 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
925 GLsizei count
, GLenum type
, const GLvoid
* indices
)
927 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
928 GET_CURRENT_CONTEXT(ctx
);
930 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
931 _mesa_enum_to_string(mode
), start
, end
, count
,
932 _mesa_enum_to_string(type
), indices
);
935 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
941 * Called by glDrawElements() in immediate mode.
944 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
945 const GLvoid
* indices
)
947 GET_CURRENT_CONTEXT(ctx
);
949 if (MESA_VERBOSE
& VERBOSE_DRAW
)
950 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
951 _mesa_enum_to_string(mode
), count
,
952 _mesa_enum_to_string(type
), indices
);
956 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
958 if (_mesa_is_no_error_enabled(ctx
)) {
960 _mesa_update_state(ctx
);
962 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
966 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
967 count
, type
, indices
, 0, 1, 0);
972 * Called by glDrawElementsBaseVertex() in immediate mode.
975 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
976 const GLvoid
* indices
, GLint basevertex
)
978 GET_CURRENT_CONTEXT(ctx
);
980 if (MESA_VERBOSE
& VERBOSE_DRAW
)
981 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
982 _mesa_enum_to_string(mode
), count
,
983 _mesa_enum_to_string(type
), indices
);
987 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
989 if (_mesa_is_no_error_enabled(ctx
)) {
991 _mesa_update_state(ctx
);
993 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
997 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
998 count
, type
, indices
, basevertex
, 1, 0);
1003 * Called by glDrawElementsInstanced() in immediate mode.
1005 static void GLAPIENTRY
1006 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1007 const GLvoid
* indices
, GLsizei numInstances
)
1009 GET_CURRENT_CONTEXT(ctx
);
1011 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1012 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1013 _mesa_enum_to_string(mode
), count
,
1014 _mesa_enum_to_string(type
), indices
);
1016 FLUSH_FOR_DRAW(ctx
);
1018 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1020 if (_mesa_is_no_error_enabled(ctx
)) {
1022 _mesa_update_state(ctx
);
1024 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1025 indices
, numInstances
))
1029 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1030 count
, type
, indices
, 0, numInstances
, 0);
1035 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1037 static void GLAPIENTRY
1038 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1039 GLenum type
, const GLvoid
* indices
,
1040 GLsizei numInstances
,
1043 GET_CURRENT_CONTEXT(ctx
);
1045 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1047 "glDrawElementsInstancedBaseVertex"
1048 "(%s, %d, %s, %p, %d; %d)\n",
1049 _mesa_enum_to_string(mode
), count
,
1050 _mesa_enum_to_string(type
), indices
,
1051 numInstances
, basevertex
);
1053 FLUSH_FOR_DRAW(ctx
);
1055 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1057 if (_mesa_is_no_error_enabled(ctx
)) {
1059 _mesa_update_state(ctx
);
1061 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1062 indices
, numInstances
))
1066 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1067 count
, type
, indices
,
1068 basevertex
, numInstances
, 0);
1073 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1075 static void GLAPIENTRY
1076 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1078 const GLvoid
*indices
,
1079 GLsizei numInstances
,
1080 GLuint baseInstance
)
1082 GET_CURRENT_CONTEXT(ctx
);
1084 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1086 "glDrawElementsInstancedBaseInstance"
1087 "(%s, %d, %s, %p, %d, %d)\n",
1088 _mesa_enum_to_string(mode
), count
,
1089 _mesa_enum_to_string(type
), indices
,
1090 numInstances
, baseInstance
);
1092 FLUSH_FOR_DRAW(ctx
);
1094 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1096 if (_mesa_is_no_error_enabled(ctx
)) {
1098 _mesa_update_state(ctx
);
1100 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1101 indices
, numInstances
))
1105 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1106 count
, type
, indices
, 0, numInstances
,
1112 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1114 static void GLAPIENTRY
1115 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1118 const GLvoid
*indices
,
1119 GLsizei numInstances
,
1121 GLuint baseInstance
)
1123 GET_CURRENT_CONTEXT(ctx
);
1125 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1127 "glDrawElementsInstancedBaseVertexBaseInstance"
1128 "(%s, %d, %s, %p, %d, %d, %d)\n",
1129 _mesa_enum_to_string(mode
), count
,
1130 _mesa_enum_to_string(type
), indices
,
1131 numInstances
, basevertex
, baseInstance
);
1133 FLUSH_FOR_DRAW(ctx
);
1135 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1137 if (_mesa_is_no_error_enabled(ctx
)) {
1139 _mesa_update_state(ctx
);
1141 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1142 indices
, numInstances
))
1146 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1147 count
, type
, indices
, basevertex
,
1148 numInstances
, baseInstance
);
1153 * Inner support for both _mesa_MultiDrawElements() and
1154 * _mesa_MultiDrawRangeElements().
1155 * This does the actual rendering after we've checked array indexes, etc.
1158 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1159 const GLsizei
*count
, GLenum type
,
1160 const GLvoid
* const *indices
,
1161 GLsizei primcount
, const GLint
*basevertex
)
1163 struct _mesa_index_buffer ib
;
1164 struct _mesa_prim
*prim
;
1165 unsigned int index_type_size
= sizeof_ib_type(type
);
1166 uintptr_t min_index_ptr
, max_index_ptr
;
1167 GLboolean fallback
= GL_FALSE
;
1173 prim
= calloc(primcount
, sizeof(*prim
));
1175 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1179 min_index_ptr
= (uintptr_t) indices
[0];
1181 for (i
= 0; i
< primcount
; i
++) {
1182 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1183 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1184 index_type_size
* count
[i
]);
1187 /* Check if we can handle this thing as a bunch of index offsets from the
1188 * same index pointer. If we can't, then we have to fall back to doing
1189 * a draw_prims per primitive.
1190 * Check that the difference between each prim's indexes is a multiple of
1191 * the index/element size.
1193 if (index_type_size
!= 1) {
1194 for (i
= 0; i
< primcount
; i
++) {
1195 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1203 /* Draw primitives individually if one count is zero, so we can easily skip
1206 for (i
= 0; i
< primcount
; i
++) {
1207 if (count
[i
] == 0) {
1213 /* If the index buffer isn't in a VBO, then treating the application's
1214 * subranges of the index buffer as one large index buffer may lead to
1215 * us reading unmapped memory.
1217 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1221 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1222 ib
.index_size
= sizeof_ib_type(type
);
1223 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1224 ib
.ptr
= (void *) min_index_ptr
;
1226 for (i
= 0; i
< primcount
; i
++) {
1227 prim
[i
].begin
= (i
== 0);
1228 prim
[i
].end
= (i
== primcount
- 1);
1229 prim
[i
].mode
= mode
;
1231 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1232 prim
[i
].count
= count
[i
];
1233 prim
[i
].indexed
= 1;
1234 prim
[i
].num_instances
= 1;
1235 prim
[i
].base_instance
= 0;
1236 prim
[i
].draw_id
= i
;
1237 if (basevertex
!= NULL
)
1238 prim
[i
].basevertex
= basevertex
[i
];
1240 prim
[i
].basevertex
= 0;
1243 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1244 false, 0, ~0, NULL
, 0);
1247 /* render one prim at a time */
1248 for (i
= 0; i
< primcount
; i
++) {
1251 ib
.count
= count
[i
];
1252 ib
.index_size
= sizeof_ib_type(type
);
1253 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1254 ib
.ptr
= indices
[i
];
1258 prim
[0].mode
= mode
;
1260 prim
[0].count
= count
[i
];
1261 prim
[0].indexed
= 1;
1262 prim
[0].num_instances
= 1;
1263 prim
[0].base_instance
= 0;
1264 prim
[0].draw_id
= i
;
1265 if (basevertex
!= NULL
)
1266 prim
[0].basevertex
= basevertex
[i
];
1268 prim
[0].basevertex
= 0;
1270 ctx
->Driver
.Draw(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0);
1276 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1283 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1284 const GLvoid
* const *indices
, GLsizei primcount
)
1286 GET_CURRENT_CONTEXT(ctx
);
1288 FLUSH_FOR_DRAW(ctx
);
1290 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1292 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1296 if (skip_validated_draw(ctx
))
1299 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1305 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1306 const GLsizei
*count
, GLenum type
,
1307 const GLvoid
* const *indices
,
1309 const GLsizei
*basevertex
)
1311 GET_CURRENT_CONTEXT(ctx
);
1313 FLUSH_FOR_DRAW(ctx
);
1315 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1317 if (_mesa_is_no_error_enabled(ctx
)) {
1319 _mesa_update_state(ctx
);
1321 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1326 if (skip_validated_draw(ctx
))
1329 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1335 * Draw a GL primitive using a vertex count obtained from transform feedback.
1336 * \param mode the type of GL primitive to draw
1337 * \param obj the transform feedback object to use
1338 * \param stream index of the transform feedback stream from which to
1339 * get the primitive count.
1340 * \param numInstances number of instances to draw
1343 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1344 struct gl_transform_feedback_object
*obj
,
1345 GLuint stream
, GLuint numInstances
)
1347 struct _mesa_prim prim
;
1349 FLUSH_FOR_DRAW(ctx
);
1351 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1353 if (_mesa_is_no_error_enabled(ctx
)) {
1355 _mesa_update_state(ctx
);
1357 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1363 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1364 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1365 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1367 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1368 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1372 if (skip_validated_draw(ctx
))
1375 /* init most fields to zero */
1376 memset(&prim
, 0, sizeof(prim
));
1380 prim
.num_instances
= numInstances
;
1381 prim
.base_instance
= 0;
1383 /* Maybe we should do some primitive splitting for primitive restart
1384 * (like in DrawArrays), but we have no way to know how many vertices
1385 * will be rendered. */
1387 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
);
1389 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1396 * Like DrawArrays, but take the count from a transform feedback object.
1397 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1398 * \param name the transform feedback object
1399 * User still has to setup of the vertex attribute info with
1400 * glVertexPointer, glColorPointer, etc.
1401 * Part of GL_ARB_transform_feedback2.
1404 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1406 GET_CURRENT_CONTEXT(ctx
);
1407 struct gl_transform_feedback_object
*obj
=
1408 _mesa_lookup_transform_feedback_object(ctx
, name
);
1410 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1411 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1412 _mesa_enum_to_string(mode
), name
);
1414 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1418 static void GLAPIENTRY
1419 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1421 GET_CURRENT_CONTEXT(ctx
);
1422 struct gl_transform_feedback_object
*obj
=
1423 _mesa_lookup_transform_feedback_object(ctx
, name
);
1425 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1426 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1427 _mesa_enum_to_string(mode
), name
, stream
);
1429 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1433 static void GLAPIENTRY
1434 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1437 GET_CURRENT_CONTEXT(ctx
);
1438 struct gl_transform_feedback_object
*obj
=
1439 _mesa_lookup_transform_feedback_object(ctx
, name
);
1441 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1442 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1443 _mesa_enum_to_string(mode
), name
);
1445 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1449 static void GLAPIENTRY
1450 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1454 GET_CURRENT_CONTEXT(ctx
);
1455 struct gl_transform_feedback_object
*obj
=
1456 _mesa_lookup_transform_feedback_object(ctx
, name
);
1458 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1459 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1460 "(%s, %u, %u, %i)\n",
1461 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1463 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1468 _mesa_validated_drawarraysindirect(struct gl_context
*ctx
,
1469 GLenum mode
, const GLvoid
*indirect
)
1471 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1472 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1473 1 /* draw_count */ , 16 /* stride */ ,
1476 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1482 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1484 const GLvoid
*indirect
,
1485 GLsizei primcount
, GLsizei stride
)
1487 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1492 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1493 primcount
, stride
, NULL
, 0, NULL
);
1495 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1501 _mesa_validated_drawelementsindirect(struct gl_context
*ctx
,
1502 GLenum mode
, GLenum type
,
1503 const GLvoid
*indirect
)
1505 struct _mesa_index_buffer ib
;
1507 ib
.count
= 0; /* unknown */
1508 ib
.index_size
= sizeof_ib_type(type
);
1509 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1512 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1513 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1514 1 /* draw_count */ , 20 /* stride */ ,
1517 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1523 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1524 GLenum mode
, GLenum type
,
1525 const GLvoid
*indirect
,
1526 GLsizei primcount
, GLsizei stride
)
1528 struct _mesa_index_buffer ib
;
1529 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1534 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1536 ib
.count
= 0; /* unknown */
1537 ib
.index_size
= sizeof_ib_type(type
);
1538 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1541 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1542 ctx
->DrawIndirectBuffer
, offset
,
1543 primcount
, stride
, NULL
, 0, &ib
);
1545 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1551 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1554 static void GLAPIENTRY
1555 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1557 GET_CURRENT_CONTEXT(ctx
);
1559 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1560 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1561 _mesa_enum_to_string(mode
), indirect
);
1563 /* From the ARB_draw_indirect spec:
1565 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1566 * compatibility profile, this indicates that DrawArraysIndirect and
1567 * DrawElementsIndirect are to source their arguments directly from the
1568 * pointer passed as their <indirect> parameters."
1570 if (ctx
->API
== API_OPENGL_COMPAT
&&
1571 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1572 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1574 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1580 FLUSH_FOR_DRAW(ctx
);
1582 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1584 if (_mesa_is_no_error_enabled(ctx
)) {
1586 _mesa_update_state(ctx
);
1588 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1592 if (skip_validated_draw(ctx
))
1595 _mesa_validated_drawarraysindirect(ctx
, mode
, indirect
);
1599 static void GLAPIENTRY
1600 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1602 GET_CURRENT_CONTEXT(ctx
);
1604 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1605 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1606 _mesa_enum_to_string(mode
),
1607 _mesa_enum_to_string(type
), indirect
);
1609 /* From the ARB_draw_indirect spec:
1611 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1612 * compatibility profile, this indicates that DrawArraysIndirect and
1613 * DrawElementsIndirect are to source their arguments directly from the
1614 * pointer passed as their <indirect> parameters."
1616 if (ctx
->API
== API_OPENGL_COMPAT
&&
1617 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1619 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1620 * may not come from a client array and must come from an index buffer.
1621 * If no element array buffer is bound, an INVALID_OPERATION error is
1624 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1625 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1626 "glDrawElementsIndirect(no buffer bound "
1627 "to GL_ELEMENT_ARRAY_BUFFER)");
1629 DrawElementsIndirectCommand
*cmd
=
1630 (DrawElementsIndirectCommand
*) indirect
;
1632 /* Convert offset to pointer */
1633 void *offset
= (void *)
1634 (uintptr_t)((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1636 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1646 FLUSH_FOR_DRAW(ctx
);
1648 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1650 if (_mesa_is_no_error_enabled(ctx
)) {
1652 _mesa_update_state(ctx
);
1654 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1658 if (skip_validated_draw(ctx
))
1661 _mesa_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1665 static void GLAPIENTRY
1666 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1667 GLsizei primcount
, GLsizei stride
)
1669 GET_CURRENT_CONTEXT(ctx
);
1671 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1672 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1673 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1675 /* If <stride> is zero, the array elements are treated as tightly packed. */
1677 stride
= sizeof(DrawArraysIndirectCommand
);
1679 /* From the ARB_draw_indirect spec:
1681 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1682 * compatibility profile, this indicates that DrawArraysIndirect and
1683 * DrawElementsIndirect are to source their arguments directly from the
1684 * pointer passed as their <indirect> parameters."
1686 if (ctx
->API
== API_OPENGL_COMPAT
&&
1687 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1689 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1690 "glMultiDrawArraysIndirect"))
1693 const uint8_t *ptr
= (const uint8_t *) indirect
;
1694 for (unsigned i
= 0; i
< primcount
; i
++) {
1695 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1696 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1697 cmd
->count
, cmd
->primCount
,
1701 ptr
+= sizeof(DrawArraysIndirectCommand
);
1710 FLUSH_FOR_DRAW(ctx
);
1712 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1714 if (_mesa_is_no_error_enabled(ctx
)) {
1716 _mesa_update_state(ctx
);
1718 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1723 if (skip_validated_draw(ctx
))
1726 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1731 static void GLAPIENTRY
1732 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1733 const GLvoid
*indirect
,
1734 GLsizei primcount
, GLsizei stride
)
1736 GET_CURRENT_CONTEXT(ctx
);
1738 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1739 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1740 _mesa_enum_to_string(mode
),
1741 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1743 /* If <stride> is zero, the array elements are treated as tightly packed. */
1745 stride
= sizeof(DrawElementsIndirectCommand
);
1748 /* From the ARB_draw_indirect spec:
1750 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1751 * compatibility profile, this indicates that DrawArraysIndirect and
1752 * DrawElementsIndirect are to source their arguments directly from the
1753 * pointer passed as their <indirect> parameters."
1755 if (ctx
->API
== API_OPENGL_COMPAT
&&
1756 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1758 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1759 * may not come from a client array and must come from an index buffer.
1760 * If no element array buffer is bound, an INVALID_OPERATION error is
1763 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1764 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1765 "glMultiDrawElementsIndirect(no buffer bound "
1766 "to GL_ELEMENT_ARRAY_BUFFER)");
1771 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1772 "glMultiDrawArraysIndirect"))
1775 const uint8_t *ptr
= (const uint8_t *) indirect
;
1776 for (unsigned i
= 0; i
< primcount
; i
++) {
1777 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1780 ptr
+= sizeof(DrawElementsIndirectCommand
);
1789 FLUSH_FOR_DRAW(ctx
);
1791 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1793 if (_mesa_is_no_error_enabled(ctx
)) {
1795 _mesa_update_state(ctx
);
1797 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1802 if (skip_validated_draw(ctx
))
1805 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1811 _mesa_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1814 GLintptr drawcount_offset
,
1815 GLsizei maxdrawcount
,
1818 GLsizeiptr offset
= indirect
;
1820 if (maxdrawcount
== 0)
1823 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1824 ctx
->DrawIndirectBuffer
, offset
,
1825 maxdrawcount
, stride
,
1826 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1828 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1834 _mesa_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1835 GLenum mode
, GLenum type
,
1837 GLintptr drawcount_offset
,
1838 GLsizei maxdrawcount
,
1841 struct _mesa_index_buffer ib
;
1842 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1844 if (maxdrawcount
== 0)
1847 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1849 ib
.count
= 0; /* unknown */
1850 ib
.index_size
= sizeof_ib_type(type
);
1851 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1854 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1855 ctx
->DrawIndirectBuffer
, offset
,
1856 maxdrawcount
, stride
,
1857 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1859 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1864 static void GLAPIENTRY
1865 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1866 GLintptr drawcount_offset
,
1867 GLsizei maxdrawcount
, GLsizei stride
)
1869 GET_CURRENT_CONTEXT(ctx
);
1871 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1872 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1873 "(%s, %lx, %lx, %i, %i)\n",
1874 _mesa_enum_to_string(mode
),
1875 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1876 maxdrawcount
, stride
);
1878 /* If <stride> is zero, the array elements are treated as tightly packed. */
1880 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1882 FLUSH_FOR_DRAW(ctx
);
1884 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1886 if (_mesa_is_no_error_enabled(ctx
)) {
1888 _mesa_update_state(ctx
);
1890 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1893 maxdrawcount
, stride
))
1897 if (skip_validated_draw(ctx
))
1900 _mesa_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1902 maxdrawcount
, stride
);
1906 static void GLAPIENTRY
1907 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1909 GLintptr drawcount_offset
,
1910 GLsizei maxdrawcount
, GLsizei stride
)
1912 GET_CURRENT_CONTEXT(ctx
);
1914 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1915 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1916 "(%s, %s, %lx, %lx, %i, %i)\n",
1917 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1918 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1919 maxdrawcount
, stride
);
1921 /* If <stride> is zero, the array elements are treated as tightly packed. */
1923 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1925 FLUSH_FOR_DRAW(ctx
);
1927 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1929 if (_mesa_is_no_error_enabled(ctx
)) {
1931 _mesa_update_state(ctx
);
1933 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1936 maxdrawcount
, stride
))
1940 if (skip_validated_draw(ctx
))
1943 _mesa_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1944 drawcount_offset
, maxdrawcount
,
1950 * Initialize the dispatch table with the VBO functions for drawing.
1953 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1954 struct _glapi_table
*exec
)
1956 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1957 SET_DrawElements(exec
, _mesa_DrawElements
);
1959 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1960 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
1963 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
1964 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
1966 if (ctx
->API
== API_OPENGL_COMPAT
) {
1967 SET_Rectf(exec
, _mesa_exec_Rectf
);
1968 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
1969 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
1972 if (ctx
->API
!= API_OPENGLES
&&
1973 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1974 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
1975 SET_MultiDrawElementsBaseVertex(exec
,
1976 _mesa_MultiDrawElementsBaseVertex
);
1978 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1979 SET_DrawRangeElementsBaseVertex(exec
,
1980 _mesa_DrawRangeElementsBaseVertex
);
1981 SET_DrawElementsInstancedBaseVertex(exec
,
1982 _mesa_exec_DrawElementsInstancedBaseVertex
);
1986 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1987 SET_DrawArraysInstancedBaseInstance(exec
,
1988 _mesa_exec_DrawArraysInstancedBaseInstance
);
1989 SET_DrawElementsInstancedBaseInstance(exec
,
1990 _mesa_exec_DrawElementsInstancedBaseInstance
);
1991 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1992 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1995 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
1996 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
1997 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
2000 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2001 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
2002 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
2005 if (_mesa_is_desktop_gl(ctx
)) {
2006 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
2007 SET_DrawTransformFeedbackStream(exec
,
2008 _mesa_exec_DrawTransformFeedbackStream
);
2009 SET_DrawTransformFeedbackInstanced(exec
,
2010 _mesa_exec_DrawTransformFeedbackInstanced
);
2011 SET_DrawTransformFeedbackStreamInstanced(exec
,
2012 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
2013 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
2014 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
2015 SET_MultiDrawArraysIndirectCountARB(exec
,
2016 _mesa_exec_MultiDrawArraysIndirectCount
);
2017 SET_MultiDrawElementsIndirectCountARB(exec
,
2018 _mesa_exec_MultiDrawElementsIndirectCount
);
2024 /* GL_IBM_multimode_draw_arrays */
2026 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
2027 const GLsizei
* count
,
2028 GLsizei primcount
, GLint modestride
)
2030 GET_CURRENT_CONTEXT(ctx
);
2033 FLUSH_VERTICES(ctx
, 0);
2035 for ( i
= 0 ; i
< primcount
; i
++ ) {
2036 if ( count
[i
] > 0 ) {
2037 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2038 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
2044 /* GL_IBM_multimode_draw_arrays */
2046 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
2047 GLenum type
, const GLvoid
* const * indices
,
2048 GLsizei primcount
, GLint modestride
)
2050 GET_CURRENT_CONTEXT(ctx
);
2053 FLUSH_VERTICES(ctx
, 0);
2055 /* XXX not sure about ARB_vertex_buffer_object handling here */
2057 for ( i
= 0 ; i
< primcount
; i
++ ) {
2058 if ( count
[i
] > 0 ) {
2059 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2060 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,