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
= {
370 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
371 GL_TRUE
, start
, start
+ count
- 1,
372 numInstances
, baseInstance
, NULL
, 0);
374 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
381 * Execute a glRectf() function.
383 static void GLAPIENTRY
384 _mesa_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
386 GET_CURRENT_CONTEXT(ctx
);
387 ASSERT_OUTSIDE_BEGIN_END(ctx
);
389 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
390 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
391 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
392 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
393 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
394 CALL_End(GET_DISPATCH(), ());
398 static void GLAPIENTRY
399 _mesa_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
401 GET_CURRENT_CONTEXT(ctx
);
411 prim
= GL_LINE_STRIP
;
414 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
418 /* No effect if vertex maps disabled.
420 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
423 du
= ctx
->Eval
.MapGrid1du
;
424 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
426 CALL_Begin(GET_DISPATCH(), (prim
));
427 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
428 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
430 CALL_End(GET_DISPATCH(), ());
434 static void GLAPIENTRY
435 _mesa_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
437 GET_CURRENT_CONTEXT(ctx
);
438 GLfloat u
, du
, v
, dv
, v1
, u1
;
447 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
451 /* No effect if vertex maps disabled.
453 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
456 du
= ctx
->Eval
.MapGrid2du
;
457 dv
= ctx
->Eval
.MapGrid2dv
;
458 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
459 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
463 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
464 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
465 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
466 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
469 CALL_End(GET_DISPATCH(), ());
472 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
473 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
474 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
475 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
477 CALL_End(GET_DISPATCH(), ());
479 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
480 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
481 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
482 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
484 CALL_End(GET_DISPATCH(), ());
488 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
489 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
490 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
491 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
492 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
494 CALL_End(GET_DISPATCH(), ());
502 * Called from glDrawArrays when in immediate mode (not display list mode).
505 _mesa_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
507 GET_CURRENT_CONTEXT(ctx
);
509 if (MESA_VERBOSE
& VERBOSE_DRAW
)
510 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
511 _mesa_enum_to_string(mode
), start
, count
);
515 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
517 if (_mesa_is_no_error_enabled(ctx
)) {
519 _mesa_update_state(ctx
);
521 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
526 check_draw_arrays_data(ctx
, start
, count
);
528 _mesa_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
531 print_draw_arrays(ctx
, mode
, start
, count
);
536 * Called from glDrawArraysInstanced when in immediate mode (not
537 * display list mode).
540 _mesa_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
541 GLsizei numInstances
)
543 GET_CURRENT_CONTEXT(ctx
);
545 if (MESA_VERBOSE
& VERBOSE_DRAW
)
546 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
547 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
551 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
553 if (_mesa_is_no_error_enabled(ctx
)) {
555 _mesa_update_state(ctx
);
557 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
563 check_draw_arrays_data(ctx
, start
, count
);
565 _mesa_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
568 print_draw_arrays(ctx
, mode
, start
, count
);
573 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
575 static void GLAPIENTRY
576 _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
577 GLsizei count
, GLsizei numInstances
,
580 GET_CURRENT_CONTEXT(ctx
);
582 if (MESA_VERBOSE
& VERBOSE_DRAW
)
584 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
585 _mesa_enum_to_string(mode
), first
, count
,
586 numInstances
, baseInstance
);
590 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
592 if (_mesa_is_no_error_enabled(ctx
)) {
594 _mesa_update_state(ctx
);
596 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
602 check_draw_arrays_data(ctx
, first
, count
);
604 _mesa_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
607 print_draw_arrays(ctx
, mode
, first
, count
);
611 #define MAX_ALLOCA_PRIMS (50000 / sizeof(*prim))
613 /* Use calloc for large allocations and alloca for small allocations. */
614 /* We have to use a macro because alloca is local within the function. */
615 #define ALLOC_PRIMS(prim, primcount, func) do { \
616 if (unlikely(primcount > MAX_ALLOCA_PRIMS)) { \
617 prim = calloc(primcount, sizeof(*prim)); \
619 _mesa_error(ctx, GL_OUT_OF_MEMORY, func); \
623 prim = alloca(primcount * sizeof(*prim)); \
627 #define FREE_PRIMS(prim, primcount) do { \
628 if (primcount > MAX_ALLOCA_PRIMS) \
634 * Called from glMultiDrawArrays when in immediate mode.
636 static void GLAPIENTRY
637 _mesa_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
638 const GLsizei
*count
, GLsizei primcount
)
640 GET_CURRENT_CONTEXT(ctx
);
643 if (MESA_VERBOSE
& VERBOSE_DRAW
)
645 "glMultiDrawArrays(%s, %p, %p, %d)\n",
646 _mesa_enum_to_string(mode
), first
, count
, primcount
);
650 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
652 if (_mesa_is_no_error_enabled(ctx
)) {
654 _mesa_update_state(ctx
);
656 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
660 if (skip_validated_draw(ctx
))
663 struct _mesa_prim
*prim
;
665 ALLOC_PRIMS(prim
, primcount
, "glMultiDrawElements");
667 for (i
= 0; i
< primcount
; i
++) {
672 prim
[i
].start
= first
[i
];
673 prim
[i
].count
= count
[i
];
674 prim
[i
].basevertex
= 0;
677 ctx
->Driver
.Draw(ctx
, prim
, primcount
, NULL
, GL_FALSE
, 0, 0, 1, 0,
680 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
683 FREE_PRIMS(prim
, primcount
);
689 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
694 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
697 ctx
->Driver
.MapBufferRange(ctx
, 0,
698 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
700 ctx
->Array
.VAO
->IndexBufferObj
,
703 case GL_UNSIGNED_BYTE
:
705 const GLubyte
*us
= (const GLubyte
*) map
;
707 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
708 printf("%02x ", us
[i
]);
715 case GL_UNSIGNED_SHORT
:
717 const GLushort
*us
= (const GLushort
*) map
;
719 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
720 printf("%04x ", us
[i
]);
727 case GL_UNSIGNED_INT
:
729 const GLuint
*us
= (const GLuint
*) map
;
731 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
732 printf("%08x ", us
[i
]);
743 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
749 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
750 const GLvoid
*indices
)
755 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
757 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
760 if (skip_validated_draw(ctx
))
768 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
769 * Do the rendering for a glDrawElements or glDrawRangeElements call after
770 * we've validated buffer bounds, etc.
773 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
774 GLboolean index_bounds_valid
,
775 GLuint start
, GLuint end
,
776 GLsizei count
, GLenum type
,
777 const GLvoid
* indices
,
778 GLint basevertex
, GLuint numInstances
,
781 struct _mesa_index_buffer ib
;
782 struct _mesa_prim prim
;
784 if (!index_bounds_valid
) {
789 if (skip_draw_elements(ctx
, count
, indices
))
793 ib
.index_size
= sizeof_ib_type(type
);
794 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
802 prim
.basevertex
= basevertex
;
805 /* Need to give special consideration to rendering a range of
806 * indices starting somewhere above zero. Typically the
807 * application is issuing multiple DrawRangeElements() to draw
808 * successive primitives layed out linearly in the vertex arrays.
809 * Unless the vertex arrays are all in a VBO (or locked as with
810 * CVA), the OpenGL semantics imply that we need to re-read or
811 * re-upload the vertex data on each draw call.
813 * In the case of hardware tnl, we want to avoid starting the
814 * upload at zero, as it will mean every draw call uploads an
815 * increasing amount of not-used vertex data. Worse - in the
816 * software tnl module, all those vertices might be transformed and
817 * lit but never rendered.
819 * If we just upload or transform the vertices in start..end,
820 * however, the indices will be incorrect.
822 * At this level, we don't know exactly what the requirements of
823 * the backend are going to be, though it will likely boil down to
826 * 1) Do nothing, everything is in a VBO and is processed once
829 * 2) Adjust the indices and vertex arrays so that start becomes
832 * Rather than doing anything here, I'll provide a helper function
833 * for the latter case elsewhere.
836 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
837 index_bounds_valid
, start
, end
,
838 numInstances
, baseInstance
, NULL
, 0);
840 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
847 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
850 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
851 GLsizei count
, GLenum type
,
852 const GLvoid
* indices
, GLint basevertex
)
854 static GLuint warnCount
= 0;
855 GLboolean index_bounds_valid
= GL_TRUE
;
857 /* This is only useful to catch invalid values in the "end" parameter
860 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
862 GET_CURRENT_CONTEXT(ctx
);
864 if (MESA_VERBOSE
& VERBOSE_DRAW
)
866 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
867 _mesa_enum_to_string(mode
), start
, end
, count
,
868 _mesa_enum_to_string(type
), indices
, basevertex
);
872 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
874 if (_mesa_is_no_error_enabled(ctx
)) {
876 _mesa_update_state(ctx
);
878 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
883 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
884 /* The application requested we draw using a range of indices that's
885 * outside the bounds of the current VBO. This is invalid and appears
886 * to give undefined results. The safest thing to do is to simply
887 * ignore the range, in case the application botched their range tracking
888 * but did provide valid indices. Also issue a warning indicating that
889 * the application is broken.
891 if (warnCount
++ < 10) {
892 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
893 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
894 "\trange is outside VBO bounds (max=%u); ignoring.\n"
895 "\tThis should be fixed in the application.",
896 start
, end
, basevertex
, count
, type
, indices
,
899 index_bounds_valid
= GL_FALSE
;
902 /* NOTE: It's important that 'end' is a reasonable value.
903 * in _tnl_draw_prims(), we use end to determine how many vertices
904 * to transform. If it's too large, we can unnecessarily split prims
905 * or we can read/write out of memory in several different places!
908 /* Catch/fix some potential user errors */
909 if (type
== GL_UNSIGNED_BYTE
) {
910 start
= MIN2(start
, 0xff);
911 end
= MIN2(end
, 0xff);
913 else if (type
== GL_UNSIGNED_SHORT
) {
914 start
= MIN2(start
, 0xffff);
915 end
= MIN2(end
, 0xffff);
919 printf("glDraw[Range]Elements{,BaseVertex}"
920 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
922 start
, end
, type
, count
,
923 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
926 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
927 index_bounds_valid
= GL_FALSE
;
930 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
932 (void) check_draw_elements_data
;
935 if (!index_bounds_valid
) {
940 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
941 count
, type
, indices
, basevertex
, 1, 0);
946 * Called by glDrawRangeElements() in immediate mode.
949 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
950 GLsizei count
, GLenum type
, const GLvoid
* indices
)
952 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
953 GET_CURRENT_CONTEXT(ctx
);
955 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
956 _mesa_enum_to_string(mode
), start
, end
, count
,
957 _mesa_enum_to_string(type
), indices
);
960 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
966 * Called by glDrawElements() in immediate mode.
969 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
970 const GLvoid
* indices
)
972 GET_CURRENT_CONTEXT(ctx
);
974 if (MESA_VERBOSE
& VERBOSE_DRAW
)
975 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
976 _mesa_enum_to_string(mode
), count
,
977 _mesa_enum_to_string(type
), indices
);
981 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
983 if (_mesa_is_no_error_enabled(ctx
)) {
985 _mesa_update_state(ctx
);
987 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
991 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
992 count
, type
, indices
, 0, 1, 0);
997 * Called by glDrawElementsBaseVertex() in immediate mode.
1000 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1001 const GLvoid
* indices
, GLint basevertex
)
1003 GET_CURRENT_CONTEXT(ctx
);
1005 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1006 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1007 _mesa_enum_to_string(mode
), count
,
1008 _mesa_enum_to_string(type
), indices
);
1010 FLUSH_FOR_DRAW(ctx
);
1012 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1014 if (_mesa_is_no_error_enabled(ctx
)) {
1016 _mesa_update_state(ctx
);
1018 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1022 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1023 count
, type
, indices
, basevertex
, 1, 0);
1028 * Called by glDrawElementsInstanced() in immediate mode.
1030 static void GLAPIENTRY
1031 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1032 const GLvoid
* indices
, GLsizei numInstances
)
1034 GET_CURRENT_CONTEXT(ctx
);
1036 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1037 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1038 _mesa_enum_to_string(mode
), count
,
1039 _mesa_enum_to_string(type
), indices
);
1041 FLUSH_FOR_DRAW(ctx
);
1043 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1045 if (_mesa_is_no_error_enabled(ctx
)) {
1047 _mesa_update_state(ctx
);
1049 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1050 indices
, numInstances
))
1054 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1055 count
, type
, indices
, 0, numInstances
, 0);
1060 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1062 static void GLAPIENTRY
1063 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1064 GLenum type
, const GLvoid
* indices
,
1065 GLsizei numInstances
,
1068 GET_CURRENT_CONTEXT(ctx
);
1070 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1072 "glDrawElementsInstancedBaseVertex"
1073 "(%s, %d, %s, %p, %d; %d)\n",
1074 _mesa_enum_to_string(mode
), count
,
1075 _mesa_enum_to_string(type
), indices
,
1076 numInstances
, basevertex
);
1078 FLUSH_FOR_DRAW(ctx
);
1080 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1082 if (_mesa_is_no_error_enabled(ctx
)) {
1084 _mesa_update_state(ctx
);
1086 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1087 indices
, numInstances
))
1091 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1092 count
, type
, indices
,
1093 basevertex
, numInstances
, 0);
1098 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1100 static void GLAPIENTRY
1101 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1103 const GLvoid
*indices
,
1104 GLsizei numInstances
,
1105 GLuint baseInstance
)
1107 GET_CURRENT_CONTEXT(ctx
);
1109 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1111 "glDrawElementsInstancedBaseInstance"
1112 "(%s, %d, %s, %p, %d, %d)\n",
1113 _mesa_enum_to_string(mode
), count
,
1114 _mesa_enum_to_string(type
), indices
,
1115 numInstances
, baseInstance
);
1117 FLUSH_FOR_DRAW(ctx
);
1119 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1121 if (_mesa_is_no_error_enabled(ctx
)) {
1123 _mesa_update_state(ctx
);
1125 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1126 indices
, numInstances
))
1130 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1131 count
, type
, indices
, 0, numInstances
,
1137 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1139 static void GLAPIENTRY
1140 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1143 const GLvoid
*indices
,
1144 GLsizei numInstances
,
1146 GLuint baseInstance
)
1148 GET_CURRENT_CONTEXT(ctx
);
1150 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1152 "glDrawElementsInstancedBaseVertexBaseInstance"
1153 "(%s, %d, %s, %p, %d, %d, %d)\n",
1154 _mesa_enum_to_string(mode
), count
,
1155 _mesa_enum_to_string(type
), indices
,
1156 numInstances
, basevertex
, baseInstance
);
1158 FLUSH_FOR_DRAW(ctx
);
1160 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1162 if (_mesa_is_no_error_enabled(ctx
)) {
1164 _mesa_update_state(ctx
);
1166 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1167 indices
, numInstances
))
1171 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1172 count
, type
, indices
, basevertex
,
1173 numInstances
, baseInstance
);
1178 * Inner support for both _mesa_MultiDrawElements() and
1179 * _mesa_MultiDrawRangeElements().
1180 * This does the actual rendering after we've checked array indexes, etc.
1183 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1184 const GLsizei
*count
, GLenum type
,
1185 const GLvoid
* const *indices
,
1186 GLsizei primcount
, const GLint
*basevertex
)
1188 struct _mesa_index_buffer ib
;
1189 unsigned int index_type_size
= sizeof_ib_type(type
);
1190 uintptr_t min_index_ptr
, max_index_ptr
;
1191 GLboolean fallback
= GL_FALSE
;
1197 min_index_ptr
= (uintptr_t) indices
[0];
1199 for (i
= 0; i
< primcount
; i
++) {
1200 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1201 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1202 index_type_size
* count
[i
]);
1205 /* Check if we can handle this thing as a bunch of index offsets from the
1206 * same index pointer. If we can't, then we have to fall back to doing
1207 * a draw_prims per primitive.
1208 * Check that the difference between each prim's indexes is a multiple of
1209 * the index/element size.
1211 if (index_type_size
!= 1) {
1212 for (i
= 0; i
< primcount
; i
++) {
1213 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1221 /* If the index buffer isn't in a VBO, then treating the application's
1222 * subranges of the index buffer as one large index buffer may lead to
1223 * us reading unmapped memory.
1225 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1229 struct _mesa_prim
*prim
;
1231 ALLOC_PRIMS(prim
, primcount
, "glMultiDrawElements");
1233 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1234 ib
.index_size
= sizeof_ib_type(type
);
1235 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1236 ib
.ptr
= (void *) min_index_ptr
;
1238 for (i
= 0; i
< primcount
; i
++) {
1241 prim
[i
].mode
= mode
;
1243 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1244 prim
[i
].count
= count
[i
];
1245 prim
[i
].draw_id
= i
;
1246 if (basevertex
!= NULL
)
1247 prim
[i
].basevertex
= basevertex
[i
];
1249 prim
[i
].basevertex
= 0;
1252 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1253 false, 0, ~0, 1, 0, NULL
, 0);
1254 FREE_PRIMS(prim
, primcount
);
1257 /* render one prim at a time */
1258 for (i
= 0; i
< primcount
; i
++) {
1262 ib
.count
= count
[i
];
1263 ib
.index_size
= sizeof_ib_type(type
);
1264 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1265 ib
.ptr
= indices
[i
];
1267 struct _mesa_prim prim
;
1272 prim
.count
= count
[i
];
1274 if (basevertex
!= NULL
)
1275 prim
.basevertex
= basevertex
[i
];
1277 prim
.basevertex
= 0;
1279 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
, false, 0, ~0, 1, 0, NULL
, 0);
1283 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1290 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1291 const GLvoid
* const *indices
, GLsizei primcount
)
1293 GET_CURRENT_CONTEXT(ctx
);
1295 FLUSH_FOR_DRAW(ctx
);
1297 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1299 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1303 if (skip_validated_draw(ctx
))
1306 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1312 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1313 const GLsizei
*count
, GLenum type
,
1314 const GLvoid
* const *indices
,
1316 const GLsizei
*basevertex
)
1318 GET_CURRENT_CONTEXT(ctx
);
1320 FLUSH_FOR_DRAW(ctx
);
1322 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1324 if (_mesa_is_no_error_enabled(ctx
)) {
1326 _mesa_update_state(ctx
);
1328 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1333 if (skip_validated_draw(ctx
))
1336 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1342 * Draw a GL primitive using a vertex count obtained from transform feedback.
1343 * \param mode the type of GL primitive to draw
1344 * \param obj the transform feedback object to use
1345 * \param stream index of the transform feedback stream from which to
1346 * get the primitive count.
1347 * \param numInstances number of instances to draw
1350 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1351 struct gl_transform_feedback_object
*obj
,
1352 GLuint stream
, GLuint numInstances
)
1354 struct _mesa_prim prim
;
1356 FLUSH_FOR_DRAW(ctx
);
1358 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1360 if (_mesa_is_no_error_enabled(ctx
)) {
1362 _mesa_update_state(ctx
);
1364 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1370 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1371 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1372 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1374 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1375 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1379 if (skip_validated_draw(ctx
))
1382 /* init most fields to zero */
1383 memset(&prim
, 0, sizeof(prim
));
1388 /* Maybe we should do some primitive splitting for primitive restart
1389 * (like in DrawArrays), but we have no way to know how many vertices
1390 * will be rendered. */
1392 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, numInstances
, 0,
1395 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1402 * Like DrawArrays, but take the count from a transform feedback object.
1403 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1404 * \param name the transform feedback object
1405 * User still has to setup of the vertex attribute info with
1406 * glVertexPointer, glColorPointer, etc.
1407 * Part of GL_ARB_transform_feedback2.
1410 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1412 GET_CURRENT_CONTEXT(ctx
);
1413 struct gl_transform_feedback_object
*obj
=
1414 _mesa_lookup_transform_feedback_object(ctx
, name
);
1416 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1417 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1418 _mesa_enum_to_string(mode
), name
);
1420 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1424 static void GLAPIENTRY
1425 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1427 GET_CURRENT_CONTEXT(ctx
);
1428 struct gl_transform_feedback_object
*obj
=
1429 _mesa_lookup_transform_feedback_object(ctx
, name
);
1431 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1432 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1433 _mesa_enum_to_string(mode
), name
, stream
);
1435 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1439 static void GLAPIENTRY
1440 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1443 GET_CURRENT_CONTEXT(ctx
);
1444 struct gl_transform_feedback_object
*obj
=
1445 _mesa_lookup_transform_feedback_object(ctx
, name
);
1447 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1448 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1449 _mesa_enum_to_string(mode
), name
);
1451 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1455 static void GLAPIENTRY
1456 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1460 GET_CURRENT_CONTEXT(ctx
);
1461 struct gl_transform_feedback_object
*obj
=
1462 _mesa_lookup_transform_feedback_object(ctx
, name
);
1464 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1465 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1466 "(%s, %u, %u, %i)\n",
1467 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1469 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1474 _mesa_validated_drawarraysindirect(struct gl_context
*ctx
,
1475 GLenum mode
, const GLvoid
*indirect
)
1477 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1478 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1479 1 /* draw_count */ , 16 /* stride */ ,
1482 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1488 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1490 const GLvoid
*indirect
,
1491 GLsizei primcount
, GLsizei stride
)
1493 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1498 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1499 primcount
, stride
, NULL
, 0, NULL
);
1501 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1507 _mesa_validated_drawelementsindirect(struct gl_context
*ctx
,
1508 GLenum mode
, GLenum type
,
1509 const GLvoid
*indirect
)
1511 struct _mesa_index_buffer ib
;
1513 ib
.count
= 0; /* unknown */
1514 ib
.index_size
= sizeof_ib_type(type
);
1515 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1518 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1519 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1520 1 /* draw_count */ , 20 /* stride */ ,
1523 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1529 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1530 GLenum mode
, GLenum type
,
1531 const GLvoid
*indirect
,
1532 GLsizei primcount
, GLsizei stride
)
1534 struct _mesa_index_buffer ib
;
1535 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1540 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1542 ib
.count
= 0; /* unknown */
1543 ib
.index_size
= sizeof_ib_type(type
);
1544 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1547 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1548 ctx
->DrawIndirectBuffer
, offset
,
1549 primcount
, stride
, NULL
, 0, &ib
);
1551 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1557 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1560 static void GLAPIENTRY
1561 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1563 GET_CURRENT_CONTEXT(ctx
);
1565 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1566 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1567 _mesa_enum_to_string(mode
), indirect
);
1569 /* From the ARB_draw_indirect spec:
1571 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1572 * compatibility profile, this indicates that DrawArraysIndirect and
1573 * DrawElementsIndirect are to source their arguments directly from the
1574 * pointer passed as their <indirect> parameters."
1576 if (ctx
->API
== API_OPENGL_COMPAT
&&
1577 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1578 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1580 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1586 FLUSH_FOR_DRAW(ctx
);
1588 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1590 if (_mesa_is_no_error_enabled(ctx
)) {
1592 _mesa_update_state(ctx
);
1594 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1598 if (skip_validated_draw(ctx
))
1601 _mesa_validated_drawarraysindirect(ctx
, mode
, indirect
);
1605 static void GLAPIENTRY
1606 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1608 GET_CURRENT_CONTEXT(ctx
);
1610 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1611 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1612 _mesa_enum_to_string(mode
),
1613 _mesa_enum_to_string(type
), indirect
);
1615 /* From the ARB_draw_indirect spec:
1617 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1618 * compatibility profile, this indicates that DrawArraysIndirect and
1619 * DrawElementsIndirect are to source their arguments directly from the
1620 * pointer passed as their <indirect> parameters."
1622 if (ctx
->API
== API_OPENGL_COMPAT
&&
1623 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1625 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1626 * may not come from a client array and must come from an index buffer.
1627 * If no element array buffer is bound, an INVALID_OPERATION error is
1630 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1631 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1632 "glDrawElementsIndirect(no buffer bound "
1633 "to GL_ELEMENT_ARRAY_BUFFER)");
1635 DrawElementsIndirectCommand
*cmd
=
1636 (DrawElementsIndirectCommand
*) indirect
;
1638 /* Convert offset to pointer */
1639 void *offset
= (void *)
1640 (uintptr_t)((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1642 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1652 FLUSH_FOR_DRAW(ctx
);
1654 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1656 if (_mesa_is_no_error_enabled(ctx
)) {
1658 _mesa_update_state(ctx
);
1660 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1664 if (skip_validated_draw(ctx
))
1667 _mesa_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1671 static void GLAPIENTRY
1672 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1673 GLsizei primcount
, GLsizei stride
)
1675 GET_CURRENT_CONTEXT(ctx
);
1677 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1678 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1679 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1681 /* If <stride> is zero, the array elements are treated as tightly packed. */
1683 stride
= sizeof(DrawArraysIndirectCommand
);
1685 /* From the ARB_draw_indirect spec:
1687 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1688 * compatibility profile, this indicates that DrawArraysIndirect and
1689 * DrawElementsIndirect are to source their arguments directly from the
1690 * pointer passed as their <indirect> parameters."
1692 if (ctx
->API
== API_OPENGL_COMPAT
&&
1693 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1695 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1696 "glMultiDrawArraysIndirect"))
1699 const uint8_t *ptr
= (const uint8_t *) indirect
;
1700 for (unsigned i
= 0; i
< primcount
; i
++) {
1701 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1702 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1703 cmd
->count
, cmd
->primCount
,
1707 ptr
+= sizeof(DrawArraysIndirectCommand
);
1716 FLUSH_FOR_DRAW(ctx
);
1718 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1720 if (_mesa_is_no_error_enabled(ctx
)) {
1722 _mesa_update_state(ctx
);
1724 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1729 if (skip_validated_draw(ctx
))
1732 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1737 static void GLAPIENTRY
1738 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1739 const GLvoid
*indirect
,
1740 GLsizei primcount
, GLsizei stride
)
1742 GET_CURRENT_CONTEXT(ctx
);
1744 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1745 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1746 _mesa_enum_to_string(mode
),
1747 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1749 /* If <stride> is zero, the array elements are treated as tightly packed. */
1751 stride
= sizeof(DrawElementsIndirectCommand
);
1754 /* From the ARB_draw_indirect spec:
1756 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1757 * compatibility profile, this indicates that DrawArraysIndirect and
1758 * DrawElementsIndirect are to source their arguments directly from the
1759 * pointer passed as their <indirect> parameters."
1761 if (ctx
->API
== API_OPENGL_COMPAT
&&
1762 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1764 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1765 * may not come from a client array and must come from an index buffer.
1766 * If no element array buffer is bound, an INVALID_OPERATION error is
1769 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1770 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1771 "glMultiDrawElementsIndirect(no buffer bound "
1772 "to GL_ELEMENT_ARRAY_BUFFER)");
1777 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1778 "glMultiDrawArraysIndirect"))
1781 const uint8_t *ptr
= (const uint8_t *) indirect
;
1782 for (unsigned i
= 0; i
< primcount
; i
++) {
1783 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1786 ptr
+= sizeof(DrawElementsIndirectCommand
);
1795 FLUSH_FOR_DRAW(ctx
);
1797 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1799 if (_mesa_is_no_error_enabled(ctx
)) {
1801 _mesa_update_state(ctx
);
1803 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1808 if (skip_validated_draw(ctx
))
1811 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1817 _mesa_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1820 GLintptr drawcount_offset
,
1821 GLsizei maxdrawcount
,
1824 GLsizeiptr offset
= indirect
;
1826 if (maxdrawcount
== 0)
1829 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1830 ctx
->DrawIndirectBuffer
, offset
,
1831 maxdrawcount
, stride
,
1832 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1834 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1840 _mesa_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1841 GLenum mode
, GLenum type
,
1843 GLintptr drawcount_offset
,
1844 GLsizei maxdrawcount
,
1847 struct _mesa_index_buffer ib
;
1848 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1850 if (maxdrawcount
== 0)
1853 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1855 ib
.count
= 0; /* unknown */
1856 ib
.index_size
= sizeof_ib_type(type
);
1857 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1860 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1861 ctx
->DrawIndirectBuffer
, offset
,
1862 maxdrawcount
, stride
,
1863 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1865 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1870 static void GLAPIENTRY
1871 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1872 GLintptr drawcount_offset
,
1873 GLsizei maxdrawcount
, GLsizei stride
)
1875 GET_CURRENT_CONTEXT(ctx
);
1877 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1878 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1879 "(%s, %lx, %lx, %i, %i)\n",
1880 _mesa_enum_to_string(mode
),
1881 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1882 maxdrawcount
, stride
);
1884 /* If <stride> is zero, the array elements are treated as tightly packed. */
1886 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1888 FLUSH_FOR_DRAW(ctx
);
1890 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1892 if (_mesa_is_no_error_enabled(ctx
)) {
1894 _mesa_update_state(ctx
);
1896 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1899 maxdrawcount
, stride
))
1903 if (skip_validated_draw(ctx
))
1906 _mesa_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1908 maxdrawcount
, stride
);
1912 static void GLAPIENTRY
1913 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1915 GLintptr drawcount_offset
,
1916 GLsizei maxdrawcount
, GLsizei stride
)
1918 GET_CURRENT_CONTEXT(ctx
);
1920 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1921 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1922 "(%s, %s, %lx, %lx, %i, %i)\n",
1923 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1924 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1925 maxdrawcount
, stride
);
1927 /* If <stride> is zero, the array elements are treated as tightly packed. */
1929 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1931 FLUSH_FOR_DRAW(ctx
);
1933 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1935 if (_mesa_is_no_error_enabled(ctx
)) {
1937 _mesa_update_state(ctx
);
1939 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1942 maxdrawcount
, stride
))
1946 if (skip_validated_draw(ctx
))
1949 _mesa_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1950 drawcount_offset
, maxdrawcount
,
1956 * Initialize the dispatch table with the VBO functions for drawing.
1959 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1960 struct _glapi_table
*exec
)
1962 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1963 SET_DrawElements(exec
, _mesa_DrawElements
);
1965 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1966 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
1969 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
1970 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
1972 if (ctx
->API
== API_OPENGL_COMPAT
) {
1973 SET_Rectf(exec
, _mesa_exec_Rectf
);
1974 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
1975 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
1978 if (ctx
->API
!= API_OPENGLES
&&
1979 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1980 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
1981 SET_MultiDrawElementsBaseVertex(exec
,
1982 _mesa_MultiDrawElementsBaseVertex
);
1984 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1985 SET_DrawRangeElementsBaseVertex(exec
,
1986 _mesa_DrawRangeElementsBaseVertex
);
1987 SET_DrawElementsInstancedBaseVertex(exec
,
1988 _mesa_exec_DrawElementsInstancedBaseVertex
);
1992 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1993 SET_DrawArraysInstancedBaseInstance(exec
,
1994 _mesa_exec_DrawArraysInstancedBaseInstance
);
1995 SET_DrawElementsInstancedBaseInstance(exec
,
1996 _mesa_exec_DrawElementsInstancedBaseInstance
);
1997 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1998 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
2001 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
2002 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
2003 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
2006 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2007 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
2008 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
2011 if (_mesa_is_desktop_gl(ctx
)) {
2012 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
2013 SET_DrawTransformFeedbackStream(exec
,
2014 _mesa_exec_DrawTransformFeedbackStream
);
2015 SET_DrawTransformFeedbackInstanced(exec
,
2016 _mesa_exec_DrawTransformFeedbackInstanced
);
2017 SET_DrawTransformFeedbackStreamInstanced(exec
,
2018 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
2019 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
2020 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
2021 SET_MultiDrawArraysIndirectCountARB(exec
,
2022 _mesa_exec_MultiDrawArraysIndirectCount
);
2023 SET_MultiDrawElementsIndirectCountARB(exec
,
2024 _mesa_exec_MultiDrawElementsIndirectCount
);
2030 /* GL_IBM_multimode_draw_arrays */
2032 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
2033 const GLsizei
* count
,
2034 GLsizei primcount
, GLint modestride
)
2036 GET_CURRENT_CONTEXT(ctx
);
2039 FLUSH_VERTICES(ctx
, 0);
2041 for ( i
= 0 ; i
< primcount
; i
++ ) {
2042 if ( count
[i
] > 0 ) {
2043 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2044 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
2050 /* GL_IBM_multimode_draw_arrays */
2052 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
2053 GLenum type
, const GLvoid
* const * indices
,
2054 GLsizei primcount
, GLint modestride
)
2056 GET_CURRENT_CONTEXT(ctx
);
2059 FLUSH_VERTICES(ctx
, 0);
2061 /* XXX not sure about ARB_vertex_buffer_object handling here */
2063 for ( i
= 0 ; i
< primcount
; i
++ ) {
2064 if ( count
[i
] > 0 ) {
2065 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2066 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,