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 **************************************************************************/
30 #include "main/arrayobj.h"
31 #include "main/glheader.h"
32 #include "main/context.h"
33 #include "main/state.h"
34 #include "main/draw_validate.h"
35 #include "main/dispatch.h"
36 #include "main/varray.h"
37 #include "main/bufferobj.h"
38 #include "main/enums.h"
39 #include "main/macros.h"
40 #include "main/transformfeedback.h"
47 } DrawArraysIndirectCommand
;
55 } DrawElementsIndirectCommand
;
59 * Check that element 'j' of the array has reasonable data.
61 * For debugging purposes; not normally used.
64 check_array_data(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
65 GLuint attrib
, GLuint j
)
67 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
69 const struct gl_vertex_buffer_binding
*binding
=
70 &vao
->BufferBinding
[array
->BufferBindingIndex
];
71 struct gl_buffer_object
*bo
= binding
->BufferObj
;
72 const void *data
= array
->Ptr
;
73 if (_mesa_is_bufferobj(bo
)) {
74 if (!bo
->Mappings
[MAP_INTERNAL
].Pointer
) {
76 bo
->Mappings
[MAP_INTERNAL
].Pointer
=
77 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
,
78 GL_MAP_READ_BIT
, bo
, MAP_INTERNAL
);
80 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
81 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
83 switch (array
->Type
) {
86 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
88 for (k
= 0; k
< array
->Size
; k
++) {
89 if (IS_INF_OR_NAN(f
[k
]) || f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
90 printf("Bad array data:\n");
91 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
92 printf(" Array %u at %p\n", attrib
, (void *) array
);
93 printf(" Type 0x%x, Size %d, Stride %d\n",
94 array
->Type
, array
->Size
, binding
->Stride
);
95 printf(" Address/offset %p in Buffer Object %u\n",
96 array
->Ptr
, bo
->Name
);
97 f
[k
] = 1.0F
; /* XXX replace the bad value! */
99 /*assert(!IS_INF_OR_NAN(f[k])); */
111 * Unmap the buffer object referenced by given array, if mapped.
114 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
117 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
118 if (array
->Enabled
) {
119 const struct gl_vertex_buffer_binding
*binding
=
120 &vao
->BufferBinding
[array
->BufferBindingIndex
];
121 struct gl_buffer_object
*bo
= binding
->BufferObj
;
122 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
123 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
130 sizeof_ib_type(GLenum type
)
133 case GL_UNSIGNED_INT
:
134 return sizeof(GLuint
);
135 case GL_UNSIGNED_SHORT
:
136 return sizeof(GLushort
);
137 case GL_UNSIGNED_BYTE
:
138 return sizeof(GLubyte
);
140 assert(!"unsupported index data type");
141 /* In case assert is turned off */
147 * Examine the array's data for NaNs, etc.
148 * For debug purposes; not normally used.
151 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
152 GLenum elemType
, const void *elements
,
155 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
160 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
161 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
162 vao
->IndexBufferObj
->Size
,
164 vao
->IndexBufferObj
, MAP_INTERNAL
);
165 elements
= ADD_POINTERS(elements
, elemMap
);
168 for (i
= 0; i
< count
; i
++) {
173 case GL_UNSIGNED_BYTE
:
174 j
= ((const GLubyte
*) elements
)[i
];
176 case GL_UNSIGNED_SHORT
:
177 j
= ((const GLushort
*) elements
)[i
];
179 case GL_UNSIGNED_INT
:
180 j
= ((const GLuint
*) elements
)[i
];
183 unreachable("Unexpected index buffer type");
186 /* check element j of each enabled array */
187 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
188 check_array_data(ctx
, vao
, k
, j
);
192 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
193 ctx
->Driver
.UnmapBuffer(ctx
, vao
->IndexBufferObj
, MAP_INTERNAL
);
196 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
197 unmap_array_buffer(ctx
, vao
, k
);
203 * Check array data, looking for NaNs, etc.
206 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
213 * Check if we should skip the draw call even after validation was successful.
216 skip_validated_draw(struct gl_context
*ctx
)
220 /* For ES2, we can draw if we have a vertex program/shader). */
221 return ctx
->VertexProgram
._Current
== NULL
;
224 /* For OpenGL ES, only draw if we have vertex positions
226 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
230 case API_OPENGL_CORE
:
231 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
234 * "If there is no active program for the vertex or fragment shader
235 * stages, the results of vertex and/or fragment processing will be
236 * undefined. However, this is not an error."
238 * The fragment shader is not tested here because other state (e.g.,
239 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
241 return ctx
->VertexProgram
._Current
== NULL
;
243 case API_OPENGL_COMPAT
:
244 if (ctx
->VertexProgram
._Current
!= NULL
) {
245 /* Draw regardless of whether or not we have any vertex arrays.
246 * (Ex: could draw a point using a constant vertex pos)
250 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
253 return (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
&&
254 !ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
259 unreachable("Invalid API value in check_valid_to_render()");
267 * Print info/data for glDrawArrays(), for debugging.
270 print_draw_arrays(struct gl_context
*ctx
,
271 GLenum mode
, GLint start
, GLsizei count
)
273 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
275 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
279 for (i
= 0; i
< VERT_ATTRIB_MAX
; ++i
) {
280 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 enabled %d "
289 "ptr %p Bufobj %u\n",
290 gl_vert_attrib_name((gl_vert_attrib
) i
),
291 array
->Size
, binding
->Stride
, array
->Enabled
,
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 vbo_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 vbo_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 vbo_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 vbo_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).
539 static void GLAPIENTRY
540 vbo_exec_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 if (_mesa_is_no_error_enabled(ctx
)) {
551 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
554 _mesa_update_state(ctx
);
556 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
558 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
563 check_draw_arrays_data(ctx
, start
, count
);
565 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
568 print_draw_arrays(ctx
, mode
, start
, count
);
573 * Called from glDrawArraysInstanced when in immediate mode (not
574 * display list mode).
576 static void GLAPIENTRY
577 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
578 GLsizei numInstances
)
580 GET_CURRENT_CONTEXT(ctx
);
582 if (MESA_VERBOSE
& VERBOSE_DRAW
)
583 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
584 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
588 if (_mesa_is_no_error_enabled(ctx
)) {
589 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
592 _mesa_update_state(ctx
);
594 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
596 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
602 check_draw_arrays_data(ctx
, start
, count
);
604 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
607 print_draw_arrays(ctx
, mode
, start
, count
);
612 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
614 static void GLAPIENTRY
615 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
616 GLsizei count
, GLsizei numInstances
,
619 GET_CURRENT_CONTEXT(ctx
);
621 if (MESA_VERBOSE
& VERBOSE_DRAW
)
623 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
624 _mesa_enum_to_string(mode
), first
, count
,
625 numInstances
, baseInstance
);
629 if (_mesa_is_no_error_enabled(ctx
)) {
630 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
633 _mesa_update_state(ctx
);
635 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
637 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
643 check_draw_arrays_data(ctx
, first
, count
);
645 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
648 print_draw_arrays(ctx
, mode
, first
, count
);
653 * Called from glMultiDrawArrays when in immediate mode.
655 static void GLAPIENTRY
656 vbo_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
657 const GLsizei
*count
, GLsizei primcount
)
659 GET_CURRENT_CONTEXT(ctx
);
662 if (MESA_VERBOSE
& VERBOSE_DRAW
)
664 "glMultiDrawArrays(%s, %p, %p, %d)\n",
665 _mesa_enum_to_string(mode
), first
, count
, primcount
);
669 if (_mesa_is_no_error_enabled(ctx
)) {
670 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
673 _mesa_update_state(ctx
);
675 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
677 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
681 for (i
= 0; i
< primcount
; i
++) {
684 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
686 /* The GL_ARB_shader_draw_parameters spec adds the following after the
687 * pseudo-code describing glMultiDrawArrays:
689 * "The index of the draw (<i> in the above pseudo-code) may be
690 * read by a vertex shader as <gl_DrawIDARB>, as described in
693 vbo_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
696 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
704 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
709 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
712 ctx
->Driver
.MapBufferRange(ctx
, 0,
713 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
715 ctx
->Array
.VAO
->IndexBufferObj
,
718 case GL_UNSIGNED_BYTE
:
720 const GLubyte
*us
= (const GLubyte
*) map
;
722 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
723 printf("%02x ", us
[i
]);
730 case GL_UNSIGNED_SHORT
:
732 const GLushort
*us
= (const GLushort
*) map
;
734 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
735 printf("%04x ", us
[i
]);
742 case GL_UNSIGNED_INT
:
744 const GLuint
*us
= (const GLuint
*) map
;
746 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
747 printf("%08x ", us
[i
]);
758 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
764 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
765 const GLvoid
*indices
)
770 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
772 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
775 if (skip_validated_draw(ctx
))
783 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
784 * Do the rendering for a glDrawElements or glDrawRangeElements call after
785 * we've validated buffer bounds, etc.
788 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
789 GLboolean index_bounds_valid
,
790 GLuint start
, GLuint end
,
791 GLsizei count
, GLenum type
,
792 const GLvoid
* indices
,
793 GLint basevertex
, GLuint numInstances
,
796 struct _mesa_index_buffer ib
;
797 struct _mesa_prim prim
;
799 if (!index_bounds_valid
) {
804 if (skip_draw_elements(ctx
, count
, indices
))
808 ib
.index_size
= sizeof_ib_type(type
);
809 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
820 prim
.is_indirect
= 0;
821 prim
.basevertex
= basevertex
;
822 prim
.num_instances
= numInstances
;
823 prim
.base_instance
= baseInstance
;
826 /* Need to give special consideration to rendering a range of
827 * indices starting somewhere above zero. Typically the
828 * application is issuing multiple DrawRangeElements() to draw
829 * successive primitives layed out linearly in the vertex arrays.
830 * Unless the vertex arrays are all in a VBO (or locked as with
831 * CVA), the OpenGL semantics imply that we need to re-read or
832 * re-upload the vertex data on each draw call.
834 * In the case of hardware tnl, we want to avoid starting the
835 * upload at zero, as it will mean every draw call uploads an
836 * increasing amount of not-used vertex data. Worse - in the
837 * software tnl module, all those vertices might be transformed and
838 * lit but never rendered.
840 * If we just upload or transform the vertices in start..end,
841 * however, the indices will be incorrect.
843 * At this level, we don't know exactly what the requirements of
844 * the backend are going to be, though it will likely boil down to
847 * 1) Do nothing, everything is in a VBO and is processed once
850 * 2) Adjust the indices and vertex arrays so that start becomes
853 * Rather than doing anything here, I'll provide a helper function
854 * for the latter case elsewhere.
857 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
858 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
860 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
867 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
869 static void GLAPIENTRY
870 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
871 GLsizei count
, GLenum type
,
872 const GLvoid
* indices
, GLint basevertex
)
874 static GLuint warnCount
= 0;
875 GLboolean index_bounds_valid
= GL_TRUE
;
877 /* This is only useful to catch invalid values in the "end" parameter
880 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
882 GET_CURRENT_CONTEXT(ctx
);
884 if (MESA_VERBOSE
& VERBOSE_DRAW
)
886 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
887 _mesa_enum_to_string(mode
), start
, end
, count
,
888 _mesa_enum_to_string(type
), indices
, basevertex
);
892 if (_mesa_is_no_error_enabled(ctx
)) {
893 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
896 _mesa_update_state(ctx
);
898 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
900 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
905 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
906 /* The application requested we draw using a range of indices that's
907 * outside the bounds of the current VBO. This is invalid and appears
908 * to give undefined results. The safest thing to do is to simply
909 * ignore the range, in case the application botched their range tracking
910 * but did provide valid indices. Also issue a warning indicating that
911 * the application is broken.
913 if (warnCount
++ < 10) {
914 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
915 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
916 "\trange is outside VBO bounds (max=%u); ignoring.\n"
917 "\tThis should be fixed in the application.",
918 start
, end
, basevertex
, count
, type
, indices
,
921 index_bounds_valid
= GL_FALSE
;
924 /* NOTE: It's important that 'end' is a reasonable value.
925 * in _tnl_draw_prims(), we use end to determine how many vertices
926 * to transform. If it's too large, we can unnecessarily split prims
927 * or we can read/write out of memory in several different places!
930 /* Catch/fix some potential user errors */
931 if (type
== GL_UNSIGNED_BYTE
) {
932 start
= MIN2(start
, 0xff);
933 end
= MIN2(end
, 0xff);
935 else if (type
== GL_UNSIGNED_SHORT
) {
936 start
= MIN2(start
, 0xffff);
937 end
= MIN2(end
, 0xffff);
941 printf("glDraw[Range]Elements{,BaseVertex}"
942 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
944 start
, end
, type
, count
,
945 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
948 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
949 index_bounds_valid
= GL_FALSE
;
952 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
954 (void) check_draw_elements_data
;
957 if (!index_bounds_valid
) {
962 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
963 count
, type
, indices
, basevertex
, 1, 0);
968 * Called by glDrawRangeElements() in immediate mode.
970 static void GLAPIENTRY
971 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
972 GLsizei count
, GLenum type
, const GLvoid
* indices
)
974 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
975 GET_CURRENT_CONTEXT(ctx
);
977 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
978 _mesa_enum_to_string(mode
), start
, end
, count
,
979 _mesa_enum_to_string(type
), indices
);
982 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
988 * Called by glDrawElements() in immediate mode.
990 static void GLAPIENTRY
991 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
992 const GLvoid
* indices
)
994 GET_CURRENT_CONTEXT(ctx
);
996 if (MESA_VERBOSE
& VERBOSE_DRAW
)
997 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
998 _mesa_enum_to_string(mode
), count
,
999 _mesa_enum_to_string(type
), indices
);
1001 FLUSH_FOR_DRAW(ctx
);
1003 if (_mesa_is_no_error_enabled(ctx
)) {
1004 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1007 _mesa_update_state(ctx
);
1009 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1011 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1015 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1016 count
, type
, indices
, 0, 1, 0);
1021 * Called by glDrawElementsBaseVertex() in immediate mode.
1023 static void GLAPIENTRY
1024 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1025 const GLvoid
* indices
, GLint basevertex
)
1027 GET_CURRENT_CONTEXT(ctx
);
1029 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1030 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1031 _mesa_enum_to_string(mode
), count
,
1032 _mesa_enum_to_string(type
), indices
);
1034 FLUSH_FOR_DRAW(ctx
);
1036 if (_mesa_is_no_error_enabled(ctx
)) {
1037 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1040 _mesa_update_state(ctx
);
1042 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1044 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1048 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1049 count
, type
, indices
, basevertex
, 1, 0);
1054 * Called by glDrawElementsInstanced() in immediate mode.
1056 static void GLAPIENTRY
1057 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1058 const GLvoid
* indices
, GLsizei numInstances
)
1060 GET_CURRENT_CONTEXT(ctx
);
1062 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1063 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1064 _mesa_enum_to_string(mode
), count
,
1065 _mesa_enum_to_string(type
), indices
);
1067 FLUSH_FOR_DRAW(ctx
);
1069 if (_mesa_is_no_error_enabled(ctx
)) {
1070 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1073 _mesa_update_state(ctx
);
1075 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1077 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1078 indices
, numInstances
))
1082 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1083 count
, type
, indices
, 0, numInstances
, 0);
1088 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1090 static void GLAPIENTRY
1091 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1092 GLenum type
, const GLvoid
* indices
,
1093 GLsizei numInstances
,
1096 GET_CURRENT_CONTEXT(ctx
);
1098 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1100 "glDrawElementsInstancedBaseVertex"
1101 "(%s, %d, %s, %p, %d; %d)\n",
1102 _mesa_enum_to_string(mode
), count
,
1103 _mesa_enum_to_string(type
), indices
,
1104 numInstances
, basevertex
);
1106 FLUSH_FOR_DRAW(ctx
);
1108 if (_mesa_is_no_error_enabled(ctx
)) {
1109 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1112 _mesa_update_state(ctx
);
1114 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1116 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1117 indices
, numInstances
))
1121 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1122 count
, type
, indices
,
1123 basevertex
, numInstances
, 0);
1128 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1130 static void GLAPIENTRY
1131 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1133 const GLvoid
*indices
,
1134 GLsizei numInstances
,
1135 GLuint baseInstance
)
1137 GET_CURRENT_CONTEXT(ctx
);
1139 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1141 "glDrawElementsInstancedBaseInstance"
1142 "(%s, %d, %s, %p, %d, %d)\n",
1143 _mesa_enum_to_string(mode
), count
,
1144 _mesa_enum_to_string(type
), indices
,
1145 numInstances
, baseInstance
);
1147 FLUSH_FOR_DRAW(ctx
);
1149 if (_mesa_is_no_error_enabled(ctx
)) {
1150 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1153 _mesa_update_state(ctx
);
1155 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1157 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1158 indices
, numInstances
))
1162 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1163 count
, type
, indices
, 0, numInstances
,
1169 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1171 static void GLAPIENTRY
1172 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1175 const GLvoid
*indices
,
1176 GLsizei numInstances
,
1178 GLuint baseInstance
)
1180 GET_CURRENT_CONTEXT(ctx
);
1182 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1184 "glDrawElementsInstancedBaseVertexBaseInstance"
1185 "(%s, %d, %s, %p, %d, %d, %d)\n",
1186 _mesa_enum_to_string(mode
), count
,
1187 _mesa_enum_to_string(type
), indices
,
1188 numInstances
, basevertex
, baseInstance
);
1190 FLUSH_FOR_DRAW(ctx
);
1192 if (_mesa_is_no_error_enabled(ctx
)) {
1193 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1196 _mesa_update_state(ctx
);
1198 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1200 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1201 indices
, numInstances
))
1205 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1206 count
, type
, indices
, basevertex
,
1207 numInstances
, baseInstance
);
1212 * Inner support for both _mesa_MultiDrawElements() and
1213 * _mesa_MultiDrawRangeElements().
1214 * This does the actual rendering after we've checked array indexes, etc.
1217 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1218 const GLsizei
*count
, GLenum type
,
1219 const GLvoid
* const *indices
,
1220 GLsizei primcount
, const GLint
*basevertex
)
1222 struct _mesa_index_buffer ib
;
1223 struct _mesa_prim
*prim
;
1224 unsigned int index_type_size
= sizeof_ib_type(type
);
1225 uintptr_t min_index_ptr
, max_index_ptr
;
1226 GLboolean fallback
= GL_FALSE
;
1232 prim
= calloc(primcount
, sizeof(*prim
));
1234 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1238 min_index_ptr
= (uintptr_t) indices
[0];
1240 for (i
= 0; i
< primcount
; i
++) {
1241 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1242 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1243 index_type_size
* count
[i
]);
1246 /* Check if we can handle this thing as a bunch of index offsets from the
1247 * same index pointer. If we can't, then we have to fall back to doing
1248 * a draw_prims per primitive.
1249 * Check that the difference between each prim's indexes is a multiple of
1250 * the index/element size.
1252 if (index_type_size
!= 1) {
1253 for (i
= 0; i
< primcount
; i
++) {
1254 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1262 /* Draw primitives individually if one count is zero, so we can easily skip
1265 for (i
= 0; i
< primcount
; i
++) {
1266 if (count
[i
] == 0) {
1272 /* If the index buffer isn't in a VBO, then treating the application's
1273 * subranges of the index buffer as one large index buffer may lead to
1274 * us reading unmapped memory.
1276 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1280 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1281 ib
.index_size
= sizeof_ib_type(type
);
1282 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1283 ib
.ptr
= (void *) min_index_ptr
;
1285 for (i
= 0; i
< primcount
; i
++) {
1286 prim
[i
].begin
= (i
== 0);
1287 prim
[i
].end
= (i
== primcount
- 1);
1290 prim
[i
].mode
= mode
;
1292 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1293 prim
[i
].count
= count
[i
];
1294 prim
[i
].indexed
= 1;
1295 prim
[i
].num_instances
= 1;
1296 prim
[i
].base_instance
= 0;
1297 prim
[i
].draw_id
= i
;
1298 prim
[i
].is_indirect
= 0;
1299 if (basevertex
!= NULL
)
1300 prim
[i
].basevertex
= basevertex
[i
];
1302 prim
[i
].basevertex
= 0;
1305 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1306 false, 0, ~0, NULL
, 0, NULL
);
1309 /* render one prim at a time */
1310 for (i
= 0; i
< primcount
; i
++) {
1313 ib
.count
= count
[i
];
1314 ib
.index_size
= sizeof_ib_type(type
);
1315 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1316 ib
.ptr
= indices
[i
];
1322 prim
[0].mode
= mode
;
1324 prim
[0].count
= count
[i
];
1325 prim
[0].indexed
= 1;
1326 prim
[0].num_instances
= 1;
1327 prim
[0].base_instance
= 0;
1328 prim
[0].draw_id
= i
;
1329 prim
[0].is_indirect
= 0;
1330 if (basevertex
!= NULL
)
1331 prim
[0].basevertex
= basevertex
[i
];
1333 prim
[0].basevertex
= 0;
1335 ctx
->Driver
.Draw(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1341 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1347 static void GLAPIENTRY
1348 vbo_exec_MultiDrawElements(GLenum mode
,
1349 const GLsizei
*count
, GLenum type
,
1350 const GLvoid
* const *indices
, GLsizei primcount
)
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_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1362 if (skip_validated_draw(ctx
))
1365 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1370 static void GLAPIENTRY
1371 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1372 const GLsizei
*count
, GLenum type
,
1373 const GLvoid
* const *indices
,
1375 const GLsizei
*basevertex
)
1377 GET_CURRENT_CONTEXT(ctx
);
1379 FLUSH_FOR_DRAW(ctx
);
1381 if (_mesa_is_no_error_enabled(ctx
)) {
1382 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1385 _mesa_update_state(ctx
);
1387 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1389 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1394 if (skip_validated_draw(ctx
))
1397 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1403 * Draw a GL primitive using a vertex count obtained from transform feedback.
1404 * \param mode the type of GL primitive to draw
1405 * \param obj the transform feedback object to use
1406 * \param stream index of the transform feedback stream from which to
1407 * get the primitive count.
1408 * \param numInstances number of instances to draw
1411 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1412 struct gl_transform_feedback_object
*obj
,
1413 GLuint stream
, GLuint numInstances
)
1415 struct _mesa_prim prim
;
1417 FLUSH_FOR_DRAW(ctx
);
1419 if (_mesa_is_no_error_enabled(ctx
)) {
1420 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1423 _mesa_update_state(ctx
);
1425 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1427 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1433 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1434 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1435 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1437 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1438 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1442 if (skip_validated_draw(ctx
))
1445 /* init most fields to zero */
1446 memset(&prim
, 0, sizeof(prim
));
1450 prim
.num_instances
= numInstances
;
1451 prim
.base_instance
= 0;
1452 prim
.is_indirect
= 0;
1454 /* Maybe we should do some primitive splitting for primitive restart
1455 * (like in DrawArrays), but we have no way to know how many vertices
1456 * will be rendered. */
1458 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1460 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1467 * Like DrawArrays, but take the count from a transform feedback object.
1468 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1469 * \param name the transform feedback object
1470 * User still has to setup of the vertex attribute info with
1471 * glVertexPointer, glColorPointer, etc.
1472 * Part of GL_ARB_transform_feedback2.
1474 static void GLAPIENTRY
1475 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1477 GET_CURRENT_CONTEXT(ctx
);
1478 struct gl_transform_feedback_object
*obj
=
1479 _mesa_lookup_transform_feedback_object(ctx
, name
);
1481 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1482 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1483 _mesa_enum_to_string(mode
), name
);
1485 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1489 static void GLAPIENTRY
1490 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1492 GET_CURRENT_CONTEXT(ctx
);
1493 struct gl_transform_feedback_object
*obj
=
1494 _mesa_lookup_transform_feedback_object(ctx
, name
);
1496 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1497 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1498 _mesa_enum_to_string(mode
), name
, stream
);
1500 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1504 static void GLAPIENTRY
1505 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1508 GET_CURRENT_CONTEXT(ctx
);
1509 struct gl_transform_feedback_object
*obj
=
1510 _mesa_lookup_transform_feedback_object(ctx
, name
);
1512 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1513 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1514 _mesa_enum_to_string(mode
), name
);
1516 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1520 static void GLAPIENTRY
1521 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1525 GET_CURRENT_CONTEXT(ctx
);
1526 struct gl_transform_feedback_object
*obj
=
1527 _mesa_lookup_transform_feedback_object(ctx
, name
);
1529 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1530 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1531 "(%s, %u, %u, %i)\n",
1532 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1534 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1539 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1540 GLenum mode
, const GLvoid
*indirect
)
1542 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1543 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1544 1 /* draw_count */ , 16 /* stride */ ,
1547 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1553 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1555 const GLvoid
*indirect
,
1556 GLsizei primcount
, GLsizei stride
)
1558 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1563 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1564 primcount
, stride
, NULL
, 0, NULL
);
1566 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1572 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1573 GLenum mode
, GLenum type
,
1574 const GLvoid
*indirect
)
1576 struct _mesa_index_buffer ib
;
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
, (GLsizeiptr
) indirect
,
1585 1 /* draw_count */ , 20 /* stride */ ,
1588 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1594 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1595 GLenum mode
, GLenum type
,
1596 const GLvoid
*indirect
,
1597 GLsizei primcount
, GLsizei stride
)
1599 struct _mesa_index_buffer ib
;
1600 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1605 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1607 ib
.count
= 0; /* unknown */
1608 ib
.index_size
= sizeof_ib_type(type
);
1609 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1612 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1613 ctx
->DrawIndirectBuffer
, offset
,
1614 primcount
, stride
, NULL
, 0, &ib
);
1616 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1622 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1625 static void GLAPIENTRY
1626 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1628 GET_CURRENT_CONTEXT(ctx
);
1630 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1631 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1632 _mesa_enum_to_string(mode
), indirect
);
1634 /* From the ARB_draw_indirect spec:
1636 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1637 * compatibility profile, this indicates that DrawArraysIndirect and
1638 * DrawElementsIndirect are to source their arguments directly from the
1639 * pointer passed as their <indirect> parameters."
1641 if (ctx
->API
== API_OPENGL_COMPAT
&&
1642 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1643 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) indirect
;
1645 vbo_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
, cmd
->count
,
1651 FLUSH_FOR_DRAW(ctx
);
1653 if (_mesa_is_no_error_enabled(ctx
)) {
1654 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1657 _mesa_update_state(ctx
);
1659 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1661 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1665 if (skip_validated_draw(ctx
))
1668 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1672 static void GLAPIENTRY
1673 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1675 GET_CURRENT_CONTEXT(ctx
);
1677 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1678 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1679 _mesa_enum_to_string(mode
),
1680 _mesa_enum_to_string(type
), indirect
);
1682 /* From the ARB_draw_indirect spec:
1684 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1685 * compatibility profile, this indicates that DrawArraysIndirect and
1686 * DrawElementsIndirect are to source their arguments directly from the
1687 * pointer passed as their <indirect> parameters."
1689 if (ctx
->API
== API_OPENGL_COMPAT
&&
1690 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1692 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1693 * may not come from a client array and must come from an index buffer.
1694 * If no element array buffer is bound, an INVALID_OPERATION error is
1697 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1699 "glDrawElementsIndirect(no buffer bound "
1700 "to GL_ELEMENT_ARRAY_BUFFER)");
1702 DrawElementsIndirectCommand
*cmd
=
1703 (DrawElementsIndirectCommand
*) indirect
;
1705 /* Convert offset to pointer */
1706 void *offset
= (void *)
1707 ((cmd
->firstIndex
* _mesa_sizeof_type(type
)) & 0xffffffffUL
);
1709 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(mode
, cmd
->count
,
1719 FLUSH_FOR_DRAW(ctx
);
1721 if (_mesa_is_no_error_enabled(ctx
)) {
1722 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1725 _mesa_update_state(ctx
);
1727 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1729 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1733 if (skip_validated_draw(ctx
))
1736 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1740 static void GLAPIENTRY
1741 vbo_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1742 GLsizei primcount
, GLsizei stride
)
1744 GET_CURRENT_CONTEXT(ctx
);
1746 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1747 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1748 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1750 /* If <stride> is zero, the array elements are treated as tightly packed. */
1752 stride
= sizeof(DrawArraysIndirectCommand
);
1754 /* From the ARB_draw_indirect spec:
1756 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1757 * compatibility profile, this indicates that DrawArraysIndirect and
1758 * DrawElementsIndirect are to source their arguments directly from the
1759 * pointer passed as their <indirect> parameters."
1761 if (ctx
->API
== API_OPENGL_COMPAT
&&
1762 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1764 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1765 "glMultiDrawArraysIndirect"))
1768 const ubyte
*ptr
= (const ubyte
*) indirect
;
1769 for (unsigned i
= 0; i
< primcount
; i
++) {
1770 DrawArraysIndirectCommand
*cmd
= (DrawArraysIndirectCommand
*) ptr
;
1771 vbo_exec_DrawArraysInstancedBaseInstance(mode
, cmd
->first
,
1772 cmd
->count
, cmd
->primCount
,
1776 ptr
+= sizeof(DrawArraysIndirectCommand
);
1785 FLUSH_FOR_DRAW(ctx
);
1787 if (_mesa_is_no_error_enabled(ctx
)) {
1788 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1791 _mesa_update_state(ctx
);
1793 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1795 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1800 if (skip_validated_draw(ctx
))
1803 vbo_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1808 static void GLAPIENTRY
1809 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1810 const GLvoid
*indirect
,
1811 GLsizei primcount
, GLsizei stride
)
1813 GET_CURRENT_CONTEXT(ctx
);
1815 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1816 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1817 _mesa_enum_to_string(mode
),
1818 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1820 /* If <stride> is zero, the array elements are treated as tightly packed. */
1822 stride
= sizeof(DrawElementsIndirectCommand
);
1825 /* From the ARB_draw_indirect spec:
1827 * "Initially zero is bound to DRAW_INDIRECT_BUFFER. In the
1828 * compatibility profile, this indicates that DrawArraysIndirect and
1829 * DrawElementsIndirect are to source their arguments directly from the
1830 * pointer passed as their <indirect> parameters."
1832 if (ctx
->API
== API_OPENGL_COMPAT
&&
1833 !_mesa_is_bufferobj(ctx
->DrawIndirectBuffer
)) {
1835 * Unlike regular DrawElementsInstancedBaseVertex commands, the indices
1836 * may not come from a client array and must come from an index buffer.
1837 * If no element array buffer is bound, an INVALID_OPERATION error is
1840 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
1841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1842 "glMultiDrawElementsIndirect(no buffer bound "
1843 "to GL_ELEMENT_ARRAY_BUFFER)");
1848 if (!_mesa_valid_draw_indirect_multi(ctx
, primcount
, stride
,
1849 "glMultiDrawArraysIndirect"))
1852 const ubyte
*ptr
= (const ubyte
*) indirect
;
1853 for (unsigned i
= 0; i
< primcount
; i
++) {
1854 vbo_exec_DrawElementsIndirect(mode
, type
, ptr
);
1857 ptr
+= sizeof(DrawElementsIndirectCommand
);
1866 FLUSH_FOR_DRAW(ctx
);
1868 if (_mesa_is_no_error_enabled(ctx
)) {
1869 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1872 _mesa_update_state(ctx
);
1874 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1876 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1881 if (skip_validated_draw(ctx
))
1884 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1890 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1893 GLintptr drawcount_offset
,
1894 GLsizei maxdrawcount
,
1897 GLsizeiptr offset
= indirect
;
1899 if (maxdrawcount
== 0)
1902 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1903 ctx
->DrawIndirectBuffer
, offset
,
1904 maxdrawcount
, stride
,
1905 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1907 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1913 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1914 GLenum mode
, GLenum type
,
1916 GLintptr drawcount_offset
,
1917 GLsizei maxdrawcount
,
1920 struct _mesa_index_buffer ib
;
1921 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1923 if (maxdrawcount
== 0)
1926 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1928 ib
.count
= 0; /* unknown */
1929 ib
.index_size
= sizeof_ib_type(type
);
1930 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1933 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1934 ctx
->DrawIndirectBuffer
, offset
,
1935 maxdrawcount
, stride
,
1936 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1938 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1943 static void GLAPIENTRY
1944 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1945 GLintptr drawcount_offset
,
1946 GLsizei maxdrawcount
, GLsizei stride
)
1948 GET_CURRENT_CONTEXT(ctx
);
1950 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1951 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1952 "(%s, %lx, %lx, %i, %i)\n",
1953 _mesa_enum_to_string(mode
),
1954 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1955 maxdrawcount
, stride
);
1957 /* If <stride> is zero, the array elements are treated as tightly packed. */
1959 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1961 FLUSH_FOR_DRAW(ctx
);
1963 if (_mesa_is_no_error_enabled(ctx
)) {
1964 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1967 _mesa_update_state(ctx
);
1969 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1971 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1974 maxdrawcount
, stride
))
1978 if (skip_validated_draw(ctx
))
1981 vbo_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1983 maxdrawcount
, stride
);
1987 static void GLAPIENTRY
1988 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1990 GLintptr drawcount_offset
,
1991 GLsizei maxdrawcount
, GLsizei stride
)
1993 GET_CURRENT_CONTEXT(ctx
);
1995 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1996 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1997 "(%s, %s, %lx, %lx, %i, %i)\n",
1998 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1999 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
2000 maxdrawcount
, stride
);
2002 /* If <stride> is zero, the array elements are treated as tightly packed. */
2004 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
2006 FLUSH_FOR_DRAW(ctx
);
2008 if (_mesa_is_no_error_enabled(ctx
)) {
2009 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
2012 _mesa_update_state(ctx
);
2014 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
2016 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
2019 maxdrawcount
, stride
))
2023 if (skip_validated_draw(ctx
))
2026 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
2027 drawcount_offset
, maxdrawcount
,
2033 * Initialize the dispatch table with the VBO functions for drawing.
2036 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
2037 struct _glapi_table
*exec
)
2039 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
2040 SET_DrawElements(exec
, vbo_exec_DrawElements
);
2042 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2043 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
2046 SET_MultiDrawArrays(exec
, vbo_exec_MultiDrawArrays
);
2047 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
2049 if (ctx
->API
== API_OPENGL_COMPAT
) {
2050 SET_Rectf(exec
, vbo_exec_Rectf
);
2051 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
2052 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
2055 if (ctx
->API
!= API_OPENGLES
&&
2056 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
2057 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
2058 SET_MultiDrawElementsBaseVertex(exec
,
2059 vbo_exec_MultiDrawElementsBaseVertex
);
2061 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2062 SET_DrawRangeElementsBaseVertex(exec
,
2063 vbo_exec_DrawRangeElementsBaseVertex
);
2064 SET_DrawElementsInstancedBaseVertex(exec
,
2065 vbo_exec_DrawElementsInstancedBaseVertex
);
2069 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2070 SET_DrawArraysInstancedBaseInstance(exec
,
2071 vbo_exec_DrawArraysInstancedBaseInstance
);
2072 SET_DrawElementsInstancedBaseInstance(exec
,
2073 vbo_exec_DrawElementsInstancedBaseInstance
);
2074 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
2075 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
2078 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
)) {
2079 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
2080 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
2083 if (ctx
->API
== API_OPENGL_CORE
) {
2084 SET_MultiDrawArraysIndirectCountARB(exec
,
2085 vbo_exec_MultiDrawArraysIndirectCount
);
2086 SET_MultiDrawElementsIndirectCountARB(exec
,
2087 vbo_exec_MultiDrawElementsIndirectCount
);
2090 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2091 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
2092 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
2095 if (_mesa_is_desktop_gl(ctx
)) {
2096 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
2097 SET_DrawTransformFeedbackStream(exec
,
2098 vbo_exec_DrawTransformFeedbackStream
);
2099 SET_DrawTransformFeedbackInstanced(exec
,
2100 vbo_exec_DrawTransformFeedbackInstanced
);
2101 SET_DrawTransformFeedbackStreamInstanced(exec
,
2102 vbo_exec_DrawTransformFeedbackStreamInstanced
);
2103 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
2104 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
2111 * The following functions are only used for OpenGL ES 1/2 support.
2112 * And some aren't even supported (yet) in ES 1/2.
2117 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
2119 vbo_exec_DrawArrays(mode
, first
, count
);
2124 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
2127 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
2132 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
2133 const GLvoid
*indices
)
2135 vbo_exec_DrawElements(mode
, count
, type
, indices
);
2140 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
2141 const GLvoid
*indices
, GLint basevertex
)
2143 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
2148 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
2149 GLenum type
, const GLvoid
* indices
)
2151 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
2156 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
2157 GLsizei count
, GLenum type
,
2158 const GLvoid
*indices
, GLint basevertex
)
2160 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
2161 indices
, basevertex
);
2166 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
2167 const GLvoid
** indices
, GLsizei primcount
)
2169 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
2174 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
2175 const GLsizei
*count
, GLenum type
,
2176 const GLvoid
**indices
, GLsizei primcount
,
2177 const GLint
*basevertex
)
2179 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
2180 primcount
, basevertex
);
2185 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
2187 vbo_exec_DrawTransformFeedback(mode
, name
);