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 for (i
= 0; i
< primcount
; i
++) {
663 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
665 /* The GL_ARB_shader_draw_parameters spec adds the following after the
666 * pseudo-code describing glMultiDrawArrays:
668 * "The index of the draw (<i> in the above pseudo-code) may be
669 * read by a vertex shader as <gl_DrawIDARB>, as described in
672 _mesa_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
675 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
683 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
688 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
691 ctx
->Driver
.MapBufferRange(ctx
, 0,
692 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
694 ctx
->Array
.VAO
->IndexBufferObj
,
697 case GL_UNSIGNED_BYTE
:
699 const GLubyte
*us
= (const GLubyte
*) map
;
701 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
702 printf("%02x ", us
[i
]);
709 case GL_UNSIGNED_SHORT
:
711 const GLushort
*us
= (const GLushort
*) map
;
713 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
714 printf("%04x ", us
[i
]);
721 case GL_UNSIGNED_INT
:
723 const GLuint
*us
= (const GLuint
*) map
;
725 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
726 printf("%08x ", us
[i
]);
737 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
743 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
744 const GLvoid
*indices
)
749 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
751 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
754 if (skip_validated_draw(ctx
))
762 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
763 * Do the rendering for a glDrawElements or glDrawRangeElements call after
764 * we've validated buffer bounds, etc.
767 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
768 GLboolean index_bounds_valid
,
769 GLuint start
, GLuint end
,
770 GLsizei count
, GLenum type
,
771 const GLvoid
* indices
,
772 GLint basevertex
, GLuint numInstances
,
775 struct _mesa_index_buffer ib
;
776 struct _mesa_prim prim
;
778 if (!index_bounds_valid
) {
783 if (skip_draw_elements(ctx
, count
, indices
))
787 ib
.index_size
= sizeof_ib_type(type
);
788 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
796 prim
.basevertex
= basevertex
;
799 /* Need to give special consideration to rendering a range of
800 * indices starting somewhere above zero. Typically the
801 * application is issuing multiple DrawRangeElements() to draw
802 * successive primitives layed out linearly in the vertex arrays.
803 * Unless the vertex arrays are all in a VBO (or locked as with
804 * CVA), the OpenGL semantics imply that we need to re-read or
805 * re-upload the vertex data on each draw call.
807 * In the case of hardware tnl, we want to avoid starting the
808 * upload at zero, as it will mean every draw call uploads an
809 * increasing amount of not-used vertex data. Worse - in the
810 * software tnl module, all those vertices might be transformed and
811 * lit but never rendered.
813 * If we just upload or transform the vertices in start..end,
814 * however, the indices will be incorrect.
816 * At this level, we don't know exactly what the requirements of
817 * the backend are going to be, though it will likely boil down to
820 * 1) Do nothing, everything is in a VBO and is processed once
823 * 2) Adjust the indices and vertex arrays so that start becomes
826 * Rather than doing anything here, I'll provide a helper function
827 * for the latter case elsewhere.
830 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
831 index_bounds_valid
, start
, end
,
832 numInstances
, baseInstance
, NULL
, 0);
834 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
841 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
844 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
845 GLsizei count
, GLenum type
,
846 const GLvoid
* indices
, GLint basevertex
)
848 static GLuint warnCount
= 0;
849 GLboolean index_bounds_valid
= GL_TRUE
;
851 /* This is only useful to catch invalid values in the "end" parameter
854 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
856 GET_CURRENT_CONTEXT(ctx
);
858 if (MESA_VERBOSE
& VERBOSE_DRAW
)
860 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
861 _mesa_enum_to_string(mode
), start
, end
, count
,
862 _mesa_enum_to_string(type
), indices
, basevertex
);
866 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
868 if (_mesa_is_no_error_enabled(ctx
)) {
870 _mesa_update_state(ctx
);
872 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
877 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
878 /* The application requested we draw using a range of indices that's
879 * outside the bounds of the current VBO. This is invalid and appears
880 * to give undefined results. The safest thing to do is to simply
881 * ignore the range, in case the application botched their range tracking
882 * but did provide valid indices. Also issue a warning indicating that
883 * the application is broken.
885 if (warnCount
++ < 10) {
886 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
887 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
888 "\trange is outside VBO bounds (max=%u); ignoring.\n"
889 "\tThis should be fixed in the application.",
890 start
, end
, basevertex
, count
, type
, indices
,
893 index_bounds_valid
= GL_FALSE
;
896 /* NOTE: It's important that 'end' is a reasonable value.
897 * in _tnl_draw_prims(), we use end to determine how many vertices
898 * to transform. If it's too large, we can unnecessarily split prims
899 * or we can read/write out of memory in several different places!
902 /* Catch/fix some potential user errors */
903 if (type
== GL_UNSIGNED_BYTE
) {
904 start
= MIN2(start
, 0xff);
905 end
= MIN2(end
, 0xff);
907 else if (type
== GL_UNSIGNED_SHORT
) {
908 start
= MIN2(start
, 0xffff);
909 end
= MIN2(end
, 0xffff);
913 printf("glDraw[Range]Elements{,BaseVertex}"
914 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
916 start
, end
, type
, count
,
917 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
920 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
921 index_bounds_valid
= GL_FALSE
;
924 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
926 (void) check_draw_elements_data
;
929 if (!index_bounds_valid
) {
934 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
935 count
, type
, indices
, basevertex
, 1, 0);
940 * Called by glDrawRangeElements() in immediate mode.
943 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
944 GLsizei count
, GLenum type
, const GLvoid
* indices
)
946 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
947 GET_CURRENT_CONTEXT(ctx
);
949 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
950 _mesa_enum_to_string(mode
), start
, end
, count
,
951 _mesa_enum_to_string(type
), indices
);
954 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
960 * Called by glDrawElements() in immediate mode.
963 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
964 const GLvoid
* indices
)
966 GET_CURRENT_CONTEXT(ctx
);
968 if (MESA_VERBOSE
& VERBOSE_DRAW
)
969 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
970 _mesa_enum_to_string(mode
), count
,
971 _mesa_enum_to_string(type
), indices
);
975 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
977 if (_mesa_is_no_error_enabled(ctx
)) {
979 _mesa_update_state(ctx
);
981 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
985 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
986 count
, type
, indices
, 0, 1, 0);
991 * Called by glDrawElementsBaseVertex() in immediate mode.
994 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
995 const GLvoid
* indices
, GLint basevertex
)
997 GET_CURRENT_CONTEXT(ctx
);
999 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1000 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1001 _mesa_enum_to_string(mode
), count
,
1002 _mesa_enum_to_string(type
), indices
);
1004 FLUSH_FOR_DRAW(ctx
);
1006 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1008 if (_mesa_is_no_error_enabled(ctx
)) {
1010 _mesa_update_state(ctx
);
1012 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1016 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1017 count
, type
, indices
, basevertex
, 1, 0);
1022 * Called by glDrawElementsInstanced() in immediate mode.
1024 static void GLAPIENTRY
1025 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1026 const GLvoid
* indices
, GLsizei numInstances
)
1028 GET_CURRENT_CONTEXT(ctx
);
1030 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1031 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1032 _mesa_enum_to_string(mode
), count
,
1033 _mesa_enum_to_string(type
), indices
);
1035 FLUSH_FOR_DRAW(ctx
);
1037 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1039 if (_mesa_is_no_error_enabled(ctx
)) {
1041 _mesa_update_state(ctx
);
1043 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1044 indices
, numInstances
))
1048 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1049 count
, type
, indices
, 0, numInstances
, 0);
1054 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1056 static void GLAPIENTRY
1057 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1058 GLenum type
, const GLvoid
* indices
,
1059 GLsizei numInstances
,
1062 GET_CURRENT_CONTEXT(ctx
);
1064 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1066 "glDrawElementsInstancedBaseVertex"
1067 "(%s, %d, %s, %p, %d; %d)\n",
1068 _mesa_enum_to_string(mode
), count
,
1069 _mesa_enum_to_string(type
), indices
,
1070 numInstances
, basevertex
);
1072 FLUSH_FOR_DRAW(ctx
);
1074 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1076 if (_mesa_is_no_error_enabled(ctx
)) {
1078 _mesa_update_state(ctx
);
1080 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1081 indices
, numInstances
))
1085 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1086 count
, type
, indices
,
1087 basevertex
, numInstances
, 0);
1092 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1094 static void GLAPIENTRY
1095 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1097 const GLvoid
*indices
,
1098 GLsizei numInstances
,
1099 GLuint baseInstance
)
1101 GET_CURRENT_CONTEXT(ctx
);
1103 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1105 "glDrawElementsInstancedBaseInstance"
1106 "(%s, %d, %s, %p, %d, %d)\n",
1107 _mesa_enum_to_string(mode
), count
,
1108 _mesa_enum_to_string(type
), indices
,
1109 numInstances
, baseInstance
);
1111 FLUSH_FOR_DRAW(ctx
);
1113 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1115 if (_mesa_is_no_error_enabled(ctx
)) {
1117 _mesa_update_state(ctx
);
1119 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1120 indices
, numInstances
))
1124 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1125 count
, type
, indices
, 0, numInstances
,
1131 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1133 static void GLAPIENTRY
1134 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1137 const GLvoid
*indices
,
1138 GLsizei numInstances
,
1140 GLuint baseInstance
)
1142 GET_CURRENT_CONTEXT(ctx
);
1144 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1146 "glDrawElementsInstancedBaseVertexBaseInstance"
1147 "(%s, %d, %s, %p, %d, %d, %d)\n",
1148 _mesa_enum_to_string(mode
), count
,
1149 _mesa_enum_to_string(type
), indices
,
1150 numInstances
, basevertex
, baseInstance
);
1152 FLUSH_FOR_DRAW(ctx
);
1154 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1156 if (_mesa_is_no_error_enabled(ctx
)) {
1158 _mesa_update_state(ctx
);
1160 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1161 indices
, numInstances
))
1165 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1166 count
, type
, indices
, basevertex
,
1167 numInstances
, baseInstance
);
1172 * Inner support for both _mesa_MultiDrawElements() and
1173 * _mesa_MultiDrawRangeElements().
1174 * This does the actual rendering after we've checked array indexes, etc.
1177 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1178 const GLsizei
*count
, GLenum type
,
1179 const GLvoid
* const *indices
,
1180 GLsizei primcount
, const GLint
*basevertex
)
1182 struct _mesa_index_buffer ib
;
1183 unsigned int index_type_size
= sizeof_ib_type(type
);
1184 uintptr_t min_index_ptr
, max_index_ptr
;
1185 GLboolean fallback
= GL_FALSE
;
1191 min_index_ptr
= (uintptr_t) indices
[0];
1193 for (i
= 0; i
< primcount
; i
++) {
1194 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1195 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1196 index_type_size
* count
[i
]);
1199 /* Check if we can handle this thing as a bunch of index offsets from the
1200 * same index pointer. If we can't, then we have to fall back to doing
1201 * a draw_prims per primitive.
1202 * Check that the difference between each prim's indexes is a multiple of
1203 * the index/element size.
1205 if (index_type_size
!= 1) {
1206 for (i
= 0; i
< primcount
; i
++) {
1207 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1215 /* Draw primitives individually if one count is zero, so we can easily skip
1218 for (i
= 0; i
< primcount
; i
++) {
1219 if (count
[i
] == 0) {
1225 /* If the index buffer isn't in a VBO, then treating the application's
1226 * subranges of the index buffer as one large index buffer may lead to
1227 * us reading unmapped memory.
1229 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1233 struct _mesa_prim
*prim
;
1235 ALLOC_PRIMS(prim
, primcount
, "glMultiDrawElements");
1237 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1238 ib
.index_size
= sizeof_ib_type(type
);
1239 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1240 ib
.ptr
= (void *) min_index_ptr
;
1242 for (i
= 0; i
< primcount
; i
++) {
1243 prim
[i
].begin
= (i
== 0);
1244 prim
[i
].end
= (i
== primcount
- 1);
1245 prim
[i
].mode
= mode
;
1247 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1248 prim
[i
].count
= count
[i
];
1249 prim
[i
].draw_id
= i
;
1250 if (basevertex
!= NULL
)
1251 prim
[i
].basevertex
= basevertex
[i
];
1253 prim
[i
].basevertex
= 0;
1256 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1257 false, 0, ~0, 1, 0, NULL
, 0);
1258 FREE_PRIMS(prim
, primcount
);
1261 /* render one prim at a time */
1262 for (i
= 0; i
< primcount
; i
++) {
1266 ib
.count
= count
[i
];
1267 ib
.index_size
= sizeof_ib_type(type
);
1268 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1269 ib
.ptr
= indices
[i
];
1271 struct _mesa_prim prim
;
1276 prim
.count
= count
[i
];
1278 if (basevertex
!= NULL
)
1279 prim
.basevertex
= basevertex
[i
];
1281 prim
.basevertex
= 0;
1283 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
, false, 0, ~0, 1, 0, NULL
, 0);
1287 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1294 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1295 const GLvoid
* const *indices
, GLsizei primcount
)
1297 GET_CURRENT_CONTEXT(ctx
);
1299 FLUSH_FOR_DRAW(ctx
);
1301 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1303 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1307 if (skip_validated_draw(ctx
))
1310 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1316 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1317 const GLsizei
*count
, GLenum type
,
1318 const GLvoid
* const *indices
,
1320 const GLsizei
*basevertex
)
1322 GET_CURRENT_CONTEXT(ctx
);
1324 FLUSH_FOR_DRAW(ctx
);
1326 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1328 if (_mesa_is_no_error_enabled(ctx
)) {
1330 _mesa_update_state(ctx
);
1332 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1337 if (skip_validated_draw(ctx
))
1340 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1346 * Draw a GL primitive using a vertex count obtained from transform feedback.
1347 * \param mode the type of GL primitive to draw
1348 * \param obj the transform feedback object to use
1349 * \param stream index of the transform feedback stream from which to
1350 * get the primitive count.
1351 * \param numInstances number of instances to draw
1354 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1355 struct gl_transform_feedback_object
*obj
,
1356 GLuint stream
, GLuint numInstances
)
1358 struct _mesa_prim prim
;
1360 FLUSH_FOR_DRAW(ctx
);
1362 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1364 if (_mesa_is_no_error_enabled(ctx
)) {
1366 _mesa_update_state(ctx
);
1368 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1374 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1375 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1376 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1378 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1379 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1383 if (skip_validated_draw(ctx
))
1386 /* init most fields to zero */
1387 memset(&prim
, 0, sizeof(prim
));
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, numInstances
, 0,
1399 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1406 * Like DrawArrays, but take the count from a transform feedback object.
1407 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1408 * \param name the transform feedback object
1409 * User still has to setup of the vertex attribute info with
1410 * glVertexPointer, glColorPointer, etc.
1411 * Part of GL_ARB_transform_feedback2.
1414 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1416 GET_CURRENT_CONTEXT(ctx
);
1417 struct gl_transform_feedback_object
*obj
=
1418 _mesa_lookup_transform_feedback_object(ctx
, name
);
1420 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1421 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1422 _mesa_enum_to_string(mode
), name
);
1424 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1428 static void GLAPIENTRY
1429 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1431 GET_CURRENT_CONTEXT(ctx
);
1432 struct gl_transform_feedback_object
*obj
=
1433 _mesa_lookup_transform_feedback_object(ctx
, name
);
1435 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1436 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1437 _mesa_enum_to_string(mode
), name
, stream
);
1439 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1443 static void GLAPIENTRY
1444 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1447 GET_CURRENT_CONTEXT(ctx
);
1448 struct gl_transform_feedback_object
*obj
=
1449 _mesa_lookup_transform_feedback_object(ctx
, name
);
1451 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1452 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1453 _mesa_enum_to_string(mode
), name
);
1455 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1459 static void GLAPIENTRY
1460 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1464 GET_CURRENT_CONTEXT(ctx
);
1465 struct gl_transform_feedback_object
*obj
=
1466 _mesa_lookup_transform_feedback_object(ctx
, name
);
1468 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1469 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1470 "(%s, %u, %u, %i)\n",
1471 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1473 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1478 _mesa_validated_drawarraysindirect(struct gl_context
*ctx
,
1479 GLenum mode
, const GLvoid
*indirect
)
1481 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1482 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1483 1 /* draw_count */ , 16 /* stride */ ,
1486 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1492 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1494 const GLvoid
*indirect
,
1495 GLsizei primcount
, GLsizei stride
)
1497 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1502 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1503 primcount
, stride
, NULL
, 0, NULL
);
1505 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1511 _mesa_validated_drawelementsindirect(struct gl_context
*ctx
,
1512 GLenum mode
, GLenum type
,
1513 const GLvoid
*indirect
)
1515 struct _mesa_index_buffer ib
;
1517 ib
.count
= 0; /* unknown */
1518 ib
.index_size
= sizeof_ib_type(type
);
1519 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1522 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1523 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1524 1 /* draw_count */ , 20 /* stride */ ,
1527 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1533 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1534 GLenum mode
, GLenum type
,
1535 const GLvoid
*indirect
,
1536 GLsizei primcount
, GLsizei stride
)
1538 struct _mesa_index_buffer ib
;
1539 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1544 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1546 ib
.count
= 0; /* unknown */
1547 ib
.index_size
= sizeof_ib_type(type
);
1548 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1551 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1552 ctx
->DrawIndirectBuffer
, offset
,
1553 primcount
, stride
, NULL
, 0, &ib
);
1555 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1561 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1564 static void GLAPIENTRY
1565 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1567 GET_CURRENT_CONTEXT(ctx
);
1569 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1570 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1571 _mesa_enum_to_string(mode
), indirect
);
1573 /* From the ARB_draw_indirect spec:
1575 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1576 * compatibility profile, this indicates that DrawArraysIndirect and
1577 * DrawElementsIndirect are to source their arguments directly from the
1578 * pointer passed as their <indirect> parameters."
1580 if (ctx
->API
== API_OPENGL_COMPAT
&&
1581 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1582 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1584 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1590 FLUSH_FOR_DRAW(ctx
);
1592 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1594 if (_mesa_is_no_error_enabled(ctx
)) {
1596 _mesa_update_state(ctx
);
1598 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1602 if (skip_validated_draw(ctx
))
1605 _mesa_validated_drawarraysindirect(ctx
, mode
, indirect
);
1609 static void GLAPIENTRY
1610 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1612 GET_CURRENT_CONTEXT(ctx
);
1614 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1615 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1616 _mesa_enum_to_string(mode
),
1617 _mesa_enum_to_string(type
), indirect
);
1619 /* From the ARB_draw_indirect spec:
1621 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1622 * compatibility profile, this indicates that DrawArraysIndirect and
1623 * DrawElementsIndirect are to source their arguments directly from the
1624 * pointer passed as their <indirect> parameters."
1626 if (ctx
->API
== API_OPENGL_COMPAT
&&
1627 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1629 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1630 * may not come from a client array and must come from an index buffer.
1631 * If no element array buffer is bound, an INVALID_OPERATION error is
1634 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1635 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1636 "glDrawElementsIndirect(no buffer bound "
1637 "to GL_ELEMENT_ARRAY_BUFFER)");
1639 DrawElementsIndirectCommand
*cmd
=
1640 (DrawElementsIndirectCommand
*) indirect
;
1642 /* Convert offset to pointer */
1643 void *offset
= (void *)
1644 (uintptr_t)((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1646 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1656 FLUSH_FOR_DRAW(ctx
);
1658 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1660 if (_mesa_is_no_error_enabled(ctx
)) {
1662 _mesa_update_state(ctx
);
1664 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1668 if (skip_validated_draw(ctx
))
1671 _mesa_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1675 static void GLAPIENTRY
1676 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1677 GLsizei primcount
, GLsizei stride
)
1679 GET_CURRENT_CONTEXT(ctx
);
1681 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1682 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1683 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1685 /* If <stride> is zero, the array elements are treated as tightly packed. */
1687 stride
= sizeof(DrawArraysIndirectCommand
);
1689 /* From the ARB_draw_indirect spec:
1691 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1692 * compatibility profile, this indicates that DrawArraysIndirect and
1693 * DrawElementsIndirect are to source their arguments directly from the
1694 * pointer passed as their <indirect> parameters."
1696 if (ctx
->API
== API_OPENGL_COMPAT
&&
1697 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1699 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1700 "glMultiDrawArraysIndirect"))
1703 const uint8_t *ptr
= (const uint8_t *) indirect
;
1704 for (unsigned i
= 0; i
< primcount
; i
++) {
1705 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1706 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1707 cmd
->count
, cmd
->primCount
,
1711 ptr
+= sizeof(DrawArraysIndirectCommand
);
1720 FLUSH_FOR_DRAW(ctx
);
1722 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1724 if (_mesa_is_no_error_enabled(ctx
)) {
1726 _mesa_update_state(ctx
);
1728 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1733 if (skip_validated_draw(ctx
))
1736 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1741 static void GLAPIENTRY
1742 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1743 const GLvoid
*indirect
,
1744 GLsizei primcount
, GLsizei stride
)
1746 GET_CURRENT_CONTEXT(ctx
);
1748 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1749 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1750 _mesa_enum_to_string(mode
),
1751 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1753 /* If <stride> is zero, the array elements are treated as tightly packed. */
1755 stride
= sizeof(DrawElementsIndirectCommand
);
1758 /* From the ARB_draw_indirect spec:
1760 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1761 * compatibility profile, this indicates that DrawArraysIndirect and
1762 * DrawElementsIndirect are to source their arguments directly from the
1763 * pointer passed as their <indirect> parameters."
1765 if (ctx
->API
== API_OPENGL_COMPAT
&&
1766 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1768 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1769 * may not come from a client array and must come from an index buffer.
1770 * If no element array buffer is bound, an INVALID_OPERATION error is
1773 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1774 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1775 "glMultiDrawElementsIndirect(no buffer bound "
1776 "to GL_ELEMENT_ARRAY_BUFFER)");
1781 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1782 "glMultiDrawArraysIndirect"))
1785 const uint8_t *ptr
= (const uint8_t *) indirect
;
1786 for (unsigned i
= 0; i
< primcount
; i
++) {
1787 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1790 ptr
+= sizeof(DrawElementsIndirectCommand
);
1799 FLUSH_FOR_DRAW(ctx
);
1801 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1803 if (_mesa_is_no_error_enabled(ctx
)) {
1805 _mesa_update_state(ctx
);
1807 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1812 if (skip_validated_draw(ctx
))
1815 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1821 _mesa_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1824 GLintptr drawcount_offset
,
1825 GLsizei maxdrawcount
,
1828 GLsizeiptr offset
= indirect
;
1830 if (maxdrawcount
== 0)
1833 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1834 ctx
->DrawIndirectBuffer
, offset
,
1835 maxdrawcount
, stride
,
1836 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1838 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1844 _mesa_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1845 GLenum mode
, GLenum type
,
1847 GLintptr drawcount_offset
,
1848 GLsizei maxdrawcount
,
1851 struct _mesa_index_buffer ib
;
1852 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1854 if (maxdrawcount
== 0)
1857 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1859 ib
.count
= 0; /* unknown */
1860 ib
.index_size
= sizeof_ib_type(type
);
1861 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1864 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1865 ctx
->DrawIndirectBuffer
, offset
,
1866 maxdrawcount
, stride
,
1867 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1869 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1874 static void GLAPIENTRY
1875 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1876 GLintptr drawcount_offset
,
1877 GLsizei maxdrawcount
, GLsizei stride
)
1879 GET_CURRENT_CONTEXT(ctx
);
1881 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1882 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1883 "(%s, %lx, %lx, %i, %i)\n",
1884 _mesa_enum_to_string(mode
),
1885 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1886 maxdrawcount
, stride
);
1888 /* If <stride> is zero, the array elements are treated as tightly packed. */
1890 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1892 FLUSH_FOR_DRAW(ctx
);
1894 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1896 if (_mesa_is_no_error_enabled(ctx
)) {
1898 _mesa_update_state(ctx
);
1900 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1903 maxdrawcount
, stride
))
1907 if (skip_validated_draw(ctx
))
1910 _mesa_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1912 maxdrawcount
, stride
);
1916 static void GLAPIENTRY
1917 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1919 GLintptr drawcount_offset
,
1920 GLsizei maxdrawcount
, GLsizei stride
)
1922 GET_CURRENT_CONTEXT(ctx
);
1924 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1925 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1926 "(%s, %s, %lx, %lx, %i, %i)\n",
1927 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1928 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1929 maxdrawcount
, stride
);
1931 /* If <stride> is zero, the array elements are treated as tightly packed. */
1933 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1935 FLUSH_FOR_DRAW(ctx
);
1937 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1939 if (_mesa_is_no_error_enabled(ctx
)) {
1941 _mesa_update_state(ctx
);
1943 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1946 maxdrawcount
, stride
))
1950 if (skip_validated_draw(ctx
))
1953 _mesa_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1954 drawcount_offset
, maxdrawcount
,
1960 * Initialize the dispatch table with the VBO functions for drawing.
1963 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1964 struct _glapi_table
*exec
)
1966 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1967 SET_DrawElements(exec
, _mesa_DrawElements
);
1969 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1970 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
1973 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
1974 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
1976 if (ctx
->API
== API_OPENGL_COMPAT
) {
1977 SET_Rectf(exec
, _mesa_exec_Rectf
);
1978 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
1979 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
1982 if (ctx
->API
!= API_OPENGLES
&&
1983 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1984 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
1985 SET_MultiDrawElementsBaseVertex(exec
,
1986 _mesa_MultiDrawElementsBaseVertex
);
1988 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1989 SET_DrawRangeElementsBaseVertex(exec
,
1990 _mesa_DrawRangeElementsBaseVertex
);
1991 SET_DrawElementsInstancedBaseVertex(exec
,
1992 _mesa_exec_DrawElementsInstancedBaseVertex
);
1996 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1997 SET_DrawArraysInstancedBaseInstance(exec
,
1998 _mesa_exec_DrawArraysInstancedBaseInstance
);
1999 SET_DrawElementsInstancedBaseInstance(exec
,
2000 _mesa_exec_DrawElementsInstancedBaseInstance
);
2001 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
2002 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
2005 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
2006 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
2007 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
2010 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2011 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
2012 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
2015 if (_mesa_is_desktop_gl(ctx
)) {
2016 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
2017 SET_DrawTransformFeedbackStream(exec
,
2018 _mesa_exec_DrawTransformFeedbackStream
);
2019 SET_DrawTransformFeedbackInstanced(exec
,
2020 _mesa_exec_DrawTransformFeedbackInstanced
);
2021 SET_DrawTransformFeedbackStreamInstanced(exec
,
2022 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
2023 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
2024 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
2025 SET_MultiDrawArraysIndirectCountARB(exec
,
2026 _mesa_exec_MultiDrawArraysIndirectCount
);
2027 SET_MultiDrawElementsIndirectCountARB(exec
,
2028 _mesa_exec_MultiDrawElementsIndirectCount
);
2034 /* GL_IBM_multimode_draw_arrays */
2036 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
2037 const GLsizei
* count
,
2038 GLsizei primcount
, GLint modestride
)
2040 GET_CURRENT_CONTEXT(ctx
);
2043 FLUSH_VERTICES(ctx
, 0);
2045 for ( i
= 0 ; i
< primcount
; i
++ ) {
2046 if ( count
[i
] > 0 ) {
2047 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2048 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
2054 /* GL_IBM_multimode_draw_arrays */
2056 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
2057 GLenum type
, const GLvoid
* const * indices
,
2058 GLsizei primcount
, GLint modestride
)
2060 GET_CURRENT_CONTEXT(ctx
);
2063 FLUSH_VERTICES(ctx
, 0);
2065 /* XXX not sure about ARB_vertex_buffer_object handling here */
2067 for ( i
= 0 ; i
< primcount
; i
++ ) {
2068 if ( count
[i
] > 0 ) {
2069 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2070 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,