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
->Type
) {
88 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
90 for (k
= 0; k
< array
->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
->Type
, array
->Size
, binding
->Stride
);
97 printf(" Address/offset %p in Buffer Object %u\n",
98 array
->Ptr
, bo
->Name
);
99 f
[k
] = 1.0F
; /* XXX replace the bad value! */
101 /*assert(!IS_INF_OR_NAN(f[k])); */
113 * Unmap the buffer object referenced by given array, if mapped.
116 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
119 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
120 if (vao
->Enabled
& VERT_BIT(attrib
)) {
121 const struct gl_vertex_buffer_binding
*binding
=
122 &vao
->BufferBinding
[array
->BufferBindingIndex
];
123 struct gl_buffer_object
*bo
= binding
->BufferObj
;
124 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
125 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
132 sizeof_ib_type(GLenum type
)
135 case GL_UNSIGNED_INT
:
136 return sizeof(GLuint
);
137 case GL_UNSIGNED_SHORT
:
138 return sizeof(GLushort
);
139 case GL_UNSIGNED_BYTE
:
140 return sizeof(GLubyte
);
142 assert(!"unsupported index data type");
143 /* In case assert is turned off */
149 * Examine the array's data for NaNs, etc.
150 * For debug purposes; not normally used.
153 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
154 GLenum elemType
, const void *elements
,
157 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
162 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
163 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
164 vao
->IndexBufferObj
->Size
,
166 vao
->IndexBufferObj
, MAP_INTERNAL
);
167 elements
= ADD_POINTERS(elements
, elemMap
);
170 for (i
= 0; i
< count
; i
++) {
175 case GL_UNSIGNED_BYTE
:
176 j
= ((const GLubyte
*) elements
)[i
];
178 case GL_UNSIGNED_SHORT
:
179 j
= ((const GLushort
*) elements
)[i
];
181 case GL_UNSIGNED_INT
:
182 j
= ((const GLuint
*) elements
)[i
];
185 unreachable("Unexpected index buffer type");
188 /* check element j of each enabled array */
189 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
190 check_array_data(ctx
, vao
, k
, j
);
194 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
195 ctx
->Driver
.UnmapBuffer(ctx
, vao
->IndexBufferObj
, MAP_INTERNAL
);
198 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
199 unmap_array_buffer(ctx
, vao
, k
);
205 * Check array data, looking for NaNs, etc.
208 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
215 * Check if we should skip the draw call even after validation was successful.
218 skip_validated_draw(struct gl_context
*ctx
)
222 /* For ES2, we can draw if we have a vertex program/shader). */
223 return ctx
->VertexProgram
._Current
== NULL
;
226 /* For OpenGL ES, only draw if we have vertex positions
228 if (!(ctx
->Array
.VAO
->Enabled
& VERT_BIT_POS
))
232 case API_OPENGL_CORE
:
233 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
236 * "If there is no active program for the vertex or fragment shader
237 * stages, the results of vertex and/or fragment processing will be
238 * undefined. However, this is not an error."
240 * The fragment shader is not tested here because other state (e.g.,
241 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
243 return ctx
->VertexProgram
._Current
== NULL
;
245 case API_OPENGL_COMPAT
:
246 if (ctx
->VertexProgram
._Current
!= NULL
) {
247 /* Draw regardless of whether or not we have any vertex arrays.
248 * (Ex: could draw a point using a constant vertex pos)
252 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
255 return !(ctx
->Array
.VAO
->Enabled
& (VERT_BIT_POS
|VERT_BIT_GENERIC0
));
260 unreachable("Invalid API value in check_valid_to_render()");
268 * Print info/data for glDrawArrays(), for debugging.
271 print_draw_arrays(struct gl_context
*ctx
,
272 GLenum mode
, GLint start
, GLsizei count
)
274 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
276 printf("_mesa_DrawArrays(mode 0x%x, start %d, count %d):\n",
279 GLbitfield mask
= vao
->Enabled
;
281 const gl_vert_attrib i
= u_bit_scan(&mask
);
282 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
284 const struct gl_vertex_buffer_binding
*binding
=
285 &vao
->BufferBinding
[array
->BufferBindingIndex
];
286 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
288 printf("attr %s: size %d stride %d "
289 "ptr %p Bufobj %u\n",
290 gl_vert_attrib_name((gl_vert_attrib
) i
),
291 array
->Size
, binding
->Stride
,
292 array
->Ptr
, bufObj
->Name
);
294 if (_mesa_is_bufferobj(bufObj
)) {
295 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
296 GL_MAP_READ_BIT
, bufObj
,
298 int offset
= (int) (GLintptr
)
299 _mesa_vertex_attrib_address(array
, binding
);
302 switch (array
->Type
) {
305 case GL_UNSIGNED_INT64_ARB
:
312 float *f
= (float *) (p
+ offset
);
315 int n
= (count
- 1) * (binding
->Stride
/ (4 * multiplier
))
319 printf(" Data at offset %d:\n", offset
);
322 printf(" double[%d] = 0x%016llx %lf\n", i
,
323 ((unsigned long long *) k
)[i
], ((double *) f
)[i
]);
325 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
328 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
335 * Return a filter mask for the net enabled vao arrays.
336 * This is to mask out arrays that would otherwise supersed required current
337 * values for the fixed function shaders for example.
340 enabled_filter(const struct gl_context
*ctx
)
342 switch (ctx
->VertexProgram
._VPMode
) {
344 /* When no vertex program is active (or the vertex program is generated
345 * from fixed-function state). We put the material values into the
346 * generic slots. Since the vao has no material arrays, mute these
347 * slots from the enabled arrays so that the current material values
348 * are pulled instead of the vao arrays.
350 return VERT_BIT_FF_ALL
;
353 /* There are no shaders in OpenGL ES 1.x, so this code path should be
354 * impossible to reach. The meta code is careful to not use shaders in
357 assert(ctx
->API
!= API_OPENGLES
);
359 /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
360 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
361 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
364 if (ctx
->API
!= API_OPENGL_COMPAT
)
365 return VERT_BIT_GENERIC_ALL
;
377 * Helper function called by the other DrawArrays() functions below.
378 * This is where we handle primitive restart for drawing non-indexed
379 * arrays. If primitive restart is enabled, it typically means
380 * splitting one DrawArrays() into two.
383 _mesa_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
384 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
387 struct _mesa_prim prim
;
389 if (skip_validated_draw(ctx
))
392 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
395 memset(&prim
, 0, sizeof(prim
));
399 prim
.num_instances
= numInstances
;
400 prim
.base_instance
= baseInstance
;
401 prim
.draw_id
= drawID
;
402 prim
.is_indirect
= 0;
406 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
407 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
409 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
416 * Execute a glRectf() function.
418 static void GLAPIENTRY
419 _mesa_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
421 GET_CURRENT_CONTEXT(ctx
);
422 ASSERT_OUTSIDE_BEGIN_END(ctx
);
424 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
425 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
426 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
427 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
428 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
429 CALL_End(GET_DISPATCH(), ());
433 static void GLAPIENTRY
434 _mesa_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
436 GET_CURRENT_CONTEXT(ctx
);
446 prim
= GL_LINE_STRIP
;
449 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
453 /* No effect if vertex maps disabled.
455 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
458 du
= ctx
->Eval
.MapGrid1du
;
459 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
461 CALL_Begin(GET_DISPATCH(), (prim
));
462 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
463 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
465 CALL_End(GET_DISPATCH(), ());
469 static void GLAPIENTRY
470 _mesa_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
472 GET_CURRENT_CONTEXT(ctx
);
473 GLfloat u
, du
, v
, dv
, v1
, u1
;
482 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
486 /* No effect if vertex maps disabled.
488 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
491 du
= ctx
->Eval
.MapGrid2du
;
492 dv
= ctx
->Eval
.MapGrid2dv
;
493 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
494 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
498 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
499 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
500 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
501 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
504 CALL_End(GET_DISPATCH(), ());
507 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
508 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
509 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
510 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
512 CALL_End(GET_DISPATCH(), ());
514 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
515 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
516 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
517 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
519 CALL_End(GET_DISPATCH(), ());
523 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
524 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
525 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
526 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
527 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
529 CALL_End(GET_DISPATCH(), ());
537 * Called from glDrawArrays when in immediate mode (not display list mode).
540 _mesa_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
542 GET_CURRENT_CONTEXT(ctx
);
544 if (MESA_VERBOSE
& VERBOSE_DRAW
)
545 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
546 _mesa_enum_to_string(mode
), start
, count
);
550 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
552 if (_mesa_is_no_error_enabled(ctx
)) {
554 _mesa_update_state(ctx
);
556 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
561 check_draw_arrays_data(ctx
, start
, count
);
563 _mesa_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
566 print_draw_arrays(ctx
, mode
, start
, count
);
571 * Called from glDrawArraysInstanced when in immediate mode (not
572 * display list mode).
575 _mesa_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
576 GLsizei numInstances
)
578 GET_CURRENT_CONTEXT(ctx
);
580 if (MESA_VERBOSE
& VERBOSE_DRAW
)
581 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
582 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
586 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
588 if (_mesa_is_no_error_enabled(ctx
)) {
590 _mesa_update_state(ctx
);
592 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
598 check_draw_arrays_data(ctx
, start
, count
);
600 _mesa_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
603 print_draw_arrays(ctx
, mode
, start
, count
);
608 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
610 static void GLAPIENTRY
611 _mesa_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
612 GLsizei count
, GLsizei numInstances
,
615 GET_CURRENT_CONTEXT(ctx
);
617 if (MESA_VERBOSE
& VERBOSE_DRAW
)
619 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
620 _mesa_enum_to_string(mode
), first
, count
,
621 numInstances
, baseInstance
);
625 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
627 if (_mesa_is_no_error_enabled(ctx
)) {
629 _mesa_update_state(ctx
);
631 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
637 check_draw_arrays_data(ctx
, first
, count
);
639 _mesa_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
642 print_draw_arrays(ctx
, mode
, first
, count
);
647 * Called from glMultiDrawArrays when in immediate mode.
649 static void GLAPIENTRY
650 _mesa_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
651 const GLsizei
*count
, GLsizei primcount
)
653 GET_CURRENT_CONTEXT(ctx
);
656 if (MESA_VERBOSE
& VERBOSE_DRAW
)
658 "glMultiDrawArrays(%s, %p, %p, %d)\n",
659 _mesa_enum_to_string(mode
), first
, count
, primcount
);
663 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
665 if (_mesa_is_no_error_enabled(ctx
)) {
667 _mesa_update_state(ctx
);
669 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
673 for (i
= 0; i
< primcount
; i
++) {
676 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
678 /* The GL_ARB_shader_draw_parameters spec adds the following after the
679 * pseudo-code describing glMultiDrawArrays:
681 * "The index of the draw (<i> in the above pseudo-code) may be
682 * read by a vertex shader as <gl_DrawIDARB>, as described in
685 _mesa_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
688 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
696 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
701 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
704 ctx
->Driver
.MapBufferRange(ctx
, 0,
705 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
707 ctx
->Array
.VAO
->IndexBufferObj
,
710 case GL_UNSIGNED_BYTE
:
712 const GLubyte
*us
= (const GLubyte
*) map
;
714 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
715 printf("%02x ", us
[i
]);
722 case GL_UNSIGNED_SHORT
:
724 const GLushort
*us
= (const GLushort
*) map
;
726 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
727 printf("%04x ", us
[i
]);
734 case GL_UNSIGNED_INT
:
736 const GLuint
*us
= (const GLuint
*) map
;
738 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
739 printf("%08x ", us
[i
]);
750 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
756 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
757 const GLvoid
*indices
)
762 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
764 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
767 if (skip_validated_draw(ctx
))
775 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
776 * Do the rendering for a glDrawElements or glDrawRangeElements call after
777 * we've validated buffer bounds, etc.
780 _mesa_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
781 GLboolean index_bounds_valid
,
782 GLuint start
, GLuint end
,
783 GLsizei count
, GLenum type
,
784 const GLvoid
* indices
,
785 GLint basevertex
, GLuint numInstances
,
788 struct _mesa_index_buffer ib
;
789 struct _mesa_prim prim
;
791 if (!index_bounds_valid
) {
796 if (skip_draw_elements(ctx
, count
, indices
))
800 ib
.index_size
= sizeof_ib_type(type
);
801 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
811 prim
.is_indirect
= 0;
812 prim
.basevertex
= basevertex
;
813 prim
.num_instances
= numInstances
;
814 prim
.base_instance
= baseInstance
;
817 /* Need to give special consideration to rendering a range of
818 * indices starting somewhere above zero. Typically the
819 * application is issuing multiple DrawRangeElements() to draw
820 * successive primitives layed out linearly in the vertex arrays.
821 * Unless the vertex arrays are all in a VBO (or locked as with
822 * CVA), the OpenGL semantics imply that we need to re-read or
823 * re-upload the vertex data on each draw call.
825 * In the case of hardware tnl, we want to avoid starting the
826 * upload at zero, as it will mean every draw call uploads an
827 * increasing amount of not-used vertex data. Worse - in the
828 * software tnl module, all those vertices might be transformed and
829 * lit but never rendered.
831 * If we just upload or transform the vertices in start..end,
832 * however, the indices will be incorrect.
834 * At this level, we don't know exactly what the requirements of
835 * the backend are going to be, though it will likely boil down to
838 * 1) Do nothing, everything is in a VBO and is processed once
841 * 2) Adjust the indices and vertex arrays so that start becomes
844 * Rather than doing anything here, I'll provide a helper function
845 * for the latter case elsewhere.
848 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
849 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
851 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
858 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
861 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
862 GLsizei count
, GLenum type
,
863 const GLvoid
* indices
, GLint basevertex
)
865 static GLuint warnCount
= 0;
866 GLboolean index_bounds_valid
= GL_TRUE
;
868 /* This is only useful to catch invalid values in the "end" parameter
871 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
873 GET_CURRENT_CONTEXT(ctx
);
875 if (MESA_VERBOSE
& VERBOSE_DRAW
)
877 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
878 _mesa_enum_to_string(mode
), start
, end
, count
,
879 _mesa_enum_to_string(type
), indices
, basevertex
);
883 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
885 if (_mesa_is_no_error_enabled(ctx
)) {
887 _mesa_update_state(ctx
);
889 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
894 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
895 /* The application requested we draw using a range of indices that's
896 * outside the bounds of the current VBO. This is invalid and appears
897 * to give undefined results. The safest thing to do is to simply
898 * ignore the range, in case the application botched their range tracking
899 * but did provide valid indices. Also issue a warning indicating that
900 * the application is broken.
902 if (warnCount
++ < 10) {
903 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
904 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
905 "\trange is outside VBO bounds (max=%u); ignoring.\n"
906 "\tThis should be fixed in the application.",
907 start
, end
, basevertex
, count
, type
, indices
,
910 index_bounds_valid
= GL_FALSE
;
913 /* NOTE: It's important that 'end' is a reasonable value.
914 * in _tnl_draw_prims(), we use end to determine how many vertices
915 * to transform. If it's too large, we can unnecessarily split prims
916 * or we can read/write out of memory in several different places!
919 /* Catch/fix some potential user errors */
920 if (type
== GL_UNSIGNED_BYTE
) {
921 start
= MIN2(start
, 0xff);
922 end
= MIN2(end
, 0xff);
924 else if (type
== GL_UNSIGNED_SHORT
) {
925 start
= MIN2(start
, 0xffff);
926 end
= MIN2(end
, 0xffff);
930 printf("glDraw[Range]Elements{,BaseVertex}"
931 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
933 start
, end
, type
, count
,
934 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
937 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
938 index_bounds_valid
= GL_FALSE
;
941 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
943 (void) check_draw_elements_data
;
946 if (!index_bounds_valid
) {
951 _mesa_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
952 count
, type
, indices
, basevertex
, 1, 0);
957 * Called by glDrawRangeElements() in immediate mode.
960 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
961 GLsizei count
, GLenum type
, const GLvoid
* indices
)
963 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
964 GET_CURRENT_CONTEXT(ctx
);
966 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
967 _mesa_enum_to_string(mode
), start
, end
, count
,
968 _mesa_enum_to_string(type
), indices
);
971 _mesa_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
977 * Called by glDrawElements() in immediate mode.
980 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
981 const GLvoid
* indices
)
983 GET_CURRENT_CONTEXT(ctx
);
985 if (MESA_VERBOSE
& VERBOSE_DRAW
)
986 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
987 _mesa_enum_to_string(mode
), count
,
988 _mesa_enum_to_string(type
), indices
);
992 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
994 if (_mesa_is_no_error_enabled(ctx
)) {
996 _mesa_update_state(ctx
);
998 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1002 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1003 count
, type
, indices
, 0, 1, 0);
1008 * Called by glDrawElementsBaseVertex() in immediate mode.
1011 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1012 const GLvoid
* indices
, GLint basevertex
)
1014 GET_CURRENT_CONTEXT(ctx
);
1016 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1017 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1018 _mesa_enum_to_string(mode
), count
,
1019 _mesa_enum_to_string(type
), indices
);
1021 FLUSH_FOR_DRAW(ctx
);
1023 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1025 if (_mesa_is_no_error_enabled(ctx
)) {
1027 _mesa_update_state(ctx
);
1029 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1033 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1034 count
, type
, indices
, basevertex
, 1, 0);
1039 * Called by glDrawElementsInstanced() in immediate mode.
1041 static void GLAPIENTRY
1042 _mesa_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1043 const GLvoid
* indices
, GLsizei numInstances
)
1045 GET_CURRENT_CONTEXT(ctx
);
1047 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1048 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1049 _mesa_enum_to_string(mode
), count
,
1050 _mesa_enum_to_string(type
), indices
);
1052 FLUSH_FOR_DRAW(ctx
);
1054 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1056 if (_mesa_is_no_error_enabled(ctx
)) {
1058 _mesa_update_state(ctx
);
1060 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1061 indices
, numInstances
))
1065 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1066 count
, type
, indices
, 0, numInstances
, 0);
1071 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1073 static void GLAPIENTRY
1074 _mesa_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1075 GLenum type
, const GLvoid
* indices
,
1076 GLsizei numInstances
,
1079 GET_CURRENT_CONTEXT(ctx
);
1081 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1083 "glDrawElementsInstancedBaseVertex"
1084 "(%s, %d, %s, %p, %d; %d)\n",
1085 _mesa_enum_to_string(mode
), count
,
1086 _mesa_enum_to_string(type
), indices
,
1087 numInstances
, basevertex
);
1089 FLUSH_FOR_DRAW(ctx
);
1091 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1093 if (_mesa_is_no_error_enabled(ctx
)) {
1095 _mesa_update_state(ctx
);
1097 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1098 indices
, numInstances
))
1102 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1103 count
, type
, indices
,
1104 basevertex
, numInstances
, 0);
1109 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1111 static void GLAPIENTRY
1112 _mesa_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1114 const GLvoid
*indices
,
1115 GLsizei numInstances
,
1116 GLuint baseInstance
)
1118 GET_CURRENT_CONTEXT(ctx
);
1120 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1122 "glDrawElementsInstancedBaseInstance"
1123 "(%s, %d, %s, %p, %d, %d)\n",
1124 _mesa_enum_to_string(mode
), count
,
1125 _mesa_enum_to_string(type
), indices
,
1126 numInstances
, baseInstance
);
1128 FLUSH_FOR_DRAW(ctx
);
1130 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1132 if (_mesa_is_no_error_enabled(ctx
)) {
1134 _mesa_update_state(ctx
);
1136 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1137 indices
, numInstances
))
1141 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1142 count
, type
, indices
, 0, numInstances
,
1148 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1150 static void GLAPIENTRY
1151 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1154 const GLvoid
*indices
,
1155 GLsizei numInstances
,
1157 GLuint baseInstance
)
1159 GET_CURRENT_CONTEXT(ctx
);
1161 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1163 "glDrawElementsInstancedBaseVertexBaseInstance"
1164 "(%s, %d, %s, %p, %d, %d, %d)\n",
1165 _mesa_enum_to_string(mode
), count
,
1166 _mesa_enum_to_string(type
), indices
,
1167 numInstances
, basevertex
, baseInstance
);
1169 FLUSH_FOR_DRAW(ctx
);
1171 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1173 if (_mesa_is_no_error_enabled(ctx
)) {
1175 _mesa_update_state(ctx
);
1177 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1178 indices
, numInstances
))
1182 _mesa_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1183 count
, type
, indices
, basevertex
,
1184 numInstances
, baseInstance
);
1189 * Inner support for both _mesa_MultiDrawElements() and
1190 * _mesa_MultiDrawRangeElements().
1191 * This does the actual rendering after we've checked array indexes, etc.
1194 _mesa_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1195 const GLsizei
*count
, GLenum type
,
1196 const GLvoid
* const *indices
,
1197 GLsizei primcount
, const GLint
*basevertex
)
1199 struct _mesa_index_buffer ib
;
1200 struct _mesa_prim
*prim
;
1201 unsigned int index_type_size
= sizeof_ib_type(type
);
1202 uintptr_t min_index_ptr
, max_index_ptr
;
1203 GLboolean fallback
= GL_FALSE
;
1209 prim
= calloc(primcount
, sizeof(*prim
));
1211 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1215 min_index_ptr
= (uintptr_t) indices
[0];
1217 for (i
= 0; i
< primcount
; i
++) {
1218 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1219 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1220 index_type_size
* count
[i
]);
1223 /* Check if we can handle this thing as a bunch of index offsets from the
1224 * same index pointer. If we can't, then we have to fall back to doing
1225 * a draw_prims per primitive.
1226 * Check that the difference between each prim's indexes is a multiple of
1227 * the index/element size.
1229 if (index_type_size
!= 1) {
1230 for (i
= 0; i
< primcount
; i
++) {
1231 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1239 /* Draw primitives individually if one count is zero, so we can easily skip
1242 for (i
= 0; i
< primcount
; i
++) {
1243 if (count
[i
] == 0) {
1249 /* If the index buffer isn't in a VBO, then treating the application's
1250 * subranges of the index buffer as one large index buffer may lead to
1251 * us reading unmapped memory.
1253 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1257 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1258 ib
.index_size
= sizeof_ib_type(type
);
1259 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1260 ib
.ptr
= (void *) min_index_ptr
;
1262 for (i
= 0; i
< primcount
; i
++) {
1263 prim
[i
].begin
= (i
== 0);
1264 prim
[i
].end
= (i
== primcount
- 1);
1266 prim
[i
].mode
= mode
;
1268 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1269 prim
[i
].count
= count
[i
];
1270 prim
[i
].indexed
= 1;
1271 prim
[i
].num_instances
= 1;
1272 prim
[i
].base_instance
= 0;
1273 prim
[i
].draw_id
= i
;
1274 prim
[i
].is_indirect
= 0;
1275 if (basevertex
!= NULL
)
1276 prim
[i
].basevertex
= basevertex
[i
];
1278 prim
[i
].basevertex
= 0;
1281 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1282 false, 0, ~0, NULL
, 0, NULL
);
1285 /* render one prim at a time */
1286 for (i
= 0; i
< primcount
; i
++) {
1289 ib
.count
= count
[i
];
1290 ib
.index_size
= sizeof_ib_type(type
);
1291 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1292 ib
.ptr
= indices
[i
];
1297 prim
[0].mode
= mode
;
1299 prim
[0].count
= count
[i
];
1300 prim
[0].indexed
= 1;
1301 prim
[0].num_instances
= 1;
1302 prim
[0].base_instance
= 0;
1303 prim
[0].draw_id
= i
;
1304 prim
[0].is_indirect
= 0;
1305 if (basevertex
!= NULL
)
1306 prim
[0].basevertex
= basevertex
[i
];
1308 prim
[0].basevertex
= 0;
1310 ctx
->Driver
.Draw(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1316 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1323 _mesa_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1324 const GLvoid
* const *indices
, GLsizei primcount
)
1326 GET_CURRENT_CONTEXT(ctx
);
1328 FLUSH_FOR_DRAW(ctx
);
1330 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1332 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1336 if (skip_validated_draw(ctx
))
1339 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1345 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1346 const GLsizei
*count
, GLenum type
,
1347 const GLvoid
* const *indices
,
1349 const GLsizei
*basevertex
)
1351 GET_CURRENT_CONTEXT(ctx
);
1353 FLUSH_FOR_DRAW(ctx
);
1355 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1357 if (_mesa_is_no_error_enabled(ctx
)) {
1359 _mesa_update_state(ctx
);
1361 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1366 if (skip_validated_draw(ctx
))
1369 _mesa_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1375 * Draw a GL primitive using a vertex count obtained from transform feedback.
1376 * \param mode the type of GL primitive to draw
1377 * \param obj the transform feedback object to use
1378 * \param stream index of the transform feedback stream from which to
1379 * get the primitive count.
1380 * \param numInstances number of instances to draw
1383 _mesa_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1384 struct gl_transform_feedback_object
*obj
,
1385 GLuint stream
, GLuint numInstances
)
1387 struct _mesa_prim prim
;
1389 FLUSH_FOR_DRAW(ctx
);
1391 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1393 if (_mesa_is_no_error_enabled(ctx
)) {
1395 _mesa_update_state(ctx
);
1397 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1403 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1404 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1405 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1407 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1408 _mesa_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1412 if (skip_validated_draw(ctx
))
1415 /* init most fields to zero */
1416 memset(&prim
, 0, sizeof(prim
));
1420 prim
.num_instances
= numInstances
;
1421 prim
.base_instance
= 0;
1422 prim
.is_indirect
= 0;
1424 /* Maybe we should do some primitive splitting for primitive restart
1425 * (like in DrawArrays), but we have no way to know how many vertices
1426 * will be rendered. */
1428 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1430 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1437 * Like DrawArrays, but take the count from a transform feedback object.
1438 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1439 * \param name the transform feedback object
1440 * User still has to setup of the vertex attribute info with
1441 * glVertexPointer, glColorPointer, etc.
1442 * Part of GL_ARB_transform_feedback2.
1445 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1447 GET_CURRENT_CONTEXT(ctx
);
1448 struct gl_transform_feedback_object
*obj
=
1449 _mesa_lookup_transform_feedback_object(ctx
, name
);
1451 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1452 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1453 _mesa_enum_to_string(mode
), name
);
1455 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1459 static void GLAPIENTRY
1460 _mesa_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
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
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1468 _mesa_enum_to_string(mode
), name
, stream
);
1470 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1474 static void GLAPIENTRY
1475 _mesa_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1478 GET_CURRENT_CONTEXT(ctx
);
1479 struct gl_transform_feedback_object
*obj
=
1480 _mesa_lookup_transform_feedback_object(ctx
, name
);
1482 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1483 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1484 _mesa_enum_to_string(mode
), name
);
1486 _mesa_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1490 static void GLAPIENTRY
1491 _mesa_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1495 GET_CURRENT_CONTEXT(ctx
);
1496 struct gl_transform_feedback_object
*obj
=
1497 _mesa_lookup_transform_feedback_object(ctx
, name
);
1499 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1500 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1501 "(%s, %u, %u, %i)\n",
1502 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1504 _mesa_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1509 _mesa_validated_drawarraysindirect(struct gl_context
*ctx
,
1510 GLenum mode
, const GLvoid
*indirect
)
1512 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1513 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1514 1 /* draw_count */ , 16 /* stride */ ,
1517 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1523 _mesa_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1525 const GLvoid
*indirect
,
1526 GLsizei primcount
, GLsizei stride
)
1528 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1533 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1534 primcount
, stride
, NULL
, 0, NULL
);
1536 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1542 _mesa_validated_drawelementsindirect(struct gl_context
*ctx
,
1543 GLenum mode
, GLenum type
,
1544 const GLvoid
*indirect
)
1546 struct _mesa_index_buffer ib
;
1548 ib
.count
= 0; /* unknown */
1549 ib
.index_size
= sizeof_ib_type(type
);
1550 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1553 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1554 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1555 1 /* draw_count */ , 20 /* stride */ ,
1558 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1564 _mesa_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1565 GLenum mode
, GLenum type
,
1566 const GLvoid
*indirect
,
1567 GLsizei primcount
, GLsizei stride
)
1569 struct _mesa_index_buffer ib
;
1570 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1575 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1577 ib
.count
= 0; /* unknown */
1578 ib
.index_size
= sizeof_ib_type(type
);
1579 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1582 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1583 ctx
->DrawIndirectBuffer
, offset
,
1584 primcount
, stride
, NULL
, 0, &ib
);
1586 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1592 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1595 static void GLAPIENTRY
1596 _mesa_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1598 GET_CURRENT_CONTEXT(ctx
);
1600 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1601 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1602 _mesa_enum_to_string(mode
), indirect
);
1604 /* From the ARB_draw_indirect spec:
1606 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1607 * compatibility profile, this indicates that DrawArraysIndirect and
1608 * DrawElementsIndirect are to source their arguments directly from the
1609 * pointer passed as their <indirect> parameters."
1611 if (ctx
->API
== API_OPENGL_COMPAT
&&
1612 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1613 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1615 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1621 FLUSH_FOR_DRAW(ctx
);
1623 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1625 if (_mesa_is_no_error_enabled(ctx
)) {
1627 _mesa_update_state(ctx
);
1629 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1633 if (skip_validated_draw(ctx
))
1636 _mesa_validated_drawarraysindirect(ctx
, mode
, indirect
);
1640 static void GLAPIENTRY
1641 _mesa_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1643 GET_CURRENT_CONTEXT(ctx
);
1645 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1646 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1647 _mesa_enum_to_string(mode
),
1648 _mesa_enum_to_string(type
), indirect
);
1650 /* From the ARB_draw_indirect spec:
1652 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1653 * compatibility profile, this indicates that DrawArraysIndirect and
1654 * DrawElementsIndirect are to source their arguments directly from the
1655 * pointer passed as their <indirect> parameters."
1657 if (ctx
->API
== API_OPENGL_COMPAT
&&
1658 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1660 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1661 * may not come from a client array and must come from an index buffer.
1662 * If no element array buffer is bound, an INVALID_OPERATION error is
1665 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1666 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1667 "glDrawElementsIndirect(no buffer bound "
1668 "to GL_ELEMENT_ARRAY_BUFFER)");
1670 DrawElementsIndirectCommand
*cmd
=
1671 (DrawElementsIndirectCommand
*) indirect
;
1673 /* Convert offset to pointer */
1674 void *offset
= (void *)
1675 ((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1677 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1687 FLUSH_FOR_DRAW(ctx
);
1689 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1691 if (_mesa_is_no_error_enabled(ctx
)) {
1693 _mesa_update_state(ctx
);
1695 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1699 if (skip_validated_draw(ctx
))
1702 _mesa_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1706 static void GLAPIENTRY
1707 _mesa_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1708 GLsizei primcount
, GLsizei stride
)
1710 GET_CURRENT_CONTEXT(ctx
);
1712 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1713 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1714 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1716 /* If <stride> is zero, the array elements are treated as tightly packed. */
1718 stride
= sizeof(DrawArraysIndirectCommand
);
1720 /* From the ARB_draw_indirect spec:
1722 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1723 * compatibility profile, this indicates that DrawArraysIndirect and
1724 * DrawElementsIndirect are to source their arguments directly from the
1725 * pointer passed as their <indirect> parameters."
1727 if (ctx
->API
== API_OPENGL_COMPAT
&&
1728 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1730 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1731 "glMultiDrawArraysIndirect"))
1734 const ubyte
*ptr
= (const ubyte
*) indirect
;
1735 for (unsigned i
= 0; i
< primcount
; i
++) {
1736 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1737 _mesa_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1738 cmd
->count
, cmd
->primCount
,
1742 ptr
+= sizeof(DrawArraysIndirectCommand
);
1751 FLUSH_FOR_DRAW(ctx
);
1753 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1755 if (_mesa_is_no_error_enabled(ctx
)) {
1757 _mesa_update_state(ctx
);
1759 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1764 if (skip_validated_draw(ctx
))
1767 _mesa_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1772 static void GLAPIENTRY
1773 _mesa_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1774 const GLvoid
*indirect
,
1775 GLsizei primcount
, GLsizei stride
)
1777 GET_CURRENT_CONTEXT(ctx
);
1779 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1780 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1781 _mesa_enum_to_string(mode
),
1782 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1784 /* If <stride> is zero, the array elements are treated as tightly packed. */
1786 stride
= sizeof(DrawElementsIndirectCommand
);
1789 /* From the ARB_draw_indirect spec:
1791 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1792 * compatibility profile, this indicates that DrawArraysIndirect and
1793 * DrawElementsIndirect are to source their arguments directly from the
1794 * pointer passed as their <indirect> parameters."
1796 if (ctx
->API
== API_OPENGL_COMPAT
&&
1797 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1799 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1800 * may not come from a client array and must come from an index buffer.
1801 * If no element array buffer is bound, an INVALID_OPERATION error is
1804 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1805 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1806 "glMultiDrawElementsIndirect(no buffer bound "
1807 "to GL_ELEMENT_ARRAY_BUFFER)");
1812 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1813 "glMultiDrawArraysIndirect"))
1816 const ubyte
*ptr
= (const ubyte
*) indirect
;
1817 for (unsigned i
= 0; i
< primcount
; i
++) {
1818 _mesa_exec_DrawElementsIndirect(mode
, type
, ptr
);
1821 ptr
+= sizeof(DrawElementsIndirectCommand
);
1830 FLUSH_FOR_DRAW(ctx
);
1832 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1834 if (_mesa_is_no_error_enabled(ctx
)) {
1836 _mesa_update_state(ctx
);
1838 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1843 if (skip_validated_draw(ctx
))
1846 _mesa_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1852 _mesa_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1855 GLintptr drawcount_offset
,
1856 GLsizei maxdrawcount
,
1859 GLsizeiptr offset
= indirect
;
1861 if (maxdrawcount
== 0)
1864 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1865 ctx
->DrawIndirectBuffer
, offset
,
1866 maxdrawcount
, stride
,
1867 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1869 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1875 _mesa_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1876 GLenum mode
, GLenum type
,
1878 GLintptr drawcount_offset
,
1879 GLsizei maxdrawcount
,
1882 struct _mesa_index_buffer ib
;
1883 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1885 if (maxdrawcount
== 0)
1888 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1890 ib
.count
= 0; /* unknown */
1891 ib
.index_size
= sizeof_ib_type(type
);
1892 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1895 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1896 ctx
->DrawIndirectBuffer
, offset
,
1897 maxdrawcount
, stride
,
1898 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1900 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1905 static void GLAPIENTRY
1906 _mesa_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1907 GLintptr drawcount_offset
,
1908 GLsizei maxdrawcount
, GLsizei stride
)
1910 GET_CURRENT_CONTEXT(ctx
);
1912 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1913 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1914 "(%s, %lx, %lx, %i, %i)\n",
1915 _mesa_enum_to_string(mode
),
1916 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1917 maxdrawcount
, stride
);
1919 /* If <stride> is zero, the array elements are treated as tightly packed. */
1921 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1923 FLUSH_FOR_DRAW(ctx
);
1925 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1927 if (_mesa_is_no_error_enabled(ctx
)) {
1929 _mesa_update_state(ctx
);
1931 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1934 maxdrawcount
, stride
))
1938 if (skip_validated_draw(ctx
))
1941 _mesa_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1943 maxdrawcount
, stride
);
1947 static void GLAPIENTRY
1948 _mesa_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1950 GLintptr drawcount_offset
,
1951 GLsizei maxdrawcount
, GLsizei stride
)
1953 GET_CURRENT_CONTEXT(ctx
);
1955 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1956 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1957 "(%s, %s, %lx, %lx, %i, %i)\n",
1958 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1959 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1960 maxdrawcount
, stride
);
1962 /* If <stride> is zero, the array elements are treated as tightly packed. */
1964 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1966 FLUSH_FOR_DRAW(ctx
);
1968 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1970 if (_mesa_is_no_error_enabled(ctx
)) {
1972 _mesa_update_state(ctx
);
1974 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1977 maxdrawcount
, stride
))
1981 if (skip_validated_draw(ctx
))
1984 _mesa_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1985 drawcount_offset
, maxdrawcount
,
1991 * Initialize the dispatch table with the VBO functions for drawing.
1994 _mesa_initialize_exec_dispatch(const struct gl_context
*ctx
,
1995 struct _glapi_table
*exec
)
1997 SET_DrawArrays(exec
, _mesa_DrawArrays
);
1998 SET_DrawElements(exec
, _mesa_DrawElements
);
2000 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2001 SET_DrawRangeElements(exec
, _mesa_DrawRangeElements
);
2004 SET_MultiDrawArrays(exec
, _mesa_exec_MultiDrawArrays
);
2005 SET_MultiDrawElementsEXT(exec
, _mesa_MultiDrawElements
);
2007 if (ctx
->API
== API_OPENGL_COMPAT
) {
2008 SET_Rectf(exec
, _mesa_exec_Rectf
);
2009 SET_EvalMesh1(exec
, _mesa_exec_EvalMesh1
);
2010 SET_EvalMesh2(exec
, _mesa_exec_EvalMesh2
);
2013 if (ctx
->API
!= API_OPENGLES
&&
2014 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
2015 SET_DrawElementsBaseVertex(exec
, _mesa_DrawElementsBaseVertex
);
2016 SET_MultiDrawElementsBaseVertex(exec
,
2017 _mesa_MultiDrawElementsBaseVertex
);
2019 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2020 SET_DrawRangeElementsBaseVertex(exec
,
2021 _mesa_DrawRangeElementsBaseVertex
);
2022 SET_DrawElementsInstancedBaseVertex(exec
,
2023 _mesa_exec_DrawElementsInstancedBaseVertex
);
2027 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2028 SET_DrawArraysInstancedBaseInstance(exec
,
2029 _mesa_exec_DrawArraysInstancedBaseInstance
);
2030 SET_DrawElementsInstancedBaseInstance(exec
,
2031 _mesa_exec_DrawElementsInstancedBaseInstance
);
2032 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
2033 _mesa_exec_DrawElementsInstancedBaseVertexBaseInstance
);
2036 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
2037 SET_DrawArraysIndirect(exec
, _mesa_exec_DrawArraysIndirect
);
2038 SET_DrawElementsIndirect(exec
, _mesa_exec_DrawElementsIndirect
);
2041 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2042 SET_DrawArraysInstancedARB(exec
, _mesa_DrawArraysInstanced
);
2043 SET_DrawElementsInstancedARB(exec
, _mesa_exec_DrawElementsInstanced
);
2046 if (_mesa_is_desktop_gl(ctx
)) {
2047 SET_DrawTransformFeedback(exec
, _mesa_DrawTransformFeedback
);
2048 SET_DrawTransformFeedbackStream(exec
,
2049 _mesa_exec_DrawTransformFeedbackStream
);
2050 SET_DrawTransformFeedbackInstanced(exec
,
2051 _mesa_exec_DrawTransformFeedbackInstanced
);
2052 SET_DrawTransformFeedbackStreamInstanced(exec
,
2053 _mesa_exec_DrawTransformFeedbackStreamInstanced
);
2054 SET_MultiDrawArraysIndirect(exec
, _mesa_exec_MultiDrawArraysIndirect
);
2055 SET_MultiDrawElementsIndirect(exec
, _mesa_exec_MultiDrawElementsIndirect
);
2056 SET_MultiDrawArraysIndirectCountARB(exec
,
2057 _mesa_exec_MultiDrawArraysIndirectCount
);
2058 SET_MultiDrawElementsIndirectCountARB(exec
,
2059 _mesa_exec_MultiDrawElementsIndirectCount
);
2065 /* GL_IBM_multimode_draw_arrays */
2067 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
2068 const GLsizei
* count
,
2069 GLsizei primcount
, GLint modestride
)
2071 GET_CURRENT_CONTEXT(ctx
);
2074 FLUSH_VERTICES(ctx
, 0);
2076 for ( i
= 0 ; i
< primcount
; i
++ ) {
2077 if ( count
[i
] > 0 ) {
2078 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2079 CALL_DrawArrays(ctx
->CurrentServerDispatch
, ( m
, first
[i
], count
[i
] ));
2085 /* GL_IBM_multimode_draw_arrays */
2087 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
2088 GLenum type
, const GLvoid
* const * indices
,
2089 GLsizei primcount
, GLint modestride
)
2091 GET_CURRENT_CONTEXT(ctx
);
2094 FLUSH_VERTICES(ctx
, 0);
2096 /* XXX not sure about ARB_vertex_buffer_object handling here */
2098 for ( i
= 0 ; i
< primcount
; i
++ ) {
2099 if ( count
[i
] > 0 ) {
2100 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
2101 CALL_DrawElements(ctx
->CurrentServerDispatch
, ( m
, count
[i
], type
,
2109 * Helper function for _mesa_draw_indirect below that additionally takes a zero
2110 * initialized array of _mesa_prim scratch space memory as the last argument.
2113 draw_indirect(struct gl_context
*ctx
, GLuint mode
,
2114 struct gl_buffer_object
*indirect_data
,
2115 GLsizeiptr indirect_offset
, unsigned draw_count
,
2117 struct gl_buffer_object
*indirect_draw_count_buffer
,
2118 GLsizeiptr indirect_draw_count_offset
,
2119 const struct _mesa_index_buffer
*ib
,
2120 struct _mesa_prim
*prim
)
2123 prim
[draw_count
- 1].end
= 1;
2124 for (unsigned i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
2125 prim
[i
].mode
= mode
;
2126 prim
[i
].indexed
= !!ib
;
2127 prim
[i
].indirect_offset
= indirect_offset
;
2128 prim
[i
].is_indirect
= 1;
2129 prim
[i
].draw_id
= i
;
2132 /* This should always be true at this time */
2133 assert(indirect_data
== ctx
->DrawIndirectBuffer
);
2135 ctx
->Driver
.Draw(ctx
, prim
, draw_count
, ib
, false, 0u, ~0u,
2136 NULL
, 0, indirect_data
);
2141 * Function to be put into dd_function_table::DrawIndirect as fallback.
2142 * Calls into dd_function_table::Draw past adapting call arguments.
2143 * See dd_function_table::DrawIndirect for call argument documentation.
2146 _mesa_draw_indirect(struct gl_context
*ctx
, GLuint mode
,
2147 struct gl_buffer_object
*indirect_data
,
2148 GLsizeiptr indirect_offset
, unsigned draw_count
,
2150 struct gl_buffer_object
*indirect_draw_count_buffer
,
2151 GLsizeiptr indirect_draw_count_offset
,
2152 const struct _mesa_index_buffer
*ib
)
2154 /* Use alloca for the prim space if we are somehow in bounds. */
2155 if (draw_count
*sizeof(struct _mesa_prim
) < 1024) {
2156 struct _mesa_prim
*space
= alloca(draw_count
*sizeof(struct _mesa_prim
));
2157 memset(space
, 0, draw_count
*sizeof(struct _mesa_prim
));
2159 draw_indirect(ctx
, mode
, indirect_data
, indirect_offset
, draw_count
,
2160 stride
, indirect_draw_count_buffer
,
2161 indirect_draw_count_offset
, ib
, space
);
2163 struct _mesa_prim
*space
= calloc(draw_count
, sizeof(struct _mesa_prim
));
2164 if (space
== NULL
) {
2165 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
2166 (draw_count
> 1) ? "Multi" : "",
2167 ib
? "Elements" : "Arrays",
2168 indirect_data
? "CountARB" : "");
2172 draw_indirect(ctx
, mode
, indirect_data
, indirect_offset
, draw_count
,
2173 stride
, indirect_draw_count_buffer
,
2174 indirect_draw_count_offset
, ib
, space
);