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 if (!bo
->Mappings
[MAP_INTERNAL
].Pointer
) {
78 bo
->Mappings
[MAP_INTERNAL
].Pointer
=
79 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
,
80 GL_MAP_READ_BIT
, bo
, MAP_INTERNAL
);
82 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
83 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
85 switch (array
->Format
.Type
) {
88 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
90 for (k
= 0; k
< array
->Format
.Size
; k
++) {
91 if (IS_INF_OR_NAN(f
[k
]) || f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
92 printf("Bad array data:\n");
93 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
94 printf(" Array %u at %p\n", attrib
, (void *) array
);
95 printf(" Type 0x%x, Size %d, Stride %d\n",
96 array
->Format
.Type
, array
->Format
.Size
,
98 printf(" Address/offset %p in Buffer Object %u\n",
99 array
->Ptr
, bo
->Name
);
100 f
[k
] = 1.0F
; /* XXX replace the bad value! */
102 /*assert(!IS_INF_OR_NAN(f[k])); */
114 * Unmap the buffer object referenced by given array, if mapped.
117 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
120 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
121 if (vao
->Enabled
& VERT_BIT(attrib
)) {
122 const struct gl_vertex_buffer_binding
*binding
=
123 &vao
->BufferBinding
[array
->BufferBindingIndex
];
124 struct gl_buffer_object
*bo
= binding
->BufferObj
;
125 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
126 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
133 sizeof_ib_type(GLenum type
)
136 case GL_UNSIGNED_INT
:
137 return sizeof(GLuint
);
138 case GL_UNSIGNED_SHORT
:
139 return sizeof(GLushort
);
140 case GL_UNSIGNED_BYTE
:
141 return sizeof(GLubyte
);
143 assert(!"unsupported index data type");
144 /* In case assert is turned off */
150 * Examine the array's data for NaNs, etc.
151 * For debug purposes; not normally used.
154 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
155 GLenum elemType
, const void *elements
,
158 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
163 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
164 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
165 vao
->IndexBufferObj
->Size
,
167 vao
->IndexBufferObj
, MAP_INTERNAL
);
168 elements
= ADD_POINTERS(elements
, elemMap
);
171 for (i
= 0; i
< count
; i
++) {
176 case GL_UNSIGNED_BYTE
:
177 j
= ((const GLubyte
*) elements
)[i
];
179 case GL_UNSIGNED_SHORT
:
180 j
= ((const GLushort
*) elements
)[i
];
182 case GL_UNSIGNED_INT
:
183 j
= ((const GLuint
*) elements
)[i
];
186 unreachable("Unexpected index buffer type");
189 /* check element j of each enabled array */
190 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
191 check_array_data(ctx
, vao
, k
, j
);
195 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
196 ctx
->Driver
.UnmapBuffer(ctx
, vao
->IndexBufferObj
, MAP_INTERNAL
);
199 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
200 unmap_array_buffer(ctx
, vao
, k
);
206 * Check array data, looking for NaNs, etc.
209 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
216 * Check if we should skip the draw call even after validation was successful.
219 skip_validated_draw(struct gl_context
*ctx
)
223 /* For ES2, we can draw if we have a vertex program/shader). */
224 return ctx
->VertexProgram
._Current
== NULL
;
227 /* For OpenGL ES, only draw if we have vertex positions
229 if (!(ctx
->Array
.VAO
->Enabled
& VERT_BIT_POS
))
233 case API_OPENGL_CORE
:
234 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
237 * "If there is no active program for the vertex or fragment shader
238 * stages, the results of vertex and/or fragment processing will be
239 * undefined. However, this is not an error."
241 * The fragment shader is not tested here because other state (e.g.,
242 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
244 return ctx
->VertexProgram
._Current
== NULL
;
246 case API_OPENGL_COMPAT
:
247 if (ctx
->VertexProgram
._Current
!= NULL
) {
248 /* Draw regardless of whether or not we have any vertex arrays.
249 * (Ex: could draw a point using a constant vertex pos)
253 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
256 return !(ctx
->Array
.VAO
->Enabled
& (VERT_BIT_POS
|VERT_BIT_GENERIC0
));
261 unreachable("Invalid API value in check_valid_to_render()");
269 * Print info/data for glDrawArrays(), for debugging.
272 print_draw_arrays(struct gl_context
*ctx
,
273 GLenum mode
, GLint start
, GLsizei count
)
275 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
277 printf("_mesa_DrawArrays(mode 0x%x, start %d, count %d):\n",
280 GLbitfield mask
= vao
->Enabled
;
282 const gl_vert_attrib i
= u_bit_scan(&mask
);
283 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
285 const struct gl_vertex_buffer_binding
*binding
=
286 &vao
->BufferBinding
[array
->BufferBindingIndex
];
287 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
289 printf("attr %s: size %d stride %d "
290 "ptr %p Bufobj %u\n",
291 gl_vert_attrib_name((gl_vert_attrib
) i
),
292 array
->Format
.Size
, binding
->Stride
,
293 array
->Ptr
, bufObj
->Name
);
295 if (_mesa_is_bufferobj(bufObj
)) {
296 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
297 GL_MAP_READ_BIT
, bufObj
,
299 int offset
= (int) (GLintptr
)
300 _mesa_vertex_attrib_address(array
, binding
);
303 switch (array
->Format
.Type
) {
306 case GL_UNSIGNED_INT64_ARB
:
313 float *f
= (float *) (p
+ offset
);
316 int n
= (count
- 1) * (binding
->Stride
/ (4 * multiplier
))
317 + array
->Format
.Size
;
320 printf(" Data at offset %d:\n", offset
);
323 printf(" double[%d] = 0x%016llx %lf\n", i
,
324 ((unsigned long long *) k
)[i
], ((double *) f
)[i
]);
326 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
329 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
336 * Return a filter mask for the net enabled vao arrays.
337 * This is to mask out arrays that would otherwise supersed required current
338 * values for the fixed function shaders for example.
341 enabled_filter(const struct gl_context
*ctx
)
343 switch (ctx
->VertexProgram
._VPMode
) {
345 /* When no vertex program is active (or the vertex program is generated
346 * from fixed-function state). We put the material values into the
347 * generic slots. Since the vao has no material arrays, mute these
348 * slots from the enabled arrays so that the current material values
349 * are pulled instead of the vao arrays.
351 return VERT_BIT_FF_ALL
;
354 /* There are no shaders in OpenGL ES 1.x, so this code path should be
355 * impossible to reach. The meta code is careful to not use shaders in
358 assert(ctx
->API
!= API_OPENGLES
);
360 /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
361 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
362 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
365 if (ctx
->API
!= API_OPENGL_COMPAT
)
366 return VERT_BIT_GENERIC_ALL
;
378 * Helper function called by the other DrawArrays() functions below.
379 * This is where we handle primitive restart for drawing non-indexed
380 * arrays. If primitive restart is enabled, it typically means
381 * splitting one DrawArrays() into two.
384 _mesa_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
385 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
388 struct _mesa_prim prim
;
390 if (skip_validated_draw(ctx
))
393 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
396 memset(&prim
, 0, sizeof(prim
));
400 prim
.num_instances
= numInstances
;
401 prim
.base_instance
= baseInstance
;
402 prim
.draw_id
= drawID
;
403 prim
.is_indirect
= 0;
407 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
408 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
410 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
417 * Execute a glRectf() function.
419 static void GLAPIENTRY
420 _mesa_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
422 GET_CURRENT_CONTEXT(ctx
);
423 ASSERT_OUTSIDE_BEGIN_END(ctx
);
425 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
426 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
427 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
428 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
429 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
430 CALL_End(GET_DISPATCH(), ());
434 static void GLAPIENTRY
435 _mesa_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
437 GET_CURRENT_CONTEXT(ctx
);
447 prim
= GL_LINE_STRIP
;
450 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
454 /* No effect if vertex maps disabled.
456 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
459 du
= ctx
->Eval
.MapGrid1du
;
460 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
462 CALL_Begin(GET_DISPATCH(), (prim
));
463 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
464 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
466 CALL_End(GET_DISPATCH(), ());
470 static void GLAPIENTRY
471 _mesa_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
473 GET_CURRENT_CONTEXT(ctx
);
474 GLfloat u
, du
, v
, dv
, v1
, u1
;
483 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
487 /* No effect if vertex maps disabled.
489 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
492 du
= ctx
->Eval
.MapGrid2du
;
493 dv
= ctx
->Eval
.MapGrid2dv
;
494 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
495 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
499 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
500 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
501 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
502 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
505 CALL_End(GET_DISPATCH(), ());
508 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
509 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
510 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
511 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
513 CALL_End(GET_DISPATCH(), ());
515 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
516 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
517 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
518 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
520 CALL_End(GET_DISPATCH(), ());
524 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
525 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
526 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
527 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
528 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
530 CALL_End(GET_DISPATCH(), ());
538 * Called from glDrawArrays when in immediate mode (not display list mode).
541 _mesa_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
543 GET_CURRENT_CONTEXT(ctx
);
545 if (MESA_VERBOSE
& VERBOSE_DRAW
)
546 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
547 _mesa_enum_to_string(mode
), start
, count
);
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_DrawArrays(ctx
, mode
, count
))
562 check_draw_arrays_data(ctx
, start
, count
);
564 _mesa_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
567 print_draw_arrays(ctx
, mode
, start
, count
);
572 * Called from glDrawArraysInstanced when in immediate mode (not
573 * display list mode).
576 _mesa_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
577 GLsizei numInstances
)
579 GET_CURRENT_CONTEXT(ctx
);
581 if (MESA_VERBOSE
& VERBOSE_DRAW
)
582 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
583 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
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
, start
, count
,
599 check_draw_arrays_data(ctx
, start
, count
);
601 _mesa_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
604 print_draw_arrays(ctx
, mode
, start
, count
);
609 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
611 static void GLAPIENTRY
612 _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
613 GLsizei count
, GLsizei numInstances
,
616 GET_CURRENT_CONTEXT(ctx
);
618 if (MESA_VERBOSE
& VERBOSE_DRAW
)
620 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
621 _mesa_enum_to_string(mode
), first
, count
,
622 numInstances
, baseInstance
);
626 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
628 if (_mesa_is_no_error_enabled(ctx
)) {
630 _mesa_update_state(ctx
);
632 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
638 check_draw_arrays_data(ctx
, first
, count
);
640 _mesa_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
643 print_draw_arrays(ctx
, mode
, first
, count
);
648 * Called from glMultiDrawArrays when in immediate mode.
650 static void GLAPIENTRY
651 _mesa_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
652 const GLsizei
*count
, GLsizei primcount
)
654 GET_CURRENT_CONTEXT(ctx
);
657 if (MESA_VERBOSE
& VERBOSE_DRAW
)
659 "glMultiDrawArrays(%s, %p, %p, %d)\n",
660 _mesa_enum_to_string(mode
), first
, count
, primcount
);
664 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
666 if (_mesa_is_no_error_enabled(ctx
)) {
668 _mesa_update_state(ctx
);
670 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
674 for (i
= 0; i
< primcount
; i
++) {
677 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
679 /* The GL_ARB_shader_draw_parameters spec adds the following after the
680 * pseudo-code describing glMultiDrawArrays:
682 * "The index of the draw (<i> in the above pseudo-code) may be
683 * read by a vertex shader as <gl_DrawIDARB>, as described in
686 _mesa_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
689 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
697 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
702 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
705 ctx
->Driver
.MapBufferRange(ctx
, 0,
706 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
708 ctx
->Array
.VAO
->IndexBufferObj
,
711 case GL_UNSIGNED_BYTE
:
713 const GLubyte
*us
= (const GLubyte
*) map
;
715 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
716 printf("%02x ", us
[i
]);
723 case GL_UNSIGNED_SHORT
:
725 const GLushort
*us
= (const GLushort
*) map
;
727 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
728 printf("%04x ", us
[i
]);
735 case GL_UNSIGNED_INT
:
737 const GLuint
*us
= (const GLuint
*) map
;
739 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
740 printf("%08x ", us
[i
]);
751 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
757 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
758 const GLvoid
*indices
)
763 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
765 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
768 if (skip_validated_draw(ctx
))
776 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
777 * Do the rendering for a glDrawElements or glDrawRangeElements call after
778 * we've validated buffer bounds, etc.
781 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
782 GLboolean index_bounds_valid
,
783 GLuint start
, GLuint end
,
784 GLsizei count
, GLenum type
,
785 const GLvoid
* indices
,
786 GLint basevertex
, GLuint numInstances
,
789 struct _mesa_index_buffer ib
;
790 struct _mesa_prim prim
;
792 if (!index_bounds_valid
) {
797 if (skip_draw_elements(ctx
, count
, indices
))
801 ib
.index_size
= sizeof_ib_type(type
);
802 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
812 prim
.is_indirect
= 0;
813 prim
.basevertex
= basevertex
;
814 prim
.num_instances
= numInstances
;
815 prim
.base_instance
= baseInstance
;
818 /* Need to give special consideration to rendering a range of
819 * indices starting somewhere above zero. Typically the
820 * application is issuing multiple DrawRangeElements() to draw
821 * successive primitives layed out linearly in the vertex arrays.
822 * Unless the vertex arrays are all in a VBO (or locked as with
823 * CVA), the OpenGL semantics imply that we need to re-read or
824 * re-upload the vertex data on each draw call.
826 * In the case of hardware tnl, we want to avoid starting the
827 * upload at zero, as it will mean every draw call uploads an
828 * increasing amount of not-used vertex data. Worse - in the
829 * software tnl module, all those vertices might be transformed and
830 * lit but never rendered.
832 * If we just upload or transform the vertices in start..end,
833 * however, the indices will be incorrect.
835 * At this level, we don't know exactly what the requirements of
836 * the backend are going to be, though it will likely boil down to
839 * 1) Do nothing, everything is in a VBO and is processed once
842 * 2) Adjust the indices and vertex arrays so that start becomes
845 * Rather than doing anything here, I'll provide a helper function
846 * for the latter case elsewhere.
849 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
850 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
852 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
859 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
862 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
863 GLsizei count
, GLenum type
,
864 const GLvoid
* indices
, GLint basevertex
)
866 static GLuint warnCount
= 0;
867 GLboolean index_bounds_valid
= GL_TRUE
;
869 /* This is only useful to catch invalid values in the "end" parameter
872 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
874 GET_CURRENT_CONTEXT(ctx
);
876 if (MESA_VERBOSE
& VERBOSE_DRAW
)
878 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
879 _mesa_enum_to_string(mode
), start
, end
, count
,
880 _mesa_enum_to_string(type
), indices
, basevertex
);
884 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
886 if (_mesa_is_no_error_enabled(ctx
)) {
888 _mesa_update_state(ctx
);
890 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
895 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
896 /* The application requested we draw using a range of indices that's
897 * outside the bounds of the current VBO. This is invalid and appears
898 * to give undefined results. The safest thing to do is to simply
899 * ignore the range, in case the application botched their range tracking
900 * but did provide valid indices. Also issue a warning indicating that
901 * the application is broken.
903 if (warnCount
++ < 10) {
904 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
905 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
906 "\trange is outside VBO bounds (max=%u); ignoring.\n"
907 "\tThis should be fixed in the application.",
908 start
, end
, basevertex
, count
, type
, indices
,
911 index_bounds_valid
= GL_FALSE
;
914 /* NOTE: It's important that 'end' is a reasonable value.
915 * in _tnl_draw_prims(), we use end to determine how many vertices
916 * to transform. If it's too large, we can unnecessarily split prims
917 * or we can read/write out of memory in several different places!
920 /* Catch/fix some potential user errors */
921 if (type
== GL_UNSIGNED_BYTE
) {
922 start
= MIN2(start
, 0xff);
923 end
= MIN2(end
, 0xff);
925 else if (type
== GL_UNSIGNED_SHORT
) {
926 start
= MIN2(start
, 0xffff);
927 end
= MIN2(end
, 0xffff);
931 printf("glDraw[Range]Elements{,BaseVertex}"
932 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
934 start
, end
, type
, count
,
935 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
938 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
939 index_bounds_valid
= GL_FALSE
;
942 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
944 (void) check_draw_elements_data
;
947 if (!index_bounds_valid
) {
952 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
953 count
, type
, indices
, basevertex
, 1, 0);
958 * Called by glDrawRangeElements() in immediate mode.
961 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
962 GLsizei count
, GLenum type
, const GLvoid
* indices
)
964 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
965 GET_CURRENT_CONTEXT(ctx
);
967 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
968 _mesa_enum_to_string(mode
), start
, end
, count
,
969 _mesa_enum_to_string(type
), indices
);
972 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
978 * Called by glDrawElements() in immediate mode.
981 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
982 const GLvoid
* indices
)
984 GET_CURRENT_CONTEXT(ctx
);
986 if (MESA_VERBOSE
& VERBOSE_DRAW
)
987 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
988 _mesa_enum_to_string(mode
), count
,
989 _mesa_enum_to_string(type
), indices
);
993 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
995 if (_mesa_is_no_error_enabled(ctx
)) {
997 _mesa_update_state(ctx
);
999 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1003 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1004 count
, type
, indices
, 0, 1, 0);
1009 * Called by glDrawElementsBaseVertex() in immediate mode.
1012 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1013 const GLvoid
* indices
, GLint basevertex
)
1015 GET_CURRENT_CONTEXT(ctx
);
1017 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1018 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1019 _mesa_enum_to_string(mode
), count
,
1020 _mesa_enum_to_string(type
), indices
);
1022 FLUSH_FOR_DRAW(ctx
);
1024 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1026 if (_mesa_is_no_error_enabled(ctx
)) {
1028 _mesa_update_state(ctx
);
1030 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1034 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1035 count
, type
, indices
, basevertex
, 1, 0);
1040 * Called by glDrawElementsInstanced() in immediate mode.
1042 static void GLAPIENTRY
1043 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1044 const GLvoid
* indices
, GLsizei numInstances
)
1046 GET_CURRENT_CONTEXT(ctx
);
1048 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1049 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1050 _mesa_enum_to_string(mode
), count
,
1051 _mesa_enum_to_string(type
), indices
);
1053 FLUSH_FOR_DRAW(ctx
);
1055 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1057 if (_mesa_is_no_error_enabled(ctx
)) {
1059 _mesa_update_state(ctx
);
1061 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1062 indices
, numInstances
))
1066 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1067 count
, type
, indices
, 0, numInstances
, 0);
1072 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1074 static void GLAPIENTRY
1075 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1076 GLenum type
, const GLvoid
* indices
,
1077 GLsizei numInstances
,
1080 GET_CURRENT_CONTEXT(ctx
);
1082 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1084 "glDrawElementsInstancedBaseVertex"
1085 "(%s, %d, %s, %p, %d; %d)\n",
1086 _mesa_enum_to_string(mode
), count
,
1087 _mesa_enum_to_string(type
), indices
,
1088 numInstances
, basevertex
);
1090 FLUSH_FOR_DRAW(ctx
);
1092 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1094 if (_mesa_is_no_error_enabled(ctx
)) {
1096 _mesa_update_state(ctx
);
1098 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1099 indices
, numInstances
))
1103 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1104 count
, type
, indices
,
1105 basevertex
, numInstances
, 0);
1110 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1112 static void GLAPIENTRY
1113 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1115 const GLvoid
*indices
,
1116 GLsizei numInstances
,
1117 GLuint baseInstance
)
1119 GET_CURRENT_CONTEXT(ctx
);
1121 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1123 "glDrawElementsInstancedBaseInstance"
1124 "(%s, %d, %s, %p, %d, %d)\n",
1125 _mesa_enum_to_string(mode
), count
,
1126 _mesa_enum_to_string(type
), indices
,
1127 numInstances
, baseInstance
);
1129 FLUSH_FOR_DRAW(ctx
);
1131 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1133 if (_mesa_is_no_error_enabled(ctx
)) {
1135 _mesa_update_state(ctx
);
1137 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1138 indices
, numInstances
))
1142 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1143 count
, type
, indices
, 0, numInstances
,
1149 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1151 static void GLAPIENTRY
1152 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1155 const GLvoid
*indices
,
1156 GLsizei numInstances
,
1158 GLuint baseInstance
)
1160 GET_CURRENT_CONTEXT(ctx
);
1162 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1164 "glDrawElementsInstancedBaseVertexBaseInstance"
1165 "(%s, %d, %s, %p, %d, %d, %d)\n",
1166 _mesa_enum_to_string(mode
), count
,
1167 _mesa_enum_to_string(type
), indices
,
1168 numInstances
, basevertex
, baseInstance
);
1170 FLUSH_FOR_DRAW(ctx
);
1172 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1174 if (_mesa_is_no_error_enabled(ctx
)) {
1176 _mesa_update_state(ctx
);
1178 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1179 indices
, numInstances
))
1183 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1184 count
, type
, indices
, basevertex
,
1185 numInstances
, baseInstance
);
1190 * Inner support for both _mesa_MultiDrawElements() and
1191 * _mesa_MultiDrawRangeElements().
1192 * This does the actual rendering after we've checked array indexes, etc.
1195 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1196 const GLsizei
*count
, GLenum type
,
1197 const GLvoid
* const *indices
,
1198 GLsizei primcount
, const GLint
*basevertex
)
1200 struct _mesa_index_buffer ib
;
1201 struct _mesa_prim
*prim
;
1202 unsigned int index_type_size
= sizeof_ib_type(type
);
1203 uintptr_t min_index_ptr
, max_index_ptr
;
1204 GLboolean fallback
= GL_FALSE
;
1210 prim
= calloc(primcount
, sizeof(*prim
));
1212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1216 min_index_ptr
= (uintptr_t) indices
[0];
1218 for (i
= 0; i
< primcount
; i
++) {
1219 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1220 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1221 index_type_size
* count
[i
]);
1224 /* Check if we can handle this thing as a bunch of index offsets from the
1225 * same index pointer. If we can't, then we have to fall back to doing
1226 * a draw_prims per primitive.
1227 * Check that the difference between each prim's indexes is a multiple of
1228 * the index/element size.
1230 if (index_type_size
!= 1) {
1231 for (i
= 0; i
< primcount
; i
++) {
1232 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1240 /* Draw primitives individually if one count is zero, so we can easily skip
1243 for (i
= 0; i
< primcount
; i
++) {
1244 if (count
[i
] == 0) {
1250 /* If the index buffer isn't in a VBO, then treating the application's
1251 * subranges of the index buffer as one large index buffer may lead to
1252 * us reading unmapped memory.
1254 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1258 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1259 ib
.index_size
= sizeof_ib_type(type
);
1260 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1261 ib
.ptr
= (void *) min_index_ptr
;
1263 for (i
= 0; i
< primcount
; i
++) {
1264 prim
[i
].begin
= (i
== 0);
1265 prim
[i
].end
= (i
== primcount
- 1);
1267 prim
[i
].mode
= mode
;
1269 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1270 prim
[i
].count
= count
[i
];
1271 prim
[i
].indexed
= 1;
1272 prim
[i
].num_instances
= 1;
1273 prim
[i
].base_instance
= 0;
1274 prim
[i
].draw_id
= i
;
1275 prim
[i
].is_indirect
= 0;
1276 if (basevertex
!= NULL
)
1277 prim
[i
].basevertex
= basevertex
[i
];
1279 prim
[i
].basevertex
= 0;
1282 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1283 false, 0, ~0, NULL
, 0, NULL
);
1286 /* render one prim at a time */
1287 for (i
= 0; i
< primcount
; i
++) {
1290 ib
.count
= count
[i
];
1291 ib
.index_size
= sizeof_ib_type(type
);
1292 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1293 ib
.ptr
= indices
[i
];
1298 prim
[0].mode
= mode
;
1300 prim
[0].count
= count
[i
];
1301 prim
[0].indexed
= 1;
1302 prim
[0].num_instances
= 1;
1303 prim
[0].base_instance
= 0;
1304 prim
[0].draw_id
= i
;
1305 prim
[0].is_indirect
= 0;
1306 if (basevertex
!= NULL
)
1307 prim
[0].basevertex
= basevertex
[i
];
1309 prim
[0].basevertex
= 0;
1311 ctx
->Driver
.Draw(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1317 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1324 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1325 const GLvoid
* const *indices
, GLsizei primcount
)
1327 GET_CURRENT_CONTEXT(ctx
);
1329 FLUSH_FOR_DRAW(ctx
);
1331 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1333 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 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1347 const GLsizei
*count
, GLenum type
,
1348 const GLvoid
* const *indices
,
1350 const GLsizei
*basevertex
)
1352 GET_CURRENT_CONTEXT(ctx
);
1354 FLUSH_FOR_DRAW(ctx
);
1356 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1358 if (_mesa_is_no_error_enabled(ctx
)) {
1360 _mesa_update_state(ctx
);
1362 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1367 if (skip_validated_draw(ctx
))
1370 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1376 * Draw a GL primitive using a vertex count obtained from transform feedback.
1377 * \param mode the type of GL primitive to draw
1378 * \param obj the transform feedback object to use
1379 * \param stream index of the transform feedback stream from which to
1380 * get the primitive count.
1381 * \param numInstances number of instances to draw
1384 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1385 struct gl_transform_feedback_object
*obj
,
1386 GLuint stream
, GLuint numInstances
)
1388 struct _mesa_prim prim
;
1390 FLUSH_FOR_DRAW(ctx
);
1392 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1394 if (_mesa_is_no_error_enabled(ctx
)) {
1396 _mesa_update_state(ctx
);
1398 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1404 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1405 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1406 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1408 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1409 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1413 if (skip_validated_draw(ctx
))
1416 /* init most fields to zero */
1417 memset(&prim
, 0, sizeof(prim
));
1421 prim
.num_instances
= numInstances
;
1422 prim
.base_instance
= 0;
1423 prim
.is_indirect
= 0;
1425 /* Maybe we should do some primitive splitting for primitive restart
1426 * (like in DrawArrays), but we have no way to know how many vertices
1427 * will be rendered. */
1429 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1431 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1438 * Like DrawArrays, but take the count from a transform feedback object.
1439 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1440 * \param name the transform feedback object
1441 * User still has to setup of the vertex attribute info with
1442 * glVertexPointer, glColorPointer, etc.
1443 * Part of GL_ARB_transform_feedback2.
1446 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1448 GET_CURRENT_CONTEXT(ctx
);
1449 struct gl_transform_feedback_object
*obj
=
1450 _mesa_lookup_transform_feedback_object(ctx
, name
);
1452 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1453 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1454 _mesa_enum_to_string(mode
), name
);
1456 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1460 static void GLAPIENTRY
1461 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1463 GET_CURRENT_CONTEXT(ctx
);
1464 struct gl_transform_feedback_object
*obj
=
1465 _mesa_lookup_transform_feedback_object(ctx
, name
);
1467 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1468 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1469 _mesa_enum_to_string(mode
), name
, stream
);
1471 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1475 static void GLAPIENTRY
1476 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1479 GET_CURRENT_CONTEXT(ctx
);
1480 struct gl_transform_feedback_object
*obj
=
1481 _mesa_lookup_transform_feedback_object(ctx
, name
);
1483 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1484 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1485 _mesa_enum_to_string(mode
), name
);
1487 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1491 static void GLAPIENTRY
1492 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1496 GET_CURRENT_CONTEXT(ctx
);
1497 struct gl_transform_feedback_object
*obj
=
1498 _mesa_lookup_transform_feedback_object(ctx
, name
);
1500 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1501 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1502 "(%s, %u, %u, %i)\n",
1503 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1505 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1510 _mesa_validated_drawarraysindirect(struct gl_context
*ctx
,
1511 GLenum mode
, const GLvoid
*indirect
)
1513 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1514 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1515 1 /* draw_count */ , 16 /* stride */ ,
1518 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1524 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1526 const GLvoid
*indirect
,
1527 GLsizei primcount
, GLsizei stride
)
1529 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1534 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1535 primcount
, stride
, NULL
, 0, NULL
);
1537 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1543 _mesa_validated_drawelementsindirect(struct gl_context
*ctx
,
1544 GLenum mode
, GLenum type
,
1545 const GLvoid
*indirect
)
1547 struct _mesa_index_buffer ib
;
1549 ib
.count
= 0; /* unknown */
1550 ib
.index_size
= sizeof_ib_type(type
);
1551 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1554 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1555 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1556 1 /* draw_count */ , 20 /* stride */ ,
1559 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1565 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1566 GLenum mode
, GLenum type
,
1567 const GLvoid
*indirect
,
1568 GLsizei primcount
, GLsizei stride
)
1570 struct _mesa_index_buffer ib
;
1571 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1576 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1578 ib
.count
= 0; /* unknown */
1579 ib
.index_size
= sizeof_ib_type(type
);
1580 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1583 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1584 ctx
->DrawIndirectBuffer
, offset
,
1585 primcount
, stride
, NULL
, 0, &ib
);
1587 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1593 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1596 static void GLAPIENTRY
1597 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1599 GET_CURRENT_CONTEXT(ctx
);
1601 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1602 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1603 _mesa_enum_to_string(mode
), indirect
);
1605 /* From the ARB_draw_indirect spec:
1607 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1608 * compatibility profile, this indicates that DrawArraysIndirect and
1609 * DrawElementsIndirect are to source their arguments directly from the
1610 * pointer passed as their <indirect> parameters."
1612 if (ctx
->API
== API_OPENGL_COMPAT
&&
1613 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1614 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1616 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1622 FLUSH_FOR_DRAW(ctx
);
1624 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1626 if (_mesa_is_no_error_enabled(ctx
)) {
1628 _mesa_update_state(ctx
);
1630 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1634 if (skip_validated_draw(ctx
))
1637 _mesa_validated_drawarraysindirect(ctx
, mode
, indirect
);
1641 static void GLAPIENTRY
1642 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1644 GET_CURRENT_CONTEXT(ctx
);
1646 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1647 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1648 _mesa_enum_to_string(mode
),
1649 _mesa_enum_to_string(type
), indirect
);
1651 /* From the ARB_draw_indirect spec:
1653 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1654 * compatibility profile, this indicates that DrawArraysIndirect and
1655 * DrawElementsIndirect are to source their arguments directly from the
1656 * pointer passed as their <indirect> parameters."
1658 if (ctx
->API
== API_OPENGL_COMPAT
&&
1659 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1661 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1662 * may not come from a client array and must come from an index buffer.
1663 * If no element array buffer is bound, an INVALID_OPERATION error is
1666 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1668 "glDrawElementsIndirect(no buffer bound "
1669 "to GL_ELEMENT_ARRAY_BUFFER)");
1671 DrawElementsIndirectCommand
*cmd
=
1672 (DrawElementsIndirectCommand
*) indirect
;
1674 /* Convert offset to pointer */
1675 void *offset
= (void *)
1676 ((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1678 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1688 FLUSH_FOR_DRAW(ctx
);
1690 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1692 if (_mesa_is_no_error_enabled(ctx
)) {
1694 _mesa_update_state(ctx
);
1696 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1700 if (skip_validated_draw(ctx
))
1703 _mesa_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1707 static void GLAPIENTRY
1708 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1709 GLsizei primcount
, GLsizei stride
)
1711 GET_CURRENT_CONTEXT(ctx
);
1713 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1714 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1715 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1717 /* If <stride> is zero, the array elements are treated as tightly packed. */
1719 stride
= sizeof(DrawArraysIndirectCommand
);
1721 /* From the ARB_draw_indirect spec:
1723 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1724 * compatibility profile, this indicates that DrawArraysIndirect and
1725 * DrawElementsIndirect are to source their arguments directly from the
1726 * pointer passed as their <indirect> parameters."
1728 if (ctx
->API
== API_OPENGL_COMPAT
&&
1729 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1731 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1732 "glMultiDrawArraysIndirect"))
1735 const ubyte
*ptr
= (const ubyte
*) indirect
;
1736 for (unsigned i
= 0; i
< primcount
; i
++) {
1737 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1738 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1739 cmd
->count
, cmd
->primCount
,
1743 ptr
+= sizeof(DrawArraysIndirectCommand
);
1752 FLUSH_FOR_DRAW(ctx
);
1754 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1756 if (_mesa_is_no_error_enabled(ctx
)) {
1758 _mesa_update_state(ctx
);
1760 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1765 if (skip_validated_draw(ctx
))
1768 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1773 static void GLAPIENTRY
1774 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1775 const GLvoid
*indirect
,
1776 GLsizei primcount
, GLsizei stride
)
1778 GET_CURRENT_CONTEXT(ctx
);
1780 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1781 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1782 _mesa_enum_to_string(mode
),
1783 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1785 /* If <stride> is zero, the array elements are treated as tightly packed. */
1787 stride
= sizeof(DrawElementsIndirectCommand
);
1790 /* From the ARB_draw_indirect spec:
1792 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1793 * compatibility profile, this indicates that DrawArraysIndirect and
1794 * DrawElementsIndirect are to source their arguments directly from the
1795 * pointer passed as their <indirect> parameters."
1797 if (ctx
->API
== API_OPENGL_COMPAT
&&
1798 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1800 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1801 * may not come from a client array and must come from an index buffer.
1802 * If no element array buffer is bound, an INVALID_OPERATION error is
1805 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1806 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1807 "glMultiDrawElementsIndirect(no buffer bound "
1808 "to GL_ELEMENT_ARRAY_BUFFER)");
1813 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1814 "glMultiDrawArraysIndirect"))
1817 const ubyte
*ptr
= (const ubyte
*) indirect
;
1818 for (unsigned i
= 0; i
< primcount
; i
++) {
1819 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1822 ptr
+= sizeof(DrawElementsIndirectCommand
);
1831 FLUSH_FOR_DRAW(ctx
);
1833 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1835 if (_mesa_is_no_error_enabled(ctx
)) {
1837 _mesa_update_state(ctx
);
1839 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1844 if (skip_validated_draw(ctx
))
1847 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1853 _mesa_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1856 GLintptr drawcount_offset
,
1857 GLsizei maxdrawcount
,
1860 GLsizeiptr offset
= indirect
;
1862 if (maxdrawcount
== 0)
1865 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1866 ctx
->DrawIndirectBuffer
, offset
,
1867 maxdrawcount
, stride
,
1868 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1870 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1876 _mesa_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1877 GLenum mode
, GLenum type
,
1879 GLintptr drawcount_offset
,
1880 GLsizei maxdrawcount
,
1883 struct _mesa_index_buffer ib
;
1884 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1886 if (maxdrawcount
== 0)
1889 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1891 ib
.count
= 0; /* unknown */
1892 ib
.index_size
= sizeof_ib_type(type
);
1893 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1896 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1897 ctx
->DrawIndirectBuffer
, offset
,
1898 maxdrawcount
, stride
,
1899 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1901 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1906 static void GLAPIENTRY
1907 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1908 GLintptr drawcount_offset
,
1909 GLsizei maxdrawcount
, GLsizei stride
)
1911 GET_CURRENT_CONTEXT(ctx
);
1913 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1914 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1915 "(%s, %lx, %lx, %i, %i)\n",
1916 _mesa_enum_to_string(mode
),
1917 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1918 maxdrawcount
, stride
);
1920 /* If <stride> is zero, the array elements are treated as tightly packed. */
1922 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1924 FLUSH_FOR_DRAW(ctx
);
1926 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1928 if (_mesa_is_no_error_enabled(ctx
)) {
1930 _mesa_update_state(ctx
);
1932 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1935 maxdrawcount
, stride
))
1939 if (skip_validated_draw(ctx
))
1942 _mesa_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1944 maxdrawcount
, stride
);
1948 static void GLAPIENTRY
1949 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1951 GLintptr drawcount_offset
,
1952 GLsizei maxdrawcount
, GLsizei stride
)
1954 GET_CURRENT_CONTEXT(ctx
);
1956 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1957 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1958 "(%s, %s, %lx, %lx, %i, %i)\n",
1959 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1960 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1961 maxdrawcount
, stride
);
1963 /* If <stride> is zero, the array elements are treated as tightly packed. */
1965 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1967 FLUSH_FOR_DRAW(ctx
);
1969 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1971 if (_mesa_is_no_error_enabled(ctx
)) {
1973 _mesa_update_state(ctx
);
1975 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1978 maxdrawcount
, stride
))
1982 if (skip_validated_draw(ctx
))
1985 _mesa_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1986 drawcount_offset
, maxdrawcount
,
1992 * Initialize the dispatch table with the VBO functions for drawing.
1995 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1996 struct _glapi_table
*exec
)
1998 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1999 SET_DrawElements(exec
, _mesa_DrawElements
);
2001 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2002 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
2005 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
2006 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
2008 if (ctx
->API
== API_OPENGL_COMPAT
) {
2009 SET_Rectf(exec
, _mesa_exec_Rectf
);
2010 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
2011 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
2014 if (ctx
->API
!= API_OPENGLES
&&
2015 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
2016 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
2017 SET_MultiDrawElementsBaseVertex(exec
,
2018 _mesa_MultiDrawElementsBaseVertex
);
2020 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2021 SET_DrawRangeElementsBaseVertex(exec
,
2022 _mesa_DrawRangeElementsBaseVertex
);
2023 SET_DrawElementsInstancedBaseVertex(exec
,
2024 _mesa_exec_DrawElementsInstancedBaseVertex
);
2028 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2029 SET_DrawArraysInstancedBaseInstance(exec
,
2030 _mesa_exec_DrawArraysInstancedBaseInstance
);
2031 SET_DrawElementsInstancedBaseInstance(exec
,
2032 _mesa_exec_DrawElementsInstancedBaseInstance
);
2033 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
2034 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
2037 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
2038 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
2039 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
2042 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2043 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
2044 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
2047 if (_mesa_is_desktop_gl(ctx
)) {
2048 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
2049 SET_DrawTransformFeedbackStream(exec
,
2050 _mesa_exec_DrawTransformFeedbackStream
);
2051 SET_DrawTransformFeedbackInstanced(exec
,
2052 _mesa_exec_DrawTransformFeedbackInstanced
);
2053 SET_DrawTransformFeedbackStreamInstanced(exec
,
2054 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
2055 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
2056 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
2057 SET_MultiDrawArraysIndirectCountARB(exec
,
2058 _mesa_exec_MultiDrawArraysIndirectCount
);
2059 SET_MultiDrawElementsIndirectCountARB(exec
,
2060 _mesa_exec_MultiDrawElementsIndirectCount
);
2066 /* GL_IBM_multimode_draw_arrays */
2068 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
2069 const GLsizei
* count
,
2070 GLsizei primcount
, GLint modestride
)
2072 GET_CURRENT_CONTEXT(ctx
);
2075 FLUSH_VERTICES(ctx
, 0);
2077 for ( i
= 0 ; i
< primcount
; i
++ ) {
2078 if ( count
[i
] > 0 ) {
2079 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2080 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
2086 /* GL_IBM_multimode_draw_arrays */
2088 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
2089 GLenum type
, const GLvoid
* const * indices
,
2090 GLsizei primcount
, GLint modestride
)
2092 GET_CURRENT_CONTEXT(ctx
);
2095 FLUSH_VERTICES(ctx
, 0);
2097 /* XXX not sure about ARB_vertex_buffer_object handling here */
2099 for ( i
= 0 ; i
< primcount
; i
++ ) {
2100 if ( count
[i
] > 0 ) {
2101 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2102 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,
2110 * Helper function for _mesa_draw_indirect below that additionally takes a zero
2111 * initialized array of _mesa_prim scratch space memory as the last argument.
2114 draw_indirect(struct gl_context
*ctx
, GLuint mode
,
2115 struct gl_buffer_object
*indirect_data
,
2116 GLsizeiptr indirect_offset
, unsigned draw_count
,
2118 struct gl_buffer_object
*indirect_draw_count_buffer
,
2119 GLsizeiptr indirect_draw_count_offset
,
2120 const struct _mesa_index_buffer
*ib
,
2121 struct _mesa_prim
*prim
)
2124 prim
[draw_count
- 1].end
= 1;
2125 for (unsigned i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
2126 prim
[i
].mode
= mode
;
2127 prim
[i
].indexed
= !!ib
;
2128 prim
[i
].indirect_offset
= indirect_offset
;
2129 prim
[i
].is_indirect
= 1;
2130 prim
[i
].draw_id
= i
;
2133 /* This should always be true at this time */
2134 assert(indirect_data
== ctx
->DrawIndirectBuffer
);
2136 ctx
->Driver
.Draw(ctx
, prim
, draw_count
, ib
, false, 0u, ~0u,
2137 NULL
, 0, indirect_data
);
2142 * Function to be put into dd_function_table::DrawIndirect as fallback.
2143 * Calls into dd_function_table::Draw past adapting call arguments.
2144 * See dd_function_table::DrawIndirect for call argument documentation.
2147 _mesa_draw_indirect(struct gl_context
*ctx
, GLuint mode
,
2148 struct gl_buffer_object
*indirect_data
,
2149 GLsizeiptr indirect_offset
, unsigned draw_count
,
2151 struct gl_buffer_object
*indirect_draw_count_buffer
,
2152 GLsizeiptr indirect_draw_count_offset
,
2153 const struct _mesa_index_buffer
*ib
)
2155 /* Use alloca for the prim space if we are somehow in bounds. */
2156 if (draw_count
*sizeof(struct _mesa_prim
) < 1024) {
2157 struct _mesa_prim
*space
= alloca(draw_count
*sizeof(struct _mesa_prim
));
2158 memset(space
, 0, draw_count
*sizeof(struct _mesa_prim
));
2160 draw_indirect(ctx
, mode
, indirect_data
, indirect_offset
, draw_count
,
2161 stride
, indirect_draw_count_buffer
,
2162 indirect_draw_count_offset
, ib
, space
);
2164 struct _mesa_prim
*space
= calloc(draw_count
, sizeof(struct _mesa_prim
));
2165 if (space
== NULL
) {
2166 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
2167 (draw_count
> 1) ? "Multi" : "",
2168 ib
? "Elements" : "Arrays",
2169 indirect_data
? "CountARB" : "");
2173 draw_indirect(ctx
, mode
, indirect_data
, indirect_offset
, draw_count
,
2174 stride
, indirect_draw_count_buffer
,
2175 indirect_draw_count_offset
, ib
, space
);