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/api_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"
42 #include "vbo_context.h"
46 * Check that element 'j' of the array has reasonable data.
48 * For debugging purposes; not normally used.
51 check_array_data(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
52 GLuint attrib
, GLuint j
)
54 const struct gl_vertex_attrib_array
*array
= &vao
->VertexAttrib
[attrib
];
56 const struct gl_vertex_buffer_binding
*binding
=
57 &vao
->VertexBinding
[array
->VertexBinding
];
58 struct gl_buffer_object
*bo
= binding
->BufferObj
;
59 const void *data
= array
->Ptr
;
60 if (_mesa_is_bufferobj(bo
)) {
61 if (!bo
->Mappings
[MAP_INTERNAL
].Pointer
) {
63 bo
->Mappings
[MAP_INTERNAL
].Pointer
=
64 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
,
68 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
69 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
71 switch (array
->Type
) {
74 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
76 for (k
= 0; k
< array
->Size
; k
++) {
77 if (IS_INF_OR_NAN(f
[k
]) ||
78 f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
79 printf("Bad array data:\n");
80 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
81 printf(" Array %u at %p\n", attrib
, (void* ) array
);
82 printf(" Type 0x%x, Size %d, Stride %d\n",
83 array
->Type
, array
->Size
, binding
->Stride
);
84 printf(" Address/offset %p in Buffer Object %u\n",
85 array
->Ptr
, bo
->Name
);
86 f
[k
] = 1.0F
; /* XXX replace the bad value! */
88 /*assert(!IS_INF_OR_NAN(f[k]));*/
100 * Unmap the buffer object referenced by given array, if mapped.
103 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
106 const struct gl_vertex_attrib_array
*array
= &vao
->VertexAttrib
[attrib
];
107 if (array
->Enabled
) {
108 const struct gl_vertex_buffer_binding
*binding
=
109 &vao
->VertexBinding
[array
->VertexBinding
];
110 struct gl_buffer_object
*bo
= binding
->BufferObj
;
111 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
112 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
119 * Examine the array's data for NaNs, etc.
120 * For debug purposes; not normally used.
123 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
124 const void *elements
, GLint basevertex
)
126 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
131 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
132 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
133 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
135 ctx
->Array
.VAO
->IndexBufferObj
,
137 elements
= ADD_POINTERS(elements
, elemMap
);
140 for (i
= 0; i
< count
; i
++) {
145 case GL_UNSIGNED_BYTE
:
146 j
= ((const GLubyte
*) elements
)[i
];
148 case GL_UNSIGNED_SHORT
:
149 j
= ((const GLushort
*) elements
)[i
];
151 case GL_UNSIGNED_INT
:
152 j
= ((const GLuint
*) elements
)[i
];
158 /* check element j of each enabled array */
159 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
160 check_array_data(ctx
, vao
, k
, j
);
164 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
165 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
169 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
170 unmap_array_buffer(ctx
, vao
, k
);
176 * Check array data, looking for NaNs, etc.
179 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
186 * Print info/data for glDrawArrays(), for debugging.
189 print_draw_arrays(struct gl_context
*ctx
,
190 GLenum mode
, GLint start
, GLsizei count
)
192 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
194 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
198 for (i
= 0; i
< VERT_ATTRIB_MAX
; ++i
) {
199 const struct gl_vertex_attrib_array
*array
= &vao
->VertexAttrib
[i
];
203 const struct gl_vertex_buffer_binding
*binding
=
204 &vao
->VertexBinding
[array
->VertexBinding
];
205 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
207 printf("attr %s: size %d stride %d enabled %d "
208 "ptr %p Bufobj %u\n",
209 gl_vert_attrib_name((gl_vert_attrib
)i
),
210 array
->Size
, binding
->Stride
, array
->Enabled
,
211 array
->Ptr
, bufObj
->Name
);
213 if (_mesa_is_bufferobj(bufObj
)) {
214 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
215 GL_MAP_READ_BIT
, bufObj
,
217 int offset
= (int) (GLintptr
)
218 _mesa_vertex_attrib_address(array
, binding
);
219 float *f
= (float *) (p
+ offset
);
222 int n
= (count
* binding
->Stride
) / 4;
225 printf(" Data at offset %d:\n", offset
);
226 for (i
= 0; i
< n
; i
++) {
227 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
229 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
236 * Set the vbo->exec->inputs[] pointers to point to the enabled
237 * vertex arrays. This depends on the current vertex program/shader
238 * being executed because of whether or not generic vertex arrays
239 * alias the conventional vertex arrays.
240 * For arrays that aren't enabled, we set the input[attrib] pointer
241 * to point at a zero-stride current value "array".
244 recalculate_input_bindings(struct gl_context
*ctx
)
246 struct vbo_context
*vbo
= vbo_context(ctx
);
247 struct vbo_exec_context
*exec
= &vbo
->exec
;
248 const struct gl_vertex_attrib_array
*array
= ctx
->Array
.VAO
->VertexAttrib
;
249 struct gl_client_array
*vertexAttrib
= ctx
->Array
.VAO
->_VertexAttrib
;
250 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
251 GLbitfield64 const_inputs
= 0x0;
254 switch (get_program_mode(ctx
)) {
256 /* When no vertex program is active (or the vertex program is generated
257 * from fixed-function state). We put the material values into the
258 * generic slots. This is the only situation where material values
259 * are available as per-vertex attributes.
261 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
262 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
263 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
265 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
266 const_inputs
|= VERT_BIT(i
);
270 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
271 inputs
[VERT_ATTRIB_GENERIC(i
)] =
272 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+i
];
273 const_inputs
|= VERT_BIT_GENERIC(i
);
276 /* Could use just about anything, just to fill in the empty
279 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
280 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
281 const_inputs
|= VERT_BIT_GENERIC(i
);
286 /* There are no shaders in OpenGL ES 1.x, so this code path should be
287 * impossible to reach. The meta code is careful to not use shaders in
290 assert(ctx
->API
!= API_OPENGLES
);
292 /* In the compatibility profile of desktop OpenGL, the generic[0]
293 * attribute array aliases and overrides the legacy position array.
294 * Otherwise, legacy attributes available in the legacy slots,
295 * generic attributes in the generic slots and materials are not
296 * available as per-vertex attributes.
298 * In all other APIs, only the generic attributes exist, and none of the
299 * slots are considered "magic."
301 if (ctx
->API
== API_OPENGL_COMPAT
) {
302 if (array
[VERT_ATTRIB_GENERIC0
].Enabled
)
303 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
304 else if (array
[VERT_ATTRIB_POS
].Enabled
)
305 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
307 inputs
[0] = &vbo
->currval
[VBO_ATTRIB_POS
];
308 const_inputs
|= VERT_BIT_POS
;
311 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
312 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
313 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
315 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
316 const_inputs
|= VERT_BIT_FF(i
);
320 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
321 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
322 inputs
[VERT_ATTRIB_GENERIC(i
)] =
323 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
325 inputs
[VERT_ATTRIB_GENERIC(i
)] =
326 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
327 const_inputs
|= VERT_BIT_GENERIC(i
);
331 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
333 /* Other parts of the code assume that inputs[0] through
334 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
335 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
338 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
339 assert(!array
[VERT_ATTRIB_FF(i
)].Enabled
);
341 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
342 const_inputs
|= VERT_BIT_FF(i
);
345 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
346 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
347 inputs
[VERT_ATTRIB_GENERIC(i
)] =
348 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
350 inputs
[VERT_ATTRIB_GENERIC(i
)] =
351 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
352 const_inputs
|= VERT_BIT_GENERIC(i
);
360 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
361 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
366 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
367 * These will point to the arrays to actually use for drawing. Some will
368 * be user-provided arrays, other will be zero-stride const-valued arrays.
369 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
370 * validation must be done after this call.
373 vbo_bind_arrays(struct gl_context
*ctx
)
375 struct vbo_context
*vbo
= vbo_context(ctx
);
376 struct vbo_exec_context
*exec
= &vbo
->exec
;
378 vbo_draw_method(vbo
, DRAW_ARRAYS
);
380 if (exec
->array
.recalculate_inputs
) {
381 recalculate_input_bindings(ctx
);
382 exec
->array
.recalculate_inputs
= GL_FALSE
;
384 /* Again... because we may have changed the bitmask of per-vertex varying
385 * attributes. If we regenerate the fixed-function vertex program now
386 * we may be able to prune down the number of vertex attributes which we
387 * need in the shader.
390 /* Setting "validating" to TRUE prevents _mesa_update_state from
391 * invalidating what we just did.
393 exec
->validating
= GL_TRUE
;
394 _mesa_update_state(ctx
);
395 exec
->validating
= GL_FALSE
;
399 if (!_mesa_all_buffers_are_unmapped(ctx
->Array
.VAO
)) {
400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
401 "draw call (vertex buffers are mapped)");
409 * Helper function called by the other DrawArrays() functions below.
410 * This is where we handle primitive restart for drawing non-indexed
411 * arrays. If primitive restart is enabled, it typically means
412 * splitting one DrawArrays() into two.
415 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
416 GLsizei count
, GLuint numInstances
, GLuint baseInstance
)
418 struct vbo_context
*vbo
= vbo_context(ctx
);
419 struct _mesa_prim prim
[2];
421 if (!vbo_bind_arrays(ctx
))
424 /* init most fields to zero */
425 memset(prim
, 0, sizeof(prim
));
429 prim
[0].num_instances
= numInstances
;
430 prim
[0].base_instance
= baseInstance
;
431 prim
[0].is_indirect
= 0;
433 /* Implement the primitive restart index */
434 if (ctx
->Array
.PrimitiveRestart
&& !ctx
->Array
.PrimitiveRestartFixedIndex
&&
435 ctx
->Array
.RestartIndex
< count
) {
436 GLuint primCount
= 0;
438 if (ctx
->Array
.RestartIndex
== start
) {
439 /* special case: RestartIndex at beginning */
441 prim
[0].start
= start
+ 1;
442 prim
[0].count
= count
- 1;
446 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
447 /* special case: RestartIndex at end */
449 prim
[0].start
= start
;
450 prim
[0].count
= count
- 1;
455 /* general case: RestartIndex in middle, split into two prims */
456 prim
[0].start
= start
;
457 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
460 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
461 prim
[1].count
= count
- prim
[1].start
;
467 /* draw one or two prims */
468 vbo
->draw_prims(ctx
, prim
, primCount
, NULL
,
469 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
473 /* no prim restart */
474 prim
[0].start
= start
;
475 prim
[0].count
= count
;
477 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
478 GL_TRUE
, start
, start
+ count
- 1,
482 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
489 * Execute a glRectf() function.
491 static void GLAPIENTRY
492 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
494 GET_CURRENT_CONTEXT(ctx
);
495 ASSERT_OUTSIDE_BEGIN_END(ctx
);
497 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
498 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
499 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
500 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
501 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
502 CALL_End(GET_DISPATCH(), ());
506 static void GLAPIENTRY
507 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
509 GET_CURRENT_CONTEXT(ctx
);
519 prim
= GL_LINE_STRIP
;
522 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)" );
526 /* No effect if vertex maps disabled.
528 if (!ctx
->Eval
.Map1Vertex4
&&
529 !ctx
->Eval
.Map1Vertex3
)
532 du
= ctx
->Eval
.MapGrid1du
;
533 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
535 CALL_Begin(GET_DISPATCH(), (prim
));
536 for (i
=i1
;i
<=i2
;i
++,u
+=du
) {
537 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
539 CALL_End(GET_DISPATCH(), ());
543 static void GLAPIENTRY
544 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
546 GET_CURRENT_CONTEXT(ctx
);
547 GLfloat u
, du
, v
, dv
, v1
, u1
;
556 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)" );
560 /* No effect if vertex maps disabled.
562 if (!ctx
->Eval
.Map2Vertex4
&&
563 !ctx
->Eval
.Map2Vertex3
)
566 du
= ctx
->Eval
.MapGrid2du
;
567 dv
= ctx
->Eval
.MapGrid2dv
;
568 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
569 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
573 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
574 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
575 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
576 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
579 CALL_End(GET_DISPATCH(), ());
582 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
583 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
584 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
585 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
587 CALL_End(GET_DISPATCH(), ());
589 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
590 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
591 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
592 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
594 CALL_End(GET_DISPATCH(), ());
598 for (v
=v1
,j
=j1
;j
<j2
;j
++,v
+=dv
) {
599 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
600 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
601 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
602 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+dv
));
604 CALL_End(GET_DISPATCH(), ());
612 * Called from glDrawArrays when in immediate mode (not display list mode).
614 static void GLAPIENTRY
615 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
617 GET_CURRENT_CONTEXT(ctx
);
619 if (MESA_VERBOSE
& VERBOSE_DRAW
)
620 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
621 _mesa_enum_to_string(mode
), start
, count
);
623 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
627 check_draw_arrays_data(ctx
, start
, count
);
629 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0);
632 print_draw_arrays(ctx
, mode
, start
, count
);
637 * Called from glDrawArraysInstanced when in immediate mode (not
638 * display list mode).
640 static void GLAPIENTRY
641 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
642 GLsizei numInstances
)
644 GET_CURRENT_CONTEXT(ctx
);
646 if (MESA_VERBOSE
& VERBOSE_DRAW
)
647 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
648 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
650 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
654 check_draw_arrays_data(ctx
, start
, count
);
656 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0);
659 print_draw_arrays(ctx
, mode
, start
, count
);
664 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
666 static void GLAPIENTRY
667 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
, GLsizei count
,
668 GLsizei numInstances
, GLuint baseInstance
)
670 GET_CURRENT_CONTEXT(ctx
);
672 if (MESA_VERBOSE
& VERBOSE_DRAW
)
673 _mesa_debug(ctx
, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
674 _mesa_enum_to_string(mode
), first
, count
,
675 numInstances
, baseInstance
);
677 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
682 check_draw_arrays_data(ctx
, first
, count
);
684 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
);
687 print_draw_arrays(ctx
, mode
, first
, count
);
693 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
698 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
701 ctx
->Driver
.MapBufferRange(ctx
, 0,
702 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
704 ctx
->Array
.VAO
->IndexBufferObj
,
707 case GL_UNSIGNED_BYTE
:
709 const GLubyte
*us
= (const GLubyte
*) map
;
711 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
712 printf("%02x ", us
[i
]);
719 case GL_UNSIGNED_SHORT
:
721 const GLushort
*us
= (const GLushort
*) map
;
723 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
724 printf("%04x ", us
[i
]);
731 case GL_UNSIGNED_INT
:
733 const GLuint
*us
= (const GLuint
*) map
;
735 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
736 printf("%08x ", us
[i
]);
747 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
754 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
755 * Do the rendering for a glDrawElements or glDrawRangeElements call after
756 * we've validated buffer bounds, etc.
759 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
760 GLboolean index_bounds_valid
,
761 GLuint start
, GLuint end
,
762 GLsizei count
, GLenum type
,
763 const GLvoid
*indices
,
764 GLint basevertex
, GLuint numInstances
,
767 struct vbo_context
*vbo
= vbo_context(ctx
);
768 struct _mesa_index_buffer ib
;
769 struct _mesa_prim prim
[1];
771 if (!vbo_bind_arrays(ctx
))
776 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
785 prim
[0].count
= count
;
787 prim
[0].is_indirect
= 0;
788 prim
[0].basevertex
= basevertex
;
789 prim
[0].num_instances
= numInstances
;
790 prim
[0].base_instance
= baseInstance
;
792 /* Need to give special consideration to rendering a range of
793 * indices starting somewhere above zero. Typically the
794 * application is issuing multiple DrawRangeElements() to draw
795 * successive primitives layed out linearly in the vertex arrays.
796 * Unless the vertex arrays are all in a VBO (or locked as with
797 * CVA), the OpenGL semantics imply that we need to re-read or
798 * re-upload the vertex data on each draw call.
800 * In the case of hardware tnl, we want to avoid starting the
801 * upload at zero, as it will mean every draw call uploads an
802 * increasing amount of not-used vertex data. Worse - in the
803 * software tnl module, all those vertices might be transformed and
804 * lit but never rendered.
806 * If we just upload or transform the vertices in start..end,
807 * however, the indices will be incorrect.
809 * At this level, we don't know exactly what the requirements of
810 * the backend are going to be, though it will likely boil down to
813 * 1) Do nothing, everything is in a VBO and is processed once
816 * 2) Adjust the indices and vertex arrays so that start becomes
819 * Rather than doing anything here, I'll provide a helper function
820 * for the latter case elsewhere.
823 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
824 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
826 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
833 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
835 static void GLAPIENTRY
836 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
837 GLuint start
, GLuint end
,
838 GLsizei count
, GLenum type
,
839 const GLvoid
*indices
,
842 static GLuint warnCount
= 0;
843 GLboolean index_bounds_valid
= GL_TRUE
;
845 /* This is only useful to catch invalid values in the "end" parameter
848 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
850 GET_CURRENT_CONTEXT(ctx
);
852 if (MESA_VERBOSE
& VERBOSE_DRAW
)
854 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
855 _mesa_enum_to_string(mode
), start
, end
, count
,
856 _mesa_enum_to_string(type
), indices
, basevertex
);
858 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
862 if ((int) end
+ basevertex
< 0 ||
863 start
+ basevertex
>= max_element
) {
864 /* The application requested we draw using a range of indices that's
865 * outside the bounds of the current VBO. This is invalid and appears
866 * to give undefined results. The safest thing to do is to simply
867 * ignore the range, in case the application botched their range tracking
868 * but did provide valid indices. Also issue a warning indicating that
869 * the application is broken.
871 if (warnCount
++ < 10) {
872 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
873 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
874 "\trange is outside VBO bounds (max=%u); ignoring.\n"
875 "\tThis should be fixed in the application.",
876 start
, end
, basevertex
, count
, type
, indices
,
879 index_bounds_valid
= GL_FALSE
;
882 /* NOTE: It's important that 'end' is a reasonable value.
883 * in _tnl_draw_prims(), we use end to determine how many vertices
884 * to transform. If it's too large, we can unnecessarily split prims
885 * or we can read/write out of memory in several different places!
888 /* Catch/fix some potential user errors */
889 if (type
== GL_UNSIGNED_BYTE
) {
890 start
= MIN2(start
, 0xff);
891 end
= MIN2(end
, 0xff);
893 else if (type
== GL_UNSIGNED_SHORT
) {
894 start
= MIN2(start
, 0xffff);
895 end
= MIN2(end
, 0xffff);
899 printf("glDraw[Range]Elements{,BaseVertex}"
900 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
902 start
, end
, type
, count
,
903 ctx
->Array
.VAO
->IndexBufferObj
->Name
,
907 if ((int) start
+ basevertex
< 0 ||
908 end
+ basevertex
>= max_element
)
909 index_bounds_valid
= GL_FALSE
;
912 check_draw_elements_data(ctx
, count
, type
, indices
);
914 (void) check_draw_elements_data
;
917 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
918 count
, type
, indices
, basevertex
, 1, 0);
923 * Called by glDrawRangeElements() in immediate mode.
925 static void GLAPIENTRY
926 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
927 GLsizei count
, GLenum type
, const GLvoid
*indices
)
929 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
930 GET_CURRENT_CONTEXT(ctx
);
932 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
933 _mesa_enum_to_string(mode
), start
, end
, count
,
934 _mesa_enum_to_string(type
), indices
);
937 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
943 * Called by glDrawElements() in immediate mode.
945 static void GLAPIENTRY
946 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
947 const GLvoid
*indices
)
949 GET_CURRENT_CONTEXT(ctx
);
951 if (MESA_VERBOSE
& VERBOSE_DRAW
)
952 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
953 _mesa_enum_to_string(mode
), count
,
954 _mesa_enum_to_string(type
), indices
);
956 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
959 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
960 count
, type
, indices
, 0, 1, 0);
965 * Called by glDrawElementsBaseVertex() in immediate mode.
967 static void GLAPIENTRY
968 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
969 const GLvoid
*indices
, GLint basevertex
)
971 GET_CURRENT_CONTEXT(ctx
);
973 if (MESA_VERBOSE
& VERBOSE_DRAW
)
974 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
975 _mesa_enum_to_string(mode
), count
,
976 _mesa_enum_to_string(type
), indices
, basevertex
);
978 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
981 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
982 count
, type
, indices
, basevertex
, 1, 0);
987 * Called by glDrawElementsInstanced() in immediate mode.
989 static void GLAPIENTRY
990 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
991 const GLvoid
*indices
, GLsizei numInstances
)
993 GET_CURRENT_CONTEXT(ctx
);
995 if (MESA_VERBOSE
& VERBOSE_DRAW
)
996 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
997 _mesa_enum_to_string(mode
), count
,
998 _mesa_enum_to_string(type
), indices
, numInstances
);
1000 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1004 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1005 count
, type
, indices
, 0, numInstances
, 0);
1010 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1012 static void GLAPIENTRY
1013 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1014 const GLvoid
*indices
, GLsizei numInstances
,
1017 GET_CURRENT_CONTEXT(ctx
);
1019 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1020 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1021 _mesa_enum_to_string(mode
), count
,
1022 _mesa_enum_to_string(type
), indices
,
1023 numInstances
, basevertex
);
1025 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1029 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1030 count
, type
, indices
, basevertex
, numInstances
, 0);
1035 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1037 static void GLAPIENTRY
1038 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1039 const GLvoid
*indices
, GLsizei numInstances
,
1040 GLuint baseInstance
)
1042 GET_CURRENT_CONTEXT(ctx
);
1044 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1045 _mesa_debug(ctx
, "glDrawElementsInstancedBaseInstance(%s, %d, %s, %p, %d, %d)\n",
1046 _mesa_enum_to_string(mode
), count
,
1047 _mesa_enum_to_string(type
), indices
,
1048 numInstances
, baseInstance
);
1050 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1054 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1055 count
, type
, indices
, 0, numInstances
,
1061 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1063 static void GLAPIENTRY
1064 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1065 const GLvoid
*indices
, GLsizei numInstances
,
1066 GLint basevertex
, GLuint baseInstance
)
1068 GET_CURRENT_CONTEXT(ctx
);
1070 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1071 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertexBaseInstance(%s, %d, %s, %p, %d, %d, %d)\n",
1072 _mesa_enum_to_string(mode
), count
,
1073 _mesa_enum_to_string(type
), indices
,
1074 numInstances
, basevertex
, baseInstance
);
1076 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1080 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1081 count
, type
, indices
, basevertex
, numInstances
,
1087 * Inner support for both _mesa_MultiDrawElements() and
1088 * _mesa_MultiDrawRangeElements().
1089 * This does the actual rendering after we've checked array indexes, etc.
1092 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1093 const GLsizei
*count
, GLenum type
,
1094 const GLvoid
* const *indices
,
1096 const GLint
*basevertex
)
1098 struct vbo_context
*vbo
= vbo_context(ctx
);
1099 struct _mesa_index_buffer ib
;
1100 struct _mesa_prim
*prim
;
1101 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1102 uintptr_t min_index_ptr
, max_index_ptr
;
1103 GLboolean fallback
= GL_FALSE
;
1109 prim
= calloc(primcount
, sizeof(*prim
));
1111 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1115 if (!vbo_bind_arrays(ctx
)) {
1120 min_index_ptr
= (uintptr_t)indices
[0];
1122 for (i
= 0; i
< primcount
; i
++) {
1123 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1124 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1125 index_type_size
* count
[i
]);
1128 /* Check if we can handle this thing as a bunch of index offsets from the
1129 * same index pointer. If we can't, then we have to fall back to doing
1130 * a draw_prims per primitive.
1131 * Check that the difference between each prim's indexes is a multiple of
1132 * the index/element size.
1134 if (index_type_size
!= 1) {
1135 for (i
= 0; i
< primcount
; i
++) {
1136 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1143 /* Draw primitives individually if one count is zero, so we can easily skip
1146 for (i
= 0; i
< primcount
; i
++) {
1147 if (count
[i
] == 0) {
1153 /* If the index buffer isn't in a VBO, then treating the application's
1154 * subranges of the index buffer as one large index buffer may lead to
1155 * us reading unmapped memory.
1157 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1161 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1163 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1164 ib
.ptr
= (void *)min_index_ptr
;
1166 for (i
= 0; i
< primcount
; i
++) {
1167 prim
[i
].begin
= (i
== 0);
1168 prim
[i
].end
= (i
== primcount
- 1);
1171 prim
[i
].mode
= mode
;
1172 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1173 prim
[i
].count
= count
[i
];
1174 prim
[i
].indexed
= 1;
1175 prim
[i
].num_instances
= 1;
1176 prim
[i
].base_instance
= 0;
1177 prim
[i
].draw_id
= i
;
1178 prim
[i
].is_indirect
= 0;
1179 if (basevertex
!= NULL
)
1180 prim
[i
].basevertex
= basevertex
[i
];
1182 prim
[i
].basevertex
= 0;
1185 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1186 false, ~0, ~0, NULL
, 0, NULL
);
1188 /* render one prim at a time */
1189 for (i
= 0; i
< primcount
; i
++) {
1192 ib
.count
= count
[i
];
1194 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1195 ib
.ptr
= indices
[i
];
1201 prim
[0].mode
= mode
;
1203 prim
[0].count
= count
[i
];
1204 prim
[0].indexed
= 1;
1205 prim
[0].num_instances
= 1;
1206 prim
[0].base_instance
= 0;
1207 prim
[0].draw_id
= i
;
1208 prim
[0].is_indirect
= 0;
1209 if (basevertex
!= NULL
)
1210 prim
[0].basevertex
= basevertex
[i
];
1212 prim
[0].basevertex
= 0;
1214 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
1215 false, ~0, ~0, NULL
, 0, NULL
);
1221 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1227 static void GLAPIENTRY
1228 vbo_exec_MultiDrawElements(GLenum mode
,
1229 const GLsizei
*count
, GLenum type
,
1230 const GLvoid
* const *indices
,
1233 GET_CURRENT_CONTEXT(ctx
);
1235 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1239 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1244 static void GLAPIENTRY
1245 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1246 const GLsizei
*count
, GLenum type
,
1247 const GLvoid
* const *indices
,
1249 const GLsizei
*basevertex
)
1251 GET_CURRENT_CONTEXT(ctx
);
1253 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1257 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1262 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1263 struct gl_transform_feedback_object
*obj
,
1264 GLuint stream
, GLuint numInstances
)
1266 struct vbo_context
*vbo
= vbo_context(ctx
);
1267 struct _mesa_prim prim
[2];
1269 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1274 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1275 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1276 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1277 GLsizei n
= ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1278 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0);
1282 if (!vbo_bind_arrays(ctx
))
1285 /* init most fields to zero */
1286 memset(prim
, 0, sizeof(prim
));
1289 prim
[0].mode
= mode
;
1290 prim
[0].num_instances
= numInstances
;
1291 prim
[0].base_instance
= 0;
1292 prim
[0].is_indirect
= 0;
1294 /* Maybe we should do some primitive splitting for primitive restart
1295 * (like in DrawArrays), but we have no way to know how many vertices
1296 * will be rendered. */
1298 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
1299 GL_FALSE
, ~0, ~0, obj
, stream
, NULL
);
1301 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1307 * Like DrawArrays, but take the count from a transform feedback object.
1308 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1309 * \param name the transform feedback object
1310 * User still has to setup of the vertex attribute info with
1311 * glVertexPointer, glColorPointer, etc.
1312 * Part of GL_ARB_transform_feedback2.
1314 static void GLAPIENTRY
1315 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1317 GET_CURRENT_CONTEXT(ctx
);
1318 struct gl_transform_feedback_object
*obj
=
1319 _mesa_lookup_transform_feedback_object(ctx
, name
);
1321 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1322 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1323 _mesa_enum_to_string(mode
), name
);
1325 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1328 static void GLAPIENTRY
1329 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1331 GET_CURRENT_CONTEXT(ctx
);
1332 struct gl_transform_feedback_object
*obj
=
1333 _mesa_lookup_transform_feedback_object(ctx
, name
);
1335 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1336 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1337 _mesa_enum_to_string(mode
), name
, stream
);
1339 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1342 static void GLAPIENTRY
1343 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1346 GET_CURRENT_CONTEXT(ctx
);
1347 struct gl_transform_feedback_object
*obj
=
1348 _mesa_lookup_transform_feedback_object(ctx
, name
);
1350 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1351 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1352 _mesa_enum_to_string(mode
), name
);
1354 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1357 static void GLAPIENTRY
1358 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1359 GLuint stream
, GLsizei primcount
)
1361 GET_CURRENT_CONTEXT(ctx
);
1362 struct gl_transform_feedback_object
*obj
=
1363 _mesa_lookup_transform_feedback_object(ctx
, name
);
1365 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1366 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1367 "(%s, %u, %u, %i)\n",
1368 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1370 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1374 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1375 GLenum mode
, const GLvoid
*indirect
)
1377 struct vbo_context
*vbo
= vbo_context(ctx
);
1379 if (!vbo_bind_arrays(ctx
))
1382 vbo
->draw_indirect_prims(ctx
, mode
,
1383 ctx
->DrawIndirectBuffer
, (GLsizeiptr
)indirect
,
1384 1 /* draw_count */, 16 /* stride */,
1387 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1392 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1394 const GLvoid
*indirect
,
1395 GLsizei primcount
, GLsizei stride
)
1397 struct vbo_context
*vbo
= vbo_context(ctx
);
1398 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1403 if (!vbo_bind_arrays(ctx
))
1406 vbo
->draw_indirect_prims(ctx
, mode
,
1407 ctx
->DrawIndirectBuffer
, offset
,
1411 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1416 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1417 GLenum mode
, GLenum type
,
1418 const GLvoid
*indirect
)
1420 struct vbo_context
*vbo
= vbo_context(ctx
);
1421 struct _mesa_index_buffer ib
;
1423 if (!vbo_bind_arrays(ctx
))
1426 ib
.count
= 0; /* unknown */
1428 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1431 vbo
->draw_indirect_prims(ctx
, mode
,
1432 ctx
->DrawIndirectBuffer
, (GLsizeiptr
)indirect
,
1433 1 /* draw_count */, 20 /* stride */,
1437 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1442 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1443 GLenum mode
, GLenum type
,
1444 const GLvoid
*indirect
,
1445 GLsizei primcount
, GLsizei stride
)
1447 struct vbo_context
*vbo
= vbo_context(ctx
);
1448 struct _mesa_index_buffer ib
;
1449 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1454 if (!vbo_bind_arrays(ctx
))
1457 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1459 ib
.count
= 0; /* unknown */
1461 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1464 vbo
->draw_indirect_prims(ctx
, mode
,
1465 ctx
->DrawIndirectBuffer
, offset
,
1470 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1475 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1478 static void GLAPIENTRY
1479 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1481 GET_CURRENT_CONTEXT(ctx
);
1483 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1484 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1485 _mesa_enum_to_string(mode
), indirect
);
1487 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1490 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1493 static void GLAPIENTRY
1494 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
,
1495 const GLvoid
*indirect
)
1497 GET_CURRENT_CONTEXT(ctx
);
1499 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1500 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1501 _mesa_enum_to_string(mode
),
1502 _mesa_enum_to_string(type
), indirect
);
1504 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1507 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1510 static void GLAPIENTRY
1511 vbo_exec_MultiDrawArraysIndirect(GLenum mode
,
1512 const GLvoid
*indirect
,
1513 GLsizei primcount
, GLsizei stride
)
1515 GET_CURRENT_CONTEXT(ctx
);
1517 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1518 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1519 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1521 /* If <stride> is zero, the array elements are treated as tightly packed. */
1523 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1525 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
,
1530 vbo_validated_multidrawarraysindirect(ctx
, mode
,
1535 static void GLAPIENTRY
1536 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1537 const GLvoid
*indirect
,
1538 GLsizei primcount
, GLsizei stride
)
1540 GET_CURRENT_CONTEXT(ctx
);
1542 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1543 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1544 _mesa_enum_to_string(mode
),
1545 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1547 /* If <stride> is zero, the array elements are treated as tightly packed. */
1549 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1551 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
,
1556 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
,
1562 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1566 GLsizei maxdrawcount
,
1569 struct vbo_context
*vbo
= vbo_context(ctx
);
1570 GLsizeiptr offset
= indirect
;
1572 if (maxdrawcount
== 0)
1575 if (!vbo_bind_arrays(ctx
))
1578 vbo
->draw_indirect_prims(ctx
, mode
,
1579 ctx
->DrawIndirectBuffer
, offset
,
1580 maxdrawcount
, stride
,
1581 ctx
->ParameterBuffer
, drawcount
,
1584 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1589 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1590 GLenum mode
, GLenum type
,
1593 GLsizei maxdrawcount
,
1596 struct vbo_context
*vbo
= vbo_context(ctx
);
1597 struct _mesa_index_buffer ib
;
1598 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1600 if (maxdrawcount
== 0)
1603 if (!vbo_bind_arrays(ctx
))
1606 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1608 ib
.count
= 0; /* unknown */
1610 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1613 vbo
->draw_indirect_prims(ctx
, mode
,
1614 ctx
->DrawIndirectBuffer
, offset
,
1615 maxdrawcount
, stride
,
1616 ctx
->ParameterBuffer
, drawcount
,
1619 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1623 static void GLAPIENTRY
1624 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
,
1627 GLsizei maxdrawcount
, GLsizei stride
)
1629 GET_CURRENT_CONTEXT(ctx
);
1631 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1632 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1633 "(%s, %lx, %lx, %i, %i)\n",
1634 _mesa_enum_to_string(mode
), indirect
,
1635 drawcount
, maxdrawcount
, stride
);
1637 /* If <stride> is zero, the array elements are treated as tightly packed. */
1639 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1641 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1642 indirect
, drawcount
,
1643 maxdrawcount
, stride
))
1646 vbo_validated_multidrawarraysindirectcount(ctx
, mode
,
1647 indirect
, drawcount
,
1648 maxdrawcount
, stride
);
1651 static void GLAPIENTRY
1652 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1655 GLsizei maxdrawcount
, GLsizei stride
)
1657 GET_CURRENT_CONTEXT(ctx
);
1659 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1660 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1661 "(%s, %s, %lx, %lx, %i, %i)\n",
1662 _mesa_enum_to_string(mode
),
1663 _mesa_enum_to_string(type
), indirect
,
1664 drawcount
, maxdrawcount
, stride
);
1666 /* If <stride> is zero, the array elements are treated as tightly packed. */
1668 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1670 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1671 indirect
, drawcount
,
1672 maxdrawcount
, stride
))
1675 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
,
1676 indirect
, drawcount
,
1677 maxdrawcount
, stride
);
1682 * Initialize the dispatch table with the VBO functions for drawing.
1685 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1686 struct _glapi_table
*exec
)
1688 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1689 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1691 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1692 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1695 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1697 if (ctx
->API
== API_OPENGL_COMPAT
) {
1698 SET_Rectf(exec
, vbo_exec_Rectf
);
1699 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1700 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1703 if (ctx
->API
!= API_OPENGLES
&&
1704 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1705 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1706 SET_MultiDrawElementsBaseVertex(exec
, vbo_exec_MultiDrawElementsBaseVertex
);
1708 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1709 SET_DrawRangeElementsBaseVertex(exec
, vbo_exec_DrawRangeElementsBaseVertex
);
1710 SET_DrawElementsInstancedBaseVertex(exec
, vbo_exec_DrawElementsInstancedBaseVertex
);
1714 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1715 SET_DrawArraysInstancedBaseInstance(exec
, vbo_exec_DrawArraysInstancedBaseInstance
);
1716 SET_DrawElementsInstancedBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseInstance
);
1717 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1720 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1721 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1722 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1725 if (ctx
->API
== API_OPENGL_CORE
) {
1726 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1727 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1728 SET_MultiDrawArraysIndirectCountARB(exec
, vbo_exec_MultiDrawArraysIndirectCount
);
1729 SET_MultiDrawElementsIndirectCountARB(exec
, vbo_exec_MultiDrawElementsIndirectCount
);
1732 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1733 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1734 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1737 if (_mesa_is_desktop_gl(ctx
)) {
1738 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1739 SET_DrawTransformFeedbackStream(exec
, vbo_exec_DrawTransformFeedbackStream
);
1740 SET_DrawTransformFeedbackInstanced(exec
, vbo_exec_DrawTransformFeedbackInstanced
);
1741 SET_DrawTransformFeedbackStreamInstanced(exec
, vbo_exec_DrawTransformFeedbackStreamInstanced
);
1748 * The following functions are only used for OpenGL ES 1/2 support.
1749 * And some aren't even supported (yet) in ES 1/2.
1754 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1756 vbo_exec_DrawArrays(mode
, first
, count
);
1760 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
1763 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
1767 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1768 const GLvoid
*indices
)
1770 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1775 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1776 const GLvoid
*indices
, GLint basevertex
)
1778 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1783 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1784 GLenum type
, const GLvoid
*indices
)
1786 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1791 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1792 GLsizei count
, GLenum type
,
1793 const GLvoid
*indices
, GLint basevertex
)
1795 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1796 indices
, basevertex
);
1801 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1802 const GLvoid
**indices
, GLsizei primcount
)
1804 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1809 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1810 const GLsizei
*count
, GLenum type
,
1811 const GLvoid
**indices
, GLsizei primcount
,
1812 const GLint
*basevertex
)
1814 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1815 primcount
, basevertex
);
1819 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1821 vbo_exec_DrawTransformFeedback(mode
, name
);