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 struct _mesa_prim prim
;
357 if (skip_validated_draw(ctx
))
360 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
363 memset(&prim
, 0, sizeof(prim
));
367 prim
.num_instances
= numInstances
;
368 prim
.base_instance
= baseInstance
;
369 prim
.draw_id
= drawID
;
370 prim
.is_indirect
= 0;
374 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
375 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
377 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
384 * Execute a glRectf() function.
386 static void GLAPIENTRY
387 _mesa_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
389 GET_CURRENT_CONTEXT(ctx
);
390 ASSERT_OUTSIDE_BEGIN_END(ctx
);
392 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
393 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
394 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
395 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
396 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
397 CALL_End(GET_DISPATCH(), ());
401 static void GLAPIENTRY
402 _mesa_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
404 GET_CURRENT_CONTEXT(ctx
);
414 prim
= GL_LINE_STRIP
;
417 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
421 /* No effect if vertex maps disabled.
423 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
426 du
= ctx
->Eval
.MapGrid1du
;
427 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
429 CALL_Begin(GET_DISPATCH(), (prim
));
430 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
431 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
433 CALL_End(GET_DISPATCH(), ());
437 static void GLAPIENTRY
438 _mesa_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
440 GET_CURRENT_CONTEXT(ctx
);
441 GLfloat u
, du
, v
, dv
, v1
, u1
;
450 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
454 /* No effect if vertex maps disabled.
456 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
459 du
= ctx
->Eval
.MapGrid2du
;
460 dv
= ctx
->Eval
.MapGrid2dv
;
461 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
462 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
466 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
467 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
468 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
469 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
472 CALL_End(GET_DISPATCH(), ());
475 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
476 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
477 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
478 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
480 CALL_End(GET_DISPATCH(), ());
482 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
483 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
484 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
485 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
487 CALL_End(GET_DISPATCH(), ());
491 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
492 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
493 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
494 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
495 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
497 CALL_End(GET_DISPATCH(), ());
505 * Called from glDrawArrays when in immediate mode (not display list mode).
508 _mesa_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
510 GET_CURRENT_CONTEXT(ctx
);
512 if (MESA_VERBOSE
& VERBOSE_DRAW
)
513 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
514 _mesa_enum_to_string(mode
), start
, count
);
518 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
520 if (_mesa_is_no_error_enabled(ctx
)) {
522 _mesa_update_state(ctx
);
524 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
529 check_draw_arrays_data(ctx
, start
, count
);
531 _mesa_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
534 print_draw_arrays(ctx
, mode
, start
, count
);
539 * Called from glDrawArraysInstanced when in immediate mode (not
540 * display list mode).
543 _mesa_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
544 GLsizei numInstances
)
546 GET_CURRENT_CONTEXT(ctx
);
548 if (MESA_VERBOSE
& VERBOSE_DRAW
)
549 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
550 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
554 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
556 if (_mesa_is_no_error_enabled(ctx
)) {
558 _mesa_update_state(ctx
);
560 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
566 check_draw_arrays_data(ctx
, start
, count
);
568 _mesa_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
571 print_draw_arrays(ctx
, mode
, start
, count
);
576 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
578 static void GLAPIENTRY
579 _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
580 GLsizei count
, GLsizei numInstances
,
583 GET_CURRENT_CONTEXT(ctx
);
585 if (MESA_VERBOSE
& VERBOSE_DRAW
)
587 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
588 _mesa_enum_to_string(mode
), first
, count
,
589 numInstances
, baseInstance
);
593 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
595 if (_mesa_is_no_error_enabled(ctx
)) {
597 _mesa_update_state(ctx
);
599 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
605 check_draw_arrays_data(ctx
, first
, count
);
607 _mesa_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
610 print_draw_arrays(ctx
, mode
, first
, count
);
615 * Called from glMultiDrawArrays when in immediate mode.
617 static void GLAPIENTRY
618 _mesa_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
619 const GLsizei
*count
, GLsizei primcount
)
621 GET_CURRENT_CONTEXT(ctx
);
624 if (MESA_VERBOSE
& VERBOSE_DRAW
)
626 "glMultiDrawArrays(%s, %p, %p, %d)\n",
627 _mesa_enum_to_string(mode
), first
, count
, primcount
);
631 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
633 if (_mesa_is_no_error_enabled(ctx
)) {
635 _mesa_update_state(ctx
);
637 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
641 for (i
= 0; i
< primcount
; i
++) {
644 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
646 /* The GL_ARB_shader_draw_parameters spec adds the following after the
647 * pseudo-code describing glMultiDrawArrays:
649 * "The index of the draw (<i> in the above pseudo-code) may be
650 * read by a vertex shader as <gl_DrawIDARB>, as described in
653 _mesa_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
656 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
664 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
669 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
672 ctx
->Driver
.MapBufferRange(ctx
, 0,
673 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
675 ctx
->Array
.VAO
->IndexBufferObj
,
678 case GL_UNSIGNED_BYTE
:
680 const GLubyte
*us
= (const GLubyte
*) map
;
682 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
683 printf("%02x ", us
[i
]);
690 case GL_UNSIGNED_SHORT
:
692 const GLushort
*us
= (const GLushort
*) map
;
694 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
695 printf("%04x ", us
[i
]);
702 case GL_UNSIGNED_INT
:
704 const GLuint
*us
= (const GLuint
*) map
;
706 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
707 printf("%08x ", us
[i
]);
718 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
724 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
725 const GLvoid
*indices
)
730 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
732 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
735 if (skip_validated_draw(ctx
))
743 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
744 * Do the rendering for a glDrawElements or glDrawRangeElements call after
745 * we've validated buffer bounds, etc.
748 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
749 GLboolean index_bounds_valid
,
750 GLuint start
, GLuint end
,
751 GLsizei count
, GLenum type
,
752 const GLvoid
* indices
,
753 GLint basevertex
, GLuint numInstances
,
756 struct _mesa_index_buffer ib
;
757 struct _mesa_prim prim
;
759 if (!index_bounds_valid
) {
764 if (skip_draw_elements(ctx
, count
, indices
))
768 ib
.index_size
= sizeof_ib_type(type
);
769 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
779 prim
.is_indirect
= 0;
780 prim
.basevertex
= basevertex
;
781 prim
.num_instances
= numInstances
;
782 prim
.base_instance
= baseInstance
;
785 /* Need to give special consideration to rendering a range of
786 * indices starting somewhere above zero. Typically the
787 * application is issuing multiple DrawRangeElements() to draw
788 * successive primitives layed out linearly in the vertex arrays.
789 * Unless the vertex arrays are all in a VBO (or locked as with
790 * CVA), the OpenGL semantics imply that we need to re-read or
791 * re-upload the vertex data on each draw call.
793 * In the case of hardware tnl, we want to avoid starting the
794 * upload at zero, as it will mean every draw call uploads an
795 * increasing amount of not-used vertex data. Worse - in the
796 * software tnl module, all those vertices might be transformed and
797 * lit but never rendered.
799 * If we just upload or transform the vertices in start..end,
800 * however, the indices will be incorrect.
802 * At this level, we don't know exactly what the requirements of
803 * the backend are going to be, though it will likely boil down to
806 * 1) Do nothing, everything is in a VBO and is processed once
809 * 2) Adjust the indices and vertex arrays so that start becomes
812 * Rather than doing anything here, I'll provide a helper function
813 * for the latter case elsewhere.
816 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
817 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
819 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
826 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
829 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
830 GLsizei count
, GLenum type
,
831 const GLvoid
* indices
, GLint basevertex
)
833 static GLuint warnCount
= 0;
834 GLboolean index_bounds_valid
= GL_TRUE
;
836 /* This is only useful to catch invalid values in the "end" parameter
839 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
841 GET_CURRENT_CONTEXT(ctx
);
843 if (MESA_VERBOSE
& VERBOSE_DRAW
)
845 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
846 _mesa_enum_to_string(mode
), start
, end
, count
,
847 _mesa_enum_to_string(type
), indices
, basevertex
);
851 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
853 if (_mesa_is_no_error_enabled(ctx
)) {
855 _mesa_update_state(ctx
);
857 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
862 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
863 /* The application requested we draw using a range of indices that's
864 * outside the bounds of the current VBO. This is invalid and appears
865 * to give undefined results. The safest thing to do is to simply
866 * ignore the range, in case the application botched their range tracking
867 * but did provide valid indices. Also issue a warning indicating that
868 * the application is broken.
870 if (warnCount
++ < 10) {
871 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
872 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
873 "\trange is outside VBO bounds (max=%u); ignoring.\n"
874 "\tThis should be fixed in the application.",
875 start
, end
, basevertex
, count
, type
, indices
,
878 index_bounds_valid
= GL_FALSE
;
881 /* NOTE: It's important that 'end' is a reasonable value.
882 * in _tnl_draw_prims(), we use end to determine how many vertices
883 * to transform. If it's too large, we can unnecessarily split prims
884 * or we can read/write out of memory in several different places!
887 /* Catch/fix some potential user errors */
888 if (type
== GL_UNSIGNED_BYTE
) {
889 start
= MIN2(start
, 0xff);
890 end
= MIN2(end
, 0xff);
892 else if (type
== GL_UNSIGNED_SHORT
) {
893 start
= MIN2(start
, 0xffff);
894 end
= MIN2(end
, 0xffff);
898 printf("glDraw[Range]Elements{,BaseVertex}"
899 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
901 start
, end
, type
, count
,
902 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
905 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
906 index_bounds_valid
= GL_FALSE
;
909 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
911 (void) check_draw_elements_data
;
914 if (!index_bounds_valid
) {
919 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
920 count
, type
, indices
, basevertex
, 1, 0);
925 * Called by glDrawRangeElements() in immediate mode.
928 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
929 GLsizei count
, GLenum type
, const GLvoid
* indices
)
931 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
932 GET_CURRENT_CONTEXT(ctx
);
934 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
935 _mesa_enum_to_string(mode
), start
, end
, count
,
936 _mesa_enum_to_string(type
), indices
);
939 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
945 * Called by glDrawElements() in immediate mode.
948 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
949 const GLvoid
* indices
)
951 GET_CURRENT_CONTEXT(ctx
);
953 if (MESA_VERBOSE
& VERBOSE_DRAW
)
954 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
955 _mesa_enum_to_string(mode
), count
,
956 _mesa_enum_to_string(type
), indices
);
960 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
962 if (_mesa_is_no_error_enabled(ctx
)) {
964 _mesa_update_state(ctx
);
966 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
970 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
971 count
, type
, indices
, 0, 1, 0);
976 * Called by glDrawElementsBaseVertex() in immediate mode.
979 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
980 const GLvoid
* indices
, GLint basevertex
)
982 GET_CURRENT_CONTEXT(ctx
);
984 if (MESA_VERBOSE
& VERBOSE_DRAW
)
985 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
986 _mesa_enum_to_string(mode
), count
,
987 _mesa_enum_to_string(type
), indices
);
991 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
993 if (_mesa_is_no_error_enabled(ctx
)) {
995 _mesa_update_state(ctx
);
997 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1001 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1002 count
, type
, indices
, basevertex
, 1, 0);
1007 * Called by glDrawElementsInstanced() in immediate mode.
1009 static void GLAPIENTRY
1010 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1011 const GLvoid
* indices
, GLsizei numInstances
)
1013 GET_CURRENT_CONTEXT(ctx
);
1015 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1016 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1017 _mesa_enum_to_string(mode
), count
,
1018 _mesa_enum_to_string(type
), indices
);
1020 FLUSH_FOR_DRAW(ctx
);
1022 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1024 if (_mesa_is_no_error_enabled(ctx
)) {
1026 _mesa_update_state(ctx
);
1028 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1029 indices
, numInstances
))
1033 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1034 count
, type
, indices
, 0, numInstances
, 0);
1039 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1041 static void GLAPIENTRY
1042 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1043 GLenum type
, const GLvoid
* indices
,
1044 GLsizei numInstances
,
1047 GET_CURRENT_CONTEXT(ctx
);
1049 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1051 "glDrawElementsInstancedBaseVertex"
1052 "(%s, %d, %s, %p, %d; %d)\n",
1053 _mesa_enum_to_string(mode
), count
,
1054 _mesa_enum_to_string(type
), indices
,
1055 numInstances
, basevertex
);
1057 FLUSH_FOR_DRAW(ctx
);
1059 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1061 if (_mesa_is_no_error_enabled(ctx
)) {
1063 _mesa_update_state(ctx
);
1065 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1066 indices
, numInstances
))
1070 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1071 count
, type
, indices
,
1072 basevertex
, numInstances
, 0);
1077 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1079 static void GLAPIENTRY
1080 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1082 const GLvoid
*indices
,
1083 GLsizei numInstances
,
1084 GLuint baseInstance
)
1086 GET_CURRENT_CONTEXT(ctx
);
1088 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1090 "glDrawElementsInstancedBaseInstance"
1091 "(%s, %d, %s, %p, %d, %d)\n",
1092 _mesa_enum_to_string(mode
), count
,
1093 _mesa_enum_to_string(type
), indices
,
1094 numInstances
, baseInstance
);
1096 FLUSH_FOR_DRAW(ctx
);
1098 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1100 if (_mesa_is_no_error_enabled(ctx
)) {
1102 _mesa_update_state(ctx
);
1104 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1105 indices
, numInstances
))
1109 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1110 count
, type
, indices
, 0, numInstances
,
1116 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1118 static void GLAPIENTRY
1119 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1122 const GLvoid
*indices
,
1123 GLsizei numInstances
,
1125 GLuint baseInstance
)
1127 GET_CURRENT_CONTEXT(ctx
);
1129 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1131 "glDrawElementsInstancedBaseVertexBaseInstance"
1132 "(%s, %d, %s, %p, %d, %d, %d)\n",
1133 _mesa_enum_to_string(mode
), count
,
1134 _mesa_enum_to_string(type
), indices
,
1135 numInstances
, basevertex
, baseInstance
);
1137 FLUSH_FOR_DRAW(ctx
);
1139 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1141 if (_mesa_is_no_error_enabled(ctx
)) {
1143 _mesa_update_state(ctx
);
1145 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1146 indices
, numInstances
))
1150 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1151 count
, type
, indices
, basevertex
,
1152 numInstances
, baseInstance
);
1157 * Inner support for both _mesa_MultiDrawElements() and
1158 * _mesa_MultiDrawRangeElements().
1159 * This does the actual rendering after we've checked array indexes, etc.
1162 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1163 const GLsizei
*count
, GLenum type
,
1164 const GLvoid
* const *indices
,
1165 GLsizei primcount
, const GLint
*basevertex
)
1167 struct _mesa_index_buffer ib
;
1168 struct _mesa_prim
*prim
;
1169 unsigned int index_type_size
= sizeof_ib_type(type
);
1170 uintptr_t min_index_ptr
, max_index_ptr
;
1171 GLboolean fallback
= GL_FALSE
;
1177 prim
= calloc(primcount
, sizeof(*prim
));
1179 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1183 min_index_ptr
= (uintptr_t) indices
[0];
1185 for (i
= 0; i
< primcount
; i
++) {
1186 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1187 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1188 index_type_size
* count
[i
]);
1191 /* Check if we can handle this thing as a bunch of index offsets from the
1192 * same index pointer. If we can't, then we have to fall back to doing
1193 * a draw_prims per primitive.
1194 * Check that the difference between each prim's indexes is a multiple of
1195 * the index/element size.
1197 if (index_type_size
!= 1) {
1198 for (i
= 0; i
< primcount
; i
++) {
1199 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1207 /* Draw primitives individually if one count is zero, so we can easily skip
1210 for (i
= 0; i
< primcount
; i
++) {
1211 if (count
[i
] == 0) {
1217 /* If the index buffer isn't in a VBO, then treating the application's
1218 * subranges of the index buffer as one large index buffer may lead to
1219 * us reading unmapped memory.
1221 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1225 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1226 ib
.index_size
= sizeof_ib_type(type
);
1227 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1228 ib
.ptr
= (void *) min_index_ptr
;
1230 for (i
= 0; i
< primcount
; i
++) {
1231 prim
[i
].begin
= (i
== 0);
1232 prim
[i
].end
= (i
== primcount
- 1);
1234 prim
[i
].mode
= mode
;
1236 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1237 prim
[i
].count
= count
[i
];
1238 prim
[i
].indexed
= 1;
1239 prim
[i
].num_instances
= 1;
1240 prim
[i
].base_instance
= 0;
1241 prim
[i
].draw_id
= i
;
1242 prim
[i
].is_indirect
= 0;
1243 if (basevertex
!= NULL
)
1244 prim
[i
].basevertex
= basevertex
[i
];
1246 prim
[i
].basevertex
= 0;
1249 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1250 false, 0, ~0, NULL
, 0, NULL
);
1253 /* render one prim at a time */
1254 for (i
= 0; i
< primcount
; i
++) {
1257 ib
.count
= count
[i
];
1258 ib
.index_size
= sizeof_ib_type(type
);
1259 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1260 ib
.ptr
= indices
[i
];
1265 prim
[0].mode
= mode
;
1267 prim
[0].count
= count
[i
];
1268 prim
[0].indexed
= 1;
1269 prim
[0].num_instances
= 1;
1270 prim
[0].base_instance
= 0;
1271 prim
[0].draw_id
= i
;
1272 prim
[0].is_indirect
= 0;
1273 if (basevertex
!= NULL
)
1274 prim
[0].basevertex
= basevertex
[i
];
1276 prim
[0].basevertex
= 0;
1278 ctx
->Driver
.Draw(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1284 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1291 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1292 const GLvoid
* const *indices
, GLsizei primcount
)
1294 GET_CURRENT_CONTEXT(ctx
);
1296 FLUSH_FOR_DRAW(ctx
);
1298 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1300 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1304 if (skip_validated_draw(ctx
))
1307 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1313 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1314 const GLsizei
*count
, GLenum type
,
1315 const GLvoid
* const *indices
,
1317 const GLsizei
*basevertex
)
1319 GET_CURRENT_CONTEXT(ctx
);
1321 FLUSH_FOR_DRAW(ctx
);
1323 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1325 if (_mesa_is_no_error_enabled(ctx
)) {
1327 _mesa_update_state(ctx
);
1329 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1334 if (skip_validated_draw(ctx
))
1337 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1343 * Draw a GL primitive using a vertex count obtained from transform feedback.
1344 * \param mode the type of GL primitive to draw
1345 * \param obj the transform feedback object to use
1346 * \param stream index of the transform feedback stream from which to
1347 * get the primitive count.
1348 * \param numInstances number of instances to draw
1351 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1352 struct gl_transform_feedback_object
*obj
,
1353 GLuint stream
, GLuint numInstances
)
1355 struct _mesa_prim prim
;
1357 FLUSH_FOR_DRAW(ctx
);
1359 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1361 if (_mesa_is_no_error_enabled(ctx
)) {
1363 _mesa_update_state(ctx
);
1365 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1371 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1372 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1373 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1375 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1376 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1380 if (skip_validated_draw(ctx
))
1383 /* init most fields to zero */
1384 memset(&prim
, 0, sizeof(prim
));
1388 prim
.num_instances
= numInstances
;
1389 prim
.base_instance
= 0;
1390 prim
.is_indirect
= 0;
1392 /* Maybe we should do some primitive splitting for primitive restart
1393 * (like in DrawArrays), but we have no way to know how many vertices
1394 * will be rendered. */
1396 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1398 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1405 * Like DrawArrays, but take the count from a transform feedback object.
1406 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1407 * \param name the transform feedback object
1408 * User still has to setup of the vertex attribute info with
1409 * glVertexPointer, glColorPointer, etc.
1410 * Part of GL_ARB_transform_feedback2.
1413 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1415 GET_CURRENT_CONTEXT(ctx
);
1416 struct gl_transform_feedback_object
*obj
=
1417 _mesa_lookup_transform_feedback_object(ctx
, name
);
1419 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1420 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1421 _mesa_enum_to_string(mode
), name
);
1423 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1427 static void GLAPIENTRY
1428 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1430 GET_CURRENT_CONTEXT(ctx
);
1431 struct gl_transform_feedback_object
*obj
=
1432 _mesa_lookup_transform_feedback_object(ctx
, name
);
1434 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1435 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1436 _mesa_enum_to_string(mode
), name
, stream
);
1438 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1442 static void GLAPIENTRY
1443 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1446 GET_CURRENT_CONTEXT(ctx
);
1447 struct gl_transform_feedback_object
*obj
=
1448 _mesa_lookup_transform_feedback_object(ctx
, name
);
1450 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1451 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1452 _mesa_enum_to_string(mode
), name
);
1454 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1458 static void GLAPIENTRY
1459 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1463 GET_CURRENT_CONTEXT(ctx
);
1464 struct gl_transform_feedback_object
*obj
=
1465 _mesa_lookup_transform_feedback_object(ctx
, name
);
1467 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1468 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1469 "(%s, %u, %u, %i)\n",
1470 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1472 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1477 _mesa_validated_drawarraysindirect(struct gl_context
*ctx
,
1478 GLenum mode
, const GLvoid
*indirect
)
1480 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1481 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1482 1 /* draw_count */ , 16 /* stride */ ,
1485 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1491 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1493 const GLvoid
*indirect
,
1494 GLsizei primcount
, GLsizei stride
)
1496 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1501 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1502 primcount
, stride
, NULL
, 0, NULL
);
1504 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1510 _mesa_validated_drawelementsindirect(struct gl_context
*ctx
,
1511 GLenum mode
, GLenum type
,
1512 const GLvoid
*indirect
)
1514 struct _mesa_index_buffer ib
;
1516 ib
.count
= 0; /* unknown */
1517 ib
.index_size
= sizeof_ib_type(type
);
1518 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1521 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1522 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1523 1 /* draw_count */ , 20 /* stride */ ,
1526 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1532 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1533 GLenum mode
, GLenum type
,
1534 const GLvoid
*indirect
,
1535 GLsizei primcount
, GLsizei stride
)
1537 struct _mesa_index_buffer ib
;
1538 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1543 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1545 ib
.count
= 0; /* unknown */
1546 ib
.index_size
= sizeof_ib_type(type
);
1547 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1550 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1551 ctx
->DrawIndirectBuffer
, offset
,
1552 primcount
, stride
, NULL
, 0, &ib
);
1554 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1560 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1563 static void GLAPIENTRY
1564 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1566 GET_CURRENT_CONTEXT(ctx
);
1568 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1569 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1570 _mesa_enum_to_string(mode
), indirect
);
1572 /* From the ARB_draw_indirect spec:
1574 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1575 * compatibility profile, this indicates that DrawArraysIndirect and
1576 * DrawElementsIndirect are to source their arguments directly from the
1577 * pointer passed as their <indirect> parameters."
1579 if (ctx
->API
== API_OPENGL_COMPAT
&&
1580 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1581 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1583 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1589 FLUSH_FOR_DRAW(ctx
);
1591 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1593 if (_mesa_is_no_error_enabled(ctx
)) {
1595 _mesa_update_state(ctx
);
1597 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1601 if (skip_validated_draw(ctx
))
1604 _mesa_validated_drawarraysindirect(ctx
, mode
, indirect
);
1608 static void GLAPIENTRY
1609 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1611 GET_CURRENT_CONTEXT(ctx
);
1613 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1614 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1615 _mesa_enum_to_string(mode
),
1616 _mesa_enum_to_string(type
), indirect
);
1618 /* From the ARB_draw_indirect spec:
1620 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1621 * compatibility profile, this indicates that DrawArraysIndirect and
1622 * DrawElementsIndirect are to source their arguments directly from the
1623 * pointer passed as their <indirect> parameters."
1625 if (ctx
->API
== API_OPENGL_COMPAT
&&
1626 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1628 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1629 * may not come from a client array and must come from an index buffer.
1630 * If no element array buffer is bound, an INVALID_OPERATION error is
1633 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1634 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1635 "glDrawElementsIndirect(no buffer bound "
1636 "to GL_ELEMENT_ARRAY_BUFFER)");
1638 DrawElementsIndirectCommand
*cmd
=
1639 (DrawElementsIndirectCommand
*) indirect
;
1641 /* Convert offset to pointer */
1642 void *offset
= (void *)
1643 (uintptr_t)((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1645 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1655 FLUSH_FOR_DRAW(ctx
);
1657 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1659 if (_mesa_is_no_error_enabled(ctx
)) {
1661 _mesa_update_state(ctx
);
1663 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1667 if (skip_validated_draw(ctx
))
1670 _mesa_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1674 static void GLAPIENTRY
1675 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1676 GLsizei primcount
, GLsizei stride
)
1678 GET_CURRENT_CONTEXT(ctx
);
1680 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1681 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1682 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1684 /* If <stride> is zero, the array elements are treated as tightly packed. */
1686 stride
= sizeof(DrawArraysIndirectCommand
);
1688 /* From the ARB_draw_indirect spec:
1690 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1691 * compatibility profile, this indicates that DrawArraysIndirect and
1692 * DrawElementsIndirect are to source their arguments directly from the
1693 * pointer passed as their <indirect> parameters."
1695 if (ctx
->API
== API_OPENGL_COMPAT
&&
1696 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1698 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1699 "glMultiDrawArraysIndirect"))
1702 const uint8_t *ptr
= (const uint8_t *) indirect
;
1703 for (unsigned i
= 0; i
< primcount
; i
++) {
1704 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1705 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1706 cmd
->count
, cmd
->primCount
,
1710 ptr
+= sizeof(DrawArraysIndirectCommand
);
1719 FLUSH_FOR_DRAW(ctx
);
1721 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1723 if (_mesa_is_no_error_enabled(ctx
)) {
1725 _mesa_update_state(ctx
);
1727 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1732 if (skip_validated_draw(ctx
))
1735 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1740 static void GLAPIENTRY
1741 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1742 const GLvoid
*indirect
,
1743 GLsizei primcount
, GLsizei stride
)
1745 GET_CURRENT_CONTEXT(ctx
);
1747 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1748 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1749 _mesa_enum_to_string(mode
),
1750 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1752 /* If <stride> is zero, the array elements are treated as tightly packed. */
1754 stride
= sizeof(DrawElementsIndirectCommand
);
1757 /* From the ARB_draw_indirect spec:
1759 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1760 * compatibility profile, this indicates that DrawArraysIndirect and
1761 * DrawElementsIndirect are to source their arguments directly from the
1762 * pointer passed as their <indirect> parameters."
1764 if (ctx
->API
== API_OPENGL_COMPAT
&&
1765 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1767 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1768 * may not come from a client array and must come from an index buffer.
1769 * If no element array buffer is bound, an INVALID_OPERATION error is
1772 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1774 "glMultiDrawElementsIndirect(no buffer bound "
1775 "to GL_ELEMENT_ARRAY_BUFFER)");
1780 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1781 "glMultiDrawArraysIndirect"))
1784 const uint8_t *ptr
= (const uint8_t *) indirect
;
1785 for (unsigned i
= 0; i
< primcount
; i
++) {
1786 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1789 ptr
+= sizeof(DrawElementsIndirectCommand
);
1798 FLUSH_FOR_DRAW(ctx
);
1800 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1802 if (_mesa_is_no_error_enabled(ctx
)) {
1804 _mesa_update_state(ctx
);
1806 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1811 if (skip_validated_draw(ctx
))
1814 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1820 _mesa_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1823 GLintptr drawcount_offset
,
1824 GLsizei maxdrawcount
,
1827 GLsizeiptr offset
= indirect
;
1829 if (maxdrawcount
== 0)
1832 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1833 ctx
->DrawIndirectBuffer
, offset
,
1834 maxdrawcount
, stride
,
1835 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1837 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1843 _mesa_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1844 GLenum mode
, GLenum type
,
1846 GLintptr drawcount_offset
,
1847 GLsizei maxdrawcount
,
1850 struct _mesa_index_buffer ib
;
1851 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1853 if (maxdrawcount
== 0)
1856 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1858 ib
.count
= 0; /* unknown */
1859 ib
.index_size
= sizeof_ib_type(type
);
1860 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1863 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1864 ctx
->DrawIndirectBuffer
, offset
,
1865 maxdrawcount
, stride
,
1866 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1868 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1873 static void GLAPIENTRY
1874 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1875 GLintptr drawcount_offset
,
1876 GLsizei maxdrawcount
, GLsizei stride
)
1878 GET_CURRENT_CONTEXT(ctx
);
1880 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1881 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1882 "(%s, %lx, %lx, %i, %i)\n",
1883 _mesa_enum_to_string(mode
),
1884 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1885 maxdrawcount
, stride
);
1887 /* If <stride> is zero, the array elements are treated as tightly packed. */
1889 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1891 FLUSH_FOR_DRAW(ctx
);
1893 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1895 if (_mesa_is_no_error_enabled(ctx
)) {
1897 _mesa_update_state(ctx
);
1899 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1902 maxdrawcount
, stride
))
1906 if (skip_validated_draw(ctx
))
1909 _mesa_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1911 maxdrawcount
, stride
);
1915 static void GLAPIENTRY
1916 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1918 GLintptr drawcount_offset
,
1919 GLsizei maxdrawcount
, GLsizei stride
)
1921 GET_CURRENT_CONTEXT(ctx
);
1923 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1924 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1925 "(%s, %s, %lx, %lx, %i, %i)\n",
1926 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1927 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1928 maxdrawcount
, stride
);
1930 /* If <stride> is zero, the array elements are treated as tightly packed. */
1932 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1934 FLUSH_FOR_DRAW(ctx
);
1936 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1938 if (_mesa_is_no_error_enabled(ctx
)) {
1940 _mesa_update_state(ctx
);
1942 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1945 maxdrawcount
, stride
))
1949 if (skip_validated_draw(ctx
))
1952 _mesa_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1953 drawcount_offset
, maxdrawcount
,
1959 * Initialize the dispatch table with the VBO functions for drawing.
1962 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1963 struct _glapi_table
*exec
)
1965 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1966 SET_DrawElements(exec
, _mesa_DrawElements
);
1968 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1969 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
1972 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
1973 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
1975 if (ctx
->API
== API_OPENGL_COMPAT
) {
1976 SET_Rectf(exec
, _mesa_exec_Rectf
);
1977 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
1978 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
1981 if (ctx
->API
!= API_OPENGLES
&&
1982 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1983 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
1984 SET_MultiDrawElementsBaseVertex(exec
,
1985 _mesa_MultiDrawElementsBaseVertex
);
1987 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1988 SET_DrawRangeElementsBaseVertex(exec
,
1989 _mesa_DrawRangeElementsBaseVertex
);
1990 SET_DrawElementsInstancedBaseVertex(exec
,
1991 _mesa_exec_DrawElementsInstancedBaseVertex
);
1995 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1996 SET_DrawArraysInstancedBaseInstance(exec
,
1997 _mesa_exec_DrawArraysInstancedBaseInstance
);
1998 SET_DrawElementsInstancedBaseInstance(exec
,
1999 _mesa_exec_DrawElementsInstancedBaseInstance
);
2000 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
2001 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
2004 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
2005 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
2006 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
2009 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2010 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
2011 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
2014 if (_mesa_is_desktop_gl(ctx
)) {
2015 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
2016 SET_DrawTransformFeedbackStream(exec
,
2017 _mesa_exec_DrawTransformFeedbackStream
);
2018 SET_DrawTransformFeedbackInstanced(exec
,
2019 _mesa_exec_DrawTransformFeedbackInstanced
);
2020 SET_DrawTransformFeedbackStreamInstanced(exec
,
2021 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
2022 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
2023 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
2024 SET_MultiDrawArraysIndirectCountARB(exec
,
2025 _mesa_exec_MultiDrawArraysIndirectCount
);
2026 SET_MultiDrawElementsIndirectCountARB(exec
,
2027 _mesa_exec_MultiDrawElementsIndirectCount
);
2033 /* GL_IBM_multimode_draw_arrays */
2035 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
2036 const GLsizei
* count
,
2037 GLsizei primcount
, GLint modestride
)
2039 GET_CURRENT_CONTEXT(ctx
);
2042 FLUSH_VERTICES(ctx
, 0);
2044 for ( i
= 0 ; i
< primcount
; i
++ ) {
2045 if ( count
[i
] > 0 ) {
2046 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2047 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
2053 /* GL_IBM_multimode_draw_arrays */
2055 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
2056 GLenum type
, const GLvoid
* const * indices
,
2057 GLsizei primcount
, GLint modestride
)
2059 GET_CURRENT_CONTEXT(ctx
);
2062 FLUSH_VERTICES(ctx
, 0);
2064 /* XXX not sure about ARB_vertex_buffer_object handling here */
2066 for ( i
= 0 ; i
< primcount
; i
++ ) {
2067 if ( count
[i
] > 0 ) {
2068 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2069 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,
2077 * Helper function for _mesa_draw_indirect below that additionally takes a zero
2078 * initialized array of _mesa_prim scratch space memory as the last argument.
2081 draw_indirect(struct gl_context
*ctx
, GLuint mode
,
2082 struct gl_buffer_object
*indirect_data
,
2083 GLsizeiptr indirect_offset
, unsigned draw_count
,
2085 struct gl_buffer_object
*indirect_draw_count_buffer
,
2086 GLsizeiptr indirect_draw_count_offset
,
2087 const struct _mesa_index_buffer
*ib
,
2088 struct _mesa_prim
*prim
)
2091 prim
[draw_count
- 1].end
= 1;
2092 for (unsigned i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
2093 prim
[i
].mode
= mode
;
2094 prim
[i
].indexed
= !!ib
;
2095 prim
[i
].indirect_offset
= indirect_offset
;
2096 prim
[i
].is_indirect
= 1;
2097 prim
[i
].draw_id
= i
;
2100 /* This should always be true at this time */
2101 assert(indirect_data
== ctx
->DrawIndirectBuffer
);
2103 ctx
->Driver
.Draw(ctx
, prim
, draw_count
, ib
, false, 0u, ~0u,
2104 NULL
, 0, indirect_data
);
2109 * Function to be put into dd_function_table::DrawIndirect as fallback.
2110 * Calls into dd_function_table::Draw past adapting call arguments.
2111 * See dd_function_table::DrawIndirect for call argument documentation.
2114 _mesa_draw_indirect(struct gl_context
*ctx
, GLuint mode
,
2115 struct gl_buffer_object
*indirect_data
,
2116 GLsizeiptr indirect_offset
, unsigned draw_count
,
2118 struct gl_buffer_object
*indirect_draw_count_buffer
,
2119 GLsizeiptr indirect_draw_count_offset
,
2120 const struct _mesa_index_buffer
*ib
)
2122 /* Use alloca for the prim space if we are somehow in bounds. */
2123 if (draw_count
*sizeof(struct _mesa_prim
) < 1024) {
2124 struct _mesa_prim
*space
= alloca(draw_count
*sizeof(struct _mesa_prim
));
2125 memset(space
, 0, draw_count
*sizeof(struct _mesa_prim
));
2127 draw_indirect(ctx
, mode
, indirect_data
, indirect_offset
, draw_count
,
2128 stride
, indirect_draw_count_buffer
,
2129 indirect_draw_count_offset
, ib
, space
);
2131 struct _mesa_prim
*space
= calloc(draw_count
, sizeof(struct _mesa_prim
));
2132 if (space
== NULL
) {
2133 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
2134 (draw_count
> 1) ? "Multi" : "",
2135 ib
? "Elements" : "Arrays",
2136 indirect_data
? "CountARB" : "");
2140 draw_indirect(ctx
, mode
, indirect_data
, indirect_offset
, draw_count
,
2141 stride
, indirect_draw_count_buffer
,
2142 indirect_draw_count_offset
, ib
, space
);