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"
44 * Check that element 'j' of the array has reasonable data.
46 * For debugging purposes; not normally used.
49 check_array_data(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
50 GLuint attrib
, GLuint j
)
52 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
54 const struct gl_vertex_buffer_binding
*binding
=
55 &vao
->BufferBinding
[array
->BufferBindingIndex
];
56 struct gl_buffer_object
*bo
= binding
->BufferObj
;
57 const void *data
= array
->Ptr
;
58 if (_mesa_is_bufferobj(bo
)) {
59 if (!bo
->Mappings
[MAP_INTERNAL
].Pointer
) {
61 bo
->Mappings
[MAP_INTERNAL
].Pointer
=
62 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
,
63 GL_MAP_READ_BIT
, bo
, MAP_INTERNAL
);
65 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
66 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
68 switch (array
->Type
) {
71 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
73 for (k
= 0; k
< array
->Size
; k
++) {
74 if (IS_INF_OR_NAN(f
[k
]) || f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
75 printf("Bad array data:\n");
76 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
77 printf(" Array %u at %p\n", attrib
, (void *) array
);
78 printf(" Type 0x%x, Size %d, Stride %d\n",
79 array
->Type
, array
->Size
, binding
->Stride
);
80 printf(" Address/offset %p in Buffer Object %u\n",
81 array
->Ptr
, bo
->Name
);
82 f
[k
] = 1.0F
; /* XXX replace the bad value! */
84 /*assert(!IS_INF_OR_NAN(f[k])); */
96 * Unmap the buffer object referenced by given array, if mapped.
99 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
102 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
103 if (array
->Enabled
) {
104 const struct gl_vertex_buffer_binding
*binding
=
105 &vao
->BufferBinding
[array
->BufferBindingIndex
];
106 struct gl_buffer_object
*bo
= binding
->BufferObj
;
107 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
108 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
115 sizeof_ib_type(GLenum type
)
118 case GL_UNSIGNED_INT
:
119 return sizeof(GLuint
);
120 case GL_UNSIGNED_SHORT
:
121 return sizeof(GLushort
);
122 case GL_UNSIGNED_BYTE
:
123 return sizeof(GLubyte
);
125 assert(!"unsupported index data type");
126 /* In case assert is turned off */
132 * Examine the array's data for NaNs, etc.
133 * For debug purposes; not normally used.
136 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
137 GLenum elemType
, const void *elements
,
140 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
145 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
146 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
147 vao
->IndexBufferObj
->Size
,
149 vao
->IndexBufferObj
, MAP_INTERNAL
);
150 elements
= ADD_POINTERS(elements
, elemMap
);
153 for (i
= 0; i
< count
; i
++) {
158 case GL_UNSIGNED_BYTE
:
159 j
= ((const GLubyte
*) elements
)[i
];
161 case GL_UNSIGNED_SHORT
:
162 j
= ((const GLushort
*) elements
)[i
];
164 case GL_UNSIGNED_INT
:
165 j
= ((const GLuint
*) elements
)[i
];
168 unreachable("Unexpected index buffer type");
171 /* check element j of each enabled array */
172 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
173 check_array_data(ctx
, vao
, k
, j
);
177 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
178 ctx
->Driver
.UnmapBuffer(ctx
, vao
->IndexBufferObj
, MAP_INTERNAL
);
181 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
182 unmap_array_buffer(ctx
, vao
, k
);
188 * Check array data, looking for NaNs, etc.
191 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
198 * Check if we should skip the draw call even after validation was successful.
201 skip_validated_draw(struct gl_context
*ctx
)
205 /* For ES2, we can draw if we have a vertex program/shader). */
206 return ctx
->VertexProgram
._Current
== NULL
;
209 /* For OpenGL ES, only draw if we have vertex positions
211 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
215 case API_OPENGL_CORE
:
216 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
219 * "If there is no active program for the vertex or fragment shader
220 * stages, the results of vertex and/or fragment processing will be
221 * undefined. However, this is not an error."
223 * The fragment shader is not tested here because other state (e.g.,
224 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
226 return ctx
->VertexProgram
._Current
== NULL
;
228 case API_OPENGL_COMPAT
:
229 if (ctx
->VertexProgram
._Current
!= NULL
) {
230 /* Draw regardless of whether or not we have any vertex arrays.
231 * (Ex: could draw a point using a constant vertex pos)
235 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
238 return (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
&&
239 !ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
244 unreachable("Invalid API value in check_valid_to_render()");
252 * Print info/data for glDrawArrays(), for debugging.
255 print_draw_arrays(struct gl_context
*ctx
,
256 GLenum mode
, GLint start
, GLsizei count
)
258 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
260 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
264 for (i
= 0; i
< VERT_ATTRIB_MAX
; ++i
) {
265 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
269 const struct gl_vertex_buffer_binding
*binding
=
270 &vao
->BufferBinding
[array
->BufferBindingIndex
];
271 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
273 printf("attr %s: size %d stride %d enabled %d "
274 "ptr %p Bufobj %u\n",
275 gl_vert_attrib_name((gl_vert_attrib
) i
),
276 array
->Size
, binding
->Stride
, array
->Enabled
,
277 array
->Ptr
, bufObj
->Name
);
279 if (_mesa_is_bufferobj(bufObj
)) {
280 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
281 GL_MAP_READ_BIT
, bufObj
,
283 int offset
= (int) (GLintptr
)
284 _mesa_vertex_attrib_address(array
, binding
);
287 switch (array
->Type
) {
290 case GL_UNSIGNED_INT64_ARB
:
297 float *f
= (float *) (p
+ offset
);
300 int n
= (count
- 1) * (binding
->Stride
/ (4 * multiplier
))
304 printf(" Data at offset %d:\n", offset
);
307 printf(" double[%d] = 0x%016llx %lf\n", i
,
308 ((unsigned long long *) k
)[i
], ((double *) f
)[i
]);
310 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
313 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
320 * Return a filter mask for the net enabled vao arrays.
321 * This is to mask out arrays that would otherwise supersed required current
322 * values for the fixed function shaders for example.
325 enabled_filter(const struct gl_context
*ctx
)
327 switch (ctx
->VertexProgram
._VPMode
) {
329 /* When no vertex program is active (or the vertex program is generated
330 * from fixed-function state). We put the material values into the
331 * generic slots. Since the vao has no material arrays, mute these
332 * slots from the enabled arrays so that the current material values
333 * are pulled instead of the vao arrays.
335 return VERT_BIT_FF_ALL
;
338 /* There are no shaders in OpenGL ES 1.x, so this code path should be
339 * impossible to reach. The meta code is careful to not use shaders in
342 assert(ctx
->API
!= API_OPENGLES
);
344 /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
345 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
346 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
349 if (ctx
->API
!= API_OPENGL_COMPAT
)
350 return VERT_BIT_GENERIC_ALL
;
362 * Helper function called by the other DrawArrays() functions below.
363 * This is where we handle primitive restart for drawing non-indexed
364 * arrays. If primitive restart is enabled, it typically means
365 * splitting one DrawArrays() into two.
368 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
369 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
372 struct _mesa_prim prim
;
374 if (skip_validated_draw(ctx
))
377 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
380 memset(&prim
, 0, sizeof(prim
));
384 prim
.num_instances
= numInstances
;
385 prim
.base_instance
= baseInstance
;
386 prim
.draw_id
= drawID
;
387 prim
.is_indirect
= 0;
391 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
,
392 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
394 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
401 * Execute a glRectf() function.
403 static void GLAPIENTRY
404 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
406 GET_CURRENT_CONTEXT(ctx
);
407 ASSERT_OUTSIDE_BEGIN_END(ctx
);
409 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
410 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
411 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
412 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
413 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
414 CALL_End(GET_DISPATCH(), ());
418 static void GLAPIENTRY
419 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
421 GET_CURRENT_CONTEXT(ctx
);
431 prim
= GL_LINE_STRIP
;
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
438 /* No effect if vertex maps disabled.
440 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
443 du
= ctx
->Eval
.MapGrid1du
;
444 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
446 CALL_Begin(GET_DISPATCH(), (prim
));
447 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
448 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
450 CALL_End(GET_DISPATCH(), ());
454 static void GLAPIENTRY
455 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
457 GET_CURRENT_CONTEXT(ctx
);
458 GLfloat u
, du
, v
, dv
, v1
, u1
;
467 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
471 /* No effect if vertex maps disabled.
473 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
476 du
= ctx
->Eval
.MapGrid2du
;
477 dv
= ctx
->Eval
.MapGrid2dv
;
478 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
479 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
483 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
484 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
485 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
486 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
489 CALL_End(GET_DISPATCH(), ());
492 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
493 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
494 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
495 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
497 CALL_End(GET_DISPATCH(), ());
499 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
500 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
501 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
502 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
504 CALL_End(GET_DISPATCH(), ());
508 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
509 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
510 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
511 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
512 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
514 CALL_End(GET_DISPATCH(), ());
522 * Called from glDrawArrays when in immediate mode (not display list mode).
524 static void GLAPIENTRY
525 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
527 GET_CURRENT_CONTEXT(ctx
);
529 if (MESA_VERBOSE
& VERBOSE_DRAW
)
530 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
531 _mesa_enum_to_string(mode
), start
, count
);
535 if (_mesa_is_no_error_enabled(ctx
)) {
536 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
539 _mesa_update_state(ctx
);
541 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
543 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
548 check_draw_arrays_data(ctx
, start
, count
);
550 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
553 print_draw_arrays(ctx
, mode
, start
, count
);
558 * Called from glDrawArraysInstanced when in immediate mode (not
559 * display list mode).
561 static void GLAPIENTRY
562 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
563 GLsizei numInstances
)
565 GET_CURRENT_CONTEXT(ctx
);
567 if (MESA_VERBOSE
& VERBOSE_DRAW
)
568 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
569 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
573 if (_mesa_is_no_error_enabled(ctx
)) {
574 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
577 _mesa_update_state(ctx
);
579 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
581 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
587 check_draw_arrays_data(ctx
, start
, count
);
589 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
592 print_draw_arrays(ctx
, mode
, start
, count
);
597 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
599 static void GLAPIENTRY
600 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
601 GLsizei count
, GLsizei numInstances
,
604 GET_CURRENT_CONTEXT(ctx
);
606 if (MESA_VERBOSE
& VERBOSE_DRAW
)
608 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
609 _mesa_enum_to_string(mode
), first
, count
,
610 numInstances
, baseInstance
);
614 if (_mesa_is_no_error_enabled(ctx
)) {
615 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
618 _mesa_update_state(ctx
);
620 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
622 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
628 check_draw_arrays_data(ctx
, first
, count
);
630 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
633 print_draw_arrays(ctx
, mode
, first
, count
);
638 * Called from glMultiDrawArrays when in immediate mode.
640 static void GLAPIENTRY
641 vbo_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
642 const GLsizei
*count
, GLsizei primcount
)
644 GET_CURRENT_CONTEXT(ctx
);
647 if (MESA_VERBOSE
& VERBOSE_DRAW
)
649 "glMultiDrawArrays(%s, %p, %p, %d)\n",
650 _mesa_enum_to_string(mode
), first
, count
, primcount
);
654 if (_mesa_is_no_error_enabled(ctx
)) {
655 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
658 _mesa_update_state(ctx
);
660 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
662 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
666 for (i
= 0; i
< primcount
; i
++) {
669 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
671 /* The GL_ARB_shader_draw_parameters spec adds the following after the
672 * pseudo-code describing glMultiDrawArrays:
674 * "The index of the draw (<i> in the above pseudo-code) may be
675 * read by a vertex shader as <gl_DrawIDARB>, as described in
678 vbo_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
681 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
689 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
694 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
697 ctx
->Driver
.MapBufferRange(ctx
, 0,
698 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
700 ctx
->Array
.VAO
->IndexBufferObj
,
703 case GL_UNSIGNED_BYTE
:
705 const GLubyte
*us
= (const GLubyte
*) map
;
707 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
708 printf("%02x ", us
[i
]);
715 case GL_UNSIGNED_SHORT
:
717 const GLushort
*us
= (const GLushort
*) map
;
719 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
720 printf("%04x ", us
[i
]);
727 case GL_UNSIGNED_INT
:
729 const GLuint
*us
= (const GLuint
*) map
;
731 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
732 printf("%08x ", us
[i
]);
743 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
749 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
750 const GLvoid
*indices
)
755 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
757 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
760 if (skip_validated_draw(ctx
))
768 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
769 * Do the rendering for a glDrawElements or glDrawRangeElements call after
770 * we've validated buffer bounds, etc.
773 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
774 GLboolean index_bounds_valid
,
775 GLuint start
, GLuint end
,
776 GLsizei count
, GLenum type
,
777 const GLvoid
* indices
,
778 GLint basevertex
, GLuint numInstances
,
781 struct _mesa_index_buffer ib
;
782 struct _mesa_prim prim
;
784 if (!index_bounds_valid
) {
789 if (skip_draw_elements(ctx
, count
, indices
))
793 ib
.index_size
= sizeof_ib_type(type
);
794 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
805 prim
.is_indirect
= 0;
806 prim
.basevertex
= basevertex
;
807 prim
.num_instances
= numInstances
;
808 prim
.base_instance
= baseInstance
;
811 /* Need to give special consideration to rendering a range of
812 * indices starting somewhere above zero. Typically the
813 * application is issuing multiple DrawRangeElements() to draw
814 * successive primitives layed out linearly in the vertex arrays.
815 * Unless the vertex arrays are all in a VBO (or locked as with
816 * CVA), the OpenGL semantics imply that we need to re-read or
817 * re-upload the vertex data on each draw call.
819 * In the case of hardware tnl, we want to avoid starting the
820 * upload at zero, as it will mean every draw call uploads an
821 * increasing amount of not-used vertex data. Worse - in the
822 * software tnl module, all those vertices might be transformed and
823 * lit but never rendered.
825 * If we just upload or transform the vertices in start..end,
826 * however, the indices will be incorrect.
828 * At this level, we don't know exactly what the requirements of
829 * the backend are going to be, though it will likely boil down to
832 * 1) Do nothing, everything is in a VBO and is processed once
835 * 2) Adjust the indices and vertex arrays so that start becomes
838 * Rather than doing anything here, I'll provide a helper function
839 * for the latter case elsewhere.
842 ctx
->Driver
.Draw(ctx
, &prim
, 1, &ib
,
843 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
845 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
852 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
854 static void GLAPIENTRY
855 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
856 GLsizei count
, GLenum type
,
857 const GLvoid
* indices
, GLint basevertex
)
859 static GLuint warnCount
= 0;
860 GLboolean index_bounds_valid
= GL_TRUE
;
862 /* This is only useful to catch invalid values in the "end" parameter
865 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
867 GET_CURRENT_CONTEXT(ctx
);
869 if (MESA_VERBOSE
& VERBOSE_DRAW
)
871 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
872 _mesa_enum_to_string(mode
), start
, end
, count
,
873 _mesa_enum_to_string(type
), indices
, basevertex
);
877 if (_mesa_is_no_error_enabled(ctx
)) {
878 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
881 _mesa_update_state(ctx
);
883 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
885 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
890 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
891 /* The application requested we draw using a range of indices that's
892 * outside the bounds of the current VBO. This is invalid and appears
893 * to give undefined results. The safest thing to do is to simply
894 * ignore the range, in case the application botched their range tracking
895 * but did provide valid indices. Also issue a warning indicating that
896 * the application is broken.
898 if (warnCount
++ < 10) {
899 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
900 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
901 "\trange is outside VBO bounds (max=%u); ignoring.\n"
902 "\tThis should be fixed in the application.",
903 start
, end
, basevertex
, count
, type
, indices
,
906 index_bounds_valid
= GL_FALSE
;
909 /* NOTE: It's important that 'end' is a reasonable value.
910 * in _tnl_draw_prims(), we use end to determine how many vertices
911 * to transform. If it's too large, we can unnecessarily split prims
912 * or we can read/write out of memory in several different places!
915 /* Catch/fix some potential user errors */
916 if (type
== GL_UNSIGNED_BYTE
) {
917 start
= MIN2(start
, 0xff);
918 end
= MIN2(end
, 0xff);
920 else if (type
== GL_UNSIGNED_SHORT
) {
921 start
= MIN2(start
, 0xffff);
922 end
= MIN2(end
, 0xffff);
926 printf("glDraw[Range]Elements{,BaseVertex}"
927 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
929 start
, end
, type
, count
,
930 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
933 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
934 index_bounds_valid
= GL_FALSE
;
937 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
939 (void) check_draw_elements_data
;
942 if (!index_bounds_valid
) {
947 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
948 count
, type
, indices
, basevertex
, 1, 0);
953 * Called by glDrawRangeElements() in immediate mode.
955 static void GLAPIENTRY
956 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
957 GLsizei count
, GLenum type
, const GLvoid
* indices
)
959 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
960 GET_CURRENT_CONTEXT(ctx
);
962 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
963 _mesa_enum_to_string(mode
), start
, end
, count
,
964 _mesa_enum_to_string(type
), indices
);
967 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
973 * Called by glDrawElements() in immediate mode.
975 static void GLAPIENTRY
976 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
977 const GLvoid
* indices
)
979 GET_CURRENT_CONTEXT(ctx
);
981 if (MESA_VERBOSE
& VERBOSE_DRAW
)
982 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
983 _mesa_enum_to_string(mode
), count
,
984 _mesa_enum_to_string(type
), indices
);
988 if (_mesa_is_no_error_enabled(ctx
)) {
989 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
992 _mesa_update_state(ctx
);
994 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
996 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1000 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1001 count
, type
, indices
, 0, 1, 0);
1006 * Called by glDrawElementsBaseVertex() in immediate mode.
1008 static void GLAPIENTRY
1009 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1010 const GLvoid
* indices
, GLint basevertex
)
1012 GET_CURRENT_CONTEXT(ctx
);
1014 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1015 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1016 _mesa_enum_to_string(mode
), count
,
1017 _mesa_enum_to_string(type
), indices
);
1019 FLUSH_FOR_DRAW(ctx
);
1021 if (_mesa_is_no_error_enabled(ctx
)) {
1022 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1025 _mesa_update_state(ctx
);
1027 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1029 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1033 vbo_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 vbo_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 if (_mesa_is_no_error_enabled(ctx
)) {
1055 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1058 _mesa_update_state(ctx
);
1060 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1062 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1063 indices
, numInstances
))
1067 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1068 count
, type
, indices
, 0, numInstances
, 0);
1073 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1075 static void GLAPIENTRY
1076 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1077 GLenum type
, const GLvoid
* indices
,
1078 GLsizei numInstances
,
1081 GET_CURRENT_CONTEXT(ctx
);
1083 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1085 "glDrawElementsInstancedBaseVertex"
1086 "(%s, %d, %s, %p, %d; %d)\n",
1087 _mesa_enum_to_string(mode
), count
,
1088 _mesa_enum_to_string(type
), indices
,
1089 numInstances
, basevertex
);
1091 FLUSH_FOR_DRAW(ctx
);
1093 if (_mesa_is_no_error_enabled(ctx
)) {
1094 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1097 _mesa_update_state(ctx
);
1099 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1101 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1102 indices
, numInstances
))
1106 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1107 count
, type
, indices
,
1108 basevertex
, numInstances
, 0);
1113 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1115 static void GLAPIENTRY
1116 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1118 const GLvoid
*indices
,
1119 GLsizei numInstances
,
1120 GLuint baseInstance
)
1122 GET_CURRENT_CONTEXT(ctx
);
1124 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1126 "glDrawElementsInstancedBaseInstance"
1127 "(%s, %d, %s, %p, %d, %d)\n",
1128 _mesa_enum_to_string(mode
), count
,
1129 _mesa_enum_to_string(type
), indices
,
1130 numInstances
, baseInstance
);
1132 FLUSH_FOR_DRAW(ctx
);
1134 if (_mesa_is_no_error_enabled(ctx
)) {
1135 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1138 _mesa_update_state(ctx
);
1140 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1142 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1143 indices
, numInstances
))
1147 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1148 count
, type
, indices
, 0, numInstances
,
1154 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1156 static void GLAPIENTRY
1157 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1160 const GLvoid
*indices
,
1161 GLsizei numInstances
,
1163 GLuint baseInstance
)
1165 GET_CURRENT_CONTEXT(ctx
);
1167 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1169 "glDrawElementsInstancedBaseVertexBaseInstance"
1170 "(%s, %d, %s, %p, %d, %d, %d)\n",
1171 _mesa_enum_to_string(mode
), count
,
1172 _mesa_enum_to_string(type
), indices
,
1173 numInstances
, basevertex
, baseInstance
);
1175 FLUSH_FOR_DRAW(ctx
);
1177 if (_mesa_is_no_error_enabled(ctx
)) {
1178 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1181 _mesa_update_state(ctx
);
1183 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1185 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1186 indices
, numInstances
))
1190 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1191 count
, type
, indices
, basevertex
,
1192 numInstances
, baseInstance
);
1197 * Inner support for both _mesa_MultiDrawElements() and
1198 * _mesa_MultiDrawRangeElements().
1199 * This does the actual rendering after we've checked array indexes, etc.
1202 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1203 const GLsizei
*count
, GLenum type
,
1204 const GLvoid
* const *indices
,
1205 GLsizei primcount
, const GLint
*basevertex
)
1207 struct _mesa_index_buffer ib
;
1208 struct _mesa_prim
*prim
;
1209 unsigned int index_type_size
= sizeof_ib_type(type
);
1210 uintptr_t min_index_ptr
, max_index_ptr
;
1211 GLboolean fallback
= GL_FALSE
;
1217 prim
= calloc(primcount
, sizeof(*prim
));
1219 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1223 min_index_ptr
= (uintptr_t) indices
[0];
1225 for (i
= 0; i
< primcount
; i
++) {
1226 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1227 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1228 index_type_size
* count
[i
]);
1231 /* Check if we can handle this thing as a bunch of index offsets from the
1232 * same index pointer. If we can't, then we have to fall back to doing
1233 * a draw_prims per primitive.
1234 * Check that the difference between each prim's indexes is a multiple of
1235 * the index/element size.
1237 if (index_type_size
!= 1) {
1238 for (i
= 0; i
< primcount
; i
++) {
1239 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1247 /* Draw primitives individually if one count is zero, so we can easily skip
1250 for (i
= 0; i
< primcount
; i
++) {
1251 if (count
[i
] == 0) {
1257 /* If the index buffer isn't in a VBO, then treating the application's
1258 * subranges of the index buffer as one large index buffer may lead to
1259 * us reading unmapped memory.
1261 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1265 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1266 ib
.index_size
= sizeof_ib_type(type
);
1267 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1268 ib
.ptr
= (void *) min_index_ptr
;
1270 for (i
= 0; i
< primcount
; i
++) {
1271 prim
[i
].begin
= (i
== 0);
1272 prim
[i
].end
= (i
== primcount
- 1);
1275 prim
[i
].mode
= mode
;
1277 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1278 prim
[i
].count
= count
[i
];
1279 prim
[i
].indexed
= 1;
1280 prim
[i
].num_instances
= 1;
1281 prim
[i
].base_instance
= 0;
1282 prim
[i
].draw_id
= i
;
1283 prim
[i
].is_indirect
= 0;
1284 if (basevertex
!= NULL
)
1285 prim
[i
].basevertex
= basevertex
[i
];
1287 prim
[i
].basevertex
= 0;
1290 ctx
->Driver
.Draw(ctx
, prim
, primcount
, &ib
,
1291 false, 0, ~0, NULL
, 0, NULL
);
1294 /* render one prim at a time */
1295 for (i
= 0; i
< primcount
; i
++) {
1298 ib
.count
= count
[i
];
1299 ib
.index_size
= sizeof_ib_type(type
);
1300 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1301 ib
.ptr
= indices
[i
];
1307 prim
[0].mode
= mode
;
1309 prim
[0].count
= count
[i
];
1310 prim
[0].indexed
= 1;
1311 prim
[0].num_instances
= 1;
1312 prim
[0].base_instance
= 0;
1313 prim
[0].draw_id
= i
;
1314 prim
[0].is_indirect
= 0;
1315 if (basevertex
!= NULL
)
1316 prim
[0].basevertex
= basevertex
[i
];
1318 prim
[0].basevertex
= 0;
1320 ctx
->Driver
.Draw(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1326 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1332 static void GLAPIENTRY
1333 vbo_exec_MultiDrawElements(GLenum mode
,
1334 const GLsizei
*count
, GLenum type
,
1335 const GLvoid
* const *indices
, GLsizei primcount
)
1337 GET_CURRENT_CONTEXT(ctx
);
1339 FLUSH_FOR_DRAW(ctx
);
1341 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1343 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1347 if (skip_validated_draw(ctx
))
1350 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1355 static void GLAPIENTRY
1356 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1357 const GLsizei
*count
, GLenum type
,
1358 const GLvoid
* const *indices
,
1360 const GLsizei
*basevertex
)
1362 GET_CURRENT_CONTEXT(ctx
);
1364 FLUSH_FOR_DRAW(ctx
);
1366 if (_mesa_is_no_error_enabled(ctx
)) {
1367 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1370 _mesa_update_state(ctx
);
1372 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1374 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1379 if (skip_validated_draw(ctx
))
1382 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1388 * Draw a GL primitive using a vertex count obtained from transform feedback.
1389 * \param mode the type of GL primitive to draw
1390 * \param obj the transform feedback object to use
1391 * \param stream index of the transform feedback stream from which to
1392 * get the primitive count.
1393 * \param numInstances number of instances to draw
1396 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1397 struct gl_transform_feedback_object
*obj
,
1398 GLuint stream
, GLuint numInstances
)
1400 struct _mesa_prim prim
;
1402 FLUSH_FOR_DRAW(ctx
);
1404 if (_mesa_is_no_error_enabled(ctx
)) {
1405 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1408 _mesa_update_state(ctx
);
1410 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1412 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1418 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1419 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1420 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1422 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1423 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1427 if (skip_validated_draw(ctx
))
1430 /* init most fields to zero */
1431 memset(&prim
, 0, sizeof(prim
));
1435 prim
.num_instances
= numInstances
;
1436 prim
.base_instance
= 0;
1437 prim
.is_indirect
= 0;
1439 /* Maybe we should do some primitive splitting for primitive restart
1440 * (like in DrawArrays), but we have no way to know how many vertices
1441 * will be rendered. */
1443 ctx
->Driver
.Draw(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1445 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1452 * Like DrawArrays, but take the count from a transform feedback object.
1453 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1454 * \param name the transform feedback object
1455 * User still has to setup of the vertex attribute info with
1456 * glVertexPointer, glColorPointer, etc.
1457 * Part of GL_ARB_transform_feedback2.
1459 static void GLAPIENTRY
1460 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1462 GET_CURRENT_CONTEXT(ctx
);
1463 struct gl_transform_feedback_object
*obj
=
1464 _mesa_lookup_transform_feedback_object(ctx
, name
);
1466 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1467 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1468 _mesa_enum_to_string(mode
), name
);
1470 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1474 static void GLAPIENTRY
1475 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
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
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1483 _mesa_enum_to_string(mode
), name
, stream
);
1485 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1489 static void GLAPIENTRY
1490 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1493 GET_CURRENT_CONTEXT(ctx
);
1494 struct gl_transform_feedback_object
*obj
=
1495 _mesa_lookup_transform_feedback_object(ctx
, name
);
1497 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1498 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1499 _mesa_enum_to_string(mode
), name
);
1501 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1505 static void GLAPIENTRY
1506 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1510 GET_CURRENT_CONTEXT(ctx
);
1511 struct gl_transform_feedback_object
*obj
=
1512 _mesa_lookup_transform_feedback_object(ctx
, name
);
1514 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1515 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1516 "(%s, %u, %u, %i)\n",
1517 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1519 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1524 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1525 GLenum mode
, const GLvoid
*indirect
)
1527 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1528 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1529 1 /* draw_count */ , 16 /* stride */ ,
1532 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1538 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1540 const GLvoid
*indirect
,
1541 GLsizei primcount
, GLsizei stride
)
1543 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1548 ctx
->Driver
.DrawIndirect(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1549 primcount
, stride
, NULL
, 0, NULL
);
1551 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1557 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1558 GLenum mode
, GLenum type
,
1559 const GLvoid
*indirect
)
1561 struct _mesa_index_buffer ib
;
1563 ib
.count
= 0; /* unknown */
1564 ib
.index_size
= sizeof_ib_type(type
);
1565 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1568 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1569 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1570 1 /* draw_count */ , 20 /* stride */ ,
1573 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1579 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1580 GLenum mode
, GLenum type
,
1581 const GLvoid
*indirect
,
1582 GLsizei primcount
, GLsizei stride
)
1584 struct _mesa_index_buffer ib
;
1585 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1590 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1592 ib
.count
= 0; /* unknown */
1593 ib
.index_size
= sizeof_ib_type(type
);
1594 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1597 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1598 ctx
->DrawIndirectBuffer
, offset
,
1599 primcount
, stride
, NULL
, 0, &ib
);
1601 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1607 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1610 static void GLAPIENTRY
1611 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1613 GET_CURRENT_CONTEXT(ctx
);
1615 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1616 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1617 _mesa_enum_to_string(mode
), indirect
);
1619 FLUSH_FOR_DRAW(ctx
);
1621 if (_mesa_is_no_error_enabled(ctx
)) {
1622 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1625 _mesa_update_state(ctx
);
1627 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1629 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1633 if (skip_validated_draw(ctx
))
1636 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1640 static void GLAPIENTRY
1641 vbo_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 FLUSH_FOR_DRAW(ctx
);
1652 if (_mesa_is_no_error_enabled(ctx
)) {
1653 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1656 _mesa_update_state(ctx
);
1658 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1660 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1664 if (skip_validated_draw(ctx
))
1667 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1671 static void GLAPIENTRY
1672 vbo_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1673 GLsizei primcount
, GLsizei stride
)
1675 GET_CURRENT_CONTEXT(ctx
);
1677 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1678 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1679 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1681 /* If <stride> is zero, the array elements are treated as tightly packed. */
1683 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1685 FLUSH_FOR_DRAW(ctx
);
1687 if (_mesa_is_no_error_enabled(ctx
)) {
1688 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1691 _mesa_update_state(ctx
);
1693 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1695 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1700 if (skip_validated_draw(ctx
))
1703 vbo_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1708 static void GLAPIENTRY
1709 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1710 const GLvoid
*indirect
,
1711 GLsizei primcount
, GLsizei stride
)
1713 GET_CURRENT_CONTEXT(ctx
);
1715 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1716 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1717 _mesa_enum_to_string(mode
),
1718 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1720 /* If <stride> is zero, the array elements are treated as tightly packed. */
1722 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1724 FLUSH_FOR_DRAW(ctx
);
1726 if (_mesa_is_no_error_enabled(ctx
)) {
1727 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1730 _mesa_update_state(ctx
);
1732 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1734 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1739 if (skip_validated_draw(ctx
))
1742 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1748 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1751 GLintptr drawcount_offset
,
1752 GLsizei maxdrawcount
,
1755 GLsizeiptr offset
= indirect
;
1757 if (maxdrawcount
== 0)
1760 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1761 ctx
->DrawIndirectBuffer
, offset
,
1762 maxdrawcount
, stride
,
1763 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1765 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1771 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1772 GLenum mode
, GLenum type
,
1774 GLintptr drawcount_offset
,
1775 GLsizei maxdrawcount
,
1778 struct _mesa_index_buffer ib
;
1779 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1781 if (maxdrawcount
== 0)
1784 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1786 ib
.count
= 0; /* unknown */
1787 ib
.index_size
= sizeof_ib_type(type
);
1788 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1791 ctx
->Driver
.DrawIndirect(ctx
, mode
,
1792 ctx
->DrawIndirectBuffer
, offset
,
1793 maxdrawcount
, stride
,
1794 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1796 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1801 static void GLAPIENTRY
1802 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1803 GLintptr drawcount_offset
,
1804 GLsizei maxdrawcount
, GLsizei stride
)
1806 GET_CURRENT_CONTEXT(ctx
);
1808 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1809 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1810 "(%s, %lx, %lx, %i, %i)\n",
1811 _mesa_enum_to_string(mode
),
1812 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1813 maxdrawcount
, stride
);
1815 /* If <stride> is zero, the array elements are treated as tightly packed. */
1817 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1819 FLUSH_FOR_DRAW(ctx
);
1821 if (_mesa_is_no_error_enabled(ctx
)) {
1822 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1825 _mesa_update_state(ctx
);
1827 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1829 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1832 maxdrawcount
, stride
))
1836 if (skip_validated_draw(ctx
))
1839 vbo_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1841 maxdrawcount
, stride
);
1845 static void GLAPIENTRY
1846 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1848 GLintptr drawcount_offset
,
1849 GLsizei maxdrawcount
, GLsizei stride
)
1851 GET_CURRENT_CONTEXT(ctx
);
1853 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1854 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1855 "(%s, %s, %lx, %lx, %i, %i)\n",
1856 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1857 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1858 maxdrawcount
, stride
);
1860 /* If <stride> is zero, the array elements are treated as tightly packed. */
1862 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1864 FLUSH_FOR_DRAW(ctx
);
1866 if (_mesa_is_no_error_enabled(ctx
)) {
1867 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1870 _mesa_update_state(ctx
);
1872 _mesa_set_draw_vao(ctx
, ctx
->Array
.VAO
, enabled_filter(ctx
));
1874 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1877 maxdrawcount
, stride
))
1881 if (skip_validated_draw(ctx
))
1884 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1885 drawcount_offset
, maxdrawcount
,
1891 * Initialize the dispatch table with the VBO functions for drawing.
1894 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1895 struct _glapi_table
*exec
)
1897 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1898 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1900 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1901 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1904 SET_MultiDrawArrays(exec
, vbo_exec_MultiDrawArrays
);
1905 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1907 if (ctx
->API
== API_OPENGL_COMPAT
) {
1908 SET_Rectf(exec
, vbo_exec_Rectf
);
1909 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1910 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1913 if (ctx
->API
!= API_OPENGLES
&&
1914 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1915 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1916 SET_MultiDrawElementsBaseVertex(exec
,
1917 vbo_exec_MultiDrawElementsBaseVertex
);
1919 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1920 SET_DrawRangeElementsBaseVertex(exec
,
1921 vbo_exec_DrawRangeElementsBaseVertex
);
1922 SET_DrawElementsInstancedBaseVertex(exec
,
1923 vbo_exec_DrawElementsInstancedBaseVertex
);
1927 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1928 SET_DrawArraysInstancedBaseInstance(exec
,
1929 vbo_exec_DrawArraysInstancedBaseInstance
);
1930 SET_DrawElementsInstancedBaseInstance(exec
,
1931 vbo_exec_DrawElementsInstancedBaseInstance
);
1932 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1933 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1936 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1937 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1938 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1941 if (ctx
->API
== API_OPENGL_CORE
) {
1942 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1943 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1944 SET_MultiDrawArraysIndirectCountARB(exec
,
1945 vbo_exec_MultiDrawArraysIndirectCount
);
1946 SET_MultiDrawElementsIndirectCountARB(exec
,
1947 vbo_exec_MultiDrawElementsIndirectCount
);
1950 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1951 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1952 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1955 if (_mesa_is_desktop_gl(ctx
)) {
1956 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1957 SET_DrawTransformFeedbackStream(exec
,
1958 vbo_exec_DrawTransformFeedbackStream
);
1959 SET_DrawTransformFeedbackInstanced(exec
,
1960 vbo_exec_DrawTransformFeedbackInstanced
);
1961 SET_DrawTransformFeedbackStreamInstanced(exec
,
1962 vbo_exec_DrawTransformFeedbackStreamInstanced
);
1969 * The following functions are only used for OpenGL ES 1/2 support.
1970 * And some aren't even supported (yet) in ES 1/2.
1975 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1977 vbo_exec_DrawArrays(mode
, first
, count
);
1982 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
1985 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
1990 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1991 const GLvoid
*indices
)
1993 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1998 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1999 const GLvoid
*indices
, GLint basevertex
)
2001 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
2006 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
2007 GLenum type
, const GLvoid
* indices
)
2009 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
2014 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
2015 GLsizei count
, GLenum type
,
2016 const GLvoid
*indices
, GLint basevertex
)
2018 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
2019 indices
, basevertex
);
2024 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
2025 const GLvoid
** indices
, GLsizei primcount
)
2027 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
2032 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
2033 const GLsizei
*count
, GLenum type
,
2034 const GLvoid
**indices
, GLsizei primcount
,
2035 const GLint
*basevertex
)
2037 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
2038 primcount
, basevertex
);
2043 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
2045 vbo_exec_DrawTransformFeedback(mode
, name
);