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 get_index_size(GLenum type
, struct _mesa_index_buffer
*ib
)
110 /* The type is already validated, so use a fast conversion.
112 * GL_UNSIGNED_BYTE - GL_UNSIGNED_BYTE = 0
113 * GL_UNSIGNED_SHORT - GL_UNSIGNED_BYTE = 2
114 * GL_UNSIGNED_INT - GL_UNSIGNED_BYTE = 4
116 * Divide by 2 to get 0,1,2.
118 ib
->index_size_shift
= (type
- GL_UNSIGNED_BYTE
) >> 1;
122 * Examine the array's data for NaNs, etc.
123 * For debug purposes; not normally used.
126 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
127 GLenum elemType
, const void *elements
,
130 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
134 _mesa_vao_map(ctx
, vao
, GL_MAP_READ_BIT
);
136 if (_mesa_is_bufferobj(vao
->IndexBufferObj
))
138 ADD_POINTERS(vao
->IndexBufferObj
->Mappings
[MAP_INTERNAL
].Pointer
, elements
);
140 for (i
= 0; i
< count
; i
++) {
145 case GL_UNSIGNED_BYTE
:
146 j
= ((const GLubyte
*) elements
)[i
];
148 case GL_UNSIGNED_SHORT
:
149 j
= ((const GLushort
*) elements
)[i
];
151 case GL_UNSIGNED_INT
:
152 j
= ((const GLuint
*) elements
)[i
];
155 unreachable("Unexpected index buffer type");
158 /* check element j of each enabled array */
159 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
160 check_array_data(ctx
, vao
, k
, j
);
164 _mesa_vao_unmap(ctx
, vao
);
169 * Check array data, looking for NaNs, etc.
172 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
179 * Check if we should skip the draw call even after validation was successful.
182 skip_validated_draw(struct gl_context
*ctx
)
186 /* For ES2, we can draw if we have a vertex program/shader). */
187 return ctx
->VertexProgram
._Current
== NULL
;
190 /* For OpenGL ES, only draw if we have vertex positions
192 if (!(ctx
->Array
.VAO
->Enabled
& VERT_BIT_POS
))
196 case API_OPENGL_CORE
:
197 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
200 * "If there is no active program for the vertex or fragment shader
201 * stages, the results of vertex and/or fragment processing will be
202 * undefined. However, this is not an error."
204 * The fragment shader is not tested here because other state (e.g.,
205 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
207 return ctx
->VertexProgram
._Current
== NULL
;
209 case API_OPENGL_COMPAT
:
210 if (ctx
->VertexProgram
._Current
!= NULL
) {
211 /* Draw regardless of whether or not we have any vertex arrays.
212 * (Ex: could draw a point using a constant vertex pos)
216 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
219 return !(ctx
->Array
.VAO
->Enabled
& (VERT_BIT_POS
|VERT_BIT_GENERIC0
));
224 unreachable("Invalid API value in check_valid_to_render()");
232 * Print info/data for glDrawArrays(), for debugging.
235 print_draw_arrays(struct gl_context
*ctx
,
236 GLenum mode
, GLint start
, GLsizei count
)
238 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
240 printf("_mesa_DrawArrays(mode 0x%x, start %d, count %d):\n",
243 _mesa_vao_map_arrays(ctx
, vao
, GL_MAP_READ_BIT
);
245 GLbitfield mask
= vao
->Enabled
;
247 const gl_vert_attrib i
= u_bit_scan(&mask
);
248 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
250 const struct gl_vertex_buffer_binding
*binding
=
251 &vao
->BufferBinding
[array
->BufferBindingIndex
];
252 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
254 printf("attr %s: size %d stride %d "
255 "ptr %p Bufobj %u\n",
256 gl_vert_attrib_name((gl_vert_attrib
) i
),
257 array
->Format
.Size
, binding
->Stride
,
258 array
->Ptr
, bufObj
->Name
);
260 if (_mesa_is_bufferobj(bufObj
)) {
261 GLubyte
*p
= bufObj
->Mappings
[MAP_INTERNAL
].Pointer
;
262 int offset
= (int) (GLintptr
)
263 _mesa_vertex_attrib_address(array
, binding
);
266 switch (array
->Format
.Type
) {
269 case GL_UNSIGNED_INT64_ARB
:
276 float *f
= (float *) (p
+ offset
);
279 int n
= (count
- 1) * (binding
->Stride
/ (4 * multiplier
))
280 + array
->Format
.Size
;
283 printf(" Data at offset %d:\n", offset
);
286 printf(" double[%d] = 0x%016llx %lf\n", i
,
287 ((unsigned long long *) k
)[i
], ((double *) f
)[i
]);
289 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
295 _mesa_vao_unmap_arrays(ctx
, vao
);
300 * Return a filter mask for the net enabled vao arrays.
301 * This is to mask out arrays that would otherwise supersed required current
302 * values for the fixed function shaders for example.
305 enabled_filter(const struct gl_context
*ctx
)
307 switch (ctx
->VertexProgram
._VPMode
) {
309 /* When no vertex program is active (or the vertex program is generated
310 * from fixed-function state). We put the material values into the
311 * generic slots. Since the vao has no material arrays, mute these
312 * slots from the enabled arrays so that the current material values
313 * are pulled instead of the vao arrays.
315 return VERT_BIT_FF_ALL
;
318 /* There are no shaders in OpenGL ES 1.x, so this code path should be
319 * impossible to reach. The meta code is careful to not use shaders in
322 assert(ctx
->API
!= API_OPENGLES
);
324 /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
325 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
326 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
329 if (ctx
->API
!= API_OPENGL_COMPAT
)
330 return VERT_BIT_GENERIC_ALL
;
342 * Helper function called by the other DrawArrays() functions below.
343 * This is where we handle primitive restart for drawing non-indexed
344 * arrays. If primitive restart is enabled, it typically means
345 * splitting one DrawArrays() into two.
348 _mesa_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
349 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
352 if (skip_validated_draw(ctx
))
355 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
358 struct _mesa_prim prim
= {
367 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
368 GL_TRUE
, start
, start
+ count
- 1,
369 numInstances
, baseInstance
, NULL
, 0);
371 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
378 * Execute a glRectf() function.
380 static void GLAPIENTRY
381 _mesa_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
383 GET_CURRENT_CONTEXT(ctx
);
384 ASSERT_OUTSIDE_BEGIN_END(ctx
);
386 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
387 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
388 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
389 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
390 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
391 CALL_End(GET_DISPATCH(), ());
395 static void GLAPIENTRY
396 _mesa_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
398 GET_CURRENT_CONTEXT(ctx
);
408 prim
= GL_LINE_STRIP
;
411 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
415 /* No effect if vertex maps disabled.
417 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
420 du
= ctx
->Eval
.MapGrid1du
;
421 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
423 CALL_Begin(GET_DISPATCH(), (prim
));
424 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
425 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
427 CALL_End(GET_DISPATCH(), ());
431 static void GLAPIENTRY
432 _mesa_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
434 GET_CURRENT_CONTEXT(ctx
);
435 GLfloat u
, du
, v
, dv
, v1
, u1
;
444 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
448 /* No effect if vertex maps disabled.
450 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
453 du
= ctx
->Eval
.MapGrid2du
;
454 dv
= ctx
->Eval
.MapGrid2dv
;
455 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
456 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
460 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
461 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
462 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
463 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
466 CALL_End(GET_DISPATCH(), ());
469 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
470 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
471 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
472 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
474 CALL_End(GET_DISPATCH(), ());
476 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
477 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
478 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
479 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
481 CALL_End(GET_DISPATCH(), ());
485 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
486 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
487 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
488 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
489 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
491 CALL_End(GET_DISPATCH(), ());
499 * Called from glDrawArrays when in immediate mode (not display list mode).
502 _mesa_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
504 GET_CURRENT_CONTEXT(ctx
);
506 if (MESA_VERBOSE
& VERBOSE_DRAW
)
507 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
508 _mesa_enum_to_string(mode
), start
, count
);
512 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
514 if (_mesa_is_no_error_enabled(ctx
)) {
516 _mesa_update_state(ctx
);
518 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
523 check_draw_arrays_data(ctx
, start
, count
);
525 _mesa_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
528 print_draw_arrays(ctx
, mode
, start
, count
);
533 * Called from glDrawArraysInstanced when in immediate mode (not
534 * display list mode).
537 _mesa_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
538 GLsizei numInstances
)
540 GET_CURRENT_CONTEXT(ctx
);
542 if (MESA_VERBOSE
& VERBOSE_DRAW
)
543 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
544 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
548 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
550 if (_mesa_is_no_error_enabled(ctx
)) {
552 _mesa_update_state(ctx
);
554 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
560 check_draw_arrays_data(ctx
, start
, count
);
562 _mesa_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
565 print_draw_arrays(ctx
, mode
, start
, count
);
570 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
572 static void GLAPIENTRY
573 _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
574 GLsizei count
, GLsizei numInstances
,
577 GET_CURRENT_CONTEXT(ctx
);
579 if (MESA_VERBOSE
& VERBOSE_DRAW
)
581 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
582 _mesa_enum_to_string(mode
), first
, count
,
583 numInstances
, baseInstance
);
587 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
589 if (_mesa_is_no_error_enabled(ctx
)) {
591 _mesa_update_state(ctx
);
593 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
599 check_draw_arrays_data(ctx
, first
, count
);
601 _mesa_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
604 print_draw_arrays(ctx
, mode
, first
, count
);
608 #define MAX_ALLOCA_PRIMS (50000 / sizeof(*prim))
610 /* Use calloc for large allocations and alloca for small allocations. */
611 /* We have to use a macro because alloca is local within the function. */
612 #define ALLOC_PRIMS(prim, primcount, func) do { \
613 if (unlikely(primcount > MAX_ALLOCA_PRIMS)) { \
614 prim = calloc(primcount, sizeof(*prim)); \
616 _mesa_error(ctx, GL_OUT_OF_MEMORY, func); \
620 prim = alloca(primcount * sizeof(*prim)); \
624 #define FREE_PRIMS(prim, primcount) do { \
625 if (primcount > MAX_ALLOCA_PRIMS) \
631 * Called from glMultiDrawArrays when in immediate mode.
633 static void GLAPIENTRY
634 _mesa_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
635 const GLsizei
*count
, GLsizei primcount
)
637 GET_CURRENT_CONTEXT(ctx
);
640 if (MESA_VERBOSE
& VERBOSE_DRAW
)
642 "glMultiDrawArrays(%s, %p, %p, %d)\n",
643 _mesa_enum_to_string(mode
), first
, count
, primcount
);
647 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
649 if (_mesa_is_no_error_enabled(ctx
)) {
651 _mesa_update_state(ctx
);
653 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
657 if (skip_validated_draw(ctx
))
660 struct _mesa_prim
*prim
;
662 ALLOC_PRIMS(prim
, primcount
, "glMultiDrawElements");
664 for (i
= 0; i
< primcount
; i
++) {
669 prim
[i
].start
= first
[i
];
670 prim
[i
].count
= count
[i
];
671 prim
[i
].basevertex
= 0;
674 ctx
->Driver
.Draw(ctx
, prim
, primcount
, NULL
, GL_FALSE
, 0, 0, 1, 0,
677 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
680 FREE_PRIMS(prim
, primcount
);
686 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
691 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
694 ctx
->Driver
.MapBufferRange(ctx
, 0,
695 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
697 ctx
->Array
.VAO
->IndexBufferObj
,
700 case GL_UNSIGNED_BYTE
:
702 const GLubyte
*us
= (const GLubyte
*) map
;
704 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
705 printf("%02x ", us
[i
]);
712 case GL_UNSIGNED_SHORT
:
714 const GLushort
*us
= (const GLushort
*) map
;
716 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
717 printf("%04x ", us
[i
]);
724 case GL_UNSIGNED_INT
:
726 const GLuint
*us
= (const GLuint
*) map
;
728 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
729 printf("%08x ", us
[i
]);
740 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
746 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
747 const GLvoid
*indices
)
752 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
754 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
757 if (skip_validated_draw(ctx
))
765 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
766 * Do the rendering for a glDrawElements or glDrawRangeElements call after
767 * we've validated buffer bounds, etc.
770 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
771 GLboolean index_bounds_valid
,
772 GLuint start
, GLuint end
,
773 GLsizei count
, GLenum type
,
774 const GLvoid
* indices
,
775 GLint basevertex
, GLuint numInstances
,
778 struct _mesa_index_buffer ib
;
779 struct _mesa_prim prim
;
781 if (!index_bounds_valid
) {
786 if (skip_draw_elements(ctx
, count
, indices
))
790 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
792 get_index_size(type
, &ib
);
799 prim
.basevertex
= basevertex
;
802 /* Need to give special consideration to rendering a range of
803 * indices starting somewhere above zero. Typically the
804 * application is issuing multiple DrawRangeElements() to draw
805 * successive primitives layed out linearly in the vertex arrays.
806 * Unless the vertex arrays are all in a VBO (or locked as with
807 * CVA), the OpenGL semantics imply that we need to re-read or
808 * re-upload the vertex data on each draw call.
810 * In the case of hardware tnl, we want to avoid starting the
811 * upload at zero, as it will mean every draw call uploads an
812 * increasing amount of not-used vertex data. Worse - in the
813 * software tnl module, all those vertices might be transformed and
814 * lit but never rendered.
816 * If we just upload or transform the vertices in start..end,
817 * however, the indices will be incorrect.
819 * At this level, we don't know exactly what the requirements of
820 * the backend are going to be, though it will likely boil down to
823 * 1) Do nothing, everything is in a VBO and is processed once
826 * 2) Adjust the indices and vertex arrays so that start becomes
829 * Rather than doing anything here, I'll provide a helper function
830 * for the latter case elsewhere.
833 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
834 index_bounds_valid
, start
, end
,
835 numInstances
, baseInstance
, NULL
, 0);
837 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
844 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
847 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
848 GLsizei count
, GLenum type
,
849 const GLvoid
* indices
, GLint basevertex
)
851 static GLuint warnCount
= 0;
852 GLboolean index_bounds_valid
= GL_TRUE
;
854 /* This is only useful to catch invalid values in the "end" parameter
857 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
859 GET_CURRENT_CONTEXT(ctx
);
861 if (MESA_VERBOSE
& VERBOSE_DRAW
)
863 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
864 _mesa_enum_to_string(mode
), start
, end
, count
,
865 _mesa_enum_to_string(type
), indices
, basevertex
);
869 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
871 if (_mesa_is_no_error_enabled(ctx
)) {
873 _mesa_update_state(ctx
);
875 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
880 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
881 /* The application requested we draw using a range of indices that's
882 * outside the bounds of the current VBO. This is invalid and appears
883 * to give undefined results. The safest thing to do is to simply
884 * ignore the range, in case the application botched their range tracking
885 * but did provide valid indices. Also issue a warning indicating that
886 * the application is broken.
888 if (warnCount
++ < 10) {
889 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
890 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
891 "\trange is outside VBO bounds (max=%u); ignoring.\n"
892 "\tThis should be fixed in the application.",
893 start
, end
, basevertex
, count
, type
, indices
,
896 index_bounds_valid
= GL_FALSE
;
899 /* NOTE: It's important that 'end' is a reasonable value.
900 * in _tnl_draw_prims(), we use end to determine how many vertices
901 * to transform. If it's too large, we can unnecessarily split prims
902 * or we can read/write out of memory in several different places!
905 /* Catch/fix some potential user errors */
906 if (type
== GL_UNSIGNED_BYTE
) {
907 start
= MIN2(start
, 0xff);
908 end
= MIN2(end
, 0xff);
910 else if (type
== GL_UNSIGNED_SHORT
) {
911 start
= MIN2(start
, 0xffff);
912 end
= MIN2(end
, 0xffff);
916 printf("glDraw[Range]Elements{,BaseVertex}"
917 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
919 start
, end
, type
, count
,
920 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
923 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
924 index_bounds_valid
= GL_FALSE
;
927 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
929 (void) check_draw_elements_data
;
932 if (!index_bounds_valid
) {
937 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
938 count
, type
, indices
, basevertex
, 1, 0);
943 * Called by glDrawRangeElements() in immediate mode.
946 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
947 GLsizei count
, GLenum type
, const GLvoid
* indices
)
949 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
950 GET_CURRENT_CONTEXT(ctx
);
952 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
953 _mesa_enum_to_string(mode
), start
, end
, count
,
954 _mesa_enum_to_string(type
), indices
);
957 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
963 * Called by glDrawElements() in immediate mode.
966 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
967 const GLvoid
* indices
)
969 GET_CURRENT_CONTEXT(ctx
);
971 if (MESA_VERBOSE
& VERBOSE_DRAW
)
972 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
973 _mesa_enum_to_string(mode
), count
,
974 _mesa_enum_to_string(type
), indices
);
978 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
980 if (_mesa_is_no_error_enabled(ctx
)) {
982 _mesa_update_state(ctx
);
984 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
988 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
989 count
, type
, indices
, 0, 1, 0);
994 * Called by glDrawElementsBaseVertex() in immediate mode.
997 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
998 const GLvoid
* indices
, GLint basevertex
)
1000 GET_CURRENT_CONTEXT(ctx
);
1002 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1003 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1004 _mesa_enum_to_string(mode
), count
,
1005 _mesa_enum_to_string(type
), indices
);
1007 FLUSH_FOR_DRAW(ctx
);
1009 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1011 if (_mesa_is_no_error_enabled(ctx
)) {
1013 _mesa_update_state(ctx
);
1015 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1019 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1020 count
, type
, indices
, basevertex
, 1, 0);
1025 * Called by glDrawElementsInstanced() in immediate mode.
1027 static void GLAPIENTRY
1028 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1029 const GLvoid
* indices
, GLsizei numInstances
)
1031 GET_CURRENT_CONTEXT(ctx
);
1033 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1034 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1035 _mesa_enum_to_string(mode
), count
,
1036 _mesa_enum_to_string(type
), indices
);
1038 FLUSH_FOR_DRAW(ctx
);
1040 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1042 if (_mesa_is_no_error_enabled(ctx
)) {
1044 _mesa_update_state(ctx
);
1046 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1047 indices
, numInstances
))
1051 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1052 count
, type
, indices
, 0, numInstances
, 0);
1057 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1059 static void GLAPIENTRY
1060 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1061 GLenum type
, const GLvoid
* indices
,
1062 GLsizei numInstances
,
1065 GET_CURRENT_CONTEXT(ctx
);
1067 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1069 "glDrawElementsInstancedBaseVertex"
1070 "(%s, %d, %s, %p, %d; %d)\n",
1071 _mesa_enum_to_string(mode
), count
,
1072 _mesa_enum_to_string(type
), indices
,
1073 numInstances
, basevertex
);
1075 FLUSH_FOR_DRAW(ctx
);
1077 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1079 if (_mesa_is_no_error_enabled(ctx
)) {
1081 _mesa_update_state(ctx
);
1083 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1084 indices
, numInstances
))
1088 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1089 count
, type
, indices
,
1090 basevertex
, numInstances
, 0);
1095 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1097 static void GLAPIENTRY
1098 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1100 const GLvoid
*indices
,
1101 GLsizei numInstances
,
1102 GLuint baseInstance
)
1104 GET_CURRENT_CONTEXT(ctx
);
1106 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1108 "glDrawElementsInstancedBaseInstance"
1109 "(%s, %d, %s, %p, %d, %d)\n",
1110 _mesa_enum_to_string(mode
), count
,
1111 _mesa_enum_to_string(type
), indices
,
1112 numInstances
, baseInstance
);
1114 FLUSH_FOR_DRAW(ctx
);
1116 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1118 if (_mesa_is_no_error_enabled(ctx
)) {
1120 _mesa_update_state(ctx
);
1122 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1123 indices
, numInstances
))
1127 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1128 count
, type
, indices
, 0, numInstances
,
1134 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1136 static void GLAPIENTRY
1137 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1140 const GLvoid
*indices
,
1141 GLsizei numInstances
,
1143 GLuint baseInstance
)
1145 GET_CURRENT_CONTEXT(ctx
);
1147 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1149 "glDrawElementsInstancedBaseVertexBaseInstance"
1150 "(%s, %d, %s, %p, %d, %d, %d)\n",
1151 _mesa_enum_to_string(mode
), count
,
1152 _mesa_enum_to_string(type
), indices
,
1153 numInstances
, basevertex
, baseInstance
);
1155 FLUSH_FOR_DRAW(ctx
);
1157 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1159 if (_mesa_is_no_error_enabled(ctx
)) {
1161 _mesa_update_state(ctx
);
1163 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1164 indices
, numInstances
))
1168 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1169 count
, type
, indices
, basevertex
,
1170 numInstances
, baseInstance
);
1175 * Inner support for both _mesa_MultiDrawElements() and
1176 * _mesa_MultiDrawRangeElements().
1177 * This does the actual rendering after we've checked array indexes, etc.
1180 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1181 const GLsizei
*count
, GLenum type
,
1182 const GLvoid
* const *indices
,
1183 GLsizei primcount
, const GLint
*basevertex
)
1185 struct _mesa_index_buffer ib
;
1186 uintptr_t min_index_ptr
, max_index_ptr
;
1187 GLboolean fallback
= GL_FALSE
;
1193 get_index_size(type
, &ib
);
1195 min_index_ptr
= (uintptr_t) indices
[0];
1197 for (i
= 0; i
< primcount
; i
++) {
1198 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1199 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1200 (count
[i
] << ib
.index_size_shift
));
1203 /* Check if we can handle this thing as a bunch of index offsets from the
1204 * same index pointer. If we can't, then we have to fall back to doing
1205 * a draw_prims per primitive.
1206 * Check that the difference between each prim's indexes is a multiple of
1207 * the index/element size.
1209 if (ib
.index_size_shift
) {
1210 for (i
= 0; i
< primcount
; i
++) {
1211 if ((((uintptr_t) indices
[i
] - min_index_ptr
) &
1212 ((1 << ib
.index_size_shift
) - 1)) != 0) {
1219 if (ctx
->Const
.MultiDrawWithUserIndices
) {
1220 /* Check whether prim[i].start would overflow. */
1221 if (((max_index_ptr
- min_index_ptr
) >> ib
.index_size_shift
) > UINT_MAX
)
1224 /* If the index buffer isn't in a VBO, then treating the application's
1225 * subranges of the index buffer as one large index buffer may lead to
1226 * us reading unmapped memory.
1228 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
) >> ib
.index_size_shift
;
1238 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1239 ib
.ptr
= (void *) min_index_ptr
;
1241 for (i
= 0; i
< primcount
; i
++) {
1244 prim
[i
].mode
= mode
;
1246 ((uintptr_t) indices
[i
] - min_index_ptr
) >> ib
.index_size_shift
;
1247 prim
[i
].count
= count
[i
];
1248 prim
[i
].draw_id
= i
;
1249 if (basevertex
!= NULL
)
1250 prim
[i
].basevertex
= basevertex
[i
];
1252 prim
[i
].basevertex
= 0;
1255 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1256 false, 0, ~0, 1, 0, NULL
, 0);
1257 FREE_PRIMS(prim
, primcount
);
1260 /* render one prim at a time */
1261 for (i
= 0; i
< primcount
; i
++) {
1265 ib
.count
= count
[i
];
1266 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1267 ib
.ptr
= indices
[i
];
1269 struct _mesa_prim prim
;
1274 prim
.count
= count
[i
];
1276 if (basevertex
!= NULL
)
1277 prim
.basevertex
= basevertex
[i
];
1279 prim
.basevertex
= 0;
1281 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
, false, 0, ~0, 1, 0, NULL
, 0);
1285 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1292 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1293 const GLvoid
* const *indices
, GLsizei primcount
)
1295 GET_CURRENT_CONTEXT(ctx
);
1297 FLUSH_FOR_DRAW(ctx
);
1299 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1301 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1305 if (skip_validated_draw(ctx
))
1308 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1314 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1315 const GLsizei
*count
, GLenum type
,
1316 const GLvoid
* const *indices
,
1318 const GLsizei
*basevertex
)
1320 GET_CURRENT_CONTEXT(ctx
);
1322 FLUSH_FOR_DRAW(ctx
);
1324 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1326 if (_mesa_is_no_error_enabled(ctx
)) {
1328 _mesa_update_state(ctx
);
1330 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1335 if (skip_validated_draw(ctx
))
1338 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1344 * Draw a GL primitive using a vertex count obtained from transform feedback.
1345 * \param mode the type of GL primitive to draw
1346 * \param obj the transform feedback object to use
1347 * \param stream index of the transform feedback stream from which to
1348 * get the primitive count.
1349 * \param numInstances number of instances to draw
1352 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1353 struct gl_transform_feedback_object
*obj
,
1354 GLuint stream
, GLuint numInstances
)
1356 struct _mesa_prim prim
;
1358 FLUSH_FOR_DRAW(ctx
);
1360 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1362 if (_mesa_is_no_error_enabled(ctx
)) {
1364 _mesa_update_state(ctx
);
1366 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1372 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1373 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1374 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1376 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1377 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1381 if (skip_validated_draw(ctx
))
1384 /* init most fields to zero */
1385 memset(&prim
, 0, sizeof(prim
));
1390 /* Maybe we should do some primitive splitting for primitive restart
1391 * (like in DrawArrays), but we have no way to know how many vertices
1392 * will be rendered. */
1394 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, numInstances
, 0,
1397 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1404 * Like DrawArrays, but take the count from a transform feedback object.
1405 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1406 * \param name the transform feedback object
1407 * User still has to setup of the vertex attribute info with
1408 * glVertexPointer, glColorPointer, etc.
1409 * Part of GL_ARB_transform_feedback2.
1412 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1414 GET_CURRENT_CONTEXT(ctx
);
1415 struct gl_transform_feedback_object
*obj
=
1416 _mesa_lookup_transform_feedback_object(ctx
, name
);
1418 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1419 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1420 _mesa_enum_to_string(mode
), name
);
1422 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1426 static void GLAPIENTRY
1427 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1429 GET_CURRENT_CONTEXT(ctx
);
1430 struct gl_transform_feedback_object
*obj
=
1431 _mesa_lookup_transform_feedback_object(ctx
, name
);
1433 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1434 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1435 _mesa_enum_to_string(mode
), name
, stream
);
1437 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1441 static void GLAPIENTRY
1442 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1445 GET_CURRENT_CONTEXT(ctx
);
1446 struct gl_transform_feedback_object
*obj
=
1447 _mesa_lookup_transform_feedback_object(ctx
, name
);
1449 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1450 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1451 _mesa_enum_to_string(mode
), name
);
1453 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1457 static void GLAPIENTRY
1458 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1462 GET_CURRENT_CONTEXT(ctx
);
1463 struct gl_transform_feedback_object
*obj
=
1464 _mesa_lookup_transform_feedback_object(ctx
, name
);
1466 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1467 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1468 "(%s, %u, %u, %i)\n",
1469 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1471 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1476 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
, GLenum mode
,
1478 GLintptr drawcount_offset
,
1479 GLsizei drawcount
, GLsizei stride
,
1480 struct gl_buffer_object
*drawcount_buffer
)
1482 /* If drawcount_buffer is set, drawcount is the maximum draw count.*/
1486 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, indirect
,
1487 drawcount
, stride
, drawcount_buffer
,
1488 drawcount_offset
, NULL
);
1490 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1496 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1497 GLenum mode
, GLenum type
,
1499 GLintptr drawcount_offset
,
1500 GLsizei drawcount
, GLsizei stride
,
1501 struct gl_buffer_object
*drawcount_buffer
)
1503 /* If drawcount_buffer is set, drawcount is the maximum draw count.*/
1507 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1508 struct _mesa_index_buffer ib
;
1509 ib
.count
= 0; /* unknown */
1510 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1512 get_index_size(type
, &ib
);
1514 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, indirect
,
1515 drawcount
, stride
, drawcount_buffer
,
1516 drawcount_offset
, &ib
);
1518 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1524 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1527 static void GLAPIENTRY
1528 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1530 GET_CURRENT_CONTEXT(ctx
);
1532 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1533 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1534 _mesa_enum_to_string(mode
), indirect
);
1536 /* From the ARB_draw_indirect spec:
1538 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1539 * compatibility profile, this indicates that DrawArraysIndirect and
1540 * DrawElementsIndirect are to source their arguments directly from the
1541 * pointer passed as their <indirect> parameters."
1543 if (ctx
->API
== API_OPENGL_COMPAT
&&
1544 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1545 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1547 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1553 FLUSH_FOR_DRAW(ctx
);
1555 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1557 if (_mesa_is_no_error_enabled(ctx
)) {
1559 _mesa_update_state(ctx
);
1561 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1565 if (skip_validated_draw(ctx
))
1568 _mesa_validated_multidrawarraysindirect(ctx
, mode
, (GLintptr
)indirect
,
1573 static void GLAPIENTRY
1574 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1576 GET_CURRENT_CONTEXT(ctx
);
1578 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1579 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1580 _mesa_enum_to_string(mode
),
1581 _mesa_enum_to_string(type
), indirect
);
1583 /* From the ARB_draw_indirect spec:
1585 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1586 * compatibility profile, this indicates that DrawArraysIndirect and
1587 * DrawElementsIndirect are to source their arguments directly from the
1588 * pointer passed as their <indirect> parameters."
1590 if (ctx
->API
== API_OPENGL_COMPAT
&&
1591 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1593 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1594 * may not come from a client array and must come from an index buffer.
1595 * If no element array buffer is bound, an INVALID_OPERATION error is
1598 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1599 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1600 "glDrawElementsIndirect(no buffer bound "
1601 "to GL_ELEMENT_ARRAY_BUFFER)");
1603 DrawElementsIndirectCommand
*cmd
=
1604 (DrawElementsIndirectCommand
*) indirect
;
1606 /* Convert offset to pointer */
1607 void *offset
= (void *)
1608 (uintptr_t)((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1610 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1620 FLUSH_FOR_DRAW(ctx
);
1622 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1624 if (_mesa_is_no_error_enabled(ctx
)) {
1626 _mesa_update_state(ctx
);
1628 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1632 if (skip_validated_draw(ctx
))
1635 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
,
1636 (GLintptr
)indirect
, 0,
1641 static void GLAPIENTRY
1642 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1643 GLsizei primcount
, GLsizei stride
)
1645 GET_CURRENT_CONTEXT(ctx
);
1647 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1648 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1649 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1651 /* If <stride> is zero, the array elements are treated as tightly packed. */
1653 stride
= sizeof(DrawArraysIndirectCommand
);
1655 /* From the ARB_draw_indirect spec:
1657 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1658 * compatibility profile, this indicates that DrawArraysIndirect and
1659 * DrawElementsIndirect are to source their arguments directly from the
1660 * pointer passed as their <indirect> parameters."
1662 if (ctx
->API
== API_OPENGL_COMPAT
&&
1663 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1665 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1666 "glMultiDrawArraysIndirect"))
1669 const uint8_t *ptr
= (const uint8_t *) indirect
;
1670 for (unsigned i
= 0; i
< primcount
; i
++) {
1671 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1672 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1673 cmd
->count
, cmd
->primCount
,
1677 ptr
+= sizeof(DrawArraysIndirectCommand
);
1686 FLUSH_FOR_DRAW(ctx
);
1688 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1690 if (_mesa_is_no_error_enabled(ctx
)) {
1692 _mesa_update_state(ctx
);
1694 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1699 if (skip_validated_draw(ctx
))
1702 _mesa_validated_multidrawarraysindirect(ctx
, mode
, (GLintptr
)indirect
, 0,
1703 primcount
, stride
, NULL
);
1707 static void GLAPIENTRY
1708 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1709 const GLvoid
*indirect
,
1710 GLsizei primcount
, GLsizei stride
)
1712 GET_CURRENT_CONTEXT(ctx
);
1714 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1715 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1716 _mesa_enum_to_string(mode
),
1717 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1719 /* If <stride> is zero, the array elements are treated as tightly packed. */
1721 stride
= sizeof(DrawElementsIndirectCommand
);
1724 /* From the ARB_draw_indirect spec:
1726 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1727 * compatibility profile, this indicates that DrawArraysIndirect and
1728 * DrawElementsIndirect are to source their arguments directly from the
1729 * pointer passed as their <indirect> parameters."
1731 if (ctx
->API
== API_OPENGL_COMPAT
&&
1732 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1734 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1735 * may not come from a client array and must come from an index buffer.
1736 * If no element array buffer is bound, an INVALID_OPERATION error is
1739 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1740 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1741 "glMultiDrawElementsIndirect(no buffer bound "
1742 "to GL_ELEMENT_ARRAY_BUFFER)");
1747 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1748 "glMultiDrawArraysIndirect"))
1751 const uint8_t *ptr
= (const uint8_t *) indirect
;
1752 for (unsigned i
= 0; i
< primcount
; i
++) {
1753 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1756 ptr
+= sizeof(DrawElementsIndirectCommand
);
1765 FLUSH_FOR_DRAW(ctx
);
1767 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1769 if (_mesa_is_no_error_enabled(ctx
)) {
1771 _mesa_update_state(ctx
);
1773 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1778 if (skip_validated_draw(ctx
))
1781 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
,
1782 (GLintptr
)indirect
, 0, primcount
,
1787 static void GLAPIENTRY
1788 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1789 GLintptr drawcount_offset
,
1790 GLsizei maxdrawcount
, GLsizei stride
)
1792 GET_CURRENT_CONTEXT(ctx
);
1794 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1795 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1796 "(%s, %lx, %lx, %i, %i)\n",
1797 _mesa_enum_to_string(mode
),
1798 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1799 maxdrawcount
, stride
);
1801 /* If <stride> is zero, the array elements are treated as tightly packed. */
1803 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1805 FLUSH_FOR_DRAW(ctx
);
1807 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1809 if (_mesa_is_no_error_enabled(ctx
)) {
1811 _mesa_update_state(ctx
);
1813 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1816 maxdrawcount
, stride
))
1820 if (skip_validated_draw(ctx
))
1823 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1824 drawcount_offset
, maxdrawcount
,
1825 stride
, ctx
->ParameterBuffer
);
1829 static void GLAPIENTRY
1830 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1832 GLintptr drawcount_offset
,
1833 GLsizei maxdrawcount
, GLsizei stride
)
1835 GET_CURRENT_CONTEXT(ctx
);
1837 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1838 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1839 "(%s, %s, %lx, %lx, %i, %i)\n",
1840 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1841 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1842 maxdrawcount
, stride
);
1844 /* If <stride> is zero, the array elements are treated as tightly packed. */
1846 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1848 FLUSH_FOR_DRAW(ctx
);
1850 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1852 if (_mesa_is_no_error_enabled(ctx
)) {
1854 _mesa_update_state(ctx
);
1856 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1859 maxdrawcount
, stride
))
1863 if (skip_validated_draw(ctx
))
1866 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1867 drawcount_offset
, maxdrawcount
,
1868 stride
, ctx
->ParameterBuffer
);
1873 * Initialize the dispatch table with the VBO functions for drawing.
1876 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1877 struct _glapi_table
*exec
)
1879 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1880 SET_DrawElements(exec
, _mesa_DrawElements
);
1882 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1883 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
1886 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
1887 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
1889 if (ctx
->API
== API_OPENGL_COMPAT
) {
1890 SET_Rectf(exec
, _mesa_exec_Rectf
);
1891 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
1892 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
1895 if (ctx
->API
!= API_OPENGLES
&&
1896 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1897 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
1898 SET_MultiDrawElementsBaseVertex(exec
,
1899 _mesa_MultiDrawElementsBaseVertex
);
1901 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1902 SET_DrawRangeElementsBaseVertex(exec
,
1903 _mesa_DrawRangeElementsBaseVertex
);
1904 SET_DrawElementsInstancedBaseVertex(exec
,
1905 _mesa_exec_DrawElementsInstancedBaseVertex
);
1909 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1910 SET_DrawArraysInstancedBaseInstance(exec
,
1911 _mesa_exec_DrawArraysInstancedBaseInstance
);
1912 SET_DrawElementsInstancedBaseInstance(exec
,
1913 _mesa_exec_DrawElementsInstancedBaseInstance
);
1914 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1915 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1918 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
1919 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
1920 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
1923 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1924 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
1925 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
1928 if (_mesa_is_desktop_gl(ctx
)) {
1929 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
1930 SET_DrawTransformFeedbackStream(exec
,
1931 _mesa_exec_DrawTransformFeedbackStream
);
1932 SET_DrawTransformFeedbackInstanced(exec
,
1933 _mesa_exec_DrawTransformFeedbackInstanced
);
1934 SET_DrawTransformFeedbackStreamInstanced(exec
,
1935 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
1936 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
1937 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
1938 SET_MultiDrawArraysIndirectCountARB(exec
,
1939 _mesa_exec_MultiDrawArraysIndirectCount
);
1940 SET_MultiDrawElementsIndirectCountARB(exec
,
1941 _mesa_exec_MultiDrawElementsIndirectCount
);
1947 /* GL_IBM_multimode_draw_arrays */
1949 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
1950 const GLsizei
* count
,
1951 GLsizei primcount
, GLint modestride
)
1953 GET_CURRENT_CONTEXT(ctx
);
1956 FLUSH_VERTICES(ctx
, 0);
1958 for ( i
= 0 ; i
< primcount
; i
++ ) {
1959 if ( count
[i
] > 0 ) {
1960 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
1961 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
1967 /* GL_IBM_multimode_draw_arrays */
1969 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
1970 GLenum type
, const GLvoid
* const * indices
,
1971 GLsizei primcount
, GLint modestride
)
1973 GET_CURRENT_CONTEXT(ctx
);
1976 FLUSH_VERTICES(ctx
, 0);
1978 /* XXX not sure about ARB_vertex_buffer_object handling here */
1980 for ( i
= 0 ; i
< primcount
; i
++ ) {
1981 if ( count
[i
] > 0 ) {
1982 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
1983 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,